org.el 941 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  85. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  86. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  87. ;;
  88. ;; In Org buffers, the value of `outline-regexp' is that of
  89. ;; `org-outline-regexp'. The only function still directly relying on
  90. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  91. ;; job when `orgstruct-mode' is active.
  92. (defvar org-outline-regexp "\\*+ "
  93. "Regexp to match Org headlines.")
  94. (defvar org-outline-regexp-bol "^\\*+ "
  95. "Regexp to match Org headlines.
  96. This is similar to `org-outline-regexp' but additionally makes
  97. sure that we are at the beginning of the line.")
  98. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  99. "Matches a headline, putting stars and text into groups.
  100. Stars are put in group 1 and the trimmed body in group 2.")
  101. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  102. (unless (boundp 'calendar-view-holidays-initially-flag)
  103. (org-defvaralias 'calendar-view-holidays-initially-flag
  104. 'view-calendar-holidays-initially))
  105. (unless (boundp 'calendar-view-diary-initially-flag)
  106. (org-defvaralias 'calendar-view-diary-initially-flag
  107. 'view-diary-entries-initially))
  108. (unless (boundp 'diary-fancy-buffer)
  109. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  112. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  113. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  114. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  115. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  116. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  117. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  118. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  119. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  120. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  121. (declare-function org-clock-update-time-maybe "org-clock" ())
  122. (declare-function org-clocktable-shift "org-clock" (dir n))
  123. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  124. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  125. (declare-function orgtbl-mode "org-table" (&optional arg))
  126. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  127. (declare-function org-beamer-mode "ox-beamer" ())
  128. (declare-function org-table-blank-field "org-table" ())
  129. (declare-function org-table-edit-field "org-table" (arg))
  130. (declare-function org-table-insert-row "org-table" (&optional arg))
  131. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  132. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  133. (declare-function org-id-get-create "org-id" (&optional force))
  134. (declare-function org-add-archive-files "org-archive" (files))
  135. (declare-function org-id-find-id-file "org-id" (id))
  136. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  137. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  138. (declare-function org-agenda-redo "org-agenda" (&optional all))
  139. (declare-function org-table-align "org-table" ())
  140. (declare-function org-table-begin "org-table" (&optional table-type))
  141. (declare-function org-table-blank-field "org-table" ())
  142. (declare-function org-table-end "org-table" (&optional table-type))
  143. (declare-function org-table-end-of-field "org-table" (&optional n))
  144. (declare-function org-table-insert-row "org-table" (&optional arg))
  145. (declare-function org-table-paste-rectangle "org-table" ())
  146. (declare-function org-table-maybe-eval-formula "org-table" ())
  147. (declare-function org-table-maybe-recalculate-line "org-table" ())
  148. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  149. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  150. (declare-function org-element-at-point "org-element" ())
  151. (declare-function org-element-cache-reset "org-element" (&optional all))
  152. (declare-function org-element-cache-refresh "org-element" (pos))
  153. (declare-function org-element-contents "org-element" (element))
  154. (declare-function org-element-context "org-element" (&optional element))
  155. (declare-function org-element-interpret-data "org-element"
  156. (data &optional parent))
  157. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  158. (declare-function org-element-parse-buffer "org-element"
  159. (&optional granularity visible-only))
  160. (declare-function org-element-property "org-element" (property element))
  161. (declare-function org-element-put-property "org-element"
  162. (element property value))
  163. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  164. (declare-function org-element-parse-buffer "org-element"
  165. (&optional granularity visible-only))
  166. (declare-function org-element-type "org-element" (element))
  167. (defsubst org-uniquify (list)
  168. "Non-destructively remove duplicate elements from LIST."
  169. (let ((res (copy-sequence list))) (delete-dups res)))
  170. (defsubst org-get-at-bol (property)
  171. "Get text property PROPERTY at the beginning of line."
  172. (get-text-property (point-at-bol) property))
  173. (defsubst org-trim (s)
  174. "Remove whitespace at the beginning and the end of string S."
  175. (replace-regexp-in-string
  176. "\\`[ \t\n\r]+" ""
  177. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  178. ;; load languages based on value of `org-babel-load-languages'
  179. (defvar org-babel-load-languages)
  180. ;;;###autoload
  181. (defun org-babel-do-load-languages (sym value)
  182. "Load the languages defined in `org-babel-load-languages'."
  183. (set-default sym value)
  184. (mapc (lambda (pair)
  185. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  186. (if active
  187. (progn
  188. (require (intern (concat "ob-" lang))))
  189. (progn
  190. (funcall 'fmakunbound
  191. (intern (concat "org-babel-execute:" lang)))
  192. (funcall 'fmakunbound
  193. (intern (concat "org-babel-expand-body:" lang)))))))
  194. org-babel-load-languages))
  195. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  196. ;;;###autoload
  197. (defun org-babel-load-file (file &optional compile)
  198. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  199. This function exports the source code using `org-babel-tangle'
  200. and then loads the resulting file using `load-file'. With prefix
  201. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  202. file to byte-code before it is loaded."
  203. (interactive "fFile to load: \nP")
  204. (let* ((age (lambda (file)
  205. (float-time
  206. (time-subtract (current-time)
  207. (nth 5 (or (file-attributes (file-truename file))
  208. (file-attributes file)))))))
  209. (base-name (file-name-sans-extension file))
  210. (exported-file (concat base-name ".el")))
  211. ;; tangle if the org-mode file is newer than the elisp file
  212. (unless (and (file-exists-p exported-file)
  213. (> (funcall age file) (funcall age exported-file)))
  214. (setq exported-file
  215. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  216. (message "%s %s"
  217. (if compile
  218. (progn (byte-compile-file exported-file 'load)
  219. "Compiled and loaded")
  220. (progn (load-file exported-file) "Loaded"))
  221. exported-file)))
  222. (defcustom org-babel-load-languages '((emacs-lisp . t))
  223. "Languages which can be evaluated in Org-mode buffers.
  224. This list can be used to load support for any of the languages
  225. below, note that each language will depend on a different set of
  226. system executables and/or Emacs modes. When a language is
  227. \"loaded\", then code blocks in that language can be evaluated
  228. with `org-babel-execute-src-block' bound by default to C-c
  229. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  230. be set to remove code block evaluation from the C-c C-c
  231. keybinding. By default only Emacs Lisp (which has no
  232. requirements) is loaded."
  233. :group 'org-babel
  234. :set 'org-babel-do-load-languages
  235. :version "24.1"
  236. :type '(alist :tag "Babel Languages"
  237. :key-type
  238. (choice
  239. (const :tag "Awk" awk)
  240. (const :tag "C" C)
  241. (const :tag "R" R)
  242. (const :tag "Asymptote" asymptote)
  243. (const :tag "Calc" calc)
  244. (const :tag "Clojure" clojure)
  245. (const :tag "CSS" css)
  246. (const :tag "Ditaa" ditaa)
  247. (const :tag "Dot" dot)
  248. (const :tag "Emacs Lisp" emacs-lisp)
  249. (const :tag "Forth" forth)
  250. (const :tag "Fortran" fortran)
  251. (const :tag "Gnuplot" gnuplot)
  252. (const :tag "Haskell" haskell)
  253. (const :tag "IO" io)
  254. (const :tag "J" J)
  255. (const :tag "Java" java)
  256. (const :tag "Javascript" js)
  257. (const :tag "LaTeX" latex)
  258. (const :tag "Ledger" ledger)
  259. (const :tag "Lilypond" lilypond)
  260. (const :tag "Lisp" lisp)
  261. (const :tag "Makefile" makefile)
  262. (const :tag "Maxima" maxima)
  263. (const :tag "Matlab" matlab)
  264. (const :tag "Mscgen" mscgen)
  265. (const :tag "Ocaml" ocaml)
  266. (const :tag "Octave" octave)
  267. (const :tag "Org" org)
  268. (const :tag "Perl" perl)
  269. (const :tag "Pico Lisp" picolisp)
  270. (const :tag "PlantUML" plantuml)
  271. (const :tag "Python" python)
  272. (const :tag "Ruby" ruby)
  273. (const :tag "Sass" sass)
  274. (const :tag "Scala" scala)
  275. (const :tag "Scheme" scheme)
  276. (const :tag "Screen" screen)
  277. (const :tag "Shell Script" shell)
  278. (const :tag "Shen" shen)
  279. (const :tag "Sql" sql)
  280. (const :tag "Sqlite" sqlite)
  281. (const :tag "ebnf2ps" ebnf2ps))
  282. :value-type (boolean :tag "Activate" :value t)))
  283. ;;;; Customization variables
  284. (defcustom org-clone-delete-id nil
  285. "Remove ID property of clones of a subtree.
  286. When non-nil, clones of a subtree don't inherit the ID property.
  287. Otherwise they inherit the ID property with a new unique
  288. identifier."
  289. :type 'boolean
  290. :version "24.1"
  291. :group 'org-id)
  292. ;;; Version
  293. (org-check-version)
  294. ;;;###autoload
  295. (defun org-version (&optional here full message)
  296. "Show the org-mode version.
  297. Interactively, or when MESSAGE is non-nil, show it in echo area.
  298. With prefix argument, or when HERE is non-nil, insert it at point.
  299. In non-interactive uses, a reduced version string is output unless
  300. FULL is given."
  301. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  302. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  303. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  304. (load-suffixes (list ".el"))
  305. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  306. (org-trash (or
  307. (and (fboundp 'org-release) (fboundp 'org-git-version))
  308. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  309. (load-suffixes save-load-suffixes)
  310. (org-version (org-release))
  311. (git-version (org-git-version))
  312. (version (format "Org-mode version %s (%s @ %s)"
  313. org-version
  314. git-version
  315. (if org-install-dir
  316. (if (string= org-dir org-install-dir)
  317. org-install-dir
  318. (concat "mixed installation! " org-install-dir " and " org-dir))
  319. "org-loaddefs.el can not be found!")))
  320. (version1 (if full version org-version)))
  321. (when here (insert version1))
  322. (when message (message "%s" version1))
  323. version1))
  324. (defconst org-version (org-version))
  325. ;;; Syntax Constants
  326. ;;;; Block
  327. (defconst org-block-regexp
  328. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  329. "Regular expression for hiding blocks.")
  330. (defconst org-dblock-start-re
  331. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  332. "Matches the start line of a dynamic block, with parameters.")
  333. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  334. "Matches the end of a dynamic block.")
  335. ;;;; Clock and Planning
  336. (defconst org-clock-string "CLOCK:"
  337. "String used as prefix for timestamps clocking work hours on an item.")
  338. (defconst org-closed-string "CLOSED:"
  339. "String used as the prefix for timestamps logging closing a TODO entry.")
  340. (defconst org-deadline-string "DEADLINE:"
  341. "String to mark deadline entries.
  342. A deadline is this string, followed by a time stamp. Should be a word,
  343. terminated by a colon. You can insert a schedule keyword and
  344. a timestamp with \\[org-deadline].")
  345. (defconst org-scheduled-string "SCHEDULED:"
  346. "String to mark scheduled TODO entries.
  347. A schedule is this string, followed by a time stamp. Should be a word,
  348. terminated by a colon. You can insert a schedule keyword and
  349. a timestamp with \\[org-schedule].")
  350. (defconst org-ds-keyword-length
  351. (+ 2
  352. (apply #'max
  353. (mapcar #'length
  354. (list org-deadline-string org-scheduled-string
  355. org-clock-string org-closed-string))))
  356. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  357. (defconst org-planning-line-re
  358. (concat "^[ \t]*"
  359. (regexp-opt
  360. (list org-closed-string org-deadline-string org-scheduled-string)
  361. t))
  362. "Matches a line with planning info.
  363. Matched keyword is in group 1.")
  364. (defconst org-clock-line-re
  365. (concat "^[ \t]*" org-clock-string)
  366. "Matches a line with clock info.")
  367. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  368. "Matches the DEADLINE keyword.")
  369. (defconst org-deadline-time-regexp
  370. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  371. "Matches the DEADLINE keyword together with a time stamp.")
  372. (defconst org-deadline-time-hour-regexp
  373. (concat "\\<" org-deadline-string
  374. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  375. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  376. (defconst org-deadline-line-regexp
  377. (concat "\\<\\(" org-deadline-string "\\).*")
  378. "Matches the DEADLINE keyword and the rest of the line.")
  379. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  380. "Matches the SCHEDULED keyword.")
  381. (defconst org-scheduled-time-regexp
  382. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  383. "Matches the SCHEDULED keyword together with a time stamp.")
  384. (defconst org-scheduled-time-hour-regexp
  385. (concat "\\<" org-scheduled-string
  386. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  387. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  388. (defconst org-closed-time-regexp
  389. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  390. "Matches the CLOSED keyword together with a time stamp.")
  391. (defconst org-keyword-time-regexp
  392. (concat "\\<"
  393. (regexp-opt
  394. (list org-scheduled-string org-deadline-string org-closed-string
  395. org-clock-string)
  396. t)
  397. " *[[<]\\([^]>]+\\)[]>]")
  398. "Matches any of the 4 keywords, together with the time stamp.")
  399. (defconst org-keyword-time-not-clock-regexp
  400. (concat
  401. "\\<"
  402. (regexp-opt
  403. (list org-scheduled-string org-deadline-string org-closed-string) t)
  404. " *[[<]\\([^]>]+\\)[]>]")
  405. "Matches any of the 3 keywords, together with the time stamp.")
  406. (defconst org-maybe-keyword-time-regexp
  407. (concat "\\(\\<"
  408. (regexp-opt
  409. (list org-scheduled-string org-deadline-string org-closed-string
  410. org-clock-string)
  411. t)
  412. "\\)?"
  413. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  414. "\\|"
  415. "<%%([^\r\n>]*>\\)")
  416. "Matches a timestamp, possibly preceded by a keyword.")
  417. (defconst org-all-time-keywords
  418. (mapcar (lambda (w) (substring w 0 -1))
  419. (list org-scheduled-string org-deadline-string
  420. org-clock-string org-closed-string))
  421. "List of time keywords.")
  422. ;;;; Drawer
  423. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  424. "Matches first or last line of a hidden block.
  425. Group 1 contains drawer's name or \"END\".")
  426. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  427. "Regular expression matching the first line of a property drawer.")
  428. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  429. "Regular expression matching the last line of a property drawer.")
  430. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  431. "Regular expression matching the first line of a clock drawer.")
  432. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  433. "Regular expression matching the last line of a clock drawer.")
  434. (defconst org-property-drawer-re
  435. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  436. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  437. "[ \t]*:END:[ \t]*$")
  438. "Matches an entire property drawer.")
  439. (defconst org-clock-drawer-re
  440. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  441. org-clock-drawer-end-re "\\)\n?")
  442. "Matches an entire clock drawer.")
  443. ;;;; Headline
  444. (defconst org-heading-keyword-regexp-format
  445. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  446. "Printf format for a regexp matching a headline with some keyword.
  447. This regexp will match the headline of any node which has the
  448. exact keyword that is put into the format. The keyword isn't in
  449. any group by default, but the stars and the body are.")
  450. (defconst org-heading-keyword-maybe-regexp-format
  451. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  452. "Printf format for a regexp matching a headline, possibly with some keyword.
  453. This regexp can match any headline with the specified keyword, or
  454. without a keyword. The keyword isn't in any group by default,
  455. but the stars and the body are.")
  456. (defconst org-archive-tag "ARCHIVE"
  457. "The tag that marks a subtree as archived.
  458. An archived subtree does not open during visibility cycling, and does
  459. not contribute to the agenda listings.")
  460. (defconst org-comment-string "COMMENT"
  461. "Entries starting with this keyword will never be exported.
  462. An entry can be toggled between COMMENT and normal with
  463. \\[org-toggle-comment].")
  464. ;;;; LaTeX Environments and Fragments
  465. (defconst org-latex-regexps
  466. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  467. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  468. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  469. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  470. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  471. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  472. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  473. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  474. "Regular expressions for matching embedded LaTeX.")
  475. ;;;; Node Property
  476. (defconst org-effort-property "Effort"
  477. "The property that is being used to keep track of effort estimates.
  478. Effort estimates given in this property need to have the format H:MM.")
  479. ;;;; Table
  480. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  481. "Detect an org-type or table-type table.")
  482. (defconst org-table-line-regexp "^[ \t]*|"
  483. "Detect an org-type table line.")
  484. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  485. "Detect an org-type table line.")
  486. (defconst org-table-hline-regexp "^[ \t]*|-"
  487. "Detect an org-type table hline.")
  488. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  489. "Detect a table-type table hline.")
  490. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  491. "Detect the first line outside a table when searching from within it.
  492. This works for both table types.")
  493. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  494. "Detect a #+TBLFM line.")
  495. ;;;; Timestamp
  496. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  497. "Regular expression for fast time stamp matching.")
  498. (defconst org-ts-regexp-inactive
  499. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  500. "Regular expression for fast inactive time stamp matching.")
  501. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  502. "Regular expression for fast time stamp matching.")
  503. (defconst org-ts-regexp0
  504. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  505. "Regular expression matching time strings for analysis.
  506. This one does not require the space after the date, so it can be used
  507. on a string that terminates immediately after the date.")
  508. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  509. "Regular expression matching time strings for analysis.")
  510. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  511. "Regular expression matching time stamps, with groups.")
  512. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  513. "Regular expression matching time stamps (also [..]), with groups.")
  514. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  515. "Regular expression matching a time stamp range.")
  516. (defconst org-tr-regexp-both
  517. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  518. "Regular expression matching a time stamp range.")
  519. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  520. org-ts-regexp "\\)?")
  521. "Regular expression matching a time stamp or time stamp range.")
  522. (defconst org-tsr-regexp-both
  523. (concat org-ts-regexp-both "\\(--?-?"
  524. org-ts-regexp-both "\\)?")
  525. "Regular expression matching a time stamp or time stamp range.
  526. The time stamps may be either active or inactive.")
  527. (defconst org-repeat-re
  528. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  529. "Regular expression for specifying repeated events.
  530. After a match, group 1 contains the repeat expression.")
  531. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  532. "Formats for `format-time-string' which are used for time stamps.")
  533. ;;; The custom variables
  534. (defgroup org nil
  535. "Outline-based notes management and organizer."
  536. :tag "Org"
  537. :group 'outlines
  538. :group 'calendar)
  539. (defcustom org-mode-hook nil
  540. "Mode hook for Org-mode, run after the mode was turned on."
  541. :group 'org
  542. :type 'hook)
  543. (defcustom org-load-hook nil
  544. "Hook that is run after org.el has been loaded."
  545. :group 'org
  546. :type 'hook)
  547. (defcustom org-log-buffer-setup-hook nil
  548. "Hook that is run after an Org log buffer is created."
  549. :group 'org
  550. :version "24.1"
  551. :type 'hook)
  552. (defvar org-modules) ; defined below
  553. (defvar org-modules-loaded nil
  554. "Have the modules been loaded already?")
  555. (defun org-load-modules-maybe (&optional force)
  556. "Load all extensions listed in `org-modules'."
  557. (when (or force (not org-modules-loaded))
  558. (mapc (lambda (ext)
  559. (condition-case nil (require ext)
  560. (error (message "Problems while trying to load feature `%s'" ext))))
  561. org-modules)
  562. (setq org-modules-loaded t)))
  563. (defun org-set-modules (var value)
  564. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  565. (set var value)
  566. (when (featurep 'org)
  567. (org-load-modules-maybe 'force)
  568. (org-element-cache-reset 'all)))
  569. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  570. "Modules that should always be loaded together with org.el.
  571. If a description starts with <C>, the file is not part of Emacs
  572. and loading it will require that you have downloaded and properly
  573. installed the Org mode distribution.
  574. You can also use this system to load external packages (i.e. neither Org
  575. core modules, nor modules from the CONTRIB directory). Just add symbols
  576. to the end of the list. If the package is called org-xyz.el, then you need
  577. to add the symbol `xyz', and the package must have a call to:
  578. \(provide 'org-xyz)
  579. For export specific modules, see also `org-export-backends'."
  580. :group 'org
  581. :set 'org-set-modules
  582. :version "24.4"
  583. :package-version '(Org . "8.0")
  584. :type
  585. '(set :greedy t
  586. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  587. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  588. (const :tag " crypt: Encryption of subtrees" org-crypt)
  589. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  590. (const :tag " docview: Links to doc-view buffers" org-docview)
  591. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  592. (const :tag " habit: Track your consistency with habits" org-habit)
  593. (const :tag " id: Global IDs for identifying entries" org-id)
  594. (const :tag " info: Links to Info nodes" org-info)
  595. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  596. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  597. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  598. (const :tag " mouse: Additional mouse support" org-mouse)
  599. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  600. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  601. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  602. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  603. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  604. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  605. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  606. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  607. (const :tag "C collector: Collect properties into tables" org-collector)
  608. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  609. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  610. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  611. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  612. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  613. (const :tag "C eval: Include command output as text" org-eval)
  614. (const :tag "C eww: Store link to url of eww" org-eww)
  615. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  616. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  617. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  618. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  619. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  620. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  621. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  622. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  623. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  624. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  625. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  626. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  627. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  628. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  629. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  630. (const :tag "C registry: A registry for Org-mode links" org-registry)
  631. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  632. (const :tag "C secretary: Team management with org-mode" org-secretary)
  633. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  634. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  635. (const :tag "C track: Keep up with Org-mode development" org-track)
  636. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  637. (const :tag "C vm: Links to VM folders/messages" org-vm)
  638. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  639. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  640. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  641. (defvar org-export--registered-backends) ; From ox.el.
  642. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  643. (declare-function org-export-backend-name "ox" (backend))
  644. (defcustom org-export-backends '(ascii html icalendar latex)
  645. "List of export back-ends that should be always available.
  646. If a description starts with <C>, the file is not part of Emacs
  647. and loading it will require that you have downloaded and properly
  648. installed the Org mode distribution.
  649. Unlike to `org-modules', libraries in this list will not be
  650. loaded along with Org, but only once the export framework is
  651. needed.
  652. This variable needs to be set before org.el is loaded. If you
  653. need to make a change while Emacs is running, use the customize
  654. interface or run the following code, where VAL stands for the new
  655. value of the variable, after updating it:
  656. \(progn
  657. \(setq org-export--registered-backends
  658. \(org-remove-if-not
  659. \(lambda (backend)
  660. \(let ((name (org-export-backend-name backend)))
  661. \(or (memq name val)
  662. \(catch 'parentp
  663. \(dolist (b val)
  664. \(and (org-export-derived-backend-p b name)
  665. \(throw 'parentp t)))))))
  666. org-export--registered-backends))
  667. \(let ((new-list (mapcar 'org-export-backend-name
  668. org-export--registered-backends)))
  669. \(dolist (backend val)
  670. \(cond
  671. \((not (load (format \"ox-%s\" backend) t t))
  672. \(message \"Problems while trying to load export back-end `%s'\"
  673. backend))
  674. \((not (memq backend new-list)) (push backend new-list))))
  675. \(set-default 'org-export-backends new-list)))
  676. Adding a back-end to this list will also pull the back-end it
  677. depends on, if any."
  678. :group 'org
  679. :group 'org-export
  680. :version "24.4"
  681. :package-version '(Org . "8.0")
  682. :initialize 'custom-initialize-set
  683. :set (lambda (var val)
  684. (if (not (featurep 'ox)) (set-default var val)
  685. ;; Any back-end not required anymore (not present in VAL and not
  686. ;; a parent of any back-end in the new value) is removed from the
  687. ;; list of registered back-ends.
  688. (setq org-export--registered-backends
  689. (org-remove-if-not
  690. (lambda (backend)
  691. (let ((name (org-export-backend-name backend)))
  692. (or (memq name val)
  693. (catch 'parentp
  694. (dolist (b val)
  695. (and (org-export-derived-backend-p b name)
  696. (throw 'parentp t)))))))
  697. org-export--registered-backends))
  698. ;; Now build NEW-LIST of both new back-ends and required
  699. ;; parents.
  700. (let ((new-list (mapcar 'org-export-backend-name
  701. org-export--registered-backends)))
  702. (dolist (backend val)
  703. (cond
  704. ((not (load (format "ox-%s" backend) t t))
  705. (message "Problems while trying to load export back-end `%s'"
  706. backend))
  707. ((not (memq backend new-list)) (push backend new-list))))
  708. ;; Set VAR to that list with fixed dependencies.
  709. (set-default var new-list))))
  710. :type '(set :greedy t
  711. (const :tag " ascii Export buffer to ASCII format" ascii)
  712. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  713. (const :tag " html Export buffer to HTML format" html)
  714. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  715. (const :tag " latex Export buffer to LaTeX format" latex)
  716. (const :tag " man Export buffer to MAN format" man)
  717. (const :tag " md Export buffer to Markdown format" md)
  718. (const :tag " odt Export buffer to ODT format" odt)
  719. (const :tag " org Export buffer to Org format" org)
  720. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  721. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  722. (const :tag "C deck Export buffer to deck.js presentations" deck)
  723. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  724. (const :tag "C groff Export buffer to Groff format" groff)
  725. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  726. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  727. (const :tag "C s5 Export buffer to s5 presentations" s5)
  728. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  729. (eval-after-load 'ox
  730. '(mapc
  731. (lambda (backend)
  732. (condition-case nil (require (intern (format "ox-%s" backend)))
  733. (error (message "Problems while trying to load export back-end `%s'"
  734. backend))))
  735. org-export-backends))
  736. (defcustom org-support-shift-select nil
  737. "Non-nil means make shift-cursor commands select text when possible.
  738. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  739. start selecting a region, or enlarge regions started in this way.
  740. In Org-mode, in special contexts, these same keys are used for
  741. other purposes, important enough to compete with shift selection.
  742. Org tries to balance these needs by supporting `shift-select-mode'
  743. outside these special contexts, under control of this variable.
  744. The default of this variable is nil, to avoid confusing behavior. Shifted
  745. cursor keys will then execute Org commands in the following contexts:
  746. - on a headline, changing TODO state (left/right) and priority (up/down)
  747. - on a time stamp, changing the time
  748. - in a plain list item, changing the bullet type
  749. - in a property definition line, switching between allowed values
  750. - in the BEGIN line of a clock table (changing the time block).
  751. Outside these contexts, the commands will throw an error.
  752. When this variable is t and the cursor is not in a special
  753. context, Org-mode will support shift-selection for making and
  754. enlarging regions. To make this more effective, the bullet
  755. cycling will no longer happen anywhere in an item line, but only
  756. if the cursor is exactly on the bullet.
  757. If you set this variable to the symbol `always', then the keys
  758. will not be special in headlines, property lines, and item lines,
  759. to make shift selection work there as well. If this is what you
  760. want, you can use the following alternative commands: `C-c C-t'
  761. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  762. can be used to switch TODO sets, `C-c -' to cycle item bullet
  763. types, and properties can be edited by hand or in column view.
  764. However, when the cursor is on a timestamp, shift-cursor commands
  765. will still edit the time stamp - this is just too good to give up.
  766. XEmacs user should have this variable set to nil, because
  767. `shift-select-mode' is in Emacs 23 or later only."
  768. :group 'org
  769. :type '(choice
  770. (const :tag "Never" nil)
  771. (const :tag "When outside special context" t)
  772. (const :tag "Everywhere except timestamps" always)))
  773. (defcustom org-loop-over-headlines-in-active-region nil
  774. "Shall some commands act upon headlines in the active region?
  775. When set to `t', some commands will be performed in all headlines
  776. within the active region.
  777. When set to `start-level', some commands will be performed in all
  778. headlines within the active region, provided that these headlines
  779. are of the same level than the first one.
  780. When set to a string, those commands will be performed on the
  781. matching headlines within the active region. Such string must be
  782. a tags/property/todo match as it is used in the agenda tags view.
  783. The list of commands is: `org-schedule', `org-deadline',
  784. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  785. `org-archive-to-archive-sibling'. The archiving commands skip
  786. already archived entries."
  787. :type '(choice (const :tag "Don't loop" nil)
  788. (const :tag "All headlines in active region" t)
  789. (const :tag "In active region, headlines at the same level than the first one" start-level)
  790. (string :tag "Tags/Property/Todo matcher"))
  791. :version "24.1"
  792. :group 'org-todo
  793. :group 'org-archive)
  794. (defgroup org-startup nil
  795. "Options concerning startup of Org-mode."
  796. :tag "Org Startup"
  797. :group 'org)
  798. (defcustom org-startup-folded t
  799. "Non-nil means entering Org-mode will switch to OVERVIEW.
  800. This can also be configured on a per-file basis by adding one of
  801. the following lines anywhere in the buffer:
  802. #+STARTUP: fold (or `overview', this is equivalent)
  803. #+STARTUP: nofold (or `showall', this is equivalent)
  804. #+STARTUP: content
  805. #+STARTUP: showeverything
  806. By default, this option is ignored when Org opens agenda files
  807. for the first time. If you want the agenda to honor the startup
  808. option, set `org-agenda-inhibit-startup' to nil."
  809. :group 'org-startup
  810. :type '(choice
  811. (const :tag "nofold: show all" nil)
  812. (const :tag "fold: overview" t)
  813. (const :tag "content: all headlines" content)
  814. (const :tag "show everything, even drawers" showeverything)))
  815. (defcustom org-startup-truncated t
  816. "Non-nil means entering Org-mode will set `truncate-lines'.
  817. This is useful since some lines containing links can be very long and
  818. uninteresting. Also tables look terrible when wrapped."
  819. :group 'org-startup
  820. :type 'boolean)
  821. (defcustom org-startup-indented nil
  822. "Non-nil means turn on `org-indent-mode' on startup.
  823. This can also be configured on a per-file basis by adding one of
  824. the following lines anywhere in the buffer:
  825. #+STARTUP: indent
  826. #+STARTUP: noindent"
  827. :group 'org-structure
  828. :type '(choice
  829. (const :tag "Not" nil)
  830. (const :tag "Globally (slow on startup in large files)" t)))
  831. (defcustom org-use-sub-superscripts t
  832. "Non-nil means interpret \"_\" and \"^\" for display.
  833. If you want to control how Org exports those characters, see
  834. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  835. used to be an alias for `org-export-with-sub-superscripts' in
  836. Org <8.0, it is not anymore.
  837. When this option is turned on, you can use TeX-like syntax for
  838. sub- and superscripts within the buffer. Several characters after
  839. \"_\" or \"^\" will be considered as a single item - so grouping
  840. with {} is normally not needed. For example, the following things
  841. will be parsed as single sub- or superscripts:
  842. 10^24 or 10^tau several digits will be considered 1 item.
  843. 10^-12 or 10^-tau a leading sign with digits or a word
  844. x^2-y^3 will be read as x^2 - y^3, because items are
  845. terminated by almost any nonword/nondigit char.
  846. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  847. Still, ambiguity is possible. So when in doubt, use {} to enclose
  848. the sub/superscript. If you set this variable to the symbol `{}',
  849. the braces are *required* in order to trigger interpretations as
  850. sub/superscript. This can be helpful in documents that need \"_\"
  851. frequently in plain text."
  852. :group 'org-startup
  853. :version "24.4"
  854. :package-version '(Org . "8.0")
  855. :type '(choice
  856. (const :tag "Always interpret" t)
  857. (const :tag "Only with braces" {})
  858. (const :tag "Never interpret" nil)))
  859. (defcustom org-startup-with-beamer-mode nil
  860. "Non-nil means turn on `org-beamer-mode' on startup.
  861. This can also be configured on a per-file basis by adding one of
  862. the following lines anywhere in the buffer:
  863. #+STARTUP: beamer"
  864. :group 'org-startup
  865. :version "24.1"
  866. :type 'boolean)
  867. (defcustom org-startup-align-all-tables nil
  868. "Non-nil means align all tables when visiting a file.
  869. This is useful when the column width in tables is forced with <N> cookies
  870. in table fields. Such tables will look correct only after the first re-align.
  871. This can also be configured on a per-file basis by adding one of
  872. the following lines anywhere in the buffer:
  873. #+STARTUP: align
  874. #+STARTUP: noalign"
  875. :group 'org-startup
  876. :type 'boolean)
  877. (defcustom org-startup-with-inline-images nil
  878. "Non-nil means show inline images when loading a new Org file.
  879. This can also be configured on a per-file basis by adding one of
  880. the following lines anywhere in the buffer:
  881. #+STARTUP: inlineimages
  882. #+STARTUP: noinlineimages"
  883. :group 'org-startup
  884. :version "24.1"
  885. :type 'boolean)
  886. (defcustom org-startup-with-latex-preview nil
  887. "Non-nil means preview LaTeX fragments when loading a new Org file.
  888. This can also be configured on a per-file basis by adding one of
  889. the following lines anywhere in the buffer:
  890. #+STARTUP: latexpreview
  891. #+STARTUP: nolatexpreview"
  892. :group 'org-startup
  893. :version "24.4"
  894. :package-version '(Org . "8.0")
  895. :type 'boolean)
  896. (defcustom org-insert-mode-line-in-empty-file nil
  897. "Non-nil means insert the first line setting Org-mode in empty files.
  898. When the function `org-mode' is called interactively in an empty file, this
  899. normally means that the file name does not automatically trigger Org-mode.
  900. To ensure that the file will always be in Org-mode in the future, a
  901. line enforcing Org-mode will be inserted into the buffer, if this option
  902. has been set."
  903. :group 'org-startup
  904. :type 'boolean)
  905. (defcustom org-replace-disputed-keys nil
  906. "Non-nil means use alternative key bindings for some keys.
  907. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  908. These keys are also used by other packages like shift-selection-mode'
  909. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  910. If you want to use Org-mode together with one of these other modes,
  911. or more generally if you would like to move some Org-mode commands to
  912. other keys, set this variable and configure the keys with the variable
  913. `org-disputed-keys'.
  914. This option is only relevant at load-time of Org-mode, and must be set
  915. *before* org.el is loaded. Changing it requires a restart of Emacs to
  916. become effective."
  917. :group 'org-startup
  918. :type 'boolean)
  919. (defcustom org-use-extra-keys nil
  920. "Non-nil means use extra key sequence definitions for certain commands.
  921. This happens automatically if you run XEmacs or if `window-system'
  922. is nil. This variable lets you do the same manually. You must
  923. set it before loading org.
  924. Example: on Carbon Emacs 22 running graphically, with an external
  925. keyboard on a Powerbook, the default way of setting M-left might
  926. not work for either Alt or ESC. Setting this variable will make
  927. it work for ESC."
  928. :group 'org-startup
  929. :type 'boolean)
  930. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  931. (defcustom org-disputed-keys
  932. '(([(shift up)] . [(meta p)])
  933. ([(shift down)] . [(meta n)])
  934. ([(shift left)] . [(meta -)])
  935. ([(shift right)] . [(meta +)])
  936. ([(control shift right)] . [(meta shift +)])
  937. ([(control shift left)] . [(meta shift -)]))
  938. "Keys for which Org-mode and other modes compete.
  939. This is an alist, cars are the default keys, second element specifies
  940. the alternative to use when `org-replace-disputed-keys' is t.
  941. Keys can be specified in any syntax supported by `define-key'.
  942. The value of this option takes effect only at Org-mode's startup,
  943. therefore you'll have to restart Emacs to apply it after changing."
  944. :group 'org-startup
  945. :type 'alist)
  946. (defun org-key (key)
  947. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  948. Or return the original if not disputed.
  949. Also apply the translations defined in `org-xemacs-key-equivalents'."
  950. (when org-replace-disputed-keys
  951. (let* ((nkey (key-description key))
  952. (x (org-find-if (lambda (x)
  953. (equal (key-description (car x)) nkey))
  954. org-disputed-keys)))
  955. (setq key (if x (cdr x) key))))
  956. (when (featurep 'xemacs)
  957. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  958. key)
  959. (defun org-find-if (predicate seq)
  960. (catch 'exit
  961. (while seq
  962. (if (funcall predicate (car seq))
  963. (throw 'exit (car seq))
  964. (pop seq)))))
  965. (defun org-defkey (keymap key def)
  966. "Define a key, possibly translated, as returned by `org-key'."
  967. (define-key keymap (org-key key) def))
  968. (defcustom org-ellipsis nil
  969. "The ellipsis to use in the Org-mode outline.
  970. When nil, just use the standard three dots.
  971. When a string, use that string instead.
  972. When a face, use the standard 3 dots, but with the specified face.
  973. The change affects only Org-mode (which will then use its own display table).
  974. Changing this requires executing \\[org-mode] in a buffer to become
  975. effective."
  976. :group 'org-startup
  977. :type '(choice (const :tag "Default" nil)
  978. (face :tag "Face" :value org-warning)
  979. (string :tag "String" :value "...#")))
  980. (defvar org-display-table nil
  981. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  982. (defgroup org-keywords nil
  983. "Keywords in Org-mode."
  984. :tag "Org Keywords"
  985. :group 'org)
  986. (defcustom org-closed-keep-when-no-todo nil
  987. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  988. :group 'org-todo
  989. :group 'org-keywords
  990. :version "24.4"
  991. :package-version '(Org . "8.0")
  992. :type 'boolean)
  993. (defgroup org-structure nil
  994. "Options concerning the general structure of Org-mode files."
  995. :tag "Org Structure"
  996. :group 'org)
  997. (defgroup org-reveal-location nil
  998. "Options about how to make context of a location visible."
  999. :tag "Org Reveal Location"
  1000. :group 'org-structure)
  1001. (defconst org-context-choice
  1002. '(choice
  1003. (const :tag "Always" t)
  1004. (const :tag "Never" nil)
  1005. (repeat :greedy t :tag "Individual contexts"
  1006. (cons
  1007. (choice :tag "Context"
  1008. (const agenda)
  1009. (const org-goto)
  1010. (const occur-tree)
  1011. (const tags-tree)
  1012. (const link-search)
  1013. (const mark-goto)
  1014. (const bookmark-jump)
  1015. (const isearch)
  1016. (const default))
  1017. (boolean))))
  1018. "Contexts for the reveal options.")
  1019. (defcustom org-show-hierarchy-above '((default . t))
  1020. "Non-nil means show full hierarchy when revealing a location.
  1021. Org-mode often shows locations in an org-mode file which might have
  1022. been invisible before. When this is set, the hierarchy of headings
  1023. above the exposed location is shown.
  1024. Turning this off for example for sparse trees makes them very compact.
  1025. Instead of t, this can also be an alist specifying this option for different
  1026. contexts. Valid contexts are
  1027. agenda when exposing an entry from the agenda
  1028. org-goto when using the command `org-goto' on key C-c C-j
  1029. occur-tree when using the command `org-occur' on key C-c /
  1030. tags-tree when constructing a sparse tree based on tags matches
  1031. link-search when exposing search matches associated with a link
  1032. mark-goto when exposing the jump goal of a mark
  1033. bookmark-jump when exposing a bookmark location
  1034. isearch when exiting from an incremental search
  1035. default default for all contexts not set explicitly"
  1036. :group 'org-reveal-location
  1037. :type org-context-choice)
  1038. (defcustom org-show-following-heading '((default . nil))
  1039. "Non-nil means show following heading when revealing a location.
  1040. Org-mode often shows locations in an org-mode file which might have
  1041. been invisible before. When this is set, the heading following the
  1042. match is shown.
  1043. Turning this off for example for sparse trees makes them very compact,
  1044. but makes it harder to edit the location of the match. In such a case,
  1045. use the command \\[org-reveal] to show more context.
  1046. Instead of t, this can also be an alist specifying this option for different
  1047. contexts. See `org-show-hierarchy-above' for valid contexts."
  1048. :group 'org-reveal-location
  1049. :type org-context-choice)
  1050. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  1051. "Non-nil means show all sibling heading when revealing a location.
  1052. Org-mode often shows locations in an org-mode file which might have
  1053. been invisible before. When this is set, the sibling of the current entry
  1054. heading are all made visible. If `org-show-hierarchy-above' is t,
  1055. the same happens on each level of the hierarchy above the current entry.
  1056. By default this is on for the isearch context, off for all other contexts.
  1057. Turning this off for example for sparse trees makes them very compact,
  1058. but makes it harder to edit the location of the match. In such a case,
  1059. use the command \\[org-reveal] to show more context.
  1060. Instead of t, this can also be an alist specifying this option for different
  1061. contexts. See `org-show-hierarchy-above' for valid contexts."
  1062. :group 'org-reveal-location
  1063. :type org-context-choice
  1064. :version "24.4"
  1065. :package-version '(Org . "8.0"))
  1066. (defcustom org-show-entry-below '((default . nil))
  1067. "Non-nil means show the entry below a headline when revealing a location.
  1068. Org-mode often shows locations in an org-mode file which might have
  1069. been invisible before. When this is set, the text below the headline that is
  1070. exposed is also shown.
  1071. By default this is off for all contexts.
  1072. Instead of t, this can also be an alist specifying this option for different
  1073. contexts. See `org-show-hierarchy-above' for valid contexts."
  1074. :group 'org-reveal-location
  1075. :type org-context-choice)
  1076. (defcustom org-indirect-buffer-display 'other-window
  1077. "How should indirect tree buffers be displayed?
  1078. This applies to indirect buffers created with the commands
  1079. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1080. Valid values are:
  1081. current-window Display in the current window
  1082. other-window Just display in another window.
  1083. dedicated-frame Create one new frame, and re-use it each time.
  1084. new-frame Make a new frame each time. Note that in this case
  1085. previously-made indirect buffers are kept, and you need to
  1086. kill these buffers yourself."
  1087. :group 'org-structure
  1088. :group 'org-agenda-windows
  1089. :type '(choice
  1090. (const :tag "In current window" current-window)
  1091. (const :tag "In current frame, other window" other-window)
  1092. (const :tag "Each time a new frame" new-frame)
  1093. (const :tag "One dedicated frame" dedicated-frame)))
  1094. (defcustom org-use-speed-commands nil
  1095. "Non-nil means activate single letter commands at beginning of a headline.
  1096. This may also be a function to test for appropriate locations where speed
  1097. commands should be active.
  1098. For example, to activate speed commands when the point is on any
  1099. star at the beginning of the headline, you can do this:
  1100. (setq org-use-speed-commands
  1101. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1102. :group 'org-structure
  1103. :type '(choice
  1104. (const :tag "Never" nil)
  1105. (const :tag "At beginning of headline stars" t)
  1106. (function)))
  1107. (defcustom org-speed-commands-user nil
  1108. "Alist of additional speed commands.
  1109. This list will be checked before `org-speed-commands-default'
  1110. when the variable `org-use-speed-commands' is non-nil
  1111. and when the cursor is at the beginning of a headline.
  1112. The car if each entry is a string with a single letter, which must
  1113. be assigned to `self-insert-command' in the global map.
  1114. The cdr is either a command to be called interactively, a function
  1115. to be called, or a form to be evaluated.
  1116. An entry that is just a list with a single string will be interpreted
  1117. as a descriptive headline that will be added when listing the speed
  1118. commands in the Help buffer using the `?' speed command."
  1119. :group 'org-structure
  1120. :type '(repeat :value ("k" . ignore)
  1121. (choice :value ("k" . ignore)
  1122. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1123. (cons :tag "Letter and Command"
  1124. (string :tag "Command letter")
  1125. (choice
  1126. (function)
  1127. (sexp))))))
  1128. (defcustom org-bookmark-names-plist
  1129. '(:last-capture "org-capture-last-stored"
  1130. :last-refile "org-refile-last-stored"
  1131. :last-capture-marker "org-capture-last-stored-marker")
  1132. "Names for bookmarks automatically set by some Org commands.
  1133. This can provide strings as names for a number of bookmarks Org sets
  1134. automatically. The following keys are currently implemented:
  1135. :last-capture
  1136. :last-capture-marker
  1137. :last-refile
  1138. When a key does not show up in the property list, the corresponding bookmark
  1139. is not set."
  1140. :group 'org-structure
  1141. :type 'plist)
  1142. (defgroup org-cycle nil
  1143. "Options concerning visibility cycling in Org-mode."
  1144. :tag "Org Cycle"
  1145. :group 'org-structure)
  1146. (defcustom org-cycle-skip-children-state-if-no-children t
  1147. "Non-nil means skip CHILDREN state in entries that don't have any."
  1148. :group 'org-cycle
  1149. :type 'boolean)
  1150. (defcustom org-cycle-max-level nil
  1151. "Maximum level which should still be subject to visibility cycling.
  1152. Levels higher than this will, for cycling, be treated as text, not a headline.
  1153. When `org-odd-levels-only' is set, a value of N in this variable actually
  1154. means 2N-1 stars as the limiting headline.
  1155. When nil, cycle all levels.
  1156. Note that the limiting level of cycling is also influenced by
  1157. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1158. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1159. than its value."
  1160. :group 'org-cycle
  1161. :type '(choice
  1162. (const :tag "No limit" nil)
  1163. (integer :tag "Maximum level")))
  1164. (defcustom org-hide-block-startup nil
  1165. "Non-nil means entering Org-mode will fold all blocks.
  1166. This can also be set in on a per-file basis with
  1167. #+STARTUP: hideblocks
  1168. #+STARTUP: showblocks"
  1169. :group 'org-startup
  1170. :group 'org-cycle
  1171. :type 'boolean)
  1172. (defcustom org-cycle-global-at-bob nil
  1173. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1174. This makes it possible to do global cycling without having to use S-TAB or
  1175. \\[universal-argument] TAB. For this special case to work, the first line
  1176. of the buffer must not be a headline -- it may be empty or some other text.
  1177. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1178. sure the cursor stays at the beginning of the buffer. When this option is
  1179. nil, don't do anything special at the beginning of the buffer."
  1180. :group 'org-cycle
  1181. :type 'boolean)
  1182. (defcustom org-cycle-level-after-item/entry-creation t
  1183. "Non-nil means cycle entry level or item indentation in new empty entries.
  1184. When the cursor is at the end of an empty headline, i.e., with only stars
  1185. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1186. and then all possible ancestor states, before returning to the original state.
  1187. This makes data entry extremely fast: M-RET to create a new headline,
  1188. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1189. When the cursor is at the end of an empty plain list item, one TAB will
  1190. make it a subitem, two or more tabs will back up to make this an item
  1191. higher up in the item hierarchy."
  1192. :group 'org-cycle
  1193. :type 'boolean)
  1194. (defcustom org-cycle-emulate-tab t
  1195. "Where should `org-cycle' emulate TAB.
  1196. nil Never
  1197. white Only in completely white lines
  1198. whitestart Only at the beginning of lines, before the first non-white char
  1199. t Everywhere except in headlines
  1200. exc-hl-bol Everywhere except at the start of a headline
  1201. If TAB is used in a place where it does not emulate TAB, the current subtree
  1202. visibility is cycled."
  1203. :group 'org-cycle
  1204. :type '(choice (const :tag "Never" nil)
  1205. (const :tag "Only in completely white lines" white)
  1206. (const :tag "Before first char in a line" whitestart)
  1207. (const :tag "Everywhere except in headlines" t)
  1208. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1209. (defcustom org-cycle-separator-lines 2
  1210. "Number of empty lines needed to keep an empty line between collapsed trees.
  1211. If you leave an empty line between the end of a subtree and the following
  1212. headline, this empty line is hidden when the subtree is folded.
  1213. Org-mode will leave (exactly) one empty line visible if the number of
  1214. empty lines is equal or larger to the number given in this variable.
  1215. So the default 2 means at least 2 empty lines after the end of a subtree
  1216. are needed to produce free space between a collapsed subtree and the
  1217. following headline.
  1218. If the number is negative, and the number of empty lines is at least -N,
  1219. all empty lines are shown.
  1220. Special case: when 0, never leave empty lines in collapsed view."
  1221. :group 'org-cycle
  1222. :type 'integer)
  1223. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1224. (defcustom org-pre-cycle-hook nil
  1225. "Hook that is run before visibility cycling is happening.
  1226. The function(s) in this hook must accept a single argument which indicates
  1227. the new state that will be set right after running this hook. The
  1228. argument is a symbol. Before a global state change, it can have the values
  1229. `overview', `content', or `all'. Before a local state change, it can have
  1230. the values `folded', `children', or `subtree'."
  1231. :group 'org-cycle
  1232. :type 'hook)
  1233. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1234. org-cycle-hide-drawers
  1235. org-cycle-hide-inline-tasks
  1236. org-cycle-show-empty-lines
  1237. org-optimize-window-after-visibility-change)
  1238. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1239. The function(s) in this hook must accept a single argument which indicates
  1240. the new state that was set by the most recent `org-cycle' command. The
  1241. argument is a symbol. After a global state change, it can have the values
  1242. `overview', `contents', or `all'. After a local state change, it can have
  1243. the values `folded', `children', or `subtree'."
  1244. :group 'org-cycle
  1245. :type 'hook)
  1246. (defgroup org-edit-structure nil
  1247. "Options concerning structure editing in Org-mode."
  1248. :tag "Org Edit Structure"
  1249. :group 'org-structure)
  1250. (defcustom org-odd-levels-only nil
  1251. "Non-nil means skip even levels and only use odd levels for the outline.
  1252. This has the effect that two stars are being added/taken away in
  1253. promotion/demotion commands. It also influences how levels are
  1254. handled by the exporters.
  1255. Changing it requires restart of `font-lock-mode' to become effective
  1256. for fontification also in regions already fontified.
  1257. You may also set this on a per-file basis by adding one of the following
  1258. lines to the buffer:
  1259. #+STARTUP: odd
  1260. #+STARTUP: oddeven"
  1261. :group 'org-edit-structure
  1262. :group 'org-appearance
  1263. :type 'boolean)
  1264. (defcustom org-adapt-indentation t
  1265. "Non-nil means adapt indentation to outline node level.
  1266. When this variable is set, Org assumes that you write outlines by
  1267. indenting text in each node to align with the headline (after the
  1268. stars). The following issues are influenced by this variable:
  1269. - The indentation is increased by one space in a demotion
  1270. command, and decreased by one in a promotion command. However,
  1271. in the latter case, if shifting some line in the entry body
  1272. would alter document structure (e.g., insert a new headline),
  1273. indentation is not changed at all.
  1274. - Property drawers and planning information is inserted indented
  1275. when this variable is set. When nil, they will not be indented.
  1276. - TAB indents a line relative to current level. The lines below
  1277. a headline will be indented when this variable is set.
  1278. Note that this is all about true indentation, by adding and
  1279. removing space characters. See also `org-indent.el' which does
  1280. level-dependent indentation in a virtual way, i.e. at display
  1281. time in Emacs."
  1282. :group 'org-edit-structure
  1283. :type 'boolean)
  1284. (defcustom org-special-ctrl-a/e nil
  1285. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1286. When t, `C-a' will bring back the cursor to the beginning of the
  1287. headline text, i.e. after the stars and after a possible TODO
  1288. keyword. In an item, this will be the position after bullet and
  1289. check-box, if any. When the cursor is already at that position,
  1290. another `C-a' will bring it to the beginning of the line.
  1291. `C-e' will jump to the end of the headline, ignoring the presence
  1292. of tags in the headline. A second `C-e' will then jump to the
  1293. true end of the line, after any tags. This also means that, when
  1294. this variable is non-nil, `C-e' also will never jump beyond the
  1295. end of the heading of a folded section, i.e. not after the
  1296. ellipses.
  1297. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1298. normally, going to the true line boundary first. Only a directly
  1299. following, identical keypress will bring the cursor to the
  1300. special positions.
  1301. This may also be a cons cell where the behavior for `C-a' and
  1302. `C-e' is set separately."
  1303. :group 'org-edit-structure
  1304. :type '(choice
  1305. (const :tag "off" nil)
  1306. (const :tag "on: after stars/bullet and before tags first" t)
  1307. (const :tag "reversed: true line boundary first" reversed)
  1308. (cons :tag "Set C-a and C-e separately"
  1309. (choice :tag "Special C-a"
  1310. (const :tag "off" nil)
  1311. (const :tag "on: after stars/bullet first" t)
  1312. (const :tag "reversed: before stars/bullet first" reversed))
  1313. (choice :tag "Special C-e"
  1314. (const :tag "off" nil)
  1315. (const :tag "on: before tags first" t)
  1316. (const :tag "reversed: after tags first" reversed)))))
  1317. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1318. (defcustom org-special-ctrl-k nil
  1319. "Non-nil means `C-k' will behave specially in headlines.
  1320. When nil, `C-k' will call the default `kill-line' command.
  1321. When t, the following will happen while the cursor is in the headline:
  1322. - When the cursor is at the beginning of a headline, kill the entire
  1323. line and possible the folded subtree below the line.
  1324. - When in the middle of the headline text, kill the headline up to the tags.
  1325. - When after the headline text, kill the tags."
  1326. :group 'org-edit-structure
  1327. :type 'boolean)
  1328. (defcustom org-ctrl-k-protect-subtree nil
  1329. "Non-nil means, do not delete a hidden subtree with C-k.
  1330. When set to the symbol `error', simply throw an error when C-k is
  1331. used to kill (part-of) a headline that has hidden text behind it.
  1332. Any other non-nil value will result in a query to the user, if it is
  1333. OK to kill that hidden subtree. When nil, kill without remorse."
  1334. :group 'org-edit-structure
  1335. :version "24.1"
  1336. :type '(choice
  1337. (const :tag "Do not protect hidden subtrees" nil)
  1338. (const :tag "Protect hidden subtrees with a security query" t)
  1339. (const :tag "Never kill a hidden subtree with C-k" error)))
  1340. (defcustom org-special-ctrl-o t
  1341. "Non-nil means, make `C-o' insert a row in tables."
  1342. :group 'org-edit-structure
  1343. :type 'boolean)
  1344. (defcustom org-catch-invisible-edits nil
  1345. "Check if in invisible region before inserting or deleting a character.
  1346. Valid values are:
  1347. nil Do not check, so just do invisible edits.
  1348. error Throw an error and do nothing.
  1349. show Make point visible, and do the requested edit.
  1350. show-and-error Make point visible, then throw an error and abort the edit.
  1351. smart Make point visible, and do insertion/deletion if it is
  1352. adjacent to visible text and the change feels predictable.
  1353. Never delete a previously invisible character or add in the
  1354. middle or right after an invisible region. Basically, this
  1355. allows insertion and backward-delete right before ellipses.
  1356. FIXME: maybe in this case we should not even show?"
  1357. :group 'org-edit-structure
  1358. :version "24.1"
  1359. :type '(choice
  1360. (const :tag "Do not check" nil)
  1361. (const :tag "Throw error when trying to edit" error)
  1362. (const :tag "Unhide, but do not do the edit" show-and-error)
  1363. (const :tag "Show invisible part and do the edit" show)
  1364. (const :tag "Be smart and do the right thing" smart)))
  1365. (defcustom org-yank-folded-subtrees t
  1366. "Non-nil means when yanking subtrees, fold them.
  1367. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1368. it starts with a heading and all other headings in it are either children
  1369. or siblings, then fold all the subtrees. However, do this only if no
  1370. text after the yank would be swallowed into a folded tree by this action."
  1371. :group 'org-edit-structure
  1372. :type 'boolean)
  1373. (defcustom org-yank-adjusted-subtrees nil
  1374. "Non-nil means when yanking subtrees, adjust the level.
  1375. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1376. this function for details."
  1377. :group 'org-edit-structure
  1378. :type 'boolean)
  1379. (defcustom org-M-RET-may-split-line '((default . t))
  1380. "Non-nil means M-RET will split the line at the cursor position.
  1381. When nil, it will go to the end of the line before making a
  1382. new line.
  1383. You may also set this option in a different way for different
  1384. contexts. Valid contexts are:
  1385. headline when creating a new headline
  1386. item when creating a new item
  1387. table in a table field
  1388. default the value to be used for all contexts not explicitly
  1389. customized"
  1390. :group 'org-structure
  1391. :group 'org-table
  1392. :type '(choice
  1393. (const :tag "Always" t)
  1394. (const :tag "Never" nil)
  1395. (repeat :greedy t :tag "Individual contexts"
  1396. (cons
  1397. (choice :tag "Context"
  1398. (const headline)
  1399. (const item)
  1400. (const table)
  1401. (const default))
  1402. (boolean)))))
  1403. (defcustom org-insert-heading-respect-content nil
  1404. "Non-nil means insert new headings after the current subtree.
  1405. When nil, the new heading is created directly after the current line.
  1406. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1407. this variable on for the duration of the command."
  1408. :group 'org-structure
  1409. :type 'boolean)
  1410. (defcustom org-blank-before-new-entry '((heading . auto)
  1411. (plain-list-item . auto))
  1412. "Should `org-insert-heading' leave a blank line before new heading/item?
  1413. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1414. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1415. which case Org will look at the surrounding headings/items and try to
  1416. make an intelligent decision whether to insert a blank line or not.
  1417. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1418. the setting here is ignored and no empty line is inserted to avoid breaking
  1419. the list structure."
  1420. :group 'org-edit-structure
  1421. :type '(list
  1422. (cons (const heading)
  1423. (choice (const :tag "Never" nil)
  1424. (const :tag "Always" t)
  1425. (const :tag "Auto" auto)))
  1426. (cons (const plain-list-item)
  1427. (choice (const :tag "Never" nil)
  1428. (const :tag "Always" t)
  1429. (const :tag "Auto" auto)))))
  1430. (defcustom org-insert-heading-hook nil
  1431. "Hook being run after inserting a new heading."
  1432. :group 'org-edit-structure
  1433. :type 'hook)
  1434. (defcustom org-enable-fixed-width-editor t
  1435. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1436. This currently only means they are never auto-wrapped.
  1437. When nil, such lines will be treated like ordinary lines."
  1438. :group 'org-edit-structure
  1439. :type 'boolean)
  1440. (defcustom org-goto-auto-isearch t
  1441. "Non-nil means typing characters in `org-goto' starts incremental search.
  1442. When nil, you can use these keybindings to navigate the buffer:
  1443. q Quit the org-goto interface
  1444. n Go to the next visible heading
  1445. p Go to the previous visible heading
  1446. f Go one heading forward on same level
  1447. b Go one heading backward on same level
  1448. u Go one heading up"
  1449. :group 'org-edit-structure
  1450. :type 'boolean)
  1451. (defgroup org-sparse-trees nil
  1452. "Options concerning sparse trees in Org-mode."
  1453. :tag "Org Sparse Trees"
  1454. :group 'org-structure)
  1455. (defcustom org-highlight-sparse-tree-matches t
  1456. "Non-nil means highlight all matches that define a sparse tree.
  1457. The highlights will automatically disappear the next time the buffer is
  1458. changed by an edit command."
  1459. :group 'org-sparse-trees
  1460. :type 'boolean)
  1461. (defcustom org-remove-highlights-with-change t
  1462. "Non-nil means any change to the buffer will remove temporary highlights.
  1463. Such highlights are created by `org-occur' and `org-clock-display'.
  1464. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1465. The highlights created by `org-toggle-latex-fragment' always need
  1466. `C-c C-x C-l' to be removed."
  1467. :group 'org-sparse-trees
  1468. :group 'org-time
  1469. :type 'boolean)
  1470. (defcustom org-occur-hook '(org-first-headline-recenter)
  1471. "Hook that is run after `org-occur' has constructed a sparse tree.
  1472. This can be used to recenter the window to show as much of the structure
  1473. as possible."
  1474. :group 'org-sparse-trees
  1475. :type 'hook)
  1476. (defgroup org-imenu-and-speedbar nil
  1477. "Options concerning imenu and speedbar in Org-mode."
  1478. :tag "Org Imenu and Speedbar"
  1479. :group 'org-structure)
  1480. (defcustom org-imenu-depth 2
  1481. "The maximum level for Imenu access to Org-mode headlines.
  1482. This also applied for speedbar access."
  1483. :group 'org-imenu-and-speedbar
  1484. :type 'integer)
  1485. (defgroup org-table nil
  1486. "Options concerning tables in Org-mode."
  1487. :tag "Org Table"
  1488. :group 'org)
  1489. (defcustom org-enable-table-editor 'optimized
  1490. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1491. When nil, such lines will be treated like ordinary lines.
  1492. When equal to the symbol `optimized', the table editor will be optimized to
  1493. do the following:
  1494. - Automatic overwrite mode in front of whitespace in table fields.
  1495. This makes the structure of the table stay in tact as long as the edited
  1496. field does not exceed the column width.
  1497. - Minimize the number of realigns. Normally, the table is aligned each time
  1498. TAB or RET are pressed to move to another field. With optimization this
  1499. happens only if changes to a field might have changed the column width.
  1500. Optimization requires replacing the functions `self-insert-command',
  1501. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1502. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1503. very good at guessing when a re-align will be necessary, but you can always
  1504. force one with \\[org-ctrl-c-ctrl-c].
  1505. If you would like to use the optimized version in Org-mode, but the
  1506. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1507. This variable can be used to turn on and off the table editor during a session,
  1508. but in order to toggle optimization, a restart is required.
  1509. See also the variable `org-table-auto-blank-field'."
  1510. :group 'org-table
  1511. :type '(choice
  1512. (const :tag "off" nil)
  1513. (const :tag "on" t)
  1514. (const :tag "on, optimized" optimized)))
  1515. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1516. (version<= emacs-version "24.1"))
  1517. "Non-nil means cluster self-insert commands for undo when possible.
  1518. If this is set, then, like in the Emacs command loop, 20 consecutive
  1519. characters will be undone together.
  1520. This is configurable, because there is some impact on typing performance."
  1521. :group 'org-table
  1522. :type 'boolean)
  1523. (defcustom org-table-tab-recognizes-table.el t
  1524. "Non-nil means TAB will automatically notice a table.el table.
  1525. When it sees such a table, it moves point into it and - if necessary -
  1526. calls `table-recognize-table'."
  1527. :group 'org-table-editing
  1528. :type 'boolean)
  1529. (defgroup org-link nil
  1530. "Options concerning links in Org-mode."
  1531. :tag "Org Link"
  1532. :group 'org)
  1533. (defvar org-link-abbrev-alist-local nil
  1534. "Buffer-local version of `org-link-abbrev-alist', which see.
  1535. The value of this is taken from the #+LINK lines.")
  1536. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1537. (defcustom org-link-abbrev-alist nil
  1538. "Alist of link abbreviations.
  1539. The car of each element is a string, to be replaced at the start of a link.
  1540. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1541. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1542. [[linkkey:tag][description]]
  1543. The 'linkkey' must be a word word, starting with a letter, followed
  1544. by letters, numbers, '-' or '_'.
  1545. If REPLACE is a string, the tag will simply be appended to create the link.
  1546. If the string contains \"%s\", the tag will be inserted there. If the string
  1547. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1548. at that point (see the function `url-hexify-string'). If the string contains
  1549. the specifier \"%(my-function)\", then the custom function `my-function' will
  1550. be invoked: this function takes the tag as its only argument and must return
  1551. a string.
  1552. REPLACE may also be a function that will be called with the tag as the
  1553. only argument to create the link, which should be returned as a string.
  1554. See the manual for examples."
  1555. :group 'org-link
  1556. :type '(repeat
  1557. (cons
  1558. (string :tag "Protocol")
  1559. (choice
  1560. (string :tag "Format")
  1561. (function)))))
  1562. (defcustom org-descriptive-links t
  1563. "Non-nil means Org will display descriptive links.
  1564. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1565. \"Org Website\", hiding the link itself and just displaying its
  1566. description. When set to `nil', Org will display the full links
  1567. literally.
  1568. You can interactively set the value of this variable by calling
  1569. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1570. :group 'org-link
  1571. :type 'boolean)
  1572. (defcustom org-link-file-path-type 'adaptive
  1573. "How the path name in file links should be stored.
  1574. Valid values are:
  1575. relative Relative to the current directory, i.e. the directory of the file
  1576. into which the link is being inserted.
  1577. absolute Absolute path, if possible with ~ for home directory.
  1578. noabbrev Absolute path, no abbreviation of home directory.
  1579. adaptive Use relative path for files in the current directory and sub-
  1580. directories of it. For other files, use an absolute path."
  1581. :group 'org-link
  1582. :type '(choice
  1583. (const relative)
  1584. (const absolute)
  1585. (const noabbrev)
  1586. (const adaptive)))
  1587. (defvaralias 'org-activate-links 'org-highlight-links)
  1588. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1589. "Types of links that should be highlighted in Org-mode files.
  1590. This is a list of symbols, each one of them leading to the
  1591. highlighting of a certain link type.
  1592. You can still open links that are not highlighted.
  1593. In principle, it does not hurt to turn on highlighting for all
  1594. link types. There may be a small gain when turning off unused
  1595. link types. The types are:
  1596. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1597. angle Links in angular brackets that may contain whitespace like
  1598. <bbdb:Carsten Dominik>.
  1599. plain Plain links in normal text, no whitespace, like http://google.com.
  1600. radio Text that is matched by a radio target, see manual for details.
  1601. tag Tag settings in a headline (link to tag search).
  1602. date Time stamps (link to calendar).
  1603. footnote Footnote labels.
  1604. If you set this variable during an Emacs session, use `org-mode-restart'
  1605. in the Org buffer so that the change takes effect."
  1606. :group 'org-link
  1607. :group 'org-appearance
  1608. :type '(set :greedy t
  1609. (const :tag "Double bracket links" bracket)
  1610. (const :tag "Angular bracket links" angle)
  1611. (const :tag "Plain text links" plain)
  1612. (const :tag "Radio target matches" radio)
  1613. (const :tag "Tags" tag)
  1614. (const :tag "Timestamps" date)
  1615. (const :tag "Footnotes" footnote)))
  1616. (defcustom org-make-link-description-function nil
  1617. "Function to use for generating link descriptions from links.
  1618. When nil, the link location will be used. This function must take
  1619. two parameters: the first one is the link, the second one is the
  1620. description generated by `org-insert-link'. The function should
  1621. return the description to use."
  1622. :group 'org-link
  1623. :type '(choice (const nil) (function)))
  1624. (defgroup org-link-store nil
  1625. "Options concerning storing links in Org-mode."
  1626. :tag "Org Store Link"
  1627. :group 'org-link)
  1628. (defcustom org-url-hexify-p t
  1629. "When non-nil, hexify URL when creating a link."
  1630. :type 'boolean
  1631. :version "24.3"
  1632. :group 'org-link-store)
  1633. (defcustom org-email-link-description-format "Email %c: %.30s"
  1634. "Format of the description part of a link to an email or usenet message.
  1635. The following %-escapes will be replaced by corresponding information:
  1636. %F full \"From\" field
  1637. %f name, taken from \"From\" field, address if no name
  1638. %T full \"To\" field
  1639. %t first name in \"To\" field, address if no name
  1640. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1641. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1642. %s subject
  1643. %d date
  1644. %m message-id.
  1645. You may use normal field width specification between the % and the letter.
  1646. This is for example useful to limit the length of the subject.
  1647. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1648. :group 'org-link-store
  1649. :type 'string)
  1650. (defcustom org-from-is-user-regexp
  1651. (let (r1 r2)
  1652. (when (and user-mail-address (not (string= user-mail-address "")))
  1653. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1654. (when (and user-full-name (not (string= user-full-name "")))
  1655. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1656. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1657. "Regexp matched against the \"From:\" header of an email or usenet message.
  1658. It should match if the message is from the user him/herself."
  1659. :group 'org-link-store
  1660. :type 'regexp)
  1661. (defcustom org-context-in-file-links t
  1662. "Non-nil means file links from `org-store-link' contain context.
  1663. A search string will be added to the file name with :: as separator and
  1664. used to find the context when the link is activated by the command
  1665. `org-open-at-point'. When this option is t, the entire active region
  1666. will be placed in the search string of the file link. If set to a
  1667. positive integer, only the first n lines of context will be stored.
  1668. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1669. negates this setting for the duration of the command."
  1670. :group 'org-link-store
  1671. :type '(choice boolean integer))
  1672. (defcustom org-keep-stored-link-after-insertion nil
  1673. "Non-nil means keep link in list for entire session.
  1674. The command `org-store-link' adds a link pointing to the current
  1675. location to an internal list. These links accumulate during a session.
  1676. The command `org-insert-link' can be used to insert links into any
  1677. Org-mode file (offering completion for all stored links). When this
  1678. option is nil, every link which has been inserted once using \\[org-insert-link]
  1679. will be removed from the list, to make completing the unused links
  1680. more efficient."
  1681. :group 'org-link-store
  1682. :type 'boolean)
  1683. (defgroup org-link-follow nil
  1684. "Options concerning following links in Org-mode."
  1685. :tag "Org Follow Link"
  1686. :group 'org-link)
  1687. (defcustom org-link-translation-function nil
  1688. "Function to translate links with different syntax to Org syntax.
  1689. This can be used to translate links created for example by the Planner
  1690. or emacs-wiki packages to Org syntax.
  1691. The function must accept two parameters, a TYPE containing the link
  1692. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1693. which is everything after the link protocol. It should return a cons
  1694. with possibly modified values of type and path.
  1695. Org contains a function for this, so if you set this variable to
  1696. `org-translate-link-from-planner', you should be able follow many
  1697. links created by planner."
  1698. :group 'org-link-follow
  1699. :type '(choice (const nil) (function)))
  1700. (defcustom org-follow-link-hook nil
  1701. "Hook that is run after a link has been followed."
  1702. :group 'org-link-follow
  1703. :type 'hook)
  1704. (defcustom org-tab-follows-link nil
  1705. "Non-nil means on links TAB will follow the link.
  1706. Needs to be set before org.el is loaded.
  1707. This really should not be used, it does not make sense, and the
  1708. implementation is bad."
  1709. :group 'org-link-follow
  1710. :type 'boolean)
  1711. (defcustom org-return-follows-link nil
  1712. "Non-nil means on links RET will follow the link.
  1713. In tables, the special behavior of RET has precedence."
  1714. :group 'org-link-follow
  1715. :type 'boolean)
  1716. (defcustom org-mouse-1-follows-link
  1717. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1718. "Non-nil means mouse-1 on a link will follow the link.
  1719. A longer mouse click will still set point. Does not work on XEmacs.
  1720. Needs to be set before org.el is loaded."
  1721. :group 'org-link-follow
  1722. :version "24.4"
  1723. :package-version '(Org . "8.3")
  1724. :type '(choice
  1725. (const :tag "A double click follows the link" double)
  1726. (const :tag "Unconditionally follow the link with mouse-1" t)
  1727. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1728. (defcustom org-mark-ring-length 4
  1729. "Number of different positions to be recorded in the ring.
  1730. Changing this requires a restart of Emacs to work correctly."
  1731. :group 'org-link-follow
  1732. :type 'integer)
  1733. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1734. "Non-nil means internal links in Org files must exactly match a headline.
  1735. When nil, the link search tries to match a phrase with all words
  1736. in the search text."
  1737. :group 'org-link-follow
  1738. :version "24.1"
  1739. :type '(choice
  1740. (const :tag "Use fuzzy text search" nil)
  1741. (const :tag "Match only exact headline" t)
  1742. (const :tag "Match exact headline or query to create it"
  1743. query-to-create)))
  1744. (defcustom org-link-frame-setup
  1745. '((vm . vm-visit-folder-other-frame)
  1746. (vm-imap . vm-visit-imap-folder-other-frame)
  1747. (gnus . org-gnus-no-new-news)
  1748. (file . find-file-other-window)
  1749. (wl . wl-other-frame))
  1750. "Setup the frame configuration for following links.
  1751. When following a link with Emacs, it may often be useful to display
  1752. this link in another window or frame. This variable can be used to
  1753. set this up for the different types of links.
  1754. For VM, use any of
  1755. `vm-visit-folder'
  1756. `vm-visit-folder-other-window'
  1757. `vm-visit-folder-other-frame'
  1758. For Gnus, use any of
  1759. `gnus'
  1760. `gnus-other-frame'
  1761. `org-gnus-no-new-news'
  1762. For FILE, use any of
  1763. `find-file'
  1764. `find-file-other-window'
  1765. `find-file-other-frame'
  1766. For Wanderlust use any of
  1767. `wl'
  1768. `wl-other-frame'
  1769. For the calendar, use the variable `calendar-setup'.
  1770. For BBDB, it is currently only possible to display the matches in
  1771. another window."
  1772. :group 'org-link-follow
  1773. :type '(list
  1774. (cons (const vm)
  1775. (choice
  1776. (const vm-visit-folder)
  1777. (const vm-visit-folder-other-window)
  1778. (const vm-visit-folder-other-frame)))
  1779. (cons (const vm-imap)
  1780. (choice
  1781. (const vm-visit-imap-folder)
  1782. (const vm-visit-imap-folder-other-window)
  1783. (const vm-visit-imap-folder-other-frame)))
  1784. (cons (const gnus)
  1785. (choice
  1786. (const gnus)
  1787. (const gnus-other-frame)
  1788. (const org-gnus-no-new-news)))
  1789. (cons (const file)
  1790. (choice
  1791. (const find-file)
  1792. (const find-file-other-window)
  1793. (const find-file-other-frame)))
  1794. (cons (const wl)
  1795. (choice
  1796. (const wl)
  1797. (const wl-other-frame)))))
  1798. (defcustom org-display-internal-link-with-indirect-buffer nil
  1799. "Non-nil means use indirect buffer to display infile links.
  1800. Activating internal links (from one location in a file to another location
  1801. in the same file) normally just jumps to the location. When the link is
  1802. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1803. is displayed in
  1804. another window. When this option is set, the other window actually displays
  1805. an indirect buffer clone of the current buffer, to avoid any visibility
  1806. changes to the current buffer."
  1807. :group 'org-link-follow
  1808. :type 'boolean)
  1809. (defcustom org-open-non-existing-files nil
  1810. "Non-nil means `org-open-file' will open non-existing files.
  1811. When nil, an error will be generated.
  1812. This variable applies only to external applications because they
  1813. might choke on non-existing files. If the link is to a file that
  1814. will be opened in Emacs, the variable is ignored."
  1815. :group 'org-link-follow
  1816. :type 'boolean)
  1817. (defcustom org-open-directory-means-index-dot-org nil
  1818. "Non-nil means a link to a directory really means to index.org.
  1819. When nil, following a directory link will run dired or open a finder/explorer
  1820. window on that directory."
  1821. :group 'org-link-follow
  1822. :type 'boolean)
  1823. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1824. "Non-nil means ask for confirmation before executing shell links.
  1825. Shell links can be dangerous: just think about a link
  1826. [[shell:rm -rf ~/*][Google Search]]
  1827. This link would show up in your Org-mode document as \"Google Search\",
  1828. but really it would remove your entire home directory.
  1829. Therefore we advise against setting this variable to nil.
  1830. Just change it to `y-or-n-p' if you want to confirm with a
  1831. single keystroke rather than having to type \"yes\"."
  1832. :group 'org-link-follow
  1833. :type '(choice
  1834. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1835. (const :tag "with y-or-n (faster)" y-or-n-p)
  1836. (const :tag "no confirmation (dangerous)" nil)))
  1837. (put 'org-confirm-shell-link-function
  1838. 'safe-local-variable
  1839. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1840. (defcustom org-confirm-shell-link-not-regexp ""
  1841. "A regexp to skip confirmation for shell links."
  1842. :group 'org-link-follow
  1843. :version "24.1"
  1844. :type 'regexp)
  1845. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1846. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1847. Elisp links can be dangerous: just think about a link
  1848. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1849. This link would show up in your Org-mode document as \"Google Search\",
  1850. but really it would remove your entire home directory.
  1851. Therefore we advise against setting this variable to nil.
  1852. Just change it to `y-or-n-p' if you want to confirm with a
  1853. single keystroke rather than having to type \"yes\"."
  1854. :group 'org-link-follow
  1855. :type '(choice
  1856. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1857. (const :tag "with y-or-n (faster)" y-or-n-p)
  1858. (const :tag "no confirmation (dangerous)" nil)))
  1859. (put 'org-confirm-shell-link-function
  1860. 'safe-local-variable
  1861. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1862. (defcustom org-confirm-elisp-link-not-regexp ""
  1863. "A regexp to skip confirmation for Elisp links."
  1864. :group 'org-link-follow
  1865. :version "24.1"
  1866. :type 'regexp)
  1867. (defconst org-file-apps-defaults-gnu
  1868. '((remote . emacs)
  1869. (system . mailcap)
  1870. (t . mailcap))
  1871. "Default file applications on a UNIX or GNU/Linux system.
  1872. See `org-file-apps'.")
  1873. (defconst org-file-apps-defaults-macosx
  1874. '((remote . emacs)
  1875. (t . "open %s")
  1876. (system . "open %s")
  1877. ("ps.gz" . "gv %s")
  1878. ("eps.gz" . "gv %s")
  1879. ("dvi" . "xdvi %s")
  1880. ("fig" . "xfig %s"))
  1881. "Default file applications on a MacOS X system.
  1882. The system \"open\" is known as a default, but we use X11 applications
  1883. for some files for which the OS does not have a good default.
  1884. See `org-file-apps'.")
  1885. (defconst org-file-apps-defaults-windowsnt
  1886. (list
  1887. '(remote . emacs)
  1888. (cons t
  1889. (list (if (featurep 'xemacs)
  1890. 'mswindows-shell-execute
  1891. 'w32-shell-execute)
  1892. "open" 'file))
  1893. (cons 'system
  1894. (list (if (featurep 'xemacs)
  1895. 'mswindows-shell-execute
  1896. 'w32-shell-execute)
  1897. "open" 'file)))
  1898. "Default file applications on a Windows NT system.
  1899. The system \"open\" is used for most files.
  1900. See `org-file-apps'.")
  1901. (defcustom org-file-apps
  1902. '((auto-mode . emacs)
  1903. ("\\.mm\\'" . default)
  1904. ("\\.x?html?\\'" . default)
  1905. ("\\.pdf\\'" . default))
  1906. "External applications for opening `file:path' items in a document.
  1907. Org-mode uses system defaults for different file types, but
  1908. you can use this variable to set the application for a given file
  1909. extension. The entries in this list are cons cells where the car identifies
  1910. files and the cdr the corresponding command. Possible values for the
  1911. file identifier are
  1912. \"string\" A string as a file identifier can be interpreted in different
  1913. ways, depending on its contents:
  1914. - Alphanumeric characters only:
  1915. Match links with this file extension.
  1916. Example: (\"pdf\" . \"evince %s\")
  1917. to open PDFs with evince.
  1918. - Regular expression: Match links where the
  1919. filename matches the regexp. If you want to
  1920. use groups here, use shy groups.
  1921. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1922. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1923. to open *.html and *.xhtml with firefox.
  1924. - Regular expression which contains (non-shy) groups:
  1925. Match links where the whole link, including \"::\", and
  1926. anything after that, matches the regexp.
  1927. In a custom command string, %1, %2, etc. are replaced with
  1928. the parts of the link that were matched by the groups.
  1929. For backwards compatibility, if a command string is given
  1930. that does not use any of the group matches, this case is
  1931. handled identically to the second one (i.e. match against
  1932. file name only).
  1933. In a custom lisp form, you can access the group matches with
  1934. (match-string n link).
  1935. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1936. to open [[file:document.pdf::5]] with evince at page 5.
  1937. `directory' Matches a directory
  1938. `remote' Matches a remote file, accessible through tramp or efs.
  1939. Remote files most likely should be visited through Emacs
  1940. because external applications cannot handle such paths.
  1941. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1942. so all files Emacs knows how to handle. Using this with
  1943. command `emacs' will open most files in Emacs. Beware that this
  1944. will also open html files inside Emacs, unless you add
  1945. (\"html\" . default) to the list as well.
  1946. t Default for files not matched by any of the other options.
  1947. `system' The system command to open files, like `open' on Windows
  1948. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1949. that will be selected if you call `C-c C-o' with a double
  1950. \\[universal-argument] \\[universal-argument] prefix.
  1951. Possible values for the command are:
  1952. `emacs' The file will be visited by the current Emacs process.
  1953. `default' Use the default application for this file type, which is the
  1954. association for t in the list, most likely in the system-specific
  1955. part.
  1956. This can be used to overrule an unwanted setting in the
  1957. system-specific variable.
  1958. `system' Use the system command for opening files, like \"open\".
  1959. This command is specified by the entry whose car is `system'.
  1960. Most likely, the system-specific version of this variable
  1961. does define this command, but you can overrule/replace it
  1962. here.
  1963. string A command to be executed by a shell; %s will be replaced
  1964. by the path to the file.
  1965. sexp A Lisp form which will be evaluated. The file path will
  1966. be available in the Lisp variable `file'.
  1967. For more examples, see the system specific constants
  1968. `org-file-apps-defaults-macosx'
  1969. `org-file-apps-defaults-windowsnt'
  1970. `org-file-apps-defaults-gnu'."
  1971. :group 'org-link-follow
  1972. :type '(repeat
  1973. (cons (choice :value ""
  1974. (string :tag "Extension")
  1975. (const :tag "System command to open files" system)
  1976. (const :tag "Default for unrecognized files" t)
  1977. (const :tag "Remote file" remote)
  1978. (const :tag "Links to a directory" directory)
  1979. (const :tag "Any files that have Emacs modes"
  1980. auto-mode))
  1981. (choice :value ""
  1982. (const :tag "Visit with Emacs" emacs)
  1983. (const :tag "Use default" default)
  1984. (const :tag "Use the system command" system)
  1985. (string :tag "Command")
  1986. (sexp :tag "Lisp form")))))
  1987. (defcustom org-doi-server-url "http://dx.doi.org/"
  1988. "The URL of the DOI server."
  1989. :type 'string
  1990. :version "24.3"
  1991. :group 'org-link-follow)
  1992. (defgroup org-refile nil
  1993. "Options concerning refiling entries in Org-mode."
  1994. :tag "Org Refile"
  1995. :group 'org)
  1996. (defcustom org-directory "~/org"
  1997. "Directory with org files.
  1998. This is just a default location to look for Org files. There is no need
  1999. at all to put your files into this directory. It is only used in the
  2000. following situations:
  2001. 1. When a capture template specifies a target file that is not an
  2002. absolute path. The path will then be interpreted relative to
  2003. `org-directory'
  2004. 2. When a capture note is filed away in an interactive way (when exiting the
  2005. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  2006. with `org-directory' as the default path."
  2007. :group 'org-refile
  2008. :group 'org-capture
  2009. :type 'directory)
  2010. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2011. "Default target for storing notes.
  2012. Used as a fall back file for org-capture.el, for templates that
  2013. do not specify a target file."
  2014. :group 'org-refile
  2015. :group 'org-capture
  2016. :type '(choice
  2017. (const :tag "Default from remember-data-file" nil)
  2018. file))
  2019. (defcustom org-goto-interface 'outline
  2020. "The default interface to be used for `org-goto'.
  2021. Allowed values are:
  2022. outline The interface shows an outline of the relevant file
  2023. and the correct heading is found by moving through
  2024. the outline or by searching with incremental search.
  2025. outline-path-completion Headlines in the current buffer are offered via
  2026. completion. This is the interface also used by
  2027. the refile command."
  2028. :group 'org-refile
  2029. :type '(choice
  2030. (const :tag "Outline" outline)
  2031. (const :tag "Outline-path-completion" outline-path-completion)))
  2032. (defcustom org-goto-max-level 5
  2033. "Maximum target level when running `org-goto' with refile interface."
  2034. :group 'org-refile
  2035. :type 'integer)
  2036. (defcustom org-reverse-note-order nil
  2037. "Non-nil means store new notes at the beginning of a file or entry.
  2038. When nil, new notes will be filed to the end of a file or entry.
  2039. This can also be a list with cons cells of regular expressions that
  2040. are matched against file names, and values."
  2041. :group 'org-capture
  2042. :group 'org-refile
  2043. :type '(choice
  2044. (const :tag "Reverse always" t)
  2045. (const :tag "Reverse never" nil)
  2046. (repeat :tag "By file name regexp"
  2047. (cons regexp boolean))))
  2048. (defcustom org-log-refile nil
  2049. "Information to record when a task is refiled.
  2050. Possible values are:
  2051. nil Don't add anything
  2052. time Add a time stamp to the task
  2053. note Prompt for a note and add it with template `org-log-note-headings'
  2054. This option can also be set with on a per-file-basis with
  2055. #+STARTUP: nologrefile
  2056. #+STARTUP: logrefile
  2057. #+STARTUP: lognoterefile
  2058. You can have local logging settings for a subtree by setting the LOGGING
  2059. property to one or more of these keywords.
  2060. When bulk-refiling from the agenda, the value `note' is forbidden and
  2061. will temporarily be changed to `time'."
  2062. :group 'org-refile
  2063. :group 'org-progress
  2064. :version "24.1"
  2065. :type '(choice
  2066. (const :tag "No logging" nil)
  2067. (const :tag "Record timestamp" time)
  2068. (const :tag "Record timestamp with note." note)))
  2069. (defcustom org-refile-targets nil
  2070. "Targets for refiling entries with \\[org-refile].
  2071. This is a list of cons cells. Each cell contains:
  2072. - a specification of the files to be considered, either a list of files,
  2073. or a symbol whose function or variable value will be used to retrieve
  2074. a file name or a list of file names. If you use `org-agenda-files' for
  2075. that, all agenda files will be scanned for targets. Nil means consider
  2076. headings in the current buffer.
  2077. - A specification of how to find candidate refile targets. This may be
  2078. any of:
  2079. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2080. This tag has to be present in all target headlines, inheritance will
  2081. not be considered.
  2082. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2083. todo keyword.
  2084. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2085. headlines that are refiling targets.
  2086. - a cons cell (:level . N). Any headline of level N is considered a target.
  2087. Note that, when `org-odd-levels-only' is set, level corresponds to
  2088. order in hierarchy, not to the number of stars.
  2089. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2090. Note that, when `org-odd-levels-only' is set, level corresponds to
  2091. order in hierarchy, not to the number of stars.
  2092. Each element of this list generates a set of possible targets.
  2093. The union of these sets is presented (with completion) to
  2094. the user by `org-refile'.
  2095. You can set the variable `org-refile-target-verify-function' to a function
  2096. to verify each headline found by the simple criteria above.
  2097. When this variable is nil, all top-level headlines in the current buffer
  2098. are used, equivalent to the value `((nil . (:level . 1))'."
  2099. :group 'org-refile
  2100. :type '(repeat
  2101. (cons
  2102. (choice :value org-agenda-files
  2103. (const :tag "All agenda files" org-agenda-files)
  2104. (const :tag "Current buffer" nil)
  2105. (function) (variable) (file))
  2106. (choice :tag "Identify target headline by"
  2107. (cons :tag "Specific tag" (const :value :tag) (string))
  2108. (cons :tag "TODO keyword" (const :value :todo) (string))
  2109. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2110. (cons :tag "Level number" (const :value :level) (integer))
  2111. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2112. (defcustom org-refile-target-verify-function nil
  2113. "Function to verify if the headline at point should be a refile target.
  2114. The function will be called without arguments, with point at the
  2115. beginning of the headline. It should return t and leave point
  2116. where it is if the headline is a valid target for refiling.
  2117. If the target should not be selected, the function must return nil.
  2118. In addition to this, it may move point to a place from where the search
  2119. should be continued. For example, the function may decide that the entire
  2120. subtree of the current entry should be excluded and move point to the end
  2121. of the subtree."
  2122. :group 'org-refile
  2123. :type '(choice
  2124. (const nil)
  2125. (function)))
  2126. (defcustom org-refile-use-cache nil
  2127. "Non-nil means cache refile targets to speed up the process.
  2128. The cache for a particular file will be updated automatically when
  2129. the buffer has been killed, or when any of the marker used for flagging
  2130. refile targets no longer points at a live buffer.
  2131. If you have added new entries to a buffer that might themselves be targets,
  2132. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2133. find that easier, `C-u C-u C-u C-c C-w'."
  2134. :group 'org-refile
  2135. :version "24.1"
  2136. :type 'boolean)
  2137. (defcustom org-refile-use-outline-path nil
  2138. "Non-nil means provide refile targets as paths.
  2139. So a level 3 headline will be available as level1/level2/level3.
  2140. When the value is `file', also include the file name (without directory)
  2141. into the path. In this case, you can also stop the completion after
  2142. the file name, to get entries inserted as top level in the file.
  2143. When `full-file-path', include the full file path."
  2144. :group 'org-refile
  2145. :type '(choice
  2146. (const :tag "Not" nil)
  2147. (const :tag "Yes" t)
  2148. (const :tag "Start with file name" file)
  2149. (const :tag "Start with full file path" full-file-path)))
  2150. (defcustom org-outline-path-complete-in-steps t
  2151. "Non-nil means complete the outline path in hierarchical steps.
  2152. When Org-mode uses the refile interface to select an outline path
  2153. \(see variable `org-refile-use-outline-path'), the completion of
  2154. the path can be done is a single go, or if can be done in steps down
  2155. the headline hierarchy. Going in steps is probably the best if you
  2156. do not use a special completion package like `ido' or `icicles'.
  2157. However, when using these packages, going in one step can be very
  2158. fast, while still showing the whole path to the entry."
  2159. :group 'org-refile
  2160. :type 'boolean)
  2161. (defcustom org-refile-allow-creating-parent-nodes nil
  2162. "Non-nil means allow to create new nodes as refile targets.
  2163. New nodes are then created by adding \"/new node name\" to the completion
  2164. of an existing node. When the value of this variable is `confirm',
  2165. new node creation must be confirmed by the user (recommended).
  2166. When nil, the completion must match an existing entry.
  2167. Note that, if the new heading is not seen by the criteria
  2168. listed in `org-refile-targets', multiple instances of the same
  2169. heading would be created by trying again to file under the new
  2170. heading."
  2171. :group 'org-refile
  2172. :type '(choice
  2173. (const :tag "Never" nil)
  2174. (const :tag "Always" t)
  2175. (const :tag "Prompt for confirmation" confirm)))
  2176. (defcustom org-refile-active-region-within-subtree nil
  2177. "Non-nil means also refile active region within a subtree.
  2178. By default `org-refile' doesn't allow refiling regions if they
  2179. don't contain a set of subtrees, but it might be convenient to
  2180. do so sometimes: in that case, the first line of the region is
  2181. converted to a headline before refiling."
  2182. :group 'org-refile
  2183. :version "24.1"
  2184. :type 'boolean)
  2185. (defgroup org-todo nil
  2186. "Options concerning TODO items in Org-mode."
  2187. :tag "Org TODO"
  2188. :group 'org)
  2189. (defgroup org-progress nil
  2190. "Options concerning Progress logging in Org-mode."
  2191. :tag "Org Progress"
  2192. :group 'org-time)
  2193. (defvar org-todo-interpretation-widgets
  2194. '((:tag "Sequence (cycling hits every state)" sequence)
  2195. (:tag "Type (cycling directly to DONE)" type))
  2196. "The available interpretation symbols for customizing `org-todo-keywords'.
  2197. Interested libraries should add to this list.")
  2198. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2199. "List of TODO entry keyword sequences and their interpretation.
  2200. \\<org-mode-map>This is a list of sequences.
  2201. Each sequence starts with a symbol, either `sequence' or `type',
  2202. indicating if the keywords should be interpreted as a sequence of
  2203. action steps, or as different types of TODO items. The first
  2204. keywords are states requiring action - these states will select a headline
  2205. for inclusion into the global TODO list Org-mode produces. If one of
  2206. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2207. signify that no further action is necessary. If \"|\" is not found,
  2208. the last keyword is treated as the only DONE state of the sequence.
  2209. The command \\[org-todo] cycles an entry through these states, and one
  2210. additional state where no keyword is present. For details about this
  2211. cycling, see the manual.
  2212. TODO keywords and interpretation can also be set on a per-file basis with
  2213. the special #+SEQ_TODO and #+TYP_TODO lines.
  2214. Each keyword can optionally specify a character for fast state selection
  2215. \(in combination with the variable `org-use-fast-todo-selection')
  2216. and specifiers for state change logging, using the same syntax that
  2217. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2218. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2219. indicates to record a time stamp each time this state is selected.
  2220. Each keyword may also specify if a timestamp or a note should be
  2221. recorded when entering or leaving the state, by adding additional
  2222. characters in the parenthesis after the keyword. This looks like this:
  2223. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2224. record only the time of the state change. With X and Y being either
  2225. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2226. Y when leaving the state if and only if the *target* state does not
  2227. define X. You may omit any of the fast-selection key or X or /Y,
  2228. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2229. For backward compatibility, this variable may also be just a list
  2230. of keywords. In this case the interpretation (sequence or type) will be
  2231. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2232. :group 'org-todo
  2233. :group 'org-keywords
  2234. :type '(choice
  2235. (repeat :tag "Old syntax, just keywords"
  2236. (string :tag "Keyword"))
  2237. (repeat :tag "New syntax"
  2238. (cons
  2239. (choice
  2240. :tag "Interpretation"
  2241. ;;Quick and dirty way to see
  2242. ;;`org-todo-interpretations'. This takes the
  2243. ;;place of item arguments
  2244. :convert-widget
  2245. (lambda (widget)
  2246. (widget-put widget
  2247. :args (mapcar
  2248. (lambda (x)
  2249. (widget-convert
  2250. (cons 'const x)))
  2251. org-todo-interpretation-widgets))
  2252. widget))
  2253. (repeat
  2254. (string :tag "Keyword"))))))
  2255. (defvar org-todo-keywords-1 nil
  2256. "All TODO and DONE keywords active in a buffer.")
  2257. (make-variable-buffer-local 'org-todo-keywords-1)
  2258. (defvar org-todo-keywords-for-agenda nil)
  2259. (defvar org-done-keywords-for-agenda nil)
  2260. (defvar org-todo-keyword-alist-for-agenda nil)
  2261. (defvar org-tag-alist-for-agenda nil
  2262. "Alist of all tags from all agenda files.")
  2263. (defvar org-tag-groups-alist-for-agenda nil
  2264. "Alist of all groups tags from all current agenda files.")
  2265. (defvar org-tag-groups-alist nil)
  2266. (make-variable-buffer-local 'org-tag-groups-alist)
  2267. (defvar org-agenda-contributing-files nil)
  2268. (defvar org-not-done-keywords nil)
  2269. (make-variable-buffer-local 'org-not-done-keywords)
  2270. (defvar org-done-keywords nil)
  2271. (make-variable-buffer-local 'org-done-keywords)
  2272. (defvar org-todo-heads nil)
  2273. (make-variable-buffer-local 'org-todo-heads)
  2274. (defvar org-todo-sets nil)
  2275. (make-variable-buffer-local 'org-todo-sets)
  2276. (defvar org-todo-log-states nil)
  2277. (make-variable-buffer-local 'org-todo-log-states)
  2278. (defvar org-todo-kwd-alist nil)
  2279. (make-variable-buffer-local 'org-todo-kwd-alist)
  2280. (defvar org-todo-key-alist nil)
  2281. (make-variable-buffer-local 'org-todo-key-alist)
  2282. (defvar org-todo-key-trigger nil)
  2283. (make-variable-buffer-local 'org-todo-key-trigger)
  2284. (defcustom org-todo-interpretation 'sequence
  2285. "Controls how TODO keywords are interpreted.
  2286. This variable is in principle obsolete and is only used for
  2287. backward compatibility, if the interpretation of todo keywords is
  2288. not given already in `org-todo-keywords'. See that variable for
  2289. more information."
  2290. :group 'org-todo
  2291. :group 'org-keywords
  2292. :type '(choice (const sequence)
  2293. (const type)))
  2294. (defcustom org-use-fast-todo-selection t
  2295. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2296. This variable describes if and under what circumstances the cycling
  2297. mechanism for TODO keywords will be replaced by a single-key, direct
  2298. selection scheme.
  2299. When nil, fast selection is never used.
  2300. When the symbol `prefix', it will be used when `org-todo' is called
  2301. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2302. `C-u t' in an agenda buffer.
  2303. When t, fast selection is used by default. In this case, the prefix
  2304. argument forces cycling instead.
  2305. In all cases, the special interface is only used if access keys have
  2306. actually been assigned by the user, i.e. if keywords in the configuration
  2307. are followed by a letter in parenthesis, like TODO(t)."
  2308. :group 'org-todo
  2309. :type '(choice
  2310. (const :tag "Never" nil)
  2311. (const :tag "By default" t)
  2312. (const :tag "Only with C-u C-c C-t" prefix)))
  2313. (defcustom org-provide-todo-statistics t
  2314. "Non-nil means update todo statistics after insert and toggle.
  2315. ALL-HEADLINES means update todo statistics by including headlines
  2316. with no TODO keyword as well, counting them as not done.
  2317. A list of TODO keywords means the same, but skip keywords that are
  2318. not in this list.
  2319. When set to a list of two lists, the first list contains keywords
  2320. to consider as TODO keywords, the second list contains keywords
  2321. to consider as DONE keywords.
  2322. When this is set, todo statistics is updated in the parent of the
  2323. current entry each time a todo state is changed."
  2324. :group 'org-todo
  2325. :type '(choice
  2326. (const :tag "Yes, only for TODO entries" t)
  2327. (const :tag "Yes, including all entries" all-headlines)
  2328. (repeat :tag "Yes, for TODOs in this list"
  2329. (string :tag "TODO keyword"))
  2330. (list :tag "Yes, for TODOs and DONEs in these lists"
  2331. (repeat (string :tag "TODO keyword"))
  2332. (repeat (string :tag "DONE keyword")))
  2333. (other :tag "No TODO statistics" nil)))
  2334. (defcustom org-hierarchical-todo-statistics t
  2335. "Non-nil means TODO statistics covers just direct children.
  2336. When nil, all entries in the subtree are considered.
  2337. This has only an effect if `org-provide-todo-statistics' is set.
  2338. To set this to nil for only a single subtree, use a COOKIE_DATA
  2339. property and include the word \"recursive\" into the value."
  2340. :group 'org-todo
  2341. :type 'boolean)
  2342. (defcustom org-after-todo-state-change-hook nil
  2343. "Hook which is run after the state of a TODO item was changed.
  2344. The new state (a string with a TODO keyword, or nil) is available in the
  2345. Lisp variable `org-state'."
  2346. :group 'org-todo
  2347. :type 'hook)
  2348. (defvar org-blocker-hook nil
  2349. "Hook for functions that are allowed to block a state change.
  2350. Functions in this hook should not modify the buffer.
  2351. Each function gets as its single argument a property list,
  2352. see `org-trigger-hook' for more information about this list.
  2353. If any of the functions in this hook returns nil, the state change
  2354. is blocked.")
  2355. (defvar org-trigger-hook nil
  2356. "Hook for functions that are triggered by a state change.
  2357. Each function gets as its single argument a property list with at
  2358. least the following elements:
  2359. (:type type-of-change :position pos-at-entry-start
  2360. :from old-state :to new-state)
  2361. Depending on the type, more properties may be present.
  2362. This mechanism is currently implemented for:
  2363. TODO state changes
  2364. ------------------
  2365. :type todo-state-change
  2366. :from previous state (keyword as a string), or nil, or a symbol
  2367. 'todo' or 'done', to indicate the general type of state.
  2368. :to new state, like in :from")
  2369. (defcustom org-enforce-todo-dependencies nil
  2370. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2371. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2372. be blocked if any prior sibling is not yet done.
  2373. Finally, if the parent is blocked because of ordered siblings of its own,
  2374. the child will also be blocked."
  2375. :set (lambda (var val)
  2376. (set var val)
  2377. (if val
  2378. (add-hook 'org-blocker-hook
  2379. 'org-block-todo-from-children-or-siblings-or-parent)
  2380. (remove-hook 'org-blocker-hook
  2381. 'org-block-todo-from-children-or-siblings-or-parent)))
  2382. :group 'org-todo
  2383. :type 'boolean)
  2384. (defcustom org-enforce-todo-checkbox-dependencies nil
  2385. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2386. When this is nil, checkboxes have no influence on switching TODO states.
  2387. When non-nil, you first need to check off all check boxes before the TODO
  2388. entry can be switched to DONE.
  2389. This variable needs to be set before org.el is loaded, and you need to
  2390. restart Emacs after a change to make the change effective. The only way
  2391. to change is while Emacs is running is through the customize interface."
  2392. :set (lambda (var val)
  2393. (set var val)
  2394. (if val
  2395. (add-hook 'org-blocker-hook
  2396. 'org-block-todo-from-checkboxes)
  2397. (remove-hook 'org-blocker-hook
  2398. 'org-block-todo-from-checkboxes)))
  2399. :group 'org-todo
  2400. :type 'boolean)
  2401. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2402. "Non-nil means inserting a TODO heading is treated as state change.
  2403. So when the command \\[org-insert-todo-heading] is used, state change
  2404. logging will apply if appropriate. When nil, the new TODO item will
  2405. be inserted directly, and no logging will take place."
  2406. :group 'org-todo
  2407. :type 'boolean)
  2408. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2409. "Non-nil means switching TODO states with S-cursor counts as state change.
  2410. This is the default behavior. However, setting this to nil allows a
  2411. convenient way to select a TODO state and bypass any logging associated
  2412. with that."
  2413. :group 'org-todo
  2414. :type 'boolean)
  2415. (defcustom org-todo-state-tags-triggers nil
  2416. "Tag changes that should be triggered by TODO state changes.
  2417. This is a list. Each entry is
  2418. (state-change (tag . flag) .......)
  2419. State-change can be a string with a state, and empty string to indicate the
  2420. state that has no TODO keyword, or it can be one of the symbols `todo'
  2421. or `done', meaning any not-done or done state, respectively."
  2422. :group 'org-todo
  2423. :group 'org-tags
  2424. :type '(repeat
  2425. (cons (choice :tag "When changing to"
  2426. (const :tag "Not-done state" todo)
  2427. (const :tag "Done state" done)
  2428. (string :tag "State"))
  2429. (repeat
  2430. (cons :tag "Tag action"
  2431. (string :tag "Tag")
  2432. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2433. (defcustom org-log-done nil
  2434. "Information to record when a task moves to the DONE state.
  2435. Possible values are:
  2436. nil Don't add anything, just change the keyword
  2437. time Add a time stamp to the task
  2438. note Prompt for a note and add it with template `org-log-note-headings'
  2439. This option can also be set with on a per-file-basis with
  2440. #+STARTUP: nologdone
  2441. #+STARTUP: logdone
  2442. #+STARTUP: lognotedone
  2443. You can have local logging settings for a subtree by setting the LOGGING
  2444. property to one or more of these keywords."
  2445. :group 'org-todo
  2446. :group 'org-progress
  2447. :type '(choice
  2448. (const :tag "No logging" nil)
  2449. (const :tag "Record CLOSED timestamp" time)
  2450. (const :tag "Record CLOSED timestamp with note." note)))
  2451. ;; Normalize old uses of org-log-done.
  2452. (cond
  2453. ((eq org-log-done t) (setq org-log-done 'time))
  2454. ((and (listp org-log-done) (memq 'done org-log-done))
  2455. (setq org-log-done 'note)))
  2456. (defcustom org-log-reschedule nil
  2457. "Information to record when the scheduling date of a tasks is modified.
  2458. Possible values are:
  2459. nil Don't add anything, just change the date
  2460. time Add a time stamp to the task
  2461. note Prompt for a note and add it with template `org-log-note-headings'
  2462. This option can also be set with on a per-file-basis with
  2463. #+STARTUP: nologreschedule
  2464. #+STARTUP: logreschedule
  2465. #+STARTUP: lognotereschedule"
  2466. :group 'org-todo
  2467. :group 'org-progress
  2468. :type '(choice
  2469. (const :tag "No logging" nil)
  2470. (const :tag "Record timestamp" time)
  2471. (const :tag "Record timestamp with note." note)))
  2472. (defcustom org-log-redeadline nil
  2473. "Information to record when the deadline date of a tasks is modified.
  2474. Possible values are:
  2475. nil Don't add anything, just change the date
  2476. time Add a time stamp to the task
  2477. note Prompt for a note and add it with template `org-log-note-headings'
  2478. This option can also be set with on a per-file-basis with
  2479. #+STARTUP: nologredeadline
  2480. #+STARTUP: logredeadline
  2481. #+STARTUP: lognoteredeadline
  2482. You can have local logging settings for a subtree by setting the LOGGING
  2483. property to one or more of these keywords."
  2484. :group 'org-todo
  2485. :group 'org-progress
  2486. :type '(choice
  2487. (const :tag "No logging" nil)
  2488. (const :tag "Record timestamp" time)
  2489. (const :tag "Record timestamp with note." note)))
  2490. (defcustom org-log-note-clock-out nil
  2491. "Non-nil means record a note when clocking out of an item.
  2492. This can also be configured on a per-file basis by adding one of
  2493. the following lines anywhere in the buffer:
  2494. #+STARTUP: lognoteclock-out
  2495. #+STARTUP: nolognoteclock-out"
  2496. :group 'org-todo
  2497. :group 'org-progress
  2498. :type 'boolean)
  2499. (defcustom org-log-done-with-time t
  2500. "Non-nil means the CLOSED time stamp will contain date and time.
  2501. When nil, only the date will be recorded."
  2502. :group 'org-progress
  2503. :type 'boolean)
  2504. (defcustom org-log-note-headings
  2505. '((done . "CLOSING NOTE %t")
  2506. (state . "State %-12s from %-12S %t")
  2507. (note . "Note taken on %t")
  2508. (reschedule . "Rescheduled from %S on %t")
  2509. (delschedule . "Not scheduled, was %S on %t")
  2510. (redeadline . "New deadline from %S on %t")
  2511. (deldeadline . "Removed deadline, was %S on %t")
  2512. (refile . "Refiled on %t")
  2513. (clock-out . ""))
  2514. "Headings for notes added to entries.
  2515. The value is an alist, with the car being a symbol indicating the note
  2516. context, and the cdr is the heading to be used. The heading may also be the
  2517. empty string.
  2518. %t in the heading will be replaced by a time stamp.
  2519. %T will be an active time stamp instead the default inactive one
  2520. %d will be replaced by a short-format time stamp.
  2521. %D will be replaced by an active short-format time stamp.
  2522. %s will be replaced by the new TODO state, in double quotes.
  2523. %S will be replaced by the old TODO state, in double quotes.
  2524. %u will be replaced by the user name.
  2525. %U will be replaced by the full user name.
  2526. In fact, it is not a good idea to change the `state' entry, because
  2527. agenda log mode depends on the format of these entries."
  2528. :group 'org-todo
  2529. :group 'org-progress
  2530. :type '(list :greedy t
  2531. (cons (const :tag "Heading when closing an item" done) string)
  2532. (cons (const :tag
  2533. "Heading when changing todo state (todo sequence only)"
  2534. state) string)
  2535. (cons (const :tag "Heading when just taking a note" note) string)
  2536. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2537. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2538. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2539. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2540. (cons (const :tag "Heading when refiling" refile) string)
  2541. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2542. (unless (assq 'note org-log-note-headings)
  2543. (push '(note . "%t") org-log-note-headings))
  2544. (defcustom org-log-into-drawer nil
  2545. "Non-nil means insert state change notes and time stamps into a drawer.
  2546. When nil, state changes notes will be inserted after the headline and
  2547. any scheduling and clock lines, but not inside a drawer.
  2548. The value of this variable should be the name of the drawer to use.
  2549. LOGBOOK is proposed as the default drawer for this purpose, you can
  2550. also set this to a string to define the drawer of your choice.
  2551. A value of t is also allowed, representing \"LOGBOOK\".
  2552. A value of t or nil can also be set with on a per-file-basis with
  2553. #+STARTUP: logdrawer
  2554. #+STARTUP: nologdrawer
  2555. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2556. will be ignored.
  2557. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2558. a subtree.
  2559. Do not check directly this variable in a Lisp program. Call
  2560. function `org-log-into-drawer' instead."
  2561. :group 'org-todo
  2562. :group 'org-progress
  2563. :type '(choice
  2564. (const :tag "Not into a drawer" nil)
  2565. (const :tag "LOGBOOK" t)
  2566. (string :tag "Other")))
  2567. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2568. (defun org-log-into-drawer ()
  2569. "Name of the log drawer, as a string, or nil.
  2570. This is the value of `org-log-into-drawer'. However, if the
  2571. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2572. be used instead of the default value."
  2573. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2574. (cond ((equal p "nil") nil)
  2575. ((equal p "t") "LOGBOOK")
  2576. ((stringp p) p)
  2577. (p "LOGBOOK")
  2578. ((stringp org-log-into-drawer) org-log-into-drawer)
  2579. (org-log-into-drawer "LOGBOOK"))))
  2580. (defcustom org-log-state-notes-insert-after-drawers nil
  2581. "Non-nil means insert state change notes after any drawers in entry.
  2582. Only the drawers that *immediately* follow the headline and the
  2583. deadline/scheduled line are skipped.
  2584. When nil, insert notes right after the heading and perhaps the line
  2585. with deadline/scheduling if present.
  2586. This variable will have no effect if `org-log-into-drawer' is
  2587. set."
  2588. :group 'org-todo
  2589. :group 'org-progress
  2590. :type 'boolean)
  2591. (defcustom org-log-states-order-reversed t
  2592. "Non-nil means the latest state note will be directly after heading.
  2593. When nil, the state change notes will be ordered according to time.
  2594. This option can also be set with on a per-file-basis with
  2595. #+STARTUP: logstatesreversed
  2596. #+STARTUP: nologstatesreversed"
  2597. :group 'org-todo
  2598. :group 'org-progress
  2599. :type 'boolean)
  2600. (defcustom org-todo-repeat-to-state nil
  2601. "The TODO state to which a repeater should return the repeating task.
  2602. By default this is the first task in a TODO sequence, or the previous state
  2603. in a TODO_TYP set. But you can specify another task here.
  2604. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2605. :group 'org-todo
  2606. :version "24.1"
  2607. :type '(choice (const :tag "Head of sequence" nil)
  2608. (string :tag "Specific state")))
  2609. (defcustom org-log-repeat 'time
  2610. "Non-nil means record moving through the DONE state when triggering repeat.
  2611. An auto-repeating task is immediately switched back to TODO when
  2612. marked DONE. If you are not logging state changes (by adding \"@\"
  2613. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2614. record a closing note, there will be no record of the task moving
  2615. through DONE. This variable forces taking a note anyway.
  2616. nil Don't force a record
  2617. time Record a time stamp
  2618. note Prompt for a note and add it with template `org-log-note-headings'
  2619. This option can also be set with on a per-file-basis with
  2620. #+STARTUP: nologrepeat
  2621. #+STARTUP: logrepeat
  2622. #+STARTUP: lognoterepeat
  2623. You can have local logging settings for a subtree by setting the LOGGING
  2624. property to one or more of these keywords."
  2625. :group 'org-todo
  2626. :group 'org-progress
  2627. :type '(choice
  2628. (const :tag "Don't force a record" nil)
  2629. (const :tag "Force recording the DONE state" time)
  2630. (const :tag "Force recording a note with the DONE state" note)))
  2631. (defgroup org-priorities nil
  2632. "Priorities in Org-mode."
  2633. :tag "Org Priorities"
  2634. :group 'org-todo)
  2635. (defcustom org-enable-priority-commands t
  2636. "Non-nil means priority commands are active.
  2637. When nil, these commands will be disabled, so that you never accidentally
  2638. set a priority."
  2639. :group 'org-priorities
  2640. :type 'boolean)
  2641. (defcustom org-highest-priority ?A
  2642. "The highest priority of TODO items. A character like ?A, ?B etc.
  2643. Must have a smaller ASCII number than `org-lowest-priority'."
  2644. :group 'org-priorities
  2645. :type 'character)
  2646. (defcustom org-lowest-priority ?C
  2647. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2648. Must have a larger ASCII number than `org-highest-priority'."
  2649. :group 'org-priorities
  2650. :type 'character)
  2651. (defcustom org-default-priority ?B
  2652. "The default priority of TODO items.
  2653. This is the priority an item gets if no explicit priority is given.
  2654. When starting to cycle on an empty priority the first step in the cycle
  2655. depends on `org-priority-start-cycle-with-default'. The resulting first
  2656. step priority must not exceed the range from `org-highest-priority' to
  2657. `org-lowest-priority' which means that `org-default-priority' has to be
  2658. in this range exclusive or inclusive the range boundaries. Else the
  2659. first step refuses to set the default and the second will fall back
  2660. to (depending on the command used) the highest or lowest priority."
  2661. :group 'org-priorities
  2662. :type 'character)
  2663. (defcustom org-priority-start-cycle-with-default t
  2664. "Non-nil means start with default priority when starting to cycle.
  2665. When this is nil, the first step in the cycle will be (depending on the
  2666. command used) one higher or lower than the default priority.
  2667. See also `org-default-priority'."
  2668. :group 'org-priorities
  2669. :type 'boolean)
  2670. (defcustom org-get-priority-function nil
  2671. "Function to extract the priority from a string.
  2672. The string is normally the headline. If this is nil Org computes the
  2673. priority from the priority cookie like [#A] in the headline. It returns
  2674. an integer, increasing by 1000 for each priority level.
  2675. The user can set a different function here, which should take a string
  2676. as an argument and return the numeric priority."
  2677. :group 'org-priorities
  2678. :version "24.1"
  2679. :type '(choice
  2680. (const nil)
  2681. (function)))
  2682. (defgroup org-time nil
  2683. "Options concerning time stamps and deadlines in Org-mode."
  2684. :tag "Org Time"
  2685. :group 'org)
  2686. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2687. "Number of minutes to round time stamps to.
  2688. These are two values, the first applies when first creating a time stamp.
  2689. The second applies when changing it with the commands `S-up' and `S-down'.
  2690. When changing the time stamp, this means that it will change in steps
  2691. of N minutes, as given by the second value.
  2692. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2693. numbers should be factors of 60, so for example 5, 10, 15.
  2694. When this is larger than 1, you can still force an exact time stamp by using
  2695. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2696. and by using a prefix arg to `S-up/down' to specify the exact number
  2697. of minutes to shift."
  2698. :group 'org-time
  2699. :get (lambda (var) ; Make sure both elements are there
  2700. (if (integerp (default-value var))
  2701. (list (default-value var) 5)
  2702. (default-value var)))
  2703. :type '(list
  2704. (integer :tag "when inserting times")
  2705. (integer :tag "when modifying times")))
  2706. ;; Normalize old customizations of this variable.
  2707. (when (integerp org-time-stamp-rounding-minutes)
  2708. (setq org-time-stamp-rounding-minutes
  2709. (list org-time-stamp-rounding-minutes
  2710. org-time-stamp-rounding-minutes)))
  2711. (defcustom org-display-custom-times nil
  2712. "Non-nil means overlay custom formats over all time stamps.
  2713. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2714. To turn this on on a per-file basis, insert anywhere in the file:
  2715. #+STARTUP: customtime"
  2716. :group 'org-time
  2717. :set 'set-default
  2718. :type 'sexp)
  2719. (make-variable-buffer-local 'org-display-custom-times)
  2720. (defcustom org-time-stamp-custom-formats
  2721. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2722. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2723. These are overlaid over the default ISO format if the variable
  2724. `org-display-custom-times' is set. Time like %H:%M should be at the
  2725. end of the second format. The custom formats are also honored by export
  2726. commands, if custom time display is turned on at the time of export."
  2727. :group 'org-time
  2728. :type 'sexp)
  2729. (defun org-time-stamp-format (&optional long inactive)
  2730. "Get the right format for a time string."
  2731. (let ((f (if long (cdr org-time-stamp-formats)
  2732. (car org-time-stamp-formats))))
  2733. (if inactive
  2734. (concat "[" (substring f 1 -1) "]")
  2735. f)))
  2736. (defcustom org-time-clocksum-format
  2737. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2738. "The format string used when creating CLOCKSUM lines.
  2739. This is also used when Org mode generates a time duration.
  2740. The value can be a single format string containing two
  2741. %-sequences, which will be filled with the number of hours and
  2742. minutes in that order.
  2743. Alternatively, the value can be a plist associating any of the
  2744. keys :years, :months, :weeks, :days, :hours or :minutes with
  2745. format strings. The time duration is formatted using only the
  2746. time components that are needed and concatenating the results.
  2747. If a time unit in absent, it falls back to the next smallest
  2748. unit.
  2749. The keys :require-years, :require-months, :require-days,
  2750. :require-weeks, :require-hours, :require-minutes are also
  2751. meaningful. A non-nil value for these keys indicates that the
  2752. corresponding time component should always be included, even if
  2753. its value is 0.
  2754. For example,
  2755. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2756. :require-minutes t)
  2757. means durations longer than a day will be expressed in days,
  2758. hours and minutes, and durations less than a day will always be
  2759. expressed in hours and minutes (even for durations less than an
  2760. hour).
  2761. The value
  2762. \(:days \"%dd\" :minutes \"%dm\")
  2763. means durations longer than a day will be expressed in days and
  2764. minutes, and durations less than a day will be expressed entirely
  2765. in minutes (even for durations longer than an hour)."
  2766. :group 'org-time
  2767. :group 'org-clock
  2768. :version "24.4"
  2769. :package-version '(Org . "8.0")
  2770. :type '(choice (string :tag "Format string")
  2771. (set :tag "Plist"
  2772. (group :inline t (const :tag "Years" :years)
  2773. (string :tag "Format string"))
  2774. (group :inline t
  2775. (const :tag "Always show years" :require-years)
  2776. (const t))
  2777. (group :inline t (const :tag "Months" :months)
  2778. (string :tag "Format string"))
  2779. (group :inline t
  2780. (const :tag "Always show months" :require-months)
  2781. (const t))
  2782. (group :inline t (const :tag "Weeks" :weeks)
  2783. (string :tag "Format string"))
  2784. (group :inline t
  2785. (const :tag "Always show weeks" :require-weeks)
  2786. (const t))
  2787. (group :inline t (const :tag "Days" :days)
  2788. (string :tag "Format string"))
  2789. (group :inline t
  2790. (const :tag "Always show days" :require-days)
  2791. (const t))
  2792. (group :inline t (const :tag "Hours" :hours)
  2793. (string :tag "Format string"))
  2794. (group :inline t
  2795. (const :tag "Always show hours" :require-hours)
  2796. (const t))
  2797. (group :inline t (const :tag "Minutes" :minutes)
  2798. (string :tag "Format string"))
  2799. (group :inline t
  2800. (const :tag "Always show minutes" :require-minutes)
  2801. (const t)))))
  2802. (defcustom org-time-clocksum-use-fractional nil
  2803. "When non-nil, \\[org-clock-display] uses fractional times.
  2804. See `org-time-clocksum-format' for more on time clock formats."
  2805. :group 'org-time
  2806. :group 'org-clock
  2807. :version "24.3"
  2808. :type 'boolean)
  2809. (defcustom org-time-clocksum-use-effort-durations nil
  2810. "When non-nil, \\[org-clock-display] uses effort durations.
  2811. E.g. by default, one day is considered to be a 8 hours effort,
  2812. so a task that has been clocked for 16 hours will be displayed
  2813. as during 2 days in the clock display or in the clocktable.
  2814. See `org-effort-durations' on how to set effort durations
  2815. and `org-time-clocksum-format' for more on time clock formats."
  2816. :group 'org-time
  2817. :group 'org-clock
  2818. :version "24.4"
  2819. :package-version '(Org . "8.0")
  2820. :type 'boolean)
  2821. (defcustom org-time-clocksum-fractional-format "%.2f"
  2822. "The format string used when creating CLOCKSUM lines,
  2823. or when Org mode generates a time duration, if
  2824. `org-time-clocksum-use-fractional' is enabled.
  2825. The value can be a single format string containing one
  2826. %-sequence, which will be filled with the number of hours as
  2827. a float.
  2828. Alternatively, the value can be a plist associating any of the
  2829. keys :years, :months, :weeks, :days, :hours or :minutes with
  2830. a format string. The time duration is formatted using the
  2831. largest time unit which gives a non-zero integer part. If all
  2832. specified formats have zero integer part, the smallest time unit
  2833. is used."
  2834. :group 'org-time
  2835. :type '(choice (string :tag "Format string")
  2836. (set (group :inline t (const :tag "Years" :years)
  2837. (string :tag "Format string"))
  2838. (group :inline t (const :tag "Months" :months)
  2839. (string :tag "Format string"))
  2840. (group :inline t (const :tag "Weeks" :weeks)
  2841. (string :tag "Format string"))
  2842. (group :inline t (const :tag "Days" :days)
  2843. (string :tag "Format string"))
  2844. (group :inline t (const :tag "Hours" :hours)
  2845. (string :tag "Format string"))
  2846. (group :inline t (const :tag "Minutes" :minutes)
  2847. (string :tag "Format string")))))
  2848. (defcustom org-deadline-warning-days 14
  2849. "Number of days before expiration during which a deadline becomes active.
  2850. This variable governs the display in sparse trees and in the agenda.
  2851. When 0 or negative, it means use this number (the absolute value of it)
  2852. even if a deadline has a different individual lead time specified.
  2853. Custom commands can set this variable in the options section."
  2854. :group 'org-time
  2855. :group 'org-agenda-daily/weekly
  2856. :type 'integer)
  2857. (defcustom org-scheduled-delay-days 0
  2858. "Number of days before a scheduled item becomes active.
  2859. This variable governs the display in sparse trees and in the agenda.
  2860. The default value (i.e. 0) means: don't delay scheduled item.
  2861. When negative, it means use this number (the absolute value of it)
  2862. even if a scheduled item has a different individual delay time
  2863. specified.
  2864. Custom commands can set this variable in the options section."
  2865. :group 'org-time
  2866. :group 'org-agenda-daily/weekly
  2867. :version "24.4"
  2868. :package-version '(Org . "8.0")
  2869. :type 'integer)
  2870. (defcustom org-read-date-prefer-future t
  2871. "Non-nil means assume future for incomplete date input from user.
  2872. This affects the following situations:
  2873. 1. The user gives a month but not a year.
  2874. For example, if it is April and you enter \"feb 2\", this will be read
  2875. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2876. 2. The user gives a day, but no month.
  2877. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2878. read this as the third of *next* month. However, if you enter \"17\",
  2879. it will be considered as *this* month.
  2880. If you set this variable to the symbol `time', then also the following
  2881. will work:
  2882. 3. If the user gives a time.
  2883. If the time is before now, it will be interpreted as tomorrow.
  2884. Currently none of this works for ISO week specifications.
  2885. When this option is nil, the current day, month and year will always be
  2886. used as defaults.
  2887. See also `org-agenda-jump-prefer-future'."
  2888. :group 'org-time
  2889. :type '(choice
  2890. (const :tag "Never" nil)
  2891. (const :tag "Check month and day" t)
  2892. (const :tag "Check month, day, and time" time)))
  2893. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2894. "Should the agenda jump command prefer the future for incomplete dates?
  2895. The default is to do the same as configured in `org-read-date-prefer-future'.
  2896. But you can also set a deviating value here.
  2897. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2898. :group 'org-agenda
  2899. :group 'org-time
  2900. :version "24.1"
  2901. :type '(choice
  2902. (const :tag "Use org-read-date-prefer-future"
  2903. org-read-date-prefer-future)
  2904. (const :tag "Never" nil)
  2905. (const :tag "Always" t)))
  2906. (defcustom org-read-date-force-compatible-dates t
  2907. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2908. Depending on the system Emacs is running on, certain dates cannot
  2909. be represented with the type used internally to represent time.
  2910. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2911. correctly. Some systems allow for earlier dates, some for later,
  2912. some for both. One way to find out it to insert any date into an
  2913. Org buffer, putting the cursor on the year and hitting S-up and
  2914. S-down to test the range.
  2915. When this variable is set to t, the date/time prompt will not let
  2916. you specify dates outside the 1970-2037 range, so it is certain that
  2917. these dates will work in whatever version of Emacs you are
  2918. running, and also that you can move a file from one Emacs implementation
  2919. to another. WHenever Org is forcing the year for you, it will display
  2920. a message and beep.
  2921. When this variable is nil, Org will check if the date is
  2922. representable in the specific Emacs implementation you are using.
  2923. If not, it will force a year, usually the current year, and beep
  2924. to remind you. Currently this setting is not recommended because
  2925. the likelihood that you will open your Org files in an Emacs that
  2926. has limited date range is not negligible.
  2927. A workaround for this problem is to use diary sexp dates for time
  2928. stamps outside of this range."
  2929. :group 'org-time
  2930. :version "24.1"
  2931. :type 'boolean)
  2932. (defcustom org-read-date-display-live t
  2933. "Non-nil means display current interpretation of date prompt live.
  2934. This display will be in an overlay, in the minibuffer."
  2935. :group 'org-time
  2936. :type 'boolean)
  2937. (defcustom org-read-date-popup-calendar t
  2938. "Non-nil means pop up a calendar when prompting for a date.
  2939. In the calendar, the date can be selected with mouse-1. However, the
  2940. minibuffer will also be active, and you can simply enter the date as well.
  2941. When nil, only the minibuffer will be available."
  2942. :group 'org-time
  2943. :type 'boolean)
  2944. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2945. 'org-read-date-popup-calendar)
  2946. (make-obsolete-variable
  2947. 'org-read-date-minibuffer-setup-hook
  2948. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2949. (defcustom org-read-date-minibuffer-setup-hook nil
  2950. "Hook to be used to set up keys for the date/time interface.
  2951. Add key definitions to `minibuffer-local-map', which will be a
  2952. temporary copy.
  2953. WARNING: This option is obsolete, you should use
  2954. `org-read-date-minibuffer-local-map' to set up keys."
  2955. :group 'org-time
  2956. :type 'hook)
  2957. (defcustom org-extend-today-until 0
  2958. "The hour when your day really ends. Must be an integer.
  2959. This has influence for the following applications:
  2960. - When switching the agenda to \"today\". It it is still earlier than
  2961. the time given here, the day recognized as TODAY is actually yesterday.
  2962. - When a date is read from the user and it is still before the time given
  2963. here, the current date and time will be assumed to be yesterday, 23:59.
  2964. Also, timestamps inserted in capture templates follow this rule.
  2965. IMPORTANT: This is a feature whose implementation is and likely will
  2966. remain incomplete. Really, it is only here because past midnight seems to
  2967. be the favorite working time of John Wiegley :-)"
  2968. :group 'org-time
  2969. :type 'integer)
  2970. (defcustom org-use-effective-time nil
  2971. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2972. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2973. \"effective time\" of any timestamps between midnight and 8am will be
  2974. 23:59 of the previous day."
  2975. :group 'org-time
  2976. :version "24.1"
  2977. :type 'boolean)
  2978. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2979. "When non-nil, use the last clock out time for `org-todo'.
  2980. Note that this option has precedence over the combined use of
  2981. `org-use-effective-time' and `org-extend-today-until'."
  2982. :group 'org-time
  2983. :version "24.4"
  2984. :package-version '(Org . "8.0")
  2985. :type 'boolean)
  2986. (defcustom org-edit-timestamp-down-means-later nil
  2987. "Non-nil means S-down will increase the time in a time stamp.
  2988. When nil, S-up will increase."
  2989. :group 'org-time
  2990. :type 'boolean)
  2991. (defcustom org-calendar-follow-timestamp-change t
  2992. "Non-nil means make the calendar window follow timestamp changes.
  2993. When a timestamp is modified and the calendar window is visible, it will be
  2994. moved to the new date."
  2995. :group 'org-time
  2996. :type 'boolean)
  2997. (defgroup org-tags nil
  2998. "Options concerning tags in Org-mode."
  2999. :tag "Org Tags"
  3000. :group 'org)
  3001. (defcustom org-tag-alist nil
  3002. "List of tags allowed in Org-mode files.
  3003. When this list is nil, Org-mode will base TAG input on what is already in the
  3004. buffer.
  3005. The value of this variable is an alist, the car of each entry must be a
  3006. keyword as a string, the cdr may be a character that is used to select
  3007. that tag through the fast-tag-selection interface.
  3008. See the manual for details."
  3009. :group 'org-tags
  3010. :type '(repeat
  3011. (choice
  3012. (cons (string :tag "Tag name")
  3013. (character :tag "Access char"))
  3014. (list :tag "Start radio group"
  3015. (const :startgroup)
  3016. (option (string :tag "Group description")))
  3017. (list :tag "Group tags delimiter"
  3018. (const :grouptags))
  3019. (list :tag "End radio group"
  3020. (const :endgroup)
  3021. (option (string :tag "Group description")))
  3022. (const :tag "New line" (:newline)))))
  3023. (defcustom org-tag-persistent-alist nil
  3024. "List of tags that will always appear in all Org-mode files.
  3025. This is in addition to any in buffer settings or customizations
  3026. of `org-tag-alist'.
  3027. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3028. The value of this variable is an alist, the car of each entry must be a
  3029. keyword as a string, the cdr may be a character that is used to select
  3030. that tag through the fast-tag-selection interface.
  3031. See the manual for details.
  3032. To disable these tags on a per-file basis, insert anywhere in the file:
  3033. #+STARTUP: noptag"
  3034. :group 'org-tags
  3035. :type '(repeat
  3036. (choice
  3037. (cons (string :tag "Tag name")
  3038. (character :tag "Access char"))
  3039. (const :tag "Start radio group" (:startgroup))
  3040. (const :tag "Group tags delimiter" (:grouptags))
  3041. (const :tag "End radio group" (:endgroup))
  3042. (const :tag "New line" (:newline)))))
  3043. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3044. "If non-nil, always offer completion for all tags of all agenda files.
  3045. Instead of customizing this variable directly, you might want to
  3046. set it locally for capture buffers, because there no list of
  3047. tags in that file can be created dynamically (there are none).
  3048. (add-hook 'org-capture-mode-hook
  3049. (lambda ()
  3050. (set (make-local-variable
  3051. 'org-complete-tags-always-offer-all-agenda-tags)
  3052. t)))"
  3053. :group 'org-tags
  3054. :version "24.1"
  3055. :type 'boolean)
  3056. (defvar org-file-tags nil
  3057. "List of tags that can be inherited by all entries in the file.
  3058. The tags will be inherited if the variable `org-use-tag-inheritance'
  3059. says they should be.
  3060. This variable is populated from #+FILETAGS lines.")
  3061. (defcustom org-use-fast-tag-selection 'auto
  3062. "Non-nil means use fast tag selection scheme.
  3063. This is a special interface to select and deselect tags with single keys.
  3064. When nil, fast selection is never used.
  3065. When the symbol `auto', fast selection is used if and only if selection
  3066. characters for tags have been configured, either through the variable
  3067. `org-tag-alist' or through a #+TAGS line in the buffer.
  3068. When t, fast selection is always used and selection keys are assigned
  3069. automatically if necessary."
  3070. :group 'org-tags
  3071. :type '(choice
  3072. (const :tag "Always" t)
  3073. (const :tag "Never" nil)
  3074. (const :tag "When selection characters are configured" auto)))
  3075. (defcustom org-fast-tag-selection-single-key nil
  3076. "Non-nil means fast tag selection exits after first change.
  3077. When nil, you have to press RET to exit it.
  3078. During fast tag selection, you can toggle this flag with `C-c'.
  3079. This variable can also have the value `expert'. In this case, the window
  3080. displaying the tags menu is not even shown, until you press C-c again."
  3081. :group 'org-tags
  3082. :type '(choice
  3083. (const :tag "No" nil)
  3084. (const :tag "Yes" t)
  3085. (const :tag "Expert" expert)))
  3086. (defvar org-fast-tag-selection-include-todo nil
  3087. "Non-nil means fast tags selection interface will also offer TODO states.
  3088. This is an undocumented feature, you should not rely on it.")
  3089. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3090. "The column to which tags should be indented in a headline.
  3091. If this number is positive, it specifies the column. If it is negative,
  3092. it means that the tags should be flushright to that column. For example,
  3093. -80 works well for a normal 80 character screen.
  3094. When 0, place tags directly after headline text, with only one space in
  3095. between."
  3096. :group 'org-tags
  3097. :type 'integer)
  3098. (defcustom org-auto-align-tags t
  3099. "Non-nil keeps tags aligned when modifying headlines.
  3100. Some operations (i.e. demoting) change the length of a headline and
  3101. therefore shift the tags around. With this option turned on, after
  3102. each such operation the tags are again aligned to `org-tags-column'."
  3103. :group 'org-tags
  3104. :type 'boolean)
  3105. (defcustom org-use-tag-inheritance t
  3106. "Non-nil means tags in levels apply also for sublevels.
  3107. When nil, only the tags directly given in a specific line apply there.
  3108. This may also be a list of tags that should be inherited, or a regexp that
  3109. matches tags that should be inherited. Additional control is possible
  3110. with the variable `org-tags-exclude-from-inheritance' which gives an
  3111. explicit list of tags to be excluded from inheritance, even if the value of
  3112. `org-use-tag-inheritance' would select it for inheritance.
  3113. If this option is t, a match early-on in a tree can lead to a large
  3114. number of matches in the subtree when constructing the agenda or creating
  3115. a sparse tree. If you only want to see the first match in a tree during
  3116. a search, check out the variable `org-tags-match-list-sublevels'."
  3117. :group 'org-tags
  3118. :type '(choice
  3119. (const :tag "Not" nil)
  3120. (const :tag "Always" t)
  3121. (repeat :tag "Specific tags" (string :tag "Tag"))
  3122. (regexp :tag "Tags matched by regexp")))
  3123. (defcustom org-tags-exclude-from-inheritance nil
  3124. "List of tags that should never be inherited.
  3125. This is a way to exclude a few tags from inheritance. For way to do
  3126. the opposite, to actively allow inheritance for selected tags,
  3127. see the variable `org-use-tag-inheritance'."
  3128. :group 'org-tags
  3129. :type '(repeat (string :tag "Tag")))
  3130. (defun org-tag-inherit-p (tag)
  3131. "Check if TAG is one that should be inherited."
  3132. (cond
  3133. ((member tag org-tags-exclude-from-inheritance) nil)
  3134. ((eq org-use-tag-inheritance t) t)
  3135. ((not org-use-tag-inheritance) nil)
  3136. ((stringp org-use-tag-inheritance)
  3137. (string-match org-use-tag-inheritance tag))
  3138. ((listp org-use-tag-inheritance)
  3139. (member tag org-use-tag-inheritance))
  3140. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3141. (defcustom org-tags-match-list-sublevels t
  3142. "Non-nil means list also sublevels of headlines matching a search.
  3143. This variable applies to tags/property searches, and also to stuck
  3144. projects because this search is based on a tags match as well.
  3145. When set to the symbol `indented', sublevels are indented with
  3146. leading dots.
  3147. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3148. the sublevels of a headline matching a tag search often also match
  3149. the same search. Listing all of them can create very long lists.
  3150. Setting this variable to nil causes subtrees of a match to be skipped.
  3151. This variable is semi-obsolete and probably should always be true. It
  3152. is better to limit inheritance to certain tags using the variables
  3153. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3154. :group 'org-tags
  3155. :type '(choice
  3156. (const :tag "No, don't list them" nil)
  3157. (const :tag "Yes, do list them" t)
  3158. (const :tag "List them, indented with leading dots" indented)))
  3159. (defcustom org-tags-sort-function nil
  3160. "When set, tags are sorted using this function as a comparator."
  3161. :group 'org-tags
  3162. :type '(choice
  3163. (const :tag "No sorting" nil)
  3164. (const :tag "Alphabetical" string<)
  3165. (const :tag "Reverse alphabetical" string>)
  3166. (function :tag "Custom function" nil)))
  3167. (defvar org-tags-history nil
  3168. "History of minibuffer reads for tags.")
  3169. (defvar org-last-tags-completion-table nil
  3170. "The last used completion table for tags.")
  3171. (defvar org-after-tags-change-hook nil
  3172. "Hook that is run after the tags in a line have changed.")
  3173. (defgroup org-properties nil
  3174. "Options concerning properties in Org-mode."
  3175. :tag "Org Properties"
  3176. :group 'org)
  3177. (defcustom org-property-format "%-10s %s"
  3178. "How property key/value pairs should be formatted by `indent-line'.
  3179. When `indent-line' hits a property definition, it will format the line
  3180. according to this format, mainly to make sure that the values are
  3181. lined-up with respect to each other."
  3182. :group 'org-properties
  3183. :type 'string)
  3184. (defcustom org-properties-postprocess-alist nil
  3185. "Alist of properties and functions to adjust inserted values.
  3186. Elements of this alist must be of the form
  3187. ([string] [function])
  3188. where [string] must be a property name and [function] must be a
  3189. lambda expression: this lambda expression must take one argument,
  3190. the value to adjust, and return the new value as a string.
  3191. For example, this element will allow the property \"Remaining\"
  3192. to be updated wrt the relation between the \"Effort\" property
  3193. and the clock summary:
  3194. ((\"Remaining\" (lambda(value)
  3195. (let ((clocksum (org-clock-sum-current-item))
  3196. (effort (org-duration-string-to-minutes
  3197. (org-entry-get (point) \"Effort\"))))
  3198. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3199. :group 'org-properties
  3200. :version "24.1"
  3201. :type '(alist :key-type (string :tag "Property")
  3202. :value-type (function :tag "Function")))
  3203. (defcustom org-use-property-inheritance nil
  3204. "Non-nil means properties apply also for sublevels.
  3205. This setting is chiefly used during property searches. Turning it on can
  3206. cause significant overhead when doing a search, which is why it is not
  3207. on by default.
  3208. When nil, only the properties directly given in the current entry count.
  3209. When t, every property is inherited. The value may also be a list of
  3210. properties that should have inheritance, or a regular expression matching
  3211. properties that should be inherited.
  3212. However, note that some special properties use inheritance under special
  3213. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3214. and the properties ending in \"_ALL\" when they are used as descriptor
  3215. for valid values of a property.
  3216. Note for programmers:
  3217. When querying an entry with `org-entry-get', you can control if inheritance
  3218. should be used. By default, `org-entry-get' looks only at the local
  3219. properties. You can request inheritance by setting the inherit argument
  3220. to t (to force inheritance) or to `selective' (to respect the setting
  3221. in this variable)."
  3222. :group 'org-properties
  3223. :type '(choice
  3224. (const :tag "Not" nil)
  3225. (const :tag "Always" t)
  3226. (repeat :tag "Specific properties" (string :tag "Property"))
  3227. (regexp :tag "Properties matched by regexp")))
  3228. (defun org-property-inherit-p (property)
  3229. "Check if PROPERTY is one that should be inherited."
  3230. (cond
  3231. ((eq org-use-property-inheritance t) t)
  3232. ((not org-use-property-inheritance) nil)
  3233. ((stringp org-use-property-inheritance)
  3234. (string-match org-use-property-inheritance property))
  3235. ((listp org-use-property-inheritance)
  3236. (member property org-use-property-inheritance))
  3237. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3238. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3239. "The default column format, if no other format has been defined.
  3240. This variable can be set on the per-file basis by inserting a line
  3241. #+COLUMNS: %25ITEM ....."
  3242. :group 'org-properties
  3243. :type 'string)
  3244. (defcustom org-columns-ellipses ".."
  3245. "The ellipses to be used when a field in column view is truncated.
  3246. When this is the empty string, as many characters as possible are shown,
  3247. but then there will be no visual indication that the field has been truncated.
  3248. When this is a string of length N, the last N characters of a truncated
  3249. field are replaced by this string. If the column is narrower than the
  3250. ellipses string, only part of the ellipses string will be shown."
  3251. :group 'org-properties
  3252. :type 'string)
  3253. (defcustom org-columns-modify-value-for-display-function nil
  3254. "Function that modifies values for display in column view.
  3255. For example, it can be used to cut out a certain part from a time stamp.
  3256. The function must take 2 arguments:
  3257. column-title The title of the column (*not* the property name)
  3258. value The value that should be modified.
  3259. The function should return the value that should be displayed,
  3260. or nil if the normal value should be used."
  3261. :group 'org-properties
  3262. :type '(choice (const nil) (function)))
  3263. (defconst org-global-properties-fixed
  3264. '(("VISIBILITY_ALL" . "folded children content all")
  3265. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3266. "List of property/value pairs that can be inherited by any entry.
  3267. These are fixed values, for the preset properties. The user variable
  3268. that can be used to add to this list is `org-global-properties'.
  3269. The entries in this list are cons cells where the car is a property
  3270. name and cdr is a string with the value. If the value represents
  3271. multiple items like an \"_ALL\" property, separate the items by
  3272. spaces.")
  3273. (defcustom org-global-properties nil
  3274. "List of property/value pairs that can be inherited by any entry.
  3275. This list will be combined with the constant `org-global-properties-fixed'.
  3276. The entries in this list are cons cells where the car is a property
  3277. name and cdr is a string with the value.
  3278. You can set buffer-local values for the same purpose in the variable
  3279. `org-file-properties' this by adding lines like
  3280. #+PROPERTY: NAME VALUE"
  3281. :group 'org-properties
  3282. :type '(repeat
  3283. (cons (string :tag "Property")
  3284. (string :tag "Value"))))
  3285. (defvar org-file-properties nil
  3286. "List of property/value pairs that can be inherited by any entry.
  3287. Valid for the current buffer.
  3288. This variable is populated from #+PROPERTY lines.")
  3289. (make-variable-buffer-local 'org-file-properties)
  3290. (defgroup org-agenda nil
  3291. "Options concerning agenda views in Org-mode."
  3292. :tag "Org Agenda"
  3293. :group 'org)
  3294. (defvar org-category nil
  3295. "Variable used by org files to set a category for agenda display.
  3296. Such files should use a file variable to set it, for example
  3297. # -*- mode: org; org-category: \"ELisp\"
  3298. or contain a special line
  3299. #+CATEGORY: ELisp
  3300. If the file does not specify a category, then file's base name
  3301. is used instead.")
  3302. (make-variable-buffer-local 'org-category)
  3303. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3304. (defcustom org-agenda-files nil
  3305. "The files to be used for agenda display.
  3306. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3307. \\[org-remove-file]. You can also use customize to edit the list.
  3308. If an entry is a directory, all files in that directory that are matched by
  3309. `org-agenda-file-regexp' will be part of the file list.
  3310. If the value of the variable is not a list but a single file name, then
  3311. the list of agenda files is actually stored and maintained in that file, one
  3312. agenda file per line. In this file paths can be given relative to
  3313. `org-directory'. Tilde expansion and environment variable substitution
  3314. are also made."
  3315. :group 'org-agenda
  3316. :type '(choice
  3317. (repeat :tag "List of files and directories" file)
  3318. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3319. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3320. "Regular expression to match files for `org-agenda-files'.
  3321. If any element in the list in that variable contains a directory instead
  3322. of a normal file, all files in that directory that are matched by this
  3323. regular expression will be included."
  3324. :group 'org-agenda
  3325. :type 'regexp)
  3326. (defcustom org-agenda-text-search-extra-files nil
  3327. "List of extra files to be searched by text search commands.
  3328. These files will be searched in addition to the agenda files by the
  3329. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3330. Note that these files will only be searched for text search commands,
  3331. not for the other agenda views like todo lists, tag searches or the weekly
  3332. agenda. This variable is intended to list notes and possibly archive files
  3333. that should also be searched by these two commands.
  3334. In fact, if the first element in the list is the symbol `agenda-archives',
  3335. then all archive files of all agenda files will be added to the search
  3336. scope."
  3337. :group 'org-agenda
  3338. :type '(set :greedy t
  3339. (const :tag "Agenda Archives" agenda-archives)
  3340. (repeat :inline t (file))))
  3341. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3342. 'org-agenda-text-search-extra-files)
  3343. (defcustom org-agenda-skip-unavailable-files nil
  3344. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3345. A nil value means to remove them, after a query, from the list."
  3346. :group 'org-agenda
  3347. :type 'boolean)
  3348. (defcustom org-calendar-to-agenda-key [?c]
  3349. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3350. The command `org-calendar-goto-agenda' will be bound to this key. The
  3351. default is the character `c' because then `c' can be used to switch back and
  3352. forth between agenda and calendar."
  3353. :group 'org-agenda
  3354. :type 'sexp)
  3355. (defcustom org-calendar-insert-diary-entry-key [?i]
  3356. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3357. This option is irrelevant until `org-agenda-diary-file' has been configured
  3358. to point to an Org-mode file. When that is the case, the command
  3359. `org-agenda-diary-entry' will be bound to the key given here, by default
  3360. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3361. if you want to continue doing this, you need to change this to a different
  3362. key."
  3363. :group 'org-agenda
  3364. :type 'sexp)
  3365. (defcustom org-agenda-diary-file 'diary-file
  3366. "File to which to add new entries with the `i' key in agenda and calendar.
  3367. When this is the symbol `diary-file', the functionality in the Emacs
  3368. calendar will be used to add entries to the `diary-file'. But when this
  3369. points to a file, `org-agenda-diary-entry' will be used instead."
  3370. :group 'org-agenda
  3371. :type '(choice
  3372. (const :tag "The standard Emacs diary file" diary-file)
  3373. (file :tag "Special Org file diary entries")))
  3374. (eval-after-load "calendar"
  3375. '(progn
  3376. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3377. 'org-calendar-goto-agenda)
  3378. (add-hook 'calendar-mode-hook
  3379. (lambda ()
  3380. (unless (eq org-agenda-diary-file 'diary-file)
  3381. (define-key calendar-mode-map
  3382. org-calendar-insert-diary-entry-key
  3383. 'org-agenda-diary-entry))))))
  3384. (defgroup org-latex nil
  3385. "Options for embedding LaTeX code into Org-mode."
  3386. :tag "Org LaTeX"
  3387. :group 'org)
  3388. (defcustom org-format-latex-options
  3389. '(:foreground default :background default :scale 1.0
  3390. :html-foreground "Black" :html-background "Transparent"
  3391. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3392. "Options for creating images from LaTeX fragments.
  3393. This is a property list with the following properties:
  3394. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3395. `default' means use the foreground of the default face.
  3396. `auto' means use the foreground from the text face.
  3397. :background the background color, or \"Transparent\".
  3398. `default' means use the background of the default face.
  3399. `auto' means use the background from the text face.
  3400. :scale a scaling factor for the size of the images, to get more pixels
  3401. :html-foreground, :html-background, :html-scale
  3402. the same numbers for HTML export.
  3403. :matchers a list indicating which matchers should be used to
  3404. find LaTeX fragments. Valid members of this list are:
  3405. \"begin\" find environments
  3406. \"$1\" find single characters surrounded by $.$
  3407. \"$\" find math expressions surrounded by $...$
  3408. \"$$\" find math expressions surrounded by $$....$$
  3409. \"\\(\" find math expressions surrounded by \\(...\\)
  3410. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3411. :group 'org-latex
  3412. :type 'plist)
  3413. (defcustom org-format-latex-signal-error t
  3414. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3415. When nil, just push out a message."
  3416. :group 'org-latex
  3417. :version "24.1"
  3418. :type 'boolean)
  3419. (defcustom org-latex-to-mathml-jar-file nil
  3420. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3421. Use this to specify additional executable file say a jar file.
  3422. When using MathToWeb as the converter, specify the full-path to
  3423. your mathtoweb.jar file."
  3424. :group 'org-latex
  3425. :version "24.1"
  3426. :type '(choice
  3427. (const :tag "None" nil)
  3428. (file :tag "JAR file" :must-match t)))
  3429. (defcustom org-latex-to-mathml-convert-command nil
  3430. "Command to convert LaTeX fragments to MathML.
  3431. Replace format-specifiers in the command as noted below and use
  3432. `shell-command' to convert LaTeX to MathML.
  3433. %j: Executable file in fully expanded form as specified by
  3434. `org-latex-to-mathml-jar-file'.
  3435. %I: Input LaTeX file in fully expanded form
  3436. %o: Output MathML file
  3437. This command is used by `org-create-math-formula'.
  3438. When using MathToWeb as the converter, set this to
  3439. \"java -jar %j -unicode -force -df %o %I\"."
  3440. :group 'org-latex
  3441. :version "24.1"
  3442. :type '(choice
  3443. (const :tag "None" nil)
  3444. (string :tag "\nShell command")))
  3445. (defcustom org-latex-create-formula-image-program 'dvipng
  3446. "Program to convert LaTeX fragments with.
  3447. dvipng Process the LaTeX fragments to dvi file, then convert
  3448. dvi files to png files using dvipng.
  3449. This will also include processing of non-math environments.
  3450. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3451. to convert pdf files to png files"
  3452. :group 'org-latex
  3453. :version "24.1"
  3454. :type '(choice
  3455. (const :tag "dvipng" dvipng)
  3456. (const :tag "imagemagick" imagemagick)))
  3457. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3458. "Path to store latex preview images.
  3459. A relative path here creates many directories relative to the
  3460. processed org files paths. An absolute path puts all preview
  3461. images at the same place."
  3462. :group 'org-latex
  3463. :version "24.3"
  3464. :type 'string)
  3465. (defun org-format-latex-mathml-available-p ()
  3466. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3467. (save-match-data
  3468. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3469. org-latex-to-mathml-convert-command)
  3470. (let ((executable (car (split-string
  3471. org-latex-to-mathml-convert-command))))
  3472. (when (executable-find executable)
  3473. (if (string-match
  3474. "%j" org-latex-to-mathml-convert-command)
  3475. (file-readable-p org-latex-to-mathml-jar-file)
  3476. t))))))
  3477. (defcustom org-format-latex-header "\\documentclass{article}
  3478. \\usepackage[usenames]{color}
  3479. \[PACKAGES]
  3480. \[DEFAULT-PACKAGES]
  3481. \\pagestyle{empty} % do not remove
  3482. % The settings below are copied from fullpage.sty
  3483. \\setlength{\\textwidth}{\\paperwidth}
  3484. \\addtolength{\\textwidth}{-3cm}
  3485. \\setlength{\\oddsidemargin}{1.5cm}
  3486. \\addtolength{\\oddsidemargin}{-2.54cm}
  3487. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3488. \\setlength{\\textheight}{\\paperheight}
  3489. \\addtolength{\\textheight}{-\\headheight}
  3490. \\addtolength{\\textheight}{-\\headsep}
  3491. \\addtolength{\\textheight}{-\\footskip}
  3492. \\addtolength{\\textheight}{-3cm}
  3493. \\setlength{\\topmargin}{1.5cm}
  3494. \\addtolength{\\topmargin}{-2.54cm}"
  3495. "The document header used for processing LaTeX fragments.
  3496. It is imperative that this header make sure that no page number
  3497. appears on the page. The package defined in the variables
  3498. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3499. will either replace the placeholder \"[PACKAGES]\" in this
  3500. header, or they will be appended."
  3501. :group 'org-latex
  3502. :type 'string)
  3503. (defun org-set-packages-alist (var val)
  3504. "Set the packages alist and make sure it has 3 elements per entry."
  3505. (set var (mapcar (lambda (x)
  3506. (if (and (consp x) (= (length x) 2))
  3507. (list (car x) (nth 1 x) t)
  3508. x))
  3509. val)))
  3510. (defun org-get-packages-alist (var)
  3511. "Get the packages alist and make sure it has 3 elements per entry."
  3512. (mapcar (lambda (x)
  3513. (if (and (consp x) (= (length x) 2))
  3514. (list (car x) (nth 1 x) t)
  3515. x))
  3516. (default-value var)))
  3517. (defcustom org-latex-default-packages-alist
  3518. '(("AUTO" "inputenc" t)
  3519. ("T1" "fontenc" t)
  3520. ("" "fixltx2e" nil)
  3521. ("" "graphicx" t)
  3522. ("" "longtable" nil)
  3523. ("" "float" nil)
  3524. ("" "wrapfig" nil)
  3525. ("" "rotating" nil)
  3526. ("normalem" "ulem" t)
  3527. ("" "amsmath" t)
  3528. ("" "textcomp" t)
  3529. ("" "marvosym" t)
  3530. ("" "wasysym" t)
  3531. ("" "amssymb" t)
  3532. ("" "capt-of" nil)
  3533. ("" "hyperref" nil)
  3534. "\\tolerance=1000")
  3535. "Alist of default packages to be inserted in the header.
  3536. Change this only if one of the packages here causes an
  3537. incompatibility with another package you are using.
  3538. The packages in this list are needed by one part or another of
  3539. Org mode to function properly:
  3540. - inputenc, fontenc: for basic font and character selection
  3541. - fixltx2e: Important patches of LaTeX itself
  3542. - graphicx: for including images
  3543. - longtable: For multipage tables
  3544. - float, wrapfig: for figure placement
  3545. - rotating: for sideways figures and tables
  3546. - ulem: for underline and strike-through
  3547. - amsmath: for subscript and superscript and math environments
  3548. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3549. for interpreting the entities in `org-entities'. You can skip
  3550. some of these packages if you don't use any of their symbols.
  3551. - capt-of: for captions outside of floats
  3552. - hyperref: for cross references
  3553. Therefore you should not modify this variable unless you know
  3554. what you are doing. The one reason to change it anyway is that
  3555. you might be loading some other package that conflicts with one
  3556. of the default packages. Each element is either a cell or
  3557. a string.
  3558. A cell is of the format:
  3559. \( \"options\" \"package\" SNIPPET-FLAG).
  3560. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3561. when compiling LaTeX snippets into images for inclusion into
  3562. non-LaTeX output.
  3563. A string will be inserted as-is in the header of the document."
  3564. :group 'org-latex
  3565. :group 'org-export-latex
  3566. :set 'org-set-packages-alist
  3567. :get 'org-get-packages-alist
  3568. :version "24.1"
  3569. :type '(repeat
  3570. (choice
  3571. (list :tag "options/package pair"
  3572. (string :tag "options")
  3573. (string :tag "package")
  3574. (boolean :tag "Snippet"))
  3575. (string :tag "A line of LaTeX"))))
  3576. (defcustom org-latex-packages-alist nil
  3577. "Alist of packages to be inserted in every LaTeX header.
  3578. These will be inserted after `org-latex-default-packages-alist'.
  3579. Each element is either a cell or a string.
  3580. A cell is of the format:
  3581. \(\"options\" \"package\" SNIPPET-FLAG)
  3582. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3583. needed when turning LaTeX snippets into images for inclusion into
  3584. non-LaTeX output.
  3585. A string will be inserted as-is in the header of the document.
  3586. Make sure that you only list packages here which:
  3587. - you want in every file;
  3588. - do not conflict with the setup in `org-format-latex-header';
  3589. - do not conflict with the default packages in
  3590. `org-latex-default-packages-alist'."
  3591. :group 'org-latex
  3592. :group 'org-export-latex
  3593. :set 'org-set-packages-alist
  3594. :get 'org-get-packages-alist
  3595. :type '(repeat
  3596. (choice
  3597. (list :tag "options/package pair"
  3598. (string :tag "options")
  3599. (string :tag "package")
  3600. (boolean :tag "Snippet"))
  3601. (string :tag "A line of LaTeX"))))
  3602. (defgroup org-appearance nil
  3603. "Settings for Org-mode appearance."
  3604. :tag "Org Appearance"
  3605. :group 'org)
  3606. (defcustom org-level-color-stars-only nil
  3607. "Non-nil means fontify only the stars in each headline.
  3608. When nil, the entire headline is fontified.
  3609. Changing it requires restart of `font-lock-mode' to become effective
  3610. also in regions already fontified."
  3611. :group 'org-appearance
  3612. :type 'boolean)
  3613. (defcustom org-hide-leading-stars nil
  3614. "Non-nil means hide the first N-1 stars in a headline.
  3615. This works by using the face `org-hide' for these stars. This
  3616. face is white for a light background, and black for a dark
  3617. background. You may have to customize the face `org-hide' to
  3618. make this work.
  3619. Changing it requires restart of `font-lock-mode' to become effective
  3620. also in regions already fontified.
  3621. You may also set this on a per-file basis by adding one of the following
  3622. lines to the buffer:
  3623. #+STARTUP: hidestars
  3624. #+STARTUP: showstars"
  3625. :group 'org-appearance
  3626. :type 'boolean)
  3627. (defcustom org-hidden-keywords nil
  3628. "List of symbols corresponding to keywords to be hidden the org buffer.
  3629. For example, a value '(title) for this list will make the document's title
  3630. appear in the buffer without the initial #+TITLE: keyword."
  3631. :group 'org-appearance
  3632. :version "24.1"
  3633. :type '(set (const :tag "#+AUTHOR" author)
  3634. (const :tag "#+DATE" date)
  3635. (const :tag "#+EMAIL" email)
  3636. (const :tag "#+TITLE" title)))
  3637. (defcustom org-custom-properties nil
  3638. "List of properties (as strings) with a special meaning.
  3639. The default use of these custom properties is to let the user
  3640. hide them with `org-toggle-custom-properties-visibility'."
  3641. :group 'org-properties
  3642. :group 'org-appearance
  3643. :version "24.3"
  3644. :type '(repeat (string :tag "Property Name")))
  3645. (defcustom org-fontify-done-headline nil
  3646. "Non-nil means change the face of a headline if it is marked DONE.
  3647. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3648. When this is non-nil, the headline after the keyword is set to the
  3649. `org-headline-done' as an additional indication."
  3650. :group 'org-appearance
  3651. :type 'boolean)
  3652. (defcustom org-fontify-emphasized-text t
  3653. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3654. Changing this variable requires a restart of Emacs to take effect."
  3655. :group 'org-appearance
  3656. :type 'boolean)
  3657. (defcustom org-fontify-whole-heading-line nil
  3658. "Non-nil means fontify the whole line for headings.
  3659. This is useful when setting a background color for the
  3660. org-level-* faces."
  3661. :group 'org-appearance
  3662. :type 'boolean)
  3663. (defcustom org-highlight-latex-and-related nil
  3664. "Non-nil means highlight LaTeX related syntax in the buffer.
  3665. When non nil, the value should be a list containing any of the
  3666. following symbols:
  3667. `latex' Highlight LaTeX snippets and environments.
  3668. `script' Highlight subscript and superscript.
  3669. `entities' Highlight entities."
  3670. :group 'org-appearance
  3671. :version "24.4"
  3672. :package-version '(Org . "8.0")
  3673. :type '(choice
  3674. (const :tag "No highlighting" nil)
  3675. (set :greedy t :tag "Highlight"
  3676. (const :tag "LaTeX snippets and environments" latex)
  3677. (const :tag "Subscript and superscript" script)
  3678. (const :tag "Entities" entities))))
  3679. (defcustom org-hide-emphasis-markers nil
  3680. "Non-nil mean font-lock should hide the emphasis marker characters."
  3681. :group 'org-appearance
  3682. :type 'boolean)
  3683. (defcustom org-hide-macro-markers nil
  3684. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3685. :group 'org-appearance
  3686. :type 'boolean)
  3687. (defcustom org-pretty-entities nil
  3688. "Non-nil means show entities as UTF8 characters.
  3689. When nil, the \\name form remains in the buffer."
  3690. :group 'org-appearance
  3691. :version "24.1"
  3692. :type 'boolean)
  3693. (defcustom org-pretty-entities-include-sub-superscripts t
  3694. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3695. :group 'org-appearance
  3696. :version "24.1"
  3697. :type 'boolean)
  3698. (defvar org-emph-re nil
  3699. "Regular expression for matching emphasis.
  3700. After a match, the match groups contain these elements:
  3701. 0 The match of the full regular expression, including the characters
  3702. before and after the proper match
  3703. 1 The character before the proper match, or empty at beginning of line
  3704. 2 The proper match, including the leading and trailing markers
  3705. 3 The leading marker like * or /, indicating the type of highlighting
  3706. 4 The text between the emphasis markers, not including the markers
  3707. 5 The character after the match, empty at the end of a line")
  3708. (defvar org-verbatim-re nil
  3709. "Regular expression for matching verbatim text.")
  3710. (defvar org-emphasis-regexp-components) ; defined just below
  3711. (defvar org-emphasis-alist) ; defined just below
  3712. (defun org-set-emph-re (var val)
  3713. "Set variable and compute the emphasis regular expression."
  3714. (set var val)
  3715. (when (and (boundp 'org-emphasis-alist)
  3716. (boundp 'org-emphasis-regexp-components)
  3717. org-emphasis-alist org-emphasis-regexp-components)
  3718. (let* ((e org-emphasis-regexp-components)
  3719. (pre (car e))
  3720. (post (nth 1 e))
  3721. (border (nth 2 e))
  3722. (body (nth 3 e))
  3723. (nl (nth 4 e))
  3724. (body1 (concat body "*?"))
  3725. (markers (mapconcat 'car org-emphasis-alist ""))
  3726. (vmarkers (mapconcat
  3727. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3728. org-emphasis-alist "")))
  3729. ;; make sure special characters appear at the right position in the class
  3730. (if (string-match "\\^" markers)
  3731. (setq markers (concat (replace-match "" t t markers) "^")))
  3732. (if (string-match "-" markers)
  3733. (setq markers (concat (replace-match "" t t markers) "-")))
  3734. (if (string-match "\\^" vmarkers)
  3735. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3736. (if (string-match "-" vmarkers)
  3737. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3738. (if (> nl 0)
  3739. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3740. (int-to-string nl) "\\}")))
  3741. ;; Make the regexp
  3742. (setq org-emph-re
  3743. (concat "\\([" pre "]\\|^\\)"
  3744. "\\("
  3745. "\\([" markers "]\\)"
  3746. "\\("
  3747. "[^" border "]\\|"
  3748. "[^" border "]"
  3749. body1
  3750. "[^" border "]"
  3751. "\\)"
  3752. "\\3\\)"
  3753. "\\([" post "]\\|$\\)"))
  3754. (setq org-verbatim-re
  3755. (concat "\\([" pre "]\\|^\\)"
  3756. "\\("
  3757. "\\([" vmarkers "]\\)"
  3758. "\\("
  3759. "[^" border "]\\|"
  3760. "[^" border "]"
  3761. body1
  3762. "[^" border "]"
  3763. "\\)"
  3764. "\\3\\)"
  3765. "\\([" post "]\\|$\\)")))))
  3766. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3767. ;; set this option proved cumbersome. See this message/thread:
  3768. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3769. (defvar org-emphasis-regexp-components
  3770. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3771. "Components used to build the regular expression for emphasis.
  3772. This is a list with five entries. Terminology: In an emphasis string
  3773. like \" *strong word* \", we call the initial space PREMATCH, the final
  3774. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3775. and \"trong wor\" is the body. The different components in this variable
  3776. specify what is allowed/forbidden in each part:
  3777. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3778. post Chars allowed as postmatch. End of line will be allowed too.
  3779. border The chars *forbidden* as border characters.
  3780. body-regexp A regexp like \".\" to match a body character. Don't use
  3781. non-shy groups here, and don't allow newline here.
  3782. newline The maximum number of newlines allowed in an emphasis exp.
  3783. You need to reload Org or to restart Emacs after customizing this.")
  3784. (defcustom org-emphasis-alist
  3785. `(("*" bold)
  3786. ("/" italic)
  3787. ("_" underline)
  3788. ("=" org-verbatim verbatim)
  3789. ("~" org-code verbatim)
  3790. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3791. "Alist of characters and faces to emphasize text.
  3792. Text starting and ending with a special character will be emphasized,
  3793. for example *bold*, _underlined_ and /italic/. This variable sets the
  3794. marker characters and the face to be used by font-lock for highlighting
  3795. in Org-mode Emacs buffers.
  3796. You need to reload Org or to restart Emacs after customizing this."
  3797. :group 'org-appearance
  3798. :set 'org-set-emph-re
  3799. :version "24.4"
  3800. :package-version '(Org . "8.0")
  3801. :type '(repeat
  3802. (list
  3803. (string :tag "Marker character")
  3804. (choice
  3805. (face :tag "Font-lock-face")
  3806. (plist :tag "Face property list"))
  3807. (option (const verbatim)))))
  3808. (defvar org-protecting-blocks
  3809. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3810. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3811. This is needed for font-lock setup.")
  3812. ;;; Miscellaneous options
  3813. (defgroup org-completion nil
  3814. "Completion in Org-mode."
  3815. :tag "Org Completion"
  3816. :group 'org)
  3817. (defcustom org-completion-use-ido nil
  3818. "Non-nil means use ido completion wherever possible.
  3819. Note that `ido-mode' must be active for this variable to be relevant.
  3820. If you decide to turn this variable on, you might well want to turn off
  3821. `org-outline-path-complete-in-steps'.
  3822. See also `org-completion-use-iswitchb'."
  3823. :group 'org-completion
  3824. :type 'boolean)
  3825. (defcustom org-completion-use-iswitchb nil
  3826. "Non-nil means use iswitchb completion wherever possible.
  3827. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3828. If you decide to turn this variable on, you might well want to turn off
  3829. `org-outline-path-complete-in-steps'.
  3830. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3831. :group 'org-completion
  3832. :type 'boolean)
  3833. (defcustom org-completion-fallback-command 'hippie-expand
  3834. "The expansion command called by \\[pcomplete] in normal context.
  3835. Normal means, no org-mode-specific context."
  3836. :group 'org-completion
  3837. :type 'function)
  3838. ;;; Functions and variables from their packages
  3839. ;; Declared here to avoid compiler warnings
  3840. ;; XEmacs only
  3841. (defvar outline-mode-menu-heading)
  3842. (defvar outline-mode-menu-show)
  3843. (defvar outline-mode-menu-hide)
  3844. (defvar zmacs-regions) ; XEmacs regions
  3845. ;; Emacs only
  3846. (defvar mark-active)
  3847. ;; Various packages
  3848. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3849. (declare-function calendar-forward-day "cal-move" (arg))
  3850. (declare-function calendar-goto-date "cal-move" (date))
  3851. (declare-function calendar-goto-today "cal-move" ())
  3852. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3853. (defvar calc-embedded-close-formula)
  3854. (defvar calc-embedded-open-formula)
  3855. (declare-function cdlatex-tab "ext:cdlatex" ())
  3856. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3857. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3858. (defvar font-lock-unfontify-region-function)
  3859. (declare-function iswitchb-read-buffer "iswitchb"
  3860. (prompt &optional default require-match start matches-set))
  3861. (defvar iswitchb-temp-buflist)
  3862. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3863. (defvar org-agenda-tags-todo-honor-ignore-options)
  3864. (declare-function org-agenda-skip "org-agenda" ())
  3865. (declare-function
  3866. org-agenda-format-item "org-agenda"
  3867. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3868. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3869. (declare-function org-agenda-change-all-lines "org-agenda"
  3870. (newhead hdmarker &optional fixface just-this))
  3871. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3872. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3873. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3874. (beg end))
  3875. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3876. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3877. "org-agenda" (&optional end))
  3878. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3879. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3880. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3881. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3882. (declare-function org-indent-mode "org-indent" (&optional arg))
  3883. (declare-function parse-time-string "parse-time" (string))
  3884. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3885. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3886. (defvar remember-data-file)
  3887. (defvar texmathp-why)
  3888. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3889. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3890. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3891. ;;;###autoload
  3892. (defun turn-on-orgtbl ()
  3893. "Unconditionally turn on `orgtbl-mode'."
  3894. (require 'org-table)
  3895. (orgtbl-mode 1))
  3896. (defun org-at-table-p (&optional table-type)
  3897. "Return t if the cursor is inside an org-type table.
  3898. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3899. (if org-enable-table-editor
  3900. (save-excursion
  3901. (beginning-of-line 1)
  3902. (looking-at (if table-type org-table-any-line-regexp
  3903. org-table-line-regexp)))
  3904. nil))
  3905. (defsubst org-table-p () (org-at-table-p))
  3906. (defun org-at-table.el-p ()
  3907. "Return t if and only if we are at a table.el table."
  3908. (and (org-at-table-p 'any)
  3909. (save-excursion
  3910. (goto-char (org-table-begin 'any))
  3911. (looking-at org-table1-hline-regexp))))
  3912. (defun org-table-recognize-table.el ()
  3913. "If there is a table.el table nearby, recognize it and move into it."
  3914. (if org-table-tab-recognizes-table.el
  3915. (if (org-at-table.el-p)
  3916. (progn
  3917. (beginning-of-line 1)
  3918. (if (looking-at org-table-dataline-regexp)
  3919. nil
  3920. (if (looking-at org-table1-hline-regexp)
  3921. (progn
  3922. (beginning-of-line 2)
  3923. (if (looking-at org-table-any-border-regexp)
  3924. (beginning-of-line -1)))))
  3925. (if (re-search-forward "|" (org-table-end t) t)
  3926. (progn
  3927. (require 'table)
  3928. (if (table--at-cell-p (point))
  3929. t
  3930. (message "recognizing table.el table...")
  3931. (table-recognize-table)
  3932. (message "recognizing table.el table...done")))
  3933. (error "This should not happen"))
  3934. t)
  3935. nil)
  3936. nil))
  3937. (defun org-at-table-hline-p ()
  3938. "Return t if the cursor is inside a hline in a table."
  3939. (if org-enable-table-editor
  3940. (save-excursion
  3941. (beginning-of-line 1)
  3942. (looking-at org-table-hline-regexp))
  3943. nil))
  3944. (defun org-table-map-tables (function &optional quietly)
  3945. "Apply FUNCTION to the start of all tables in the buffer."
  3946. (save-excursion
  3947. (save-restriction
  3948. (widen)
  3949. (goto-char (point-min))
  3950. (while (re-search-forward org-table-any-line-regexp nil t)
  3951. (unless quietly
  3952. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3953. (beginning-of-line 1)
  3954. (when (and (looking-at org-table-line-regexp)
  3955. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3956. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3957. (save-excursion (funcall function))
  3958. (or (looking-at org-table-line-regexp)
  3959. (forward-char 1)))
  3960. (re-search-forward org-table-any-border-regexp nil 1))))
  3961. (unless quietly (message "Mapping tables: done")))
  3962. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3963. (declare-function org-clock-update-mode-line "org-clock" ())
  3964. (declare-function org-resolve-clocks "org-clock"
  3965. (&optional also-non-dangling-p prompt last-valid))
  3966. (defun org-at-TBLFM-p (&optional pos)
  3967. "Return t when point (or POS) is in #+TBLFM line."
  3968. (save-excursion
  3969. (let ((pos pos)))
  3970. (goto-char (or pos (point)))
  3971. (beginning-of-line 1)
  3972. (looking-at org-TBLFM-regexp)))
  3973. (defvar org-clock-start-time)
  3974. (defvar org-clock-marker (make-marker)
  3975. "Marker recording the last clock-in.")
  3976. (defvar org-clock-hd-marker (make-marker)
  3977. "Marker recording the last clock-in, but the headline position.")
  3978. (defvar org-clock-heading ""
  3979. "The heading of the current clock entry.")
  3980. (defun org-clock-is-active ()
  3981. "Return the buffer where the clock is currently running.
  3982. Return nil if no clock is running."
  3983. (marker-buffer org-clock-marker))
  3984. (defun org-check-running-clock ()
  3985. "Check if the current buffer contains the running clock.
  3986. If yes, offer to stop it and to save the buffer with the changes."
  3987. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3988. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3989. (buffer-name))))
  3990. (org-clock-out)
  3991. (when (y-or-n-p "Save changed buffer?")
  3992. (save-buffer))))
  3993. (defun org-clocktable-try-shift (dir n)
  3994. "Check if this line starts a clock table, if yes, shift the time block."
  3995. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3996. (org-clocktable-shift dir n)))
  3997. ;;;###autoload
  3998. (defun org-clock-persistence-insinuate ()
  3999. "Set up hooks for clock persistence."
  4000. (require 'org-clock)
  4001. (add-hook 'org-mode-hook 'org-clock-load)
  4002. (add-hook 'kill-emacs-hook 'org-clock-save))
  4003. (defgroup org-archive nil
  4004. "Options concerning archiving in Org-mode."
  4005. :tag "Org Archive"
  4006. :group 'org-structure)
  4007. (defcustom org-archive-location "%s_archive::"
  4008. "The location where subtrees should be archived.
  4009. The value of this variable is a string, consisting of two parts,
  4010. separated by a double-colon. The first part is a filename and
  4011. the second part is a headline.
  4012. When the filename is omitted, archiving happens in the same file.
  4013. %s in the filename will be replaced by the current file
  4014. name (without the directory part). Archiving to a different file
  4015. is useful to keep archived entries from contributing to the
  4016. Org-mode Agenda.
  4017. The archived entries will be filed as subtrees of the specified
  4018. headline. When the headline is omitted, the subtrees are simply
  4019. filed away at the end of the file, as top-level entries. Also in
  4020. the heading you can use %s to represent the file name, this can be
  4021. useful when using the same archive for a number of different files.
  4022. Here are a few examples:
  4023. \"%s_archive::\"
  4024. If the current file is Projects.org, archive in file
  4025. Projects.org_archive, as top-level trees. This is the default.
  4026. \"::* Archived Tasks\"
  4027. Archive in the current file, under the top-level headline
  4028. \"* Archived Tasks\".
  4029. \"~/org/archive.org::\"
  4030. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4031. \"~/org/archive.org::* From %s\"
  4032. Archive in file ~/org/archive.org (absolute path), under headlines
  4033. \"From FILENAME\" where file name is the current file name.
  4034. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4035. The \"datetree/\" string is special, signifying to archive
  4036. items to the datetree. Items are placed in either the CLOSED
  4037. date of the item, or the current date if there is no CLOSED date.
  4038. The heading will be a subentry to the current date. There doesn't
  4039. need to be a heading, but there always needs to be a slash after
  4040. datetree. For example, to store archived items directly in the
  4041. datetree, use \"~/org/datetree.org::datetree/\".
  4042. \"basement::** Finished Tasks\"
  4043. Archive in file ./basement (relative path), as level 3 trees
  4044. below the level 2 heading \"** Finished Tasks\".
  4045. You may set this option on a per-file basis by adding to the buffer a
  4046. line like
  4047. #+ARCHIVE: basement::** Finished Tasks
  4048. You may also define it locally for a subtree by setting an ARCHIVE property
  4049. in the entry. If such a property is found in an entry, or anywhere up
  4050. the hierarchy, it will be used."
  4051. :group 'org-archive
  4052. :type 'string)
  4053. (defcustom org-agenda-skip-archived-trees t
  4054. "Non-nil means the agenda will skip any items located in archived trees.
  4055. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4056. variable is no longer recommended, you should leave it at the value t.
  4057. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4058. :group 'org-archive
  4059. :group 'org-agenda-skip
  4060. :type 'boolean)
  4061. (defcustom org-columns-skip-archived-trees t
  4062. "Non-nil means ignore archived trees when creating column view."
  4063. :group 'org-archive
  4064. :group 'org-properties
  4065. :type 'boolean)
  4066. (defcustom org-cycle-open-archived-trees nil
  4067. "Non-nil means `org-cycle' will open archived trees.
  4068. An archived tree is a tree marked with the tag ARCHIVE.
  4069. When nil, archived trees will stay folded. You can still open them with
  4070. normal outline commands like `show-all', but not with the cycling commands."
  4071. :group 'org-archive
  4072. :group 'org-cycle
  4073. :type 'boolean)
  4074. (defcustom org-sparse-tree-open-archived-trees nil
  4075. "Non-nil means sparse tree construction shows matches in archived trees.
  4076. When nil, matches in these trees are highlighted, but the trees are kept in
  4077. collapsed state."
  4078. :group 'org-archive
  4079. :group 'org-sparse-trees
  4080. :type 'boolean)
  4081. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  4082. "The default date type when building a sparse tree.
  4083. When this is nil, a date is a scheduled or a deadline timestamp.
  4084. Otherwise, these types are allowed:
  4085. all: all timestamps
  4086. active: only active timestamps (<...>)
  4087. inactive: only inactive timestamps (<...)
  4088. scheduled: only scheduled timestamps
  4089. deadline: only deadline timestamps"
  4090. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  4091. (const :tag "All timestamps" all)
  4092. (const :tag "Only active timestamps" active)
  4093. (const :tag "Only inactive timestamps" inactive)
  4094. (const :tag "Only scheduled timestamps" scheduled)
  4095. (const :tag "Only deadline timestamps" deadline)
  4096. (const :tag "Only closed timestamps" closed))
  4097. :version "24.3"
  4098. :group 'org-sparse-trees)
  4099. (defun org-cycle-hide-archived-subtrees (state)
  4100. "Re-hide all archived subtrees after a visibility state change."
  4101. (when (and (not org-cycle-open-archived-trees)
  4102. (not (memq state '(overview folded))))
  4103. (save-excursion
  4104. (let* ((globalp (memq state '(contents all)))
  4105. (beg (if globalp (point-min) (point)))
  4106. (end (if globalp (point-max) (org-end-of-subtree t))))
  4107. (org-hide-archived-subtrees beg end)
  4108. (goto-char beg)
  4109. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4110. (message "%s" (substitute-command-keys
  4111. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4112. (defun org-force-cycle-archived ()
  4113. "Cycle subtree even if it is archived."
  4114. (interactive)
  4115. (setq this-command 'org-cycle)
  4116. (let ((org-cycle-open-archived-trees t))
  4117. (call-interactively 'org-cycle)))
  4118. (defun org-hide-archived-subtrees (beg end)
  4119. "Re-hide all archived subtrees after a visibility state change."
  4120. (org-with-wide-buffer
  4121. (let ((case-fold-search nil)
  4122. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4123. (goto-char beg)
  4124. (while (and (< (point) end) (re-search-forward re end t))
  4125. (when (member org-archive-tag (org-get-tags))
  4126. (org-flag-subtree t)
  4127. (org-end-of-subtree t))))))
  4128. (declare-function outline-end-of-heading "outline" ())
  4129. (declare-function outline-flag-region "outline" (from to flag))
  4130. (defun org-flag-subtree (flag)
  4131. (save-excursion
  4132. (org-back-to-heading t)
  4133. (outline-end-of-heading)
  4134. (outline-flag-region (point)
  4135. (progn (org-end-of-subtree t) (point))
  4136. flag)))
  4137. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4138. ;; Declare Column View Code
  4139. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4140. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4141. (declare-function org-columns-compute "org-colview" (property))
  4142. ;; Declare ID code
  4143. (declare-function org-id-store-link "org-id")
  4144. (declare-function org-id-locations-load "org-id")
  4145. (declare-function org-id-locations-save "org-id")
  4146. (defvar org-id-track-globally)
  4147. ;;; Variables for pre-computed regular expressions, all buffer local
  4148. (defvar org-todo-regexp nil
  4149. "Matches any of the TODO state keywords.")
  4150. (make-variable-buffer-local 'org-todo-regexp)
  4151. (defvar org-not-done-regexp nil
  4152. "Matches any of the TODO state keywords except the last one.")
  4153. (make-variable-buffer-local 'org-not-done-regexp)
  4154. (defvar org-not-done-heading-regexp nil
  4155. "Matches a TODO headline that is not done.")
  4156. (make-variable-buffer-local 'org-not-done-regexp)
  4157. (defvar org-todo-line-regexp nil
  4158. "Matches a headline and puts TODO state into group 2 if present.")
  4159. (make-variable-buffer-local 'org-todo-line-regexp)
  4160. (defvar org-complex-heading-regexp nil
  4161. "Matches a headline and puts everything into groups:
  4162. group 1: the stars
  4163. group 2: The todo keyword, maybe
  4164. group 3: Priority cookie
  4165. group 4: True headline
  4166. group 5: Tags")
  4167. (make-variable-buffer-local 'org-complex-heading-regexp)
  4168. (defvar org-complex-heading-regexp-format nil
  4169. "Printf format to make regexp to match an exact headline.
  4170. This regexp will match the headline of any node which has the
  4171. exact headline text that is put into the format, but may have any
  4172. TODO state, priority and tags.")
  4173. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4174. (defvar org-todo-line-tags-regexp nil
  4175. "Matches a headline and puts TODO state into group 2 if present.
  4176. Also put tags into group 4 if tags are present.")
  4177. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4178. (defconst org-plain-time-of-day-regexp
  4179. (concat
  4180. "\\(\\<[012]?[0-9]"
  4181. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4182. "\\(--?"
  4183. "\\(\\<[012]?[0-9]"
  4184. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4185. "\\)?")
  4186. "Regular expression to match a plain time or time range.
  4187. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4188. groups carry important information:
  4189. 0 the full match
  4190. 1 the first time, range or not
  4191. 8 the second time, if it is a range.")
  4192. (defconst org-plain-time-extension-regexp
  4193. (concat
  4194. "\\(\\<[012]?[0-9]"
  4195. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4196. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4197. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4198. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4199. groups carry important information:
  4200. 0 the full match
  4201. 7 hours of duration
  4202. 9 minutes of duration")
  4203. (defconst org-stamp-time-of-day-regexp
  4204. (concat
  4205. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4206. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4207. "\\(--?"
  4208. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4209. "Regular expression to match a timestamp time or time range.
  4210. After a match, the following groups carry important information:
  4211. 0 the full match
  4212. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4213. 2 the first time, range or not
  4214. 4 the second time, if it is a range.")
  4215. (defconst org-startup-options
  4216. '(("fold" org-startup-folded t)
  4217. ("overview" org-startup-folded t)
  4218. ("nofold" org-startup-folded nil)
  4219. ("showall" org-startup-folded nil)
  4220. ("showeverything" org-startup-folded showeverything)
  4221. ("content" org-startup-folded content)
  4222. ("indent" org-startup-indented t)
  4223. ("noindent" org-startup-indented nil)
  4224. ("hidestars" org-hide-leading-stars t)
  4225. ("showstars" org-hide-leading-stars nil)
  4226. ("odd" org-odd-levels-only t)
  4227. ("oddeven" org-odd-levels-only nil)
  4228. ("align" org-startup-align-all-tables t)
  4229. ("noalign" org-startup-align-all-tables nil)
  4230. ("inlineimages" org-startup-with-inline-images t)
  4231. ("noinlineimages" org-startup-with-inline-images nil)
  4232. ("latexpreview" org-startup-with-latex-preview t)
  4233. ("nolatexpreview" org-startup-with-latex-preview nil)
  4234. ("customtime" org-display-custom-times t)
  4235. ("logdone" org-log-done time)
  4236. ("lognotedone" org-log-done note)
  4237. ("nologdone" org-log-done nil)
  4238. ("lognoteclock-out" org-log-note-clock-out t)
  4239. ("nolognoteclock-out" org-log-note-clock-out nil)
  4240. ("logrepeat" org-log-repeat state)
  4241. ("lognoterepeat" org-log-repeat note)
  4242. ("logdrawer" org-log-into-drawer t)
  4243. ("nologdrawer" org-log-into-drawer nil)
  4244. ("logstatesreversed" org-log-states-order-reversed t)
  4245. ("nologstatesreversed" org-log-states-order-reversed nil)
  4246. ("nologrepeat" org-log-repeat nil)
  4247. ("logreschedule" org-log-reschedule time)
  4248. ("lognotereschedule" org-log-reschedule note)
  4249. ("nologreschedule" org-log-reschedule nil)
  4250. ("logredeadline" org-log-redeadline time)
  4251. ("lognoteredeadline" org-log-redeadline note)
  4252. ("nologredeadline" org-log-redeadline nil)
  4253. ("logrefile" org-log-refile time)
  4254. ("lognoterefile" org-log-refile note)
  4255. ("nologrefile" org-log-refile nil)
  4256. ("fninline" org-footnote-define-inline t)
  4257. ("nofninline" org-footnote-define-inline nil)
  4258. ("fnlocal" org-footnote-section nil)
  4259. ("fnauto" org-footnote-auto-label t)
  4260. ("fnprompt" org-footnote-auto-label nil)
  4261. ("fnconfirm" org-footnote-auto-label confirm)
  4262. ("fnplain" org-footnote-auto-label plain)
  4263. ("fnadjust" org-footnote-auto-adjust t)
  4264. ("nofnadjust" org-footnote-auto-adjust nil)
  4265. ("constcgs" constants-unit-system cgs)
  4266. ("constSI" constants-unit-system SI)
  4267. ("noptag" org-tag-persistent-alist nil)
  4268. ("hideblocks" org-hide-block-startup t)
  4269. ("nohideblocks" org-hide-block-startup nil)
  4270. ("beamer" org-startup-with-beamer-mode t)
  4271. ("entitiespretty" org-pretty-entities t)
  4272. ("entitiesplain" org-pretty-entities nil))
  4273. "Variable associated with STARTUP options for org-mode.
  4274. Each element is a list of three items: the startup options (as written
  4275. in the #+STARTUP line), the corresponding variable, and the value to set
  4276. this variable to if the option is found. An optional forth element PUSH
  4277. means to push this value onto the list in the variable.")
  4278. (defcustom org-group-tags t
  4279. "When non-nil (the default), use group tags.
  4280. This can be turned on/off through `org-toggle-tags-groups'."
  4281. :group 'org-tags
  4282. :group 'org-startup
  4283. :type 'boolean)
  4284. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4285. (defun org-toggle-tags-groups ()
  4286. "Toggle support for group tags.
  4287. Support for group tags is controlled by the option
  4288. `org-group-tags', which is non-nil by default."
  4289. (interactive)
  4290. (setq org-group-tags (not org-group-tags))
  4291. (cond ((and (derived-mode-p 'org-agenda-mode)
  4292. org-group-tags)
  4293. (org-agenda-redo))
  4294. ((derived-mode-p 'org-mode)
  4295. (let ((org-inhibit-startup t)) (org-mode))))
  4296. (message "Groups tags support has been turned %s"
  4297. (if org-group-tags "on" "off")))
  4298. (defun org-set-regexps-and-options (&optional tags-only)
  4299. "Precompute regular expressions used in the current buffer.
  4300. When optional argument TAGS-ONLY is non-nil, only compute tags
  4301. related expressions."
  4302. (when (derived-mode-p 'org-mode)
  4303. (let ((alist (org--setup-collect-keywords
  4304. (org-make-options-regexp
  4305. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4306. (and (not tags-only)
  4307. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4308. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4309. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4310. (org--setup-process-tags
  4311. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4312. (unless tags-only
  4313. ;; File properties.
  4314. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4315. ;; Archive location.
  4316. (let ((archive (cdr (assq 'archive alist))))
  4317. (when archive (org-set-local 'org-archive-location archive)))
  4318. ;; Category.
  4319. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4320. (when cat
  4321. (org-set-local 'org-category (intern cat))
  4322. (org-set-local 'org-file-properties
  4323. (org--update-property-plist
  4324. "CATEGORY" cat org-file-properties))))
  4325. ;; Columns.
  4326. (let ((column (cdr (assq 'columns alist))))
  4327. (when column (org-set-local 'org-columns-default-format column)))
  4328. ;; Constants.
  4329. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4330. ;; Link abbreviations.
  4331. (let ((links (cdr (assq 'link alist))))
  4332. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4333. ;; Priorities.
  4334. (let ((priorities (cdr (assq 'priorities alist))))
  4335. (when priorities
  4336. (org-set-local 'org-highest-priority (nth 0 priorities))
  4337. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4338. (org-set-local 'org-default-priority (nth 2 priorities))))
  4339. ;; Scripts.
  4340. (let ((scripts (assq 'scripts alist)))
  4341. (when scripts
  4342. (org-set-local 'org-use-sub-superscripts (cdr scripts))))
  4343. ;; Startup options.
  4344. (let ((startup (cdr (assq 'startup alist))))
  4345. (dolist (option startup)
  4346. (let ((entry (assoc-string option org-startup-options t)))
  4347. (when entry
  4348. (let ((var (nth 1 entry))
  4349. (val (nth 2 entry)))
  4350. (if (not (nth 3 entry)) (org-set-local var val)
  4351. (unless (listp (symbol-value var))
  4352. (org-set-local var nil))
  4353. (add-to-list var val)))))))
  4354. ;; TODO keywords.
  4355. (org-set-local 'org-todo-kwd-alist nil)
  4356. (org-set-local 'org-todo-key-alist nil)
  4357. (org-set-local 'org-todo-key-trigger nil)
  4358. (org-set-local 'org-todo-keywords-1 nil)
  4359. (org-set-local 'org-done-keywords nil)
  4360. (org-set-local 'org-todo-heads nil)
  4361. (org-set-local 'org-todo-sets nil)
  4362. (org-set-local 'org-todo-log-states nil)
  4363. (let ((todo-sequences
  4364. (or (nreverse (cdr (assq 'todo alist)))
  4365. (let ((d (default-value 'org-todo-keywords)))
  4366. (if (not (stringp (car d))) d
  4367. ;; XXX: Backward compatibility code.
  4368. (list (cons org-todo-interpretation d)))))))
  4369. (dolist (sequence todo-sequences)
  4370. (let* ((sequence (or (run-hook-with-args-until-success
  4371. 'org-todo-setup-filter-hook sequence)
  4372. sequence))
  4373. (sequence-type (car sequence))
  4374. (keywords (cdr sequence))
  4375. (sep (member "|" keywords))
  4376. names alist)
  4377. (dolist (k (remove "|" keywords))
  4378. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4379. k)
  4380. (error "Invalid TODO keyword %s" k))
  4381. (let ((name (match-string 1 k))
  4382. (key (match-string 2 k))
  4383. (log (org-extract-log-state-settings k)))
  4384. (push name names)
  4385. (push (cons name (and key (string-to-char key))) alist)
  4386. (when log (push log org-todo-log-states))))
  4387. (let* ((names (nreverse names))
  4388. (done (if sep (org-remove-keyword-keys (cdr sep))
  4389. (last names)))
  4390. (head (car names))
  4391. (tail (list sequence-type head (car done) (org-last done))))
  4392. (add-to-list 'org-todo-heads head 'append)
  4393. (push names org-todo-sets)
  4394. (setq org-done-keywords (append org-done-keywords done nil))
  4395. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4396. (setq org-todo-key-alist
  4397. (append org-todo-key-alist
  4398. (and alist
  4399. (append '((:startgroup))
  4400. (nreverse alist)
  4401. '((:endgroup))))))
  4402. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4403. (setq org-todo-sets (nreverse org-todo-sets)
  4404. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4405. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4406. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4407. ;; Compute the regular expressions and other local variables.
  4408. ;; Using `org-outline-regexp-bol' would complicate them much,
  4409. ;; because of the fixed white space at the end of that string.
  4410. (if (not org-done-keywords)
  4411. (setq org-done-keywords
  4412. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4413. (setq org-not-done-keywords
  4414. (org-delete-all org-done-keywords
  4415. (copy-sequence org-todo-keywords-1))
  4416. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4417. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4418. org-not-done-heading-regexp
  4419. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4420. org-todo-line-regexp
  4421. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4422. org-complex-heading-regexp
  4423. (concat "^\\(\\*+\\)"
  4424. "\\(?: +" org-todo-regexp "\\)?"
  4425. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4426. "\\(?: +\\(.*?\\)\\)??"
  4427. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4428. "[ \t]*$")
  4429. org-complex-heading-regexp-format
  4430. (concat "^\\(\\*+\\)"
  4431. "\\(?: +" org-todo-regexp "\\)?"
  4432. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4433. "\\(?: +"
  4434. ;; Stats cookies can be stuck to body.
  4435. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4436. "\\(%s\\)"
  4437. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4438. "\\)"
  4439. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4440. "[ \t]*$")
  4441. org-todo-line-tags-regexp
  4442. (concat "^\\(\\*+\\)"
  4443. "\\(?: +" org-todo-regexp "\\)?"
  4444. "\\(?: +\\(.*?\\)\\)??"
  4445. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4446. "[ \t]*$"))
  4447. (org-compute-latex-and-related-regexp)))))
  4448. (defun org--setup-collect-keywords (regexp &optional files alist)
  4449. "Return setup keywords values as an alist.
  4450. REGEXP matches a subset of setup keywords. FILES is a list of
  4451. file names already visited. It is used to avoid circular setup
  4452. files. ALIST, when non-nil, is the alist computed so far.
  4453. Return value contains the following keys: `archive', `category',
  4454. `columns', `constants', `filetags', `link', `priorities',
  4455. `property', `scripts', `startup', `tags' and `todo'."
  4456. (org-with-wide-buffer
  4457. (goto-char (point-min))
  4458. (let ((case-fold-search t))
  4459. (while (re-search-forward regexp nil t)
  4460. (let ((element (org-element-at-point)))
  4461. (when (eq (org-element-type element) 'keyword)
  4462. (let ((key (org-element-property :key element))
  4463. (value (org-element-property :value element)))
  4464. (cond
  4465. ((equal key "ARCHIVE")
  4466. (when (org-string-nw-p value)
  4467. (push (cons 'archive value) alist)))
  4468. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4469. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4470. ((equal key "CONSTANTS")
  4471. (let* ((constants (assq 'constants alist))
  4472. (store (cdr constants)))
  4473. (dolist (pair (org-split-string value))
  4474. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4475. pair)
  4476. (let* ((name (match-string 1 pair))
  4477. (value (match-string 2 pair))
  4478. (old (assoc name store)))
  4479. (if old (setcdr old value)
  4480. (push (cons name value) store)))))
  4481. (if constants (setcdr constants store)
  4482. (push (cons 'constants store) alist))))
  4483. ((equal key "FILETAGS")
  4484. (when (org-string-nw-p value)
  4485. (let ((old (assq 'filetags alist))
  4486. (new (apply #'nconc
  4487. (mapcar (lambda (x) (org-split-string x ":"))
  4488. (org-split-string value)))))
  4489. (if old (setcdr old (append new (cdr old)))
  4490. (push (cons 'filetags new) alist)))))
  4491. ((equal key "LINK")
  4492. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4493. (let ((links (assq 'link alist))
  4494. (pair (cons (org-match-string-no-properties 1 value)
  4495. (org-match-string-no-properties 2 value))))
  4496. (if links (push pair (cdr links))
  4497. (push (list 'link pair) alist)))))
  4498. ((equal key "OPTIONS")
  4499. (when (and (org-string-nw-p value)
  4500. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4501. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4502. ((equal key "PRIORITIES")
  4503. (push (cons 'priorities
  4504. (let ((prio (org-split-string value)))
  4505. (if (< (length prio) 3) '(?A ?C ?B)
  4506. (mapcar #'string-to-char prio))))
  4507. alist))
  4508. ((equal key "PROPERTY")
  4509. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4510. (let* ((property (assq 'property alist))
  4511. (value (org--update-property-plist
  4512. (org-match-string-no-properties 1 value)
  4513. (org-match-string-no-properties 2 value)
  4514. (cdr property))))
  4515. (if property (setcdr property value)
  4516. (push (cons 'property value) alist)))))
  4517. ((equal key "STARTUP")
  4518. (let ((startup (assq 'startup alist)))
  4519. (if startup
  4520. (setcdr startup
  4521. (append (cdr startup) (org-split-string value)))
  4522. (push (cons 'startup (org-split-string value)) alist))))
  4523. ((equal key "TAGS")
  4524. (let ((tag-cell (assq 'tags alist)))
  4525. (if tag-cell
  4526. (setcdr tag-cell
  4527. (append (cdr tag-cell)
  4528. '("\\n")
  4529. (org-split-string value)))
  4530. (push (cons 'tags (org-split-string value)) alist))))
  4531. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4532. (let ((todo (assq 'todo alist))
  4533. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4534. (org-split-string value))))
  4535. (if todo (push value (cdr todo))
  4536. (push (list 'todo value) alist))))
  4537. ((equal key "SETUPFILE")
  4538. (unless buffer-read-only ; Do not check in Gnus messages.
  4539. (let ((f (and (org-string-nw-p value)
  4540. (expand-file-name
  4541. (org-remove-double-quotes value)))))
  4542. (when (and f (file-readable-p f) (not (member f files)))
  4543. (with-temp-buffer
  4544. (insert-file-contents f)
  4545. (setq alist
  4546. ;; Fake Org mode to benefit from cache
  4547. ;; without recurring needlessly.
  4548. (let ((major-mode 'org-mode))
  4549. (org--setup-collect-keywords
  4550. regexp (cons f files) alist)))))))))))))))
  4551. alist)
  4552. (defun org--setup-process-tags (tags filetags)
  4553. "Precompute variables used for tags.
  4554. TAGS is a list of tags and tag group symbols, as strings.
  4555. FILETAGS is a list of tags, as strings."
  4556. ;; Process the file tags.
  4557. (org-set-local 'org-file-tags
  4558. (mapcar #'org-add-prop-inherited filetags))
  4559. ;; Provide default tags if no local tags are found.
  4560. (when (and (not tags) org-tag-alist)
  4561. (setq tags
  4562. (mapcar (lambda (tag)
  4563. (case (car tag)
  4564. (:startgroup "{")
  4565. (:endgroup "}")
  4566. (:grouptags ":")
  4567. (:newline "\\n")
  4568. (otherwise (concat (car tag)
  4569. (and (characterp (cdr tag))
  4570. (format "(%c)" (cdr tag)))))))
  4571. org-tag-alist)))
  4572. ;; Process the tags.
  4573. (org-set-local 'org-tag-groups-alist nil)
  4574. (org-set-local 'org-tag-alist nil)
  4575. (let (group-flag)
  4576. (while tags
  4577. (let ((e (car tags)))
  4578. (setq tags (cdr tags))
  4579. (cond
  4580. ((equal e "{")
  4581. (push '(:startgroup) org-tag-alist)
  4582. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4583. ((equal e "}")
  4584. (push '(:endgroup) org-tag-alist)
  4585. (setq group-flag nil))
  4586. ((equal e ":")
  4587. (push '(:grouptags) org-tag-alist)
  4588. (setq group-flag 'append))
  4589. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4590. ((string-match
  4591. (org-re "\\`\\([[:alnum:]_@#%]+\\)\\(?:(\\(.\\))\\)?\\'") e)
  4592. (let ((tag (match-string 1 e))
  4593. (key (and (match-beginning 2)
  4594. (string-to-char (match-string 2 e)))))
  4595. (cond ((eq group-flag 'append)
  4596. (setcar org-tag-groups-alist
  4597. (append (car org-tag-groups-alist) (list tag))))
  4598. (group-flag (push (list tag) org-tag-groups-alist)))
  4599. (unless (assoc tag org-tag-alist)
  4600. (push (cons tag key) org-tag-alist))))))))
  4601. (setq org-tag-alist (nreverse org-tag-alist)))
  4602. (defun org-file-contents (file &optional noerror)
  4603. "Return the contents of FILE, as a string."
  4604. (if (and file (file-readable-p file))
  4605. (with-temp-buffer
  4606. (insert-file-contents file)
  4607. (buffer-string))
  4608. (funcall (if noerror 'message 'error)
  4609. "Cannot read file \"%s\"%s"
  4610. file
  4611. (let ((from (buffer-file-name (buffer-base-buffer))))
  4612. (if from (concat " (referenced in file \"" from "\")") "")))))
  4613. (defun org-extract-log-state-settings (x)
  4614. "Extract the log state setting from a TODO keyword string.
  4615. This will extract info from a string like \"WAIT(w@/!)\"."
  4616. (let (kw key log1 log2)
  4617. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4618. (setq kw (match-string 1 x)
  4619. key (and (match-end 2) (match-string 2 x))
  4620. log1 (and (match-end 3) (match-string 3 x))
  4621. log2 (and (match-end 4) (match-string 4 x)))
  4622. (and (or log1 log2)
  4623. (list kw
  4624. (and log1 (if (equal log1 "!") 'time 'note))
  4625. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4626. (defun org-remove-keyword-keys (list)
  4627. "Remove a pair of parenthesis at the end of each string in LIST."
  4628. (mapcar (lambda (x)
  4629. (if (string-match "(.*)$" x)
  4630. (substring x 0 (match-beginning 0))
  4631. x))
  4632. list))
  4633. (defun org-assign-fast-keys (alist)
  4634. "Assign fast keys to a keyword-key alist.
  4635. Respect keys that are already there."
  4636. (let (new e (alt ?0))
  4637. (while (setq e (pop alist))
  4638. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4639. (cdr e)) ;; Key already assigned.
  4640. (push e new)
  4641. (let ((clist (string-to-list (downcase (car e))))
  4642. (used (append new alist)))
  4643. (when (= (car clist) ?@)
  4644. (pop clist))
  4645. (while (and clist (rassoc (car clist) used))
  4646. (pop clist))
  4647. (unless clist
  4648. (while (rassoc alt used)
  4649. (incf alt)))
  4650. (push (cons (car e) (or (car clist) alt)) new))))
  4651. (nreverse new)))
  4652. ;;; Some variables used in various places
  4653. (defvar org-window-configuration nil
  4654. "Used in various places to store a window configuration.")
  4655. (defvar org-selected-window nil
  4656. "Used in various places to store a window configuration.")
  4657. (defvar org-finish-function nil
  4658. "Function to be called when `C-c C-c' is used.
  4659. This is for getting out of special buffers like capture.")
  4660. ;; FIXME: Occasionally check by commenting these, to make sure
  4661. ;; no other functions uses these, forgetting to let-bind them.
  4662. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4663. (defvar org-last-state)
  4664. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4665. ;; Defined somewhere in this file, but used before definition.
  4666. (defvar org-entities) ;; defined in org-entities.el
  4667. (defvar org-struct-menu)
  4668. (defvar org-org-menu)
  4669. (defvar org-tbl-menu)
  4670. ;;;; Define the Org-mode
  4671. ;; We use a before-change function to check if a table might need
  4672. ;; an update.
  4673. (defvar org-table-may-need-update t
  4674. "Indicates that a table might need an update.
  4675. This variable is set by `org-before-change-function'.
  4676. `org-table-align' sets it back to nil.")
  4677. (defun org-before-change-function (beg end)
  4678. "Every change indicates that a table might need an update."
  4679. (setq org-table-may-need-update t))
  4680. (defvar org-mode-map)
  4681. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4682. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4683. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4684. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4685. (defvar org-table-buffer-is-an nil)
  4686. (defvar bidi-paragraph-direction)
  4687. (defvar buffer-face-mode-face)
  4688. (require 'outline)
  4689. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4690. (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"))
  4691. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4692. ;; Other stuff we need.
  4693. (require 'time-date)
  4694. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4695. (require 'easymenu)
  4696. (require 'overlay)
  4697. ;; (require 'org-macs) moved higher up in the file before it is first used
  4698. (require 'org-entities)
  4699. ;; (require 'org-compat) moved higher up in the file before it is first used
  4700. (require 'org-faces)
  4701. (require 'org-list)
  4702. (require 'org-pcomplete)
  4703. (require 'org-src)
  4704. (require 'org-footnote)
  4705. (require 'org-macro)
  4706. ;; babel
  4707. (require 'ob)
  4708. ;;;###autoload
  4709. (define-derived-mode org-mode outline-mode "Org"
  4710. "Outline-based notes management and organizer, alias
  4711. \"Carsten's outline-mode for keeping track of everything.\"
  4712. Org-mode develops organizational tasks around a NOTES file which
  4713. contains information about projects as plain text. Org-mode is
  4714. implemented on top of outline-mode, which is ideal to keep the content
  4715. of large files well structured. It supports ToDo items, deadlines and
  4716. time stamps, which magically appear in the diary listing of the Emacs
  4717. calendar. Tables are easily created with a built-in table editor.
  4718. Plain text URL-like links connect to websites, emails (VM), Usenet
  4719. messages (Gnus), BBDB entries, and any files related to the project.
  4720. For printing and sharing of notes, an Org-mode file (or a part of it)
  4721. can be exported as a structured ASCII or HTML file.
  4722. The following commands are available:
  4723. \\{org-mode-map}"
  4724. ;; Get rid of Outline menus, they are not needed
  4725. ;; Need to do this here because define-derived-mode sets up
  4726. ;; the keymap so late. Still, it is a waste to call this each time
  4727. ;; we switch another buffer into org-mode.
  4728. (if (featurep 'xemacs)
  4729. (when (boundp 'outline-mode-menu-heading)
  4730. ;; Assume this is Greg's port, it uses easymenu
  4731. (easy-menu-remove outline-mode-menu-heading)
  4732. (easy-menu-remove outline-mode-menu-show)
  4733. (easy-menu-remove outline-mode-menu-hide))
  4734. (define-key org-mode-map [menu-bar headings] 'undefined)
  4735. (define-key org-mode-map [menu-bar hide] 'undefined)
  4736. (define-key org-mode-map [menu-bar show] 'undefined))
  4737. (org-load-modules-maybe)
  4738. (easy-menu-add org-org-menu)
  4739. (easy-menu-add org-tbl-menu)
  4740. (org-install-agenda-files-menu)
  4741. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4742. (add-to-invisibility-spec '(org-cwidth))
  4743. (add-to-invisibility-spec '(org-hide-block . t))
  4744. (when (featurep 'xemacs)
  4745. (org-set-local 'line-move-ignore-invisible t))
  4746. (org-set-local 'outline-regexp org-outline-regexp)
  4747. (org-set-local 'outline-level 'org-outline-level)
  4748. (setq bidi-paragraph-direction 'left-to-right)
  4749. (when (and org-ellipsis
  4750. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4751. (fboundp 'make-glyph-code))
  4752. (unless org-display-table
  4753. (setq org-display-table (make-display-table)))
  4754. (set-display-table-slot
  4755. org-display-table 4
  4756. (vconcat (mapcar
  4757. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4758. org-ellipsis)))
  4759. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4760. (setq buffer-display-table org-display-table))
  4761. (org-set-regexps-and-options)
  4762. (org-set-font-lock-defaults)
  4763. (when (and org-tag-faces (not org-tags-special-faces-re))
  4764. ;; tag faces set outside customize.... force initialization.
  4765. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4766. ;; Calc embedded
  4767. (org-set-local 'calc-embedded-open-mode "# ")
  4768. ;; Modify a few syntax entries
  4769. (modify-syntax-entry ?@ "w")
  4770. (modify-syntax-entry ?\" "\"")
  4771. (modify-syntax-entry ?\\ "_")
  4772. (modify-syntax-entry ?~ "_")
  4773. (if org-startup-truncated (setq truncate-lines t))
  4774. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4775. (org-set-local 'font-lock-unfontify-region-function
  4776. 'org-unfontify-region)
  4777. ;; Activate before-change-function
  4778. (org-set-local 'org-table-may-need-update t)
  4779. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4780. 'local)
  4781. ;; Check for running clock before killing a buffer
  4782. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4783. ;; Initialize macros templates.
  4784. (org-macro-initialize-templates)
  4785. ;; Initialize radio targets.
  4786. (org-update-radio-target-regexp)
  4787. ;; Indentation.
  4788. (org-set-local 'indent-line-function 'org-indent-line)
  4789. (org-set-local 'indent-region-function 'org-indent-region)
  4790. ;; Filling and auto-filling.
  4791. (org-setup-filling)
  4792. ;; Comments.
  4793. (org-setup-comments-handling)
  4794. ;; Initialize cache.
  4795. (org-element-cache-reset)
  4796. ;; Beginning/end of defun
  4797. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4798. (org-set-local 'end-of-defun-function
  4799. (lambda ()
  4800. (if (not (org-at-heading-p))
  4801. (org-forward-element)
  4802. (org-forward-element)
  4803. (forward-char -1))))
  4804. ;; Next error for sparse trees
  4805. (org-set-local 'next-error-function 'org-occur-next-match)
  4806. ;; Make sure dependence stuff works reliably, even for users who set it
  4807. ;; too late :-(
  4808. (if org-enforce-todo-dependencies
  4809. (add-hook 'org-blocker-hook
  4810. 'org-block-todo-from-children-or-siblings-or-parent)
  4811. (remove-hook 'org-blocker-hook
  4812. 'org-block-todo-from-children-or-siblings-or-parent))
  4813. (if org-enforce-todo-checkbox-dependencies
  4814. (add-hook 'org-blocker-hook
  4815. 'org-block-todo-from-checkboxes)
  4816. (remove-hook 'org-blocker-hook
  4817. 'org-block-todo-from-checkboxes))
  4818. ;; Align options lines
  4819. (org-set-local
  4820. 'align-mode-rules-list
  4821. '((org-in-buffer-settings
  4822. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4823. (modes . '(org-mode)))))
  4824. ;; Imenu
  4825. (org-set-local 'imenu-create-index-function
  4826. 'org-imenu-get-tree)
  4827. ;; Make isearch reveal context
  4828. (if (or (featurep 'xemacs)
  4829. (not (boundp 'outline-isearch-open-invisible-function)))
  4830. ;; Emacs 21 and XEmacs make use of the hook
  4831. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4832. ;; Emacs 22 deals with this through a special variable
  4833. (org-set-local 'outline-isearch-open-invisible-function
  4834. (lambda (&rest ignore) (org-show-context 'isearch))))
  4835. ;; Setup the pcomplete hooks
  4836. (set (make-local-variable 'pcomplete-command-completion-function)
  4837. 'org-pcomplete-initial)
  4838. (set (make-local-variable 'pcomplete-command-name-function)
  4839. 'org-command-at-point)
  4840. (set (make-local-variable 'pcomplete-default-completion-function)
  4841. 'ignore)
  4842. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4843. 'org-parse-arguments)
  4844. (set (make-local-variable 'pcomplete-termination-string) "")
  4845. (when (>= emacs-major-version 23)
  4846. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4847. ;; If empty file that did not turn on org-mode automatically, make it to.
  4848. (if (and org-insert-mode-line-in-empty-file
  4849. (org-called-interactively-p 'any)
  4850. (= (point-min) (point-max)))
  4851. (insert "# -*- mode: org -*-\n\n"))
  4852. (unless org-inhibit-startup
  4853. (org-unmodified
  4854. (and org-startup-with-beamer-mode (org-beamer-mode))
  4855. (when org-startup-align-all-tables
  4856. (org-table-map-tables 'org-table-align 'quietly))
  4857. (when org-startup-with-inline-images
  4858. (org-display-inline-images))
  4859. (when org-startup-with-latex-preview
  4860. (org-toggle-latex-fragment))
  4861. (unless org-inhibit-startup-visibility-stuff
  4862. (org-set-startup-visibility))
  4863. (org-refresh-effort-properties)))
  4864. ;; Try to set org-hide correctly
  4865. (let ((foreground (org-find-invisible-foreground)))
  4866. (if foreground
  4867. (set-face-foreground 'org-hide foreground))))
  4868. ;; Update `customize-package-emacs-version-alist'
  4869. (add-to-list 'customize-package-emacs-version-alist
  4870. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4871. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4872. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4873. (defvar org-mode-transpose-word-syntax-table
  4874. (let ((st (make-syntax-table text-mode-syntax-table)))
  4875. (mapc (lambda(c) (modify-syntax-entry
  4876. (string-to-char (car c)) "w p" st))
  4877. org-emphasis-alist)
  4878. st))
  4879. (when (fboundp 'abbrev-table-put)
  4880. (abbrev-table-put org-mode-abbrev-table
  4881. :parents (list text-mode-abbrev-table)))
  4882. (defun org-find-invisible-foreground ()
  4883. (let ((candidates (remove
  4884. "unspecified-bg"
  4885. (nconc
  4886. (list (face-background 'default)
  4887. (face-background 'org-default))
  4888. (mapcar
  4889. (lambda (alist)
  4890. (when (boundp alist)
  4891. (cdr (assoc 'background-color (symbol-value alist)))))
  4892. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4893. (list (face-foreground 'org-hide))))))
  4894. (car (remove nil candidates))))
  4895. (defun org-current-time (&optional rounding-minutes past)
  4896. "Current time, possibly rounded to ROUNDING-MINUTES.
  4897. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4898. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4899. the rounding returns a past time."
  4900. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4901. (car org-time-stamp-rounding-minutes)))
  4902. (time (decode-time)) res)
  4903. (if (< r 1)
  4904. (current-time)
  4905. (setq res
  4906. (apply 'encode-time
  4907. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4908. (nthcdr 2 time))))
  4909. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4910. (seconds-to-time (- (org-float-time res) (* r 60)))
  4911. res))))
  4912. (defun org-today ()
  4913. "Return today date, considering `org-extend-today-until'."
  4914. (time-to-days
  4915. (time-subtract (current-time)
  4916. (list 0 (* 3600 org-extend-today-until) 0))))
  4917. ;;;; Font-Lock stuff, including the activators
  4918. (defvar org-mouse-map (make-sparse-keymap))
  4919. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4920. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4921. (when org-mouse-1-follows-link
  4922. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4923. (when org-tab-follows-link
  4924. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4925. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4926. (require 'font-lock)
  4927. (defconst org-non-link-chars "]\t\n\r<>")
  4928. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4929. "file+sys" "news" "shell" "elisp" "doi" "message"
  4930. "help"))
  4931. (defvar org-link-types-re nil
  4932. "Matches a link that has a url-like prefix like \"http:\"")
  4933. (defvar org-link-re-with-space nil
  4934. "Matches a link with spaces, optional angular brackets around it.")
  4935. (defvar org-link-re-with-space2 nil
  4936. "Matches a link with spaces, optional angular brackets around it.")
  4937. (defvar org-link-re-with-space3 nil
  4938. "Matches a link with spaces, only for internal part in bracket links.")
  4939. (defvar org-angle-link-re nil
  4940. "Matches link with angular brackets, spaces are allowed.")
  4941. (defvar org-plain-link-re nil
  4942. "Matches plain link, without spaces.")
  4943. (defvar org-bracket-link-regexp nil
  4944. "Matches a link in double brackets.")
  4945. (defvar org-bracket-link-analytic-regexp nil
  4946. "Regular expression used to analyze links.
  4947. Here is what the match groups contain after a match:
  4948. 1: http:
  4949. 2: http
  4950. 3: path
  4951. 4: [desc]
  4952. 5: desc")
  4953. (defvar org-bracket-link-analytic-regexp++ nil
  4954. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4955. (defvar org-any-link-re nil
  4956. "Regular expression matching any link.")
  4957. (defconst org-match-sexp-depth 3
  4958. "Number of stacked braces for sub/superscript matching.")
  4959. (defun org-create-multibrace-regexp (left right n)
  4960. "Create a regular expression which will match a balanced sexp.
  4961. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4962. as single character strings.
  4963. The regexp returned will match the entire expression including the
  4964. delimiters. It will also define a single group which contains the
  4965. match except for the outermost delimiters. The maximum depth of
  4966. stacked delimiters is N. Escaping delimiters is not possible."
  4967. (let* ((nothing (concat "[^" left right "]*?"))
  4968. (or "\\|")
  4969. (re nothing)
  4970. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4971. (while (> n 1)
  4972. (setq n (1- n)
  4973. re (concat re or next)
  4974. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4975. (concat left "\\(" re "\\)" right)))
  4976. (defconst org-match-substring-regexp
  4977. (concat
  4978. "\\(\\S-\\)\\([_^]\\)\\("
  4979. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4980. "\\|"
  4981. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4982. "\\|"
  4983. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4984. "The regular expression matching a sub- or superscript.")
  4985. (defconst org-match-substring-with-braces-regexp
  4986. (concat
  4987. "\\(\\S-\\)\\([_^]\\)"
  4988. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4989. "The regular expression matching a sub- or superscript, forcing braces.")
  4990. (defun org-make-link-regexps ()
  4991. "Update the link regular expressions.
  4992. This should be called after the variable `org-link-types' has changed."
  4993. (let ((types-re (regexp-opt org-link-types t)))
  4994. (setq org-link-types-re
  4995. (concat "\\`" types-re ":")
  4996. org-link-re-with-space
  4997. (concat "<?" types-re ":"
  4998. "\\([^" org-non-link-chars " ]"
  4999. "[^" org-non-link-chars "]*"
  5000. "[^" org-non-link-chars " ]\\)>?")
  5001. org-link-re-with-space2
  5002. (concat "<?" types-re ":"
  5003. "\\([^" org-non-link-chars " ]"
  5004. "[^\t\n\r]*"
  5005. "[^" org-non-link-chars " ]\\)>?")
  5006. org-link-re-with-space3
  5007. (concat "<?" types-re ":"
  5008. "\\([^" org-non-link-chars " ]"
  5009. "[^\t\n\r]*\\)")
  5010. org-angle-link-re
  5011. (concat "<" types-re ":"
  5012. "\\([^" org-non-link-chars " ]"
  5013. "[^" org-non-link-chars "]*"
  5014. "\\)>")
  5015. org-plain-link-re
  5016. (concat
  5017. "\\<" types-re ":"
  5018. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5019. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5020. org-bracket-link-regexp
  5021. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5022. org-bracket-link-analytic-regexp
  5023. (concat
  5024. "\\[\\["
  5025. "\\(" types-re ":\\)?"
  5026. "\\([^]]+\\)"
  5027. "\\]"
  5028. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5029. "\\]")
  5030. org-bracket-link-analytic-regexp++
  5031. (concat
  5032. "\\[\\["
  5033. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5034. "\\([^]]+\\)"
  5035. "\\]"
  5036. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5037. "\\]")
  5038. org-any-link-re
  5039. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5040. org-angle-link-re "\\)\\|\\("
  5041. org-plain-link-re "\\)"))))
  5042. (org-make-link-regexps)
  5043. (defvar org-emph-face nil)
  5044. (defun org-do-emphasis-faces (limit)
  5045. "Run through the buffer and emphasize strings."
  5046. (let (rtn a)
  5047. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5048. (let* ((border (char-after (match-beginning 3)))
  5049. (bre (regexp-quote (char-to-string border))))
  5050. (if (and (not (= border (char-after (match-beginning 4))))
  5051. (not (save-match-data
  5052. (string-match (concat bre ".*" bre)
  5053. (replace-regexp-in-string
  5054. "\n" " "
  5055. (substring (match-string 2) 1 -1))))))
  5056. (progn
  5057. (setq rtn t)
  5058. (setq a (assoc (match-string 3) org-emphasis-alist))
  5059. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5060. 'face
  5061. (nth 1 a))
  5062. (and (nth 2 a)
  5063. (org-remove-flyspell-overlays-in
  5064. (match-beginning 0) (match-end 0)))
  5065. (add-text-properties (match-beginning 2) (match-end 2)
  5066. '(font-lock-multiline t org-emphasis t))
  5067. (when org-hide-emphasis-markers
  5068. (add-text-properties (match-end 4) (match-beginning 5)
  5069. '(invisible org-link))
  5070. (add-text-properties (match-beginning 3) (match-end 3)
  5071. '(invisible org-link))))))
  5072. (goto-char (1+ (match-beginning 0))))
  5073. rtn))
  5074. (defun org-emphasize (&optional char)
  5075. "Insert or change an emphasis, i.e. a font like bold or italic.
  5076. If there is an active region, change that region to a new emphasis.
  5077. If there is no region, just insert the marker characters and position
  5078. the cursor between them.
  5079. CHAR should be the marker character. If it is a space, it means to
  5080. remove the emphasis of the selected region.
  5081. If CHAR is not given (for example in an interactive call) it will be
  5082. prompted for."
  5083. (interactive)
  5084. (let ((erc org-emphasis-regexp-components)
  5085. (prompt "")
  5086. (string "") beg end move c s)
  5087. (if (org-region-active-p)
  5088. (setq beg (region-beginning) end (region-end)
  5089. string (buffer-substring beg end))
  5090. (setq move t))
  5091. (unless char
  5092. (message "Emphasis marker or tag: [%s]"
  5093. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5094. (setq char (read-char-exclusive)))
  5095. (if (equal char ?\ )
  5096. (setq s "" move nil)
  5097. (unless (assoc (char-to-string char) org-emphasis-alist)
  5098. (user-error "No such emphasis marker: \"%c\"" char))
  5099. (setq s (char-to-string char)))
  5100. (while (and (> (length string) 1)
  5101. (equal (substring string 0 1) (substring string -1))
  5102. (assoc (substring string 0 1) org-emphasis-alist))
  5103. (setq string (substring string 1 -1)))
  5104. (setq string (concat s string s))
  5105. (if beg (delete-region beg end))
  5106. (unless (or (bolp)
  5107. (string-match (concat "[" (nth 0 erc) "\n]")
  5108. (char-to-string (char-before (point)))))
  5109. (insert " "))
  5110. (unless (or (eobp)
  5111. (string-match (concat "[" (nth 1 erc) "\n]")
  5112. (char-to-string (char-after (point)))))
  5113. (insert " ") (backward-char 1))
  5114. (insert string)
  5115. (and move (backward-char 1))))
  5116. (defconst org-nonsticky-props
  5117. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5118. (defsubst org-rear-nonsticky-at (pos)
  5119. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5120. (defun org-activate-plain-links (limit)
  5121. "Add link properties for plain links."
  5122. (let (f hl)
  5123. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5124. (not (member 'org-tag
  5125. (get-text-property (1- (match-beginning 0)) 'face)))
  5126. (not (org-in-src-block-p)))
  5127. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5128. (setq f (get-text-property (match-beginning 0) 'face))
  5129. (setq hl (org-match-string-no-properties 0))
  5130. (if (or (eq f 'org-tag)
  5131. (and (listp f) (memq 'org-tag f)))
  5132. nil
  5133. (add-text-properties (match-beginning 0) (match-end 0)
  5134. (list 'mouse-face 'highlight
  5135. 'face 'org-link
  5136. 'htmlize-link `(:uri ,hl)
  5137. 'keymap org-mouse-map))
  5138. (org-rear-nonsticky-at (match-end 0)))
  5139. t)))
  5140. (defun org-activate-code (limit)
  5141. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5142. (progn
  5143. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5144. (remove-text-properties (match-beginning 0) (match-end 0)
  5145. '(display t invisible t intangible t))
  5146. t)))
  5147. (defcustom org-src-fontify-natively t
  5148. "When non-nil, fontify code in code blocks."
  5149. :type 'boolean
  5150. :version "24.4"
  5151. :package-version '(Org . "8.3")
  5152. :group 'org-appearance
  5153. :group 'org-babel)
  5154. (defcustom org-allow-promoting-top-level-subtree nil
  5155. "When non-nil, allow promoting a top level subtree.
  5156. The leading star of the top level headline will be replaced
  5157. by a #."
  5158. :type 'boolean
  5159. :version "24.1"
  5160. :group 'org-appearance)
  5161. (defun org-fontify-meta-lines-and-blocks (limit)
  5162. (condition-case nil
  5163. (org-fontify-meta-lines-and-blocks-1 limit)
  5164. (error (message "org-mode fontification error"))))
  5165. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5166. "Fontify #+ lines and blocks."
  5167. (let ((case-fold-search t))
  5168. (if (re-search-forward
  5169. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5170. limit t)
  5171. (let ((beg (match-beginning 0))
  5172. (block-start (match-end 0))
  5173. (block-end nil)
  5174. (lang (match-string 7))
  5175. (beg1 (line-beginning-position 2))
  5176. (dc1 (downcase (match-string 2)))
  5177. (dc3 (downcase (match-string 3)))
  5178. end end1 quoting block-type ovl)
  5179. (cond
  5180. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5181. ;; a single line of backend-specific content
  5182. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5183. (remove-text-properties (match-beginning 0) (match-end 0)
  5184. '(display t invisible t intangible t))
  5185. (add-text-properties (match-beginning 1) (match-end 3)
  5186. '(font-lock-fontified t face org-meta-line))
  5187. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5188. '(font-lock-fontified t face org-block))
  5189. ; for backend-specific code
  5190. t)
  5191. ((and (match-end 4) (equal dc3 "+begin"))
  5192. ;; Truly a block
  5193. (setq block-type (downcase (match-string 5))
  5194. quoting (member block-type org-protecting-blocks))
  5195. (when (re-search-forward
  5196. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5197. nil t) ;; on purpose, we look further than LIMIT
  5198. (setq end (min (point-max) (match-end 0))
  5199. end1 (min (point-max) (1- (match-beginning 0))))
  5200. (setq block-end (match-beginning 0))
  5201. (when quoting
  5202. (org-remove-flyspell-overlays-in beg1 end1)
  5203. (remove-text-properties beg end
  5204. '(display t invisible t intangible t)))
  5205. (add-text-properties
  5206. beg end '(font-lock-fontified t font-lock-multiline t))
  5207. (add-text-properties beg beg1 '(face org-meta-line))
  5208. (org-remove-flyspell-overlays-in beg beg1)
  5209. (add-text-properties ; For end_src
  5210. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5211. (org-remove-flyspell-overlays-in end1 end)
  5212. (cond
  5213. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5214. (org-src-font-lock-fontify-block lang block-start block-end)
  5215. (add-text-properties beg1 block-end '(src-block t)))
  5216. (quoting
  5217. (add-text-properties beg1 (min (point-max) (1+ end1))
  5218. '(face org-block))) ; end of source block
  5219. ((not org-fontify-quote-and-verse-blocks))
  5220. ((string= block-type "quote")
  5221. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5222. ((string= block-type "verse")
  5223. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5224. (add-text-properties beg beg1 '(face org-block-begin-line))
  5225. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5226. '(face org-block-end-line))
  5227. t))
  5228. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5229. (org-remove-flyspell-overlays-in
  5230. (match-beginning 0)
  5231. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5232. (add-text-properties
  5233. beg (match-end 3)
  5234. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5235. '(font-lock-fontified t invisible t)
  5236. '(font-lock-fontified t face org-document-info-keyword)))
  5237. (add-text-properties
  5238. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5239. (if (string-equal dc1 "+title:")
  5240. '(font-lock-fontified t face org-document-title)
  5241. '(font-lock-fontified t face org-document-info))))
  5242. ((or (equal dc1 "+results")
  5243. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5244. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5245. "+call:" "+header:" "+headers:" "+name:"))
  5246. (and (match-end 4) (equal dc3 "+attr")))
  5247. (org-remove-flyspell-overlays-in
  5248. (match-beginning 0)
  5249. (if (equal "+caption:" dc1) (match-end 2) (match-end 0)))
  5250. (add-text-properties
  5251. beg (match-end 0)
  5252. '(font-lock-fontified t face org-meta-line))
  5253. t)
  5254. ((member dc3 '(" " ""))
  5255. (org-remove-flyspell-overlays-in beg (match-end 0))
  5256. (add-text-properties
  5257. beg (match-end 0)
  5258. '(font-lock-fontified t face font-lock-comment-face)))
  5259. (t ;; just any other in-buffer setting, but not indented
  5260. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5261. (add-text-properties
  5262. beg (match-end 0)
  5263. '(font-lock-fontified t face org-meta-line))
  5264. t))))))
  5265. (defun org-fontify-drawers (limit)
  5266. "Fontify drawers."
  5267. (when (re-search-forward org-drawer-regexp limit t)
  5268. (add-text-properties
  5269. (match-beginning 0) (match-end 0)
  5270. '(font-lock-fontified t face org-special-keyword))
  5271. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5272. t))
  5273. (defun org-fontify-macros (limit)
  5274. "Fontify macros."
  5275. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5276. (add-text-properties
  5277. (match-beginning 0) (match-end 0)
  5278. '(font-lock-fontified t face org-macro))
  5279. (when org-hide-macro-markers
  5280. (add-text-properties (match-end 2) (match-beginning 2)
  5281. '(invisible t))
  5282. (add-text-properties (match-beginning 1) (match-end 1)
  5283. '(invisible t)))
  5284. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5285. t))
  5286. (defun org-activate-angle-links (limit)
  5287. "Add text properties for angle links."
  5288. (if (and (re-search-forward org-angle-link-re limit t)
  5289. (not (org-in-src-block-p)))
  5290. (progn
  5291. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5292. (add-text-properties (match-beginning 0) (match-end 0)
  5293. (list 'mouse-face 'highlight
  5294. 'keymap org-mouse-map))
  5295. (org-rear-nonsticky-at (match-end 0))
  5296. t)))
  5297. (defun org-activate-footnote-links (limit)
  5298. "Add text properties for footnotes."
  5299. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5300. (when fn
  5301. (let* ((beg (nth 1 fn))
  5302. (end (nth 2 fn))
  5303. (label (car fn))
  5304. (referencep (/= (line-beginning-position) beg)))
  5305. (when (and referencep (nth 3 fn))
  5306. (save-excursion
  5307. (goto-char beg)
  5308. (search-forward (or label "fn:"))
  5309. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5310. (add-text-properties beg end
  5311. (list 'mouse-face 'highlight
  5312. 'keymap org-mouse-map
  5313. 'help-echo
  5314. (if referencep "Footnote reference"
  5315. "Footnote definition")
  5316. 'font-lock-fontified t
  5317. 'font-lock-multiline t
  5318. 'face 'org-footnote))))))
  5319. (defun org-activate-bracket-links (limit)
  5320. "Add text properties for bracketed links."
  5321. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5322. (not (org-in-src-block-p)))
  5323. (let* ((hl (org-match-string-no-properties 1))
  5324. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5325. (ip (org-maybe-intangible
  5326. (list 'invisible 'org-link
  5327. 'keymap org-mouse-map 'mouse-face 'highlight
  5328. 'font-lock-multiline t 'help-echo help
  5329. 'htmlize-link `(:uri ,hl))))
  5330. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5331. 'font-lock-multiline t 'help-echo help
  5332. 'htmlize-link `(:uri ,hl))))
  5333. ;; We need to remove the invisible property here. Table narrowing
  5334. ;; may have made some of this invisible.
  5335. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5336. (remove-text-properties (match-beginning 0) (match-end 0)
  5337. '(invisible nil))
  5338. (if (match-end 3)
  5339. (progn
  5340. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5341. (org-rear-nonsticky-at (match-beginning 3))
  5342. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5343. (org-rear-nonsticky-at (match-end 3))
  5344. (add-text-properties (match-end 3) (match-end 0) ip)
  5345. (org-rear-nonsticky-at (match-end 0)))
  5346. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5347. (org-rear-nonsticky-at (match-beginning 1))
  5348. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5349. (org-rear-nonsticky-at (match-end 1))
  5350. (add-text-properties (match-end 1) (match-end 0) ip)
  5351. (org-rear-nonsticky-at (match-end 0)))
  5352. t)))
  5353. (defun org-activate-dates (limit)
  5354. "Add text properties for dates."
  5355. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5356. (not (equal (char-before (match-beginning 0)) 91)))
  5357. (progn
  5358. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5359. (add-text-properties (match-beginning 0) (match-end 0)
  5360. (list 'mouse-face 'highlight
  5361. 'keymap org-mouse-map))
  5362. (org-rear-nonsticky-at (match-end 0))
  5363. (when org-display-custom-times
  5364. (if (match-end 3)
  5365. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5366. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5367. t)))
  5368. (defvar org-target-link-regexp nil
  5369. "Regular expression matching radio targets in plain text.")
  5370. (make-variable-buffer-local 'org-target-link-regexp)
  5371. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5372. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5373. border border border))
  5374. "Regular expression matching a link target.")
  5375. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5376. "Regular expression matching a radio target.")
  5377. (defconst org-any-target-regexp
  5378. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5379. "Regular expression matching any target.")
  5380. (defun org-activate-target-links (limit)
  5381. "Add text properties for target matches."
  5382. (when org-target-link-regexp
  5383. (let ((case-fold-search t))
  5384. (if (re-search-forward org-target-link-regexp limit t)
  5385. (progn
  5386. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5387. (add-text-properties (match-beginning 1) (match-end 1)
  5388. (list 'mouse-face 'highlight
  5389. 'keymap org-mouse-map
  5390. 'help-echo "Radio target link"
  5391. 'org-linked-text t))
  5392. (org-rear-nonsticky-at (match-end 1))
  5393. t)))))
  5394. (defun org-update-radio-target-regexp ()
  5395. "Find all radio targets in this file and update the regular expression.
  5396. Also refresh fontification if needed."
  5397. (interactive)
  5398. (let ((old-regexp org-target-link-regexp)
  5399. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5400. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5401. (targets
  5402. (org-with-wide-buffer
  5403. (goto-char (point-min))
  5404. (let (rtn)
  5405. (while (re-search-forward org-radio-target-regexp nil t)
  5406. ;; Make sure point is really within the object.
  5407. (backward-char)
  5408. (let ((obj (org-element-context)))
  5409. (when (eq (org-element-type obj) 'radio-target)
  5410. (add-to-list 'rtn (org-element-property :value obj)))))
  5411. rtn))))
  5412. (setq org-target-link-regexp
  5413. (and targets
  5414. (concat before-re
  5415. (mapconcat
  5416. (lambda (x)
  5417. (replace-regexp-in-string
  5418. " +" "\\s-+" (regexp-quote x) t t))
  5419. targets
  5420. "\\|")
  5421. after-re)))
  5422. (unless (equal old-regexp org-target-link-regexp)
  5423. ;; Clean-up cache.
  5424. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5425. ((not org-target-link-regexp) old-regexp)
  5426. (t
  5427. (concat before-re
  5428. (mapconcat
  5429. (lambda (re)
  5430. (substring re (length before-re)
  5431. (- (length after-re))))
  5432. (list old-regexp org-target-link-regexp)
  5433. "\\|")
  5434. after-re)))))
  5435. (org-with-wide-buffer
  5436. (goto-char (point-min))
  5437. (while (re-search-forward regexp nil t)
  5438. (org-element-cache-refresh (match-beginning 1)))))
  5439. ;; Re fontify buffer.
  5440. (when (memq 'radio org-highlight-links)
  5441. (org-restart-font-lock)))))
  5442. (defun org-hide-wide-columns (limit)
  5443. (let (s e)
  5444. (setq s (text-property-any (point) (or limit (point-max))
  5445. 'org-cwidth t))
  5446. (when s
  5447. (setq e (next-single-property-change s 'org-cwidth))
  5448. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5449. (goto-char e)
  5450. t)))
  5451. (defvar org-latex-and-related-regexp nil
  5452. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5453. (defun org-compute-latex-and-related-regexp ()
  5454. "Compute regular expression for LaTeX, entities and sub/superscript.
  5455. Result depends on variable `org-highlight-latex-and-related'."
  5456. (org-set-local
  5457. 'org-latex-and-related-regexp
  5458. (let* ((re-sub
  5459. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5460. ((eq org-use-sub-superscripts '{})
  5461. (list org-match-substring-with-braces-regexp))
  5462. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5463. (re-latex
  5464. (when (memq 'latex org-highlight-latex-and-related)
  5465. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5466. (delq nil
  5467. (mapcar (lambda (x)
  5468. (and (member (car x) matchers) (nth 1 x)))
  5469. org-latex-regexps)))))
  5470. (re-entities
  5471. (when (memq 'entities org-highlight-latex-and-related)
  5472. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5473. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5474. (defun org-do-latex-and-related (limit)
  5475. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5476. LIMIT bounds the search for syntax to highlight. Stop at first
  5477. highlighted object, if any. Return t if some highlighting was
  5478. done, nil otherwise."
  5479. (when (org-string-nw-p org-latex-and-related-regexp)
  5480. (catch 'found
  5481. (while (re-search-forward org-latex-and-related-regexp limit t)
  5482. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5483. 'face))
  5484. '(org-code org-verbatim underline))
  5485. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5486. '(?_ ?^))
  5487. 1
  5488. 0)))
  5489. (font-lock-prepend-text-property
  5490. (+ offset (match-beginning 0)) (match-end 0)
  5491. 'face 'org-latex-and-related)
  5492. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5493. '(font-lock-multiline t)))
  5494. (throw 'found t)))
  5495. nil)))
  5496. (defun org-restart-font-lock ()
  5497. "Restart `font-lock-mode', to force refontification."
  5498. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5499. (font-lock-mode -1)
  5500. (font-lock-mode 1)))
  5501. (defun org-activate-tags (limit)
  5502. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5503. (progn
  5504. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5505. (add-text-properties (match-beginning 1) (match-end 1)
  5506. (list 'mouse-face 'highlight
  5507. 'keymap org-mouse-map))
  5508. (org-rear-nonsticky-at (match-end 1))
  5509. t)))
  5510. (defun org-outline-level ()
  5511. "Compute the outline level of the heading at point.
  5512. If this is called at a normal headline, the level is the number of stars.
  5513. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5514. (org-with-wide-buffer
  5515. (end-of-line)
  5516. (if (re-search-backward org-outline-regexp-bol nil t)
  5517. (1- (- (match-end 0) (match-beginning 0)))
  5518. 0)))
  5519. (defvar org-font-lock-keywords nil)
  5520. (defsubst org-re-property (property &optional literal allow-null)
  5521. "Return a regexp matching a PROPERTY line.
  5522. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5523. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5524. non-nil, match properties even without a value.
  5525. Match group 3 is set to the value when it exists. If there is no
  5526. value and ALLOW-NULL is non-nil, it is set to the empty string."
  5527. (concat
  5528. "^\\(?4:[ \t]*\\)"
  5529. (format "\\(?1::\\(?2:%s\\):\\)"
  5530. (if literal property (regexp-quote property)))
  5531. (if allow-null
  5532. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$"
  5533. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$")))
  5534. (defconst org-property-re
  5535. (org-re-property "\\S-+" 'literal t)
  5536. "Regular expression matching a property line.
  5537. There are four matching groups:
  5538. 1: :PROPKEY: including the leading and trailing colon,
  5539. 2: PROPKEY without the leading and trailing colon,
  5540. 3: PROPVAL without leading or trailing spaces,
  5541. 4: the indentation of the current line,
  5542. 5: trailing whitespace.")
  5543. (defvar org-font-lock-hook nil
  5544. "Functions to be called for special font lock stuff.")
  5545. (defvar org-font-lock-set-keywords-hook nil
  5546. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5547. This is called after `org-font-lock-extra-keywords' is defined, but before
  5548. it is installed to be used by font lock. This can be useful if something
  5549. needs to be inserted at a specific position in the font-lock sequence.")
  5550. (defun org-font-lock-hook (limit)
  5551. "Run `org-font-lock-hook' within LIMIT."
  5552. (run-hook-with-args 'org-font-lock-hook limit))
  5553. (defun org-set-font-lock-defaults ()
  5554. "Set font lock defaults for the current buffer."
  5555. (let* ((em org-fontify-emphasized-text)
  5556. (lk org-highlight-links)
  5557. (org-font-lock-extra-keywords
  5558. (list
  5559. ;; Call the hook
  5560. '(org-font-lock-hook)
  5561. ;; Headlines
  5562. `(,(if org-fontify-whole-heading-line
  5563. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5564. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5565. (1 (org-get-level-face 1))
  5566. (2 (org-get-level-face 2))
  5567. (3 (org-get-level-face 3)))
  5568. ;; Table lines
  5569. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5570. (1 'org-table t))
  5571. ;; Table internals
  5572. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5573. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5574. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5575. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5576. ;; Drawers
  5577. '(org-fontify-drawers)
  5578. ;; Properties
  5579. (list org-property-re
  5580. '(1 'org-special-keyword t)
  5581. '(3 'org-property-value t))
  5582. ;; Links
  5583. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5584. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5585. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5586. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5587. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5588. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5589. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5590. ;; Targets.
  5591. (list org-any-target-regexp '(0 'org-target t))
  5592. ;; Diary sexps.
  5593. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5594. ;; Macro
  5595. '(org-fontify-macros)
  5596. '(org-hide-wide-columns (0 nil append))
  5597. ;; TODO keyword
  5598. (list (format org-heading-keyword-regexp-format
  5599. org-todo-regexp)
  5600. '(2 (org-get-todo-face 2) t))
  5601. ;; DONE
  5602. (if org-fontify-done-headline
  5603. (list (format org-heading-keyword-regexp-format
  5604. (concat
  5605. "\\(?:"
  5606. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5607. "\\)"))
  5608. '(2 'org-headline-done t))
  5609. nil)
  5610. ;; Priorities
  5611. '(org-font-lock-add-priority-faces)
  5612. ;; Tags
  5613. '(org-font-lock-add-tag-faces)
  5614. ;; Tags groups
  5615. (if (and org-group-tags org-tag-groups-alist)
  5616. (list (concat org-outline-regexp-bol ".+\\(:"
  5617. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5618. ":\\).*$")
  5619. '(1 'org-tag-group prepend)))
  5620. ;; Special keywords
  5621. (list (concat "\\<" org-comment-string) '(0 'org-special-keyword t))
  5622. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5623. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5624. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5625. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5626. ;; Emphasis
  5627. (if em
  5628. (if (featurep 'xemacs)
  5629. '(org-do-emphasis-faces (0 nil append))
  5630. '(org-do-emphasis-faces)))
  5631. ;; Checkboxes
  5632. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5633. 1 'org-checkbox prepend)
  5634. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5635. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5636. (0 (org-get-checkbox-statistics-face) t)))
  5637. ;; Description list items
  5638. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5639. 1 'org-list-dt prepend)
  5640. ;; ARCHIVEd headings
  5641. (list (concat
  5642. org-outline-regexp-bol
  5643. "\\(.*:" org-archive-tag ":.*\\)")
  5644. '(1 'org-archived prepend))
  5645. ;; Specials
  5646. '(org-do-latex-and-related)
  5647. '(org-fontify-entities)
  5648. '(org-raise-scripts)
  5649. ;; Code
  5650. '(org-activate-code (1 'org-code t))
  5651. ;; COMMENT
  5652. (list (format
  5653. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5654. org-todo-regexp
  5655. org-comment-string)
  5656. '(9 'org-special-keyword t))
  5657. ;; Blocks and meta lines
  5658. '(org-fontify-meta-lines-and-blocks))))
  5659. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5660. (run-hooks 'org-font-lock-set-keywords-hook)
  5661. ;; Now set the full font-lock-keywords
  5662. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5663. (org-set-local 'font-lock-defaults
  5664. '(org-font-lock-keywords t nil nil backward-paragraph))
  5665. (kill-local-variable 'font-lock-keywords) nil))
  5666. (defun org-toggle-pretty-entities ()
  5667. "Toggle the composition display of entities as UTF8 characters."
  5668. (interactive)
  5669. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5670. (org-restart-font-lock)
  5671. (if org-pretty-entities
  5672. (message "Entities are now displayed as UTF8 characters")
  5673. (save-restriction
  5674. (widen)
  5675. (org-decompose-region (point-min) (point-max))
  5676. (message "Entities are now displayed as plain text"))))
  5677. (defvar org-custom-properties-overlays nil
  5678. "List of overlays used for custom properties.")
  5679. (make-variable-buffer-local 'org-custom-properties-overlays)
  5680. (defun org-toggle-custom-properties-visibility ()
  5681. "Display or hide properties in `org-custom-properties'."
  5682. (interactive)
  5683. (if org-custom-properties-overlays
  5684. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5685. (setq org-custom-properties-overlays nil))
  5686. (when org-custom-properties
  5687. (org-with-wide-buffer
  5688. (goto-char (point-min))
  5689. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5690. (while (re-search-forward regexp nil t)
  5691. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5692. (when (and end (< (point) end))
  5693. ;; Hide first custom property in current drawer.
  5694. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5695. (overlay-put o 'invisible t)
  5696. (overlay-put o 'org-custom-property t)
  5697. (push o org-custom-properties-overlays))
  5698. ;; Hide additional custom properties in the same drawer.
  5699. (while (re-search-forward regexp end t)
  5700. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5701. (overlay-put o 'invisible t)
  5702. (overlay-put o 'org-custom-property t)
  5703. (push o org-custom-properties-overlays)))))
  5704. ;; Each entry is limited to a single property drawer.
  5705. (outline-next-heading)))))))
  5706. (defun org-fontify-entities (limit)
  5707. "Find an entity to fontify."
  5708. (let (ee)
  5709. (when org-pretty-entities
  5710. (catch 'match
  5711. (while (re-search-forward
  5712. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5713. limit t)
  5714. (if (and (not (org-at-comment-p))
  5715. (setq ee (org-entity-get (match-string 1)))
  5716. (= (length (nth 6 ee)) 1))
  5717. (let*
  5718. ((end (if (equal (match-string 2) "{}")
  5719. (match-end 2)
  5720. (match-end 1))))
  5721. (add-text-properties
  5722. (match-beginning 0) end
  5723. (list 'font-lock-fontified t))
  5724. (compose-region (match-beginning 0) end
  5725. (nth 6 ee) nil)
  5726. (backward-char 1)
  5727. (throw 'match t))))
  5728. nil))))
  5729. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5730. "Fontify string S like in Org-mode."
  5731. (with-temp-buffer
  5732. (insert s)
  5733. (let ((org-odd-levels-only odd-levels))
  5734. (org-mode)
  5735. (font-lock-ensure)
  5736. (buffer-string))))
  5737. (defvar org-m nil)
  5738. (defvar org-l nil)
  5739. (defvar org-f nil)
  5740. (defun org-get-level-face (n)
  5741. "Get the right face for match N in font-lock matching of headlines."
  5742. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5743. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5744. (if org-cycle-level-faces
  5745. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5746. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5747. (cond
  5748. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5749. ((eq n 2) org-f)
  5750. (t (if org-level-color-stars-only nil org-f))))
  5751. (defun org-get-todo-face (kwd)
  5752. "Get the right face for a TODO keyword KWD.
  5753. If KWD is a number, get the corresponding match group."
  5754. (if (numberp kwd) (setq kwd (match-string kwd)))
  5755. (or (org-face-from-face-or-color
  5756. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5757. (and (member kwd org-done-keywords) 'org-done)
  5758. 'org-todo))
  5759. (defun org-face-from-face-or-color (context inherit face-or-color)
  5760. "Create a face list that inherits INHERIT, but sets the foreground color.
  5761. When FACE-OR-COLOR is not a string, just return it."
  5762. (if (stringp face-or-color)
  5763. (list :inherit inherit
  5764. (cdr (assoc context org-faces-easy-properties))
  5765. face-or-color)
  5766. face-or-color))
  5767. (defun org-font-lock-add-tag-faces (limit)
  5768. "Add the special tag faces."
  5769. (when (and org-tag-faces org-tags-special-faces-re)
  5770. (while (re-search-forward org-tags-special-faces-re limit t)
  5771. (add-text-properties (match-beginning 1) (match-end 1)
  5772. (list 'face (org-get-tag-face 1)
  5773. 'font-lock-fontified t))
  5774. (backward-char 1))))
  5775. (defun org-font-lock-add-priority-faces (limit)
  5776. "Add the special priority faces."
  5777. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5778. (when (save-match-data (org-at-heading-p))
  5779. (add-text-properties
  5780. (match-beginning 0) (match-end 0)
  5781. (list 'face (or (org-face-from-face-or-color
  5782. 'priority 'org-priority
  5783. (cdr (assoc (char-after (match-beginning 1))
  5784. org-priority-faces)))
  5785. 'org-priority)
  5786. 'font-lock-fontified t)))))
  5787. (defun org-get-tag-face (kwd)
  5788. "Get the right face for a TODO keyword KWD.
  5789. If KWD is a number, get the corresponding match group."
  5790. (if (numberp kwd) (setq kwd (match-string kwd)))
  5791. (or (org-face-from-face-or-color
  5792. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5793. 'org-tag))
  5794. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5795. "Remove fontification and activation overlays from links."
  5796. (font-lock-default-unfontify-region beg end)
  5797. (let* ((buffer-undo-list t)
  5798. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5799. (inhibit-modification-hooks t)
  5800. deactivate-mark buffer-file-name buffer-file-truename)
  5801. (org-decompose-region beg end)
  5802. (remove-text-properties beg end
  5803. '(mouse-face t keymap t org-linked-text t
  5804. invisible t intangible t
  5805. org-emphasis t))
  5806. (org-remove-font-lock-display-properties beg end)))
  5807. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5808. ((raise 0.3) (height 0.7))
  5809. ((raise -0.5))
  5810. ((raise 0.5)))
  5811. "Display properties for showing superscripts and subscripts.")
  5812. (defun org-remove-font-lock-display-properties (beg end)
  5813. "Remove specific display properties that have been added by font lock.
  5814. The will remove the raise properties that are used to show superscripts
  5815. and subscripts."
  5816. (let (next prop)
  5817. (while (< beg end)
  5818. (setq next (next-single-property-change beg 'display nil end)
  5819. prop (get-text-property beg 'display))
  5820. (if (member prop org-script-display)
  5821. (put-text-property beg next 'display nil))
  5822. (setq beg next))))
  5823. (defun org-raise-scripts (limit)
  5824. "Add raise properties to sub/superscripts."
  5825. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5826. (if (re-search-forward
  5827. (if (eq org-use-sub-superscripts t)
  5828. org-match-substring-regexp
  5829. org-match-substring-with-braces-regexp)
  5830. limit t)
  5831. (let* ((pos (point)) table-p comment-p
  5832. (mpos (match-beginning 3))
  5833. (emph-p (get-text-property mpos 'org-emphasis))
  5834. (link-p (get-text-property mpos 'mouse-face))
  5835. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5836. (goto-char (point-at-bol))
  5837. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5838. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5839. (goto-char pos)
  5840. ;; Handle a_b^c
  5841. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5842. (if (or comment-p emph-p link-p keyw-p)
  5843. t
  5844. (put-text-property (match-beginning 3) (match-end 0)
  5845. 'display
  5846. (if (equal (char-after (match-beginning 2)) ?^)
  5847. (nth (if table-p 3 1) org-script-display)
  5848. (nth (if table-p 2 0) org-script-display)))
  5849. (add-text-properties (match-beginning 2) (match-end 2)
  5850. (list 'invisible t
  5851. 'org-dwidth t 'org-dwidth-n 1))
  5852. (if (and (eq (char-after (match-beginning 3)) ?{)
  5853. (eq (char-before (match-end 3)) ?}))
  5854. (progn
  5855. (add-text-properties
  5856. (match-beginning 3) (1+ (match-beginning 3))
  5857. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5858. (add-text-properties
  5859. (1- (match-end 3)) (match-end 3)
  5860. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5861. t)))))
  5862. ;;;; Visibility cycling, including org-goto and indirect buffer
  5863. ;;; Cycling
  5864. (defvar org-cycle-global-status nil)
  5865. (make-variable-buffer-local 'org-cycle-global-status)
  5866. (put 'org-cycle-global-status 'org-state t)
  5867. (defvar org-cycle-subtree-status nil)
  5868. (make-variable-buffer-local 'org-cycle-subtree-status)
  5869. (put 'org-cycle-subtree-status 'org-state t)
  5870. (defvar org-inlinetask-min-level)
  5871. (defun org-unlogged-message (&rest args)
  5872. "Display a message, but avoid logging it in the *Messages* buffer."
  5873. (let ((message-log-max nil))
  5874. (apply 'message args)))
  5875. ;;;###autoload
  5876. (defun org-cycle (&optional arg)
  5877. "TAB-action and visibility cycling for Org-mode.
  5878. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5879. is outline visibility cycling, but it also invokes other actions
  5880. in special contexts.
  5881. - When this function is called with a prefix argument, rotate the entire
  5882. buffer through 3 states (global cycling)
  5883. 1. OVERVIEW: Show only top-level headlines.
  5884. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5885. 3. SHOW ALL: Show everything.
  5886. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5887. determined by the variable `org-startup-folded', and by any VISIBILITY
  5888. properties in the buffer.
  5889. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5890. including any drawers.
  5891. - When inside a table, re-align the table and move to the next field.
  5892. - When point is at the beginning of a headline, rotate the subtree started
  5893. by this line through 3 different states (local cycling)
  5894. 1. FOLDED: Only the main headline is shown.
  5895. 2. CHILDREN: The main headline and the direct children are shown.
  5896. From this state, you can move to one of the children
  5897. and zoom in further.
  5898. 3. SUBTREE: Show the entire subtree, including body text.
  5899. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5900. - When point is at the beginning of an empty headline and the variable
  5901. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5902. of the headline by demoting and promoting it to likely levels. This
  5903. speeds up creation document structure by pressing TAB once or several
  5904. times right after creating a new headline.
  5905. - When there is a numeric prefix, go up to a heading with level ARG, do
  5906. a `show-subtree' and return to the previous cursor position. If ARG
  5907. is negative, go up that many levels.
  5908. - When point is not at the beginning of a headline, execute the global
  5909. binding for TAB, which is re-indenting the line. See the option
  5910. `org-cycle-emulate-tab' for details.
  5911. - Special case: if point is at the beginning of the buffer and there is
  5912. no headline in line 1, this function will act as if called with prefix arg
  5913. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5914. But only if also the variable `org-cycle-global-at-bob' is t."
  5915. (interactive "P")
  5916. (org-load-modules-maybe)
  5917. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5918. (and org-cycle-level-after-item/entry-creation
  5919. (or (org-cycle-level)
  5920. (org-cycle-item-indentation))))
  5921. (let* ((limit-level
  5922. (or org-cycle-max-level
  5923. (and (boundp 'org-inlinetask-min-level)
  5924. org-inlinetask-min-level
  5925. (1- org-inlinetask-min-level))))
  5926. (nstars (and limit-level
  5927. (if org-odd-levels-only
  5928. (and limit-level (1- (* limit-level 2)))
  5929. limit-level)))
  5930. (org-outline-regexp
  5931. (if (not (derived-mode-p 'org-mode))
  5932. outline-regexp
  5933. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5934. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5935. (not (looking-at org-outline-regexp))))
  5936. (org-cycle-hook
  5937. (if bob-special
  5938. (delq 'org-optimize-window-after-visibility-change
  5939. (copy-sequence org-cycle-hook))
  5940. org-cycle-hook))
  5941. (pos (point)))
  5942. (if (or bob-special (equal arg '(4)))
  5943. ;; special case: use global cycling
  5944. (setq arg t))
  5945. (cond
  5946. ((equal arg '(16))
  5947. (setq last-command 'dummy)
  5948. (org-set-startup-visibility)
  5949. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5950. ((equal arg '(64))
  5951. (show-all)
  5952. (org-unlogged-message "Entire buffer visible, including drawers"))
  5953. ;; Try cdlatex TAB completion
  5954. ((org-try-cdlatex-tab))
  5955. ;; Table: enter it or move to the next field.
  5956. ((org-at-table-p 'any)
  5957. (if (org-at-table.el-p)
  5958. (message "Use C-c ' to edit table.el tables")
  5959. (if arg (org-table-edit-field t)
  5960. (org-table-justify-field-maybe)
  5961. (call-interactively 'org-table-next-field))))
  5962. ((run-hook-with-args-until-success
  5963. 'org-tab-after-check-for-table-hook))
  5964. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5965. ((eq arg t) (org-cycle-internal-global))
  5966. ;; Drawers: delegate to `org-flag-drawer'.
  5967. ((save-excursion
  5968. (beginning-of-line 1)
  5969. (looking-at org-drawer-regexp))
  5970. (org-flag-drawer ; toggle block visibility
  5971. (not (get-char-property (match-end 0) 'invisible))))
  5972. ;; Show-subtree, ARG levels up from here.
  5973. ((integerp arg)
  5974. (save-excursion
  5975. (org-back-to-heading)
  5976. (outline-up-heading (if (< arg 0) (- arg)
  5977. (- (funcall outline-level) arg)))
  5978. (org-show-subtree)))
  5979. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5980. ((and (featurep 'org-inlinetask)
  5981. (org-inlinetask-at-task-p)
  5982. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5983. (org-inlinetask-toggle-visibility))
  5984. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5985. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5986. (save-excursion (beginning-of-line 1)
  5987. (looking-at org-outline-regexp)))
  5988. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5989. (org-cycle-internal-local))
  5990. ;; From there: TAB emulation and template completion.
  5991. (buffer-read-only (org-back-to-heading))
  5992. ((run-hook-with-args-until-success
  5993. 'org-tab-after-check-for-cycling-hook))
  5994. ((org-try-structure-completion))
  5995. ((run-hook-with-args-until-success
  5996. 'org-tab-before-tab-emulation-hook))
  5997. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5998. (or (not (bolp))
  5999. (not (looking-at org-outline-regexp))))
  6000. (call-interactively (global-key-binding "\t")))
  6001. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6002. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6003. (or (and (eq org-cycle-emulate-tab 'white)
  6004. (= (match-end 0) (point-at-eol)))
  6005. (and (eq org-cycle-emulate-tab 'whitestart)
  6006. (>= (match-end 0) pos))))
  6007. t
  6008. (eq org-cycle-emulate-tab t))
  6009. (call-interactively (global-key-binding "\t")))
  6010. (t (save-excursion
  6011. (org-back-to-heading)
  6012. (org-cycle)))))))
  6013. (defun org-cycle-internal-global ()
  6014. "Do the global cycling action."
  6015. ;; Hack to avoid display of messages for .org attachments in Gnus
  6016. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6017. (cond
  6018. ((and (eq last-command this-command)
  6019. (eq org-cycle-global-status 'overview))
  6020. ;; We just created the overview - now do table of contents
  6021. ;; This can be slow in very large buffers, so indicate action
  6022. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6023. (unless ga (org-unlogged-message "CONTENTS..."))
  6024. (org-content)
  6025. (unless ga (org-unlogged-message "CONTENTS...done"))
  6026. (setq org-cycle-global-status 'contents)
  6027. (run-hook-with-args 'org-cycle-hook 'contents))
  6028. ((and (eq last-command this-command)
  6029. (eq org-cycle-global-status 'contents))
  6030. ;; We just showed the table of contents - now show everything
  6031. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6032. (show-all)
  6033. (unless ga (org-unlogged-message "SHOW ALL"))
  6034. (setq org-cycle-global-status 'all)
  6035. (run-hook-with-args 'org-cycle-hook 'all))
  6036. (t
  6037. ;; Default action: go to overview
  6038. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6039. (org-overview)
  6040. (unless ga (org-unlogged-message "OVERVIEW"))
  6041. (setq org-cycle-global-status 'overview)
  6042. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6043. (defvar org-called-with-limited-levels nil
  6044. "Non-nil when `org-with-limited-levels' is currently active.")
  6045. (defun org-cycle-internal-local ()
  6046. "Do the local cycling action."
  6047. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6048. ;; First, determine end of headline (EOH), end of subtree or item
  6049. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6050. (save-excursion
  6051. (if (org-at-item-p)
  6052. (progn
  6053. (beginning-of-line)
  6054. (setq struct (org-list-struct))
  6055. (setq eoh (point-at-eol))
  6056. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6057. (setq has-children (org-list-has-child-p (point) struct)))
  6058. (org-back-to-heading)
  6059. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6060. (setq eos (save-excursion (org-end-of-subtree t t)
  6061. (when (bolp) (backward-char)) (point)))
  6062. (setq has-children
  6063. (or (save-excursion
  6064. (let ((level (funcall outline-level)))
  6065. (outline-next-heading)
  6066. (and (org-at-heading-p t)
  6067. (> (funcall outline-level) level))))
  6068. (save-excursion
  6069. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6070. ;; Determine end invisible part of buffer (EOL)
  6071. (beginning-of-line 2)
  6072. ;; XEmacs doesn't have `next-single-char-property-change'
  6073. (if (featurep 'xemacs)
  6074. (while (and (not (eobp)) ;; this is like `next-line'
  6075. (get-char-property (1- (point)) 'invisible))
  6076. (beginning-of-line 2))
  6077. (while (and (not (eobp)) ;; this is like `next-line'
  6078. (get-char-property (1- (point)) 'invisible))
  6079. (goto-char (next-single-char-property-change (point) 'invisible))
  6080. (and (eolp) (beginning-of-line 2))))
  6081. (setq eol (point)))
  6082. ;; Find out what to do next and set `this-command'
  6083. (cond
  6084. ((= eos eoh)
  6085. ;; Nothing is hidden behind this heading
  6086. (unless (org-before-first-heading-p)
  6087. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6088. (org-unlogged-message "EMPTY ENTRY")
  6089. (setq org-cycle-subtree-status nil)
  6090. (save-excursion
  6091. (goto-char eos)
  6092. (outline-next-heading)
  6093. (if (outline-invisible-p) (org-flag-heading nil))))
  6094. ((and (or (>= eol eos)
  6095. (not (string-match "\\S-" (buffer-substring eol eos))))
  6096. (or has-children
  6097. (not (setq children-skipped
  6098. org-cycle-skip-children-state-if-no-children))))
  6099. ;; Entire subtree is hidden in one line: children view
  6100. (unless (org-before-first-heading-p)
  6101. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6102. (if (org-at-item-p)
  6103. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6104. (org-show-entry)
  6105. (org-with-limited-levels (show-children))
  6106. ;; FIXME: This slows down the func way too much.
  6107. ;; How keep drawers hidden in subtree anyway?
  6108. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6109. ;; (org-cycle-hide-drawers 'subtree))
  6110. ;; Fold every list in subtree to top-level items.
  6111. (when (eq org-cycle-include-plain-lists 'integrate)
  6112. (save-excursion
  6113. (org-back-to-heading)
  6114. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6115. (beginning-of-line 1)
  6116. (let* ((struct (org-list-struct))
  6117. (prevs (org-list-prevs-alist struct))
  6118. (end (org-list-get-bottom-point struct)))
  6119. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6120. (org-list-get-all-items (point) struct prevs))
  6121. (goto-char (if (< end eos) end eos)))))))
  6122. (org-unlogged-message "CHILDREN")
  6123. (save-excursion
  6124. (goto-char eos)
  6125. (outline-next-heading)
  6126. (if (outline-invisible-p) (org-flag-heading nil)))
  6127. (setq org-cycle-subtree-status 'children)
  6128. (unless (org-before-first-heading-p)
  6129. (run-hook-with-args 'org-cycle-hook 'children)))
  6130. ((or children-skipped
  6131. (and (eq last-command this-command)
  6132. (eq org-cycle-subtree-status 'children)))
  6133. ;; We just showed the children, or no children are there,
  6134. ;; now show everything.
  6135. (unless (org-before-first-heading-p)
  6136. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6137. (outline-flag-region eoh eos nil)
  6138. (org-unlogged-message
  6139. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6140. (setq org-cycle-subtree-status 'subtree)
  6141. (unless (org-before-first-heading-p)
  6142. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6143. (t
  6144. ;; Default action: hide the subtree.
  6145. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6146. (outline-flag-region eoh eos t)
  6147. (org-unlogged-message "FOLDED")
  6148. (setq org-cycle-subtree-status 'folded)
  6149. (unless (org-before-first-heading-p)
  6150. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6151. ;;;###autoload
  6152. (defun org-global-cycle (&optional arg)
  6153. "Cycle the global visibility. For details see `org-cycle'.
  6154. With \\[universal-argument] prefix arg, switch to startup visibility.
  6155. With a numeric prefix, show all headlines up to that level."
  6156. (interactive "P")
  6157. (let ((org-cycle-include-plain-lists
  6158. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6159. (cond
  6160. ((integerp arg)
  6161. (show-all)
  6162. (hide-sublevels arg)
  6163. (setq org-cycle-global-status 'contents))
  6164. ((equal arg '(4))
  6165. (org-set-startup-visibility)
  6166. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6167. (t
  6168. (org-cycle '(4))))))
  6169. (defun org-set-startup-visibility ()
  6170. "Set the visibility required by startup options and properties."
  6171. (cond
  6172. ((eq org-startup-folded t)
  6173. (org-overview))
  6174. ((eq org-startup-folded 'content)
  6175. (org-content))
  6176. ((or (eq org-startup-folded 'showeverything)
  6177. (eq org-startup-folded nil))
  6178. (show-all)))
  6179. (unless (eq org-startup-folded 'showeverything)
  6180. (if org-hide-block-startup (org-hide-block-all))
  6181. (org-set-visibility-according-to-property 'no-cleanup)
  6182. (org-cycle-hide-archived-subtrees 'all)
  6183. (org-cycle-hide-drawers 'all)
  6184. (org-cycle-show-empty-lines t)))
  6185. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6186. "Switch subtree visibilities according to :VISIBILITY: property."
  6187. (interactive)
  6188. (let (org-show-entry-below state)
  6189. (save-excursion
  6190. (goto-char (point-min))
  6191. (while (re-search-forward
  6192. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6193. nil t)
  6194. (setq state (match-string 1))
  6195. (save-excursion
  6196. (org-back-to-heading t)
  6197. (hide-subtree)
  6198. (org-reveal)
  6199. (cond
  6200. ((equal state '("fold" "folded"))
  6201. (hide-subtree))
  6202. ((equal state "children")
  6203. (org-show-hidden-entry)
  6204. (show-children))
  6205. ((equal state "content")
  6206. (save-excursion
  6207. (save-restriction
  6208. (org-narrow-to-subtree)
  6209. (org-content))))
  6210. ((member state '("all" "showall"))
  6211. (show-subtree)))))
  6212. (unless no-cleanup
  6213. (org-cycle-hide-archived-subtrees 'all)
  6214. (org-cycle-hide-drawers 'all)
  6215. (org-cycle-show-empty-lines 'all)))))
  6216. ;; This function uses outline-regexp instead of the more fundamental
  6217. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6218. ;; buffers, where outline-regexp is needed.
  6219. (defun org-overview ()
  6220. "Switch to overview mode, showing only top-level headlines.
  6221. This shows all headlines with a level equal or greater than the level
  6222. of the first headline in the buffer. This is important, because if the
  6223. first headline is not level one, then (hide-sublevels 1) gives confusing
  6224. results."
  6225. (interactive)
  6226. (save-excursion
  6227. (let ((level
  6228. (save-excursion
  6229. (goto-char (point-min))
  6230. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6231. (progn
  6232. (goto-char (match-beginning 0))
  6233. (funcall outline-level))))))
  6234. (and level (hide-sublevels level)))))
  6235. (defun org-content (&optional arg)
  6236. "Show all headlines in the buffer, like a table of contents.
  6237. With numerical argument N, show content up to level N."
  6238. (interactive "P")
  6239. (org-overview)
  6240. (save-excursion
  6241. ;; Visit all headings and show their offspring
  6242. (and (integerp arg) (org-overview))
  6243. (goto-char (point-max))
  6244. (catch 'exit
  6245. (while (and (progn (condition-case nil
  6246. (outline-previous-visible-heading 1)
  6247. (error (goto-char (point-min))))
  6248. t)
  6249. (looking-at org-outline-regexp))
  6250. (if (integerp arg)
  6251. (show-children (1- arg))
  6252. (show-branches))
  6253. (if (bobp) (throw 'exit nil))))))
  6254. (defun org-optimize-window-after-visibility-change (state)
  6255. "Adjust the window after a change in outline visibility.
  6256. This function is the default value of the hook `org-cycle-hook'."
  6257. (when (get-buffer-window (current-buffer))
  6258. (cond
  6259. ((eq state 'content) nil)
  6260. ((eq state 'all) nil)
  6261. ((eq state 'folded) nil)
  6262. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6263. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6264. (defun org-remove-empty-overlays-at (pos)
  6265. "Remove outline overlays that do not contain non-white stuff."
  6266. (mapc
  6267. (lambda (o)
  6268. (and (eq 'outline (overlay-get o 'invisible))
  6269. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6270. (overlay-end o))))
  6271. (delete-overlay o)))
  6272. (overlays-at pos)))
  6273. (defun org-clean-visibility-after-subtree-move ()
  6274. "Fix visibility issues after moving a subtree."
  6275. ;; First, find a reasonable region to look at:
  6276. ;; Start two siblings above, end three below
  6277. (let* ((beg (save-excursion
  6278. (and (org-get-last-sibling)
  6279. (org-get-last-sibling))
  6280. (point)))
  6281. (end (save-excursion
  6282. (and (org-get-next-sibling)
  6283. (org-get-next-sibling)
  6284. (org-get-next-sibling))
  6285. (if (org-at-heading-p)
  6286. (point-at-eol)
  6287. (point))))
  6288. (level (looking-at "\\*+"))
  6289. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6290. (save-excursion
  6291. (save-restriction
  6292. (narrow-to-region beg end)
  6293. (when re
  6294. ;; Properly fold already folded siblings
  6295. (goto-char (point-min))
  6296. (while (re-search-forward re nil t)
  6297. (if (and (not (outline-invisible-p))
  6298. (save-excursion
  6299. (goto-char (point-at-eol)) (outline-invisible-p)))
  6300. (hide-entry))))
  6301. (org-cycle-show-empty-lines 'overview)
  6302. (org-cycle-hide-drawers 'overview)))))
  6303. (defun org-cycle-show-empty-lines (state)
  6304. "Show empty lines above all visible headlines.
  6305. The region to be covered depends on STATE when called through
  6306. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6307. entire buffer covered. Note that an empty line is only shown if there
  6308. are at least `org-cycle-separator-lines' empty lines before the headline."
  6309. (when (not (= org-cycle-separator-lines 0))
  6310. (save-excursion
  6311. (let* ((n (abs org-cycle-separator-lines))
  6312. (re (cond
  6313. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6314. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6315. (t (let ((ns (number-to-string (- n 2))))
  6316. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6317. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6318. beg end b e)
  6319. (cond
  6320. ((memq state '(overview contents t))
  6321. (setq beg (point-min) end (point-max)))
  6322. ((memq state '(children folded))
  6323. (setq beg (point) end (progn (org-end-of-subtree t t)
  6324. (beginning-of-line 2)
  6325. (point)))))
  6326. (when beg
  6327. (goto-char beg)
  6328. (while (re-search-forward re end t)
  6329. (unless (get-char-property (match-end 1) 'invisible)
  6330. (setq e (match-end 1))
  6331. (if (< org-cycle-separator-lines 0)
  6332. (setq b (save-excursion
  6333. (goto-char (match-beginning 0))
  6334. (org-back-over-empty-lines)
  6335. (if (save-excursion
  6336. (goto-char (max (point-min) (1- (point))))
  6337. (org-at-heading-p))
  6338. (1- (point))
  6339. (point))))
  6340. (setq b (match-beginning 1)))
  6341. (outline-flag-region b e nil)))))))
  6342. ;; Never hide empty lines at the end of the file.
  6343. (save-excursion
  6344. (goto-char (point-max))
  6345. (outline-previous-heading)
  6346. (outline-end-of-heading)
  6347. (if (and (looking-at "[ \t\n]+")
  6348. (= (match-end 0) (point-max)))
  6349. (outline-flag-region (point) (match-end 0) nil))))
  6350. (defun org-show-empty-lines-in-parent ()
  6351. "Move to the parent and re-show empty lines before visible headlines."
  6352. (save-excursion
  6353. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6354. (org-cycle-show-empty-lines context))))
  6355. (defun org-files-list ()
  6356. "Return `org-agenda-files' list, plus all open org-mode files.
  6357. This is useful for operations that need to scan all of a user's
  6358. open and agenda-wise Org files."
  6359. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6360. (dolist (buf (buffer-list))
  6361. (with-current-buffer buf
  6362. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6363. (let ((file (expand-file-name (buffer-file-name))))
  6364. (unless (member file files)
  6365. (push file files))))))
  6366. files))
  6367. (defsubst org-entry-beginning-position ()
  6368. "Return the beginning position of the current entry."
  6369. (save-excursion (outline-back-to-heading t) (point)))
  6370. (defsubst org-entry-end-position ()
  6371. "Return the end position of the current entry."
  6372. (save-excursion (outline-next-heading) (point)))
  6373. (defun org-cycle-hide-drawers (state &optional exceptions)
  6374. "Re-hide all drawers after a visibility state change.
  6375. When non-nil, optional argument EXCEPTIONS is a list of strings
  6376. specifying which drawers should not be hidden."
  6377. (when (and (derived-mode-p 'org-mode)
  6378. (not (memq state '(overview folded contents))))
  6379. (save-excursion
  6380. (let* ((globalp (memq state '(contents all)))
  6381. (beg (if globalp (point-min) (point)))
  6382. (end (if globalp (point-max)
  6383. (if (eq state 'children)
  6384. (save-excursion (outline-next-heading) (point))
  6385. (org-end-of-subtree t)))))
  6386. (goto-char beg)
  6387. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6388. (unless (member-ignore-case (match-string 1) exceptions)
  6389. (let ((drawer (org-element-at-point)))
  6390. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6391. (org-flag-drawer t drawer)
  6392. ;; Make sure to skip drawer entirely or we might flag
  6393. ;; it another time when matching its ending line with
  6394. ;; `org-drawer-regexp'.
  6395. (goto-char (org-element-property :end drawer))))))))))
  6396. (defun org-cycle-hide-inline-tasks (state)
  6397. "Re-hide inline tasks when switching to 'contents or 'children
  6398. visibility state."
  6399. (case state
  6400. (contents
  6401. (when (org-bound-and-true-p org-inlinetask-min-level)
  6402. (hide-sublevels (1- org-inlinetask-min-level))))
  6403. (children
  6404. (when (featurep 'org-inlinetask)
  6405. (save-excursion
  6406. (while (and (outline-next-heading)
  6407. (org-inlinetask-at-task-p))
  6408. (org-inlinetask-toggle-visibility)
  6409. (org-inlinetask-goto-end)))))))
  6410. (defun org-flag-drawer (flag &optional element)
  6411. "When FLAG is non-nil, hide the drawer we are at.
  6412. Otherwise make it visible. When optional argument ELEMENT is
  6413. a parsed drawer, as returned by `org-element-at-point', hide or
  6414. show that drawer instead."
  6415. (when (save-excursion
  6416. (beginning-of-line)
  6417. (org-looking-at-p org-drawer-regexp))
  6418. (let ((drawer (or element (org-element-at-point))))
  6419. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6420. (let ((post (org-element-property :post-affiliated drawer)))
  6421. (save-excursion
  6422. (outline-flag-region
  6423. (progn (goto-char post) (line-end-position))
  6424. (progn (goto-char (org-element-property :end drawer))
  6425. (skip-chars-backward " \r\t\n")
  6426. (line-end-position))
  6427. flag))
  6428. ;; When the drawer is hidden away, make sure point lies in
  6429. ;; a visible part of the buffer.
  6430. (when (and flag (> (line-beginning-position) post))
  6431. (goto-char post)))))))
  6432. (defun org-subtree-end-visible-p ()
  6433. "Is the end of the current subtree visible?"
  6434. (pos-visible-in-window-p
  6435. (save-excursion (org-end-of-subtree t) (point))))
  6436. (defun org-first-headline-recenter ()
  6437. "Move cursor to the first headline and recenter the headline."
  6438. (goto-char (point-min))
  6439. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6440. (set-window-start (selected-window) (point-at-bol))))
  6441. ;;; Saving and restoring visibility
  6442. (defun org-outline-overlay-data (&optional use-markers)
  6443. "Return a list of the locations of all outline overlays.
  6444. These are overlays with the `invisible' property value `outline'.
  6445. The return value is a list of cons cells, with start and stop
  6446. positions for each overlay.
  6447. If USE-MARKERS is set, return the positions as markers."
  6448. (let (beg end)
  6449. (save-excursion
  6450. (save-restriction
  6451. (widen)
  6452. (delq nil
  6453. (mapcar (lambda (o)
  6454. (when (eq (overlay-get o 'invisible) 'outline)
  6455. (setq beg (overlay-start o)
  6456. end (overlay-end o))
  6457. (and beg end (> end beg)
  6458. (if use-markers
  6459. (cons (copy-marker beg)
  6460. (copy-marker end t))
  6461. (cons beg end)))))
  6462. (overlays-in (point-min) (point-max))))))))
  6463. (defun org-set-outline-overlay-data (data)
  6464. "Create visibility overlays for all positions in DATA.
  6465. DATA should have been made by `org-outline-overlay-data'."
  6466. (let (o)
  6467. (save-excursion
  6468. (save-restriction
  6469. (widen)
  6470. (show-all)
  6471. (mapc (lambda (c)
  6472. (outline-flag-region (car c) (cdr c) t))
  6473. data)))))
  6474. ;;; Folding of blocks
  6475. (defvar org-hide-block-overlays nil
  6476. "Overlays hiding blocks.")
  6477. (make-variable-buffer-local 'org-hide-block-overlays)
  6478. (defun org-block-map (function &optional start end)
  6479. "Call FUNCTION at the head of all source blocks in the current buffer.
  6480. Optional arguments START and END can be used to limit the range."
  6481. (let ((start (or start (point-min)))
  6482. (end (or end (point-max))))
  6483. (save-excursion
  6484. (goto-char start)
  6485. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6486. (save-excursion
  6487. (save-match-data
  6488. (goto-char (match-beginning 0))
  6489. (funcall function)))))))
  6490. (defun org-hide-block-toggle-all ()
  6491. "Toggle the visibility of all blocks in the current buffer."
  6492. (org-block-map 'org-hide-block-toggle))
  6493. (defun org-hide-block-all ()
  6494. "Fold all blocks in the current buffer."
  6495. (interactive)
  6496. (org-show-block-all)
  6497. (org-block-map 'org-hide-block-toggle-maybe))
  6498. (defun org-show-block-all ()
  6499. "Unfold all blocks in the current buffer."
  6500. (interactive)
  6501. (mapc 'delete-overlay org-hide-block-overlays)
  6502. (setq org-hide-block-overlays nil))
  6503. (defun org-hide-block-toggle-maybe ()
  6504. "Toggle visibility of block at point.
  6505. Unlike to `org-hide-block-toggle', this function does not throw
  6506. an error. Return a non-nil value when toggling is successful."
  6507. (interactive)
  6508. (ignore-errors (org-hide-block-toggle)))
  6509. (defun org-hide-block-toggle (&optional force)
  6510. "Toggle the visibility of the current block.
  6511. When optional argument FORCE is `off', make block visible. If it
  6512. is non-nil, hide it unconditionally. Throw an error when not at
  6513. a block. Return a non-nil value when toggling is successful."
  6514. (interactive)
  6515. (let ((element (org-element-at-point)))
  6516. (unless (memq (org-element-type element)
  6517. '(center-block comment-block dynamic-block example-block
  6518. export-block quote-block special-block
  6519. src-block verse-block))
  6520. (user-error "Not at a block"))
  6521. (let* ((start (save-excursion
  6522. (goto-char (org-element-property :post-affiliated element))
  6523. (line-end-position)))
  6524. (end (save-excursion
  6525. (goto-char (org-element-property :end element))
  6526. (skip-chars-backward " \r\t\n")
  6527. (line-end-position)))
  6528. (overlays (overlays-at start)))
  6529. (cond
  6530. ;; Do nothing when not before or at the block opening line or
  6531. ;; at the block closing line.
  6532. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6533. ((and (not (eq force 'off))
  6534. (not (memq t (mapcar
  6535. (lambda (o)
  6536. (eq (overlay-get o 'invisible) 'org-hide-block))
  6537. overlays))))
  6538. (let ((ov (make-overlay start end)))
  6539. (overlay-put ov 'invisible 'org-hide-block)
  6540. ;; Make the block accessible to `isearch'.
  6541. (overlay-put
  6542. ov 'isearch-open-invisible
  6543. (lambda (ov)
  6544. (when (memq ov org-hide-block-overlays)
  6545. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6546. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6547. (delete-overlay ov))))
  6548. (push ov org-hide-block-overlays)
  6549. ;; When the block is hidden away, make sure point is left in
  6550. ;; a visible part of the buffer.
  6551. (when (> (line-beginning-position) start)
  6552. (goto-char start)
  6553. (beginning-of-line))
  6554. ;; Signal successful toggling.
  6555. t))
  6556. ((or (not force) (eq force 'off))
  6557. (dolist (ov overlays t)
  6558. (when (memq ov org-hide-block-overlays)
  6559. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6560. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6561. (delete-overlay ov))))))))
  6562. ;; org-tab-after-check-for-cycling-hook
  6563. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6564. ;; Remove overlays when changing major mode
  6565. (add-hook 'org-mode-hook
  6566. (lambda () (org-add-hook 'change-major-mode-hook
  6567. 'org-show-block-all 'append 'local)))
  6568. ;;; Org-goto
  6569. (defvar org-goto-window-configuration nil)
  6570. (defvar org-goto-marker nil)
  6571. (defvar org-goto-map)
  6572. (defun org-goto-map ()
  6573. "Set the keymap `org-goto'."
  6574. (setq org-goto-map
  6575. (let ((map (make-sparse-keymap)))
  6576. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6577. mouse-drag-region universal-argument org-occur))
  6578. cmd)
  6579. (while (setq cmd (pop cmds))
  6580. (substitute-key-definition cmd cmd map global-map)))
  6581. (suppress-keymap map)
  6582. (org-defkey map "\C-m" 'org-goto-ret)
  6583. (org-defkey map [(return)] 'org-goto-ret)
  6584. (org-defkey map [(left)] 'org-goto-left)
  6585. (org-defkey map [(right)] 'org-goto-right)
  6586. (org-defkey map [(control ?g)] 'org-goto-quit)
  6587. (org-defkey map "\C-i" 'org-cycle)
  6588. (org-defkey map [(tab)] 'org-cycle)
  6589. (org-defkey map [(down)] 'outline-next-visible-heading)
  6590. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6591. (if org-goto-auto-isearch
  6592. (if (fboundp 'define-key-after)
  6593. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6594. nil)
  6595. (org-defkey map "q" 'org-goto-quit)
  6596. (org-defkey map "n" 'outline-next-visible-heading)
  6597. (org-defkey map "p" 'outline-previous-visible-heading)
  6598. (org-defkey map "f" 'outline-forward-same-level)
  6599. (org-defkey map "b" 'outline-backward-same-level)
  6600. (org-defkey map "u" 'outline-up-heading))
  6601. (org-defkey map "/" 'org-occur)
  6602. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6603. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6604. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6605. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6606. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6607. map)))
  6608. (defconst org-goto-help
  6609. "Browse buffer copy, to find location or copy text.%s
  6610. RET=jump to location C-g=quit and return to previous location
  6611. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6612. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6613. (defun org-goto (&optional alternative-interface)
  6614. "Look up a different location in the current file, keeping current visibility.
  6615. When you want look-up or go to a different location in a
  6616. document, the fastest way is often to fold the entire buffer and
  6617. then dive into the tree. This method has the disadvantage, that
  6618. the previous location will be folded, which may not be what you
  6619. want.
  6620. This command works around this by showing a copy of the current
  6621. buffer in an indirect buffer, in overview mode. You can dive
  6622. into the tree in that copy, use org-occur and incremental search
  6623. to find a location. When pressing RET or `Q', the command
  6624. returns to the original buffer in which the visibility is still
  6625. unchanged. After RET it will also jump to the location selected
  6626. in the indirect buffer and expose the headline hierarchy above.
  6627. With a prefix argument, use the alternative interface: e.g. if
  6628. `org-goto-interface' is 'outline use 'outline-path-completion."
  6629. (interactive "P")
  6630. (org-goto-map)
  6631. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6632. (org-refile-use-outline-path t)
  6633. (org-refile-target-verify-function nil)
  6634. (interface
  6635. (if (not alternative-interface)
  6636. org-goto-interface
  6637. (if (eq org-goto-interface 'outline)
  6638. 'outline-path-completion
  6639. 'outline)))
  6640. (org-goto-start-pos (point))
  6641. (selected-point
  6642. (if (eq interface 'outline)
  6643. (car (org-get-location (current-buffer) org-goto-help))
  6644. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6645. (org-refile-check-position pa)
  6646. (nth 3 pa)))))
  6647. (if selected-point
  6648. (progn
  6649. (org-mark-ring-push org-goto-start-pos)
  6650. (goto-char selected-point)
  6651. (if (or (outline-invisible-p) (org-invisible-p2))
  6652. (org-show-context 'org-goto)))
  6653. (message "Quit"))))
  6654. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6655. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6656. (defvar org-goto-local-auto-isearch-map) ; defined below
  6657. (defun org-get-location (buf help)
  6658. "Let the user select a location in the Org-mode buffer BUF.
  6659. This function uses a recursive edit. It returns the selected position
  6660. or nil."
  6661. (org-no-popups
  6662. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6663. (isearch-hide-immediately nil)
  6664. (isearch-search-fun-function
  6665. (lambda () 'org-goto-local-search-headings))
  6666. (org-goto-selected-point org-goto-exit-command))
  6667. (save-excursion
  6668. (save-window-excursion
  6669. (delete-other-windows)
  6670. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6671. (org-pop-to-buffer-same-window
  6672. (condition-case nil
  6673. (make-indirect-buffer (current-buffer) "*org-goto*")
  6674. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6675. (with-output-to-temp-buffer "*Org Help*"
  6676. (princ (format help (if org-goto-auto-isearch
  6677. " Just type for auto-isearch."
  6678. " n/p/f/b/u to navigate, q to quit."))))
  6679. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6680. (setq buffer-read-only nil)
  6681. (let ((org-startup-truncated t)
  6682. (org-startup-folded nil)
  6683. (org-startup-align-all-tables nil))
  6684. (org-mode)
  6685. (org-overview))
  6686. (setq buffer-read-only t)
  6687. (if (and (boundp 'org-goto-start-pos)
  6688. (integer-or-marker-p org-goto-start-pos))
  6689. (let ((org-show-hierarchy-above t)
  6690. (org-show-siblings t)
  6691. (org-show-following-heading t))
  6692. (goto-char org-goto-start-pos)
  6693. (and (outline-invisible-p) (org-show-context)))
  6694. (goto-char (point-min)))
  6695. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6696. (message "Select location and press RET")
  6697. (use-local-map org-goto-map)
  6698. (recursive-edit)))
  6699. (kill-buffer "*org-goto*")
  6700. (cons org-goto-selected-point org-goto-exit-command))))
  6701. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6702. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6703. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6704. (if (fboundp 'isearch-other-control-char)
  6705. (progn
  6706. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6707. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6708. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6709. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6710. (define-key org-goto-local-auto-isearch-map [return] nil))
  6711. (defun org-goto-local-search-headings (string bound noerror)
  6712. "Search and make sure that any matches are in headlines."
  6713. (catch 'return
  6714. (while (if isearch-forward
  6715. (search-forward string bound noerror)
  6716. (search-backward string bound noerror))
  6717. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6718. (and (member :headline context)
  6719. (not (member :tags context))))
  6720. (throw 'return (point))))))
  6721. (defun org-goto-local-auto-isearch ()
  6722. "Start isearch."
  6723. (interactive)
  6724. (goto-char (point-min))
  6725. (let ((keys (this-command-keys)))
  6726. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6727. (isearch-mode t)
  6728. (isearch-process-search-char (string-to-char keys)))))
  6729. (defun org-goto-ret (&optional arg)
  6730. "Finish `org-goto' by going to the new location."
  6731. (interactive "P")
  6732. (setq org-goto-selected-point (point)
  6733. org-goto-exit-command 'return)
  6734. (throw 'exit nil))
  6735. (defun org-goto-left ()
  6736. "Finish `org-goto' by going to the new location."
  6737. (interactive)
  6738. (if (org-at-heading-p)
  6739. (progn
  6740. (beginning-of-line 1)
  6741. (setq org-goto-selected-point (point)
  6742. org-goto-exit-command 'left)
  6743. (throw 'exit nil))
  6744. (user-error "Not on a heading")))
  6745. (defun org-goto-right ()
  6746. "Finish `org-goto' by going to the new location."
  6747. (interactive)
  6748. (if (org-at-heading-p)
  6749. (progn
  6750. (setq org-goto-selected-point (point)
  6751. org-goto-exit-command 'right)
  6752. (throw 'exit nil))
  6753. (user-error "Not on a heading")))
  6754. (defun org-goto-quit ()
  6755. "Finish `org-goto' without cursor motion."
  6756. (interactive)
  6757. (setq org-goto-selected-point nil)
  6758. (setq org-goto-exit-command 'quit)
  6759. (throw 'exit nil))
  6760. ;;; Indirect buffer display of subtrees
  6761. (defvar org-indirect-dedicated-frame nil
  6762. "This is the frame being used for indirect tree display.")
  6763. (defvar org-last-indirect-buffer nil)
  6764. (defun org-tree-to-indirect-buffer (&optional arg)
  6765. "Create indirect buffer and narrow it to current subtree.
  6766. With a numerical prefix ARG, go up to this level and then take that tree.
  6767. If ARG is negative, go up that many levels.
  6768. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6769. indirect buffer previously made with this command, to avoid proliferation of
  6770. indirect buffers. However, when you call the command with a \
  6771. \\[universal-argument] prefix, or
  6772. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6773. is kept so that you can work with several indirect buffers at the same time.
  6774. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6775. \\[universal-argument] prefix also
  6776. requests that a new frame be made for the new buffer, so that the dedicated
  6777. frame is not changed."
  6778. (interactive "P")
  6779. (let ((cbuf (current-buffer))
  6780. (cwin (selected-window))
  6781. (pos (point))
  6782. beg end level heading ibuf)
  6783. (save-excursion
  6784. (org-back-to-heading t)
  6785. (when (numberp arg)
  6786. (setq level (org-outline-level))
  6787. (if (< arg 0) (setq arg (+ level arg)))
  6788. (while (> (setq level (org-outline-level)) arg)
  6789. (org-up-heading-safe)))
  6790. (setq beg (point)
  6791. heading (org-get-heading))
  6792. (org-end-of-subtree t t)
  6793. (if (org-at-heading-p) (backward-char 1))
  6794. (setq end (point)))
  6795. (if (and (buffer-live-p org-last-indirect-buffer)
  6796. (not (eq org-indirect-buffer-display 'new-frame))
  6797. (not arg))
  6798. (kill-buffer org-last-indirect-buffer))
  6799. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6800. org-last-indirect-buffer ibuf)
  6801. (cond
  6802. ((or (eq org-indirect-buffer-display 'new-frame)
  6803. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6804. (select-frame (make-frame))
  6805. (delete-other-windows)
  6806. (org-pop-to-buffer-same-window ibuf)
  6807. (org-set-frame-title heading))
  6808. ((eq org-indirect-buffer-display 'dedicated-frame)
  6809. (raise-frame
  6810. (select-frame (or (and org-indirect-dedicated-frame
  6811. (frame-live-p org-indirect-dedicated-frame)
  6812. org-indirect-dedicated-frame)
  6813. (setq org-indirect-dedicated-frame (make-frame)))))
  6814. (delete-other-windows)
  6815. (org-pop-to-buffer-same-window ibuf)
  6816. (org-set-frame-title (concat "Indirect: " heading)))
  6817. ((eq org-indirect-buffer-display 'current-window)
  6818. (org-pop-to-buffer-same-window ibuf))
  6819. ((eq org-indirect-buffer-display 'other-window)
  6820. (pop-to-buffer ibuf))
  6821. (t (error "Invalid value")))
  6822. (if (featurep 'xemacs)
  6823. (save-excursion (org-mode) (turn-on-font-lock)))
  6824. (narrow-to-region beg end)
  6825. (show-all)
  6826. (goto-char pos)
  6827. (run-hook-with-args 'org-cycle-hook 'all)
  6828. (and (window-live-p cwin) (select-window cwin))))
  6829. (defun org-get-indirect-buffer (&optional buffer heading)
  6830. (setq buffer (or buffer (current-buffer)))
  6831. (let ((n 1) (base (buffer-name buffer)) bname)
  6832. (while (buffer-live-p
  6833. (get-buffer
  6834. (setq bname
  6835. (concat base "-"
  6836. (if heading (concat heading "-" (number-to-string n))
  6837. (number-to-string n))))))
  6838. (setq n (1+ n)))
  6839. (condition-case nil
  6840. (make-indirect-buffer buffer bname 'clone)
  6841. (error (make-indirect-buffer buffer bname)))))
  6842. (defun org-set-frame-title (title)
  6843. "Set the title of the current frame to the string TITLE."
  6844. ;; FIXME: how to name a single frame in XEmacs???
  6845. (unless (featurep 'xemacs)
  6846. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6847. ;;;; Structure editing
  6848. ;;; Inserting headlines
  6849. (defun org-previous-line-empty-p (&optional next)
  6850. "Is the previous line a blank line?
  6851. When NEXT is non-nil, check the next line instead."
  6852. (save-excursion
  6853. (and (not (bobp))
  6854. (or (beginning-of-line (if next 2 0)) t)
  6855. (save-match-data
  6856. (looking-at "[ \t]*$")))))
  6857. (defun org-insert-heading (&optional arg invisible-ok)
  6858. "Insert a new heading or an item with the same depth at point.
  6859. If point is at the beginning of a heading or a list item, insert
  6860. a new heading or a new item above the current one. If point is
  6861. at the beginning of a normal line, turn the line into a heading.
  6862. If point is in the middle of a headline or a list item, split the
  6863. headline or the item and create a new headline/item with the text
  6864. in the current line after point \(see `org-M-RET-may-split-line'
  6865. on how to modify this behavior).
  6866. With one universal prefix argument, set the user option
  6867. `org-insert-heading-respect-content' to t for the duration of
  6868. the command. This modifies the behavior described above in this
  6869. ways: on list items and at the beginning of normal lines, force
  6870. the insertion of a heading after the current subtree.
  6871. With two universal prefix arguments, insert the heading at the
  6872. end of the grandparent subtree. For example, if point is within
  6873. a 2nd-level heading, then it will insert a 2nd-level heading at
  6874. the end of the 1st-level parent heading.
  6875. If point is at the beginning of a headline, insert a sibling
  6876. before the current headline. If point is not at the beginning,
  6877. split the line and create a new headline with the text in the
  6878. current line after point \(see `org-M-RET-may-split-line' on how
  6879. to modify this behavior).
  6880. If point is at the beginning of a normal line, turn this line
  6881. into a heading.
  6882. When INVISIBLE-OK is set, stop at invisible headlines when going
  6883. back. This is important for non-interactive uses of the
  6884. command."
  6885. (interactive "P")
  6886. (if (org-called-interactively-p 'any) (org-reveal))
  6887. (let ((itemp (org-in-item-p))
  6888. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6889. (respect-content (or org-insert-heading-respect-content
  6890. (equal arg '(4))))
  6891. (initial-content "")
  6892. (adjust-empty-lines t))
  6893. (cond
  6894. ((or (= (buffer-size) 0)
  6895. (and (not (save-excursion
  6896. (and (ignore-errors (org-back-to-heading invisible-ok))
  6897. (org-at-heading-p))))
  6898. (or arg (not itemp))))
  6899. ;; At beginning of buffer or so high up that only a heading
  6900. ;; makes sense.
  6901. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6902. ((not respect-content)
  6903. (unless may-split (end-of-line))
  6904. (insert "\n* "))
  6905. ((re-search-forward org-outline-regexp-bol nil t)
  6906. (beginning-of-line)
  6907. (insert "* \n")
  6908. (backward-char))
  6909. (t (goto-char (point-max))
  6910. (insert "\n* ")))
  6911. (run-hooks 'org-insert-heading-hook))
  6912. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6913. (t
  6914. ;; Maybe move at the end of the subtree
  6915. (when (equal arg '(16))
  6916. (org-up-heading-safe)
  6917. (org-end-of-subtree t))
  6918. ;; Insert a heading
  6919. (save-restriction
  6920. (widen)
  6921. (let* ((level nil)
  6922. (on-heading (org-at-heading-p))
  6923. (empty-line-p (if on-heading
  6924. (org-previous-line-empty-p)
  6925. ;; We will decide later
  6926. nil))
  6927. ;; Get a level string to fall back on
  6928. (fix-level
  6929. (if (org-before-first-heading-p) "*"
  6930. (save-excursion
  6931. (org-back-to-heading t)
  6932. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6933. (looking-at org-outline-regexp)
  6934. (make-string (1- (length (match-string 0))) ?*))))
  6935. (stars
  6936. (save-excursion
  6937. (condition-case nil
  6938. (progn
  6939. (org-back-to-heading invisible-ok)
  6940. (when (and (not on-heading)
  6941. (featurep 'org-inlinetask)
  6942. (integerp org-inlinetask-min-level)
  6943. (>= (length (match-string 0))
  6944. org-inlinetask-min-level))
  6945. ;; Find a heading level before the inline task
  6946. (while (and (setq level (org-up-heading-safe))
  6947. (>= level org-inlinetask-min-level)))
  6948. (if (org-at-heading-p)
  6949. (org-back-to-heading invisible-ok)
  6950. (error "This should not happen")))
  6951. (unless (and (save-excursion
  6952. (save-match-data
  6953. (org-backward-heading-same-level
  6954. 1 invisible-ok))
  6955. (= (point) (match-beginning 0)))
  6956. (not (org-previous-line-empty-p t)))
  6957. (setq empty-line-p (or empty-line-p
  6958. (org-previous-line-empty-p))))
  6959. (match-string 0))
  6960. (error (or fix-level "* ")))))
  6961. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6962. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6963. pos hide-previous previous-pos)
  6964. ;; If we insert after content, move there and clean up whitespace
  6965. (when (and respect-content
  6966. (not (org-looking-at-p org-outline-regexp-bol)))
  6967. (if (not (org-before-first-heading-p))
  6968. (org-end-of-subtree nil t)
  6969. (re-search-forward org-outline-regexp-bol)
  6970. (beginning-of-line 0))
  6971. (skip-chars-backward " \r\n")
  6972. (and (not (looking-back "^\\*+"))
  6973. (looking-at "[ \t]+") (replace-match ""))
  6974. (unless (eobp) (forward-char 1))
  6975. (when (looking-at "^\\*")
  6976. (unless (bobp) (backward-char 1))
  6977. (insert "\n")))
  6978. ;; If we are splitting, grab the text that should be moved to the new headline
  6979. (when may-split
  6980. (if (org-on-heading-p)
  6981. ;; This is a heading, we split intelligently (keeping tags)
  6982. (let ((pos (point)))
  6983. (goto-char (point-at-bol))
  6984. (unless (looking-at org-complex-heading-regexp)
  6985. (error "This should not happen"))
  6986. (when (and (match-beginning 4)
  6987. (> pos (match-beginning 4))
  6988. (< pos (match-end 4)))
  6989. (setq initial-content (buffer-substring pos (match-end 4)))
  6990. (goto-char pos)
  6991. (delete-region (point) (match-end 4))
  6992. (if (looking-at "[ \t]*$")
  6993. (replace-match "")
  6994. (insert (make-string (length initial-content) ?\ )))
  6995. (setq initial-content (org-trim initial-content)))
  6996. (goto-char pos))
  6997. ;; a normal line
  6998. (setq initial-content
  6999. (org-trim (buffer-substring (point) (point-at-eol))))
  7000. (delete-region (point) (point-at-eol))))
  7001. ;; If we are at the beginning of the line, insert before it. Else after
  7002. (cond
  7003. ((and (bolp) (looking-at "[ \t]*$")))
  7004. ((and (bolp) (not (looking-at "[ \t]*$")))
  7005. (open-line 1))
  7006. (t
  7007. (goto-char (point-at-eol))
  7008. (insert "\n")))
  7009. ;; Insert the new heading
  7010. (insert stars)
  7011. (just-one-space)
  7012. (insert initial-content)
  7013. (when adjust-empty-lines
  7014. (if (or (not blank)
  7015. (and blank (not (org-previous-line-empty-p))))
  7016. (org-N-empty-lines-before-current (if blank 1 0))))
  7017. (run-hooks 'org-insert-heading-hook)))))))
  7018. (defun org-N-empty-lines-before-current (N)
  7019. "Make the number of empty lines before current exactly N.
  7020. So this will delete or add empty lines."
  7021. (save-excursion
  7022. (beginning-of-line)
  7023. (let ((p (point)))
  7024. (skip-chars-backward " \r\t\n")
  7025. (unless (bolp) (forward-line))
  7026. (delete-region (point) p))
  7027. (when (> N 0) (insert (make-string N ?\n)))))
  7028. (defun org-get-heading (&optional no-tags no-todo)
  7029. "Return the heading of the current entry, without the stars.
  7030. When NO-TAGS is non-nil, don't include tags.
  7031. When NO-TODO is non-nil, don't include TODO keywords."
  7032. (save-excursion
  7033. (org-back-to-heading t)
  7034. (cond
  7035. ((and no-tags no-todo)
  7036. (looking-at org-complex-heading-regexp)
  7037. (match-string 4))
  7038. (no-tags
  7039. (looking-at (concat org-outline-regexp
  7040. "\\(.*?\\)"
  7041. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7042. (match-string 1))
  7043. (no-todo
  7044. (looking-at org-todo-line-regexp)
  7045. (match-string 3))
  7046. (t (looking-at org-heading-regexp)
  7047. (match-string 2)))))
  7048. (defvar orgstruct-mode) ; defined below
  7049. (defun org-heading-components ()
  7050. "Return the components of the current heading.
  7051. This is a list with the following elements:
  7052. - the level as an integer
  7053. - the reduced level, different if `org-odd-levels-only' is set.
  7054. - the TODO keyword, or nil
  7055. - the priority character, like ?A, or nil if no priority is given
  7056. - the headline text itself, or the tags string if no headline text
  7057. - the tags string, or nil."
  7058. (save-excursion
  7059. (org-back-to-heading t)
  7060. (if (let (case-fold-search)
  7061. (looking-at
  7062. (if orgstruct-mode
  7063. org-heading-regexp
  7064. org-complex-heading-regexp)))
  7065. (if orgstruct-mode
  7066. (list (length (match-string 1))
  7067. (org-reduced-level (length (match-string 1)))
  7068. nil
  7069. nil
  7070. (match-string 2)
  7071. nil)
  7072. (list (length (match-string 1))
  7073. (org-reduced-level (length (match-string 1)))
  7074. (org-match-string-no-properties 2)
  7075. (and (match-end 3) (aref (match-string 3) 2))
  7076. (org-match-string-no-properties 4)
  7077. (org-match-string-no-properties 5))))))
  7078. (defun org-get-entry ()
  7079. "Get the entry text, after heading, entire subtree."
  7080. (save-excursion
  7081. (org-back-to-heading t)
  7082. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7083. (defun org-insert-heading-after-current ()
  7084. "Insert a new heading with same level as current, after current subtree."
  7085. (interactive)
  7086. (org-back-to-heading)
  7087. (org-insert-heading)
  7088. (org-move-subtree-down)
  7089. (end-of-line 1))
  7090. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7091. "Insert heading with `org-insert-heading-respect-content' set to t."
  7092. (interactive)
  7093. (org-insert-heading '(4) invisible-ok))
  7094. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7095. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7096. (interactive)
  7097. (org-insert-todo-heading force-state '(4)))
  7098. (defun org-insert-todo-heading (arg &optional force-heading)
  7099. "Insert a new heading with the same level and TODO state as current heading.
  7100. If the heading has no TODO state, or if the state is DONE, use the first
  7101. state (TODO by default). Also with one prefix arg, force first state. With
  7102. two prefix args, force inserting at the end of the parent subtree."
  7103. (interactive "P")
  7104. (when (or force-heading (not (org-insert-item 'checkbox)))
  7105. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7106. force-heading))
  7107. (save-excursion
  7108. (org-back-to-heading)
  7109. (outline-previous-heading)
  7110. (looking-at org-todo-line-regexp))
  7111. (let*
  7112. ((new-mark-x
  7113. (if (or (equal arg '(4))
  7114. (not (match-beginning 2))
  7115. (member (match-string 2) org-done-keywords))
  7116. (car org-todo-keywords-1)
  7117. (match-string 2)))
  7118. (new-mark
  7119. (or
  7120. (run-hook-with-args-until-success
  7121. 'org-todo-get-default-hook new-mark-x nil)
  7122. new-mark-x)))
  7123. (beginning-of-line 1)
  7124. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7125. (if org-treat-insert-todo-heading-as-state-change
  7126. (org-todo new-mark)
  7127. (insert new-mark " "))))
  7128. (when org-provide-todo-statistics
  7129. (org-update-parent-todo-statistics))))
  7130. (defun org-insert-subheading (arg)
  7131. "Insert a new subheading and demote it.
  7132. Works for outline headings and for plain lists alike."
  7133. (interactive "P")
  7134. (org-insert-heading arg)
  7135. (cond
  7136. ((org-at-heading-p) (org-do-demote))
  7137. ((org-at-item-p) (org-indent-item))))
  7138. (defun org-insert-todo-subheading (arg)
  7139. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7140. Works for outline headings and for plain lists alike."
  7141. (interactive "P")
  7142. (org-insert-todo-heading arg)
  7143. (cond
  7144. ((org-at-heading-p) (org-do-demote))
  7145. ((org-at-item-p) (org-indent-item))))
  7146. ;;; Promotion and Demotion
  7147. (defvar org-after-demote-entry-hook nil
  7148. "Hook run after an entry has been demoted.
  7149. The cursor will be at the beginning of the entry.
  7150. When a subtree is being demoted, the hook will be called for each node.")
  7151. (defvar org-after-promote-entry-hook nil
  7152. "Hook run after an entry has been promoted.
  7153. The cursor will be at the beginning of the entry.
  7154. When a subtree is being promoted, the hook will be called for each node.")
  7155. (defun org-promote-subtree ()
  7156. "Promote the entire subtree.
  7157. See also `org-promote'."
  7158. (interactive)
  7159. (save-excursion
  7160. (org-with-limited-levels (org-map-tree 'org-promote)))
  7161. (org-fix-position-after-promote))
  7162. (defun org-demote-subtree ()
  7163. "Demote the entire subtree. See `org-demote'.
  7164. See also `org-promote'."
  7165. (interactive)
  7166. (save-excursion
  7167. (org-with-limited-levels (org-map-tree 'org-demote)))
  7168. (org-fix-position-after-promote))
  7169. (defun org-do-promote ()
  7170. "Promote the current heading higher up the tree.
  7171. If the region is active in `transient-mark-mode', promote all headings
  7172. in the region."
  7173. (interactive)
  7174. (save-excursion
  7175. (if (org-region-active-p)
  7176. (org-map-region 'org-promote (region-beginning) (region-end))
  7177. (org-promote)))
  7178. (org-fix-position-after-promote))
  7179. (defun org-do-demote ()
  7180. "Demote the current heading lower down the tree.
  7181. If the region is active in `transient-mark-mode', demote all headings
  7182. in the region."
  7183. (interactive)
  7184. (save-excursion
  7185. (if (org-region-active-p)
  7186. (org-map-region 'org-demote (region-beginning) (region-end))
  7187. (org-demote)))
  7188. (org-fix-position-after-promote))
  7189. (defun org-fix-position-after-promote ()
  7190. "Make sure that after pro/demotion cursor position is right."
  7191. (let ((pos (point)))
  7192. (when (save-excursion
  7193. (beginning-of-line 1)
  7194. (looking-at org-todo-line-regexp)
  7195. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7196. (cond ((eobp) (insert " "))
  7197. ((eolp) (insert " "))
  7198. ((equal (char-after) ?\ ) (forward-char 1))))))
  7199. (defun org-current-level ()
  7200. "Return the level of the current entry, or nil if before the first headline.
  7201. The level is the number of stars at the beginning of the headline."
  7202. (save-excursion
  7203. (org-with-limited-levels
  7204. (if (ignore-errors (org-back-to-heading t))
  7205. (funcall outline-level)))))
  7206. (defun org-get-previous-line-level ()
  7207. "Return the outline depth of the last headline before the current line.
  7208. Returns 0 for the first headline in the buffer, and nil if before the
  7209. first headline."
  7210. (and (org-current-level)
  7211. (or (and (/= (line-beginning-position) (point-min))
  7212. (save-excursion (beginning-of-line 0) (org-current-level)))
  7213. 0)))
  7214. (defun org-reduced-level (l)
  7215. "Compute the effective level of a heading.
  7216. This takes into account the setting of `org-odd-levels-only'."
  7217. (cond
  7218. ((zerop l) 0)
  7219. (org-odd-levels-only (1+ (floor (/ l 2))))
  7220. (t l)))
  7221. (defun org-level-increment ()
  7222. "Return the number of stars that will be added or removed at a
  7223. time to headlines when structure editing, based on the value of
  7224. `org-odd-levels-only'."
  7225. (if org-odd-levels-only 2 1))
  7226. (defun org-get-valid-level (level &optional change)
  7227. "Rectify a level change under the influence of `org-odd-levels-only'
  7228. LEVEL is a current level, CHANGE is by how much the level should be
  7229. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7230. even level numbers will become the next higher odd number."
  7231. (if org-odd-levels-only
  7232. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7233. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7234. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7235. (max 1 (+ level (or change 0)))))
  7236. (if (boundp 'define-obsolete-function-alias)
  7237. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7238. (define-obsolete-function-alias 'org-get-legal-level
  7239. 'org-get-valid-level)
  7240. (define-obsolete-function-alias 'org-get-legal-level
  7241. 'org-get-valid-level "23.1")))
  7242. (defun org-promote ()
  7243. "Promote the current heading higher up the tree."
  7244. (org-with-wide-buffer
  7245. (org-back-to-heading t)
  7246. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7247. after-change-functions))
  7248. (level (save-match-data (funcall outline-level)))
  7249. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7250. (diff (abs (- level (length up-head) -1))))
  7251. (cond
  7252. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7253. (replace-match "# " nil t))
  7254. ((= level 1)
  7255. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7256. (t (replace-match up-head nil t)))
  7257. (unless (= level 1)
  7258. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7259. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7260. (run-hooks 'org-after-promote-entry-hook))))
  7261. (defun org-demote ()
  7262. "Demote the current heading lower down the tree."
  7263. (org-with-wide-buffer
  7264. (org-back-to-heading t)
  7265. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7266. after-change-functions))
  7267. (level (save-match-data (funcall outline-level)))
  7268. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7269. (diff (abs (- level (length down-head) -1))))
  7270. (replace-match down-head nil t)
  7271. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7272. (when org-adapt-indentation (org-fixup-indentation diff))
  7273. (run-hooks 'org-after-demote-entry-hook))))
  7274. (defun org-cycle-level ()
  7275. "Cycle the level of an empty headline through possible states.
  7276. This goes first to child, then to parent, level, then up the hierarchy.
  7277. After top level, it switches back to sibling level."
  7278. (interactive)
  7279. (let ((org-adapt-indentation nil))
  7280. (when (org-point-at-end-of-empty-headline)
  7281. (setq this-command 'org-cycle-level) ; Only needed for caching
  7282. (let ((cur-level (org-current-level))
  7283. (prev-level (org-get-previous-line-level)))
  7284. (cond
  7285. ;; If first headline in file, promote to top-level.
  7286. ((= prev-level 0)
  7287. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7288. do (org-do-promote)))
  7289. ;; If same level as prev, demote one.
  7290. ((= prev-level cur-level)
  7291. (org-do-demote))
  7292. ;; If parent is top-level, promote to top level if not already.
  7293. ((= prev-level 1)
  7294. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7295. do (org-do-promote)))
  7296. ;; If top-level, return to prev-level.
  7297. ((= cur-level 1)
  7298. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7299. do (org-do-demote)))
  7300. ;; If less than prev-level, promote one.
  7301. ((< cur-level prev-level)
  7302. (org-do-promote))
  7303. ;; If deeper than prev-level, promote until higher than
  7304. ;; prev-level.
  7305. ((> cur-level prev-level)
  7306. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7307. do (org-do-promote))))
  7308. t))))
  7309. (defun org-map-tree (fun)
  7310. "Call FUN for every heading underneath the current one."
  7311. (org-back-to-heading)
  7312. (let ((level (funcall outline-level)))
  7313. (save-excursion
  7314. (funcall fun)
  7315. (while (and (progn
  7316. (outline-next-heading)
  7317. (> (funcall outline-level) level))
  7318. (not (eobp)))
  7319. (funcall fun)))))
  7320. (defun org-map-region (fun beg end)
  7321. "Call FUN for every heading between BEG and END."
  7322. (let ((org-ignore-region t))
  7323. (save-excursion
  7324. (setq end (copy-marker end))
  7325. (goto-char beg)
  7326. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7327. (< (point) end))
  7328. (funcall fun))
  7329. (while (and (progn
  7330. (outline-next-heading)
  7331. (< (point) end))
  7332. (not (eobp)))
  7333. (funcall fun)))))
  7334. (defun org-fixup-indentation (diff)
  7335. "Change the indentation in the current entry by DIFF.
  7336. DIFF is an integer. Indentation is done according to the
  7337. following rules:
  7338. - Planning information and property drawers are always indented
  7339. according to the new level of the headline;
  7340. - Footnote definitions and their contents are ignored;
  7341. - Inlinetasks' boundaries are not shifted;
  7342. - Empty lines are ignored;
  7343. - Other lines' indentation are shifted by DIFF columns, unless
  7344. it would introduce a structural change in the document, in
  7345. which case no shifting is done at all.
  7346. Assume point is at a heading or an inlinetask beginning."
  7347. (org-with-wide-buffer
  7348. (narrow-to-region (line-beginning-position)
  7349. (save-excursion
  7350. (if (org-with-limited-levels (org-at-heading-p))
  7351. (org-with-limited-levels (outline-next-heading))
  7352. (org-inlinetask-goto-end))
  7353. (point)))
  7354. (forward-line)
  7355. ;; Indent properly planning info and property drawer.
  7356. (when (org-looking-at-p org-planning-line-re)
  7357. (org-indent-line)
  7358. (forward-line))
  7359. (when (looking-at org-property-drawer-re)
  7360. (goto-char (match-end 0))
  7361. (forward-line)
  7362. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7363. (catch 'no-shift
  7364. (when (zerop diff) (throw 'no-shift nil))
  7365. ;; If DIFF is negative, first check if a shift is possible at all
  7366. ;; (e.g., it doesn't break structure). This can only happen if
  7367. ;; some contents are not properly indented.
  7368. (let ((case-fold-search t))
  7369. (when (< diff 0)
  7370. (let ((diff (- diff))
  7371. (forbidden-re (concat org-outline-regexp
  7372. "\\|"
  7373. (substring org-footnote-definition-re 1))))
  7374. (save-excursion
  7375. (while (not (eobp))
  7376. (cond
  7377. ((org-looking-at-p "[ \t]*$") (forward-line))
  7378. ((and (org-looking-at-p org-footnote-definition-re)
  7379. (let ((e (org-element-at-point)))
  7380. (and (eq (org-element-type e) 'footnote-definition)
  7381. (goto-char (org-element-property :end e))))))
  7382. ((org-looking-at-p org-outline-regexp) (forward-line))
  7383. ;; Give up if shifting would move before column 0 or
  7384. ;; if it would introduce a headline or a footnote
  7385. ;; definition.
  7386. (t
  7387. (skip-chars-forward " \t")
  7388. (let ((ind (current-column)))
  7389. (when (or (< ind diff)
  7390. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7391. (throw 'no-shift nil)))
  7392. ;; Ignore contents of example blocks and source
  7393. ;; blocks if their indentation is meant to be
  7394. ;; preserved. Jump to block's closing line.
  7395. (beginning-of-line)
  7396. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7397. (let ((e (org-element-at-point)))
  7398. (and (memq (org-element-type e)
  7399. '(example-block src-block))
  7400. (or org-src-preserve-indentation
  7401. (org-element-property :preserve-indent e))
  7402. (goto-char (org-element-property :end e))
  7403. (progn (skip-chars-backward " \r\t\n")
  7404. (beginning-of-line)
  7405. t))))
  7406. (forward-line))))))))
  7407. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7408. ;; by DIFF. Also skip contents of source or example blocks
  7409. ;; when indentation is meant to be preserved.
  7410. (while (not (eobp))
  7411. (cond
  7412. ((and (org-looking-at-p org-footnote-definition-re)
  7413. (let ((e (org-element-at-point)))
  7414. (and (eq (org-element-type e) 'footnote-definition)
  7415. (goto-char (org-element-property :end e))))))
  7416. ((org-looking-at-p org-outline-regexp) (forward-line))
  7417. ((org-looking-at-p "[ \t]*$") (forward-line))
  7418. (t
  7419. (org-indent-line-to (+ (org-get-indentation) diff))
  7420. (beginning-of-line)
  7421. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7422. (let ((e (org-element-at-point)))
  7423. (and (memq (org-element-type e)
  7424. '(example-block src-block))
  7425. (or org-src-preserve-indentation
  7426. (org-element-property :preserve-indent e))
  7427. (goto-char (org-element-property :end e))
  7428. (progn (skip-chars-backward " \r\t\n")
  7429. (beginning-of-line)
  7430. t))))
  7431. (forward-line)))))))))
  7432. (defun org-convert-to-odd-levels ()
  7433. "Convert an org-mode file with all levels allowed to one with odd levels.
  7434. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7435. level 5 etc."
  7436. (interactive)
  7437. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7438. (let ((outline-level 'org-outline-level)
  7439. (org-odd-levels-only nil) n)
  7440. (save-excursion
  7441. (goto-char (point-min))
  7442. (while (re-search-forward "^\\*\\*+ " nil t)
  7443. (setq n (- (length (match-string 0)) 2))
  7444. (while (>= (setq n (1- n)) 0)
  7445. (org-demote))
  7446. (end-of-line 1))))))
  7447. (defun org-convert-to-oddeven-levels ()
  7448. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7449. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7450. file contains a section with an even level, conversion would
  7451. destroy the structure of the file. An error is signaled in this
  7452. case."
  7453. (interactive)
  7454. (goto-char (point-min))
  7455. ;; First check if there are no even levels
  7456. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7457. (org-show-context t)
  7458. (error "Not all levels are odd in this file. Conversion not possible"))
  7459. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7460. (let ((outline-regexp org-outline-regexp)
  7461. (outline-level 'org-outline-level)
  7462. (org-odd-levels-only nil) n)
  7463. (save-excursion
  7464. (goto-char (point-min))
  7465. (while (re-search-forward "^\\*\\*+ " nil t)
  7466. (setq n (/ (1- (length (match-string 0))) 2))
  7467. (while (>= (setq n (1- n)) 0)
  7468. (org-promote))
  7469. (end-of-line 1))))))
  7470. (defun org-tr-level (n)
  7471. "Make N odd if required."
  7472. (if org-odd-levels-only (1+ (/ n 2)) n))
  7473. ;;; Vertical tree motion, cutting and pasting of subtrees
  7474. (defun org-move-subtree-up (&optional arg)
  7475. "Move the current subtree up past ARG headlines of the same level."
  7476. (interactive "p")
  7477. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7478. (defun org-move-subtree-down (&optional arg)
  7479. "Move the current subtree down past ARG headlines of the same level."
  7480. (interactive "p")
  7481. (setq arg (prefix-numeric-value arg))
  7482. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7483. 'org-get-last-sibling))
  7484. (ins-point (make-marker))
  7485. (cnt (abs arg))
  7486. (col (current-column))
  7487. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7488. ;; Select the tree
  7489. (org-back-to-heading)
  7490. (setq beg0 (point))
  7491. (save-excursion
  7492. (setq ne-beg (org-back-over-empty-lines))
  7493. (setq beg (point)))
  7494. (save-match-data
  7495. (save-excursion (outline-end-of-heading)
  7496. (setq folded (outline-invisible-p)))
  7497. (progn (org-end-of-subtree nil t)
  7498. (unless (eobp) (backward-char))))
  7499. (outline-next-heading)
  7500. (setq ne-end (org-back-over-empty-lines))
  7501. (setq end (point))
  7502. (goto-char beg0)
  7503. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7504. ;; include less whitespace
  7505. (save-excursion
  7506. (goto-char beg)
  7507. (forward-line (- ne-beg ne-end))
  7508. (setq beg (point))))
  7509. ;; Find insertion point, with error handling
  7510. (while (> cnt 0)
  7511. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7512. (progn (goto-char beg0)
  7513. (user-error "Cannot move past superior level or buffer limit")))
  7514. (setq cnt (1- cnt)))
  7515. (if (> arg 0)
  7516. ;; Moving forward - still need to move over subtree
  7517. (progn (org-end-of-subtree t t)
  7518. (save-excursion
  7519. (org-back-over-empty-lines)
  7520. (or (bolp) (newline)))))
  7521. (setq ne-ins (org-back-over-empty-lines))
  7522. (move-marker ins-point (point))
  7523. (setq txt (buffer-substring beg end))
  7524. (org-save-markers-in-region beg end)
  7525. (delete-region beg end)
  7526. (org-remove-empty-overlays-at beg)
  7527. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7528. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7529. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7530. (let ((bbb (point)))
  7531. (insert-before-markers txt)
  7532. (org-reinstall-markers-in-region bbb)
  7533. (move-marker ins-point bbb))
  7534. (or (bolp) (insert "\n"))
  7535. (setq ins-end (point))
  7536. (goto-char ins-point)
  7537. (org-skip-whitespace)
  7538. (when (and (< arg 0)
  7539. (org-first-sibling-p)
  7540. (> ne-ins ne-beg))
  7541. ;; Move whitespace back to beginning
  7542. (save-excursion
  7543. (goto-char ins-end)
  7544. (let ((kill-whole-line t))
  7545. (kill-line (- ne-ins ne-beg)) (point)))
  7546. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7547. (move-marker ins-point nil)
  7548. (if folded
  7549. (hide-subtree)
  7550. (org-show-entry)
  7551. (show-children)
  7552. (org-cycle-hide-drawers 'children))
  7553. (org-clean-visibility-after-subtree-move)
  7554. ;; move back to the initial column we were at
  7555. (move-to-column col)))
  7556. (defvar org-subtree-clip ""
  7557. "Clipboard for cut and paste of subtrees.
  7558. This is actually only a copy of the kill, because we use the normal kill
  7559. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7560. (defvar org-subtree-clip-folded nil
  7561. "Was the last copied subtree folded?
  7562. This is used to fold the tree back after pasting.")
  7563. (defun org-cut-subtree (&optional n)
  7564. "Cut the current subtree into the clipboard.
  7565. With prefix arg N, cut this many sequential subtrees.
  7566. This is a short-hand for marking the subtree and then cutting it."
  7567. (interactive "p")
  7568. (org-copy-subtree n 'cut))
  7569. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7570. "Copy the current subtree it in the clipboard.
  7571. With prefix arg N, copy this many sequential subtrees.
  7572. This is a short-hand for marking the subtree and then copying it.
  7573. If CUT is non-nil, actually cut the subtree.
  7574. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7575. of some markers in the region, even if CUT is non-nil. This is
  7576. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7577. (interactive "p")
  7578. (let (beg end folded (beg0 (point)))
  7579. (if (org-called-interactively-p 'any)
  7580. (org-back-to-heading nil) ; take what looks like a subtree
  7581. (org-back-to-heading t)) ; take what is really there
  7582. (setq beg (point))
  7583. (skip-chars-forward " \t\r\n")
  7584. (save-match-data
  7585. (if nosubtrees
  7586. (outline-next-heading)
  7587. (save-excursion (outline-end-of-heading)
  7588. (setq folded (outline-invisible-p)))
  7589. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7590. (org-end-of-subtree t t)))
  7591. (setq end (point))
  7592. (goto-char beg0)
  7593. (when (> end beg)
  7594. (setq org-subtree-clip-folded folded)
  7595. (when (or cut force-store-markers)
  7596. (org-save-markers-in-region beg end))
  7597. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7598. (setq org-subtree-clip (current-kill 0))
  7599. (message "%s: Subtree(s) with %d characters"
  7600. (if cut "Cut" "Copied")
  7601. (length org-subtree-clip)))))
  7602. (defun org-paste-subtree (&optional level tree for-yank remove)
  7603. "Paste the clipboard as a subtree, with modification of headline level.
  7604. The entire subtree is promoted or demoted in order to match a new headline
  7605. level.
  7606. If the cursor is at the beginning of a headline, the same level as
  7607. that headline is used to paste the tree.
  7608. If not, the new level is derived from the *visible* headings
  7609. before and after the insertion point, and taken to be the inferior headline
  7610. level of the two. So if the previous visible heading is level 3 and the
  7611. next is level 4 (or vice versa), level 4 will be used for insertion.
  7612. This makes sure that the subtree remains an independent subtree and does
  7613. not swallow low level entries.
  7614. You can also force a different level, either by using a numeric prefix
  7615. argument, or by inserting the heading marker by hand. For example, if the
  7616. cursor is after \"*****\", then the tree will be shifted to level 5.
  7617. If optional TREE is given, use this text instead of the kill ring.
  7618. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7619. move back over whitespace before inserting, and move point to the end of
  7620. the inserted text when done.
  7621. When REMOVE is non-nil, remove the subtree from the clipboard."
  7622. (interactive "P")
  7623. (setq tree (or tree (and kill-ring (current-kill 0))))
  7624. (unless (org-kill-is-subtree-p tree)
  7625. (user-error "%s"
  7626. (substitute-command-keys
  7627. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7628. (org-with-limited-levels
  7629. (let* ((visp (not (outline-invisible-p)))
  7630. (txt tree)
  7631. (^re_ "\\(\\*+\\)[ \t]*")
  7632. (old-level (if (string-match org-outline-regexp-bol txt)
  7633. (- (match-end 0) (match-beginning 0) 1)
  7634. -1))
  7635. (force-level (cond (level (prefix-numeric-value level))
  7636. ((and (looking-at "[ \t]*$")
  7637. (string-match
  7638. "^\\*+$" (buffer-substring
  7639. (point-at-bol) (point))))
  7640. (- (match-end 0) (match-beginning 0)))
  7641. ((and (bolp)
  7642. (looking-at org-outline-regexp))
  7643. (- (match-end 0) (point) 1))))
  7644. (previous-level (save-excursion
  7645. (condition-case nil
  7646. (progn
  7647. (outline-previous-visible-heading 1)
  7648. (if (looking-at ^re_)
  7649. (- (match-end 0) (match-beginning 0) 1)
  7650. 1))
  7651. (error 1))))
  7652. (next-level (save-excursion
  7653. (condition-case nil
  7654. (progn
  7655. (or (looking-at org-outline-regexp)
  7656. (outline-next-visible-heading 1))
  7657. (if (looking-at ^re_)
  7658. (- (match-end 0) (match-beginning 0) 1)
  7659. 1))
  7660. (error 1))))
  7661. (new-level (or force-level (max previous-level next-level)))
  7662. (shift (if (or (= old-level -1)
  7663. (= new-level -1)
  7664. (= old-level new-level))
  7665. 0
  7666. (- new-level old-level)))
  7667. (delta (if (> shift 0) -1 1))
  7668. (func (if (> shift 0) 'org-demote 'org-promote))
  7669. (org-odd-levels-only nil)
  7670. beg end newend)
  7671. ;; Remove the forced level indicator
  7672. (if force-level
  7673. (delete-region (point-at-bol) (point)))
  7674. ;; Paste
  7675. (beginning-of-line (if (bolp) 1 2))
  7676. (setq beg (point))
  7677. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7678. (insert-before-markers txt)
  7679. (unless (string-match "\n\\'" txt) (insert "\n"))
  7680. (setq newend (point))
  7681. (org-reinstall-markers-in-region beg)
  7682. (setq end (point))
  7683. (goto-char beg)
  7684. (skip-chars-forward " \t\n\r")
  7685. (setq beg (point))
  7686. (if (and (outline-invisible-p) visp)
  7687. (save-excursion (outline-show-heading)))
  7688. ;; Shift if necessary
  7689. (unless (= shift 0)
  7690. (save-restriction
  7691. (narrow-to-region beg end)
  7692. (while (not (= shift 0))
  7693. (org-map-region func (point-min) (point-max))
  7694. (setq shift (+ delta shift)))
  7695. (goto-char (point-min))
  7696. (setq newend (point-max))))
  7697. (when (or (org-called-interactively-p 'interactive) for-yank)
  7698. (message "Clipboard pasted as level %d subtree" new-level))
  7699. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7700. kill-ring
  7701. (eq org-subtree-clip (current-kill 0))
  7702. org-subtree-clip-folded)
  7703. ;; The tree was folded before it was killed/copied
  7704. (hide-subtree))
  7705. (and for-yank (goto-char newend))
  7706. (and remove (setq kill-ring (cdr kill-ring))))))
  7707. (defun org-kill-is-subtree-p (&optional txt)
  7708. "Check if the current kill is an outline subtree, or a set of trees.
  7709. Returns nil if kill does not start with a headline, or if the first
  7710. headline level is not the largest headline level in the tree.
  7711. So this will actually accept several entries of equal levels as well,
  7712. which is OK for `org-paste-subtree'.
  7713. If optional TXT is given, check this string instead of the current kill."
  7714. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7715. (re (org-get-limited-outline-regexp))
  7716. (^re (concat "^" re))
  7717. (start-level (and kill
  7718. (string-match
  7719. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7720. kill)
  7721. (- (match-end 2) (match-beginning 2) 1)))
  7722. (start (1+ (or (match-beginning 2) -1))))
  7723. (if (not start-level)
  7724. (progn
  7725. nil) ;; does not even start with a heading
  7726. (catch 'exit
  7727. (while (setq start (string-match ^re kill (1+ start)))
  7728. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7729. (throw 'exit nil)))
  7730. t))))
  7731. (defvar org-markers-to-move nil
  7732. "Markers that should be moved with a cut-and-paste operation.
  7733. Those markers are stored together with their positions relative to
  7734. the start of the region.")
  7735. (defun org-save-markers-in-region (beg end)
  7736. "Check markers in region.
  7737. If these markers are between BEG and END, record their position relative
  7738. to BEG, so that after moving the block of text, we can put the markers back
  7739. into place.
  7740. This function gets called just before an entry or tree gets cut from the
  7741. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7742. called immediately, to move the markers with the entries."
  7743. (setq org-markers-to-move nil)
  7744. (when (featurep 'org-clock)
  7745. (org-clock-save-markers-for-cut-and-paste beg end))
  7746. (when (featurep 'org-agenda)
  7747. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7748. (defun org-check-and-save-marker (marker beg end)
  7749. "Check if MARKER is between BEG and END.
  7750. If yes, remember the marker and the distance to BEG."
  7751. (when (and (marker-buffer marker)
  7752. (equal (marker-buffer marker) (current-buffer)))
  7753. (if (and (>= marker beg) (< marker end))
  7754. (push (cons marker (- marker beg)) org-markers-to-move))))
  7755. (defun org-reinstall-markers-in-region (beg)
  7756. "Move all remembered markers to their position relative to BEG."
  7757. (mapc (lambda (x)
  7758. (move-marker (car x) (+ beg (cdr x))))
  7759. org-markers-to-move)
  7760. (setq org-markers-to-move nil))
  7761. (defun org-narrow-to-subtree ()
  7762. "Narrow buffer to the current subtree."
  7763. (interactive)
  7764. (save-excursion
  7765. (save-match-data
  7766. (org-with-limited-levels
  7767. (narrow-to-region
  7768. (progn (org-back-to-heading t) (point))
  7769. (progn (org-end-of-subtree t t)
  7770. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7771. (point)))))))
  7772. (defun org-narrow-to-block ()
  7773. "Narrow buffer to the current block."
  7774. (interactive)
  7775. (let* ((case-fold-search t)
  7776. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7777. "^[ \t]*#\\+end_.*")))
  7778. (if blockp
  7779. (narrow-to-region (car blockp) (cdr blockp))
  7780. (user-error "Not in a block"))))
  7781. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7782. "Clone the task (subtree) at point N times.
  7783. The clones will be inserted as siblings.
  7784. In interactive use, the user will be prompted for the number of
  7785. clones to be produced. If the entry has a timestamp, the user
  7786. will also be prompted for a time shift, which may be a repeater
  7787. as used in time stamps, for example `+3d'. To disable this,
  7788. you can call the function with a universal prefix argument.
  7789. When a valid repeater is given and the entry contains any time
  7790. stamps, the clones will become a sequence in time, with time
  7791. stamps in the subtree shifted for each clone produced. If SHIFT
  7792. is nil or the empty string, time stamps will be left alone. The
  7793. ID property of the original subtree is removed.
  7794. If the original subtree did contain time stamps with a repeater,
  7795. the following will happen:
  7796. - the repeater will be removed in each clone
  7797. - an additional clone will be produced, with the current, unshifted
  7798. date(s) in the entry.
  7799. - the original entry will be placed *after* all the clones, with
  7800. repeater intact.
  7801. - the start days in the repeater in the original entry will be shifted
  7802. to past the last clone.
  7803. In this way you can spell out a number of instances of a repeating task,
  7804. and still retain the repeater to cover future instances of the task."
  7805. (interactive "nNumber of clones to produce: ")
  7806. (let ((shift
  7807. (or shift
  7808. (if (and (not (equal current-prefix-arg '(4)))
  7809. (save-excursion
  7810. (re-search-forward org-ts-regexp-both
  7811. (save-excursion
  7812. (org-end-of-subtree t)
  7813. (point)) t)))
  7814. (read-from-minibuffer
  7815. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7816. ""))) ;; No time shift
  7817. (n-no-remove -1)
  7818. (drawer-re org-drawer-regexp)
  7819. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7820. beg end template task idprop
  7821. shift-n shift-what doshift nmin nmax)
  7822. (if (not (and (integerp n) (> n 0)))
  7823. (user-error "Invalid number of replications %s" n))
  7824. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7825. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7826. shift)))
  7827. (user-error "Invalid shift specification %s" shift))
  7828. (when doshift
  7829. (setq shift-n (string-to-number (match-string 1 shift))
  7830. shift-what (cdr (assoc (match-string 2 shift)
  7831. '(("d" . day) ("w" . week)
  7832. ("m" . month) ("y" . year))))))
  7833. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7834. (setq nmin 1 nmax n)
  7835. (org-back-to-heading t)
  7836. (setq beg (point))
  7837. (setq idprop (org-entry-get nil "ID"))
  7838. (org-end-of-subtree t t)
  7839. (or (bolp) (insert "\n"))
  7840. (setq end (point))
  7841. (setq template (buffer-substring beg end))
  7842. (when (and doshift
  7843. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7844. (delete-region beg end)
  7845. (setq end beg)
  7846. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7847. (goto-char end)
  7848. (loop for n from nmin to nmax do
  7849. ;; prepare clone
  7850. (with-temp-buffer
  7851. (insert template)
  7852. (org-mode)
  7853. (goto-char (point-min))
  7854. (org-show-subtree)
  7855. (and idprop (if org-clone-delete-id
  7856. (org-entry-delete nil "ID")
  7857. (org-id-get-create t)))
  7858. (unless (= n 0)
  7859. (while (re-search-forward org-clock-re nil t)
  7860. (kill-whole-line))
  7861. (goto-char (point-min))
  7862. (while (re-search-forward drawer-re nil t)
  7863. (org-remove-empty-drawer-at (point))))
  7864. (goto-char (point-min))
  7865. (when doshift
  7866. (while (re-search-forward org-ts-regexp-both nil t)
  7867. (org-timestamp-change (* n shift-n) shift-what))
  7868. (unless (= n n-no-remove)
  7869. (goto-char (point-min))
  7870. (while (re-search-forward org-ts-regexp nil t)
  7871. (save-excursion
  7872. (goto-char (match-beginning 0))
  7873. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7874. (delete-region (match-beginning 1) (match-end 1)))))))
  7875. (setq task (buffer-string)))
  7876. (insert task))
  7877. (goto-char beg)))
  7878. ;;; Outline Sorting
  7879. (defun org-sort (with-case)
  7880. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7881. Optional argument WITH-CASE means sort case-sensitively."
  7882. (interactive "P")
  7883. (cond
  7884. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7885. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7886. (t
  7887. (org-call-with-arg 'org-sort-entries with-case))))
  7888. (defun org-sort-remove-invisible (s)
  7889. "Remove invisible links from string S."
  7890. (remove-text-properties 0 (length s) org-rm-props s)
  7891. (while (string-match org-bracket-link-regexp s)
  7892. (setq s (replace-match (if (match-end 2)
  7893. (match-string 3 s)
  7894. (match-string 1 s)) t t s)))
  7895. (let ((st (format " %s " s)))
  7896. (while (string-match org-emph-re st)
  7897. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7898. (setq s (substring st 1 -1)))
  7899. s)
  7900. (defvar org-priority-regexp) ; defined later in the file
  7901. (defvar org-after-sorting-entries-or-items-hook nil
  7902. "Hook that is run after a bunch of entries or items have been sorted.
  7903. When children are sorted, the cursor is in the parent line when this
  7904. hook gets called. When a region or a plain list is sorted, the cursor
  7905. will be in the first entry of the sorted region/list.")
  7906. (defun org-sort-entries
  7907. (&optional with-case sorting-type getkey-func compare-func property)
  7908. "Sort entries on a certain level of an outline tree.
  7909. If there is an active region, the entries in the region are sorted.
  7910. Else, if the cursor is before the first entry, sort the top-level items.
  7911. Else, the children of the entry at point are sorted.
  7912. Sorting can be alphabetically, numerically, by date/time as given by
  7913. a time stamp, by a property, by priority order, or by a custom function.
  7914. The command prompts for the sorting type unless it has been given to the
  7915. function through the SORTING-TYPE argument, which needs to be a character,
  7916. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7917. the precise meaning of each character:
  7918. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7919. c By creation time, which is assumed to be the first inactive time stamp
  7920. at the beginning of a line.
  7921. d By deadline date/time.
  7922. k By clocking time.
  7923. n Numerically, by converting the beginning of the entry/item to a number.
  7924. o By order of TODO keywords.
  7925. p By priority according to the cookie.
  7926. r By the value of a property.
  7927. s By scheduled date/time.
  7928. t By date/time, either the first active time stamp in the entry, or, if
  7929. none exist, by the first inactive one.
  7930. Capital letters will reverse the sort order.
  7931. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7932. called with point at the beginning of the record. It must return either
  7933. a string or a number that should serve as the sorting key for that record.
  7934. Comparing entries ignores case by default. However, with an optional argument
  7935. WITH-CASE, the sorting considers case as well.
  7936. Sorting is done against the visible part of the headlines, it ignores hidden
  7937. links.
  7938. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7939. (interactive "P")
  7940. (let ((case-func (if with-case 'identity 'downcase))
  7941. (cmstr
  7942. ;; The clock marker is lost when using `sort-subr', let's
  7943. ;; store the clocking string.
  7944. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7945. (save-excursion
  7946. (goto-char org-clock-marker)
  7947. (looking-back "^.*") (match-string-no-properties 0))))
  7948. start beg end stars re re2
  7949. txt what tmp)
  7950. ;; Find beginning and end of region to sort
  7951. (cond
  7952. ((org-region-active-p)
  7953. ;; we will sort the region
  7954. (setq end (region-end)
  7955. what "region")
  7956. (goto-char (region-beginning))
  7957. (if (not (org-at-heading-p)) (outline-next-heading))
  7958. (setq start (point)))
  7959. ((or (org-at-heading-p)
  7960. (ignore-errors (progn (org-back-to-heading) t)))
  7961. ;; we will sort the children of the current headline
  7962. (org-back-to-heading)
  7963. (setq start (point)
  7964. end (progn (org-end-of-subtree t t)
  7965. (or (bolp) (insert "\n"))
  7966. (when (>= (org-back-over-empty-lines) 1)
  7967. (forward-line 1))
  7968. (point))
  7969. what "children")
  7970. (goto-char start)
  7971. (show-subtree)
  7972. (outline-next-heading))
  7973. (t
  7974. ;; we will sort the top-level entries in this file
  7975. (goto-char (point-min))
  7976. (or (org-at-heading-p) (outline-next-heading))
  7977. (setq start (point))
  7978. (goto-char (point-max))
  7979. (beginning-of-line 1)
  7980. (when (looking-at ".*?\\S-")
  7981. ;; File ends in a non-white line
  7982. (end-of-line 1)
  7983. (insert "\n"))
  7984. (setq end (point-max))
  7985. (setq what "top-level")
  7986. (goto-char start)
  7987. (show-all)))
  7988. (setq beg (point))
  7989. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7990. (looking-at "\\(\\*+\\)")
  7991. (setq stars (match-string 1)
  7992. re (concat "^" (regexp-quote stars) " +")
  7993. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7994. txt (buffer-substring beg end))
  7995. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7996. (if (and (not (equal stars "*")) (string-match re2 txt))
  7997. (user-error "Region to sort contains a level above the first entry"))
  7998. (unless sorting-type
  7999. (message
  8000. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8001. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8002. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8003. what)
  8004. (setq sorting-type (read-char-exclusive))
  8005. (unless getkey-func
  8006. (and (= (downcase sorting-type) ?f)
  8007. (setq getkey-func
  8008. (org-icompleting-read "Sort using function: "
  8009. obarray 'fboundp t nil nil))
  8010. (setq getkey-func (intern getkey-func))))
  8011. (and (= (downcase sorting-type) ?r)
  8012. (not property)
  8013. (setq property
  8014. (org-icompleting-read "Property: "
  8015. (mapcar 'list (org-buffer-property-keys t))
  8016. nil t))))
  8017. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8018. (message "Sorting entries...")
  8019. (save-restriction
  8020. (narrow-to-region start end)
  8021. (let ((dcst (downcase sorting-type))
  8022. (case-fold-search nil)
  8023. (now (current-time)))
  8024. (sort-subr
  8025. (/= dcst sorting-type)
  8026. ;; This function moves to the beginning character of the "record" to
  8027. ;; be sorted.
  8028. (lambda nil
  8029. (if (re-search-forward re nil t)
  8030. (goto-char (match-beginning 0))
  8031. (goto-char (point-max))))
  8032. ;; This function moves to the last character of the "record" being
  8033. ;; sorted.
  8034. (lambda nil
  8035. (save-match-data
  8036. (condition-case nil
  8037. (outline-forward-same-level 1)
  8038. (error
  8039. (goto-char (point-max))))))
  8040. ;; This function returns the value that gets sorted against.
  8041. (lambda nil
  8042. (cond
  8043. ((= dcst ?n)
  8044. (if (looking-at org-complex-heading-regexp)
  8045. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8046. nil))
  8047. ((= dcst ?a)
  8048. (if (looking-at org-complex-heading-regexp)
  8049. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8050. nil))
  8051. ((= dcst ?k)
  8052. (or (get-text-property (point) :org-clock-minutes) 0))
  8053. ((= dcst ?t)
  8054. (let ((end (save-excursion (outline-next-heading) (point))))
  8055. (if (or (re-search-forward org-ts-regexp end t)
  8056. (re-search-forward org-ts-regexp-both end t))
  8057. (org-time-string-to-seconds (match-string 0))
  8058. (org-float-time now))))
  8059. ((= dcst ?c)
  8060. (let ((end (save-excursion (outline-next-heading) (point))))
  8061. (if (re-search-forward
  8062. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8063. end t)
  8064. (org-time-string-to-seconds (match-string 0))
  8065. (org-float-time now))))
  8066. ((= dcst ?s)
  8067. (let ((end (save-excursion (outline-next-heading) (point))))
  8068. (if (re-search-forward org-scheduled-time-regexp end t)
  8069. (org-time-string-to-seconds (match-string 1))
  8070. (org-float-time now))))
  8071. ((= dcst ?d)
  8072. (let ((end (save-excursion (outline-next-heading) (point))))
  8073. (if (re-search-forward org-deadline-time-regexp end t)
  8074. (org-time-string-to-seconds (match-string 1))
  8075. (org-float-time now))))
  8076. ((= dcst ?p)
  8077. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8078. (string-to-char (match-string 2))
  8079. org-default-priority))
  8080. ((= dcst ?r)
  8081. (or (org-entry-get nil property) ""))
  8082. ((= dcst ?o)
  8083. (if (looking-at org-complex-heading-regexp)
  8084. (let* ((m (match-string 2))
  8085. (s (if (member m org-done-keywords) '- '+)))
  8086. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8087. ((= dcst ?f)
  8088. (if getkey-func
  8089. (progn
  8090. (setq tmp (funcall getkey-func))
  8091. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8092. tmp)
  8093. (error "Invalid key function `%s'" getkey-func)))
  8094. (t (error "Invalid sorting type `%c'" sorting-type))))
  8095. nil
  8096. (cond
  8097. ((= dcst ?a) 'string<)
  8098. ((= dcst ?f) compare-func)
  8099. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8100. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8101. ;; Reset the clock marker if needed
  8102. (when cmstr
  8103. (save-excursion
  8104. (goto-char start)
  8105. (search-forward cmstr nil t)
  8106. (move-marker org-clock-marker (point))))
  8107. (message "Sorting entries...done")))
  8108. ;;; The orgstruct minor mode
  8109. ;; Define a minor mode which can be used in other modes in order to
  8110. ;; integrate the org-mode structure editing commands.
  8111. ;; This is really a hack, because the org-mode structure commands use
  8112. ;; keys which normally belong to the major mode. Here is how it
  8113. ;; works: The minor mode defines all the keys necessary to operate the
  8114. ;; structure commands, but wraps the commands into a function which
  8115. ;; tests if the cursor is currently at a headline or a plain list
  8116. ;; item. If that is the case, the structure command is used,
  8117. ;; temporarily setting many Org-mode variables like regular
  8118. ;; expressions for filling etc. However, when any of those keys is
  8119. ;; used at a different location, function uses `key-binding' to look
  8120. ;; up if the key has an associated command in another currently active
  8121. ;; keymap (minor modes, major mode, global), and executes that
  8122. ;; command. There might be problems if any of the keys is otherwise
  8123. ;; used as a prefix key.
  8124. (defcustom orgstruct-heading-prefix-regexp ""
  8125. "Regexp that matches the custom prefix of Org headlines in
  8126. orgstruct(++)-mode."
  8127. :group 'org
  8128. :version "24.4"
  8129. :package-version '(Org . "8.3")
  8130. :type 'regexp)
  8131. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8132. (defcustom orgstruct-setup-hook nil
  8133. "Hook run after orgstruct-mode-map is filled."
  8134. :group 'org
  8135. :version "24.4"
  8136. :package-version '(Org . "8.0")
  8137. :type 'hook)
  8138. (defvar orgstruct-initialized nil)
  8139. (defvar org-local-vars nil
  8140. "List of local variables, for use by `orgstruct-mode'.")
  8141. ;;;###autoload
  8142. (define-minor-mode orgstruct-mode
  8143. "Toggle the minor mode `orgstruct-mode'.
  8144. This mode is for using Org-mode structure commands in other
  8145. modes. The following keys behave as if Org-mode were active, if
  8146. the cursor is on a headline, or on a plain list item (both as
  8147. defined by Org-mode)."
  8148. nil " OrgStruct" (make-sparse-keymap)
  8149. (funcall (if orgstruct-mode
  8150. 'add-to-invisibility-spec
  8151. 'remove-from-invisibility-spec)
  8152. '(outline . t))
  8153. (when orgstruct-mode
  8154. (org-load-modules-maybe)
  8155. (unless orgstruct-initialized
  8156. (orgstruct-setup)
  8157. (setq orgstruct-initialized t))))
  8158. ;;;###autoload
  8159. (defun turn-on-orgstruct ()
  8160. "Unconditionally turn on `orgstruct-mode'."
  8161. (orgstruct-mode 1))
  8162. (defvar org-fb-vars nil)
  8163. (make-variable-buffer-local 'org-fb-vars)
  8164. (defun orgstruct++-mode (&optional arg)
  8165. "Toggle `orgstruct-mode', the enhanced version of it.
  8166. In addition to setting orgstruct-mode, this also exports all
  8167. indentation and autofilling variables from org-mode into the
  8168. buffer. It will also recognize item context in multiline items."
  8169. (interactive "P")
  8170. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8171. (if (< arg 1)
  8172. (progn (orgstruct-mode -1)
  8173. (mapc (lambda(v)
  8174. (org-set-local (car v)
  8175. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8176. org-fb-vars))
  8177. (orgstruct-mode 1)
  8178. (setq org-fb-vars nil)
  8179. (unless org-local-vars
  8180. (setq org-local-vars (org-get-local-variables)))
  8181. (let (var val)
  8182. (mapc
  8183. (lambda (x)
  8184. (when (string-match
  8185. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8186. (symbol-name (car x)))
  8187. (setq var (car x) val (nth 1 x))
  8188. (push (list var `(quote ,(eval var))) org-fb-vars)
  8189. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8190. org-local-vars)
  8191. (org-set-local 'orgstruct-is-++ t))))
  8192. (defvar orgstruct-is-++ nil
  8193. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8194. (make-variable-buffer-local 'orgstruct-is-++)
  8195. ;;;###autoload
  8196. (defun turn-on-orgstruct++ ()
  8197. "Unconditionally turn on `orgstruct++-mode'."
  8198. (orgstruct++-mode 1))
  8199. (defun orgstruct-error ()
  8200. "Error when there is no default binding for a structure key."
  8201. (interactive)
  8202. (funcall (if (fboundp 'user-error)
  8203. 'user-error
  8204. 'error)
  8205. "This key has no function outside structure elements"))
  8206. (defun orgstruct-setup ()
  8207. "Setup orgstruct keymap."
  8208. (dolist (cell '((org-demote . t)
  8209. (org-metaleft . t)
  8210. (org-metaright . t)
  8211. (org-promote . t)
  8212. (org-shiftmetaleft . t)
  8213. (org-shiftmetaright . t)
  8214. org-backward-element
  8215. org-backward-heading-same-level
  8216. org-ctrl-c-ret
  8217. org-ctrl-c-minus
  8218. org-ctrl-c-star
  8219. org-cycle
  8220. org-forward-heading-same-level
  8221. org-insert-heading
  8222. org-insert-heading-respect-content
  8223. org-kill-note-or-show-branches
  8224. org-mark-subtree
  8225. org-meta-return
  8226. org-metadown
  8227. org-metaup
  8228. org-narrow-to-subtree
  8229. org-promote-subtree
  8230. org-reveal
  8231. org-shiftdown
  8232. org-shiftleft
  8233. org-shiftmetadown
  8234. org-shiftmetaup
  8235. org-shiftright
  8236. org-shifttab
  8237. org-shifttab
  8238. org-shiftup
  8239. org-show-subtree
  8240. org-sort
  8241. org-up-element
  8242. outline-demote
  8243. outline-next-visible-heading
  8244. outline-previous-visible-heading
  8245. outline-promote
  8246. outline-up-heading
  8247. show-children))
  8248. (let ((f (or (car-safe cell) cell))
  8249. (disable-when-heading-prefix (cdr-safe cell)))
  8250. (when (fboundp f)
  8251. (let ((new-bindings))
  8252. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8253. (where-is-internal f outline-mode-map)))
  8254. (push binding new-bindings)
  8255. ;; TODO use local-function-key-map
  8256. (dolist (rep '(("<tab>" . "TAB")
  8257. ("<return>" . "RET")
  8258. ("<escape>" . "ESC")
  8259. ("<delete>" . "DEL")))
  8260. (setq binding (read-kbd-macro
  8261. (let ((case-fold-search))
  8262. (replace-regexp-in-string
  8263. (regexp-quote (cdr rep))
  8264. (car rep)
  8265. (key-description binding)))))
  8266. (pushnew binding new-bindings :test 'equal)))
  8267. (dolist (binding new-bindings)
  8268. (let ((key (lookup-key orgstruct-mode-map binding)))
  8269. (when (or (not key) (numberp key))
  8270. (ignore-errors
  8271. (org-defkey orgstruct-mode-map
  8272. binding
  8273. (orgstruct-make-binding
  8274. f binding disable-when-heading-prefix))))))))))
  8275. (run-hooks 'orgstruct-setup-hook))
  8276. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8277. "Create a function for binding in the structure minor mode.
  8278. FUN is the command to call inside a table. KEY is the key that
  8279. should be checked in for a command to execute outside of tables.
  8280. Non-nil `disable-when-heading-prefix' means to disable the command
  8281. if `orgstruct-heading-prefix-regexp' is not empty."
  8282. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8283. (let ((nname name)
  8284. (i 0))
  8285. (while (fboundp (intern nname))
  8286. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8287. (setq name (intern nname)))
  8288. (eval
  8289. (let ((bindings '((org-heading-regexp
  8290. (concat "^"
  8291. orgstruct-heading-prefix-regexp
  8292. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8293. (org-outline-regexp
  8294. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8295. (org-outline-regexp-bol
  8296. (concat "^" org-outline-regexp))
  8297. (outline-regexp org-outline-regexp)
  8298. (outline-heading-end-regexp "\n")
  8299. (outline-level 'org-outline-level)
  8300. (outline-heading-alist))))
  8301. `(defun ,name (arg)
  8302. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8303. "Outside of structure, run the binding of `"
  8304. (key-description key) "'."
  8305. (when disable-when-heading-prefix
  8306. (concat
  8307. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8308. "back to the default binding due to limitations of Org's implementation of\n"
  8309. "`" (symbol-name fun) "'.")))
  8310. (interactive "p")
  8311. (let* ((disable
  8312. ,(and disable-when-heading-prefix
  8313. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8314. (fallback
  8315. (or disable
  8316. (not
  8317. (let* ,bindings
  8318. (org-context-p 'headline 'item
  8319. ,(when (memq fun
  8320. '(org-insert-heading
  8321. org-insert-heading-respect-content
  8322. org-meta-return))
  8323. '(when orgstruct-is-++
  8324. 'item-body))))))))
  8325. (if fallback
  8326. (let* ((orgstruct-mode)
  8327. (binding
  8328. (let ((key ,key))
  8329. (catch 'exit
  8330. (dolist
  8331. (rep
  8332. '(nil
  8333. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8334. ("<\\([^>]*\\)return>" . "\\1RET")
  8335. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8336. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8337. nil)
  8338. (when rep
  8339. (setq key (read-kbd-macro
  8340. (let ((case-fold-search))
  8341. (replace-regexp-in-string
  8342. (car rep)
  8343. (cdr rep)
  8344. (key-description key))))))
  8345. (when (key-binding key)
  8346. (throw 'exit (key-binding key))))))))
  8347. (if (keymapp binding)
  8348. (org-set-transient-map binding)
  8349. (let ((func (or binding
  8350. (unless disable
  8351. 'orgstruct-error))))
  8352. (when func
  8353. (call-interactively func)))))
  8354. (org-run-like-in-org-mode
  8355. (lambda ()
  8356. (interactive)
  8357. (let* ,bindings
  8358. (call-interactively ',fun)))))))))
  8359. name))
  8360. (defun org-contextualize-keys (alist contexts)
  8361. "Return valid elements in ALIST depending on CONTEXTS.
  8362. `org-agenda-custom-commands' or `org-capture-templates' are the
  8363. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8364. or `org-capture-templates-contexts' are the associated contexts
  8365. definitions."
  8366. (let ((contexts
  8367. ;; normalize contexts
  8368. (mapcar
  8369. (lambda(c) (cond ((listp (cadr c))
  8370. (list (car c) (car c) (cadr c)))
  8371. ((string= "" (cadr c))
  8372. (list (car c) (car c) (caddr c)))
  8373. (t c))) contexts))
  8374. (a alist) c r s)
  8375. ;; loop over all commands or templates
  8376. (while (setq c (pop a))
  8377. (let (vrules repl)
  8378. (cond
  8379. ((not (assoc (car c) contexts))
  8380. (push c r))
  8381. ((and (assoc (car c) contexts)
  8382. (setq vrules (org-contextualize-validate-key
  8383. (car c) contexts)))
  8384. (mapc (lambda (vr)
  8385. (when (not (equal (car vr) (cadr vr)))
  8386. (setq repl vr))) vrules)
  8387. (if (not repl) (push c r)
  8388. (push (cadr repl) s)
  8389. (push
  8390. (cons (car c)
  8391. (cdr (or (assoc (cadr repl) alist)
  8392. (error "Undefined key `%s' as contextual replacement for `%s'"
  8393. (cadr repl) (car c)))))
  8394. r))))))
  8395. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8396. (delq
  8397. nil
  8398. (delete-dups
  8399. (mapcar (lambda (x)
  8400. (let ((tpl (car x)))
  8401. (when (not (delq
  8402. nil
  8403. (mapcar (lambda(y)
  8404. (equal y tpl)) s))) x)))
  8405. (reverse r))))))
  8406. (defun org-contextualize-validate-key (key contexts)
  8407. "Check CONTEXTS for agenda or capture KEY."
  8408. (let (r rr res)
  8409. (while (setq r (pop contexts))
  8410. (mapc
  8411. (lambda (rr)
  8412. (when
  8413. (and (equal key (car r))
  8414. (if (functionp rr) (funcall rr)
  8415. (or (and (eq (car rr) 'in-file)
  8416. (buffer-file-name)
  8417. (string-match (cdr rr) (buffer-file-name)))
  8418. (and (eq (car rr) 'in-mode)
  8419. (string-match (cdr rr) (symbol-name major-mode)))
  8420. (and (eq (car rr) 'in-buffer)
  8421. (string-match (cdr rr) (buffer-name)))
  8422. (when (and (eq (car rr) 'not-in-file)
  8423. (buffer-file-name))
  8424. (not (string-match (cdr rr) (buffer-file-name))))
  8425. (when (eq (car rr) 'not-in-mode)
  8426. (not (string-match (cdr rr) (symbol-name major-mode))))
  8427. (when (eq (car rr) 'not-in-buffer)
  8428. (not (string-match (cdr rr) (buffer-name)))))))
  8429. (push r res)))
  8430. (car (last r))))
  8431. (delete-dups (delq nil res))))
  8432. (defun org-context-p (&rest contexts)
  8433. "Check if local context is any of CONTEXTS.
  8434. Possible values in the list of contexts are `table', `headline', and `item'."
  8435. (let ((pos (point)))
  8436. (goto-char (point-at-bol))
  8437. (prog1 (or (and (memq 'table contexts)
  8438. (looking-at "[ \t]*|"))
  8439. (and (memq 'headline contexts)
  8440. (looking-at org-outline-regexp))
  8441. (and (memq 'item contexts)
  8442. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8443. (and (memq 'item-body contexts)
  8444. (org-in-item-p)))
  8445. (goto-char pos))))
  8446. (defun org-get-local-variables ()
  8447. "Return a list of all local variables in an Org mode buffer."
  8448. (let (varlist)
  8449. (with-current-buffer (get-buffer-create "*Org tmp*")
  8450. (erase-buffer)
  8451. (org-mode)
  8452. (setq varlist (buffer-local-variables)))
  8453. (kill-buffer "*Org tmp*")
  8454. (delq nil
  8455. (mapcar
  8456. (lambda (x)
  8457. (setq x
  8458. (if (symbolp x)
  8459. (list x)
  8460. (list (car x) (cdr x))))
  8461. (if (and (not (get (car x) 'org-state))
  8462. (string-match
  8463. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8464. (symbol-name (car x))))
  8465. x nil))
  8466. varlist))))
  8467. (defun org-clone-local-variables (from-buffer &optional regexp)
  8468. "Clone local variables from FROM-BUFFER.
  8469. Optional argument REGEXP selects variables to clone."
  8470. (mapc
  8471. (lambda (pair)
  8472. (and (symbolp (car pair))
  8473. (or (null regexp)
  8474. (string-match regexp (symbol-name (car pair))))
  8475. (set (make-local-variable (car pair))
  8476. (cdr pair))))
  8477. (buffer-local-variables from-buffer)))
  8478. ;;;###autoload
  8479. (defun org-run-like-in-org-mode (cmd)
  8480. "Run a command, pretending that the current buffer is in Org-mode.
  8481. This will temporarily bind local variables that are typically bound in
  8482. Org-mode to the values they have in Org-mode, and then interactively
  8483. call CMD."
  8484. (org-load-modules-maybe)
  8485. (unless org-local-vars
  8486. (setq org-local-vars (org-get-local-variables)))
  8487. (let (binds)
  8488. (dolist (var org-local-vars)
  8489. (when (or (not (boundp (car var)))
  8490. (eq (symbol-value (car var))
  8491. (default-value (car var))))
  8492. (push (list (car var) `(quote ,(cadr var))) binds)))
  8493. (eval `(let ,binds
  8494. (call-interactively (quote ,cmd))))))
  8495. (defun org-get-category (&optional pos force-refresh)
  8496. "Get the category applying to position POS."
  8497. (save-match-data
  8498. (if force-refresh (org-refresh-category-properties))
  8499. (let ((pos (or pos (point))))
  8500. (or (get-text-property pos 'org-category)
  8501. (progn (org-refresh-category-properties)
  8502. (get-text-property pos 'org-category))))))
  8503. ;;; Refresh properties
  8504. (defun org-refresh-properties (dprop tprop)
  8505. "Refresh buffer text properties.
  8506. DPROP is the drawer property and TPROP is either the
  8507. corresponding text property to set, or an alist with each element
  8508. being a text property (as a symbol) and a function to apply to
  8509. the value of the drawer property."
  8510. (let ((case-fold-search t)
  8511. (inhibit-read-only t))
  8512. (org-with-silent-modifications
  8513. (save-excursion
  8514. (save-restriction
  8515. (widen)
  8516. (goto-char (point-min))
  8517. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8518. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8519. (defun org-refresh-property (tprop p)
  8520. "Refresh the buffer text property TPROP from the drawer property P.
  8521. The refresh happens only for the current tree (not subtree)."
  8522. (save-excursion
  8523. (org-back-to-heading t)
  8524. ;; tprop is a text property symbol
  8525. (if (symbolp tprop)
  8526. (put-text-property
  8527. (point) (or (outline-next-heading) (point-max)) tprop p)
  8528. ;; tprop is an alist with (properties . function) elements
  8529. (mapc (lambda(al)
  8530. (save-excursion
  8531. (put-text-property
  8532. (point-at-bol) (or (outline-next-heading) (point-max))
  8533. (car al)
  8534. (funcall (cdr al) p))))
  8535. tprop))))
  8536. (defun org-refresh-category-properties ()
  8537. "Refresh category text properties in the buffer."
  8538. (let ((case-fold-search t)
  8539. (inhibit-read-only t)
  8540. (def-cat (cond
  8541. ((null org-category)
  8542. (if buffer-file-name
  8543. (file-name-sans-extension
  8544. (file-name-nondirectory buffer-file-name))
  8545. "???"))
  8546. ((symbolp org-category) (symbol-name org-category))
  8547. (t org-category)))
  8548. beg end cat pos optionp)
  8549. (org-with-silent-modifications
  8550. (save-excursion
  8551. (save-restriction
  8552. (widen)
  8553. (goto-char (point-min))
  8554. (put-text-property (point) (point-max) 'org-category def-cat)
  8555. (while (re-search-forward
  8556. "^[ \t]*\\(\\(?:#\\+\\|:\\)CATEGORY:\\)\\(.*\\)" nil t)
  8557. (setq pos (match-end 0)
  8558. optionp (equal (char-after (match-beginning 0)) ?#)
  8559. cat (org-trim (match-string 2)))
  8560. (if optionp
  8561. (setq beg (point-at-bol) end (point-max))
  8562. (org-back-to-heading t)
  8563. (setq beg (point) end (org-end-of-subtree t t)))
  8564. (put-text-property beg end 'org-category cat)
  8565. (goto-char pos)))))))
  8566. (defun org-refresh-stats-properties ()
  8567. "Refresh stats text properties in the buffer."
  8568. (let (stats)
  8569. (org-with-silent-modifications
  8570. (save-excursion
  8571. (save-restriction
  8572. (widen)
  8573. (goto-char (point-min))
  8574. (while (re-search-forward
  8575. (concat org-outline-regexp-bol ".*"
  8576. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8577. nil t)
  8578. (setq stats (cond ((equal (match-string 3) "0") 0)
  8579. ((match-string 2)
  8580. (/ (* (string-to-number (match-string 2)) 100)
  8581. (string-to-number (match-string 3))))
  8582. (t (string-to-number (match-string 1)))))
  8583. (org-back-to-heading t)
  8584. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8585. 'org-stats stats)))))))
  8586. (defun org-refresh-effort-properties ()
  8587. "Refresh effort properties"
  8588. (org-refresh-properties
  8589. org-effort-property
  8590. '((effort . identity)
  8591. (effort-minutes . org-duration-string-to-minutes))))
  8592. ;;;; Link Stuff
  8593. ;;; Link abbreviations
  8594. (defun org-link-expand-abbrev (link)
  8595. "Apply replacements as defined in `org-link-abbrev-alist'."
  8596. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8597. (let* ((key (match-string 1 link))
  8598. (as (or (assoc key org-link-abbrev-alist-local)
  8599. (assoc key org-link-abbrev-alist)))
  8600. (tag (and (match-end 2) (match-string 3 link)))
  8601. rpl)
  8602. (if (not as)
  8603. link
  8604. (setq rpl (cdr as))
  8605. (cond
  8606. ((symbolp rpl) (funcall rpl tag))
  8607. ((string-match "%(\\([^)]+\\))" rpl)
  8608. (replace-match
  8609. (save-match-data
  8610. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8611. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8612. ((string-match "%h" rpl)
  8613. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8614. (t (concat rpl tag)))))
  8615. link))
  8616. ;;; Storing and inserting links
  8617. (defvar org-insert-link-history nil
  8618. "Minibuffer history for links inserted with `org-insert-link'.")
  8619. (defvar org-stored-links nil
  8620. "Contains the links stored with `org-store-link'.")
  8621. (defvar org-store-link-plist nil
  8622. "Plist with info about the most recently link created with `org-store-link'.")
  8623. (defvar org-link-protocols nil
  8624. "Link protocols added to Org-mode using `org-add-link-type'.")
  8625. (defvar org-store-link-functions nil
  8626. "List of functions that are called to create and store a link.
  8627. Each function will be called in turn until one returns a non-nil
  8628. value. Each function should check if it is responsible for creating
  8629. this link (for example by looking at the major mode).
  8630. If not, it must exit and return nil.
  8631. If yes, it should return a non-nil value after a calling
  8632. `org-store-link-props' with a list of properties and values.
  8633. Special properties are:
  8634. :type The link prefix, like \"http\". This must be given.
  8635. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8636. This is obligatory as well.
  8637. :description Optional default description for the second pair
  8638. of brackets in an Org-mode link. The user can still change
  8639. this when inserting this link into an Org-mode buffer.
  8640. In addition to these, any additional properties can be specified
  8641. and then used in capture templates.")
  8642. (defun org-add-link-type (type &optional follow export)
  8643. "Add TYPE to the list of `org-link-types'.
  8644. Re-compute all regular expressions depending on `org-link-types'
  8645. FOLLOW and EXPORT are two functions.
  8646. FOLLOW should take the link path as the single argument and do whatever
  8647. is necessary to follow the link, for example find a file or display
  8648. a mail message.
  8649. EXPORT should format the link path for export to one of the export formats.
  8650. It should be a function accepting three arguments:
  8651. path the path of the link, the text after the prefix (like \"http:\")
  8652. desc the description of the link, if any, or a description added by
  8653. org-export-normalize-links if there is none
  8654. format the export format, a symbol like `html' or `latex' or `ascii'..
  8655. The function may use the FORMAT information to return different values
  8656. depending on the format. The return value will be put literally into
  8657. the exported file. If the return value is nil, this means Org should
  8658. do what it normally does with links which do not have EXPORT defined.
  8659. Org mode has a built-in default for exporting links. If you are happy with
  8660. this default, there is no need to define an export function for the link
  8661. type. For a simple example of an export function, see `org-bbdb.el'."
  8662. (add-to-list 'org-link-types type t)
  8663. (org-make-link-regexps)
  8664. (org-element-update-syntax)
  8665. (if (assoc type org-link-protocols)
  8666. (setcdr (assoc type org-link-protocols) (list follow export))
  8667. (push (list type follow export) org-link-protocols)))
  8668. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8669. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8670. ;;;###autoload
  8671. (defun org-store-link (arg)
  8672. "\\<org-mode-map>Store an org-link to the current location.
  8673. This link is added to `org-stored-links' and can later be inserted
  8674. into an org-buffer with \\[org-insert-link].
  8675. For some link types, a prefix arg is interpreted.
  8676. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8677. For file links, arg negates `org-context-in-file-links'.
  8678. A double prefix arg force skipping storing functions that are not
  8679. part of Org's core.
  8680. A triple prefix arg force storing a link for each line in the
  8681. active region."
  8682. (interactive "P")
  8683. (org-load-modules-maybe)
  8684. (if (and (equal arg '(64)) (org-region-active-p))
  8685. (save-excursion
  8686. (let ((end (region-end)))
  8687. (goto-char (region-beginning))
  8688. (set-mark (point))
  8689. (while (< (point-at-eol) end)
  8690. (move-end-of-line 1) (activate-mark)
  8691. (let (current-prefix-arg)
  8692. (call-interactively 'org-store-link))
  8693. (move-beginning-of-line 2)
  8694. (set-mark (point)))))
  8695. (org-with-limited-levels
  8696. (setq org-store-link-plist nil)
  8697. (let (link cpltxt desc description search
  8698. txt custom-id agenda-link sfuns sfunsn)
  8699. (cond
  8700. ;; Store a link using an external link type
  8701. ((and (not (equal arg '(16)))
  8702. (setq sfuns
  8703. (delq
  8704. nil (mapcar (lambda (f)
  8705. (let (fs) (if (funcall f) (push f fs))))
  8706. org-store-link-functions))
  8707. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8708. (or (and (cdr sfuns)
  8709. (funcall (intern
  8710. (completing-read
  8711. "Which function for creating the link? "
  8712. sfunsn nil t (car sfunsn)))))
  8713. (funcall (caar sfuns)))
  8714. (setq link (plist-get org-store-link-plist :link)
  8715. desc (or (plist-get org-store-link-plist
  8716. :description) link))))
  8717. ;; Store a link from a source code buffer
  8718. ((org-src-edit-buffer-p)
  8719. (let (label gc)
  8720. (while (or (not label)
  8721. (save-excursion
  8722. (save-restriction
  8723. (widen)
  8724. (goto-char (point-min))
  8725. (re-search-forward
  8726. (regexp-quote (format org-coderef-label-format label))
  8727. nil t))))
  8728. (when label (message "Label exists already") (sit-for 2))
  8729. (setq label (read-string "Code line label: " label)))
  8730. (end-of-line 1)
  8731. (setq link (format org-coderef-label-format label))
  8732. (setq gc (- 79 (length link)))
  8733. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8734. (insert link)
  8735. (setq link (concat "(" label ")") desc nil)))
  8736. ;; We are in the agenda, link to referenced location
  8737. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8738. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8739. (get-text-property (point) 'org-marker))))
  8740. (when m
  8741. (org-with-point-at m
  8742. (setq agenda-link
  8743. (if (org-called-interactively-p 'any)
  8744. (call-interactively 'org-store-link)
  8745. (org-store-link nil)))))))
  8746. ((eq major-mode 'calendar-mode)
  8747. (let ((cd (calendar-cursor-to-date)))
  8748. (setq link
  8749. (format-time-string
  8750. (car org-time-stamp-formats)
  8751. (apply 'encode-time
  8752. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8753. nil nil nil))))
  8754. (org-store-link-props :type "calendar" :date cd)))
  8755. ((eq major-mode 'help-mode)
  8756. (setq link (concat "help:" (save-excursion
  8757. (goto-char (point-min))
  8758. (looking-at "^[^ ]+")
  8759. (match-string 0))))
  8760. (org-store-link-props :type "help"))
  8761. ((eq major-mode 'w3-mode)
  8762. (setq cpltxt (if (and (buffer-name)
  8763. (not (string-match "Untitled" (buffer-name))))
  8764. (buffer-name)
  8765. (url-view-url t))
  8766. link (url-view-url t))
  8767. (org-store-link-props :type "w3" :url (url-view-url t)))
  8768. ((eq major-mode 'image-mode)
  8769. (setq cpltxt (concat "file:"
  8770. (abbreviate-file-name buffer-file-name))
  8771. link cpltxt)
  8772. (org-store-link-props :type "image" :file buffer-file-name))
  8773. ;; In dired, store a link to the file of the current line
  8774. ((derived-mode-p 'dired-mode)
  8775. (let ((file (dired-get-filename nil t)))
  8776. (setq file (if file
  8777. (abbreviate-file-name
  8778. (expand-file-name (dired-get-filename nil t)))
  8779. ;; otherwise, no file so use current directory.
  8780. default-directory))
  8781. (setq cpltxt (concat "file:" file)
  8782. link cpltxt)))
  8783. ((setq search (run-hook-with-args-until-success
  8784. 'org-create-file-search-functions))
  8785. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8786. "::" search))
  8787. (setq cpltxt (or description link)))
  8788. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8789. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8790. (cond
  8791. ;; Store a link using the target at point
  8792. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8793. (setq cpltxt
  8794. (concat "file:"
  8795. (abbreviate-file-name
  8796. (buffer-file-name (buffer-base-buffer)))
  8797. "::" (match-string 1))
  8798. link cpltxt))
  8799. ((and (featurep 'org-id)
  8800. (or (eq org-id-link-to-org-use-id t)
  8801. (and (org-called-interactively-p 'any)
  8802. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8803. (and (eq org-id-link-to-org-use-id
  8804. 'create-if-interactive-and-no-custom-id)
  8805. (not custom-id))))
  8806. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8807. ;; Store a link using the ID at point
  8808. (setq link (condition-case nil
  8809. (prog1 (org-id-store-link)
  8810. (setq desc (or (plist-get org-store-link-plist
  8811. :description)
  8812. "")))
  8813. (error
  8814. ;; Probably before first headline, link only to file
  8815. (concat "file:"
  8816. (abbreviate-file-name
  8817. (buffer-file-name (buffer-base-buffer))))))))
  8818. (t
  8819. ;; Just link to current headline
  8820. (setq cpltxt (concat "file:"
  8821. (abbreviate-file-name
  8822. (buffer-file-name (buffer-base-buffer)))))
  8823. ;; Add a context search string
  8824. (when (org-xor org-context-in-file-links arg)
  8825. (let* ((ee (org-element-at-point))
  8826. (et (org-element-type ee))
  8827. (ev (plist-get (cadr ee) :value))
  8828. (ek (plist-get (cadr ee) :key))
  8829. (eok (and (stringp ek) (string-match "name" ek))))
  8830. (setq txt (cond
  8831. ((org-at-heading-p) nil)
  8832. ((and (eq et 'keyword) eok) ev)
  8833. ((org-region-active-p)
  8834. (buffer-substring (region-beginning) (region-end)))))
  8835. (when (or (null txt) (string-match "\\S-" txt))
  8836. (setq cpltxt
  8837. (concat cpltxt "::"
  8838. (condition-case nil
  8839. (org-make-org-heading-search-string txt)
  8840. (error "")))
  8841. desc (or (and (eq et 'keyword) eok ev)
  8842. (nth 4 (ignore-errors (org-heading-components)))
  8843. "NONE")))))
  8844. (if (string-match "::\\'" cpltxt)
  8845. (setq cpltxt (substring cpltxt 0 -2)))
  8846. (setq link cpltxt))))
  8847. ((buffer-file-name (buffer-base-buffer))
  8848. ;; Just link to this file here.
  8849. (setq cpltxt (concat "file:"
  8850. (abbreviate-file-name
  8851. (buffer-file-name (buffer-base-buffer)))))
  8852. ;; Add a context string.
  8853. (when (org-xor org-context-in-file-links arg)
  8854. (setq txt (if (org-region-active-p)
  8855. (buffer-substring (region-beginning) (region-end))
  8856. (buffer-substring (point-at-bol) (point-at-eol))))
  8857. ;; Only use search option if there is some text.
  8858. (when (string-match "\\S-" txt)
  8859. (setq cpltxt
  8860. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8861. desc "NONE")))
  8862. (setq link cpltxt))
  8863. ((org-called-interactively-p 'interactive)
  8864. (user-error "No method for storing a link from this buffer"))
  8865. (t (setq link nil)))
  8866. ;; We're done setting link and desc, clean up
  8867. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8868. (setq link (or link cpltxt)
  8869. desc (or desc cpltxt))
  8870. (cond ((equal desc "NONE") (setq desc nil))
  8871. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8872. (let ((d0 (match-string 3 desc))
  8873. (p0 (match-string 5 desc)))
  8874. (setq desc
  8875. (replace-regexp-in-string
  8876. org-bracket-link-regexp
  8877. (concat (or p0 d0)
  8878. (if (equal (length (match-string 0 desc))
  8879. (length desc)) "*" "")) desc)))))
  8880. ;; Return the link
  8881. (if (not (and (or (org-called-interactively-p 'any)
  8882. executing-kbd-macro) link))
  8883. (or agenda-link (and link (org-make-link-string link desc)))
  8884. (push (list link desc) org-stored-links)
  8885. (message "Stored: %s" (or desc link))
  8886. (when custom-id
  8887. (setq link (concat "file:" (abbreviate-file-name
  8888. (buffer-file-name)) "::#" custom-id))
  8889. (push (list link desc) org-stored-links))
  8890. (car org-stored-links))))))
  8891. (defun org-store-link-props (&rest plist)
  8892. "Store link properties, extract names and addresses."
  8893. (let (x adr)
  8894. (when (setq x (plist-get plist :from))
  8895. (setq adr (mail-extract-address-components x))
  8896. (setq plist (plist-put plist :fromname (car adr)))
  8897. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8898. (when (setq x (plist-get plist :to))
  8899. (setq adr (mail-extract-address-components x))
  8900. (setq plist (plist-put plist :toname (car adr)))
  8901. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8902. (let ((from (plist-get plist :from))
  8903. (to (plist-get plist :to)))
  8904. (when (and from to org-from-is-user-regexp)
  8905. (setq plist
  8906. (plist-put plist :fromto
  8907. (if (string-match org-from-is-user-regexp from)
  8908. (concat "to %t")
  8909. (concat "from %f"))))))
  8910. (setq org-store-link-plist plist))
  8911. (defun org-add-link-props (&rest plist)
  8912. "Add these properties to the link property list."
  8913. (let (key value)
  8914. (while plist
  8915. (setq key (pop plist) value (pop plist))
  8916. (setq org-store-link-plist
  8917. (plist-put org-store-link-plist key value)))))
  8918. (defun org-email-link-description (&optional fmt)
  8919. "Return the description part of an email link.
  8920. This takes information from `org-store-link-plist' and formats it
  8921. according to FMT (default from `org-email-link-description-format')."
  8922. (setq fmt (or fmt org-email-link-description-format))
  8923. (let* ((p org-store-link-plist)
  8924. (to (plist-get p :toaddress))
  8925. (from (plist-get p :fromaddress))
  8926. (table
  8927. (list
  8928. (cons "%c" (plist-get p :fromto))
  8929. (cons "%F" (plist-get p :from))
  8930. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8931. (cons "%T" (plist-get p :to))
  8932. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8933. (cons "%s" (plist-get p :subject))
  8934. (cons "%d" (plist-get p :date))
  8935. (cons "%m" (plist-get p :message-id)))))
  8936. (when (string-match "%c" fmt)
  8937. ;; Check if the user wrote this message
  8938. (if (and org-from-is-user-regexp from to
  8939. (save-match-data (string-match org-from-is-user-regexp from)))
  8940. (setq fmt (replace-match "to %t" t t fmt))
  8941. (setq fmt (replace-match "from %f" t t fmt))))
  8942. (org-replace-escapes fmt table)))
  8943. (defun org-make-org-heading-search-string (&optional string)
  8944. "Make search string for the current headline or STRING."
  8945. (let ((s (or string
  8946. (and (derived-mode-p 'org-mode)
  8947. (save-excursion
  8948. (org-back-to-heading t)
  8949. (org-element-property :raw-value (org-element-at-point))))))
  8950. (lines org-context-in-file-links))
  8951. (or string (setq s (concat "*" s))) ; Add * for headlines
  8952. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8953. (when (and string (integerp lines) (> lines 0))
  8954. (let ((slines (org-split-string s "\n")))
  8955. (when (< lines (length slines))
  8956. (setq s (mapconcat
  8957. 'identity
  8958. (reverse (nthcdr (- (length slines) lines)
  8959. (reverse slines))) "\n")))))
  8960. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8961. (defun org-make-link-string (link &optional description)
  8962. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8963. (unless (string-match "\\S-" link)
  8964. (error "Empty link"))
  8965. (when (and description
  8966. (stringp description)
  8967. (not (string-match "\\S-" description)))
  8968. (setq description nil))
  8969. (when (stringp description)
  8970. ;; Remove brackets from the description, they are fatal.
  8971. (while (string-match "\\[" description)
  8972. (setq description (replace-match "{" t t description)))
  8973. (while (string-match "\\]" description)
  8974. (setq description (replace-match "}" t t description))))
  8975. (when (equal link description)
  8976. ;; No description needed, it is identical
  8977. (setq description nil))
  8978. (when (and (not description)
  8979. (not (string-match (org-image-file-name-regexp) link))
  8980. (not (equal link (org-link-escape link))))
  8981. (setq description (org-extract-attributes link)))
  8982. (setq link
  8983. (cond ((string-match (org-image-file-name-regexp) link) link)
  8984. ((string-match org-link-types-re link)
  8985. (concat (match-string 1 link)
  8986. (org-link-escape (substring link (match-end 1)))))
  8987. (t (org-link-escape link))))
  8988. (concat "[[" link "]"
  8989. (if description (concat "[" description "]") "")
  8990. "]"))
  8991. (defconst org-link-escape-chars
  8992. ;;%20 %5B %5D
  8993. '(?\ ?\[ ?\])
  8994. "List of characters that should be escaped in a link when stored to Org.
  8995. This is the list that is used for internal purposes.")
  8996. (defconst org-link-escape-chars-browser
  8997. ;;%20 %22
  8998. '(?\ ?\")
  8999. "List of characters to be escaped before handing over to the browser.
  9000. If you consider using this constant then you probably want to use
  9001. the function `org-link-escape-browser' instead. See there why
  9002. this constant is a candidate to be removed once Org drops support
  9003. for Emacs 24.1 and 24.2.")
  9004. (defun org-link-escape (text &optional table merge)
  9005. "Return percent escaped representation of TEXT.
  9006. TEXT is a string with the text to escape.
  9007. Optional argument TABLE is a list with characters that should be
  9008. escaped. When nil, `org-link-escape-chars' is used.
  9009. If optional argument MERGE is set, merge TABLE into
  9010. `org-link-escape-chars'."
  9011. ;; Don't escape chars in internal links
  9012. (if (string-match "^\\*[[:alnum:]]+" text)
  9013. text
  9014. (cond
  9015. ((and table merge)
  9016. (mapc (lambda (defchr)
  9017. (unless (member defchr table)
  9018. (setq table (cons defchr table))))
  9019. org-link-escape-chars))
  9020. ((null table)
  9021. (setq table org-link-escape-chars)))
  9022. (mapconcat
  9023. (lambda (char)
  9024. (if (or (member char table)
  9025. (and (or (< char 32) (= char ?\%) (> char 126))
  9026. org-url-hexify-p))
  9027. (mapconcat (lambda (sequence-element)
  9028. (format "%%%.2X" sequence-element))
  9029. (or (encode-coding-char char 'utf-8)
  9030. (error "Unable to percent escape character: %s"
  9031. (char-to-string char))) "")
  9032. (char-to-string char))) text "")))
  9033. (defun org-link-escape-browser (text)
  9034. "Escape some characters before handing over to the browser.
  9035. This function is a candidate to be removed together with the
  9036. constant `org-link-escape-chars-browser' once Org drops support
  9037. for Emacs 24.1 and 24.2. All calls to this function will have to
  9038. be replaced with `url-encode-url' which is available since Emacs
  9039. 24.3.1."
  9040. ;; Example with the Org link
  9041. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9042. ;; to open the browser with +subject:"Release 8.2" filled into the
  9043. ;; query field: In this case the variable TEXT contains the
  9044. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9045. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9046. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9047. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9048. (if (fboundp 'url-encode-url)
  9049. (url-encode-url text)
  9050. (if (org-string-match-p
  9051. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9052. text)
  9053. (org-link-escape text org-link-escape-chars-browser)
  9054. text)))
  9055. (defun org-link-unescape (str)
  9056. "Unhex hexified Unicode strings as returned from the JavaScript function
  9057. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  9058. (unless (and (null str) (string= "" str))
  9059. (let ((pos 0) (case-fold-search t) unhexed)
  9060. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  9061. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  9062. (setq str (replace-match unhexed t t str))
  9063. (setq pos (+ pos (length unhexed))))))
  9064. str)
  9065. (defun org-link-unescape-compound (hex)
  9066. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9067. Note: this function also decodes single byte encodings like
  9068. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9069. (save-match-data
  9070. (let* ((bytes (cdr (split-string hex "%")))
  9071. (ret "")
  9072. (eat 0)
  9073. (sum 0))
  9074. (while bytes
  9075. (let* ((val (string-to-number (pop bytes) 16))
  9076. (shift-xor
  9077. (if (= 0 eat)
  9078. (cond
  9079. ((>= val 252) (cons 6 252))
  9080. ((>= val 248) (cons 5 248))
  9081. ((>= val 240) (cons 4 240))
  9082. ((>= val 224) (cons 3 224))
  9083. ((>= val 192) (cons 2 192))
  9084. (t (cons 0 0)))
  9085. (cons 6 128))))
  9086. (if (>= val 192) (setq eat (car shift-xor)))
  9087. (setq val (logxor val (cdr shift-xor)))
  9088. (setq sum (+ (lsh sum (car shift-xor)) val))
  9089. (if (> eat 0) (setq eat (- eat 1)))
  9090. (cond
  9091. ((= 0 eat) ;multi byte
  9092. (setq ret (concat ret (org-char-to-string sum)))
  9093. (setq sum 0))
  9094. ((not bytes) ; single byte(s)
  9095. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9096. ret)))
  9097. (defun org-link-unescape-single-byte-sequence (hex)
  9098. "Unhexify hex-encoded single byte character sequences."
  9099. (mapconcat (lambda (byte)
  9100. (char-to-string (string-to-number byte 16)))
  9101. (cdr (split-string hex "%")) ""))
  9102. (defun org-xor (a b)
  9103. "Exclusive or."
  9104. (if a (not b) b))
  9105. (defun org-fixup-message-id-for-http (s)
  9106. "Replace special characters in a message id, so it can be used in an http query."
  9107. (when (string-match "%" s)
  9108. (setq s (mapconcat (lambda (c)
  9109. (if (eq c ?%)
  9110. "%25"
  9111. (char-to-string c)))
  9112. s "")))
  9113. (while (string-match "<" s)
  9114. (setq s (replace-match "%3C" t t s)))
  9115. (while (string-match ">" s)
  9116. (setq s (replace-match "%3E" t t s)))
  9117. (while (string-match "@" s)
  9118. (setq s (replace-match "%40" t t s)))
  9119. s)
  9120. (defun org-link-prettify (link)
  9121. "Return a human-readable representation of LINK.
  9122. The car of LINK must be a raw link.
  9123. The cdr of LINK must be either a link description or nil."
  9124. (let ((desc (or (cadr link) "<no description>")))
  9125. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9126. "<" (car link) ">")))
  9127. ;;;###autoload
  9128. (defun org-insert-link-global ()
  9129. "Insert a link like Org-mode does.
  9130. This command can be called in any mode to insert a link in Org-mode syntax."
  9131. (interactive)
  9132. (org-load-modules-maybe)
  9133. (org-run-like-in-org-mode 'org-insert-link))
  9134. (defun org-insert-all-links (arg &optional pre post)
  9135. "Insert all links in `org-stored-links'.
  9136. When a universal prefix, do not delete the links from `org-stored-links'.
  9137. When `ARG' is a number, insert the last N link(s).
  9138. `PRE' and `POST' are optional arguments to define a string to
  9139. prepend or to append."
  9140. (interactive "P")
  9141. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9142. (links (copy-seq org-stored-links))
  9143. (pr (or pre "- "))
  9144. (po (or post "\n"))
  9145. (cnt 1) l)
  9146. (if (null org-stored-links)
  9147. (message "No link to insert")
  9148. (while (and (or (listp arg) (>= arg cnt))
  9149. (setq l (if (listp arg)
  9150. (pop links)
  9151. (pop org-stored-links))))
  9152. (setq cnt (1+ cnt))
  9153. (insert pr)
  9154. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9155. (insert po)))))
  9156. (defun org-insert-last-stored-link (arg)
  9157. "Insert the last link stored in `org-stored-links'."
  9158. (interactive "p")
  9159. (org-insert-all-links arg "" "\n"))
  9160. (defun org-link-fontify-links-to-this-file ()
  9161. "Fontify links to the current file in `org-stored-links'."
  9162. (let ((f (buffer-file-name)) a b)
  9163. (setq a (mapcar (lambda(l)
  9164. (let ((ll (car l)))
  9165. (when (and (string-match "^file:\\(.+\\)::" ll)
  9166. (equal f (expand-file-name (match-string 1 ll))))
  9167. ll)))
  9168. org-stored-links))
  9169. (when (featurep 'org-id)
  9170. (setq b (mapcar (lambda(l)
  9171. (let ((ll (car l)))
  9172. (when (and (string-match "^id:\\(.+\\)$" ll)
  9173. (equal f (expand-file-name
  9174. (or (org-id-find-id-file
  9175. (match-string 1 ll)) ""))))
  9176. ll)))
  9177. org-stored-links)))
  9178. (mapcar (lambda(l)
  9179. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9180. (delq nil (append a b)))))
  9181. (defvar org-link-links-in-this-file nil)
  9182. (defun org-insert-link (&optional complete-file link-location default-description)
  9183. "Insert a link. At the prompt, enter the link.
  9184. Completion can be used to insert any of the link protocol prefixes like
  9185. http or ftp in use.
  9186. The history can be used to select a link previously stored with
  9187. `org-store-link'. When the empty string is entered (i.e. if you just
  9188. press RET at the prompt), the link defaults to the most recently
  9189. stored link. As SPC triggers completion in the minibuffer, you need to
  9190. use M-SPC or C-q SPC to force the insertion of a space character.
  9191. You will also be prompted for a description, and if one is given, it will
  9192. be displayed in the buffer instead of the link.
  9193. If there is already a link at point, this command will allow you to edit link
  9194. and description parts.
  9195. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9196. be selected using completion. The path to the file will be relative to the
  9197. current directory if the file is in the current directory or a subdirectory.
  9198. Otherwise, the link will be the absolute path as completed in the minibuffer
  9199. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9200. option `org-link-file-path-type'.
  9201. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9202. the current directory or below.
  9203. With three \\[universal-argument] prefixes, negate the meaning of
  9204. `org-keep-stored-link-after-insertion'.
  9205. If `org-make-link-description-function' is non-nil, this function will be
  9206. called with the link target, and the result will be the default
  9207. link description.
  9208. If the LINK-LOCATION parameter is non-nil, this value will be
  9209. used as the link location instead of reading one interactively.
  9210. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9211. be used as the default description."
  9212. (interactive "P")
  9213. (let* ((wcf (current-window-configuration))
  9214. (origbuf (current-buffer))
  9215. (region (if (org-region-active-p)
  9216. (buffer-substring (region-beginning) (region-end))))
  9217. (remove (and region (list (region-beginning) (region-end))))
  9218. (desc region)
  9219. tmphist ; byte-compile incorrectly complains about this
  9220. (link link-location)
  9221. (abbrevs org-link-abbrev-alist-local)
  9222. entry file all-prefixes auto-desc)
  9223. (cond
  9224. (link-location) ; specified by arg, just use it.
  9225. ((org-in-regexp org-bracket-link-regexp 1)
  9226. ;; We do have a link at point, and we are going to edit it.
  9227. (setq remove (list (match-beginning 0) (match-end 0)))
  9228. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9229. (setq link (read-string "Link: "
  9230. (org-link-unescape
  9231. (org-match-string-no-properties 1)))))
  9232. ((or (org-in-regexp org-angle-link-re)
  9233. (org-in-regexp org-plain-link-re))
  9234. ;; Convert to bracket link
  9235. (setq remove (list (match-beginning 0) (match-end 0))
  9236. link (read-string "Link: "
  9237. (org-remove-angle-brackets (match-string 0)))))
  9238. ((member complete-file '((4) (16)))
  9239. ;; Completing read for file names.
  9240. (setq link (org-file-complete-link complete-file)))
  9241. (t
  9242. ;; Read link, with completion for stored links.
  9243. (org-link-fontify-links-to-this-file)
  9244. (org-switch-to-buffer-other-window "*Org Links*")
  9245. (with-current-buffer "*Org Links*"
  9246. (erase-buffer)
  9247. (insert "Insert a link.
  9248. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9249. (when org-stored-links
  9250. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9251. (insert (mapconcat 'org-link-prettify
  9252. (reverse org-stored-links) "\n")))
  9253. (goto-char (point-min)))
  9254. (let ((cw (selected-window)))
  9255. (select-window (get-buffer-window "*Org Links*" 'visible))
  9256. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9257. (unless (pos-visible-in-window-p (point-max))
  9258. (org-fit-window-to-buffer))
  9259. (and (window-live-p cw) (select-window cw)))
  9260. ;; Fake a link history, containing the stored links.
  9261. (setq tmphist (append (mapcar 'car org-stored-links)
  9262. org-insert-link-history))
  9263. (setq all-prefixes (append (mapcar 'car abbrevs)
  9264. (mapcar 'car org-link-abbrev-alist)
  9265. org-link-types))
  9266. (unwind-protect
  9267. (progn
  9268. (setq link
  9269. (org-completing-read
  9270. "Link: "
  9271. (append
  9272. (mapcar (lambda (x) (concat x ":"))
  9273. all-prefixes)
  9274. (mapcar 'car org-stored-links))
  9275. nil nil nil
  9276. 'tmphist
  9277. (caar org-stored-links)))
  9278. (if (not (string-match "\\S-" link))
  9279. (user-error "No link selected"))
  9280. (mapc (lambda(l)
  9281. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9282. org-stored-links)
  9283. (if (or (member link all-prefixes)
  9284. (and (equal ":" (substring link -1))
  9285. (member (substring link 0 -1) all-prefixes)
  9286. (setq link (substring link 0 -1))))
  9287. (setq link (with-current-buffer origbuf
  9288. (org-link-try-special-completion link)))))
  9289. (set-window-configuration wcf)
  9290. (kill-buffer "*Org Links*"))
  9291. (setq entry (assoc link org-stored-links))
  9292. (or entry (push link org-insert-link-history))
  9293. (setq desc (or desc (nth 1 entry)))))
  9294. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9295. (not org-keep-stored-link-after-insertion))
  9296. (setq org-stored-links (delq (assoc link org-stored-links)
  9297. org-stored-links)))
  9298. (if (and (string-match org-plain-link-re link)
  9299. (not (string-match org-ts-regexp link)))
  9300. ;; URL-like link, normalize the use of angular brackets.
  9301. (setq link (org-remove-angle-brackets link)))
  9302. ;; Check if we are linking to the current file with a search
  9303. ;; option If yes, simplify the link by using only the search
  9304. ;; option.
  9305. (when (and buffer-file-name
  9306. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9307. (let* ((path (match-string 1 link))
  9308. (case-fold-search nil)
  9309. (search (match-string 2 link)))
  9310. (save-match-data
  9311. (if (equal (file-truename buffer-file-name) (file-truename path))
  9312. ;; We are linking to this same file, with a search option
  9313. (setq link search)))))
  9314. ;; Check if we can/should use a relative path. If yes, simplify the link
  9315. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9316. (let* ((type (match-string 1 link))
  9317. (path (match-string 2 link))
  9318. (origpath path)
  9319. (case-fold-search nil))
  9320. (cond
  9321. ((or (eq org-link-file-path-type 'absolute)
  9322. (equal complete-file '(16)))
  9323. (setq path (abbreviate-file-name (expand-file-name path))))
  9324. ((eq org-link-file-path-type 'noabbrev)
  9325. (setq path (expand-file-name path)))
  9326. ((eq org-link-file-path-type 'relative)
  9327. (setq path (file-relative-name path)))
  9328. (t
  9329. (save-match-data
  9330. (if (string-match (concat "^" (regexp-quote
  9331. (expand-file-name
  9332. (file-name-as-directory
  9333. default-directory))))
  9334. (expand-file-name path))
  9335. ;; We are linking a file with relative path name.
  9336. (setq path (substring (expand-file-name path)
  9337. (match-end 0)))
  9338. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9339. (setq link (concat type path))
  9340. (if (equal desc origpath)
  9341. (setq desc path))))
  9342. (if org-make-link-description-function
  9343. (setq desc
  9344. (or (condition-case nil
  9345. (funcall org-make-link-description-function link desc)
  9346. (error (progn (message "Can't get link description from `%s'"
  9347. (symbol-name org-make-link-description-function))
  9348. (sit-for 2) nil)))
  9349. (read-string "Description: " default-description)))
  9350. (if default-description (setq desc default-description)
  9351. (setq desc (or (and auto-desc desc)
  9352. (read-string "Description: " desc)))))
  9353. (unless (string-match "\\S-" desc) (setq desc nil))
  9354. (if remove (apply 'delete-region remove))
  9355. (insert (org-make-link-string link desc))))
  9356. (defun org-link-try-special-completion (type)
  9357. "If there is completion support for link type TYPE, offer it."
  9358. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9359. (if (functionp fun)
  9360. (funcall fun)
  9361. (read-string "Link (no completion support): " (concat type ":")))))
  9362. (defun org-file-complete-link (&optional arg)
  9363. "Create a file link using completion."
  9364. (let (file link)
  9365. (setq file (org-iread-file-name "File: "))
  9366. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9367. (pwd1 (file-name-as-directory (abbreviate-file-name
  9368. (expand-file-name ".")))))
  9369. (cond
  9370. ((equal arg '(16))
  9371. (setq link (concat
  9372. "file:"
  9373. (abbreviate-file-name (expand-file-name file)))))
  9374. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9375. (setq link (concat "file:" (match-string 1 file))))
  9376. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9377. (expand-file-name file))
  9378. (setq link (concat
  9379. "file:" (match-string 1 (expand-file-name file)))))
  9380. (t (setq link (concat "file:" file)))))
  9381. link))
  9382. (defun org-iread-file-name (&rest args)
  9383. "Read-file-name using `ido-mode' speedup if available.
  9384. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9385. See `read-file-name' for a description of parameters."
  9386. (org-without-partial-completion
  9387. (if (and org-completion-use-ido
  9388. (fboundp 'ido-read-file-name)
  9389. (boundp 'ido-mode) ido-mode
  9390. (listp (second args)))
  9391. (let ((ido-enter-matching-directory nil))
  9392. (apply 'ido-read-file-name args))
  9393. (apply 'read-file-name args))))
  9394. (defun org-completing-read (&rest args)
  9395. "Completing-read with SPACE being a normal character."
  9396. (let ((enable-recursive-minibuffers t)
  9397. (minibuffer-local-completion-map
  9398. (copy-keymap minibuffer-local-completion-map)))
  9399. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9400. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9401. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9402. (apply 'org-icompleting-read args)))
  9403. (defun org-completing-read-no-i (&rest args)
  9404. (let (org-completion-use-ido org-completion-use-iswitchb)
  9405. (apply 'org-completing-read args)))
  9406. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9407. "Use iswitch as a completing-read replacement to choose from choices.
  9408. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9409. from."
  9410. (let* ((iswitchb-use-virtual-buffers nil)
  9411. (iswitchb-make-buflist-hook
  9412. (lambda ()
  9413. (setq iswitchb-temp-buflist choices))))
  9414. (iswitchb-read-buffer prompt)))
  9415. (defun org-icompleting-read (&rest args)
  9416. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9417. (org-without-partial-completion
  9418. (if (and org-completion-use-ido
  9419. (fboundp 'ido-completing-read)
  9420. (boundp 'ido-mode) ido-mode
  9421. (listp (second args)))
  9422. (let ((ido-enter-matching-directory nil))
  9423. (apply 'ido-completing-read (concat (car args))
  9424. (if (consp (car (nth 1 args)))
  9425. (mapcar 'car (nth 1 args))
  9426. (nth 1 args))
  9427. (cddr args)))
  9428. (if (and org-completion-use-iswitchb
  9429. (boundp 'iswitchb-mode) iswitchb-mode
  9430. (listp (second args)))
  9431. (apply 'org-iswitchb-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. (apply 'completing-read args)))))
  9437. (defun org-extract-attributes (s)
  9438. "Extract the attributes cookie from a string and set as text property."
  9439. (let (a attr (start 0) key value)
  9440. (save-match-data
  9441. (when (string-match "{{\\([^}]+\\)}}$" s)
  9442. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9443. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9444. (setq key (match-string 1 a) value (match-string 2 a)
  9445. start (match-end 0)
  9446. attr (plist-put attr (intern key) value))))
  9447. (org-add-props s nil 'org-attr attr))
  9448. s))
  9449. ;;; Opening/following a link
  9450. (defvar org-link-search-failed nil)
  9451. (defvar org-open-link-functions nil
  9452. "Hook for functions finding a plain text link.
  9453. These functions must take a single argument, the link content.
  9454. They will be called for links that look like [[link text][description]]
  9455. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9456. like a filename (e.g. \"./blue.png\").
  9457. These functions will be called *before* Org attempts to resolve the
  9458. link by doing text searches in the current buffer - so if you want a
  9459. link \"[[target]]\" to still find \"<<target>>\", your function should
  9460. handle this as a special case.
  9461. When the function does handle the link, it must return a non-nil value.
  9462. If it decides that it is not responsible for this link, it must return
  9463. nil to indicate that that Org-mode can continue with other options
  9464. like exact and fuzzy text search.")
  9465. (defun org-next-link (&optional search-backward)
  9466. "Move forward to the next link.
  9467. If the link is in hidden text, expose it."
  9468. (interactive "P")
  9469. (when (and org-link-search-failed (eq this-command last-command))
  9470. (goto-char (point-min))
  9471. (message "Link search wrapped back to beginning of buffer"))
  9472. (setq org-link-search-failed nil)
  9473. (let* ((pos (point))
  9474. (ct (org-context))
  9475. (a (assoc :link ct))
  9476. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9477. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9478. ((looking-at org-any-link-re)
  9479. ;; Don't stay stuck at link without an org-link face
  9480. (forward-char (if search-backward -1 1))))
  9481. (if (funcall srch-fun org-any-link-re nil t)
  9482. (progn
  9483. (goto-char (match-beginning 0))
  9484. (if (outline-invisible-p) (org-show-context)))
  9485. (goto-char pos)
  9486. (setq org-link-search-failed t)
  9487. (message "No further link found"))))
  9488. (defun org-previous-link ()
  9489. "Move backward to the previous link.
  9490. If the link is in hidden text, expose it."
  9491. (interactive)
  9492. (funcall 'org-next-link t))
  9493. (defun org-translate-link (s)
  9494. "Translate a link string if a translation function has been defined."
  9495. (if (and org-link-translation-function
  9496. (fboundp org-link-translation-function)
  9497. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9498. (progn
  9499. (setq s (funcall org-link-translation-function
  9500. (match-string 1 s) (match-string 2 s)))
  9501. (concat (car s) ":" (cdr s)))
  9502. s))
  9503. (defun org-translate-link-from-planner (type path)
  9504. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9505. This is still an experimental function, your mileage may vary."
  9506. (cond
  9507. ((member type '("http" "https" "news" "ftp"))
  9508. ;; standard Internet links are the same.
  9509. nil)
  9510. ((and (equal type "irc") (string-match "^//" path))
  9511. ;; Planner has two / at the beginning of an irc link, we have 1.
  9512. ;; We should have zero, actually....
  9513. (setq path (substring path 1)))
  9514. ((and (equal type "lisp") (string-match "^/" path))
  9515. ;; Planner has a slash, we do not.
  9516. (setq type "elisp" path (substring path 1)))
  9517. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9518. ;; A typical message link. Planner has the id after the final slash,
  9519. ;; we separate it with a hash mark
  9520. (setq path (concat (match-string 1 path) "#"
  9521. (org-remove-angle-brackets (match-string 2 path))))))
  9522. (cons type path))
  9523. (defun org-find-file-at-mouse (ev)
  9524. "Open file link or URL at mouse."
  9525. (interactive "e")
  9526. (mouse-set-point ev)
  9527. (org-open-at-point 'in-emacs))
  9528. (defun org-open-at-mouse (ev)
  9529. "Open file link or URL at mouse.
  9530. See the docstring of `org-open-file' for details."
  9531. (interactive "e")
  9532. (mouse-set-point ev)
  9533. (if (eq major-mode 'org-agenda-mode)
  9534. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9535. (org-open-at-point))
  9536. (defvar org-window-config-before-follow-link nil
  9537. "The window configuration before following a link.
  9538. This is saved in case the need arises to restore it.")
  9539. (defvar org-open-link-marker (make-marker)
  9540. "Marker pointing to the location where `org-open-at-point' was called.")
  9541. ;;;###autoload
  9542. (defun org-open-at-point-global ()
  9543. "Follow a link like Org-mode does.
  9544. This command can be called in any mode to follow a link that has
  9545. Org-mode syntax."
  9546. (interactive)
  9547. (org-run-like-in-org-mode 'org-open-at-point))
  9548. ;;;###autoload
  9549. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9550. "Open a link in the string S, as if it was in Org-mode."
  9551. (interactive "sLink: \nP")
  9552. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9553. (with-temp-buffer
  9554. (let ((org-inhibit-startup (not reference-buffer)))
  9555. (org-mode)
  9556. (insert s)
  9557. (goto-char (point-min))
  9558. (when reference-buffer
  9559. (setq org-link-abbrev-alist-local
  9560. (with-current-buffer reference-buffer
  9561. org-link-abbrev-alist-local)))
  9562. (org-open-at-point arg reference-buffer)))))
  9563. (defvar org-open-at-point-functions nil
  9564. "Hook that is run when following a link at point.
  9565. Functions in this hook must return t if they identify and follow
  9566. a link at point. If they don't find anything interesting at point,
  9567. they must return nil.")
  9568. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9569. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9570. (defun org-open-at-point (&optional arg reference-buffer)
  9571. "Open link, timestamp, footnote or tags at point.
  9572. When point is on a link, follow it. Normally, files will be
  9573. opened by an appropriate application. If the optional prefix
  9574. argument ARG is non-nil, Emacs will visit the file. With
  9575. a double prefix argument, try to open outside of Emacs, in the
  9576. application the system uses for this file type.
  9577. When point is on a timestamp, open the agenda at the day
  9578. specified.
  9579. When point is a footnote definition, move to the first reference
  9580. found. If it is on a reference, move to the associated
  9581. definition.
  9582. When point is on a headline, display a list of every link in the
  9583. entry, so it is possible to pick one, or all, of them. If point
  9584. is on a tag, call `org-tags-view' instead.
  9585. When optional argument REFERENCE-BUFFER is non-nil, it should
  9586. specify a buffer from where the link search should happen. This
  9587. is used internally by `org-open-link-from-string'.
  9588. On top of syntactically correct links, this function will open
  9589. the link at point in comments or comment blocks and the first
  9590. link in a property drawer line."
  9591. (interactive "P")
  9592. ;; On a code block, open block's results.
  9593. (unless (call-interactively 'org-babel-open-src-block-result)
  9594. (org-load-modules-maybe)
  9595. (move-marker org-open-link-marker (point))
  9596. (setq org-window-config-before-follow-link (current-window-configuration))
  9597. (org-remove-occur-highlights nil nil t)
  9598. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9599. (let* ((context (org-element-context)) type value)
  9600. ;; On an unsupported type, check if point is contained within
  9601. ;; a support one.
  9602. (while (and (not (memq (setq type (org-element-type context))
  9603. '(comment comment-block
  9604. headline inlinetask link
  9605. footnote-definition footnote-reference
  9606. node-property timestamp)))
  9607. (setq context (org-element-property :parent context))))
  9608. (setq value (org-element-property :value context))
  9609. (cond
  9610. ;; Blank lines at the beginning of buffer: bail out.
  9611. ((not context) (user-error "No link found"))
  9612. ;; Exception n°1: links in property drawers
  9613. ((eq type 'node-property)
  9614. (org-open-link-from-string
  9615. (and (string-match org-any-link-re value)
  9616. (match-string-no-properties 0 value))))
  9617. ;; Exception n°2: links in comments.
  9618. ((memq type '(comment comment-block))
  9619. (save-excursion
  9620. (skip-chars-forward "\\S-" (point-at-eol))
  9621. (let ((string-rear (replace-regexp-in-string
  9622. "^[ \t]*# [ \t]*" ""
  9623. (buffer-substring (point) (line-beginning-position))))
  9624. (string-front (buffer-substring (point) (line-end-position))))
  9625. (with-temp-buffer
  9626. (let ((org-inhibit-startup t)) (org-mode))
  9627. (insert value)
  9628. (goto-char (point-min))
  9629. (when (and (search-forward string-rear nil t)
  9630. (search-forward string-front (line-end-position) t))
  9631. (goto-char (match-beginning 0))
  9632. (org-open-at-point)
  9633. (when (string= string-rear "") (forward-char)))))))
  9634. ;; On a headline or an inlinetask, but not on a timestamp,
  9635. ;; a link, a footnote reference or on tags.
  9636. ((and (memq type '(headline inlinetask))
  9637. ;; Not on tags.
  9638. (progn (save-excursion (beginning-of-line)
  9639. (looking-at org-complex-heading-regexp))
  9640. (or (not (match-beginning 5))
  9641. (< (point) (match-beginning 5)))))
  9642. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9643. (links (car data))
  9644. (links-end (cdr data)))
  9645. (if links
  9646. (dolist (link (if (stringp links) (list links) links))
  9647. (search-forward link nil links-end)
  9648. (goto-char (match-beginning 0))
  9649. (org-open-at-point))
  9650. (require 'org-attach)
  9651. (org-attach-reveal 'if-exists))))
  9652. ;; Do nothing on white spaces after an object, unless point
  9653. ;; is right after it.
  9654. ((> (point)
  9655. (save-excursion
  9656. (goto-char (org-element-property :end context))
  9657. (skip-chars-backward " \t")
  9658. (point)))
  9659. (user-error "No link found"))
  9660. ((eq type 'timestamp) (org-follow-timestamp-link))
  9661. ;; On tags within a headline or an inlinetask.
  9662. ((and (memq type '(headline inlinetask))
  9663. (progn (save-excursion (beginning-of-line)
  9664. (looking-at org-complex-heading-regexp))
  9665. (and (match-beginning 5)
  9666. (>= (point) (match-beginning 5)))))
  9667. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9668. ((eq type 'link)
  9669. ;; When link is located within the description of another
  9670. ;; link (e.g., an inline image), always open the parent
  9671. ;; link.
  9672. (let*((link (let ((up (org-element-property :parent context)))
  9673. (if (eq (org-element-type up) 'link) up context)))
  9674. (type (org-element-property :type link))
  9675. (path (org-link-unescape (org-element-property :path link))))
  9676. ;; Switch back to REFERENCE-BUFFER needed when called in
  9677. ;; a temporary buffer through `org-open-link-from-string'.
  9678. (with-current-buffer (or reference-buffer (current-buffer))
  9679. (cond
  9680. ((equal type "file")
  9681. (if (string-match "[*?{]" (file-name-nondirectory path))
  9682. (dired path)
  9683. ;; Look into `org-link-protocols' in order to find
  9684. ;; a DEDICATED-FUNCTION to open file. The function
  9685. ;; will be applied on raw link instead of parsed
  9686. ;; link due to the limitation in `org-add-link-type'
  9687. ;; ("open" function called with a single argument).
  9688. ;; If no such function is found, fallback to
  9689. ;; `org-open-file'.
  9690. ;;
  9691. ;; Note : "file+emacs" and "file+sys" types are
  9692. ;; hard-coded in order to escape the previous
  9693. ;; limitation.
  9694. (let* ((option (org-element-property :search-option link))
  9695. (app (org-element-property :application link))
  9696. (dedicated-function
  9697. (nth 1 (assoc app org-link-protocols))))
  9698. (if dedicated-function
  9699. (funcall dedicated-function
  9700. (concat path
  9701. (and option (concat "::" option))))
  9702. (apply 'org-open-file
  9703. path
  9704. (cond (arg)
  9705. ((equal app "emacs") 'emacs)
  9706. ((equal app "sys") 'system))
  9707. (cond ((not option) nil)
  9708. ((org-string-match-p "\\`[0-9]+\\'" option)
  9709. (list (string-to-number option)))
  9710. (t (list nil
  9711. (org-link-unescape option)))))))))
  9712. ((assoc type org-link-protocols)
  9713. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9714. ((equal type "help")
  9715. (let ((f-or-v (intern path)))
  9716. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9717. ((boundp f-or-v) (describe-variable f-or-v))
  9718. (t (error "Not a known function or variable")))))
  9719. ((member type '("http" "https" "ftp" "mailto" "news"))
  9720. (browse-url (org-link-escape-browser (concat type ":" path))))
  9721. ((equal type "doi")
  9722. (browse-url
  9723. (org-link-escape-browser (concat org-doi-server-url path))))
  9724. ((equal type "message") (browse-url (concat type ":" path)))
  9725. ((equal type "shell")
  9726. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9727. (cmd path))
  9728. (if (or (and (org-string-nw-p
  9729. org-confirm-shell-link-not-regexp)
  9730. (string-match
  9731. org-confirm-shell-link-not-regexp cmd))
  9732. (not org-confirm-shell-link-function)
  9733. (funcall org-confirm-shell-link-function
  9734. (format "Execute \"%s\" in shell? "
  9735. (org-add-props cmd nil
  9736. 'face 'org-warning))))
  9737. (progn
  9738. (message "Executing %s" cmd)
  9739. (shell-command cmd buf)
  9740. (when (featurep 'midnight)
  9741. (setq clean-buffer-list-kill-buffer-names
  9742. (cons buf
  9743. clean-buffer-list-kill-buffer-names))))
  9744. (user-error "Abort"))))
  9745. ((equal type "elisp")
  9746. (let ((cmd path))
  9747. (if (or (and (org-string-nw-p
  9748. org-confirm-elisp-link-not-regexp)
  9749. (org-string-match-p
  9750. org-confirm-elisp-link-not-regexp cmd))
  9751. (not org-confirm-elisp-link-function)
  9752. (funcall org-confirm-elisp-link-function
  9753. (format "Execute \"%s\" as elisp? "
  9754. (org-add-props cmd nil
  9755. 'face 'org-warning))))
  9756. (message "%s => %s" cmd
  9757. (if (eq (string-to-char cmd) ?\()
  9758. (eval (read cmd))
  9759. (call-interactively (read cmd))))
  9760. (user-error "Abort"))))
  9761. ((equal type "id")
  9762. (require 'ord-id)
  9763. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9764. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9765. (unless (run-hook-with-args-until-success
  9766. 'org-open-link-functions path)
  9767. (if (not arg) (org-mark-ring-push)
  9768. (switch-to-buffer-other-window
  9769. (org-get-buffer-for-internal-link (current-buffer))))
  9770. (let ((cmd `(org-link-search
  9771. ,(if (member type '("custom-id" "coderef"))
  9772. (org-element-property :raw-link link)
  9773. path)
  9774. ,(cond ((equal arg '(4)) 'occur)
  9775. ((equal arg '(16)) 'org-occur))
  9776. ,(org-element-property :begin link))))
  9777. (condition-case nil
  9778. (let ((org-link-search-inhibit-query t))
  9779. (eval cmd))
  9780. (error (progn (widen) (eval cmd)))))))
  9781. (t (browse-url-at-point))))))
  9782. ;; On a footnote reference or at a footnote definition's label.
  9783. ((or (eq type 'footnote-reference)
  9784. (and (eq type 'footnote-definition)
  9785. (save-excursion
  9786. ;; Do not validate action when point is on the
  9787. ;; spaces right after the footnote label, in
  9788. ;; order to be on par with behaviour on links.
  9789. (skip-chars-forward " \t")
  9790. (let ((begin
  9791. (org-element-property :contents-begin context)))
  9792. (if begin (< (point) begin)
  9793. (= (org-element-property :post-affiliated context)
  9794. (line-beginning-position)))))))
  9795. (org-footnote-action))
  9796. (t (user-error "No link found")))))
  9797. (move-marker org-open-link-marker nil)
  9798. (run-hook-with-args 'org-follow-link-hook)))
  9799. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9800. "Offer links in the current entry and return the selected link.
  9801. If there is only one link, return it.
  9802. If NTH is an integer, return the NTH link found.
  9803. If ZERO is a string, check also this string for a link, and if
  9804. there is one, return it."
  9805. (with-current-buffer buffer
  9806. (save-excursion
  9807. (save-restriction
  9808. (widen)
  9809. (goto-char marker)
  9810. (let ((cnt ?0)
  9811. (in-emacs (if (integerp nth) nil nth))
  9812. have-zero end links link c)
  9813. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9814. (push (match-string 0 zero) links)
  9815. (setq cnt (1- cnt) have-zero t))
  9816. (save-excursion
  9817. (org-back-to-heading t)
  9818. (setq end (save-excursion (outline-next-heading) (point)))
  9819. (while (re-search-forward org-any-link-re end t)
  9820. (push (match-string 0) links))
  9821. (setq links (org-uniquify (reverse links))))
  9822. (cond
  9823. ((null links)
  9824. (message "No links"))
  9825. ((equal (length links) 1)
  9826. (setq link (car links)))
  9827. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9828. (setq link (nth (if have-zero nth (1- nth)) links)))
  9829. (t ; we have to select a link
  9830. (save-excursion
  9831. (save-window-excursion
  9832. (delete-other-windows)
  9833. (with-output-to-temp-buffer "*Select Link*"
  9834. (mapc (lambda (l)
  9835. (if (not (string-match org-bracket-link-regexp l))
  9836. (princ (format "[%c] %s\n" (incf cnt)
  9837. (org-remove-angle-brackets l)))
  9838. (if (match-end 3)
  9839. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9840. (match-string 3 l) (match-string 1 l)))
  9841. (princ (format "[%c] %s\n" (incf cnt)
  9842. (match-string 1 l))))))
  9843. links))
  9844. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9845. (message "Select link to open, RET to open all:")
  9846. (setq c (read-char-exclusive))
  9847. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9848. (when (equal c ?q) (user-error "Abort"))
  9849. (if (equal c ?\C-m)
  9850. (setq link links)
  9851. (setq nth (- c ?0))
  9852. (if have-zero (setq nth (1+ nth)))
  9853. (unless (and (integerp nth) (>= (length links) nth))
  9854. (user-error "Invalid link selection"))
  9855. (setq link (nth (1- nth) links)))))
  9856. (cons link end))))))
  9857. ;; TODO: These functions are deprecated since `org-open-at-point'
  9858. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9859. (defun org-open-file-with-system (path)
  9860. "Open file at PATH using the system way of opening it."
  9861. (org-open-file path 'system))
  9862. (defun org-open-file-with-emacs (path)
  9863. "Open file at PATH in Emacs."
  9864. (org-open-file path 'emacs))
  9865. ;;; File search
  9866. (defvar org-create-file-search-functions nil
  9867. "List of functions to construct the right search string for a file link.
  9868. These functions are called in turn with point at the location to
  9869. which the link should point.
  9870. A function in the hook should first test if it would like to
  9871. handle this file type, for example by checking the `major-mode'
  9872. or the file extension. If it decides not to handle this file, it
  9873. should just return nil to give other functions a chance. If it
  9874. does handle the file, it must return the search string to be used
  9875. when following the link. The search string will be part of the
  9876. file link, given after a double colon, and `org-open-at-point'
  9877. will automatically search for it. If special measures must be
  9878. taken to make the search successful, another function should be
  9879. added to the companion hook `org-execute-file-search-functions',
  9880. which see.
  9881. A function in this hook may also use `setq' to set the variable
  9882. `description' to provide a suggestion for the descriptive text to
  9883. be used for this link when it gets inserted into an Org-mode
  9884. buffer with \\[org-insert-link].")
  9885. (defvar org-execute-file-search-functions nil
  9886. "List of functions to execute a file search triggered by a link.
  9887. Functions added to this hook must accept a single argument, the
  9888. search string that was part of the file link, the part after the
  9889. double colon. The function must first check if it would like to
  9890. handle this search, for example by checking the `major-mode' or
  9891. the file extension. If it decides not to handle this search, it
  9892. should just return nil to give other functions a chance. If it
  9893. does handle the search, it must return a non-nil value to keep
  9894. other functions from trying.
  9895. Each function can access the current prefix argument through the
  9896. variable `current-prefix-arg'. Note that a single prefix is used
  9897. to force opening a link in Emacs, so it may be good to only use a
  9898. numeric or double prefix to guide the search function.
  9899. In case this is needed, a function in this hook can also restore
  9900. the window configuration before `org-open-at-point' was called using:
  9901. (set-window-configuration org-window-config-before-follow-link)")
  9902. (defun org-link-search (s &optional type avoid-pos stealth)
  9903. "Search for a link search option.
  9904. If S is surrounded by forward slashes, it is interpreted as a
  9905. regular expression. In org-mode files, this will create an `org-occur'
  9906. sparse tree. In ordinary files, `occur' will be used to list matches.
  9907. If the current buffer is in `dired-mode', grep will be used to search
  9908. in all files. If AVOID-POS is given, ignore matches near that position.
  9909. When optional argument STEALTH is non-nil, do not modify
  9910. visibility around point, thus ignoring
  9911. `org-show-hierarchy-above', `org-show-following-heading' and
  9912. `org-show-siblings' variables."
  9913. (let ((case-fold-search t)
  9914. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9915. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9916. (append '(("") (" ") ("\t") ("\n"))
  9917. org-emphasis-alist)
  9918. "\\|") "\\)"))
  9919. (pos (point))
  9920. (pre nil) (post nil)
  9921. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9922. (cond
  9923. ;; First check if there are any special search functions
  9924. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9925. ;; Now try the builtin stuff
  9926. ((and (equal (string-to-char s0) ?#)
  9927. (> (length s0) 1)
  9928. (save-excursion
  9929. (goto-char (point-min))
  9930. (and
  9931. (re-search-forward
  9932. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9933. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9934. (setq type 'dedicated
  9935. pos (match-beginning 0))))
  9936. ;; There is an exact target for this
  9937. (goto-char pos)
  9938. (org-back-to-heading t)))
  9939. ((save-excursion
  9940. (goto-char (point-min))
  9941. (and
  9942. (re-search-forward
  9943. (concat "<<" (regexp-quote s0) ">>") nil t)
  9944. (setq type 'dedicated
  9945. pos (match-beginning 0))))
  9946. ;; There is an exact target for this
  9947. (goto-char pos))
  9948. ((save-excursion
  9949. (goto-char (point-min))
  9950. (and
  9951. (re-search-forward
  9952. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9953. (setq type 'dedicated pos (match-beginning 0))))
  9954. ;; Found an element with a matching #+name affiliated keyword.
  9955. (goto-char pos))
  9956. ((and (string-match "^(\\(.*\\))$" s0)
  9957. (save-excursion
  9958. (goto-char (point-min))
  9959. (and
  9960. (re-search-forward
  9961. (concat "[^[]" (regexp-quote
  9962. (format org-coderef-label-format
  9963. (match-string 1 s0))))
  9964. nil t)
  9965. (setq type 'dedicated
  9966. pos (1+ (match-beginning 0))))))
  9967. ;; There is a coderef target for this
  9968. (goto-char pos))
  9969. ((string-match "^/\\(.*\\)/$" s)
  9970. ;; A regular expression
  9971. (cond
  9972. ((derived-mode-p 'org-mode)
  9973. (org-occur (match-string 1 s)))
  9974. (t (org-do-occur (match-string 1 s)))))
  9975. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9976. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9977. (goto-char (point-min))
  9978. (cond
  9979. ((let (case-fold-search)
  9980. (re-search-forward (format org-complex-heading-regexp-format
  9981. (regexp-quote s))
  9982. nil t))
  9983. ;; OK, found a match
  9984. (setq type 'dedicated)
  9985. (goto-char (match-beginning 0)))
  9986. ((and (not org-link-search-inhibit-query)
  9987. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9988. (y-or-n-p "No match - create this as a new heading? "))
  9989. (goto-char (point-max))
  9990. (or (bolp) (newline))
  9991. (insert "* " s "\n")
  9992. (beginning-of-line 0))
  9993. (t
  9994. (goto-char pos)
  9995. (error "No match"))))
  9996. (t
  9997. ;; A normal search string
  9998. (when (equal (string-to-char s) ?*)
  9999. ;; Anchor on headlines, post may include tags.
  10000. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  10001. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  10002. s (substring s 1)))
  10003. (remove-text-properties
  10004. 0 (length s)
  10005. '(face nil mouse-face nil keymap nil fontified nil) s)
  10006. ;; Make a series of regular expressions to find a match
  10007. (setq words (org-split-string s "[ \n\r\t]+")
  10008. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10009. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10010. "\\)" markers)
  10011. re2a_ (concat "\\(" (mapconcat 'downcase words
  10012. "[ \t\r\n]+") "\\)[ \t\r\n]")
  10013. re2a (concat "[ \t\r\n]" re2a_)
  10014. re4_ (concat "\\(" (mapconcat 'downcase words
  10015. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10016. re4 (concat "[^a-zA-Z_]" re4_)
  10017. re1 (concat pre re2 post)
  10018. re3 (concat pre (if pre re4_ re4) post)
  10019. re5 (concat pre ".*" re4)
  10020. re2 (concat pre re2)
  10021. re2a (concat pre (if pre re2a_ re2a))
  10022. re4 (concat pre (if pre re4_ re4))
  10023. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  10024. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  10025. re5 "\\)"))
  10026. (cond
  10027. ((eq type 'org-occur) (org-occur reall))
  10028. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10029. (t (goto-char (point-min))
  10030. (setq type 'fuzzy)
  10031. (if (or (and (org-search-not-self 1 re0 nil t)
  10032. (setq type 'dedicated))
  10033. (org-search-not-self 1 re1 nil t)
  10034. (org-search-not-self 1 re2 nil t)
  10035. (org-search-not-self 1 re2a nil t)
  10036. (org-search-not-self 1 re3 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-add-planning-info (what &optional time &rest remove)
  12214. "Insert new timestamp with keyword in the planning line.
  12215. WHAT indicates what kind of time stamp to add. It is a symbol
  12216. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12217. the time to use. If none is given, the user is prompted for
  12218. a date. REMOVE indicates what kind of entries to remove. An old
  12219. WHAT entry will also be removed."
  12220. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12221. (catch 'exit
  12222. (when (and (memq what '(scheduled deadline))
  12223. (or (not time)
  12224. (and (stringp time)
  12225. (string-match "^[-+]+[0-9]" time))))
  12226. ;; Try to get a default date/time from existing timestamp
  12227. (save-excursion
  12228. (org-back-to-heading t)
  12229. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12230. (when (re-search-forward (if (eq what 'scheduled)
  12231. org-scheduled-time-regexp
  12232. org-deadline-time-regexp)
  12233. end t)
  12234. (setq ts (match-string 1)
  12235. default-time (apply 'encode-time (org-parse-time-string ts))
  12236. default-input (and ts (org-get-compact-tod ts)))))))
  12237. (when what
  12238. (setq time
  12239. (if (stringp time)
  12240. ;; This is a string (relative or absolute), set
  12241. ;; proper date.
  12242. (apply #'encode-time
  12243. (org-read-date-analyze
  12244. time default-time (decode-time default-time)))
  12245. ;; If necessary, get the time from the user
  12246. (or time (org-read-date nil 'to-time nil nil
  12247. default-time default-input)))))
  12248. (org-with-wide-buffer
  12249. (org-back-to-heading t)
  12250. (forward-line)
  12251. (unless (bolp) (insert "\n"))
  12252. (cond ((org-looking-at-p org-planning-line-re)
  12253. ;; Move to current indentation.
  12254. (skip-chars-forward " \t")
  12255. ;; Check if we have to remove something.
  12256. (dolist (type (if what (cons what remove) remove))
  12257. (when (save-excursion
  12258. (re-search-forward
  12259. (case type
  12260. (closed org-closed-time-regexp)
  12261. (deadline org-deadline-time-regexp)
  12262. (scheduled org-scheduled-time-regexp)
  12263. (otherwise (error "Invalid planning type: %s" type)))
  12264. (line-end-position) t))
  12265. (replace-match "")
  12266. (when (looking-at "--+<[^>]+>") (replace-match ""))
  12267. (when (and (not what) (eq type 'closed))
  12268. (save-excursion
  12269. (beginning-of-line)
  12270. (if (looking-at "[ \t]*$")
  12271. (delete-region (point) (1+ (point-at-eol)))))))
  12272. ;; Remove leading white spaces.
  12273. (when (and (not (bolp)) (looking-at "[ \t]+")) (replace-match ""))))
  12274. ((not what) (throw 'exit nil)) ; Nothing to do.
  12275. (t (insert-before-markers "\n")
  12276. (backward-char 1)
  12277. (when org-adapt-indentation
  12278. (org-indent-to-column (1+ (org-outline-level))))))
  12279. (when what
  12280. ;; Insert planning keyword.
  12281. (insert (case what
  12282. (closed org-closed-string)
  12283. (deadline org-deadline-string)
  12284. (scheduled org-scheduled-string)
  12285. (otherwise (error "Invalid planning type: %s" what)))
  12286. " ")
  12287. ;; Insert associated timestamp.
  12288. (let ((ts (org-insert-time-stamp
  12289. time
  12290. (or org-time-was-given
  12291. (and (eq what 'closed) org-log-done-with-time))
  12292. (eq what 'closed)
  12293. nil nil (list org-end-time-was-given))))
  12294. (unless (eolp) (insert " "))
  12295. ts))))))
  12296. (defvar org-log-note-marker (make-marker))
  12297. (defvar org-log-note-purpose nil)
  12298. (defvar org-log-note-state nil)
  12299. (defvar org-log-note-previous-state nil)
  12300. (defvar org-log-note-extra nil)
  12301. (defvar org-log-note-window-configuration nil)
  12302. (defvar org-log-note-return-to (make-marker))
  12303. (defvar org-log-note-effective-time nil
  12304. "Remembered current time so that dynamically scoped
  12305. `org-extend-today-until' affects tha timestamps in state change
  12306. log")
  12307. (defvar org-log-post-message nil
  12308. "Message to be displayed after a log note has been stored.
  12309. The auto-repeater uses this.")
  12310. (defun org-add-note ()
  12311. "Add a note to the current entry.
  12312. This is done in the same way as adding a state change note."
  12313. (interactive)
  12314. (org-add-log-setup 'note nil nil 'findpos nil))
  12315. (defun org-log-beginning (&optional create)
  12316. "Return expected start of log notes in current entry.
  12317. When optional argument CREATE is non-nil, the function creates
  12318. a drawer to store notes, if necessary. Returned position ignores
  12319. narrowing."
  12320. (org-with-wide-buffer
  12321. (org-back-to-heading t)
  12322. ;; Skip planning info and property drawer.
  12323. (forward-line)
  12324. (when (org-looking-at-p org-planning-line-re) (forward-line))
  12325. (when (looking-at org-property-drawer-re)
  12326. (goto-char (match-end 0))
  12327. (forward-line))
  12328. (let ((end (if (org-at-heading-p) (point)
  12329. (save-excursion (outline-next-heading) (point))))
  12330. (drawer (org-log-into-drawer)))
  12331. (cond
  12332. (drawer
  12333. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12334. (case-fold-search t))
  12335. (catch 'exit
  12336. ;; Try to find existing drawer.
  12337. (while (re-search-forward regexp end t)
  12338. (let ((element (org-element-at-point)))
  12339. (when (eq (org-element-type element) 'drawer)
  12340. (let ((cend (org-element-property :contents-end element)))
  12341. (when (and (not org-log-states-order-reversed) cend)
  12342. (goto-char cend)))
  12343. (throw 'exit nil))))
  12344. ;; No drawer found. Create one, if permitted.
  12345. (when create
  12346. (unless (bolp) (insert "\n"))
  12347. (let ((beg (point)))
  12348. (insert ":" drawer ":\n:END:\n")
  12349. (org-indent-region beg (point)))
  12350. (end-of-line -1)))))
  12351. (org-log-state-notes-insert-after-drawers
  12352. (while (and (looking-at org-drawer-regexp)
  12353. (progn (goto-char (match-end 0))
  12354. (re-search-forward org-property-end-re end t)))
  12355. (forward-line)))))
  12356. (if (bolp) (point) (line-beginning-position 2))))
  12357. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12358. "Set up the post command hook to take a note.
  12359. If this is about to TODO state change, the new state is expected in STATE.
  12360. When FINDPOS is non-nil, find the correct position for the note in
  12361. the current entry. If not, assume that it can be inserted at point.
  12362. HOW is an indicator what kind of note should be created.
  12363. EXTRA is additional text that will be inserted into the notes buffer."
  12364. (org-with-wide-buffer
  12365. (when findpos
  12366. (goto-char (org-log-beginning t))
  12367. (unless org-log-states-order-reversed
  12368. (org-skip-over-state-notes)
  12369. (skip-chars-backward " \t\n\r")
  12370. (forward-line)))
  12371. (move-marker org-log-note-marker (point))
  12372. ;; Preserve position even if a property drawer is inserted in the
  12373. ;; process.
  12374. (set-marker-insertion-type org-log-note-marker t)
  12375. (setq org-log-note-purpose purpose
  12376. org-log-note-state state
  12377. org-log-note-previous-state prev-state
  12378. org-log-note-how how
  12379. org-log-note-extra extra
  12380. org-log-note-effective-time (org-current-effective-time))
  12381. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12382. (defun org-skip-over-state-notes ()
  12383. "Skip past the list of State notes in an entry."
  12384. (when (ignore-errors (goto-char (org-in-item-p)))
  12385. (let* ((struct (org-list-struct))
  12386. (prevs (org-list-prevs-alist struct))
  12387. (regexp
  12388. (concat "[ \t]*- +"
  12389. (replace-regexp-in-string
  12390. " +" " +"
  12391. (org-replace-escapes
  12392. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12393. `(("%d" . ,org-ts-regexp-inactive)
  12394. ("%D" . ,org-ts-regexp)
  12395. ("%s" . "\"\\S-+\"")
  12396. ("%S" . "\"\\S-+\"")
  12397. ("%t" . ,org-ts-regexp-inactive)
  12398. ("%T" . ,org-ts-regexp)
  12399. ("%u" . ".*?")
  12400. ("%U" . ".*?")))))))
  12401. (while (org-looking-at-p regexp)
  12402. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12403. (org-list-get-item-end (point) struct)))))))
  12404. (defun org-add-log-note (&optional purpose)
  12405. "Pop up a window for taking a note, and add this note later at point."
  12406. (remove-hook 'post-command-hook 'org-add-log-note)
  12407. (setq org-log-note-window-configuration (current-window-configuration))
  12408. (delete-other-windows)
  12409. (move-marker org-log-note-return-to (point))
  12410. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12411. (goto-char org-log-note-marker)
  12412. (org-switch-to-buffer-other-window "*Org Note*")
  12413. (erase-buffer)
  12414. (if (memq org-log-note-how '(time state))
  12415. (let (current-prefix-arg) (org-store-log-note))
  12416. (let ((org-inhibit-startup t)) (org-mode))
  12417. (insert (format "# Insert note for %s.
  12418. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12419. (cond
  12420. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12421. ((eq org-log-note-purpose 'done) "closed todo item")
  12422. ((eq org-log-note-purpose 'state)
  12423. (format "state change from \"%s\" to \"%s\""
  12424. (or org-log-note-previous-state "")
  12425. (or org-log-note-state "")))
  12426. ((eq org-log-note-purpose 'reschedule)
  12427. "rescheduling")
  12428. ((eq org-log-note-purpose 'delschedule)
  12429. "no longer scheduled")
  12430. ((eq org-log-note-purpose 'redeadline)
  12431. "changing deadline")
  12432. ((eq org-log-note-purpose 'deldeadline)
  12433. "removing deadline")
  12434. ((eq org-log-note-purpose 'refile)
  12435. "refiling")
  12436. ((eq org-log-note-purpose 'note)
  12437. "this entry")
  12438. (t (error "This should not happen")))))
  12439. (if org-log-note-extra (insert org-log-note-extra))
  12440. (org-set-local 'org-finish-function 'org-store-log-note)
  12441. (run-hooks 'org-log-buffer-setup-hook)))
  12442. (defvar org-note-abort nil) ; dynamically scoped
  12443. (defun org-store-log-note ()
  12444. "Finish taking a log note, and insert it to where it belongs."
  12445. (let ((txt (buffer-string)))
  12446. (kill-buffer (current-buffer))
  12447. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12448. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12449. (setq txt (replace-match "" t t txt)))
  12450. (if (string-match "\\s-+\\'" txt)
  12451. (setq txt (replace-match "" t t txt)))
  12452. (setq lines (org-split-string txt "\n"))
  12453. (when (and note (string-match "\\S-" note))
  12454. (setq note
  12455. (org-replace-escapes
  12456. note
  12457. (list (cons "%u" (user-login-name))
  12458. (cons "%U" user-full-name)
  12459. (cons "%t" (format-time-string
  12460. (org-time-stamp-format 'long 'inactive)
  12461. org-log-note-effective-time))
  12462. (cons "%T" (format-time-string
  12463. (org-time-stamp-format 'long nil)
  12464. org-log-note-effective-time))
  12465. (cons "%d" (format-time-string
  12466. (org-time-stamp-format nil 'inactive)
  12467. org-log-note-effective-time))
  12468. (cons "%D" (format-time-string
  12469. (org-time-stamp-format nil nil)
  12470. org-log-note-effective-time))
  12471. (cons "%s" (if org-log-note-state
  12472. (concat "\"" org-log-note-state "\"")
  12473. ""))
  12474. (cons "%S" (if org-log-note-previous-state
  12475. (concat "\"" org-log-note-previous-state "\"")
  12476. "\"\"")))))
  12477. (when lines (setq note (concat note " \\\\")))
  12478. (push note lines))
  12479. (when (or current-prefix-arg org-note-abort)
  12480. (when (org-log-into-drawer)
  12481. (org-remove-empty-drawer-at org-log-note-marker))
  12482. (setq lines nil))
  12483. (when lines
  12484. (with-current-buffer (marker-buffer org-log-note-marker)
  12485. (save-excursion
  12486. (goto-char org-log-note-marker)
  12487. (move-marker org-log-note-marker nil)
  12488. ;; Make sure point is at the beginning of an empty line.
  12489. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12490. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12491. ;; In an existing list, add a new item at the top level.
  12492. ;; Otherwise, indent line like a regular one.
  12493. (let ((itemp (org-in-item-p)))
  12494. (if itemp
  12495. (org-indent-line-to
  12496. (let ((struct (save-excursion
  12497. (goto-char itemp) (org-list-struct))))
  12498. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12499. (org-indent-line)))
  12500. (insert (org-list-bullet-string "-") (pop lines))
  12501. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12502. (dolist (line lines)
  12503. (insert "\n")
  12504. (org-indent-line-to ind)
  12505. (insert line)))
  12506. (message "Note stored")
  12507. (org-back-to-heading t)
  12508. (org-cycle-hide-drawers 'children))
  12509. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12510. ;; from within `org-add-log-note' because `buffer-undo-list'
  12511. ;; is then modified outside of `org-with-remote-undo'.
  12512. (when (eq this-command 'org-agenda-todo)
  12513. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12514. ;; Don't add undo information when called from `org-agenda-todo'
  12515. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12516. (set-window-configuration org-log-note-window-configuration)
  12517. (with-current-buffer (marker-buffer org-log-note-return-to)
  12518. (goto-char org-log-note-return-to))
  12519. (move-marker org-log-note-return-to nil)
  12520. (and org-log-post-message (message "%s" org-log-post-message))))
  12521. (defun org-remove-empty-drawer-at (pos)
  12522. "Remove an empty drawer at position POS.
  12523. POS may also be a marker."
  12524. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12525. (org-with-wide-buffer
  12526. (goto-char pos)
  12527. (let ((drawer (org-element-at-point)))
  12528. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12529. (not (org-element-property :contents-begin drawer)))
  12530. (delete-region (org-element-property :begin drawer)
  12531. (progn (goto-char (org-element-property :end drawer))
  12532. (skip-chars-backward " \r\t\n")
  12533. (forward-line)
  12534. (point))))))))
  12535. (defvar org-ts-type nil)
  12536. (defun org-sparse-tree (&optional arg type)
  12537. "Create a sparse tree, prompt for the details.
  12538. This command can create sparse trees. You first need to select the type
  12539. of match used to create the tree:
  12540. t Show all TODO entries.
  12541. T Show entries with a specific TODO keyword.
  12542. m Show entries selected by a tags/property match.
  12543. p Enter a property name and its value (both with completion on existing
  12544. names/values) and show entries with that property.
  12545. r Show entries matching a regular expression (`/' can be used as well).
  12546. b Show deadlines and scheduled items before a date.
  12547. a Show deadlines and scheduled items after a date.
  12548. d Show deadlines due within `org-deadline-warning-days'.
  12549. D Show deadlines and scheduled items between a date range."
  12550. (interactive "P")
  12551. (setq type (or type org-sparse-tree-default-date-type))
  12552. (setq org-ts-type type)
  12553. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12554. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12555. [c]ycle through date types: %s"
  12556. (case type
  12557. (all "all timestamps")
  12558. (scheduled "only scheduled")
  12559. (deadline "only deadline")
  12560. (active "only active timestamps")
  12561. (inactive "only inactive timestamps")
  12562. (scheduled-or-deadline "scheduled/deadline")
  12563. (closed "with a closed time-stamp")
  12564. (otherwise "scheduled/deadline")))
  12565. (let ((answer (read-char-exclusive)))
  12566. (case answer
  12567. (?c
  12568. (org-sparse-tree
  12569. arg
  12570. (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
  12571. inactive closed)))))
  12572. (?d (call-interactively 'org-check-deadlines))
  12573. (?b (call-interactively 'org-check-before-date))
  12574. (?a (call-interactively 'org-check-after-date))
  12575. (?D (call-interactively 'org-check-dates-range))
  12576. (?t (call-interactively 'org-show-todo-tree))
  12577. (?T (org-show-todo-tree '(4)))
  12578. (?m (call-interactively 'org-match-sparse-tree))
  12579. ((?p ?P)
  12580. (let* ((kwd (org-icompleting-read
  12581. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12582. (value (org-icompleting-read
  12583. "Value: " (mapcar 'list (org-property-values kwd)))))
  12584. (unless (string-match "\\`{.*}\\'" value)
  12585. (setq value (concat "\"" value "\"")))
  12586. (org-match-sparse-tree arg (concat kwd "=" value))))
  12587. ((?r ?R ?/) (call-interactively 'org-occur))
  12588. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12589. (defvar org-occur-highlights nil
  12590. "List of overlays used for occur matches.")
  12591. (make-variable-buffer-local 'org-occur-highlights)
  12592. (defvar org-occur-parameters nil
  12593. "Parameters of the active org-occur calls.
  12594. This is a list, each call to org-occur pushes as cons cell,
  12595. containing the regular expression and the callback, onto the list.
  12596. The list can contain several entries if `org-occur' has been called
  12597. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12598. will only contain one set of parameters. When the highlights are
  12599. removed (for example with `C-c C-c', or with the next edit (depending
  12600. on `org-remove-highlights-with-change'), this variable is emptied
  12601. as well.")
  12602. (make-variable-buffer-local 'org-occur-parameters)
  12603. (defun org-occur (regexp &optional keep-previous callback)
  12604. "Make a compact tree which shows all matches of REGEXP.
  12605. The tree will show the lines where the regexp matches, and all higher
  12606. headlines above the match. It will also show the heading after the match,
  12607. to make sure editing the matching entry is easy.
  12608. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12609. call to `org-occur' will be kept, to allow stacking of calls to this
  12610. command.
  12611. If CALLBACK is non-nil, it is a function which is called to confirm
  12612. that the match should indeed be shown."
  12613. (interactive "sRegexp: \nP")
  12614. (when (equal regexp "")
  12615. (user-error "Regexp cannot be empty"))
  12616. (unless keep-previous
  12617. (org-remove-occur-highlights nil nil t))
  12618. (push (cons regexp callback) org-occur-parameters)
  12619. (let ((cnt 0))
  12620. (save-excursion
  12621. (goto-char (point-min))
  12622. (if (or (not keep-previous) ; do not want to keep
  12623. (not org-occur-highlights)) ; no previous matches
  12624. ;; hide everything
  12625. (org-overview))
  12626. (while (re-search-forward regexp nil t)
  12627. (when (or (not callback)
  12628. (save-match-data (funcall callback)))
  12629. (setq cnt (1+ cnt))
  12630. (when org-highlight-sparse-tree-matches
  12631. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12632. (org-show-context 'occur-tree))))
  12633. (when org-remove-highlights-with-change
  12634. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12635. nil 'local))
  12636. (unless org-sparse-tree-open-archived-trees
  12637. (org-hide-archived-subtrees (point-min) (point-max)))
  12638. (run-hooks 'org-occur-hook)
  12639. (if (org-called-interactively-p 'interactive)
  12640. (message "%d match(es) for regexp %s" cnt regexp))
  12641. cnt))
  12642. (defun org-occur-next-match (&optional n reset)
  12643. "Function for `next-error-function' to find sparse tree matches.
  12644. N is the number of matches to move, when negative move backwards.
  12645. RESET is entirely ignored - this function always goes back to the
  12646. starting point when no match is found."
  12647. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12648. (search-func (if (< n 0)
  12649. 'previous-single-char-property-change
  12650. 'next-single-char-property-change))
  12651. (n (abs n))
  12652. (pos (point))
  12653. p1)
  12654. (catch 'exit
  12655. (while (setq p1 (funcall search-func (point) 'org-type))
  12656. (when (equal p1 limit)
  12657. (goto-char pos)
  12658. (user-error "No more matches"))
  12659. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12660. (setq n (1- n))
  12661. (when (= n 0)
  12662. (goto-char p1)
  12663. (throw 'exit (point))))
  12664. (goto-char p1))
  12665. (goto-char p1)
  12666. (user-error "No more matches"))))
  12667. (defun org-show-context (&optional key)
  12668. "Make sure point and context are visible.
  12669. How much context is shown depends upon the variables
  12670. `org-show-hierarchy-above', `org-show-following-heading',
  12671. `org-show-entry-below' and `org-show-siblings'."
  12672. (let ((heading-p (org-at-heading-p t))
  12673. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12674. (following-p (org-get-alist-option org-show-following-heading key))
  12675. (entry-p (org-get-alist-option org-show-entry-below key))
  12676. (siblings-p (org-get-alist-option org-show-siblings key)))
  12677. ;; Show heading or entry text
  12678. (if (and heading-p (not entry-p))
  12679. (org-flag-heading nil) ; only show the heading
  12680. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12681. (org-show-hidden-entry))) ; show entire entry
  12682. (when following-p
  12683. ;; Show next sibling, or heading below text
  12684. (save-excursion
  12685. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12686. (org-flag-heading nil))))
  12687. (when siblings-p (org-show-siblings))
  12688. (when hierarchy-p
  12689. ;; show all higher headings, possibly with siblings
  12690. (save-excursion
  12691. (while (and (ignore-errors (progn (org-up-heading-all 1) t))
  12692. (not (bobp)))
  12693. (org-flag-heading nil)
  12694. (when siblings-p (org-show-siblings)))))))
  12695. (defvar org-reveal-start-hook nil
  12696. "Hook run before revealing a location.")
  12697. (defun org-reveal (&optional siblings)
  12698. "Show current entry, hierarchy above it, and the following headline.
  12699. This can be used to show a consistent set of context around locations
  12700. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12701. not t for the search context.
  12702. With optional argument SIBLINGS, on each level of the hierarchy all
  12703. siblings are shown. This repairs the tree structure to what it would
  12704. look like when opened with hierarchical calls to `org-cycle'.
  12705. With double optional argument \\[universal-argument] \\[universal-argument], \
  12706. go to the parent and show the
  12707. entire tree."
  12708. (interactive "P")
  12709. (run-hooks 'org-reveal-start-hook)
  12710. (let ((org-show-hierarchy-above t)
  12711. (org-show-following-heading t)
  12712. (org-show-siblings (if siblings t org-show-siblings)))
  12713. (org-show-context nil))
  12714. (when (equal siblings '(16))
  12715. (save-excursion
  12716. (when (org-up-heading-safe)
  12717. (org-show-subtree)
  12718. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12719. (defun org-highlight-new-match (beg end)
  12720. "Highlight from BEG to END and mark the highlight is an occur headline."
  12721. (let ((ov (make-overlay beg end)))
  12722. (overlay-put ov 'face 'secondary-selection)
  12723. (overlay-put ov 'org-type 'org-occur)
  12724. (push ov org-occur-highlights)))
  12725. (defun org-remove-occur-highlights (&optional beg end noremove)
  12726. "Remove the occur highlights from the buffer.
  12727. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12728. from the `before-change-functions' in the current buffer."
  12729. (interactive)
  12730. (unless org-inhibit-highlight-removal
  12731. (mapc 'delete-overlay org-occur-highlights)
  12732. (setq org-occur-highlights nil)
  12733. (setq org-occur-parameters nil)
  12734. (unless noremove
  12735. (remove-hook 'before-change-functions
  12736. 'org-remove-occur-highlights 'local))))
  12737. ;;;; Priorities
  12738. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12739. "Regular expression matching the priority indicator.")
  12740. (defvar org-remove-priority-next-time nil)
  12741. (defun org-priority-up ()
  12742. "Increase the priority of the current item."
  12743. (interactive)
  12744. (org-priority 'up))
  12745. (defun org-priority-down ()
  12746. "Decrease the priority of the current item."
  12747. (interactive)
  12748. (org-priority 'down))
  12749. (defun org-priority (&optional action show)
  12750. "Change the priority of an item.
  12751. ACTION can be `set', `up', `down', or a character."
  12752. (interactive "P")
  12753. (if (equal action '(4))
  12754. (org-show-priority)
  12755. (unless org-enable-priority-commands
  12756. (user-error "Priority commands are disabled"))
  12757. (setq action (or action 'set))
  12758. (let (current new news have remove)
  12759. (save-excursion
  12760. (org-back-to-heading t)
  12761. (if (looking-at org-priority-regexp)
  12762. (setq current (string-to-char (match-string 2))
  12763. have t))
  12764. (cond
  12765. ((eq action 'remove)
  12766. (setq remove t new ?\ ))
  12767. ((or (eq action 'set)
  12768. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12769. (if (not (eq action 'set))
  12770. (setq new action)
  12771. (message "Priority %c-%c, SPC to remove: "
  12772. org-highest-priority org-lowest-priority)
  12773. (save-match-data
  12774. (setq new (read-char-exclusive))))
  12775. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12776. (= (upcase org-lowest-priority) org-lowest-priority))
  12777. (setq new (upcase new)))
  12778. (cond ((equal new ?\ ) (setq remove t))
  12779. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12780. (user-error "Priority must be between `%c' and `%c'"
  12781. org-highest-priority org-lowest-priority))))
  12782. ((eq action 'up)
  12783. (setq new (if have
  12784. (1- current) ; normal cycling
  12785. ;; last priority was empty
  12786. (if (eq last-command this-command)
  12787. org-lowest-priority ; wrap around empty to lowest
  12788. ;; default
  12789. (if org-priority-start-cycle-with-default
  12790. org-default-priority
  12791. (1- org-default-priority))))))
  12792. ((eq action 'down)
  12793. (setq new (if have
  12794. (1+ current) ; normal cycling
  12795. ;; last priority was empty
  12796. (if (eq last-command this-command)
  12797. org-highest-priority ; wrap around empty to highest
  12798. ;; default
  12799. (if org-priority-start-cycle-with-default
  12800. org-default-priority
  12801. (1+ org-default-priority))))))
  12802. (t (user-error "Invalid action")))
  12803. (if (or (< (upcase new) org-highest-priority)
  12804. (> (upcase new) org-lowest-priority))
  12805. (if (and (memq action '(up down))
  12806. (not have) (not (eq last-command this-command)))
  12807. ;; `new' is from default priority
  12808. (error
  12809. "The default can not be set, see `org-default-priority' why")
  12810. ;; normal cycling: `new' is beyond highest/lowest priority
  12811. ;; and is wrapped around to the empty priority
  12812. (setq remove t)))
  12813. (setq news (format "%c" new))
  12814. (if have
  12815. (if remove
  12816. (replace-match "" t t nil 1)
  12817. (replace-match news t t nil 2))
  12818. (if remove
  12819. (user-error "No priority cookie found in line")
  12820. (let ((case-fold-search nil))
  12821. (looking-at org-todo-line-regexp))
  12822. (if (match-end 2)
  12823. (progn
  12824. (goto-char (match-end 2))
  12825. (insert " [#" news "]"))
  12826. (goto-char (match-beginning 3))
  12827. (insert "[#" news "] "))))
  12828. (org-set-tags nil 'align))
  12829. (if remove
  12830. (message "Priority removed")
  12831. (message "Priority of current item set to %s" news)))))
  12832. (defun org-show-priority ()
  12833. "Show the priority of the current item.
  12834. This priority is composed of the main priority given with the [#A] cookies,
  12835. and by additional input from the age of a schedules or deadline entry."
  12836. (interactive)
  12837. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12838. (org-get-at-bol 'priority)
  12839. (save-excursion
  12840. (save-match-data
  12841. (beginning-of-line)
  12842. (and (looking-at org-heading-regexp)
  12843. (org-get-priority (match-string 0))))))))
  12844. (message "Priority is %d" (if pri pri -1000))))
  12845. (defun org-get-priority (s)
  12846. "Find priority cookie and return priority."
  12847. (save-match-data
  12848. (if (functionp org-get-priority-function)
  12849. (funcall org-get-priority-function)
  12850. (if (not (string-match org-priority-regexp s))
  12851. (* 1000 (- org-lowest-priority org-default-priority))
  12852. (* 1000 (- org-lowest-priority
  12853. (string-to-char (match-string 2 s))))))))
  12854. ;;;; Tags
  12855. (defvar org-agenda-archives-mode)
  12856. (defvar org-map-continue-from nil
  12857. "Position from where mapping should continue.
  12858. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12859. (defvar org-scanner-tags nil
  12860. "The current tag list while the tags scanner is running.")
  12861. (defvar org-trust-scanner-tags nil
  12862. "Should `org-get-tags-at' use the tags for the scanner.
  12863. This is for internal dynamical scoping only.
  12864. When this is non-nil, the function `org-get-tags-at' will return the value
  12865. of `org-scanner-tags' instead of building the list by itself. This
  12866. can lead to large speed-ups when the tags scanner is used in a file with
  12867. many entries, and when the list of tags is retrieved, for example to
  12868. obtain a list of properties. Building the tags list for each entry in such
  12869. a file becomes an N^2 operation - but with this variable set, it scales
  12870. as N.")
  12871. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12872. "Scan headline tags with inheritance and produce output ACTION.
  12873. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12874. or `agenda' to produce an entry list for an agenda view. It can also be
  12875. a Lisp form or a function that should be called at each matched headline, in
  12876. this case the return value is a list of all return values from these calls.
  12877. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12878. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12879. only lines with a not-done TODO keyword are included in the output.
  12880. This should be the same variable that was scoped into
  12881. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12882. START-LEVEL can be a string with asterisks, reducing the scope to
  12883. headlines matching this string."
  12884. (require 'org-agenda)
  12885. (let* ((re (concat "^"
  12886. (if start-level
  12887. ;; Get the correct level to match
  12888. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12889. org-outline-regexp)
  12890. " *\\(\\<\\("
  12891. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12892. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12893. (props (list 'face 'default
  12894. 'done-face 'org-agenda-done
  12895. 'undone-face 'default
  12896. 'mouse-face 'highlight
  12897. 'org-not-done-regexp org-not-done-regexp
  12898. 'org-todo-regexp org-todo-regexp
  12899. 'org-complex-heading-regexp org-complex-heading-regexp
  12900. 'help-echo
  12901. (format "mouse-2 or RET jump to org file %s"
  12902. (abbreviate-file-name
  12903. (or (buffer-file-name (buffer-base-buffer))
  12904. (buffer-name (buffer-base-buffer)))))))
  12905. (org-map-continue-from nil)
  12906. lspos tags tags-list
  12907. (tags-alist (list (cons 0 org-file-tags)))
  12908. (llast 0) rtn rtn1 level category i txt
  12909. todo marker entry priority)
  12910. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12911. (setq action (list 'lambda nil action)))
  12912. (save-excursion
  12913. (goto-char (point-min))
  12914. (when (eq action 'sparse-tree)
  12915. (org-overview)
  12916. (org-remove-occur-highlights))
  12917. (while (let (case-fold-search)
  12918. (re-search-forward re nil t))
  12919. (setq org-map-continue-from nil)
  12920. (catch :skip
  12921. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12922. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12923. (goto-char (setq lspos (match-beginning 0)))
  12924. (setq level (org-reduced-level (org-outline-level))
  12925. category (org-get-category))
  12926. (setq i llast llast level)
  12927. ;; remove tag lists from same and sublevels
  12928. (while (>= i level)
  12929. (when (setq entry (assoc i tags-alist))
  12930. (setq tags-alist (delete entry tags-alist)))
  12931. (setq i (1- i)))
  12932. ;; add the next tags
  12933. (when tags
  12934. (setq tags (org-split-string tags ":")
  12935. tags-alist
  12936. (cons (cons level tags) tags-alist)))
  12937. ;; compile tags for current headline
  12938. (setq tags-list
  12939. (if org-use-tag-inheritance
  12940. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12941. tags)
  12942. org-scanner-tags tags-list)
  12943. (when org-use-tag-inheritance
  12944. (setcdr (car tags-alist)
  12945. (mapcar (lambda (x)
  12946. (setq x (copy-sequence x))
  12947. (org-add-prop-inherited x))
  12948. (cdar tags-alist))))
  12949. (when (and tags org-use-tag-inheritance
  12950. (or (not (eq t org-use-tag-inheritance))
  12951. org-tags-exclude-from-inheritance))
  12952. ;; selective inheritance, remove uninherited ones
  12953. (setcdr (car tags-alist)
  12954. (org-remove-uninherited-tags (cdar tags-alist))))
  12955. (when (and
  12956. ;; eval matcher only when the todo condition is OK
  12957. (and (or (not todo-only) (member todo org-not-done-keywords))
  12958. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12959. (eval matcher)))
  12960. ;; Call the skipper, but return t if it does not skip,
  12961. ;; so that the `and' form continues evaluating
  12962. (progn
  12963. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12964. t)
  12965. ;; Check if timestamps are deselecting this entry
  12966. (or (not todo-only)
  12967. (and (member todo org-not-done-keywords)
  12968. (or (not org-agenda-tags-todo-honor-ignore-options)
  12969. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12970. ;; select this headline
  12971. (cond
  12972. ((eq action 'sparse-tree)
  12973. (and org-highlight-sparse-tree-matches
  12974. (org-get-heading) (match-end 0)
  12975. (org-highlight-new-match
  12976. (match-beginning 1) (match-end 1)))
  12977. (org-show-context 'tags-tree))
  12978. ((eq action 'agenda)
  12979. (setq txt (org-agenda-format-item
  12980. ""
  12981. (concat
  12982. (if (eq org-tags-match-list-sublevels 'indented)
  12983. (make-string (1- level) ?.) "")
  12984. (org-get-heading))
  12985. level category
  12986. tags-list)
  12987. priority (org-get-priority txt))
  12988. (goto-char lspos)
  12989. (setq marker (org-agenda-new-marker))
  12990. (org-add-props txt props
  12991. 'org-marker marker 'org-hd-marker marker 'org-category category
  12992. 'todo-state todo
  12993. 'priority priority 'type "tagsmatch")
  12994. (push txt rtn))
  12995. ((functionp action)
  12996. (setq org-map-continue-from nil)
  12997. (save-excursion
  12998. (setq rtn1 (funcall action))
  12999. (push rtn1 rtn)))
  13000. (t (user-error "Invalid action")))
  13001. ;; if we are to skip sublevels, jump to end of subtree
  13002. (unless org-tags-match-list-sublevels
  13003. (org-end-of-subtree t)
  13004. (backward-char 1))))
  13005. ;; Get the correct position from where to continue
  13006. (if org-map-continue-from
  13007. (goto-char org-map-continue-from)
  13008. (and (= (point) lspos) (end-of-line 1)))))
  13009. (when (and (eq action 'sparse-tree)
  13010. (not org-sparse-tree-open-archived-trees))
  13011. (org-hide-archived-subtrees (point-min) (point-max)))
  13012. (nreverse rtn)))
  13013. (defun org-remove-uninherited-tags (tags)
  13014. "Remove all tags that are not inherited from the list TAGS."
  13015. (cond
  13016. ((eq org-use-tag-inheritance t)
  13017. (if org-tags-exclude-from-inheritance
  13018. (org-delete-all org-tags-exclude-from-inheritance tags)
  13019. tags))
  13020. ((not org-use-tag-inheritance) nil)
  13021. ((stringp org-use-tag-inheritance)
  13022. (delq nil (mapcar
  13023. (lambda (x)
  13024. (if (and (string-match org-use-tag-inheritance x)
  13025. (not (member x org-tags-exclude-from-inheritance)))
  13026. x nil))
  13027. tags)))
  13028. ((listp org-use-tag-inheritance)
  13029. (delq nil (mapcar
  13030. (lambda (x)
  13031. (if (member x org-use-tag-inheritance) x nil))
  13032. tags)))))
  13033. (defun org-match-sparse-tree (&optional todo-only match)
  13034. "Create a sparse tree according to tags string MATCH.
  13035. MATCH can contain positive and negative selection of tags, like
  13036. \"+WORK+URGENT-WITHBOSS\".
  13037. If optional argument TODO-ONLY is non-nil, only select lines that are
  13038. also TODO lines."
  13039. (interactive "P")
  13040. (org-agenda-prepare-buffers (list (current-buffer)))
  13041. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13042. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13043. (defvar org-cached-props nil)
  13044. (defun org-cached-entry-get (pom property)
  13045. (if (or (eq t org-use-property-inheritance)
  13046. (and (stringp org-use-property-inheritance)
  13047. (let ((case-fold-search t))
  13048. (org-string-match-p org-use-property-inheritance property)))
  13049. (and (listp org-use-property-inheritance)
  13050. (member-ignore-case property org-use-property-inheritance)))
  13051. ;; Caching is not possible, check it directly.
  13052. (org-entry-get pom property 'inherit)
  13053. ;; Get all properties, so we can do complicated checks easily.
  13054. (cdr (assoc-string property
  13055. (or org-cached-props
  13056. (setq org-cached-props (org-entry-properties pom)))
  13057. t))))
  13058. (defun org-global-tags-completion-table (&optional files)
  13059. "Return the list of all tags in all agenda buffer/files.
  13060. Optional FILES argument is a list of files which can be used
  13061. instead of the agenda files."
  13062. (save-excursion
  13063. (org-uniquify
  13064. (delq nil
  13065. (apply 'append
  13066. (mapcar
  13067. (lambda (file)
  13068. (set-buffer (find-file-noselect file))
  13069. (append (org-get-buffer-tags)
  13070. (mapcar (lambda (x) (if (stringp (car-safe x))
  13071. (list (car-safe x)) nil))
  13072. org-tag-alist)))
  13073. (if (and files (car files))
  13074. files
  13075. (org-agenda-files))))))))
  13076. (defun org-make-tags-matcher (match)
  13077. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13078. The variable `todo-only' is scoped dynamically into this function.
  13079. It will be set to t if the matcher restricts matching to TODO entries,
  13080. otherwise will not be touched.
  13081. Returns a cons of the selection string MATCH and the constructed
  13082. lisp form implementing the matcher. The matcher is to be evaluated
  13083. at an Org entry, with point on the headline, and returns t if the
  13084. entry matches the selection string MATCH. The returned lisp form
  13085. references two variables with information about the entry, which
  13086. must be bound around the form's evaluation: todo, the TODO keyword
  13087. at the entry (or nil of none); and tags-list, the list of all tags
  13088. at the entry including inherited ones. Additionally, the category
  13089. of the entry (if any) must be specified as the text property
  13090. 'org-category on the headline.
  13091. See also `org-scan-tags'.
  13092. "
  13093. (declare (special todo-only))
  13094. (unless (boundp 'todo-only)
  13095. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13096. (unless match
  13097. ;; Get a new match request, with completion against the global
  13098. ;; tags table and the local tags in current buffer
  13099. (let ((org-last-tags-completion-table
  13100. (org-uniquify
  13101. (delq nil (append (org-get-buffer-tags)
  13102. (org-global-tags-completion-table))))))
  13103. (setq match (org-completing-read-no-i
  13104. "Match: " 'org-tags-completion-function nil nil nil
  13105. 'org-tags-history))))
  13106. ;; Parse the string and create a lisp form
  13107. (let ((match0 match)
  13108. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13109. minus tag mm
  13110. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13111. orterms term orlist re-p str-p level-p level-op time-p
  13112. prop-p pn pv po gv rest (start 0) (ss 0))
  13113. ;; Expand group tags
  13114. (setq match (org-tags-expand match))
  13115. ;; Check if there is a TODO part of this match, which would be the
  13116. ;; part after a "/". TO make sure that this slash is not part of
  13117. ;; a property value to be matched against, we also check that there
  13118. ;; is no " after that slash.
  13119. ;; First, find the last slash
  13120. (while (string-match "/+" match ss)
  13121. (setq start (match-beginning 0) ss (match-end 0)))
  13122. (if (and (string-match "/+" match start)
  13123. (not (save-match-data (string-match "\"" match start))))
  13124. ;; match contains also a todo-matching request
  13125. (progn
  13126. (setq tagsmatch (substring match 0 (match-beginning 0))
  13127. todomatch (substring match (match-end 0)))
  13128. (if (string-match "^!" todomatch)
  13129. (setq todo-only t todomatch (substring todomatch 1)))
  13130. (if (string-match "^\\s-*$" todomatch)
  13131. (setq todomatch nil)))
  13132. ;; only matching tags
  13133. (setq tagsmatch match todomatch nil))
  13134. ;; Make the tags matcher
  13135. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13136. (setq tagsmatcher t)
  13137. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13138. (while (setq term (pop orterms))
  13139. (while (and (equal (substring term -1) "\\") orterms)
  13140. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13141. (while (string-match re term)
  13142. (setq rest (substring term (match-end 0))
  13143. minus (and (match-end 1)
  13144. (equal (match-string 1 term) "-"))
  13145. tag (save-match-data (replace-regexp-in-string
  13146. "\\\\-" "-"
  13147. (match-string 2 term)))
  13148. re-p (equal (string-to-char tag) ?{)
  13149. level-p (match-end 4)
  13150. prop-p (match-end 5)
  13151. mm (cond
  13152. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13153. (level-p
  13154. (setq level-op (org-op-to-function (match-string 3 term)))
  13155. `(,level-op level ,(string-to-number
  13156. (match-string 4 term))))
  13157. (prop-p
  13158. (setq pn (match-string 5 term)
  13159. po (match-string 6 term)
  13160. pv (match-string 7 term)
  13161. re-p (equal (string-to-char pv) ?{)
  13162. str-p (equal (string-to-char pv) ?\")
  13163. time-p (save-match-data
  13164. (string-match "^\"[[<].*[]>]\"$" pv))
  13165. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13166. (if time-p (setq pv (org-matcher-time pv)))
  13167. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13168. (cond
  13169. ((equal pn "CATEGORY")
  13170. (setq gv '(get-text-property (point) 'org-category)))
  13171. ((equal pn "TODO")
  13172. (setq gv 'todo))
  13173. (t
  13174. (setq gv `(org-cached-entry-get nil ,pn))))
  13175. (if re-p
  13176. (if (eq po 'org<>)
  13177. `(not (string-match ,pv (or ,gv "")))
  13178. `(string-match ,pv (or ,gv "")))
  13179. (if str-p
  13180. `(,po (or ,gv "") ,pv)
  13181. `(,po (string-to-number (or ,gv ""))
  13182. ,(string-to-number pv) ))))
  13183. (t `(member ,tag tags-list)))
  13184. mm (if minus (list 'not mm) mm)
  13185. term rest)
  13186. (push mm tagsmatcher))
  13187. (push (if (> (length tagsmatcher) 1)
  13188. (cons 'and tagsmatcher)
  13189. (car tagsmatcher))
  13190. orlist)
  13191. (setq tagsmatcher nil))
  13192. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13193. (setq tagsmatcher
  13194. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13195. ;; Make the todo matcher
  13196. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13197. (setq todomatcher t)
  13198. (setq orterms (org-split-string todomatch "|") orlist nil)
  13199. (while (setq term (pop orterms))
  13200. (while (string-match re term)
  13201. (setq minus (and (match-end 1)
  13202. (equal (match-string 1 term) "-"))
  13203. kwd (match-string 2 term)
  13204. re-p (equal (string-to-char kwd) ?{)
  13205. term (substring term (match-end 0))
  13206. mm (if re-p
  13207. `(string-match ,(substring kwd 1 -1) todo)
  13208. (list 'equal 'todo kwd))
  13209. mm (if minus (list 'not mm) mm))
  13210. (push mm todomatcher))
  13211. (push (if (> (length todomatcher) 1)
  13212. (cons 'and todomatcher)
  13213. (car todomatcher))
  13214. orlist)
  13215. (setq todomatcher nil))
  13216. (setq todomatcher (if (> (length orlist) 1)
  13217. (cons 'or orlist) (car orlist))))
  13218. ;; Return the string and lisp forms of the matcher
  13219. (setq matcher (if todomatcher
  13220. (list 'and tagsmatcher todomatcher)
  13221. tagsmatcher))
  13222. (when todo-only
  13223. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13224. matcher)))
  13225. (cons match0 matcher)))
  13226. (defun org-tags-expand (match &optional single-as-list downcased)
  13227. "Expand group tags in MATCH.
  13228. This replaces every group tag in MATCH with a regexp tag search.
  13229. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13230. will be replaced like this:
  13231. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13232. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13233. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13234. Replacing by a regexp preserves the structure of the match.
  13235. E.g., this expansion
  13236. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13237. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13238. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13239. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13240. assumed to be a single group tag, and the function will return
  13241. the list of tags in this group.
  13242. When DOWNCASE is non-nil, expand downcased TAGS."
  13243. (if org-group-tags
  13244. (let* ((case-fold-search t)
  13245. (stable org-mode-syntax-table)
  13246. (tal (or org-tag-groups-alist-for-agenda
  13247. org-tag-groups-alist))
  13248. (tal (if downcased
  13249. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13250. (tml (mapcar 'car tal))
  13251. (rtnmatch match) rpl)
  13252. ;; @ and _ are allowed as word-components in tags
  13253. (modify-syntax-entry ?@ "w" stable)
  13254. (modify-syntax-entry ?_ "w" stable)
  13255. (while (and tml
  13256. (with-syntax-table stable
  13257. (string-match
  13258. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13259. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13260. (let* ((dir (match-string 1 rtnmatch))
  13261. (tag (match-string 2 rtnmatch))
  13262. (tag (if downcased (downcase tag) tag)))
  13263. (setq tml (delete tag tml))
  13264. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13265. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13266. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13267. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13268. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13269. (if single-as-list
  13270. (or (reverse rpl) (list rtnmatch))
  13271. rtnmatch))
  13272. (if single-as-list (list (if downcased (downcase match) match))
  13273. match)))
  13274. (defun org-op-to-function (op &optional stringp)
  13275. "Turn an operator into the appropriate function."
  13276. (setq op
  13277. (cond
  13278. ((equal op "<" ) '(< string< org-time<))
  13279. ((equal op ">" ) '(> org-string> org-time>))
  13280. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13281. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13282. ((member op '("=" "==")) '(= string= org-time=))
  13283. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13284. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13285. (defun org<> (a b) (not (= a b)))
  13286. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13287. (defun org-string>= (a b) (not (string< a b)))
  13288. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13289. (defun org-string<> (a b) (not (string= a b)))
  13290. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13291. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13292. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13293. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13294. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13295. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13296. (defun org-2ft (s)
  13297. "Convert S to a floating point time.
  13298. If S is already a number, just return it. If it is a string, parse
  13299. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13300. (cond
  13301. ((numberp s) s)
  13302. ((stringp s)
  13303. (condition-case nil
  13304. (float-time (apply 'encode-time (org-parse-time-string s)))
  13305. (error 0.)))
  13306. (t 0.)))
  13307. (defun org-time-today ()
  13308. "Time in seconds today at 0:00.
  13309. Returns the float number of seconds since the beginning of the
  13310. epoch to the beginning of today (00:00)."
  13311. (float-time (apply 'encode-time
  13312. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13313. (defun org-matcher-time (s)
  13314. "Interpret a time comparison value."
  13315. (save-match-data
  13316. (cond
  13317. ((string= s "<now>") (float-time))
  13318. ((string= s "<today>") (org-time-today))
  13319. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13320. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13321. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13322. (+ (org-time-today)
  13323. (* (string-to-number (match-string 1 s))
  13324. (cdr (assoc (match-string 2 s)
  13325. '(("d" . 86400.0) ("w" . 604800.0)
  13326. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13327. (t (org-2ft s)))))
  13328. (defun org-match-any-p (re list)
  13329. "Does re match any element of list?"
  13330. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13331. (delq nil list))
  13332. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13333. (defvar org-tags-overlay (make-overlay 1 1))
  13334. (org-detach-overlay org-tags-overlay)
  13335. (defun org-get-local-tags-at (&optional pos)
  13336. "Get a list of tags defined in the current headline."
  13337. (org-get-tags-at pos 'local))
  13338. (defun org-get-local-tags ()
  13339. "Get a list of tags defined in the current headline."
  13340. (org-get-tags-at nil 'local))
  13341. (defun org-get-tags-at (&optional pos local)
  13342. "Get a list of all headline tags applicable at POS.
  13343. POS defaults to point. If tags are inherited, the list contains
  13344. the targets in the same sequence as the headlines appear, i.e.
  13345. the tags of the current headline come last.
  13346. When LOCAL is non-nil, only return tags from the current headline,
  13347. ignore inherited ones."
  13348. (interactive)
  13349. (if (and org-trust-scanner-tags
  13350. (or (not pos) (equal pos (point)))
  13351. (not local))
  13352. org-scanner-tags
  13353. (let (tags ltags lastpos parent)
  13354. (save-excursion
  13355. (save-restriction
  13356. (widen)
  13357. (goto-char (or pos (point)))
  13358. (save-match-data
  13359. (catch 'done
  13360. (condition-case nil
  13361. (progn
  13362. (org-back-to-heading t)
  13363. (while (not (equal lastpos (point)))
  13364. (setq lastpos (point))
  13365. (when (looking-at
  13366. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13367. (setq ltags (org-split-string
  13368. (org-match-string-no-properties 1) ":"))
  13369. (when parent
  13370. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13371. (setq tags (append
  13372. (if parent
  13373. (org-remove-uninherited-tags ltags)
  13374. ltags)
  13375. tags)))
  13376. (or org-use-tag-inheritance (throw 'done t))
  13377. (if local (throw 'done t))
  13378. (or (org-up-heading-safe) (error nil))
  13379. (setq parent t)))
  13380. (error nil)))))
  13381. (if local
  13382. tags
  13383. (reverse (delete-dups
  13384. (reverse (append
  13385. (org-remove-uninherited-tags
  13386. org-file-tags) tags)))))))))
  13387. (defun org-add-prop-inherited (s)
  13388. (add-text-properties 0 (length s) '(inherited t) s)
  13389. s)
  13390. (defun org-toggle-tag (tag &optional onoff)
  13391. "Toggle the tag TAG for the current line.
  13392. If ONOFF is `on' or `off', don't toggle but set to this state."
  13393. (let (res current)
  13394. (save-excursion
  13395. (org-back-to-heading t)
  13396. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13397. (point-at-eol) t)
  13398. (progn
  13399. (setq current (match-string 1))
  13400. (replace-match ""))
  13401. (setq current ""))
  13402. (setq current (nreverse (org-split-string current ":")))
  13403. (cond
  13404. ((eq onoff 'on)
  13405. (setq res t)
  13406. (or (member tag current) (push tag current)))
  13407. ((eq onoff 'off)
  13408. (or (not (member tag current)) (setq current (delete tag current))))
  13409. (t (if (member tag current)
  13410. (setq current (delete tag current))
  13411. (setq res t)
  13412. (push tag current))))
  13413. (end-of-line 1)
  13414. (if current
  13415. (progn
  13416. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13417. (org-set-tags nil t))
  13418. (delete-horizontal-space))
  13419. (run-hooks 'org-after-tags-change-hook))
  13420. res))
  13421. (defun org-align-tags-here (to-col)
  13422. ;; Assumes that this is a headline
  13423. "Align tags on the current headline to TO-COL."
  13424. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13425. (beginning-of-line 1)
  13426. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13427. (< pos (match-beginning 2)))
  13428. (progn
  13429. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13430. (goto-char (match-beginning 1))
  13431. (insert " ")
  13432. (delete-region (point) (1+ (match-beginning 2)))
  13433. (setq ncol (max (current-column)
  13434. (1+ col)
  13435. (if (> to-col 0)
  13436. to-col
  13437. (- (abs to-col) tags-l))))
  13438. (setq p (point))
  13439. (insert (make-string (- ncol (current-column)) ?\ ))
  13440. (setq ncol (current-column))
  13441. (when indent-tabs-mode (tabify p (point-at-eol)))
  13442. (org-move-to-column (min ncol col)))
  13443. (goto-char pos))))
  13444. (defun org-set-tags-command (&optional arg just-align)
  13445. "Call the set-tags command for the current entry."
  13446. (interactive "P")
  13447. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13448. (org-set-tags arg just-align)
  13449. (save-excursion
  13450. (unless (and (org-region-active-p)
  13451. org-loop-over-headlines-in-active-region)
  13452. (org-back-to-heading t))
  13453. (org-set-tags arg just-align))))
  13454. (defun org-set-tags-to (data)
  13455. "Set the tags of the current entry to DATA, replacing the current tags.
  13456. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13457. If DATA is nil or the empty string, any tags will be removed."
  13458. (interactive "sTags: ")
  13459. (setq data
  13460. (cond
  13461. ((eq data nil) "")
  13462. ((equal data "") "")
  13463. ((stringp data)
  13464. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13465. ":"))
  13466. ((listp data)
  13467. (concat ":" (mapconcat 'identity data ":") ":"))))
  13468. (when data
  13469. (save-excursion
  13470. (org-back-to-heading t)
  13471. (when (looking-at org-complex-heading-regexp)
  13472. (if (match-end 5)
  13473. (progn
  13474. (goto-char (match-beginning 5))
  13475. (insert data)
  13476. (delete-region (point) (point-at-eol))
  13477. (org-set-tags nil 'align))
  13478. (goto-char (point-at-eol))
  13479. (insert " " data)
  13480. (org-set-tags nil 'align)))
  13481. (beginning-of-line 1)
  13482. (if (looking-at ".*?\\([ \t]+\\)$")
  13483. (delete-region (match-beginning 1) (match-end 1))))))
  13484. (defun org-align-all-tags ()
  13485. "Align the tags i all headings."
  13486. (interactive)
  13487. (save-excursion
  13488. (or (ignore-errors (org-back-to-heading t))
  13489. (outline-next-heading))
  13490. (if (org-at-heading-p)
  13491. (org-set-tags t)
  13492. (message "No headings"))))
  13493. (defvar org-indent-indentation-per-level)
  13494. (defun org-set-tags (&optional arg just-align)
  13495. "Set the tags for the current headline.
  13496. With prefix ARG, realign all tags in headings in the current buffer.
  13497. When JUST-ALIGN is non-nil, only align tags."
  13498. (interactive "P")
  13499. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13500. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13501. 'region-start-level 'region))
  13502. org-loop-over-headlines-in-active-region)
  13503. (org-map-entries
  13504. ;; We don't use ARG and JUST-ALIGN here because these args
  13505. ;; are not useful when looping over headlines.
  13506. `(org-set-tags)
  13507. org-loop-over-headlines-in-active-region
  13508. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13509. (let* ((re org-outline-regexp-bol)
  13510. (current (unless arg (org-get-tags-string)))
  13511. (col (current-column))
  13512. (org-setting-tags t)
  13513. table current-tags inherited-tags ; computed below when needed
  13514. tags p0 c0 c1 rpl di tc level)
  13515. (if arg
  13516. (save-excursion
  13517. (goto-char (point-min))
  13518. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13519. (while (re-search-forward re nil t)
  13520. (org-set-tags nil t)
  13521. (end-of-line 1)))
  13522. (message "All tags realigned to column %d" org-tags-column))
  13523. (if just-align
  13524. (setq tags current)
  13525. ;; Get a new set of tags from the user
  13526. (save-excursion
  13527. (setq table (append org-tag-persistent-alist
  13528. (or org-tag-alist (org-get-buffer-tags))
  13529. (and
  13530. org-complete-tags-always-offer-all-agenda-tags
  13531. (org-global-tags-completion-table
  13532. (org-agenda-files))))
  13533. org-last-tags-completion-table table
  13534. current-tags (org-split-string current ":")
  13535. inherited-tags (nreverse
  13536. (nthcdr (length current-tags)
  13537. (nreverse (org-get-tags-at))))
  13538. tags
  13539. (if (or (eq t org-use-fast-tag-selection)
  13540. (and org-use-fast-tag-selection
  13541. (delq nil (mapcar 'cdr table))))
  13542. (org-fast-tag-selection
  13543. current-tags inherited-tags table
  13544. (if org-fast-tag-selection-include-todo
  13545. org-todo-key-alist))
  13546. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13547. (org-trim
  13548. (org-icompleting-read "Tags: "
  13549. 'org-tags-completion-function
  13550. nil nil current 'org-tags-history))))))
  13551. (while (string-match "[-+&]+" tags)
  13552. ;; No boolean logic, just a list
  13553. (setq tags (replace-match ":" t t tags))))
  13554. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13555. (if org-tags-sort-function
  13556. (setq tags (mapconcat 'identity
  13557. (sort (org-split-string
  13558. tags (org-re "[^[:alnum:]_@#%]+"))
  13559. org-tags-sort-function) ":")))
  13560. (if (string-match "\\`[\t ]*\\'" tags)
  13561. (setq tags "")
  13562. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13563. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13564. ;; Insert new tags at the correct column
  13565. (beginning-of-line 1)
  13566. (setq level (or (and (looking-at org-outline-regexp)
  13567. (- (match-end 0) (point) 1))
  13568. 1))
  13569. (cond
  13570. ((and (equal current "") (equal tags "")))
  13571. ((re-search-forward
  13572. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13573. (point-at-eol) t)
  13574. (if (equal tags "")
  13575. (setq rpl "")
  13576. (goto-char (match-beginning 0))
  13577. (setq c0 (current-column)
  13578. ;; compute offset for the case of org-indent-mode active
  13579. di (if (org-bound-and-true-p org-indent-mode)
  13580. (* (1- org-indent-indentation-per-level) (1- level))
  13581. 0)
  13582. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13583. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13584. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13585. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13586. (replace-match rpl t t)
  13587. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13588. tags)
  13589. (t (error "Tags alignment failed")))
  13590. (org-move-to-column col)
  13591. (unless just-align
  13592. (run-hooks 'org-after-tags-change-hook))))))
  13593. (defun org-change-tag-in-region (beg end tag off)
  13594. "Add or remove TAG for each entry in the region.
  13595. This works in the agenda, and also in an org-mode buffer."
  13596. (interactive
  13597. (list (region-beginning) (region-end)
  13598. (let ((org-last-tags-completion-table
  13599. (if (derived-mode-p 'org-mode)
  13600. (org-uniquify
  13601. (delq nil (append (org-get-buffer-tags)
  13602. (org-global-tags-completion-table))))
  13603. (org-global-tags-completion-table))))
  13604. (org-icompleting-read
  13605. "Tag: " 'org-tags-completion-function nil nil nil
  13606. 'org-tags-history))
  13607. (progn
  13608. (message "[s]et or [r]emove? ")
  13609. (equal (read-char-exclusive) ?r))))
  13610. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13611. (let ((agendap (equal major-mode 'org-agenda-mode))
  13612. l1 l2 m buf pos newhead (cnt 0))
  13613. (goto-char end)
  13614. (setq l2 (1- (org-current-line)))
  13615. (goto-char beg)
  13616. (setq l1 (org-current-line))
  13617. (loop for l from l1 to l2 do
  13618. (org-goto-line l)
  13619. (setq m (get-text-property (point) 'org-hd-marker))
  13620. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13621. (and agendap m))
  13622. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13623. pos (if agendap m (point)))
  13624. (with-current-buffer buf
  13625. (save-excursion
  13626. (save-restriction
  13627. (goto-char pos)
  13628. (setq cnt (1+ cnt))
  13629. (org-toggle-tag tag (if off 'off 'on))
  13630. (setq newhead (org-get-heading)))))
  13631. (and agendap (org-agenda-change-all-lines newhead m))))
  13632. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13633. (defun org-tags-completion-function (string predicate &optional flag)
  13634. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13635. (confirm (lambda (x) (stringp (car x)))))
  13636. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13637. (setq s1 (match-string 1 string)
  13638. s2 (match-string 2 string))
  13639. (setq s1 "" s2 string))
  13640. (cond
  13641. ((eq flag nil)
  13642. ;; try completion
  13643. (setq rtn (try-completion s2 ctable confirm))
  13644. (if (stringp rtn)
  13645. (setq rtn
  13646. (concat s1 s2 (substring rtn (length s2))
  13647. (if (and org-add-colon-after-tag-completion
  13648. (assoc rtn ctable))
  13649. ":" ""))))
  13650. rtn)
  13651. ((eq flag t)
  13652. ;; all-completions
  13653. (all-completions s2 ctable confirm))
  13654. ((eq flag 'lambda)
  13655. ;; exact match?
  13656. (assoc s2 ctable)))))
  13657. (defun org-fast-tag-insert (kwd tags face &optional end)
  13658. "Insert KDW, and the TAGS, the latter with face FACE.
  13659. Also insert END."
  13660. (insert (format "%-12s" (concat kwd ":"))
  13661. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13662. (or end "")))
  13663. (defun org-fast-tag-show-exit (flag)
  13664. (save-excursion
  13665. (org-goto-line 3)
  13666. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13667. (replace-match ""))
  13668. (when flag
  13669. (end-of-line 1)
  13670. (org-move-to-column (- (window-width) 19) t)
  13671. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13672. (defun org-set-current-tags-overlay (current prefix)
  13673. "Add an overlay to CURRENT tag with PREFIX."
  13674. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13675. (if (featurep 'xemacs)
  13676. (org-overlay-display org-tags-overlay (concat prefix s)
  13677. 'secondary-selection)
  13678. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13679. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13680. (defvar org-last-tag-selection-key nil)
  13681. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13682. "Fast tag selection with single keys.
  13683. CURRENT is the current list of tags in the headline, INHERITED is the
  13684. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13685. possibly with grouping information. TODO-TABLE is a similar table with
  13686. TODO keywords, should these have keys assigned to them.
  13687. If the keys are nil, a-z are automatically assigned.
  13688. Returns the new tags string, or nil to not change the current settings."
  13689. (let* ((fulltable (append table todo-table))
  13690. (maxlen (apply 'max (mapcar
  13691. (lambda (x)
  13692. (if (stringp (car x)) (string-width (car x)) 0))
  13693. fulltable)))
  13694. (buf (current-buffer))
  13695. (expert (eq org-fast-tag-selection-single-key 'expert))
  13696. (buffer-tags nil)
  13697. (fwidth (+ maxlen 3 1 3))
  13698. (ncol (/ (- (window-width) 4) fwidth))
  13699. (i-face 'org-done)
  13700. (c-face 'org-todo)
  13701. tg cnt e c char c1 c2 ntable tbl rtn
  13702. ov-start ov-end ov-prefix
  13703. (exit-after-next org-fast-tag-selection-single-key)
  13704. (done-keywords org-done-keywords)
  13705. groups ingroup)
  13706. (save-excursion
  13707. (beginning-of-line 1)
  13708. (if (looking-at
  13709. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13710. (setq ov-start (match-beginning 1)
  13711. ov-end (match-end 1)
  13712. ov-prefix "")
  13713. (setq ov-start (1- (point-at-eol))
  13714. ov-end (1+ ov-start))
  13715. (skip-chars-forward "^\n\r")
  13716. (setq ov-prefix
  13717. (concat
  13718. (buffer-substring (1- (point)) (point))
  13719. (if (> (current-column) org-tags-column)
  13720. " "
  13721. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13722. (move-overlay org-tags-overlay ov-start ov-end)
  13723. (save-window-excursion
  13724. (if expert
  13725. (set-buffer (get-buffer-create " *Org tags*"))
  13726. (delete-other-windows)
  13727. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13728. (org-switch-to-buffer-other-window " *Org tags*"))
  13729. (erase-buffer)
  13730. (org-set-local 'org-done-keywords done-keywords)
  13731. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13732. (org-fast-tag-insert "Current" current c-face "\n\n")
  13733. (org-fast-tag-show-exit exit-after-next)
  13734. (org-set-current-tags-overlay current ov-prefix)
  13735. (setq tbl fulltable char ?a cnt 0)
  13736. (while (setq e (pop tbl))
  13737. (cond
  13738. ((equal (car e) :startgroup)
  13739. (push '() groups) (setq ingroup t)
  13740. (when (not (= cnt 0))
  13741. (setq cnt 0)
  13742. (insert "\n"))
  13743. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13744. ((equal (car e) :endgroup)
  13745. (setq ingroup nil cnt 0)
  13746. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13747. ((equal e '(:newline))
  13748. (when (not (= cnt 0))
  13749. (setq cnt 0)
  13750. (insert "\n")
  13751. (setq e (car tbl))
  13752. (while (equal (car tbl) '(:newline))
  13753. (insert "\n")
  13754. (setq tbl (cdr tbl)))))
  13755. ((equal e '(:grouptags)) nil)
  13756. (t
  13757. (setq tg (copy-sequence (car e)) c2 nil)
  13758. (if (cdr e)
  13759. (setq c (cdr e))
  13760. ;; automatically assign a character.
  13761. (setq c1 (string-to-char
  13762. (downcase (substring
  13763. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13764. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13765. (while (or (rassoc char ntable) (rassoc char table))
  13766. (setq char (1+ char)))
  13767. (setq c2 c1))
  13768. (setq c (or c2 char)))
  13769. (if ingroup (push tg (car groups)))
  13770. (setq tg (org-add-props tg nil 'face
  13771. (cond
  13772. ((not (assoc tg table))
  13773. (org-get-todo-face tg))
  13774. ((member tg current) c-face)
  13775. ((member tg inherited) i-face))))
  13776. (if (equal (caar tbl) :grouptags)
  13777. (org-add-props tg nil 'face 'org-tag-group))
  13778. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13779. (insert "[" c "] " tg (make-string
  13780. (- fwidth 4 (length tg)) ?\ ))
  13781. (push (cons tg c) ntable)
  13782. (when (= (setq cnt (1+ cnt)) ncol)
  13783. (insert "\n")
  13784. (if ingroup (insert " "))
  13785. (setq cnt 0)))))
  13786. (setq ntable (nreverse ntable))
  13787. (insert "\n")
  13788. (goto-char (point-min))
  13789. (if (not expert) (org-fit-window-to-buffer))
  13790. (setq rtn
  13791. (catch 'exit
  13792. (while t
  13793. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13794. (if (not groups) "no " "")
  13795. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13796. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13797. (setq org-last-tag-selection-key c)
  13798. (cond
  13799. ((= c ?\r) (throw 'exit t))
  13800. ((= c ?!)
  13801. (setq groups (not groups))
  13802. (goto-char (point-min))
  13803. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13804. ((= c ?\C-c)
  13805. (if (not expert)
  13806. (org-fast-tag-show-exit
  13807. (setq exit-after-next (not exit-after-next)))
  13808. (setq expert nil)
  13809. (delete-other-windows)
  13810. (set-window-buffer (split-window-vertically) " *Org tags*")
  13811. (org-switch-to-buffer-other-window " *Org tags*")
  13812. (org-fit-window-to-buffer)))
  13813. ((or (= c ?\C-g)
  13814. (and (= c ?q) (not (rassoc c ntable))))
  13815. (org-detach-overlay org-tags-overlay)
  13816. (setq quit-flag t))
  13817. ((= c ?\ )
  13818. (setq current nil)
  13819. (if exit-after-next (setq exit-after-next 'now)))
  13820. ((= c ?\t)
  13821. (condition-case nil
  13822. (setq tg (org-icompleting-read
  13823. "Tag: "
  13824. (or buffer-tags
  13825. (with-current-buffer buf
  13826. (org-get-buffer-tags)))))
  13827. (quit (setq tg "")))
  13828. (when (string-match "\\S-" tg)
  13829. (add-to-list 'buffer-tags (list tg))
  13830. (if (member tg current)
  13831. (setq current (delete tg current))
  13832. (push tg current)))
  13833. (if exit-after-next (setq exit-after-next 'now)))
  13834. ((setq e (rassoc c todo-table) tg (car e))
  13835. (with-current-buffer buf
  13836. (save-excursion (org-todo tg)))
  13837. (if exit-after-next (setq exit-after-next 'now)))
  13838. ((setq e (rassoc c ntable) tg (car e))
  13839. (if (member tg current)
  13840. (setq current (delete tg current))
  13841. (loop for g in groups do
  13842. (if (member tg g)
  13843. (mapc (lambda (x)
  13844. (setq current (delete x current)))
  13845. g)))
  13846. (push tg current))
  13847. (if exit-after-next (setq exit-after-next 'now))))
  13848. ;; Create a sorted list
  13849. (setq current
  13850. (sort current
  13851. (lambda (a b)
  13852. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13853. (if (eq exit-after-next 'now) (throw 'exit t))
  13854. (goto-char (point-min))
  13855. (beginning-of-line 2)
  13856. (delete-region (point) (point-at-eol))
  13857. (org-fast-tag-insert "Current" current c-face)
  13858. (org-set-current-tags-overlay current ov-prefix)
  13859. (while (re-search-forward
  13860. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13861. (setq tg (match-string 1))
  13862. (add-text-properties
  13863. (match-beginning 1) (match-end 1)
  13864. (list 'face
  13865. (cond
  13866. ((member tg current) c-face)
  13867. ((member tg inherited) i-face)
  13868. (t (get-text-property (match-beginning 1) 'face))))))
  13869. (goto-char (point-min)))))
  13870. (org-detach-overlay org-tags-overlay)
  13871. (if rtn
  13872. (mapconcat 'identity current ":")
  13873. nil))))
  13874. (defun org-get-tags-string ()
  13875. "Get the TAGS string in the current headline."
  13876. (unless (org-at-heading-p t)
  13877. (user-error "Not on a heading"))
  13878. (save-excursion
  13879. (beginning-of-line 1)
  13880. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13881. (org-match-string-no-properties 1)
  13882. "")))
  13883. (defun org-get-tags ()
  13884. "Get the list of tags specified in the current headline."
  13885. (org-split-string (org-get-tags-string) ":"))
  13886. (defun org-get-buffer-tags ()
  13887. "Get a table of all tags used in the buffer, for completion."
  13888. (org-with-wide-buffer
  13889. (goto-char (point-min))
  13890. (let ((tag-re (concat org-outline-regexp-bol
  13891. "\\(?:.*?[ \t]\\)?"
  13892. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  13893. tags)
  13894. (while (re-search-forward tag-re nil t)
  13895. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  13896. (push tag tags)))
  13897. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  13898. ;;;; The mapping API
  13899. (defun org-map-entries (func &optional match scope &rest skip)
  13900. "Call FUNC at each headline selected by MATCH in SCOPE.
  13901. FUNC is a function or a lisp form. The function will be called without
  13902. arguments, with the cursor positioned at the beginning of the headline.
  13903. The return values of all calls to the function will be collected and
  13904. returned as a list.
  13905. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13906. does not need to preserve point. After evaluation, the cursor will be
  13907. moved to the end of the line (presumably of the headline of the
  13908. processed entry) and search continues from there. Under some
  13909. circumstances, this may not produce the wanted results. For example,
  13910. if you have removed (e.g. archived) the current (sub)tree it could
  13911. mean that the next entry will be skipped entirely. In such cases, you
  13912. can specify the position from where search should continue by making
  13913. FUNC set the variable `org-map-continue-from' to the desired buffer
  13914. position.
  13915. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13916. Only headlines that are matched by this query will be considered during
  13917. the iteration. When MATCH is nil or t, all headlines will be
  13918. visited by the iteration.
  13919. SCOPE determines the scope of this command. It can be any of:
  13920. nil The current buffer, respecting the restriction if any
  13921. tree The subtree started with the entry at point
  13922. region The entries within the active region, if any
  13923. region-start-level
  13924. The entries within the active region, but only those at
  13925. the same level than the first one.
  13926. file The current buffer, without restriction
  13927. file-with-archives
  13928. The current buffer, and any archives associated with it
  13929. agenda All agenda files
  13930. agenda-with-archives
  13931. All agenda files with any archive files associated with them
  13932. \(file1 file2 ...)
  13933. If this is a list, all files in the list will be scanned
  13934. The remaining args are treated as settings for the skipping facilities of
  13935. the scanner. The following items can be given here:
  13936. archive skip trees with the archive tag
  13937. comment skip trees with the COMMENT keyword
  13938. function or Emacs Lisp form:
  13939. will be used as value for `org-agenda-skip-function', so
  13940. whenever the function returns a position, FUNC will not be
  13941. called for that entry and search will continue from the
  13942. position returned
  13943. If your function needs to retrieve the tags including inherited tags
  13944. at the *current* entry, you can use the value of the variable
  13945. `org-scanner-tags' which will be much faster than getting the value
  13946. with `org-get-tags-at'. If your function gets properties with
  13947. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13948. to t around the call to `org-entry-properties' to get the same speedup.
  13949. Note that if your function moves around to retrieve tags and properties at
  13950. a *different* entry, you cannot use these techniques."
  13951. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13952. (not (org-region-active-p)))
  13953. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13954. (org-agenda-skip-archived-trees (memq 'archive skip))
  13955. (org-agenda-skip-comment-trees (memq 'comment skip))
  13956. (org-agenda-skip-function
  13957. (car (org-delete-all '(comment archive) skip)))
  13958. (org-tags-match-list-sublevels t)
  13959. (start-level (eq scope 'region-start-level))
  13960. matcher file res
  13961. org-todo-keywords-for-agenda
  13962. org-done-keywords-for-agenda
  13963. org-todo-keyword-alist-for-agenda
  13964. org-tag-alist-for-agenda
  13965. todo-only)
  13966. (cond
  13967. ((eq match t) (setq matcher t))
  13968. ((eq match nil) (setq matcher t))
  13969. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13970. (save-excursion
  13971. (save-restriction
  13972. (cond ((eq scope 'tree)
  13973. (org-back-to-heading t)
  13974. (org-narrow-to-subtree)
  13975. (setq scope nil))
  13976. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13977. (org-region-active-p))
  13978. ;; If needed, set start-level to a string like "2"
  13979. (when start-level
  13980. (save-excursion
  13981. (goto-char (region-beginning))
  13982. (unless (org-at-heading-p) (outline-next-heading))
  13983. (setq start-level (org-current-level))))
  13984. (narrow-to-region (region-beginning)
  13985. (save-excursion
  13986. (goto-char (region-end))
  13987. (unless (and (bolp) (org-at-heading-p))
  13988. (outline-next-heading))
  13989. (point)))
  13990. (setq scope nil)))
  13991. (if (not scope)
  13992. (progn
  13993. (org-agenda-prepare-buffers
  13994. (list (buffer-file-name (current-buffer))))
  13995. (setq res (org-scan-tags func matcher todo-only start-level)))
  13996. ;; Get the right scope
  13997. (cond
  13998. ((and scope (listp scope) (symbolp (car scope)))
  13999. (setq scope (eval scope)))
  14000. ((eq scope 'agenda)
  14001. (setq scope (org-agenda-files t)))
  14002. ((eq scope 'agenda-with-archives)
  14003. (setq scope (org-agenda-files t))
  14004. (setq scope (org-add-archive-files scope)))
  14005. ((eq scope 'file)
  14006. (setq scope (list (buffer-file-name))))
  14007. ((eq scope 'file-with-archives)
  14008. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14009. (org-agenda-prepare-buffers scope)
  14010. (while (setq file (pop scope))
  14011. (with-current-buffer (org-find-base-buffer-visiting file)
  14012. (save-excursion
  14013. (save-restriction
  14014. (widen)
  14015. (goto-char (point-min))
  14016. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14017. res)))
  14018. ;;; Properties API
  14019. (defconst org-special-properties
  14020. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14021. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14022. "The special properties valid in Org mode.
  14023. These are properties that are not defined in the property drawer,
  14024. but in some other way.")
  14025. (defconst org-default-properties
  14026. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14027. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14028. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14029. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14030. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14031. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14032. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14033. "Some properties that are used by Org mode for various purposes.
  14034. Being in this list makes sure that they are offered for completion.")
  14035. (defun org--update-property-plist (key val props)
  14036. "Associate KEY to VAL in alist PROPS.
  14037. Modifications are made by side-effect. Return new alist."
  14038. (let* ((appending (string= (substring key -1) "+"))
  14039. (key (if appending (substring key 0 -1) key))
  14040. (old (assoc-string key props t)))
  14041. (if (not old) (cons (cons key val) props)
  14042. (setcdr old (if appending (concat (cdr old) " " val) val))
  14043. props)))
  14044. (defun org-get-property-block (&optional beg force)
  14045. "Return the (beg . end) range of the body of the property drawer.
  14046. BEG is the beginning of the current subtree, or of the part
  14047. before the first headline. If it is not given, it will be found.
  14048. If the drawer does not exist, create it if FORCE is non-nil, or
  14049. return nil."
  14050. (org-with-wide-buffer
  14051. (when beg (goto-char beg))
  14052. (unless (org-before-first-heading-p)
  14053. (let ((beg (cond (beg)
  14054. ((or (not (featurep 'org-inlinetask))
  14055. (org-inlinetask-in-task-p))
  14056. (org-back-to-heading t))
  14057. (t (org-with-limited-levels (org-back-to-heading t))))))
  14058. (forward-line)
  14059. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14060. (cond ((looking-at org-property-drawer-re)
  14061. (forward-line)
  14062. (cons (point) (progn (goto-char (match-end 0))
  14063. (line-beginning-position))))
  14064. (force
  14065. (goto-char beg)
  14066. (org-insert-property-drawer)
  14067. (let ((pos (save-excursion (search-forward ":END:")
  14068. (line-beginning-position))))
  14069. (cons pos pos))))))))
  14070. (defun org-at-property-p ()
  14071. "Non-nil when point is inside a property drawer.
  14072. See `org-property-re' for match data, if applicable."
  14073. (save-excursion
  14074. (beginning-of-line)
  14075. (and (looking-at org-property-re)
  14076. (let ((property-drawer (save-match-data (org-get-property-block))))
  14077. (and property-drawer (< (point) (cdr property-drawer)))))))
  14078. (defun org-property-action ()
  14079. "Do an action on properties."
  14080. (interactive)
  14081. (unless (org-at-property-p) (user-error "Not at a property"))
  14082. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14083. (let ((c (read-char-exclusive)))
  14084. (case c
  14085. (?s (call-interactively #'org-set-property))
  14086. (?d (call-interactively #'org-delete-property))
  14087. (?D (call-interactively #'org-delete-property-globally))
  14088. (?c (call-interactively #'org-compute-property-at-point))
  14089. (otherwise (user-error "No such property action %c" c)))))
  14090. (defun org-inc-effort ()
  14091. "Increment the value of the effort property in the current entry."
  14092. (interactive)
  14093. (org-set-effort nil t))
  14094. (defvar org-clock-effort) ; Defined in org-clock.el.
  14095. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14096. (defun org-set-effort (&optional value increment)
  14097. "Set the effort property of the current entry.
  14098. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14099. 10th allowed value.
  14100. When INCREMENT is non-nil, set the property to the next allowed value."
  14101. (interactive "P")
  14102. (if (equal value 0) (setq value 10))
  14103. (let* ((completion-ignore-case t)
  14104. (prop org-effort-property)
  14105. (cur (org-entry-get nil prop))
  14106. (allowed (org-property-get-allowed-values nil prop 'table))
  14107. (existing (mapcar 'list (org-property-values prop)))
  14108. (heading (nth 4 (org-heading-components)))
  14109. rpl
  14110. (val (cond
  14111. ((stringp value) value)
  14112. ((and allowed (integerp value))
  14113. (or (car (nth (1- value) allowed))
  14114. (car (org-last allowed))))
  14115. ((and allowed increment)
  14116. (or (caadr (member (list cur) allowed))
  14117. (user-error "Allowed effort values are not set")))
  14118. (allowed
  14119. (message "Select 1-9,0, [RET%s]: %s"
  14120. (if cur (concat "=" cur) "")
  14121. (mapconcat 'car allowed " "))
  14122. (setq rpl (read-char-exclusive))
  14123. (if (equal rpl ?\r)
  14124. cur
  14125. (setq rpl (- rpl ?0))
  14126. (if (equal rpl 0) (setq rpl 10))
  14127. (if (and (> rpl 0) (<= rpl (length allowed)))
  14128. (car (nth (1- rpl) allowed))
  14129. (org-completing-read "Effort: " allowed nil))))
  14130. (t
  14131. (let (org-completion-use-ido org-completion-use-iswitchb)
  14132. (org-completing-read
  14133. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14134. (concat "[" cur "]") "")
  14135. ": ")
  14136. existing nil nil "" nil cur))))))
  14137. (unless (equal (org-entry-get nil prop) val)
  14138. (org-entry-put nil prop val))
  14139. (org-refresh-property
  14140. '((effort . identity)
  14141. (effort-minutes . org-duration-string-to-minutes))
  14142. val)
  14143. (when (string= heading org-clock-current-task)
  14144. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14145. (org-clock-update-mode-line))
  14146. (message "%s is now %s" prop val)))
  14147. (defun org-entry-properties (&optional pom which)
  14148. "Get all properties of the current entry.
  14149. When POM is a buffer position, get all properties from the entry
  14150. there instead.
  14151. This includes the TODO keyword, the tags, time strings for
  14152. deadline, scheduled, and clocking, and any additional properties
  14153. defined in the entry.
  14154. If WHICH is nil or `all', get all properties. If WHICH is
  14155. `special' or `standard', only get that subclass. If WHICH is
  14156. a string, only get that property.
  14157. Return value is an alist. Keys are properties, as upcased
  14158. strings."
  14159. (org-with-point-at pom
  14160. (when (and (derived-mode-p 'org-mode)
  14161. (ignore-errors (org-back-to-heading t)))
  14162. (catch 'exit
  14163. (let* ((beg (point))
  14164. (specific (and (stringp which) (upcase which)))
  14165. (which (cond ((not specific) which)
  14166. ((member specific org-special-properties) 'special)
  14167. (t 'standard)))
  14168. props)
  14169. ;; Get the special properties, like TODO and TAGS.
  14170. (when (memq which '(nil all special))
  14171. (when (or (not specific) (string= specific "CLOCKSUM"))
  14172. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14173. (when clocksum
  14174. (push (cons "CLOCKSUM"
  14175. (org-columns-number-to-string
  14176. (/ (float clocksum) 60.) 'add_times))
  14177. props)))
  14178. (when specific (throw 'exit props)))
  14179. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14180. (let ((clocksumt (get-text-property (point)
  14181. :org-clock-minutes-today)))
  14182. (when clocksumt
  14183. (push (cons "CLOCKSUM_T"
  14184. (org-columns-number-to-string
  14185. (/ (float clocksumt) 60.) 'add_times))
  14186. props)))
  14187. (when specific (throw 'exit props)))
  14188. (when (or (not specific) (string= specific "ITEM"))
  14189. (when (looking-at org-complex-heading-regexp)
  14190. (push (cons "ITEM"
  14191. (concat
  14192. (org-match-string-no-properties 1)
  14193. (let ((title (org-match-string-no-properties 4)))
  14194. (when (org-string-nw-p title)
  14195. (concat " " (org-remove-tabs title))))))
  14196. props))
  14197. (when specific (throw 'exit props)))
  14198. (when (or (not specific) (string= specific "TODO"))
  14199. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14200. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14201. (when specific (throw 'exit props)))
  14202. (when (or (not specific) (string= specific "PRIORITY"))
  14203. (when (looking-at org-priority-regexp)
  14204. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14205. (when specific (throw 'exit props)))
  14206. (when (or (not specific) (string= specific "FILE"))
  14207. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14208. props)
  14209. (when specific (throw 'exit props)))
  14210. (when (or (not specific) (string= specific "TAGS"))
  14211. (let ((value (org-string-nw-p (org-get-tags-string))))
  14212. (when value (push (cons "TAGS" value) props)))
  14213. (when specific (throw 'exit props)))
  14214. (when (or (not specific) (string= specific "ALLTAGS"))
  14215. (let ((value (org-get-tags-at)))
  14216. (when value
  14217. (push (cons "ALLTAGS"
  14218. (format ":%s:" (mapconcat #'identity value ":")))
  14219. props)))
  14220. (when specific (throw 'exit props)))
  14221. (when (or (not specific) (string= specific "BLOCKED"))
  14222. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14223. (when specific (throw 'exit props)))
  14224. (when (or (not specific)
  14225. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14226. (forward-line)
  14227. (when (org-looking-at-p org-planning-line-re)
  14228. (end-of-line)
  14229. (let ((bol (line-beginning-position))
  14230. ;; Backward compatibility: time keywords used to
  14231. ;; be configurable (before 8.3). Make sure we
  14232. ;; get the correct keyword.
  14233. (key-assoc `(("CLOSED" . ,org-closed-string)
  14234. ("DEADLINE" . ,org-deadline-string)
  14235. ("SCHEDULED" . ,org-scheduled-string))))
  14236. (dolist (pair (if specific (list (assoc specific key-assoc))
  14237. key-assoc))
  14238. (save-excursion
  14239. (when (search-backward (cdr pair) bol t)
  14240. (goto-char (match-end 0))
  14241. (skip-chars-forward " \t")
  14242. (and (looking-at org-ts-regexp-both)
  14243. (push (cons (car pair)
  14244. (org-match-string-no-properties 0))
  14245. props)))))))
  14246. (when specific (throw 'exit props)))
  14247. (when (or (not specific)
  14248. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14249. (let ((find-ts
  14250. (lambda (end ts)
  14251. (let ((regexp (if (or (string= specific "TIMESTAMP")
  14252. (assoc "TIMESTAMP_IA" ts))
  14253. org-ts-regexp
  14254. org-ts-regexp-both)))
  14255. (catch 'next
  14256. (while (re-search-forward regexp end t)
  14257. (backward-char)
  14258. (let ((object (org-element-context)))
  14259. ;; Accept to match timestamps in node
  14260. ;; properties, too.
  14261. (when (memq (org-element-type object)
  14262. '(node-property timestamp))
  14263. (let ((type
  14264. (org-element-property :type object)))
  14265. (cond
  14266. ((and (memq type '(active active-range))
  14267. (not (equal specific "TIMESTAMP_IA")))
  14268. (unless (assoc "TIMESTAMP" ts)
  14269. (push (cons "TIMESTAMP"
  14270. (org-element-property
  14271. :raw-value object))
  14272. ts)
  14273. (when specific (throw 'exit ts))))
  14274. ((and (memq type '(inactive inactive-range))
  14275. (not (string= specific "TIMESTAMP")))
  14276. (unless (assoc "TIMESTAMP_IA" ts)
  14277. (push (cons "TIMESTAMP_IA"
  14278. (org-element-property
  14279. :raw-value object))
  14280. ts)
  14281. (when specific (throw 'exit ts))))))
  14282. ;; Both timestamp types are found,
  14283. ;; move to next part.
  14284. (when (= (length ts) 2) (throw 'next ts)))))
  14285. ts)))))
  14286. (goto-char beg)
  14287. ;; First look for timestamps within headline.
  14288. (let ((ts (funcall find-ts (line-end-position) nil)))
  14289. (if (= (length ts) 2) (setq props (nconc ts props))
  14290. (forward-line)
  14291. ;; Then find timestamps in the section, skipping
  14292. ;; planning line.
  14293. (when (org-looking-at-p org-planning-line-re)
  14294. (forward-line))
  14295. (let ((end (save-excursion (outline-next-heading))))
  14296. (setq props (nconc (funcall find-ts end ts) props))))))))
  14297. ;; Get the standard properties, like :PROP:.
  14298. (when (memq which '(nil all standard))
  14299. ;; If we are looking after a specific property, delegate
  14300. ;; to `org-entry-get', which is faster. However, make an
  14301. ;; exception for "CATEGORY", since it can be also set
  14302. ;; through keywords (i.e. #+CATEGORY).
  14303. (if (and specific (not (equal specific "CATEGORY")))
  14304. (let ((value (org-entry-get beg specific nil t)))
  14305. (throw 'exit (and value (list (cons specific value)))))
  14306. (let ((range (org-get-property-block beg)))
  14307. (when range
  14308. (let ((end (cdr range)) seen-base)
  14309. (goto-char (car range))
  14310. ;; Unlike to `org--update-property-plist', we
  14311. ;; handle the case where base values is found
  14312. ;; after its extension. We also forbid standard
  14313. ;; properties to be named as special properties.
  14314. (while (re-search-forward org-property-re end t)
  14315. (let* ((key (upcase (org-match-string-no-properties 2)))
  14316. (extendp (org-string-match-p "\\+\\'" key))
  14317. (key-base (if extendp (substring key 0 -1) key))
  14318. (value (org-match-string-no-properties 3)))
  14319. (cond
  14320. ((member-ignore-case key-base org-special-properties))
  14321. (extendp
  14322. (setq props
  14323. (org--update-property-plist key value props)))
  14324. ((member key seen-base))
  14325. (t (push key seen-base)
  14326. (let ((p (assoc-string key props t)))
  14327. (if p (setcdr p (concat value " " (cdr p)))
  14328. (push (cons key value) props))))))))))))
  14329. (unless (assoc "CATEGORY" props)
  14330. (push (cons "CATEGORY" (org-get-category beg)) props)
  14331. (when (string= specific "CATEGORY") (throw 'exit props)))
  14332. ;; Return value.
  14333. (append (get-text-property beg 'org-summaries) props))))))
  14334. (defun org-entry-get (pom property &optional inherit literal-nil)
  14335. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14336. If INHERIT is non-nil and the entry does not have the property,
  14337. then also check higher levels of the hierarchy. If INHERIT is
  14338. the symbol `selective', use inheritance only if the setting in
  14339. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14340. If the property is present but empty, the return value is the
  14341. empty string. If the property is not present at all, nil is
  14342. returned. In any other case, return the value as a string.
  14343. Search is case-insensitive.
  14344. If LITERAL-NIL is set, return the string value \"nil\" as
  14345. a string, do not interpret it as the list atom nil. This is used
  14346. for inheritance when a \"nil\" value can supersede a non-nil
  14347. value higher up the hierarchy."
  14348. (org-with-point-at pom
  14349. (cond
  14350. ((and inherit
  14351. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14352. (org-entry-get-with-inheritance property literal-nil))
  14353. ((member-ignore-case property org-special-properties)
  14354. ;; We need a special property. Use `org-entry-properties' to
  14355. ;; retrieve it, but specify the wanted property.
  14356. (cdr (assoc-string property (org-entry-properties nil property))))
  14357. (t
  14358. (let ((range (org-get-property-block)))
  14359. (when range
  14360. (let* ((case-fold-search t)
  14361. (end (cdr range))
  14362. (props
  14363. (let ((global
  14364. (or (assoc-string property org-file-properties t)
  14365. (assoc-string property org-global-properties t)
  14366. (assoc-string
  14367. property org-global-properties-fixed t))))
  14368. ;; Make sure to not re-use GLOBAL as
  14369. ;; `org--update-property-plist' would alter it by
  14370. ;; side-effect.
  14371. (and global (list (cons property (cdr global))))))
  14372. (find-value
  14373. (lambda (key)
  14374. (when (re-search-forward (org-re-property key nil t) end t)
  14375. (setq props
  14376. (org--update-property-plist
  14377. key (org-match-string-no-properties 3) props))))))
  14378. (goto-char (car range))
  14379. ;; Find base value.
  14380. (save-excursion (funcall find-value property))
  14381. ;; Find additional values.
  14382. (let ((property+ (concat property "+")))
  14383. (while (funcall find-value property+)))
  14384. ;; Return final value.
  14385. (let ((val (cdr (assoc-string property props t))))
  14386. (if literal-nil val (org-not-nil val))))))))))
  14387. (defun org-property-or-variable-value (var &optional inherit)
  14388. "Check if there is a property fixing the value of VAR.
  14389. If yes, return this value. If not, return the current value of the variable."
  14390. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14391. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14392. (read prop)
  14393. (symbol-value var))))
  14394. (defun org-entry-delete (pom property)
  14395. "Delete the property PROPERTY from entry at point-or-marker POM."
  14396. (unless (member property org-special-properties)
  14397. (org-with-point-at pom
  14398. (let ((range (org-get-property-block)))
  14399. (when range
  14400. (let ((begin (car range))
  14401. (end (copy-marker (cdr range))))
  14402. (goto-char begin)
  14403. (when (re-search-forward (org-re-property property nil t) end t)
  14404. (delete-region (match-beginning 0) (line-beginning-position 2))
  14405. ;; If drawer is empty, remove it altogether.
  14406. (when (= begin end)
  14407. (delete-region (line-beginning-position 0)
  14408. (line-beginning-position 2)))
  14409. (set-marker end nil))))))))
  14410. ;; Multi-values properties are properties that contain multiple values
  14411. ;; These values are assumed to be single words, separated by whitespace.
  14412. (defun org-entry-add-to-multivalued-property (pom property value)
  14413. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14414. (let* ((old (org-entry-get pom property))
  14415. (values (and old (org-split-string old "[ \t]"))))
  14416. (setq value (org-entry-protect-space value))
  14417. (unless (member value values)
  14418. (setq values (append values (list value)))
  14419. (org-entry-put pom property
  14420. (mapconcat 'identity values " ")))))
  14421. (defun org-entry-remove-from-multivalued-property (pom property value)
  14422. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14423. (let* ((old (org-entry-get pom property))
  14424. (values (and old (org-split-string old "[ \t]"))))
  14425. (setq value (org-entry-protect-space value))
  14426. (when (member value values)
  14427. (setq values (delete value values))
  14428. (org-entry-put pom property
  14429. (mapconcat 'identity values " ")))))
  14430. (defun org-entry-member-in-multivalued-property (pom property value)
  14431. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14432. (let* ((old (org-entry-get pom property))
  14433. (values (and old (org-split-string old "[ \t]"))))
  14434. (setq value (org-entry-protect-space value))
  14435. (member value values)))
  14436. (defun org-entry-get-multivalued-property (pom property)
  14437. "Return a list of values in a multivalued property."
  14438. (let* ((value (org-entry-get pom property))
  14439. (values (and value (org-split-string value "[ \t]"))))
  14440. (mapcar 'org-entry-restore-space values)))
  14441. (defun org-entry-put-multivalued-property (pom property &rest values)
  14442. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14443. VALUES should be a list of strings. Spaces will be protected."
  14444. (org-entry-put pom property
  14445. (mapconcat 'org-entry-protect-space values " "))
  14446. (let* ((value (org-entry-get pom property))
  14447. (values (and value (org-split-string value "[ \t]"))))
  14448. (mapcar 'org-entry-restore-space values)))
  14449. (defun org-entry-protect-space (s)
  14450. "Protect spaces and newline in string S."
  14451. (while (string-match " " s)
  14452. (setq s (replace-match "%20" t t s)))
  14453. (while (string-match "\n" s)
  14454. (setq s (replace-match "%0A" t t s)))
  14455. s)
  14456. (defun org-entry-restore-space (s)
  14457. "Restore spaces and newline in string S."
  14458. (while (string-match "%20" s)
  14459. (setq s (replace-match " " t t s)))
  14460. (while (string-match "%0A" s)
  14461. (setq s (replace-match "\n" t t s)))
  14462. s)
  14463. (defvar org-entry-property-inherited-from (make-marker)
  14464. "Marker pointing to the entry from where a property was inherited.
  14465. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14466. location of the entry where the inheritance search matched. If there was
  14467. no match, the marker will point nowhere.
  14468. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14469. is set.")
  14470. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14471. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14472. The search will stop at the first ancestor which has the property defined.
  14473. If the value found is \"nil\", return nil to show that the property
  14474. should be considered as undefined (this is the meaning of nil here).
  14475. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14476. (move-marker org-entry-property-inherited-from nil)
  14477. (let (value)
  14478. (org-with-wide-buffer
  14479. (catch 'exit
  14480. (while t
  14481. (when (setq value (org-entry-get nil property nil literal-nil))
  14482. (org-back-to-heading t)
  14483. (move-marker org-entry-property-inherited-from (point))
  14484. (throw 'exit nil))
  14485. (or (org-up-heading-safe) (throw 'exit nil)))))
  14486. (unless value
  14487. (setq value
  14488. (cdr (or (assoc-string property org-file-properties t)
  14489. (assoc-string property org-global-properties t)
  14490. (assoc-string property org-global-properties-fixed t)))))
  14491. (if literal-nil value (org-not-nil value))))
  14492. (defvar org-property-changed-functions nil
  14493. "Hook called when the value of a property has changed.
  14494. Each hook function should accept two arguments, the name of the property
  14495. and the new value.")
  14496. (defun org-entry-put (pom property value)
  14497. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14498. If the value is `nil', it is converted to the empty string. If
  14499. it is not a string, an error is raised.
  14500. PROPERTY can be any regular property (see
  14501. `org-special-properties'). It can also be \"TODO\",
  14502. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14503. For the last two properties, VALUE may have any of the special
  14504. values \"earlier\" and \"later\". The function then increases or
  14505. decreases scheduled or deadline date by one day."
  14506. (cond ((null value) (setq value ""))
  14507. ((not (stringp value)) (error "Properties values should be strings")))
  14508. (org-with-point-at pom
  14509. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14510. (org-back-to-heading t)
  14511. (org-with-limited-levels (org-back-to-heading t)))
  14512. (let ((beg (point)))
  14513. (cond
  14514. ((equal property "TODO")
  14515. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14516. ((not (member value org-todo-keywords-1))
  14517. (user-error "\"%s\" is not a valid TODO state" value)))
  14518. (org-todo value)
  14519. (org-set-tags nil 'align))
  14520. ((equal property "PRIORITY")
  14521. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14522. (org-set-tags nil 'align))
  14523. ((equal property "SCHEDULED")
  14524. (forward-line)
  14525. (if (and (org-looking-at-p org-planning-line-re)
  14526. (re-search-forward
  14527. org-scheduled-time-regexp (line-end-position) t))
  14528. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14529. ((string= value "later") (org-timestamp-change 1 'day))
  14530. ((string= value "") (org-schedule '(4)))
  14531. (t (org-schedule nil value)))
  14532. (if (member value '("earlier" "later" ""))
  14533. (call-interactively #'org-schedule)
  14534. (org-schedule nil value))))
  14535. ((equal property "DEADLINE")
  14536. (forward-line)
  14537. (if (and (org-looking-at-p org-planning-line-re)
  14538. (re-search-forward
  14539. org-deadline-time-regexp (line-end-position) t))
  14540. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14541. ((string= value "later") (org-timestamp-change 1 'day))
  14542. ((string= value "") (org-deadline '(4)))
  14543. (t (org-deadline nil value)))
  14544. (if (member value '("earlier" "later" ""))
  14545. (call-interactively #'org-deadline)
  14546. (org-deadline nil value))))
  14547. ((member property org-special-properties)
  14548. (error "The %s property cannot be set with `org-entry-put'" property))
  14549. (t
  14550. (let* ((range (org-get-property-block beg 'force))
  14551. (end (cdr range))
  14552. (case-fold-search t))
  14553. (goto-char (car range))
  14554. (if (re-search-forward (org-re-property property nil t) end t)
  14555. (progn (delete-region (match-beginning 0) (match-end 0))
  14556. (goto-char (match-beginning 0)))
  14557. (goto-char end)
  14558. (insert "\n")
  14559. (backward-char))
  14560. (insert ":" property ":")
  14561. (when value (insert " " value))
  14562. (org-indent-line)))))
  14563. (run-hook-with-args 'org-property-changed-functions property value)))
  14564. (defun org-buffer-property-keys (&optional specials defaults columns)
  14565. "Get all property keys in the current buffer.
  14566. When SPECIALS is non-nil, also list the special properties that
  14567. reflect things like tags and TODO state.
  14568. When DEFAULTS is non-nil, also include properties that has
  14569. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14570. DESCRIPTION, LOCATION, and LOGGING and others.
  14571. When COLUMNS in non-nil, also include property names given in
  14572. COLUMN formats in the current buffer."
  14573. (let ((case-fold-search t)
  14574. (props (append
  14575. (and specials org-special-properties)
  14576. (and defaults (cons org-effort-property org-default-properties))
  14577. nil)))
  14578. (org-with-wide-buffer
  14579. (goto-char (point-min))
  14580. (while (re-search-forward org-property-start-re nil t)
  14581. (let ((range (org-get-property-block)))
  14582. (catch 'skip
  14583. (unless range
  14584. (when (and (not (org-before-first-heading-p))
  14585. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14586. (line-beginning-position))))
  14587. (org-get-property-block nil t))
  14588. (throw 'skip nil))
  14589. (goto-char (car range))
  14590. (let ((begin (car range))
  14591. (end (cdr range)))
  14592. ;; Make sure that found property block is not located
  14593. ;; before current point, as it would generate an infloop.
  14594. ;; It can happen, for example, in the following
  14595. ;; situation:
  14596. ;;
  14597. ;; * Headline
  14598. ;; :PROPERTIES:
  14599. ;; ...
  14600. ;; :END:
  14601. ;; *************** Inlinetask
  14602. ;; #+BEGIN_EXAMPLE
  14603. ;; :PROPERTIES:
  14604. ;; #+END_EXAMPLE
  14605. ;;
  14606. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14607. (while (< (point) end)
  14608. (let ((p (progn (looking-at org-property-re)
  14609. (org-match-string-no-properties 2))))
  14610. ;; Only add true property name, not extension symbol.
  14611. (add-to-list 'props
  14612. (if (not (org-string-match-p "\\+\\'" p)) p
  14613. (substring p 0 -1))))
  14614. (forward-line))))
  14615. (outline-next-heading)))
  14616. (when columns
  14617. (goto-char (point-min))
  14618. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14619. (let ((element (org-element-at-point)))
  14620. (when (memq (org-element-type element) '(keyword node-property))
  14621. (let ((value (org-element-property :value element))
  14622. (start 0))
  14623. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14624. (setq start (match-end 0))
  14625. (let ((p (org-match-string-no-properties 1 value)))
  14626. (unless (member-ignore-case p org-special-properties)
  14627. (add-to-list 'props p))))))))))
  14628. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14629. (defun org-property-values (key)
  14630. "List all non-nil values of property KEY in current buffer."
  14631. (org-with-wide-buffer
  14632. (goto-char (point-min))
  14633. (let ((case-fold-search t)
  14634. (re (org-re-property key))
  14635. values)
  14636. (while (re-search-forward re nil t)
  14637. (add-to-list 'values (org-entry-get (point) key)))
  14638. values)))
  14639. (defun org-insert-property-drawer ()
  14640. "Insert a property drawer into the current entry."
  14641. (org-with-wide-buffer
  14642. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14643. (org-back-to-heading t)
  14644. (org-with-limited-levels (org-back-to-heading t)))
  14645. (forward-line)
  14646. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14647. (unless (org-looking-at-p org-property-drawer-re)
  14648. (let ((inhibit-read-only t))
  14649. (unless (bolp) (insert "\n"))
  14650. (let ((begin (point)))
  14651. (insert ":PROPERTIES:\n:END:\n")
  14652. (org-indent-region begin (point)))))))
  14653. (defun org-insert-drawer (&optional arg drawer)
  14654. "Insert a drawer at point.
  14655. When optional argument ARG is non-nil, insert a property drawer.
  14656. Optional argument DRAWER, when non-nil, is a string representing
  14657. drawer's name. Otherwise, the user is prompted for a name.
  14658. If a region is active, insert the drawer around that region
  14659. instead.
  14660. Point is left between drawer's boundaries."
  14661. (interactive "P")
  14662. (let* ((drawer (if arg "PROPERTIES"
  14663. (or drawer (read-from-minibuffer "Drawer: ")))))
  14664. (cond
  14665. ;; With C-u, fall back on `org-insert-property-drawer'
  14666. (arg (org-insert-property-drawer))
  14667. ;; Check validity of suggested drawer's name.
  14668. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14669. (user-error "Invalid drawer name"))
  14670. ;; With an active region, insert a drawer at point.
  14671. ((not (org-region-active-p))
  14672. (progn
  14673. (unless (bolp) (insert "\n"))
  14674. (insert (format ":%s:\n\n:END:\n" drawer))
  14675. (forward-line -2)))
  14676. ;; Otherwise, insert the drawer at point
  14677. (t
  14678. (let ((rbeg (region-beginning))
  14679. (rend (copy-marker (region-end))))
  14680. (unwind-protect
  14681. (progn
  14682. (goto-char rbeg)
  14683. (beginning-of-line)
  14684. (when (save-excursion
  14685. (re-search-forward org-outline-regexp-bol rend t))
  14686. (user-error "Drawers cannot contain headlines"))
  14687. ;; Position point at the beginning of the first
  14688. ;; non-blank line in region. Insert drawer's opening
  14689. ;; there, then indent it.
  14690. (org-skip-whitespace)
  14691. (beginning-of-line)
  14692. (insert ":" drawer ":\n")
  14693. (forward-line -1)
  14694. (indent-for-tab-command)
  14695. ;; Move point to the beginning of the first blank line
  14696. ;; after the last non-blank line in region. Insert
  14697. ;; drawer's closing, then indent it.
  14698. (goto-char rend)
  14699. (skip-chars-backward " \r\t\n")
  14700. (insert "\n:END:")
  14701. (deactivate-mark t)
  14702. (indent-for-tab-command)
  14703. (unless (eolp) (insert "\n")))
  14704. ;; Clear marker, whatever the outcome of insertion is.
  14705. (set-marker rend nil)))))))
  14706. (defvar org-property-set-functions-alist nil
  14707. "Property set function alist.
  14708. Each entry should have the following format:
  14709. (PROPERTY . READ-FUNCTION)
  14710. The read function will be called with the same argument as
  14711. `org-completing-read'.")
  14712. (defun org-set-property-function (property)
  14713. "Get the function that should be used to set PROPERTY.
  14714. This is computed according to `org-property-set-functions-alist'."
  14715. (or (cdr (assoc property org-property-set-functions-alist))
  14716. 'org-completing-read))
  14717. (defun org-read-property-value (property)
  14718. "Read PROPERTY value from user."
  14719. (let* ((completion-ignore-case t)
  14720. (allowed (org-property-get-allowed-values nil property 'table))
  14721. (cur (org-entry-get nil property))
  14722. (prompt (concat property " value"
  14723. (if (and cur (string-match "\\S-" cur))
  14724. (concat " [" cur "]") "") ": "))
  14725. (set-function (org-set-property-function property))
  14726. (val (if allowed
  14727. (funcall set-function prompt allowed nil
  14728. (not (get-text-property 0 'org-unrestricted
  14729. (caar allowed))))
  14730. (let (org-completion-use-ido org-completion-use-iswitchb)
  14731. (funcall set-function prompt
  14732. (mapcar 'list (org-property-values property))
  14733. nil nil "" nil cur)))))
  14734. (org-trim val)))
  14735. (defvar org-last-set-property nil)
  14736. (defvar org-last-set-property-value nil)
  14737. (defun org-read-property-name ()
  14738. "Read a property name."
  14739. (let* ((completion-ignore-case t)
  14740. (keys (org-buffer-property-keys nil t t))
  14741. (default-prop (or (save-excursion
  14742. (save-match-data
  14743. (beginning-of-line)
  14744. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14745. (null (string= (match-string 1) "END"))
  14746. (match-string 1))))
  14747. org-last-set-property))
  14748. (property (org-icompleting-read
  14749. (concat "Property"
  14750. (if default-prop (concat " [" default-prop "]") "")
  14751. ": ")
  14752. (mapcar 'list keys)
  14753. nil nil nil nil
  14754. default-prop)))
  14755. (if (member property keys)
  14756. property
  14757. (or (cdr (assoc (downcase property)
  14758. (mapcar (lambda (x) (cons (downcase x) x))
  14759. keys)))
  14760. property))))
  14761. (defun org-set-property-and-value (use-last)
  14762. "Allow to set [PROPERTY]: [value] direction from prompt.
  14763. When use-default, don't even ask, just use the last
  14764. \"[PROPERTY]: [value]\" string from the history."
  14765. (interactive "P")
  14766. (let* ((completion-ignore-case t)
  14767. (pv (or (and use-last org-last-set-property-value)
  14768. (org-completing-read
  14769. "Enter a \"[Property]: [value]\" pair: "
  14770. nil nil nil nil nil
  14771. org-last-set-property-value)))
  14772. prop val)
  14773. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14774. (setq prop (match-string 1 pv)
  14775. val (match-string 2 pv))
  14776. (org-set-property prop val))))
  14777. (defun org-set-property (property value)
  14778. "In the current entry, set PROPERTY to VALUE.
  14779. When called interactively, this will prompt for a property name, offering
  14780. completion on existing and default properties. And then it will prompt
  14781. for a value, offering completion either on allowed values (via an inherited
  14782. xxx_ALL property) or on existing values in other instances of this property
  14783. in the current file."
  14784. (interactive (list nil nil))
  14785. (let* ((property (or property (org-read-property-name)))
  14786. (value (or value (org-read-property-value property)))
  14787. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14788. (setq org-last-set-property property)
  14789. (setq org-last-set-property-value (concat property ": " value))
  14790. ;; Possibly postprocess the inserted value:
  14791. (when fn (setq value (funcall fn value)))
  14792. (unless (equal (org-entry-get nil property) value)
  14793. (org-entry-put nil property value))))
  14794. (defun org-delete-property (property)
  14795. "In the current entry, delete PROPERTY."
  14796. (interactive
  14797. (let* ((completion-ignore-case t)
  14798. (cat (org-entry-get (point) "CATEGORY"))
  14799. (props0 (org-entry-properties nil 'standard))
  14800. (props (if cat props0
  14801. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14802. (prop (if (< 1 (length props))
  14803. (org-icompleting-read "Property: " props nil t)
  14804. (caar props))))
  14805. (list prop)))
  14806. (if (not property)
  14807. (message "No property to delete in this entry")
  14808. (org-entry-delete nil property)
  14809. (message "Property \"%s\" deleted" property)))
  14810. (defun org-delete-property-globally (property)
  14811. "Remove PROPERTY globally, from all entries."
  14812. (interactive
  14813. (let* ((completion-ignore-case t)
  14814. (prop (org-icompleting-read
  14815. "Globally remove property: "
  14816. (mapcar 'list (org-buffer-property-keys)))))
  14817. (list prop)))
  14818. (save-excursion
  14819. (save-restriction
  14820. (widen)
  14821. (goto-char (point-min))
  14822. (let ((cnt 0))
  14823. (while (re-search-forward
  14824. (org-re-property property)
  14825. nil t)
  14826. (setq cnt (1+ cnt))
  14827. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14828. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14829. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14830. (defun org-compute-property-at-point ()
  14831. "Compute the property at point.
  14832. This looks for an enclosing column format, extracts the operator and
  14833. then applies it to the property in the column format's scope."
  14834. (interactive)
  14835. (unless (org-at-property-p)
  14836. (user-error "Not at a property"))
  14837. (let ((prop (org-match-string-no-properties 2)))
  14838. (org-columns-get-format-and-top-level)
  14839. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14840. (user-error "No operator defined for property %s" prop))
  14841. (org-columns-compute prop)))
  14842. (defvar org-property-allowed-value-functions nil
  14843. "Hook for functions supplying allowed values for a specific property.
  14844. The functions must take a single argument, the name of the property, and
  14845. return a flat list of allowed values. If \":ETC\" is one of
  14846. the values, this means that these values are intended as defaults for
  14847. completion, but that other values should be allowed too.
  14848. The functions must return nil if they are not responsible for this
  14849. property.")
  14850. (defun org-property-get-allowed-values (pom property &optional table)
  14851. "Get allowed values for the property PROPERTY.
  14852. When TABLE is non-nil, return an alist that can directly be used for
  14853. completion."
  14854. (let (vals)
  14855. (cond
  14856. ((equal property "TODO")
  14857. (setq vals (org-with-point-at pom
  14858. (append org-todo-keywords-1 '("")))))
  14859. ((equal property "PRIORITY")
  14860. (let ((n org-lowest-priority))
  14861. (while (>= n org-highest-priority)
  14862. (push (char-to-string n) vals)
  14863. (setq n (1- n)))))
  14864. ((member property org-special-properties))
  14865. ((setq vals (run-hook-with-args-until-success
  14866. 'org-property-allowed-value-functions property)))
  14867. (t
  14868. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14869. (when (and vals (string-match "\\S-" vals))
  14870. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14871. (setq vals (mapcar (lambda (x)
  14872. (cond ((stringp x) x)
  14873. ((numberp x) (number-to-string x))
  14874. ((symbolp x) (symbol-name x))
  14875. (t "???")))
  14876. vals)))))
  14877. (when (member ":ETC" vals)
  14878. (setq vals (remove ":ETC" vals))
  14879. (org-add-props (car vals) '(org-unrestricted t)))
  14880. (if table (mapcar 'list vals) vals)))
  14881. (defun org-property-previous-allowed-value (&optional previous)
  14882. "Switch to the next allowed value for this property."
  14883. (interactive)
  14884. (org-property-next-allowed-value t))
  14885. (defun org-property-next-allowed-value (&optional previous)
  14886. "Switch to the next allowed value for this property."
  14887. (interactive)
  14888. (unless (org-at-property-p)
  14889. (user-error "Not at a property"))
  14890. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14891. (key (match-string 2))
  14892. (value (match-string 3))
  14893. (allowed (or (org-property-get-allowed-values (point) key)
  14894. (and (member value '("[ ]" "[-]" "[X]"))
  14895. '("[ ]" "[X]"))))
  14896. (heading (save-match-data (nth 4 (org-heading-components))))
  14897. nval)
  14898. (unless allowed
  14899. (user-error "Allowed values for this property have not been defined"))
  14900. (if previous (setq allowed (reverse allowed)))
  14901. (if (member value allowed)
  14902. (setq nval (car (cdr (member value allowed)))))
  14903. (setq nval (or nval (car allowed)))
  14904. (if (equal nval value)
  14905. (user-error "Only one allowed value for this property"))
  14906. (org-at-property-p)
  14907. (replace-match (concat " :" key ": " nval) t t)
  14908. (org-indent-line)
  14909. (beginning-of-line 1)
  14910. (skip-chars-forward " \t")
  14911. (when (equal prop org-effort-property)
  14912. (org-refresh-property
  14913. '((effort . identity)
  14914. (effort-minutes . org-duration-string-to-minutes))
  14915. nval)
  14916. (when (string= org-clock-current-task heading)
  14917. (setq org-clock-effort nval)
  14918. (org-clock-update-mode-line)))
  14919. (run-hook-with-args 'org-property-changed-functions key nval)))
  14920. (defun org-find-olp (path &optional this-buffer)
  14921. "Return a marker pointing to the entry at outline path OLP.
  14922. If anything goes wrong, throw an error.
  14923. You can wrap this call to catch the error like this:
  14924. (condition-case msg
  14925. (org-mobile-locate-entry (match-string 4))
  14926. (error (nth 1 msg)))
  14927. The return value will then be either a string with the error message,
  14928. or a marker if everything is OK.
  14929. If THIS-BUFFER is set, the outline path does not contain a file,
  14930. only headings."
  14931. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14932. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14933. (level 1)
  14934. (lmin 1)
  14935. (lmax 1)
  14936. limit re end found pos heading cnt flevel)
  14937. (unless buffer (error "File not found :%s" file))
  14938. (with-current-buffer buffer
  14939. (save-excursion
  14940. (save-restriction
  14941. (widen)
  14942. (setq limit (point-max))
  14943. (goto-char (point-min))
  14944. (while (setq heading (pop path))
  14945. (setq re (format org-complex-heading-regexp-format
  14946. (regexp-quote heading)))
  14947. (setq cnt 0 pos (point))
  14948. (while (re-search-forward re end t)
  14949. (setq level (- (match-end 1) (match-beginning 1)))
  14950. (if (and (>= level lmin) (<= level lmax))
  14951. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14952. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14953. lmax heading))
  14954. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14955. lmax heading))
  14956. (goto-char found)
  14957. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14958. (setq end (save-excursion (org-end-of-subtree t t))))
  14959. (when (org-at-heading-p)
  14960. (point-marker)))))))
  14961. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14962. "Find node HEADING in BUFFER.
  14963. Return a marker to the heading if it was found, or nil if not.
  14964. If POS-ONLY is set, return just the position instead of a marker.
  14965. The heading text must match exact, but it may have a TODO keyword,
  14966. a priority cookie and tags in the standard locations."
  14967. (with-current-buffer (or buffer (current-buffer))
  14968. (save-excursion
  14969. (save-restriction
  14970. (widen)
  14971. (goto-char (point-min))
  14972. (let (case-fold-search)
  14973. (if (re-search-forward
  14974. (format org-complex-heading-regexp-format
  14975. (regexp-quote heading)) nil t)
  14976. (if pos-only
  14977. (match-beginning 0)
  14978. (move-marker (make-marker) (match-beginning 0)))))))))
  14979. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14980. "Find Org node headline HEADING in all .org files in directory DIR.
  14981. When the target headline is found, return a marker to this location."
  14982. (let ((files (directory-files (or dir default-directory)
  14983. t "\\`[^.#].*\\.org\\'"))
  14984. file visiting m buffer)
  14985. (catch 'found
  14986. (while (setq file (pop files))
  14987. (message "trying %s" file)
  14988. (setq visiting (org-find-base-buffer-visiting file))
  14989. (setq buffer (or visiting (find-file-noselect file)))
  14990. (setq m (org-find-exact-headline-in-buffer
  14991. heading buffer))
  14992. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14993. (and m (throw 'found m))))))
  14994. (defun org-find-entry-with-id (ident)
  14995. "Locate the entry that contains the ID property with exact value IDENT.
  14996. IDENT can be a string, a symbol or a number, this function will search for
  14997. the string representation of it.
  14998. Return the position where this entry starts, or nil if there is no such entry."
  14999. (interactive "sID: ")
  15000. (let ((id (cond
  15001. ((stringp ident) ident)
  15002. ((symbol-name ident) (symbol-name ident))
  15003. ((numberp ident) (number-to-string ident))
  15004. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15005. (case-fold-search nil))
  15006. (save-excursion
  15007. (save-restriction
  15008. (widen)
  15009. (goto-char (point-min))
  15010. (when (re-search-forward
  15011. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15012. nil t)
  15013. (org-back-to-heading t)
  15014. (point))))))
  15015. ;;;; Timestamps
  15016. (defvar org-last-changed-timestamp nil)
  15017. (defvar org-last-inserted-timestamp nil
  15018. "The last time stamp inserted with `org-insert-time-stamp'.")
  15019. (defvar org-ts-what) ; dynamically scoped parameter
  15020. (defun org-time-stamp (arg &optional inactive)
  15021. "Prompt for a date/time and insert a time stamp.
  15022. If the user specifies a time like HH:MM or if this command is
  15023. called with at least one prefix argument, the time stamp contains
  15024. the date and the time. Otherwise, only the date is be included.
  15025. All parts of a date not specified by the user is filled in from
  15026. the current date/time. So if you just press return without
  15027. typing anything, the time stamp will represent the current
  15028. date/time.
  15029. If there is already a timestamp at the cursor, it will be
  15030. modified.
  15031. With two universal prefix arguments, insert an active timestamp
  15032. with the current time without prompting the user.
  15033. When called from lisp, the timestamp is inactive if INACTIVE is
  15034. non-nil."
  15035. (interactive "P")
  15036. (let* ((ts nil)
  15037. (default-time
  15038. ;; Default time is either today, or, when entering a range,
  15039. ;; the range start.
  15040. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15041. (save-excursion
  15042. (re-search-backward
  15043. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15044. (- (point) 20) t)))
  15045. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15046. (current-time)))
  15047. (default-input (and ts (org-get-compact-tod ts)))
  15048. (repeater (save-excursion
  15049. (save-match-data
  15050. (beginning-of-line)
  15051. (when (re-search-forward
  15052. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  15053. (save-excursion (progn (end-of-line) (point))) t)
  15054. (match-string 0)))))
  15055. org-time-was-given org-end-time-was-given time)
  15056. (cond
  15057. ((and (org-at-timestamp-p t)
  15058. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15059. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15060. (insert "--")
  15061. (setq time (let ((this-command this-command))
  15062. (org-read-date arg 'totime nil nil
  15063. default-time default-input inactive)))
  15064. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15065. ((org-at-timestamp-p t)
  15066. (setq time (let ((this-command this-command))
  15067. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  15068. (when (org-at-timestamp-p t) ; just to get the match data
  15069. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  15070. (replace-match "")
  15071. (setq org-last-changed-timestamp
  15072. (org-insert-time-stamp
  15073. time (or org-time-was-given arg)
  15074. inactive nil nil (list org-end-time-was-given)))
  15075. (when repeater (goto-char (1- (point))) (insert " " repeater)
  15076. (setq org-last-changed-timestamp
  15077. (concat (substring org-last-inserted-timestamp 0 -1)
  15078. " " repeater ">"))))
  15079. (message "Timestamp updated"))
  15080. ((equal arg '(16))
  15081. (org-insert-time-stamp (current-time) t inactive))
  15082. (t
  15083. (setq time (let ((this-command this-command))
  15084. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  15085. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  15086. nil nil (list org-end-time-was-given))))))
  15087. ;; FIXME: can we use this for something else, like computing time differences?
  15088. (defun org-get-compact-tod (s)
  15089. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15090. (let* ((t1 (match-string 1 s))
  15091. (h1 (string-to-number (match-string 2 s)))
  15092. (m1 (string-to-number (match-string 3 s)))
  15093. (t2 (and (match-end 4) (match-string 5 s)))
  15094. (h2 (and t2 (string-to-number (match-string 6 s))))
  15095. (m2 (and t2 (string-to-number (match-string 7 s))))
  15096. dh dm)
  15097. (if (not t2)
  15098. t1
  15099. (setq dh (- h2 h1) dm (- m2 m1))
  15100. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15101. (concat t1 "+" (number-to-string dh)
  15102. (and (/= 0 dm) (format ":%02d" dm)))))))
  15103. (defun org-time-stamp-inactive (&optional arg)
  15104. "Insert an inactive time stamp.
  15105. An inactive time stamp is enclosed in square brackets instead of angle
  15106. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15107. does not link to the calendar and cannot be changed with the S-cursor keys.
  15108. So these are more for recording a certain time/date."
  15109. (interactive "P")
  15110. (org-time-stamp arg 'inactive))
  15111. (defvar org-date-ovl (make-overlay 1 1))
  15112. (overlay-put org-date-ovl 'face 'org-date-selected)
  15113. (org-detach-overlay org-date-ovl)
  15114. (defvar org-ans1) ; dynamically scoped parameter
  15115. (defvar org-ans2) ; dynamically scoped parameter
  15116. (defvar org-plain-time-of-day-regexp) ; defined below
  15117. (defvar org-overriding-default-time nil) ; dynamically scoped
  15118. (defvar org-read-date-overlay nil)
  15119. (defvar org-dcst nil) ; dynamically scoped
  15120. (defvar org-read-date-history nil)
  15121. (defvar org-read-date-final-answer nil)
  15122. (defvar org-read-date-analyze-futurep nil)
  15123. (defvar org-read-date-analyze-forced-year nil)
  15124. (defvar org-read-date-inactive)
  15125. (defvar org-read-date-minibuffer-local-map
  15126. (let* ((map (make-sparse-keymap)))
  15127. (set-keymap-parent map minibuffer-local-map)
  15128. (org-defkey map (kbd ".")
  15129. (lambda () (interactive)
  15130. ;; Are we at the beginning of the prompt?
  15131. (if (looking-back "^[^:]+: ")
  15132. (org-eval-in-calendar '(calendar-goto-today))
  15133. (insert "."))))
  15134. (org-defkey map (kbd "C-.")
  15135. (lambda () (interactive)
  15136. (org-eval-in-calendar '(calendar-goto-today))))
  15137. (org-defkey map [(meta shift left)]
  15138. (lambda () (interactive)
  15139. (org-eval-in-calendar '(calendar-backward-month 1))))
  15140. (org-defkey map [(meta shift right)]
  15141. (lambda () (interactive)
  15142. (org-eval-in-calendar '(calendar-forward-month 1))))
  15143. (org-defkey map [(meta shift up)]
  15144. (lambda () (interactive)
  15145. (org-eval-in-calendar '(calendar-backward-year 1))))
  15146. (org-defkey map [(meta shift down)]
  15147. (lambda () (interactive)
  15148. (org-eval-in-calendar '(calendar-forward-year 1))))
  15149. (org-defkey map [?\e (shift left)]
  15150. (lambda () (interactive)
  15151. (org-eval-in-calendar '(calendar-backward-month 1))))
  15152. (org-defkey map [?\e (shift right)]
  15153. (lambda () (interactive)
  15154. (org-eval-in-calendar '(calendar-forward-month 1))))
  15155. (org-defkey map [?\e (shift up)]
  15156. (lambda () (interactive)
  15157. (org-eval-in-calendar '(calendar-backward-year 1))))
  15158. (org-defkey map [?\e (shift down)]
  15159. (lambda () (interactive)
  15160. (org-eval-in-calendar '(calendar-forward-year 1))))
  15161. (org-defkey map [(shift up)]
  15162. (lambda () (interactive)
  15163. (org-eval-in-calendar '(calendar-backward-week 1))))
  15164. (org-defkey map [(shift down)]
  15165. (lambda () (interactive)
  15166. (org-eval-in-calendar '(calendar-forward-week 1))))
  15167. (org-defkey map [(shift left)]
  15168. (lambda () (interactive)
  15169. (org-eval-in-calendar '(calendar-backward-day 1))))
  15170. (org-defkey map [(shift right)]
  15171. (lambda () (interactive)
  15172. (org-eval-in-calendar '(calendar-forward-day 1))))
  15173. (org-defkey map "!"
  15174. (lambda () (interactive)
  15175. (org-eval-in-calendar '(diary-view-entries))
  15176. (message "")))
  15177. (org-defkey map ">"
  15178. (lambda () (interactive)
  15179. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15180. (org-defkey map "<"
  15181. (lambda () (interactive)
  15182. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15183. (org-defkey map "\C-v"
  15184. (lambda () (interactive)
  15185. (org-eval-in-calendar
  15186. '(calendar-scroll-left-three-months 1))))
  15187. (org-defkey map "\M-v"
  15188. (lambda () (interactive)
  15189. (org-eval-in-calendar
  15190. '(calendar-scroll-right-three-months 1))))
  15191. map)
  15192. "Keymap for minibuffer commands when using `org-read-date'.")
  15193. (defvar org-def)
  15194. (defvar org-defdecode)
  15195. (defvar org-with-time)
  15196. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15197. default-time default-input inactive)
  15198. "Read a date, possibly a time, and make things smooth for the user.
  15199. The prompt will suggest to enter an ISO date, but you can also enter anything
  15200. which will at least partially be understood by `parse-time-string'.
  15201. Unrecognized parts of the date will default to the current day, month, year,
  15202. hour and minute. If this command is called to replace a timestamp at point,
  15203. or to enter the second timestamp of a range, the default time is taken
  15204. from the existing stamp. Furthermore, the command prefers the future,
  15205. so if you are giving a date where the year is not given, and the day-month
  15206. combination is already past in the current year, it will assume you
  15207. mean next year. For details, see the manual. A few examples:
  15208. 3-2-5 --> 2003-02-05
  15209. feb 15 --> currentyear-02-15
  15210. 2/15 --> currentyear-02-15
  15211. sep 12 9 --> 2009-09-12
  15212. 12:45 --> today 12:45
  15213. 22 sept 0:34 --> currentyear-09-22 0:34
  15214. 12 --> currentyear-currentmonth-12
  15215. Fri --> nearest Friday after today
  15216. -Tue --> last Tuesday
  15217. etc.
  15218. Furthermore you can specify a relative date by giving, as the *first* thing
  15219. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15220. change in days weeks, months, years.
  15221. With a single plus or minus, the date is relative to today. With a double
  15222. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15223. +4d --> four days from today
  15224. +4 --> same as above
  15225. +2w --> two weeks from today
  15226. ++5 --> five days from default date
  15227. The function understands only English month and weekday abbreviations.
  15228. While prompting, a calendar is popped up - you can also select the
  15229. date with the mouse (button 1). The calendar shows a period of three
  15230. months. To scroll it to other months, use the keys `>' and `<'.
  15231. If you don't like the calendar, turn it off with
  15232. \(setq org-read-date-popup-calendar nil)
  15233. With optional argument TO-TIME, the date will immediately be converted
  15234. to an internal time.
  15235. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15236. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15237. still enter a time, and this function will inform the calling routine
  15238. about this change. The calling routine may then choose to change the
  15239. format used to insert the time stamp into the buffer to include the time.
  15240. With optional argument FROM-STRING, read from this string instead from
  15241. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15242. the time/date that is used for everything that is not specified by the
  15243. user."
  15244. (require 'parse-time)
  15245. (let* ((org-time-stamp-rounding-minutes
  15246. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15247. (org-dcst org-display-custom-times)
  15248. (ct (org-current-time))
  15249. (org-def (or org-overriding-default-time default-time ct))
  15250. (org-defdecode (decode-time org-def))
  15251. (dummy (progn
  15252. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15253. (setcar (nthcdr 2 org-defdecode) -1)
  15254. (setcar (nthcdr 1 org-defdecode) 59)
  15255. (setq org-def (apply 'encode-time org-defdecode)
  15256. org-defdecode (decode-time org-def)))))
  15257. (cur-frame (selected-frame))
  15258. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15259. (calendar-frame-setup nil)
  15260. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15261. (calendar-move-hook nil)
  15262. (calendar-view-diary-initially-flag nil)
  15263. (calendar-view-holidays-initially-flag nil)
  15264. (timestr (format-time-string
  15265. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15266. (prompt (concat (if prompt (concat prompt " ") "")
  15267. (format "Date+time [%s]: " timestr)))
  15268. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15269. (cond
  15270. (from-string (setq ans from-string))
  15271. (org-read-date-popup-calendar
  15272. (save-excursion
  15273. (save-window-excursion
  15274. (calendar)
  15275. (when (eq calendar-setup 'calendar-only)
  15276. (setq cal-frame
  15277. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15278. (select-frame cal-frame))
  15279. (org-eval-in-calendar '(setq cursor-type nil) t)
  15280. (unwind-protect
  15281. (progn
  15282. (calendar-forward-day (- (time-to-days org-def)
  15283. (calendar-absolute-from-gregorian
  15284. (calendar-current-date))))
  15285. (org-eval-in-calendar nil t)
  15286. (let* ((old-map (current-local-map))
  15287. (map (copy-keymap calendar-mode-map))
  15288. (minibuffer-local-map
  15289. (copy-keymap org-read-date-minibuffer-local-map)))
  15290. (org-defkey map (kbd "RET") 'org-calendar-select)
  15291. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15292. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15293. (unwind-protect
  15294. (progn
  15295. (use-local-map map)
  15296. (setq org-read-date-inactive inactive)
  15297. (add-hook 'post-command-hook 'org-read-date-display)
  15298. (setq org-ans0 (read-string prompt default-input
  15299. 'org-read-date-history nil))
  15300. ;; org-ans0: from prompt
  15301. ;; org-ans1: from mouse click
  15302. ;; org-ans2: from calendar motion
  15303. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15304. (remove-hook 'post-command-hook 'org-read-date-display)
  15305. (use-local-map old-map)
  15306. (when org-read-date-overlay
  15307. (delete-overlay org-read-date-overlay)
  15308. (setq org-read-date-overlay nil)))))
  15309. (bury-buffer "*Calendar*")
  15310. (when cal-frame
  15311. (delete-frame cal-frame)
  15312. (select-frame-set-input-focus cur-frame))))))
  15313. (t ; Naked prompt only
  15314. (unwind-protect
  15315. (setq ans (read-string prompt default-input
  15316. 'org-read-date-history timestr))
  15317. (when org-read-date-overlay
  15318. (delete-overlay org-read-date-overlay)
  15319. (setq org-read-date-overlay nil)))))
  15320. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15321. (when org-read-date-analyze-forced-year
  15322. (message "Year was forced into %s"
  15323. (if org-read-date-force-compatible-dates
  15324. "compatible range (1970-2037)"
  15325. "range representable on this machine"))
  15326. (ding))
  15327. ;; One round trip to get rid of 34th of August and stuff like that....
  15328. (setq final (decode-time (apply 'encode-time final)))
  15329. (setq org-read-date-final-answer ans)
  15330. (if to-time
  15331. (apply 'encode-time final)
  15332. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15333. (format "%04d-%02d-%02d %02d:%02d"
  15334. (nth 5 final) (nth 4 final) (nth 3 final)
  15335. (nth 2 final) (nth 1 final))
  15336. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15337. (defun org-read-date-display ()
  15338. "Display the current date prompt interpretation in the minibuffer."
  15339. (when org-read-date-display-live
  15340. (when org-read-date-overlay
  15341. (delete-overlay org-read-date-overlay))
  15342. (when (minibufferp (current-buffer))
  15343. (save-excursion
  15344. (end-of-line 1)
  15345. (while (not (equal (buffer-substring
  15346. (max (point-min) (- (point) 4)) (point))
  15347. " "))
  15348. (insert " ")))
  15349. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15350. " " (or org-ans1 org-ans2)))
  15351. (org-end-time-was-given nil)
  15352. (f (org-read-date-analyze ans org-def org-defdecode))
  15353. (fmts (if org-dcst
  15354. org-time-stamp-custom-formats
  15355. org-time-stamp-formats))
  15356. (fmt (if (or org-with-time
  15357. (and (boundp 'org-time-was-given) org-time-was-given))
  15358. (cdr fmts)
  15359. (car fmts)))
  15360. (txt (format-time-string fmt (apply 'encode-time f)))
  15361. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15362. (txt (concat "=> " txt)))
  15363. (when (and org-end-time-was-given
  15364. (string-match org-plain-time-of-day-regexp txt))
  15365. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15366. org-end-time-was-given
  15367. (substring txt (match-end 0)))))
  15368. (when org-read-date-analyze-futurep
  15369. (setq txt (concat txt " (=>F)")))
  15370. (setq org-read-date-overlay
  15371. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15372. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15373. (defun org-read-date-analyze (ans org-def org-defdecode)
  15374. "Analyze the combined answer of the date prompt."
  15375. ;; FIXME: cleanup and comment
  15376. (let ((nowdecode (decode-time (current-time)))
  15377. delta deltan deltaw deltadef year month day
  15378. hour minute second wday pm h2 m2 tl wday1
  15379. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15380. (setq org-read-date-analyze-futurep nil
  15381. org-read-date-analyze-forced-year nil)
  15382. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15383. (setq ans "+0"))
  15384. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15385. (setq ans (replace-match "" t t ans)
  15386. deltan (car delta)
  15387. deltaw (nth 1 delta)
  15388. deltadef (nth 2 delta)))
  15389. ;; Check if there is an iso week date in there. If yes, store the
  15390. ;; info and postpone interpreting it until the rest of the parsing
  15391. ;; is done.
  15392. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15393. (setq iso-year (if (match-end 1)
  15394. (org-small-year-to-year
  15395. (string-to-number (match-string 1 ans))))
  15396. iso-weekday (if (match-end 3)
  15397. (string-to-number (match-string 3 ans)))
  15398. iso-week (string-to-number (match-string 2 ans)))
  15399. (setq ans (replace-match "" t t ans)))
  15400. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15401. (when (string-match
  15402. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15403. (setq year (if (match-end 2)
  15404. (string-to-number (match-string 2 ans))
  15405. (progn (setq kill-year t)
  15406. (string-to-number (format-time-string "%Y"))))
  15407. month (string-to-number (match-string 3 ans))
  15408. day (string-to-number (match-string 4 ans)))
  15409. (if (< year 100) (setq year (+ 2000 year)))
  15410. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15411. t nil ans)))
  15412. ;; Help matching dotted european dates
  15413. (when (string-match
  15414. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15415. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15416. (setq kill-year t)
  15417. (string-to-number (format-time-string "%Y")))
  15418. day (string-to-number (match-string 1 ans))
  15419. month (string-to-number (match-string 2 ans))
  15420. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15421. t nil ans)))
  15422. ;; Help matching american dates, like 5/30 or 5/30/7
  15423. (when (string-match
  15424. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15425. (setq year (if (match-end 4)
  15426. (string-to-number (match-string 4 ans))
  15427. (progn (setq kill-year t)
  15428. (string-to-number (format-time-string "%Y"))))
  15429. month (string-to-number (match-string 1 ans))
  15430. day (string-to-number (match-string 2 ans)))
  15431. (if (< year 100) (setq year (+ 2000 year)))
  15432. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15433. t nil ans)))
  15434. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15435. ;; If there is a time with am/pm, and *no* time without it, we convert
  15436. ;; so that matching will be successful.
  15437. (loop for i from 1 to 2 do ; twice, for end time as well
  15438. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15439. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15440. (setq hour (string-to-number (match-string 1 ans))
  15441. minute (if (match-end 3)
  15442. (string-to-number (match-string 3 ans))
  15443. 0)
  15444. pm (equal ?p
  15445. (string-to-char (downcase (match-string 4 ans)))))
  15446. (if (and (= hour 12) (not pm))
  15447. (setq hour 0)
  15448. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15449. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15450. t t ans))))
  15451. ;; Check if a time range is given as a duration
  15452. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15453. (setq hour (string-to-number (match-string 1 ans))
  15454. h2 (+ hour (string-to-number (match-string 3 ans)))
  15455. minute (string-to-number (match-string 2 ans))
  15456. m2 (+ minute (if (match-end 5) (string-to-number
  15457. (match-string 5 ans))0)))
  15458. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15459. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15460. t t ans)))
  15461. ;; Check if there is a time range
  15462. (when (boundp 'org-end-time-was-given)
  15463. (setq org-time-was-given nil)
  15464. (when (and (string-match org-plain-time-of-day-regexp ans)
  15465. (match-end 8))
  15466. (setq org-end-time-was-given (match-string 8 ans))
  15467. (setq ans (concat (substring ans 0 (match-beginning 7))
  15468. (substring ans (match-end 7))))))
  15469. (setq tl (parse-time-string ans)
  15470. day (or (nth 3 tl) (nth 3 org-defdecode))
  15471. month (or (nth 4 tl)
  15472. (if (and org-read-date-prefer-future
  15473. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15474. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15475. (nth 4 org-defdecode)))
  15476. year (or (and (not kill-year) (nth 5 tl))
  15477. (if (and org-read-date-prefer-future
  15478. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15479. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15480. (nth 5 org-defdecode)))
  15481. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15482. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15483. second (or (nth 0 tl) 0)
  15484. wday (nth 6 tl))
  15485. (when (and (eq org-read-date-prefer-future 'time)
  15486. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15487. (equal day (nth 3 nowdecode))
  15488. (equal month (nth 4 nowdecode))
  15489. (equal year (nth 5 nowdecode))
  15490. (nth 2 tl)
  15491. (or (< (nth 2 tl) (nth 2 nowdecode))
  15492. (and (= (nth 2 tl) (nth 2 nowdecode))
  15493. (nth 1 tl)
  15494. (< (nth 1 tl) (nth 1 nowdecode)))))
  15495. (setq day (1+ day)
  15496. futurep t))
  15497. ;; Special date definitions below
  15498. (cond
  15499. (iso-week
  15500. ;; There was an iso week
  15501. (require 'cal-iso)
  15502. (setq futurep nil)
  15503. (setq year (or iso-year year)
  15504. day (or iso-weekday wday 1)
  15505. wday nil ; to make sure that the trigger below does not match
  15506. iso-date (calendar-gregorian-from-absolute
  15507. (calendar-absolute-from-iso
  15508. (list iso-week day year))))
  15509. ; FIXME: Should we also push ISO weeks into the future?
  15510. ; (when (and org-read-date-prefer-future
  15511. ; (not iso-year)
  15512. ; (< (calendar-absolute-from-gregorian iso-date)
  15513. ; (time-to-days (current-time))))
  15514. ; (setq year (1+ year)
  15515. ; iso-date (calendar-gregorian-from-absolute
  15516. ; (calendar-absolute-from-iso
  15517. ; (list iso-week day year)))))
  15518. (setq month (car iso-date)
  15519. year (nth 2 iso-date)
  15520. day (nth 1 iso-date)))
  15521. (deltan
  15522. (setq futurep nil)
  15523. (unless deltadef
  15524. (let ((now (decode-time (current-time))))
  15525. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15526. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15527. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15528. ((equal deltaw "m") (setq month (+ month deltan)))
  15529. ((equal deltaw "y") (setq year (+ year deltan)))))
  15530. ((and wday (not (nth 3 tl)))
  15531. ;; Weekday was given, but no day, so pick that day in the week
  15532. ;; on or after the derived date.
  15533. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15534. (unless (equal wday wday1)
  15535. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15536. (if (and (boundp 'org-time-was-given)
  15537. (nth 2 tl))
  15538. (setq org-time-was-given t))
  15539. (if (< year 100) (setq year (+ 2000 year)))
  15540. ;; Check of the date is representable
  15541. (if org-read-date-force-compatible-dates
  15542. (progn
  15543. (if (< year 1970)
  15544. (setq year 1970 org-read-date-analyze-forced-year t))
  15545. (if (> year 2037)
  15546. (setq year 2037 org-read-date-analyze-forced-year t)))
  15547. (condition-case nil
  15548. (ignore (encode-time second minute hour day month year))
  15549. (error
  15550. (setq year (nth 5 org-defdecode))
  15551. (setq org-read-date-analyze-forced-year t))))
  15552. (setq org-read-date-analyze-futurep futurep)
  15553. (list second minute hour day month year)))
  15554. (defvar parse-time-weekdays)
  15555. (defun org-read-date-get-relative (s today default)
  15556. "Check string S for special relative date string.
  15557. TODAY and DEFAULT are internal times, for today and for a default.
  15558. Return shift list (N what def-flag)
  15559. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15560. N is the number of WHATs to shift.
  15561. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15562. the DEFAULT date rather than TODAY."
  15563. (require 'parse-time)
  15564. (when (and
  15565. (string-match
  15566. (concat
  15567. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15568. "\\([0-9]+\\)?"
  15569. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15570. "\\([ \t]\\|$\\)") s)
  15571. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15572. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15573. (string-to-char (substring (match-string 1 s) -1))
  15574. ?+))
  15575. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15576. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15577. (what (if (match-end 3) (match-string 3 s) "d"))
  15578. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15579. (date (if rel default today))
  15580. (wday (nth 6 (decode-time date)))
  15581. delta)
  15582. (if wday1
  15583. (progn
  15584. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15585. (if (= delta 0) (setq delta 7))
  15586. (if (= dir ?-)
  15587. (progn
  15588. (setq delta (- delta 7))
  15589. (if (= delta 0) (setq delta -7))))
  15590. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15591. (list delta "d" rel))
  15592. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15593. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15594. "Turn a user-specified date into the internal representation.
  15595. The internal representation needed by the calendar is (month day year).
  15596. This is a wrapper to handle the brain-dead convention in calendar that
  15597. user function argument order change dependent on argument order."
  15598. (if (boundp 'calendar-date-style)
  15599. (cond
  15600. ((eq calendar-date-style 'american)
  15601. (list arg1 arg2 arg3))
  15602. ((eq calendar-date-style 'european)
  15603. (list arg2 arg1 arg3))
  15604. ((eq calendar-date-style 'iso)
  15605. (list arg2 arg3 arg1)))
  15606. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15607. (if (org-bound-and-true-p european-calendar-style)
  15608. (list arg2 arg1 arg3)
  15609. (list arg1 arg2 arg3)))))
  15610. (defun org-eval-in-calendar (form &optional keepdate)
  15611. "Eval FORM in the calendar window and return to current window.
  15612. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15613. otherwise stick to the current value of `org-ans2'."
  15614. (let ((sf (selected-frame))
  15615. (sw (selected-window)))
  15616. (select-window (get-buffer-window "*Calendar*" t))
  15617. (eval form)
  15618. (when (and (not keepdate) (calendar-cursor-to-date))
  15619. (let* ((date (calendar-cursor-to-date))
  15620. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15621. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15622. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15623. (select-window sw)
  15624. (org-select-frame-set-input-focus sf)))
  15625. (defun org-calendar-select ()
  15626. "Return to `org-read-date' with the date currently selected.
  15627. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15628. (interactive)
  15629. (when (calendar-cursor-to-date)
  15630. (let* ((date (calendar-cursor-to-date))
  15631. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15632. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15633. (if (active-minibuffer-window) (exit-minibuffer))))
  15634. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15635. "Insert a date stamp for the date given by the internal TIME.
  15636. WITH-HM means use the stamp format that includes the time of the day.
  15637. INACTIVE means use square brackets instead of angular ones, so that the
  15638. stamp will not contribute to the agenda.
  15639. PRE and POST are optional strings to be inserted before and after the
  15640. stamp.
  15641. The command returns the inserted time stamp."
  15642. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15643. stamp)
  15644. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15645. (insert-before-markers (or pre ""))
  15646. (when (listp extra)
  15647. (setq extra (car extra))
  15648. (if (and (stringp extra)
  15649. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15650. (setq extra (format "-%02d:%02d"
  15651. (string-to-number (match-string 1 extra))
  15652. (string-to-number (match-string 2 extra))))
  15653. (setq extra nil)))
  15654. (when extra
  15655. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15656. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15657. (insert-before-markers (or post ""))
  15658. (setq org-last-inserted-timestamp stamp)))
  15659. (defun org-toggle-time-stamp-overlays ()
  15660. "Toggle the use of custom time stamp formats."
  15661. (interactive)
  15662. (setq org-display-custom-times (not org-display-custom-times))
  15663. (unless org-display-custom-times
  15664. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15665. (while (setq p (next-single-property-change p 'display))
  15666. (if (and (get-text-property p 'display)
  15667. (eq (get-text-property p 'face) 'org-date))
  15668. (remove-text-properties
  15669. p (setq p (next-single-property-change p 'display))
  15670. '(display t))))
  15671. (set-buffer-modified-p bmp)))
  15672. (if (featurep 'xemacs)
  15673. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15674. (org-restart-font-lock)
  15675. (setq org-table-may-need-update t)
  15676. (if org-display-custom-times
  15677. (message "Time stamps are overlaid with custom format")
  15678. (message "Time stamp overlays removed")))
  15679. (defun org-display-custom-time (beg end)
  15680. "Overlay modified time stamp format over timestamp between BEG and END."
  15681. (let* ((ts (buffer-substring beg end))
  15682. t1 w1 with-hm tf time str w2 (off 0))
  15683. (save-match-data
  15684. (setq t1 (org-parse-time-string ts t))
  15685. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15686. (setq off (- (match-end 0) (match-beginning 0)))))
  15687. (setq end (- end off))
  15688. (setq w1 (- end beg)
  15689. with-hm (and (nth 1 t1) (nth 2 t1))
  15690. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15691. time (org-fix-decoded-time t1)
  15692. str (org-add-props
  15693. (format-time-string
  15694. (substring tf 1 -1) (apply 'encode-time time))
  15695. nil 'mouse-face 'highlight)
  15696. w2 (length str))
  15697. (if (not (= w2 w1))
  15698. (add-text-properties (1+ beg) (+ 2 beg)
  15699. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15700. (if (featurep 'xemacs)
  15701. (progn
  15702. (put-text-property beg end 'invisible t)
  15703. (put-text-property beg end 'end-glyph (make-glyph str)))
  15704. (put-text-property beg end 'display str))))
  15705. (defun org-fix-decoded-time (time)
  15706. "Set 0 instead of nil for the first 6 elements of time.
  15707. Don't touch the rest."
  15708. (let ((n 0))
  15709. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15710. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15711. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15712. "Difference between TIMESTAMP-STRING and now in days.
  15713. If SECONDS is non-nil, return the difference in seconds."
  15714. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15715. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15716. (funcall fdiff (current-time)))))
  15717. (defun org-deadline-close (timestamp-string &optional ndays)
  15718. "Is the time in TIMESTAMP-STRING close to the current date?"
  15719. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15720. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15721. (not (org-entry-is-done-p))))
  15722. (defun org-get-wdays (ts &optional delay zero-delay)
  15723. "Get the deadline lead time appropriate for timestring TS.
  15724. When DELAY is non-nil, get the delay time for scheduled items
  15725. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15726. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15727. don't try to find the delay cookie in the scheduled timestamp."
  15728. (let ((tv (if delay org-scheduled-delay-days
  15729. org-deadline-warning-days)))
  15730. (cond
  15731. ((or (and delay (< tv 0))
  15732. (and delay zero-delay (<= tv 0))
  15733. (and (not delay) (<= tv 0)))
  15734. ;; Enforce this value no matter what
  15735. (- tv))
  15736. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15737. ;; lead time is specified.
  15738. (floor (* (string-to-number (match-string 1 ts))
  15739. (cdr (assoc (match-string 2 ts)
  15740. '(("d" . 1) ("w" . 7)
  15741. ("m" . 30.4) ("y" . 365.25)
  15742. ("h" . 0.041667)))))))
  15743. ;; go for the default.
  15744. (t tv))))
  15745. (defun org-calendar-select-mouse (ev)
  15746. "Return to `org-read-date' with the date currently selected.
  15747. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15748. (interactive "e")
  15749. (mouse-set-point ev)
  15750. (when (calendar-cursor-to-date)
  15751. (let* ((date (calendar-cursor-to-date))
  15752. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15753. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15754. (if (active-minibuffer-window) (exit-minibuffer))))
  15755. (defun org-check-deadlines (ndays)
  15756. "Check if there are any deadlines due or past due.
  15757. A deadline is considered due if it happens within `org-deadline-warning-days'
  15758. days from today's date. If the deadline appears in an entry marked DONE,
  15759. it is not shown. The prefix arg NDAYS can be used to test that many
  15760. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15761. (interactive "P")
  15762. (let* ((org-warn-days
  15763. (cond
  15764. ((equal ndays '(4)) 100000)
  15765. (ndays (prefix-numeric-value ndays))
  15766. (t (abs org-deadline-warning-days))))
  15767. (case-fold-search nil)
  15768. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15769. (callback
  15770. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15771. (message "%d deadlines past-due or due within %d days"
  15772. (org-occur regexp nil callback)
  15773. org-warn-days)))
  15774. (defsubst org-re-timestamp (type)
  15775. "Return a regexp for timestamp TYPE.
  15776. Allowed values for TYPE are:
  15777. all: all timestamps
  15778. active: only active timestamps (<...>)
  15779. inactive: only inactive timestamps ([...])
  15780. scheduled: only scheduled timestamps
  15781. deadline: only deadline timestamps
  15782. closed: only closed time-stamps
  15783. When TYPE is nil, fall back on returning a regexp that matches
  15784. both scheduled and deadline timestamps."
  15785. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15786. ((eq type 'active) org-ts-regexp)
  15787. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15788. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15789. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15790. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15791. ((eq type 'scheduled-or-deadline)
  15792. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15793. (defun org-check-before-date (date)
  15794. "Check if there are deadlines or scheduled entries before DATE."
  15795. (interactive (list (org-read-date)))
  15796. (let ((case-fold-search nil)
  15797. (regexp (org-re-timestamp org-ts-type))
  15798. (callback
  15799. (lambda () (time-less-p
  15800. (org-time-string-to-time (match-string 1))
  15801. (org-time-string-to-time date)))))
  15802. (message "%d entries before %s"
  15803. (org-occur regexp nil callback) date)))
  15804. (defun org-check-after-date (date)
  15805. "Check if there are deadlines or scheduled entries after DATE."
  15806. (interactive (list (org-read-date)))
  15807. (let ((case-fold-search nil)
  15808. (regexp (org-re-timestamp org-ts-type))
  15809. (callback
  15810. (lambda () (not
  15811. (time-less-p
  15812. (org-time-string-to-time (match-string 1))
  15813. (org-time-string-to-time date))))))
  15814. (message "%d entries after %s"
  15815. (org-occur regexp nil callback) date)))
  15816. (defun org-check-dates-range (start-date end-date)
  15817. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15818. (interactive (list (org-read-date nil nil nil "Range starts")
  15819. (org-read-date nil nil nil "Range end")))
  15820. (let ((case-fold-search nil)
  15821. (regexp (org-re-timestamp org-ts-type))
  15822. (callback
  15823. (lambda ()
  15824. (let ((match (match-string 1)))
  15825. (and
  15826. (not (time-less-p
  15827. (org-time-string-to-time match)
  15828. (org-time-string-to-time start-date)))
  15829. (time-less-p
  15830. (org-time-string-to-time match)
  15831. (org-time-string-to-time end-date)))))))
  15832. (message "%d entries between %s and %s"
  15833. (org-occur regexp nil callback) start-date end-date)))
  15834. (defun org-evaluate-time-range (&optional to-buffer)
  15835. "Evaluate a time range by computing the difference between start and end.
  15836. Normally the result is just printed in the echo area, but with prefix arg
  15837. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15838. If the time range is actually in a table, the result is inserted into the
  15839. next column.
  15840. For time difference computation, a year is assumed to be exactly 365
  15841. days in order to avoid rounding problems."
  15842. (interactive "P")
  15843. (or
  15844. (org-clock-update-time-maybe)
  15845. (save-excursion
  15846. (unless (org-at-date-range-p t)
  15847. (goto-char (point-at-bol))
  15848. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15849. (if (not (org-at-date-range-p t))
  15850. (user-error "Not at a time-stamp range, and none found in current line")))
  15851. (let* ((ts1 (match-string 1))
  15852. (ts2 (match-string 2))
  15853. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15854. (match-end (match-end 0))
  15855. (time1 (org-time-string-to-time ts1))
  15856. (time2 (org-time-string-to-time ts2))
  15857. (t1 (org-float-time time1))
  15858. (t2 (org-float-time time2))
  15859. (diff (abs (- t2 t1)))
  15860. (negative (< (- t2 t1) 0))
  15861. ;; (ys (floor (* 365 24 60 60)))
  15862. (ds (* 24 60 60))
  15863. (hs (* 60 60))
  15864. (fy "%dy %dd %02d:%02d")
  15865. (fy1 "%dy %dd")
  15866. (fd "%dd %02d:%02d")
  15867. (fd1 "%dd")
  15868. (fh "%02d:%02d")
  15869. y d h m align)
  15870. (if havetime
  15871. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15872. y 0
  15873. d (floor (/ diff ds)) diff (mod diff ds)
  15874. h (floor (/ diff hs)) diff (mod diff hs)
  15875. m (floor (/ diff 60)))
  15876. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15877. y 0
  15878. d (floor (+ (/ diff ds) 0.5))
  15879. h 0 m 0))
  15880. (if (not to-buffer)
  15881. (message "%s" (org-make-tdiff-string y d h m))
  15882. (if (org-at-table-p)
  15883. (progn
  15884. (goto-char match-end)
  15885. (setq align t)
  15886. (and (looking-at " *|") (goto-char (match-end 0))))
  15887. (goto-char match-end))
  15888. (if (looking-at
  15889. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15890. (replace-match ""))
  15891. (if negative (insert " -"))
  15892. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15893. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15894. (insert " " (format fh h m))))
  15895. (if align (org-table-align))
  15896. (message "Time difference inserted")))))
  15897. (defun org-make-tdiff-string (y d h m)
  15898. (let ((fmt "")
  15899. (l nil))
  15900. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15901. l (push y l)))
  15902. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15903. l (push d l)))
  15904. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15905. l (push h l)))
  15906. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15907. l (push m l)))
  15908. (apply 'format fmt (nreverse l))))
  15909. (defun org-time-string-to-time (s &optional buffer pos)
  15910. "Convert a timestamp string into internal time."
  15911. (condition-case errdata
  15912. (apply 'encode-time (org-parse-time-string s))
  15913. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15914. s (if (not (and buffer pos))
  15915. ""
  15916. (format " at %d in buffer `%s'" pos buffer))
  15917. (cdr errdata)))))
  15918. (defun org-time-string-to-seconds (s)
  15919. "Convert a timestamp string to a number of seconds."
  15920. (org-float-time (org-time-string-to-time s)))
  15921. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15922. "Convert a time stamp to an absolute day number.
  15923. If there is a specifier for a cyclic time stamp, get the closest
  15924. date to DAYNR.
  15925. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15926. The variable `date' is bound by the calendar when this is called."
  15927. (cond
  15928. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15929. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15930. daynr
  15931. (+ daynr 1000)))
  15932. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15933. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15934. (time-to-days (current-time))) (match-string 0 s)
  15935. prefer show-all))
  15936. (t (time-to-days
  15937. (condition-case errdata
  15938. (apply 'encode-time (org-parse-time-string s))
  15939. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15940. s (if (not (and buffer pos))
  15941. ""
  15942. (format " at %d in buffer `%s'" pos buffer))
  15943. (cdr errdata))))))))
  15944. (defun org-days-to-iso-week (days)
  15945. "Return the iso week number."
  15946. (require 'cal-iso)
  15947. (car (calendar-iso-from-absolute days)))
  15948. (defun org-small-year-to-year (year)
  15949. "Convert 2-digit years into 4-digit years.
  15950. YEAR is expanded into one of the 30 next years, if possible, or
  15951. into a past one. Any year larger than 99 is returned unchanged."
  15952. (if (>= year 100) year
  15953. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  15954. (century (/ current 100))
  15955. (offset (- year (% current 100))))
  15956. (cond ((> offset 30) (+ (* (1- century) 100) year))
  15957. ((> offset -70) (+ (* century 100) year))
  15958. (t (+ (* (1+ century) 100) year))))))
  15959. (defun org-time-from-absolute (d)
  15960. "Return the time corresponding to date D.
  15961. D may be an absolute day number, or a calendar-type list (month day year)."
  15962. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15963. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15964. (defun org-calendar-holiday ()
  15965. "List of holidays, for Diary display in Org-mode."
  15966. (require 'holidays)
  15967. (let ((hl (funcall
  15968. (if (fboundp 'calendar-check-holidays)
  15969. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15970. (if hl (mapconcat 'identity hl "; "))))
  15971. (defun org-diary-sexp-entry (sexp entry date)
  15972. "Process a SEXP diary ENTRY for DATE."
  15973. (require 'diary-lib)
  15974. (let ((result (if calendar-debug-sexp
  15975. (let ((stack-trace-on-error t))
  15976. (eval (car (read-from-string sexp))))
  15977. (condition-case nil
  15978. (eval (car (read-from-string sexp)))
  15979. (error
  15980. (beep)
  15981. (message "Bad sexp at line %d in %s: %s"
  15982. (org-current-line)
  15983. (buffer-file-name) sexp)
  15984. (sleep-for 2))))))
  15985. (cond ((stringp result) (split-string result "; "))
  15986. ((and (consp result)
  15987. (not (consp (cdr result)))
  15988. (stringp (cdr result))) (cdr result))
  15989. ((and (consp result)
  15990. (stringp (car result))) result)
  15991. (result entry))))
  15992. (defun org-diary-to-ical-string (frombuf)
  15993. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15994. This uses the icalendar.el library."
  15995. (let* ((tmpdir (if (featurep 'xemacs)
  15996. (temp-directory)
  15997. temporary-file-directory))
  15998. (tmpfile (make-temp-name
  15999. (expand-file-name "orgics" tmpdir)))
  16000. buf rtn b e)
  16001. (with-current-buffer frombuf
  16002. (icalendar-export-region (point-min) (point-max) tmpfile)
  16003. (setq buf (find-buffer-visiting tmpfile))
  16004. (set-buffer buf)
  16005. (goto-char (point-min))
  16006. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16007. (setq b (match-beginning 0)))
  16008. (goto-char (point-max))
  16009. (if (re-search-backward "^END:VEVENT" nil t)
  16010. (setq e (match-end 0)))
  16011. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16012. (kill-buffer buf)
  16013. (delete-file tmpfile)
  16014. rtn))
  16015. (defun org-closest-date (start current change prefer show-all)
  16016. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16017. When PREFER is `past', return a date that is either CURRENT or past.
  16018. When PREFER is `future', return a date that is either CURRENT or future.
  16019. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16020. ;; Make the proper lists from the dates
  16021. (catch 'exit
  16022. (let ((a1 '(("h" . hour)
  16023. ("d" . day)
  16024. ("w" . week)
  16025. ("m" . month)
  16026. ("y" . year)))
  16027. (shour (nth 2 (org-parse-time-string start)))
  16028. dn dw sday cday n1 n2 n0
  16029. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16030. (setq start (org-date-to-gregorian start)
  16031. current (org-date-to-gregorian
  16032. (if show-all
  16033. current
  16034. (time-to-days (current-time))))
  16035. sday (calendar-absolute-from-gregorian start)
  16036. cday (calendar-absolute-from-gregorian current))
  16037. (if (<= cday sday) (throw 'exit sday))
  16038. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16039. (setq dn (string-to-number (match-string 1 change))
  16040. dw (cdr (assoc (match-string 2 change) a1)))
  16041. (user-error "Invalid change specifier: %s" change))
  16042. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16043. (cond
  16044. ((eq dw 'hour)
  16045. (let ((missing-hours
  16046. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16047. dn)))
  16048. (setq n1 (if (zerop missing-hours) cday
  16049. (- cday (1+ (floor (/ missing-hours 24)))))
  16050. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16051. ((eq dw 'day)
  16052. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16053. n2 (+ n1 dn)))
  16054. ((eq dw 'year)
  16055. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16056. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16057. (setq date1 (list m d y1)
  16058. n1 (calendar-absolute-from-gregorian date1)
  16059. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16060. n2 (calendar-absolute-from-gregorian date2)))
  16061. ((eq dw 'month)
  16062. ;; approx number of month between the two dates
  16063. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16064. ;; How often does dn fit in there?
  16065. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16066. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16067. m (+ m nm)
  16068. ny (floor (/ m 12))
  16069. y (+ y ny)
  16070. m (- m (* ny 12)))
  16071. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16072. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16073. (setq m2 (+ m dn) y2 y)
  16074. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16075. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16076. (while (<= n2 cday)
  16077. (setq n1 n2 m m2 y y2)
  16078. (setq m2 (+ m dn) y2 y)
  16079. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16080. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16081. ;; Make sure n1 is the earlier date
  16082. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16083. (if show-all
  16084. (cond
  16085. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16086. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16087. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16088. (cond
  16089. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16090. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16091. (t (if (= cday n1) n1 n2)))))))
  16092. (defun org-date-to-gregorian (date)
  16093. "Turn any specification of DATE into a Gregorian date for the calendar."
  16094. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16095. ((and (listp date) (= (length date) 3)) date)
  16096. ((stringp date)
  16097. (setq date (org-parse-time-string date))
  16098. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16099. ((listp date)
  16100. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16101. (defun org-parse-time-string (s &optional nodefault)
  16102. "Parse the standard Org-mode time string.
  16103. This should be a lot faster than the normal `parse-time-string'.
  16104. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16105. hour and minute fields will be nil if not given."
  16106. (cond ((string-match org-ts-regexp0 s)
  16107. (list 0
  16108. (if (or (match-beginning 8) (not nodefault))
  16109. (string-to-number (or (match-string 8 s) "0")))
  16110. (if (or (match-beginning 7) (not nodefault))
  16111. (string-to-number (or (match-string 7 s) "0")))
  16112. (string-to-number (match-string 4 s))
  16113. (string-to-number (match-string 3 s))
  16114. (string-to-number (match-string 2 s))
  16115. nil nil nil))
  16116. ((string-match "^<[^>]+>$" s)
  16117. (decode-time (seconds-to-time (org-matcher-time s))))
  16118. (t (error "Not a standard Org-mode time string: %s" s))))
  16119. (defun org-timestamp-up (&optional arg)
  16120. "Increase the date item at the cursor by one.
  16121. If the cursor is on the year, change the year. If it is on the month,
  16122. the day or the time, change that.
  16123. With prefix ARG, change by that many units."
  16124. (interactive "p")
  16125. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16126. (defun org-timestamp-down (&optional arg)
  16127. "Decrease the date item at the cursor by one.
  16128. If the cursor is on the year, change the year. If it is on the month,
  16129. the day or the time, change that.
  16130. With prefix ARG, change by that many units."
  16131. (interactive "p")
  16132. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16133. (defun org-timestamp-up-day (&optional arg)
  16134. "Increase the date in the time stamp by one day.
  16135. With prefix ARG, change that many days."
  16136. (interactive "p")
  16137. (if (and (not (org-at-timestamp-p t))
  16138. (org-at-heading-p))
  16139. (org-todo 'up)
  16140. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16141. (defun org-timestamp-down-day (&optional arg)
  16142. "Decrease the date in the time stamp by one day.
  16143. With prefix ARG, change that many days."
  16144. (interactive "p")
  16145. (if (and (not (org-at-timestamp-p t))
  16146. (org-at-heading-p))
  16147. (org-todo 'down)
  16148. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16149. (defun org-at-timestamp-p (&optional inactive-ok)
  16150. "Determine if the cursor is in or at a timestamp."
  16151. (interactive)
  16152. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16153. (pos (point))
  16154. (ans (or (looking-at tsr)
  16155. (save-excursion
  16156. (skip-chars-backward "^[<\n\r\t")
  16157. (if (> (point) (point-min)) (backward-char 1))
  16158. (and (looking-at tsr)
  16159. (> (- (match-end 0) pos) -1))))))
  16160. (and ans
  16161. (boundp 'org-ts-what)
  16162. (setq org-ts-what
  16163. (cond
  16164. ((= pos (match-beginning 0)) 'bracket)
  16165. ;; Point is considered to be "on the bracket" whether
  16166. ;; it's really on it or right after it.
  16167. ((= pos (1- (match-end 0))) 'bracket)
  16168. ((= pos (match-end 0)) 'after)
  16169. ((org-pos-in-match-range pos 2) 'year)
  16170. ((org-pos-in-match-range pos 3) 'month)
  16171. ((org-pos-in-match-range pos 7) 'hour)
  16172. ((org-pos-in-match-range pos 8) 'minute)
  16173. ((or (org-pos-in-match-range pos 4)
  16174. (org-pos-in-match-range pos 5)) 'day)
  16175. ((and (> pos (or (match-end 8) (match-end 5)))
  16176. (< pos (match-end 0)))
  16177. (- pos (or (match-end 8) (match-end 5))))
  16178. (t 'day))))
  16179. ans))
  16180. (defun org-toggle-timestamp-type ()
  16181. "Toggle the type (<active> or [inactive]) of a time stamp."
  16182. (interactive)
  16183. (when (org-at-timestamp-p t)
  16184. (let ((beg (match-beginning 0)) (end (match-end 0))
  16185. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16186. (save-excursion
  16187. (goto-char beg)
  16188. (while (re-search-forward "[][<>]" end t)
  16189. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16190. t t)))
  16191. (message "Timestamp is now %sactive"
  16192. (if (equal (char-after beg) ?<) "" "in")))))
  16193. (defun org-at-clock-log-p nil
  16194. "Is the cursor on the clock log line?"
  16195. (save-excursion
  16196. (move-beginning-of-line 1)
  16197. (looking-at org-clock-line-re)))
  16198. (defvar org-clock-history) ; defined in org-clock.el
  16199. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16200. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16201. "Change the date in the time stamp at point.
  16202. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16203. `year', `minute', `second'. If WHAT is not given, the cursor position
  16204. in the timestamp determines what will be changed.
  16205. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16206. (let ((origin (point)) origin-cat
  16207. with-hm inactive
  16208. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16209. org-ts-what
  16210. extra rem
  16211. ts time time0 fixnext clrgx)
  16212. (if (not (org-at-timestamp-p t))
  16213. (user-error "Not at a timestamp"))
  16214. (if (and (not what) (eq org-ts-what 'bracket))
  16215. (org-toggle-timestamp-type)
  16216. ;; Point isn't on brackets. Remember the part of the time-stamp
  16217. ;; the point was in. Indeed, size of time-stamps may change,
  16218. ;; but point must be kept in the same category nonetheless.
  16219. (setq origin-cat org-ts-what)
  16220. (if (and (not what) (not (eq org-ts-what 'day))
  16221. org-display-custom-times
  16222. (get-text-property (point) 'display)
  16223. (not (get-text-property (1- (point)) 'display)))
  16224. (setq org-ts-what 'day))
  16225. (setq org-ts-what (or what org-ts-what)
  16226. inactive (= (char-after (match-beginning 0)) ?\[)
  16227. ts (match-string 0))
  16228. (replace-match "")
  16229. (when (string-match
  16230. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16231. ts)
  16232. (setq extra (match-string 1 ts))
  16233. (if suppress-tmp-delay
  16234. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16235. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16236. (setq with-hm t))
  16237. (setq time0 (org-parse-time-string ts))
  16238. (when (and updown
  16239. (eq org-ts-what 'minute)
  16240. (not current-prefix-arg))
  16241. ;; This looks like s-up and s-down. Change by one rounding step.
  16242. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16243. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16244. (setcar (cdr time0) (+ (nth 1 time0)
  16245. (if (> n 0) (- rem) (- dm rem))))))
  16246. (setq time
  16247. (encode-time (or (car time0) 0)
  16248. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16249. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16250. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16251. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16252. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16253. (nthcdr 6 time0)))
  16254. (when (and (member org-ts-what '(hour minute))
  16255. extra
  16256. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16257. (setq extra (org-modify-ts-extra
  16258. extra
  16259. (if (eq org-ts-what 'hour) 2 5)
  16260. n dm)))
  16261. (when (integerp org-ts-what)
  16262. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16263. (if (eq what 'calendar)
  16264. (let ((cal-date (org-get-date-from-calendar)))
  16265. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16266. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16267. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16268. (setcar time0 (or (car time0) 0))
  16269. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16270. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16271. (setq time (apply 'encode-time time0))))
  16272. ;; Insert the new time-stamp, and ensure point stays in the same
  16273. ;; category as before (i.e. not after the last position in that
  16274. ;; category).
  16275. (let ((pos (point)))
  16276. ;; Stay before inserted string. `save-excursion' is of no use.
  16277. (setq org-last-changed-timestamp
  16278. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16279. (goto-char pos))
  16280. (save-match-data
  16281. (looking-at org-ts-regexp3)
  16282. (goto-char (cond
  16283. ;; `day' category ends before `hour' if any, or at
  16284. ;; the end of the day name.
  16285. ((eq origin-cat 'day)
  16286. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16287. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16288. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16289. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16290. ;; `year' and `month' have both fixed size: point
  16291. ;; couldn't have moved into another part.
  16292. (t origin))))
  16293. ;; Update clock if on a CLOCK line.
  16294. (org-clock-update-time-maybe)
  16295. ;; Maybe adjust the closest clock in `org-clock-history'
  16296. (when org-clock-adjust-closest
  16297. (if (not (and (org-at-clock-log-p)
  16298. (< 1 (length (delq nil (mapcar 'marker-position
  16299. org-clock-history))))))
  16300. (message "No clock to adjust")
  16301. (cond ((save-excursion ; fix previous clock?
  16302. (re-search-backward org-ts-regexp0 nil t)
  16303. (org-looking-back (concat org-clock-string " \\[")))
  16304. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16305. ((save-excursion ; fix next clock?
  16306. (re-search-backward org-ts-regexp0 nil t)
  16307. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16308. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16309. (save-window-excursion
  16310. ;; Find closest clock to point, adjust the previous/next one in history
  16311. (let* ((p (save-excursion (org-back-to-heading t)))
  16312. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16313. (clfixnth
  16314. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16315. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16316. (if (not clfixpos)
  16317. (message "No clock to adjust")
  16318. (save-excursion
  16319. (org-goto-marker-or-bmk clfixpos)
  16320. (org-show-subtree)
  16321. (when (re-search-forward clrgx nil t)
  16322. (goto-char (match-beginning 1))
  16323. (let (org-clock-adjust-closest)
  16324. (org-timestamp-change n org-ts-what updown))
  16325. (message "Clock adjusted in %s for heading: %s"
  16326. (file-name-nondirectory (buffer-file-name))
  16327. (org-get-heading t t)))))))))
  16328. ;; Try to recenter the calendar window, if any.
  16329. (if (and org-calendar-follow-timestamp-change
  16330. (get-buffer-window "*Calendar*" t)
  16331. (memq org-ts-what '(day month year)))
  16332. (org-recenter-calendar (time-to-days time))))))
  16333. (defun org-modify-ts-extra (s pos n dm)
  16334. "Change the different parts of the lead-time and repeat fields in timestamp."
  16335. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16336. ng h m new rem)
  16337. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16338. (cond
  16339. ((or (org-pos-in-match-range pos 2)
  16340. (org-pos-in-match-range pos 3))
  16341. (setq m (string-to-number (match-string 3 s))
  16342. h (string-to-number (match-string 2 s)))
  16343. (if (org-pos-in-match-range pos 2)
  16344. (setq h (+ h n))
  16345. (setq n (* dm (org-no-warnings (signum n))))
  16346. (when (not (= 0 (setq rem (% m dm))))
  16347. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16348. (setq m (+ m n)))
  16349. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16350. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16351. (setq h (min 24 (max 0 h)))
  16352. (setq ng 1 new (format "-%02d:%02d" h m)))
  16353. ((org-pos-in-match-range pos 6)
  16354. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16355. ((org-pos-in-match-range pos 5)
  16356. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16357. ((org-pos-in-match-range pos 9)
  16358. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16359. ((org-pos-in-match-range pos 8)
  16360. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16361. (when ng
  16362. (setq s (concat
  16363. (substring s 0 (match-beginning ng))
  16364. new
  16365. (substring s (match-end ng))))))
  16366. s))
  16367. (defun org-recenter-calendar (date)
  16368. "If the calendar is visible, recenter it to DATE."
  16369. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16370. (when cwin
  16371. (let ((calendar-move-hook nil))
  16372. (with-selected-window cwin
  16373. (calendar-goto-date (if (listp date) date
  16374. (calendar-gregorian-from-absolute date))))))))
  16375. (defun org-goto-calendar (&optional arg)
  16376. "Go to the Emacs calendar at the current date.
  16377. If there is a time stamp in the current line, go to that date.
  16378. A prefix ARG can be used to force the current date."
  16379. (interactive "P")
  16380. (let ((tsr org-ts-regexp) diff
  16381. (calendar-move-hook nil)
  16382. (calendar-view-holidays-initially-flag nil)
  16383. (calendar-view-diary-initially-flag nil))
  16384. (if (or (org-at-timestamp-p)
  16385. (save-excursion
  16386. (beginning-of-line 1)
  16387. (looking-at (concat ".*" tsr))))
  16388. (let ((d1 (time-to-days (current-time)))
  16389. (d2 (time-to-days
  16390. (org-time-string-to-time (match-string 1)))))
  16391. (setq diff (- d2 d1))))
  16392. (calendar)
  16393. (calendar-goto-today)
  16394. (if (and diff (not arg)) (calendar-forward-day diff))))
  16395. (defun org-get-date-from-calendar ()
  16396. "Return a list (month day year) of date at point in calendar."
  16397. (with-current-buffer "*Calendar*"
  16398. (save-match-data
  16399. (calendar-cursor-to-date))))
  16400. (defun org-date-from-calendar ()
  16401. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16402. If there is already a time stamp at the cursor position, update it."
  16403. (interactive)
  16404. (if (org-at-timestamp-p t)
  16405. (org-timestamp-change 0 'calendar)
  16406. (let ((cal-date (org-get-date-from-calendar)))
  16407. (org-insert-time-stamp
  16408. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16409. (defcustom org-effort-durations
  16410. `(("min" . 1)
  16411. ("h" . 60)
  16412. ("d" . ,(* 60 8))
  16413. ("w" . ,(* 60 8 5))
  16414. ("m" . ,(* 60 8 5 4))
  16415. ("y" . ,(* 60 8 5 40)))
  16416. "Conversion factor to minutes for an effort modifier.
  16417. Each entry has the form (MODIFIER . MINUTES).
  16418. In an effort string, a number followed by MODIFIER is multiplied
  16419. by the specified number of MINUTES to obtain an effort in
  16420. minutes.
  16421. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16422. effort string \"2hours\" is equivalent to 120 minutes."
  16423. :group 'org-agenda
  16424. :version "25.1"
  16425. :package-version '(Org . "8.3")
  16426. :type '(alist :key-type (string :tag "Modifier")
  16427. :value-type (number :tag "Minutes")))
  16428. (defun org-minutes-to-clocksum-string (m)
  16429. "Format number of minutes as a clocksum string.
  16430. The format is determined by `org-time-clocksum-format',
  16431. `org-time-clocksum-use-fractional' and
  16432. `org-time-clocksum-fractional-format' and
  16433. `org-time-clocksum-use-effort-durations'."
  16434. (let ((clocksum "")
  16435. (m (round m)) ; Don't allow fractions of minutes
  16436. h d w mo y fmt n)
  16437. (setq h (if org-time-clocksum-use-effort-durations
  16438. (cdr (assoc "h" org-effort-durations)) 60)
  16439. d (if org-time-clocksum-use-effort-durations
  16440. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16441. w (if org-time-clocksum-use-effort-durations
  16442. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16443. mo (if org-time-clocksum-use-effort-durations
  16444. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16445. y (if org-time-clocksum-use-effort-durations
  16446. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16447. ;; fractional format
  16448. (if org-time-clocksum-use-fractional
  16449. (cond
  16450. ;; single format string
  16451. ((stringp org-time-clocksum-fractional-format)
  16452. (format org-time-clocksum-fractional-format (/ m (float h))))
  16453. ;; choice of fractional formats for different time units
  16454. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16455. (> (/ (truncate m) (* y d h)) 0))
  16456. (format fmt (/ m (* y d (float h)))))
  16457. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16458. (> (/ (truncate m) (* mo d h)) 0))
  16459. (format fmt (/ m (* mo d (float h)))))
  16460. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16461. (> (/ (truncate m) (* w d h)) 0))
  16462. (format fmt (/ m (* w d (float h)))))
  16463. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16464. (> (/ (truncate m) (* d h)) 0))
  16465. (format fmt (/ m (* d (float h)))))
  16466. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16467. (> (/ (truncate m) h) 0))
  16468. (format fmt (/ m (float h))))
  16469. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16470. (format fmt m))
  16471. ;; fall back to smallest time unit with a format
  16472. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16473. (format fmt (/ m (float h))))
  16474. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16475. (format fmt (/ m (* d (float h)))))
  16476. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16477. (format fmt (/ m (* w d (float h)))))
  16478. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16479. (format fmt (/ m (* mo d (float h)))))
  16480. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16481. (format fmt (/ m (* y d (float h))))))
  16482. ;; standard (non-fractional) format, with single format string
  16483. (if (stringp org-time-clocksum-format)
  16484. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16485. ;; separate formats components
  16486. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16487. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16488. (plist-get org-time-clocksum-format :require-years))
  16489. (setq clocksum (concat clocksum (format fmt n))
  16490. m (- m (* n y d h))))
  16491. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16492. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16493. (plist-get org-time-clocksum-format :require-months))
  16494. (setq clocksum (concat clocksum (format fmt n))
  16495. m (- m (* n mo d h))))
  16496. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16497. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16498. (plist-get org-time-clocksum-format :require-weeks))
  16499. (setq clocksum (concat clocksum (format fmt n))
  16500. m (- m (* n w d h))))
  16501. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16502. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16503. (plist-get org-time-clocksum-format :require-days))
  16504. (setq clocksum (concat clocksum (format fmt n))
  16505. m (- m (* n d h))))
  16506. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16507. (or (> (setq n (/ (truncate m) h)) 0)
  16508. (plist-get org-time-clocksum-format :require-hours))
  16509. (setq clocksum (concat clocksum (format fmt n))
  16510. m (- m (* n h))))
  16511. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16512. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16513. (setq clocksum (concat clocksum (format fmt m))))
  16514. ;; return formatted time duration
  16515. clocksum))))
  16516. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16517. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16518. "Org mode version 8.0")
  16519. (defun org-hours-to-clocksum-string (n)
  16520. (org-minutes-to-clocksum-string (* n 60)))
  16521. (defun org-hh:mm-string-to-minutes (s)
  16522. "Convert a string H:MM to a number of minutes.
  16523. If the string is just a number, interpret it as minutes.
  16524. In fact, the first hh:mm or number in the string will be taken,
  16525. there can be extra stuff in the string.
  16526. If no number is found, the return value is 0."
  16527. (cond
  16528. ((integerp s) s)
  16529. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16530. (+ (* (string-to-number (match-string 1 s)) 60)
  16531. (string-to-number (match-string 2 s))))
  16532. ((string-match "\\([0-9]+\\)" s)
  16533. (string-to-number (match-string 1 s)))
  16534. (t 0)))
  16535. (defcustom org-image-actual-width t
  16536. "Should we use the actual width of images when inlining them?
  16537. When set to `t', always use the image width.
  16538. When set to a number, use imagemagick (when available) to set
  16539. the image's width to this value.
  16540. When set to a number in a list, try to get the width from any
  16541. #+ATTR.* keyword if it matches a width specification like
  16542. #+ATTR_HTML: :width 300px
  16543. and fall back on that number if none is found.
  16544. When set to nil, try to get the width from an #+ATTR.* keyword
  16545. and fall back on the original width if none is found.
  16546. This requires Emacs >= 24.1, build with imagemagick support."
  16547. :group 'org-appearance
  16548. :version "24.4"
  16549. :package-version '(Org . "8.0")
  16550. :type '(choice
  16551. (const :tag "Use the image width" t)
  16552. (integer :tag "Use a number of pixels")
  16553. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16554. (const :tag "Use #+ATTR* or don't resize" nil)))
  16555. (defcustom org-agenda-inhibit-startup nil
  16556. "Inhibit startup when preparing agenda buffers.
  16557. When this variable is `t', the initialization of the Org agenda
  16558. buffers is inhibited: e.g. the visibility state is not set, the
  16559. tables are not re-aligned, etc."
  16560. :type 'boolean
  16561. :version "24.3"
  16562. :group 'org-agenda)
  16563. (define-obsolete-variable-alias
  16564. 'org-agenda-ignore-drawer-properties
  16565. 'org-agenda-ignore-properties "25.1")
  16566. (defcustom org-agenda-ignore-properties nil
  16567. "Avoid updating text properties when building the agenda.
  16568. Properties are used to prepare buffers for effort estimates,
  16569. appointments, statistics and subtree-local categories.
  16570. If you don't use these in the agenda, you can add them to this
  16571. list and agenda building will be a bit faster.
  16572. The value is a list, with zero or more of the symbols `effort', `appt',
  16573. `stats' or `category'."
  16574. :type '(set :greedy t
  16575. (const effort)
  16576. (const appt)
  16577. (const stats)
  16578. (const category))
  16579. :version "25.1"
  16580. :package-version '(Org . "8.3")
  16581. :group 'org-agenda)
  16582. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16583. "Convert a duration string S to minutes.
  16584. A bare number is interpreted as minutes, modifiers can be set by
  16585. customizing `org-effort-durations' (which see).
  16586. Entries containing a colon are interpreted as H:MM by
  16587. `org-hh:mm-string-to-minutes'."
  16588. (let ((result 0)
  16589. (re (concat "\\([0-9.]+\\) *\\("
  16590. (regexp-opt (mapcar 'car org-effort-durations))
  16591. "\\)")))
  16592. (while (string-match re s)
  16593. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16594. (string-to-number (match-string 1 s))))
  16595. (setq s (replace-match "" nil t s)))
  16596. (setq result (floor result))
  16597. (incf result (org-hh:mm-string-to-minutes s))
  16598. (if output-to-string (number-to-string result) result)))
  16599. ;;;; Files
  16600. (defun org-save-all-org-buffers ()
  16601. "Save all Org-mode buffers without user confirmation."
  16602. (interactive)
  16603. (message "Saving all Org-mode buffers...")
  16604. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16605. (when (featurep 'org-id) (org-id-locations-save))
  16606. (message "Saving all Org-mode buffers... done"))
  16607. (defun org-revert-all-org-buffers ()
  16608. "Revert all Org-mode buffers.
  16609. Prompt for confirmation when there are unsaved changes.
  16610. Be sure you know what you are doing before letting this function
  16611. overwrite your changes.
  16612. This function is useful in a setup where one tracks org files
  16613. with a version control system, to revert on one machine after pulling
  16614. changes from another. I believe the procedure must be like this:
  16615. 1. M-x org-save-all-org-buffers
  16616. 2. Pull changes from the other machine, resolve conflicts
  16617. 3. M-x org-revert-all-org-buffers"
  16618. (interactive)
  16619. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16620. (user-error "Abort"))
  16621. (save-excursion
  16622. (save-window-excursion
  16623. (mapc
  16624. (lambda (b)
  16625. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16626. (with-current-buffer b buffer-file-name))
  16627. (org-pop-to-buffer-same-window b)
  16628. (revert-buffer t 'no-confirm)))
  16629. (buffer-list))
  16630. (when (and (featurep 'org-id) org-id-track-globally)
  16631. (org-id-locations-load)))))
  16632. ;;;; Agenda files
  16633. ;;;###autoload
  16634. (defun org-switchb (&optional arg)
  16635. "Switch between Org buffers.
  16636. With one prefix argument, restrict available buffers to files.
  16637. With two prefix arguments, restrict available buffers to agenda files.
  16638. Defaults to `iswitchb' for buffer name completion.
  16639. Set `org-completion-use-ido' to make it use ido instead."
  16640. (interactive "P")
  16641. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16642. ((equal arg '(16)) (org-buffer-list 'agenda))
  16643. (t (org-buffer-list))))
  16644. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16645. (org-completion-use-ido org-completion-use-ido))
  16646. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16647. (setq org-completion-use-iswitchb t))
  16648. (org-pop-to-buffer-same-window
  16649. (org-icompleting-read "Org buffer: "
  16650. (mapcar 'list (mapcar 'buffer-name blist))
  16651. nil t))))
  16652. ;;; Define some older names previously used for this functionality
  16653. ;;;###autoload
  16654. (defalias 'org-ido-switchb 'org-switchb)
  16655. ;;;###autoload
  16656. (defalias 'org-iswitchb 'org-switchb)
  16657. (defun org-buffer-list (&optional predicate exclude-tmp)
  16658. "Return a list of Org buffers.
  16659. PREDICATE can be `export', `files' or `agenda'.
  16660. export restrict the list to Export buffers.
  16661. files restrict the list to buffers visiting Org files.
  16662. agenda restrict the list to buffers visiting agenda files.
  16663. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16664. (let* ((bfn nil)
  16665. (agenda-files (and (eq predicate 'agenda)
  16666. (mapcar 'file-truename (org-agenda-files t))))
  16667. (filter
  16668. (cond
  16669. ((eq predicate 'files)
  16670. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16671. ((eq predicate 'export)
  16672. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16673. ((eq predicate 'agenda)
  16674. (lambda (b)
  16675. (with-current-buffer b
  16676. (and (derived-mode-p 'org-mode)
  16677. (setq bfn (buffer-file-name b))
  16678. (member (file-truename bfn) agenda-files)))))
  16679. (t (lambda (b) (with-current-buffer b
  16680. (or (derived-mode-p 'org-mode)
  16681. (string-match "\*Org .*Export"
  16682. (buffer-name b)))))))))
  16683. (delq nil
  16684. (mapcar
  16685. (lambda(b)
  16686. (if (and (funcall filter b)
  16687. (or (not exclude-tmp)
  16688. (not (string-match "tmp" (buffer-name b)))))
  16689. b
  16690. nil))
  16691. (buffer-list)))))
  16692. (defun org-agenda-files (&optional unrestricted archives)
  16693. "Get the list of agenda files.
  16694. Optional UNRESTRICTED means return the full list even if a restriction
  16695. is currently in place.
  16696. When ARCHIVES is t, include all archive files that are really being
  16697. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16698. `org-agenda-archives-mode' is t."
  16699. (let ((files
  16700. (cond
  16701. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16702. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16703. ((listp org-agenda-files) org-agenda-files)
  16704. (t (error "Invalid value of `org-agenda-files'")))))
  16705. (setq files (apply 'append
  16706. (mapcar (lambda (f)
  16707. (if (file-directory-p f)
  16708. (directory-files
  16709. f t org-agenda-file-regexp)
  16710. (list f)))
  16711. files)))
  16712. (when org-agenda-skip-unavailable-files
  16713. (setq files (delq nil
  16714. (mapcar (function
  16715. (lambda (file)
  16716. (and (file-readable-p file) file)))
  16717. files))))
  16718. (when (or (eq archives t)
  16719. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16720. (setq files (org-add-archive-files files)))
  16721. files))
  16722. (defun org-agenda-file-p (&optional file)
  16723. "Return non-nil, if FILE is an agenda file.
  16724. If FILE is omitted, use the file associated with the current
  16725. buffer."
  16726. (let ((fname (or file (buffer-file-name))))
  16727. (and fname
  16728. (member (file-truename fname)
  16729. (mapcar #'file-truename (org-agenda-files t))))))
  16730. (defun org-edit-agenda-file-list ()
  16731. "Edit the list of agenda files.
  16732. Depending on setup, this either uses customize to edit the variable
  16733. `org-agenda-files', or it visits the file that is holding the list. In the
  16734. latter case, the buffer is set up in a way that saving it automatically kills
  16735. the buffer and restores the previous window configuration."
  16736. (interactive)
  16737. (if (stringp org-agenda-files)
  16738. (let ((cw (current-window-configuration)))
  16739. (find-file org-agenda-files)
  16740. (org-set-local 'org-window-configuration cw)
  16741. (org-add-hook 'after-save-hook
  16742. (lambda ()
  16743. (set-window-configuration
  16744. (prog1 org-window-configuration
  16745. (kill-buffer (current-buffer))))
  16746. (org-install-agenda-files-menu)
  16747. (message "New agenda file list installed"))
  16748. nil 'local)
  16749. (message "%s" (substitute-command-keys
  16750. "Edit list and finish with \\[save-buffer]")))
  16751. (customize-variable 'org-agenda-files)))
  16752. (defun org-store-new-agenda-file-list (list)
  16753. "Set new value for the agenda file list and save it correctly."
  16754. (if (stringp org-agenda-files)
  16755. (let ((fe (org-read-agenda-file-list t)) b u)
  16756. (while (setq b (find-buffer-visiting org-agenda-files))
  16757. (kill-buffer b))
  16758. (with-temp-file org-agenda-files
  16759. (insert
  16760. (mapconcat
  16761. (lambda (f) ;; Keep un-expanded entries.
  16762. (if (setq u (assoc f fe))
  16763. (cdr u)
  16764. f))
  16765. list "\n")
  16766. "\n")))
  16767. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16768. (org-insert-mode-line-in-empty-file nil))
  16769. (setq org-agenda-files list)
  16770. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16771. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16772. "Read the list of agenda files from a file.
  16773. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16774. filenames, used by `org-store-new-agenda-file-list' to write back
  16775. un-expanded file names."
  16776. (when (file-directory-p org-agenda-files)
  16777. (error "`org-agenda-files' cannot be a single directory"))
  16778. (when (stringp org-agenda-files)
  16779. (with-temp-buffer
  16780. (insert-file-contents org-agenda-files)
  16781. (mapcar
  16782. (lambda (f)
  16783. (let ((e (expand-file-name (substitute-in-file-name f)
  16784. org-directory)))
  16785. (if pair-with-expansion
  16786. (cons e f)
  16787. e)))
  16788. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16789. ;;;###autoload
  16790. (defun org-cycle-agenda-files ()
  16791. "Cycle through the files in `org-agenda-files'.
  16792. If the current buffer visits an agenda file, find the next one in the list.
  16793. If the current buffer does not, find the first agenda file."
  16794. (interactive)
  16795. (let* ((fs (org-agenda-files t))
  16796. (files (append fs (list (car fs))))
  16797. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16798. file)
  16799. (unless files (user-error "No agenda files"))
  16800. (catch 'exit
  16801. (while (setq file (pop files))
  16802. (if (equal (file-truename file) tcf)
  16803. (when (car files)
  16804. (find-file (car files))
  16805. (throw 'exit t))))
  16806. (find-file (car fs)))
  16807. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16808. (defun org-agenda-file-to-front (&optional to-end)
  16809. "Move/add the current file to the top of the agenda file list.
  16810. If the file is not present in the list, it is added to the front. If it is
  16811. present, it is moved there. With optional argument TO-END, add/move to the
  16812. end of the list."
  16813. (interactive "P")
  16814. (let ((org-agenda-skip-unavailable-files nil)
  16815. (file-alist (mapcar (lambda (x)
  16816. (cons (file-truename x) x))
  16817. (org-agenda-files t)))
  16818. (ctf (file-truename
  16819. (or buffer-file-name
  16820. (user-error "Please save the current buffer to a file"))))
  16821. x had)
  16822. (setq x (assoc ctf file-alist) had x)
  16823. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16824. (if to-end
  16825. (setq file-alist (append (delq x file-alist) (list x)))
  16826. (setq file-alist (cons x (delq x file-alist))))
  16827. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16828. (org-install-agenda-files-menu)
  16829. (message "File %s to %s of agenda file list"
  16830. (if had "moved" "added") (if to-end "end" "front"))))
  16831. (defun org-remove-file (&optional file)
  16832. "Remove current file from the list of files in variable `org-agenda-files'.
  16833. These are the files which are being checked for agenda entries.
  16834. Optional argument FILE means use this file instead of the current."
  16835. (interactive)
  16836. (let* ((org-agenda-skip-unavailable-files nil)
  16837. (file (or file buffer-file-name
  16838. (user-error "Current buffer does not visit a file")))
  16839. (true-file (file-truename file))
  16840. (afile (abbreviate-file-name file))
  16841. (files (delq nil (mapcar
  16842. (lambda (x)
  16843. (if (equal true-file
  16844. (file-truename x))
  16845. nil x))
  16846. (org-agenda-files t)))))
  16847. (if (not (= (length files) (length (org-agenda-files t))))
  16848. (progn
  16849. (org-store-new-agenda-file-list files)
  16850. (org-install-agenda-files-menu)
  16851. (message "Removed from Org Agenda list: %s" afile))
  16852. (message "File was not in list: %s (not removed)" afile))))
  16853. (defun org-file-menu-entry (file)
  16854. (vector file (list 'find-file file) t))
  16855. (defun org-check-agenda-file (file)
  16856. "Make sure FILE exists. If not, ask user what to do."
  16857. (when (not (file-exists-p file))
  16858. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16859. (abbreviate-file-name file))
  16860. (let ((r (downcase (read-char-exclusive))))
  16861. (cond
  16862. ((equal r ?r)
  16863. (org-remove-file file)
  16864. (throw 'nextfile t))
  16865. (t (user-error "Abort"))))))
  16866. (defun org-get-agenda-file-buffer (file)
  16867. "Get an agenda buffer visiting FILE.
  16868. If the buffer needs to be created, add it to the list of buffers
  16869. which might be released later."
  16870. (let ((buf (org-find-base-buffer-visiting file)))
  16871. (if buf
  16872. buf ; just return it
  16873. ;; Make a new buffer and remember it
  16874. (setq buf (find-file-noselect file))
  16875. (if buf (push buf org-agenda-new-buffers))
  16876. buf)))
  16877. (defun org-release-buffers (blist)
  16878. "Release all buffers in list, asking the user for confirmation when needed.
  16879. When a buffer is unmodified, it is just killed. When modified, it is saved
  16880. \(if the user agrees) and then killed."
  16881. (let (buf file)
  16882. (while (setq buf (pop blist))
  16883. (setq file (buffer-file-name buf))
  16884. (when (and (buffer-modified-p buf)
  16885. file
  16886. (y-or-n-p (format "Save file %s? " file)))
  16887. (with-current-buffer buf (save-buffer)))
  16888. (kill-buffer buf))))
  16889. (defun org-agenda-prepare-buffers (files)
  16890. "Create buffers for all agenda files, protect archived trees and comments."
  16891. (interactive)
  16892. (let ((pa '(:org-archived t))
  16893. (pc '(:org-comment t))
  16894. (pall '(:org-archived t :org-comment t))
  16895. (inhibit-read-only t)
  16896. (org-inhibit-startup org-agenda-inhibit-startup)
  16897. (rea (concat ":" org-archive-tag ":"))
  16898. file re pos)
  16899. (setq org-tag-alist-for-agenda nil
  16900. org-tag-groups-alist-for-agenda nil)
  16901. (save-excursion
  16902. (save-restriction
  16903. (while (setq file (pop files))
  16904. (catch 'nextfile
  16905. (if (bufferp file)
  16906. (set-buffer file)
  16907. (org-check-agenda-file file)
  16908. (set-buffer (org-get-agenda-file-buffer file)))
  16909. (widen)
  16910. (org-set-regexps-and-options 'tags-only)
  16911. (setq pos (point))
  16912. (or (memq 'category org-agenda-ignore-properties)
  16913. (org-refresh-category-properties))
  16914. (or (memq 'stats org-agenda-ignore-properties)
  16915. (org-refresh-stats-properties))
  16916. (or (memq 'effort org-agenda-ignore-properties)
  16917. (org-refresh-effort-properties))
  16918. (or (memq 'appt org-agenda-ignore-properties)
  16919. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16920. (setq org-todo-keywords-for-agenda
  16921. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16922. (setq org-done-keywords-for-agenda
  16923. (append org-done-keywords-for-agenda org-done-keywords))
  16924. (setq org-todo-keyword-alist-for-agenda
  16925. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16926. (setq org-tag-alist-for-agenda
  16927. (org-uniquify
  16928. (append org-tag-alist-for-agenda
  16929. org-tag-alist
  16930. org-tag-persistent-alist)))
  16931. (if org-group-tags
  16932. (setq org-tag-groups-alist-for-agenda
  16933. (org-uniquify-alist
  16934. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16935. (org-with-silent-modifications
  16936. (save-excursion
  16937. (remove-text-properties (point-min) (point-max) pall)
  16938. (when org-agenda-skip-archived-trees
  16939. (goto-char (point-min))
  16940. (while (re-search-forward rea nil t)
  16941. (if (org-at-heading-p t)
  16942. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16943. (goto-char (point-min))
  16944. (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
  16945. (while (re-search-forward re nil t)
  16946. (when (save-match-data (org-in-commented-heading-p t))
  16947. (add-text-properties
  16948. (match-beginning 0) (org-end-of-subtree t) pc)))))
  16949. (goto-char pos)))))
  16950. (setq org-todo-keywords-for-agenda
  16951. (org-uniquify org-todo-keywords-for-agenda))
  16952. (setq org-todo-keyword-alist-for-agenda
  16953. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16954. ;;;; CDLaTeX minor mode
  16955. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16956. "Keymap for the minor `org-cdlatex-mode'.")
  16957. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16958. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16959. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16960. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16961. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  16962. (defvar org-cdlatex-texmathp-advice-is-done nil
  16963. "Flag remembering if we have applied the advice to texmathp already.")
  16964. (define-minor-mode org-cdlatex-mode
  16965. "Toggle the minor `org-cdlatex-mode'.
  16966. This mode supports entering LaTeX environment and math in LaTeX fragments
  16967. in Org-mode.
  16968. \\{org-cdlatex-mode-map}"
  16969. nil " OCDL" nil
  16970. (when org-cdlatex-mode
  16971. (require 'cdlatex)
  16972. (run-hooks 'cdlatex-mode-hook)
  16973. (cdlatex-compute-tables))
  16974. (unless org-cdlatex-texmathp-advice-is-done
  16975. (setq org-cdlatex-texmathp-advice-is-done t)
  16976. (defadvice texmathp (around org-math-always-on activate)
  16977. "Always return t in org-mode buffers.
  16978. This is because we want to insert math symbols without dollars even outside
  16979. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16980. an embedded LaTeX fragment, let texmathp do its job.
  16981. \\[org-cdlatex-mode-map]"
  16982. (interactive)
  16983. (let (p)
  16984. (cond
  16985. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16986. ((eq this-command 'cdlatex-math-symbol)
  16987. (setq ad-return-value t
  16988. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16989. (t
  16990. (let ((p (org-inside-LaTeX-fragment-p)))
  16991. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16992. (setq ad-return-value t
  16993. texmathp-why '("Org-mode embedded math" . 0))
  16994. (if p ad-do-it)))))))))
  16995. (defun turn-on-org-cdlatex ()
  16996. "Unconditionally turn on `org-cdlatex-mode'."
  16997. (org-cdlatex-mode 1))
  16998. (defun org-try-cdlatex-tab ()
  16999. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17000. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17001. - inside a LaTeX fragment, or
  17002. - after the first word in a line, where an abbreviation expansion could
  17003. insert a LaTeX environment."
  17004. (when org-cdlatex-mode
  17005. (cond
  17006. ;; Before any word on the line: No expansion possible.
  17007. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17008. ;; Just after first word on the line: Expand it. Make sure it
  17009. ;; cannot happen on headlines, though.
  17010. ((save-excursion
  17011. (skip-chars-backward "a-zA-Z0-9*")
  17012. (skip-chars-backward " \t")
  17013. (and (bolp) (not (org-at-heading-p))))
  17014. (cdlatex-tab) t)
  17015. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17016. (defun org-cdlatex-underscore-caret (&optional arg)
  17017. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17018. Revert to the normal definition outside of these fragments."
  17019. (interactive "P")
  17020. (if (org-inside-LaTeX-fragment-p)
  17021. (call-interactively 'cdlatex-sub-superscript)
  17022. (let (org-cdlatex-mode)
  17023. (call-interactively (key-binding (vector last-input-event))))))
  17024. (defun org-cdlatex-math-modify (&optional arg)
  17025. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17026. Revert to the normal definition outside of these fragments."
  17027. (interactive "P")
  17028. (if (org-inside-LaTeX-fragment-p)
  17029. (call-interactively 'cdlatex-math-modify)
  17030. (let (org-cdlatex-mode)
  17031. (call-interactively (key-binding (vector last-input-event))))))
  17032. (defun org-cdlatex-environment-indent (&optional environment item)
  17033. "Execute `cdlatex-environment' and indent the inserted environment."
  17034. (interactive)
  17035. (cdlatex-environment environment item)
  17036. (let ((element (org-element-at-point)))
  17037. (org-indent-region (org-element-property :begin element)
  17038. (org-element-property :end element))))
  17039. ;;;; LaTeX fragments
  17040. (defun org-inside-LaTeX-fragment-p ()
  17041. "Test if point is inside a LaTeX fragment.
  17042. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17043. sequence appearing also before point.
  17044. Even though the matchers for math are configurable, this function assumes
  17045. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17046. delimiters are skipped when they have been removed by customization.
  17047. The return value is nil, or a cons cell with the delimiter and the
  17048. position of this delimiter.
  17049. This function does a reasonably good job, but can locally be fooled by
  17050. for example currency specifications. For example it will assume being in
  17051. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17052. fragments that are properly closed, but during editing, we have to live
  17053. with the uncertainty caused by missing closing delimiters. This function
  17054. looks only before point, not after."
  17055. (catch 'exit
  17056. (let ((pos (point))
  17057. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17058. (lim (progn
  17059. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17060. (point)))
  17061. dd-on str (start 0) m re)
  17062. (goto-char pos)
  17063. (when dodollar
  17064. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17065. re (nth 1 (assoc "$" org-latex-regexps)))
  17066. (while (string-match re str start)
  17067. (cond
  17068. ((= (match-end 0) (length str))
  17069. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17070. ((= (match-end 0) (- (length str) 5))
  17071. (throw 'exit nil))
  17072. (t (setq start (match-end 0))))))
  17073. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17074. (goto-char pos)
  17075. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17076. (and (match-beginning 2) (throw 'exit nil))
  17077. ;; count $$
  17078. (while (re-search-backward "\\$\\$" lim t)
  17079. (setq dd-on (not dd-on)))
  17080. (goto-char pos)
  17081. (if dd-on (cons "$$" m))))))
  17082. (defun org-inside-latex-macro-p ()
  17083. "Is point inside a LaTeX macro or its arguments?"
  17084. (save-match-data
  17085. (org-in-regexp
  17086. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17087. (defvar org-latex-fragment-image-overlays nil
  17088. "List of overlays carrying the images of latex fragments.")
  17089. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17090. (defun org-remove-latex-fragment-image-overlays ()
  17091. "Remove all overlays with LaTeX fragment images in current buffer."
  17092. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  17093. (setq org-latex-fragment-image-overlays nil))
  17094. (define-obsolete-function-alias
  17095. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17096. (defun org-toggle-latex-fragment (&optional subtree)
  17097. "Preview the LaTeX fragment at point, or all locally or globally.
  17098. If the cursor is in a LaTeX fragment, create the image and overlay
  17099. it over the source code. If there is no fragment at point, display
  17100. all fragments in the current text, from one headline to the next. With
  17101. prefix SUBTREE, display all fragments in the current subtree. With a
  17102. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  17103. the cursor is before the first headline,
  17104. display all fragments in the buffer.
  17105. The images can be removed again with \\[org-toggle-latex-fragment]."
  17106. (interactive "P")
  17107. (unless (buffer-file-name (buffer-base-buffer))
  17108. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17109. (if org-latex-fragment-image-overlays
  17110. (progn (org-remove-latex-fragment-image-overlays)
  17111. (message "LaTeX fragments images removed"))
  17112. (when (display-graphic-p)
  17113. (org-remove-latex-fragment-image-overlays)
  17114. (org-with-wide-buffer
  17115. (let (beg end msg)
  17116. (cond
  17117. ((equal subtree '(16))
  17118. (setq beg (point-min) end (point-max)
  17119. msg "Creating images for buffer...%s"))
  17120. ((equal subtree '(4))
  17121. (org-back-to-heading)
  17122. (setq beg (point) end (org-end-of-subtree t)
  17123. msg "Creating images for subtree...%s"))
  17124. ((let ((context (org-element-context)))
  17125. (when (memq (org-element-type context)
  17126. '(latex-environment latex-fragment))
  17127. (setq beg (org-element-property :begin context)
  17128. end (org-element-property :end context)
  17129. msg "Creating image...%s"))))
  17130. ((org-before-first-heading-p)
  17131. (setq beg (point-min) end (point-max)
  17132. msg "Creating images for buffer...%s"))
  17133. (t
  17134. (org-back-to-heading)
  17135. (setq beg (point) end (progn (outline-next-heading) (point))
  17136. msg "Creating images for entry...%s")))
  17137. (message msg "")
  17138. (narrow-to-region beg end)
  17139. (goto-char beg)
  17140. (org-format-latex
  17141. (concat org-latex-preview-ltxpng-directory
  17142. (file-name-sans-extension
  17143. (file-name-nondirectory
  17144. (buffer-file-name (buffer-base-buffer)))))
  17145. default-directory 'overlays msg 'forbuffer
  17146. org-latex-create-formula-image-program)
  17147. (message msg "done. Use `C-c C-x C-l' to remove images."))))))
  17148. (defun org-format-latex
  17149. (prefix &optional dir overlays msg forbuffer processing-type)
  17150. "Replace LaTeX fragments with links to an image, and produce images.
  17151. Some of the options can be changed using the variable
  17152. `org-format-latex-options'."
  17153. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17154. (unless (eq processing-type 'verbatim)
  17155. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17156. (cnt 0)
  17157. checkdir-flag)
  17158. (goto-char (point-min))
  17159. (while (re-search-forward math-regexp nil t)
  17160. (unless (and overlays
  17161. (eq (get-char-property (point) 'org-overlay-type)
  17162. 'org-latex-overlay))
  17163. (let* ((context (org-element-context))
  17164. (type (org-element-type context)))
  17165. (when (memq type '(latex-environment latex-fragment))
  17166. (let ((block-type (eq type 'latex-environment))
  17167. (value (org-element-property :value context))
  17168. (beg (org-element-property :begin context))
  17169. (end (save-excursion
  17170. (goto-char (org-element-property :end context))
  17171. (skip-chars-backward " \r\t\n")
  17172. (point))))
  17173. (case processing-type
  17174. (mathjax
  17175. ;; Prepare for MathJax processing.
  17176. (if (eq (char-after beg) ?$)
  17177. (save-excursion
  17178. (delete-region beg end)
  17179. (insert "\\(" (substring value 1 -1) "\\)"))
  17180. (goto-char end)))
  17181. ((dvipng imagemagick)
  17182. ;; Process to an image.
  17183. (incf cnt)
  17184. (goto-char beg)
  17185. (let* ((face (face-at-point))
  17186. ;; Get the colors from the face at point.
  17187. (fg
  17188. (let ((color (plist-get org-format-latex-options
  17189. :foreground)))
  17190. (if (and forbuffer (eq color 'auto))
  17191. (face-attribute face :foreground nil 'default)
  17192. color)))
  17193. (bg
  17194. (let ((color (plist-get org-format-latex-options
  17195. :background)))
  17196. (if (and forbuffer (eq color 'auto))
  17197. (face-attribute face :background nil 'default)
  17198. color)))
  17199. (hash (sha1 (prin1-to-string
  17200. (list org-format-latex-header
  17201. org-latex-default-packages-alist
  17202. org-latex-packages-alist
  17203. org-format-latex-options
  17204. forbuffer value fg bg))))
  17205. (absprefix (expand-file-name prefix dir))
  17206. (linkfile (format "%s_%s.png" prefix hash))
  17207. (movefile (format "%s_%s.png" absprefix hash))
  17208. (sep (and block-type "\n\n"))
  17209. (link (concat sep "[[file:" linkfile "]]" sep))
  17210. (options
  17211. (org-combine-plists
  17212. org-format-latex-options
  17213. `(:foreground ,fg :background ,bg))))
  17214. (when msg (message msg cnt))
  17215. (unless checkdir-flag ; Ensure the directory exists.
  17216. (setq checkdir-flag t)
  17217. (let ((todir (file-name-directory absprefix)))
  17218. (unless (file-directory-p todir)
  17219. (make-directory todir t))))
  17220. (unless (file-exists-p movefile)
  17221. (org-create-formula-image
  17222. value movefile options forbuffer processing-type))
  17223. (if overlays
  17224. (progn
  17225. (dolist (o (overlays-in beg end))
  17226. (when (eq (overlay-get o 'org-overlay-type)
  17227. 'org-latex-overlay)
  17228. (delete-overlay o)))
  17229. (let ((ov (make-overlay beg end)))
  17230. (overlay-put ov
  17231. 'org-overlay-type
  17232. 'org-latex-overlay)
  17233. (if (featurep 'xemacs)
  17234. (progn
  17235. (overlay-put ov 'invisible t)
  17236. (overlay-put
  17237. ov 'end-glyph
  17238. (make-glyph
  17239. (vector 'png :file movefile))))
  17240. (overlay-put
  17241. ov 'display
  17242. (list 'image
  17243. :type 'png
  17244. :file movefile
  17245. :ascent 'center)))
  17246. (push ov org-latex-fragment-image-overlays))
  17247. (goto-char end))
  17248. (delete-region beg end)
  17249. (insert
  17250. (org-add-props link
  17251. (list 'org-latex-src
  17252. (replace-regexp-in-string "\"" "" value)
  17253. 'org-latex-src-embed-type
  17254. (if block-type 'paragraph 'character)))))))
  17255. (mathml
  17256. ;; Process to MathML.
  17257. (unless (org-format-latex-mathml-available-p)
  17258. (user-error "LaTeX to MathML converter not configured"))
  17259. (incf cnt)
  17260. (when msg (message msg cnt))
  17261. (goto-char beg)
  17262. (delete-region beg end)
  17263. (insert (org-format-latex-as-mathml
  17264. value block-type prefix dir)))
  17265. (otherwise
  17266. (error "Unknown conversion type %s for LaTeX fragments"
  17267. processing-type)))))))))))
  17268. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17269. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17270. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17271. sucessful, return the portion between \"<math...> </math>\"
  17272. elements otherwise return nil. When MATHML-FILE is specified,
  17273. write the results in to that file. When invoked as an
  17274. interactive command, prompt for LATEX-FRAG, with initial value
  17275. set to the current active region and echo the results for user
  17276. inspection."
  17277. (interactive (list (let ((frag (when (org-region-active-p)
  17278. (buffer-substring-no-properties
  17279. (region-beginning) (region-end)))))
  17280. (read-string "LaTeX Fragment: " frag nil frag))))
  17281. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17282. (let* ((tmp-in-file (file-relative-name
  17283. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17284. (ignore (write-region latex-frag nil tmp-in-file))
  17285. (tmp-out-file (file-relative-name
  17286. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17287. (cmd (format-spec
  17288. org-latex-to-mathml-convert-command
  17289. `((?j . ,(shell-quote-argument
  17290. (expand-file-name org-latex-to-mathml-jar-file)))
  17291. (?I . ,(shell-quote-argument tmp-in-file))
  17292. (?o . ,(shell-quote-argument tmp-out-file)))))
  17293. mathml shell-command-output)
  17294. (when (org-called-interactively-p 'any)
  17295. (unless (org-format-latex-mathml-available-p)
  17296. (user-error "LaTeX to MathML converter not configured")))
  17297. (message "Running %s" cmd)
  17298. (setq shell-command-output (shell-command-to-string cmd))
  17299. (setq mathml
  17300. (when (file-readable-p tmp-out-file)
  17301. (with-current-buffer (find-file-noselect tmp-out-file t)
  17302. (goto-char (point-min))
  17303. (when (re-search-forward
  17304. (concat
  17305. (regexp-quote
  17306. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17307. "\\(.\\|\n\\)*"
  17308. (regexp-quote "</math>")) nil t)
  17309. (prog1 (match-string 0) (kill-buffer))))))
  17310. (cond
  17311. (mathml
  17312. (setq mathml
  17313. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17314. (when mathml-file
  17315. (write-region mathml nil mathml-file))
  17316. (when (org-called-interactively-p 'any)
  17317. (message mathml)))
  17318. ((message "LaTeX to MathML conversion failed")
  17319. (message shell-command-output)))
  17320. (delete-file tmp-in-file)
  17321. (when (file-exists-p tmp-out-file)
  17322. (delete-file tmp-out-file))
  17323. mathml))
  17324. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17325. prefix &optional dir)
  17326. "Use `org-create-math-formula' but check local cache first."
  17327. (let* ((absprefix (expand-file-name prefix dir))
  17328. (print-length nil) (print-level nil)
  17329. (formula-id (concat
  17330. "formula-"
  17331. (sha1
  17332. (prin1-to-string
  17333. (list latex-frag
  17334. org-latex-to-mathml-convert-command)))))
  17335. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17336. (formula-cache-dir (file-name-directory formula-cache)))
  17337. (unless (file-directory-p formula-cache-dir)
  17338. (make-directory formula-cache-dir t))
  17339. (unless (file-exists-p formula-cache)
  17340. (org-create-math-formula latex-frag formula-cache))
  17341. (if (file-exists-p formula-cache)
  17342. ;; Successful conversion. Return the link to MathML file.
  17343. (org-add-props
  17344. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17345. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17346. 'org-latex-src-embed-type (if latex-frag-type
  17347. 'paragraph 'character)))
  17348. ;; Failed conversion. Return the LaTeX fragment verbatim
  17349. latex-frag)))
  17350. (defun org-create-formula-image (string tofile options buffer &optional type)
  17351. "Create an image from LaTeX source using dvipng or convert.
  17352. This function calls either `org-create-formula-image-with-dvipng'
  17353. or `org-create-formula-image-with-imagemagick' depending on the
  17354. value of `org-latex-create-formula-image-program' or on the value
  17355. of the optional TYPE variable.
  17356. Note: ultimately these two function should be combined as they
  17357. share a good deal of logic."
  17358. (org-check-external-command
  17359. "latex" "needed to convert LaTeX fragments to images")
  17360. (funcall
  17361. (case (or type org-latex-create-formula-image-program)
  17362. ('dvipng
  17363. (org-check-external-command
  17364. "dvipng" "needed to convert LaTeX fragments to images")
  17365. 'org-create-formula-image-with-dvipng)
  17366. ('imagemagick
  17367. (org-check-external-command
  17368. "convert" "you need to install imagemagick")
  17369. 'org-create-formula-image-with-imagemagick)
  17370. (t (error
  17371. "Invalid value of `org-latex-create-formula-image-program'")))
  17372. string tofile options buffer))
  17373. (declare-function org-export-get-backend "ox" (name))
  17374. (declare-function org-export--get-global-options "ox" (&optional backend))
  17375. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17376. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17377. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17378. (defun org-create-formula--latex-header ()
  17379. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17380. (let ((info (org-combine-plists (org-export--get-global-options
  17381. (org-export-get-backend 'latex))
  17382. (org-export--get-inbuffer-options
  17383. (org-export-get-backend 'latex)))))
  17384. (org-latex-guess-babel-language
  17385. (org-latex-guess-inputenc
  17386. (org-splice-latex-header
  17387. org-format-latex-header
  17388. org-latex-default-packages-alist
  17389. org-latex-packages-alist t
  17390. (plist-get info :latex-header)))
  17391. info)))
  17392. ;; This function borrows from Ganesh Swami's latex2png.el
  17393. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17394. "This calls dvipng."
  17395. (require 'ox-latex)
  17396. (let* ((tmpdir (if (featurep 'xemacs)
  17397. (temp-directory)
  17398. temporary-file-directory))
  17399. (texfilebase (make-temp-name
  17400. (expand-file-name "orgtex" tmpdir)))
  17401. (texfile (concat texfilebase ".tex"))
  17402. (dvifile (concat texfilebase ".dvi"))
  17403. (pngfile (concat texfilebase ".png"))
  17404. (fnh (if (featurep 'xemacs)
  17405. (font-height (face-font 'default))
  17406. (face-attribute 'default :height nil)))
  17407. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17408. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17409. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17410. "Black"))
  17411. (bg (or (plist-get options (if buffer :background :html-background))
  17412. "Transparent")))
  17413. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17414. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17415. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17416. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17417. (let ((latex-header (org-create-formula--latex-header)))
  17418. (with-temp-file texfile
  17419. (insert latex-header)
  17420. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17421. (let ((dir default-directory))
  17422. (ignore-errors
  17423. (cd tmpdir)
  17424. (call-process "latex" nil nil nil texfile))
  17425. (cd dir))
  17426. (if (not (file-exists-p dvifile))
  17427. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17428. (ignore-errors
  17429. (if (featurep 'xemacs)
  17430. (call-process "dvipng" nil nil nil
  17431. "-fg" fg "-bg" bg
  17432. "-T" "tight"
  17433. "-o" pngfile
  17434. dvifile)
  17435. (call-process "dvipng" nil nil nil
  17436. "-fg" fg "-bg" bg
  17437. "-D" dpi
  17438. ;;"-x" scale "-y" scale
  17439. "-T" "tight"
  17440. "-o" pngfile
  17441. dvifile)))
  17442. (if (not (file-exists-p pngfile))
  17443. (if org-format-latex-signal-error
  17444. (error "Failed to create png file from %s" texfile)
  17445. (message "Failed to create png file from %s" texfile)
  17446. nil)
  17447. ;; Use the requested file name and clean up
  17448. (copy-file pngfile tofile 'replace)
  17449. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17450. (if (file-exists-p (concat texfilebase e))
  17451. (delete-file (concat texfilebase e))))
  17452. pngfile))))
  17453. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17454. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17455. "This calls convert, which is included into imagemagick."
  17456. (require 'ox-latex)
  17457. (let* ((tmpdir (if (featurep 'xemacs)
  17458. (temp-directory)
  17459. temporary-file-directory))
  17460. (texfilebase (make-temp-name
  17461. (expand-file-name "orgtex" tmpdir)))
  17462. (texfile (concat texfilebase ".tex"))
  17463. (pdffile (concat texfilebase ".pdf"))
  17464. (pngfile (concat texfilebase ".png"))
  17465. (fnh (if (featurep 'xemacs)
  17466. (font-height (face-font 'default))
  17467. (face-attribute 'default :height nil)))
  17468. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17469. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17470. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17471. "black"))
  17472. (bg (or (plist-get options (if buffer :background :html-background))
  17473. "white")))
  17474. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17475. (setq fg (org-latex-color-format fg)))
  17476. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17477. (setq bg (org-latex-color-format
  17478. (if (string= bg "Transparent") "white" bg))))
  17479. (let ((latex-header (org-create-formula--latex-header)))
  17480. (with-temp-file texfile
  17481. (insert latex-header)
  17482. (insert "\n\\begin{document}\n"
  17483. "\\definecolor{fg}{rgb}{" fg "}\n"
  17484. "\\definecolor{bg}{rgb}{" bg "}\n"
  17485. "\n\\pagecolor{bg}\n"
  17486. "\n{\\color{fg}\n"
  17487. string
  17488. "\n}\n"
  17489. "\n\\end{document}\n")))
  17490. (org-latex-compile texfile t)
  17491. (if (not (file-exists-p pdffile))
  17492. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17493. (ignore-errors
  17494. (if (featurep 'xemacs)
  17495. (call-process "convert" nil nil nil
  17496. "-density" "96"
  17497. "-trim"
  17498. "-antialias"
  17499. pdffile
  17500. "-quality" "100"
  17501. ;; "-sharpen" "0x1.0"
  17502. pngfile)
  17503. (call-process "convert" nil nil nil
  17504. "-density" dpi
  17505. "-trim"
  17506. "-antialias"
  17507. pdffile
  17508. "-quality" "100"
  17509. ;; "-sharpen" "0x1.0"
  17510. pngfile)))
  17511. (if (not (file-exists-p pngfile))
  17512. (if org-format-latex-signal-error
  17513. (error "Failed to create png file from %s" texfile)
  17514. (message "Failed to create png file from %s" texfile)
  17515. nil)
  17516. ;; Use the requested file name and clean up
  17517. (copy-file pngfile tofile 'replace)
  17518. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17519. (if (file-exists-p (concat texfilebase e))
  17520. (delete-file (concat texfilebase e))))
  17521. pngfile))))
  17522. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17523. "Fill a LaTeX header template TPL.
  17524. In the template, the following place holders will be recognized:
  17525. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17526. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17527. [PACKAGES] \\usepackage statements for PKG
  17528. [NO-PACKAGES] do not include PKG
  17529. [EXTRA] the string EXTRA
  17530. [NO-EXTRA] do not include EXTRA
  17531. For backward compatibility, if both the positive and the negative place
  17532. holder is missing, the positive one (without the \"NO-\") will be
  17533. assumed to be present at the end of the template.
  17534. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17535. EXTRA is a string.
  17536. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17537. (let (rpl (end ""))
  17538. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17539. (setq rpl (if (or (match-end 1) (not def-pkg))
  17540. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17541. tpl (replace-match rpl t t tpl))
  17542. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17543. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17544. (setq rpl (if (or (match-end 1) (not pkg))
  17545. "" (org-latex-packages-to-string pkg snippets-p t))
  17546. tpl (replace-match rpl t t tpl))
  17547. (if pkg (setq end
  17548. (concat end "\n"
  17549. (org-latex-packages-to-string pkg snippets-p)))))
  17550. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17551. (setq rpl (if (or (match-end 1) (not extra))
  17552. "" (concat extra "\n"))
  17553. tpl (replace-match rpl t t tpl))
  17554. (if (and extra (string-match "\\S-" extra))
  17555. (setq end (concat end "\n" extra))))
  17556. (if (string-match "\\S-" end)
  17557. (concat tpl "\n" end)
  17558. tpl)))
  17559. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17560. "Turn an alist of packages into a string with the \\usepackage macros."
  17561. (setq pkg (mapconcat (lambda(p)
  17562. (cond
  17563. ((stringp p) p)
  17564. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17565. (format "%% Package %s omitted" (cadr p)))
  17566. ((equal "" (car p))
  17567. (format "\\usepackage{%s}" (cadr p)))
  17568. (t
  17569. (format "\\usepackage[%s]{%s}"
  17570. (car p) (cadr p)))))
  17571. pkg
  17572. "\n"))
  17573. (if newline (concat pkg "\n") pkg))
  17574. (defun org-dvipng-color (attr)
  17575. "Return a RGB color specification for dvipng."
  17576. (apply 'format "rgb %s %s %s"
  17577. (mapcar 'org-normalize-color
  17578. (if (featurep 'xemacs)
  17579. (color-rgb-components
  17580. (face-property 'default
  17581. (cond ((eq attr :foreground) 'foreground)
  17582. ((eq attr :background) 'background))))
  17583. (color-values (face-attribute 'default attr nil))))))
  17584. (defun org-dvipng-color-format (color-name)
  17585. "Convert COLOR-NAME to a RGB color value for dvipng."
  17586. (apply 'format "rgb %s %s %s"
  17587. (mapcar 'org-normalize-color
  17588. (color-values color-name))))
  17589. (defun org-latex-color (attr)
  17590. "Return a RGB color for the LaTeX color package."
  17591. (apply 'format "%s,%s,%s"
  17592. (mapcar 'org-normalize-color
  17593. (if (featurep 'xemacs)
  17594. (color-rgb-components
  17595. (face-property 'default
  17596. (cond ((eq attr :foreground) 'foreground)
  17597. ((eq attr :background) 'background))))
  17598. (color-values (face-attribute 'default attr nil))))))
  17599. (defun org-latex-color-format (color-name)
  17600. "Convert COLOR-NAME to a RGB color value."
  17601. (apply 'format "%s,%s,%s"
  17602. (mapcar 'org-normalize-color
  17603. (color-values color-name))))
  17604. (defun org-normalize-color (value)
  17605. "Return string to be used as color value for an RGB component."
  17606. (format "%g" (/ value 65535.0)))
  17607. ;; Image display
  17608. (defvar org-inline-image-overlays nil)
  17609. (make-variable-buffer-local 'org-inline-image-overlays)
  17610. (defun org-toggle-inline-images (&optional include-linked)
  17611. "Toggle the display of inline images.
  17612. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17613. (interactive "P")
  17614. (if org-inline-image-overlays
  17615. (progn
  17616. (org-remove-inline-images)
  17617. (message "Inline image display turned off"))
  17618. (org-display-inline-images include-linked)
  17619. (if (and (org-called-interactively-p)
  17620. org-inline-image-overlays)
  17621. (message "%d images displayed inline"
  17622. (length org-inline-image-overlays))
  17623. (message "No images to display inline"))))
  17624. (defun org-redisplay-inline-images ()
  17625. "Refresh the display of inline images."
  17626. (interactive)
  17627. (if (not org-inline-image-overlays)
  17628. (org-toggle-inline-images)
  17629. (org-toggle-inline-images)
  17630. (org-toggle-inline-images)))
  17631. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17632. "Display inline images.
  17633. An inline image is a link which follows either of these
  17634. conventions:
  17635. 1. Its path is a file with an extension matching return value
  17636. from `image-file-name-regexp' and it has no contents.
  17637. 2. Its description consists in a single link of the previous
  17638. type.
  17639. When optional argument INCLUDE-LINKED is non-nil, also links with
  17640. a text description part will be inlined. This can be nice for
  17641. a quick look at those images, but it does not reflect what
  17642. exported files will look like.
  17643. When optional argument REFRESH is non-nil, refresh existing
  17644. images between BEG and END. This will create new image displays
  17645. only if necessary. BEG and END default to the buffer
  17646. boundaries."
  17647. (interactive "P")
  17648. (when (display-graphic-p)
  17649. (unless refresh
  17650. (org-remove-inline-images)
  17651. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17652. (org-with-wide-buffer
  17653. (goto-char (or beg (point-min)))
  17654. (let ((case-fold-search t)
  17655. (file-extension-re (org-image-file-name-regexp)))
  17656. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17657. (let ((link (save-match-data (org-element-context))))
  17658. ;; Check if we're at an inline image.
  17659. (when (and (equal (org-element-property :type link) "file")
  17660. (or include-linked
  17661. (not (org-element-property :contents-begin link)))
  17662. (let ((parent (org-element-property :parent link)))
  17663. (or (not (eq (org-element-type parent) 'link))
  17664. (not (cdr (org-element-contents parent)))))
  17665. (org-string-match-p file-extension-re
  17666. (org-element-property :path link)))
  17667. (let ((file (expand-file-name (org-element-property :path link))))
  17668. (when (file-exists-p file)
  17669. (let ((width
  17670. ;; Apply `org-image-actual-width' specifications.
  17671. (cond
  17672. ((not (image-type-available-p 'imagemagick)) nil)
  17673. ((eq org-image-actual-width t) nil)
  17674. ((listp org-image-actual-width)
  17675. (or
  17676. ;; First try to find a width among
  17677. ;; attributes associated to the paragraph
  17678. ;; containing link.
  17679. (let ((paragraph
  17680. (let ((e link))
  17681. (while (and (setq e (org-element-property
  17682. :parent e))
  17683. (not (eq (org-element-type e)
  17684. 'paragraph))))
  17685. e)))
  17686. (when paragraph
  17687. (save-excursion
  17688. (goto-char (org-element-property :begin paragraph))
  17689. (when
  17690. (re-search-forward
  17691. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17692. (org-element-property
  17693. :post-affiliated paragraph)
  17694. t)
  17695. (string-to-number (match-string 1))))))
  17696. ;; Otherwise, fall-back to provided number.
  17697. (car org-image-actual-width)))
  17698. ((numberp org-image-actual-width)
  17699. org-image-actual-width)))
  17700. (old (get-char-property-and-overlay
  17701. (org-element-property :begin link)
  17702. 'org-image-overlay)))
  17703. (if (and (car-safe old) refresh)
  17704. (image-refresh (overlay-get (cdr old) 'display))
  17705. (let ((image (create-image file
  17706. (and width 'imagemagick)
  17707. nil
  17708. :width width)))
  17709. (when image
  17710. (let* ((link
  17711. ;; If inline image is the description
  17712. ;; of another link, be sure to
  17713. ;; consider the latter as the one to
  17714. ;; apply the overlay on.
  17715. (let ((parent
  17716. (org-element-property :parent link)))
  17717. (if (eq (org-element-type parent) 'link)
  17718. parent
  17719. link)))
  17720. (ov (make-overlay
  17721. (org-element-property :begin link)
  17722. (progn
  17723. (goto-char
  17724. (org-element-property :end link))
  17725. (skip-chars-backward " \t")
  17726. (point)))))
  17727. (overlay-put ov 'display image)
  17728. (overlay-put ov 'face 'default)
  17729. (overlay-put ov 'org-image-overlay t)
  17730. (overlay-put
  17731. ov 'modification-hooks
  17732. (list 'org-display-inline-remove-overlay))
  17733. (push ov org-inline-image-overlays)))))))))))))))
  17734. (define-obsolete-function-alias
  17735. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17736. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17737. "Remove inline-display overlay if a corresponding region is modified."
  17738. (let ((inhibit-modification-hooks t))
  17739. (when (and ov after)
  17740. (delete ov org-inline-image-overlays)
  17741. (delete-overlay ov))))
  17742. (defun org-remove-inline-images ()
  17743. "Remove inline display of images."
  17744. (interactive)
  17745. (mapc 'delete-overlay org-inline-image-overlays)
  17746. (setq org-inline-image-overlays nil))
  17747. ;;;; Key bindings
  17748. ;; Outline functions from `outline-mode-prefix-map'
  17749. ;; that can be remapped in Org:
  17750. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17751. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17752. (define-key org-mode-map [remap outline-forward-same-level]
  17753. 'org-forward-heading-same-level)
  17754. (define-key org-mode-map [remap outline-backward-same-level]
  17755. 'org-backward-heading-same-level)
  17756. (define-key org-mode-map [remap show-branches]
  17757. 'org-kill-note-or-show-branches)
  17758. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17759. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17760. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17761. ;; Outline functions from `outline-mode-prefix-map' that can not
  17762. ;; be remapped in Org:
  17763. ;;
  17764. ;; - the column "key binding" shows whether the Outline function is still
  17765. ;; available in Org mode on the same key that it has been bound to in
  17766. ;; Outline mode:
  17767. ;; - "overridden": key used for a different functionality in Org mode
  17768. ;; - else: key still bound to the same Outline function in Org mode
  17769. ;;
  17770. ;; | Outline function | key binding | Org replacement |
  17771. ;; |------------------------------------+-------------+-----------------------|
  17772. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17773. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17774. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17775. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17776. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17777. ;; | `show-entry' | overridden | no replacement |
  17778. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17779. ;; | `show-branches' | `C-c C-k' | still same function |
  17780. ;; | `show-subtree' | overridden | visibility cycling |
  17781. ;; | `show-all' | overridden | no replacement |
  17782. ;; | `hide-subtree' | overridden | visibility cycling |
  17783. ;; | `hide-body' | overridden | no replacement |
  17784. ;; | `hide-entry' | overridden | visibility cycling |
  17785. ;; | `hide-leaves' | overridden | no replacement |
  17786. ;; | `hide-sublevels' | overridden | no replacement |
  17787. ;; | `hide-other' | overridden | no replacement |
  17788. ;; Make `C-c C-x' a prefix key
  17789. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17790. ;; TAB key with modifiers
  17791. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17792. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17793. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17794. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17795. ;; The following line is necessary under Suse GNU/Linux
  17796. (unless (featurep 'xemacs)
  17797. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17798. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17799. (define-key org-mode-map [backtab] 'org-shifttab)
  17800. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17801. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17802. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17803. ;; Cursor keys with modifiers
  17804. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17805. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17806. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17807. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17808. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  17809. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  17810. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17811. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17812. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17813. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17814. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17815. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17816. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17817. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17818. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17819. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17820. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17821. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17822. ;; Babel keys
  17823. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17824. (mapc (lambda (pair)
  17825. (define-key org-babel-map (car pair) (cdr pair)))
  17826. org-babel-key-bindings)
  17827. ;;; Extra keys for tty access.
  17828. ;; We only set them when really needed because otherwise the
  17829. ;; menus don't show the simple keys
  17830. (when (or org-use-extra-keys
  17831. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17832. (not window-system))
  17833. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17834. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17835. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17836. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17837. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17838. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17839. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17840. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17841. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17842. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17843. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17844. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17845. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17846. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17847. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17848. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17849. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17850. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17851. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17852. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17853. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17854. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17855. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17856. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17857. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17858. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17859. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17860. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17861. ;; All the other keys
  17862. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17863. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17864. (if (boundp 'narrow-map)
  17865. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17866. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17867. (if (boundp 'narrow-map)
  17868. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17869. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17870. (if (boundp 'narrow-map)
  17871. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17872. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17873. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17874. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17875. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17876. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17877. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17878. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17879. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17880. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17881. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17882. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17883. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17884. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17885. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17886. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17887. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17888. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17889. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17890. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17891. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17892. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17893. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17894. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17895. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17896. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17897. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17898. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17899. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17900. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17901. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17902. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17903. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17904. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17905. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17906. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17907. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17908. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17909. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  17910. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17911. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17912. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17913. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17914. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17915. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17916. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17917. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17918. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17919. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17920. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17921. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17922. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17923. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17924. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17925. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17926. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17927. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17928. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17929. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17930. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17931. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17932. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17933. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17934. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  17935. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17936. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17937. (org-defkey org-mode-map "\C-m" 'org-return)
  17938. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17939. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17940. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17941. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17942. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17943. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17944. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17945. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  17946. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  17947. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17948. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17949. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17950. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17951. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17952. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17953. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17954. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  17955. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17956. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17957. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17958. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17959. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17960. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17961. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17962. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17963. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17964. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17965. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17966. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17967. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17968. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17969. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17970. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17971. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17972. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17973. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17974. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17975. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17976. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  17977. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17978. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17979. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17980. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17981. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17982. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17983. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17984. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17985. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17986. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17987. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17988. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17989. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17990. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17991. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17992. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17993. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17994. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17995. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17996. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17997. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17998. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17999. (when (featurep 'xemacs)
  18000. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18001. (defconst org-speed-commands-default
  18002. '(
  18003. ("Outline Navigation")
  18004. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  18005. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  18006. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18007. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18008. ("F" . org-next-block)
  18009. ("B" . org-previous-block)
  18010. ("u" . (org-speed-move-safe 'outline-up-heading))
  18011. ("j" . org-goto)
  18012. ("g" . (org-refile t))
  18013. ("Outline Visibility")
  18014. ("c" . org-cycle)
  18015. ("C" . org-shifttab)
  18016. (" " . org-display-outline-path)
  18017. ("s" . org-narrow-to-subtree)
  18018. ("=" . org-columns)
  18019. ("Outline Structure Editing")
  18020. ("U" . org-metaup)
  18021. ("D" . org-metadown)
  18022. ("r" . org-metaright)
  18023. ("l" . org-metaleft)
  18024. ("R" . org-shiftmetaright)
  18025. ("L" . org-shiftmetaleft)
  18026. ("i" . (progn (forward-char 1) (call-interactively
  18027. 'org-insert-heading-respect-content)))
  18028. ("^" . org-sort)
  18029. ("w" . org-refile)
  18030. ("a" . org-archive-subtree-default-with-confirmation)
  18031. ("@" . org-mark-subtree)
  18032. ("#" . org-toggle-comment)
  18033. ("Clock Commands")
  18034. ("I" . org-clock-in)
  18035. ("O" . org-clock-out)
  18036. ("Meta Data Editing")
  18037. ("t" . org-todo)
  18038. ("," . (org-priority))
  18039. ("0" . (org-priority ?\ ))
  18040. ("1" . (org-priority ?A))
  18041. ("2" . (org-priority ?B))
  18042. ("3" . (org-priority ?C))
  18043. (":" . org-set-tags-command)
  18044. ("e" . org-set-effort)
  18045. ("E" . org-inc-effort)
  18046. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18047. (org-entry-put (point) "APPT_WARNTIME" m)))
  18048. ("Agenda Views etc")
  18049. ("v" . org-agenda)
  18050. ("/" . org-sparse-tree)
  18051. ("Misc")
  18052. ("o" . org-open-at-point)
  18053. ("?" . org-speed-command-help)
  18054. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18055. (">" . (org-agenda-remove-restriction-lock))
  18056. )
  18057. "The default speed commands.")
  18058. (defun org-print-speed-command (e)
  18059. (if (> (length (car e)) 1)
  18060. (progn
  18061. (princ "\n")
  18062. (princ (car e))
  18063. (princ "\n")
  18064. (princ (make-string (length (car e)) ?-))
  18065. (princ "\n"))
  18066. (princ (car e))
  18067. (princ " ")
  18068. (if (symbolp (cdr e))
  18069. (princ (symbol-name (cdr e)))
  18070. (prin1 (cdr e)))
  18071. (princ "\n")))
  18072. (defun org-speed-command-help ()
  18073. "Show the available speed commands."
  18074. (interactive)
  18075. (if (not org-use-speed-commands)
  18076. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18077. (with-output-to-temp-buffer "*Help*"
  18078. (princ "User-defined Speed commands\n===========================\n")
  18079. (mapc 'org-print-speed-command org-speed-commands-user)
  18080. (princ "\n")
  18081. (princ "Built-in Speed commands\n=======================\n")
  18082. (mapc 'org-print-speed-command org-speed-commands-default))
  18083. (with-current-buffer "*Help*"
  18084. (setq truncate-lines t))))
  18085. (defun org-speed-move-safe (cmd)
  18086. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18087. If not, return to the original position and throw an error."
  18088. (interactive)
  18089. (let ((pos (point)))
  18090. (call-interactively cmd)
  18091. (unless (and (bolp) (org-at-heading-p))
  18092. (goto-char pos)
  18093. (error "Boundary reached while executing %s" cmd))))
  18094. (defvar org-self-insert-command-undo-counter 0)
  18095. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18096. (defvar org-speed-command nil)
  18097. (define-obsolete-function-alias
  18098. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18099. (defun org-speed-command-activate (keys)
  18100. "Hook for activating single-letter speed commands.
  18101. `org-speed-commands-default' specifies a minimal command set.
  18102. Use `org-speed-commands-user' for further customization."
  18103. (when (or (and (bolp) (looking-at org-outline-regexp))
  18104. (and (functionp org-use-speed-commands)
  18105. (funcall org-use-speed-commands)))
  18106. (cdr (assoc keys (append org-speed-commands-user
  18107. org-speed-commands-default)))))
  18108. (define-obsolete-function-alias
  18109. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18110. (defun org-babel-speed-command-activate (keys)
  18111. "Hook for activating single-letter code block commands."
  18112. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18113. (cdr (assoc keys org-babel-key-bindings))))
  18114. (defcustom org-speed-command-hook
  18115. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18116. "Hook for activating speed commands at strategic locations.
  18117. Hook functions are called in sequence until a valid handler is
  18118. found.
  18119. Each hook takes a single argument, a user-pressed command key
  18120. which is also a `self-insert-command' from the global map.
  18121. Within the hook, examine the cursor position and the command key
  18122. and return nil or a valid handler as appropriate. Handler could
  18123. be one of an interactive command, a function, or a form.
  18124. Set `org-use-speed-commands' to non-nil value to enable this
  18125. hook. The default setting is `org-speed-command-activate'."
  18126. :group 'org-structure
  18127. :version "24.1"
  18128. :type 'hook)
  18129. (defun org-self-insert-command (N)
  18130. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18131. If the cursor is in a table looking at whitespace, the whitespace is
  18132. overwritten, and the table is not marked as requiring realignment."
  18133. (interactive "p")
  18134. (org-check-before-invisible-edit 'insert)
  18135. (cond
  18136. ((and org-use-speed-commands
  18137. (let ((kv (this-command-keys-vector)))
  18138. (setq org-speed-command
  18139. (run-hook-with-args-until-success
  18140. 'org-speed-command-hook
  18141. (make-string 1 (aref kv (1- (length kv))))))))
  18142. (cond
  18143. ((commandp org-speed-command)
  18144. (setq this-command org-speed-command)
  18145. (call-interactively org-speed-command))
  18146. ((functionp org-speed-command)
  18147. (funcall org-speed-command))
  18148. ((and org-speed-command (listp org-speed-command))
  18149. (eval org-speed-command))
  18150. (t (let (org-use-speed-commands)
  18151. (call-interactively 'org-self-insert-command)))))
  18152. ((and
  18153. (org-table-p)
  18154. (progn
  18155. ;; check if we blank the field, and if that triggers align
  18156. (and (featurep 'org-table) org-table-auto-blank-field
  18157. (memq last-command
  18158. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18159. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18160. ;; got extra space, this field does not determine column width
  18161. (let (org-table-may-need-update) (org-table-blank-field))
  18162. ;; no extra space, this field may determine column width
  18163. (org-table-blank-field)))
  18164. t)
  18165. (eq N 1)
  18166. (looking-at "[^|\n]* |"))
  18167. (let (org-table-may-need-update)
  18168. (goto-char (1- (match-end 0)))
  18169. (backward-delete-char 1)
  18170. (goto-char (match-beginning 0))
  18171. (self-insert-command N)))
  18172. (t
  18173. (setq org-table-may-need-update t)
  18174. (self-insert-command N)
  18175. (org-fix-tags-on-the-fly)
  18176. (if org-self-insert-cluster-for-undo
  18177. (if (not (eq last-command 'org-self-insert-command))
  18178. (setq org-self-insert-command-undo-counter 1)
  18179. (if (>= org-self-insert-command-undo-counter 20)
  18180. (setq org-self-insert-command-undo-counter 1)
  18181. (and (> org-self-insert-command-undo-counter 0)
  18182. buffer-undo-list (listp buffer-undo-list)
  18183. (not (cadr buffer-undo-list)) ; remove nil entry
  18184. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18185. (setq org-self-insert-command-undo-counter
  18186. (1+ org-self-insert-command-undo-counter))))))))
  18187. (defun org-check-before-invisible-edit (kind)
  18188. "Check is editing if kind KIND would be dangerous with invisible text around.
  18189. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18190. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18191. (if (and org-catch-invisible-edits
  18192. (or (not (boundp 'visible-mode)) (not visible-mode))
  18193. (or (get-char-property (point) 'invisible)
  18194. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18195. ;; OK, we need to take a closer look
  18196. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18197. (invisible-before-point (if (bobp) nil (get-char-property
  18198. (1- (point)) 'invisible)))
  18199. (border-and-ok-direction
  18200. (or
  18201. ;; Check if we are acting predictably before invisible text
  18202. (and invisible-at-point (not invisible-before-point)
  18203. (memq kind '(insert delete-backward)))
  18204. ;; Check if we are acting predictably after invisible text
  18205. ;; This works not well, and I have turned it off. It seems
  18206. ;; better to always show and stop after invisible text.
  18207. ;; (and (not invisible-at-point) invisible-before-point
  18208. ;; (memq kind '(insert delete)))
  18209. )))
  18210. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18211. (memq invisible-before-point '(outline org-hide-block t)))
  18212. (if (eq org-catch-invisible-edits 'error)
  18213. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18214. (if (and org-custom-properties-overlays
  18215. (y-or-n-p "Display invisible properties in this buffer? "))
  18216. (org-toggle-custom-properties-visibility)
  18217. ;; Make the area visible
  18218. (save-excursion
  18219. (if invisible-before-point
  18220. (goto-char (previous-single-char-property-change
  18221. (point) 'invisible)))
  18222. (show-subtree))
  18223. (cond
  18224. ((eq org-catch-invisible-edits 'show)
  18225. ;; That's it, we do the edit after showing
  18226. (message
  18227. "Unfolding invisible region around point before editing")
  18228. (sit-for 1))
  18229. ((and (eq org-catch-invisible-edits 'smart)
  18230. border-and-ok-direction)
  18231. (message "Unfolding invisible region around point before editing"))
  18232. (t
  18233. ;; Don't do the edit, make the user repeat it in full visibility
  18234. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18235. (defun org-fix-tags-on-the-fly ()
  18236. (when (and (equal (char-after (point-at-bol)) ?*)
  18237. (org-at-heading-p))
  18238. (org-align-tags-here org-tags-column)))
  18239. (defun org-delete-backward-char (N)
  18240. "Like `delete-backward-char', insert whitespace at field end in tables.
  18241. When deleting backwards, in tables this function will insert whitespace in
  18242. front of the next \"|\" separator, to keep the table aligned. The table will
  18243. still be marked for re-alignment if the field did fill the entire column,
  18244. because, in this case the deletion might narrow the column."
  18245. (interactive "p")
  18246. (save-match-data
  18247. (org-check-before-invisible-edit 'delete-backward)
  18248. (if (and (org-table-p)
  18249. (eq N 1)
  18250. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18251. (looking-at ".*?|"))
  18252. (let ((pos (point))
  18253. (noalign (looking-at "[^|\n\r]* |"))
  18254. (c org-table-may-need-update))
  18255. (backward-delete-char N)
  18256. (if (not overwrite-mode)
  18257. (progn
  18258. (skip-chars-forward "^|")
  18259. (insert " ")
  18260. (goto-char (1- pos))))
  18261. ;; noalign: if there were two spaces at the end, this field
  18262. ;; does not determine the width of the column.
  18263. (if noalign (setq org-table-may-need-update c)))
  18264. (backward-delete-char N)
  18265. (org-fix-tags-on-the-fly))))
  18266. (defun org-delete-char (N)
  18267. "Like `delete-char', but insert whitespace at field end in tables.
  18268. When deleting characters, in tables this function will insert whitespace in
  18269. front of the next \"|\" separator, to keep the table aligned. The table will
  18270. still be marked for re-alignment if the field did fill the entire column,
  18271. because, in this case the deletion might narrow the column."
  18272. (interactive "p")
  18273. (save-match-data
  18274. (org-check-before-invisible-edit 'delete)
  18275. (if (and (org-table-p)
  18276. (not (bolp))
  18277. (not (= (char-after) ?|))
  18278. (eq N 1))
  18279. (if (looking-at ".*?|")
  18280. (let ((pos (point))
  18281. (noalign (looking-at "[^|\n\r]* |"))
  18282. (c org-table-may-need-update))
  18283. (replace-match
  18284. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18285. (goto-char pos)
  18286. ;; noalign: if there were two spaces at the end, this field
  18287. ;; does not determine the width of the column.
  18288. (if noalign (setq org-table-may-need-update c)))
  18289. (delete-char N))
  18290. (delete-char N)
  18291. (org-fix-tags-on-the-fly))))
  18292. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18293. (put 'org-self-insert-command 'delete-selection
  18294. (lambda ()
  18295. (not (run-hook-with-args-until-success
  18296. 'self-insert-uses-region-functions))))
  18297. (put 'orgtbl-self-insert-command 'delete-selection
  18298. (lambda ()
  18299. (not (run-hook-with-args-until-success
  18300. 'self-insert-uses-region-functions))))
  18301. (put 'org-delete-char 'delete-selection 'supersede)
  18302. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18303. (put 'org-yank 'delete-selection 'yank)
  18304. ;; Make `flyspell-mode' delay after some commands
  18305. (put 'org-self-insert-command 'flyspell-delayed t)
  18306. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18307. (put 'org-delete-char 'flyspell-delayed t)
  18308. (put 'org-delete-backward-char 'flyspell-delayed t)
  18309. ;; Make pabbrev-mode expand after org-mode commands
  18310. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18311. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18312. (defun org-remap (map &rest commands)
  18313. "In MAP, remap the functions given in COMMANDS.
  18314. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18315. (let (new old)
  18316. (while commands
  18317. (setq old (pop commands) new (pop commands))
  18318. (if (fboundp 'command-remapping)
  18319. (org-defkey map (vector 'remap old) new)
  18320. (substitute-key-definition old new map global-map)))))
  18321. (defun org-transpose-words ()
  18322. "Transpose words for Org.
  18323. This uses the `org-mode-transpose-word-syntax-table' syntax
  18324. table, which interprets characters in `org-emphasis-alist' as
  18325. word constituents."
  18326. (interactive)
  18327. (with-syntax-table org-mode-transpose-word-syntax-table
  18328. (call-interactively 'transpose-words)))
  18329. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18330. (when (eq org-enable-table-editor 'optimized)
  18331. ;; If the user wants maximum table support, we need to hijack
  18332. ;; some standard editing functions
  18333. (org-remap org-mode-map
  18334. 'self-insert-command 'org-self-insert-command
  18335. 'delete-char 'org-delete-char
  18336. 'delete-backward-char 'org-delete-backward-char)
  18337. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18338. (defvar org-ctrl-c-ctrl-c-hook nil
  18339. "Hook for functions attaching themselves to `C-c C-c'.
  18340. This can be used to add additional functionality to the C-c C-c
  18341. key which executes context-dependent commands. This hook is run
  18342. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18343. run after the last test.
  18344. Each function will be called with no arguments. The function
  18345. must check if the context is appropriate for it to act. If yes,
  18346. it should do its thing and then return a non-nil value. If the
  18347. context is wrong, just do nothing and return nil.")
  18348. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18349. "Hook for functions attaching themselves to `C-c C-c'.
  18350. This can be used to add additional functionality to the C-c C-c
  18351. key which executes context-dependent commands. This hook is run
  18352. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18353. before the first test.
  18354. Each function will be called with no arguments. The function
  18355. must check if the context is appropriate for it to act. If yes,
  18356. it should do its thing and then return a non-nil value. If the
  18357. context is wrong, just do nothing and return nil.")
  18358. (defvar org-tab-first-hook nil
  18359. "Hook for functions to attach themselves to TAB.
  18360. See `org-ctrl-c-ctrl-c-hook' for more information.
  18361. This hook runs as the first action when TAB is pressed, even before
  18362. `org-cycle' messes around with the `outline-regexp' to cater for
  18363. inline tasks and plain list item folding.
  18364. If any function in this hook returns t, any other actions that
  18365. would have been caused by TAB (such as table field motion or visibility
  18366. cycling) will not occur.")
  18367. (defvar org-tab-after-check-for-table-hook nil
  18368. "Hook for functions to attach themselves to TAB.
  18369. See `org-ctrl-c-ctrl-c-hook' for more information.
  18370. This hook runs after it has been established that the cursor is not in a
  18371. table, but before checking if the cursor is in a headline or if global cycling
  18372. should be done.
  18373. If any function in this hook returns t, not other actions like visibility
  18374. cycling will be done.")
  18375. (defvar org-tab-after-check-for-cycling-hook nil
  18376. "Hook for functions to attach themselves to TAB.
  18377. See `org-ctrl-c-ctrl-c-hook' for more information.
  18378. This hook runs after it has been established that not table field motion and
  18379. not visibility should be done because of current context. This is probably
  18380. the place where a package like yasnippets can hook in.")
  18381. (defvar org-tab-before-tab-emulation-hook nil
  18382. "Hook for functions to attach themselves to TAB.
  18383. See `org-ctrl-c-ctrl-c-hook' for more information.
  18384. This hook runs after every other options for TAB have been exhausted, but
  18385. before indentation and \t insertion takes place.")
  18386. (defvar org-metaleft-hook nil
  18387. "Hook for functions attaching themselves to `M-left'.
  18388. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18389. (defvar org-metaright-hook nil
  18390. "Hook for functions attaching themselves to `M-right'.
  18391. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18392. (defvar org-metaup-hook nil
  18393. "Hook for functions attaching themselves to `M-up'.
  18394. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18395. (defvar org-metadown-hook nil
  18396. "Hook for functions attaching themselves to `M-down'.
  18397. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18398. (defvar org-shiftmetaleft-hook nil
  18399. "Hook for functions attaching themselves to `M-S-left'.
  18400. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18401. (defvar org-shiftmetaright-hook nil
  18402. "Hook for functions attaching themselves to `M-S-right'.
  18403. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18404. (defvar org-shiftmetaup-hook nil
  18405. "Hook for functions attaching themselves to `M-S-up'.
  18406. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18407. (defvar org-shiftmetadown-hook nil
  18408. "Hook for functions attaching themselves to `M-S-down'.
  18409. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18410. (defvar org-metareturn-hook nil
  18411. "Hook for functions attaching themselves to `M-RET'.
  18412. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18413. (defvar org-shiftup-hook nil
  18414. "Hook for functions attaching themselves to `S-up'.
  18415. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18416. (defvar org-shiftup-final-hook nil
  18417. "Hook for functions attaching themselves to `S-up'.
  18418. This one runs after all other options except shift-select have been excluded.
  18419. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18420. (defvar org-shiftdown-hook nil
  18421. "Hook for functions attaching themselves to `S-down'.
  18422. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18423. (defvar org-shiftdown-final-hook nil
  18424. "Hook for functions attaching themselves to `S-down'.
  18425. This one runs after all other options except shift-select have been excluded.
  18426. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18427. (defvar org-shiftleft-hook nil
  18428. "Hook for functions attaching themselves to `S-left'.
  18429. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18430. (defvar org-shiftleft-final-hook nil
  18431. "Hook for functions attaching themselves to `S-left'.
  18432. This one runs after all other options except shift-select have been excluded.
  18433. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18434. (defvar org-shiftright-hook nil
  18435. "Hook for functions attaching themselves to `S-right'.
  18436. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18437. (defvar org-shiftright-final-hook nil
  18438. "Hook for functions attaching themselves to `S-right'.
  18439. This one runs after all other options except shift-select have been excluded.
  18440. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18441. (defun org-modifier-cursor-error ()
  18442. "Throw an error, a modified cursor command was applied in wrong context."
  18443. (user-error "This command is active in special context like tables, headlines or items"))
  18444. (defun org-shiftselect-error ()
  18445. "Throw an error because Shift-Cursor command was applied in wrong context."
  18446. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18447. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18448. (user-error "This command works only in special context like headlines or timestamps")))
  18449. (defun org-call-for-shift-select (cmd)
  18450. (let ((this-command-keys-shift-translated t))
  18451. (call-interactively cmd)))
  18452. (defun org-shifttab (&optional arg)
  18453. "Global visibility cycling or move to previous table field.
  18454. Call `org-table-previous-field' within a table.
  18455. When ARG is nil, cycle globally through visibility states.
  18456. When ARG is a numeric prefix, show contents of this level."
  18457. (interactive "P")
  18458. (cond
  18459. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18460. ((integerp arg)
  18461. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18462. (message "Content view to level: %d" arg)
  18463. (org-content (prefix-numeric-value arg2))
  18464. (org-cycle-show-empty-lines t)
  18465. (setq org-cycle-global-status 'overview)))
  18466. (t (call-interactively 'org-global-cycle))))
  18467. (defun org-shiftmetaleft ()
  18468. "Promote subtree or delete table column.
  18469. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18470. `org-table-delete-column', depending on context. See the
  18471. individual commands for more information."
  18472. (interactive)
  18473. (cond
  18474. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18475. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18476. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18477. ((if (not (org-region-active-p)) (org-at-item-p)
  18478. (save-excursion (goto-char (region-beginning))
  18479. (org-at-item-p)))
  18480. (call-interactively 'org-outdent-item-tree))
  18481. (t (org-modifier-cursor-error))))
  18482. (defun org-shiftmetaright ()
  18483. "Demote subtree or insert table column.
  18484. Calls `org-demote-subtree', `org-indent-item-tree', or
  18485. `org-table-insert-column', depending on context. See the
  18486. individual commands for more information."
  18487. (interactive)
  18488. (cond
  18489. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18490. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18491. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18492. ((if (not (org-region-active-p)) (org-at-item-p)
  18493. (save-excursion (goto-char (region-beginning))
  18494. (org-at-item-p)))
  18495. (call-interactively 'org-indent-item-tree))
  18496. (t (org-modifier-cursor-error))))
  18497. (defun org-shiftmetaup (&optional arg)
  18498. "Drag the line at point up.
  18499. In a table, kill the current row.
  18500. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18501. but also adjust the previous clocked item in the clock history.
  18502. Everywhere else, drag the line at point up."
  18503. (interactive "P")
  18504. (cond
  18505. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18506. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18507. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18508. (call-interactively 'org-timestamp-up)))
  18509. (t (call-interactively 'org-drag-line-backward))))
  18510. (defun org-shiftmetadown (&optional arg)
  18511. "Drag the line at point down.
  18512. In a table, insert an empty row at the current line.
  18513. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18514. but also adjust the previous clocked item in the clock history.
  18515. Everywhere else, drag the line at point down."
  18516. (interactive "P")
  18517. (cond
  18518. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18519. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18520. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18521. (call-interactively 'org-timestamp-down)))
  18522. (t (call-interactively 'org-drag-line-forward))))
  18523. (defsubst org-hidden-tree-error ()
  18524. (user-error
  18525. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18526. (defun org-metaleft (&optional arg)
  18527. "Promote heading or move table column to left.
  18528. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18529. With no specific context, calls the Emacs default `backward-word'.
  18530. See the individual commands for more information."
  18531. (interactive "P")
  18532. (cond
  18533. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18534. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18535. ((org-with-limited-levels
  18536. (or (org-at-heading-p)
  18537. (and (org-region-active-p)
  18538. (save-excursion
  18539. (goto-char (region-beginning))
  18540. (org-at-heading-p)))))
  18541. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18542. (call-interactively 'org-do-promote))
  18543. ;; At an inline task.
  18544. ((org-at-heading-p)
  18545. (call-interactively 'org-inlinetask-promote))
  18546. ((or (org-at-item-p)
  18547. (and (org-region-active-p)
  18548. (save-excursion
  18549. (goto-char (region-beginning))
  18550. (org-at-item-p))))
  18551. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18552. (call-interactively 'org-outdent-item))
  18553. (t (call-interactively 'backward-word))))
  18554. (defun org-metaright (&optional arg)
  18555. "Demote a subtree, a list item or move table column to right.
  18556. In front of a drawer or a block keyword, indent it correctly.
  18557. With no specific context, calls the Emacs default `forward-word'.
  18558. See the individual commands for more information."
  18559. (interactive "P")
  18560. (cond
  18561. ((run-hook-with-args-until-success 'org-metaright-hook))
  18562. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18563. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18564. ((org-at-block-p) (call-interactively 'org-indent-block))
  18565. ((org-with-limited-levels
  18566. (or (org-at-heading-p)
  18567. (and (org-region-active-p)
  18568. (save-excursion
  18569. (goto-char (region-beginning))
  18570. (org-at-heading-p)))))
  18571. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18572. (call-interactively 'org-do-demote))
  18573. ;; At an inline task.
  18574. ((org-at-heading-p)
  18575. (call-interactively 'org-inlinetask-demote))
  18576. ((or (org-at-item-p)
  18577. (and (org-region-active-p)
  18578. (save-excursion
  18579. (goto-char (region-beginning))
  18580. (org-at-item-p))))
  18581. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18582. (call-interactively 'org-indent-item))
  18583. (t (call-interactively 'forward-word))))
  18584. (defun org-check-for-hidden (what)
  18585. "Check if there are hidden headlines/items in the current visual line.
  18586. WHAT can be either `headlines' or `items'. If the current line is
  18587. an outline or item heading and it has a folded subtree below it,
  18588. this function returns t, nil otherwise."
  18589. (let ((re (cond
  18590. ((eq what 'headlines) org-outline-regexp-bol)
  18591. ((eq what 'items) (org-item-beginning-re))
  18592. (t (error "This should not happen"))))
  18593. beg end)
  18594. (save-excursion
  18595. (catch 'exit
  18596. (unless (org-region-active-p)
  18597. (setq beg (point-at-bol))
  18598. (beginning-of-line 2)
  18599. (while (and (not (eobp)) ;; this is like `next-line'
  18600. (get-char-property (1- (point)) 'invisible))
  18601. (beginning-of-line 2))
  18602. (setq end (point))
  18603. (goto-char beg)
  18604. (goto-char (point-at-eol))
  18605. (setq end (max end (point)))
  18606. (while (re-search-forward re end t)
  18607. (if (get-char-property (match-beginning 0) 'invisible)
  18608. (throw 'exit t))))
  18609. nil))))
  18610. (defun org-metaup (&optional arg)
  18611. "Move subtree up or move table row up.
  18612. Calls `org-move-subtree-up' or `org-table-move-row' or
  18613. `org-move-item-up', depending on context. See the individual commands
  18614. for more information."
  18615. (interactive "P")
  18616. (cond
  18617. ((run-hook-with-args-until-success 'org-metaup-hook))
  18618. ((org-region-active-p)
  18619. (let* ((a (min (region-beginning) (region-end)))
  18620. (b (1- (max (region-beginning) (region-end))))
  18621. (c (save-excursion (goto-char a)
  18622. (move-beginning-of-line 0)))
  18623. (d (save-excursion (goto-char a)
  18624. (move-end-of-line 0) (point))))
  18625. (transpose-regions a b c d)
  18626. (goto-char c)))
  18627. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18628. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18629. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18630. (t (org-drag-element-backward))))
  18631. (defun org-metadown (&optional arg)
  18632. "Move subtree down or move table row down.
  18633. Calls `org-move-subtree-down' or `org-table-move-row' or
  18634. `org-move-item-down', depending on context. See the individual
  18635. commands for more information."
  18636. (interactive "P")
  18637. (cond
  18638. ((run-hook-with-args-until-success 'org-metadown-hook))
  18639. ((org-region-active-p)
  18640. (let* ((a (min (region-beginning) (region-end)))
  18641. (b (max (region-beginning) (region-end)))
  18642. (c (save-excursion (goto-char b)
  18643. (move-beginning-of-line 1)))
  18644. (d (save-excursion (goto-char b)
  18645. (move-end-of-line 1) (1+ (point)))))
  18646. (transpose-regions a b c d)
  18647. (goto-char d)))
  18648. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18649. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18650. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18651. (t (org-drag-element-forward))))
  18652. (defun org-shiftup (&optional arg)
  18653. "Increase item in timestamp or increase priority of current headline.
  18654. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18655. depending on context. See the individual commands for more information."
  18656. (interactive "P")
  18657. (cond
  18658. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18659. ((and org-support-shift-select (org-region-active-p))
  18660. (org-call-for-shift-select 'previous-line))
  18661. ((org-at-timestamp-p t)
  18662. (call-interactively (if org-edit-timestamp-down-means-later
  18663. 'org-timestamp-down 'org-timestamp-up)))
  18664. ((and (not (eq org-support-shift-select 'always))
  18665. org-enable-priority-commands
  18666. (org-at-heading-p))
  18667. (call-interactively 'org-priority-up))
  18668. ((and (not org-support-shift-select) (org-at-item-p))
  18669. (call-interactively 'org-previous-item))
  18670. ((org-clocktable-try-shift 'up arg))
  18671. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18672. (org-support-shift-select
  18673. (org-call-for-shift-select 'previous-line))
  18674. (t (org-shiftselect-error))))
  18675. (defun org-shiftdown (&optional arg)
  18676. "Decrease item in timestamp or decrease priority of current headline.
  18677. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18678. depending on context. See the individual commands for more information."
  18679. (interactive "P")
  18680. (cond
  18681. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18682. ((and org-support-shift-select (org-region-active-p))
  18683. (org-call-for-shift-select 'next-line))
  18684. ((org-at-timestamp-p t)
  18685. (call-interactively (if org-edit-timestamp-down-means-later
  18686. 'org-timestamp-up 'org-timestamp-down)))
  18687. ((and (not (eq org-support-shift-select 'always))
  18688. org-enable-priority-commands
  18689. (org-at-heading-p))
  18690. (call-interactively 'org-priority-down))
  18691. ((and (not org-support-shift-select) (org-at-item-p))
  18692. (call-interactively 'org-next-item))
  18693. ((org-clocktable-try-shift 'down arg))
  18694. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18695. (org-support-shift-select
  18696. (org-call-for-shift-select 'next-line))
  18697. (t (org-shiftselect-error))))
  18698. (defun org-shiftright (&optional arg)
  18699. "Cycle the thing at point or in the current line, depending on context.
  18700. Depending on context, this does one of the following:
  18701. - switch a timestamp at point one day into the future
  18702. - on a headline, switch to the next TODO keyword.
  18703. - on an item, switch entire list to the next bullet type
  18704. - on a property line, switch to the next allowed value
  18705. - on a clocktable definition line, move time block into the future"
  18706. (interactive "P")
  18707. (cond
  18708. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18709. ((and org-support-shift-select (org-region-active-p))
  18710. (org-call-for-shift-select 'forward-char))
  18711. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18712. ((and (not (eq org-support-shift-select 'always))
  18713. (org-at-heading-p))
  18714. (let ((org-inhibit-logging
  18715. (not org-treat-S-cursor-todo-selection-as-state-change))
  18716. (org-inhibit-blocking
  18717. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18718. (org-call-with-arg 'org-todo 'right)))
  18719. ((or (and org-support-shift-select
  18720. (not (eq org-support-shift-select 'always))
  18721. (org-at-item-bullet-p))
  18722. (and (not org-support-shift-select) (org-at-item-p)))
  18723. (org-call-with-arg 'org-cycle-list-bullet nil))
  18724. ((and (not (eq org-support-shift-select 'always))
  18725. (org-at-property-p))
  18726. (call-interactively 'org-property-next-allowed-value))
  18727. ((org-clocktable-try-shift 'right arg))
  18728. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18729. (org-support-shift-select
  18730. (org-call-for-shift-select 'forward-char))
  18731. (t (org-shiftselect-error))))
  18732. (defun org-shiftleft (&optional arg)
  18733. "Cycle the thing at point or in the current line, depending on context.
  18734. Depending on context, this does one of the following:
  18735. - switch a timestamp at point one day into the past
  18736. - on a headline, switch to the previous TODO keyword.
  18737. - on an item, switch entire list to the previous bullet type
  18738. - on a property line, switch to the previous allowed value
  18739. - on a clocktable definition line, move time block into the past"
  18740. (interactive "P")
  18741. (cond
  18742. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18743. ((and org-support-shift-select (org-region-active-p))
  18744. (org-call-for-shift-select 'backward-char))
  18745. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18746. ((and (not (eq org-support-shift-select 'always))
  18747. (org-at-heading-p))
  18748. (let ((org-inhibit-logging
  18749. (not org-treat-S-cursor-todo-selection-as-state-change))
  18750. (org-inhibit-blocking
  18751. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18752. (org-call-with-arg 'org-todo 'left)))
  18753. ((or (and org-support-shift-select
  18754. (not (eq org-support-shift-select 'always))
  18755. (org-at-item-bullet-p))
  18756. (and (not org-support-shift-select) (org-at-item-p)))
  18757. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18758. ((and (not (eq org-support-shift-select 'always))
  18759. (org-at-property-p))
  18760. (call-interactively 'org-property-previous-allowed-value))
  18761. ((org-clocktable-try-shift 'left arg))
  18762. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18763. (org-support-shift-select
  18764. (org-call-for-shift-select 'backward-char))
  18765. (t (org-shiftselect-error))))
  18766. (defun org-shiftcontrolright ()
  18767. "Switch to next TODO set."
  18768. (interactive)
  18769. (cond
  18770. ((and org-support-shift-select (org-region-active-p))
  18771. (org-call-for-shift-select 'forward-word))
  18772. ((and (not (eq org-support-shift-select 'always))
  18773. (org-at-heading-p))
  18774. (org-call-with-arg 'org-todo 'nextset))
  18775. (org-support-shift-select
  18776. (org-call-for-shift-select 'forward-word))
  18777. (t (org-shiftselect-error))))
  18778. (defun org-shiftcontrolleft ()
  18779. "Switch to previous TODO set."
  18780. (interactive)
  18781. (cond
  18782. ((and org-support-shift-select (org-region-active-p))
  18783. (org-call-for-shift-select 'backward-word))
  18784. ((and (not (eq org-support-shift-select 'always))
  18785. (org-at-heading-p))
  18786. (org-call-with-arg 'org-todo 'previousset))
  18787. (org-support-shift-select
  18788. (org-call-for-shift-select 'backward-word))
  18789. (t (org-shiftselect-error))))
  18790. (defun org-shiftcontrolup (&optional n)
  18791. "Change timestamps synchronously up in CLOCK log lines.
  18792. Optional argument N tells to change by that many units."
  18793. (interactive "P")
  18794. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18795. (let (org-support-shift-select)
  18796. (org-clock-timestamps-up n))
  18797. (user-error "Not at a clock log")))
  18798. (defun org-shiftcontroldown (&optional n)
  18799. "Change timestamps synchronously down in CLOCK log lines.
  18800. Optional argument N tells to change by that many units."
  18801. (interactive "P")
  18802. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18803. (let (org-support-shift-select)
  18804. (org-clock-timestamps-down n))
  18805. (user-error "Not at a clock log")))
  18806. (defun org-increase-number-at-point (&optional inc)
  18807. "Increment the number at point.
  18808. With an optional prefix numeric argument INC, increment using
  18809. this numeric value."
  18810. (interactive "p")
  18811. (if (not (number-at-point))
  18812. (user-error "Not on a number")
  18813. (unless inc (setq inc 1))
  18814. (let ((pos (point))
  18815. (beg (skip-chars-backward "-+^/*0-9eE."))
  18816. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  18817. (setq nap (buffer-substring-no-properties
  18818. (+ pos beg) (+ pos beg end)))
  18819. (delete-region (+ pos beg) (+ pos beg end))
  18820. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  18821. (when (org-at-table-p)
  18822. (org-table-align)
  18823. (org-table-end-of-field 1))))
  18824. (defun org-decrease-number-at-point (&optional inc)
  18825. "Decrement the number at point.
  18826. With an optional prefix numeric argument INC, decrement using
  18827. this numeric value."
  18828. (interactive "p")
  18829. (org-increase-number-at-point (- inc)))
  18830. (defun org-ctrl-c-ret ()
  18831. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18832. (interactive)
  18833. (cond
  18834. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18835. (t (call-interactively 'org-insert-heading))))
  18836. (defun org-find-visible ()
  18837. (let ((s (point)))
  18838. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18839. (get-char-property s 'invisible)))
  18840. s))
  18841. (defun org-find-invisible ()
  18842. (let ((s (point)))
  18843. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18844. (not (get-char-property s 'invisible))))
  18845. s))
  18846. (defun org-copy-visible (beg end)
  18847. "Copy the visible parts of the region."
  18848. (interactive "r")
  18849. (let (snippets s)
  18850. (save-excursion
  18851. (save-restriction
  18852. (narrow-to-region beg end)
  18853. (setq s (goto-char (point-min)))
  18854. (while (not (= (point) (point-max)))
  18855. (goto-char (org-find-invisible))
  18856. (push (buffer-substring s (point)) snippets)
  18857. (setq s (goto-char (org-find-visible))))))
  18858. (kill-new (apply 'concat (nreverse snippets)))))
  18859. (defun org-copy-special ()
  18860. "Copy region in table or copy current subtree.
  18861. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18862. See the individual commands for more information."
  18863. (interactive)
  18864. (call-interactively
  18865. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18866. (defun org-cut-special ()
  18867. "Cut region in table or cut current subtree.
  18868. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18869. See the individual commands for more information."
  18870. (interactive)
  18871. (call-interactively
  18872. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18873. (defun org-paste-special (arg)
  18874. "Paste rectangular region into table, or past subtree relative to level.
  18875. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18876. See the individual commands for more information."
  18877. (interactive "P")
  18878. (if (org-at-table-p)
  18879. (org-table-paste-rectangle)
  18880. (org-paste-subtree arg)))
  18881. (defsubst org-in-fixed-width-region-p ()
  18882. "Is point in a fixed-width region?"
  18883. (save-match-data
  18884. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18885. (defun org-edit-special (&optional arg)
  18886. "Call a special editor for the element at point.
  18887. When at a table, call the formula editor with `org-table-edit-formulas'.
  18888. When in a source code block, call `org-edit-src-code'.
  18889. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18890. When in an export block, call `org-edit-export-block'.
  18891. When at an #+INCLUDE keyword, visit the included file.
  18892. On a link, call `ffap' to visit the link at point.
  18893. Otherwise, return a user error."
  18894. (interactive "P")
  18895. (let ((element (org-element-at-point)))
  18896. (assert (not buffer-read-only) nil
  18897. "Buffer is read-only: %s" (buffer-name))
  18898. (case (org-element-type element)
  18899. (src-block
  18900. (if (not arg) (org-edit-src-code)
  18901. (let* ((info (org-babel-get-src-block-info))
  18902. (lang (nth 0 info))
  18903. (params (nth 2 info))
  18904. (session (cdr (assq :session params))))
  18905. (if (not session) (org-edit-src-code)
  18906. ;; At a src-block with a session and function called with
  18907. ;; an ARG: switch to the buffer related to the inferior
  18908. ;; process.
  18909. (switch-to-buffer
  18910. (funcall (intern (concat "org-babel-prep-session:" lang))
  18911. session params))))))
  18912. (keyword
  18913. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18914. (org-open-link-from-string
  18915. (format "[[%s]]"
  18916. (expand-file-name
  18917. (let ((value (org-element-property :value element)))
  18918. (cond ((not (org-string-nw-p value))
  18919. (user-error "No file to edit"))
  18920. ((string-match "\\`\"\\(.*?\\)\"" value)
  18921. (match-string 1 value))
  18922. ((string-match "\\`[^ \t\"]\\S-*" value)
  18923. (match-string 0 value))
  18924. (t (user-error "No valid file specified")))))))
  18925. (user-error "No special environment to edit here")))
  18926. (table
  18927. (if (eq (org-element-property :type element) 'table.el)
  18928. (org-edit-table.el)
  18929. (call-interactively 'org-table-edit-formulas)))
  18930. ;; Only Org tables contain `table-row' type elements.
  18931. (table-row (call-interactively 'org-table-edit-formulas))
  18932. ((example-block src-block) (org-edit-src-code))
  18933. (export-block (org-edit-export-block))
  18934. (fixed-width (org-edit-fixed-width-region))
  18935. (otherwise
  18936. ;; No notable element at point. Though, we may be at a link,
  18937. ;; which is an object. Thus, scan deeper.
  18938. (if (eq (org-element-type (org-element-context element)) 'link)
  18939. (call-interactively 'ffap)
  18940. (user-error "No special environment to edit here"))))))
  18941. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18942. (defun org-ctrl-c-ctrl-c (&optional arg)
  18943. "Set tags in headline, or update according to changed information at point.
  18944. This command does many different things, depending on context:
  18945. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18946. this is what we do.
  18947. - If the cursor is on a statistics cookie, update it.
  18948. - If the cursor is in a headline, prompt for tags and insert them
  18949. into the current line, aligned to `org-tags-column'. When called
  18950. with prefix arg, realign all tags in the current buffer.
  18951. - If the cursor is in one of the special #+KEYWORD lines, this
  18952. triggers scanning the buffer for these lines and updating the
  18953. information.
  18954. - If the cursor is inside a table, realign the table. This command
  18955. works even if the automatic table editor has been turned off.
  18956. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18957. the entire table.
  18958. - If the cursor is at a footnote reference or definition, jump to
  18959. the corresponding definition or references, respectively.
  18960. - If the cursor is a the beginning of a dynamic block, update it.
  18961. - If the current buffer is a capture buffer, close note and file it.
  18962. - If the cursor is on a <<<target>>>, update radio targets and
  18963. corresponding links in this buffer.
  18964. - If the cursor is on a numbered item in a plain list, renumber the
  18965. ordered list.
  18966. - If the cursor is on a checkbox, toggle it.
  18967. - If the cursor is on a code block, evaluate it. The variable
  18968. `org-confirm-babel-evaluate' can be used to control prompting
  18969. before code block evaluation, by default every code block
  18970. evaluation requires confirmation. Code block evaluation can be
  18971. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18972. (interactive "P")
  18973. (cond
  18974. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18975. org-occur-highlights)
  18976. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18977. (org-remove-occur-highlights)
  18978. (message "Temporary highlights/overlays removed from current buffer"))
  18979. ((and (local-variable-p 'org-finish-function (current-buffer))
  18980. (fboundp org-finish-function))
  18981. (funcall org-finish-function))
  18982. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18983. (t
  18984. (let* ((context (org-element-context)) (type (org-element-type context)))
  18985. ;; Test if point is within a blank line.
  18986. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18987. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18988. (user-error "C-c C-c can do nothing useful at this location"))
  18989. (case type
  18990. ;; When at a link, act according to the parent instead.
  18991. (link (setq context (org-element-property :parent context))
  18992. (setq type (org-element-type context)))
  18993. ;; Unsupported object types: refer to the first supported
  18994. ;; element or object containing it.
  18995. ((bold code entity export-snippet inline-babel-call inline-src-block
  18996. italic latex-fragment line-break macro strike-through subscript
  18997. superscript underline verbatim)
  18998. (while (and (setq context (org-element-property :parent context))
  18999. (not (memq (setq type (org-element-type context))
  19000. '(radio-target paragraph verse-block
  19001. table-cell)))))))
  19002. ;; For convenience: at the first line of a paragraph on the
  19003. ;; same line as an item, apply function on that item instead.
  19004. (when (eq type 'paragraph)
  19005. (let ((parent (org-element-property :parent context)))
  19006. (when (and (eq (org-element-type parent) 'item)
  19007. (= (point-at-bol) (org-element-property :begin parent)))
  19008. (setq context parent type 'item))))
  19009. ;; Act according to type of element or object at point.
  19010. (case type
  19011. (clock (org-clock-update-time-maybe))
  19012. (dynamic-block
  19013. (save-excursion
  19014. (goto-char (org-element-property :post-affiliated context))
  19015. (org-update-dblock)))
  19016. (footnote-definition
  19017. (goto-char (org-element-property :post-affiliated context))
  19018. (call-interactively 'org-footnote-action))
  19019. (footnote-reference (call-interactively 'org-footnote-action))
  19020. ((headline inlinetask)
  19021. (save-excursion (goto-char (org-element-property :begin context))
  19022. (call-interactively 'org-set-tags)))
  19023. (item
  19024. ;; At an item: a double C-u set checkbox to "[-]"
  19025. ;; unconditionally, whereas a single one will toggle its
  19026. ;; presence. Without an universal argument, if the item
  19027. ;; has a checkbox, toggle it. Otherwise repair the list.
  19028. (let* ((box (org-element-property :checkbox context))
  19029. (struct (org-element-property :structure context))
  19030. (old-struct (copy-tree struct))
  19031. (parents (org-list-parents-alist struct))
  19032. (prevs (org-list-prevs-alist struct))
  19033. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19034. (org-list-set-checkbox
  19035. (org-element-property :begin context) struct
  19036. (cond ((equal arg '(16)) "[-]")
  19037. ((and (not box) (equal arg '(4))) "[ ]")
  19038. ((or (not box) (equal arg '(4))) nil)
  19039. ((eq box 'on) "[ ]")
  19040. (t "[X]")))
  19041. ;; Mimic `org-list-write-struct' but with grabbing
  19042. ;; a return value from `org-list-struct-fix-box'.
  19043. (org-list-struct-fix-ind struct parents 2)
  19044. (org-list-struct-fix-item-end struct)
  19045. (org-list-struct-fix-bul struct prevs)
  19046. (org-list-struct-fix-ind struct parents)
  19047. (let ((block-item
  19048. (org-list-struct-fix-box struct parents prevs orderedp)))
  19049. (if (and box (equal struct old-struct))
  19050. (if (equal arg '(16))
  19051. (message "Checkboxes already reset")
  19052. (user-error "Cannot toggle this checkbox: %s"
  19053. (if (eq box 'on)
  19054. "all subitems checked"
  19055. "unchecked subitems")))
  19056. (org-list-struct-apply-struct struct old-struct)
  19057. (org-update-checkbox-count-maybe))
  19058. (when block-item
  19059. (message "Checkboxes were removed due to empty box at line %d"
  19060. (org-current-line block-item))))))
  19061. (keyword
  19062. (let ((org-inhibit-startup-visibility-stuff t)
  19063. (org-startup-align-all-tables nil))
  19064. (when (boundp 'org-table-coordinate-overlays)
  19065. (mapc 'delete-overlay org-table-coordinate-overlays)
  19066. (setq org-table-coordinate-overlays nil))
  19067. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19068. (message "Local setup has been refreshed"))
  19069. (plain-list
  19070. ;; At a plain list, with a double C-u argument, set
  19071. ;; checkboxes of each item to "[-]", whereas a single one
  19072. ;; will toggle their presence according to the state of the
  19073. ;; first item in the list. Without an argument, repair the
  19074. ;; list.
  19075. (let* ((begin (org-element-property :contents-begin context))
  19076. (beginm (move-marker (make-marker) begin))
  19077. (struct (org-element-property :structure context))
  19078. (old-struct (copy-tree struct))
  19079. (first-box (save-excursion
  19080. (goto-char begin)
  19081. (looking-at org-list-full-item-re)
  19082. (match-string-no-properties 3)))
  19083. (new-box (cond ((equal arg '(16)) "[-]")
  19084. ((equal arg '(4)) (unless first-box "[ ]"))
  19085. ((equal first-box "[X]") "[ ]")
  19086. (t "[X]"))))
  19087. (cond
  19088. (arg
  19089. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19090. (org-list-get-all-items
  19091. begin struct (org-list-prevs-alist struct))))
  19092. ((and first-box (eq (point) begin))
  19093. ;; For convenience, when point is at bol on the first
  19094. ;; item of the list and no argument is provided, simply
  19095. ;; toggle checkbox of that item, if any.
  19096. (org-list-set-checkbox begin struct new-box)))
  19097. (org-list-write-struct
  19098. struct (org-list-parents-alist struct) old-struct)
  19099. (org-update-checkbox-count-maybe)
  19100. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19101. ((property-drawer node-property)
  19102. (call-interactively 'org-property-action))
  19103. ((radio-target target)
  19104. (call-interactively 'org-update-radio-target-regexp))
  19105. (statistics-cookie
  19106. (call-interactively 'org-update-statistics-cookies))
  19107. ((table table-cell table-row)
  19108. ;; At a table, recalculate every field and align it. Also
  19109. ;; send the table if necessary. If the table has
  19110. ;; a `table.el' type, just give up. At a table row or
  19111. ;; cell, maybe recalculate line but always align table.
  19112. (if (eq (org-element-property :type context) 'table.el)
  19113. (message "Use C-c ' to edit table.el tables")
  19114. (let ((org-enable-table-editor t))
  19115. (if (or (eq type 'table)
  19116. ;; Check if point is at a TBLFM line.
  19117. (and (eq type 'table-row)
  19118. (= (point) (org-element-property :end context))))
  19119. (save-excursion
  19120. (if (org-at-TBLFM-p)
  19121. (progn (require 'org-table)
  19122. (org-table-calc-current-TBLFM))
  19123. (goto-char (org-element-property :contents-begin context))
  19124. (org-call-with-arg 'org-table-recalculate (or arg t))
  19125. (orgtbl-send-table 'maybe)))
  19126. (org-table-maybe-eval-formula)
  19127. (cond (arg (call-interactively 'org-table-recalculate))
  19128. ((org-table-maybe-recalculate-line))
  19129. (t (org-table-align)))))))
  19130. (timestamp (org-timestamp-change 0 'day))
  19131. (otherwise
  19132. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19133. (user-error
  19134. "C-c C-c can do nothing useful at this location")))))))))
  19135. (defun org-mode-restart ()
  19136. (interactive)
  19137. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19138. (funcall major-mode)
  19139. (hack-local-variables)
  19140. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19141. (org-indent-mode -1)))
  19142. (message "%s restarted" major-mode))
  19143. (defun org-kill-note-or-show-branches ()
  19144. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19145. (interactive)
  19146. (if (not org-finish-function)
  19147. (progn
  19148. (hide-subtree)
  19149. (call-interactively 'show-branches))
  19150. (let ((org-note-abort t))
  19151. (funcall org-finish-function))))
  19152. (defun org-open-line (n)
  19153. "Insert a new row in tables, call `open-line' elsewhere.
  19154. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19155. (interactive "*p")
  19156. (cond
  19157. ((not org-special-ctrl-o)
  19158. (open-line n))
  19159. ((org-at-table-p)
  19160. (org-table-insert-row))
  19161. (t
  19162. (open-line n))))
  19163. (defun org-return (&optional indent)
  19164. "Goto next table row or insert a newline.
  19165. Calls `org-table-next-row' or `newline', depending on context.
  19166. When optional INDENT argument is non-nil, call
  19167. `newline-and-indent' instead of `newline'.
  19168. When `org-return-follows-link' is non-nil and point is on
  19169. a timestamp or a link, call `org-open-at-point'. However, it
  19170. will not happen if point is in a table or on a \"dead\"
  19171. object (e.g., within a comment). In these case, you need to use
  19172. `org-open-at-point' directly."
  19173. (interactive)
  19174. (if (and (save-excursion
  19175. (beginning-of-line)
  19176. (looking-at org-todo-line-regexp))
  19177. (match-beginning 3)
  19178. (>= (point) (match-beginning 3)))
  19179. ;; Point is on headline tags. Do not break them: add a newline
  19180. ;; after the headline instead.
  19181. (progn (org-show-entry)
  19182. (end-of-line)
  19183. (if indent (newline-and-indent) (newline)))
  19184. (let* ((context (if org-return-follows-link (org-element-context)
  19185. (org-element-at-point)))
  19186. (type (org-element-type context)))
  19187. (cond
  19188. ;; In a table, call `org-table-next-row'.
  19189. ((or (and (eq type 'table)
  19190. (>= (point) (org-element-property :contents-begin context))
  19191. (< (point) (org-element-property :contents-end context)))
  19192. (org-element-lineage context '(table-row table-cell) t))
  19193. (org-table-justify-field-maybe)
  19194. (call-interactively #'org-table-next-row))
  19195. ;; On a link or a timestamp but not on white spaces after it,
  19196. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19197. ((and org-return-follows-link
  19198. (memq type '(link timestamp))
  19199. (< (point)
  19200. (save-excursion (goto-char (org-element-property :end context))
  19201. (skip-chars-backward " \t")
  19202. (point))))
  19203. (call-interactively #'org-open-at-point))
  19204. ;; In a list, make sure indenting keeps trailing text within.
  19205. ((and indent
  19206. (not (eolp))
  19207. (org-element-lineage context '(item plain-list) t))
  19208. (let ((trailing-data
  19209. (delete-and-extract-region (point) (line-end-position))))
  19210. (newline-and-indent)
  19211. (save-excursion (insert trailing-data))))
  19212. (t (if indent (newline-and-indent) (newline)))))))
  19213. (defun org-return-indent ()
  19214. "Goto next table row or insert a newline and indent.
  19215. Calls `org-table-next-row' or `newline-and-indent', depending on
  19216. context. See the individual commands for more information."
  19217. (interactive)
  19218. (org-return t))
  19219. (defun org-ctrl-c-star ()
  19220. "Compute table, or change heading status of lines.
  19221. Calls `org-table-recalculate' or `org-toggle-heading',
  19222. depending on context."
  19223. (interactive)
  19224. (cond
  19225. ((org-at-table-p)
  19226. (call-interactively 'org-table-recalculate))
  19227. (t
  19228. ;; Convert all lines in region to list items
  19229. (call-interactively 'org-toggle-heading))))
  19230. (defun org-ctrl-c-minus ()
  19231. "Insert separator line in table or modify bullet status of line.
  19232. Also turns a plain line or a region of lines into list items.
  19233. Calls `org-table-insert-hline', `org-toggle-item', or
  19234. `org-cycle-list-bullet', depending on context."
  19235. (interactive)
  19236. (cond
  19237. ((org-at-table-p)
  19238. (call-interactively 'org-table-insert-hline))
  19239. ((org-region-active-p)
  19240. (call-interactively 'org-toggle-item))
  19241. ((org-in-item-p)
  19242. (call-interactively 'org-cycle-list-bullet))
  19243. (t
  19244. (call-interactively 'org-toggle-item))))
  19245. (defun org-toggle-item (arg)
  19246. "Convert headings or normal lines to items, items to normal lines.
  19247. If there is no active region, only the current line is considered.
  19248. If the first non blank line in the region is a headline, convert
  19249. all headlines to items, shifting text accordingly.
  19250. If it is an item, convert all items to normal lines.
  19251. If it is normal text, change region into a list of items.
  19252. With a prefix argument ARG, change the region in a single item."
  19253. (interactive "P")
  19254. (let ((shift-text
  19255. (function
  19256. ;; Shift text in current section to IND, from point to END.
  19257. ;; The function leaves point to END line.
  19258. (lambda (ind end)
  19259. (let ((min-i 1000) (end (copy-marker end)))
  19260. ;; First determine the minimum indentation (MIN-I) of
  19261. ;; the text.
  19262. (save-excursion
  19263. (catch 'exit
  19264. (while (< (point) end)
  19265. (let ((i (org-get-indentation)))
  19266. (cond
  19267. ;; Skip blank lines and inline tasks.
  19268. ((looking-at "^[ \t]*$"))
  19269. ((looking-at org-outline-regexp-bol))
  19270. ;; We can't find less than 0 indentation.
  19271. ((zerop i) (throw 'exit (setq min-i 0)))
  19272. ((< i min-i) (setq min-i i))))
  19273. (forward-line))))
  19274. ;; Then indent each line so that a line indented to
  19275. ;; MIN-I becomes indented to IND. Ignore blank lines
  19276. ;; and inline tasks in the process.
  19277. (let ((delta (- ind min-i)))
  19278. (while (< (point) end)
  19279. (unless (or (looking-at "^[ \t]*$")
  19280. (looking-at org-outline-regexp-bol))
  19281. (org-indent-line-to (+ (org-get-indentation) delta)))
  19282. (forward-line)))))))
  19283. (skip-blanks
  19284. (function
  19285. ;; Return beginning of first non-blank line, starting from
  19286. ;; line at POS.
  19287. (lambda (pos)
  19288. (save-excursion
  19289. (goto-char pos)
  19290. (skip-chars-forward " \r\t\n")
  19291. (point-at-bol)))))
  19292. beg end)
  19293. ;; Determine boundaries of changes.
  19294. (if (org-region-active-p)
  19295. (setq beg (funcall skip-blanks (region-beginning))
  19296. end (copy-marker (region-end)))
  19297. (setq beg (funcall skip-blanks (point-at-bol))
  19298. end (copy-marker (point-at-eol))))
  19299. ;; Depending on the starting line, choose an action on the text
  19300. ;; between BEG and END.
  19301. (org-with-limited-levels
  19302. (save-excursion
  19303. (goto-char beg)
  19304. (cond
  19305. ;; Case 1. Start at an item: de-itemize. Note that it only
  19306. ;; happens when a region is active: `org-ctrl-c-minus'
  19307. ;; would call `org-cycle-list-bullet' otherwise.
  19308. ((org-at-item-p)
  19309. (while (< (point) end)
  19310. (when (org-at-item-p)
  19311. (skip-chars-forward " \t")
  19312. (delete-region (point) (match-end 0)))
  19313. (forward-line)))
  19314. ;; Case 2. Start at an heading: convert to items.
  19315. ((org-at-heading-p)
  19316. (let* ((bul (org-list-bullet-string "-"))
  19317. (bul-len (length bul))
  19318. (done (org-entry-is-done-p))
  19319. (todo (org-entry-is-todo-p))
  19320. ;; Indentation of the first heading. It should be
  19321. ;; relative to the indentation of its parent, if any.
  19322. (start-ind (save-excursion
  19323. (cond
  19324. ((not org-adapt-indentation) 0)
  19325. ((not (outline-previous-heading)) 0)
  19326. (t (length (match-string 0))))))
  19327. ;; Level of first heading. Further headings will be
  19328. ;; compared to it to determine hierarchy in the list.
  19329. (ref-level (org-reduced-level (org-outline-level))))
  19330. (when (or done todo) (org-todo ""))
  19331. (while (< (point) end)
  19332. (let* ((level (org-reduced-level (org-outline-level)))
  19333. (delta (max 0 (- level ref-level))))
  19334. ;; If current headline is less indented than the first
  19335. ;; one, set it as reference, in order to preserve
  19336. ;; subtrees.
  19337. (when (< level ref-level) (setq ref-level level))
  19338. (replace-match bul t t)
  19339. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19340. (when (or done todo)
  19341. (let* ((struct (org-list-struct))
  19342. (old (copy-tree struct)))
  19343. (org-list-set-checkbox (line-beginning-position)
  19344. struct
  19345. (if done "[X]" "[ ]"))
  19346. (org-list-write-struct struct
  19347. (org-list-parents-alist struct)
  19348. old)))
  19349. ;; Ensure all text down to END (or SECTION-END) belongs
  19350. ;; to the newly created item.
  19351. (let ((section-end (save-excursion
  19352. (or (outline-next-heading) (point)))))
  19353. (forward-line)
  19354. (funcall shift-text
  19355. (+ start-ind (* (1+ delta) bul-len))
  19356. (min end section-end)))))))
  19357. ;; Case 3. Normal line with ARG: make the first line of region
  19358. ;; an item, and shift indentation of others lines to
  19359. ;; set them as item's body.
  19360. (arg (let* ((bul (org-list-bullet-string "-"))
  19361. (bul-len (length bul))
  19362. (ref-ind (org-get-indentation)))
  19363. (skip-chars-forward " \t")
  19364. (insert bul)
  19365. (forward-line)
  19366. (while (< (point) end)
  19367. ;; Ensure that lines less indented than first one
  19368. ;; still get included in item body.
  19369. (funcall shift-text
  19370. (+ ref-ind bul-len)
  19371. (min end (save-excursion (or (outline-next-heading)
  19372. (point)))))
  19373. (forward-line))))
  19374. ;; Case 4. Normal line without ARG: turn each non-item line
  19375. ;; into an item.
  19376. (t
  19377. (while (< (point) end)
  19378. (unless (or (org-at-heading-p) (org-at-item-p))
  19379. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19380. (replace-match
  19381. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19382. (forward-line))))))))
  19383. (defun org-toggle-heading (&optional nstars)
  19384. "Convert headings to normal text, or items or text to headings.
  19385. If there is no active region, only convert the current line.
  19386. With a \\[universal-argument] prefix, convert the whole list at
  19387. point into heading.
  19388. In a region:
  19389. - If the first non blank line is a headline, remove the stars
  19390. from all headlines in the region.
  19391. - If it is a normal line, turn each and every normal line (i.e.,
  19392. not an heading or an item) in the region into headings. If you
  19393. want to convert only the first line of this region, use one
  19394. universal prefix argument.
  19395. - If it is a plain list item, turn all plain list items into headings.
  19396. When converting a line into a heading, the number of stars is chosen
  19397. such that the lines become children of the current entry. However,
  19398. when a numeric prefix argument is given, its value determines the
  19399. number of stars to add."
  19400. (interactive "P")
  19401. (let ((skip-blanks
  19402. (function
  19403. ;; Return beginning of first non-blank line, starting from
  19404. ;; line at POS.
  19405. (lambda (pos)
  19406. (save-excursion
  19407. (goto-char pos)
  19408. (while (org-at-comment-p) (forward-line))
  19409. (skip-chars-forward " \r\t\n")
  19410. (point-at-bol)))))
  19411. beg end toggled)
  19412. ;; Determine boundaries of changes. If a universal prefix has
  19413. ;; been given, put the list in a region. If region ends at a bol,
  19414. ;; do not consider the last line to be in the region.
  19415. (when (and current-prefix-arg (org-at-item-p))
  19416. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19417. (org-mark-element))
  19418. (if (org-region-active-p)
  19419. (setq beg (funcall skip-blanks (region-beginning))
  19420. end (copy-marker (save-excursion
  19421. (goto-char (region-end))
  19422. (if (bolp) (point) (point-at-eol)))))
  19423. (setq beg (funcall skip-blanks (point-at-bol))
  19424. end (copy-marker (point-at-eol))))
  19425. ;; Ensure inline tasks don't count as headings.
  19426. (org-with-limited-levels
  19427. (save-excursion
  19428. (goto-char beg)
  19429. (cond
  19430. ;; Case 1. Started at an heading: de-star headings.
  19431. ((org-at-heading-p)
  19432. (while (< (point) end)
  19433. (when (org-at-heading-p t)
  19434. (looking-at org-outline-regexp) (replace-match "")
  19435. (setq toggled t))
  19436. (forward-line)))
  19437. ;; Case 2. Started at an item: change items into headlines.
  19438. ;; One star will be added by `org-list-to-subtree'.
  19439. ((org-at-item-p)
  19440. (let* ((stars (make-string
  19441. ;; subtract the star that will be added again by
  19442. ;; `org-list-to-subtree'
  19443. (if (numberp nstars) (1- nstars)
  19444. (or (org-current-level) 0))
  19445. ?*))
  19446. (add-stars
  19447. (cond (nstars "") ; stars from prefix only
  19448. ((equal stars "") "") ; before first heading
  19449. (org-odd-levels-only "*") ; inside heading, odd
  19450. (t "")))) ; inside heading, oddeven
  19451. (while (< (point) end)
  19452. (when (org-at-item-p)
  19453. ;; Pay attention to cases when region ends before list.
  19454. (let* ((struct (org-list-struct))
  19455. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19456. (save-restriction
  19457. (narrow-to-region (point) list-end)
  19458. (insert
  19459. (org-list-to-subtree
  19460. (org-list-parse-list t)
  19461. '(:istart (concat stars add-stars (funcall get-stars depth))
  19462. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19463. (setq toggled t))
  19464. (forward-line))))
  19465. ;; Case 3. Started at normal text: make every line an heading,
  19466. ;; skipping headlines and items.
  19467. (t (let* ((stars
  19468. (make-string
  19469. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19470. (add-stars
  19471. (cond (nstars "") ; stars from prefix only
  19472. ((equal stars "") "*") ; before first heading
  19473. (org-odd-levels-only "**") ; inside heading, odd
  19474. (t "*"))) ; inside heading, oddeven
  19475. (rpl (concat stars add-stars " "))
  19476. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19477. (while (< (point) (if (equal nstars '(4)) lend end))
  19478. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19479. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19480. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19481. (forward-line)))))))
  19482. (unless toggled (message "Cannot toggle heading from here"))))
  19483. (defun org-meta-return (&optional arg)
  19484. "Insert a new heading or wrap a region in a table.
  19485. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19486. on context. See the individual commands for more information."
  19487. (interactive "P")
  19488. (org-check-before-invisible-edit 'insert)
  19489. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19490. (let* ((element (org-element-at-point))
  19491. (type (org-element-type element)))
  19492. (when (eq type 'table-row)
  19493. (setq element (org-element-property :parent element))
  19494. (setq type 'table))
  19495. (if (and (eq type 'table)
  19496. (eq (org-element-property :type element) 'org)
  19497. (>= (point) (org-element-property :contents-begin element))
  19498. (< (point) (org-element-property :contents-end element)))
  19499. (call-interactively 'org-table-wrap-region)
  19500. (call-interactively 'org-insert-heading)))))
  19501. ;;; Menu entries
  19502. (defsubst org-in-subtree-not-table-p ()
  19503. "Are we in a subtree and not in a table?"
  19504. (and (not (org-before-first-heading-p))
  19505. (not (org-at-table-p))))
  19506. ;; Define the Org-mode menus
  19507. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19508. '("Tbl"
  19509. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19510. ["Next Field" org-cycle (org-at-table-p)]
  19511. ["Previous Field" org-shifttab (org-at-table-p)]
  19512. ["Next Row" org-return (org-at-table-p)]
  19513. "--"
  19514. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19515. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19516. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19517. "--"
  19518. ("Column"
  19519. ["Move Column Left" org-metaleft (org-at-table-p)]
  19520. ["Move Column Right" org-metaright (org-at-table-p)]
  19521. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19522. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19523. ("Row"
  19524. ["Move Row Up" org-metaup (org-at-table-p)]
  19525. ["Move Row Down" org-metadown (org-at-table-p)]
  19526. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19527. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19528. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19529. "--"
  19530. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19531. ("Rectangle"
  19532. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19533. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19534. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19535. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19536. "--"
  19537. ("Calculate"
  19538. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19539. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19540. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19541. "--"
  19542. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19543. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19544. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19545. "--"
  19546. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19547. "--"
  19548. ["Sum Column/Rectangle" org-table-sum
  19549. (or (org-at-table-p) (org-region-active-p))]
  19550. ["Which Column?" org-table-current-column (org-at-table-p)])
  19551. ["Debug Formulas"
  19552. org-table-toggle-formula-debugger
  19553. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19554. ["Show Col/Row Numbers"
  19555. org-table-toggle-coordinate-overlays
  19556. :style toggle
  19557. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19558. "--"
  19559. ["Create" org-table-create (and (not (org-at-table-p))
  19560. org-enable-table-editor)]
  19561. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19562. ["Import from File" org-table-import (not (org-at-table-p))]
  19563. ["Export to File" org-table-export (org-at-table-p)]
  19564. "--"
  19565. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19566. "--"
  19567. ("Plot"
  19568. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19569. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19570. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19571. '("Org"
  19572. ("Show/Hide"
  19573. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19574. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19575. ["Sparse Tree..." org-sparse-tree t]
  19576. ["Reveal Context" org-reveal t]
  19577. ["Show All" show-all t]
  19578. "--"
  19579. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19580. "--"
  19581. ["New Heading" org-insert-heading t]
  19582. ("Navigate Headings"
  19583. ["Up" outline-up-heading t]
  19584. ["Next" outline-next-visible-heading t]
  19585. ["Previous" outline-previous-visible-heading t]
  19586. ["Next Same Level" outline-forward-same-level t]
  19587. ["Previous Same Level" outline-backward-same-level t]
  19588. "--"
  19589. ["Jump" org-goto t])
  19590. ("Edit Structure"
  19591. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19592. "--"
  19593. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19594. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19595. "--"
  19596. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19597. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19598. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19599. "--"
  19600. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19601. "--"
  19602. ["Copy visible text" org-copy-visible t]
  19603. "--"
  19604. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19605. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19606. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19607. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19608. "--"
  19609. ["Sort Region/Children" org-sort t]
  19610. "--"
  19611. ["Convert to odd levels" org-convert-to-odd-levels t]
  19612. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19613. ("Editing"
  19614. ["Emphasis..." org-emphasize t]
  19615. ["Edit Source Example" org-edit-special t]
  19616. "--"
  19617. ["Footnote new/jump" org-footnote-action t]
  19618. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19619. ("Archive"
  19620. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19621. "--"
  19622. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19623. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19624. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19625. )
  19626. "--"
  19627. ("Hyperlinks"
  19628. ["Store Link (Global)" org-store-link t]
  19629. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19630. ["Insert Link" org-insert-link t]
  19631. ["Follow Link" org-open-at-point t]
  19632. "--"
  19633. ["Next link" org-next-link t]
  19634. ["Previous link" org-previous-link t]
  19635. "--"
  19636. ["Descriptive Links"
  19637. org-toggle-link-display
  19638. :style radio
  19639. :selected org-descriptive-links
  19640. ]
  19641. ["Literal Links"
  19642. org-toggle-link-display
  19643. :style radio
  19644. :selected (not org-descriptive-links)])
  19645. "--"
  19646. ("TODO Lists"
  19647. ["TODO/DONE/-" org-todo t]
  19648. ("Select keyword"
  19649. ["Next keyword" org-shiftright (org-at-heading-p)]
  19650. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19651. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19652. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19653. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19654. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19655. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19656. "--"
  19657. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19658. :selected org-enforce-todo-dependencies :style toggle :active t]
  19659. "Settings for tree at point"
  19660. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19661. :selected (org-entry-get nil "ORDERED")
  19662. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19663. ["Do Children parallel" org-toggle-ordered-property :style radio
  19664. :selected (not (org-entry-get nil "ORDERED"))
  19665. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19666. "--"
  19667. ["Set Priority" org-priority t]
  19668. ["Priority Up" org-shiftup t]
  19669. ["Priority Down" org-shiftdown t]
  19670. "--"
  19671. ["Get news from all feeds" org-feed-update-all t]
  19672. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19673. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19674. ("TAGS and Properties"
  19675. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19676. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19677. "--"
  19678. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19679. ["Column view of properties" org-columns t]
  19680. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19681. ("Dates and Scheduling"
  19682. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19683. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19684. ("Change Date"
  19685. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19686. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19687. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19688. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19689. ["Compute Time Range" org-evaluate-time-range t]
  19690. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19691. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19692. "--"
  19693. ["Custom time format" org-toggle-time-stamp-overlays
  19694. :style radio :selected org-display-custom-times]
  19695. "--"
  19696. ["Goto Calendar" org-goto-calendar t]
  19697. ["Date from Calendar" org-date-from-calendar t]
  19698. "--"
  19699. ["Start/Restart Timer" org-timer-start t]
  19700. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19701. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19702. ["Insert Timer String" org-timer t]
  19703. ["Insert Timer Item" org-timer-item t])
  19704. ("Logging work"
  19705. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19706. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19707. ["Clock out" org-clock-out t]
  19708. ["Clock cancel" org-clock-cancel t]
  19709. "--"
  19710. ["Mark as default task" org-clock-mark-default-task t]
  19711. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19712. ["Goto running clock" org-clock-goto t]
  19713. "--"
  19714. ["Display times" org-clock-display t]
  19715. ["Create clock table" org-clock-report t]
  19716. "--"
  19717. ["Record DONE time"
  19718. (progn (setq org-log-done (not org-log-done))
  19719. (message "Switching to %s will %s record a timestamp"
  19720. (car org-done-keywords)
  19721. (if org-log-done "automatically" "not")))
  19722. :style toggle :selected org-log-done])
  19723. "--"
  19724. ["Agenda Command..." org-agenda t]
  19725. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19726. ("File List for Agenda")
  19727. ("Special views current file"
  19728. ["TODO Tree" org-show-todo-tree t]
  19729. ["Check Deadlines" org-check-deadlines t]
  19730. ["Timeline" org-timeline t]
  19731. ["Tags/Property tree" org-match-sparse-tree t])
  19732. "--"
  19733. ["Export/Publish..." org-export-dispatch t]
  19734. ("LaTeX"
  19735. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19736. :selected org-cdlatex-mode]
  19737. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19738. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19739. ["Modify math symbol" org-cdlatex-math-modify
  19740. (org-inside-LaTeX-fragment-p)]
  19741. ["Insert citation" org-reftex-citation t]
  19742. "--"
  19743. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19744. "--"
  19745. ("MobileOrg"
  19746. ["Push Files and Views" org-mobile-push t]
  19747. ["Get Captured and Flagged" org-mobile-pull t]
  19748. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19749. "--"
  19750. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19751. "--"
  19752. ("Documentation"
  19753. ["Show Version" org-version t]
  19754. ["Info Documentation" org-info t])
  19755. ("Customize"
  19756. ["Browse Org Group" org-customize t]
  19757. "--"
  19758. ["Expand This Menu" org-create-customize-menu
  19759. (fboundp 'customize-menu-create)])
  19760. ["Send bug report" org-submit-bug-report t]
  19761. "--"
  19762. ("Refresh/Reload"
  19763. ["Refresh setup current buffer" org-mode-restart t]
  19764. ["Reload Org (after update)" org-reload t]
  19765. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19766. ))
  19767. (defun org-info (&optional node)
  19768. "Read documentation for Org-mode in the info system.
  19769. With optional NODE, go directly to that node."
  19770. (interactive)
  19771. (info (format "(org)%s" (or node ""))))
  19772. ;;;###autoload
  19773. (defun org-submit-bug-report ()
  19774. "Submit a bug report on Org-mode via mail.
  19775. Don't hesitate to report any problems or inaccurate documentation.
  19776. If you don't have setup sending mail from (X)Emacs, please copy the
  19777. output buffer into your mail program, as it gives us important
  19778. information about your Org-mode version and configuration."
  19779. (interactive)
  19780. (require 'reporter)
  19781. (org-load-modules-maybe)
  19782. (org-require-autoloaded-modules)
  19783. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19784. (reporter-submit-bug-report
  19785. "emacs-orgmode@gnu.org"
  19786. (org-version nil 'full)
  19787. (let (list)
  19788. (save-window-excursion
  19789. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19790. (delete-other-windows)
  19791. (erase-buffer)
  19792. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19793. We would like to add your full Org-mode and Outline configuration to the
  19794. bug report. This greatly simplifies the work of the maintainer and
  19795. other experts on the mailing list.
  19796. HOWEVER, some variables you have customized may contain private
  19797. information. The names of customers, colleagues, or friends, might
  19798. appear in the form of file names, tags, todo states, or search strings.
  19799. If you answer yes to the prompt, you might want to check and remove
  19800. such private information before sending the email.")
  19801. (add-text-properties (point-min) (point-max) '(face org-warning))
  19802. (when (yes-or-no-p "Include your Org-mode configuration ")
  19803. (mapatoms
  19804. (lambda (v)
  19805. (and (boundp v)
  19806. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19807. (or (and (symbol-value v)
  19808. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19809. (and
  19810. (get v 'custom-type) (get v 'standard-value)
  19811. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19812. (push v list)))))
  19813. (kill-buffer (get-buffer "*Warn about privacy*"))
  19814. list))
  19815. nil nil
  19816. "Remember to cover the basics, that is, what you expected to happen and
  19817. what in fact did happen. You don't know how to make a good report? See
  19818. http://orgmode.org/manual/Feedback.html#Feedback
  19819. Your bug report will be posted to the Org-mode mailing list.
  19820. ------------------------------------------------------------------------")
  19821. (save-excursion
  19822. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19823. (replace-match "\\1Bug: \\3 [\\2]")))))
  19824. (defun org-install-agenda-files-menu ()
  19825. (let ((bl (buffer-list)))
  19826. (save-excursion
  19827. (while bl
  19828. (set-buffer (pop bl))
  19829. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19830. (when (derived-mode-p 'org-mode)
  19831. (easy-menu-change
  19832. '("Org") "File List for Agenda"
  19833. (append
  19834. (list
  19835. ["Edit File List" (org-edit-agenda-file-list) t]
  19836. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19837. ["Remove Current File from List" org-remove-file t]
  19838. ["Cycle through agenda files" org-cycle-agenda-files t]
  19839. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19840. "--")
  19841. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19842. ;;;; Documentation
  19843. (defun org-require-autoloaded-modules ()
  19844. (interactive)
  19845. (mapc 'require
  19846. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19847. org-table org-timer)))
  19848. ;;;###autoload
  19849. (defun org-reload (&optional uncompiled)
  19850. "Reload all org lisp files.
  19851. With prefix arg UNCOMPILED, load the uncompiled versions."
  19852. (interactive "P")
  19853. (require 'loadhist)
  19854. (let* ((org-dir (org-find-library-dir "org"))
  19855. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19856. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19857. (remove-re (mapconcat 'identity
  19858. (mapcar (lambda (f) (concat "^" f "$"))
  19859. (list (if (featurep 'xemacs)
  19860. "org-colview"
  19861. "org-colview-xemacs")
  19862. "org" "org-loaddefs" "org-version"))
  19863. "\\|"))
  19864. (feats (delete-dups
  19865. (mapcar 'file-name-sans-extension
  19866. (mapcar 'file-name-nondirectory
  19867. (delq nil
  19868. (mapcar 'feature-file
  19869. features))))))
  19870. (lfeat (append
  19871. (sort
  19872. (setq feats
  19873. (delq nil (mapcar
  19874. (lambda (f)
  19875. (if (and (string-match feature-re f)
  19876. (not (string-match remove-re f)))
  19877. f nil))
  19878. feats)))
  19879. 'string-lessp)
  19880. (list "org-version" "org")))
  19881. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19882. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19883. load-uncore load-misses)
  19884. (setq load-misses
  19885. (delq 't
  19886. (mapcar (lambda (f)
  19887. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19888. (and (string= org-dir contrib-dir)
  19889. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19890. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19891. (add-to-list 'load-uncore f 'append)
  19892. 't)
  19893. f))
  19894. lfeat)))
  19895. (if load-uncore
  19896. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19897. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19898. (if load-misses
  19899. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19900. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19901. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19902. ;;;###autoload
  19903. (defun org-customize ()
  19904. "Call the customize function with org as argument."
  19905. (interactive)
  19906. (org-load-modules-maybe)
  19907. (org-require-autoloaded-modules)
  19908. (customize-browse 'org))
  19909. (defun org-create-customize-menu ()
  19910. "Create a full customization menu for Org-mode, insert it into the menu."
  19911. (interactive)
  19912. (org-load-modules-maybe)
  19913. (org-require-autoloaded-modules)
  19914. (if (fboundp 'customize-menu-create)
  19915. (progn
  19916. (easy-menu-change
  19917. '("Org") "Customize"
  19918. `(["Browse Org group" org-customize t]
  19919. "--"
  19920. ,(customize-menu-create 'org)
  19921. ["Set" Custom-set t]
  19922. ["Save" Custom-save t]
  19923. ["Reset to Current" Custom-reset-current t]
  19924. ["Reset to Saved" Custom-reset-saved t]
  19925. ["Reset to Standard Settings" Custom-reset-standard t]))
  19926. (message "\"Org\"-menu now contains full customization menu"))
  19927. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19928. ;;;; Miscellaneous stuff
  19929. ;;; Generally useful functions
  19930. (defsubst org-get-at-eol (property n)
  19931. "Get text property PROPERTY at the end of line less N characters."
  19932. (get-text-property (- (point-at-eol) n) property))
  19933. (defun org-find-text-property-in-string (prop s)
  19934. "Return the first non-nil value of property PROP in string S."
  19935. (or (get-text-property 0 prop s)
  19936. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19937. prop s)))
  19938. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19939. "Display the given MESSAGE as a warning."
  19940. (if (fboundp 'display-warning)
  19941. (display-warning 'org message
  19942. (if (featurep 'xemacs) 'warning :warning))
  19943. (let ((buf (get-buffer-create "*Org warnings*")))
  19944. (with-current-buffer buf
  19945. (goto-char (point-max))
  19946. (insert "Warning (Org): " message)
  19947. (unless (bolp)
  19948. (newline)))
  19949. (display-buffer buf)
  19950. (sit-for 0))))
  19951. (defun org-eval (form)
  19952. "Eval FORM and return result."
  19953. (condition-case error
  19954. (eval form)
  19955. (error (format "%%![Error: %s]" error))))
  19956. (defun org-in-clocktable-p ()
  19957. "Check if the cursor is in a clocktable."
  19958. (let ((pos (point)) start)
  19959. (save-excursion
  19960. (end-of-line 1)
  19961. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19962. (setq start (match-beginning 0))
  19963. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19964. (>= (match-end 0) pos)
  19965. start))))
  19966. (defun org-in-verbatim-emphasis ()
  19967. (save-match-data
  19968. (and (org-in-regexp org-emph-re 2)
  19969. (>= (point) (match-beginning 3))
  19970. (<= (point) (match-end 4))
  19971. (member (match-string 3) '("=" "~")))))
  19972. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19973. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19974. (if (and marker (marker-buffer marker)
  19975. (buffer-live-p (marker-buffer marker)))
  19976. (progn
  19977. (org-pop-to-buffer-same-window (marker-buffer marker))
  19978. (if (or (> marker (point-max)) (< marker (point-min)))
  19979. (widen))
  19980. (goto-char marker)
  19981. (org-show-context 'org-goto))
  19982. (if bookmark
  19983. (bookmark-jump bookmark)
  19984. (error "Cannot find location"))))
  19985. (defun org-quote-csv-field (s)
  19986. "Quote field for inclusion in CSV material."
  19987. (if (string-match "[\",]" s)
  19988. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19989. s))
  19990. (defun org-force-self-insert (N)
  19991. "Needed to enforce self-insert under remapping."
  19992. (interactive "p")
  19993. (self-insert-command N))
  19994. (defun org-string-width (s)
  19995. "Compute width of string, ignoring invisible characters.
  19996. This ignores character with invisibility property `org-link', and also
  19997. characters with property `org-cwidth', because these will become invisible
  19998. upon the next fontification round."
  19999. (let (b l)
  20000. (when (or (eq t buffer-invisibility-spec)
  20001. (assq 'org-link buffer-invisibility-spec))
  20002. (while (setq b (text-property-any 0 (length s)
  20003. 'invisible 'org-link s))
  20004. (setq s (concat (substring s 0 b)
  20005. (substring s (or (next-single-property-change
  20006. b 'invisible s) (length s)))))))
  20007. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20008. (setq s (concat (substring s 0 b)
  20009. (substring s (or (next-single-property-change
  20010. b 'org-cwidth s) (length s))))))
  20011. (setq l (string-width s) b -1)
  20012. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20013. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20014. l))
  20015. (defun org-shorten-string (s maxlength)
  20016. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20017. If the string is shorter or has length MAXLENGTH, just return the
  20018. original string. If it is longer, the functions finds a space in the
  20019. string, breaks this string off at that locations and adds three dots
  20020. as ellipsis. Including the ellipsis, the string will not be longer
  20021. than MAXLENGTH. If finding a good breaking point in the string does
  20022. not work, the string is just chopped off in the middle of a word
  20023. if necessary."
  20024. (if (<= (length s) maxlength)
  20025. s
  20026. (let* ((n (max (- maxlength 4) 1))
  20027. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20028. (if (string-match re s)
  20029. (concat (match-string 1 s) "...")
  20030. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20031. (defun org-get-indentation (&optional line)
  20032. "Get the indentation of the current line, interpreting tabs.
  20033. When LINE is given, assume it represents a line and compute its indentation."
  20034. (if line
  20035. (if (string-match "^ *" (org-remove-tabs line))
  20036. (match-end 0))
  20037. (save-excursion
  20038. (beginning-of-line 1)
  20039. (skip-chars-forward " \t")
  20040. (current-column))))
  20041. (defun org-get-string-indentation (s)
  20042. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20043. (let ((n -1) (i 0) (w tab-width) c)
  20044. (catch 'exit
  20045. (while (< (setq n (1+ n)) (length s))
  20046. (setq c (aref s n))
  20047. (cond ((= c ?\ ) (setq i (1+ i)))
  20048. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20049. (t (throw 'exit t)))))
  20050. i))
  20051. (defun org-remove-tabs (s &optional width)
  20052. "Replace tabulators in S with spaces.
  20053. Assumes that s is a single line, starting in column 0."
  20054. (setq width (or width tab-width))
  20055. (while (string-match "\t" s)
  20056. (setq s (replace-match
  20057. (make-string
  20058. (- (* width (/ (+ (match-beginning 0) width) width))
  20059. (match-beginning 0)) ?\ )
  20060. t t s)))
  20061. s)
  20062. (defun org-fix-indentation (line ind)
  20063. "Fix indentation in LINE.
  20064. IND is a cons cell with target and minimum indentation.
  20065. If the current indentation in LINE is smaller than the minimum,
  20066. leave it alone. If it is larger than ind, set it to the target."
  20067. (let* ((l (org-remove-tabs line))
  20068. (i (org-get-indentation l))
  20069. (i1 (car ind)) (i2 (cdr ind)))
  20070. (if (>= i i2) (setq l (substring line i2)))
  20071. (if (> i1 0)
  20072. (concat (make-string i1 ?\ ) l)
  20073. l)))
  20074. (defun org-remove-indentation (code &optional n)
  20075. "Remove the maximum common indentation from the lines in CODE.
  20076. N may optionally be the number of spaces to remove."
  20077. (with-temp-buffer
  20078. (insert code)
  20079. (org-do-remove-indentation n)
  20080. (buffer-string)))
  20081. (defun org-do-remove-indentation (&optional n)
  20082. "Remove the maximum common indentation from the buffer."
  20083. (untabify (point-min) (point-max))
  20084. (let ((min 10000) re)
  20085. (if n
  20086. (setq min n)
  20087. (goto-char (point-min))
  20088. (while (re-search-forward "^ *[^ \n]" nil t)
  20089. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20090. (unless (or (= min 0) (= min 10000))
  20091. (setq re (format "^ \\{%d\\}" min))
  20092. (goto-char (point-min))
  20093. (while (re-search-forward re nil t)
  20094. (replace-match "")
  20095. (end-of-line 1))
  20096. min)))
  20097. (defun org-fill-template (template alist)
  20098. "Find each %key of ALIST in TEMPLATE and replace it."
  20099. (let ((case-fold-search nil)
  20100. entry key value)
  20101. (setq alist (sort (copy-sequence alist)
  20102. (lambda (a b) (< (length (car a)) (length (car b))))))
  20103. (while (setq entry (pop alist))
  20104. (setq template
  20105. (replace-regexp-in-string
  20106. (concat "%" (regexp-quote (car entry)))
  20107. (or (cdr entry) "") template t t)))
  20108. template))
  20109. (defun org-base-buffer (buffer)
  20110. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20111. (if (not buffer)
  20112. buffer
  20113. (or (buffer-base-buffer buffer)
  20114. buffer)))
  20115. (defun org-wrap (string &optional width lines)
  20116. "Wrap string to either a number of lines, or a width in characters.
  20117. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20118. that costs. If there is a word longer than WIDTH, the text is actually
  20119. wrapped to the length of that word.
  20120. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20121. many lines, whatever width that takes.
  20122. The return value is a list of lines, without newlines at the end."
  20123. (let* ((words (org-split-string string "[ \t\n]+"))
  20124. (maxword (apply 'max (mapcar 'org-string-width words)))
  20125. w ll)
  20126. (cond (width
  20127. (org-do-wrap words (max maxword width)))
  20128. (lines
  20129. (setq w maxword)
  20130. (setq ll (org-do-wrap words maxword))
  20131. (if (<= (length ll) lines)
  20132. ll
  20133. (setq ll words)
  20134. (while (> (length ll) lines)
  20135. (setq w (1+ w))
  20136. (setq ll (org-do-wrap words w)))
  20137. ll))
  20138. (t (error "Cannot wrap this")))))
  20139. (defun org-do-wrap (words width)
  20140. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20141. (let (lines line)
  20142. (while words
  20143. (setq line (pop words))
  20144. (while (and words (< (+ (length line) (length (car words))) width))
  20145. (setq line (concat line " " (pop words))))
  20146. (setq lines (push line lines)))
  20147. (nreverse lines)))
  20148. (defun org-split-string (string &optional separators)
  20149. "Splits STRING into substrings at SEPARATORS.
  20150. No empty strings are returned if there are matches at the beginning
  20151. and end of string."
  20152. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  20153. (start 0)
  20154. notfirst
  20155. (list nil))
  20156. (while (and (string-match rexp string
  20157. (if (and notfirst
  20158. (= start (match-beginning 0))
  20159. (< start (length string)))
  20160. (1+ start) start))
  20161. (< (match-beginning 0) (length string)))
  20162. (setq notfirst t)
  20163. (or (eq (match-beginning 0) 0)
  20164. (and (eq (match-beginning 0) (match-end 0))
  20165. (eq (match-beginning 0) start))
  20166. (setq list
  20167. (cons (substring string start (match-beginning 0))
  20168. list)))
  20169. (setq start (match-end 0)))
  20170. (or (eq start (length string))
  20171. (setq list
  20172. (cons (substring string start)
  20173. list)))
  20174. (nreverse list)))
  20175. (defun org-quote-vert (s)
  20176. "Replace \"|\" with \"\\vert\"."
  20177. (while (string-match "|" s)
  20178. (setq s (replace-match "\\vert" t t s)))
  20179. s)
  20180. (defun org-uuidgen-p (s)
  20181. "Is S an ID created by UUIDGEN?"
  20182. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20183. (defun org-in-src-block-p (&optional inside)
  20184. "Whether point is in a code source block.
  20185. When INSIDE is non-nil, don't consider we are within a src block
  20186. when point is at #+BEGIN_SRC or #+END_SRC."
  20187. (let ((case-fold-search t) ov)
  20188. (or (and (eq (get-char-property (point) 'src-block) t))
  20189. (and (not inside)
  20190. (save-match-data
  20191. (save-excursion
  20192. (beginning-of-line)
  20193. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20194. (defun org-context ()
  20195. "Return a list of contexts of the current cursor position.
  20196. If several contexts apply, all are returned.
  20197. Each context entry is a list with a symbol naming the context, and
  20198. two positions indicating start and end of the context. Possible
  20199. contexts are:
  20200. :headline anywhere in a headline
  20201. :headline-stars on the leading stars in a headline
  20202. :todo-keyword on a TODO keyword (including DONE) in a headline
  20203. :tags on the TAGS in a headline
  20204. :priority on the priority cookie in a headline
  20205. :item on the first line of a plain list item
  20206. :item-bullet on the bullet/number of a plain list item
  20207. :checkbox on the checkbox in a plain list item
  20208. :table in an org-mode table
  20209. :table-special on a special filed in a table
  20210. :table-table in a table.el table
  20211. :clocktable in a clocktable
  20212. :src-block in a source block
  20213. :link on a hyperlink
  20214. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20215. :target on a <<target>>
  20216. :radio-target on a <<<radio-target>>>
  20217. :latex-fragment on a LaTeX fragment
  20218. :latex-preview on a LaTeX fragment with overlaid preview image
  20219. This function expects the position to be visible because it uses font-lock
  20220. faces as a help to recognize the following contexts: :table-special, :link,
  20221. and :keyword."
  20222. (let* ((f (get-text-property (point) 'face))
  20223. (faces (if (listp f) f (list f)))
  20224. (case-fold-search t)
  20225. (p (point)) clist o)
  20226. ;; First the large context
  20227. (cond
  20228. ((org-at-heading-p t)
  20229. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20230. (when (progn
  20231. (beginning-of-line 1)
  20232. (looking-at org-todo-line-tags-regexp))
  20233. (push (org-point-in-group p 1 :headline-stars) clist)
  20234. (push (org-point-in-group p 2 :todo-keyword) clist)
  20235. (push (org-point-in-group p 4 :tags) clist))
  20236. (goto-char p)
  20237. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20238. (if (looking-at "\\[#[A-Z0-9]\\]")
  20239. (push (org-point-in-group p 0 :priority) clist)))
  20240. ((org-at-item-p)
  20241. (push (org-point-in-group p 2 :item-bullet) clist)
  20242. (push (list :item (point-at-bol)
  20243. (save-excursion (org-end-of-item) (point)))
  20244. clist)
  20245. (and (org-at-item-checkbox-p)
  20246. (push (org-point-in-group p 0 :checkbox) clist)))
  20247. ((org-at-table-p)
  20248. (push (list :table (org-table-begin) (org-table-end)) clist)
  20249. (if (memq 'org-formula faces)
  20250. (push (list :table-special
  20251. (previous-single-property-change p 'face)
  20252. (next-single-property-change p 'face)) clist)))
  20253. ((org-at-table-p 'any)
  20254. (push (list :table-table) clist)))
  20255. (goto-char p)
  20256. (let ((case-fold-search t))
  20257. ;; New the "medium" contexts: clocktables, source blocks
  20258. (cond ((org-in-clocktable-p)
  20259. (push (list :clocktable
  20260. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20261. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20262. (match-beginning 1))
  20263. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20264. (match-end 0))) clist))
  20265. ((org-in-src-block-p)
  20266. (push (list :src-block
  20267. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20268. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20269. (match-beginning 1))
  20270. (and (search-forward "#+END_SRC" nil t)
  20271. (match-beginning 0))) clist))))
  20272. (goto-char p)
  20273. ;; Now the small context
  20274. (cond
  20275. ((org-at-timestamp-p)
  20276. (push (org-point-in-group p 0 :timestamp) clist))
  20277. ((memq 'org-link faces)
  20278. (push (list :link
  20279. (previous-single-property-change p 'face)
  20280. (next-single-property-change p 'face)) clist))
  20281. ((memq 'org-special-keyword faces)
  20282. (push (list :keyword
  20283. (previous-single-property-change p 'face)
  20284. (next-single-property-change p 'face)) clist))
  20285. ((org-at-target-p)
  20286. (push (org-point-in-group p 0 :target) clist)
  20287. (goto-char (1- (match-beginning 0)))
  20288. (if (looking-at org-radio-target-regexp)
  20289. (push (org-point-in-group p 0 :radio-target) clist))
  20290. (goto-char p))
  20291. ((setq o (car (delq nil
  20292. (mapcar
  20293. (lambda (x)
  20294. (if (memq x org-latex-fragment-image-overlays) x))
  20295. (overlays-at (point))))))
  20296. (push (list :latex-fragment
  20297. (overlay-start o) (overlay-end o)) clist)
  20298. (push (list :latex-preview
  20299. (overlay-start o) (overlay-end o)) clist))
  20300. ((org-inside-LaTeX-fragment-p)
  20301. ;; FIXME: positions wrong.
  20302. (push (list :latex-fragment (point) (point)) clist)))
  20303. (setq clist (nreverse (delq nil clist)))
  20304. clist))
  20305. ;; FIXME: Compare with at-regexp-p Do we need both?
  20306. (defun org-in-regexp (re &optional nlines visually)
  20307. "Check if point is inside a match of regexp.
  20308. Normally only the current line is checked, but you can include NLINES extra
  20309. lines both before and after point into the search.
  20310. If VISUALLY is set, require that the cursor is not after the match but
  20311. really on, so that the block visually is on the match."
  20312. (catch 'exit
  20313. (let ((pos (point))
  20314. (eol (point-at-eol (+ 1 (or nlines 0))))
  20315. (inc (if visually 1 0)))
  20316. (save-excursion
  20317. (beginning-of-line (- 1 (or nlines 0)))
  20318. (while (re-search-forward re eol t)
  20319. (if (and (<= (match-beginning 0) pos)
  20320. (>= (+ inc (match-end 0)) pos))
  20321. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20322. (defun org-at-regexp-p (regexp)
  20323. "Is point inside a match of REGEXP in the current line?"
  20324. (catch 'exit
  20325. (save-excursion
  20326. (let ((pos (point)) (end (point-at-eol)))
  20327. (beginning-of-line 1)
  20328. (while (re-search-forward regexp end t)
  20329. (if (and (<= (match-beginning 0) pos)
  20330. (>= (match-end 0) pos))
  20331. (throw 'exit t)))
  20332. nil))))
  20333. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20334. "Non-nil when point is between matches of START-RE and END-RE.
  20335. Also return a non-nil value when point is on one of the matches.
  20336. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20337. buffer positions. Default values are the positions of headlines
  20338. surrounding the point.
  20339. The functions returns a cons cell whose car (resp. cdr) is the
  20340. position before START-RE (resp. after END-RE)."
  20341. (save-match-data
  20342. (let ((pos (point))
  20343. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20344. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20345. beg end)
  20346. (save-excursion
  20347. ;; Point is on a block when on START-RE or if START-RE can be
  20348. ;; found before it...
  20349. (and (or (org-at-regexp-p start-re)
  20350. (re-search-backward start-re limit-up t))
  20351. (setq beg (match-beginning 0))
  20352. ;; ... and END-RE after it...
  20353. (goto-char (match-end 0))
  20354. (re-search-forward end-re limit-down t)
  20355. (> (setq end (match-end 0)) pos)
  20356. ;; ... without another START-RE in-between.
  20357. (goto-char (match-beginning 0))
  20358. (not (re-search-backward start-re (1+ beg) t))
  20359. ;; Return value.
  20360. (cons beg end))))))
  20361. (defun org-in-block-p (names)
  20362. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20363. NAMES is a list of strings containing names of blocks.
  20364. Return first block name matched, or nil. Beware that in case of
  20365. nested blocks, the returned name may not belong to the closest
  20366. block from point."
  20367. (save-match-data
  20368. (catch 'exit
  20369. (let ((case-fold-search t)
  20370. (lim-up (save-excursion (outline-previous-heading)))
  20371. (lim-down (save-excursion (outline-next-heading))))
  20372. (mapc (lambda (name)
  20373. (let ((n (regexp-quote name)))
  20374. (when (org-between-regexps-p
  20375. (concat "^[ \t]*#\\+begin_" n)
  20376. (concat "^[ \t]*#\\+end_" n)
  20377. lim-up lim-down)
  20378. (throw 'exit n))))
  20379. names))
  20380. nil)))
  20381. (defun org-in-drawer-p ()
  20382. "Non-nil if point is within a drawer.
  20383. If point is within a drawer, return it, as parsed data."
  20384. (let ((element (save-match-data (org-element-at-point))))
  20385. (while (and element (not (memq (org-element-type element)
  20386. '(drawer property-drawer))))
  20387. (setq element (org-element-property :parent element)))
  20388. element))
  20389. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20390. "Call `multi-occur' with buffers for all agenda files."
  20391. (interactive "sOrg-files matching: \np")
  20392. (let* ((files (org-agenda-files))
  20393. (tnames (mapcar 'file-truename files))
  20394. (extra org-agenda-text-search-extra-files)
  20395. f)
  20396. (when (eq (car extra) 'agenda-archives)
  20397. (setq extra (cdr extra))
  20398. (setq files (org-add-archive-files files)))
  20399. (while (setq f (pop extra))
  20400. (unless (member (file-truename f) tnames)
  20401. (add-to-list 'files f 'append)
  20402. (add-to-list 'tnames (file-truename f) 'append)))
  20403. (multi-occur
  20404. (mapcar (lambda (x)
  20405. (with-current-buffer
  20406. (or (get-file-buffer x) (find-file-noselect x))
  20407. (widen)
  20408. (current-buffer)))
  20409. files)
  20410. regexp)))
  20411. (if (boundp 'occur-mode-find-occurrence-hook)
  20412. ;; Emacs 23
  20413. (add-hook 'occur-mode-find-occurrence-hook
  20414. (lambda ()
  20415. (when (derived-mode-p 'org-mode)
  20416. (org-reveal))))
  20417. ;; Emacs 22
  20418. (defadvice occur-mode-goto-occurrence
  20419. (after org-occur-reveal activate)
  20420. (and (derived-mode-p 'org-mode) (org-reveal)))
  20421. (defadvice occur-mode-goto-occurrence-other-window
  20422. (after org-occur-reveal activate)
  20423. (and (derived-mode-p 'org-mode) (org-reveal)))
  20424. (defadvice occur-mode-display-occurrence
  20425. (after org-occur-reveal activate)
  20426. (when (derived-mode-p 'org-mode)
  20427. (let ((pos (occur-mode-find-occurrence)))
  20428. (with-current-buffer (marker-buffer pos)
  20429. (save-excursion
  20430. (goto-char pos)
  20431. (org-reveal)))))))
  20432. (defun org-occur-link-in-agenda-files ()
  20433. "Create a link and search for it in the agendas.
  20434. The link is not stored in `org-stored-links', it is just created
  20435. for the search purpose."
  20436. (interactive)
  20437. (let ((link (condition-case nil
  20438. (org-store-link nil)
  20439. (error "Unable to create a link to here"))))
  20440. (org-occur-in-agenda-files (regexp-quote link))))
  20441. (defun org-reverse-string (string)
  20442. "Return the reverse of STRING."
  20443. (apply 'string (reverse (string-to-list string))))
  20444. ;; defsubst org-uniquify must be defined before first use
  20445. (defun org-uniquify-alist (alist)
  20446. "Merge elements of ALIST with the same key.
  20447. For example, in this alist:
  20448. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20449. => '((a 1 3) (b 2))
  20450. merge (a 1) and (a 3) into (a 1 3).
  20451. The function returns the new ALIST."
  20452. (let (rtn)
  20453. (mapc
  20454. (lambda (e)
  20455. (let (n)
  20456. (if (not (assoc (car e) rtn))
  20457. (push e rtn)
  20458. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20459. (setq rtn (assq-delete-all (car e) rtn))
  20460. (push n rtn))))
  20461. alist)
  20462. rtn))
  20463. (defun org-delete-all (elts list)
  20464. "Remove all elements in ELTS from LIST."
  20465. (while elts
  20466. (setq list (delete (pop elts) list)))
  20467. list)
  20468. (defun org-count (cl-item cl-seq)
  20469. "Count the number of occurrences of ITEM in SEQ.
  20470. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20471. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20472. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20473. (while (< cl-start cl-end)
  20474. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20475. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20476. (setq cl-start (1+ cl-start)))
  20477. cl-count))
  20478. (defun org-remove-if (predicate seq)
  20479. "Remove everything from SEQ that fulfills PREDICATE."
  20480. (let (res e)
  20481. (while seq
  20482. (setq e (pop seq))
  20483. (if (not (funcall predicate e)) (push e res)))
  20484. (nreverse res)))
  20485. (defun org-remove-if-not (predicate seq)
  20486. "Remove everything from SEQ that does not fulfill PREDICATE."
  20487. (let (res e)
  20488. (while seq
  20489. (setq e (pop seq))
  20490. (if (funcall predicate e) (push e res)))
  20491. (nreverse res)))
  20492. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20493. "Reduce two-argument FUNCTION across SEQ.
  20494. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20495. \":initial-value\" removed."
  20496. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20497. (cadr (memq :initial-value cl-keys)))
  20498. (cl-seq (pop cl-seq))
  20499. (t (funcall cl-func)))))
  20500. (while cl-seq
  20501. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20502. cl-accum))
  20503. (defun org-every (pred seq)
  20504. "Return true if PREDICATE is true of every element of SEQ.
  20505. Adapted from `every' in cl.el."
  20506. (catch 'org-every
  20507. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20508. t))
  20509. (defun org-some (pred seq)
  20510. "Return true if PREDICATE is true of any element of SEQ.
  20511. Adapted from `some' in cl.el."
  20512. (catch 'org-some
  20513. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20514. nil))
  20515. (defun org-back-over-empty-lines ()
  20516. "Move backwards over whitespace, to the beginning of the first empty line.
  20517. Returns the number of empty lines passed."
  20518. (let ((pos (point)))
  20519. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20520. (skip-chars-backward " \t\n\r")
  20521. (unless (eobp)
  20522. (forward-line -1)))
  20523. (beginning-of-line 2)
  20524. (goto-char (min (point) pos))
  20525. (count-lines (point) pos)))
  20526. (defun org-skip-whitespace ()
  20527. (skip-chars-forward " \t\n\r"))
  20528. (defun org-point-in-group (point group &optional context)
  20529. "Check if POINT is in match-group GROUP.
  20530. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20531. match. If the match group does not exist or point is not inside it,
  20532. return nil."
  20533. (and (match-beginning group)
  20534. (>= point (match-beginning group))
  20535. (<= point (match-end group))
  20536. (if context
  20537. (list context (match-beginning group) (match-end group))
  20538. t)))
  20539. (defun org-switch-to-buffer-other-window (&rest args)
  20540. "Switch to buffer in a second window on the current frame.
  20541. In particular, do not allow pop-up frames.
  20542. Returns the newly created buffer."
  20543. (org-no-popups
  20544. (apply 'switch-to-buffer-other-window args)))
  20545. (defun org-combine-plists (&rest plists)
  20546. "Create a single property list from all plists in PLISTS.
  20547. The process starts by copying the first list, and then setting properties
  20548. from the other lists. Settings in the last list are the most significant
  20549. ones and overrule settings in the other lists."
  20550. (let ((rtn (copy-sequence (pop plists)))
  20551. p v ls)
  20552. (while plists
  20553. (setq ls (pop plists))
  20554. (while ls
  20555. (setq p (pop ls) v (pop ls))
  20556. (setq rtn (plist-put rtn p v))))
  20557. rtn))
  20558. (defun org-replace-escapes (string table)
  20559. "Replace %-escapes in STRING with values in TABLE.
  20560. TABLE is an association list with keys like \"%a\" and string values.
  20561. The sequences in STRING may contain normal field width and padding information,
  20562. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20563. so values can contain further %-escapes if they are define later in TABLE."
  20564. (let ((tbl (copy-alist table))
  20565. (case-fold-search nil)
  20566. (pchg 0)
  20567. e re rpl)
  20568. (while (setq e (pop tbl))
  20569. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20570. (when (and (cdr e) (string-match re (cdr e)))
  20571. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20572. (safe "SREF"))
  20573. (add-text-properties 0 3 (list 'sref sref) safe)
  20574. (setcdr e (replace-match safe t t (cdr e)))))
  20575. (while (string-match re string)
  20576. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20577. (cdr e)))
  20578. (setq string (replace-match rpl t t string))))
  20579. (while (setq pchg (next-property-change pchg string))
  20580. (let ((sref (get-text-property pchg 'sref string)))
  20581. (when (and sref (string-match "SREF" string pchg))
  20582. (setq string (replace-match sref t t string)))))
  20583. string))
  20584. (defun org-sublist (list start end)
  20585. "Return a section of LIST, from START to END.
  20586. Counting starts at 1."
  20587. (let (rtn (c start))
  20588. (setq list (nthcdr (1- start) list))
  20589. (while (and list (<= c end))
  20590. (push (pop list) rtn)
  20591. (setq c (1+ c)))
  20592. (nreverse rtn)))
  20593. (defun org-find-base-buffer-visiting (file)
  20594. "Like `find-buffer-visiting' but always return the base buffer and
  20595. not an indirect buffer."
  20596. (let ((buf (or (get-file-buffer file)
  20597. (find-buffer-visiting file))))
  20598. (if buf
  20599. (or (buffer-base-buffer buf) buf)
  20600. nil)))
  20601. (defun org-image-file-name-regexp (&optional extensions)
  20602. "Return regexp matching the file names of images.
  20603. If EXTENSIONS is given, only match these."
  20604. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20605. (image-file-name-regexp)
  20606. (let ((image-file-name-extensions
  20607. (or extensions
  20608. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20609. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20610. (concat "\\."
  20611. (regexp-opt (nconc (mapcar 'upcase
  20612. image-file-name-extensions)
  20613. image-file-name-extensions)
  20614. t)
  20615. "\\'"))))
  20616. (defun org-file-image-p (file &optional extensions)
  20617. "Return non-nil if FILE is an image."
  20618. (save-match-data
  20619. (string-match (org-image-file-name-regexp extensions) file)))
  20620. (defun org-get-cursor-date (&optional with-time)
  20621. "Return the date at cursor in as a time.
  20622. This works in the calendar and in the agenda, anywhere else it just
  20623. returns the current time.
  20624. If WITH-TIME is non-nil, returns the time of the event at point (in
  20625. the agenda) or the current time of the day."
  20626. (let (date day defd tp tm hod mod)
  20627. (when with-time
  20628. (setq tp (get-text-property (point) 'time))
  20629. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20630. (setq hod (string-to-number (match-string 1 tp))
  20631. mod (string-to-number (match-string 2 tp))))
  20632. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20633. mod (nth 1 (decode-time (current-time))))))
  20634. (cond
  20635. ((eq major-mode 'calendar-mode)
  20636. (setq date (calendar-cursor-to-date)
  20637. defd (encode-time 0 (or mod 0) (or hod 0)
  20638. (nth 1 date) (nth 0 date) (nth 2 date))))
  20639. ((eq major-mode 'org-agenda-mode)
  20640. (setq day (get-text-property (point) 'day))
  20641. (if day
  20642. (setq date (calendar-gregorian-from-absolute day)
  20643. defd (encode-time 0 (or mod 0) (or hod 0)
  20644. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20645. (or defd (current-time))))
  20646. (defun org-mark-subtree (&optional up)
  20647. "Mark the current subtree.
  20648. This puts point at the start of the current subtree, and mark at
  20649. the end. If a numeric prefix UP is given, move up into the
  20650. hierarchy of headlines by UP levels before marking the subtree."
  20651. (interactive "P")
  20652. (org-with-limited-levels
  20653. (cond ((org-at-heading-p) (beginning-of-line))
  20654. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20655. (t (outline-previous-visible-heading 1))))
  20656. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20657. (if (org-called-interactively-p 'any)
  20658. (call-interactively 'org-mark-element)
  20659. (org-mark-element)))
  20660. ;;; Indentation
  20661. (defun org--get-expected-indentation (element contentsp)
  20662. "Expected indentation column for current line, according to ELEMENT.
  20663. ELEMENT is an element containing point. CONTENTSP is non-nil
  20664. when indentation is to be computed according to contents of
  20665. ELEMENT."
  20666. (let ((type (org-element-type element))
  20667. (start (org-element-property :begin element)))
  20668. (org-with-wide-buffer
  20669. (cond
  20670. (contentsp
  20671. (case type
  20672. (footnote-definition 0)
  20673. ((headline inlinetask nil)
  20674. (if (not org-adapt-indentation) 0
  20675. (let ((level (org-current-level)))
  20676. (if level (1+ level) 0))))
  20677. (item
  20678. (org-list-item-body-column
  20679. (org-element-property :post-affiliated element)))
  20680. (plain-list
  20681. (save-excursion
  20682. (goto-char (org-element-property :post-affiliated element))
  20683. (org-get-indentation)))
  20684. (otherwise
  20685. (goto-char start)
  20686. (org-get-indentation))))
  20687. ((memq type '(headline inlinetask nil))
  20688. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20689. (org--get-expected-indentation element t)
  20690. 0))
  20691. ((eq type 'footnote-definition) 0)
  20692. ;; First paragraph of a footnote definition or an item.
  20693. ;; Indent like parent.
  20694. ((< (line-beginning-position) start)
  20695. (org--get-expected-indentation
  20696. (org-element-property :parent element) t))
  20697. ;; At first line: indent according to previous sibling, if any,
  20698. ;; ignoring footnote definitions and inline tasks, or parent's
  20699. ;; contents.
  20700. ((= (line-beginning-position) start)
  20701. (catch 'exit
  20702. (while t
  20703. (if (= (point-min) start) (throw 'exit 0)
  20704. (goto-char (1- start))
  20705. (let* ((previous (org-element-at-point))
  20706. (parent previous))
  20707. (while (and parent (<= (org-element-property :end parent) start))
  20708. (setq previous parent
  20709. parent (org-element-property :parent parent)))
  20710. (cond
  20711. ((not previous) (throw 'exit 0))
  20712. ((> (org-element-property :end previous) start)
  20713. (throw 'exit (org--get-expected-indentation previous t)))
  20714. ((memq (org-element-type previous)
  20715. '(footnote-definition inlinetask))
  20716. (setq start (org-element-property :begin previous)))
  20717. (t (goto-char (org-element-property :begin previous))
  20718. (throw 'exit
  20719. (if (bolp) (org-get-indentation)
  20720. ;; At first paragraph in an item or
  20721. ;; a footnote definition.
  20722. (org--get-expected-indentation
  20723. (org-element-property :parent previous) t))))))))))
  20724. ;; Otherwise, move to the first non-blank line above.
  20725. (t
  20726. (beginning-of-line)
  20727. (let ((pos (point)))
  20728. (skip-chars-backward " \r\t\n")
  20729. (cond
  20730. ;; Two blank lines end a footnote definition or a plain
  20731. ;; list. When we indent an empty line after them, the
  20732. ;; containing list or footnote definition is over, so it
  20733. ;; qualifies as a previous sibling. Therefore, we indent
  20734. ;; like its first line.
  20735. ((and (memq type '(footnote-definition plain-list))
  20736. (> (count-lines (point) pos) 2))
  20737. (goto-char start)
  20738. (org-get-indentation))
  20739. ;; Line above is the first one of a paragraph at the
  20740. ;; beginning of an item or a footnote definition. Indent
  20741. ;; like parent.
  20742. ((< (line-beginning-position) start)
  20743. (org--get-expected-indentation
  20744. (org-element-property :parent element) t))
  20745. ;; POS is after contents in a greater element. Indent like
  20746. ;; the beginning of the element.
  20747. ;;
  20748. ;; As a special case, if point is at the end of a footnote
  20749. ;; definition or an item, indent like the very last element
  20750. ;; within.
  20751. ((and (not (eq type 'paragraph))
  20752. (let ((cend (org-element-property :contents-end element)))
  20753. (and cend (<= cend pos))))
  20754. (if (memq type '(footnote-definition item plain-list))
  20755. (org--get-expected-indentation (org-element-at-point) nil)
  20756. (goto-char start)
  20757. (org-get-indentation)))
  20758. ;; In any other case, indent like the current line.
  20759. (t (org-get-indentation)))))))))
  20760. (defun org--align-node-property ()
  20761. "Align node property at point.
  20762. Alignment is done according to `org-property-format', which see."
  20763. (when (save-excursion
  20764. (beginning-of-line)
  20765. (looking-at org-property-re))
  20766. (replace-match
  20767. (concat (match-string 4)
  20768. (org-trim
  20769. (format org-property-format (match-string 1) (match-string 3))))
  20770. t t)))
  20771. (defun org-indent-line ()
  20772. "Indent line depending on context.
  20773. Indentation is done according to the following rules:
  20774. - Footnote definitions, headlines and inline tasks have to
  20775. start at column 0.
  20776. - On the very first line of an element, consider, in order, the
  20777. next rules until one matches:
  20778. 1. If there's a sibling element before, ignoring footnote
  20779. definitions and inline tasks, indent like its first line.
  20780. 2. If element has a parent, indent like its contents. More
  20781. precisely, if parent is an item, indent after the
  20782. description part, if any, or the bullet (see
  20783. `org-list-description-max-indent'). Else, indent like
  20784. parent's first line.
  20785. 3. Otherwise, indent relatively to current level, if
  20786. `org-adapt-indentation' is non-nil, or to left margin.
  20787. - On a blank line at the end of an element, indent according to
  20788. the type of the element. More precisely
  20789. 1. If element is a plain list, an item, or a footnote
  20790. definition, indent like the very last element within.
  20791. 2. If element is a paragraph, indent like its last non blank
  20792. line.
  20793. 3. Otherwise, indent like its very first line.
  20794. - In the code part of a source block, use language major mode
  20795. to indent current line if `org-src-tab-acts-natively' is
  20796. non-nil. If it is nil, do nothing.
  20797. - Otherwise, indent like the first non-blank line above.
  20798. The function doesn't indent an item as it could break the whole
  20799. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  20800. \\[org-shiftmetaright].
  20801. Also align node properties according to `org-property-format'."
  20802. (interactive)
  20803. (cond
  20804. (orgstruct-is-++
  20805. (let ((indent-line-function
  20806. (cadadr (assq 'indent-line-function org-fb-vars))))
  20807. (indent-according-to-mode)))
  20808. ((org-at-heading-p) 'noindent)
  20809. (t
  20810. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  20811. (type (org-element-type element)))
  20812. (cond ((and (memq type '(plain-list item))
  20813. (= (line-beginning-position)
  20814. (org-element-property :post-affiliated element)))
  20815. 'noindent)
  20816. ((and (eq type 'src-block)
  20817. org-src-tab-acts-natively
  20818. (> (line-beginning-position)
  20819. (org-element-property :post-affiliated element))
  20820. (< (line-beginning-position)
  20821. (org-with-wide-buffer
  20822. (goto-char (org-element-property :end element))
  20823. (skip-chars-backward " \r\t\n")
  20824. (line-beginning-position))))
  20825. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  20826. (t
  20827. (let ((column (org--get-expected-indentation element nil)))
  20828. ;; Preserve current column.
  20829. (if (<= (current-column) (current-indentation))
  20830. (org-indent-line-to column)
  20831. (save-excursion (org-indent-line-to column))))
  20832. ;; Align node property. Also preserve current column.
  20833. (when (eq type 'node-property)
  20834. (let ((column (current-column)))
  20835. (org--align-node-property)
  20836. (org-move-to-column column)))))))))
  20837. (defun org-indent-region (start end)
  20838. "Indent each non-blank line in the region.
  20839. Called from a program, START and END specify the region to
  20840. indent. The function will not indent contents of example blocks,
  20841. verse blocks and export blocks as leading white spaces are
  20842. assumed to be significant there."
  20843. (interactive "r")
  20844. (save-excursion
  20845. (goto-char start)
  20846. (skip-chars-forward " \r\t\n")
  20847. (unless (eobp) (beginning-of-line))
  20848. (let ((indent-to
  20849. (lambda (ind pos)
  20850. ;; Set IND as indentation for all lines between point and
  20851. ;; POS or END, whichever comes first. Blank lines are
  20852. ;; ignored. Leave point after POS once done.
  20853. (let ((limit (copy-marker (min end pos))))
  20854. (while (< (point) limit)
  20855. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  20856. (forward-line))
  20857. (set-marker limit nil))))
  20858. (end (copy-marker end)))
  20859. (while (< (point) end)
  20860. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  20861. (let* ((element (org-element-at-point))
  20862. (type (org-element-type element))
  20863. (element-end (copy-marker (org-element-property :end element)))
  20864. (ind (org--get-expected-indentation element nil)))
  20865. (cond
  20866. ((or (memq type '(paragraph table table-row))
  20867. (not (or (org-element-property :contents-begin element)
  20868. (memq type
  20869. '(example-block export-block src-block)))))
  20870. ;; Elements here are indented as a single block. Also
  20871. ;; align node properties.
  20872. (when (eq type 'node-property)
  20873. (org--align-node-property)
  20874. (beginning-of-line))
  20875. (funcall indent-to ind element-end))
  20876. (t
  20877. ;; Elements in this category consist of three parts:
  20878. ;; before the contents, the contents, and after the
  20879. ;; contents. The contents are treated specially,
  20880. ;; according to the element type, or not indented at
  20881. ;; all. Other parts are indented as a single block.
  20882. (let* ((post (copy-marker
  20883. (org-element-property :post-affiliated element)))
  20884. (cbeg
  20885. (copy-marker
  20886. (cond
  20887. ((not (org-element-property :contents-begin element))
  20888. ;; Fake contents for source blocks.
  20889. (org-with-wide-buffer
  20890. (goto-char post)
  20891. (forward-line)
  20892. (point)))
  20893. ((memq type '(footnote-definition item plain-list))
  20894. ;; Contents in these elements could start on
  20895. ;; the same line as the beginning of the
  20896. ;; element. Make sure we start indenting
  20897. ;; from the second line.
  20898. (org-with-wide-buffer
  20899. (goto-char post)
  20900. (end-of-line)
  20901. (skip-chars-forward " \r\t\n")
  20902. (if (eobp) (point) (line-beginning-position))))
  20903. (t (org-element-property :contents-begin element)))))
  20904. (cend (copy-marker
  20905. (or (org-element-property :contents-end element)
  20906. ;; Fake contents for source blocks.
  20907. (org-with-wide-buffer
  20908. (goto-char element-end)
  20909. (skip-chars-backward " \r\t\n")
  20910. (line-beginning-position)))
  20911. t)))
  20912. ;; Do not change items indentation individually as it
  20913. ;; might break the list as a whole. On the other
  20914. ;; hand, when at a plain list, indent it as a whole.
  20915. (cond ((eq type 'plain-list)
  20916. (let ((offset (- ind (org-get-indentation))))
  20917. (unless (zerop offset)
  20918. (indent-rigidly (org-element-property :begin element)
  20919. (org-element-property :end element)
  20920. offset))
  20921. (goto-char cbeg)))
  20922. ((eq type 'item) (goto-char cbeg))
  20923. (t (funcall indent-to ind cbeg)))
  20924. (when (< (point) end)
  20925. (case type
  20926. ((example-block export-block verse-block))
  20927. (src-block
  20928. ;; In a source block, indent source code
  20929. ;; according to language major mode, but only if
  20930. ;; `org-src-tab-acts-natively' is non-nil.
  20931. (when (and (< (point) end) org-src-tab-acts-natively)
  20932. (ignore-errors
  20933. (org-babel-do-in-edit-buffer
  20934. (indent-region (point-min) (point-max))))))
  20935. (t (org-indent-region (point) (min cend end))))
  20936. (goto-char (min cend end))
  20937. (when (< (point) end) (funcall indent-to ind element-end)))
  20938. (set-marker post nil)
  20939. (set-marker cbeg nil)
  20940. (set-marker cend nil))))
  20941. (set-marker element-end nil))))
  20942. (set-marker end nil))))
  20943. (defun org-indent-drawer ()
  20944. "Indent the drawer at point."
  20945. (interactive)
  20946. (unless (save-excursion
  20947. (beginning-of-line)
  20948. (org-looking-at-p org-drawer-regexp))
  20949. (user-error "Not at a drawer"))
  20950. (let ((element (org-element-at-point)))
  20951. (unless (memq (org-element-type element) '(drawer property-drawer))
  20952. (user-error "Not at a drawer"))
  20953. (org-with-wide-buffer
  20954. (org-indent-region (org-element-property :begin element)
  20955. (org-element-property :end element))))
  20956. (message "Drawer at point indented"))
  20957. (defun org-indent-block ()
  20958. "Indent the block at point."
  20959. (interactive)
  20960. (unless (save-excursion
  20961. (beginning-of-line)
  20962. (let ((case-fold-search t))
  20963. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  20964. (user-error "Not at a block"))
  20965. (let ((element (org-element-at-point)))
  20966. (unless (memq (org-element-type element)
  20967. '(comment-block center-block dynamic-block example-block
  20968. export-block quote-block special-block
  20969. src-block verse-block))
  20970. (user-error "Not at a block"))
  20971. (org-with-wide-buffer
  20972. (org-indent-region (org-element-property :begin element)
  20973. (org-element-property :end element))))
  20974. (message "Block at point indented"))
  20975. ;;; Filling
  20976. ;; We use our own fill-paragraph and auto-fill functions.
  20977. ;; `org-fill-paragraph' relies on adaptive filling and context
  20978. ;; checking. Appropriate `fill-prefix' is computed with
  20979. ;; `org-adaptive-fill-function'.
  20980. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20981. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20982. ;; `org-adaptive-fill-function' value. Internally,
  20983. ;; `org-comment-line-break-function' breaks the line.
  20984. ;; `org-setup-filling' installs filling and auto-filling related
  20985. ;; variables during `org-mode' initialization.
  20986. (defvar org-element-paragraph-separate) ; org-element.el
  20987. (defun org-setup-filling ()
  20988. (require 'org-element)
  20989. ;; Prevent auto-fill from inserting unwanted new items.
  20990. (when (boundp 'fill-nobreak-predicate)
  20991. (org-set-local
  20992. 'fill-nobreak-predicate
  20993. (org-uniquify
  20994. (append fill-nobreak-predicate
  20995. '(org-fill-line-break-nobreak-p
  20996. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20997. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20998. (org-set-local 'paragraph-start paragraph-ending)
  20999. (org-set-local 'paragraph-separate paragraph-ending))
  21000. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21001. (org-set-local 'auto-fill-inhibit-regexp nil)
  21002. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21003. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21004. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21005. (defun org-fill-line-break-nobreak-p ()
  21006. "Non-nil when a new line at point would create an Org line break."
  21007. (save-excursion
  21008. (skip-chars-backward "[ \t]")
  21009. (skip-chars-backward "\\\\")
  21010. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21011. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21012. "Non-nil when a new line at point would split a timestamp."
  21013. (and (org-at-timestamp-p t)
  21014. (not (looking-at org-ts-regexp-both))))
  21015. (declare-function message-in-body-p "message" ())
  21016. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21017. (defun org-adaptive-fill-function ()
  21018. "Compute a fill prefix for the current line.
  21019. Return fill prefix, as a string, or nil if current line isn't
  21020. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21021. matches in paragraphs or comments, use it."
  21022. (catch 'exit
  21023. (when (derived-mode-p 'message-mode)
  21024. (save-excursion
  21025. (beginning-of-line)
  21026. (cond ((or (not (message-in-body-p))
  21027. (looking-at orgtbl-line-start-regexp))
  21028. (throw 'exit nil))
  21029. ((looking-at message-cite-prefix-regexp)
  21030. (throw 'exit (match-string-no-properties 0)))
  21031. ((looking-at org-outline-regexp)
  21032. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21033. (org-with-wide-buffer
  21034. (unless (org-at-heading-p)
  21035. (let* ((p (line-beginning-position))
  21036. (element (save-excursion
  21037. (beginning-of-line)
  21038. (org-element-at-point)))
  21039. (type (org-element-type element))
  21040. (post-affiliated (org-element-property :post-affiliated element)))
  21041. (unless (< p post-affiliated)
  21042. (case type
  21043. (comment
  21044. (save-excursion
  21045. (beginning-of-line)
  21046. (looking-at "[ \t]*")
  21047. (concat (match-string 0) "# ")))
  21048. (footnote-definition "")
  21049. ((item plain-list)
  21050. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21051. (paragraph
  21052. ;; Fill prefix is usually the same as the current line,
  21053. ;; unless the paragraph is at the beginning of an item.
  21054. (let ((parent (org-element-property :parent element)))
  21055. (save-excursion
  21056. (beginning-of-line)
  21057. (cond ((eq (org-element-type parent) 'item)
  21058. (make-string (org-list-item-body-column
  21059. (org-element-property :begin parent))
  21060. ?\s))
  21061. ((and adaptive-fill-regexp
  21062. ;; Locally disable
  21063. ;; `adaptive-fill-function' to let
  21064. ;; `fill-context-prefix' handle
  21065. ;; `adaptive-fill-regexp' variable.
  21066. (let (adaptive-fill-function)
  21067. (fill-context-prefix
  21068. post-affiliated
  21069. (org-element-property :end element)))))
  21070. ((looking-at "[ \t]+") (match-string 0))
  21071. (t "")))))
  21072. (comment-block
  21073. ;; Only fill contents if P is within block boundaries.
  21074. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21075. (forward-line)
  21076. (point)))
  21077. (cend (save-excursion
  21078. (goto-char (org-element-property :end element))
  21079. (skip-chars-backward " \r\t\n")
  21080. (line-beginning-position))))
  21081. (when (and (>= p cbeg) (< p cend))
  21082. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21083. (match-string 0)
  21084. "")))))))))))
  21085. (declare-function message-goto-body "message" ())
  21086. (defvar message-cite-prefix-regexp) ; From message.el
  21087. (defun org-fill-paragraph (&optional justify)
  21088. "Fill element at point, when applicable.
  21089. This function only applies to comment blocks, comments, example
  21090. blocks and paragraphs. Also, as a special case, re-align table
  21091. when point is at one.
  21092. If JUSTIFY is non-nil (interactively, with prefix argument),
  21093. justify as well. If `sentence-end-double-space' is non-nil, then
  21094. period followed by one space does not end a sentence, so don't
  21095. break a line there. The variable `fill-column' controls the
  21096. width for filling.
  21097. For convenience, when point is at a plain list, an item or
  21098. a footnote definition, try to fill the first paragraph within."
  21099. (interactive)
  21100. (if (and (derived-mode-p 'message-mode)
  21101. (or (not (message-in-body-p))
  21102. (save-excursion (move-beginning-of-line 1)
  21103. (looking-at message-cite-prefix-regexp))))
  21104. ;; First ensure filling is correct in message-mode.
  21105. (let ((fill-paragraph-function
  21106. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21107. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21108. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21109. (paragraph-separate
  21110. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21111. (fill-paragraph nil))
  21112. (with-syntax-table org-mode-transpose-word-syntax-table
  21113. ;; Move to end of line in order to get the first paragraph
  21114. ;; within a plain list or a footnote definition.
  21115. (let ((element (save-excursion
  21116. (end-of-line)
  21117. (or (ignore-errors (org-element-at-point))
  21118. (user-error "An element cannot be parsed line %d"
  21119. (line-number-at-pos (point)))))))
  21120. ;; First check if point is in a blank line at the beginning of
  21121. ;; the buffer. In that case, ignore filling.
  21122. (case (org-element-type element)
  21123. ;; Use major mode filling function is src blocks.
  21124. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21125. ;; Align Org tables, leave table.el tables as-is.
  21126. (table-row (org-table-align) t)
  21127. (table
  21128. (when (eq (org-element-property :type element) 'org)
  21129. (save-excursion
  21130. (goto-char (org-element-property :post-affiliated element))
  21131. (org-table-align)))
  21132. t)
  21133. (paragraph
  21134. ;; Paragraphs may contain `line-break' type objects.
  21135. (let ((beg (max (point-min)
  21136. (org-element-property :contents-begin element)))
  21137. (end (min (point-max)
  21138. (org-element-property :contents-end element))))
  21139. ;; Do nothing if point is at an affiliated keyword.
  21140. (if (< (line-end-position) beg) t
  21141. (when (derived-mode-p 'message-mode)
  21142. ;; In `message-mode', do not fill following citation
  21143. ;; in current paragraph nor text before message body.
  21144. (let ((body-start (save-excursion (message-goto-body))))
  21145. (when body-start (setq beg (max body-start beg))))
  21146. (when (save-excursion
  21147. (re-search-forward
  21148. (concat "^" message-cite-prefix-regexp) end t))
  21149. (setq end (match-beginning 0))))
  21150. ;; Fill paragraph, taking line breaks into account.
  21151. (save-excursion
  21152. (goto-char beg)
  21153. (let ((cuts (list beg)))
  21154. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21155. (when (eq 'line-break
  21156. (org-element-type
  21157. (save-excursion (backward-char)
  21158. (org-element-context))))
  21159. (push (point) cuts)))
  21160. (dolist (c (delq end cuts))
  21161. (fill-region-as-paragraph c end justify)
  21162. (setq end c))))
  21163. t)))
  21164. ;; Contents of `comment-block' type elements should be
  21165. ;; filled as plain text, but only if point is within block
  21166. ;; markers.
  21167. (comment-block
  21168. (let* ((case-fold-search t)
  21169. (beg (save-excursion
  21170. (goto-char (org-element-property :begin element))
  21171. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21172. (forward-line)
  21173. (point)))
  21174. (end (save-excursion
  21175. (goto-char (org-element-property :end element))
  21176. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21177. (line-beginning-position))))
  21178. (if (or (< (point) beg) (> (point) end)) t
  21179. (fill-region-as-paragraph
  21180. (save-excursion (end-of-line)
  21181. (re-search-backward "^[ \t]*$" beg 'move)
  21182. (line-beginning-position))
  21183. (save-excursion (beginning-of-line)
  21184. (re-search-forward "^[ \t]*$" end 'move)
  21185. (line-beginning-position))
  21186. justify))))
  21187. ;; Fill comments.
  21188. (comment
  21189. (let ((begin (org-element-property :post-affiliated element))
  21190. (end (org-element-property :end element)))
  21191. (when (and (>= (point) begin) (<= (point) end))
  21192. (let ((begin (save-excursion
  21193. (end-of-line)
  21194. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21195. (progn (forward-line) (point))
  21196. begin)))
  21197. (end (save-excursion
  21198. (end-of-line)
  21199. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21200. (1- (line-beginning-position))
  21201. (skip-chars-backward " \r\t\n")
  21202. (line-end-position)))))
  21203. ;; Do not fill comments when at a blank line.
  21204. (when (> end begin)
  21205. (let ((fill-prefix
  21206. (save-excursion
  21207. (beginning-of-line)
  21208. (looking-at "[ \t]*#")
  21209. (let ((comment-prefix (match-string 0)))
  21210. (goto-char (match-end 0))
  21211. (if (looking-at adaptive-fill-regexp)
  21212. (concat comment-prefix (match-string 0))
  21213. (concat comment-prefix " "))))))
  21214. (save-excursion
  21215. (fill-region-as-paragraph begin end justify))))))
  21216. t))
  21217. ;; Ignore every other element.
  21218. (otherwise t))))))
  21219. (defun org-auto-fill-function ()
  21220. "Auto-fill function."
  21221. ;; Check if auto-filling is meaningful.
  21222. (let ((fc (current-fill-column)))
  21223. (when (and fc (> (current-column) fc))
  21224. (let* ((fill-prefix (org-adaptive-fill-function))
  21225. ;; Enforce empty fill prefix, if required. Otherwise, it
  21226. ;; will be computed again.
  21227. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21228. (when fill-prefix (do-auto-fill))))))
  21229. (defun org-comment-line-break-function (&optional soft)
  21230. "Break line at point and indent, continuing comment if within one.
  21231. The inserted newline is marked hard if variable
  21232. `use-hard-newlines' is true, unless optional argument SOFT is
  21233. non-nil."
  21234. (if soft (insert-and-inherit ?\n) (newline 1))
  21235. (save-excursion (forward-char -1) (delete-horizontal-space))
  21236. (delete-horizontal-space)
  21237. (indent-to-left-margin)
  21238. (insert-before-markers-and-inherit fill-prefix))
  21239. ;;; Fixed Width Areas
  21240. (defun org-toggle-fixed-width ()
  21241. "Toggle fixed-width markup.
  21242. Add or remove fixed-width markup on current line, whenever it
  21243. makes sense. Return an error otherwise.
  21244. If a region is active and if it contains only fixed-width areas
  21245. or blank lines, remove all fixed-width markup in it. If the
  21246. region contains anything else, convert all non-fixed-width lines
  21247. to fixed-width ones.
  21248. Blank lines at the end of the region are ignored unless the
  21249. region only contains such lines."
  21250. (interactive)
  21251. (if (not (org-region-active-p))
  21252. ;; No region:
  21253. ;;
  21254. ;; Remove fixed width marker only in a fixed-with element.
  21255. ;;
  21256. ;; Add fixed width maker in paragraphs, in blank lines after
  21257. ;; elements or at the beginning of a headline or an inlinetask,
  21258. ;; and before any one-line elements (e.g., a clock).
  21259. (progn
  21260. (beginning-of-line)
  21261. (let* ((element (org-element-at-point))
  21262. (type (org-element-type element)))
  21263. (cond
  21264. ((and (eq type 'fixed-width)
  21265. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21266. (replace-match
  21267. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21268. ((and (memq type '(babel-call clock comment diary-sexp headline
  21269. horizontal-rule keyword paragraph
  21270. planning))
  21271. (<= (org-element-property :post-affiliated element) (point)))
  21272. (skip-chars-forward " \t")
  21273. (insert ": "))
  21274. ((and (org-looking-at-p "[ \t]*$")
  21275. (or (eq type 'inlinetask)
  21276. (save-excursion
  21277. (skip-chars-forward " \r\t\n")
  21278. (<= (org-element-property :end element) (point)))))
  21279. (delete-region (point) (line-end-position))
  21280. (org-indent-line)
  21281. (insert ": "))
  21282. (t (user-error "Cannot insert a fixed-width line here")))))
  21283. ;; Region active.
  21284. (let* ((begin (save-excursion
  21285. (goto-char (region-beginning))
  21286. (line-beginning-position)))
  21287. (end (copy-marker
  21288. (save-excursion
  21289. (goto-char (region-end))
  21290. (unless (eolp) (beginning-of-line))
  21291. (if (save-excursion (re-search-backward "\\S-" begin t))
  21292. (progn (skip-chars-backward " \r\t\n") (point))
  21293. (point)))))
  21294. (all-fixed-width-p
  21295. (catch 'not-all-p
  21296. (save-excursion
  21297. (goto-char begin)
  21298. (skip-chars-forward " \r\t\n")
  21299. (when (eobp) (throw 'not-all-p nil))
  21300. (while (< (point) end)
  21301. (let ((element (org-element-at-point)))
  21302. (if (eq (org-element-type element) 'fixed-width)
  21303. (goto-char (org-element-property :end element))
  21304. (throw 'not-all-p nil))))
  21305. t))))
  21306. (if all-fixed-width-p
  21307. (save-excursion
  21308. (goto-char begin)
  21309. (while (< (point) end)
  21310. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21311. (replace-match
  21312. "" nil nil nil
  21313. (if (= (line-end-position) (match-end 0)) 0 1)))
  21314. (forward-line)))
  21315. (let ((min-ind (point-max)))
  21316. ;; Find minimum indentation across all lines.
  21317. (save-excursion
  21318. (goto-char begin)
  21319. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21320. (setq min-ind 0)
  21321. (catch 'zerop
  21322. (while (< (point) end)
  21323. (unless (org-looking-at-p "[ \t]*$")
  21324. (let ((ind (org-get-indentation)))
  21325. (setq min-ind (min min-ind ind))
  21326. (when (zerop ind) (throw 'zerop t))))
  21327. (forward-line)))))
  21328. ;; Loop over all lines and add fixed-width markup everywhere
  21329. ;; but in fixed-width lines.
  21330. (save-excursion
  21331. (goto-char begin)
  21332. (while (< (point) end)
  21333. (cond
  21334. ((org-at-heading-p)
  21335. (insert ": ")
  21336. (forward-line)
  21337. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21338. (insert ":")
  21339. (forward-line)))
  21340. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21341. (let* ((element (org-element-at-point))
  21342. (element-end (org-element-property :end element)))
  21343. (if (eq (org-element-type element) 'fixed-width)
  21344. (progn (goto-char element-end)
  21345. (skip-chars-backward " \r\t\n")
  21346. (forward-line))
  21347. (let ((limit (min end element-end)))
  21348. (while (< (point) limit)
  21349. (org-move-to-column min-ind t)
  21350. (insert ": ")
  21351. (forward-line))))))
  21352. (t
  21353. (org-move-to-column min-ind t)
  21354. (insert ": ")
  21355. (forward-line)))))))
  21356. (set-marker end nil))))
  21357. ;;; Comments
  21358. ;; Org comments syntax is quite complex. It requires the entire line
  21359. ;; to be just a comment. Also, even with the right syntax at the
  21360. ;; beginning of line, some some elements (i.e. verse-block or
  21361. ;; example-block) don't accept comments. Usual Emacs comment commands
  21362. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21363. ;; Org still relies on `comment-dwim', but cannot trust
  21364. ;; `comment-only-p'. So, `comment-region-function' and
  21365. ;; `uncomment-region-function' both point
  21366. ;; to`org-comment-or-uncomment-region'. Eventually,
  21367. ;; `org-insert-comment' takes care of insertion of comments at the
  21368. ;; beginning of line.
  21369. ;; `org-setup-comments-handling' install comments related variables
  21370. ;; during `org-mode' initialization.
  21371. (defun org-setup-comments-handling ()
  21372. (interactive)
  21373. (org-set-local 'comment-use-syntax nil)
  21374. (org-set-local 'comment-start "# ")
  21375. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21376. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21377. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21378. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21379. (defun org-insert-comment ()
  21380. "Insert an empty comment above current line.
  21381. If the line is empty, insert comment at its beginning. When
  21382. point is within a source block, comment according to the related
  21383. major mode."
  21384. (if (let ((element (org-element-at-point)))
  21385. (and (eq (org-element-type element) 'src-block)
  21386. (< (save-excursion
  21387. (goto-char (org-element-property :post-affiliated element))
  21388. (line-end-position))
  21389. (point))
  21390. (> (save-excursion
  21391. (goto-char (org-element-property :end element))
  21392. (skip-chars-backward " \r\t\n")
  21393. (line-beginning-position))
  21394. (point))))
  21395. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21396. (beginning-of-line)
  21397. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21398. (open-line 1))
  21399. (org-indent-line)
  21400. (insert "# ")))
  21401. (defvar comment-empty-lines) ; From newcomment.el.
  21402. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  21403. "Comment or uncomment each non-blank line in the region.
  21404. Uncomment each non-blank line between BEG and END if it only
  21405. contains commented lines. Otherwise, comment them. If region is
  21406. strictly within a source block, use appropriate comment syntax."
  21407. (if (let ((element (org-element-at-point)))
  21408. (and (eq (org-element-type element) 'src-block)
  21409. (< (save-excursion
  21410. (goto-char (org-element-property :post-affiliated element))
  21411. (line-end-position))
  21412. beg)
  21413. (>= (save-excursion
  21414. (goto-char (org-element-property :end element))
  21415. (skip-chars-backward " \r\t\n")
  21416. (line-beginning-position))
  21417. end)))
  21418. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21419. (save-restriction
  21420. ;; Restrict region
  21421. (narrow-to-region (save-excursion (goto-char beg)
  21422. (skip-chars-forward " \r\t\n" end)
  21423. (line-beginning-position))
  21424. (save-excursion (goto-char end)
  21425. (skip-chars-backward " \r\t\n" beg)
  21426. (line-end-position)))
  21427. (let ((uncommentp
  21428. ;; UNCOMMENTP is non-nil when every non blank line between
  21429. ;; BEG and END is a comment.
  21430. (save-excursion
  21431. (goto-char (point-min))
  21432. (while (and (not (eobp))
  21433. (let ((element (org-element-at-point)))
  21434. (and (eq (org-element-type element) 'comment)
  21435. (goto-char (min (point-max)
  21436. (org-element-property
  21437. :end element)))))))
  21438. (eobp))))
  21439. (if uncommentp
  21440. ;; Only blank lines and comments in region: uncomment it.
  21441. (save-excursion
  21442. (goto-char (point-min))
  21443. (while (not (eobp))
  21444. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21445. (replace-match "" nil nil nil 1))
  21446. (forward-line)))
  21447. ;; Comment each line in region.
  21448. (let ((min-indent (point-max)))
  21449. ;; First find the minimum indentation across all lines.
  21450. (save-excursion
  21451. (goto-char (point-min))
  21452. (while (and (not (eobp)) (not (zerop min-indent)))
  21453. (unless (looking-at "[ \t]*$")
  21454. (setq min-indent (min min-indent (current-indentation))))
  21455. (forward-line)))
  21456. ;; Then loop over all lines.
  21457. (save-excursion
  21458. (goto-char (point-min))
  21459. (while (not (eobp))
  21460. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21461. ;; Don't get fooled by invisible text (e.g. link path)
  21462. ;; when moving to column MIN-INDENT.
  21463. (let ((buffer-invisibility-spec nil))
  21464. (org-move-to-column min-indent t))
  21465. (insert comment-start))
  21466. (forward-line)))))))))
  21467. (defun org-comment-dwim (arg)
  21468. "Call `comment-dwim' within a source edit buffer if needed."
  21469. (interactive "*P")
  21470. (if (org-in-src-block-p)
  21471. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21472. (call-interactively 'comment-dwim)))
  21473. ;;; Planning
  21474. ;; This section contains tools to operate on timestamp objects, as
  21475. ;; returned by, e.g. `org-element-context'.
  21476. (defun org-timestamp-has-time-p (timestamp)
  21477. "Non-nil when TIMESTAMP has a time specified."
  21478. (org-element-property :hour-start timestamp))
  21479. (defun org-timestamp-format (timestamp format &optional end utc)
  21480. "Format a TIMESTAMP element into a string.
  21481. FORMAT is a format specifier to be passed to
  21482. `format-time-string'.
  21483. When optional argument END is non-nil, use end of date-range or
  21484. time-range, if possible.
  21485. When optional argument UTC is non-nil, time will be expressed as
  21486. Universal Time."
  21487. (format-time-string
  21488. format
  21489. (apply 'encode-time
  21490. (cons 0
  21491. (mapcar
  21492. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21493. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21494. '(:minute-start :hour-start :day-start :month-start
  21495. :year-start)))))
  21496. utc))
  21497. (defun org-timestamp-split-range (timestamp &optional end)
  21498. "Extract a timestamp object from a date or time range.
  21499. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  21500. the end of the range. Otherwise, extract its start.
  21501. Return a new timestamp object sharing the same parent as
  21502. TIMESTAMP."
  21503. (let ((type (org-element-property :type timestamp)))
  21504. (if (memq type '(active inactive diary)) timestamp
  21505. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  21506. ;; Set new type.
  21507. (org-element-put-property
  21508. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21509. ;; Copy start properties over end properties if END is
  21510. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21511. (let ((p-alist '((:minute-start . :minute-end)
  21512. (:hour-start . :hour-end)
  21513. (:day-start . :day-end)
  21514. (:month-start . :month-end)
  21515. (:year-start . :year-end))))
  21516. (dolist (p-cell p-alist)
  21517. (org-element-put-property
  21518. split-ts
  21519. (funcall (if end 'car 'cdr) p-cell)
  21520. (org-element-property
  21521. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  21522. ;; Eventually refresh `:raw-value'.
  21523. (org-element-put-property split-ts :raw-value nil)
  21524. (org-element-put-property
  21525. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21526. (defun org-timestamp-translate (timestamp &optional boundary)
  21527. "Translate TIMESTAMP object to custom format.
  21528. Format string is defined in `org-time-stamp-custom-formats',
  21529. which see.
  21530. When optional argument BOUNDARY is non-nil, it is either the
  21531. symbol `start' or `end'. In this case, only translate the
  21532. starting or ending part of TIMESTAMP if it is a date or time
  21533. range. Otherwise, translate both parts.
  21534. Return timestamp as-is if `org-display-custom-times' is nil or if
  21535. it has a `diary' type."
  21536. (let ((type (org-element-property :type timestamp)))
  21537. (if (or (not org-display-custom-times) (eq type 'diary))
  21538. (org-element-interpret-data timestamp)
  21539. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21540. org-time-stamp-custom-formats)))
  21541. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21542. (concat (org-timestamp-format timestamp fmt)
  21543. "--"
  21544. (org-timestamp-format timestamp fmt t))
  21545. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21546. ;;; Other stuff.
  21547. (defun org-reftex-citation ()
  21548. "Use reftex-citation to insert a citation into the buffer.
  21549. This looks for a line like
  21550. #+BIBLIOGRAPHY: foo plain option:-d
  21551. and derives from it that foo.bib is the bibliography file relevant
  21552. for this document. It then installs the necessary environment for RefTeX
  21553. to work in this buffer and calls `reftex-citation' to insert a citation
  21554. into the buffer.
  21555. Export of such citations to both LaTeX and HTML is handled by the contributed
  21556. package ox-bibtex by Taru Karttunen."
  21557. (interactive)
  21558. (let ((reftex-docstruct-symbol 'rds)
  21559. (reftex-cite-format "\\cite{%l}")
  21560. rds bib)
  21561. (save-excursion
  21562. (save-restriction
  21563. (widen)
  21564. (let ((case-fold-search t)
  21565. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21566. (if (not (save-excursion
  21567. (or (re-search-forward re nil t)
  21568. (re-search-backward re nil t))))
  21569. (user-error "No bibliography defined in file")
  21570. (setq bib (concat (match-string 1) ".bib")
  21571. rds (list (list 'bib bib)))))))
  21572. (call-interactively 'reftex-citation)))
  21573. ;;;; Functions extending outline functionality
  21574. (defun org-beginning-of-line (&optional arg)
  21575. "Go to the beginning of the current line. If that is invisible, continue
  21576. to a visible line beginning. This makes the function of C-a more intuitive.
  21577. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21578. first attempt, and only move to after the tags when the cursor is already
  21579. beyond the end of the headline."
  21580. (interactive "P")
  21581. (let ((pos (point))
  21582. (special (if (consp org-special-ctrl-a/e)
  21583. (car org-special-ctrl-a/e)
  21584. org-special-ctrl-a/e))
  21585. deactivate-mark refpos)
  21586. (if (org-bound-and-true-p visual-line-mode)
  21587. (beginning-of-visual-line 1)
  21588. (beginning-of-line 1))
  21589. (if (and arg (fboundp 'move-beginning-of-line))
  21590. (call-interactively 'move-beginning-of-line)
  21591. (if (bobp)
  21592. nil
  21593. (backward-char 1)
  21594. (if (org-truely-invisible-p)
  21595. (while (and (not (bobp)) (org-truely-invisible-p))
  21596. (backward-char 1)
  21597. (beginning-of-line 1))
  21598. (forward-char 1))))
  21599. (when special
  21600. (cond
  21601. ((and (looking-at org-complex-heading-regexp)
  21602. (= (char-after (match-end 1)) ?\ ))
  21603. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21604. (point-at-eol)))
  21605. (goto-char
  21606. (if (eq special t)
  21607. (cond ((> pos refpos) refpos)
  21608. ((= pos (point)) refpos)
  21609. (t (point)))
  21610. (cond ((> pos (point)) (point))
  21611. ((not (eq last-command this-command)) (point))
  21612. (t refpos)))))
  21613. ((org-at-item-p)
  21614. ;; Being at an item and not looking at an the item means point
  21615. ;; was previously moved to beginning of a visual line, which
  21616. ;; doesn't contain the item. Therefore, do nothing special,
  21617. ;; just stay here.
  21618. (when (looking-at org-list-full-item-re)
  21619. ;; Set special position at first white space character after
  21620. ;; bullet, and check-box, if any.
  21621. (let ((after-bullet
  21622. (let ((box (match-end 3)))
  21623. (if (not box) (match-end 1)
  21624. (let ((after (char-after box)))
  21625. (if (and after (= after ? )) (1+ box) box))))))
  21626. ;; Special case: Move point to special position when
  21627. ;; currently after it or at beginning of line.
  21628. (if (eq special t)
  21629. (when (or (> pos after-bullet) (= (point) pos))
  21630. (goto-char after-bullet))
  21631. ;; Reversed case: Move point to special position when
  21632. ;; point was already at beginning of line and command is
  21633. ;; repeated.
  21634. (when (and (= (point) pos) (eq last-command this-command))
  21635. (goto-char after-bullet))))))))
  21636. (org-no-warnings
  21637. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21638. (setq disable-point-adjustment
  21639. (or (not (invisible-p (point)))
  21640. (not (invisible-p (max (point-min) (1- (point))))))))
  21641. (defun org-end-of-line (&optional arg)
  21642. "Go to the end of the line.
  21643. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21644. tags on the first attempt, and only move to after the tags when
  21645. the cursor is already beyond the end of the headline."
  21646. (interactive "P")
  21647. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21648. org-special-ctrl-a/e))
  21649. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21650. 'end-of-visual-line)
  21651. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21652. (t 'end-of-line)))
  21653. deactivate-mark)
  21654. (if (or (not special) arg) (call-interactively move-fun)
  21655. (let* ((element (save-excursion (beginning-of-line)
  21656. (org-element-at-point)))
  21657. (type (org-element-type element)))
  21658. (cond
  21659. ((memq type '(headline inlinetask))
  21660. (let ((pos (point)))
  21661. (beginning-of-line 1)
  21662. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21663. (if (eq special t)
  21664. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21665. (goto-char (match-beginning 1))
  21666. (goto-char (match-end 0)))
  21667. (if (or (< pos (match-end 0))
  21668. (not (eq this-command last-command)))
  21669. (goto-char (match-end 0))
  21670. (goto-char (match-beginning 1))))
  21671. (call-interactively move-fun))))
  21672. ((outline-invisible-p (line-end-position))
  21673. ;; If element is hidden, `move-end-of-line' would put point
  21674. ;; after it. Use `end-of-line' to stay on current line.
  21675. (call-interactively 'end-of-line))
  21676. (t (call-interactively move-fun)))))
  21677. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21678. (setq disable-point-adjustment
  21679. (or (not (invisible-p (point)))
  21680. (not (invisible-p (max (point-min) (1- (point))))))))
  21681. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21682. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21683. (defun org-backward-sentence (&optional arg)
  21684. "Go to beginning of sentence, or beginning of table field.
  21685. This will call `backward-sentence' or `org-table-beginning-of-field',
  21686. depending on context."
  21687. (interactive "P")
  21688. (cond
  21689. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21690. (t (call-interactively 'backward-sentence))))
  21691. (defun org-forward-sentence (&optional arg)
  21692. "Go to end of sentence, or end of table field.
  21693. This will call `forward-sentence' or `org-table-end-of-field',
  21694. depending on context."
  21695. (interactive "P")
  21696. (cond
  21697. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21698. (t (call-interactively 'forward-sentence))))
  21699. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21700. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21701. (defun org-kill-line (&optional arg)
  21702. "Kill line, to tags or end of line."
  21703. (interactive "P")
  21704. (cond
  21705. ((or (not org-special-ctrl-k)
  21706. (bolp)
  21707. (not (org-at-heading-p)))
  21708. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21709. org-ctrl-k-protect-subtree)
  21710. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21711. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21712. (user-error "C-k aborted as it would kill a hidden subtree")))
  21713. (call-interactively
  21714. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21715. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21716. (kill-region (point) (match-beginning 1))
  21717. (org-set-tags nil t))
  21718. (t (kill-region (point) (point-at-eol)))))
  21719. (define-key org-mode-map "\C-k" 'org-kill-line)
  21720. (defun org-yank (&optional arg)
  21721. "Yank. If the kill is a subtree, treat it specially.
  21722. This command will look at the current kill and check if is a single
  21723. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21724. cursor is at the beginning of a line or after the stars of a currently
  21725. empty headline, then the yank is handled specially. How exactly depends
  21726. on the value of the following variables, both set by default.
  21727. `org-yank-folded-subtrees'
  21728. When set, the subtree(s) will be folded after insertion, but only
  21729. if doing so would now swallow text after the yanked text.
  21730. `org-yank-adjusted-subtrees'
  21731. When set, the subtree will be promoted or demoted in order to
  21732. fit into the local outline tree structure, which means that the
  21733. level will be adjusted so that it becomes the smaller one of the
  21734. two *visible* surrounding headings.
  21735. Any prefix to this command will cause `yank' to be called directly with
  21736. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21737. will just
  21738. plainly yank the text as it is.
  21739. \[1] The test checks if the first non-white line is a heading
  21740. and if there are no other headings with fewer stars."
  21741. (interactive "P")
  21742. (org-yank-generic 'yank arg))
  21743. (defun org-yank-generic (command arg)
  21744. "Perform some yank-like command.
  21745. This function implements the behavior described in the `org-yank'
  21746. documentation. However, it has been generalized to work for any
  21747. interactive command with similar behavior."
  21748. ;; pretend to be command COMMAND
  21749. (setq this-command command)
  21750. (if arg
  21751. (call-interactively command)
  21752. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21753. (and (org-kill-is-subtree-p)
  21754. (or (bolp)
  21755. (and (looking-at "[ \t]*$")
  21756. (string-match
  21757. "\\`\\*+\\'"
  21758. (buffer-substring (point-at-bol) (point)))))))
  21759. swallowp)
  21760. (cond
  21761. ((and subtreep org-yank-folded-subtrees)
  21762. (let ((beg (point))
  21763. end)
  21764. (if (and subtreep org-yank-adjusted-subtrees)
  21765. (org-paste-subtree nil nil 'for-yank)
  21766. (call-interactively command))
  21767. (setq end (point))
  21768. (goto-char beg)
  21769. (when (and (bolp) subtreep
  21770. (not (setq swallowp
  21771. (org-yank-folding-would-swallow-text beg end))))
  21772. (org-with-limited-levels
  21773. (or (looking-at org-outline-regexp)
  21774. (re-search-forward org-outline-regexp-bol end t))
  21775. (while (and (< (point) end) (looking-at org-outline-regexp))
  21776. (hide-subtree)
  21777. (org-cycle-show-empty-lines 'folded)
  21778. (condition-case nil
  21779. (outline-forward-same-level 1)
  21780. (error (goto-char end))))))
  21781. (when swallowp
  21782. (message
  21783. "Inserted text not folded because that would swallow text"))
  21784. (goto-char end)
  21785. (skip-chars-forward " \t\n\r")
  21786. (beginning-of-line 1)
  21787. (push-mark beg 'nomsg)))
  21788. ((and subtreep org-yank-adjusted-subtrees)
  21789. (let ((beg (point-at-bol)))
  21790. (org-paste-subtree nil nil 'for-yank)
  21791. (push-mark beg 'nomsg)))
  21792. (t
  21793. (call-interactively command))))))
  21794. (defun org-yank-folding-would-swallow-text (beg end)
  21795. "Would hide-subtree at BEG swallow any text after END?"
  21796. (let (level)
  21797. (org-with-limited-levels
  21798. (save-excursion
  21799. (goto-char beg)
  21800. (when (or (looking-at org-outline-regexp)
  21801. (re-search-forward org-outline-regexp-bol end t))
  21802. (setq level (org-outline-level)))
  21803. (goto-char end)
  21804. (skip-chars-forward " \t\r\n\v\f")
  21805. (if (or (eobp)
  21806. (and (bolp) (looking-at org-outline-regexp)
  21807. (<= (org-outline-level) level)))
  21808. nil ; Nothing would be swallowed
  21809. t))))) ; something would swallow
  21810. (define-key org-mode-map "\C-y" 'org-yank)
  21811. (defun org-truely-invisible-p ()
  21812. "Check if point is at a character currently not visible.
  21813. This version does not only check the character property, but also
  21814. `visible-mode'."
  21815. ;; Early versions of noutline don't have `outline-invisible-p'.
  21816. (if (org-bound-and-true-p visible-mode)
  21817. nil
  21818. (outline-invisible-p)))
  21819. (defun org-invisible-p2 ()
  21820. "Check if point is at a character currently not visible."
  21821. (save-excursion
  21822. (if (and (eolp) (not (bobp))) (backward-char 1))
  21823. ;; Early versions of noutline don't have `outline-invisible-p'.
  21824. (outline-invisible-p)))
  21825. (defun org-back-to-heading (&optional invisible-ok)
  21826. "Call `outline-back-to-heading', but provide a better error message."
  21827. (condition-case nil
  21828. (outline-back-to-heading invisible-ok)
  21829. (error (error "Before first headline at position %d in buffer %s"
  21830. (point) (current-buffer)))))
  21831. (defun org-before-first-heading-p ()
  21832. "Before first heading?"
  21833. (save-excursion
  21834. (end-of-line)
  21835. (null (re-search-backward org-outline-regexp-bol nil t))))
  21836. (defun org-at-heading-p (&optional ignored)
  21837. (outline-on-heading-p t))
  21838. ;; Compatibility alias with Org versions < 7.8.03
  21839. (defalias 'org-on-heading-p 'org-at-heading-p)
  21840. (defun org-in-commented-heading-p (&optional no-inheritance)
  21841. "Non-nil if point is under a commented heading.
  21842. This function also checks ancestors of the current headline,
  21843. unless optional argument NO-INHERITANCE is non-nil."
  21844. (cond
  21845. ((org-before-first-heading-p) nil)
  21846. ((let ((headline (nth 4 (org-heading-components))))
  21847. (and headline
  21848. (let ((case-fold-search nil))
  21849. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  21850. headline)))))
  21851. (no-inheritance nil)
  21852. (t
  21853. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  21854. (defun org-at-comment-p nil
  21855. "Is cursor in a commented line?"
  21856. (save-excursion
  21857. (save-match-data
  21858. (beginning-of-line)
  21859. (looking-at "^[ \t]*# "))))
  21860. (defun org-at-drawer-p nil
  21861. "Is cursor at a drawer keyword?"
  21862. (save-excursion
  21863. (move-beginning-of-line 1)
  21864. (looking-at org-drawer-regexp)))
  21865. (defun org-at-block-p nil
  21866. "Is cursor at a block keyword?"
  21867. (save-excursion
  21868. (move-beginning-of-line 1)
  21869. (looking-at org-block-regexp)))
  21870. (defun org-point-at-end-of-empty-headline ()
  21871. "If point is at the end of an empty headline, return t, else nil.
  21872. If the heading only contains a TODO keyword, it is still still considered
  21873. empty."
  21874. (and (looking-at "[ \t]*$")
  21875. (when org-todo-line-regexp
  21876. (save-excursion
  21877. (beginning-of-line 1)
  21878. (let ((case-fold-search nil))
  21879. (looking-at org-todo-line-regexp)
  21880. (string= (match-string 3) ""))))))
  21881. (defun org-at-heading-or-item-p ()
  21882. (or (org-at-heading-p) (org-at-item-p)))
  21883. (defun org-at-target-p ()
  21884. (or (org-in-regexp org-radio-target-regexp)
  21885. (org-in-regexp org-target-regexp)))
  21886. ;; Compatibility alias with Org versions < 7.8.03
  21887. (defalias 'org-on-target-p 'org-at-target-p)
  21888. (defun org-up-heading-all (arg)
  21889. "Move to the heading line of which the present line is a subheading.
  21890. This function considers both visible and invisible heading lines.
  21891. With argument, move up ARG levels."
  21892. (if (fboundp 'outline-up-heading-all)
  21893. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21894. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21895. (defun org-up-heading-safe ()
  21896. "Move to the heading line of which the present line is a subheading.
  21897. This version will not throw an error. It will return the level of the
  21898. headline found, or nil if no higher level is found.
  21899. Also, this function will be a lot faster than `outline-up-heading',
  21900. because it relies on stars being the outline starters. This can really
  21901. make a significant difference in outlines with very many siblings."
  21902. (when (ignore-errors (org-back-to-heading t))
  21903. (let ((level-up (1- (funcall outline-level))))
  21904. (and (> level-up 0)
  21905. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  21906. (funcall outline-level)))))
  21907. (defun org-first-sibling-p ()
  21908. "Is this heading the first child of its parents?"
  21909. (interactive)
  21910. (let ((re org-outline-regexp-bol)
  21911. level l)
  21912. (unless (org-at-heading-p t)
  21913. (user-error "Not at a heading"))
  21914. (setq level (funcall outline-level))
  21915. (save-excursion
  21916. (if (not (re-search-backward re nil t))
  21917. t
  21918. (setq l (funcall outline-level))
  21919. (< l level)))))
  21920. (defun org-goto-sibling (&optional previous)
  21921. "Goto the next sibling, even if it is invisible.
  21922. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21923. when a sibling was found. When none is found, return nil and don't
  21924. move point."
  21925. (let ((fun (if previous 're-search-backward 're-search-forward))
  21926. (pos (point))
  21927. (re org-outline-regexp-bol)
  21928. level l)
  21929. (when (ignore-errors (org-back-to-heading t))
  21930. (setq level (funcall outline-level))
  21931. (catch 'exit
  21932. (or previous (forward-char 1))
  21933. (while (funcall fun re nil t)
  21934. (setq l (funcall outline-level))
  21935. (when (< l level) (goto-char pos) (throw 'exit nil))
  21936. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21937. (goto-char pos)
  21938. nil))))
  21939. (defun org-show-siblings ()
  21940. "Show all siblings of the current headline."
  21941. (save-excursion
  21942. (while (org-goto-sibling) (org-flag-heading nil)))
  21943. (save-excursion
  21944. (while (org-goto-sibling 'previous)
  21945. (org-flag-heading nil))))
  21946. (defun org-goto-first-child ()
  21947. "Goto the first child, even if it is invisible.
  21948. Return t when a child was found. Otherwise don't move point and
  21949. return nil."
  21950. (let (level (pos (point)) (re org-outline-regexp-bol))
  21951. (when (ignore-errors (org-back-to-heading t))
  21952. (setq level (outline-level))
  21953. (forward-char 1)
  21954. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21955. (progn (goto-char (match-beginning 0)) t)
  21956. (goto-char pos) nil))))
  21957. (defun org-show-hidden-entry ()
  21958. "Show an entry where even the heading is hidden."
  21959. (save-excursion
  21960. (org-show-entry)))
  21961. (defun org-flag-heading (flag &optional entry)
  21962. "Flag the current heading. FLAG non-nil means make invisible.
  21963. When ENTRY is non-nil, show the entire entry."
  21964. (save-excursion
  21965. (org-back-to-heading t)
  21966. ;; Check if we should show the entire entry
  21967. (if entry
  21968. (progn
  21969. (org-show-entry)
  21970. (save-excursion
  21971. (and (outline-next-heading)
  21972. (org-flag-heading nil))))
  21973. (outline-flag-region (max (point-min) (1- (point)))
  21974. (save-excursion (outline-end-of-heading) (point))
  21975. flag))))
  21976. (defun org-get-next-sibling ()
  21977. "Move to next heading of the same level, and return point.
  21978. If there is no such heading, return nil.
  21979. This is like outline-next-sibling, but invisible headings are ok."
  21980. (let ((level (funcall outline-level)))
  21981. (outline-next-heading)
  21982. (while (and (not (eobp)) (> (funcall outline-level) level))
  21983. (outline-next-heading))
  21984. (if (or (eobp) (< (funcall outline-level) level))
  21985. nil
  21986. (point))))
  21987. (defun org-get-last-sibling ()
  21988. "Move to previous heading of the same level, and return point.
  21989. If there is no such heading, return nil."
  21990. (let ((opoint (point))
  21991. (level (funcall outline-level)))
  21992. (outline-previous-heading)
  21993. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21994. (while (and (> (funcall outline-level) level)
  21995. (not (bobp)))
  21996. (outline-previous-heading))
  21997. (if (< (funcall outline-level) level)
  21998. nil
  21999. (point)))))
  22000. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22001. "Goto to the end of a subtree."
  22002. ;; This contains an exact copy of the original function, but it uses
  22003. ;; `org-back-to-heading', to make it work also in invisible
  22004. ;; trees. And is uses an invisible-ok argument.
  22005. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22006. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22007. ;; with many children and grandchildren etc, this can be much faster
  22008. ;; than the outline version.
  22009. (org-back-to-heading invisible-ok)
  22010. (let ((first t)
  22011. (level (funcall outline-level)))
  22012. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22013. ;; A true heading (not a plain list item), in Org-mode
  22014. ;; This means we can easily find the end by looking
  22015. ;; only for the right number of stars. Using a regexp to do
  22016. ;; this is so much faster than using a Lisp loop.
  22017. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22018. (forward-char 1)
  22019. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22020. ;; something else, do it the slow way
  22021. (while (and (not (eobp))
  22022. (or first (> (funcall outline-level) level)))
  22023. (setq first nil)
  22024. (outline-next-heading)))
  22025. (unless to-heading
  22026. (if (memq (preceding-char) '(?\n ?\^M))
  22027. (progn
  22028. ;; Go to end of line before heading
  22029. (forward-char -1)
  22030. (if (memq (preceding-char) '(?\n ?\^M))
  22031. ;; leave blank line before heading
  22032. (forward-char -1))))))
  22033. (point))
  22034. (defun org-end-of-meta-data-and-drawers ()
  22035. "Jump to the first text after meta data and drawers in the current entry.
  22036. This will move over empty lines, lines with planning time stamps,
  22037. clocking lines, and drawers."
  22038. (org-back-to-heading t)
  22039. (let ((end (save-excursion (outline-next-heading) (point)))
  22040. (re (concat "\\(" org-drawer-regexp "\\)"
  22041. "\\|" "[ \t]*" org-keyword-time-regexp)))
  22042. (forward-line 1)
  22043. (while (re-search-forward re end t)
  22044. (if (not (match-end 1))
  22045. ;; empty or planning line
  22046. (forward-line 1)
  22047. ;; a drawer, find the end
  22048. (re-search-forward "^[ \t]*:END:" end 'move)
  22049. (forward-line 1)))
  22050. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  22051. (point)))
  22052. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22053. "Move forward to the ARG'th subheading at same level as this one.
  22054. Stop at the first and last subheadings of a superior heading.
  22055. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22056. non-nil it will also look at invisible ones."
  22057. (interactive "p")
  22058. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22059. (if (and arg (< arg 0))
  22060. (goto-char (point-min))
  22061. (outline-next-heading))
  22062. (org-at-heading-p)
  22063. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22064. (f (if (and arg (< arg 0))
  22065. 're-search-backward
  22066. 're-search-forward))
  22067. (count (if arg (abs arg) 1))
  22068. (result (point)))
  22069. (while (and (prog1 (> count 0)
  22070. (forward-char (if (and arg (< arg 0)) -1 1)))
  22071. (funcall f org-outline-regexp-bol nil 'move))
  22072. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22073. (cond ((< l level) (setq count 0))
  22074. ((and (= l level)
  22075. (or invisible-ok
  22076. (progn
  22077. (goto-char (line-beginning-position))
  22078. (not (outline-invisible-p)))))
  22079. (setq count (1- count))
  22080. (when (eq l level)
  22081. (setq result (point)))))))
  22082. (goto-char result))
  22083. (beginning-of-line 1)))
  22084. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22085. "Move backward to the ARG'th subheading at same level as this one.
  22086. Stop at the first and last subheadings of a superior heading."
  22087. (interactive "p")
  22088. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22089. (defun org-next-block (arg &optional backward block-regexp)
  22090. "Jump to the next block.
  22091. With a prefix argument ARG, jump forward ARG many source blocks.
  22092. When BACKWARD is non-nil, jump to the previous block.
  22093. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22094. (interactive "p")
  22095. (let ((re (or block-regexp org-block-regexp))
  22096. (re-search-fn (or (and backward 're-search-backward)
  22097. 're-search-forward)))
  22098. (if (looking-at re) (forward-char 1))
  22099. (condition-case nil
  22100. (funcall re-search-fn re nil nil arg)
  22101. (error (user-error "No %s code blocks"
  22102. (if backward "previous" "further" ))))
  22103. (goto-char (match-beginning 0)) (org-show-context)))
  22104. (defun org-previous-block (arg &optional block-regexp)
  22105. "Jump to the previous block.
  22106. With a prefix argument ARG, jump backward ARG many source blocks.
  22107. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22108. (interactive "p")
  22109. (org-next-block arg t block-regexp))
  22110. (defun org-forward-paragraph ()
  22111. "Move forward to beginning of next paragraph or equivalent.
  22112. The function moves point to the beginning of the next visible
  22113. structural element, which can be a paragraph, a table, a list
  22114. item, etc. It also provides some special moves for convenience:
  22115. - On an affiliated keyword, jump to the beginning of the
  22116. relative element.
  22117. - On an item or a footnote definition, move to the second
  22118. element inside, if any.
  22119. - On a table or a property drawer, jump after it.
  22120. - On a verse or source block, stop after blank lines."
  22121. (interactive)
  22122. (when (eobp) (user-error "Cannot move further down"))
  22123. (let* ((deactivate-mark nil)
  22124. (element (org-element-at-point))
  22125. (type (org-element-type element))
  22126. (post-affiliated (org-element-property :post-affiliated element))
  22127. (contents-begin (org-element-property :contents-begin element))
  22128. (contents-end (org-element-property :contents-end element))
  22129. (end (let ((end (org-element-property :end element)) (parent element))
  22130. (while (and (setq parent (org-element-property :parent parent))
  22131. (= (org-element-property :contents-end parent) end))
  22132. (setq end (org-element-property :end parent)))
  22133. end)))
  22134. (cond ((not element)
  22135. (skip-chars-forward " \r\t\n")
  22136. (or (eobp) (beginning-of-line)))
  22137. ;; On affiliated keywords, move to element's beginning.
  22138. ((< (point) post-affiliated)
  22139. (goto-char post-affiliated))
  22140. ;; At a table row, move to the end of the table. Similarly,
  22141. ;; at a node property, move to the end of the property
  22142. ;; drawer.
  22143. ((memq type '(node-property table-row))
  22144. (goto-char (org-element-property
  22145. :end (org-element-property :parent element))))
  22146. ((memq type '(property-drawer table)) (goto-char end))
  22147. ;; Consider blank lines as separators in verse and source
  22148. ;; blocks to ease editing.
  22149. ((memq type '(src-block verse-block))
  22150. (when (eq type 'src-block)
  22151. (setq contents-end
  22152. (save-excursion (goto-char end)
  22153. (skip-chars-backward " \r\t\n")
  22154. (line-beginning-position))))
  22155. (beginning-of-line)
  22156. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22157. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22158. (goto-char end)
  22159. (skip-chars-forward " \r\t\n")
  22160. (if (= (point) contents-end) (goto-char end)
  22161. (beginning-of-line))))
  22162. ;; With no contents, just skip element.
  22163. ((not contents-begin) (goto-char end))
  22164. ;; If contents are invisible, skip the element altogether.
  22165. ((outline-invisible-p (line-end-position))
  22166. (case type
  22167. (headline
  22168. (org-with-limited-levels (outline-next-visible-heading 1)))
  22169. ;; At a plain list, make sure we move to the next item
  22170. ;; instead of skipping the whole list.
  22171. (plain-list (forward-char)
  22172. (org-forward-paragraph))
  22173. (otherwise (goto-char end))))
  22174. ((>= (point) contents-end) (goto-char end))
  22175. ((>= (point) contents-begin)
  22176. ;; This can only happen on paragraphs and plain lists.
  22177. (case type
  22178. (paragraph (goto-char end))
  22179. ;; At a plain list, try to move to second element in
  22180. ;; first item, if possible.
  22181. (plain-list (end-of-line)
  22182. (org-forward-paragraph))))
  22183. ;; When contents start on the middle of a line (e.g. in
  22184. ;; items and footnote definitions), try to reach first
  22185. ;; element starting after current line.
  22186. ((> (line-end-position) contents-begin)
  22187. (end-of-line)
  22188. (org-forward-paragraph))
  22189. (t (goto-char contents-begin)))))
  22190. (defun org-backward-paragraph ()
  22191. "Move backward to start of previous paragraph or equivalent.
  22192. The function moves point to the beginning of the current
  22193. structural element, which can be a paragraph, a table, a list
  22194. item, etc., or to the beginning of the previous visible one if
  22195. point is already there. It also provides some special moves for
  22196. convenience:
  22197. - On an affiliated keyword, jump to the first one.
  22198. - On a table or a property drawer, move to its beginning.
  22199. - On a verse or source block, stop before blank lines."
  22200. (interactive)
  22201. (when (bobp) (user-error "Cannot move further up"))
  22202. (let* ((deactivate-mark nil)
  22203. (element (org-element-at-point))
  22204. (type (org-element-type element))
  22205. (contents-begin (org-element-property :contents-begin element))
  22206. (contents-end (org-element-property :contents-end element))
  22207. (post-affiliated (org-element-property :post-affiliated element))
  22208. (begin (org-element-property :begin element)))
  22209. (cond
  22210. ((not element) (goto-char (point-min)))
  22211. ((= (point) begin)
  22212. (backward-char)
  22213. (org-backward-paragraph))
  22214. ((<= (point) post-affiliated) (goto-char begin))
  22215. ((memq type '(node-property table-row))
  22216. (goto-char (org-element-property
  22217. :post-affiliated (org-element-property :parent element))))
  22218. ((memq type '(property-drawer table)) (goto-char begin))
  22219. ((memq type '(src-block verse-block))
  22220. (when (eq type 'src-block)
  22221. (setq contents-begin
  22222. (save-excursion (goto-char begin) (forward-line) (point))))
  22223. (if (= (point) contents-begin) (goto-char post-affiliated)
  22224. ;; Inside a verse block, see blank lines as paragraph
  22225. ;; separators.
  22226. (let ((origin (point)))
  22227. (skip-chars-backward " \r\t\n" contents-begin)
  22228. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22229. (skip-chars-forward " \r\t\n" origin)
  22230. (if (= (point) origin) (goto-char contents-begin)
  22231. (beginning-of-line))))))
  22232. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22233. ((eq type 'paragraph)
  22234. (goto-char contents-begin)
  22235. ;; When at first paragraph in an item or a footnote definition,
  22236. ;; move directly to beginning of line.
  22237. (let ((parent-contents
  22238. (org-element-property
  22239. :contents-begin (org-element-property :parent element))))
  22240. (when (and parent-contents (= parent-contents contents-begin))
  22241. (beginning-of-line))))
  22242. ;; At the end of a greater element, move to the beginning of the
  22243. ;; last element within.
  22244. ((>= (point) contents-end)
  22245. (goto-char (1- contents-end))
  22246. (org-backward-paragraph))
  22247. (t (goto-char (or post-affiliated begin))))
  22248. ;; Ensure we never leave point invisible.
  22249. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22250. (defun org-forward-element ()
  22251. "Move forward by one element.
  22252. Move to the next element at the same level, when possible."
  22253. (interactive)
  22254. (cond ((eobp) (user-error "Cannot move further down"))
  22255. ((org-with-limited-levels (org-at-heading-p))
  22256. (let ((origin (point)))
  22257. (goto-char (org-end-of-subtree nil t))
  22258. (unless (org-with-limited-levels (org-at-heading-p))
  22259. (goto-char origin)
  22260. (user-error "Cannot move further down"))))
  22261. (t
  22262. (let* ((elem (org-element-at-point))
  22263. (end (org-element-property :end elem))
  22264. (parent (org-element-property :parent elem)))
  22265. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22266. (goto-char (org-element-property :end parent)))
  22267. ((integer-or-marker-p end) (goto-char end))
  22268. (t (message "No element at point")))))))
  22269. (defun org-backward-element ()
  22270. "Move backward by one element.
  22271. Move to the previous element at the same level, when possible."
  22272. (interactive)
  22273. (cond ((bobp) (user-error "Cannot move further up"))
  22274. ((org-with-limited-levels (org-at-heading-p))
  22275. ;; At a headline, move to the previous one, if any, or stay
  22276. ;; here.
  22277. (let ((origin (point)))
  22278. (org-with-limited-levels (org-backward-heading-same-level 1))
  22279. ;; When current headline has no sibling above, move to its
  22280. ;; parent.
  22281. (when (= (point) origin)
  22282. (or (org-with-limited-levels (org-up-heading-safe))
  22283. (progn (goto-char origin)
  22284. (user-error "Cannot move further up"))))))
  22285. (t
  22286. (let* ((elem (org-element-at-point))
  22287. (beg (org-element-property :begin elem)))
  22288. (cond
  22289. ;; Move to beginning of current element if point isn't
  22290. ;; there already.
  22291. ((null beg) (message "No element at point"))
  22292. ((/= (point) beg) (goto-char beg))
  22293. (t (goto-char beg)
  22294. (skip-chars-backward " \r\t\n")
  22295. (unless (bobp)
  22296. (let ((prev (org-element-at-point)))
  22297. (goto-char (org-element-property :begin prev))
  22298. (while (and (setq prev (org-element-property :parent prev))
  22299. (<= (org-element-property :end prev) beg))
  22300. (goto-char (org-element-property :begin prev)))))))))))
  22301. (defun org-up-element ()
  22302. "Move to upper element."
  22303. (interactive)
  22304. (if (org-with-limited-levels (org-at-heading-p))
  22305. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22306. (let* ((elem (org-element-at-point))
  22307. (parent (org-element-property :parent elem)))
  22308. (if parent (goto-char (org-element-property :begin parent))
  22309. (if (org-with-limited-levels (org-before-first-heading-p))
  22310. (user-error "No surrounding element")
  22311. (org-with-limited-levels (org-back-to-heading)))))))
  22312. (defvar org-element-greater-elements)
  22313. (defun org-down-element ()
  22314. "Move to inner element."
  22315. (interactive)
  22316. (let ((element (org-element-at-point)))
  22317. (cond
  22318. ((memq (org-element-type element) '(plain-list table))
  22319. (goto-char (org-element-property :contents-begin element))
  22320. (forward-char))
  22321. ((memq (org-element-type element) org-element-greater-elements)
  22322. ;; If contents are hidden, first disclose them.
  22323. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22324. (goto-char (or (org-element-property :contents-begin element)
  22325. (user-error "No content for this element"))))
  22326. (t (user-error "No inner element")))))
  22327. (defun org-drag-element-backward ()
  22328. "Move backward element at point."
  22329. (interactive)
  22330. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22331. (let* ((elem (org-element-at-point))
  22332. (prev-elem
  22333. (save-excursion
  22334. (goto-char (org-element-property :begin elem))
  22335. (skip-chars-backward " \r\t\n")
  22336. (unless (bobp)
  22337. (let* ((beg (org-element-property :begin elem))
  22338. (prev (org-element-at-point))
  22339. (up prev))
  22340. (while (and (setq up (org-element-property :parent up))
  22341. (<= (org-element-property :end up) beg))
  22342. (setq prev up))
  22343. prev)))))
  22344. ;; Error out if no previous element or previous element is
  22345. ;; a parent of the current one.
  22346. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22347. (user-error "Cannot drag element backward")
  22348. (let ((pos (point)))
  22349. (org-element-swap-A-B prev-elem elem)
  22350. (goto-char (+ (org-element-property :begin prev-elem)
  22351. (- pos (org-element-property :begin elem)))))))))
  22352. (defun org-drag-element-forward ()
  22353. "Move forward element at point."
  22354. (interactive)
  22355. (let* ((pos (point))
  22356. (elem (org-element-at-point)))
  22357. (when (= (point-max) (org-element-property :end elem))
  22358. (user-error "Cannot drag element forward"))
  22359. (goto-char (org-element-property :end elem))
  22360. (let ((next-elem (org-element-at-point)))
  22361. (when (or (org-element-nested-p elem next-elem)
  22362. (and (eq (org-element-type next-elem) 'headline)
  22363. (not (eq (org-element-type elem) 'headline))))
  22364. (goto-char pos)
  22365. (user-error "Cannot drag element forward"))
  22366. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22367. ;; body's length (without final blanks) and by the length of
  22368. ;; blanks between ELEM and NEXT-ELEM.
  22369. (let ((size-next (- (save-excursion
  22370. (goto-char (org-element-property :end next-elem))
  22371. (skip-chars-backward " \r\t\n")
  22372. (forward-line)
  22373. ;; Small correction if buffer doesn't end
  22374. ;; with a newline character.
  22375. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22376. (org-element-property :begin next-elem)))
  22377. (size-blank (- (org-element-property :end elem)
  22378. (save-excursion
  22379. (goto-char (org-element-property :end elem))
  22380. (skip-chars-backward " \r\t\n")
  22381. (forward-line)
  22382. (point)))))
  22383. (org-element-swap-A-B elem next-elem)
  22384. (goto-char (+ pos size-next size-blank))))))
  22385. (defun org-drag-line-forward (arg)
  22386. "Drag the line at point ARG lines forward."
  22387. (interactive "p")
  22388. (dotimes (n (abs arg))
  22389. (let ((c (current-column)))
  22390. (if (< 0 arg)
  22391. (progn
  22392. (beginning-of-line 2)
  22393. (transpose-lines 1)
  22394. (beginning-of-line 0))
  22395. (transpose-lines 1)
  22396. (beginning-of-line -1))
  22397. (org-move-to-column c))))
  22398. (defun org-drag-line-backward (arg)
  22399. "Drag the line at point ARG lines backward."
  22400. (interactive "p")
  22401. (org-drag-line-forward (- arg)))
  22402. (defun org-mark-element ()
  22403. "Put point at beginning of this element, mark at end.
  22404. Interactively, if this command is repeated or (in Transient Mark
  22405. mode) if the mark is active, it marks the next element after the
  22406. ones already marked."
  22407. (interactive)
  22408. (let (deactivate-mark)
  22409. (if (and (org-called-interactively-p 'any)
  22410. (or (and (eq last-command this-command) (mark t))
  22411. (and transient-mark-mode mark-active)))
  22412. (set-mark
  22413. (save-excursion
  22414. (goto-char (mark))
  22415. (goto-char (org-element-property :end (org-element-at-point)))))
  22416. (let ((element (org-element-at-point)))
  22417. (end-of-line)
  22418. (push-mark (org-element-property :end element) t t)
  22419. (goto-char (org-element-property :begin element))))))
  22420. (defun org-narrow-to-element ()
  22421. "Narrow buffer to current element."
  22422. (interactive)
  22423. (let ((elem (org-element-at-point)))
  22424. (cond
  22425. ((eq (car elem) 'headline)
  22426. (narrow-to-region
  22427. (org-element-property :begin elem)
  22428. (org-element-property :end elem)))
  22429. ((memq (car elem) org-element-greater-elements)
  22430. (narrow-to-region
  22431. (org-element-property :contents-begin elem)
  22432. (org-element-property :contents-end elem)))
  22433. (t
  22434. (narrow-to-region
  22435. (org-element-property :begin elem)
  22436. (org-element-property :end elem))))))
  22437. (defun org-transpose-element ()
  22438. "Transpose current and previous elements, keeping blank lines between.
  22439. Point is moved after both elements."
  22440. (interactive)
  22441. (org-skip-whitespace)
  22442. (let ((end (org-element-property :end (org-element-at-point))))
  22443. (org-drag-element-backward)
  22444. (goto-char end)))
  22445. (defun org-unindent-buffer ()
  22446. "Un-indent the visible part of the buffer.
  22447. Relative indentation (between items, inside blocks, etc.) isn't
  22448. modified."
  22449. (interactive)
  22450. (unless (eq major-mode 'org-mode)
  22451. (user-error "Cannot un-indent a buffer not in Org mode"))
  22452. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22453. unindent-tree ; For byte-compiler.
  22454. (unindent-tree
  22455. (function
  22456. (lambda (contents)
  22457. (mapc
  22458. (lambda (element)
  22459. (if (memq (org-element-type element) '(headline section))
  22460. (funcall unindent-tree (org-element-contents element))
  22461. (save-excursion
  22462. (save-restriction
  22463. (narrow-to-region
  22464. (org-element-property :begin element)
  22465. (org-element-property :end element))
  22466. (org-do-remove-indentation)))))
  22467. (reverse contents))))))
  22468. (funcall unindent-tree (org-element-contents parse-tree))))
  22469. (defun org-show-subtree ()
  22470. "Show everything after this heading at deeper levels."
  22471. (interactive)
  22472. (outline-flag-region
  22473. (point)
  22474. (save-excursion
  22475. (org-end-of-subtree t t))
  22476. nil))
  22477. (defun org-show-entry ()
  22478. "Show the body directly following this heading.
  22479. Show the heading too, if it is currently invisible."
  22480. (interactive)
  22481. (save-excursion
  22482. (ignore-errors
  22483. (org-back-to-heading t)
  22484. (outline-flag-region
  22485. (max (point-min) (1- (point)))
  22486. (save-excursion
  22487. (if (re-search-forward
  22488. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22489. (match-beginning 1)
  22490. (point-max)))
  22491. nil)
  22492. (org-cycle-hide-drawers 'children))))
  22493. (defun org-make-options-regexp (kwds &optional extra)
  22494. "Make a regular expression for keyword lines.
  22495. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22496. when non-nil, is a regexp matching keywords names."
  22497. (concat "^[ \t]*#\\+\\("
  22498. (regexp-opt kwds)
  22499. (and extra (concat (and kwds "\\|") extra))
  22500. "\\):[ \t]*\\(.*\\)"))
  22501. ;; Make isearch reveal the necessary context
  22502. (defun org-isearch-end ()
  22503. "Reveal context after isearch exits."
  22504. (when isearch-success ; only if search was successful
  22505. (if (featurep 'xemacs)
  22506. ;; Under XEmacs, the hook is run in the correct place,
  22507. ;; we directly show the context.
  22508. (org-show-context 'isearch)
  22509. ;; In Emacs the hook runs *before* restoring the overlays.
  22510. ;; So we have to use a one-time post-command-hook to do this.
  22511. ;; (Emacs 22 has a special variable, see function `org-mode')
  22512. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22513. isearch-mode-end-hook-quit)
  22514. ;; Only when the isearch was not quitted.
  22515. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22516. 'append 'local)))))
  22517. (defun org-isearch-post-command ()
  22518. "Remove self from hook, and show context."
  22519. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22520. (org-show-context 'isearch))
  22521. ;;;; Integration with and fixes for other packages
  22522. ;;; Imenu support
  22523. (defvar org-imenu-markers nil
  22524. "All markers currently used by Imenu.")
  22525. (make-variable-buffer-local 'org-imenu-markers)
  22526. (defun org-imenu-new-marker (&optional pos)
  22527. "Return a new marker for use by Imenu, and remember the marker."
  22528. (let ((m (make-marker)))
  22529. (move-marker m (or pos (point)))
  22530. (push m org-imenu-markers)
  22531. m))
  22532. (defun org-imenu-get-tree ()
  22533. "Produce the index for Imenu."
  22534. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22535. (setq org-imenu-markers nil)
  22536. (let* ((n org-imenu-depth)
  22537. (re (concat "^" (org-get-limited-outline-regexp)))
  22538. (subs (make-vector (1+ n) nil))
  22539. (last-level 0)
  22540. m level head0 head)
  22541. (save-excursion
  22542. (save-restriction
  22543. (widen)
  22544. (goto-char (point-max))
  22545. (while (re-search-backward re nil t)
  22546. (setq level (org-reduced-level (funcall outline-level)))
  22547. (when (and (<= level n)
  22548. (looking-at org-complex-heading-regexp)
  22549. (setq head0 (org-match-string-no-properties 4)))
  22550. (setq head (org-link-display-format head0)
  22551. m (org-imenu-new-marker))
  22552. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22553. (if (>= level last-level)
  22554. (push (cons head m) (aref subs level))
  22555. (push (cons head (aref subs (1+ level))) (aref subs level))
  22556. (loop for i from (1+ level) to n do (aset subs i nil)))
  22557. (setq last-level level)))))
  22558. (aref subs 1)))
  22559. (eval-after-load "imenu"
  22560. '(progn
  22561. (add-hook 'imenu-after-jump-hook
  22562. (lambda ()
  22563. (if (derived-mode-p 'org-mode)
  22564. (org-show-context 'org-goto))))))
  22565. (defun org-link-display-format (link)
  22566. "Replace a link with its the description.
  22567. If there is no description, use the link target."
  22568. (save-match-data
  22569. (if (string-match org-bracket-link-analytic-regexp link)
  22570. (replace-match (if (match-end 5)
  22571. (match-string 5 link)
  22572. (concat (match-string 1 link)
  22573. (match-string 3 link)))
  22574. nil t link)
  22575. link)))
  22576. (defun org-toggle-link-display ()
  22577. "Toggle the literal or descriptive display of links."
  22578. (interactive)
  22579. (if org-descriptive-links
  22580. (progn (org-remove-from-invisibility-spec '(org-link))
  22581. (org-restart-font-lock)
  22582. (setq org-descriptive-links nil))
  22583. (progn (add-to-invisibility-spec '(org-link))
  22584. (org-restart-font-lock)
  22585. (setq org-descriptive-links t))))
  22586. ;; Speedbar support
  22587. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22588. "Overlay marking the agenda restriction line in speedbar.")
  22589. (overlay-put org-speedbar-restriction-lock-overlay
  22590. 'face 'org-agenda-restriction-lock)
  22591. (overlay-put org-speedbar-restriction-lock-overlay
  22592. 'help-echo "Agendas are currently limited to this item.")
  22593. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22594. (defun org-speedbar-set-agenda-restriction ()
  22595. "Restrict future agenda commands to the location at point in speedbar.
  22596. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22597. (interactive)
  22598. (require 'org-agenda)
  22599. (let (p m tp np dir txt)
  22600. (cond
  22601. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22602. 'org-imenu t))
  22603. (setq m (get-text-property p 'org-imenu-marker))
  22604. (with-current-buffer (marker-buffer m)
  22605. (goto-char m)
  22606. (org-agenda-set-restriction-lock 'subtree)))
  22607. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22608. 'speedbar-function 'speedbar-find-file))
  22609. (setq tp (previous-single-property-change
  22610. (1+ p) 'speedbar-function)
  22611. np (next-single-property-change
  22612. tp 'speedbar-function)
  22613. dir (speedbar-line-directory)
  22614. txt (buffer-substring-no-properties (or tp (point-min))
  22615. (or np (point-max))))
  22616. (with-current-buffer (find-file-noselect
  22617. (let ((default-directory dir))
  22618. (expand-file-name txt)))
  22619. (unless (derived-mode-p 'org-mode)
  22620. (user-error "Cannot restrict to non-Org-mode file"))
  22621. (org-agenda-set-restriction-lock 'file)))
  22622. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22623. (move-overlay org-speedbar-restriction-lock-overlay
  22624. (point-at-bol) (point-at-eol))
  22625. (setq current-prefix-arg nil)
  22626. (org-agenda-maybe-redo)))
  22627. (defvar speedbar-file-key-map)
  22628. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22629. (eval-after-load "speedbar"
  22630. '(progn
  22631. (speedbar-add-supported-extension ".org")
  22632. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22633. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22634. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22635. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22636. (add-hook 'speedbar-visiting-tag-hook
  22637. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22638. ;;; Fixes and Hacks for problems with other packages
  22639. (defun org--flyspell-object-check-p (element)
  22640. "Non-nil when Flyspell can check object at point.
  22641. ELEMENT is the element at point."
  22642. (let ((object (save-excursion
  22643. (when (org-looking-at-p "\\>") (backward-char))
  22644. (org-element-context element))))
  22645. (case (org-element-type object)
  22646. ;; Prevent checks in links due to keybinding conflict with
  22647. ;; Flyspell.
  22648. ((code entity export-snippet inline-babel-call
  22649. inline-src-block line-break latex-fragment link macro
  22650. statistics-cookie target timestamp verbatim)
  22651. nil)
  22652. (footnote-reference
  22653. ;; Only in inline footnotes, within the definition.
  22654. (and (eq (org-element-property :type object) 'inline)
  22655. (< (save-excursion
  22656. (goto-char (org-element-property :begin object))
  22657. (search-forward ":" nil t 2))
  22658. (point))))
  22659. (otherwise t))))
  22660. (defun org-mode-flyspell-verify ()
  22661. "Function used for `flyspell-generic-check-word-predicate'."
  22662. (if (org-at-heading-p)
  22663. ;; At a headline or an inlinetask, check title only. This is
  22664. ;; faster than relying on `org-element-at-point'.
  22665. (and (save-excursion (beginning-of-line)
  22666. (and (let ((case-fold-search t))
  22667. (not (looking-at "\\*+ END[ \t]*$")))
  22668. (looking-at org-complex-heading-regexp)))
  22669. (match-beginning 4)
  22670. (>= (point) (match-beginning 4))
  22671. (or (not (match-beginning 5))
  22672. (< (point) (match-beginning 5))))
  22673. (let* ((element (org-element-at-point))
  22674. (post-affiliated (org-element-property :post-affiliated element)))
  22675. (cond
  22676. ;; Ignore checks in all affiliated keywords but captions.
  22677. ((< (point) post-affiliated)
  22678. (and (save-excursion
  22679. (beginning-of-line)
  22680. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22681. (> (point) (match-end 0))
  22682. (org--flyspell-object-check-p element)))
  22683. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22684. ((let ((log (org-log-into-drawer)))
  22685. (and log
  22686. (let ((drawer (org-element-lineage element '(drawer))))
  22687. (and drawer
  22688. (eq (compare-strings
  22689. log nil nil
  22690. (org-element-property :drawer-name drawer) nil nil t)
  22691. t)))))
  22692. nil)
  22693. (t
  22694. (case (org-element-type element)
  22695. ((comment quote-section) t)
  22696. (comment-block
  22697. ;; Allow checks between block markers, not on them.
  22698. (and (> (line-beginning-position) post-affiliated)
  22699. (save-excursion
  22700. (end-of-line)
  22701. (skip-chars-forward " \r\t\n")
  22702. (< (point) (org-element-property :end element)))))
  22703. ;; Arbitrary list of keywords where checks are meaningful.
  22704. ;; Make sure point is on the value part of the element.
  22705. (keyword
  22706. (and (member (org-element-property :key element)
  22707. '("DESCRIPTION" "TITLE"))
  22708. (< (save-excursion
  22709. (beginning-of-line) (search-forward ":") (point))
  22710. (point))))
  22711. ;; Check is globally allowed in paragraphs verse blocks and
  22712. ;; table rows (after affiliated keywords) but some objects
  22713. ;; must not be affected.
  22714. ((paragraph table-row verse-block)
  22715. (let ((cbeg (org-element-property :contents-begin element))
  22716. (cend (org-element-property :contents-end element)))
  22717. (and cbeg (>= (point) cbeg) (< (point) cend)
  22718. (org--flyspell-object-check-p element))))))))))
  22719. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22720. (defun org-remove-flyspell-overlays-in (beg end)
  22721. "Remove flyspell overlays in region."
  22722. (and (org-bound-and-true-p flyspell-mode)
  22723. (fboundp 'flyspell-delete-region-overlays)
  22724. (flyspell-delete-region-overlays beg end)))
  22725. (defvar flyspell-delayed-commands)
  22726. (eval-after-load "flyspell"
  22727. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22728. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22729. (eval-after-load "bookmark"
  22730. '(if (boundp 'bookmark-after-jump-hook)
  22731. ;; We can use the hook
  22732. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22733. ;; Hook not available, use advice
  22734. (defadvice bookmark-jump (after org-make-visible activate)
  22735. "Make the position visible."
  22736. (org-bookmark-jump-unhide))))
  22737. ;; Make sure saveplace shows the location if it was hidden
  22738. (eval-after-load "saveplace"
  22739. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22740. "Make the position visible."
  22741. (org-bookmark-jump-unhide)))
  22742. ;; Make sure ecb shows the location if it was hidden
  22743. (eval-after-load "ecb"
  22744. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22745. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22746. (if (derived-mode-p 'org-mode)
  22747. (org-show-context))))
  22748. (defun org-bookmark-jump-unhide ()
  22749. "Unhide the current position, to show the bookmark location."
  22750. (and (derived-mode-p 'org-mode)
  22751. (or (outline-invisible-p)
  22752. (save-excursion (goto-char (max (point-min) (1- (point))))
  22753. (outline-invisible-p)))
  22754. (org-show-context 'bookmark-jump)))
  22755. (defun org-mark-jump-unhide ()
  22756. "Make the point visible with `org-show-context' after jumping to the mark."
  22757. (when (and (derived-mode-p 'org-mode)
  22758. (outline-invisible-p))
  22759. (org-show-context 'mark-goto)))
  22760. (eval-after-load "simple"
  22761. '(defadvice pop-to-mark-command (after org-make-visible activate)
  22762. "Make the point visible with `org-show-context'."
  22763. (org-mark-jump-unhide)))
  22764. (eval-after-load "simple"
  22765. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  22766. "Make the point visible with `org-show-context'."
  22767. (org-mark-jump-unhide)))
  22768. (eval-after-load "simple"
  22769. '(defadvice pop-global-mark (after org-make-visible activate)
  22770. "Make the point visible with `org-show-context'."
  22771. (org-mark-jump-unhide)))
  22772. ;; Make session.el ignore our circular variable
  22773. (defvar session-globals-exclude)
  22774. (eval-after-load "session"
  22775. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22776. ;;;; Finish up
  22777. (provide 'org)
  22778. (run-hooks 'org-load-hook)
  22779. ;;; org.el ends here