org.el 896 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 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. (load "org-loaddefs.el" t t t)
  73. (require 'org-macs)
  74. (require 'org-compat)
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches a headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (org-defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (org-defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  106. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "ox-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-table-set-constants "org-table" ())
  114. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-agenda-redo "org-agenda" (&optional all))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-restriction "org-element" (element))
  145. (declare-function org-element-type "org-element" (element))
  146. ;; load languages based on value of `org-babel-load-languages'
  147. (defvar org-babel-load-languages)
  148. ;;;###autoload
  149. (defun org-babel-do-load-languages (sym value)
  150. "Load the languages defined in `org-babel-load-languages'."
  151. (set-default sym value)
  152. (mapc (lambda (pair)
  153. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  154. (if active
  155. (progn
  156. (require (intern (concat "ob-" lang))))
  157. (progn
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-execute:" lang)))
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-expand-body:" lang)))))))
  162. org-babel-load-languages))
  163. ;;;###autoload
  164. (defun org-babel-load-file (file &optional compile)
  165. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  166. This function exports the source code using `org-babel-tangle'
  167. and then loads the resulting file using `load-file'. With prefix
  168. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  169. file to byte-code before it is loaded."
  170. (interactive "fFile to load: \nP")
  171. (require 'ob-core)
  172. (let* ((age (lambda (file)
  173. (float-time
  174. (time-subtract (current-time)
  175. (nth 5 (or (file-attributes (file-truename file))
  176. (file-attributes file)))))))
  177. (base-name (file-name-sans-extension file))
  178. (exported-file (concat base-name ".el")))
  179. ;; tangle if the org-mode file is newer than the elisp file
  180. (unless (and (file-exists-p exported-file)
  181. (> (funcall age file) (funcall age exported-file)))
  182. (org-babel-tangle-file file exported-file "emacs-lisp"))
  183. (message "%s %s"
  184. (if compile
  185. (progn (byte-compile-file exported-file 'load)
  186. "Compiled and loaded")
  187. (progn (load-file exported-file) "Loaded"))
  188. exported-file)))
  189. (defcustom org-babel-load-languages '((emacs-lisp . t))
  190. "Languages which can be evaluated in Org-mode buffers.
  191. This list can be used to load support for any of the languages
  192. below, note that each language will depend on a different set of
  193. system executables and/or Emacs modes. When a language is
  194. \"loaded\", then code blocks in that language can be evaluated
  195. with `org-babel-execute-src-block' bound by default to C-c
  196. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  197. be set to remove code block evaluation from the C-c C-c
  198. keybinding. By default only Emacs Lisp (which has no
  199. requirements) is loaded."
  200. :group 'org-babel
  201. :set 'org-babel-do-load-languages
  202. :version "24.1"
  203. :type '(alist :tag "Babel Languages"
  204. :key-type
  205. (choice
  206. (const :tag "Awk" awk)
  207. (const :tag "C" C)
  208. (const :tag "R" R)
  209. (const :tag "Asymptote" asymptote)
  210. (const :tag "Calc" calc)
  211. (const :tag "Clojure" clojure)
  212. (const :tag "CSS" css)
  213. (const :tag "Ditaa" ditaa)
  214. (const :tag "Dot" dot)
  215. (const :tag "Emacs Lisp" emacs-lisp)
  216. (const :tag "Fortran" fortran)
  217. (const :tag "Gnuplot" gnuplot)
  218. (const :tag "Haskell" haskell)
  219. (const :tag "IO" io)
  220. (const :tag "Java" java)
  221. (const :tag "Javascript" js)
  222. (const :tag "LaTeX" latex)
  223. (const :tag "Ledger" ledger)
  224. (const :tag "Lilypond" lilypond)
  225. (const :tag "Lisp" lisp)
  226. (const :tag "Makefile" makefile)
  227. (const :tag "Maxima" maxima)
  228. (const :tag "Matlab" matlab)
  229. (const :tag "Mscgen" mscgen)
  230. (const :tag "Ocaml" ocaml)
  231. (const :tag "Octave" octave)
  232. (const :tag "Org" org)
  233. (const :tag "Perl" perl)
  234. (const :tag "Pico Lisp" picolisp)
  235. (const :tag "PlantUML" plantuml)
  236. (const :tag "Python" python)
  237. (const :tag "Ruby" ruby)
  238. (const :tag "Sass" sass)
  239. (const :tag "Scala" scala)
  240. (const :tag "Scheme" scheme)
  241. (const :tag "Screen" screen)
  242. (const :tag "Shell Script" sh)
  243. (const :tag "Shen" shen)
  244. (const :tag "Sql" sql)
  245. (const :tag "Sqlite" sqlite))
  246. :value-type (boolean :tag "Activate" :value t)))
  247. ;;;; Customization variables
  248. (defcustom org-clone-delete-id nil
  249. "Remove ID property of clones of a subtree.
  250. When non-nil, clones of a subtree don't inherit the ID property.
  251. Otherwise they inherit the ID property with a new unique
  252. identifier."
  253. :type 'boolean
  254. :version "24.1"
  255. :group 'org-id)
  256. ;;; Version
  257. (org-check-version)
  258. ;;;###autoload
  259. (defun org-version (&optional here full message)
  260. "Show the org-mode version in the echo area.
  261. With prefix argument HERE, insert it at point.
  262. When FULL is non-nil, use a verbose version string.
  263. When MESSAGE is non-nil, display a message with the version."
  264. (interactive "P")
  265. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  266. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  267. (load-suffixes (list ".el"))
  268. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  269. (org-trash (or
  270. (and (fboundp 'org-release) (fboundp 'org-git-version))
  271. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  272. (load-suffixes save-load-suffixes)
  273. (org-version (org-release))
  274. (git-version (org-git-version))
  275. (version (format "Org-mode version %s (%s @ %s)"
  276. org-version
  277. git-version
  278. (if org-install-dir
  279. (if (string= org-dir org-install-dir)
  280. org-install-dir
  281. (concat "mixed installation! " org-install-dir " and " org-dir))
  282. "org-loaddefs.el can not be found!")))
  283. (_version (if full version org-version)))
  284. (if (org-called-interactively-p 'interactive)
  285. (if here
  286. (insert version)
  287. (message version))
  288. (if message (message _version))
  289. _version)))
  290. (defconst org-version (org-version))
  291. ;;; Compatibility constants
  292. ;;; The custom variables
  293. (defgroup org nil
  294. "Outline-based notes management and organizer."
  295. :tag "Org"
  296. :group 'outlines
  297. :group 'calendar)
  298. (defcustom org-mode-hook nil
  299. "Mode hook for Org-mode, run after the mode was turned on."
  300. :group 'org
  301. :type 'hook)
  302. (defcustom org-load-hook nil
  303. "Hook that is run after org.el has been loaded."
  304. :group 'org
  305. :type 'hook)
  306. (defcustom org-log-buffer-setup-hook nil
  307. "Hook that is run after an Org log buffer is created."
  308. :group 'org
  309. :version "24.1"
  310. :type 'hook)
  311. (defvar org-modules) ; defined below
  312. (defvar org-modules-loaded nil
  313. "Have the modules been loaded already?")
  314. (defun org-load-modules-maybe (&optional force)
  315. "Load all extensions listed in `org-modules'."
  316. (when (or force (not org-modules-loaded))
  317. (mapc (lambda (ext)
  318. (condition-case nil (require ext)
  319. (error (message "Problems while trying to load feature `%s'" ext))))
  320. org-modules)
  321. (setq org-modules-loaded t)))
  322. (defun org-set-modules (var value)
  323. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  324. (set var value)
  325. (when (featurep 'org)
  326. (org-load-modules-maybe 'force)))
  327. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  328. "Modules that should always be loaded together with org.el.
  329. If a description starts with <C>, the file is not part of Emacs
  330. and loading it will require that you have downloaded and properly
  331. installed the Org mode distribution.
  332. You can also use this system to load external packages (i.e. neither Org
  333. core modules, nor modules from the CONTRIB directory). Just add symbols
  334. to the end of the list. If the package is called org-xyz.el, then you need
  335. to add the symbol `xyz', and the package must have a call to:
  336. \(provide 'org-xyz)
  337. For export specific modules, see also `org-export-backends'."
  338. :group 'org
  339. :set 'org-set-modules
  340. :version "24.4"
  341. :package-version '(Org . "8.0")
  342. :type
  343. '(set :greedy t
  344. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  345. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  346. (const :tag " crypt: Encryption of subtrees" org-crypt)
  347. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  348. (const :tag " docview: Links to doc-view buffers" org-docview)
  349. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  350. (const :tag " habit: Track your consistency with habits" org-habit)
  351. (const :tag " id: Global IDs for identifying entries" org-id)
  352. (const :tag " info: Links to Info nodes" org-info)
  353. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  354. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  355. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  356. (const :tag " mouse: Additional mouse support" org-mouse)
  357. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  358. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  359. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  360. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  361. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  362. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  363. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  364. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  365. (const :tag "C collector: Collect properties into tables" org-collector)
  366. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  367. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  368. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  369. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  370. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  371. (const :tag "C eval: Include command output as text" org-eval)
  372. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  373. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  374. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  375. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  376. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  377. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  378. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  379. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  380. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  381. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  382. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  383. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  384. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  385. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  386. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  387. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  388. (const :tag "C registry: A registry for Org-mode links" org-registry)
  389. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  390. (const :tag "C secretary: Team management with org-mode" org-secretary)
  391. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  392. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  393. (const :tag "C track: Keep up with Org-mode development" org-track)
  394. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  395. (const :tag "C vm: Links to VM folders/messages" org-vm)
  396. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  397. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  398. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  399. (defvar org-export--registered-backends) ; From ox.el.
  400. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  401. (declare-function org-export-backend-name "ox" (backend))
  402. (defcustom org-export-backends '(ascii html icalendar latex)
  403. "List of export back-ends that should be always available.
  404. If a description starts with <C>, the file is not part of Emacs
  405. and loading it will require that you have downloaded and properly
  406. installed the Org mode distribution.
  407. Unlike to `org-modules', libraries in this list will not be
  408. loaded along with Org, but only once the export framework is
  409. needed.
  410. This variable needs to be set before org.el is loaded. If you
  411. need to make a change while Emacs is running, use the customize
  412. interface or run the following code, where VAL stands for the new
  413. value of the variable, after updating it:
  414. \(progn
  415. \(setq org-export--registered-backends
  416. \(org-remove-if-not
  417. \(lambda (backend)
  418. \(let ((name (org-export-backend-name backend)))
  419. \(or (memq name val)
  420. \(catch 'parentp
  421. \(dolist (b val)
  422. \(and (org-export-derived-backend-p b name)
  423. \(throw 'parentp t)))))))
  424. org-export--registered-backends))
  425. \(let ((new-list (mapcar 'org-export-backend-name
  426. org-export--registered-backends)))
  427. \(dolist (backend val)
  428. \(cond
  429. \((not (load (format \"ox-%s\" backend) t t))
  430. \(message \"Problems while trying to load export back-end `%s'\"
  431. backend))
  432. \((not (memq backend new-list)) (push backend new-list))))
  433. \(set-default 'org-export-backends new-list)))
  434. Adding a back-end to this list will also pull the back-end it
  435. depends on, if any."
  436. :group 'org
  437. :group 'org-export
  438. :version "24.4"
  439. :package-version '(Org . "8.0")
  440. :initialize 'custom-initialize-set
  441. :set (lambda (var val)
  442. (if (not (featurep 'ox)) (set-default var val)
  443. ;; Any back-end not required anymore (not present in VAL and not
  444. ;; a parent of any back-end in the new value) is removed from the
  445. ;; list of registered back-ends.
  446. (setq org-export--registered-backends
  447. (org-remove-if-not
  448. (lambda (backend)
  449. (let ((name (org-export-backend-name backend)))
  450. (or (memq name val)
  451. (catch 'parentp
  452. (dolist (b val)
  453. (and (org-export-derived-backend-p b name)
  454. (throw 'parentp t)))))))
  455. org-export--registered-backends))
  456. ;; Now build NEW-LIST of both new back-ends and required
  457. ;; parents.
  458. (let ((new-list (mapcar 'org-export-backend-name
  459. org-export--registered-backends)))
  460. (dolist (backend val)
  461. (cond
  462. ((not (load (format "ox-%s" backend) t t))
  463. (message "Problems while trying to load export back-end `%s'"
  464. backend))
  465. ((not (memq backend new-list)) (push backend new-list))))
  466. ;; Set VAR to that list with fixed dependencies.
  467. (set-default var new-list))))
  468. :type '(set :greedy t
  469. (const :tag " ascii Export buffer to ASCII format" ascii)
  470. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  471. (const :tag " html Export buffer to HTML format" html)
  472. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  473. (const :tag " latex Export buffer to LaTeX format" latex)
  474. (const :tag " man Export buffer to MAN format" man)
  475. (const :tag " md Export buffer to Markdown format" md)
  476. (const :tag " odt Export buffer to ODT format" odt)
  477. (const :tag " org Export buffer to Org format" org)
  478. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  479. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  480. (const :tag "C deck Export buffer to deck.js presentations" deck)
  481. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  482. (const :tag "C groff Export buffer to Groff format" groff)
  483. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  484. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  485. (const :tag "C s5 Export buffer to s5 presentations" s5)
  486. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  487. (eval-after-load 'ox
  488. '(mapc
  489. (lambda (backend)
  490. (condition-case nil (require (intern (format "ox-%s" backend)))
  491. (error (message "Problems while trying to load export back-end `%s'"
  492. backend))))
  493. org-export-backends))
  494. (defcustom org-support-shift-select nil
  495. "Non-nil means make shift-cursor commands select text when possible.
  496. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  497. start selecting a region, or enlarge regions started in this way.
  498. In Org-mode, in special contexts, these same keys are used for
  499. other purposes, important enough to compete with shift selection.
  500. Org tries to balance these needs by supporting `shift-select-mode'
  501. outside these special contexts, under control of this variable.
  502. The default of this variable is nil, to avoid confusing behavior. Shifted
  503. cursor keys will then execute Org commands in the following contexts:
  504. - on a headline, changing TODO state (left/right) and priority (up/down)
  505. - on a time stamp, changing the time
  506. - in a plain list item, changing the bullet type
  507. - in a property definition line, switching between allowed values
  508. - in the BEGIN line of a clock table (changing the time block).
  509. Outside these contexts, the commands will throw an error.
  510. When this variable is t and the cursor is not in a special
  511. context, Org-mode will support shift-selection for making and
  512. enlarging regions. To make this more effective, the bullet
  513. cycling will no longer happen anywhere in an item line, but only
  514. if the cursor is exactly on the bullet.
  515. If you set this variable to the symbol `always', then the keys
  516. will not be special in headlines, property lines, and item lines,
  517. to make shift selection work there as well. If this is what you
  518. want, you can use the following alternative commands: `C-c C-t'
  519. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  520. can be used to switch TODO sets, `C-c -' to cycle item bullet
  521. types, and properties can be edited by hand or in column view.
  522. However, when the cursor is on a timestamp, shift-cursor commands
  523. will still edit the time stamp - this is just too good to give up.
  524. XEmacs user should have this variable set to nil, because
  525. `shift-select-mode' is in Emacs 23 or later only."
  526. :group 'org
  527. :type '(choice
  528. (const :tag "Never" nil)
  529. (const :tag "When outside special context" t)
  530. (const :tag "Everywhere except timestamps" always)))
  531. (defcustom org-loop-over-headlines-in-active-region nil
  532. "Shall some commands act upon headlines in the active region?
  533. When set to `t', some commands will be performed in all headlines
  534. within the active region.
  535. When set to `start-level', some commands will be performed in all
  536. headlines within the active region, provided that these headlines
  537. are of the same level than the first one.
  538. When set to a string, those commands will be performed on the
  539. matching headlines within the active region. Such string must be
  540. a tags/property/todo match as it is used in the agenda tags view.
  541. The list of commands is: `org-schedule', `org-deadline',
  542. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  543. `org-archive-to-archive-sibling'. The archiving commands skip
  544. already archived entries."
  545. :type '(choice (const :tag "Don't loop" nil)
  546. (const :tag "All headlines in active region" t)
  547. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  548. (string :tag "Tags/Property/Todo matcher"))
  549. :version "24.1"
  550. :group 'org-todo
  551. :group 'org-archive)
  552. (defgroup org-startup nil
  553. "Options concerning startup of Org-mode."
  554. :tag "Org Startup"
  555. :group 'org)
  556. (defcustom org-startup-folded t
  557. "Non-nil means entering Org-mode will switch to OVERVIEW.
  558. This can also be configured on a per-file basis by adding one of
  559. the following lines anywhere in the buffer:
  560. #+STARTUP: fold (or `overview', this is equivalent)
  561. #+STARTUP: nofold (or `showall', this is equivalent)
  562. #+STARTUP: content
  563. #+STARTUP: showeverything
  564. By default, this option is ignored when Org opens agenda files
  565. for the first time. If you want the agenda to honor the startup
  566. option, set `org-agenda-inhibit-startup' to nil."
  567. :group 'org-startup
  568. :type '(choice
  569. (const :tag "nofold: show all" nil)
  570. (const :tag "fold: overview" t)
  571. (const :tag "content: all headlines" content)
  572. (const :tag "show everything, even drawers" showeverything)))
  573. (defcustom org-startup-truncated t
  574. "Non-nil means entering Org-mode will set `truncate-lines'.
  575. This is useful since some lines containing links can be very long and
  576. uninteresting. Also tables look terrible when wrapped."
  577. :group 'org-startup
  578. :type 'boolean)
  579. (defcustom org-startup-indented nil
  580. "Non-nil means turn on `org-indent-mode' on startup.
  581. This can also be configured on a per-file basis by adding one of
  582. the following lines anywhere in the buffer:
  583. #+STARTUP: indent
  584. #+STARTUP: noindent"
  585. :group 'org-structure
  586. :type '(choice
  587. (const :tag "Not" nil)
  588. (const :tag "Globally (slow on startup in large files)" t)))
  589. (defcustom org-use-sub-superscripts t
  590. "Non-nil means interpret \"_\" and \"^\" for display.
  591. When this option is turned on, you can use TeX-like syntax for sub- and
  592. superscripts. Several characters after \"_\" or \"^\" will be
  593. considered as a single item - so grouping with {} is normally not
  594. needed. For example, the following things will be parsed as single
  595. sub- or superscripts.
  596. 10^24 or 10^tau several digits will be considered 1 item.
  597. 10^-12 or 10^-tau a leading sign with digits or a word
  598. x^2-y^3 will be read as x^2 - y^3, because items are
  599. terminated by almost any nonword/nondigit char.
  600. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  601. Still, ambiguity is possible - so when in doubt use {} to enclose
  602. the sub/superscript. If you set this variable to the symbol
  603. `{}', the braces are *required* in order to trigger
  604. interpretations as sub/superscript. This can be helpful in
  605. documents that need \"_\" frequently in plain text."
  606. :group 'org-startup
  607. :version "24.1"
  608. :type '(choice
  609. (const :tag "Always interpret" t)
  610. (const :tag "Only with braces" {})
  611. (const :tag "Never interpret" nil)))
  612. (defcustom org-startup-with-beamer-mode nil
  613. "Non-nil means turn on `org-beamer-mode' on startup.
  614. This can also be configured on a per-file basis by adding one of
  615. the following lines anywhere in the buffer:
  616. #+STARTUP: beamer"
  617. :group 'org-startup
  618. :version "24.1"
  619. :type 'boolean)
  620. (defcustom org-startup-align-all-tables nil
  621. "Non-nil means align all tables when visiting a file.
  622. This is useful when the column width in tables is forced with <N> cookies
  623. in table fields. Such tables will look correct only after the first re-align.
  624. This can also be configured on a per-file basis by adding one of
  625. the following lines anywhere in the buffer:
  626. #+STARTUP: align
  627. #+STARTUP: noalign"
  628. :group 'org-startup
  629. :type 'boolean)
  630. (defcustom org-startup-with-inline-images nil
  631. "Non-nil means show inline images when loading a new Org file.
  632. This can also be configured on a per-file basis by adding one of
  633. the following lines anywhere in the buffer:
  634. #+STARTUP: inlineimages
  635. #+STARTUP: noinlineimages"
  636. :group 'org-startup
  637. :version "24.1"
  638. :type 'boolean)
  639. (defcustom org-startup-with-latex-preview nil
  640. "Non-nil means preview LaTeX fragments when loading a new Org file.
  641. This can also be configured on a per-file basis by adding one of
  642. the followinglines anywhere in the buffer:
  643. #+STARTUP: latexpreview
  644. #+STARTUP: nolatexpreview"
  645. :group 'org-startup
  646. :version "24.4"
  647. :package-version '(Org . "8.0")
  648. :type 'boolean)
  649. (defcustom org-insert-mode-line-in-empty-file nil
  650. "Non-nil means insert the first line setting Org-mode in empty files.
  651. When the function `org-mode' is called interactively in an empty file, this
  652. normally means that the file name does not automatically trigger Org-mode.
  653. To ensure that the file will always be in Org-mode in the future, a
  654. line enforcing Org-mode will be inserted into the buffer, if this option
  655. has been set."
  656. :group 'org-startup
  657. :type 'boolean)
  658. (defcustom org-replace-disputed-keys nil
  659. "Non-nil means use alternative key bindings for some keys.
  660. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  661. These keys are also used by other packages like shift-selection-mode'
  662. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  663. If you want to use Org-mode together with one of these other modes,
  664. or more generally if you would like to move some Org-mode commands to
  665. other keys, set this variable and configure the keys with the variable
  666. `org-disputed-keys'.
  667. This option is only relevant at load-time of Org-mode, and must be set
  668. *before* org.el is loaded. Changing it requires a restart of Emacs to
  669. become effective."
  670. :group 'org-startup
  671. :type 'boolean)
  672. (defcustom org-use-extra-keys nil
  673. "Non-nil means use extra key sequence definitions for certain commands.
  674. This happens automatically if you run XEmacs or if `window-system'
  675. is nil. This variable lets you do the same manually. You must
  676. set it before loading org.
  677. Example: on Carbon Emacs 22 running graphically, with an external
  678. keyboard on a Powerbook, the default way of setting M-left might
  679. not work for either Alt or ESC. Setting this variable will make
  680. it work for ESC."
  681. :group 'org-startup
  682. :type 'boolean)
  683. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  684. (defcustom org-disputed-keys
  685. '(([(shift up)] . [(meta p)])
  686. ([(shift down)] . [(meta n)])
  687. ([(shift left)] . [(meta -)])
  688. ([(shift right)] . [(meta +)])
  689. ([(control shift right)] . [(meta shift +)])
  690. ([(control shift left)] . [(meta shift -)]))
  691. "Keys for which Org-mode and other modes compete.
  692. This is an alist, cars are the default keys, second element specifies
  693. the alternative to use when `org-replace-disputed-keys' is t.
  694. Keys can be specified in any syntax supported by `define-key'.
  695. The value of this option takes effect only at Org-mode's startup,
  696. therefore you'll have to restart Emacs to apply it after changing."
  697. :group 'org-startup
  698. :type 'alist)
  699. (defun org-key (key)
  700. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  701. Or return the original if not disputed.
  702. Also apply the translations defined in `org-xemacs-key-equivalents'."
  703. (when org-replace-disputed-keys
  704. (let* ((nkey (key-description key))
  705. (x (org-find-if (lambda (x)
  706. (equal (key-description (car x)) nkey))
  707. org-disputed-keys)))
  708. (setq key (if x (cdr x) key))))
  709. (when (featurep 'xemacs)
  710. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  711. key)
  712. (defun org-find-if (predicate seq)
  713. (catch 'exit
  714. (while seq
  715. (if (funcall predicate (car seq))
  716. (throw 'exit (car seq))
  717. (pop seq)))))
  718. (defun org-defkey (keymap key def)
  719. "Define a key, possibly translated, as returned by `org-key'."
  720. (define-key keymap (org-key key) def))
  721. (defcustom org-ellipsis nil
  722. "The ellipsis to use in the Org-mode outline.
  723. When nil, just use the standard three dots. When a string, use that instead,
  724. When a face, use the standard 3 dots, but with the specified face.
  725. The change affects only Org-mode (which will then use its own display table).
  726. Changing this requires executing `M-x org-mode' in a buffer to become
  727. effective."
  728. :group 'org-startup
  729. :type '(choice (const :tag "Default" nil)
  730. (face :tag "Face" :value org-warning)
  731. (string :tag "String" :value "...#")))
  732. (defvar org-display-table nil
  733. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  734. (defgroup org-keywords nil
  735. "Keywords in Org-mode."
  736. :tag "Org Keywords"
  737. :group 'org)
  738. (defcustom org-deadline-string "DEADLINE:"
  739. "String to mark deadline entries.
  740. A deadline is this string, followed by a time stamp. Should be a word,
  741. terminated by a colon. You can insert a schedule keyword and
  742. a timestamp with \\[org-deadline].
  743. Changes become only effective after restarting Emacs."
  744. :group 'org-keywords
  745. :type 'string)
  746. (defcustom org-scheduled-string "SCHEDULED:"
  747. "String to mark scheduled TODO entries.
  748. A schedule is this string, followed by a time stamp. Should be a word,
  749. terminated by a colon. You can insert a schedule keyword and
  750. a timestamp with \\[org-schedule].
  751. Changes become only effective after restarting Emacs."
  752. :group 'org-keywords
  753. :type 'string)
  754. (defcustom org-closed-string "CLOSED:"
  755. "String used as the prefix for timestamps logging closing a TODO entry."
  756. :group 'org-keywords
  757. :type 'string)
  758. (defcustom org-clock-string "CLOCK:"
  759. "String used as prefix for timestamps clocking work hours on an item."
  760. :group 'org-keywords
  761. :type 'string)
  762. (defcustom org-closed-keep-when-no-todo nil
  763. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  764. :group 'org-todo
  765. :group 'org-keywords
  766. :version "24.4"
  767. :package-version '(Org . "8.0")
  768. :type 'boolean)
  769. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  770. org-scheduled-string "\\|"
  771. org-deadline-string "\\|"
  772. org-closed-string "\\|"
  773. org-clock-string "\\)")
  774. "Matches a line with planning or clock info.")
  775. (defcustom org-comment-string "COMMENT"
  776. "Entries starting with this keyword will never be exported.
  777. An entry can be toggled between COMMENT and normal with
  778. \\[org-toggle-comment].
  779. Changes become only effective after restarting Emacs."
  780. :group 'org-keywords
  781. :type 'string)
  782. (defcustom org-quote-string "QUOTE"
  783. "Entries starting with this keyword will be exported in fixed-width font.
  784. Quoting applies only to the text in the entry following the headline, and does
  785. not extend beyond the next headline, even if that is lower level.
  786. An entry can be toggled between QUOTE and normal with
  787. \\[org-toggle-fixed-width-section]."
  788. :group 'org-keywords
  789. :type 'string)
  790. (defconst org-repeat-re
  791. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  792. "Regular expression for specifying repeated events.
  793. After a match, group 1 contains the repeat expression.")
  794. (defgroup org-structure nil
  795. "Options concerning the general structure of Org-mode files."
  796. :tag "Org Structure"
  797. :group 'org)
  798. (defgroup org-reveal-location nil
  799. "Options about how to make context of a location visible."
  800. :tag "Org Reveal Location"
  801. :group 'org-structure)
  802. (defconst org-context-choice
  803. '(choice
  804. (const :tag "Always" t)
  805. (const :tag "Never" nil)
  806. (repeat :greedy t :tag "Individual contexts"
  807. (cons
  808. (choice :tag "Context"
  809. (const agenda)
  810. (const org-goto)
  811. (const occur-tree)
  812. (const tags-tree)
  813. (const link-search)
  814. (const mark-goto)
  815. (const bookmark-jump)
  816. (const isearch)
  817. (const default))
  818. (boolean))))
  819. "Contexts for the reveal options.")
  820. (defcustom org-show-hierarchy-above '((default . t))
  821. "Non-nil means show full hierarchy when revealing a location.
  822. Org-mode often shows locations in an org-mode file which might have
  823. been invisible before. When this is set, the hierarchy of headings
  824. above the exposed location is shown.
  825. Turning this off for example for sparse trees makes them very compact.
  826. Instead of t, this can also be an alist specifying this option for different
  827. contexts. Valid contexts are
  828. agenda when exposing an entry from the agenda
  829. org-goto when using the command `org-goto' on key C-c C-j
  830. occur-tree when using the command `org-occur' on key C-c /
  831. tags-tree when constructing a sparse tree based on tags matches
  832. link-search when exposing search matches associated with a link
  833. mark-goto when exposing the jump goal of a mark
  834. bookmark-jump when exposing a bookmark location
  835. isearch when exiting from an incremental search
  836. default default for all contexts not set explicitly"
  837. :group 'org-reveal-location
  838. :type org-context-choice)
  839. (defcustom org-show-following-heading '((default . nil))
  840. "Non-nil means show following heading when revealing a location.
  841. Org-mode often shows locations in an org-mode file which might have
  842. been invisible before. When this is set, the heading following the
  843. match is shown.
  844. Turning this off for example for sparse trees makes them very compact,
  845. but makes it harder to edit the location of the match. In such a case,
  846. use the command \\[org-reveal] to show more context.
  847. Instead of t, this can also be an alist specifying this option for different
  848. contexts. See `org-show-hierarchy-above' for valid contexts."
  849. :group 'org-reveal-location
  850. :type org-context-choice)
  851. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  852. "Non-nil means show all sibling heading when revealing a location.
  853. Org-mode often shows locations in an org-mode file which might have
  854. been invisible before. When this is set, the sibling of the current entry
  855. heading are all made visible. If `org-show-hierarchy-above' is t,
  856. the same happens on each level of the hierarchy above the current entry.
  857. By default this is on for the isearch context, off for all other contexts.
  858. Turning this off for example for sparse trees makes them very compact,
  859. but makes it harder to edit the location of the match. In such a case,
  860. use the command \\[org-reveal] to show more context.
  861. Instead of t, this can also be an alist specifying this option for different
  862. contexts. See `org-show-hierarchy-above' for valid contexts."
  863. :group 'org-reveal-location
  864. :type org-context-choice
  865. :version "24.4"
  866. :package-version '(Org . "8.0"))
  867. (defcustom org-show-entry-below '((default . nil))
  868. "Non-nil means show the entry below a headline when revealing a location.
  869. Org-mode often shows locations in an org-mode file which might have
  870. been invisible before. When this is set, the text below the headline that is
  871. exposed is also shown.
  872. By default this is off for all contexts.
  873. Instead of t, this can also be an alist specifying this option for different
  874. contexts. See `org-show-hierarchy-above' for valid contexts."
  875. :group 'org-reveal-location
  876. :type org-context-choice)
  877. (defcustom org-indirect-buffer-display 'other-window
  878. "How should indirect tree buffers be displayed?
  879. This applies to indirect buffers created with the commands
  880. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  881. Valid values are:
  882. current-window Display in the current window
  883. other-window Just display in another window.
  884. dedicated-frame Create one new frame, and re-use it each time.
  885. new-frame Make a new frame each time. Note that in this case
  886. previously-made indirect buffers are kept, and you need to
  887. kill these buffers yourself."
  888. :group 'org-structure
  889. :group 'org-agenda-windows
  890. :type '(choice
  891. (const :tag "In current window" current-window)
  892. (const :tag "In current frame, other window" other-window)
  893. (const :tag "Each time a new frame" new-frame)
  894. (const :tag "One dedicated frame" dedicated-frame)))
  895. (defcustom org-use-speed-commands nil
  896. "Non-nil means activate single letter commands at beginning of a headline.
  897. This may also be a function to test for appropriate locations where speed
  898. commands should be active."
  899. :group 'org-structure
  900. :type '(choice
  901. (const :tag "Never" nil)
  902. (const :tag "At beginning of headline stars" t)
  903. (function)))
  904. (defcustom org-speed-commands-user nil
  905. "Alist of additional speed commands.
  906. This list will be checked before `org-speed-commands-default'
  907. when the variable `org-use-speed-commands' is non-nil
  908. and when the cursor is at the beginning of a headline.
  909. The car if each entry is a string with a single letter, which must
  910. be assigned to `self-insert-command' in the global map.
  911. The cdr is either a command to be called interactively, a function
  912. to be called, or a form to be evaluated.
  913. An entry that is just a list with a single string will be interpreted
  914. as a descriptive headline that will be added when listing the speed
  915. commands in the Help buffer using the `?' speed command."
  916. :group 'org-structure
  917. :type '(repeat :value ("k" . ignore)
  918. (choice :value ("k" . ignore)
  919. (list :tag "Descriptive Headline" (string :tag "Headline"))
  920. (cons :tag "Letter and Command"
  921. (string :tag "Command letter")
  922. (choice
  923. (function)
  924. (sexp))))))
  925. (defgroup org-cycle nil
  926. "Options concerning visibility cycling in Org-mode."
  927. :tag "Org Cycle"
  928. :group 'org-structure)
  929. (defcustom org-cycle-skip-children-state-if-no-children t
  930. "Non-nil means skip CHILDREN state in entries that don't have any."
  931. :group 'org-cycle
  932. :type 'boolean)
  933. (defcustom org-cycle-max-level nil
  934. "Maximum level which should still be subject to visibility cycling.
  935. Levels higher than this will, for cycling, be treated as text, not a headline.
  936. When `org-odd-levels-only' is set, a value of N in this variable actually
  937. means 2N-1 stars as the limiting headline.
  938. When nil, cycle all levels.
  939. Note that the limiting level of cycling is also influenced by
  940. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  941. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  942. than its value."
  943. :group 'org-cycle
  944. :type '(choice
  945. (const :tag "No limit" nil)
  946. (integer :tag "Maximum level")))
  947. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  948. "Names of drawers. Drawers are not opened by cycling on the headline above.
  949. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  950. this:
  951. :DRAWERNAME:
  952. .....
  953. :END:
  954. The drawer \"PROPERTIES\" is special for capturing properties through
  955. the property API.
  956. Drawers can be defined on the per-file basis with a line like:
  957. #+DRAWERS: HIDDEN STATE PROPERTIES"
  958. :group 'org-structure
  959. :group 'org-cycle
  960. :type '(repeat (string :tag "Drawer Name")))
  961. (defcustom org-hide-block-startup nil
  962. "Non-nil means entering Org-mode will fold all blocks.
  963. This can also be set in on a per-file basis with
  964. #+STARTUP: hideblocks
  965. #+STARTUP: showblocks"
  966. :group 'org-startup
  967. :group 'org-cycle
  968. :type 'boolean)
  969. (defcustom org-cycle-global-at-bob nil
  970. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  971. This makes it possible to do global cycling without having to use S-TAB or
  972. \\[universal-argument] TAB. For this special case to work, the first line
  973. of the buffer must not be a headline -- it may be empty or some other text.
  974. When used in this way, `org-cycle-hook' is disabled temporarily to make
  975. sure the cursor stays at the beginning of the buffer. When this option is
  976. nil, don't do anything special at the beginning of the buffer."
  977. :group 'org-cycle
  978. :type 'boolean)
  979. (defcustom org-cycle-level-after-item/entry-creation t
  980. "Non-nil means cycle entry level or item indentation in new empty entries.
  981. When the cursor is at the end of an empty headline, i.e., with only stars
  982. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  983. and then all possible ancestor states, before returning to the original state.
  984. This makes data entry extremely fast: M-RET to create a new headline,
  985. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  986. When the cursor is at the end of an empty plain list item, one TAB will
  987. make it a subitem, two or more tabs will back up to make this an item
  988. higher up in the item hierarchy."
  989. :group 'org-cycle
  990. :type 'boolean)
  991. (defcustom org-cycle-emulate-tab t
  992. "Where should `org-cycle' emulate TAB.
  993. nil Never
  994. white Only in completely white lines
  995. whitestart Only at the beginning of lines, before the first non-white char
  996. t Everywhere except in headlines
  997. exc-hl-bol Everywhere except at the start of a headline
  998. If TAB is used in a place where it does not emulate TAB, the current subtree
  999. visibility is cycled."
  1000. :group 'org-cycle
  1001. :type '(choice (const :tag "Never" nil)
  1002. (const :tag "Only in completely white lines" white)
  1003. (const :tag "Before first char in a line" whitestart)
  1004. (const :tag "Everywhere except in headlines" t)
  1005. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1006. (defcustom org-cycle-separator-lines 2
  1007. "Number of empty lines needed to keep an empty line between collapsed trees.
  1008. If you leave an empty line between the end of a subtree and the following
  1009. headline, this empty line is hidden when the subtree is folded.
  1010. Org-mode will leave (exactly) one empty line visible if the number of
  1011. empty lines is equal or larger to the number given in this variable.
  1012. So the default 2 means at least 2 empty lines after the end of a subtree
  1013. are needed to produce free space between a collapsed subtree and the
  1014. following headline.
  1015. If the number is negative, and the number of empty lines is at least -N,
  1016. all empty lines are shown.
  1017. Special case: when 0, never leave empty lines in collapsed view."
  1018. :group 'org-cycle
  1019. :type 'integer)
  1020. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1021. (defcustom org-pre-cycle-hook nil
  1022. "Hook that is run before visibility cycling is happening.
  1023. The function(s) in this hook must accept a single argument which indicates
  1024. the new state that will be set right after running this hook. The
  1025. argument is a symbol. Before a global state change, it can have the values
  1026. `overview', `content', or `all'. Before a local state change, it can have
  1027. the values `folded', `children', or `subtree'."
  1028. :group 'org-cycle
  1029. :type 'hook)
  1030. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1031. org-cycle-hide-drawers
  1032. org-cycle-hide-inline-tasks
  1033. org-cycle-show-empty-lines
  1034. org-optimize-window-after-visibility-change)
  1035. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1036. The function(s) in this hook must accept a single argument which indicates
  1037. the new state that was set by the most recent `org-cycle' command. The
  1038. argument is a symbol. After a global state change, it can have the values
  1039. `overview', `contents', or `all'. After a local state change, it can have
  1040. the values `folded', `children', or `subtree'."
  1041. :group 'org-cycle
  1042. :type 'hook)
  1043. (defgroup org-edit-structure nil
  1044. "Options concerning structure editing in Org-mode."
  1045. :tag "Org Edit Structure"
  1046. :group 'org-structure)
  1047. (defcustom org-odd-levels-only nil
  1048. "Non-nil means skip even levels and only use odd levels for the outline.
  1049. This has the effect that two stars are being added/taken away in
  1050. promotion/demotion commands. It also influences how levels are
  1051. handled by the exporters.
  1052. Changing it requires restart of `font-lock-mode' to become effective
  1053. for fontification also in regions already fontified.
  1054. You may also set this on a per-file basis by adding one of the following
  1055. lines to the buffer:
  1056. #+STARTUP: odd
  1057. #+STARTUP: oddeven"
  1058. :group 'org-edit-structure
  1059. :group 'org-appearance
  1060. :type 'boolean)
  1061. (defcustom org-adapt-indentation t
  1062. "Non-nil means adapt indentation to outline node level.
  1063. When this variable is set, Org assumes that you write outlines by
  1064. indenting text in each node to align with the headline (after the stars).
  1065. The following issues are influenced by this variable:
  1066. - When this is set and the *entire* text in an entry is indented, the
  1067. indentation is increased by one space in a demotion command, and
  1068. decreased by one in a promotion command. If any line in the entry
  1069. body starts with text at column 0, indentation is not changed at all.
  1070. - Property drawers and planning information is inserted indented when
  1071. this variable s set. When nil, they will not be indented.
  1072. - TAB indents a line relative to context. The lines below a headline
  1073. will be indented when this variable is set.
  1074. Note that this is all about true indentation, by adding and removing
  1075. space characters. See also `org-indent.el' which does level-dependent
  1076. indentation in a virtual way, i.e. at display time in Emacs."
  1077. :group 'org-edit-structure
  1078. :type 'boolean)
  1079. (defcustom org-special-ctrl-a/e nil
  1080. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1081. When t, `C-a' will bring back the cursor to the beginning of the
  1082. headline text, i.e. after the stars and after a possible TODO
  1083. keyword. In an item, this will be the position after bullet and
  1084. check-box, if any. When the cursor is already at that position,
  1085. another `C-a' will bring it to the beginning of the line.
  1086. `C-e' will jump to the end of the headline, ignoring the presence
  1087. of tags in the headline. A second `C-e' will then jump to the
  1088. true end of the line, after any tags. This also means that, when
  1089. this variable is non-nil, `C-e' also will never jump beyond the
  1090. end of the heading of a folded section, i.e. not after the
  1091. ellipses.
  1092. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1093. normally, going to the true line boundary first. Only a directly
  1094. following, identical keypress will bring the cursor to the
  1095. special positions.
  1096. This may also be a cons cell where the behavior for `C-a' and
  1097. `C-e' is set separately."
  1098. :group 'org-edit-structure
  1099. :type '(choice
  1100. (const :tag "off" nil)
  1101. (const :tag "on: after stars/bullet and before tags first" t)
  1102. (const :tag "reversed: true line boundary first" reversed)
  1103. (cons :tag "Set C-a and C-e separately"
  1104. (choice :tag "Special C-a"
  1105. (const :tag "off" nil)
  1106. (const :tag "on: after stars/bullet first" t)
  1107. (const :tag "reversed: before stars/bullet first" reversed))
  1108. (choice :tag "Special C-e"
  1109. (const :tag "off" nil)
  1110. (const :tag "on: before tags first" t)
  1111. (const :tag "reversed: after tags first" reversed)))))
  1112. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1113. (defcustom org-special-ctrl-k nil
  1114. "Non-nil means `C-k' will behave specially in headlines.
  1115. When nil, `C-k' will call the default `kill-line' command.
  1116. When t, the following will happen while the cursor is in the headline:
  1117. - When the cursor is at the beginning of a headline, kill the entire
  1118. line and possible the folded subtree below the line.
  1119. - When in the middle of the headline text, kill the headline up to the tags.
  1120. - When after the headline text, kill the tags."
  1121. :group 'org-edit-structure
  1122. :type 'boolean)
  1123. (defcustom org-ctrl-k-protect-subtree nil
  1124. "Non-nil means, do not delete a hidden subtree with C-k.
  1125. When set to the symbol `error', simply throw an error when C-k is
  1126. used to kill (part-of) a headline that has hidden text behind it.
  1127. Any other non-nil value will result in a query to the user, if it is
  1128. OK to kill that hidden subtree. When nil, kill without remorse."
  1129. :group 'org-edit-structure
  1130. :version "24.1"
  1131. :type '(choice
  1132. (const :tag "Do not protect hidden subtrees" nil)
  1133. (const :tag "Protect hidden subtrees with a security query" t)
  1134. (const :tag "Never kill a hidden subtree with C-k" error)))
  1135. (defcustom org-catch-invisible-edits nil
  1136. "Check if in invisible region before inserting or deleting a character.
  1137. Valid values are:
  1138. nil Do not check, so just do invisible edits.
  1139. error Throw an error and do nothing.
  1140. show Make point visible, and do the requested edit.
  1141. show-and-error Make point visible, then throw an error and abort the edit.
  1142. smart Make point visible, and do insertion/deletion if it is
  1143. adjacent to visible text and the change feels predictable.
  1144. Never delete a previously invisible character or add in the
  1145. middle or right after an invisible region. Basically, this
  1146. allows insertion and backward-delete right before ellipses.
  1147. FIXME: maybe in this case we should not even show?"
  1148. :group 'org-edit-structure
  1149. :version "24.1"
  1150. :type '(choice
  1151. (const :tag "Do not check" nil)
  1152. (const :tag "Throw error when trying to edit" error)
  1153. (const :tag "Unhide, but do not do the edit" show-and-error)
  1154. (const :tag "Show invisible part and do the edit" show)
  1155. (const :tag "Be smart and do the right thing" smart)))
  1156. (defcustom org-yank-folded-subtrees t
  1157. "Non-nil means when yanking subtrees, fold them.
  1158. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1159. it starts with a heading and all other headings in it are either children
  1160. or siblings, then fold all the subtrees. However, do this only if no
  1161. text after the yank would be swallowed into a folded tree by this action."
  1162. :group 'org-edit-structure
  1163. :type 'boolean)
  1164. (defcustom org-yank-adjusted-subtrees nil
  1165. "Non-nil means when yanking subtrees, adjust the level.
  1166. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1167. this function for details."
  1168. :group 'org-edit-structure
  1169. :type 'boolean)
  1170. (defcustom org-M-RET-may-split-line '((default . t))
  1171. "Non-nil means M-RET will split the line at the cursor position.
  1172. When nil, it will go to the end of the line before making a
  1173. new line.
  1174. You may also set this option in a different way for different
  1175. contexts. Valid contexts are:
  1176. headline when creating a new headline
  1177. item when creating a new item
  1178. table in a table field
  1179. default the value to be used for all contexts not explicitly
  1180. customized"
  1181. :group 'org-structure
  1182. :group 'org-table
  1183. :type '(choice
  1184. (const :tag "Always" t)
  1185. (const :tag "Never" nil)
  1186. (repeat :greedy t :tag "Individual contexts"
  1187. (cons
  1188. (choice :tag "Context"
  1189. (const headline)
  1190. (const item)
  1191. (const table)
  1192. (const default))
  1193. (boolean)))))
  1194. (defcustom org-insert-heading-respect-content nil
  1195. "Non-nil means insert new headings after the current subtree.
  1196. When nil, the new heading is created directly after the current line.
  1197. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1198. this variable on for the duration of the command."
  1199. :group 'org-structure
  1200. :type 'boolean)
  1201. (defcustom org-blank-before-new-entry '((heading . auto)
  1202. (plain-list-item . auto))
  1203. "Should `org-insert-heading' leave a blank line before new heading/item?
  1204. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1205. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1206. which case Org will look at the surrounding headings/items and try to
  1207. make an intelligent decision whether to insert a blank line or not.
  1208. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1209. the setting here is ignored and no empty line is inserted to avoid breaking
  1210. the list structure."
  1211. :group 'org-edit-structure
  1212. :type '(list
  1213. (cons (const heading)
  1214. (choice (const :tag "Never" nil)
  1215. (const :tag "Always" t)
  1216. (const :tag "Auto" auto)))
  1217. (cons (const plain-list-item)
  1218. (choice (const :tag "Never" nil)
  1219. (const :tag "Always" t)
  1220. (const :tag "Auto" auto)))))
  1221. (defcustom org-insert-heading-hook nil
  1222. "Hook being run after inserting a new heading."
  1223. :group 'org-edit-structure
  1224. :type 'hook)
  1225. (defcustom org-enable-fixed-width-editor t
  1226. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1227. This currently only means they are never auto-wrapped.
  1228. When nil, such lines will be treated like ordinary lines.
  1229. See also the QUOTE keyword."
  1230. :group 'org-edit-structure
  1231. :type 'boolean)
  1232. (defcustom org-goto-auto-isearch t
  1233. "Non-nil means typing characters in `org-goto' starts incremental search.
  1234. When nil, you can use these keybindings to navigate the buffer:
  1235. q Quit the org-goto interface
  1236. n Go to the next visible heading
  1237. p Go to the previous visible heading
  1238. f Go one heading forward on same level
  1239. b Go one heading backward on same level
  1240. u Go one heading up"
  1241. :group 'org-edit-structure
  1242. :type 'boolean)
  1243. (defgroup org-sparse-trees nil
  1244. "Options concerning sparse trees in Org-mode."
  1245. :tag "Org Sparse Trees"
  1246. :group 'org-structure)
  1247. (defcustom org-highlight-sparse-tree-matches t
  1248. "Non-nil means highlight all matches that define a sparse tree.
  1249. The highlights will automatically disappear the next time the buffer is
  1250. changed by an edit command."
  1251. :group 'org-sparse-trees
  1252. :type 'boolean)
  1253. (defcustom org-remove-highlights-with-change t
  1254. "Non-nil means any change to the buffer will remove temporary highlights.
  1255. Such highlights are created by `org-occur' and `org-clock-display'.
  1256. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1257. The highlights created by `org-preview-latex-fragment' always need
  1258. `C-c C-c' to be removed."
  1259. :group 'org-sparse-trees
  1260. :group 'org-time
  1261. :type 'boolean)
  1262. (defcustom org-occur-hook '(org-first-headline-recenter)
  1263. "Hook that is run after `org-occur' has constructed a sparse tree.
  1264. This can be used to recenter the window to show as much of the structure
  1265. as possible."
  1266. :group 'org-sparse-trees
  1267. :type 'hook)
  1268. (defgroup org-imenu-and-speedbar nil
  1269. "Options concerning imenu and speedbar in Org-mode."
  1270. :tag "Org Imenu and Speedbar"
  1271. :group 'org-structure)
  1272. (defcustom org-imenu-depth 2
  1273. "The maximum level for Imenu access to Org-mode headlines.
  1274. This also applied for speedbar access."
  1275. :group 'org-imenu-and-speedbar
  1276. :type 'integer)
  1277. (defgroup org-table nil
  1278. "Options concerning tables in Org-mode."
  1279. :tag "Org Table"
  1280. :group 'org)
  1281. (defcustom org-enable-table-editor 'optimized
  1282. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1283. When nil, such lines will be treated like ordinary lines.
  1284. When equal to the symbol `optimized', the table editor will be optimized to
  1285. do the following:
  1286. - Automatic overwrite mode in front of whitespace in table fields.
  1287. This makes the structure of the table stay in tact as long as the edited
  1288. field does not exceed the column width.
  1289. - Minimize the number of realigns. Normally, the table is aligned each time
  1290. TAB or RET are pressed to move to another field. With optimization this
  1291. happens only if changes to a field might have changed the column width.
  1292. Optimization requires replacing the functions `self-insert-command',
  1293. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1294. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1295. very good at guessing when a re-align will be necessary, but you can always
  1296. force one with \\[org-ctrl-c-ctrl-c].
  1297. If you would like to use the optimized version in Org-mode, but the
  1298. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1299. This variable can be used to turn on and off the table editor during a session,
  1300. but in order to toggle optimization, a restart is required.
  1301. See also the variable `org-table-auto-blank-field'."
  1302. :group 'org-table
  1303. :type '(choice
  1304. (const :tag "off" nil)
  1305. (const :tag "on" t)
  1306. (const :tag "on, optimized" optimized)))
  1307. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1308. (version<= emacs-version "24.1"))
  1309. "Non-nil means cluster self-insert commands for undo when possible.
  1310. If this is set, then, like in the Emacs command loop, 20 consecutive
  1311. characters will be undone together.
  1312. This is configurable, because there is some impact on typing performance."
  1313. :group 'org-table
  1314. :type 'boolean)
  1315. (defcustom org-table-tab-recognizes-table.el t
  1316. "Non-nil means TAB will automatically notice a table.el table.
  1317. When it sees such a table, it moves point into it and - if necessary -
  1318. calls `table-recognize-table'."
  1319. :group 'org-table-editing
  1320. :type 'boolean)
  1321. (defgroup org-link nil
  1322. "Options concerning links in Org-mode."
  1323. :tag "Org Link"
  1324. :group 'org)
  1325. (defvar org-link-abbrev-alist-local nil
  1326. "Buffer-local version of `org-link-abbrev-alist', which see.
  1327. The value of this is taken from the #+LINK lines.")
  1328. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1329. (defcustom org-link-abbrev-alist nil
  1330. "Alist of link abbreviations.
  1331. The car of each element is a string, to be replaced at the start of a link.
  1332. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1333. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1334. [[linkkey:tag][description]]
  1335. The 'linkkey' must be a word word, starting with a letter, followed
  1336. by letters, numbers, '-' or '_'.
  1337. If REPLACE is a string, the tag will simply be appended to create the link.
  1338. If the string contains \"%s\", the tag will be inserted there. If the string
  1339. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1340. at that point (see the function `url-hexify-string'). If the string contains
  1341. the specifier \"%(my-function)\", then the custom function `my-function' will
  1342. be invoked: this function takes the tag as its only argument and must return
  1343. a string.
  1344. REPLACE may also be a function that will be called with the tag as the
  1345. only argument to create the link, which should be returned as a string.
  1346. See the manual for examples."
  1347. :group 'org-link
  1348. :type '(repeat
  1349. (cons
  1350. (string :tag "Protocol")
  1351. (choice
  1352. (string :tag "Format")
  1353. (function)))))
  1354. (defcustom org-descriptive-links t
  1355. "Non-nil means Org will display descriptive links.
  1356. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1357. \"Org Website\", hiding the link itself and just displaying its
  1358. description. When set to `nil', Org will display the full links
  1359. literally.
  1360. You can interactively set the value of this variable by calling
  1361. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1362. :group 'org-link
  1363. :type 'boolean)
  1364. (defcustom org-link-file-path-type 'adaptive
  1365. "How the path name in file links should be stored.
  1366. Valid values are:
  1367. relative Relative to the current directory, i.e. the directory of the file
  1368. into which the link is being inserted.
  1369. absolute Absolute path, if possible with ~ for home directory.
  1370. noabbrev Absolute path, no abbreviation of home directory.
  1371. adaptive Use relative path for files in the current directory and sub-
  1372. directories of it. For other files, use an absolute path."
  1373. :group 'org-link
  1374. :type '(choice
  1375. (const relative)
  1376. (const absolute)
  1377. (const noabbrev)
  1378. (const adaptive)))
  1379. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1380. "Types of links that should be activated in Org-mode files.
  1381. This is a list of symbols, each leading to the activation of a certain link
  1382. type. In principle, it does not hurt to turn on most link types - there may
  1383. be a small gain when turning off unused link types. The types are:
  1384. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1385. angle Links in angular brackets that may contain whitespace like
  1386. <bbdb:Carsten Dominik>.
  1387. plain Plain links in normal text, no whitespace, like http://google.com.
  1388. radio Text that is matched by a radio target, see manual for details.
  1389. tag Tag settings in a headline (link to tag search).
  1390. date Time stamps (link to calendar).
  1391. footnote Footnote labels.
  1392. Changing this variable requires a restart of Emacs to become effective."
  1393. :group 'org-link
  1394. :type '(set :greedy t
  1395. (const :tag "Double bracket links" bracket)
  1396. (const :tag "Angular bracket links" angle)
  1397. (const :tag "Plain text links" plain)
  1398. (const :tag "Radio target matches" radio)
  1399. (const :tag "Tags" tag)
  1400. (const :tag "Timestamps" date)
  1401. (const :tag "Footnotes" footnote)))
  1402. (defcustom org-make-link-description-function nil
  1403. "Function to use for generating link descriptions from links.
  1404. When nil, the link location will be used. This function must take
  1405. two parameters: the first one is the link, the second one is the
  1406. description generated by `org-insert-link'. The function should
  1407. return the description to use."
  1408. :group 'org-link
  1409. :type '(choice (const nil) (function)))
  1410. (defgroup org-link-store nil
  1411. "Options concerning storing links in Org-mode."
  1412. :tag "Org Store Link"
  1413. :group 'org-link)
  1414. (defcustom org-url-hexify-p t
  1415. "When non-nil, hexify URL when creating a link."
  1416. :type 'boolean
  1417. :version "24.3"
  1418. :group 'org-link-store)
  1419. (defcustom org-email-link-description-format "Email %c: %.30s"
  1420. "Format of the description part of a link to an email or usenet message.
  1421. The following %-escapes will be replaced by corresponding information:
  1422. %F full \"From\" field
  1423. %f name, taken from \"From\" field, address if no name
  1424. %T full \"To\" field
  1425. %t first name in \"To\" field, address if no name
  1426. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1427. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1428. %s subject
  1429. %d date
  1430. %m message-id.
  1431. You may use normal field width specification between the % and the letter.
  1432. This is for example useful to limit the length of the subject.
  1433. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1434. :group 'org-link-store
  1435. :type 'string)
  1436. (defcustom org-from-is-user-regexp
  1437. (let (r1 r2)
  1438. (when (and user-mail-address (not (string= user-mail-address "")))
  1439. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1440. (when (and user-full-name (not (string= user-full-name "")))
  1441. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1442. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1443. "Regexp matched against the \"From:\" header of an email or usenet message.
  1444. It should match if the message is from the user him/herself."
  1445. :group 'org-link-store
  1446. :type 'regexp)
  1447. (defcustom org-context-in-file-links t
  1448. "Non-nil means file links from `org-store-link' contain context.
  1449. A search string will be added to the file name with :: as separator and
  1450. used to find the context when the link is activated by the command
  1451. `org-open-at-point'. When this option is t, the entire active region
  1452. will be placed in the search string of the file link. If set to a
  1453. positive integer, only the first n lines of context will be stored.
  1454. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1455. negates this setting for the duration of the command."
  1456. :group 'org-link-store
  1457. :type '(choice boolean integer))
  1458. (defcustom org-keep-stored-link-after-insertion nil
  1459. "Non-nil means keep link in list for entire session.
  1460. The command `org-store-link' adds a link pointing to the current
  1461. location to an internal list. These links accumulate during a session.
  1462. The command `org-insert-link' can be used to insert links into any
  1463. Org-mode file (offering completion for all stored links). When this
  1464. option is nil, every link which has been inserted once using \\[org-insert-link]
  1465. will be removed from the list, to make completing the unused links
  1466. more efficient."
  1467. :group 'org-link-store
  1468. :type 'boolean)
  1469. (defgroup org-link-follow nil
  1470. "Options concerning following links in Org-mode."
  1471. :tag "Org Follow Link"
  1472. :group 'org-link)
  1473. (defcustom org-link-translation-function nil
  1474. "Function to translate links with different syntax to Org syntax.
  1475. This can be used to translate links created for example by the Planner
  1476. or emacs-wiki packages to Org syntax.
  1477. The function must accept two parameters, a TYPE containing the link
  1478. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1479. which is everything after the link protocol. It should return a cons
  1480. with possibly modified values of type and path.
  1481. Org contains a function for this, so if you set this variable to
  1482. `org-translate-link-from-planner', you should be able follow many
  1483. links created by planner."
  1484. :group 'org-link-follow
  1485. :type '(choice (const nil) (function)))
  1486. (defcustom org-follow-link-hook nil
  1487. "Hook that is run after a link has been followed."
  1488. :group 'org-link-follow
  1489. :type 'hook)
  1490. (defcustom org-tab-follows-link nil
  1491. "Non-nil means on links TAB will follow the link.
  1492. Needs to be set before org.el is loaded.
  1493. This really should not be used, it does not make sense, and the
  1494. implementation is bad."
  1495. :group 'org-link-follow
  1496. :type 'boolean)
  1497. (defcustom org-return-follows-link nil
  1498. "Non-nil means on links RET will follow the link.
  1499. In tables, the special behavior of RET has precedence."
  1500. :group 'org-link-follow
  1501. :type 'boolean)
  1502. (defcustom org-mouse-1-follows-link
  1503. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1504. "Non-nil means mouse-1 on a link will follow the link.
  1505. A longer mouse click will still set point. Does not work on XEmacs.
  1506. Needs to be set before org.el is loaded."
  1507. :group 'org-link-follow
  1508. :type 'boolean)
  1509. (defcustom org-mark-ring-length 4
  1510. "Number of different positions to be recorded in the ring.
  1511. Changing this requires a restart of Emacs to work correctly."
  1512. :group 'org-link-follow
  1513. :type 'integer)
  1514. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1515. "Non-nil means internal links in Org files must exactly match a headline.
  1516. When nil, the link search tries to match a phrase with all words
  1517. in the search text."
  1518. :group 'org-link-follow
  1519. :version "24.1"
  1520. :type '(choice
  1521. (const :tag "Use fuzzy text search" nil)
  1522. (const :tag "Match only exact headline" t)
  1523. (const :tag "Match exact headline or query to create it"
  1524. query-to-create)))
  1525. (defcustom org-link-frame-setup
  1526. '((vm . vm-visit-folder-other-frame)
  1527. (vm-imap . vm-visit-imap-folder-other-frame)
  1528. (gnus . org-gnus-no-new-news)
  1529. (file . find-file-other-window)
  1530. (wl . wl-other-frame))
  1531. "Setup the frame configuration for following links.
  1532. When following a link with Emacs, it may often be useful to display
  1533. this link in another window or frame. This variable can be used to
  1534. set this up for the different types of links.
  1535. For VM, use any of
  1536. `vm-visit-folder'
  1537. `vm-visit-folder-other-window'
  1538. `vm-visit-folder-other-frame'
  1539. For Gnus, use any of
  1540. `gnus'
  1541. `gnus-other-frame'
  1542. `org-gnus-no-new-news'
  1543. For FILE, use any of
  1544. `find-file'
  1545. `find-file-other-window'
  1546. `find-file-other-frame'
  1547. For Wanderlust use any of
  1548. `wl'
  1549. `wl-other-frame'
  1550. For the calendar, use the variable `calendar-setup'.
  1551. For BBDB, it is currently only possible to display the matches in
  1552. another window."
  1553. :group 'org-link-follow
  1554. :type '(list
  1555. (cons (const vm)
  1556. (choice
  1557. (const vm-visit-folder)
  1558. (const vm-visit-folder-other-window)
  1559. (const vm-visit-folder-other-frame)))
  1560. (cons (const vm-imap)
  1561. (choice
  1562. (const vm-visit-imap-folder)
  1563. (const vm-visit-imap-folder-other-window)
  1564. (const vm-visit-imap-folder-other-frame)))
  1565. (cons (const gnus)
  1566. (choice
  1567. (const gnus)
  1568. (const gnus-other-frame)
  1569. (const org-gnus-no-new-news)))
  1570. (cons (const file)
  1571. (choice
  1572. (const find-file)
  1573. (const find-file-other-window)
  1574. (const find-file-other-frame)))
  1575. (cons (const wl)
  1576. (choice
  1577. (const wl)
  1578. (const wl-other-frame)))))
  1579. (defcustom org-display-internal-link-with-indirect-buffer nil
  1580. "Non-nil means use indirect buffer to display infile links.
  1581. Activating internal links (from one location in a file to another location
  1582. in the same file) normally just jumps to the location. When the link is
  1583. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1584. is displayed in
  1585. another window. When this option is set, the other window actually displays
  1586. an indirect buffer clone of the current buffer, to avoid any visibility
  1587. changes to the current buffer."
  1588. :group 'org-link-follow
  1589. :type 'boolean)
  1590. (defcustom org-open-non-existing-files nil
  1591. "Non-nil means `org-open-file' will open non-existing files.
  1592. When nil, an error will be generated.
  1593. This variable applies only to external applications because they
  1594. might choke on non-existing files. If the link is to a file that
  1595. will be opened in Emacs, the variable is ignored."
  1596. :group 'org-link-follow
  1597. :type 'boolean)
  1598. (defcustom org-open-directory-means-index-dot-org nil
  1599. "Non-nil means a link to a directory really means to index.org.
  1600. When nil, following a directory link will run dired or open a finder/explorer
  1601. window on that directory."
  1602. :group 'org-link-follow
  1603. :type 'boolean)
  1604. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1605. "Function and arguments to call for following mailto links.
  1606. This is a list with the first element being a Lisp function, and the
  1607. remaining elements being arguments to the function. In string arguments,
  1608. %a will be replaced by the address, and %s will be replaced by the subject
  1609. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1610. :group 'org-link-follow
  1611. :type '(choice
  1612. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1613. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1614. (const :tag "message-mail" (message-mail "%a" "%s"))
  1615. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1616. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1617. "Non-nil means ask for confirmation before executing shell links.
  1618. Shell links can be dangerous: just think about a link
  1619. [[shell:rm -rf ~/*][Google Search]]
  1620. This link would show up in your Org-mode document as \"Google Search\",
  1621. but really it would remove your entire home directory.
  1622. Therefore we advise against setting this variable to nil.
  1623. Just change it to `y-or-n-p' if you want to confirm with a
  1624. single keystroke rather than having to type \"yes\"."
  1625. :group 'org-link-follow
  1626. :type '(choice
  1627. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1628. (const :tag "with y-or-n (faster)" y-or-n-p)
  1629. (const :tag "no confirmation (dangerous)" nil)))
  1630. (put 'org-confirm-shell-link-function
  1631. 'safe-local-variable
  1632. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1633. (defcustom org-confirm-shell-link-not-regexp ""
  1634. "A regexp to skip confirmation for shell links."
  1635. :group 'org-link-follow
  1636. :version "24.1"
  1637. :type 'regexp)
  1638. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1639. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1640. Elisp links can be dangerous: just think about a link
  1641. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1642. This link would show up in your Org-mode document as \"Google Search\",
  1643. but really it would remove your entire home directory.
  1644. Therefore we advise against setting this variable to nil.
  1645. Just change it to `y-or-n-p' if you want to confirm with a
  1646. single keystroke rather than having to type \"yes\"."
  1647. :group 'org-link-follow
  1648. :type '(choice
  1649. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1650. (const :tag "with y-or-n (faster)" y-or-n-p)
  1651. (const :tag "no confirmation (dangerous)" nil)))
  1652. (put 'org-confirm-shell-link-function
  1653. 'safe-local-variable
  1654. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1655. (defcustom org-confirm-elisp-link-not-regexp ""
  1656. "A regexp to skip confirmation for Elisp links."
  1657. :group 'org-link-follow
  1658. :version "24.1"
  1659. :type 'regexp)
  1660. (defconst org-file-apps-defaults-gnu
  1661. '((remote . emacs)
  1662. (system . mailcap)
  1663. (t . mailcap))
  1664. "Default file applications on a UNIX or GNU/Linux system.
  1665. See `org-file-apps'.")
  1666. (defconst org-file-apps-defaults-macosx
  1667. '((remote . emacs)
  1668. (t . "open %s")
  1669. (system . "open %s")
  1670. ("ps.gz" . "gv %s")
  1671. ("eps.gz" . "gv %s")
  1672. ("dvi" . "xdvi %s")
  1673. ("fig" . "xfig %s"))
  1674. "Default file applications on a MacOS X system.
  1675. The system \"open\" is known as a default, but we use X11 applications
  1676. for some files for which the OS does not have a good default.
  1677. See `org-file-apps'.")
  1678. (defconst org-file-apps-defaults-windowsnt
  1679. (list
  1680. '(remote . emacs)
  1681. (cons t
  1682. (list (if (featurep 'xemacs)
  1683. 'mswindows-shell-execute
  1684. 'w32-shell-execute)
  1685. "open" 'file))
  1686. (cons 'system
  1687. (list (if (featurep 'xemacs)
  1688. 'mswindows-shell-execute
  1689. 'w32-shell-execute)
  1690. "open" 'file)))
  1691. "Default file applications on a Windows NT system.
  1692. The system \"open\" is used for most files.
  1693. See `org-file-apps'.")
  1694. (defcustom org-file-apps
  1695. '((auto-mode . emacs)
  1696. ("\\.mm\\'" . default)
  1697. ("\\.x?html?\\'" . default)
  1698. ("\\.pdf\\'" . default))
  1699. "External applications for opening `file:path' items in a document.
  1700. Org-mode uses system defaults for different file types, but
  1701. you can use this variable to set the application for a given file
  1702. extension. The entries in this list are cons cells where the car identifies
  1703. files and the cdr the corresponding command. Possible values for the
  1704. file identifier are
  1705. \"string\" A string as a file identifier can be interpreted in different
  1706. ways, depending on its contents:
  1707. - Alphanumeric characters only:
  1708. Match links with this file extension.
  1709. Example: (\"pdf\" . \"evince %s\")
  1710. to open PDFs with evince.
  1711. - Regular expression: Match links where the
  1712. filename matches the regexp. If you want to
  1713. use groups here, use shy groups.
  1714. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1715. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1716. to open *.html and *.xhtml with firefox.
  1717. - Regular expression which contains (non-shy) groups:
  1718. Match links where the whole link, including \"::\", and
  1719. anything after that, matches the regexp.
  1720. In a custom command string, %1, %2, etc. are replaced with
  1721. the parts of the link that were matched by the groups.
  1722. For backwards compatibility, if a command string is given
  1723. that does not use any of the group matches, this case is
  1724. handled identically to the second one (i.e. match against
  1725. file name only).
  1726. In a custom lisp form, you can access the group matches with
  1727. (match-string n link).
  1728. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1729. to open [[file:document.pdf::5]] with evince at page 5.
  1730. `directory' Matches a directory
  1731. `remote' Matches a remote file, accessible through tramp or efs.
  1732. Remote files most likely should be visited through Emacs
  1733. because external applications cannot handle such paths.
  1734. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1735. so all files Emacs knows how to handle. Using this with
  1736. command `emacs' will open most files in Emacs. Beware that this
  1737. will also open html files inside Emacs, unless you add
  1738. (\"html\" . default) to the list as well.
  1739. t Default for files not matched by any of the other options.
  1740. `system' The system command to open files, like `open' on Windows
  1741. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1742. that will be selected if you call `C-c C-o' with a double
  1743. \\[universal-argument] \\[universal-argument] prefix.
  1744. Possible values for the command are:
  1745. `emacs' The file will be visited by the current Emacs process.
  1746. `default' Use the default application for this file type, which is the
  1747. association for t in the list, most likely in the system-specific
  1748. part.
  1749. This can be used to overrule an unwanted setting in the
  1750. system-specific variable.
  1751. `system' Use the system command for opening files, like \"open\".
  1752. This command is specified by the entry whose car is `system'.
  1753. Most likely, the system-specific version of this variable
  1754. does define this command, but you can overrule/replace it
  1755. here.
  1756. string A command to be executed by a shell; %s will be replaced
  1757. by the path to the file.
  1758. sexp A Lisp form which will be evaluated. The file path will
  1759. be available in the Lisp variable `file'.
  1760. For more examples, see the system specific constants
  1761. `org-file-apps-defaults-macosx'
  1762. `org-file-apps-defaults-windowsnt'
  1763. `org-file-apps-defaults-gnu'."
  1764. :group 'org-link-follow
  1765. :type '(repeat
  1766. (cons (choice :value ""
  1767. (string :tag "Extension")
  1768. (const :tag "System command to open files" system)
  1769. (const :tag "Default for unrecognized files" t)
  1770. (const :tag "Remote file" remote)
  1771. (const :tag "Links to a directory" directory)
  1772. (const :tag "Any files that have Emacs modes"
  1773. auto-mode))
  1774. (choice :value ""
  1775. (const :tag "Visit with Emacs" emacs)
  1776. (const :tag "Use default" default)
  1777. (const :tag "Use the system command" system)
  1778. (string :tag "Command")
  1779. (sexp :tag "Lisp form")))))
  1780. (defcustom org-doi-server-url "http://dx.doi.org/"
  1781. "The URL of the DOI server."
  1782. :type 'string
  1783. :version "24.3"
  1784. :group 'org-link-follow)
  1785. (defgroup org-refile nil
  1786. "Options concerning refiling entries in Org-mode."
  1787. :tag "Org Refile"
  1788. :group 'org)
  1789. (defcustom org-directory "~/org"
  1790. "Directory with org files.
  1791. This is just a default location to look for Org files. There is no need
  1792. at all to put your files into this directory. It is only used in the
  1793. following situations:
  1794. 1. When a capture template specifies a target file that is not an
  1795. absolute path. The path will then be interpreted relative to
  1796. `org-directory'
  1797. 2. When a capture note is filed away in an interactive way (when exiting the
  1798. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1799. with `org-directory' as the default path."
  1800. :group 'org-refile
  1801. :group 'org-remember
  1802. :group 'org-capture
  1803. :type 'directory)
  1804. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1805. "Default target for storing notes.
  1806. Used as a fall back file for org-remember.el and org-capture.el, for
  1807. templates that do not specify a target file."
  1808. :group 'org-refile
  1809. :group 'org-remember
  1810. :group 'org-capture
  1811. :type '(choice
  1812. (const :tag "Default from remember-data-file" nil)
  1813. file))
  1814. (defcustom org-goto-interface 'outline
  1815. "The default interface to be used for `org-goto'.
  1816. Allowed values are:
  1817. outline The interface shows an outline of the relevant file
  1818. and the correct heading is found by moving through
  1819. the outline or by searching with incremental search.
  1820. outline-path-completion Headlines in the current buffer are offered via
  1821. completion. This is the interface also used by
  1822. the refile command."
  1823. :group 'org-refile
  1824. :type '(choice
  1825. (const :tag "Outline" outline)
  1826. (const :tag "Outline-path-completion" outline-path-completion)))
  1827. (defcustom org-goto-max-level 5
  1828. "Maximum target level when running `org-goto' with refile interface."
  1829. :group 'org-refile
  1830. :type 'integer)
  1831. (defcustom org-reverse-note-order nil
  1832. "Non-nil means store new notes at the beginning of a file or entry.
  1833. When nil, new notes will be filed to the end of a file or entry.
  1834. This can also be a list with cons cells of regular expressions that
  1835. are matched against file names, and values."
  1836. :group 'org-remember
  1837. :group 'org-capture
  1838. :group 'org-refile
  1839. :type '(choice
  1840. (const :tag "Reverse always" t)
  1841. (const :tag "Reverse never" nil)
  1842. (repeat :tag "By file name regexp"
  1843. (cons regexp boolean))))
  1844. (defcustom org-log-refile nil
  1845. "Information to record when a task is refiled.
  1846. Possible values are:
  1847. nil Don't add anything
  1848. time Add a time stamp to the task
  1849. note Prompt for a note and add it with template `org-log-note-headings'
  1850. This option can also be set with on a per-file-basis with
  1851. #+STARTUP: nologrefile
  1852. #+STARTUP: logrefile
  1853. #+STARTUP: lognoterefile
  1854. You can have local logging settings for a subtree by setting the LOGGING
  1855. property to one or more of these keywords.
  1856. When bulk-refiling from the agenda, the value `note' is forbidden and
  1857. will temporarily be changed to `time'."
  1858. :group 'org-refile
  1859. :group 'org-progress
  1860. :version "24.1"
  1861. :type '(choice
  1862. (const :tag "No logging" nil)
  1863. (const :tag "Record timestamp" time)
  1864. (const :tag "Record timestamp with note." note)))
  1865. (defcustom org-refile-targets nil
  1866. "Targets for refiling entries with \\[org-refile].
  1867. This is a list of cons cells. Each cell contains:
  1868. - a specification of the files to be considered, either a list of files,
  1869. or a symbol whose function or variable value will be used to retrieve
  1870. a file name or a list of file names. If you use `org-agenda-files' for
  1871. that, all agenda files will be scanned for targets. Nil means consider
  1872. headings in the current buffer.
  1873. - A specification of how to find candidate refile targets. This may be
  1874. any of:
  1875. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1876. This tag has to be present in all target headlines, inheritance will
  1877. not be considered.
  1878. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1879. todo keyword.
  1880. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1881. headlines that are refiling targets.
  1882. - a cons cell (:level . N). Any headline of level N is considered a target.
  1883. Note that, when `org-odd-levels-only' is set, level corresponds to
  1884. order in hierarchy, not to the number of stars.
  1885. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1886. Note that, when `org-odd-levels-only' is set, level corresponds to
  1887. order in hierarchy, not to the number of stars.
  1888. Each element of this list generates a set of possible targets.
  1889. The union of these sets is presented (with completion) to
  1890. the user by `org-refile'.
  1891. You can set the variable `org-refile-target-verify-function' to a function
  1892. to verify each headline found by the simple criteria above.
  1893. When this variable is nil, all top-level headlines in the current buffer
  1894. are used, equivalent to the value `((nil . (:level . 1))'."
  1895. :group 'org-refile
  1896. :type '(repeat
  1897. (cons
  1898. (choice :value org-agenda-files
  1899. (const :tag "All agenda files" org-agenda-files)
  1900. (const :tag "Current buffer" nil)
  1901. (function) (variable) (file))
  1902. (choice :tag "Identify target headline by"
  1903. (cons :tag "Specific tag" (const :value :tag) (string))
  1904. (cons :tag "TODO keyword" (const :value :todo) (string))
  1905. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1906. (cons :tag "Level number" (const :value :level) (integer))
  1907. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1908. (defcustom org-refile-target-verify-function nil
  1909. "Function to verify if the headline at point should be a refile target.
  1910. The function will be called without arguments, with point at the
  1911. beginning of the headline. It should return t and leave point
  1912. where it is if the headline is a valid target for refiling.
  1913. If the target should not be selected, the function must return nil.
  1914. In addition to this, it may move point to a place from where the search
  1915. should be continued. For example, the function may decide that the entire
  1916. subtree of the current entry should be excluded and move point to the end
  1917. of the subtree."
  1918. :group 'org-refile
  1919. :type '(choice
  1920. (const nil)
  1921. (function)))
  1922. (defcustom org-refile-use-cache nil
  1923. "Non-nil means cache refile targets to speed up the process.
  1924. The cache for a particular file will be updated automatically when
  1925. the buffer has been killed, or when any of the marker used for flagging
  1926. refile targets no longer points at a live buffer.
  1927. If you have added new entries to a buffer that might themselves be targets,
  1928. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1929. find that easier, `C-u C-u C-u C-c C-w'."
  1930. :group 'org-refile
  1931. :version "24.1"
  1932. :type 'boolean)
  1933. (defcustom org-refile-use-outline-path nil
  1934. "Non-nil means provide refile targets as paths.
  1935. So a level 3 headline will be available as level1/level2/level3.
  1936. When the value is `file', also include the file name (without directory)
  1937. into the path. In this case, you can also stop the completion after
  1938. the file name, to get entries inserted as top level in the file.
  1939. When `full-file-path', include the full file path."
  1940. :group 'org-refile
  1941. :type '(choice
  1942. (const :tag "Not" nil)
  1943. (const :tag "Yes" t)
  1944. (const :tag "Start with file name" file)
  1945. (const :tag "Start with full file path" full-file-path)))
  1946. (defcustom org-outline-path-complete-in-steps t
  1947. "Non-nil means complete the outline path in hierarchical steps.
  1948. When Org-mode uses the refile interface to select an outline path
  1949. \(see variable `org-refile-use-outline-path'), the completion of
  1950. the path can be done is a single go, or if can be done in steps down
  1951. the headline hierarchy. Going in steps is probably the best if you
  1952. do not use a special completion package like `ido' or `icicles'.
  1953. However, when using these packages, going in one step can be very
  1954. fast, while still showing the whole path to the entry."
  1955. :group 'org-refile
  1956. :type 'boolean)
  1957. (defcustom org-refile-allow-creating-parent-nodes nil
  1958. "Non-nil means allow to create new nodes as refile targets.
  1959. New nodes are then created by adding \"/new node name\" to the completion
  1960. of an existing node. When the value of this variable is `confirm',
  1961. new node creation must be confirmed by the user (recommended)
  1962. When nil, the completion must match an existing entry.
  1963. Note that, if the new heading is not seen by the criteria
  1964. listed in `org-refile-targets', multiple instances of the same
  1965. heading would be created by trying again to file under the new
  1966. heading."
  1967. :group 'org-refile
  1968. :type '(choice
  1969. (const :tag "Never" nil)
  1970. (const :tag "Always" t)
  1971. (const :tag "Prompt for confirmation" confirm)))
  1972. (defcustom org-refile-active-region-within-subtree nil
  1973. "Non-nil means also refile active region within a subtree.
  1974. By default `org-refile' doesn't allow refiling regions if they
  1975. don't contain a set of subtrees, but it might be convenient to
  1976. do so sometimes: in that case, the first line of the region is
  1977. converted to a headline before refiling."
  1978. :group 'org-refile
  1979. :version "24.1"
  1980. :type 'boolean)
  1981. (defgroup org-todo nil
  1982. "Options concerning TODO items in Org-mode."
  1983. :tag "Org TODO"
  1984. :group 'org)
  1985. (defgroup org-progress nil
  1986. "Options concerning Progress logging in Org-mode."
  1987. :tag "Org Progress"
  1988. :group 'org-time)
  1989. (defvar org-todo-interpretation-widgets
  1990. '((:tag "Sequence (cycling hits every state)" sequence)
  1991. (:tag "Type (cycling directly to DONE)" type))
  1992. "The available interpretation symbols for customizing `org-todo-keywords'.
  1993. Interested libraries should add to this list.")
  1994. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1995. "List of TODO entry keyword sequences and their interpretation.
  1996. \\<org-mode-map>This is a list of sequences.
  1997. Each sequence starts with a symbol, either `sequence' or `type',
  1998. indicating if the keywords should be interpreted as a sequence of
  1999. action steps, or as different types of TODO items. The first
  2000. keywords are states requiring action - these states will select a headline
  2001. for inclusion into the global TODO list Org-mode produces. If one of
  2002. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2003. signify that no further action is necessary. If \"|\" is not found,
  2004. the last keyword is treated as the only DONE state of the sequence.
  2005. The command \\[org-todo] cycles an entry through these states, and one
  2006. additional state where no keyword is present. For details about this
  2007. cycling, see the manual.
  2008. TODO keywords and interpretation can also be set on a per-file basis with
  2009. the special #+SEQ_TODO and #+TYP_TODO lines.
  2010. Each keyword can optionally specify a character for fast state selection
  2011. \(in combination with the variable `org-use-fast-todo-selection')
  2012. and specifiers for state change logging, using the same syntax that
  2013. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2014. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2015. indicates to record a time stamp each time this state is selected.
  2016. Each keyword may also specify if a timestamp or a note should be
  2017. recorded when entering or leaving the state, by adding additional
  2018. characters in the parenthesis after the keyword. This looks like this:
  2019. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2020. record only the time of the state change. With X and Y being either
  2021. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2022. Y when leaving the state if and only if the *target* state does not
  2023. define X. You may omit any of the fast-selection key or X or /Y,
  2024. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2025. For backward compatibility, this variable may also be just a list
  2026. of keywords. In this case the interpretation (sequence or type) will be
  2027. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2028. :group 'org-todo
  2029. :group 'org-keywords
  2030. :type '(choice
  2031. (repeat :tag "Old syntax, just keywords"
  2032. (string :tag "Keyword"))
  2033. (repeat :tag "New syntax"
  2034. (cons
  2035. (choice
  2036. :tag "Interpretation"
  2037. ;;Quick and dirty way to see
  2038. ;;`org-todo-interpretations'. This takes the
  2039. ;;place of item arguments
  2040. :convert-widget
  2041. (lambda (widget)
  2042. (widget-put widget
  2043. :args (mapcar
  2044. #'(lambda (x)
  2045. (widget-convert
  2046. (cons 'const x)))
  2047. org-todo-interpretation-widgets))
  2048. widget))
  2049. (repeat
  2050. (string :tag "Keyword"))))))
  2051. (defvar org-todo-keywords-1 nil
  2052. "All TODO and DONE keywords active in a buffer.")
  2053. (make-variable-buffer-local 'org-todo-keywords-1)
  2054. (defvar org-todo-keywords-for-agenda nil)
  2055. (defvar org-done-keywords-for-agenda nil)
  2056. (defvar org-drawers-for-agenda nil)
  2057. (defvar org-todo-keyword-alist-for-agenda nil)
  2058. (defvar org-tag-alist-for-agenda nil
  2059. "Alist of all tags from all agenda files.")
  2060. (defvar org-tag-groups-alist-for-agenda nil
  2061. "Alist of all groups tags from all current agenda files.")
  2062. (defvar org-tag-groups-alist nil)
  2063. (make-variable-buffer-local 'org-tag-groups-alist)
  2064. (defvar org-agenda-contributing-files nil)
  2065. (defvar org-not-done-keywords nil)
  2066. (make-variable-buffer-local 'org-not-done-keywords)
  2067. (defvar org-done-keywords nil)
  2068. (make-variable-buffer-local 'org-done-keywords)
  2069. (defvar org-todo-heads nil)
  2070. (make-variable-buffer-local 'org-todo-heads)
  2071. (defvar org-todo-sets nil)
  2072. (make-variable-buffer-local 'org-todo-sets)
  2073. (defvar org-todo-log-states nil)
  2074. (make-variable-buffer-local 'org-todo-log-states)
  2075. (defvar org-todo-kwd-alist nil)
  2076. (make-variable-buffer-local 'org-todo-kwd-alist)
  2077. (defvar org-todo-key-alist nil)
  2078. (make-variable-buffer-local 'org-todo-key-alist)
  2079. (defvar org-todo-key-trigger nil)
  2080. (make-variable-buffer-local 'org-todo-key-trigger)
  2081. (defcustom org-todo-interpretation 'sequence
  2082. "Controls how TODO keywords are interpreted.
  2083. This variable is in principle obsolete and is only used for
  2084. backward compatibility, if the interpretation of todo keywords is
  2085. not given already in `org-todo-keywords'. See that variable for
  2086. more information."
  2087. :group 'org-todo
  2088. :group 'org-keywords
  2089. :type '(choice (const sequence)
  2090. (const type)))
  2091. (defcustom org-use-fast-todo-selection t
  2092. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2093. This variable describes if and under what circumstances the cycling
  2094. mechanism for TODO keywords will be replaced by a single-key, direct
  2095. selection scheme.
  2096. When nil, fast selection is never used.
  2097. When the symbol `prefix', it will be used when `org-todo' is called
  2098. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2099. `C-u t' in an agenda buffer.
  2100. When t, fast selection is used by default. In this case, the prefix
  2101. argument forces cycling instead.
  2102. In all cases, the special interface is only used if access keys have
  2103. actually been assigned by the user, i.e. if keywords in the configuration
  2104. are followed by a letter in parenthesis, like TODO(t)."
  2105. :group 'org-todo
  2106. :type '(choice
  2107. (const :tag "Never" nil)
  2108. (const :tag "By default" t)
  2109. (const :tag "Only with C-u C-c C-t" prefix)))
  2110. (defcustom org-provide-todo-statistics t
  2111. "Non-nil means update todo statistics after insert and toggle.
  2112. ALL-HEADLINES means update todo statistics by including headlines
  2113. with no TODO keyword as well, counting them as not done.
  2114. A list of TODO keywords means the same, but skip keywords that are
  2115. not in this list.
  2116. When this is set, todo statistics is updated in the parent of the
  2117. current entry each time a todo state is changed."
  2118. :group 'org-todo
  2119. :type '(choice
  2120. (const :tag "Yes, only for TODO entries" t)
  2121. (const :tag "Yes, including all entries" 'all-headlines)
  2122. (repeat :tag "Yes, for TODOs in this list"
  2123. (string :tag "TODO keyword"))
  2124. (other :tag "No TODO statistics" nil)))
  2125. (defcustom org-hierarchical-todo-statistics t
  2126. "Non-nil means TODO statistics covers just direct children.
  2127. When nil, all entries in the subtree are considered.
  2128. This has only an effect if `org-provide-todo-statistics' is set.
  2129. To set this to nil for only a single subtree, use a COOKIE_DATA
  2130. property and include the word \"recursive\" into the value."
  2131. :group 'org-todo
  2132. :type 'boolean)
  2133. (defcustom org-after-todo-state-change-hook nil
  2134. "Hook which is run after the state of a TODO item was changed.
  2135. The new state (a string with a TODO keyword, or nil) is available in the
  2136. Lisp variable `org-state'."
  2137. :group 'org-todo
  2138. :type 'hook)
  2139. (defvar org-blocker-hook nil
  2140. "Hook for functions that are allowed to block a state change.
  2141. Functions in this hook should not modify the buffer.
  2142. Each function gets as its single argument a property list,
  2143. see `org-trigger-hook' for more information about this list.
  2144. If any of the functions in this hook returns nil, the state change
  2145. is blocked.")
  2146. (defvar org-trigger-hook nil
  2147. "Hook for functions that are triggered by a state change.
  2148. Each function gets as its single argument a property list with at
  2149. least the following elements:
  2150. (:type type-of-change :position pos-at-entry-start
  2151. :from old-state :to new-state)
  2152. Depending on the type, more properties may be present.
  2153. This mechanism is currently implemented for:
  2154. TODO state changes
  2155. ------------------
  2156. :type todo-state-change
  2157. :from previous state (keyword as a string), or nil, or a symbol
  2158. 'todo' or 'done', to indicate the general type of state.
  2159. :to new state, like in :from")
  2160. (defcustom org-enforce-todo-dependencies nil
  2161. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2162. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2163. be blocked if any prior sibling is not yet done.
  2164. Finally, if the parent is blocked because of ordered siblings of its own,
  2165. the child will also be blocked."
  2166. :set (lambda (var val)
  2167. (set var val)
  2168. (if val
  2169. (add-hook 'org-blocker-hook
  2170. 'org-block-todo-from-children-or-siblings-or-parent)
  2171. (remove-hook 'org-blocker-hook
  2172. 'org-block-todo-from-children-or-siblings-or-parent)))
  2173. :group 'org-todo
  2174. :type 'boolean)
  2175. (defcustom org-enforce-todo-checkbox-dependencies nil
  2176. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2177. When this is nil, checkboxes have no influence on switching TODO states.
  2178. When non-nil, you first need to check off all check boxes before the TODO
  2179. entry can be switched to DONE.
  2180. This variable needs to be set before org.el is loaded, and you need to
  2181. restart Emacs after a change to make the change effective. The only way
  2182. to change is while Emacs is running is through the customize interface."
  2183. :set (lambda (var val)
  2184. (set var val)
  2185. (if val
  2186. (add-hook 'org-blocker-hook
  2187. 'org-block-todo-from-checkboxes)
  2188. (remove-hook 'org-blocker-hook
  2189. 'org-block-todo-from-checkboxes)))
  2190. :group 'org-todo
  2191. :type 'boolean)
  2192. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2193. "Non-nil means inserting a TODO heading is treated as state change.
  2194. So when the command \\[org-insert-todo-heading] is used, state change
  2195. logging will apply if appropriate. When nil, the new TODO item will
  2196. be inserted directly, and no logging will take place."
  2197. :group 'org-todo
  2198. :type 'boolean)
  2199. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2200. "Non-nil means switching TODO states with S-cursor counts as state change.
  2201. This is the default behavior. However, setting this to nil allows a
  2202. convenient way to select a TODO state and bypass any logging associated
  2203. with that."
  2204. :group 'org-todo
  2205. :type 'boolean)
  2206. (defcustom org-todo-state-tags-triggers nil
  2207. "Tag changes that should be triggered by TODO state changes.
  2208. This is a list. Each entry is
  2209. (state-change (tag . flag) .......)
  2210. State-change can be a string with a state, and empty string to indicate the
  2211. state that has no TODO keyword, or it can be one of the symbols `todo'
  2212. or `done', meaning any not-done or done state, respectively."
  2213. :group 'org-todo
  2214. :group 'org-tags
  2215. :type '(repeat
  2216. (cons (choice :tag "When changing to"
  2217. (const :tag "Not-done state" todo)
  2218. (const :tag "Done state" done)
  2219. (string :tag "State"))
  2220. (repeat
  2221. (cons :tag "Tag action"
  2222. (string :tag "Tag")
  2223. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2224. (defcustom org-log-done nil
  2225. "Information to record when a task moves to the DONE state.
  2226. Possible values are:
  2227. nil Don't add anything, just change the keyword
  2228. time Add a time stamp to the task
  2229. note Prompt for a note and add it with template `org-log-note-headings'
  2230. This option can also be set with on a per-file-basis with
  2231. #+STARTUP: nologdone
  2232. #+STARTUP: logdone
  2233. #+STARTUP: lognotedone
  2234. You can have local logging settings for a subtree by setting the LOGGING
  2235. property to one or more of these keywords."
  2236. :group 'org-todo
  2237. :group 'org-progress
  2238. :type '(choice
  2239. (const :tag "No logging" nil)
  2240. (const :tag "Record CLOSED timestamp" time)
  2241. (const :tag "Record CLOSED timestamp with note." note)))
  2242. ;; Normalize old uses of org-log-done.
  2243. (cond
  2244. ((eq org-log-done t) (setq org-log-done 'time))
  2245. ((and (listp org-log-done) (memq 'done org-log-done))
  2246. (setq org-log-done 'note)))
  2247. (defcustom org-log-reschedule nil
  2248. "Information to record when the scheduling date of a tasks is modified.
  2249. Possible values are:
  2250. nil Don't add anything, just change the date
  2251. time Add a time stamp to the task
  2252. note Prompt for a note and add it with template `org-log-note-headings'
  2253. This option can also be set with on a per-file-basis with
  2254. #+STARTUP: nologreschedule
  2255. #+STARTUP: logreschedule
  2256. #+STARTUP: lognotereschedule"
  2257. :group 'org-todo
  2258. :group 'org-progress
  2259. :type '(choice
  2260. (const :tag "No logging" nil)
  2261. (const :tag "Record timestamp" time)
  2262. (const :tag "Record timestamp with note." note)))
  2263. (defcustom org-log-redeadline nil
  2264. "Information to record when the deadline date of a tasks is modified.
  2265. Possible values are:
  2266. nil Don't add anything, just change the date
  2267. time Add a time stamp to the task
  2268. note Prompt for a note and add it with template `org-log-note-headings'
  2269. This option can also be set with on a per-file-basis with
  2270. #+STARTUP: nologredeadline
  2271. #+STARTUP: logredeadline
  2272. #+STARTUP: lognoteredeadline
  2273. You can have local logging settings for a subtree by setting the LOGGING
  2274. property to one or more of these keywords."
  2275. :group 'org-todo
  2276. :group 'org-progress
  2277. :type '(choice
  2278. (const :tag "No logging" nil)
  2279. (const :tag "Record timestamp" time)
  2280. (const :tag "Record timestamp with note." note)))
  2281. (defcustom org-log-note-clock-out nil
  2282. "Non-nil means record a note when clocking out of an item.
  2283. This can also be configured on a per-file basis by adding one of
  2284. the following lines anywhere in the buffer:
  2285. #+STARTUP: lognoteclock-out
  2286. #+STARTUP: nolognoteclock-out"
  2287. :group 'org-todo
  2288. :group 'org-progress
  2289. :type 'boolean)
  2290. (defcustom org-log-done-with-time t
  2291. "Non-nil means the CLOSED time stamp will contain date and time.
  2292. When nil, only the date will be recorded."
  2293. :group 'org-progress
  2294. :type 'boolean)
  2295. (defcustom org-log-note-headings
  2296. '((done . "CLOSING NOTE %t")
  2297. (state . "State %-12s from %-12S %t")
  2298. (note . "Note taken on %t")
  2299. (reschedule . "Rescheduled from %S on %t")
  2300. (delschedule . "Not scheduled, was %S on %t")
  2301. (redeadline . "New deadline from %S on %t")
  2302. (deldeadline . "Removed deadline, was %S on %t")
  2303. (refile . "Refiled on %t")
  2304. (clock-out . ""))
  2305. "Headings for notes added to entries.
  2306. The value is an alist, with the car being a symbol indicating the note
  2307. context, and the cdr is the heading to be used. The heading may also be the
  2308. empty string.
  2309. %t in the heading will be replaced by a time stamp.
  2310. %T will be an active time stamp instead the default inactive one
  2311. %d will be replaced by a short-format time stamp.
  2312. %D will be replaced by an active short-format time stamp.
  2313. %s will be replaced by the new TODO state, in double quotes.
  2314. %S will be replaced by the old TODO state, in double quotes.
  2315. %u will be replaced by the user name.
  2316. %U will be replaced by the full user name.
  2317. In fact, it is not a good idea to change the `state' entry, because
  2318. agenda log mode depends on the format of these entries."
  2319. :group 'org-todo
  2320. :group 'org-progress
  2321. :type '(list :greedy t
  2322. (cons (const :tag "Heading when closing an item" done) string)
  2323. (cons (const :tag
  2324. "Heading when changing todo state (todo sequence only)"
  2325. state) string)
  2326. (cons (const :tag "Heading when just taking a note" note) string)
  2327. (cons (const :tag "Heading when clocking out" clock-out) string)
  2328. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2329. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2330. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2331. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2332. (cons (const :tag "Heading when refiling" refile) string)))
  2333. (unless (assq 'note org-log-note-headings)
  2334. (push '(note . "%t") org-log-note-headings))
  2335. (defcustom org-log-into-drawer nil
  2336. "Non-nil means insert state change notes and time stamps into a drawer.
  2337. When nil, state changes notes will be inserted after the headline and
  2338. any scheduling and clock lines, but not inside a drawer.
  2339. The value of this variable should be the name of the drawer to use.
  2340. LOGBOOK is proposed as the default drawer for this purpose, you can
  2341. also set this to a string to define the drawer of your choice.
  2342. A value of t is also allowed, representing \"LOGBOOK\".
  2343. A value of t or nil can also be set with on a per-file-basis with
  2344. #+STARTUP: logdrawer
  2345. #+STARTUP: nologdrawer
  2346. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2347. will be ignored.
  2348. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2349. a subtree."
  2350. :group 'org-todo
  2351. :group 'org-progress
  2352. :type '(choice
  2353. (const :tag "Not into a drawer" nil)
  2354. (const :tag "LOGBOOK" t)
  2355. (string :tag "Other")))
  2356. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2357. (defun org-log-into-drawer ()
  2358. "Return the value of `org-log-into-drawer', but let properties overrule.
  2359. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2360. used instead of the default value."
  2361. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2362. (cond
  2363. ((not p) org-log-into-drawer)
  2364. ((equal p "nil") nil)
  2365. ((equal p "t") "LOGBOOK")
  2366. (t p))))
  2367. (defcustom org-log-state-notes-insert-after-drawers nil
  2368. "Non-nil means insert state change notes after any drawers in entry.
  2369. Only the drawers that *immediately* follow the headline and the
  2370. deadline/scheduled line are skipped.
  2371. When nil, insert notes right after the heading and perhaps the line
  2372. with deadline/scheduling if present.
  2373. This variable will have no effect if `org-log-into-drawer' is
  2374. set."
  2375. :group 'org-todo
  2376. :group 'org-progress
  2377. :type 'boolean)
  2378. (defcustom org-log-states-order-reversed t
  2379. "Non-nil means the latest state note will be directly after heading.
  2380. When nil, the state change notes will be ordered according to time.
  2381. This option can also be set with on a per-file-basis with
  2382. #+STARTUP: logstatesreversed
  2383. #+STARTUP: nologstatesreversed"
  2384. :group 'org-todo
  2385. :group 'org-progress
  2386. :type 'boolean)
  2387. (defcustom org-todo-repeat-to-state nil
  2388. "The TODO state to which a repeater should return the repeating task.
  2389. By default this is the first task in a TODO sequence, or the previous state
  2390. in a TODO_TYP set. But you can specify another task here.
  2391. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2392. :group 'org-todo
  2393. :version "24.1"
  2394. :type '(choice (const :tag "Head of sequence" nil)
  2395. (string :tag "Specific state")))
  2396. (defcustom org-log-repeat 'time
  2397. "Non-nil means record moving through the DONE state when triggering repeat.
  2398. An auto-repeating task is immediately switched back to TODO when
  2399. marked DONE. If you are not logging state changes (by adding \"@\"
  2400. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2401. record a closing note, there will be no record of the task moving
  2402. through DONE. This variable forces taking a note anyway.
  2403. nil Don't force a record
  2404. time Record a time stamp
  2405. note Prompt for a note and add it with template `org-log-note-headings'
  2406. This option can also be set with on a per-file-basis with
  2407. #+STARTUP: nologrepeat
  2408. #+STARTUP: logrepeat
  2409. #+STARTUP: lognoterepeat
  2410. You can have local logging settings for a subtree by setting the LOGGING
  2411. property to one or more of these keywords."
  2412. :group 'org-todo
  2413. :group 'org-progress
  2414. :type '(choice
  2415. (const :tag "Don't force a record" nil)
  2416. (const :tag "Force recording the DONE state" time)
  2417. (const :tag "Force recording a note with the DONE state" note)))
  2418. (defgroup org-priorities nil
  2419. "Priorities in Org-mode."
  2420. :tag "Org Priorities"
  2421. :group 'org-todo)
  2422. (defcustom org-enable-priority-commands t
  2423. "Non-nil means priority commands are active.
  2424. When nil, these commands will be disabled, so that you never accidentally
  2425. set a priority."
  2426. :group 'org-priorities
  2427. :type 'boolean)
  2428. (defcustom org-highest-priority ?A
  2429. "The highest priority of TODO items. A character like ?A, ?B etc.
  2430. Must have a smaller ASCII number than `org-lowest-priority'."
  2431. :group 'org-priorities
  2432. :type 'character)
  2433. (defcustom org-lowest-priority ?C
  2434. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2435. Must have a larger ASCII number than `org-highest-priority'."
  2436. :group 'org-priorities
  2437. :type 'character)
  2438. (defcustom org-default-priority ?B
  2439. "The default priority of TODO items.
  2440. This is the priority an item gets if no explicit priority is given.
  2441. When starting to cycle on an empty priority the first step in the cycle
  2442. depends on `org-priority-start-cycle-with-default'. The resulting first
  2443. step priority must not exceed the range from `org-highest-priority' to
  2444. `org-lowest-priority' which means that `org-default-priority' has to be
  2445. in this range exclusive or inclusive the range boundaries. Else the
  2446. first step refuses to set the default and the second will fall back
  2447. to (depending on the command used) the highest or lowest priority."
  2448. :group 'org-priorities
  2449. :type 'character)
  2450. (defcustom org-priority-start-cycle-with-default t
  2451. "Non-nil means start with default priority when starting to cycle.
  2452. When this is nil, the first step in the cycle will be (depending on the
  2453. command used) one higher or lower than the default priority.
  2454. See also `org-default-priority'."
  2455. :group 'org-priorities
  2456. :type 'boolean)
  2457. (defcustom org-get-priority-function nil
  2458. "Function to extract the priority from a string.
  2459. The string is normally the headline. If this is nil Org computes the
  2460. priority from the priority cookie like [#A] in the headline. It returns
  2461. an integer, increasing by 1000 for each priority level.
  2462. The user can set a different function here, which should take a string
  2463. as an argument and return the numeric priority."
  2464. :group 'org-priorities
  2465. :version "24.1"
  2466. :type '(choice
  2467. (const nil)
  2468. (function)))
  2469. (defgroup org-time nil
  2470. "Options concerning time stamps and deadlines in Org-mode."
  2471. :tag "Org Time"
  2472. :group 'org)
  2473. (defcustom org-insert-labeled-timestamps-at-point nil
  2474. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2475. When nil, these labeled time stamps are forces into the second line of an
  2476. entry, just after the headline. When scheduling from the global TODO list,
  2477. the time stamp will always be forced into the second line."
  2478. :group 'org-time
  2479. :type 'boolean)
  2480. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2481. "Formats for `format-time-string' which are used for time stamps.
  2482. It is not recommended to change this constant.")
  2483. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2484. "Number of minutes to round time stamps to.
  2485. These are two values, the first applies when first creating a time stamp.
  2486. The second applies when changing it with the commands `S-up' and `S-down'.
  2487. When changing the time stamp, this means that it will change in steps
  2488. of N minutes, as given by the second value.
  2489. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2490. numbers should be factors of 60, so for example 5, 10, 15.
  2491. When this is larger than 1, you can still force an exact time stamp by using
  2492. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2493. and by using a prefix arg to `S-up/down' to specify the exact number
  2494. of minutes to shift."
  2495. :group 'org-time
  2496. :get #'(lambda (var) ; Make sure both elements are there
  2497. (if (integerp (default-value var))
  2498. (list (default-value var) 5)
  2499. (default-value var)))
  2500. :type '(list
  2501. (integer :tag "when inserting times")
  2502. (integer :tag "when modifying times")))
  2503. ;; Normalize old customizations of this variable.
  2504. (when (integerp org-time-stamp-rounding-minutes)
  2505. (setq org-time-stamp-rounding-minutes
  2506. (list org-time-stamp-rounding-minutes
  2507. org-time-stamp-rounding-minutes)))
  2508. (defcustom org-display-custom-times nil
  2509. "Non-nil means overlay custom formats over all time stamps.
  2510. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2511. To turn this on on a per-file basis, insert anywhere in the file:
  2512. #+STARTUP: customtime"
  2513. :group 'org-time
  2514. :set 'set-default
  2515. :type 'sexp)
  2516. (make-variable-buffer-local 'org-display-custom-times)
  2517. (defcustom org-time-stamp-custom-formats
  2518. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2519. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2520. These are overlaid over the default ISO format if the variable
  2521. `org-display-custom-times' is set. Time like %H:%M should be at the
  2522. end of the second format. The custom formats are also honored by export
  2523. commands, if custom time display is turned on at the time of export."
  2524. :group 'org-time
  2525. :type 'sexp)
  2526. (defun org-time-stamp-format (&optional long inactive)
  2527. "Get the right format for a time string."
  2528. (let ((f (if long (cdr org-time-stamp-formats)
  2529. (car org-time-stamp-formats))))
  2530. (if inactive
  2531. (concat "[" (substring f 1 -1) "]")
  2532. f)))
  2533. (defcustom org-time-clocksum-format
  2534. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2535. "The format string used when creating CLOCKSUM lines.
  2536. This is also used when Org mode generates a time duration.
  2537. The value can be a single format string containing two
  2538. %-sequences, which will be filled with the number of hours and
  2539. minutes in that order.
  2540. Alternatively, the value can be a plist associating any of the
  2541. keys :years, :months, :weeks, :days, :hours or :minutes with
  2542. format strings. The time duration is formatted using only the
  2543. time components that are needed and concatenating the results.
  2544. If a time unit in absent, it falls back to the next smallest
  2545. unit.
  2546. The keys :require-years, :require-months, :require-days,
  2547. :require-weeks, :require-hours, :require-minutes are also
  2548. meaningful. A non-nil value for these keys indicates that the
  2549. corresponding time component should always be included, even if
  2550. its value is 0.
  2551. For example,
  2552. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2553. :require-minutes t)
  2554. means durations longer than a day will be expressed in days,
  2555. hours and minutes, and durations less than a day will always be
  2556. expressed in hours and minutes (even for durations less than an
  2557. hour).
  2558. The value
  2559. \(:days \"%dd\" :minutes \"%dm\")
  2560. means durations longer than a day will be expressed in days and
  2561. minutes, and durations less than a day will be expressed entirely
  2562. in minutes (even for durations longer than an hour)."
  2563. :group 'org-time
  2564. :group 'org-clock
  2565. :version "24.4"
  2566. :package-version '(Org . "8.0")
  2567. :type '(choice (string :tag "Format string")
  2568. (set :tag "Plist"
  2569. (group :inline t (const :tag "Years" :years)
  2570. (string :tag "Format string"))
  2571. (group :inline t
  2572. (const :tag "Always show years" :require-years)
  2573. (const t))
  2574. (group :inline t (const :tag "Months" :months)
  2575. (string :tag "Format string"))
  2576. (group :inline t
  2577. (const :tag "Always show months" :require-months)
  2578. (const t))
  2579. (group :inline t (const :tag "Weeks" :weeks)
  2580. (string :tag "Format string"))
  2581. (group :inline t
  2582. (const :tag "Always show weeks" :require-weeks)
  2583. (const t))
  2584. (group :inline t (const :tag "Days" :days)
  2585. (string :tag "Format string"))
  2586. (group :inline t
  2587. (const :tag "Always show days" :require-days)
  2588. (const t))
  2589. (group :inline t (const :tag "Hours" :hours)
  2590. (string :tag "Format string"))
  2591. (group :inline t
  2592. (const :tag "Always show hours" :require-hours)
  2593. (const t))
  2594. (group :inline t (const :tag "Minutes" :minutes)
  2595. (string :tag "Format string"))
  2596. (group :inline t
  2597. (const :tag "Always show minutes" :require-minutes)
  2598. (const t)))))
  2599. (defcustom org-time-clocksum-use-fractional nil
  2600. "When non-nil, \\[org-clock-display] uses fractional times.
  2601. See `org-time-clocksum-format' for more on time clock formats."
  2602. :group 'org-time
  2603. :group 'org-clock
  2604. :version "24.3"
  2605. :type 'boolean)
  2606. (defcustom org-time-clocksum-use-effort-durations nil
  2607. "When non-nil, \\[org-clock-display] uses effort durations.
  2608. E.g. by default, one day is considered to be a 8 hours effort,
  2609. so a task that has been clocked for 16 hours will be displayed
  2610. as during 2 days in the clock display or in the clocktable.
  2611. See `org-effort-durations' on how to set effort durations
  2612. and `org-time-clocksum-format' for more on time clock formats."
  2613. :group 'org-time
  2614. :group 'org-clock
  2615. :version "24.4"
  2616. :package-version '(Org . "8.0")
  2617. :type 'boolean)
  2618. (defcustom org-time-clocksum-fractional-format "%.2f"
  2619. "The format string used when creating CLOCKSUM lines,
  2620. or when Org mode generates a time duration, if
  2621. `org-time-clocksum-use-fractional' is enabled.
  2622. The value can be a single format string containing one
  2623. %-sequence, which will be filled with the number of hours as
  2624. a float.
  2625. Alternatively, the value can be a plist associating any of the
  2626. keys :years, :months, :weeks, :days, :hours or :minutes with
  2627. a format string. The time duration is formatted using the
  2628. largest time unit which gives a non-zero integer part. If all
  2629. specified formats have zero integer part, the smallest time unit
  2630. is used."
  2631. :group 'org-time
  2632. :type '(choice (string :tag "Format string")
  2633. (set (group :inline t (const :tag "Years" :years)
  2634. (string :tag "Format string"))
  2635. (group :inline t (const :tag "Months" :months)
  2636. (string :tag "Format string"))
  2637. (group :inline t (const :tag "Weeks" :weeks)
  2638. (string :tag "Format string"))
  2639. (group :inline t (const :tag "Days" :days)
  2640. (string :tag "Format string"))
  2641. (group :inline t (const :tag "Hours" :hours)
  2642. (string :tag "Format string"))
  2643. (group :inline t (const :tag "Minutes" :minutes)
  2644. (string :tag "Format string")))))
  2645. (defcustom org-deadline-warning-days 14
  2646. "Number of days before expiration during which a deadline becomes active.
  2647. This variable governs the display in sparse trees and in the agenda.
  2648. When 0 or negative, it means use this number (the absolute value of it)
  2649. even if a deadline has a different individual lead time specified.
  2650. Custom commands can set this variable in the options section."
  2651. :group 'org-time
  2652. :group 'org-agenda-daily/weekly
  2653. :type 'integer)
  2654. (defcustom org-scheduled-delay-days 0
  2655. "Number of days before a scheduled item becomes active.
  2656. This variable governs the display in sparse trees and in the agenda.
  2657. The default value (i.e. 0) means: don't delay scheduled item.
  2658. When negative, it means use this number (the absolute value of it)
  2659. even if a scheduled item has a different individual delay time
  2660. specified.
  2661. Custom commands can set this variable in the options section."
  2662. :group 'org-time
  2663. :group 'org-agenda-daily/weekly
  2664. :version "24.4"
  2665. :package-version '(Org . "8.0")
  2666. :type 'integer)
  2667. (defcustom org-read-date-prefer-future t
  2668. "Non-nil means assume future for incomplete date input from user.
  2669. This affects the following situations:
  2670. 1. The user gives a month but not a year.
  2671. For example, if it is April and you enter \"feb 2\", this will be read
  2672. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2673. 2. The user gives a day, but no month.
  2674. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2675. read this as the third of *next* month. However, if you enter \"17\",
  2676. it will be considered as *this* month.
  2677. If you set this variable to the symbol `time', then also the following
  2678. will work:
  2679. 3. If the user gives a time.
  2680. If the time is before now, it will be interpreted as tomorrow.
  2681. Currently none of this works for ISO week specifications.
  2682. When this option is nil, the current day, month and year will always be
  2683. used as defaults.
  2684. See also `org-agenda-jump-prefer-future'."
  2685. :group 'org-time
  2686. :type '(choice
  2687. (const :tag "Never" nil)
  2688. (const :tag "Check month and day" t)
  2689. (const :tag "Check month, day, and time" time)))
  2690. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2691. "Should the agenda jump command prefer the future for incomplete dates?
  2692. The default is to do the same as configured in `org-read-date-prefer-future'.
  2693. But you can also set a deviating value here.
  2694. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2695. :group 'org-agenda
  2696. :group 'org-time
  2697. :version "24.1"
  2698. :type '(choice
  2699. (const :tag "Use org-read-date-prefer-future"
  2700. org-read-date-prefer-future)
  2701. (const :tag "Never" nil)
  2702. (const :tag "Always" t)))
  2703. (defcustom org-read-date-force-compatible-dates t
  2704. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2705. Depending on the system Emacs is running on, certain dates cannot
  2706. be represented with the type used internally to represent time.
  2707. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2708. correctly. Some systems allow for earlier dates, some for later,
  2709. some for both. One way to find out it to insert any date into an
  2710. Org buffer, putting the cursor on the year and hitting S-up and
  2711. S-down to test the range.
  2712. When this variable is set to t, the date/time prompt will not let
  2713. you specify dates outside the 1970-2037 range, so it is certain that
  2714. these dates will work in whatever version of Emacs you are
  2715. running, and also that you can move a file from one Emacs implementation
  2716. to another. WHenever Org is forcing the year for you, it will display
  2717. a message and beep.
  2718. When this variable is nil, Org will check if the date is
  2719. representable in the specific Emacs implementation you are using.
  2720. If not, it will force a year, usually the current year, and beep
  2721. to remind you. Currently this setting is not recommended because
  2722. the likelihood that you will open your Org files in an Emacs that
  2723. has limited date range is not negligible.
  2724. A workaround for this problem is to use diary sexp dates for time
  2725. stamps outside of this range."
  2726. :group 'org-time
  2727. :version "24.1"
  2728. :type 'boolean)
  2729. (defcustom org-read-date-display-live t
  2730. "Non-nil means display current interpretation of date prompt live.
  2731. This display will be in an overlay, in the minibuffer."
  2732. :group 'org-time
  2733. :type 'boolean)
  2734. (defcustom org-read-date-popup-calendar t
  2735. "Non-nil means pop up a calendar when prompting for a date.
  2736. In the calendar, the date can be selected with mouse-1. However, the
  2737. minibuffer will also be active, and you can simply enter the date as well.
  2738. When nil, only the minibuffer will be available."
  2739. :group 'org-time
  2740. :type 'boolean)
  2741. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2742. 'org-read-date-popup-calendar)
  2743. (make-obsolete-variable
  2744. 'org-read-date-minibuffer-setup-hook
  2745. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2746. (defcustom org-read-date-minibuffer-setup-hook nil
  2747. "Hook to be used to set up keys for the date/time interface.
  2748. Add key definitions to `minibuffer-local-map', which will be a
  2749. temporary copy.
  2750. WARNING: This option is obsolete, you should use
  2751. `org-read-date-minibuffer-local-map' to set up keys."
  2752. :group 'org-time
  2753. :type 'hook)
  2754. (defcustom org-extend-today-until 0
  2755. "The hour when your day really ends. Must be an integer.
  2756. This has influence for the following applications:
  2757. - When switching the agenda to \"today\". It it is still earlier than
  2758. the time given here, the day recognized as TODAY is actually yesterday.
  2759. - When a date is read from the user and it is still before the time given
  2760. here, the current date and time will be assumed to be yesterday, 23:59.
  2761. Also, timestamps inserted in capture templates follow this rule.
  2762. IMPORTANT: This is a feature whose implementation is and likely will
  2763. remain incomplete. Really, it is only here because past midnight seems to
  2764. be the favorite working time of John Wiegley :-)"
  2765. :group 'org-time
  2766. :type 'integer)
  2767. (defcustom org-use-effective-time nil
  2768. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2769. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2770. \"effective time\" of any timestamps between midnight and 8am will be
  2771. 23:59 of the previous day."
  2772. :group 'org-time
  2773. :version "24.1"
  2774. :type 'boolean)
  2775. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2776. "When non-nil, use the last clock out time for `org-todo'.
  2777. Note that this option has precedence over the combined use of
  2778. `org-use-effective-time' and `org-extend-today-until'."
  2779. :group 'org-time
  2780. :version "24.4"
  2781. :package-version '(Org . "8.0")
  2782. :type 'boolean)
  2783. (defcustom org-edit-timestamp-down-means-later nil
  2784. "Non-nil means S-down will increase the time in a time stamp.
  2785. When nil, S-up will increase."
  2786. :group 'org-time
  2787. :type 'boolean)
  2788. (defcustom org-calendar-follow-timestamp-change t
  2789. "Non-nil means make the calendar window follow timestamp changes.
  2790. When a timestamp is modified and the calendar window is visible, it will be
  2791. moved to the new date."
  2792. :group 'org-time
  2793. :type 'boolean)
  2794. (defgroup org-tags nil
  2795. "Options concerning tags in Org-mode."
  2796. :tag "Org Tags"
  2797. :group 'org)
  2798. (defcustom org-tag-alist nil
  2799. "List of tags allowed in Org-mode files.
  2800. When this list is nil, Org-mode will base TAG input on what is already in the
  2801. buffer.
  2802. The value of this variable is an alist, the car of each entry must be a
  2803. keyword as a string, the cdr may be a character that is used to select
  2804. that tag through the fast-tag-selection interface.
  2805. See the manual for details."
  2806. :group 'org-tags
  2807. :type '(repeat
  2808. (choice
  2809. (cons (string :tag "Tag name")
  2810. (character :tag "Access char"))
  2811. (list :tag "Start radio group"
  2812. (const :startgroup)
  2813. (option (string :tag "Group description")))
  2814. (list :tag "Group tags delimiter"
  2815. (const :grouptags))
  2816. (list :tag "End radio group"
  2817. (const :endgroup)
  2818. (option (string :tag "Group description")))
  2819. (const :tag "New line" (:newline)))))
  2820. (defcustom org-tag-persistent-alist nil
  2821. "List of tags that will always appear in all Org-mode files.
  2822. This is in addition to any in buffer settings or customizations
  2823. of `org-tag-alist'.
  2824. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2825. The value of this variable is an alist, the car of each entry must be a
  2826. keyword as a string, the cdr may be a character that is used to select
  2827. that tag through the fast-tag-selection interface.
  2828. See the manual for details.
  2829. To disable these tags on a per-file basis, insert anywhere in the file:
  2830. #+STARTUP: noptag"
  2831. :group 'org-tags
  2832. :type '(repeat
  2833. (choice
  2834. (cons (string :tag "Tag name")
  2835. (character :tag "Access char"))
  2836. (const :tag "Start radio group" (:startgroup))
  2837. (const :tag "Group tags delimiter" (:grouptags))
  2838. (const :tag "End radio group" (:endgroup))
  2839. (const :tag "New line" (:newline)))))
  2840. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2841. "If non-nil, always offer completion for all tags of all agenda files.
  2842. Instead of customizing this variable directly, you might want to
  2843. set it locally for capture buffers, because there no list of
  2844. tags in that file can be created dynamically (there are none).
  2845. (add-hook 'org-capture-mode-hook
  2846. (lambda ()
  2847. (set (make-local-variable
  2848. 'org-complete-tags-always-offer-all-agenda-tags)
  2849. t)))"
  2850. :group 'org-tags
  2851. :version "24.1"
  2852. :type 'boolean)
  2853. (defvar org-file-tags nil
  2854. "List of tags that can be inherited by all entries in the file.
  2855. The tags will be inherited if the variable `org-use-tag-inheritance'
  2856. says they should be.
  2857. This variable is populated from #+FILETAGS lines.")
  2858. (defcustom org-use-fast-tag-selection 'auto
  2859. "Non-nil means use fast tag selection scheme.
  2860. This is a special interface to select and deselect tags with single keys.
  2861. When nil, fast selection is never used.
  2862. When the symbol `auto', fast selection is used if and only if selection
  2863. characters for tags have been configured, either through the variable
  2864. `org-tag-alist' or through a #+TAGS line in the buffer.
  2865. When t, fast selection is always used and selection keys are assigned
  2866. automatically if necessary."
  2867. :group 'org-tags
  2868. :type '(choice
  2869. (const :tag "Always" t)
  2870. (const :tag "Never" nil)
  2871. (const :tag "When selection characters are configured" auto)))
  2872. (defcustom org-fast-tag-selection-single-key nil
  2873. "Non-nil means fast tag selection exits after first change.
  2874. When nil, you have to press RET to exit it.
  2875. During fast tag selection, you can toggle this flag with `C-c'.
  2876. This variable can also have the value `expert'. In this case, the window
  2877. displaying the tags menu is not even shown, until you press C-c again."
  2878. :group 'org-tags
  2879. :type '(choice
  2880. (const :tag "No" nil)
  2881. (const :tag "Yes" t)
  2882. (const :tag "Expert" expert)))
  2883. (defvar org-fast-tag-selection-include-todo nil
  2884. "Non-nil means fast tags selection interface will also offer TODO states.
  2885. This is an undocumented feature, you should not rely on it.")
  2886. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2887. "The column to which tags should be indented in a headline.
  2888. If this number is positive, it specifies the column. If it is negative,
  2889. it means that the tags should be flushright to that column. For example,
  2890. -80 works well for a normal 80 character screen.
  2891. When 0, place tags directly after headline text, with only one space in
  2892. between."
  2893. :group 'org-tags
  2894. :type 'integer)
  2895. (defcustom org-auto-align-tags t
  2896. "Non-nil keeps tags aligned when modifying headlines.
  2897. Some operations (i.e. demoting) change the length of a headline and
  2898. therefore shift the tags around. With this option turned on, after
  2899. each such operation the tags are again aligned to `org-tags-column'."
  2900. :group 'org-tags
  2901. :type 'boolean)
  2902. (defcustom org-use-tag-inheritance t
  2903. "Non-nil means tags in levels apply also for sublevels.
  2904. When nil, only the tags directly given in a specific line apply there.
  2905. This may also be a list of tags that should be inherited, or a regexp that
  2906. matches tags that should be inherited. Additional control is possible
  2907. with the variable `org-tags-exclude-from-inheritance' which gives an
  2908. explicit list of tags to be excluded from inheritance, even if the value of
  2909. `org-use-tag-inheritance' would select it for inheritance.
  2910. If this option is t, a match early-on in a tree can lead to a large
  2911. number of matches in the subtree when constructing the agenda or creating
  2912. a sparse tree. If you only want to see the first match in a tree during
  2913. a search, check out the variable `org-tags-match-list-sublevels'."
  2914. :group 'org-tags
  2915. :type '(choice
  2916. (const :tag "Not" nil)
  2917. (const :tag "Always" t)
  2918. (repeat :tag "Specific tags" (string :tag "Tag"))
  2919. (regexp :tag "Tags matched by regexp")))
  2920. (defcustom org-tags-exclude-from-inheritance nil
  2921. "List of tags that should never be inherited.
  2922. This is a way to exclude a few tags from inheritance. For way to do
  2923. the opposite, to actively allow inheritance for selected tags,
  2924. see the variable `org-use-tag-inheritance'."
  2925. :group 'org-tags
  2926. :type '(repeat (string :tag "Tag")))
  2927. (defun org-tag-inherit-p (tag)
  2928. "Check if TAG is one that should be inherited."
  2929. (cond
  2930. ((member tag org-tags-exclude-from-inheritance) nil)
  2931. ((eq org-use-tag-inheritance t) t)
  2932. ((not org-use-tag-inheritance) nil)
  2933. ((stringp org-use-tag-inheritance)
  2934. (string-match org-use-tag-inheritance tag))
  2935. ((listp org-use-tag-inheritance)
  2936. (member tag org-use-tag-inheritance))
  2937. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2938. (defcustom org-tags-match-list-sublevels t
  2939. "Non-nil means list also sublevels of headlines matching a search.
  2940. This variable applies to tags/property searches, and also to stuck
  2941. projects because this search is based on a tags match as well.
  2942. When set to the symbol `indented', sublevels are indented with
  2943. leading dots.
  2944. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2945. the sublevels of a headline matching a tag search often also match
  2946. the same search. Listing all of them can create very long lists.
  2947. Setting this variable to nil causes subtrees of a match to be skipped.
  2948. This variable is semi-obsolete and probably should always be true. It
  2949. is better to limit inheritance to certain tags using the variables
  2950. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2951. :group 'org-tags
  2952. :type '(choice
  2953. (const :tag "No, don't list them" nil)
  2954. (const :tag "Yes, do list them" t)
  2955. (const :tag "List them, indented with leading dots" indented)))
  2956. (defcustom org-tags-sort-function nil
  2957. "When set, tags are sorted using this function as a comparator."
  2958. :group 'org-tags
  2959. :type '(choice
  2960. (const :tag "No sorting" nil)
  2961. (const :tag "Alphabetical" string<)
  2962. (const :tag "Reverse alphabetical" string>)
  2963. (function :tag "Custom function" nil)))
  2964. (defvar org-tags-history nil
  2965. "History of minibuffer reads for tags.")
  2966. (defvar org-last-tags-completion-table nil
  2967. "The last used completion table for tags.")
  2968. (defvar org-after-tags-change-hook nil
  2969. "Hook that is run after the tags in a line have changed.")
  2970. (defgroup org-properties nil
  2971. "Options concerning properties in Org-mode."
  2972. :tag "Org Properties"
  2973. :group 'org)
  2974. (defcustom org-property-format "%-10s %s"
  2975. "How property key/value pairs should be formatted by `indent-line'.
  2976. When `indent-line' hits a property definition, it will format the line
  2977. according to this format, mainly to make sure that the values are
  2978. lined-up with respect to each other."
  2979. :group 'org-properties
  2980. :type 'string)
  2981. (defcustom org-properties-postprocess-alist nil
  2982. "Alist of properties and functions to adjust inserted values.
  2983. Elements of this alist must be of the form
  2984. ([string] [function])
  2985. where [string] must be a property name and [function] must be a
  2986. lambda expression: this lambda expression must take one argument,
  2987. the value to adjust, and return the new value as a string.
  2988. For example, this element will allow the property \"Remaining\"
  2989. to be updated wrt the relation between the \"Effort\" property
  2990. and the clock summary:
  2991. ((\"Remaining\" (lambda(value)
  2992. (let ((clocksum (org-clock-sum-current-item))
  2993. (effort (org-duration-string-to-minutes
  2994. (org-entry-get (point) \"Effort\"))))
  2995. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2996. :group 'org-properties
  2997. :version "24.1"
  2998. :type '(alist :key-type (string :tag "Property")
  2999. :value-type (function :tag "Function")))
  3000. (defcustom org-use-property-inheritance nil
  3001. "Non-nil means properties apply also for sublevels.
  3002. This setting is chiefly used during property searches. Turning it on can
  3003. cause significant overhead when doing a search, which is why it is not
  3004. on by default.
  3005. When nil, only the properties directly given in the current entry count.
  3006. When t, every property is inherited. The value may also be a list of
  3007. properties that should have inheritance, or a regular expression matching
  3008. properties that should be inherited.
  3009. However, note that some special properties use inheritance under special
  3010. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3011. and the properties ending in \"_ALL\" when they are used as descriptor
  3012. for valid values of a property.
  3013. Note for programmers:
  3014. When querying an entry with `org-entry-get', you can control if inheritance
  3015. should be used. By default, `org-entry-get' looks only at the local
  3016. properties. You can request inheritance by setting the inherit argument
  3017. to t (to force inheritance) or to `selective' (to respect the setting
  3018. in this variable)."
  3019. :group 'org-properties
  3020. :type '(choice
  3021. (const :tag "Not" nil)
  3022. (const :tag "Always" t)
  3023. (repeat :tag "Specific properties" (string :tag "Property"))
  3024. (regexp :tag "Properties matched by regexp")))
  3025. (defun org-property-inherit-p (property)
  3026. "Check if PROPERTY is one that should be inherited."
  3027. (cond
  3028. ((eq org-use-property-inheritance t) t)
  3029. ((not org-use-property-inheritance) nil)
  3030. ((stringp org-use-property-inheritance)
  3031. (string-match org-use-property-inheritance property))
  3032. ((listp org-use-property-inheritance)
  3033. (member property org-use-property-inheritance))
  3034. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3035. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3036. "The default column format, if no other format has been defined.
  3037. This variable can be set on the per-file basis by inserting a line
  3038. #+COLUMNS: %25ITEM ....."
  3039. :group 'org-properties
  3040. :type 'string)
  3041. (defcustom org-columns-ellipses ".."
  3042. "The ellipses to be used when a field in column view is truncated.
  3043. When this is the empty string, as many characters as possible are shown,
  3044. but then there will be no visual indication that the field has been truncated.
  3045. When this is a string of length N, the last N characters of a truncated
  3046. field are replaced by this string. If the column is narrower than the
  3047. ellipses string, only part of the ellipses string will be shown."
  3048. :group 'org-properties
  3049. :type 'string)
  3050. (defcustom org-columns-modify-value-for-display-function nil
  3051. "Function that modifies values for display in column view.
  3052. For example, it can be used to cut out a certain part from a time stamp.
  3053. The function must take 2 arguments:
  3054. column-title The title of the column (*not* the property name)
  3055. value The value that should be modified.
  3056. The function should return the value that should be displayed,
  3057. or nil if the normal value should be used."
  3058. :group 'org-properties
  3059. :type '(choice (const nil) (function)))
  3060. (defcustom org-effort-property "Effort"
  3061. "The property that is being used to keep track of effort estimates.
  3062. Effort estimates given in this property need to have the format H:MM."
  3063. :group 'org-properties
  3064. :group 'org-progress
  3065. :type '(string :tag "Property"))
  3066. (defconst org-global-properties-fixed
  3067. '(("VISIBILITY_ALL" . "folded children content all")
  3068. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3069. "List of property/value pairs that can be inherited by any entry.
  3070. These are fixed values, for the preset properties. The user variable
  3071. that can be used to add to this list is `org-global-properties'.
  3072. The entries in this list are cons cells where the car is a property
  3073. name and cdr is a string with the value. If the value represents
  3074. multiple items like an \"_ALL\" property, separate the items by
  3075. spaces.")
  3076. (defcustom org-global-properties nil
  3077. "List of property/value pairs that can be inherited by any entry.
  3078. This list will be combined with the constant `org-global-properties-fixed'.
  3079. The entries in this list are cons cells where the car is a property
  3080. name and cdr is a string with the value.
  3081. You can set buffer-local values for the same purpose in the variable
  3082. `org-file-properties' this by adding lines like
  3083. #+PROPERTY: NAME VALUE"
  3084. :group 'org-properties
  3085. :type '(repeat
  3086. (cons (string :tag "Property")
  3087. (string :tag "Value"))))
  3088. (defvar org-file-properties nil
  3089. "List of property/value pairs that can be inherited by any entry.
  3090. Valid for the current buffer.
  3091. This variable is populated from #+PROPERTY lines.")
  3092. (make-variable-buffer-local 'org-file-properties)
  3093. (defgroup org-agenda nil
  3094. "Options concerning agenda views in Org-mode."
  3095. :tag "Org Agenda"
  3096. :group 'org)
  3097. (defvar org-category nil
  3098. "Variable used by org files to set a category for agenda display.
  3099. Such files should use a file variable to set it, for example
  3100. # -*- mode: org; org-category: \"ELisp\"
  3101. or contain a special line
  3102. #+CATEGORY: ELisp
  3103. If the file does not specify a category, then file's base name
  3104. is used instead.")
  3105. (make-variable-buffer-local 'org-category)
  3106. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3107. (defcustom org-agenda-files nil
  3108. "The files to be used for agenda display.
  3109. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3110. \\[org-remove-file]. You can also use customize to edit the list.
  3111. If an entry is a directory, all files in that directory that are matched by
  3112. `org-agenda-file-regexp' will be part of the file list.
  3113. If the value of the variable is not a list but a single file name, then
  3114. the list of agenda files is actually stored and maintained in that file, one
  3115. agenda file per line. In this file paths can be given relative to
  3116. `org-directory'. Tilde expansion and environment variable substitution
  3117. are also made."
  3118. :group 'org-agenda
  3119. :type '(choice
  3120. (repeat :tag "List of files and directories" file)
  3121. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3122. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3123. "Regular expression to match files for `org-agenda-files'.
  3124. If any element in the list in that variable contains a directory instead
  3125. of a normal file, all files in that directory that are matched by this
  3126. regular expression will be included."
  3127. :group 'org-agenda
  3128. :type 'regexp)
  3129. (defcustom org-agenda-text-search-extra-files nil
  3130. "List of extra files to be searched by text search commands.
  3131. These files will be searched in addition to the agenda files by the
  3132. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3133. Note that these files will only be searched for text search commands,
  3134. not for the other agenda views like todo lists, tag searches or the weekly
  3135. agenda. This variable is intended to list notes and possibly archive files
  3136. that should also be searched by these two commands.
  3137. In fact, if the first element in the list is the symbol `agenda-archives',
  3138. then all archive files of all agenda files will be added to the search
  3139. scope."
  3140. :group 'org-agenda
  3141. :type '(set :greedy t
  3142. (const :tag "Agenda Archives" agenda-archives)
  3143. (repeat :inline t (file))))
  3144. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3145. 'org-agenda-text-search-extra-files)
  3146. (defcustom org-agenda-skip-unavailable-files nil
  3147. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3148. A nil value means to remove them, after a query, from the list."
  3149. :group 'org-agenda
  3150. :type 'boolean)
  3151. (defcustom org-calendar-to-agenda-key [?c]
  3152. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3153. The command `org-calendar-goto-agenda' will be bound to this key. The
  3154. default is the character `c' because then `c' can be used to switch back and
  3155. forth between agenda and calendar."
  3156. :group 'org-agenda
  3157. :type 'sexp)
  3158. (defcustom org-calendar-insert-diary-entry-key [?i]
  3159. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3160. This option is irrelevant until `org-agenda-diary-file' has been configured
  3161. to point to an Org-mode file. When that is the case, the command
  3162. `org-agenda-diary-entry' will be bound to the key given here, by default
  3163. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3164. if you want to continue doing this, you need to change this to a different
  3165. key."
  3166. :group 'org-agenda
  3167. :type 'sexp)
  3168. (defcustom org-agenda-diary-file 'diary-file
  3169. "File to which to add new entries with the `i' key in agenda and calendar.
  3170. When this is the symbol `diary-file', the functionality in the Emacs
  3171. calendar will be used to add entries to the `diary-file'. But when this
  3172. points to a file, `org-agenda-diary-entry' will be used instead."
  3173. :group 'org-agenda
  3174. :type '(choice
  3175. (const :tag "The standard Emacs diary file" diary-file)
  3176. (file :tag "Special Org file diary entries")))
  3177. (eval-after-load "calendar"
  3178. '(progn
  3179. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3180. 'org-calendar-goto-agenda)
  3181. (add-hook 'calendar-mode-hook
  3182. (lambda ()
  3183. (unless (eq org-agenda-diary-file 'diary-file)
  3184. (define-key calendar-mode-map
  3185. org-calendar-insert-diary-entry-key
  3186. 'org-agenda-diary-entry))))))
  3187. (defgroup org-latex nil
  3188. "Options for embedding LaTeX code into Org-mode."
  3189. :tag "Org LaTeX"
  3190. :group 'org)
  3191. (defcustom org-format-latex-options
  3192. '(:foreground default :background default :scale 1.0
  3193. :html-foreground "Black" :html-background "Transparent"
  3194. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3195. "Options for creating images from LaTeX fragments.
  3196. This is a property list with the following properties:
  3197. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3198. `default' means use the foreground of the default face.
  3199. `auto' means use the foreground from the text face.
  3200. :background the background color, or \"Transparent\".
  3201. `default' means use the background of the default face.
  3202. `auto' means use the background from the text face.
  3203. :scale a scaling factor for the size of the images, to get more pixels
  3204. :html-foreground, :html-background, :html-scale
  3205. the same numbers for HTML export.
  3206. :matchers a list indicating which matchers should be used to
  3207. find LaTeX fragments. Valid members of this list are:
  3208. \"begin\" find environments
  3209. \"$1\" find single characters surrounded by $.$
  3210. \"$\" find math expressions surrounded by $...$
  3211. \"$$\" find math expressions surrounded by $$....$$
  3212. \"\\(\" find math expressions surrounded by \\(...\\)
  3213. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3214. :group 'org-latex
  3215. :type 'plist)
  3216. (defcustom org-format-latex-signal-error t
  3217. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3218. When nil, just push out a message."
  3219. :group 'org-latex
  3220. :version "24.1"
  3221. :type 'boolean)
  3222. (defcustom org-latex-to-mathml-jar-file nil
  3223. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3224. Use this to specify additional executable file say a jar file.
  3225. When using MathToWeb as the converter, specify the full-path to
  3226. your mathtoweb.jar file."
  3227. :group 'org-latex
  3228. :version "24.1"
  3229. :type '(choice
  3230. (const :tag "None" nil)
  3231. (file :tag "JAR file" :must-match t)))
  3232. (defcustom org-latex-to-mathml-convert-command nil
  3233. "Command to convert LaTeX fragments to MathML.
  3234. Replace format-specifiers in the command as noted below and use
  3235. `shell-command' to convert LaTeX to MathML.
  3236. %j: Executable file in fully expanded form as specified by
  3237. `org-latex-to-mathml-jar-file'.
  3238. %I: Input LaTeX file in fully expanded form
  3239. %o: Output MathML file
  3240. This command is used by `org-create-math-formula'.
  3241. When using MathToWeb as the converter, set this to
  3242. \"java -jar %j -unicode -force -df %o %I\"."
  3243. :group 'org-latex
  3244. :version "24.1"
  3245. :type '(choice
  3246. (const :tag "None" nil)
  3247. (string :tag "\nShell command")))
  3248. (defcustom org-latex-create-formula-image-program 'dvipng
  3249. "Program to convert LaTeX fragments with.
  3250. dvipng Process the LaTeX fragments to dvi file, then convert
  3251. dvi files to png files using dvipng.
  3252. This will also include processing of non-math environments.
  3253. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3254. to convert pdf files to png files"
  3255. :group 'org-latex
  3256. :version "24.1"
  3257. :type '(choice
  3258. (const :tag "dvipng" dvipng)
  3259. (const :tag "imagemagick" imagemagick)))
  3260. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3261. "Path to store latex preview images.
  3262. A relative path here creates many directories relative to the
  3263. processed org files paths. An absolute path puts all preview
  3264. images at the same place."
  3265. :group 'org-latex
  3266. :version "24.3"
  3267. :type 'string)
  3268. (defun org-format-latex-mathml-available-p ()
  3269. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3270. (save-match-data
  3271. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3272. org-latex-to-mathml-convert-command)
  3273. (let ((executable (car (split-string
  3274. org-latex-to-mathml-convert-command))))
  3275. (when (executable-find executable)
  3276. (if (string-match
  3277. "%j" org-latex-to-mathml-convert-command)
  3278. (file-readable-p org-latex-to-mathml-jar-file)
  3279. t))))))
  3280. (defcustom org-format-latex-header "\\documentclass{article}
  3281. \\usepackage[usenames]{color}
  3282. \\usepackage{amsmath}
  3283. \\usepackage[mathscr]{eucal}
  3284. \[PACKAGES]
  3285. \[DEFAULT-PACKAGES]
  3286. \\pagestyle{empty} % do not remove
  3287. % The settings below are copied from fullpage.sty
  3288. \\setlength{\\textwidth}{\\paperwidth}
  3289. \\addtolength{\\textwidth}{-3cm}
  3290. \\setlength{\\oddsidemargin}{1.5cm}
  3291. \\addtolength{\\oddsidemargin}{-2.54cm}
  3292. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3293. \\setlength{\\textheight}{\\paperheight}
  3294. \\addtolength{\\textheight}{-\\headheight}
  3295. \\addtolength{\\textheight}{-\\headsep}
  3296. \\addtolength{\\textheight}{-\\footskip}
  3297. \\addtolength{\\textheight}{-3cm}
  3298. \\setlength{\\topmargin}{1.5cm}
  3299. \\addtolength{\\topmargin}{-2.54cm}"
  3300. "The document header used for processing LaTeX fragments.
  3301. It is imperative that this header make sure that no page number
  3302. appears on the page. The package defined in the variables
  3303. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3304. will either replace the placeholder \"[PACKAGES]\" in this
  3305. header, or they will be appended."
  3306. :group 'org-latex
  3307. :type 'string)
  3308. (defun org-set-packages-alist (var val)
  3309. "Set the packages alist and make sure it has 3 elements per entry."
  3310. (set var (mapcar (lambda (x)
  3311. (if (and (consp x) (= (length x) 2))
  3312. (list (car x) (nth 1 x) t)
  3313. x))
  3314. val)))
  3315. (defun org-get-packages-alist (var)
  3316. "Get the packages alist and make sure it has 3 elements per entry."
  3317. (mapcar (lambda (x)
  3318. (if (and (consp x) (= (length x) 2))
  3319. (list (car x) (nth 1 x) t)
  3320. x))
  3321. (default-value var)))
  3322. (defcustom org-latex-default-packages-alist
  3323. '(("AUTO" "inputenc" t)
  3324. ("T1" "fontenc" t)
  3325. ("" "fixltx2e" nil)
  3326. ("" "graphicx" t)
  3327. ("" "longtable" nil)
  3328. ("" "float" nil)
  3329. ("" "wrapfig" nil)
  3330. ("normalem" "ulem" t)
  3331. ("" "textcomp" t)
  3332. ("" "marvosym" t)
  3333. ("" "wasysym" t)
  3334. ("" "latexsym" t)
  3335. ("" "amssymb" t)
  3336. ("" "amstext" nil)
  3337. ("" "hyperref" nil)
  3338. "\\tolerance=1000")
  3339. "Alist of default packages to be inserted in the header.
  3340. Change this only if one of the packages here causes an
  3341. incompatibility with another package you are using.
  3342. The packages in this list are needed by one part or another of
  3343. Org mode to function properly:
  3344. - inputenc, fontenc: for basic font and character selection
  3345. - amstext: for subscript and superscript
  3346. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3347. symbols used for interpreting the entities in `org-entities'.
  3348. You can skip some of these packages if you don't use any of the
  3349. symbols in it.
  3350. - ulem: for underline and strike-through
  3351. - graphicx: for including images
  3352. - float, wrapfig: for figure placement
  3353. - longtable: for long tables
  3354. - hyperref: for cross references
  3355. Therefore you should not modify this variable unless you know
  3356. what you are doing. The one reason to change it anyway is that
  3357. you might be loading some other package that conflicts with one
  3358. of the default packages. Each cell is of the format
  3359. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3360. the package also needs to be included when compiling LaTeX
  3361. snippets into images for inclusion into non-LaTeX output."
  3362. :group 'org-latex
  3363. :group 'org-export-latex
  3364. :set 'org-set-packages-alist
  3365. :get 'org-get-packages-alist
  3366. :version "24.1"
  3367. :type '(repeat
  3368. (choice
  3369. (list :tag "options/package pair"
  3370. (string :tag "options")
  3371. (string :tag "package")
  3372. (boolean :tag "Snippet"))
  3373. (string :tag "A line of LaTeX"))))
  3374. (defcustom org-latex-packages-alist nil
  3375. "Alist of packages to be inserted in every LaTeX header.
  3376. These will be inserted after `org-latex-default-packages-alist'.
  3377. Each cell is of the format:
  3378. \(\"options\" \"package\" snippet-flag)
  3379. SNIPPET-FLAG, when t, indicates that this package is also needed
  3380. when turning LaTeX snippets into images for inclusion into
  3381. non-LaTeX output.
  3382. Make sure that you only list packages here which:
  3383. - you want in every file
  3384. - do not conflict with the setup in `org-format-latex-header'.
  3385. - do not conflict with the default packages in
  3386. `org-latex-default-packages-alist'."
  3387. :group 'org-latex
  3388. :group 'org-export-latex
  3389. :set 'org-set-packages-alist
  3390. :get 'org-get-packages-alist
  3391. :type '(repeat
  3392. (choice
  3393. (list :tag "options/package pair"
  3394. (string :tag "options")
  3395. (string :tag "package")
  3396. (boolean :tag "Snippet"))
  3397. (string :tag "A line of LaTeX"))))
  3398. (defgroup org-appearance nil
  3399. "Settings for Org-mode appearance."
  3400. :tag "Org Appearance"
  3401. :group 'org)
  3402. (defcustom org-level-color-stars-only nil
  3403. "Non-nil means fontify only the stars in each headline.
  3404. When nil, the entire headline is fontified.
  3405. Changing it requires restart of `font-lock-mode' to become effective
  3406. also in regions already fontified."
  3407. :group 'org-appearance
  3408. :type 'boolean)
  3409. (defcustom org-hide-leading-stars nil
  3410. "Non-nil means hide the first N-1 stars in a headline.
  3411. This works by using the face `org-hide' for these stars. This
  3412. face is white for a light background, and black for a dark
  3413. background. You may have to customize the face `org-hide' to
  3414. make this work.
  3415. Changing it requires restart of `font-lock-mode' to become effective
  3416. also in regions already fontified.
  3417. You may also set this on a per-file basis by adding one of the following
  3418. lines to the buffer:
  3419. #+STARTUP: hidestars
  3420. #+STARTUP: showstars"
  3421. :group 'org-appearance
  3422. :type 'boolean)
  3423. (defcustom org-hidden-keywords nil
  3424. "List of symbols corresponding to keywords to be hidden the org buffer.
  3425. For example, a value '(title) for this list will make the document's title
  3426. appear in the buffer without the initial #+TITLE: keyword."
  3427. :group 'org-appearance
  3428. :version "24.1"
  3429. :type '(set (const :tag "#+AUTHOR" author)
  3430. (const :tag "#+DATE" date)
  3431. (const :tag "#+EMAIL" email)
  3432. (const :tag "#+TITLE" title)))
  3433. (defcustom org-custom-properties nil
  3434. "List of properties (as strings) with a special meaning.
  3435. The default use of these custom properties is to let the user
  3436. hide them with `org-toggle-custom-properties-visibility'."
  3437. :group 'org-properties
  3438. :group 'org-appearance
  3439. :version "24.3"
  3440. :type '(repeat (string :tag "Property Name")))
  3441. (defcustom org-fontify-done-headline nil
  3442. "Non-nil means change the face of a headline if it is marked DONE.
  3443. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3444. When this is non-nil, the headline after the keyword is set to the
  3445. `org-headline-done' as an additional indication."
  3446. :group 'org-appearance
  3447. :type 'boolean)
  3448. (defcustom org-fontify-emphasized-text t
  3449. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3450. Changing this variable requires a restart of Emacs to take effect."
  3451. :group 'org-appearance
  3452. :type 'boolean)
  3453. (defcustom org-fontify-whole-heading-line nil
  3454. "Non-nil means fontify the whole line for headings.
  3455. This is useful when setting a background color for the
  3456. org-level-* faces."
  3457. :group 'org-appearance
  3458. :type 'boolean)
  3459. (defcustom org-highlight-latex-and-related nil
  3460. "Non-nil means highlight LaTeX related syntax in the buffer.
  3461. When non nil, the value should be a list containing any of the
  3462. following symbols:
  3463. `latex' Highlight LaTeX snippets and environments.
  3464. `script' Highlight subscript and superscript.
  3465. `entities' Highlight entities."
  3466. :group 'org-appearance
  3467. :version "24.4"
  3468. :package-version '(Org . "8.0")
  3469. :type '(choice
  3470. (const :tag "No highlighting" nil)
  3471. (set :greedy t :tag "Highlight"
  3472. (const :tag "LaTeX snippets and environments" latex)
  3473. (const :tag "Subscript and superscript" script)
  3474. (const :tag "Entities" entities))))
  3475. (defcustom org-hide-emphasis-markers nil
  3476. "Non-nil mean font-lock should hide the emphasis marker characters."
  3477. :group 'org-appearance
  3478. :type 'boolean)
  3479. (defcustom org-pretty-entities nil
  3480. "Non-nil means show entities as UTF8 characters.
  3481. When nil, the \\name form remains in the buffer."
  3482. :group 'org-appearance
  3483. :version "24.1"
  3484. :type 'boolean)
  3485. (defcustom org-pretty-entities-include-sub-superscripts t
  3486. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3487. :group 'org-appearance
  3488. :version "24.1"
  3489. :type 'boolean)
  3490. (defvar org-emph-re nil
  3491. "Regular expression for matching emphasis.
  3492. After a match, the match groups contain these elements:
  3493. 0 The match of the full regular expression, including the characters
  3494. before and after the proper match
  3495. 1 The character before the proper match, or empty at beginning of line
  3496. 2 The proper match, including the leading and trailing markers
  3497. 3 The leading marker like * or /, indicating the type of highlighting
  3498. 4 The text between the emphasis markers, not including the markers
  3499. 5 The character after the match, empty at the end of a line")
  3500. (defvar org-verbatim-re nil
  3501. "Regular expression for matching verbatim text.")
  3502. (defvar org-emphasis-regexp-components) ; defined just below
  3503. (defvar org-emphasis-alist) ; defined just below
  3504. (defun org-set-emph-re (var val)
  3505. "Set variable and compute the emphasis regular expression."
  3506. (set var val)
  3507. (when (and (boundp 'org-emphasis-alist)
  3508. (boundp 'org-emphasis-regexp-components)
  3509. org-emphasis-alist org-emphasis-regexp-components)
  3510. (let* ((e org-emphasis-regexp-components)
  3511. (pre (car e))
  3512. (post (nth 1 e))
  3513. (border (nth 2 e))
  3514. (body (nth 3 e))
  3515. (nl (nth 4 e))
  3516. (body1 (concat body "*?"))
  3517. (markers (mapconcat 'car org-emphasis-alist ""))
  3518. (vmarkers (mapconcat
  3519. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3520. org-emphasis-alist "")))
  3521. ;; make sure special characters appear at the right position in the class
  3522. (if (string-match "\\^" markers)
  3523. (setq markers (concat (replace-match "" t t markers) "^")))
  3524. (if (string-match "-" markers)
  3525. (setq markers (concat (replace-match "" t t markers) "-")))
  3526. (if (string-match "\\^" vmarkers)
  3527. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3528. (if (string-match "-" vmarkers)
  3529. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3530. (if (> nl 0)
  3531. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3532. (int-to-string nl) "\\}")))
  3533. ;; Make the regexp
  3534. (setq org-emph-re
  3535. (concat "\\([" pre "]\\|^\\)"
  3536. "\\("
  3537. "\\([" markers "]\\)"
  3538. "\\("
  3539. "[^" border "]\\|"
  3540. "[^" border "]"
  3541. body1
  3542. "[^" border "]"
  3543. "\\)"
  3544. "\\3\\)"
  3545. "\\([" post "]\\|$\\)"))
  3546. (setq org-verbatim-re
  3547. (concat "\\([" pre "]\\|^\\)"
  3548. "\\("
  3549. "\\([" vmarkers "]\\)"
  3550. "\\("
  3551. "[^" border "]\\|"
  3552. "[^" border "]"
  3553. body1
  3554. "[^" border "]"
  3555. "\\)"
  3556. "\\3\\)"
  3557. "\\([" post "]\\|$\\)")))))
  3558. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3559. ;; set this option proved cumbersome. See this message/thread:
  3560. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3561. (defvar org-emphasis-regexp-components
  3562. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3563. "Components used to build the regular expression for emphasis.
  3564. This is a list with five entries. Terminology: In an emphasis string
  3565. like \" *strong word* \", we call the initial space PREMATCH, the final
  3566. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3567. and \"trong wor\" is the body. The different components in this variable
  3568. specify what is allowed/forbidden in each part:
  3569. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3570. post Chars allowed as postmatch. End of line will be allowed too.
  3571. border The chars *forbidden* as border characters.
  3572. body-regexp A regexp like \".\" to match a body character. Don't use
  3573. non-shy groups here, and don't allow newline here.
  3574. newline The maximum number of newlines allowed in an emphasis exp.
  3575. You need to reload Org or to restart Emacs after customizing this.")
  3576. (defcustom org-emphasis-alist
  3577. `(("*" bold)
  3578. ("/" italic)
  3579. ("_" underline)
  3580. ("=" org-code verbatim)
  3581. ("~" org-verbatim verbatim)
  3582. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3583. "Alist of characters and faces to emphasize text.
  3584. Text starting and ending with a special character will be emphasized,
  3585. for example *bold*, _underlined_ and /italic/. This variable sets the
  3586. marker characters and the face to be used by font-lock for highlighting
  3587. in Org-mode Emacs buffers.
  3588. You need to reload Org or to restart Emacs after customizing this."
  3589. :group 'org-appearance
  3590. :set 'org-set-emph-re
  3591. :version "24.4"
  3592. :package-version '(Org . "8.0")
  3593. :type '(repeat
  3594. (list
  3595. (string :tag "Marker character")
  3596. (choice
  3597. (face :tag "Font-lock-face")
  3598. (plist :tag "Face property list"))
  3599. (option (const verbatim)))))
  3600. (defvar org-protecting-blocks
  3601. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3602. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3603. This is needed for font-lock setup.")
  3604. ;;; Miscellaneous options
  3605. (defgroup org-completion nil
  3606. "Completion in Org-mode."
  3607. :tag "Org Completion"
  3608. :group 'org)
  3609. (defcustom org-completion-use-ido nil
  3610. "Non-nil means use ido completion wherever possible.
  3611. Note that `ido-mode' must be active for this variable to be relevant.
  3612. If you decide to turn this variable on, you might well want to turn off
  3613. `org-outline-path-complete-in-steps'.
  3614. See also `org-completion-use-iswitchb'."
  3615. :group 'org-completion
  3616. :type 'boolean)
  3617. (defcustom org-completion-use-iswitchb nil
  3618. "Non-nil means use iswitchb completion wherever possible.
  3619. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3620. If you decide to turn this variable on, you might well want to turn off
  3621. `org-outline-path-complete-in-steps'.
  3622. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3623. :group 'org-completion
  3624. :type 'boolean)
  3625. (defcustom org-completion-fallback-command 'hippie-expand
  3626. "The expansion command called by \\[pcomplete] in normal context.
  3627. Normal means, no org-mode-specific context."
  3628. :group 'org-completion
  3629. :type 'function)
  3630. ;;; Functions and variables from their packages
  3631. ;; Declared here to avoid compiler warnings
  3632. ;; XEmacs only
  3633. (defvar outline-mode-menu-heading)
  3634. (defvar outline-mode-menu-show)
  3635. (defvar outline-mode-menu-hide)
  3636. (defvar zmacs-regions) ; XEmacs regions
  3637. ;; Emacs only
  3638. (defvar mark-active)
  3639. ;; Various packages
  3640. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3641. (declare-function calendar-forward-day "cal-move" (arg))
  3642. (declare-function calendar-goto-date "cal-move" (date))
  3643. (declare-function calendar-goto-today "cal-move" ())
  3644. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3645. (defvar calc-embedded-close-formula)
  3646. (defvar calc-embedded-open-formula)
  3647. (declare-function cdlatex-tab "ext:cdlatex" ())
  3648. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3649. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3650. (defvar font-lock-unfontify-region-function)
  3651. (declare-function iswitchb-read-buffer "iswitchb"
  3652. (prompt &optional default require-match start matches-set))
  3653. (defvar iswitchb-temp-buflist)
  3654. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3655. (defvar org-agenda-tags-todo-honor-ignore-options)
  3656. (declare-function org-agenda-skip "org-agenda" ())
  3657. (declare-function
  3658. org-agenda-format-item "org-agenda"
  3659. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3660. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3661. (declare-function org-agenda-change-all-lines "org-agenda"
  3662. (newhead hdmarker &optional fixface just-this))
  3663. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3664. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3665. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3666. (beg end))
  3667. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3668. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3669. "org-agenda" (&optional end))
  3670. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3671. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3672. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3673. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3674. (declare-function org-indent-mode "org-indent" (&optional arg))
  3675. (declare-function parse-time-string "parse-time" (string))
  3676. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3677. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3678. (defvar remember-data-file)
  3679. (defvar texmathp-why)
  3680. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3681. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3682. (defvar org-latex-regexps)
  3683. ;;; Autoload and prepare some org modules
  3684. ;; Some table stuff that needs to be defined here, because it is used
  3685. ;; by the functions setting up org-mode or checking for table context.
  3686. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3687. "Detect an org-type or table-type table.")
  3688. (defconst org-table-line-regexp "^[ \t]*|"
  3689. "Detect an org-type table line.")
  3690. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3691. "Detect an org-type table line.")
  3692. (defconst org-table-hline-regexp "^[ \t]*|-"
  3693. "Detect an org-type table hline.")
  3694. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3695. "Detect a table-type table hline.")
  3696. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3697. "Detect the first line outside a table when searching from within it.
  3698. This works for both table types.")
  3699. ;; Autoload the functions in org-table.el that are needed by functions here.
  3700. (eval-and-compile
  3701. (org-autoload "org-table"
  3702. '(org-table-begin org-table-blank-field org-table-end)))
  3703. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3704. "Detect a #+TBLFM line.")
  3705. ;;;###autoload
  3706. (defun turn-on-orgtbl ()
  3707. "Unconditionally turn on `orgtbl-mode'."
  3708. (require 'org-table)
  3709. (orgtbl-mode 1))
  3710. (defun org-at-table-p (&optional table-type)
  3711. "Return t if the cursor is inside an org-type table.
  3712. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3713. (if org-enable-table-editor
  3714. (save-excursion
  3715. (beginning-of-line 1)
  3716. (looking-at (if table-type org-table-any-line-regexp
  3717. org-table-line-regexp)))
  3718. nil))
  3719. (defsubst org-table-p () (org-at-table-p))
  3720. (defun org-at-table.el-p ()
  3721. "Return t if and only if we are at a table.el table."
  3722. (and (org-at-table-p 'any)
  3723. (save-excursion
  3724. (goto-char (org-table-begin 'any))
  3725. (looking-at org-table1-hline-regexp))))
  3726. (defun org-table-recognize-table.el ()
  3727. "If there is a table.el table nearby, recognize it and move into it."
  3728. (if org-table-tab-recognizes-table.el
  3729. (if (org-at-table.el-p)
  3730. (progn
  3731. (beginning-of-line 1)
  3732. (if (looking-at org-table-dataline-regexp)
  3733. nil
  3734. (if (looking-at org-table1-hline-regexp)
  3735. (progn
  3736. (beginning-of-line 2)
  3737. (if (looking-at org-table-any-border-regexp)
  3738. (beginning-of-line -1)))))
  3739. (if (re-search-forward "|" (org-table-end t) t)
  3740. (progn
  3741. (require 'table)
  3742. (if (table--at-cell-p (point))
  3743. t
  3744. (message "recognizing table.el table...")
  3745. (table-recognize-table)
  3746. (message "recognizing table.el table...done")))
  3747. (error "This should not happen"))
  3748. t)
  3749. nil)
  3750. nil))
  3751. (defun org-at-table-hline-p ()
  3752. "Return t if the cursor is inside a hline in a table."
  3753. (if org-enable-table-editor
  3754. (save-excursion
  3755. (beginning-of-line 1)
  3756. (looking-at org-table-hline-regexp))
  3757. nil))
  3758. (defun org-table-map-tables (function &optional quietly)
  3759. "Apply FUNCTION to the start of all tables in the buffer."
  3760. (save-excursion
  3761. (save-restriction
  3762. (widen)
  3763. (goto-char (point-min))
  3764. (while (re-search-forward org-table-any-line-regexp nil t)
  3765. (unless quietly
  3766. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3767. (beginning-of-line 1)
  3768. (when (and (looking-at org-table-line-regexp)
  3769. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3770. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3771. (save-excursion (funcall function))
  3772. (or (looking-at org-table-line-regexp)
  3773. (forward-char 1)))
  3774. (re-search-forward org-table-any-border-regexp nil 1))))
  3775. (unless quietly (message "Mapping tables: done")))
  3776. ;; Declare and autoload functions from org-agenda.el
  3777. (eval-and-compile
  3778. (org-autoload "org-agenda"
  3779. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3780. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3781. (declare-function org-clock-update-mode-line "org-clock" ())
  3782. (declare-function org-resolve-clocks "org-clock"
  3783. (&optional also-non-dangling-p prompt last-valid))
  3784. (defun org-at-TBLFM-p (&optional pos)
  3785. "Return t when point (or POS) is in #+TBLFM line."
  3786. (save-excursion
  3787. (let ((pos pos)))
  3788. (goto-char (or pos (point)))
  3789. (beginning-of-line 1)
  3790. (looking-at org-TBLFM-regexp)))
  3791. (defvar org-clock-start-time)
  3792. (defvar org-clock-marker (make-marker)
  3793. "Marker recording the last clock-in.")
  3794. (defvar org-clock-hd-marker (make-marker)
  3795. "Marker recording the last clock-in, but the headline position.")
  3796. (defvar org-clock-heading ""
  3797. "The heading of the current clock entry.")
  3798. (defun org-clock-is-active ()
  3799. "Return the buffer where the clock is currently running.
  3800. Return nil if no clock is running."
  3801. (marker-buffer org-clock-marker))
  3802. (eval-and-compile
  3803. (org-autoload "org-clock" '(org-clock-remove-overlays
  3804. org-clock-update-time-maybe
  3805. org-clocktable-shift)))
  3806. (defun org-check-running-clock ()
  3807. "Check if the current buffer contains the running clock.
  3808. If yes, offer to stop it and to save the buffer with the changes."
  3809. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3810. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3811. (buffer-name))))
  3812. (org-clock-out)
  3813. (when (y-or-n-p "Save changed buffer?")
  3814. (save-buffer))))
  3815. (defun org-clocktable-try-shift (dir n)
  3816. "Check if this line starts a clock table, if yes, shift the time block."
  3817. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3818. (org-clocktable-shift dir n)))
  3819. ;;;###autoload
  3820. (defun org-clock-persistence-insinuate ()
  3821. "Set up hooks for clock persistence."
  3822. (require 'org-clock)
  3823. (add-hook 'org-mode-hook 'org-clock-load)
  3824. (add-hook 'kill-emacs-hook 'org-clock-save))
  3825. ;; Define the variable already here, to make sure we have it.
  3826. (defvar org-indent-mode nil
  3827. "Non-nil if Org-Indent mode is enabled.
  3828. Use the command `org-indent-mode' to change this variable.")
  3829. ;; Autoload archiving code
  3830. ;; The stuff that is needed for cycling and tags has to be defined here.
  3831. (defgroup org-archive nil
  3832. "Options concerning archiving in Org-mode."
  3833. :tag "Org Archive"
  3834. :group 'org-structure)
  3835. (defcustom org-archive-location "%s_archive::"
  3836. "The location where subtrees should be archived.
  3837. The value of this variable is a string, consisting of two parts,
  3838. separated by a double-colon. The first part is a filename and
  3839. the second part is a headline.
  3840. When the filename is omitted, archiving happens in the same file.
  3841. %s in the filename will be replaced by the current file
  3842. name (without the directory part). Archiving to a different file
  3843. is useful to keep archived entries from contributing to the
  3844. Org-mode Agenda.
  3845. The archived entries will be filed as subtrees of the specified
  3846. headline. When the headline is omitted, the subtrees are simply
  3847. filed away at the end of the file, as top-level entries. Also in
  3848. the heading you can use %s to represent the file name, this can be
  3849. useful when using the same archive for a number of different files.
  3850. Here are a few examples:
  3851. \"%s_archive::\"
  3852. If the current file is Projects.org, archive in file
  3853. Projects.org_archive, as top-level trees. This is the default.
  3854. \"::* Archived Tasks\"
  3855. Archive in the current file, under the top-level headline
  3856. \"* Archived Tasks\".
  3857. \"~/org/archive.org::\"
  3858. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3859. \"~/org/archive.org::* From %s\"
  3860. Archive in file ~/org/archive.org (absolute path), under headlines
  3861. \"From FILENAME\" where file name is the current file name.
  3862. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3863. The \"datetree/\" string is special, signifying to archive
  3864. items to the datetree. Items are placed in either the CLOSED
  3865. date of the item, or the current date if there is no CLOSED date.
  3866. The heading will be a subentry to the current date. There doesn't
  3867. need to be a heading, but there always needs to be a slash after
  3868. datetree. For example, to store archived items directly in the
  3869. datetree, use \"~/org/datetree.org::datetree/\".
  3870. \"basement::** Finished Tasks\"
  3871. Archive in file ./basement (relative path), as level 3 trees
  3872. below the level 2 heading \"** Finished Tasks\".
  3873. You may set this option on a per-file basis by adding to the buffer a
  3874. line like
  3875. #+ARCHIVE: basement::** Finished Tasks
  3876. You may also define it locally for a subtree by setting an ARCHIVE property
  3877. in the entry. If such a property is found in an entry, or anywhere up
  3878. the hierarchy, it will be used."
  3879. :group 'org-archive
  3880. :type 'string)
  3881. (defcustom org-archive-tag "ARCHIVE"
  3882. "The tag that marks a subtree as archived.
  3883. An archived subtree does not open during visibility cycling, and does
  3884. not contribute to the agenda listings.
  3885. After changing this, font-lock must be restarted in the relevant buffers to
  3886. get the proper fontification."
  3887. :group 'org-archive
  3888. :group 'org-keywords
  3889. :type 'string)
  3890. (defcustom org-agenda-skip-archived-trees t
  3891. "Non-nil means the agenda will skip any items located in archived trees.
  3892. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3893. variable is no longer recommended, you should leave it at the value t.
  3894. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3895. :group 'org-archive
  3896. :group 'org-agenda-skip
  3897. :type 'boolean)
  3898. (defcustom org-columns-skip-archived-trees t
  3899. "Non-nil means ignore archived trees when creating column view."
  3900. :group 'org-archive
  3901. :group 'org-properties
  3902. :type 'boolean)
  3903. (defcustom org-cycle-open-archived-trees nil
  3904. "Non-nil means `org-cycle' will open archived trees.
  3905. An archived tree is a tree marked with the tag ARCHIVE.
  3906. When nil, archived trees will stay folded. You can still open them with
  3907. normal outline commands like `show-all', but not with the cycling commands."
  3908. :group 'org-archive
  3909. :group 'org-cycle
  3910. :type 'boolean)
  3911. (defcustom org-sparse-tree-open-archived-trees nil
  3912. "Non-nil means sparse tree construction shows matches in archived trees.
  3913. When nil, matches in these trees are highlighted, but the trees are kept in
  3914. collapsed state."
  3915. :group 'org-archive
  3916. :group 'org-sparse-trees
  3917. :type 'boolean)
  3918. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3919. "The default date type when building a sparse tree.
  3920. When this is nil, a date is a scheduled or a deadline timestamp.
  3921. Otherwise, these types are allowed:
  3922. all: all timestamps
  3923. active: only active timestamps (<...>)
  3924. inactive: only inactive timestamps (<...)
  3925. scheduled: only scheduled timestamps
  3926. deadline: only deadline timestamps"
  3927. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3928. (const :tag "All timestamps" all)
  3929. (const :tag "Only active timestamps" active)
  3930. (const :tag "Only inactive timestamps" inactive)
  3931. (const :tag "Only scheduled timestamps" scheduled)
  3932. (const :tag "Only deadline timestamps" deadline)
  3933. (const :tag "Only closed timestamps" closed))
  3934. :version "24.3"
  3935. :group 'org-sparse-trees)
  3936. (defun org-cycle-hide-archived-subtrees (state)
  3937. "Re-hide all archived subtrees after a visibility state change."
  3938. (when (and (not org-cycle-open-archived-trees)
  3939. (not (memq state '(overview folded))))
  3940. (save-excursion
  3941. (let* ((globalp (memq state '(contents all)))
  3942. (beg (if globalp (point-min) (point)))
  3943. (end (if globalp (point-max) (org-end-of-subtree t))))
  3944. (org-hide-archived-subtrees beg end)
  3945. (goto-char beg)
  3946. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3947. (message "%s" (substitute-command-keys
  3948. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3949. (defun org-force-cycle-archived ()
  3950. "Cycle subtree even if it is archived."
  3951. (interactive)
  3952. (setq this-command 'org-cycle)
  3953. (let ((org-cycle-open-archived-trees t))
  3954. (call-interactively 'org-cycle)))
  3955. (defun org-hide-archived-subtrees (beg end)
  3956. "Re-hide all archived subtrees after a visibility state change."
  3957. (save-excursion
  3958. (let* ((re (concat ":" org-archive-tag ":")))
  3959. (goto-char beg)
  3960. (while (re-search-forward re end t)
  3961. (when (org-at-heading-p)
  3962. (org-flag-subtree t)
  3963. (org-end-of-subtree t))))))
  3964. (declare-function outline-end-of-heading "outline" ())
  3965. (declare-function outline-flag-region "outline" (from to flag))
  3966. (defun org-flag-subtree (flag)
  3967. (save-excursion
  3968. (org-back-to-heading t)
  3969. (outline-end-of-heading)
  3970. (outline-flag-region (point)
  3971. (progn (org-end-of-subtree t) (point))
  3972. flag)))
  3973. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3974. (eval-and-compile
  3975. (org-autoload "org-archive"
  3976. '(org-add-archive-files)))
  3977. ;; Autoload Column View Code
  3978. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3979. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3980. (declare-function org-columns-compute "org-colview" (property))
  3981. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3982. '(org-columns-number-to-string
  3983. org-columns-get-format-and-top-level
  3984. org-columns-compute
  3985. org-columns-remove-overlays))
  3986. ;; Autoload ID code
  3987. (declare-function org-id-store-link "org-id")
  3988. (declare-function org-id-locations-load "org-id")
  3989. (declare-function org-id-locations-save "org-id")
  3990. (defvar org-id-track-globally)
  3991. (org-autoload "org-id"
  3992. '(org-id-new
  3993. org-id-copy
  3994. org-id-get-with-outline-path-completion
  3995. org-id-get-with-outline-drilling))
  3996. ;;; Variables for pre-computed regular expressions, all buffer local
  3997. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3998. "Matches first line of a hidden block.")
  3999. (make-variable-buffer-local 'org-drawer-regexp)
  4000. (defvar org-todo-regexp nil
  4001. "Matches any of the TODO state keywords.")
  4002. (make-variable-buffer-local 'org-todo-regexp)
  4003. (defvar org-not-done-regexp nil
  4004. "Matches any of the TODO state keywords except the last one.")
  4005. (make-variable-buffer-local 'org-not-done-regexp)
  4006. (defvar org-not-done-heading-regexp nil
  4007. "Matches a TODO headline that is not done.")
  4008. (make-variable-buffer-local 'org-not-done-regexp)
  4009. (defvar org-todo-line-regexp nil
  4010. "Matches a headline and puts TODO state into group 2 if present.")
  4011. (make-variable-buffer-local 'org-todo-line-regexp)
  4012. (defvar org-complex-heading-regexp nil
  4013. "Matches a headline and puts everything into groups:
  4014. group 1: the stars
  4015. group 2: The todo keyword, maybe
  4016. group 3: Priority cookie
  4017. group 4: True headline
  4018. group 5: Tags")
  4019. (make-variable-buffer-local 'org-complex-heading-regexp)
  4020. (defvar org-complex-heading-regexp-format nil
  4021. "Printf format to make regexp to match an exact headline.
  4022. This regexp will match the headline of any node which has the
  4023. exact headline text that is put into the format, but may have any
  4024. TODO state, priority and tags.")
  4025. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4026. (defvar org-todo-line-tags-regexp nil
  4027. "Matches a headline and puts TODO state into group 2 if present.
  4028. Also put tags into group 4 if tags are present.")
  4029. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4030. (defvar org-ds-keyword-length 12
  4031. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4032. (make-variable-buffer-local 'org-ds-keyword-length)
  4033. (defvar org-deadline-regexp nil
  4034. "Matches the DEADLINE keyword.")
  4035. (make-variable-buffer-local 'org-deadline-regexp)
  4036. (defvar org-deadline-time-regexp nil
  4037. "Matches the DEADLINE keyword together with a time stamp.")
  4038. (make-variable-buffer-local 'org-deadline-time-regexp)
  4039. (defvar org-deadline-time-hour-regexp nil
  4040. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4041. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4042. (defvar org-deadline-line-regexp nil
  4043. "Matches the DEADLINE keyword and the rest of the line.")
  4044. (make-variable-buffer-local 'org-deadline-line-regexp)
  4045. (defvar org-scheduled-regexp nil
  4046. "Matches the SCHEDULED keyword.")
  4047. (make-variable-buffer-local 'org-scheduled-regexp)
  4048. (defvar org-scheduled-time-regexp nil
  4049. "Matches the SCHEDULED keyword together with a time stamp.")
  4050. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4051. (defvar org-scheduled-time-hour-regexp nil
  4052. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4053. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4054. (defvar org-closed-time-regexp nil
  4055. "Matches the CLOSED keyword together with a time stamp.")
  4056. (make-variable-buffer-local 'org-closed-time-regexp)
  4057. (defvar org-keyword-time-regexp nil
  4058. "Matches any of the 4 keywords, together with the time stamp.")
  4059. (make-variable-buffer-local 'org-keyword-time-regexp)
  4060. (defvar org-keyword-time-not-clock-regexp nil
  4061. "Matches any of the 3 keywords, together with the time stamp.")
  4062. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4063. (defvar org-maybe-keyword-time-regexp nil
  4064. "Matches a timestamp, possibly preceded by a keyword.")
  4065. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4066. (defvar org-all-time-keywords nil
  4067. "List of time keywords.")
  4068. (make-variable-buffer-local 'org-all-time-keywords)
  4069. (defconst org-plain-time-of-day-regexp
  4070. (concat
  4071. "\\(\\<[012]?[0-9]"
  4072. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4073. "\\(--?"
  4074. "\\(\\<[012]?[0-9]"
  4075. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4076. "\\)?")
  4077. "Regular expression to match a plain time or time range.
  4078. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4079. groups carry important information:
  4080. 0 the full match
  4081. 1 the first time, range or not
  4082. 8 the second time, if it is a range.")
  4083. (defconst org-plain-time-extension-regexp
  4084. (concat
  4085. "\\(\\<[012]?[0-9]"
  4086. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4087. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4088. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4089. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4090. groups carry important information:
  4091. 0 the full match
  4092. 7 hours of duration
  4093. 9 minutes of duration")
  4094. (defconst org-stamp-time-of-day-regexp
  4095. (concat
  4096. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4097. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4098. "\\(--?"
  4099. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4100. "Regular expression to match a timestamp time or time range.
  4101. After a match, the following groups carry important information:
  4102. 0 the full match
  4103. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4104. 2 the first time, range or not
  4105. 4 the second time, if it is a range.")
  4106. (defconst org-startup-options
  4107. '(("fold" org-startup-folded t)
  4108. ("overview" org-startup-folded t)
  4109. ("nofold" org-startup-folded nil)
  4110. ("showall" org-startup-folded nil)
  4111. ("showeverything" org-startup-folded showeverything)
  4112. ("content" org-startup-folded content)
  4113. ("indent" org-startup-indented t)
  4114. ("noindent" org-startup-indented nil)
  4115. ("hidestars" org-hide-leading-stars t)
  4116. ("showstars" org-hide-leading-stars nil)
  4117. ("odd" org-odd-levels-only t)
  4118. ("oddeven" org-odd-levels-only nil)
  4119. ("align" org-startup-align-all-tables t)
  4120. ("noalign" org-startup-align-all-tables nil)
  4121. ("inlineimages" org-startup-with-inline-images t)
  4122. ("noinlineimages" org-startup-with-inline-images nil)
  4123. ("latexpreview" org-startup-with-latex-preview t)
  4124. ("nolatexpreview" org-startup-with-latex-preview nil)
  4125. ("customtime" org-display-custom-times t)
  4126. ("logdone" org-log-done time)
  4127. ("lognotedone" org-log-done note)
  4128. ("nologdone" org-log-done nil)
  4129. ("lognoteclock-out" org-log-note-clock-out t)
  4130. ("nolognoteclock-out" org-log-note-clock-out nil)
  4131. ("logrepeat" org-log-repeat state)
  4132. ("lognoterepeat" org-log-repeat note)
  4133. ("logdrawer" org-log-into-drawer t)
  4134. ("nologdrawer" org-log-into-drawer nil)
  4135. ("logstatesreversed" org-log-states-order-reversed t)
  4136. ("nologstatesreversed" org-log-states-order-reversed nil)
  4137. ("nologrepeat" org-log-repeat nil)
  4138. ("logreschedule" org-log-reschedule time)
  4139. ("lognotereschedule" org-log-reschedule note)
  4140. ("nologreschedule" org-log-reschedule nil)
  4141. ("logredeadline" org-log-redeadline time)
  4142. ("lognoteredeadline" org-log-redeadline note)
  4143. ("nologredeadline" org-log-redeadline nil)
  4144. ("logrefile" org-log-refile time)
  4145. ("lognoterefile" org-log-refile note)
  4146. ("nologrefile" org-log-refile nil)
  4147. ("fninline" org-footnote-define-inline t)
  4148. ("nofninline" org-footnote-define-inline nil)
  4149. ("fnlocal" org-footnote-section nil)
  4150. ("fnauto" org-footnote-auto-label t)
  4151. ("fnprompt" org-footnote-auto-label nil)
  4152. ("fnconfirm" org-footnote-auto-label confirm)
  4153. ("fnplain" org-footnote-auto-label plain)
  4154. ("fnadjust" org-footnote-auto-adjust t)
  4155. ("nofnadjust" org-footnote-auto-adjust nil)
  4156. ("constcgs" constants-unit-system cgs)
  4157. ("constSI" constants-unit-system SI)
  4158. ("noptag" org-tag-persistent-alist nil)
  4159. ("hideblocks" org-hide-block-startup t)
  4160. ("nohideblocks" org-hide-block-startup nil)
  4161. ("beamer" org-startup-with-beamer-mode t)
  4162. ("entitiespretty" org-pretty-entities t)
  4163. ("entitiesplain" org-pretty-entities nil))
  4164. "Variable associated with STARTUP options for org-mode.
  4165. Each element is a list of three items: the startup options (as written
  4166. in the #+STARTUP line), the corresponding variable, and the value to set
  4167. this variable to if the option is found. An optional forth element PUSH
  4168. means to push this value onto the list in the variable.")
  4169. (defun org-update-property-plist (key val props)
  4170. "Update PROPS with KEY and VAL."
  4171. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4172. (key (if appending (substring key 0 (- (length key) 1)) key))
  4173. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4174. (previous (cdr (assoc key props))))
  4175. (if appending
  4176. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4177. (cons (cons key val) remainder))))
  4178. (defconst org-block-regexp
  4179. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4180. "Regular expression for hiding blocks.")
  4181. (defconst org-heading-keyword-regexp-format
  4182. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4183. "Printf format for a regexp matching a headline with some keyword.
  4184. This regexp will match the headline of any node which has the
  4185. exact keyword that is put into the format. The keyword isn't in
  4186. any group by default, but the stars and the body are.")
  4187. (defconst org-heading-keyword-maybe-regexp-format
  4188. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4189. "Printf format for a regexp matching a headline, possibly with some keyword.
  4190. This regexp can match any headline with the specified keyword, or
  4191. without a keyword. The keyword isn't in any group by default,
  4192. but the stars and the body are.")
  4193. (defcustom org-group-tags t
  4194. "When non-nil (the default), use group tags.
  4195. This can be turned on/off through `org-toggle-tags-groups'."
  4196. :group 'org-tags
  4197. :group 'org-startup
  4198. :type 'boolean)
  4199. (defun org-toggle-tags-groups ()
  4200. "Toggle support for group tags.
  4201. Support for group tags is controlled by the option
  4202. `org-group-tags', which is non-nil by default."
  4203. (interactive)
  4204. (setq org-group-tags (not org-group-tags))
  4205. (cond ((and (derived-mode-p 'org-agenda-mode)
  4206. org-group-tags)
  4207. (org-agenda-redo))
  4208. ((derived-mode-p 'org-mode)
  4209. (let ((org-inhibit-startup t)) (org-mode))))
  4210. (message "Groups tags support has been turned %s"
  4211. (if org-group-tags "on" "off")))
  4212. (defun org-set-regexps-and-options-for-tags ()
  4213. "Precompute variables used for tags."
  4214. (when (derived-mode-p 'org-mode)
  4215. (org-set-local 'org-file-tags nil)
  4216. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4217. (splitre "[ \t]+")
  4218. (start 0)
  4219. tags ftags key value)
  4220. (save-excursion
  4221. (save-restriction
  4222. (widen)
  4223. (goto-char (point-min))
  4224. (while (re-search-forward re nil t)
  4225. (setq key (upcase (org-match-string-no-properties 1))
  4226. value (org-match-string-no-properties 2))
  4227. (if (stringp value) (setq value (org-trim value)))
  4228. (cond
  4229. ((equal key "TAGS")
  4230. (setq tags (append tags (if tags '("\\n") nil)
  4231. (org-split-string value splitre))))
  4232. ((equal key "FILETAGS")
  4233. (when (string-match "\\S-" value)
  4234. (setq ftags
  4235. (append
  4236. ftags
  4237. (apply 'append
  4238. (mapcar (lambda (x) (org-split-string x ":"))
  4239. (org-split-string value)))))))))))
  4240. ;; Process the file tags.
  4241. (and ftags (org-set-local 'org-file-tags
  4242. (mapcar 'org-add-prop-inherited ftags)))
  4243. (org-set-local 'org-tag-groups-alist nil)
  4244. ;; Process the tags.
  4245. (when (and (not tags) org-tag-alist)
  4246. (setq tags
  4247. (mapcar
  4248. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4249. ((eq (car tg) :endgroup) "}")
  4250. ((eq (car tg) :grouptags) ":")
  4251. ((eq (car tg) :newline) "\n")
  4252. (t (concat (car tg)
  4253. (if (characterp (cdr tg))
  4254. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4255. org-tag-alist)))
  4256. (let (e tgs g)
  4257. (while (setq e (pop tags))
  4258. (cond
  4259. ((equal e "{")
  4260. (progn (push '(:startgroup) tgs)
  4261. (when (equal (nth 1 tags) ":")
  4262. (push (list (replace-regexp-in-string
  4263. "(.+)$" "" (nth 0 tags)))
  4264. org-tag-groups-alist)
  4265. (setq g 0))))
  4266. ((equal e ":") (push '(:grouptags) tgs))
  4267. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4268. ((equal e "\\n") (push '(:newline) tgs))
  4269. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4270. (push (cons (match-string 1 e)
  4271. (string-to-char (match-string 2 e))) tgs)
  4272. (if (and g (> g 0))
  4273. (setcar org-tag-groups-alist
  4274. (append (car org-tag-groups-alist)
  4275. (list (match-string 1 e)))))
  4276. (if g (setq g (1+ g))))
  4277. (t (push (list e) tgs)
  4278. (if (and g (> g 0))
  4279. (setcar org-tag-groups-alist
  4280. (append (car org-tag-groups-alist) (list e))))
  4281. (if g (setq g (1+ g))))))
  4282. (org-set-local 'org-tag-alist nil)
  4283. (while (setq e (pop tgs))
  4284. (or (and (stringp (car e))
  4285. (assoc (car e) org-tag-alist))
  4286. (push e org-tag-alist)))
  4287. ;; Return a list with tag variables
  4288. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4289. (defvar org-ota nil)
  4290. (defun org-set-regexps-and-options ()
  4291. "Precompute regular expressions used in the current buffer."
  4292. (when (derived-mode-p 'org-mode)
  4293. (org-set-local 'org-todo-kwd-alist nil)
  4294. (org-set-local 'org-todo-key-alist nil)
  4295. (org-set-local 'org-todo-key-trigger nil)
  4296. (org-set-local 'org-todo-keywords-1 nil)
  4297. (org-set-local 'org-done-keywords nil)
  4298. (org-set-local 'org-todo-heads nil)
  4299. (org-set-local 'org-todo-sets nil)
  4300. (org-set-local 'org-todo-log-states nil)
  4301. (org-set-local 'org-file-properties nil)
  4302. (let ((re (org-make-options-regexp
  4303. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4304. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4305. "SETUPFILE" "OPTIONS")
  4306. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4307. (splitre "[ \t]+")
  4308. (scripts org-use-sub-superscripts)
  4309. kwds kws0 kwsa key log value cat arch const links hw dws
  4310. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4311. (start 0))
  4312. (save-excursion
  4313. (save-restriction
  4314. (widen)
  4315. (goto-char (point-min))
  4316. (while
  4317. (or (and
  4318. ext-setup-or-nil
  4319. (not org-ota)
  4320. (let (ret)
  4321. (with-temp-buffer
  4322. (insert ext-setup-or-nil)
  4323. (let ((major-mode 'org-mode) org-ota)
  4324. (setq ret (save-match-data
  4325. (org-set-regexps-and-options-for-tags)))))
  4326. ;; Append setupfile tags to existing tags
  4327. (setq org-ota t)
  4328. (setq org-file-tags
  4329. (delq nil (append org-file-tags (nth 0 ret)))
  4330. org-tag-alist
  4331. (delq nil (append org-tag-alist (nth 1 ret)))
  4332. org-tag-groups-alist
  4333. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4334. (and ext-setup-or-nil
  4335. (string-match re ext-setup-or-nil start)
  4336. (setq start (match-end 0)))
  4337. (and (setq ext-setup-or-nil nil start 0)
  4338. (re-search-forward re nil t)))
  4339. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4340. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4341. (if (stringp value) (setq value (org-trim value)))
  4342. (cond
  4343. ((equal key "CATEGORY")
  4344. (setq cat value))
  4345. ((member key '("SEQ_TODO" "TODO"))
  4346. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4347. ((equal key "TYP_TODO")
  4348. (push (cons 'type (org-split-string value splitre)) kwds))
  4349. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4350. ;; general TODO-like setup
  4351. (push (cons (intern (downcase (match-string 1 key)))
  4352. (org-split-string value splitre)) kwds))
  4353. ((equal key "COLUMNS")
  4354. (org-set-local 'org-columns-default-format value))
  4355. ((equal key "LINK")
  4356. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4357. (push (cons (match-string 1 value)
  4358. (org-trim (match-string 2 value)))
  4359. links)))
  4360. ((equal key "PRIORITIES")
  4361. (setq prio (org-split-string value " +")))
  4362. ((equal key "PROPERTY")
  4363. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4364. (setq props (org-update-property-plist (match-string 1 value)
  4365. (match-string 2 value)
  4366. props))))
  4367. ((equal key "DRAWERS")
  4368. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4369. ((equal key "CONSTANTS")
  4370. (org-table-set-constants))
  4371. ((equal key "STARTUP")
  4372. (let ((opts (org-split-string value splitre))
  4373. l var val)
  4374. (while (setq l (pop opts))
  4375. (when (setq l (assoc l org-startup-options))
  4376. (setq var (nth 1 l) val (nth 2 l))
  4377. (if (not (nth 3 l))
  4378. (set (make-local-variable var) val)
  4379. (if (not (listp (symbol-value var)))
  4380. (set (make-local-variable var) nil))
  4381. (set (make-local-variable var) (symbol-value var))
  4382. (add-to-list var val))))))
  4383. ((equal key "ARCHIVE")
  4384. (setq arch value)
  4385. (remove-text-properties 0 (length arch)
  4386. '(face t fontified t) arch))
  4387. ((equal key "OPTIONS")
  4388. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4389. (setq scripts (read (match-string 2 value)))))
  4390. ((and (equal key "SETUPFILE")
  4391. ;; Prevent checking in Gnus messages
  4392. (not buffer-read-only))
  4393. (setq setup-contents (org-file-contents
  4394. (expand-file-name
  4395. (org-remove-double-quotes value))
  4396. 'noerror))
  4397. (if (not ext-setup-or-nil)
  4398. (setq ext-setup-or-nil setup-contents start 0)
  4399. (setq ext-setup-or-nil
  4400. (concat (substring ext-setup-or-nil 0 start)
  4401. "\n" setup-contents "\n"
  4402. (substring ext-setup-or-nil start)))))))
  4403. ;; search for property blocks
  4404. (goto-char (point-min))
  4405. (while (re-search-forward org-block-regexp nil t)
  4406. (when (equal "PROPERTY" (upcase (match-string 1)))
  4407. (setq value (replace-regexp-in-string
  4408. "[\n\r]" " " (match-string 4)))
  4409. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4410. (setq props (org-update-property-plist (match-string 1 value)
  4411. (match-string 2 value)
  4412. props)))))))
  4413. (org-set-local 'org-use-sub-superscripts scripts)
  4414. (when cat
  4415. (org-set-local 'org-category (intern cat))
  4416. (push (cons "CATEGORY" cat) props))
  4417. (when prio
  4418. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4419. (setq prio (mapcar 'string-to-char prio))
  4420. (org-set-local 'org-highest-priority (nth 0 prio))
  4421. (org-set-local 'org-lowest-priority (nth 1 prio))
  4422. (org-set-local 'org-default-priority (nth 2 prio)))
  4423. (and props (org-set-local 'org-file-properties (nreverse props)))
  4424. (and drawers (org-set-local 'org-drawers drawers))
  4425. (and arch (org-set-local 'org-archive-location arch))
  4426. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4427. ;; Process the TODO keywords
  4428. (unless kwds
  4429. ;; Use the global values as if they had been given locally.
  4430. (setq kwds (default-value 'org-todo-keywords))
  4431. (if (stringp (car kwds))
  4432. (setq kwds (list (cons org-todo-interpretation
  4433. (default-value 'org-todo-keywords)))))
  4434. (setq kwds (reverse kwds)))
  4435. (setq kwds (nreverse kwds))
  4436. (let (inter kws kw)
  4437. (while (setq kws (pop kwds))
  4438. (let ((kws (or
  4439. (run-hook-with-args-until-success
  4440. 'org-todo-setup-filter-hook kws)
  4441. kws)))
  4442. (setq inter (pop kws) sep (member "|" kws)
  4443. kws0 (delete "|" (copy-sequence kws))
  4444. kwsa nil
  4445. kws1 (mapcar
  4446. (lambda (x)
  4447. ;; 1 2
  4448. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4449. (progn
  4450. (setq kw (match-string 1 x)
  4451. key (and (match-end 2) (match-string 2 x))
  4452. log (org-extract-log-state-settings x))
  4453. (push (cons kw (and key (string-to-char key))) kwsa)
  4454. (and log (push log org-todo-log-states))
  4455. kw)
  4456. (error "Invalid TODO keyword %s" x)))
  4457. kws0)
  4458. kwsa (if kwsa (append '((:startgroup))
  4459. (nreverse kwsa)
  4460. '((:endgroup))))
  4461. hw (car kws1)
  4462. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4463. tail (list inter hw (car dws) (org-last dws))))
  4464. (add-to-list 'org-todo-heads hw 'append)
  4465. (push kws1 org-todo-sets)
  4466. (setq org-done-keywords (append org-done-keywords dws nil))
  4467. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4468. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4469. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4470. (setq org-todo-sets (nreverse org-todo-sets)
  4471. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4472. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4473. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4474. ;; Compute the regular expressions and other local variables.
  4475. ;; Using `org-outline-regexp-bol' would complicate them much,
  4476. ;; because of the fixed white space at the end of that string.
  4477. (if (not org-done-keywords)
  4478. (setq org-done-keywords (and org-todo-keywords-1
  4479. (list (org-last org-todo-keywords-1)))))
  4480. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4481. (length org-scheduled-string)
  4482. (length org-clock-string)
  4483. (length org-closed-string)))
  4484. org-drawer-regexp
  4485. (concat "^[ \t]*:\\("
  4486. (mapconcat 'regexp-quote org-drawers "\\|")
  4487. "\\):[ \t]*$")
  4488. org-not-done-keywords
  4489. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4490. org-todo-regexp
  4491. (concat "\\("
  4492. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4493. "\\)")
  4494. org-not-done-regexp
  4495. (concat "\\("
  4496. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4497. "\\)")
  4498. org-not-done-heading-regexp
  4499. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4500. org-todo-line-regexp
  4501. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4502. org-complex-heading-regexp
  4503. (concat "^\\(\\*+\\)"
  4504. "\\(?: +" org-todo-regexp "\\)?"
  4505. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4506. "\\(?: +\\(.*?\\)\\)??"
  4507. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4508. "[ \t]*$")
  4509. org-complex-heading-regexp-format
  4510. (concat "^\\(\\*+\\)"
  4511. "\\(?: +" org-todo-regexp "\\)?"
  4512. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4513. "\\(?: +"
  4514. ;; Stats cookies can be stuck to body.
  4515. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4516. "\\(%s\\)"
  4517. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4518. "\\)"
  4519. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4520. "[ \t]*$")
  4521. org-todo-line-tags-regexp
  4522. (concat "^\\(\\*+\\)"
  4523. "\\(?: +" org-todo-regexp "\\)?"
  4524. "\\(?: +\\(.*?\\)\\)??"
  4525. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4526. "[ \t]*$")
  4527. org-deadline-regexp (concat "\\<" org-deadline-string)
  4528. org-deadline-time-regexp
  4529. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4530. org-deadline-time-hour-regexp
  4531. (concat "\\<" org-deadline-string
  4532. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4533. org-deadline-line-regexp
  4534. (concat "\\<\\(" org-deadline-string "\\).*")
  4535. org-scheduled-regexp
  4536. (concat "\\<" org-scheduled-string)
  4537. org-scheduled-time-regexp
  4538. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4539. org-scheduled-time-hour-regexp
  4540. (concat "\\<" org-scheduled-string
  4541. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4542. org-closed-time-regexp
  4543. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4544. org-keyword-time-regexp
  4545. (concat "\\<\\(" org-scheduled-string
  4546. "\\|" org-deadline-string
  4547. "\\|" org-closed-string
  4548. "\\|" org-clock-string "\\)"
  4549. " *[[<]\\([^]>]+\\)[]>]")
  4550. org-keyword-time-not-clock-regexp
  4551. (concat "\\<\\(" org-scheduled-string
  4552. "\\|" org-deadline-string
  4553. "\\|" org-closed-string
  4554. "\\)"
  4555. " *[[<]\\([^]>]+\\)[]>]")
  4556. org-maybe-keyword-time-regexp
  4557. (concat "\\(\\<\\(" org-scheduled-string
  4558. "\\|" org-deadline-string
  4559. "\\|" org-closed-string
  4560. "\\|" org-clock-string "\\)\\)?"
  4561. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4562. org-all-time-keywords
  4563. (mapcar (lambda (w) (substring w 0 -1))
  4564. (list org-scheduled-string org-deadline-string
  4565. org-clock-string org-closed-string)))
  4566. (setq org-ota nil)
  4567. (org-compute-latex-and-related-regexp))))
  4568. (defun org-file-contents (file &optional noerror)
  4569. "Return the contents of FILE, as a string."
  4570. (if (or (not file)
  4571. (not (file-readable-p file)))
  4572. (if noerror
  4573. (message "Cannot read file \"%s\"" file)
  4574. (error "Cannot read file \"%s\"" file))
  4575. (with-temp-buffer
  4576. (insert-file-contents file)
  4577. (buffer-string))))
  4578. (defun org-extract-log-state-settings (x)
  4579. "Extract the log state setting from a TODO keyword string.
  4580. This will extract info from a string like \"WAIT(w@/!)\"."
  4581. (let (kw key log1 log2)
  4582. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4583. (setq kw (match-string 1 x)
  4584. key (and (match-end 2) (match-string 2 x))
  4585. log1 (and (match-end 3) (match-string 3 x))
  4586. log2 (and (match-end 4) (match-string 4 x)))
  4587. (and (or log1 log2)
  4588. (list kw
  4589. (and log1 (if (equal log1 "!") 'time 'note))
  4590. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4591. (defun org-remove-keyword-keys (list)
  4592. "Remove a pair of parenthesis at the end of each string in LIST."
  4593. (mapcar (lambda (x)
  4594. (if (string-match "(.*)$" x)
  4595. (substring x 0 (match-beginning 0))
  4596. x))
  4597. list))
  4598. (defun org-assign-fast-keys (alist)
  4599. "Assign fast keys to a keyword-key alist.
  4600. Respect keys that are already there."
  4601. (let (new e (alt ?0))
  4602. (while (setq e (pop alist))
  4603. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4604. (cdr e)) ;; Key already assigned.
  4605. (push e new)
  4606. (let ((clist (string-to-list (downcase (car e))))
  4607. (used (append new alist)))
  4608. (when (= (car clist) ?@)
  4609. (pop clist))
  4610. (while (and clist (rassoc (car clist) used))
  4611. (pop clist))
  4612. (unless clist
  4613. (while (rassoc alt used)
  4614. (incf alt)))
  4615. (push (cons (car e) (or (car clist) alt)) new))))
  4616. (nreverse new)))
  4617. ;;; Some variables used in various places
  4618. (defvar org-window-configuration nil
  4619. "Used in various places to store a window configuration.")
  4620. (defvar org-selected-window nil
  4621. "Used in various places to store a window configuration.")
  4622. (defvar org-finish-function nil
  4623. "Function to be called when `C-c C-c' is used.
  4624. This is for getting out of special buffers like capture.")
  4625. ;; FIXME: Occasionally check by commenting these, to make sure
  4626. ;; no other functions uses these, forgetting to let-bind them.
  4627. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4628. (defvar org-last-state)
  4629. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4630. ;; Defined somewhere in this file, but used before definition.
  4631. (defvar org-entities) ;; defined in org-entities.el
  4632. (defvar org-struct-menu)
  4633. (defvar org-org-menu)
  4634. (defvar org-tbl-menu)
  4635. ;;;; Define the Org-mode
  4636. ;; We use a before-change function to check if a table might need
  4637. ;; an update.
  4638. (defvar org-table-may-need-update t
  4639. "Indicates that a table might need an update.
  4640. This variable is set by `org-before-change-function'.
  4641. `org-table-align' sets it back to nil.")
  4642. (defun org-before-change-function (beg end)
  4643. "Every change indicates that a table might need an update."
  4644. (setq org-table-may-need-update t))
  4645. (defvar org-mode-map)
  4646. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4647. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4648. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4649. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4650. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4651. (defvar org-table-buffer-is-an nil)
  4652. (defvar bidi-paragraph-direction)
  4653. (defvar buffer-face-mode-face)
  4654. (require 'outline)
  4655. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4656. (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"))
  4657. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4658. ;; Other stuff we need.
  4659. (require 'time-date)
  4660. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4661. (require 'easymenu)
  4662. (require 'overlay)
  4663. ;; (require 'org-macs) moved higher up in the file before it is first used
  4664. (require 'org-entities)
  4665. ;; (require 'org-compat) moved higher up in the file before it is first used
  4666. (require 'org-faces)
  4667. (require 'org-list)
  4668. (require 'org-pcomplete)
  4669. (require 'org-src)
  4670. (require 'org-footnote)
  4671. (require 'org-macro)
  4672. ;; babel
  4673. (require 'ob)
  4674. ;;;###autoload
  4675. (define-derived-mode org-mode outline-mode "Org"
  4676. "Outline-based notes management and organizer, alias
  4677. \"Carsten's outline-mode for keeping track of everything.\"
  4678. Org-mode develops organizational tasks around a NOTES file which
  4679. contains information about projects as plain text. Org-mode is
  4680. implemented on top of outline-mode, which is ideal to keep the content
  4681. of large files well structured. It supports ToDo items, deadlines and
  4682. time stamps, which magically appear in the diary listing of the Emacs
  4683. calendar. Tables are easily created with a built-in table editor.
  4684. Plain text URL-like links connect to websites, emails (VM), Usenet
  4685. messages (Gnus), BBDB entries, and any files related to the project.
  4686. For printing and sharing of notes, an Org-mode file (or a part of it)
  4687. can be exported as a structured ASCII or HTML file.
  4688. The following commands are available:
  4689. \\{org-mode-map}"
  4690. ;; Get rid of Outline menus, they are not needed
  4691. ;; Need to do this here because define-derived-mode sets up
  4692. ;; the keymap so late. Still, it is a waste to call this each time
  4693. ;; we switch another buffer into org-mode.
  4694. (if (featurep 'xemacs)
  4695. (when (boundp 'outline-mode-menu-heading)
  4696. ;; Assume this is Greg's port, it uses easymenu
  4697. (easy-menu-remove outline-mode-menu-heading)
  4698. (easy-menu-remove outline-mode-menu-show)
  4699. (easy-menu-remove outline-mode-menu-hide))
  4700. (define-key org-mode-map [menu-bar headings] 'undefined)
  4701. (define-key org-mode-map [menu-bar hide] 'undefined)
  4702. (define-key org-mode-map [menu-bar show] 'undefined))
  4703. (org-load-modules-maybe)
  4704. (easy-menu-add org-org-menu)
  4705. (easy-menu-add org-tbl-menu)
  4706. (org-install-agenda-files-menu)
  4707. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4708. (add-to-invisibility-spec '(org-cwidth))
  4709. (add-to-invisibility-spec '(org-hide-block . t))
  4710. (when (featurep 'xemacs)
  4711. (org-set-local 'line-move-ignore-invisible t))
  4712. (org-set-local 'outline-regexp org-outline-regexp)
  4713. (org-set-local 'outline-level 'org-outline-level)
  4714. (setq bidi-paragraph-direction 'left-to-right)
  4715. (when (and org-ellipsis
  4716. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4717. (fboundp 'make-glyph-code))
  4718. (unless org-display-table
  4719. (setq org-display-table (make-display-table)))
  4720. (set-display-table-slot
  4721. org-display-table 4
  4722. (vconcat (mapcar
  4723. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4724. org-ellipsis)))
  4725. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4726. (setq buffer-display-table org-display-table))
  4727. (org-set-regexps-and-options-for-tags)
  4728. (org-set-regexps-and-options)
  4729. (org-set-font-lock-defaults)
  4730. (when (and org-tag-faces (not org-tags-special-faces-re))
  4731. ;; tag faces set outside customize.... force initialization.
  4732. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4733. ;; Calc embedded
  4734. (org-set-local 'calc-embedded-open-mode "# ")
  4735. ;; Modify a few syntax entries
  4736. (modify-syntax-entry ?@ "w")
  4737. (modify-syntax-entry ?\" "\"")
  4738. (if org-startup-truncated (setq truncate-lines t))
  4739. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4740. (org-set-local 'font-lock-unfontify-region-function
  4741. 'org-unfontify-region)
  4742. ;; Activate before-change-function
  4743. (org-set-local 'org-table-may-need-update t)
  4744. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4745. 'local)
  4746. ;; Check for running clock before killing a buffer
  4747. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4748. ;; Initialize macros templates.
  4749. (org-macro-initialize-templates)
  4750. ;; Initialize radio targets.
  4751. (org-update-radio-target-regexp)
  4752. ;; Indentation.
  4753. (org-set-local 'indent-line-function 'org-indent-line)
  4754. (org-set-local 'indent-region-function 'org-indent-region)
  4755. ;; Filling and auto-filling.
  4756. (org-setup-filling)
  4757. ;; Comments.
  4758. (org-setup-comments-handling)
  4759. ;; Beginning/end of defun
  4760. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4761. (org-set-local 'end-of-defun-function 'org-forward-element)
  4762. ;; Next error for sparse trees
  4763. (org-set-local 'next-error-function 'org-occur-next-match)
  4764. ;; Make sure dependence stuff works reliably, even for users who set it
  4765. ;; too late :-(
  4766. (if org-enforce-todo-dependencies
  4767. (add-hook 'org-blocker-hook
  4768. 'org-block-todo-from-children-or-siblings-or-parent)
  4769. (remove-hook 'org-blocker-hook
  4770. 'org-block-todo-from-children-or-siblings-or-parent))
  4771. (if org-enforce-todo-checkbox-dependencies
  4772. (add-hook 'org-blocker-hook
  4773. 'org-block-todo-from-checkboxes)
  4774. (remove-hook 'org-blocker-hook
  4775. 'org-block-todo-from-checkboxes))
  4776. ;; Align options lines
  4777. (org-set-local
  4778. 'align-mode-rules-list
  4779. '((org-in-buffer-settings
  4780. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4781. (modes . '(org-mode)))))
  4782. ;; Imenu
  4783. (org-set-local 'imenu-create-index-function
  4784. 'org-imenu-get-tree)
  4785. ;; Make isearch reveal context
  4786. (if (or (featurep 'xemacs)
  4787. (not (boundp 'outline-isearch-open-invisible-function)))
  4788. ;; Emacs 21 and XEmacs make use of the hook
  4789. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4790. ;; Emacs 22 deals with this through a special variable
  4791. (org-set-local 'outline-isearch-open-invisible-function
  4792. (lambda (&rest ignore) (org-show-context 'isearch)))
  4793. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4794. ;; Setup the pcomplete hooks
  4795. (set (make-local-variable 'pcomplete-command-completion-function)
  4796. 'org-pcomplete-initial)
  4797. (set (make-local-variable 'pcomplete-command-name-function)
  4798. 'org-command-at-point)
  4799. (set (make-local-variable 'pcomplete-default-completion-function)
  4800. 'ignore)
  4801. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4802. 'org-parse-arguments)
  4803. (set (make-local-variable 'pcomplete-termination-string) "")
  4804. (when (>= emacs-major-version 23)
  4805. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4806. ;; If empty file that did not turn on org-mode automatically, make it to.
  4807. (if (and org-insert-mode-line-in-empty-file
  4808. (org-called-interactively-p 'any)
  4809. (= (point-min) (point-max)))
  4810. (insert "# -*- mode: org -*-\n\n"))
  4811. (unless org-inhibit-startup
  4812. (org-unmodified
  4813. (and org-startup-with-beamer-mode (org-beamer-mode))
  4814. (when org-startup-align-all-tables
  4815. (org-table-map-tables 'org-table-align 'quietly))
  4816. (when org-startup-with-inline-images
  4817. (org-display-inline-images))
  4818. (when org-startup-with-latex-preview
  4819. (org-preview-latex-fragment))
  4820. (unless org-inhibit-startup-visibility-stuff
  4821. (org-set-startup-visibility))))
  4822. ;; Try to set org-hide correctly
  4823. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4824. ;; Update `customize-package-emacs-version-alist'
  4825. (add-to-list 'customize-package-emacs-version-alist
  4826. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4827. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4828. ("8.0" . "24.4")))
  4829. (defvar org-mode-transpose-word-syntax-table
  4830. (let ((st (make-syntax-table)))
  4831. (mapc (lambda(c) (modify-syntax-entry
  4832. (string-to-char (car c)) "w p" st))
  4833. org-emphasis-alist)
  4834. st))
  4835. (when (fboundp 'abbrev-table-put)
  4836. (abbrev-table-put org-mode-abbrev-table
  4837. :parents (list text-mode-abbrev-table)))
  4838. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4839. (defsubst org-fix-ellipsis-at-bol ()
  4840. (save-excursion (goto-char (window-start)) (recenter 0)))
  4841. (defun org-find-invisible-foreground ()
  4842. (let ((candidates (remove
  4843. "unspecified-bg"
  4844. (nconc
  4845. (list (face-background 'default)
  4846. (face-background 'org-default))
  4847. (mapcar
  4848. (lambda (alist)
  4849. (when (boundp alist)
  4850. (cdr (assoc 'background-color (symbol-value alist)))))
  4851. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4852. (list (face-foreground 'org-hide))))))
  4853. (car (remove nil candidates))))
  4854. (defun org-current-time (&optional rounding-minutes past)
  4855. "Current time, possibly rounded to ROUNDING-MINUTES.
  4856. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4857. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4858. the rounding returns a past time."
  4859. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4860. (car org-time-stamp-rounding-minutes)))
  4861. (time (decode-time)) res)
  4862. (if (< r 1)
  4863. (current-time)
  4864. (setq res
  4865. (apply 'encode-time
  4866. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4867. (nthcdr 2 time))))
  4868. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4869. (seconds-to-time (- (org-float-time res) (* r 60)))
  4870. res))))
  4871. (defun org-today ()
  4872. "Return today date, considering `org-extend-today-until'."
  4873. (time-to-days
  4874. (time-subtract (current-time)
  4875. (list 0 (* 3600 org-extend-today-until) 0))))
  4876. ;;;; Font-Lock stuff, including the activators
  4877. (defvar org-mouse-map (make-sparse-keymap))
  4878. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4879. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4880. (when org-mouse-1-follows-link
  4881. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4882. (when org-tab-follows-link
  4883. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4884. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4885. (require 'font-lock)
  4886. (defconst org-non-link-chars "]\t\n\r<>")
  4887. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4888. "shell" "elisp" "doi" "message"))
  4889. (defvar org-link-types-re nil
  4890. "Matches a link that has a url-like prefix like \"http:\"")
  4891. (defvar org-link-re-with-space nil
  4892. "Matches a link with spaces, optional angular brackets around it.")
  4893. (defvar org-link-re-with-space2 nil
  4894. "Matches a link with spaces, optional angular brackets around it.")
  4895. (defvar org-link-re-with-space3 nil
  4896. "Matches a link with spaces, only for internal part in bracket links.")
  4897. (defvar org-angle-link-re nil
  4898. "Matches link with angular brackets, spaces are allowed.")
  4899. (defvar org-plain-link-re nil
  4900. "Matches plain link, without spaces.")
  4901. (defvar org-bracket-link-regexp nil
  4902. "Matches a link in double brackets.")
  4903. (defvar org-bracket-link-analytic-regexp nil
  4904. "Regular expression used to analyze links.
  4905. Here is what the match groups contain after a match:
  4906. 1: http:
  4907. 2: http
  4908. 3: path
  4909. 4: [desc]
  4910. 5: desc")
  4911. (defvar org-bracket-link-analytic-regexp++ nil
  4912. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4913. (defvar org-any-link-re nil
  4914. "Regular expression matching any link.")
  4915. (defconst org-match-sexp-depth 3
  4916. "Number of stacked braces for sub/superscript matching.")
  4917. (defun org-create-multibrace-regexp (left right n)
  4918. "Create a regular expression which will match a balanced sexp.
  4919. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4920. as single character strings.
  4921. The regexp returned will match the entire expression including the
  4922. delimiters. It will also define a single group which contains the
  4923. match except for the outermost delimiters. The maximum depth of
  4924. stacked delimiters is N. Escaping delimiters is not possible."
  4925. (let* ((nothing (concat "[^" left right "]*?"))
  4926. (or "\\|")
  4927. (re nothing)
  4928. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4929. (while (> n 1)
  4930. (setq n (1- n)
  4931. re (concat re or next)
  4932. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4933. (concat left "\\(" re "\\)" right)))
  4934. (defvar org-match-substring-regexp
  4935. (concat
  4936. "\\(\\S-\\)\\([_^]\\)\\("
  4937. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4938. "\\|"
  4939. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4940. "\\|"
  4941. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4942. "The regular expression matching a sub- or superscript.")
  4943. (defvar org-match-substring-with-braces-regexp
  4944. (concat
  4945. "\\(\\S-\\)\\([_^]\\)\\("
  4946. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4947. "\\)")
  4948. "The regular expression matching a sub- or superscript, forcing braces.")
  4949. (defun org-make-link-regexps ()
  4950. "Update the link regular expressions.
  4951. This should be called after the variable `org-link-types' has changed."
  4952. (setq org-link-types-re
  4953. (concat
  4954. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4955. org-link-re-with-space
  4956. (concat
  4957. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4958. "\\([^" org-non-link-chars " ]"
  4959. "[^" org-non-link-chars "]*"
  4960. "[^" org-non-link-chars " ]\\)>?")
  4961. org-link-re-with-space2
  4962. (concat
  4963. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4964. "\\([^" org-non-link-chars " ]"
  4965. "[^\t\n\r]*"
  4966. "[^" org-non-link-chars " ]\\)>?")
  4967. org-link-re-with-space3
  4968. (concat
  4969. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4970. "\\([^" org-non-link-chars " ]"
  4971. "[^\t\n\r]*\\)")
  4972. org-angle-link-re
  4973. (concat
  4974. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4975. "\\([^" org-non-link-chars " ]"
  4976. "[^" org-non-link-chars "]*"
  4977. "\\)>")
  4978. org-plain-link-re
  4979. (concat
  4980. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4981. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4982. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4983. org-bracket-link-regexp
  4984. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4985. org-bracket-link-analytic-regexp
  4986. (concat
  4987. "\\[\\["
  4988. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4989. "\\([^]]+\\)"
  4990. "\\]"
  4991. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4992. "\\]")
  4993. org-bracket-link-analytic-regexp++
  4994. (concat
  4995. "\\[\\["
  4996. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4997. "\\([^]]+\\)"
  4998. "\\]"
  4999. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5000. "\\]")
  5001. org-any-link-re
  5002. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5003. org-angle-link-re "\\)\\|\\("
  5004. org-plain-link-re "\\)")))
  5005. (org-make-link-regexps)
  5006. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  5007. "Regular expression for fast time stamp matching.")
  5008. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  5009. "Regular expression for fast time stamp matching.")
  5010. (defconst org-ts-regexp0
  5011. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5012. "Regular expression matching time strings for analysis.
  5013. This one does not require the space after the date, so it can be used
  5014. on a string that terminates immediately after the date.")
  5015. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5016. "Regular expression matching time strings for analysis.")
  5017. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5018. "Regular expression matching time stamps, with groups.")
  5019. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5020. "Regular expression matching time stamps (also [..]), with groups.")
  5021. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5022. "Regular expression matching a time stamp range.")
  5023. (defconst org-tr-regexp-both
  5024. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5025. "Regular expression matching a time stamp range.")
  5026. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5027. org-ts-regexp "\\)?")
  5028. "Regular expression matching a time stamp or time stamp range.")
  5029. (defconst org-tsr-regexp-both
  5030. (concat org-ts-regexp-both "\\(--?-?"
  5031. org-ts-regexp-both "\\)?")
  5032. "Regular expression matching a time stamp or time stamp range.
  5033. The time stamps may be either active or inactive.")
  5034. (defvar org-emph-face nil)
  5035. (defun org-do-emphasis-faces (limit)
  5036. "Run through the buffer and add overlays to emphasized strings."
  5037. (let (rtn a)
  5038. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5039. (if (not (= (char-after (match-beginning 3))
  5040. (char-after (match-beginning 4))))
  5041. (progn
  5042. (setq rtn t)
  5043. (setq a (assoc (match-string 3) org-emphasis-alist))
  5044. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5045. 'face
  5046. (nth 1 a))
  5047. (and (nth 4 a)
  5048. (org-remove-flyspell-overlays-in
  5049. (match-beginning 0) (match-end 0)))
  5050. (add-text-properties (match-beginning 2) (match-end 2)
  5051. '(font-lock-multiline t org-emphasis t))
  5052. (when org-hide-emphasis-markers
  5053. (add-text-properties (match-end 4) (match-beginning 5)
  5054. '(invisible org-link))
  5055. (add-text-properties (match-beginning 3) (match-end 3)
  5056. '(invisible org-link)))))
  5057. (backward-char 1))
  5058. rtn))
  5059. (defun org-emphasize (&optional char)
  5060. "Insert or change an emphasis, i.e. a font like bold or italic.
  5061. If there is an active region, change that region to a new emphasis.
  5062. If there is no region, just insert the marker characters and position
  5063. the cursor between them.
  5064. CHAR should be the marker character. If it is a space, it means to
  5065. remove the emphasis of the selected region.
  5066. If CHAR is not given (for example in an interactive call) it will be
  5067. prompted for."
  5068. (interactive)
  5069. (let ((erc org-emphasis-regexp-components)
  5070. (prompt "")
  5071. (string "") beg end move c s)
  5072. (if (org-region-active-p)
  5073. (setq beg (region-beginning) end (region-end)
  5074. string (buffer-substring beg end))
  5075. (setq move t))
  5076. (unless char
  5077. (message "Emphasis marker or tag: [%s]"
  5078. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5079. (setq char (read-char-exclusive)))
  5080. (if (equal char ?\ )
  5081. (setq s "" move nil)
  5082. (unless (assoc (char-to-string char) org-emphasis-alist)
  5083. (user-error "No such emphasis marker: \"%c\"" char))
  5084. (setq s (char-to-string char)))
  5085. (while (and (> (length string) 1)
  5086. (equal (substring string 0 1) (substring string -1))
  5087. (assoc (substring string 0 1) org-emphasis-alist))
  5088. (setq string (substring string 1 -1)))
  5089. (setq string (concat s string s))
  5090. (if beg (delete-region beg end))
  5091. (unless (or (bolp)
  5092. (string-match (concat "[" (nth 0 erc) "\n]")
  5093. (char-to-string (char-before (point)))))
  5094. (insert " "))
  5095. (unless (or (eobp)
  5096. (string-match (concat "[" (nth 1 erc) "\n]")
  5097. (char-to-string (char-after (point)))))
  5098. (insert " ") (backward-char 1))
  5099. (insert string)
  5100. (and move (backward-char 1))))
  5101. (defconst org-nonsticky-props
  5102. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5103. (defsubst org-rear-nonsticky-at (pos)
  5104. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5105. (defun org-activate-plain-links (limit)
  5106. "Run through the buffer and add overlays to links."
  5107. (let (f hl)
  5108. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5109. (not (org-in-src-block-p)))
  5110. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5111. (setq f (get-text-property (match-beginning 0) 'face))
  5112. (setq hl (org-match-string-no-properties 0))
  5113. (if (or (eq f 'org-tag)
  5114. (and (listp f) (memq 'org-tag f)))
  5115. nil
  5116. (add-text-properties (match-beginning 0) (match-end 0)
  5117. (list 'mouse-face 'highlight
  5118. 'face 'org-link
  5119. 'htmlize-link `(:uri ,hl)
  5120. 'keymap org-mouse-map))
  5121. (org-rear-nonsticky-at (match-end 0)))
  5122. t)))
  5123. (defun org-activate-code (limit)
  5124. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5125. (progn
  5126. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5127. (remove-text-properties (match-beginning 0) (match-end 0)
  5128. '(display t invisible t intangible t))
  5129. t)))
  5130. (defcustom org-src-fontify-natively nil
  5131. "When non-nil, fontify code in code blocks."
  5132. :type 'boolean
  5133. :version "24.1"
  5134. :group 'org-appearance
  5135. :group 'org-babel)
  5136. (defcustom org-allow-promoting-top-level-subtree nil
  5137. "When non-nil, allow promoting a top level subtree.
  5138. The leading star of the top level headline will be replaced
  5139. by a #."
  5140. :type 'boolean
  5141. :version "24.1"
  5142. :group 'org-appearance)
  5143. (defun org-fontify-meta-lines-and-blocks (limit)
  5144. (condition-case nil
  5145. (org-fontify-meta-lines-and-blocks-1 limit)
  5146. (error (message "org-mode fontification error"))))
  5147. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5148. "Fontify #+ lines and blocks."
  5149. (let ((case-fold-search t))
  5150. (if (re-search-forward
  5151. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5152. limit t)
  5153. (let ((beg (match-beginning 0))
  5154. (block-start (match-end 0))
  5155. (block-end nil)
  5156. (lang (match-string 7))
  5157. (beg1 (line-beginning-position 2))
  5158. (dc1 (downcase (match-string 2)))
  5159. (dc3 (downcase (match-string 3)))
  5160. end end1 quoting block-type ovl)
  5161. (cond
  5162. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5163. ;; a single line of backend-specific content
  5164. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5165. (remove-text-properties (match-beginning 0) (match-end 0)
  5166. '(display t invisible t intangible t))
  5167. (add-text-properties (match-beginning 1) (match-end 3)
  5168. '(font-lock-fontified t face org-meta-line))
  5169. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5170. '(font-lock-fontified t face org-block))
  5171. ; for backend-specific code
  5172. t)
  5173. ((and (match-end 4) (equal dc3 "+begin"))
  5174. ;; Truly a block
  5175. (setq block-type (downcase (match-string 5))
  5176. quoting (member block-type org-protecting-blocks))
  5177. (when (re-search-forward
  5178. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5179. nil t) ;; on purpose, we look further than LIMIT
  5180. (setq end (min (point-max) (match-end 0))
  5181. end1 (min (point-max) (1- (match-beginning 0))))
  5182. (setq block-end (match-beginning 0))
  5183. (when quoting
  5184. (remove-text-properties beg end
  5185. '(display t invisible t intangible t)))
  5186. (add-text-properties
  5187. beg end
  5188. '(font-lock-fontified t font-lock-multiline t))
  5189. (add-text-properties beg beg1 '(face org-meta-line))
  5190. (add-text-properties end1 (min (point-max) (1+ end))
  5191. '(face org-meta-line)) ; for end_src
  5192. (cond
  5193. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5194. (org-src-font-lock-fontify-block lang block-start block-end)
  5195. ;; remove old background overlays
  5196. (mapc (lambda (ov)
  5197. (if (eq (overlay-get ov 'face) 'org-block-background)
  5198. (delete-overlay ov)))
  5199. (overlays-at (/ (+ beg1 block-end) 2)))
  5200. ;; add a background overlay
  5201. (setq ovl (make-overlay beg1 block-end))
  5202. (overlay-put ovl 'face 'org-block-background)
  5203. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5204. (quoting
  5205. (add-text-properties beg1 (min (point-max) (1+ end1))
  5206. '(face org-block))) ; end of source block
  5207. ((not org-fontify-quote-and-verse-blocks))
  5208. ((string= block-type "quote")
  5209. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5210. ((string= block-type "verse")
  5211. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5212. (add-text-properties beg beg1 '(face org-block-begin-line))
  5213. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5214. '(face org-block-end-line))
  5215. t))
  5216. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5217. (add-text-properties
  5218. beg (match-end 3)
  5219. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5220. '(font-lock-fontified t invisible t)
  5221. '(font-lock-fontified t face org-document-info-keyword)))
  5222. (add-text-properties
  5223. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5224. (if (string-equal dc1 "+title:")
  5225. '(font-lock-fontified t face org-document-title)
  5226. '(font-lock-fontified t face org-document-info))))
  5227. ((or (equal dc1 "+results")
  5228. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5229. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5230. "+call:" "+header:" "+headers:" "+name:"))
  5231. (and (match-end 4) (equal dc3 "+attr")))
  5232. (add-text-properties
  5233. beg (match-end 0)
  5234. '(font-lock-fontified t face org-meta-line))
  5235. t)
  5236. ((member dc3 '(" " ""))
  5237. (add-text-properties
  5238. beg (match-end 0)
  5239. '(font-lock-fontified t face font-lock-comment-face)))
  5240. ((not (member (char-after beg) '(?\ ?\t)))
  5241. ;; just any other in-buffer setting, but not indented
  5242. (add-text-properties
  5243. beg (match-end 0)
  5244. '(font-lock-fontified t face org-meta-line))
  5245. t)
  5246. (t nil))))))
  5247. (defun org-activate-angle-links (limit)
  5248. "Run through the buffer and add overlays to links."
  5249. (if (and (re-search-forward org-angle-link-re limit t)
  5250. (not (org-in-src-block-p)))
  5251. (progn
  5252. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5253. (add-text-properties (match-beginning 0) (match-end 0)
  5254. (list 'mouse-face 'highlight
  5255. 'keymap org-mouse-map))
  5256. (org-rear-nonsticky-at (match-end 0))
  5257. t)))
  5258. (defun org-activate-footnote-links (limit)
  5259. "Run through the buffer and add overlays to footnotes."
  5260. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5261. (when fn
  5262. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5263. (org-remove-flyspell-overlays-in beg end)
  5264. (add-text-properties beg end
  5265. (list 'mouse-face 'highlight
  5266. 'keymap org-mouse-map
  5267. 'help-echo
  5268. (if (= (point-at-bol) beg)
  5269. "Footnote definition"
  5270. "Footnote reference")
  5271. 'font-lock-fontified t
  5272. 'font-lock-multiline t
  5273. 'face 'org-footnote))))))
  5274. (defun org-activate-bracket-links (limit)
  5275. "Run through the buffer and add overlays to bracketed links."
  5276. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5277. (not (org-in-src-block-p)))
  5278. (let* ((hl (org-match-string-no-properties 1))
  5279. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5280. (ip (org-maybe-intangible
  5281. (list 'invisible 'org-link
  5282. 'keymap org-mouse-map 'mouse-face 'highlight
  5283. 'font-lock-multiline t 'help-echo help
  5284. 'htmlize-link `(:uri ,hl))))
  5285. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5286. 'font-lock-multiline t 'help-echo help
  5287. 'htmlize-link `(:uri ,hl))))
  5288. ;; We need to remove the invisible property here. Table narrowing
  5289. ;; may have made some of this invisible.
  5290. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5291. (remove-text-properties (match-beginning 0) (match-end 0)
  5292. '(invisible nil))
  5293. (if (match-end 3)
  5294. (progn
  5295. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5296. (org-rear-nonsticky-at (match-beginning 3))
  5297. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5298. (org-rear-nonsticky-at (match-end 3))
  5299. (add-text-properties (match-end 3) (match-end 0) ip)
  5300. (org-rear-nonsticky-at (match-end 0)))
  5301. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5302. (org-rear-nonsticky-at (match-beginning 1))
  5303. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5304. (org-rear-nonsticky-at (match-end 1))
  5305. (add-text-properties (match-end 1) (match-end 0) ip)
  5306. (org-rear-nonsticky-at (match-end 0)))
  5307. t)))
  5308. (defun org-activate-dates (limit)
  5309. "Run through the buffer and add overlays to dates."
  5310. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5311. (not (equal (char-before (match-beginning 0)) 91)))
  5312. (progn
  5313. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5314. (add-text-properties (match-beginning 0) (match-end 0)
  5315. (list 'mouse-face 'highlight
  5316. 'keymap org-mouse-map))
  5317. (org-rear-nonsticky-at (match-end 0))
  5318. (when org-display-custom-times
  5319. (if (match-end 3)
  5320. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5321. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5322. t)))
  5323. (defvar org-target-link-regexp nil
  5324. "Regular expression matching radio targets in plain text.")
  5325. (make-variable-buffer-local 'org-target-link-regexp)
  5326. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5327. "Regular expression matching a link target.")
  5328. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5329. "Regular expression matching a radio target.")
  5330. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5331. "Regular expression matching any target.")
  5332. (defun org-activate-target-links (limit)
  5333. "Run through the buffer and add overlays to target matches."
  5334. (when org-target-link-regexp
  5335. (let ((case-fold-search t))
  5336. (if (re-search-forward org-target-link-regexp limit t)
  5337. (progn
  5338. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5339. (add-text-properties (match-beginning 0) (match-end 0)
  5340. (list 'mouse-face 'highlight
  5341. 'keymap org-mouse-map
  5342. 'help-echo "Radio target link"
  5343. 'org-linked-text t))
  5344. (org-rear-nonsticky-at (match-end 0))
  5345. t)))))
  5346. (defun org-update-radio-target-regexp ()
  5347. "Find all radio targets in this file and update the regular expression."
  5348. (interactive)
  5349. (when (memq 'radio org-activate-links)
  5350. (setq org-target-link-regexp
  5351. (org-make-target-link-regexp (org-all-targets 'radio)))
  5352. (org-restart-font-lock)))
  5353. (defun org-hide-wide-columns (limit)
  5354. (let (s e)
  5355. (setq s (text-property-any (point) (or limit (point-max))
  5356. 'org-cwidth t))
  5357. (when s
  5358. (setq e (next-single-property-change s 'org-cwidth))
  5359. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5360. (goto-char e)
  5361. t)))
  5362. (defvar org-latex-and-related-regexp nil
  5363. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5364. (defvar org-match-substring-regexp)
  5365. (defvar org-match-substring-with-braces-regexp)
  5366. (defun org-compute-latex-and-related-regexp ()
  5367. "Compute regular expression for LaTeX, entities and sub/superscript.
  5368. Result depends on variable `org-highlight-latex-and-related'."
  5369. (org-set-local
  5370. 'org-latex-and-related-regexp
  5371. (let* ((re-sub
  5372. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5373. ((eq org-use-sub-superscripts '{})
  5374. (list org-match-substring-with-braces-regexp))
  5375. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5376. (re-latex
  5377. (when (memq 'latex org-highlight-latex-and-related)
  5378. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5379. (delq nil
  5380. (mapcar (lambda (x)
  5381. (and (member (car x) matchers) (nth 1 x)))
  5382. org-latex-regexps)))))
  5383. (re-entities
  5384. (when (memq 'entities org-highlight-latex-and-related)
  5385. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5386. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5387. (defun org-do-latex-and-related (limit)
  5388. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5389. LIMIT bounds the search for syntax to highlight. Stop at first
  5390. highlighted object, if any. Return t if some highlighting was
  5391. done, nil otherwise."
  5392. (when (org-string-nw-p org-latex-and-related-regexp)
  5393. (catch 'found
  5394. (while (re-search-forward org-latex-and-related-regexp limit t)
  5395. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5396. 'face))
  5397. '(org-code org-verbatim underline))
  5398. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5399. '(?_ ?^))
  5400. 1
  5401. 0)))
  5402. (font-lock-prepend-text-property
  5403. (+ offset (match-beginning 0)) (match-end 0)
  5404. 'face 'org-latex-and-related)
  5405. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5406. '(font-lock-multiline t)))
  5407. (throw 'found t)))
  5408. nil)))
  5409. (defun org-restart-font-lock ()
  5410. "Restart `font-lock-mode', to force refontification."
  5411. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5412. (font-lock-mode -1)
  5413. (font-lock-mode 1)))
  5414. (defun org-all-targets (&optional radio)
  5415. "Return a list of all targets in this file.
  5416. When optional argument RADIO is non-nil, only find radio
  5417. targets."
  5418. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5419. (save-excursion
  5420. (goto-char (point-min))
  5421. (while (re-search-forward re nil t)
  5422. ;; Make sure point is really within the object.
  5423. (backward-char)
  5424. (let ((obj (org-element-context)))
  5425. (when (memq (org-element-type obj) '(radio-target target))
  5426. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5427. rtn)))
  5428. (defun org-make-target-link-regexp (targets)
  5429. "Make regular expression matching all strings in TARGETS.
  5430. The regular expression finds the targets also if there is a line break
  5431. between words."
  5432. (and targets
  5433. (concat
  5434. "\\<\\("
  5435. (mapconcat
  5436. (lambda (x)
  5437. (setq x (regexp-quote x))
  5438. (while (string-match " +" x)
  5439. (setq x (replace-match "\\s-+" t t x)))
  5440. x)
  5441. targets
  5442. "\\|")
  5443. "\\)\\>")))
  5444. (defun org-activate-tags (limit)
  5445. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5446. (progn
  5447. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5448. (add-text-properties (match-beginning 1) (match-end 1)
  5449. (list 'mouse-face 'highlight
  5450. 'keymap org-mouse-map))
  5451. (org-rear-nonsticky-at (match-end 1))
  5452. t)))
  5453. (defun org-outline-level ()
  5454. "Compute the outline level of the heading at point.
  5455. If this is called at a normal headline, the level is the number of stars.
  5456. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5457. (save-excursion
  5458. (if (not (condition-case nil
  5459. (org-back-to-heading t)
  5460. (error nil)))
  5461. 0
  5462. (looking-at org-outline-regexp)
  5463. (1- (- (match-end 0) (match-beginning 0))))))
  5464. (defvar org-font-lock-keywords nil)
  5465. (defsubst org-re-property (property &optional literal)
  5466. "Return a regexp matching a PROPERTY line.
  5467. Match group 3 will be set to the value if it exists."
  5468. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5469. (if literal property (regexp-quote property))
  5470. "\\):\\)[ \t]+\\(?3:[^ \t\r\n].*?\\)\\(?5:[ \t]*\\)$"))
  5471. (defconst org-property-re
  5472. (org-re-property ".*?" 'literal)
  5473. "Regular expression matching a property line.
  5474. There are four matching groups:
  5475. 1: :PROPKEY: including the leading and trailing colon,
  5476. 2: PROPKEY without the leading and trailing colon,
  5477. 3: PROPVAL without leading or trailing spaces,
  5478. 4: the indentation of the current line,
  5479. 5: trailing whitespace.")
  5480. (defvar org-font-lock-hook nil
  5481. "Functions to be called for special font lock stuff.")
  5482. (defvar org-font-lock-set-keywords-hook nil
  5483. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5484. This is called after `org-font-lock-extra-keywords' is defined, but before
  5485. it is installed to be used by font lock. This can be useful if something
  5486. needs to be inserted at a specific position in the font-lock sequence.")
  5487. (defun org-font-lock-hook (limit)
  5488. "Run `org-font-lock-hook' within LIMIT."
  5489. (run-hook-with-args 'org-font-lock-hook limit))
  5490. (defun org-set-font-lock-defaults ()
  5491. "Set font lock defaults for the current buffer."
  5492. (let* ((em org-fontify-emphasized-text)
  5493. (lk org-activate-links)
  5494. (org-font-lock-extra-keywords
  5495. (list
  5496. ;; Call the hook
  5497. '(org-font-lock-hook)
  5498. ;; Headlines
  5499. `(,(if org-fontify-whole-heading-line
  5500. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5501. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5502. (1 (org-get-level-face 1))
  5503. (2 (org-get-level-face 2))
  5504. (3 (org-get-level-face 3)))
  5505. ;; Table lines
  5506. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5507. (1 'org-table t))
  5508. ;; Table internals
  5509. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5510. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5511. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5512. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5513. ;; Drawers
  5514. (list org-drawer-regexp '(0 'org-special-keyword t))
  5515. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5516. ;; Properties
  5517. (list org-property-re
  5518. '(1 'org-special-keyword t)
  5519. '(3 'org-property-value t))
  5520. ;; Links
  5521. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5522. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5523. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5524. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5525. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5526. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5527. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5528. ;; Targets.
  5529. (list org-any-target-regexp '(0 'org-target t))
  5530. ;; Diary sexps.
  5531. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5532. ;; Macro
  5533. '("{{{.+}}}" (0 'org-macro t))
  5534. '(org-hide-wide-columns (0 nil append))
  5535. ;; TODO keyword
  5536. (list (format org-heading-keyword-regexp-format
  5537. org-todo-regexp)
  5538. '(2 (org-get-todo-face 2) t))
  5539. ;; DONE
  5540. (if org-fontify-done-headline
  5541. (list (format org-heading-keyword-regexp-format
  5542. (concat
  5543. "\\(?:"
  5544. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5545. "\\)"))
  5546. '(2 'org-headline-done t))
  5547. nil)
  5548. ;; Priorities
  5549. '(org-font-lock-add-priority-faces)
  5550. ;; Tags
  5551. '(org-font-lock-add-tag-faces)
  5552. ;; Tags groups
  5553. (if (and org-group-tags org-tag-groups-alist)
  5554. (list (concat org-outline-regexp-bol ".+\\(:"
  5555. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5556. ":\\).*$")
  5557. '(1 'org-tag-group prepend)))
  5558. ;; Special keywords
  5559. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5560. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5561. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5562. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5563. ;; Emphasis
  5564. (if em
  5565. (if (featurep 'xemacs)
  5566. '(org-do-emphasis-faces (0 nil append))
  5567. '(org-do-emphasis-faces)))
  5568. ;; Checkboxes
  5569. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5570. 1 'org-checkbox prepend)
  5571. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5572. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5573. (0 (org-get-checkbox-statistics-face) t)))
  5574. ;; Description list items
  5575. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5576. 1 'org-list-dt prepend)
  5577. ;; ARCHIVEd headings
  5578. (list (concat
  5579. org-outline-regexp-bol
  5580. "\\(.*:" org-archive-tag ":.*\\)")
  5581. '(1 'org-archived prepend))
  5582. ;; Specials
  5583. '(org-do-latex-and-related)
  5584. '(org-fontify-entities)
  5585. '(org-raise-scripts)
  5586. ;; Code
  5587. '(org-activate-code (1 'org-code t))
  5588. ;; COMMENT
  5589. (list (format org-heading-keyword-regexp-format
  5590. (concat "\\("
  5591. org-comment-string "\\|" org-quote-string
  5592. "\\)"))
  5593. '(2 'org-special-keyword t))
  5594. ;; Blocks and meta lines
  5595. '(org-fontify-meta-lines-and-blocks))))
  5596. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5597. (run-hooks 'org-font-lock-set-keywords-hook)
  5598. ;; Now set the full font-lock-keywords
  5599. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5600. (org-set-local 'font-lock-defaults
  5601. '(org-font-lock-keywords t nil nil backward-paragraph))
  5602. (kill-local-variable 'font-lock-keywords) nil))
  5603. (defun org-toggle-pretty-entities ()
  5604. "Toggle the composition display of entities as UTF8 characters."
  5605. (interactive)
  5606. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5607. (org-restart-font-lock)
  5608. (if org-pretty-entities
  5609. (message "Entities are now displayed as UTF8 characters")
  5610. (save-restriction
  5611. (widen)
  5612. (org-decompose-region (point-min) (point-max))
  5613. (message "Entities are now displayed as plain text"))))
  5614. (defvar org-custom-properties-overlays nil
  5615. "List of overlays used for custom properties.")
  5616. (make-variable-buffer-local 'org-custom-properties-overlays)
  5617. (defun org-toggle-custom-properties-visibility ()
  5618. "Display or hide properties in `org-custom-properties'."
  5619. (interactive)
  5620. (if org-custom-properties-overlays
  5621. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5622. (setq org-custom-properties-overlays nil))
  5623. (unless (not org-custom-properties)
  5624. (save-excursion
  5625. (save-restriction
  5626. (widen)
  5627. (goto-char (point-min))
  5628. (while (re-search-forward org-property-re nil t)
  5629. (mapc (lambda(p)
  5630. (when (equal p (substring (match-string 1) 1 -1))
  5631. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5632. (overlay-put o 'invisible t)
  5633. (overlay-put o 'org-custom-property t)
  5634. (push o org-custom-properties-overlays))))
  5635. org-custom-properties)))))))
  5636. (defun org-fontify-entities (limit)
  5637. "Find an entity to fontify."
  5638. (let (ee)
  5639. (when org-pretty-entities
  5640. (catch 'match
  5641. (while (re-search-forward
  5642. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5643. limit t)
  5644. (if (and (not (org-in-indented-comment-line))
  5645. (setq ee (org-entity-get (match-string 1)))
  5646. (= (length (nth 6 ee)) 1))
  5647. (let*
  5648. ((end (if (equal (match-string 2) "{}")
  5649. (match-end 2)
  5650. (match-end 1))))
  5651. (add-text-properties
  5652. (match-beginning 0) end
  5653. (list 'font-lock-fontified t))
  5654. (compose-region (match-beginning 0) end
  5655. (nth 6 ee) nil)
  5656. (backward-char 1)
  5657. (throw 'match t))))
  5658. nil))))
  5659. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5660. "Fontify string S like in Org-mode."
  5661. (with-temp-buffer
  5662. (insert s)
  5663. (let ((org-odd-levels-only odd-levels))
  5664. (org-mode)
  5665. (font-lock-fontify-buffer)
  5666. (buffer-string))))
  5667. (defvar org-m nil)
  5668. (defvar org-l nil)
  5669. (defvar org-f nil)
  5670. (defun org-get-level-face (n)
  5671. "Get the right face for match N in font-lock matching of headlines."
  5672. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5673. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5674. (if org-cycle-level-faces
  5675. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5676. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5677. (cond
  5678. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5679. ((eq n 2) org-f)
  5680. (t (if org-level-color-stars-only nil org-f))))
  5681. (defun org-get-todo-face (kwd)
  5682. "Get the right face for a TODO keyword KWD.
  5683. If KWD is a number, get the corresponding match group."
  5684. (if (numberp kwd) (setq kwd (match-string kwd)))
  5685. (or (org-face-from-face-or-color
  5686. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5687. (and (member kwd org-done-keywords) 'org-done)
  5688. 'org-todo))
  5689. (defun org-face-from-face-or-color (context inherit face-or-color)
  5690. "Create a face list that inherits INHERIT, but sets the foreground color.
  5691. When FACE-OR-COLOR is not a string, just return it."
  5692. (if (stringp face-or-color)
  5693. (list :inherit inherit
  5694. (cdr (assoc context org-faces-easy-properties))
  5695. face-or-color)
  5696. face-or-color))
  5697. (defun org-font-lock-add-tag-faces (limit)
  5698. "Add the special tag faces."
  5699. (when (and org-tag-faces org-tags-special-faces-re)
  5700. (while (re-search-forward org-tags-special-faces-re limit t)
  5701. (add-text-properties (match-beginning 1) (match-end 1)
  5702. (list 'face (org-get-tag-face 1)
  5703. 'font-lock-fontified t))
  5704. (backward-char 1))))
  5705. (defun org-font-lock-add-priority-faces (limit)
  5706. "Add the special priority faces."
  5707. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5708. (when (save-match-data (org-at-heading-p))
  5709. (add-text-properties
  5710. (match-beginning 0) (match-end 0)
  5711. (list 'face (or (org-face-from-face-or-color
  5712. 'priority 'org-priority
  5713. (cdr (assoc (char-after (match-beginning 1))
  5714. org-priority-faces)))
  5715. 'org-priority)
  5716. 'font-lock-fontified t)))))
  5717. (defun org-get-tag-face (kwd)
  5718. "Get the right face for a TODO keyword KWD.
  5719. If KWD is a number, get the corresponding match group."
  5720. (if (numberp kwd) (setq kwd (match-string kwd)))
  5721. (or (org-face-from-face-or-color
  5722. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5723. 'org-tag))
  5724. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5725. "Remove fontification and activation overlays from links."
  5726. (font-lock-default-unfontify-region beg end)
  5727. (let* ((buffer-undo-list t)
  5728. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5729. (inhibit-modification-hooks t)
  5730. deactivate-mark buffer-file-name buffer-file-truename)
  5731. (org-decompose-region beg end)
  5732. (remove-text-properties beg end
  5733. '(mouse-face t keymap t org-linked-text t
  5734. invisible t intangible t
  5735. org-no-flyspell t org-emphasis t))
  5736. (org-remove-font-lock-display-properties beg end)))
  5737. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5738. ((raise 0.3) (height 0.7))
  5739. ((raise -0.5))
  5740. ((raise 0.5)))
  5741. "Display properties for showing superscripts and subscripts.")
  5742. (defun org-remove-font-lock-display-properties (beg end)
  5743. "Remove specific display properties that have been added by font lock.
  5744. The will remove the raise properties that are used to show superscripts
  5745. and subscripts."
  5746. (let (next prop)
  5747. (while (< beg end)
  5748. (setq next (next-single-property-change beg 'display nil end)
  5749. prop (get-text-property beg 'display))
  5750. (if (member prop org-script-display)
  5751. (put-text-property beg next 'display nil))
  5752. (setq beg next))))
  5753. (defun org-raise-scripts (limit)
  5754. "Add raise properties to sub/superscripts."
  5755. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5756. (if (re-search-forward
  5757. (if (eq org-use-sub-superscripts t)
  5758. org-match-substring-regexp
  5759. org-match-substring-with-braces-regexp)
  5760. limit t)
  5761. (let* ((pos (point)) table-p comment-p
  5762. (mpos (match-beginning 3))
  5763. (emph-p (get-text-property mpos 'org-emphasis))
  5764. (link-p (get-text-property mpos 'mouse-face))
  5765. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5766. (goto-char (point-at-bol))
  5767. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5768. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5769. (goto-char pos)
  5770. ;; Handle a_b^c
  5771. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5772. (if (or comment-p emph-p link-p keyw-p)
  5773. t
  5774. (put-text-property (match-beginning 3) (match-end 0)
  5775. 'display
  5776. (if (equal (char-after (match-beginning 2)) ?^)
  5777. (nth (if table-p 3 1) org-script-display)
  5778. (nth (if table-p 2 0) org-script-display)))
  5779. (add-text-properties (match-beginning 2) (match-end 2)
  5780. (list 'invisible t
  5781. 'org-dwidth t 'org-dwidth-n 1))
  5782. (if (and (eq (char-after (match-beginning 3)) ?{)
  5783. (eq (char-before (match-end 3)) ?}))
  5784. (progn
  5785. (add-text-properties
  5786. (match-beginning 3) (1+ (match-beginning 3))
  5787. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5788. (add-text-properties
  5789. (1- (match-end 3)) (match-end 3)
  5790. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5791. t)))))
  5792. ;;;; Visibility cycling, including org-goto and indirect buffer
  5793. ;;; Cycling
  5794. (defvar org-cycle-global-status nil)
  5795. (make-variable-buffer-local 'org-cycle-global-status)
  5796. (put 'org-cycle-global-status 'org-state t)
  5797. (defvar org-cycle-subtree-status nil)
  5798. (make-variable-buffer-local 'org-cycle-subtree-status)
  5799. (put 'org-cycle-subtree-status 'org-state t)
  5800. (defvar org-inlinetask-min-level)
  5801. ;;;###autoload
  5802. (defun org-cycle (&optional arg)
  5803. "TAB-action and visibility cycling for Org-mode.
  5804. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5805. is outline visibility cycling, but it also invokes other actions
  5806. in special contexts.
  5807. - When this function is called with a prefix argument, rotate the entire
  5808. buffer through 3 states (global cycling)
  5809. 1. OVERVIEW: Show only top-level headlines.
  5810. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5811. 3. SHOW ALL: Show everything.
  5812. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5813. determined by the variable `org-startup-folded', and by any VISIBILITY
  5814. properties in the buffer.
  5815. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5816. including any drawers.
  5817. - When inside a table, re-align the table and move to the next field.
  5818. - When point is at the beginning of a headline, rotate the subtree started
  5819. by this line through 3 different states (local cycling)
  5820. 1. FOLDED: Only the main headline is shown.
  5821. 2. CHILDREN: The main headline and the direct children are shown.
  5822. From this state, you can move to one of the children
  5823. and zoom in further.
  5824. 3. SUBTREE: Show the entire subtree, including body text.
  5825. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5826. - When point is at the beginning of an empty headline and the variable
  5827. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5828. of the headline by demoting and promoting it to likely levels. This
  5829. speeds up creation document structure by pressing TAB once or several
  5830. times right after creating a new headline.
  5831. - When there is a numeric prefix, go up to a heading with level ARG, do
  5832. a `show-subtree' and return to the previous cursor position. If ARG
  5833. is negative, go up that many levels.
  5834. - When point is not at the beginning of a headline, execute the global
  5835. binding for TAB, which is re-indenting the line. See the option
  5836. `org-cycle-emulate-tab' for details.
  5837. - Special case: if point is at the beginning of the buffer and there is
  5838. no headline in line 1, this function will act as if called with prefix arg
  5839. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5840. But only if also the variable `org-cycle-global-at-bob' is t."
  5841. (interactive "P")
  5842. (org-load-modules-maybe)
  5843. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5844. (and org-cycle-level-after-item/entry-creation
  5845. (or (org-cycle-level)
  5846. (org-cycle-item-indentation))))
  5847. (let* (message-log-max ; Don't populate the *Messages* buffer
  5848. (limit-level
  5849. (or org-cycle-max-level
  5850. (and (boundp 'org-inlinetask-min-level)
  5851. org-inlinetask-min-level
  5852. (1- org-inlinetask-min-level))))
  5853. (nstars (and limit-level
  5854. (if org-odd-levels-only
  5855. (and limit-level (1- (* limit-level 2)))
  5856. limit-level)))
  5857. (org-outline-regexp
  5858. (if (not (derived-mode-p 'org-mode))
  5859. outline-regexp
  5860. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5861. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5862. (not (looking-at org-outline-regexp))))
  5863. (org-cycle-hook
  5864. (if bob-special
  5865. (delq 'org-optimize-window-after-visibility-change
  5866. (copy-sequence org-cycle-hook))
  5867. org-cycle-hook))
  5868. (pos (point)))
  5869. (if (or bob-special (equal arg '(4)))
  5870. ;; special case: use global cycling
  5871. (setq arg t))
  5872. (cond
  5873. ((equal arg '(16))
  5874. (setq last-command 'dummy)
  5875. (org-set-startup-visibility)
  5876. (message "Startup visibility, plus VISIBILITY properties"))
  5877. ((equal arg '(64))
  5878. (show-all)
  5879. (message "Entire buffer visible, including drawers"))
  5880. ;; Table: enter it or move to the next field.
  5881. ((org-at-table-p 'any)
  5882. (if (org-at-table.el-p)
  5883. (message "Use C-c ' to edit table.el tables")
  5884. (if arg (org-table-edit-field t)
  5885. (org-table-justify-field-maybe)
  5886. (call-interactively 'org-table-next-field))))
  5887. ((run-hook-with-args-until-success
  5888. 'org-tab-after-check-for-table-hook))
  5889. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5890. ((eq arg t) (org-cycle-internal-global))
  5891. ;; Drawers: delegate to `org-flag-drawer'.
  5892. ((and org-drawers org-drawer-regexp
  5893. (save-excursion
  5894. (beginning-of-line 1)
  5895. (looking-at org-drawer-regexp)))
  5896. (org-flag-drawer ; toggle block visibility
  5897. (not (get-char-property (match-end 0) 'invisible))))
  5898. ;; Show-subtree, ARG levels up from here.
  5899. ((integerp arg)
  5900. (save-excursion
  5901. (org-back-to-heading)
  5902. (outline-up-heading (if (< arg 0) (- arg)
  5903. (- (funcall outline-level) arg)))
  5904. (org-show-subtree)))
  5905. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5906. ((and (featurep 'org-inlinetask)
  5907. (org-inlinetask-at-task-p)
  5908. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5909. (org-inlinetask-toggle-visibility))
  5910. ((org-try-cdlatex-tab))
  5911. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5912. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5913. (save-excursion (beginning-of-line 1)
  5914. (looking-at org-outline-regexp)))
  5915. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5916. (org-cycle-internal-local))
  5917. ;; From there: TAB emulation and template completion.
  5918. (buffer-read-only (org-back-to-heading))
  5919. ((run-hook-with-args-until-success
  5920. 'org-tab-after-check-for-cycling-hook))
  5921. ((org-try-structure-completion))
  5922. ((run-hook-with-args-until-success
  5923. 'org-tab-before-tab-emulation-hook))
  5924. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5925. (or (not (bolp))
  5926. (not (looking-at org-outline-regexp))))
  5927. (call-interactively (global-key-binding "\t")))
  5928. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5929. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5930. (or (and (eq org-cycle-emulate-tab 'white)
  5931. (= (match-end 0) (point-at-eol)))
  5932. (and (eq org-cycle-emulate-tab 'whitestart)
  5933. (>= (match-end 0) pos))))
  5934. t
  5935. (eq org-cycle-emulate-tab t))
  5936. (call-interactively (global-key-binding "\t")))
  5937. (t (save-excursion
  5938. (org-back-to-heading)
  5939. (org-cycle)))))))
  5940. (defun org-cycle-internal-global ()
  5941. "Do the global cycling action."
  5942. ;; Hack to avoid display of messages for .org attachments in Gnus
  5943. (let (message-log-max ; Don't populate the *Messages* buffer
  5944. (ga (string-match "\\*fontification" (buffer-name))))
  5945. (cond
  5946. ((and (eq last-command this-command)
  5947. (eq org-cycle-global-status 'overview))
  5948. ;; We just created the overview - now do table of contents
  5949. ;; This can be slow in very large buffers, so indicate action
  5950. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5951. (unless ga (message "CONTENTS..."))
  5952. (org-content)
  5953. (unless ga (message "CONTENTS...done"))
  5954. (setq org-cycle-global-status 'contents)
  5955. (run-hook-with-args 'org-cycle-hook 'contents))
  5956. ((and (eq last-command this-command)
  5957. (eq org-cycle-global-status 'contents))
  5958. ;; We just showed the table of contents - now show everything
  5959. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5960. (show-all)
  5961. (unless ga (message "SHOW ALL"))
  5962. (setq org-cycle-global-status 'all)
  5963. (run-hook-with-args 'org-cycle-hook 'all))
  5964. (t
  5965. ;; Default action: go to overview
  5966. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5967. (org-overview)
  5968. (unless ga (message "OVERVIEW"))
  5969. (setq org-cycle-global-status 'overview)
  5970. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5971. (defun org-cycle-internal-local ()
  5972. "Do the local cycling action."
  5973. (let (message-log-max ; Don't populate the *Messages* buffer
  5974. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5975. ;; First, determine end of headline (EOH), end of subtree or item
  5976. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5977. (save-excursion
  5978. (if (org-at-item-p)
  5979. (progn
  5980. (beginning-of-line)
  5981. (setq struct (org-list-struct))
  5982. (setq eoh (point-at-eol))
  5983. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5984. (setq has-children (org-list-has-child-p (point) struct)))
  5985. (org-back-to-heading)
  5986. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5987. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5988. (setq has-children
  5989. (or (save-excursion
  5990. (let ((level (funcall outline-level)))
  5991. (outline-next-heading)
  5992. (and (org-at-heading-p t)
  5993. (> (funcall outline-level) level))))
  5994. (save-excursion
  5995. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5996. ;; Determine end invisible part of buffer (EOL)
  5997. (beginning-of-line 2)
  5998. ;; XEmacs doesn't have `next-single-char-property-change'
  5999. (if (featurep 'xemacs)
  6000. (while (and (not (eobp)) ;; this is like `next-line'
  6001. (get-char-property (1- (point)) 'invisible))
  6002. (beginning-of-line 2))
  6003. (while (and (not (eobp)) ;; this is like `next-line'
  6004. (get-char-property (1- (point)) 'invisible))
  6005. (goto-char (next-single-char-property-change (point) 'invisible))
  6006. (and (eolp) (beginning-of-line 2))))
  6007. (setq eol (point)))
  6008. ;; Find out what to do next and set `this-command'
  6009. (cond
  6010. ((= eos eoh)
  6011. ;; Nothing is hidden behind this heading
  6012. (unless (org-before-first-heading-p)
  6013. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6014. (message "EMPTY ENTRY")
  6015. (setq org-cycle-subtree-status nil)
  6016. (save-excursion
  6017. (goto-char eos)
  6018. (outline-next-heading)
  6019. (if (outline-invisible-p) (org-flag-heading nil))))
  6020. ((and (or (>= eol eos)
  6021. (not (string-match "\\S-" (buffer-substring eol eos))))
  6022. (or has-children
  6023. (not (setq children-skipped
  6024. org-cycle-skip-children-state-if-no-children))))
  6025. ;; Entire subtree is hidden in one line: children view
  6026. (unless (org-before-first-heading-p)
  6027. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6028. (if (org-at-item-p)
  6029. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6030. (org-show-entry)
  6031. (org-with-limited-levels (show-children))
  6032. ;; FIXME: This slows down the func way too much.
  6033. ;; How keep drawers hidden in subtree anyway?
  6034. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6035. ;; (org-cycle-hide-drawers 'subtree))
  6036. ;; Fold every list in subtree to top-level items.
  6037. (when (eq org-cycle-include-plain-lists 'integrate)
  6038. (save-excursion
  6039. (org-back-to-heading)
  6040. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6041. (beginning-of-line 1)
  6042. (let* ((struct (org-list-struct))
  6043. (prevs (org-list-prevs-alist struct))
  6044. (end (org-list-get-bottom-point struct)))
  6045. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6046. (org-list-get-all-items (point) struct prevs))
  6047. (goto-char (if (< end eos) end eos)))))))
  6048. (message "CHILDREN")
  6049. (save-excursion
  6050. (goto-char eos)
  6051. (outline-next-heading)
  6052. (if (outline-invisible-p) (org-flag-heading nil)))
  6053. (setq org-cycle-subtree-status 'children)
  6054. (unless (org-before-first-heading-p)
  6055. (run-hook-with-args 'org-cycle-hook 'children)))
  6056. ((or children-skipped
  6057. (and (eq last-command this-command)
  6058. (eq org-cycle-subtree-status 'children)))
  6059. ;; We just showed the children, or no children are there,
  6060. ;; now show everything.
  6061. (unless (org-before-first-heading-p)
  6062. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6063. (outline-flag-region eoh eos nil)
  6064. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6065. (setq org-cycle-subtree-status 'subtree)
  6066. (unless (org-before-first-heading-p)
  6067. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6068. (t
  6069. ;; Default action: hide the subtree.
  6070. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6071. (outline-flag-region eoh eos t)
  6072. (message "FOLDED")
  6073. (setq org-cycle-subtree-status 'folded)
  6074. (unless (org-before-first-heading-p)
  6075. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6076. ;;;###autoload
  6077. (defun org-global-cycle (&optional arg)
  6078. "Cycle the global visibility. For details see `org-cycle'.
  6079. With \\[universal-argument] prefix arg, switch to startup visibility.
  6080. With a numeric prefix, show all headlines up to that level."
  6081. (interactive "P")
  6082. (let ((org-cycle-include-plain-lists
  6083. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6084. (cond
  6085. ((integerp arg)
  6086. (show-all)
  6087. (hide-sublevels arg)
  6088. (setq org-cycle-global-status 'contents))
  6089. ((equal arg '(4))
  6090. (org-set-startup-visibility)
  6091. (message "Startup visibility, plus VISIBILITY properties."))
  6092. (t
  6093. (org-cycle '(4))))))
  6094. (defun org-set-startup-visibility ()
  6095. "Set the visibility required by startup options and properties."
  6096. (cond
  6097. ((eq org-startup-folded t)
  6098. (org-cycle '(4)))
  6099. ((eq org-startup-folded 'content)
  6100. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6101. (org-cycle '(4)) (org-cycle '(4)))))
  6102. (unless (eq org-startup-folded 'showeverything)
  6103. (if org-hide-block-startup (org-hide-block-all))
  6104. (org-set-visibility-according-to-property 'no-cleanup)
  6105. (org-cycle-hide-archived-subtrees 'all)
  6106. (org-cycle-hide-drawers 'all)
  6107. (org-cycle-show-empty-lines t)))
  6108. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6109. "Switch subtree visibilities according to :VISIBILITY: property."
  6110. (interactive)
  6111. (let (org-show-entry-below state)
  6112. (save-excursion
  6113. (goto-char (point-min))
  6114. (while (re-search-forward
  6115. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6116. nil t)
  6117. (setq state (match-string 1))
  6118. (save-excursion
  6119. (org-back-to-heading t)
  6120. (hide-subtree)
  6121. (org-reveal)
  6122. (cond
  6123. ((equal state '("fold" "folded"))
  6124. (hide-subtree))
  6125. ((equal state "children")
  6126. (org-show-hidden-entry)
  6127. (show-children))
  6128. ((equal state "content")
  6129. (save-excursion
  6130. (save-restriction
  6131. (org-narrow-to-subtree)
  6132. (org-content))))
  6133. ((member state '("all" "showall"))
  6134. (show-subtree)))))
  6135. (unless no-cleanup
  6136. (org-cycle-hide-archived-subtrees 'all)
  6137. (org-cycle-hide-drawers 'all)
  6138. (org-cycle-show-empty-lines 'all)))))
  6139. ;; This function uses outline-regexp instead of the more fundamental
  6140. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6141. ;; buffers, where outline-regexp is needed.
  6142. (defun org-overview ()
  6143. "Switch to overview mode, showing only top-level headlines.
  6144. Really, this shows all headlines with level equal or greater than the level
  6145. of the first headline in the buffer. This is important, because if the
  6146. first headline is not level one, then (hide-sublevels 1) gives confusing
  6147. results."
  6148. (interactive)
  6149. (let ((l (org-current-line))
  6150. (level (save-excursion
  6151. (goto-char (point-min))
  6152. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6153. (progn
  6154. (goto-char (match-beginning 0))
  6155. (funcall outline-level))))))
  6156. (and level (hide-sublevels level))
  6157. (recenter '(4))
  6158. (org-goto-line l)))
  6159. (defun org-content (&optional arg)
  6160. "Show all headlines in the buffer, like a table of contents.
  6161. With numerical argument N, show content up to level N."
  6162. (interactive "P")
  6163. (save-excursion
  6164. ;; Visit all headings and show their offspring
  6165. (and (integerp arg) (org-overview))
  6166. (goto-char (point-max))
  6167. (catch 'exit
  6168. (while (and (progn (condition-case nil
  6169. (outline-previous-visible-heading 1)
  6170. (error (goto-char (point-min))))
  6171. t)
  6172. (looking-at org-outline-regexp))
  6173. (if (integerp arg)
  6174. (show-children (1- arg))
  6175. (show-branches))
  6176. (if (bobp) (throw 'exit nil))))))
  6177. (defun org-optimize-window-after-visibility-change (state)
  6178. "Adjust the window after a change in outline visibility.
  6179. This function is the default value of the hook `org-cycle-hook'."
  6180. (when (get-buffer-window (current-buffer))
  6181. (cond
  6182. ((eq state 'content) nil)
  6183. ((eq state 'all) nil)
  6184. ((eq state 'folded) nil)
  6185. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6186. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6187. (defun org-remove-empty-overlays-at (pos)
  6188. "Remove outline overlays that do not contain non-white stuff."
  6189. (mapc
  6190. (lambda (o)
  6191. (and (eq 'outline (overlay-get o 'invisible))
  6192. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6193. (overlay-end o))))
  6194. (delete-overlay o)))
  6195. (overlays-at pos)))
  6196. (defun org-clean-visibility-after-subtree-move ()
  6197. "Fix visibility issues after moving a subtree."
  6198. ;; First, find a reasonable region to look at:
  6199. ;; Start two siblings above, end three below
  6200. (let* ((beg (save-excursion
  6201. (and (org-get-last-sibling)
  6202. (org-get-last-sibling))
  6203. (point)))
  6204. (end (save-excursion
  6205. (and (org-get-next-sibling)
  6206. (org-get-next-sibling)
  6207. (org-get-next-sibling))
  6208. (if (org-at-heading-p)
  6209. (point-at-eol)
  6210. (point))))
  6211. (level (looking-at "\\*+"))
  6212. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6213. (save-excursion
  6214. (save-restriction
  6215. (narrow-to-region beg end)
  6216. (when re
  6217. ;; Properly fold already folded siblings
  6218. (goto-char (point-min))
  6219. (while (re-search-forward re nil t)
  6220. (if (and (not (outline-invisible-p))
  6221. (save-excursion
  6222. (goto-char (point-at-eol)) (outline-invisible-p)))
  6223. (hide-entry))))
  6224. (org-cycle-show-empty-lines 'overview)
  6225. (org-cycle-hide-drawers 'overview)))))
  6226. (defun org-cycle-show-empty-lines (state)
  6227. "Show empty lines above all visible headlines.
  6228. The region to be covered depends on STATE when called through
  6229. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6230. entire buffer covered. Note that an empty line is only shown if there
  6231. are at least `org-cycle-separator-lines' empty lines before the headline."
  6232. (when (not (= org-cycle-separator-lines 0))
  6233. (save-excursion
  6234. (let* ((n (abs org-cycle-separator-lines))
  6235. (re (cond
  6236. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6237. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6238. (t (let ((ns (number-to-string (- n 2))))
  6239. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6240. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6241. beg end b e)
  6242. (cond
  6243. ((memq state '(overview contents t))
  6244. (setq beg (point-min) end (point-max)))
  6245. ((memq state '(children folded))
  6246. (setq beg (point) end (progn (org-end-of-subtree t t)
  6247. (beginning-of-line 2)
  6248. (point)))))
  6249. (when beg
  6250. (goto-char beg)
  6251. (while (re-search-forward re end t)
  6252. (unless (get-char-property (match-end 1) 'invisible)
  6253. (setq e (match-end 1))
  6254. (if (< org-cycle-separator-lines 0)
  6255. (setq b (save-excursion
  6256. (goto-char (match-beginning 0))
  6257. (org-back-over-empty-lines)
  6258. (if (save-excursion
  6259. (goto-char (max (point-min) (1- (point))))
  6260. (org-at-heading-p))
  6261. (1- (point))
  6262. (point))))
  6263. (setq b (match-beginning 1)))
  6264. (outline-flag-region b e nil)))))))
  6265. ;; Never hide empty lines at the end of the file.
  6266. (save-excursion
  6267. (goto-char (point-max))
  6268. (outline-previous-heading)
  6269. (outline-end-of-heading)
  6270. (if (and (looking-at "[ \t\n]+")
  6271. (= (match-end 0) (point-max)))
  6272. (outline-flag-region (point) (match-end 0) nil))))
  6273. (defun org-show-empty-lines-in-parent ()
  6274. "Move to the parent and re-show empty lines before visible headlines."
  6275. (save-excursion
  6276. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6277. (org-cycle-show-empty-lines context))))
  6278. (defun org-files-list ()
  6279. "Return `org-agenda-files' list, plus all open org-mode files.
  6280. This is useful for operations that need to scan all of a user's
  6281. open and agenda-wise Org files."
  6282. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6283. (dolist (buf (buffer-list))
  6284. (with-current-buffer buf
  6285. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6286. (let ((file (expand-file-name (buffer-file-name))))
  6287. (unless (member file files)
  6288. (push file files))))))
  6289. files))
  6290. (defsubst org-entry-beginning-position ()
  6291. "Return the beginning position of the current entry."
  6292. (save-excursion (outline-back-to-heading t) (point)))
  6293. (defsubst org-entry-end-position ()
  6294. "Return the end position of the current entry."
  6295. (save-excursion (outline-next-heading) (point)))
  6296. (defun org-cycle-hide-drawers (state)
  6297. "Re-hide all drawers after a visibility state change."
  6298. (when (and (derived-mode-p 'org-mode)
  6299. (not (memq state '(overview folded contents))))
  6300. (save-excursion
  6301. (let* ((globalp (memq state '(contents all)))
  6302. (beg (if globalp (point-min) (point)))
  6303. (end (if globalp (point-max)
  6304. (if (eq state 'children)
  6305. (save-excursion (outline-next-heading) (point))
  6306. (org-end-of-subtree t)))))
  6307. (goto-char beg)
  6308. (while (re-search-forward org-drawer-regexp end t)
  6309. (org-flag-drawer t))))))
  6310. (defun org-cycle-hide-inline-tasks (state)
  6311. "Re-hide inline task when switching to 'contents visibility state."
  6312. (when (and (eq state 'contents)
  6313. (boundp 'org-inlinetask-min-level)
  6314. org-inlinetask-min-level)
  6315. (hide-sublevels (1- org-inlinetask-min-level))))
  6316. (defun org-flag-drawer (flag)
  6317. "When FLAG is non-nil, hide the drawer we are within.
  6318. Otherwise make it visible."
  6319. (save-excursion
  6320. (beginning-of-line 1)
  6321. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6322. (let ((b (match-end 0)))
  6323. (if (re-search-forward
  6324. "^[ \t]*:END:"
  6325. (save-excursion (outline-next-heading) (point)) t)
  6326. (outline-flag-region b (point-at-eol) flag)
  6327. (user-error ":END: line missing at position %s" b))))))
  6328. (defun org-subtree-end-visible-p ()
  6329. "Is the end of the current subtree visible?"
  6330. (pos-visible-in-window-p
  6331. (save-excursion (org-end-of-subtree t) (point))))
  6332. (defun org-first-headline-recenter (&optional N)
  6333. "Move cursor to the first headline and recenter the headline.
  6334. Optional argument N means put the headline into the Nth line of the window."
  6335. (goto-char (point-min))
  6336. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6337. (beginning-of-line)
  6338. (recenter (prefix-numeric-value N))))
  6339. ;;; Saving and restoring visibility
  6340. (defun org-outline-overlay-data (&optional use-markers)
  6341. "Return a list of the locations of all outline overlays.
  6342. These are overlays with the `invisible' property value `outline'.
  6343. The return value is a list of cons cells, with start and stop
  6344. positions for each overlay.
  6345. If USE-MARKERS is set, return the positions as markers."
  6346. (let (beg end)
  6347. (save-excursion
  6348. (save-restriction
  6349. (widen)
  6350. (delq nil
  6351. (mapcar (lambda (o)
  6352. (when (eq (overlay-get o 'invisible) 'outline)
  6353. (setq beg (overlay-start o)
  6354. end (overlay-end o))
  6355. (and beg end (> end beg)
  6356. (if use-markers
  6357. (cons (move-marker (make-marker) beg)
  6358. (move-marker (make-marker) end))
  6359. (cons beg end)))))
  6360. (overlays-in (point-min) (point-max))))))))
  6361. (defun org-set-outline-overlay-data (data)
  6362. "Create visibility overlays for all positions in DATA.
  6363. DATA should have been made by `org-outline-overlay-data'."
  6364. (let (o)
  6365. (save-excursion
  6366. (save-restriction
  6367. (widen)
  6368. (show-all)
  6369. (mapc (lambda (c)
  6370. (outline-flag-region (car c) (cdr c) t))
  6371. data)))))
  6372. ;;; Folding of blocks
  6373. (defvar org-hide-block-overlays nil
  6374. "Overlays hiding blocks.")
  6375. (make-variable-buffer-local 'org-hide-block-overlays)
  6376. (defun org-block-map (function &optional start end)
  6377. "Call FUNCTION at the head of all source blocks in the current buffer.
  6378. Optional arguments START and END can be used to limit the range."
  6379. (let ((start (or start (point-min)))
  6380. (end (or end (point-max))))
  6381. (save-excursion
  6382. (goto-char start)
  6383. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6384. (save-excursion
  6385. (save-match-data
  6386. (goto-char (match-beginning 0))
  6387. (funcall function)))))))
  6388. (defun org-hide-block-toggle-all ()
  6389. "Toggle the visibility of all blocks in the current buffer."
  6390. (org-block-map #'org-hide-block-toggle))
  6391. (defun org-hide-block-all ()
  6392. "Fold all blocks in the current buffer."
  6393. (interactive)
  6394. (org-show-block-all)
  6395. (org-block-map #'org-hide-block-toggle-maybe))
  6396. (defun org-show-block-all ()
  6397. "Unfold all blocks in the current buffer."
  6398. (interactive)
  6399. (mapc 'delete-overlay org-hide-block-overlays)
  6400. (setq org-hide-block-overlays nil))
  6401. (defun org-hide-block-toggle-maybe ()
  6402. "Toggle visibility of block at point."
  6403. (interactive)
  6404. (let ((case-fold-search t))
  6405. (if (save-excursion
  6406. (beginning-of-line 1)
  6407. (looking-at org-block-regexp))
  6408. (progn (org-hide-block-toggle)
  6409. t) ;; to signal that we took action
  6410. nil))) ;; to signal that we did not
  6411. (defun org-hide-block-toggle (&optional force)
  6412. "Toggle the visibility of the current block."
  6413. (interactive)
  6414. (save-excursion
  6415. (beginning-of-line)
  6416. (if (re-search-forward org-block-regexp nil t)
  6417. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6418. (end (match-end 0)) ;; end of entire body
  6419. ov)
  6420. (if (memq t (mapcar (lambda (overlay)
  6421. (eq (overlay-get overlay 'invisible)
  6422. 'org-hide-block))
  6423. (overlays-at start)))
  6424. (if (or (not force) (eq force 'off))
  6425. (mapc (lambda (ov)
  6426. (when (member ov org-hide-block-overlays)
  6427. (setq org-hide-block-overlays
  6428. (delq ov org-hide-block-overlays)))
  6429. (when (eq (overlay-get ov 'invisible)
  6430. 'org-hide-block)
  6431. (delete-overlay ov)))
  6432. (overlays-at start)))
  6433. (setq ov (make-overlay start end))
  6434. (overlay-put ov 'invisible 'org-hide-block)
  6435. ;; make the block accessible to isearch
  6436. (overlay-put
  6437. ov 'isearch-open-invisible
  6438. (lambda (ov)
  6439. (when (member ov org-hide-block-overlays)
  6440. (setq org-hide-block-overlays
  6441. (delq ov org-hide-block-overlays)))
  6442. (when (eq (overlay-get ov 'invisible)
  6443. 'org-hide-block)
  6444. (delete-overlay ov))))
  6445. (push ov org-hide-block-overlays)))
  6446. (user-error "Not looking at a source block"))))
  6447. ;; org-tab-after-check-for-cycling-hook
  6448. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6449. ;; Remove overlays when changing major mode
  6450. (add-hook 'org-mode-hook
  6451. (lambda () (org-add-hook 'change-major-mode-hook
  6452. 'org-show-block-all 'append 'local)))
  6453. ;;; Org-goto
  6454. (defvar org-goto-window-configuration nil)
  6455. (defvar org-goto-marker nil)
  6456. (defvar org-goto-map)
  6457. (defun org-goto-map ()
  6458. "Set the keymap `org-goto'."
  6459. (setq org-goto-map
  6460. (let ((map (make-sparse-keymap)))
  6461. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6462. mouse-drag-region universal-argument org-occur))
  6463. cmd)
  6464. (while (setq cmd (pop cmds))
  6465. (substitute-key-definition cmd cmd map global-map)))
  6466. (suppress-keymap map)
  6467. (org-defkey map "\C-m" 'org-goto-ret)
  6468. (org-defkey map [(return)] 'org-goto-ret)
  6469. (org-defkey map [(left)] 'org-goto-left)
  6470. (org-defkey map [(right)] 'org-goto-right)
  6471. (org-defkey map [(control ?g)] 'org-goto-quit)
  6472. (org-defkey map "\C-i" 'org-cycle)
  6473. (org-defkey map [(tab)] 'org-cycle)
  6474. (org-defkey map [(down)] 'outline-next-visible-heading)
  6475. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6476. (if org-goto-auto-isearch
  6477. (if (fboundp 'define-key-after)
  6478. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6479. nil)
  6480. (org-defkey map "q" 'org-goto-quit)
  6481. (org-defkey map "n" 'outline-next-visible-heading)
  6482. (org-defkey map "p" 'outline-previous-visible-heading)
  6483. (org-defkey map "f" 'outline-forward-same-level)
  6484. (org-defkey map "b" 'outline-backward-same-level)
  6485. (org-defkey map "u" 'outline-up-heading))
  6486. (org-defkey map "/" 'org-occur)
  6487. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6488. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6489. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6490. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6491. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6492. map)))
  6493. (defconst org-goto-help
  6494. "Browse buffer copy, to find location or copy text.%s
  6495. RET=jump to location C-g=quit and return to previous location
  6496. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6497. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6498. (defun org-goto (&optional alternative-interface)
  6499. "Look up a different location in the current file, keeping current visibility.
  6500. When you want look-up or go to a different location in a
  6501. document, the fastest way is often to fold the entire buffer and
  6502. then dive into the tree. This method has the disadvantage, that
  6503. the previous location will be folded, which may not be what you
  6504. want.
  6505. This command works around this by showing a copy of the current
  6506. buffer in an indirect buffer, in overview mode. You can dive
  6507. into the tree in that copy, use org-occur and incremental search
  6508. to find a location. When pressing RET or `Q', the command
  6509. returns to the original buffer in which the visibility is still
  6510. unchanged. After RET it will also jump to the location selected
  6511. in the indirect buffer and expose the headline hierarchy above.
  6512. With a prefix argument, use the alternative interface: e.g. if
  6513. `org-goto-interface' is 'outline use 'outline-path-completion."
  6514. (interactive "P")
  6515. (org-goto-map)
  6516. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6517. (org-refile-use-outline-path t)
  6518. (org-refile-target-verify-function nil)
  6519. (interface
  6520. (if (not alternative-interface)
  6521. org-goto-interface
  6522. (if (eq org-goto-interface 'outline)
  6523. 'outline-path-completion
  6524. 'outline)))
  6525. (org-goto-start-pos (point))
  6526. (selected-point
  6527. (if (eq interface 'outline)
  6528. (car (org-get-location (current-buffer) org-goto-help))
  6529. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6530. (org-refile-check-position pa)
  6531. (nth 3 pa)))))
  6532. (if selected-point
  6533. (progn
  6534. (org-mark-ring-push org-goto-start-pos)
  6535. (goto-char selected-point)
  6536. (if (or (outline-invisible-p) (org-invisible-p2))
  6537. (org-show-context 'org-goto)))
  6538. (message "Quit"))))
  6539. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6540. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6541. (defvar org-goto-local-auto-isearch-map) ; defined below
  6542. (defun org-get-location (buf help)
  6543. "Let the user select a location in the Org-mode buffer BUF.
  6544. This function uses a recursive edit. It returns the selected position
  6545. or nil."
  6546. (org-no-popups
  6547. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6548. (isearch-hide-immediately nil)
  6549. (isearch-search-fun-function
  6550. (lambda () 'org-goto-local-search-headings))
  6551. (org-goto-selected-point org-goto-exit-command))
  6552. (save-excursion
  6553. (save-window-excursion
  6554. (delete-other-windows)
  6555. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6556. (org-pop-to-buffer-same-window
  6557. (condition-case nil
  6558. (make-indirect-buffer (current-buffer) "*org-goto*")
  6559. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6560. (with-output-to-temp-buffer "*Org Help*"
  6561. (princ (format help (if org-goto-auto-isearch
  6562. " Just type for auto-isearch."
  6563. " n/p/f/b/u to navigate, q to quit."))))
  6564. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6565. (setq buffer-read-only nil)
  6566. (let ((org-startup-truncated t)
  6567. (org-startup-folded nil)
  6568. (org-startup-align-all-tables nil))
  6569. (org-mode)
  6570. (org-overview))
  6571. (setq buffer-read-only t)
  6572. (if (and (boundp 'org-goto-start-pos)
  6573. (integer-or-marker-p org-goto-start-pos))
  6574. (let ((org-show-hierarchy-above t)
  6575. (org-show-siblings t)
  6576. (org-show-following-heading t))
  6577. (goto-char org-goto-start-pos)
  6578. (and (outline-invisible-p) (org-show-context)))
  6579. (goto-char (point-min)))
  6580. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6581. (message "Select location and press RET")
  6582. (use-local-map org-goto-map)
  6583. (recursive-edit)))
  6584. (kill-buffer "*org-goto*")
  6585. (cons org-goto-selected-point org-goto-exit-command))))
  6586. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6587. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6588. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6589. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6590. (defun org-goto-local-search-headings (string bound noerror)
  6591. "Search and make sure that any matches are in headlines."
  6592. (catch 'return
  6593. (while (if isearch-forward
  6594. (search-forward string bound noerror)
  6595. (search-backward string bound noerror))
  6596. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6597. (and (member :headline context)
  6598. (not (member :tags context))))
  6599. (throw 'return (point))))))
  6600. (defun org-goto-local-auto-isearch ()
  6601. "Start isearch."
  6602. (interactive)
  6603. (goto-char (point-min))
  6604. (let ((keys (this-command-keys)))
  6605. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6606. (isearch-mode t)
  6607. (isearch-process-search-char (string-to-char keys)))))
  6608. (defun org-goto-ret (&optional arg)
  6609. "Finish `org-goto' by going to the new location."
  6610. (interactive "P")
  6611. (setq org-goto-selected-point (point)
  6612. org-goto-exit-command 'return)
  6613. (throw 'exit nil))
  6614. (defun org-goto-left ()
  6615. "Finish `org-goto' by going to the new location."
  6616. (interactive)
  6617. (if (org-at-heading-p)
  6618. (progn
  6619. (beginning-of-line 1)
  6620. (setq org-goto-selected-point (point)
  6621. org-goto-exit-command 'left)
  6622. (throw 'exit nil))
  6623. (user-error "Not on a heading")))
  6624. (defun org-goto-right ()
  6625. "Finish `org-goto' by going to the new location."
  6626. (interactive)
  6627. (if (org-at-heading-p)
  6628. (progn
  6629. (setq org-goto-selected-point (point)
  6630. org-goto-exit-command 'right)
  6631. (throw 'exit nil))
  6632. (user-error "Not on a heading")))
  6633. (defun org-goto-quit ()
  6634. "Finish `org-goto' without cursor motion."
  6635. (interactive)
  6636. (setq org-goto-selected-point nil)
  6637. (setq org-goto-exit-command 'quit)
  6638. (throw 'exit nil))
  6639. ;;; Indirect buffer display of subtrees
  6640. (defvar org-indirect-dedicated-frame nil
  6641. "This is the frame being used for indirect tree display.")
  6642. (defvar org-last-indirect-buffer nil)
  6643. (defun org-tree-to-indirect-buffer (&optional arg)
  6644. "Create indirect buffer and narrow it to current subtree.
  6645. With a numerical prefix ARG, go up to this level and then take that tree.
  6646. If ARG is negative, go up that many levels.
  6647. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6648. indirect buffer previously made with this command, to avoid proliferation of
  6649. indirect buffers. However, when you call the command with a \
  6650. \\[universal-argument] prefix, or
  6651. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6652. is kept so that you can work with several indirect buffers at the same time.
  6653. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6654. \\[universal-argument] prefix also
  6655. requests that a new frame be made for the new buffer, so that the dedicated
  6656. frame is not changed."
  6657. (interactive "P")
  6658. (let ((cbuf (current-buffer))
  6659. (cwin (selected-window))
  6660. (pos (point))
  6661. beg end level heading ibuf)
  6662. (save-excursion
  6663. (org-back-to-heading t)
  6664. (when (numberp arg)
  6665. (setq level (org-outline-level))
  6666. (if (< arg 0) (setq arg (+ level arg)))
  6667. (while (> (setq level (org-outline-level)) arg)
  6668. (org-up-heading-safe)))
  6669. (setq beg (point)
  6670. heading (org-get-heading))
  6671. (org-end-of-subtree t t)
  6672. (if (org-at-heading-p) (backward-char 1))
  6673. (setq end (point)))
  6674. (if (and (buffer-live-p org-last-indirect-buffer)
  6675. (not (eq org-indirect-buffer-display 'new-frame))
  6676. (not arg))
  6677. (kill-buffer org-last-indirect-buffer))
  6678. (setq ibuf (org-get-indirect-buffer cbuf)
  6679. org-last-indirect-buffer ibuf)
  6680. (cond
  6681. ((or (eq org-indirect-buffer-display 'new-frame)
  6682. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6683. (select-frame (make-frame))
  6684. (delete-other-windows)
  6685. (org-pop-to-buffer-same-window ibuf)
  6686. (org-set-frame-title heading))
  6687. ((eq org-indirect-buffer-display 'dedicated-frame)
  6688. (raise-frame
  6689. (select-frame (or (and org-indirect-dedicated-frame
  6690. (frame-live-p org-indirect-dedicated-frame)
  6691. org-indirect-dedicated-frame)
  6692. (setq org-indirect-dedicated-frame (make-frame)))))
  6693. (delete-other-windows)
  6694. (org-pop-to-buffer-same-window ibuf)
  6695. (org-set-frame-title (concat "Indirect: " heading)))
  6696. ((eq org-indirect-buffer-display 'current-window)
  6697. (org-pop-to-buffer-same-window ibuf))
  6698. ((eq org-indirect-buffer-display 'other-window)
  6699. (pop-to-buffer ibuf))
  6700. (t (error "Invalid value")))
  6701. (if (featurep 'xemacs)
  6702. (save-excursion (org-mode) (turn-on-font-lock)))
  6703. (narrow-to-region beg end)
  6704. (show-all)
  6705. (goto-char pos)
  6706. (run-hook-with-args 'org-cycle-hook 'all)
  6707. (and (window-live-p cwin) (select-window cwin))))
  6708. (defun org-get-indirect-buffer (&optional buffer)
  6709. (setq buffer (or buffer (current-buffer)))
  6710. (let ((n 1) (base (buffer-name buffer)) bname)
  6711. (while (buffer-live-p
  6712. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6713. (setq n (1+ n)))
  6714. (condition-case nil
  6715. (make-indirect-buffer buffer bname 'clone)
  6716. (error (make-indirect-buffer buffer bname)))))
  6717. (defun org-set-frame-title (title)
  6718. "Set the title of the current frame to the string TITLE."
  6719. ;; FIXME: how to name a single frame in XEmacs???
  6720. (unless (featurep 'xemacs)
  6721. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6722. ;;;; Structure editing
  6723. ;;; Inserting headlines
  6724. (defun org-previous-line-empty-p (&optional next)
  6725. "Is the previous line a blank line?
  6726. When NEXT is non-nil, check the next line instead."
  6727. (save-excursion
  6728. (and (not (bobp))
  6729. (or (beginning-of-line (if next 2 0)) t)
  6730. (save-match-data
  6731. (looking-at "[ \t]*$")))))
  6732. (defun org-insert-heading (&optional arg invisible-ok)
  6733. "Insert a new heading or item with same depth at point.
  6734. If point is in a plain list and ARG is nil, create a new list item.
  6735. With one universal prefix argument, insert a heading even in lists.
  6736. With two universal prefix arguments, insert the heading at the end
  6737. of the parent subtree.
  6738. If point is at the beginning of a headline, insert a sibling before
  6739. the current headline. If point is not at the beginning, split the line
  6740. and create a new headline with the text in the current line after point
  6741. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6742. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6743. This is important for non-interactive uses of the command."
  6744. (interactive "P")
  6745. (if (org-called-interactively-p 'any) (org-reveal))
  6746. (let ((itemp (org-in-item-p)))
  6747. (cond
  6748. ((or (= (buffer-size) 0)
  6749. (and (not (save-excursion
  6750. (and (ignore-errors (org-back-to-heading invisible-ok))
  6751. (org-at-heading-p))))
  6752. (or arg (not itemp))))
  6753. (insert
  6754. (if (org-previous-line-empty-p) "" "\n")
  6755. (if (org-in-src-block-p) ",* " "* "))
  6756. (run-hooks 'org-insert-heading-hook))
  6757. ((or arg
  6758. (and (not itemp) org-insert-heading-respect-content)
  6759. (not (org-insert-item
  6760. (save-excursion
  6761. (and itemp
  6762. (goto-char itemp)
  6763. (looking-at org-list-full-item-re)
  6764. (match-string 3))))))
  6765. (let (begn endn)
  6766. (when (org-buffer-narrowed-p)
  6767. (setq begn (point-min) endn (point-max))
  6768. (widen))
  6769. (let* ((empty-line-p nil)
  6770. (eops (equal arg '(16))) ; insert at end of parent subtree
  6771. (org-insert-heading-respect-content
  6772. (or (not (null arg)) org-insert-heading-respect-content))
  6773. (level nil)
  6774. (on-heading (org-at-heading-p))
  6775. ;; Get a level to fall back on
  6776. (fix-level
  6777. (save-excursion
  6778. (org-back-to-heading t)
  6779. (looking-at org-outline-regexp)
  6780. (make-string (1- (length (match-string 0))) ?*)))
  6781. (on-empty-line
  6782. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6783. (head (save-excursion
  6784. (condition-case nil
  6785. (progn
  6786. (org-back-to-heading invisible-ok)
  6787. (when (and (not on-heading)
  6788. (featurep 'org-inlinetask)
  6789. (integerp org-inlinetask-min-level)
  6790. (>= (length (match-string 0))
  6791. org-inlinetask-min-level))
  6792. ;; Find a heading level before the inline task
  6793. (while (and (setq level (org-up-heading-safe))
  6794. (>= level org-inlinetask-min-level)))
  6795. (if (org-at-heading-p)
  6796. (org-back-to-heading invisible-ok)
  6797. (error "This should not happen")))
  6798. (unless (and (save-excursion
  6799. (save-match-data
  6800. (org-backward-heading-same-level 1 invisible-ok))
  6801. (= (point) (match-beginning 0)))
  6802. (not (org-previous-line-empty-p t)))
  6803. (setq empty-line-p (org-previous-line-empty-p)))
  6804. (match-string 0))
  6805. (error (or fix-level "* ")))))
  6806. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6807. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6808. pos hide-previous previous-pos)
  6809. (if ;; At the beginning of a heading, open a new line for insertion
  6810. (and (bolp) (org-at-heading-p)
  6811. (not eops)
  6812. (or (bobp)
  6813. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6814. (open-line (if blank 2 1))
  6815. (save-excursion
  6816. (setq previous-pos (point-at-bol))
  6817. (end-of-line)
  6818. (setq hide-previous (outline-invisible-p)))
  6819. (and org-insert-heading-respect-content
  6820. (save-excursion
  6821. (while (outline-invisible-p)
  6822. (org-show-subtree)
  6823. (org-up-heading-safe))))
  6824. (let ((split
  6825. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6826. (save-excursion
  6827. (let ((p (point)))
  6828. (goto-char (point-at-bol))
  6829. (and (looking-at org-complex-heading-regexp)
  6830. (match-beginning 4)
  6831. (> p (match-beginning 4)))))))
  6832. tags pos)
  6833. (cond
  6834. ;; Insert a new line, possibly at end of parent subtree
  6835. ((and (not arg) (not on-heading) (not on-empty-line)
  6836. (not (save-excursion
  6837. (beginning-of-line 1)
  6838. (or (looking-at org-list-full-item-re)
  6839. ;; Don't convert :end: lines to headline
  6840. (looking-at "^\\s-*:end:")
  6841. (looking-at "^\\s-*#\\+end_?")))))
  6842. (beginning-of-line 1))
  6843. (org-insert-heading-respect-content
  6844. (if (not eops)
  6845. (progn
  6846. (org-end-of-subtree nil t)
  6847. (and (looking-at "^\\*") (backward-char 1))
  6848. (while (and (not (bobp))
  6849. ;; Don't delete spaces in empty headlines
  6850. (not (looking-back org-outline-regexp))
  6851. (member (char-before) '(?\ ?\t ?\n)))
  6852. (backward-delete-char 1)))
  6853. (let ((p (point)))
  6854. (org-up-heading-safe)
  6855. (if (= p (point))
  6856. (goto-char (point-max))
  6857. (org-end-of-subtree nil t))))
  6858. (when (featurep 'org-inlinetask)
  6859. (while (and (not (eobp))
  6860. (looking-at "\\(\\*+\\)[ \t]+")
  6861. (>= (length (match-string 1))
  6862. org-inlinetask-min-level))
  6863. (org-end-of-subtree nil t)))
  6864. (or (bolp) (newline))
  6865. (or (org-previous-line-empty-p)
  6866. (and blank (newline)))
  6867. (if (or empty-line-p eops) (open-line 1)))
  6868. ;; Insert a headling containing text after point
  6869. ((org-at-heading-p)
  6870. (when hide-previous
  6871. (show-children)
  6872. (org-show-entry))
  6873. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6874. (setq tags (and (match-end 2) (match-string 2)))
  6875. (and (match-end 1)
  6876. (delete-region (match-beginning 1) (match-end 1)))
  6877. (setq pos (point-at-bol))
  6878. (or split (end-of-line 1))
  6879. (delete-horizontal-space)
  6880. (if (string-match "\\`\\*+\\'"
  6881. (buffer-substring (point-at-bol) (point)))
  6882. (insert " "))
  6883. (newline (if blank 2 1))
  6884. (when tags
  6885. (save-excursion
  6886. (goto-char pos)
  6887. (end-of-line 1)
  6888. (insert " " tags)
  6889. (org-set-tags nil 'align))))
  6890. (t
  6891. (or split (end-of-line 1))
  6892. (newline (cond ((and blank (not on-empty-line)) 2)
  6893. (blank 1)
  6894. (on-empty-line 0) (t 1)))))))
  6895. (insert head) (just-one-space)
  6896. (setq pos (point))
  6897. (end-of-line 1)
  6898. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6899. (when (and org-insert-heading-respect-content hide-previous)
  6900. (save-excursion
  6901. (goto-char previous-pos)
  6902. (hide-subtree)))
  6903. (when (and begn endn)
  6904. (narrow-to-region (min (point) begn) (max (point) endn)))
  6905. (run-hooks 'org-insert-heading-hook)))))))
  6906. (defun org-get-heading (&optional no-tags no-todo)
  6907. "Return the heading of the current entry, without the stars.
  6908. When NO-TAGS is non-nil, don't include tags.
  6909. When NO-TODO is non-nil, don't include TODO keywords."
  6910. (save-excursion
  6911. (org-back-to-heading t)
  6912. (cond
  6913. ((and no-tags no-todo)
  6914. (looking-at org-complex-heading-regexp)
  6915. (match-string 4))
  6916. (no-tags
  6917. (looking-at (concat org-outline-regexp
  6918. "\\(.*?\\)"
  6919. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6920. (match-string 1))
  6921. (no-todo
  6922. (looking-at org-todo-line-regexp)
  6923. (match-string 3))
  6924. (t (looking-at org-heading-regexp)
  6925. (match-string 2)))))
  6926. (defvar orgstruct-mode) ; defined below
  6927. (defun org-heading-components ()
  6928. "Return the components of the current heading.
  6929. This is a list with the following elements:
  6930. - the level as an integer
  6931. - the reduced level, different if `org-odd-levels-only' is set.
  6932. - the TODO keyword, or nil
  6933. - the priority character, like ?A, or nil if no priority is given
  6934. - the headline text itself, or the tags string if no headline text
  6935. - the tags string, or nil."
  6936. (save-excursion
  6937. (org-back-to-heading t)
  6938. (if (let (case-fold-search)
  6939. (looking-at
  6940. (if orgstruct-mode
  6941. org-heading-regexp
  6942. org-complex-heading-regexp)))
  6943. (if orgstruct-mode
  6944. (list (length (match-string 1))
  6945. (org-reduced-level (length (match-string 1)))
  6946. nil
  6947. nil
  6948. (match-string 2)
  6949. nil)
  6950. (list (length (match-string 1))
  6951. (org-reduced-level (length (match-string 1)))
  6952. (org-match-string-no-properties 2)
  6953. (and (match-end 3) (aref (match-string 3) 2))
  6954. (org-match-string-no-properties 4)
  6955. (org-match-string-no-properties 5))))))
  6956. (defun org-get-entry ()
  6957. "Get the entry text, after heading, entire subtree."
  6958. (save-excursion
  6959. (org-back-to-heading t)
  6960. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6961. (defun org-insert-heading-after-current ()
  6962. "Insert a new heading with same level as current, after current subtree."
  6963. (interactive)
  6964. (org-back-to-heading)
  6965. (org-insert-heading)
  6966. (org-move-subtree-down)
  6967. (end-of-line 1))
  6968. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6969. "Insert heading with `org-insert-heading-respect-content' set to t."
  6970. (interactive "P")
  6971. (let ((org-insert-heading-respect-content t))
  6972. (org-insert-heading arg invisible-ok)))
  6973. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6974. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6975. (interactive "P")
  6976. (let ((org-insert-heading-respect-content t))
  6977. (org-insert-todo-heading force-state t)))
  6978. (defun org-insert-todo-heading (arg &optional force-heading)
  6979. "Insert a new heading with the same level and TODO state as current heading.
  6980. If the heading has no TODO state, or if the state is DONE, use the first
  6981. state (TODO by default). Also one prefix arg, force first state. With two
  6982. prefix args, force inserting at the end of the parent subtree."
  6983. (interactive "P")
  6984. (when (or force-heading (not (org-insert-item 'checkbox)))
  6985. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6986. force-heading))
  6987. (save-excursion
  6988. (org-back-to-heading)
  6989. (outline-previous-heading)
  6990. (looking-at org-todo-line-regexp))
  6991. (let*
  6992. ((new-mark-x
  6993. (if (or arg
  6994. (not (match-beginning 2))
  6995. (member (match-string 2) org-done-keywords))
  6996. (car org-todo-keywords-1)
  6997. (match-string 2)))
  6998. (new-mark
  6999. (or
  7000. (run-hook-with-args-until-success
  7001. 'org-todo-get-default-hook new-mark-x nil)
  7002. new-mark-x)))
  7003. (beginning-of-line 1)
  7004. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7005. (if org-treat-insert-todo-heading-as-state-change
  7006. (org-todo new-mark)
  7007. (insert new-mark " "))))
  7008. (when org-provide-todo-statistics
  7009. (org-update-parent-todo-statistics))))
  7010. (defun org-insert-subheading (arg)
  7011. "Insert a new subheading and demote it.
  7012. Works for outline headings and for plain lists alike."
  7013. (interactive "P")
  7014. (org-insert-heading arg)
  7015. (cond
  7016. ((org-at-heading-p) (org-do-demote))
  7017. ((org-at-item-p) (org-indent-item))))
  7018. (defun org-insert-todo-subheading (arg)
  7019. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7020. Works for outline headings and for plain lists alike."
  7021. (interactive "P")
  7022. (org-insert-todo-heading arg)
  7023. (cond
  7024. ((org-at-heading-p) (org-do-demote))
  7025. ((org-at-item-p) (org-indent-item))))
  7026. ;;; Promotion and Demotion
  7027. (defvar org-after-demote-entry-hook nil
  7028. "Hook run after an entry has been demoted.
  7029. The cursor will be at the beginning of the entry.
  7030. When a subtree is being demoted, the hook will be called for each node.")
  7031. (defvar org-after-promote-entry-hook nil
  7032. "Hook run after an entry has been promoted.
  7033. The cursor will be at the beginning of the entry.
  7034. When a subtree is being promoted, the hook will be called for each node.")
  7035. (defun org-promote-subtree ()
  7036. "Promote the entire subtree.
  7037. See also `org-promote'."
  7038. (interactive)
  7039. (save-excursion
  7040. (org-with-limited-levels (org-map-tree 'org-promote)))
  7041. (org-fix-position-after-promote))
  7042. (defun org-demote-subtree ()
  7043. "Demote the entire subtree. See `org-demote'.
  7044. See also `org-promote'."
  7045. (interactive)
  7046. (save-excursion
  7047. (org-with-limited-levels (org-map-tree 'org-demote)))
  7048. (org-fix-position-after-promote))
  7049. (defun org-do-promote ()
  7050. "Promote the current heading higher up the tree.
  7051. If the region is active in `transient-mark-mode', promote all headings
  7052. in the region."
  7053. (interactive)
  7054. (save-excursion
  7055. (if (org-region-active-p)
  7056. (org-map-region 'org-promote (region-beginning) (region-end))
  7057. (org-promote)))
  7058. (org-fix-position-after-promote))
  7059. (defun org-do-demote ()
  7060. "Demote the current heading lower down the tree.
  7061. If the region is active in `transient-mark-mode', demote all headings
  7062. in the region."
  7063. (interactive)
  7064. (save-excursion
  7065. (if (org-region-active-p)
  7066. (org-map-region 'org-demote (region-beginning) (region-end))
  7067. (org-demote)))
  7068. (org-fix-position-after-promote))
  7069. (defun org-fix-position-after-promote ()
  7070. "Make sure that after pro/demotion cursor position is right."
  7071. (let ((pos (point)))
  7072. (when (save-excursion
  7073. (beginning-of-line 1)
  7074. (looking-at org-todo-line-regexp)
  7075. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7076. (cond ((eobp) (insert " "))
  7077. ((eolp) (insert " "))
  7078. ((equal (char-after) ?\ ) (forward-char 1))))))
  7079. (defun org-current-level ()
  7080. "Return the level of the current entry, or nil if before the first headline.
  7081. The level is the number of stars at the beginning of the headline."
  7082. (save-excursion
  7083. (org-with-limited-levels
  7084. (if (ignore-errors (org-back-to-heading t))
  7085. (funcall outline-level)))))
  7086. (defun org-get-previous-line-level ()
  7087. "Return the outline depth of the last headline before the current line.
  7088. Returns 0 for the first headline in the buffer, and nil if before the
  7089. first headline."
  7090. (let ((current-level (org-current-level))
  7091. (prev-level (when (> (line-number-at-pos) 1)
  7092. (save-excursion
  7093. (beginning-of-line 0)
  7094. (org-current-level)))))
  7095. (cond ((null current-level) nil) ; Before first headline
  7096. ((null prev-level) 0) ; At first headline
  7097. (prev-level))))
  7098. (defun org-reduced-level (l)
  7099. "Compute the effective level of a heading.
  7100. This takes into account the setting of `org-odd-levels-only'."
  7101. (cond
  7102. ((zerop l) 0)
  7103. (org-odd-levels-only (1+ (floor (/ l 2))))
  7104. (t l)))
  7105. (defun org-level-increment ()
  7106. "Return the number of stars that will be added or removed at a
  7107. time to headlines when structure editing, based on the value of
  7108. `org-odd-levels-only'."
  7109. (if org-odd-levels-only 2 1))
  7110. (defun org-get-valid-level (level &optional change)
  7111. "Rectify a level change under the influence of `org-odd-levels-only'
  7112. LEVEL is a current level, CHANGE is by how much the level should be
  7113. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7114. even level numbers will become the next higher odd number."
  7115. (if org-odd-levels-only
  7116. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7117. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7118. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7119. (max 1 (+ level (or change 0)))))
  7120. (if (boundp 'define-obsolete-function-alias)
  7121. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7122. (define-obsolete-function-alias 'org-get-legal-level
  7123. 'org-get-valid-level)
  7124. (define-obsolete-function-alias 'org-get-legal-level
  7125. 'org-get-valid-level "23.1")))
  7126. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7127. ;; ̀org-with-limited-levels'
  7128. (defun org-promote ()
  7129. "Promote the current heading higher up the tree.
  7130. If the region is active in `transient-mark-mode', promote all headings
  7131. in the region."
  7132. (org-back-to-heading t)
  7133. (let* ((level (save-match-data (funcall outline-level)))
  7134. (after-change-functions (remove 'flyspell-after-change-function
  7135. after-change-functions))
  7136. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7137. (diff (abs (- level (length up-head) -1))))
  7138. (cond ((and (= level 1) org-called-with-limited-levels
  7139. org-allow-promoting-top-level-subtree)
  7140. (replace-match "# " nil t))
  7141. ((= level 1)
  7142. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7143. (t (replace-match up-head nil t)))
  7144. ;; Fixup tag positioning
  7145. (unless (= level 1)
  7146. (and org-auto-align-tags (org-set-tags nil t))
  7147. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7148. (run-hooks 'org-after-promote-entry-hook)))
  7149. (defun org-demote ()
  7150. "Demote the current heading lower down the tree.
  7151. If the region is active in `transient-mark-mode', demote all headings
  7152. in the region."
  7153. (org-back-to-heading t)
  7154. (let* ((level (save-match-data (funcall outline-level)))
  7155. (after-change-functions (remove 'flyspell-after-change-function
  7156. after-change-functions))
  7157. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7158. (diff (abs (- level (length down-head) -1))))
  7159. (replace-match down-head nil t)
  7160. ;; Fixup tag positioning
  7161. (and org-auto-align-tags (org-set-tags nil t))
  7162. (if org-adapt-indentation (org-fixup-indentation diff))
  7163. (run-hooks 'org-after-demote-entry-hook)))
  7164. (defun org-cycle-level ()
  7165. "Cycle the level of an empty headline through possible states.
  7166. This goes first to child, then to parent, level, then up the hierarchy.
  7167. After top level, it switches back to sibling level."
  7168. (interactive)
  7169. (let ((org-adapt-indentation nil))
  7170. (when (org-point-at-end-of-empty-headline)
  7171. (setq this-command 'org-cycle-level) ; Only needed for caching
  7172. (let ((cur-level (org-current-level))
  7173. (prev-level (org-get-previous-line-level)))
  7174. (cond
  7175. ;; If first headline in file, promote to top-level.
  7176. ((= prev-level 0)
  7177. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7178. do (org-do-promote)))
  7179. ;; If same level as prev, demote one.
  7180. ((= prev-level cur-level)
  7181. (org-do-demote))
  7182. ;; If parent is top-level, promote to top level if not already.
  7183. ((= prev-level 1)
  7184. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7185. do (org-do-promote)))
  7186. ;; If top-level, return to prev-level.
  7187. ((= cur-level 1)
  7188. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7189. do (org-do-demote)))
  7190. ;; If less than prev-level, promote one.
  7191. ((< cur-level prev-level)
  7192. (org-do-promote))
  7193. ;; If deeper than prev-level, promote until higher than
  7194. ;; prev-level.
  7195. ((> cur-level prev-level)
  7196. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7197. do (org-do-promote))))
  7198. t))))
  7199. (defun org-map-tree (fun)
  7200. "Call FUN for every heading underneath the current one."
  7201. (org-back-to-heading)
  7202. (let ((level (funcall outline-level)))
  7203. (save-excursion
  7204. (funcall fun)
  7205. (while (and (progn
  7206. (outline-next-heading)
  7207. (> (funcall outline-level) level))
  7208. (not (eobp)))
  7209. (funcall fun)))))
  7210. (defun org-map-region (fun beg end)
  7211. "Call FUN for every heading between BEG and END."
  7212. (let ((org-ignore-region t))
  7213. (save-excursion
  7214. (setq end (copy-marker end))
  7215. (goto-char beg)
  7216. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7217. (< (point) end))
  7218. (funcall fun))
  7219. (while (and (progn
  7220. (outline-next-heading)
  7221. (< (point) end))
  7222. (not (eobp)))
  7223. (funcall fun)))))
  7224. (defvar org-property-end-re) ; silence byte-compiler
  7225. (defun org-fixup-indentation (diff)
  7226. "Change the indentation in the current entry by DIFF.
  7227. However, if any line in the current entry has no indentation, or if it
  7228. would end up with no indentation after the change, nothing at all is done."
  7229. (save-excursion
  7230. (let ((end (save-excursion (outline-next-heading)
  7231. (point-marker)))
  7232. (prohibit (if (> diff 0)
  7233. "^\\S-"
  7234. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7235. col)
  7236. (unless (save-excursion (end-of-line 1)
  7237. (re-search-forward prohibit end t))
  7238. (while (and (< (point) end)
  7239. (re-search-forward "^[ \t]+" end t))
  7240. (goto-char (match-end 0))
  7241. (setq col (current-column))
  7242. (if (< diff 0) (replace-match ""))
  7243. (org-indent-to-column (+ diff col))))
  7244. (move-marker end nil))))
  7245. (defun org-convert-to-odd-levels ()
  7246. "Convert an org-mode file with all levels allowed to one with odd levels.
  7247. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7248. level 5 etc."
  7249. (interactive)
  7250. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7251. (let ((outline-level 'org-outline-level)
  7252. (org-odd-levels-only nil) n)
  7253. (save-excursion
  7254. (goto-char (point-min))
  7255. (while (re-search-forward "^\\*\\*+ " nil t)
  7256. (setq n (- (length (match-string 0)) 2))
  7257. (while (>= (setq n (1- n)) 0)
  7258. (org-demote))
  7259. (end-of-line 1))))))
  7260. (defun org-convert-to-oddeven-levels ()
  7261. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7262. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7263. file contains a section with an even level, conversion would
  7264. destroy the structure of the file. An error is signaled in this
  7265. case."
  7266. (interactive)
  7267. (goto-char (point-min))
  7268. ;; First check if there are no even levels
  7269. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7270. (org-show-context t)
  7271. (error "Not all levels are odd in this file. Conversion not possible"))
  7272. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7273. (let ((outline-regexp org-outline-regexp)
  7274. (outline-level 'org-outline-level)
  7275. (org-odd-levels-only nil) n)
  7276. (save-excursion
  7277. (goto-char (point-min))
  7278. (while (re-search-forward "^\\*\\*+ " nil t)
  7279. (setq n (/ (1- (length (match-string 0))) 2))
  7280. (while (>= (setq n (1- n)) 0)
  7281. (org-promote))
  7282. (end-of-line 1))))))
  7283. (defun org-tr-level (n)
  7284. "Make N odd if required."
  7285. (if org-odd-levels-only (1+ (/ n 2)) n))
  7286. ;;; Vertical tree motion, cutting and pasting of subtrees
  7287. (defun org-move-subtree-up (&optional arg)
  7288. "Move the current subtree up past ARG headlines of the same level."
  7289. (interactive "p")
  7290. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7291. (defun org-move-subtree-down (&optional arg)
  7292. "Move the current subtree down past ARG headlines of the same level."
  7293. (interactive "p")
  7294. (setq arg (prefix-numeric-value arg))
  7295. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7296. 'org-get-last-sibling))
  7297. (ins-point (make-marker))
  7298. (cnt (abs arg))
  7299. (col (current-column))
  7300. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7301. ;; Select the tree
  7302. (org-back-to-heading)
  7303. (setq beg0 (point))
  7304. (save-excursion
  7305. (setq ne-beg (org-back-over-empty-lines))
  7306. (setq beg (point)))
  7307. (save-match-data
  7308. (save-excursion (outline-end-of-heading)
  7309. (setq folded (outline-invisible-p)))
  7310. (outline-end-of-subtree))
  7311. (outline-next-heading)
  7312. (setq ne-end (org-back-over-empty-lines))
  7313. (setq end (point))
  7314. (goto-char beg0)
  7315. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7316. ;; include less whitespace
  7317. (save-excursion
  7318. (goto-char beg)
  7319. (forward-line (- ne-beg ne-end))
  7320. (setq beg (point))))
  7321. ;; Find insertion point, with error handling
  7322. (while (> cnt 0)
  7323. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7324. (progn (goto-char beg0)
  7325. (user-error "Cannot move past superior level or buffer limit")))
  7326. (setq cnt (1- cnt)))
  7327. (if (> arg 0)
  7328. ;; Moving forward - still need to move over subtree
  7329. (progn (org-end-of-subtree t t)
  7330. (save-excursion
  7331. (org-back-over-empty-lines)
  7332. (or (bolp) (newline)))))
  7333. (setq ne-ins (org-back-over-empty-lines))
  7334. (move-marker ins-point (point))
  7335. (setq txt (buffer-substring beg end))
  7336. (org-save-markers-in-region beg end)
  7337. (delete-region beg end)
  7338. (org-remove-empty-overlays-at beg)
  7339. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7340. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7341. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7342. (let ((bbb (point)))
  7343. (insert-before-markers txt)
  7344. (org-reinstall-markers-in-region bbb)
  7345. (move-marker ins-point bbb))
  7346. (or (bolp) (insert "\n"))
  7347. (setq ins-end (point))
  7348. (goto-char ins-point)
  7349. (org-skip-whitespace)
  7350. (when (and (< arg 0)
  7351. (org-first-sibling-p)
  7352. (> ne-ins ne-beg))
  7353. ;; Move whitespace back to beginning
  7354. (save-excursion
  7355. (goto-char ins-end)
  7356. (let ((kill-whole-line t))
  7357. (kill-line (- ne-ins ne-beg)) (point)))
  7358. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7359. (move-marker ins-point nil)
  7360. (if folded
  7361. (hide-subtree)
  7362. (org-show-entry)
  7363. (show-children)
  7364. (org-cycle-hide-drawers 'children))
  7365. (org-clean-visibility-after-subtree-move)
  7366. ;; move back to the initial column we were at
  7367. (move-to-column col)))
  7368. (defvar org-subtree-clip ""
  7369. "Clipboard for cut and paste of subtrees.
  7370. This is actually only a copy of the kill, because we use the normal kill
  7371. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7372. (defvar org-subtree-clip-folded nil
  7373. "Was the last copied subtree folded?
  7374. This is used to fold the tree back after pasting.")
  7375. (defun org-cut-subtree (&optional n)
  7376. "Cut the current subtree into the clipboard.
  7377. With prefix arg N, cut this many sequential subtrees.
  7378. This is a short-hand for marking the subtree and then cutting it."
  7379. (interactive "p")
  7380. (org-copy-subtree n 'cut))
  7381. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7382. "Copy the current subtree it in the clipboard.
  7383. With prefix arg N, copy this many sequential subtrees.
  7384. This is a short-hand for marking the subtree and then copying it.
  7385. If CUT is non-nil, actually cut the subtree.
  7386. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7387. of some markers in the region, even if CUT is non-nil. This is
  7388. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7389. (interactive "p")
  7390. (let (beg end folded (beg0 (point)))
  7391. (if (org-called-interactively-p 'any)
  7392. (org-back-to-heading nil) ; take what looks like a subtree
  7393. (org-back-to-heading t)) ; take what is really there
  7394. (setq beg (point))
  7395. (skip-chars-forward " \t\r\n")
  7396. (save-match-data
  7397. (if nosubtrees
  7398. (outline-next-heading)
  7399. (save-excursion (outline-end-of-heading)
  7400. (setq folded (outline-invisible-p)))
  7401. (condition-case nil
  7402. (org-forward-heading-same-level (1- n) t)
  7403. (error nil))
  7404. (org-end-of-subtree t t)))
  7405. (setq end (point))
  7406. (goto-char beg0)
  7407. (when (> end beg)
  7408. (setq org-subtree-clip-folded folded)
  7409. (when (or cut force-store-markers)
  7410. (org-save-markers-in-region beg end))
  7411. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7412. (setq org-subtree-clip (current-kill 0))
  7413. (message "%s: Subtree(s) with %d characters"
  7414. (if cut "Cut" "Copied")
  7415. (length org-subtree-clip)))))
  7416. (defun org-paste-subtree (&optional level tree for-yank)
  7417. "Paste the clipboard as a subtree, with modification of headline level.
  7418. The entire subtree is promoted or demoted in order to match a new headline
  7419. level.
  7420. If the cursor is at the beginning of a headline, the same level as
  7421. that headline is used to paste the tree.
  7422. If not, the new level is derived from the *visible* headings
  7423. before and after the insertion point, and taken to be the inferior headline
  7424. level of the two. So if the previous visible heading is level 3 and the
  7425. next is level 4 (or vice versa), level 4 will be used for insertion.
  7426. This makes sure that the subtree remains an independent subtree and does
  7427. not swallow low level entries.
  7428. You can also force a different level, either by using a numeric prefix
  7429. argument, or by inserting the heading marker by hand. For example, if the
  7430. cursor is after \"*****\", then the tree will be shifted to level 5.
  7431. If optional TREE is given, use this text instead of the kill ring.
  7432. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7433. move back over whitespace before inserting, and move point to the end of
  7434. the inserted text when done."
  7435. (interactive "P")
  7436. (setq tree (or tree (and kill-ring (current-kill 0))))
  7437. (unless (org-kill-is-subtree-p tree)
  7438. (user-error "%s"
  7439. (substitute-command-keys
  7440. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7441. (org-with-limited-levels
  7442. (let* ((visp (not (outline-invisible-p)))
  7443. (txt tree)
  7444. (^re_ "\\(\\*+\\)[ \t]*")
  7445. (old-level (if (string-match org-outline-regexp-bol txt)
  7446. (- (match-end 0) (match-beginning 0) 1)
  7447. -1))
  7448. (force-level (cond (level (prefix-numeric-value level))
  7449. ((and (looking-at "[ \t]*$")
  7450. (string-match
  7451. "^\\*+$" (buffer-substring
  7452. (point-at-bol) (point))))
  7453. (- (match-end 1) (match-beginning 1)))
  7454. ((and (bolp)
  7455. (looking-at org-outline-regexp))
  7456. (- (match-end 0) (point) 1))))
  7457. (previous-level (save-excursion
  7458. (condition-case nil
  7459. (progn
  7460. (outline-previous-visible-heading 1)
  7461. (if (looking-at ^re_)
  7462. (- (match-end 0) (match-beginning 0) 1)
  7463. 1))
  7464. (error 1))))
  7465. (next-level (save-excursion
  7466. (condition-case nil
  7467. (progn
  7468. (or (looking-at org-outline-regexp)
  7469. (outline-next-visible-heading 1))
  7470. (if (looking-at ^re_)
  7471. (- (match-end 0) (match-beginning 0) 1)
  7472. 1))
  7473. (error 1))))
  7474. (new-level (or force-level (max previous-level next-level)))
  7475. (shift (if (or (= old-level -1)
  7476. (= new-level -1)
  7477. (= old-level new-level))
  7478. 0
  7479. (- new-level old-level)))
  7480. (delta (if (> shift 0) -1 1))
  7481. (func (if (> shift 0) 'org-demote 'org-promote))
  7482. (org-odd-levels-only nil)
  7483. beg end newend)
  7484. ;; Remove the forced level indicator
  7485. (if force-level
  7486. (delete-region (point-at-bol) (point)))
  7487. ;; Paste
  7488. (beginning-of-line (if (bolp) 1 2))
  7489. (setq beg (point))
  7490. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7491. (insert-before-markers txt)
  7492. (unless (string-match "\n\\'" txt) (insert "\n"))
  7493. (setq newend (point))
  7494. (org-reinstall-markers-in-region beg)
  7495. (setq end (point))
  7496. (goto-char beg)
  7497. (skip-chars-forward " \t\n\r")
  7498. (setq beg (point))
  7499. (if (and (outline-invisible-p) visp)
  7500. (save-excursion (outline-show-heading)))
  7501. ;; Shift if necessary
  7502. (unless (= shift 0)
  7503. (save-restriction
  7504. (narrow-to-region beg end)
  7505. (while (not (= shift 0))
  7506. (org-map-region func (point-min) (point-max))
  7507. (setq shift (+ delta shift)))
  7508. (goto-char (point-min))
  7509. (setq newend (point-max))))
  7510. (when (or (org-called-interactively-p 'interactive) for-yank)
  7511. (message "Clipboard pasted as level %d subtree" new-level))
  7512. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7513. kill-ring
  7514. (eq org-subtree-clip (current-kill 0))
  7515. org-subtree-clip-folded)
  7516. ;; The tree was folded before it was killed/copied
  7517. (hide-subtree))
  7518. (and for-yank (goto-char newend)))))
  7519. (defun org-kill-is-subtree-p (&optional txt)
  7520. "Check if the current kill is an outline subtree, or a set of trees.
  7521. Returns nil if kill does not start with a headline, or if the first
  7522. headline level is not the largest headline level in the tree.
  7523. So this will actually accept several entries of equal levels as well,
  7524. which is OK for `org-paste-subtree'.
  7525. If optional TXT is given, check this string instead of the current kill."
  7526. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7527. (re (org-get-limited-outline-regexp))
  7528. (^re (concat "^" re))
  7529. (start-level (and kill
  7530. (string-match
  7531. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7532. kill)
  7533. (- (match-end 2) (match-beginning 2) 1)))
  7534. (start (1+ (or (match-beginning 2) -1))))
  7535. (if (not start-level)
  7536. (progn
  7537. nil) ;; does not even start with a heading
  7538. (catch 'exit
  7539. (while (setq start (string-match ^re kill (1+ start)))
  7540. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7541. (throw 'exit nil)))
  7542. t))))
  7543. (defvar org-markers-to-move nil
  7544. "Markers that should be moved with a cut-and-paste operation.
  7545. Those markers are stored together with their positions relative to
  7546. the start of the region.")
  7547. (defun org-save-markers-in-region (beg end)
  7548. "Check markers in region.
  7549. If these markers are between BEG and END, record their position relative
  7550. to BEG, so that after moving the block of text, we can put the markers back
  7551. into place.
  7552. This function gets called just before an entry or tree gets cut from the
  7553. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7554. called immediately, to move the markers with the entries."
  7555. (setq org-markers-to-move nil)
  7556. (when (featurep 'org-clock)
  7557. (org-clock-save-markers-for-cut-and-paste beg end))
  7558. (when (featurep 'org-agenda)
  7559. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7560. (defun org-check-and-save-marker (marker beg end)
  7561. "Check if MARKER is between BEG and END.
  7562. If yes, remember the marker and the distance to BEG."
  7563. (when (and (marker-buffer marker)
  7564. (equal (marker-buffer marker) (current-buffer)))
  7565. (if (and (>= marker beg) (< marker end))
  7566. (push (cons marker (- marker beg)) org-markers-to-move))))
  7567. (defun org-reinstall-markers-in-region (beg)
  7568. "Move all remembered markers to their position relative to BEG."
  7569. (mapc (lambda (x)
  7570. (move-marker (car x) (+ beg (cdr x))))
  7571. org-markers-to-move)
  7572. (setq org-markers-to-move nil))
  7573. (defun org-narrow-to-subtree ()
  7574. "Narrow buffer to the current subtree."
  7575. (interactive)
  7576. (save-excursion
  7577. (save-match-data
  7578. (org-with-limited-levels
  7579. (narrow-to-region
  7580. (progn (org-back-to-heading t) (point))
  7581. (progn (org-end-of-subtree t t)
  7582. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7583. (point)))))))
  7584. (defun org-narrow-to-block ()
  7585. "Narrow buffer to the current block."
  7586. (interactive)
  7587. (let* ((case-fold-search t)
  7588. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7589. "^[ \t]*#\\+end_.*")))
  7590. (if blockp
  7591. (narrow-to-region (car blockp) (cdr blockp))
  7592. (user-error "Not in a block"))))
  7593. (eval-when-compile
  7594. (defvar org-property-drawer-re))
  7595. (defvar org-property-start-re) ;; defined below
  7596. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7597. "Clone the task (subtree) at point N times.
  7598. The clones will be inserted as siblings.
  7599. In interactive use, the user will be prompted for the number of
  7600. clones to be produced. If the entry has a timestamp, the user
  7601. will also be prompted for a time shift, which may be a repeater
  7602. as used in time stamps, for example `+3d'. To disable this,
  7603. you can call the function with a universal prefix argument.
  7604. When a valid repeater is given and the entry contains any time
  7605. stamps, the clones will become a sequence in time, with time
  7606. stamps in the subtree shifted for each clone produced. If SHIFT
  7607. is nil or the empty string, time stamps will be left alone. The
  7608. ID property of the original subtree is removed.
  7609. If the original subtree did contain time stamps with a repeater,
  7610. the following will happen:
  7611. - the repeater will be removed in each clone
  7612. - an additional clone will be produced, with the current, unshifted
  7613. date(s) in the entry.
  7614. - the original entry will be placed *after* all the clones, with
  7615. repeater intact.
  7616. - the start days in the repeater in the original entry will be shifted
  7617. to past the last clone.
  7618. In this way you can spell out a number of instances of a repeating task,
  7619. and still retain the repeater to cover future instances of the task."
  7620. (interactive "nNumber of clones to produce: ")
  7621. (let ((shift
  7622. (or shift
  7623. (if (and (not (equal current-prefix-arg '(4)))
  7624. (save-excursion
  7625. (re-search-forward org-ts-regexp-both
  7626. (save-excursion
  7627. (org-end-of-subtree t)
  7628. (point)) t)))
  7629. (read-from-minibuffer
  7630. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7631. ""))) ;; No time shift
  7632. (n-no-remove -1)
  7633. (drawer-re org-drawer-regexp)
  7634. beg end template task idprop
  7635. shift-n shift-what doshift nmin nmax)
  7636. (if (not (and (integerp n) (> n 0)))
  7637. (error "Invalid number of replications %s" n))
  7638. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7639. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7640. shift)))
  7641. (error "Invalid shift specification %s" shift))
  7642. (when doshift
  7643. (setq shift-n (string-to-number (match-string 1 shift))
  7644. shift-what (cdr (assoc (match-string 2 shift)
  7645. '(("d" . day) ("w" . week)
  7646. ("m" . month) ("y" . year))))))
  7647. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7648. (setq nmin 1 nmax n)
  7649. (org-back-to-heading t)
  7650. (setq beg (point))
  7651. (setq idprop (org-entry-get nil "ID"))
  7652. (org-end-of-subtree t t)
  7653. (or (bolp) (insert "\n"))
  7654. (setq end (point))
  7655. (setq template (buffer-substring beg end))
  7656. (when (and doshift
  7657. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7658. (delete-region beg end)
  7659. (setq end beg)
  7660. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7661. (goto-char end)
  7662. (loop for n from nmin to nmax do
  7663. ;; prepare clone
  7664. (with-temp-buffer
  7665. (insert template)
  7666. (org-mode)
  7667. (goto-char (point-min))
  7668. (org-show-subtree)
  7669. (and idprop (if org-clone-delete-id
  7670. (org-entry-delete nil "ID")
  7671. (org-id-get-create t)))
  7672. (unless (= n 0)
  7673. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7674. (kill-whole-line))
  7675. (goto-char (point-min))
  7676. (while (re-search-forward drawer-re nil t)
  7677. (mapc (lambda (d)
  7678. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7679. (goto-char (point-min))
  7680. (when doshift
  7681. (while (re-search-forward org-ts-regexp-both nil t)
  7682. (org-timestamp-change (* n shift-n) shift-what))
  7683. (unless (= n n-no-remove)
  7684. (goto-char (point-min))
  7685. (while (re-search-forward org-ts-regexp nil t)
  7686. (save-excursion
  7687. (goto-char (match-beginning 0))
  7688. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7689. (delete-region (match-beginning 1) (match-end 1)))))))
  7690. (setq task (buffer-string)))
  7691. (insert task))
  7692. (goto-char beg)))
  7693. ;;; Outline Sorting
  7694. (defun org-sort (with-case)
  7695. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7696. Optional argument WITH-CASE means sort case-sensitively."
  7697. (interactive "P")
  7698. (cond
  7699. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7700. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7701. (t
  7702. (org-call-with-arg 'org-sort-entries with-case))))
  7703. (defun org-sort-remove-invisible (s)
  7704. "Remove invisible links from string S."
  7705. (remove-text-properties 0 (length s) org-rm-props s)
  7706. (while (string-match org-bracket-link-regexp s)
  7707. (setq s (replace-match (if (match-end 2)
  7708. (match-string 3 s)
  7709. (match-string 1 s)) t t s)))
  7710. (let ((st (format " %s " s)))
  7711. (while (string-match org-emph-re st)
  7712. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7713. (setq s (substring st 1 -1)))
  7714. s)
  7715. (defvar org-priority-regexp) ; defined later in the file
  7716. (defvar org-after-sorting-entries-or-items-hook nil
  7717. "Hook that is run after a bunch of entries or items have been sorted.
  7718. When children are sorted, the cursor is in the parent line when this
  7719. hook gets called. When a region or a plain list is sorted, the cursor
  7720. will be in the first entry of the sorted region/list.")
  7721. (defun org-sort-entries
  7722. (&optional with-case sorting-type getkey-func compare-func property)
  7723. "Sort entries on a certain level of an outline tree.
  7724. If there is an active region, the entries in the region are sorted.
  7725. Else, if the cursor is before the first entry, sort the top-level items.
  7726. Else, the children of the entry at point are sorted.
  7727. Sorting can be alphabetically, numerically, by date/time as given by
  7728. a time stamp, by a property, by priority order, or by a custom function.
  7729. The command prompts for the sorting type unless it has been given to the
  7730. function through the SORTING-TYPE argument, which needs to be a character,
  7731. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7732. precise meaning of each character:
  7733. n Numerically, by converting the beginning of the entry/item to a number.
  7734. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7735. o By order of TODO keywords.
  7736. t By date/time, either the first active time stamp in the entry, or, if
  7737. none exist, by the first inactive one.
  7738. s By the scheduled date/time.
  7739. d By deadline date/time.
  7740. c By creation time, which is assumed to be the first inactive time stamp
  7741. at the beginning of a line.
  7742. p By priority according to the cookie.
  7743. r By the value of a property.
  7744. Capital letters will reverse the sort order.
  7745. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7746. called with point at the beginning of the record. It must return either
  7747. a string or a number that should serve as the sorting key for that record.
  7748. Comparing entries ignores case by default. However, with an optional argument
  7749. WITH-CASE, the sorting considers case as well.
  7750. Sorting is done against the visible part of the headlines, it ignores hidden
  7751. links."
  7752. (interactive "P")
  7753. (let ((case-func (if with-case 'identity 'downcase))
  7754. (cmstr
  7755. ;; The clock marker is lost when using `sort-subr', let's
  7756. ;; store the clocking string.
  7757. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7758. (save-excursion
  7759. (goto-char org-clock-marker)
  7760. (looking-back "^.*") (match-string-no-properties 0))))
  7761. start beg end stars re re2
  7762. txt what tmp)
  7763. ;; Find beginning and end of region to sort
  7764. (cond
  7765. ((org-region-active-p)
  7766. ;; we will sort the region
  7767. (setq end (region-end)
  7768. what "region")
  7769. (goto-char (region-beginning))
  7770. (if (not (org-at-heading-p)) (outline-next-heading))
  7771. (setq start (point)))
  7772. ((or (org-at-heading-p)
  7773. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7774. ;; we will sort the children of the current headline
  7775. (org-back-to-heading)
  7776. (setq start (point)
  7777. end (progn (org-end-of-subtree t t)
  7778. (or (bolp) (insert "\n"))
  7779. (org-back-over-empty-lines)
  7780. (point))
  7781. what "children")
  7782. (goto-char start)
  7783. (show-subtree)
  7784. (outline-next-heading))
  7785. (t
  7786. ;; we will sort the top-level entries in this file
  7787. (goto-char (point-min))
  7788. (or (org-at-heading-p) (outline-next-heading))
  7789. (setq start (point))
  7790. (goto-char (point-max))
  7791. (beginning-of-line 1)
  7792. (when (looking-at ".*?\\S-")
  7793. ;; File ends in a non-white line
  7794. (end-of-line 1)
  7795. (insert "\n"))
  7796. (setq end (point-max))
  7797. (setq what "top-level")
  7798. (goto-char start)
  7799. (show-all)))
  7800. (setq beg (point))
  7801. (if (>= beg end) (user-error "Nothing to sort"))
  7802. (looking-at "\\(\\*+\\)")
  7803. (setq stars (match-string 1)
  7804. re (concat "^" (regexp-quote stars) " +")
  7805. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7806. txt (buffer-substring beg end))
  7807. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7808. (if (and (not (equal stars "*")) (string-match re2 txt))
  7809. (user-error "Region to sort contains a level above the first entry"))
  7810. (unless sorting-type
  7811. (message
  7812. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7813. [t]ime [s]cheduled [d]eadline [c]reated
  7814. A/N/P/R/O/F/T/S/D/C means reversed:"
  7815. what)
  7816. (setq sorting-type (read-char-exclusive))
  7817. (unless getkey-func
  7818. (and (= (downcase sorting-type) ?f)
  7819. (setq getkey-func
  7820. (org-icompleting-read "Sort using function: "
  7821. obarray 'fboundp t nil nil))
  7822. (setq getkey-func (intern getkey-func))))
  7823. (and (= (downcase sorting-type) ?r)
  7824. (not property)
  7825. (setq property
  7826. (org-icompleting-read "Property: "
  7827. (mapcar 'list (org-buffer-property-keys t))
  7828. nil t))))
  7829. (message "Sorting entries...")
  7830. (save-restriction
  7831. (narrow-to-region start end)
  7832. (let ((dcst (downcase sorting-type))
  7833. (case-fold-search nil)
  7834. (now (current-time)))
  7835. (sort-subr
  7836. (/= dcst sorting-type)
  7837. ;; This function moves to the beginning character of the "record" to
  7838. ;; be sorted.
  7839. (lambda nil
  7840. (if (re-search-forward re nil t)
  7841. (goto-char (match-beginning 0))
  7842. (goto-char (point-max))))
  7843. ;; This function moves to the last character of the "record" being
  7844. ;; sorted.
  7845. (lambda nil
  7846. (save-match-data
  7847. (condition-case nil
  7848. (outline-forward-same-level 1)
  7849. (error
  7850. (goto-char (point-max))))))
  7851. ;; This function returns the value that gets sorted against.
  7852. (lambda nil
  7853. (cond
  7854. ((= dcst ?n)
  7855. (if (looking-at org-complex-heading-regexp)
  7856. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7857. nil))
  7858. ((= dcst ?a)
  7859. (if (looking-at org-complex-heading-regexp)
  7860. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7861. nil))
  7862. ((= dcst ?t)
  7863. (let ((end (save-excursion (outline-next-heading) (point))))
  7864. (if (or (re-search-forward org-ts-regexp end t)
  7865. (re-search-forward org-ts-regexp-both end t))
  7866. (org-time-string-to-seconds (match-string 0))
  7867. (org-float-time now))))
  7868. ((= dcst ?c)
  7869. (let ((end (save-excursion (outline-next-heading) (point))))
  7870. (if (re-search-forward
  7871. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7872. end t)
  7873. (org-time-string-to-seconds (match-string 0))
  7874. (org-float-time now))))
  7875. ((= dcst ?s)
  7876. (let ((end (save-excursion (outline-next-heading) (point))))
  7877. (if (re-search-forward org-scheduled-time-regexp end t)
  7878. (org-time-string-to-seconds (match-string 1))
  7879. (org-float-time now))))
  7880. ((= dcst ?d)
  7881. (let ((end (save-excursion (outline-next-heading) (point))))
  7882. (if (re-search-forward org-deadline-time-regexp end t)
  7883. (org-time-string-to-seconds (match-string 1))
  7884. (org-float-time now))))
  7885. ((= dcst ?p)
  7886. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7887. (string-to-char (match-string 2))
  7888. org-default-priority))
  7889. ((= dcst ?r)
  7890. (or (org-entry-get nil property) ""))
  7891. ((= dcst ?o)
  7892. (if (looking-at org-complex-heading-regexp)
  7893. (- 9999 (length (member (match-string 2)
  7894. org-todo-keywords-1)))))
  7895. ((= dcst ?f)
  7896. (if getkey-func
  7897. (progn
  7898. (setq tmp (funcall getkey-func))
  7899. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7900. tmp)
  7901. (error "Invalid key function `%s'" getkey-func)))
  7902. (t (error "Invalid sorting type `%c'" sorting-type))))
  7903. nil
  7904. (cond
  7905. ((= dcst ?a) 'string<)
  7906. ((= dcst ?f) compare-func)
  7907. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7908. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7909. ;; Reset the clock marker if needed
  7910. (when cmstr
  7911. (save-excursion
  7912. (goto-char start)
  7913. (search-forward cmstr nil t)
  7914. (move-marker org-clock-marker (point))))
  7915. (message "Sorting entries...done")))
  7916. (defun org-do-sort (table what &optional with-case sorting-type)
  7917. "Sort TABLE of WHAT according to SORTING-TYPE.
  7918. The user will be prompted for the SORTING-TYPE if the call to this
  7919. function does not specify it. WHAT is only for the prompt, to indicate
  7920. what is being sorted. The sorting key will be extracted from
  7921. the car of the elements of the table.
  7922. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7923. (unless sorting-type
  7924. (message
  7925. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7926. what)
  7927. (setq sorting-type (read-char-exclusive)))
  7928. (let ((dcst (downcase sorting-type))
  7929. extractfun comparefun)
  7930. ;; Define the appropriate functions
  7931. (cond
  7932. ((= dcst ?n)
  7933. (setq extractfun 'string-to-number
  7934. comparefun (if (= dcst sorting-type) '< '>)))
  7935. ((= dcst ?a)
  7936. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7937. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7938. comparefun (if (= dcst sorting-type)
  7939. 'string<
  7940. (lambda (a b) (and (not (string< a b))
  7941. (not (string= a b)))))))
  7942. ((= dcst ?t)
  7943. (setq extractfun
  7944. (lambda (x)
  7945. (if (or (string-match org-ts-regexp x)
  7946. (string-match org-ts-regexp-both x))
  7947. (org-float-time
  7948. (org-time-string-to-time (match-string 0 x)))
  7949. 0))
  7950. comparefun (if (= dcst sorting-type) '< '>)))
  7951. (t (error "Invalid sorting type `%c'" sorting-type)))
  7952. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7953. table)
  7954. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7955. ;;; The orgstruct minor mode
  7956. ;; Define a minor mode which can be used in other modes in order to
  7957. ;; integrate the org-mode structure editing commands.
  7958. ;; This is really a hack, because the org-mode structure commands use
  7959. ;; keys which normally belong to the major mode. Here is how it
  7960. ;; works: The minor mode defines all the keys necessary to operate the
  7961. ;; structure commands, but wraps the commands into a function which
  7962. ;; tests if the cursor is currently at a headline or a plain list
  7963. ;; item. If that is the case, the structure command is used,
  7964. ;; temporarily setting many Org-mode variables like regular
  7965. ;; expressions for filling etc. However, when any of those keys is
  7966. ;; used at a different location, function uses `key-binding' to look
  7967. ;; up if the key has an associated command in another currently active
  7968. ;; keymap (minor modes, major mode, global), and executes that
  7969. ;; command. There might be problems if any of the keys is otherwise
  7970. ;; used as a prefix key.
  7971. (defcustom orgstruct-heading-prefix-regexp nil
  7972. "Regexp that matches the custom prefix of Org headlines in
  7973. orgstruct(++)-mode."
  7974. :group 'org
  7975. :version "24.4"
  7976. :package-version '(Org . "8.0")
  7977. :type 'string)
  7978. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7979. (defcustom orgstruct-setup-hook nil
  7980. "Hook run after orgstruct-mode-map is filled."
  7981. :group 'org
  7982. :version "24.4"
  7983. :package-version '(Org . "8.0")
  7984. :type 'hook)
  7985. (defvar orgstruct-initialized nil)
  7986. (defvar org-local-vars nil
  7987. "List of local variables, for use by `orgstruct-mode'.")
  7988. ;;;###autoload
  7989. (define-minor-mode orgstruct-mode
  7990. "Toggle the minor mode `orgstruct-mode'.
  7991. This mode is for using Org-mode structure commands in other
  7992. modes. The following keys behave as if Org-mode were active, if
  7993. the cursor is on a headline, or on a plain list item (both as
  7994. defined by Org-mode)."
  7995. nil " OrgStruct" (make-sparse-keymap)
  7996. (funcall (if orgstruct-mode
  7997. 'add-to-invisibility-spec
  7998. 'remove-from-invisibility-spec)
  7999. '(outline . t))
  8000. (when orgstruct-mode
  8001. (org-load-modules-maybe)
  8002. (unless orgstruct-initialized
  8003. (orgstruct-setup)
  8004. (setq orgstruct-initialized t))))
  8005. ;;;###autoload
  8006. (defun turn-on-orgstruct ()
  8007. "Unconditionally turn on `orgstruct-mode'."
  8008. (orgstruct-mode 1))
  8009. (defvar org-fb-vars nil)
  8010. (make-variable-buffer-local 'org-fb-vars)
  8011. (defun orgstruct++-mode (&optional arg)
  8012. "Toggle `orgstruct-mode', the enhanced version of it.
  8013. In addition to setting orgstruct-mode, this also exports all
  8014. indentation and autofilling variables from org-mode into the
  8015. buffer. It will also recognize item context in multiline items."
  8016. (interactive "P")
  8017. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8018. (if (< arg 1)
  8019. (progn (orgstruct-mode -1)
  8020. (mapc (lambda(v)
  8021. (org-set-local (car v)
  8022. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8023. org-fb-vars))
  8024. (orgstruct-mode 1)
  8025. (setq org-fb-vars nil)
  8026. (unless org-local-vars
  8027. (setq org-local-vars (org-get-local-variables)))
  8028. (let (var val)
  8029. (mapc
  8030. (lambda (x)
  8031. (when (string-match
  8032. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8033. (symbol-name (car x)))
  8034. (setq var (car x) val (nth 1 x))
  8035. (push (list var `(quote ,(eval var))) org-fb-vars)
  8036. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8037. org-local-vars)
  8038. (org-set-local 'orgstruct-is-++ t))))
  8039. (defvar orgstruct-is-++ nil
  8040. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8041. (make-variable-buffer-local 'orgstruct-is-++)
  8042. ;;;###autoload
  8043. (defun turn-on-orgstruct++ ()
  8044. "Unconditionally turn on `orgstruct++-mode'."
  8045. (orgstruct++-mode 1))
  8046. (defun orgstruct-error ()
  8047. "Error when there is no default binding for a structure key."
  8048. (interactive)
  8049. (funcall (if (fboundp 'user-error)
  8050. 'user-error
  8051. 'error)
  8052. "This key has no function outside structure elements"))
  8053. (defun orgstruct-setup ()
  8054. "Setup orgstruct keymap."
  8055. (dolist (cell '((org-demote . t)
  8056. (org-metaleft . t)
  8057. (org-metaright . t)
  8058. (org-promote . t)
  8059. (org-shiftmetaleft . t)
  8060. (org-shiftmetaright . t)
  8061. org-backward-element
  8062. org-backward-heading-same-level
  8063. org-ctrl-c-ret
  8064. org-ctrl-c-minus
  8065. org-ctrl-c-star
  8066. org-cycle
  8067. org-forward-heading-same-level
  8068. org-insert-heading
  8069. org-insert-heading-respect-content
  8070. org-kill-note-or-show-branches
  8071. org-mark-subtree
  8072. org-meta-return
  8073. org-metadown
  8074. org-metaup
  8075. org-narrow-to-subtree
  8076. org-promote-subtree
  8077. org-reveal
  8078. org-shiftdown
  8079. org-shiftleft
  8080. org-shiftmetadown
  8081. org-shiftmetaup
  8082. org-shiftright
  8083. org-shifttab
  8084. org-shifttab
  8085. org-shiftup
  8086. org-show-subtree
  8087. org-sort
  8088. org-up-element
  8089. outline-demote
  8090. outline-next-visible-heading
  8091. outline-previous-visible-heading
  8092. outline-promote
  8093. outline-up-heading
  8094. show-children))
  8095. (let ((f (or (car-safe cell) cell))
  8096. (disable-when-heading-prefix (cdr-safe cell)))
  8097. (when (fboundp f)
  8098. (let ((new-bindings))
  8099. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8100. (where-is-internal f outline-mode-map)))
  8101. (push binding new-bindings)
  8102. ;; TODO use local-function-key-map
  8103. (dolist (rep '(("<tab>" . "TAB")
  8104. ("<return>" . "RET")
  8105. ("<escape>" . "ESC")
  8106. ("<delete>" . "DEL")))
  8107. (setq binding (read-kbd-macro
  8108. (let ((case-fold-search))
  8109. (replace-regexp-in-string
  8110. (regexp-quote (cdr rep))
  8111. (car rep)
  8112. (key-description binding)))))
  8113. (pushnew binding new-bindings :test 'equal)))
  8114. (dolist (binding new-bindings)
  8115. (let ((key (lookup-key orgstruct-mode-map binding)))
  8116. (when (or (not key) (numberp key))
  8117. (condition-case nil
  8118. (org-defkey orgstruct-mode-map
  8119. binding
  8120. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8121. (error nil)))))))))
  8122. (run-hooks 'orgstruct-setup-hook))
  8123. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8124. "Create a function for binding in the structure minor mode.
  8125. FUN is the command to call inside a table. KEY is the key that
  8126. should be checked in for a command to execute outside of tables.
  8127. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8128. if `orgstruct-heading-prefix-regexp' is non-nil."
  8129. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8130. (let ((nname name)
  8131. (i 0))
  8132. (while (fboundp (intern nname))
  8133. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8134. (setq name (intern nname)))
  8135. (eval
  8136. (let ((bindings '((org-heading-regexp
  8137. (concat "^"
  8138. orgstruct-heading-prefix-regexp
  8139. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8140. (org-outline-regexp
  8141. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8142. (org-outline-regexp-bol
  8143. (concat "^" org-outline-regexp))
  8144. (outline-regexp org-outline-regexp)
  8145. (outline-heading-end-regexp "\n")
  8146. (outline-level 'org-outline-level)
  8147. (outline-heading-alist))))
  8148. `(defun ,name (arg)
  8149. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8150. "Outside of structure, run the binding of `"
  8151. (key-description key) "'."
  8152. (when disable-when-heading-prefix
  8153. (concat
  8154. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8155. "back to the default binding due to limitations of Org's implementation of\n"
  8156. "`" (symbol-name fun) "'.")))
  8157. (interactive "p")
  8158. (let* ((disable
  8159. ,(when disable-when-heading-prefix
  8160. '(and orgstruct-heading-prefix-regexp
  8161. (not (string= orgstruct-heading-prefix-regexp "")))))
  8162. (fallback
  8163. (or disable
  8164. (not
  8165. (let* ,bindings
  8166. (org-context-p 'headline 'item
  8167. ,(when (memq fun
  8168. '(org-insert-heading
  8169. org-insert-heading-respect-content
  8170. org-meta-return))
  8171. '(when orgstruct-is-++
  8172. 'item-body))))))))
  8173. (if fallback
  8174. (let* ((orgstruct-mode)
  8175. (binding
  8176. (loop with key = ,key
  8177. for rep in
  8178. '(nil
  8179. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8180. ("<\\([^>]*\\)return>" . "\\1RET")
  8181. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8182. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8183. do
  8184. (when rep
  8185. (setq key (read-kbd-macro
  8186. (let ((case-fold-search))
  8187. (replace-regexp-in-string
  8188. (car rep)
  8189. (cdr rep)
  8190. (key-description key))))))
  8191. thereis (key-binding key))))
  8192. (if (keymapp binding)
  8193. (set-temporary-overlay-map binding)
  8194. (let ((func (or binding
  8195. (unless disable
  8196. 'orgstruct-error))))
  8197. (when func
  8198. (call-interactively func)))))
  8199. (org-run-like-in-org-mode
  8200. (lambda ()
  8201. (interactive)
  8202. (let* ,bindings
  8203. (call-interactively ',fun)))))))))
  8204. name))
  8205. (defun org-contextualize-keys (alist contexts)
  8206. "Return valid elements in ALIST depending on CONTEXTS.
  8207. `org-agenda-custom-commands' or `org-capture-templates' are the
  8208. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8209. or `org-capture-templates-contexts' are the associated contexts
  8210. definitions."
  8211. (let ((contexts
  8212. ;; normalize contexts
  8213. (mapcar
  8214. (lambda(c) (cond ((listp (cadr c))
  8215. (list (car c) (car c) (cadr c)))
  8216. ((string= "" (cadr c))
  8217. (list (car c) (car c) (caddr c)))
  8218. (t c))) contexts))
  8219. (a alist) c r s)
  8220. ;; loop over all commands or templates
  8221. (while (setq c (pop a))
  8222. (let (vrules repl)
  8223. (cond
  8224. ((not (assoc (car c) contexts))
  8225. (push c r))
  8226. ((and (assoc (car c) contexts)
  8227. (setq vrules (org-contextualize-validate-key
  8228. (car c) contexts)))
  8229. (mapc (lambda (vr)
  8230. (when (not (equal (car vr) (cadr vr)))
  8231. (setq repl vr))) vrules)
  8232. (if (not repl) (push c r)
  8233. (push (cadr repl) s)
  8234. (push
  8235. (cons (car c)
  8236. (cdr (or (assoc (cadr repl) alist)
  8237. (error "Undefined key `%s' as contextual replacement for `%s'"
  8238. (cadr repl) (car c)))))
  8239. r))))))
  8240. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8241. (delq
  8242. nil
  8243. (delete-dups
  8244. (mapcar (lambda (x)
  8245. (let ((tpl (car x)))
  8246. (when (not (delq
  8247. nil
  8248. (mapcar (lambda(y)
  8249. (equal y tpl)) s))) x)))
  8250. (reverse r))))))
  8251. (defun org-contextualize-validate-key (key contexts)
  8252. "Check CONTEXTS for agenda or capture KEY."
  8253. (let (r rr res)
  8254. (while (setq r (pop contexts))
  8255. (mapc
  8256. (lambda (rr)
  8257. (when
  8258. (and (equal key (car r))
  8259. (if (functionp rr) (funcall rr)
  8260. (or (and (eq (car rr) 'in-file)
  8261. (buffer-file-name)
  8262. (string-match (cdr rr) (buffer-file-name)))
  8263. (and (eq (car rr) 'in-mode)
  8264. (string-match (cdr rr) (symbol-name major-mode)))
  8265. (and (eq (car rr) 'in-buffer)
  8266. (string-match (cdr rr) (buffer-name)))
  8267. (when (and (eq (car rr) 'not-in-file)
  8268. (buffer-file-name))
  8269. (not (string-match (cdr rr) (buffer-file-name))))
  8270. (when (eq (car rr) 'not-in-mode)
  8271. (not (string-match (cdr rr) (symbol-name major-mode))))
  8272. (when (eq (car rr) 'not-in-buffer)
  8273. (not (string-match (cdr rr) (buffer-name)))))))
  8274. (push r res)))
  8275. (car (last r))))
  8276. (delete-dups (delq nil res))))
  8277. (defun org-context-p (&rest contexts)
  8278. "Check if local context is any of CONTEXTS.
  8279. Possible values in the list of contexts are `table', `headline', and `item'."
  8280. (let ((pos (point)))
  8281. (goto-char (point-at-bol))
  8282. (prog1 (or (and (memq 'table contexts)
  8283. (looking-at "[ \t]*|"))
  8284. (and (memq 'headline contexts)
  8285. (looking-at org-outline-regexp))
  8286. (and (memq 'item contexts)
  8287. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8288. (and (memq 'item-body contexts)
  8289. (org-in-item-p)))
  8290. (goto-char pos))))
  8291. (defun org-get-local-variables ()
  8292. "Return a list of all local variables in an Org mode buffer."
  8293. (let (varlist)
  8294. (with-current-buffer (get-buffer-create "*Org tmp*")
  8295. (erase-buffer)
  8296. (org-mode)
  8297. (setq varlist (buffer-local-variables)))
  8298. (kill-buffer "*Org tmp*")
  8299. (delq nil
  8300. (mapcar
  8301. (lambda (x)
  8302. (setq x
  8303. (if (symbolp x)
  8304. (list x)
  8305. (list (car x) (cdr x))))
  8306. (if (and (not (get (car x) 'org-state))
  8307. (string-match
  8308. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8309. (symbol-name (car x))))
  8310. x nil))
  8311. varlist))))
  8312. (defun org-clone-local-variables (from-buffer &optional regexp)
  8313. "Clone local variables from FROM-BUFFER.
  8314. Optional argument REGEXP selects variables to clone."
  8315. (mapc
  8316. (lambda (pair)
  8317. (and (symbolp (car pair))
  8318. (or (null regexp)
  8319. (string-match regexp (symbol-name (car pair))))
  8320. (set (make-local-variable (car pair))
  8321. (cdr pair))))
  8322. (buffer-local-variables from-buffer)))
  8323. ;;;###autoload
  8324. (defun org-run-like-in-org-mode (cmd)
  8325. "Run a command, pretending that the current buffer is in Org-mode.
  8326. This will temporarily bind local variables that are typically bound in
  8327. Org-mode to the values they have in Org-mode, and then interactively
  8328. call CMD."
  8329. (org-load-modules-maybe)
  8330. (unless org-local-vars
  8331. (setq org-local-vars (org-get-local-variables)))
  8332. (let (binds)
  8333. (dolist (var org-local-vars)
  8334. (when (or (not (boundp (car var)))
  8335. (eq (symbol-value (car var))
  8336. (default-value (car var))))
  8337. (push (list (car var) `(quote ,(cadr var))) binds)))
  8338. (eval `(let ,binds
  8339. (call-interactively (quote ,cmd))))))
  8340. ;;;; Archiving
  8341. (defun org-get-category (&optional pos force-refresh)
  8342. "Get the category applying to position POS."
  8343. (save-match-data
  8344. (if force-refresh (org-refresh-category-properties))
  8345. (let ((pos (or pos (point))))
  8346. (or (get-text-property pos 'org-category)
  8347. (progn (org-refresh-category-properties)
  8348. (get-text-property pos 'org-category))))))
  8349. (defun org-refresh-category-properties ()
  8350. "Refresh category text properties in the buffer."
  8351. (let ((case-fold-search t)
  8352. (inhibit-read-only t)
  8353. (def-cat (cond
  8354. ((null org-category)
  8355. (if buffer-file-name
  8356. (file-name-sans-extension
  8357. (file-name-nondirectory buffer-file-name))
  8358. "???"))
  8359. ((symbolp org-category) (symbol-name org-category))
  8360. (t org-category)))
  8361. beg end cat pos optionp)
  8362. (org-with-silent-modifications
  8363. (save-excursion
  8364. (save-restriction
  8365. (widen)
  8366. (goto-char (point-min))
  8367. (put-text-property (point) (point-max) 'org-category def-cat)
  8368. (while (re-search-forward
  8369. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8370. (setq pos (match-end 0)
  8371. optionp (equal (char-after (match-beginning 0)) ?#)
  8372. cat (org-trim (match-string 2)))
  8373. (if optionp
  8374. (setq beg (point-at-bol) end (point-max))
  8375. (org-back-to-heading t)
  8376. (setq beg (point) end (org-end-of-subtree t t)))
  8377. (put-text-property beg end 'org-category cat)
  8378. (put-text-property beg end 'org-category-position beg)
  8379. (goto-char pos)))))))
  8380. (defun org-refresh-properties (dprop tprop)
  8381. "Refresh buffer text properties.
  8382. DPROP is the drawer property and TPROP is the corresponding text
  8383. property to set."
  8384. (let ((case-fold-search t)
  8385. (inhibit-read-only t) p)
  8386. (org-with-silent-modifications
  8387. (save-excursion
  8388. (save-restriction
  8389. (widen)
  8390. (goto-char (point-min))
  8391. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8392. (setq p (org-match-string-no-properties 1))
  8393. (save-excursion
  8394. (org-back-to-heading t)
  8395. (put-text-property
  8396. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8397. ;;;; Link Stuff
  8398. ;;; Link abbreviations
  8399. (defun org-link-expand-abbrev (link)
  8400. "Apply replacements as defined in `org-link-abbrev-alist'."
  8401. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8402. (let* ((key (match-string 1 link))
  8403. (as (or (assoc key org-link-abbrev-alist-local)
  8404. (assoc key org-link-abbrev-alist)))
  8405. (tag (and (match-end 2) (match-string 3 link)))
  8406. rpl)
  8407. (if (not as)
  8408. link
  8409. (setq rpl (cdr as))
  8410. (cond
  8411. ((symbolp rpl) (funcall rpl tag))
  8412. ((string-match "%(\\([^)]+\\))" rpl)
  8413. (replace-match
  8414. (save-match-data
  8415. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8416. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8417. ((string-match "%h" rpl)
  8418. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8419. (t (concat rpl tag)))))
  8420. link))
  8421. ;;; Storing and inserting links
  8422. (defvar org-insert-link-history nil
  8423. "Minibuffer history for links inserted with `org-insert-link'.")
  8424. (defvar org-stored-links nil
  8425. "Contains the links stored with `org-store-link'.")
  8426. (defvar org-store-link-plist nil
  8427. "Plist with info about the most recently link created with `org-store-link'.")
  8428. (defvar org-link-protocols nil
  8429. "Link protocols added to Org-mode using `org-add-link-type'.")
  8430. (defvar org-store-link-functions nil
  8431. "List of functions that are called to create and store a link.
  8432. Each function will be called in turn until one returns a non-nil
  8433. value. Each function should check if it is responsible for creating
  8434. this link (for example by looking at the major mode).
  8435. If not, it must exit and return nil.
  8436. If yes, it should return a non-nil value after a calling
  8437. `org-store-link-props' with a list of properties and values.
  8438. Special properties are:
  8439. :type The link prefix, like \"http\". This must be given.
  8440. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8441. This is obligatory as well.
  8442. :description Optional default description for the second pair
  8443. of brackets in an Org-mode link. The user can still change
  8444. this when inserting this link into an Org-mode buffer.
  8445. In addition to these, any additional properties can be specified
  8446. and then used in capture templates.")
  8447. (defun org-add-link-type (type &optional follow export)
  8448. "Add TYPE to the list of `org-link-types'.
  8449. Re-compute all regular expressions depending on `org-link-types'
  8450. FOLLOW and EXPORT are two functions.
  8451. FOLLOW should take the link path as the single argument and do whatever
  8452. is necessary to follow the link, for example find a file or display
  8453. a mail message.
  8454. EXPORT should format the link path for export to one of the export formats.
  8455. It should be a function accepting three arguments:
  8456. path the path of the link, the text after the prefix (like \"http:\")
  8457. desc the description of the link, if any, or a description added by
  8458. org-export-normalize-links if there is none
  8459. format the export format, a symbol like `html' or `latex' or `ascii'..
  8460. The function may use the FORMAT information to return different values
  8461. depending on the format. The return value will be put literally into
  8462. the exported file. If the return value is nil, this means Org should
  8463. do what it normally does with links which do not have EXPORT defined.
  8464. Org-mode has a built-in default for exporting links. If you are happy with
  8465. this default, there is no need to define an export function for the link
  8466. type. For a simple example of an export function, see `org-bbdb.el'."
  8467. (add-to-list 'org-link-types type t)
  8468. (org-make-link-regexps)
  8469. (if (assoc type org-link-protocols)
  8470. (setcdr (assoc type org-link-protocols) (list follow export))
  8471. (push (list type follow export) org-link-protocols)))
  8472. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8473. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8474. ;;;###autoload
  8475. (defun org-store-link (arg)
  8476. "\\<org-mode-map>Store an org-link to the current location.
  8477. This link is added to `org-stored-links' and can later be inserted
  8478. into an org-buffer with \\[org-insert-link].
  8479. For some link types, a prefix arg is interpreted.
  8480. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8481. For file links, arg negates `org-context-in-file-links'.
  8482. A double prefix arg force skipping storing functions that are not
  8483. part of Org's core.
  8484. A triple prefix arg force storing a link for each line in the
  8485. active region."
  8486. (interactive "P")
  8487. (org-load-modules-maybe)
  8488. (if (and (equal arg '(64)) (org-region-active-p))
  8489. (save-excursion
  8490. (let ((end (region-end)))
  8491. (goto-char (region-beginning))
  8492. (set-mark (point))
  8493. (while (< (point-at-eol) end)
  8494. (move-end-of-line 1) (activate-mark)
  8495. (let (current-prefix-arg)
  8496. (call-interactively 'org-store-link))
  8497. (move-beginning-of-line 2)
  8498. (set-mark (point)))))
  8499. (org-with-limited-levels
  8500. (setq org-store-link-plist nil)
  8501. (let (link cpltxt desc description search
  8502. txt custom-id agenda-link sfuns sfunsn)
  8503. (cond
  8504. ;; Store a link using an external link type
  8505. ((and (not (equal arg '(16)))
  8506. (setq sfuns
  8507. (delq
  8508. nil (mapcar (lambda (f)
  8509. (let (fs) (if (funcall f) (push f fs))))
  8510. org-store-link-functions))
  8511. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8512. (or (and (cdr sfuns)
  8513. (funcall (intern
  8514. (completing-read
  8515. "Which function for creating the link? "
  8516. sfunsn t (car sfunsn)))))
  8517. (funcall (caar sfuns)))
  8518. (setq link (plist-get org-store-link-plist :link)
  8519. desc (or (plist-get org-store-link-plist
  8520. :description) link))))
  8521. ;; Store a link from a source code buffer
  8522. ((org-src-edit-buffer-p)
  8523. (let (label gc)
  8524. (while (or (not label)
  8525. (save-excursion
  8526. (save-restriction
  8527. (widen)
  8528. (goto-char (point-min))
  8529. (re-search-forward
  8530. (regexp-quote (format org-coderef-label-format label))
  8531. nil t))))
  8532. (when label (message "Label exists already") (sit-for 2))
  8533. (setq label (read-string "Code line label: " label)))
  8534. (end-of-line 1)
  8535. (setq link (format org-coderef-label-format label))
  8536. (setq gc (- 79 (length link)))
  8537. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8538. (insert link)
  8539. (setq link (concat "(" label ")") desc nil)))
  8540. ;; We are in the agenda, link to referenced location
  8541. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8542. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8543. (get-text-property (point) 'org-marker))))
  8544. (when m
  8545. (org-with-point-at m
  8546. (setq agenda-link
  8547. (if (org-called-interactively-p 'any)
  8548. (call-interactively 'org-store-link)
  8549. (org-store-link nil)))))))
  8550. ((eq major-mode 'calendar-mode)
  8551. (let ((cd (calendar-cursor-to-date)))
  8552. (setq link
  8553. (format-time-string
  8554. (car org-time-stamp-formats)
  8555. (apply 'encode-time
  8556. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8557. nil nil nil))))
  8558. (org-store-link-props :type "calendar" :date cd)))
  8559. ((eq major-mode 'help-mode)
  8560. (setq link (concat "help:" (save-excursion
  8561. (goto-char (point-min))
  8562. (looking-at "^[^ ]+")
  8563. (match-string 0))))
  8564. (org-store-link-props :type "help"))
  8565. ((eq major-mode 'w3-mode)
  8566. (setq cpltxt (if (and (buffer-name)
  8567. (not (string-match "Untitled" (buffer-name))))
  8568. (buffer-name)
  8569. (url-view-url t))
  8570. link (url-view-url t))
  8571. (org-store-link-props :type "w3" :url (url-view-url t)))
  8572. ((eq major-mode 'image-mode)
  8573. (setq cpltxt (concat "file:"
  8574. (abbreviate-file-name buffer-file-name))
  8575. link cpltxt)
  8576. (org-store-link-props :type "image" :file buffer-file-name))
  8577. ;; In dired, store a link to the file of the current line
  8578. ((eq major-mode 'dired-mode)
  8579. (let ((file (dired-get-filename nil t)))
  8580. (setq file (if file
  8581. (abbreviate-file-name
  8582. (expand-file-name (dired-get-filename nil t)))
  8583. ;; otherwise, no file so use current directory.
  8584. default-directory))
  8585. (setq cpltxt (concat "file:" file)
  8586. link cpltxt)))
  8587. ((setq search (run-hook-with-args-until-success
  8588. 'org-create-file-search-functions))
  8589. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8590. "::" search))
  8591. (setq cpltxt (or description link)))
  8592. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8593. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8594. (cond
  8595. ;; Store a link using the target at point
  8596. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8597. (setq cpltxt
  8598. (concat "file:"
  8599. (abbreviate-file-name
  8600. (buffer-file-name (buffer-base-buffer)))
  8601. "::" (match-string 1))
  8602. link cpltxt))
  8603. ((and (featurep 'org-id)
  8604. (or (eq org-id-link-to-org-use-id t)
  8605. (and (org-called-interactively-p 'any)
  8606. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8607. (and (eq org-id-link-to-org-use-id
  8608. 'create-if-interactive-and-no-custom-id)
  8609. (not custom-id))))
  8610. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8611. ;; Store a link using the ID at point
  8612. (setq link (condition-case nil
  8613. (prog1 (org-id-store-link)
  8614. (setq desc (plist-get org-store-link-plist
  8615. :description)))
  8616. (error
  8617. ;; Probably before first headline, link only to file
  8618. (concat "file:"
  8619. (abbreviate-file-name
  8620. (buffer-file-name (buffer-base-buffer))))))))
  8621. (t
  8622. ;; Just link to current headline
  8623. (setq cpltxt (concat "file:"
  8624. (abbreviate-file-name
  8625. (buffer-file-name (buffer-base-buffer)))))
  8626. ;; Add a context search string
  8627. (when (org-xor org-context-in-file-links arg)
  8628. (let* ((ee (org-element-at-point))
  8629. (et (org-element-type ee))
  8630. (ev (plist-get (cadr ee) :value))
  8631. (ek (plist-get (cadr ee) :key))
  8632. (eok (and (stringp ek) (string-match "name" ek))))
  8633. (setq txt (cond
  8634. ((org-at-heading-p) nil)
  8635. ((and (eq et 'keyword) eok) ev)
  8636. ((org-region-active-p)
  8637. (buffer-substring (region-beginning) (region-end)))))
  8638. (when (or (null txt) (string-match "\\S-" txt))
  8639. (setq cpltxt
  8640. (concat cpltxt "::"
  8641. (condition-case nil
  8642. (org-make-org-heading-search-string txt)
  8643. (error "")))
  8644. desc (or (and (eq et 'keyword) eok ev)
  8645. (nth 4 (ignore-errors (org-heading-components)))
  8646. "NONE")))))
  8647. (if (string-match "::\\'" cpltxt)
  8648. (setq cpltxt (substring cpltxt 0 -2)))
  8649. (setq link cpltxt))))
  8650. ((buffer-file-name (buffer-base-buffer))
  8651. ;; Just link to this file here.
  8652. (setq cpltxt (concat "file:"
  8653. (abbreviate-file-name
  8654. (buffer-file-name (buffer-base-buffer)))))
  8655. ;; Add a context string.
  8656. (when (org-xor org-context-in-file-links arg)
  8657. (setq txt (if (org-region-active-p)
  8658. (buffer-substring (region-beginning) (region-end))
  8659. (buffer-substring (point-at-bol) (point-at-eol))))
  8660. ;; Only use search option if there is some text.
  8661. (when (string-match "\\S-" txt)
  8662. (setq cpltxt
  8663. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8664. desc "NONE")))
  8665. (setq link cpltxt))
  8666. ((org-called-interactively-p 'interactive)
  8667. (user-error "No method for storing a link from this buffer"))
  8668. (t (setq link nil)))
  8669. ;; We're done setting link and desc, clean up
  8670. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8671. (setq link (or link cpltxt)
  8672. desc (or desc cpltxt))
  8673. (cond ((equal desc "NONE") (setq desc nil))
  8674. ((string-match org-bracket-link-analytic-regexp desc)
  8675. (let ((d0 (match-string 3 desc))
  8676. (p0 (match-string 5 desc)))
  8677. (setq desc
  8678. (replace-regexp-in-string
  8679. org-bracket-link-regexp
  8680. (concat (or p0 d0)
  8681. (if (equal (length (match-string 0 desc))
  8682. (length desc)) "*" "")) desc)))))
  8683. ;; Return the link
  8684. (if (not (and (or (org-called-interactively-p 'any)
  8685. executing-kbd-macro) link))
  8686. (or agenda-link (and link (org-make-link-string link desc)))
  8687. (push (list link desc) org-stored-links)
  8688. (message "Stored: %s" (or desc link))
  8689. (when custom-id
  8690. (setq link (concat "file:" (abbreviate-file-name
  8691. (buffer-file-name)) "::#" custom-id))
  8692. (push (list link desc) org-stored-links)))))))
  8693. (defun org-store-link-props (&rest plist)
  8694. "Store link properties, extract names and addresses."
  8695. (let (x adr)
  8696. (when (setq x (plist-get plist :from))
  8697. (setq adr (mail-extract-address-components x))
  8698. (setq plist (plist-put plist :fromname (car adr)))
  8699. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8700. (when (setq x (plist-get plist :to))
  8701. (setq adr (mail-extract-address-components x))
  8702. (setq plist (plist-put plist :toname (car adr)))
  8703. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8704. (let ((from (plist-get plist :from))
  8705. (to (plist-get plist :to)))
  8706. (when (and from to org-from-is-user-regexp)
  8707. (setq plist
  8708. (plist-put plist :fromto
  8709. (if (string-match org-from-is-user-regexp from)
  8710. (concat "to %t")
  8711. (concat "from %f"))))))
  8712. (setq org-store-link-plist plist))
  8713. (defun org-add-link-props (&rest plist)
  8714. "Add these properties to the link property list."
  8715. (let (key value)
  8716. (while plist
  8717. (setq key (pop plist) value (pop plist))
  8718. (setq org-store-link-plist
  8719. (plist-put org-store-link-plist key value)))))
  8720. (defun org-email-link-description (&optional fmt)
  8721. "Return the description part of an email link.
  8722. This takes information from `org-store-link-plist' and formats it
  8723. according to FMT (default from `org-email-link-description-format')."
  8724. (setq fmt (or fmt org-email-link-description-format))
  8725. (let* ((p org-store-link-plist)
  8726. (to (plist-get p :toaddress))
  8727. (from (plist-get p :fromaddress))
  8728. (table
  8729. (list
  8730. (cons "%c" (plist-get p :fromto))
  8731. (cons "%F" (plist-get p :from))
  8732. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8733. (cons "%T" (plist-get p :to))
  8734. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8735. (cons "%s" (plist-get p :subject))
  8736. (cons "%d" (plist-get p :date))
  8737. (cons "%m" (plist-get p :message-id)))))
  8738. (when (string-match "%c" fmt)
  8739. ;; Check if the user wrote this message
  8740. (if (and org-from-is-user-regexp from to
  8741. (save-match-data (string-match org-from-is-user-regexp from)))
  8742. (setq fmt (replace-match "to %t" t t fmt))
  8743. (setq fmt (replace-match "from %f" t t fmt))))
  8744. (org-replace-escapes fmt table)))
  8745. (defun org-make-org-heading-search-string (&optional string)
  8746. "Make search string for the current headline or STRING."
  8747. (let ((s (or string
  8748. (and (derived-mode-p 'org-mode)
  8749. (save-excursion
  8750. (org-back-to-heading t)
  8751. (org-element-property :raw-value (org-element-at-point))))))
  8752. (lines org-context-in-file-links))
  8753. (or string (setq s (concat "*" s))) ; Add * for headlines
  8754. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8755. (when (and string (integerp lines) (> lines 0))
  8756. (let ((slines (org-split-string s "\n")))
  8757. (when (< lines (length slines))
  8758. (setq s (mapconcat
  8759. 'identity
  8760. (reverse (nthcdr (- (length slines) lines)
  8761. (reverse slines))) "\n")))))
  8762. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8763. (defun org-make-link-string (link &optional description)
  8764. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8765. (unless (string-match "\\S-" link)
  8766. (error "Empty link"))
  8767. (when (and description
  8768. (stringp description)
  8769. (not (string-match "\\S-" description)))
  8770. (setq description nil))
  8771. (when (stringp description)
  8772. ;; Remove brackets from the description, they are fatal.
  8773. (while (string-match "\\[" description)
  8774. (setq description (replace-match "{" t t description)))
  8775. (while (string-match "\\]" description)
  8776. (setq description (replace-match "}" t t description))))
  8777. (when (equal link description)
  8778. ;; No description needed, it is identical
  8779. (setq description nil))
  8780. (when (and (not description)
  8781. (not (string-match (org-image-file-name-regexp) link))
  8782. (not (equal link (org-link-escape link))))
  8783. (setq description (org-extract-attributes link)))
  8784. (setq link
  8785. (cond ((string-match (org-image-file-name-regexp) link) link)
  8786. ((string-match org-link-types-re link)
  8787. (concat (match-string 1 link)
  8788. (org-link-escape (substring link (match-end 1)))))
  8789. (t (org-link-escape link))))
  8790. (concat "[[" link "]"
  8791. (if description (concat "[" description "]") "")
  8792. "]"))
  8793. (defconst org-link-escape-chars
  8794. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8795. "List of characters that should be escaped in link.
  8796. This is the list that is used for internal purposes.")
  8797. (defconst org-link-escape-chars-browser
  8798. '(?\ ?\")
  8799. "List of escapes for characters that are problematic in links.
  8800. This is the list that is used before handing over to the browser.")
  8801. (defun org-link-escape (text &optional table merge)
  8802. "Return percent escaped representation of TEXT.
  8803. TEXT is a string with the text to escape.
  8804. Optional argument TABLE is a list with characters that should be
  8805. escaped. When nil, `org-link-escape-chars' is used.
  8806. If optional argument MERGE is set, merge TABLE into
  8807. `org-link-escape-chars'."
  8808. (cond
  8809. ((and table merge)
  8810. (mapc (lambda (defchr)
  8811. (unless (member defchr table)
  8812. (setq table (cons defchr table)))) org-link-escape-chars))
  8813. ((null table)
  8814. (setq table org-link-escape-chars)))
  8815. (mapconcat
  8816. (lambda (char)
  8817. (if (or (member char table)
  8818. (and (or (< char 32) (= char 37) (> char 126))
  8819. org-url-hexify-p))
  8820. (mapconcat (lambda (sequence-element)
  8821. (format "%%%.2X" sequence-element))
  8822. (or (encode-coding-char char 'utf-8)
  8823. (error "Unable to percent escape character: %s"
  8824. (char-to-string char))) "")
  8825. (char-to-string char))) text ""))
  8826. (defun org-link-unescape (str)
  8827. "Unhex hexified Unicode strings as returned from the JavaScript function
  8828. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8829. (unless (and (null str) (string= "" str))
  8830. (let ((pos 0) (case-fold-search t) unhexed)
  8831. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8832. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8833. (setq str (replace-match unhexed t t str))
  8834. (setq pos (+ pos (length unhexed))))))
  8835. str)
  8836. (defun org-link-unescape-compound (hex)
  8837. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8838. Note: this function also decodes single byte encodings like
  8839. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8840. (save-match-data
  8841. (let* ((bytes (cdr (split-string hex "%")))
  8842. (ret "")
  8843. (eat 0)
  8844. (sum 0))
  8845. (while bytes
  8846. (let* ((val (string-to-number (pop bytes) 16))
  8847. (shift-xor
  8848. (if (= 0 eat)
  8849. (cond
  8850. ((>= val 252) (cons 6 252))
  8851. ((>= val 248) (cons 5 248))
  8852. ((>= val 240) (cons 4 240))
  8853. ((>= val 224) (cons 3 224))
  8854. ((>= val 192) (cons 2 192))
  8855. (t (cons 0 0)))
  8856. (cons 6 128))))
  8857. (if (>= val 192) (setq eat (car shift-xor)))
  8858. (setq val (logxor val (cdr shift-xor)))
  8859. (setq sum (+ (lsh sum (car shift-xor)) val))
  8860. (if (> eat 0) (setq eat (- eat 1)))
  8861. (cond
  8862. ((= 0 eat) ;multi byte
  8863. (setq ret (concat ret (org-char-to-string sum)))
  8864. (setq sum 0))
  8865. ((not bytes) ; single byte(s)
  8866. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8867. )) ;; end (while bytes
  8868. ret )))
  8869. (defun org-link-unescape-single-byte-sequence (hex)
  8870. "Unhexify hex-encoded single byte character sequences."
  8871. (mapconcat (lambda (byte)
  8872. (char-to-string (string-to-number byte 16)))
  8873. (cdr (split-string hex "%")) ""))
  8874. (defun org-xor (a b)
  8875. "Exclusive or."
  8876. (if a (not b) b))
  8877. (defun org-fixup-message-id-for-http (s)
  8878. "Replace special characters in a message id, so it can be used in an http query."
  8879. (when (string-match "%" s)
  8880. (setq s (mapconcat (lambda (c)
  8881. (if (eq c ?%)
  8882. "%25"
  8883. (char-to-string c)))
  8884. s "")))
  8885. (while (string-match "<" s)
  8886. (setq s (replace-match "%3C" t t s)))
  8887. (while (string-match ">" s)
  8888. (setq s (replace-match "%3E" t t s)))
  8889. (while (string-match "@" s)
  8890. (setq s (replace-match "%40" t t s)))
  8891. s)
  8892. (defun org-link-prettify (link)
  8893. "Return a human-readable representation of LINK.
  8894. The car of LINK must be a raw link the cdr of LINK must be either
  8895. a link description or nil."
  8896. (let ((desc (or (cadr link) "<no description>")))
  8897. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8898. "<" (car link) ">")))
  8899. ;;;###autoload
  8900. (defun org-insert-link-global ()
  8901. "Insert a link like Org-mode does.
  8902. This command can be called in any mode to insert a link in Org-mode syntax."
  8903. (interactive)
  8904. (org-load-modules-maybe)
  8905. (org-run-like-in-org-mode 'org-insert-link))
  8906. (defun org-insert-all-links (&optional keep)
  8907. "Insert all links in `org-stored-links'."
  8908. (interactive "P")
  8909. (let ((links (copy-sequence org-stored-links)) l)
  8910. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8911. (insert "- ")
  8912. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8913. (insert "\n"))))
  8914. (defun org-link-fontify-links-to-this-file ()
  8915. "Fontify links to the current file in `org-stored-links'."
  8916. (let ((f (buffer-file-name)) a b)
  8917. (setq a (mapcar (lambda(l)
  8918. (let ((ll (car l)))
  8919. (when (and (string-match "^file:\\(.+\\)::" ll)
  8920. (equal f (expand-file-name (match-string 1 ll))))
  8921. ll)))
  8922. org-stored-links))
  8923. (when (featurep 'org-id)
  8924. (setq b (mapcar (lambda(l)
  8925. (let ((ll (car l)))
  8926. (when (and (string-match "^id:\\(.+\\)$" ll)
  8927. (equal f (expand-file-name
  8928. (or (org-id-find-id-file
  8929. (match-string 1 ll)) ""))))
  8930. ll)))
  8931. org-stored-links)))
  8932. (mapcar (lambda(l)
  8933. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8934. (delq nil (append a b)))))
  8935. (defvar org-link-links-in-this-file nil)
  8936. (defun org-insert-link (&optional complete-file link-location default-description)
  8937. "Insert a link. At the prompt, enter the link.
  8938. Completion can be used to insert any of the link protocol prefixes like
  8939. http or ftp in use.
  8940. The history can be used to select a link previously stored with
  8941. `org-store-link'. When the empty string is entered (i.e. if you just
  8942. press RET at the prompt), the link defaults to the most recently
  8943. stored link. As SPC triggers completion in the minibuffer, you need to
  8944. use M-SPC or C-q SPC to force the insertion of a space character.
  8945. You will also be prompted for a description, and if one is given, it will
  8946. be displayed in the buffer instead of the link.
  8947. If there is already a link at point, this command will allow you to edit link
  8948. and description parts.
  8949. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8950. be selected using completion. The path to the file will be relative to the
  8951. current directory if the file is in the current directory or a subdirectory.
  8952. Otherwise, the link will be the absolute path as completed in the minibuffer
  8953. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8954. option `org-link-file-path-type'.
  8955. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8956. the current directory or below.
  8957. With three \\[universal-argument] prefixes, negate the meaning of
  8958. `org-keep-stored-link-after-insertion'.
  8959. If `org-make-link-description-function' is non-nil, this function will be
  8960. called with the link target, and the result will be the default
  8961. link description.
  8962. If the LINK-LOCATION parameter is non-nil, this value will be
  8963. used as the link location instead of reading one interactively.
  8964. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8965. be used as the default description."
  8966. (interactive "P")
  8967. (let* ((wcf (current-window-configuration))
  8968. (origbuf (current-buffer))
  8969. (region (if (org-region-active-p)
  8970. (buffer-substring (region-beginning) (region-end))))
  8971. (remove (and region (list (region-beginning) (region-end))))
  8972. (desc region)
  8973. tmphist ; byte-compile incorrectly complains about this
  8974. (link link-location)
  8975. (abbrevs org-link-abbrev-alist-local)
  8976. entry file all-prefixes auto-desc)
  8977. (cond
  8978. (link-location) ; specified by arg, just use it.
  8979. ((org-in-regexp org-bracket-link-regexp 1)
  8980. ;; We do have a link at point, and we are going to edit it.
  8981. (setq remove (list (match-beginning 0) (match-end 0)))
  8982. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8983. (setq link (read-string "Link: "
  8984. (org-link-unescape
  8985. (org-match-string-no-properties 1)))))
  8986. ((or (org-in-regexp org-angle-link-re)
  8987. (org-in-regexp org-plain-link-re))
  8988. ;; Convert to bracket link
  8989. (setq remove (list (match-beginning 0) (match-end 0))
  8990. link (read-string "Link: "
  8991. (org-remove-angle-brackets (match-string 0)))))
  8992. ((member complete-file '((4) (16)))
  8993. ;; Completing read for file names.
  8994. (setq link (org-file-complete-link complete-file)))
  8995. (t
  8996. ;; Read link, with completion for stored links.
  8997. (org-link-fontify-links-to-this-file)
  8998. (org-switch-to-buffer-other-window "*Org Links*")
  8999. (with-current-buffer "*Org Links*"
  9000. (erase-buffer)
  9001. (insert "Insert a link.
  9002. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9003. (when org-stored-links
  9004. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9005. (insert (mapconcat 'org-link-prettify
  9006. (reverse org-stored-links) "\n")))
  9007. (goto-char (point-min)))
  9008. (let ((cw (selected-window)))
  9009. (select-window (get-buffer-window "*Org Links*" 'visible))
  9010. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9011. (unless (pos-visible-in-window-p (point-max))
  9012. (org-fit-window-to-buffer))
  9013. (and (window-live-p cw) (select-window cw)))
  9014. ;; Fake a link history, containing the stored links.
  9015. (setq tmphist (append (mapcar 'car org-stored-links)
  9016. org-insert-link-history))
  9017. (setq all-prefixes (append (mapcar 'car abbrevs)
  9018. (mapcar 'car org-link-abbrev-alist)
  9019. org-link-types))
  9020. (unwind-protect
  9021. (progn
  9022. (setq link
  9023. (org-completing-read
  9024. "Link: "
  9025. (append
  9026. (mapcar (lambda (x) (concat x ":"))
  9027. all-prefixes)
  9028. (mapcar 'car org-stored-links))
  9029. nil nil nil
  9030. 'tmphist
  9031. (caar org-stored-links)))
  9032. (if (not (string-match "\\S-" link))
  9033. (user-error "No link selected"))
  9034. (mapc (lambda(l)
  9035. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9036. org-stored-links)
  9037. (if (or (member link all-prefixes)
  9038. (and (equal ":" (substring link -1))
  9039. (member (substring link 0 -1) all-prefixes)
  9040. (setq link (substring link 0 -1))))
  9041. (setq link (with-current-buffer origbuf
  9042. (org-link-try-special-completion link)))))
  9043. (set-window-configuration wcf)
  9044. (kill-buffer "*Org Links*"))
  9045. (setq entry (assoc link org-stored-links))
  9046. (or entry (push link org-insert-link-history))
  9047. (setq desc (or desc (nth 1 entry)))))
  9048. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9049. (not org-keep-stored-link-after-insertion))
  9050. (setq org-stored-links (delq (assoc link org-stored-links)
  9051. org-stored-links)))
  9052. (if (string-match org-plain-link-re link)
  9053. ;; URL-like link, normalize the use of angular brackets.
  9054. (setq link (org-remove-angle-brackets link)))
  9055. ;; Check if we are linking to the current file with a search
  9056. ;; option If yes, simplify the link by using only the search
  9057. ;; option.
  9058. (when (and buffer-file-name
  9059. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9060. (let* ((path (match-string 1 link))
  9061. (case-fold-search nil)
  9062. (search (match-string 2 link)))
  9063. (save-match-data
  9064. (if (equal (file-truename buffer-file-name) (file-truename path))
  9065. ;; We are linking to this same file, with a search option
  9066. (setq link search)))))
  9067. ;; Check if we can/should use a relative path. If yes, simplify the link
  9068. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9069. (let* ((type (match-string 1 link))
  9070. (path (match-string 2 link))
  9071. (origpath path)
  9072. (case-fold-search nil))
  9073. (cond
  9074. ((or (eq org-link-file-path-type 'absolute)
  9075. (equal complete-file '(16)))
  9076. (setq path (abbreviate-file-name (expand-file-name path))))
  9077. ((eq org-link-file-path-type 'noabbrev)
  9078. (setq path (expand-file-name path)))
  9079. ((eq org-link-file-path-type 'relative)
  9080. (setq path (file-relative-name path)))
  9081. (t
  9082. (save-match-data
  9083. (if (string-match (concat "^" (regexp-quote
  9084. (expand-file-name
  9085. (file-name-as-directory
  9086. default-directory))))
  9087. (expand-file-name path))
  9088. ;; We are linking a file with relative path name.
  9089. (setq path (substring (expand-file-name path)
  9090. (match-end 0)))
  9091. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9092. (setq link (concat type path))
  9093. (if (equal desc origpath)
  9094. (setq desc path))))
  9095. (if org-make-link-description-function
  9096. (setq desc
  9097. (or (condition-case nil
  9098. (funcall org-make-link-description-function link desc)
  9099. (error (progn (message "Can't get link description from `%s'"
  9100. (symbol-name org-make-link-description-function))
  9101. (sit-for 2) nil)))
  9102. (read-string "Description: " default-description)))
  9103. (if default-description (setq desc default-description)
  9104. (setq desc (or (and auto-desc desc)
  9105. (read-string "Description: " desc)))))
  9106. (unless (string-match "\\S-" desc) (setq desc nil))
  9107. (if remove (apply 'delete-region remove))
  9108. (insert (org-make-link-string link desc))))
  9109. (defun org-link-try-special-completion (type)
  9110. "If there is completion support for link type TYPE, offer it."
  9111. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9112. (if (functionp fun)
  9113. (funcall fun)
  9114. (read-string "Link (no completion support): " (concat type ":")))))
  9115. (defun org-file-complete-link (&optional arg)
  9116. "Create a file link using completion."
  9117. (let (file link)
  9118. (setq file (org-iread-file-name "File: "))
  9119. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9120. (pwd1 (file-name-as-directory (abbreviate-file-name
  9121. (expand-file-name ".")))))
  9122. (cond
  9123. ((equal arg '(16))
  9124. (setq link (concat
  9125. "file:"
  9126. (abbreviate-file-name (expand-file-name file)))))
  9127. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9128. (setq link (concat "file:" (match-string 1 file))))
  9129. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9130. (expand-file-name file))
  9131. (setq link (concat
  9132. "file:" (match-string 1 (expand-file-name file)))))
  9133. (t (setq link (concat "file:" file)))))
  9134. link))
  9135. (defun org-iread-file-name (&rest args)
  9136. "Read-file-name using `ido-mode' speedup if available.
  9137. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9138. See `read-file-name' for a description of parameters."
  9139. (org-without-partial-completion
  9140. (if (and org-completion-use-ido
  9141. (fboundp 'ido-read-file-name)
  9142. (boundp 'ido-mode) ido-mode
  9143. (listp (second args)))
  9144. (let ((ido-enter-matching-directory nil))
  9145. (apply 'ido-read-file-name args))
  9146. (apply 'read-file-name args))))
  9147. (defun org-completing-read (&rest args)
  9148. "Completing-read with SPACE being a normal character."
  9149. (let ((enable-recursive-minibuffers t)
  9150. (minibuffer-local-completion-map
  9151. (copy-keymap minibuffer-local-completion-map)))
  9152. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9153. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9154. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9155. (apply 'org-icompleting-read args)))
  9156. (defun org-completing-read-no-i (&rest args)
  9157. (let (org-completion-use-ido org-completion-use-iswitchb)
  9158. (apply 'org-completing-read args)))
  9159. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9160. "Use iswitch as a completing-read replacement to choose from choices.
  9161. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9162. from."
  9163. (let* ((iswitchb-use-virtual-buffers nil)
  9164. (iswitchb-make-buflist-hook
  9165. (lambda ()
  9166. (setq iswitchb-temp-buflist choices))))
  9167. (iswitchb-read-buffer prompt)))
  9168. (defun org-icompleting-read (&rest args)
  9169. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9170. (org-without-partial-completion
  9171. (if (and org-completion-use-ido
  9172. (fboundp 'ido-completing-read)
  9173. (boundp 'ido-mode) ido-mode
  9174. (listp (second args)))
  9175. (let ((ido-enter-matching-directory nil))
  9176. (apply 'ido-completing-read (concat (car args))
  9177. (if (consp (car (nth 1 args)))
  9178. (mapcar 'car (nth 1 args))
  9179. (nth 1 args))
  9180. (cddr args)))
  9181. (if (and org-completion-use-iswitchb
  9182. (boundp 'iswitchb-mode) iswitchb-mode
  9183. (listp (second args)))
  9184. (apply 'org-iswitchb-completing-read (concat (car args))
  9185. (if (consp (car (nth 1 args)))
  9186. (mapcar 'car (nth 1 args))
  9187. (nth 1 args))
  9188. (cddr args))
  9189. (apply 'completing-read args)))))
  9190. (defun org-extract-attributes (s)
  9191. "Extract the attributes cookie from a string and set as text property."
  9192. (let (a attr (start 0) key value)
  9193. (save-match-data
  9194. (when (string-match "{{\\([^}]+\\)}}$" s)
  9195. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9196. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9197. (setq key (match-string 1 a) value (match-string 2 a)
  9198. start (match-end 0)
  9199. attr (plist-put attr (intern key) value))))
  9200. (org-add-props s nil 'org-attr attr))
  9201. s))
  9202. ;;; Opening/following a link
  9203. (defvar org-link-search-failed nil)
  9204. (defvar org-open-link-functions nil
  9205. "Hook for functions finding a plain text link.
  9206. These functions must take a single argument, the link content.
  9207. They will be called for links that look like [[link text][description]]
  9208. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9209. like a filename (e.g. \"./blue.png\").
  9210. These functions will be called *before* Org attempts to resolve the
  9211. link by doing text searches in the current buffer - so if you want a
  9212. link \"[[target]]\" to still find \"<<target>>\", your function should
  9213. handle this as a special case.
  9214. When the function does handle the link, it must return a non-nil value.
  9215. If it decides that it is not responsible for this link, it must return
  9216. nil to indicate that that Org-mode can continue with other options
  9217. like exact and fuzzy text search.")
  9218. (defun org-next-link (&optional search-backward)
  9219. "Move forward to the next link.
  9220. If the link is in hidden text, expose it."
  9221. (interactive "P")
  9222. (when (and org-link-search-failed (eq this-command last-command))
  9223. (goto-char (point-min))
  9224. (message "Link search wrapped back to beginning of buffer"))
  9225. (setq org-link-search-failed nil)
  9226. (let* ((pos (point))
  9227. (ct (org-context))
  9228. (a (assoc :link ct))
  9229. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9230. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9231. ((looking-at org-any-link-re)
  9232. ;; Don't stay stuck at link without an org-link face
  9233. (forward-char (if search-backward -1 1))))
  9234. (if (funcall srch-fun org-any-link-re nil t)
  9235. (progn
  9236. (goto-char (match-beginning 0))
  9237. (if (outline-invisible-p) (org-show-context)))
  9238. (goto-char pos)
  9239. (setq org-link-search-failed t)
  9240. (message "No further link found"))))
  9241. (defun org-previous-link ()
  9242. "Move backward to the previous link.
  9243. If the link is in hidden text, expose it."
  9244. (interactive)
  9245. (funcall 'org-next-link t))
  9246. (defun org-translate-link (s)
  9247. "Translate a link string if a translation function has been defined."
  9248. (if (and org-link-translation-function
  9249. (fboundp org-link-translation-function)
  9250. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9251. (progn
  9252. (setq s (funcall org-link-translation-function
  9253. (match-string 1 s) (match-string 2 s)))
  9254. (concat (car s) ":" (cdr s)))
  9255. s))
  9256. (defun org-translate-link-from-planner (type path)
  9257. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9258. This is still an experimental function, your mileage may vary."
  9259. (cond
  9260. ((member type '("http" "https" "news" "ftp"))
  9261. ;; standard Internet links are the same.
  9262. nil)
  9263. ((and (equal type "irc") (string-match "^//" path))
  9264. ;; Planner has two / at the beginning of an irc link, we have 1.
  9265. ;; We should have zero, actually....
  9266. (setq path (substring path 1)))
  9267. ((and (equal type "lisp") (string-match "^/" path))
  9268. ;; Planner has a slash, we do not.
  9269. (setq type "elisp" path (substring path 1)))
  9270. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9271. ;; A typical message link. Planner has the id after the final slash,
  9272. ;; we separate it with a hash mark
  9273. (setq path (concat (match-string 1 path) "#"
  9274. (org-remove-angle-brackets (match-string 2 path))))))
  9275. (cons type path))
  9276. (defun org-find-file-at-mouse (ev)
  9277. "Open file link or URL at mouse."
  9278. (interactive "e")
  9279. (mouse-set-point ev)
  9280. (org-open-at-point 'in-emacs))
  9281. (defun org-open-at-mouse (ev)
  9282. "Open file link or URL at mouse.
  9283. See the docstring of `org-open-file' for details."
  9284. (interactive "e")
  9285. (mouse-set-point ev)
  9286. (if (eq major-mode 'org-agenda-mode)
  9287. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9288. (org-open-at-point))
  9289. (defvar org-window-config-before-follow-link nil
  9290. "The window configuration before following a link.
  9291. This is saved in case the need arises to restore it.")
  9292. (defvar org-open-link-marker (make-marker)
  9293. "Marker pointing to the location where `org-open-at-point; was called.")
  9294. ;;;###autoload
  9295. (defun org-open-at-point-global ()
  9296. "Follow a link like Org-mode does.
  9297. This command can be called in any mode to follow a link that has
  9298. Org-mode syntax."
  9299. (interactive)
  9300. (org-run-like-in-org-mode 'org-open-at-point))
  9301. ;;;###autoload
  9302. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9303. "Open a link in the string S, as if it was in Org-mode."
  9304. (interactive "sLink: \nP")
  9305. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9306. (with-temp-buffer
  9307. (let ((org-inhibit-startup (not reference-buffer)))
  9308. (org-mode)
  9309. (insert s)
  9310. (goto-char (point-min))
  9311. (when reference-buffer
  9312. (setq org-link-abbrev-alist-local
  9313. (with-current-buffer reference-buffer
  9314. org-link-abbrev-alist-local)))
  9315. (org-open-at-point arg reference-buffer)))))
  9316. (defvar org-open-at-point-functions nil
  9317. "Hook that is run when following a link at point.
  9318. Functions in this hook must return t if they identify and follow
  9319. a link at point. If they don't find anything interesting at point,
  9320. they must return nil.")
  9321. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9322. (defun org-open-at-point (&optional arg reference-buffer)
  9323. "Open link at or after point.
  9324. If there is no link at point, this function will search forward up to
  9325. the end of the current line.
  9326. Normally, files will be opened by an appropriate application. If the
  9327. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9328. With a double prefix argument, try to open outside of Emacs, in the
  9329. application the system uses for this file type."
  9330. (interactive "P")
  9331. ;; if in a code block, then open the block's results
  9332. (unless (call-interactively #'org-babel-open-src-block-result)
  9333. (org-load-modules-maybe)
  9334. (move-marker org-open-link-marker (point))
  9335. (setq org-window-config-before-follow-link (current-window-configuration))
  9336. (org-remove-occur-highlights nil nil t)
  9337. (cond
  9338. ((and (org-at-heading-p)
  9339. (not (org-at-timestamp-p t))
  9340. (not (org-in-regexp
  9341. (concat org-plain-link-re "\\|"
  9342. org-bracket-link-regexp "\\|"
  9343. org-angle-link-re "\\|"
  9344. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9345. (not (get-text-property (point) 'org-linked-text)))
  9346. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9347. (lk0 (car lkall))
  9348. (lk (if (stringp lk0) (list lk0) lk0))
  9349. (lkend (cdr lkall)))
  9350. (mapcar (lambda(l)
  9351. (search-forward l nil lkend)
  9352. (goto-char (match-beginning 0))
  9353. (org-open-at-point))
  9354. lk))
  9355. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9356. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9357. ((and (org-at-timestamp-p t)
  9358. (not (org-in-regexp org-bracket-link-regexp)))
  9359. (org-follow-timestamp-link))
  9360. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9361. (not (org-in-regexp org-any-link-re)))
  9362. (org-footnote-action))
  9363. (t
  9364. (let (type path link line search (pos (point)))
  9365. (catch 'match
  9366. (save-excursion
  9367. (skip-chars-forward "^]\n\r")
  9368. (when (org-in-regexp org-bracket-link-regexp 1)
  9369. (setq link (org-extract-attributes
  9370. (org-link-unescape (org-match-string-no-properties 1))))
  9371. (while (string-match " *\n *" link)
  9372. (setq link (replace-match " " t t link)))
  9373. (setq link (org-link-expand-abbrev link))
  9374. (cond
  9375. ((or (file-name-absolute-p link)
  9376. (string-match "^\\.\\.?/" link))
  9377. (setq type "file" path link))
  9378. ((string-match org-link-re-with-space3 link)
  9379. (setq type (match-string 1 link) path (match-string 2 link)))
  9380. ((string-match "^help:+\\(.+\\)" link)
  9381. (setq type "help" path (match-string 1 link)))
  9382. (t (setq type "thisfile" path link)))
  9383. (throw 'match t)))
  9384. (when (get-text-property (point) 'org-linked-text)
  9385. (setq type "thisfile"
  9386. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9387. (1+ (point)) (point))
  9388. path (buffer-substring
  9389. (or (previous-single-property-change pos 'org-linked-text)
  9390. (point-min))
  9391. (or (next-single-property-change pos 'org-linked-text)
  9392. (point-max)))
  9393. ;; Ensure we will search for a <<<radio>>> link, not
  9394. ;; a simple reference like <<ref>>
  9395. path (concat "<" path))
  9396. (throw 'match t))
  9397. (save-excursion
  9398. (when (or (org-in-regexp org-angle-link-re)
  9399. (let ((match (org-in-regexp org-plain-link-re)))
  9400. ;; Check a plain link is not within a bracket link
  9401. (and match
  9402. (save-excursion
  9403. (progn
  9404. (goto-char (car match))
  9405. (not (org-in-regexp org-bracket-link-regexp))))))
  9406. (let ((line_ending (save-excursion (end-of-line) (point))))
  9407. ;; We are in a line before a plain or bracket link
  9408. (or (re-search-forward org-plain-link-re line_ending t)
  9409. (re-search-forward org-bracket-link-regexp line_ending t))))
  9410. (setq type (match-string 1)
  9411. path (org-link-unescape (match-string 2)))
  9412. (throw 'match t)))
  9413. (save-excursion
  9414. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9415. (setq type "tags"
  9416. path (match-string 1))
  9417. (while (string-match ":" path)
  9418. (setq path (replace-match "+" t t path)))
  9419. (throw 'match t)))
  9420. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9421. (setq type "tree-match"
  9422. path (match-string 1))
  9423. (throw 'match t)))
  9424. (unless path
  9425. (user-error "No link found"))
  9426. ;; switch back to reference buffer
  9427. ;; needed when if called in a temporary buffer through
  9428. ;; org-open-link-from-string
  9429. (with-current-buffer (or reference-buffer (current-buffer))
  9430. ;; Remove any trailing spaces in path
  9431. (if (string-match " +\\'" path)
  9432. (setq path (replace-match "" t t path)))
  9433. (if (and org-link-translation-function
  9434. (fboundp org-link-translation-function))
  9435. ;; Check if we need to translate the link
  9436. (let ((tmp (funcall org-link-translation-function type path)))
  9437. (setq type (car tmp) path (cdr tmp))))
  9438. (cond
  9439. ((assoc type org-link-protocols)
  9440. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9441. ((equal type "help")
  9442. (let ((f-or-v (intern path)))
  9443. (cond ((fboundp f-or-v)
  9444. (describe-function f-or-v))
  9445. ((boundp f-or-v)
  9446. (describe-variable f-or-v))
  9447. (t (error "Not a known function or variable")))))
  9448. ((equal type "mailto")
  9449. (let ((cmd (car org-link-mailto-program))
  9450. (args (cdr org-link-mailto-program)) args1
  9451. (address path) (subject "") a)
  9452. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9453. (setq address (match-string 1 path)
  9454. subject (org-link-escape (match-string 2 path))))
  9455. (while args
  9456. (cond
  9457. ((not (stringp (car args))) (push (pop args) args1))
  9458. (t (setq a (pop args))
  9459. (if (string-match "%a" a)
  9460. (setq a (replace-match address t t a)))
  9461. (if (string-match "%s" a)
  9462. (setq a (replace-match subject t t a)))
  9463. (push a args1))))
  9464. (apply cmd (nreverse args1))))
  9465. ((member type '("http" "https" "ftp" "news"))
  9466. (browse-url
  9467. (concat type ":"
  9468. (if (org-string-match-p
  9469. (concat "[[:nonascii:]"
  9470. org-link-escape-chars-browser "]")
  9471. path)
  9472. (org-link-escape path org-link-escape-chars-browser)
  9473. path))))
  9474. ((string= type "doi")
  9475. (browse-url
  9476. (concat org-doi-server-url
  9477. (if (org-string-match-p
  9478. (concat "[[:nonascii:]"
  9479. org-link-escape-chars-browser "]")
  9480. path)
  9481. (org-link-escape path org-link-escape-chars-browser)
  9482. path))))
  9483. ((member type '("message"))
  9484. (browse-url (concat type ":" path)))
  9485. ((string= type "tags")
  9486. (org-tags-view arg path))
  9487. ((string= type "tree-match")
  9488. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9489. ((string= type "file")
  9490. (if (string-match "::\\([0-9]+\\)\\'" path)
  9491. (setq line (string-to-number (match-string 1 path))
  9492. path (substring path 0 (match-beginning 0)))
  9493. (if (string-match "::\\(.+\\)\\'" path)
  9494. (setq search (match-string 1 path)
  9495. path (substring path 0 (match-beginning 0)))))
  9496. (if (string-match "[*?{]" (file-name-nondirectory path))
  9497. (dired path)
  9498. (org-open-file path arg line search)))
  9499. ((string= type "shell")
  9500. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9501. (cmd path))
  9502. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9503. (string-match org-confirm-shell-link-not-regexp cmd))
  9504. (not org-confirm-shell-link-function)
  9505. (funcall org-confirm-shell-link-function
  9506. (format "Execute \"%s\" in shell? "
  9507. (org-add-props cmd nil
  9508. 'face 'org-warning))))
  9509. (progn
  9510. (message "Executing %s" cmd)
  9511. (shell-command cmd buf)
  9512. (if (featurep 'midnight)
  9513. (setq clean-buffer-list-kill-buffer-names
  9514. (cons buf clean-buffer-list-kill-buffer-names))))
  9515. (error "Abort"))))
  9516. ((string= type "elisp")
  9517. (let ((cmd path))
  9518. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9519. (string-match org-confirm-elisp-link-not-regexp cmd))
  9520. (not org-confirm-elisp-link-function)
  9521. (funcall org-confirm-elisp-link-function
  9522. (format "Execute \"%s\" as elisp? "
  9523. (org-add-props cmd nil
  9524. 'face 'org-warning))))
  9525. (message "%s => %s" cmd
  9526. (if (equal (string-to-char cmd) ?\()
  9527. (eval (read cmd))
  9528. (call-interactively (read cmd))))
  9529. (error "Abort"))))
  9530. ((and (string= type "thisfile")
  9531. (run-hook-with-args-until-success
  9532. 'org-open-link-functions path)))
  9533. ((string= type "thisfile")
  9534. (if arg
  9535. (switch-to-buffer-other-window
  9536. (org-get-buffer-for-internal-link (current-buffer)))
  9537. (org-mark-ring-push))
  9538. (let ((cmd `(org-link-search
  9539. ,path
  9540. ,(cond ((equal arg '(4)) ''occur)
  9541. ((equal arg '(16)) ''org-occur))
  9542. ,pos)))
  9543. (condition-case nil (let ((org-link-search-inhibit-query t))
  9544. (eval cmd))
  9545. (error (progn (widen) (eval cmd))))))
  9546. (t (browse-url-at-point)))))))
  9547. (move-marker org-open-link-marker nil)
  9548. (run-hook-with-args 'org-follow-link-hook)))
  9549. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9550. "Offer links in the current entry and return the selected link.
  9551. If there is only one link, return it.
  9552. If NTH is an integer, return the NTH link found.
  9553. If ZERO is a string, check also this string for a link, and if
  9554. there is one, return it."
  9555. (with-current-buffer buffer
  9556. (save-excursion
  9557. (save-restriction
  9558. (widen)
  9559. (goto-char marker)
  9560. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9561. "\\(" org-angle-link-re "\\)\\|"
  9562. "\\(" org-plain-link-re "\\)"))
  9563. (cnt ?0)
  9564. (in-emacs (if (integerp nth) nil nth))
  9565. have-zero end links link c)
  9566. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9567. (push (match-string 0 zero) links)
  9568. (setq cnt (1- cnt) have-zero t))
  9569. (save-excursion
  9570. (org-back-to-heading t)
  9571. (setq end (save-excursion (outline-next-heading) (point)))
  9572. (while (re-search-forward re end t)
  9573. (push (match-string 0) links))
  9574. (setq links (org-uniquify (reverse links))))
  9575. (cond
  9576. ((null links)
  9577. (message "No links"))
  9578. ((equal (length links) 1)
  9579. (setq link (car links)))
  9580. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9581. (setq link (nth (if have-zero nth (1- nth)) links)))
  9582. (t ; we have to select a link
  9583. (save-excursion
  9584. (save-window-excursion
  9585. (delete-other-windows)
  9586. (with-output-to-temp-buffer "*Select Link*"
  9587. (mapc (lambda (l)
  9588. (if (not (string-match org-bracket-link-regexp l))
  9589. (princ (format "[%c] %s\n" (incf cnt)
  9590. (org-remove-angle-brackets l)))
  9591. (if (match-end 3)
  9592. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9593. (match-string 3 l) (match-string 1 l)))
  9594. (princ (format "[%c] %s\n" (incf cnt)
  9595. (match-string 1 l))))))
  9596. links))
  9597. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9598. (message "Select link to open, RET to open all:")
  9599. (setq c (read-char-exclusive))
  9600. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9601. (when (equal c ?q) (error "Abort"))
  9602. (if (equal c ?\C-m)
  9603. (setq link links)
  9604. (setq nth (- c ?0))
  9605. (if have-zero (setq nth (1+ nth)))
  9606. (unless (and (integerp nth) (>= (length links) nth))
  9607. (user-error "Invalid link selection"))
  9608. (setq link (nth (1- nth) links)))))
  9609. (cons link end))))))
  9610. ;; Add special file links that specify the way of opening
  9611. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9612. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9613. (defun org-open-file-with-system (path)
  9614. "Open file at PATH using the system way of opening it."
  9615. (org-open-file path 'system))
  9616. (defun org-open-file-with-emacs (path)
  9617. "Open file at PATH in Emacs."
  9618. (org-open-file path 'emacs))
  9619. ;;; File search
  9620. (defvar org-create-file-search-functions nil
  9621. "List of functions to construct the right search string for a file link.
  9622. These functions are called in turn with point at the location to
  9623. which the link should point.
  9624. A function in the hook should first test if it would like to
  9625. handle this file type, for example by checking the `major-mode'
  9626. or the file extension. If it decides not to handle this file, it
  9627. should just return nil to give other functions a chance. If it
  9628. does handle the file, it must return the search string to be used
  9629. when following the link. The search string will be part of the
  9630. file link, given after a double colon, and `org-open-at-point'
  9631. will automatically search for it. If special measures must be
  9632. taken to make the search successful, another function should be
  9633. added to the companion hook `org-execute-file-search-functions',
  9634. which see.
  9635. A function in this hook may also use `setq' to set the variable
  9636. `description' to provide a suggestion for the descriptive text to
  9637. be used for this link when it gets inserted into an Org-mode
  9638. buffer with \\[org-insert-link].")
  9639. (defvar org-execute-file-search-functions nil
  9640. "List of functions to execute a file search triggered by a link.
  9641. Functions added to this hook must accept a single argument, the
  9642. search string that was part of the file link, the part after the
  9643. double colon. The function must first check if it would like to
  9644. handle this search, for example by checking the `major-mode' or
  9645. the file extension. If it decides not to handle this search, it
  9646. should just return nil to give other functions a chance. If it
  9647. does handle the search, it must return a non-nil value to keep
  9648. other functions from trying.
  9649. Each function can access the current prefix argument through the
  9650. variable `current-prefix-arg'. Note that a single prefix is used
  9651. to force opening a link in Emacs, so it may be good to only use a
  9652. numeric or double prefix to guide the search function.
  9653. In case this is needed, a function in this hook can also restore
  9654. the window configuration before `org-open-at-point' was called using:
  9655. (set-window-configuration org-window-config-before-follow-link)")
  9656. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9657. (defun org-link-search (s &optional type avoid-pos stealth)
  9658. "Search for a link search option.
  9659. If S is surrounded by forward slashes, it is interpreted as a
  9660. regular expression. In org-mode files, this will create an `org-occur'
  9661. sparse tree. In ordinary files, `occur' will be used to list matches.
  9662. If the current buffer is in `dired-mode', grep will be used to search
  9663. in all files. If AVOID-POS is given, ignore matches near that position.
  9664. When optional argument STEALTH is non-nil, do not modify
  9665. visibility around point, thus ignoring
  9666. `org-show-hierarchy-above', `org-show-following-heading' and
  9667. `org-show-siblings' variables."
  9668. (let ((case-fold-search t)
  9669. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9670. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9671. (append '(("") (" ") ("\t") ("\n"))
  9672. org-emphasis-alist)
  9673. "\\|") "\\)"))
  9674. (pos (point))
  9675. (pre nil) (post nil)
  9676. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9677. (cond
  9678. ;; First check if there are any special search functions
  9679. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9680. ;; Now try the builtin stuff
  9681. ((and (equal (string-to-char s0) ?#)
  9682. (> (length s0) 1)
  9683. (save-excursion
  9684. (goto-char (point-min))
  9685. (and
  9686. (re-search-forward
  9687. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9688. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9689. (setq type 'dedicated
  9690. pos (match-beginning 0))))
  9691. ;; There is an exact target for this
  9692. (goto-char pos)
  9693. (org-back-to-heading t)))
  9694. ((save-excursion
  9695. (goto-char (point-min))
  9696. (and
  9697. (re-search-forward
  9698. (concat "<<" (regexp-quote s0) ">>") nil t)
  9699. (setq type 'dedicated
  9700. pos (match-beginning 0))))
  9701. ;; There is an exact target for this
  9702. (goto-char pos))
  9703. ((save-excursion
  9704. (goto-char (point-min))
  9705. (and
  9706. (re-search-forward
  9707. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9708. (setq type 'dedicated pos (match-beginning 0))))
  9709. ;; Found an element with a matching #+name affiliated keyword.
  9710. (goto-char pos))
  9711. ((and (string-match "^(\\(.*\\))$" s0)
  9712. (save-excursion
  9713. (goto-char (point-min))
  9714. (and
  9715. (re-search-forward
  9716. (concat "[^[]" (regexp-quote
  9717. (format org-coderef-label-format
  9718. (match-string 1 s0))))
  9719. nil t)
  9720. (setq type 'dedicated
  9721. pos (1+ (match-beginning 0))))))
  9722. ;; There is a coderef target for this
  9723. (goto-char pos))
  9724. ((string-match "^/\\(.*\\)/$" s)
  9725. ;; A regular expression
  9726. (cond
  9727. ((derived-mode-p 'org-mode)
  9728. (org-occur (match-string 1 s)))
  9729. (t (org-do-occur (match-string 1 s)))))
  9730. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9731. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9732. (goto-char (point-min))
  9733. (cond
  9734. ((let (case-fold-search)
  9735. (re-search-forward (format org-complex-heading-regexp-format
  9736. (regexp-quote s))
  9737. nil t))
  9738. ;; OK, found a match
  9739. (setq type 'dedicated)
  9740. (goto-char (match-beginning 0)))
  9741. ((and (not org-link-search-inhibit-query)
  9742. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9743. (y-or-n-p "No match - create this as a new heading? "))
  9744. (goto-char (point-max))
  9745. (or (bolp) (newline))
  9746. (insert "* " s "\n")
  9747. (beginning-of-line 0))
  9748. (t
  9749. (goto-char pos)
  9750. (error "No match"))))
  9751. (t
  9752. ;; A normal search string
  9753. (when (equal (string-to-char s) ?*)
  9754. ;; Anchor on headlines, post may include tags.
  9755. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9756. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9757. s (substring s 1)))
  9758. (remove-text-properties
  9759. 0 (length s)
  9760. '(face nil mouse-face nil keymap nil fontified nil) s)
  9761. ;; Make a series of regular expressions to find a match
  9762. (setq words (org-split-string s "[ \n\r\t]+")
  9763. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9764. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9765. "\\)" markers)
  9766. re2a_ (concat "\\(" (mapconcat 'downcase words
  9767. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9768. re2a (concat "[ \t\r\n]" re2a_)
  9769. re4_ (concat "\\(" (mapconcat 'downcase words
  9770. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9771. re4 (concat "[^a-zA-Z_]" re4_)
  9772. re1 (concat pre re2 post)
  9773. re3 (concat pre (if pre re4_ re4) post)
  9774. re5 (concat pre ".*" re4)
  9775. re2 (concat pre re2)
  9776. re2a (concat pre (if pre re2a_ re2a))
  9777. re4 (concat pre (if pre re4_ re4))
  9778. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9779. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9780. re5 "\\)"))
  9781. (cond
  9782. ((eq type 'org-occur) (org-occur reall))
  9783. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9784. (t (goto-char (point-min))
  9785. (setq type 'fuzzy)
  9786. (if (or (and (org-search-not-self 1 re0 nil t)
  9787. (setq type 'dedicated))
  9788. (org-search-not-self 1 re1 nil t)
  9789. (org-search-not-self 1 re2 nil t)
  9790. (org-search-not-self 1 re2a nil t)
  9791. (org-search-not-self 1 re3 nil t)
  9792. (org-search-not-self 1 re4 nil t)
  9793. (org-search-not-self 1 re5 nil t))
  9794. (goto-char (match-beginning 1))
  9795. (goto-char pos)
  9796. (error "No match"))))))
  9797. (and (derived-mode-p 'org-mode)
  9798. (not stealth)
  9799. (org-show-context 'link-search))
  9800. type))
  9801. (defun org-search-not-self (group &rest args)
  9802. "Execute `re-search-forward', but only accept matches that do not
  9803. enclose the position of `org-open-link-marker'."
  9804. (let ((m org-open-link-marker))
  9805. (catch 'exit
  9806. (while (apply 're-search-forward args)
  9807. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9808. (goto-char (match-end group))
  9809. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9810. (> (match-beginning 0) (marker-position m))
  9811. (< (match-end 0) (marker-position m)))
  9812. (save-match-data
  9813. (or (not (org-in-regexp
  9814. org-bracket-link-analytic-regexp 1))
  9815. (not (match-end 4)) ; no description
  9816. (and (<= (match-beginning 4) (point))
  9817. (>= (match-end 4) (point))))))
  9818. (throw 'exit (point))))))))
  9819. (defun org-get-buffer-for-internal-link (buffer)
  9820. "Return a buffer to be used for displaying the link target of internal links."
  9821. (cond
  9822. ((not org-display-internal-link-with-indirect-buffer)
  9823. buffer)
  9824. ((string-match "(Clone)$" (buffer-name buffer))
  9825. (message "Buffer is already a clone, not making another one")
  9826. ;; we also do not modify visibility in this case
  9827. buffer)
  9828. (t ; make a new indirect buffer for displaying the link
  9829. (let* ((bn (buffer-name buffer))
  9830. (ibn (concat bn "(Clone)"))
  9831. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9832. (with-current-buffer ib (org-overview))
  9833. ib))))
  9834. (defun org-do-occur (regexp &optional cleanup)
  9835. "Call the Emacs command `occur'.
  9836. If CLEANUP is non-nil, remove the printout of the regular expression
  9837. in the *Occur* buffer. This is useful if the regex is long and not useful
  9838. to read."
  9839. (occur regexp)
  9840. (when cleanup
  9841. (let ((cwin (selected-window)) win beg end)
  9842. (when (setq win (get-buffer-window "*Occur*"))
  9843. (select-window win))
  9844. (goto-char (point-min))
  9845. (when (re-search-forward "match[a-z]+" nil t)
  9846. (setq beg (match-end 0))
  9847. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9848. (setq end (1- (match-beginning 0)))))
  9849. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9850. (goto-char (point-min))
  9851. (select-window cwin))))
  9852. ;;; The mark ring for links jumps
  9853. (defvar org-mark-ring nil
  9854. "Mark ring for positions before jumps in Org-mode.")
  9855. (defvar org-mark-ring-last-goto nil
  9856. "Last position in the mark ring used to go back.")
  9857. ;; Fill and close the ring
  9858. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9859. (loop for i from 1 to org-mark-ring-length do
  9860. (push (make-marker) org-mark-ring))
  9861. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9862. org-mark-ring)
  9863. (defun org-mark-ring-push (&optional pos buffer)
  9864. "Put the current position or POS into the mark ring and rotate it."
  9865. (interactive)
  9866. (setq pos (or pos (point)))
  9867. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9868. (move-marker (car org-mark-ring)
  9869. (or pos (point))
  9870. (or buffer (current-buffer)))
  9871. (message "%s"
  9872. (substitute-command-keys
  9873. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9874. (defun org-mark-ring-goto (&optional n)
  9875. "Jump to the previous position in the mark ring.
  9876. With prefix arg N, jump back that many stored positions. When
  9877. called several times in succession, walk through the entire ring.
  9878. Org-mode commands jumping to a different position in the current file,
  9879. or to another Org-mode file, automatically push the old position
  9880. onto the ring."
  9881. (interactive "p")
  9882. (let (p m)
  9883. (if (eq last-command this-command)
  9884. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9885. (setq p org-mark-ring))
  9886. (setq org-mark-ring-last-goto p)
  9887. (setq m (car p))
  9888. (org-pop-to-buffer-same-window (marker-buffer m))
  9889. (goto-char m)
  9890. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9891. (defun org-remove-angle-brackets (s)
  9892. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9893. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9894. s)
  9895. (defun org-add-angle-brackets (s)
  9896. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9897. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9898. s)
  9899. (defun org-remove-double-quotes (s)
  9900. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9901. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9902. s)
  9903. ;;; Following specific links
  9904. (defun org-follow-timestamp-link ()
  9905. "Open an agenda view for the time-stamp date/range at point."
  9906. (cond
  9907. ((org-at-date-range-p t)
  9908. (let ((org-agenda-start-on-weekday)
  9909. (t1 (match-string 1))
  9910. (t2 (match-string 2)) tt1 tt2)
  9911. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9912. tt2 (time-to-days (org-time-string-to-time t2)))
  9913. (let ((org-agenda-buffer-tmp-name
  9914. (format "*Org Agenda(a:%s)"
  9915. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9916. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9917. ((org-at-timestamp-p t)
  9918. (let ((org-agenda-buffer-tmp-name
  9919. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9920. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9921. (substring (match-string 1) 0 10)))
  9922. 1)))
  9923. (t (error "This should not happen"))))
  9924. ;;; Following file links
  9925. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9926. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9927. (declare-function mailcap-mime-info
  9928. "mailcap" (string &optional request no-decode))
  9929. (defvar org-wait nil)
  9930. (defun org-open-file (path &optional in-emacs line search)
  9931. "Open the file at PATH.
  9932. First, this expands any special file name abbreviations. Then the
  9933. configuration variable `org-file-apps' is checked if it contains an
  9934. entry for this file type, and if yes, the corresponding command is launched.
  9935. If no application is found, Emacs simply visits the file.
  9936. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9937. With a double \\[universal-argument] \\[universal-argument] \
  9938. prefix arg, Org tries to avoid opening in Emacs
  9939. and to use an external application to visit the file.
  9940. Optional LINE specifies a line to go to, optional SEARCH a string
  9941. to search for. If LINE or SEARCH is given, the file will be
  9942. opened in Emacs, unless an entry from org-file-apps that makes
  9943. use of groups in a regexp matches.
  9944. If you want to change the way frames are used when following a
  9945. link, please customize `org-link-frame-setup'.
  9946. If the file does not exist, an error is thrown."
  9947. (let* ((file (if (equal path "")
  9948. buffer-file-name
  9949. (substitute-in-file-name (expand-file-name path))))
  9950. (file-apps (append org-file-apps (org-default-apps)))
  9951. (apps (org-remove-if
  9952. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9953. (apps-dlink (org-remove-if-not
  9954. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9955. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9956. (dirp (if remp nil (file-directory-p file)))
  9957. (file (if (and dirp org-open-directory-means-index-dot-org)
  9958. (concat (file-name-as-directory file) "index.org")
  9959. file))
  9960. (a-m-a-p (assq 'auto-mode apps))
  9961. (dfile (downcase file))
  9962. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9963. (link (cond ((and (eq line nil)
  9964. (eq search nil))
  9965. file)
  9966. (line
  9967. (concat file "::" (number-to-string line)))
  9968. (search
  9969. (concat file "::" search))))
  9970. (dlink (downcase link))
  9971. (old-buffer (current-buffer))
  9972. (old-pos (point))
  9973. (old-mode major-mode)
  9974. ext cmd link-match-data)
  9975. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9976. (setq ext (match-string 1 dfile))
  9977. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9978. (setq ext (match-string 1 dfile))))
  9979. (cond
  9980. ((member in-emacs '((16) system))
  9981. (setq cmd (cdr (assoc 'system apps))))
  9982. (in-emacs (setq cmd 'emacs))
  9983. (t
  9984. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9985. (and dirp (cdr (assoc 'directory apps)))
  9986. ; first, try matching against apps-dlink
  9987. ; if we get a match here, store the match data for later
  9988. (let ((match (assoc-default dlink apps-dlink
  9989. 'string-match)))
  9990. (if match
  9991. (progn (setq link-match-data (match-data))
  9992. match)
  9993. (progn (setq in-emacs (or in-emacs line search))
  9994. nil))) ; if we have no match in apps-dlink,
  9995. ; always open the file in emacs if line or search
  9996. ; is given (for backwards compatibility)
  9997. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9998. 'string-match)
  9999. (cdr (assoc ext apps))
  10000. (cdr (assoc t apps))))))
  10001. (when (eq cmd 'system)
  10002. (setq cmd (cdr (assoc 'system apps))))
  10003. (when (eq cmd 'default)
  10004. (setq cmd (cdr (assoc t apps))))
  10005. (when (eq cmd 'mailcap)
  10006. (require 'mailcap)
  10007. (mailcap-parse-mailcaps)
  10008. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10009. (command (mailcap-mime-info mime-type)))
  10010. (if (stringp command)
  10011. (setq cmd command)
  10012. (setq cmd 'emacs))))
  10013. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10014. (not (file-exists-p file))
  10015. (not org-open-non-existing-files))
  10016. (user-error "No such file: %s" file))
  10017. (cond
  10018. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10019. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10020. (while (string-match "['\"]%s['\"]" cmd)
  10021. (setq cmd (replace-match "%s" t t cmd)))
  10022. (while (string-match "%s" cmd)
  10023. (setq cmd (replace-match
  10024. (save-match-data
  10025. (shell-quote-argument
  10026. (convert-standard-filename file)))
  10027. t t cmd)))
  10028. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10029. (save-match-data
  10030. (let ((match-index 1)
  10031. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10032. (set-match-data link-match-data)
  10033. (while (<= match-index number-of-groups)
  10034. (let ((regex (concat "%" (number-to-string match-index)))
  10035. (replace-with (match-string match-index dlink)))
  10036. (while (string-match regex cmd)
  10037. (setq cmd (replace-match replace-with t t cmd))))
  10038. (setq match-index (+ match-index 1)))))
  10039. (save-window-excursion
  10040. (message "Running %s...done" cmd)
  10041. (start-process-shell-command cmd nil cmd)
  10042. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10043. ((or (stringp cmd)
  10044. (eq cmd 'emacs))
  10045. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10046. (widen)
  10047. (if line (org-goto-line line)
  10048. (if search (org-link-search search))))
  10049. ((consp cmd)
  10050. (let ((file (convert-standard-filename file)))
  10051. (save-match-data
  10052. (set-match-data link-match-data)
  10053. (eval cmd))))
  10054. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10055. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10056. (or (not (equal old-buffer (current-buffer)))
  10057. (not (equal old-pos (point))))
  10058. (org-mark-ring-push old-pos old-buffer))))
  10059. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10060. "This function returns non-nil if `entry' uses a regular
  10061. expression which should be matched against the whole link by
  10062. org-open-file.
  10063. It assumes that is the case when the entry uses a regular
  10064. expression which has at least one grouping construct and the
  10065. action is either a lisp form or a command string containing
  10066. '%1', i.e. using at least one subexpression match as a
  10067. parameter."
  10068. (let ((selector (car entry))
  10069. (action (cdr entry)))
  10070. (if (stringp selector)
  10071. (and (> (regexp-opt-depth selector) 0)
  10072. (or (and (stringp action)
  10073. (string-match "%[0-9]" action))
  10074. (consp action)))
  10075. nil)))
  10076. (defun org-default-apps ()
  10077. "Return the default applications for this operating system."
  10078. (cond
  10079. ((eq system-type 'darwin)
  10080. org-file-apps-defaults-macosx)
  10081. ((eq system-type 'windows-nt)
  10082. org-file-apps-defaults-windowsnt)
  10083. (t org-file-apps-defaults-gnu)))
  10084. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10085. "Convert extensions to regular expressions in the cars of LIST.
  10086. Also, weed out any non-string entries, because the return value is used
  10087. only for regexp matching.
  10088. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10089. point to the symbol `emacs', indicating that the file should
  10090. be opened in Emacs."
  10091. (append
  10092. (delq nil
  10093. (mapcar (lambda (x)
  10094. (if (not (stringp (car x)))
  10095. nil
  10096. (if (string-match "\\W" (car x))
  10097. x
  10098. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10099. list))
  10100. (if add-auto-mode
  10101. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10102. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10103. (defun org-file-remote-p (file)
  10104. "Test whether FILE specifies a location on a remote system.
  10105. Return non-nil if the location is indeed remote.
  10106. For example, the filename \"/user@host:/foo\" specifies a location
  10107. on the system \"/user@host:\"."
  10108. (cond ((fboundp 'file-remote-p)
  10109. (file-remote-p file))
  10110. ((fboundp 'tramp-handle-file-remote-p)
  10111. (tramp-handle-file-remote-p file))
  10112. ((and (boundp 'ange-ftp-name-format)
  10113. (string-match (car ange-ftp-name-format) file))
  10114. t)))
  10115. ;;;; Refiling
  10116. (defun org-get-org-file ()
  10117. "Read a filename, with default directory `org-directory'."
  10118. (let ((default (or org-default-notes-file remember-data-file)))
  10119. (read-file-name (format "File name [%s]: " default)
  10120. (file-name-as-directory org-directory)
  10121. default)))
  10122. (defun org-notes-order-reversed-p ()
  10123. "Check if the current file should receive notes in reversed order."
  10124. (cond
  10125. ((not org-reverse-note-order) nil)
  10126. ((eq t org-reverse-note-order) t)
  10127. ((not (listp org-reverse-note-order)) nil)
  10128. (t (catch 'exit
  10129. (let ((all org-reverse-note-order)
  10130. entry)
  10131. (while (setq entry (pop all))
  10132. (if (string-match (car entry) buffer-file-name)
  10133. (throw 'exit (cdr entry))))
  10134. nil)))))
  10135. (defvar org-refile-target-table nil
  10136. "The list of refile targets, created by `org-refile'.")
  10137. (defvar org-agenda-new-buffers nil
  10138. "Buffers created to visit agenda files.")
  10139. (defvar org-refile-cache nil
  10140. "Cache for refile targets.")
  10141. (defvar org-refile-markers nil
  10142. "All the markers used for caching refile locations.")
  10143. (defun org-refile-marker (pos)
  10144. "Get a new refile marker, but only if caching is in use."
  10145. (if (not org-refile-use-cache)
  10146. pos
  10147. (let ((m (make-marker)))
  10148. (move-marker m pos)
  10149. (push m org-refile-markers)
  10150. m)))
  10151. (defun org-refile-cache-clear ()
  10152. "Clear the refile cache and disable all the markers."
  10153. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10154. (setq org-refile-markers nil)
  10155. (setq org-refile-cache nil)
  10156. (message "Refile cache has been cleared"))
  10157. (defun org-refile-cache-check-set (set)
  10158. "Check if all the markers in the cache still have live buffers."
  10159. (let (marker)
  10160. (catch 'exit
  10161. (while (and set (setq marker (nth 3 (pop set))))
  10162. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10163. (when (and marker (null (marker-buffer marker)))
  10164. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10165. (sit-for 3)
  10166. (throw 'exit nil)))
  10167. t)))
  10168. (defun org-refile-cache-put (set &rest identifiers)
  10169. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10170. (let* ((key (sha1 (prin1-to-string identifiers)))
  10171. (entry (assoc key org-refile-cache)))
  10172. (if entry
  10173. (setcdr entry set)
  10174. (push (cons key set) org-refile-cache))))
  10175. (defun org-refile-cache-get (&rest identifiers)
  10176. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10177. (cond
  10178. ((not org-refile-cache) nil)
  10179. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10180. (t
  10181. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10182. org-refile-cache))))
  10183. (and set (org-refile-cache-check-set set) set)))))
  10184. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10185. "Produce a table with refile targets."
  10186. (let ((case-fold-search nil)
  10187. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10188. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10189. targets tgs txt re files f desc descre fast-path-p level pos0)
  10190. (message "Getting targets...")
  10191. (with-current-buffer (or default-buffer (current-buffer))
  10192. (while (setq entry (pop entries))
  10193. (setq files (car entry) desc (cdr entry))
  10194. (setq fast-path-p nil)
  10195. (cond
  10196. ((null files) (setq files (list (current-buffer))))
  10197. ((eq files 'org-agenda-files)
  10198. (setq files (org-agenda-files 'unrestricted)))
  10199. ((and (symbolp files) (fboundp files))
  10200. (setq files (funcall files)))
  10201. ((and (symbolp files) (boundp files))
  10202. (setq files (symbol-value files))))
  10203. (if (stringp files) (setq files (list files)))
  10204. (cond
  10205. ((eq (car desc) :tag)
  10206. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10207. ((eq (car desc) :todo)
  10208. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10209. ((eq (car desc) :regexp)
  10210. (setq descre (cdr desc)))
  10211. ((eq (car desc) :level)
  10212. (setq descre (concat "^\\*\\{" (number-to-string
  10213. (if org-odd-levels-only
  10214. (1- (* 2 (cdr desc)))
  10215. (cdr desc)))
  10216. "\\}[ \t]")))
  10217. ((eq (car desc) :maxlevel)
  10218. (setq fast-path-p t)
  10219. (setq descre (concat "^\\*\\{1," (number-to-string
  10220. (if org-odd-levels-only
  10221. (1- (* 2 (cdr desc)))
  10222. (cdr desc)))
  10223. "\\}[ \t]")))
  10224. (t (error "Bad refiling target description %s" desc)))
  10225. (while (setq f (pop files))
  10226. (with-current-buffer
  10227. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10228. (or
  10229. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10230. (progn
  10231. (if (bufferp f) (setq f (buffer-file-name
  10232. (buffer-base-buffer f))))
  10233. (setq f (and f (expand-file-name f)))
  10234. (if (eq org-refile-use-outline-path 'file)
  10235. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10236. (save-excursion
  10237. (save-restriction
  10238. (widen)
  10239. (goto-char (point-min))
  10240. (while (re-search-forward descre nil t)
  10241. (goto-char (setq pos0 (point-at-bol)))
  10242. (catch 'next
  10243. (when org-refile-target-verify-function
  10244. (save-match-data
  10245. (or (funcall org-refile-target-verify-function)
  10246. (throw 'next t))))
  10247. (when (and (looking-at org-complex-heading-regexp)
  10248. (not (member (match-string 4) excluded-entries))
  10249. (match-string 4))
  10250. (setq level (org-reduced-level
  10251. (- (match-end 1) (match-beginning 1)))
  10252. txt (org-link-display-format (match-string 4))
  10253. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10254. re (format org-complex-heading-regexp-format
  10255. (regexp-quote (match-string 4))))
  10256. (when org-refile-use-outline-path
  10257. (setq txt (mapconcat
  10258. 'org-protect-slash
  10259. (append
  10260. (if (eq org-refile-use-outline-path
  10261. 'file)
  10262. (list (file-name-nondirectory
  10263. (buffer-file-name
  10264. (buffer-base-buffer))))
  10265. (if (eq org-refile-use-outline-path
  10266. 'full-file-path)
  10267. (list (buffer-file-name
  10268. (buffer-base-buffer)))))
  10269. (org-get-outline-path fast-path-p
  10270. level txt)
  10271. (list txt))
  10272. "/")))
  10273. (push (list txt f re (org-refile-marker (point)))
  10274. tgs)))
  10275. (when (= (point) pos0)
  10276. ;; verification function has not moved point
  10277. (goto-char (point-at-eol))))))))
  10278. (when org-refile-use-cache
  10279. (org-refile-cache-put tgs (buffer-file-name) descre))
  10280. (setq targets (append tgs targets))))))
  10281. (message "Getting targets...done")
  10282. (nreverse targets)))
  10283. (defun org-protect-slash (s)
  10284. (while (string-match "/" s)
  10285. (setq s (replace-match "\\" t t s)))
  10286. s)
  10287. (defvar org-olpa (make-vector 20 nil))
  10288. (defun org-get-outline-path (&optional fastp level heading)
  10289. "Return the outline path to the current entry, as a list.
  10290. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10291. routine which makes outline path derivations for an entire file,
  10292. avoiding backtracing. Refile target collection makes use of that."
  10293. (if fastp
  10294. (progn
  10295. (if (> level 19)
  10296. (error "Outline path failure, more than 19 levels"))
  10297. (loop for i from level upto 19 do
  10298. (aset org-olpa i nil))
  10299. (prog1
  10300. (delq nil (append org-olpa nil))
  10301. (aset org-olpa level heading)))
  10302. (let (rtn case-fold-search)
  10303. (save-excursion
  10304. (save-restriction
  10305. (widen)
  10306. (while (org-up-heading-safe)
  10307. (when (looking-at org-complex-heading-regexp)
  10308. (push (org-trim
  10309. (replace-regexp-in-string
  10310. ;; Remove statistical/checkboxes cookies
  10311. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10312. (org-match-string-no-properties 4)))
  10313. rtn)))
  10314. rtn)))))
  10315. (defun org-format-outline-path (path &optional width prefix separator)
  10316. "Format the outline path PATH for display.
  10317. WIDTH is the maximum number of characters that is available.
  10318. PREFIX is a prefix to be included in the returned string,
  10319. such as the file name.
  10320. SEPARATOR is inserted between the different parts of the path,
  10321. the default is \"/\"."
  10322. (setq width (or width 79))
  10323. (if prefix (setq width (- width (length prefix))))
  10324. (if (not path)
  10325. (or prefix "")
  10326. (let* ((nsteps (length path))
  10327. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10328. (maxwidth (if (<= total-width width)
  10329. 10000 ;; everything fits
  10330. ;; we need to shorten the level headings
  10331. (/ (- width nsteps) nsteps)))
  10332. (org-odd-levels-only nil)
  10333. (n 0)
  10334. (total (1+ (length prefix))))
  10335. (setq maxwidth (max maxwidth 10))
  10336. (concat prefix
  10337. (if prefix (or separator "/"))
  10338. (mapconcat
  10339. (lambda (h)
  10340. (setq n (1+ n))
  10341. (if (and (= n nsteps) (< maxwidth 10000))
  10342. (setq maxwidth (- total-width total)))
  10343. (if (< (length h) maxwidth)
  10344. (progn (setq total (+ total (length h) 1)) h)
  10345. (setq h (substring h 0 (- maxwidth 2))
  10346. total (+ total maxwidth 1))
  10347. (if (string-match "[ \t]+\\'" h)
  10348. (setq h (substring h 0 (match-beginning 0))))
  10349. (setq h (concat h "..")))
  10350. (org-add-props h nil 'face
  10351. (nth (% (1- n) org-n-level-faces)
  10352. org-level-faces))
  10353. h)
  10354. path (or separator "/"))))))
  10355. (defun org-display-outline-path (&optional file current separator just-return-string)
  10356. "Display the current outline path in the echo area.
  10357. If FILE is non-nil, prepend the output with the file name.
  10358. If CURRENT is non-nil, append the current heading to the output.
  10359. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10360. the different parts of the path and defaults to \"/\".
  10361. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10362. (interactive "P")
  10363. (let* (case-fold-search
  10364. message-log-max ; Don't populate the *Messages* buffer
  10365. (bfn (buffer-file-name (buffer-base-buffer)))
  10366. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10367. res)
  10368. (if current (setq path (append path
  10369. (save-excursion
  10370. (org-back-to-heading t)
  10371. (if (looking-at org-complex-heading-regexp)
  10372. (list (match-string 4)))))))
  10373. (setq res
  10374. (org-format-outline-path
  10375. path
  10376. (1- (frame-width))
  10377. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10378. separator))
  10379. (if just-return-string
  10380. (org-no-properties res)
  10381. (message "%s" res))))
  10382. (defvar org-refile-history nil
  10383. "History for refiling operations.")
  10384. (defvar org-after-refile-insert-hook nil
  10385. "Hook run after `org-refile' has inserted its stuff at the new location.
  10386. Note that this is still *before* the stuff will be removed from
  10387. the *old* location.")
  10388. (defvar org-capture-last-stored-marker)
  10389. (defvar org-refile-keep nil
  10390. "Non-nil means `org-refile' will copy instead of refile.")
  10391. (defun org-copy ()
  10392. "Like `org-refile', but copy."
  10393. (interactive)
  10394. (let ((org-refile-keep t))
  10395. (funcall 'org-refile nil nil nil "Copy")))
  10396. (defun org-refile (&optional goto default-buffer rfloc msg)
  10397. "Move the entry or entries at point to another heading.
  10398. The list of target headings is compiled using the information in
  10399. `org-refile-targets', which see.
  10400. At the target location, the entry is filed as a subitem of the target
  10401. heading. Depending on `org-reverse-note-order', the new subitem will
  10402. either be the first or the last subitem.
  10403. If there is an active region, all entries in that region will be moved.
  10404. However, the region must fulfill the requirement that the first heading
  10405. is the first one sets the top-level of the moved text - at most siblings
  10406. below it are allowed.
  10407. With prefix arg GOTO, the command will only visit the target location
  10408. and not actually move anything.
  10409. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10410. go to the location where the last refiling operation has put the subtree.
  10411. With a numeric prefix argument of `2', refile to the running clock.
  10412. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10413. being set to `t' and copy to the target location, don't move it.
  10414. Beware that keeping refiled entries may result in duplicated ID
  10415. properties.
  10416. RFLOC can be a refile location obtained in a different way.
  10417. MSG is a string to replace \"Refile\" in the default prompt with
  10418. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10419. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10420. If you are using target caching (see `org-refile-use-cache'),
  10421. you have to clear the target cache in order to find new targets.
  10422. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10423. prefix argument (`C-u C-u C-u C-c C-w')."
  10424. (interactive "P")
  10425. (if (member goto '(0 (64)))
  10426. (org-refile-cache-clear)
  10427. (let* ((actionmsg (or msg "Refile"))
  10428. (cbuf (current-buffer))
  10429. (regionp (org-region-active-p))
  10430. (region-start (and regionp (region-beginning)))
  10431. (region-end (and regionp (region-end)))
  10432. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10433. (org-refile-keep (if (equal goto 3) t org-refile-keep))
  10434. pos it nbuf file re level reversed)
  10435. (setq last-command nil)
  10436. (when regionp
  10437. (goto-char region-start)
  10438. (or (bolp) (goto-char (point-at-bol)))
  10439. (setq region-start (point))
  10440. (unless (or (org-kill-is-subtree-p
  10441. (buffer-substring region-start region-end))
  10442. (prog1 org-refile-active-region-within-subtree
  10443. (let ((s (point-at-eol)))
  10444. (org-toggle-heading)
  10445. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10446. (user-error "The region is not a (sequence of) subtree(s)")))
  10447. (if (equal goto '(16))
  10448. (org-refile-goto-last-stored)
  10449. (when (or
  10450. (and (equal goto 2)
  10451. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10452. (prog1
  10453. (setq it (list (or org-clock-heading "running clock")
  10454. (buffer-file-name
  10455. (marker-buffer org-clock-hd-marker))
  10456. ""
  10457. (marker-position org-clock-hd-marker)))
  10458. (setq goto nil)))
  10459. (setq it (or rfloc
  10460. (let (heading-text)
  10461. (save-excursion
  10462. (unless goto
  10463. (org-back-to-heading t)
  10464. (setq heading-text
  10465. (nth 4 (org-heading-components))))
  10466. (org-refile-get-location
  10467. (cond (goto "Goto")
  10468. (regionp (concat actionmsg " region to"))
  10469. (t (concat actionmsg " subtree \""
  10470. heading-text "\" to")))
  10471. default-buffer
  10472. (and (not (equal '(4) goto))
  10473. org-refile-allow-creating-parent-nodes)
  10474. goto))))))
  10475. (setq file (nth 1 it)
  10476. re (nth 2 it)
  10477. pos (nth 3 it))
  10478. (if (and (not goto)
  10479. pos
  10480. (equal (buffer-file-name) file)
  10481. (if regionp
  10482. (and (>= pos region-start)
  10483. (<= pos region-end))
  10484. (and (>= pos (point))
  10485. (< pos (save-excursion
  10486. (org-end-of-subtree t t))))))
  10487. (error "Cannot refile to position inside the tree or region"))
  10488. (setq nbuf (or (find-buffer-visiting file)
  10489. (find-file-noselect file)))
  10490. (if (and goto (not (equal goto 3)))
  10491. (progn
  10492. (org-pop-to-buffer-same-window nbuf)
  10493. (goto-char pos)
  10494. (org-show-context 'org-goto))
  10495. (if regionp
  10496. (progn
  10497. (org-kill-new (buffer-substring region-start region-end))
  10498. (org-save-markers-in-region region-start region-end))
  10499. (org-copy-subtree 1 nil t))
  10500. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10501. (find-file-noselect file)))
  10502. (setq reversed (org-notes-order-reversed-p))
  10503. (save-excursion
  10504. (save-restriction
  10505. (widen)
  10506. (if pos
  10507. (progn
  10508. (goto-char pos)
  10509. (looking-at org-outline-regexp)
  10510. (setq level (org-get-valid-level (funcall outline-level) 1))
  10511. (goto-char
  10512. (if reversed
  10513. (or (outline-next-heading) (point-max))
  10514. (or (save-excursion (org-get-next-sibling))
  10515. (org-end-of-subtree t t)
  10516. (point-max)))))
  10517. (setq level 1)
  10518. (if (not reversed)
  10519. (goto-char (point-max))
  10520. (goto-char (point-min))
  10521. (or (outline-next-heading) (goto-char (point-max)))))
  10522. (if (not (bolp)) (newline))
  10523. (org-paste-subtree level)
  10524. (when org-log-refile
  10525. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10526. (unless (eq org-log-refile 'note)
  10527. (save-excursion (org-add-log-note))))
  10528. (and org-auto-align-tags
  10529. (let ((org-loop-over-headlines-in-active-region nil))
  10530. (org-set-tags nil t)))
  10531. (with-demoted-errors
  10532. (bookmark-set "org-refile-last-stored"))
  10533. ;; If we are refiling for capture, make sure that the
  10534. ;; last-capture pointers point here
  10535. (when (org-bound-and-true-p org-refile-for-capture)
  10536. (with-demoted-errors
  10537. (bookmark-set "org-capture-last-stored-marker"))
  10538. (move-marker org-capture-last-stored-marker (point)))
  10539. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10540. (run-hooks 'org-after-refile-insert-hook))))
  10541. (unless org-refile-keep
  10542. (if regionp
  10543. (delete-region (point) (+ (point) (- region-end region-start)))
  10544. (delete-region
  10545. (and (org-back-to-heading t) (point))
  10546. (min (buffer-size) (org-end-of-subtree t t) (point)))))
  10547. (when (featurep 'org-inlinetask)
  10548. (org-inlinetask-remove-END-maybe))
  10549. (setq org-markers-to-move nil)
  10550. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10551. (defun org-refile-goto-last-stored ()
  10552. "Go to the location where the last refile was stored."
  10553. (interactive)
  10554. (bookmark-jump "org-refile-last-stored")
  10555. (message "This is the location of the last refile"))
  10556. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10557. no-exclude)
  10558. "Prompt the user for a refile location, using PROMPT.
  10559. PROMPT should not be suffixed with a colon and a space, because
  10560. this function appends the default value from
  10561. `org-refile-history' automatically, if that is not empty.
  10562. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10563. this is used for the GOTO interface."
  10564. (let ((org-refile-targets org-refile-targets)
  10565. (org-refile-use-outline-path org-refile-use-outline-path)
  10566. excluded-entries)
  10567. (when (and (derived-mode-p 'org-mode)
  10568. (not org-refile-use-cache)
  10569. (not no-exclude))
  10570. (org-map-tree
  10571. (lambda()
  10572. (setq excluded-entries
  10573. (append excluded-entries (list (org-get-heading t t)))))))
  10574. (setq org-refile-target-table
  10575. (org-refile-get-targets default-buffer excluded-entries)))
  10576. (unless org-refile-target-table
  10577. (user-error "No refile targets"))
  10578. (let* ((cbuf (current-buffer))
  10579. (partial-completion-mode nil)
  10580. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10581. (cfunc (if (and org-refile-use-outline-path
  10582. org-outline-path-complete-in-steps)
  10583. 'org-olpath-completing-read
  10584. 'org-icompleting-read))
  10585. (extra (if org-refile-use-outline-path "/" ""))
  10586. (cbnex (concat (buffer-name) extra))
  10587. (filename (and cfn (expand-file-name cfn)))
  10588. (tbl (mapcar
  10589. (lambda (x)
  10590. (if (and (not (member org-refile-use-outline-path
  10591. '(file full-file-path)))
  10592. (not (equal filename (nth 1 x))))
  10593. (cons (concat (car x) extra " ("
  10594. (file-name-nondirectory (nth 1 x)) ")")
  10595. (cdr x))
  10596. (cons (concat (car x) extra) (cdr x))))
  10597. org-refile-target-table))
  10598. (completion-ignore-case t)
  10599. cdef
  10600. (prompt (concat prompt
  10601. (or (and (car org-refile-history)
  10602. (concat " (default " (car org-refile-history) ")"))
  10603. (and (assoc cbnex tbl) (setq cdef cbnex)
  10604. (concat " (default " cbnex ")"))) ": "))
  10605. pa answ parent-target child parent old-hist)
  10606. (setq old-hist org-refile-history)
  10607. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10608. nil 'org-refile-history (or cdef (car org-refile-history))))
  10609. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10610. (org-refile-check-position pa)
  10611. (if pa
  10612. (progn
  10613. (when (or (not org-refile-history)
  10614. (not (eq old-hist org-refile-history))
  10615. (not (equal (car pa) (car org-refile-history))))
  10616. (setq org-refile-history
  10617. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10618. org-refile-history
  10619. (cdr org-refile-history))))
  10620. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10621. (pop org-refile-history)))
  10622. pa)
  10623. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10624. (progn
  10625. (setq parent (match-string 1 answ)
  10626. child (match-string 2 answ))
  10627. (setq parent-target (or (assoc parent tbl)
  10628. (assoc (concat parent "/") tbl)))
  10629. (when (and parent-target
  10630. (or (eq new-nodes t)
  10631. (and (eq new-nodes 'confirm)
  10632. (y-or-n-p (format "Create new node \"%s\"? "
  10633. child)))))
  10634. (org-refile-new-child parent-target child)))
  10635. (user-error "Invalid target location")))))
  10636. (declare-function org-string-nw-p "org-macs" (s))
  10637. (defun org-refile-check-position (refile-pointer)
  10638. "Check if the refile pointer matches the headline to which it points."
  10639. (let* ((file (nth 1 refile-pointer))
  10640. (re (nth 2 refile-pointer))
  10641. (pos (nth 3 refile-pointer))
  10642. buffer)
  10643. (if (and (not (markerp pos)) (not file))
  10644. (user-error "Please save the buffer to a file before refiling")
  10645. (when (org-string-nw-p re)
  10646. (setq buffer (if (markerp pos)
  10647. (marker-buffer pos)
  10648. (or (find-buffer-visiting file)
  10649. (find-file-noselect file))))
  10650. (with-current-buffer buffer
  10651. (save-excursion
  10652. (save-restriction
  10653. (widen)
  10654. (goto-char pos)
  10655. (beginning-of-line 1)
  10656. (unless (org-looking-at-p re)
  10657. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10658. (defun org-refile-new-child (parent-target child)
  10659. "Use refile target PARENT-TARGET to add new CHILD below it."
  10660. (unless parent-target
  10661. (error "Cannot find parent for new node"))
  10662. (let ((file (nth 1 parent-target))
  10663. (pos (nth 3 parent-target))
  10664. level)
  10665. (with-current-buffer (or (find-buffer-visiting file)
  10666. (find-file-noselect file))
  10667. (save-excursion
  10668. (save-restriction
  10669. (widen)
  10670. (if pos
  10671. (goto-char pos)
  10672. (goto-char (point-max))
  10673. (if (not (bolp)) (newline)))
  10674. (when (looking-at org-outline-regexp)
  10675. (setq level (funcall outline-level))
  10676. (org-end-of-subtree t t))
  10677. (org-back-over-empty-lines)
  10678. (insert "\n" (make-string
  10679. (if pos (org-get-valid-level level 1) 1) ?*)
  10680. " " child "\n")
  10681. (beginning-of-line 0)
  10682. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10683. (defun org-olpath-completing-read (prompt collection &rest args)
  10684. "Read an outline path like a file name."
  10685. (let ((thetable collection)
  10686. (org-completion-use-ido nil) ; does not work with ido.
  10687. (org-completion-use-iswitchb nil)) ; or iswitchb
  10688. (apply
  10689. 'org-icompleting-read prompt
  10690. (lambda (string predicate &optional flag)
  10691. (let (rtn r f (l (length string)))
  10692. (cond
  10693. ((eq flag nil)
  10694. ;; try completion
  10695. (try-completion string thetable))
  10696. ((eq flag t)
  10697. ;; all-completions
  10698. (setq rtn (all-completions string thetable predicate))
  10699. (mapcar
  10700. (lambda (x)
  10701. (setq r (substring x l))
  10702. (if (string-match " ([^)]*)$" x)
  10703. (setq f (match-string 0 x))
  10704. (setq f ""))
  10705. (if (string-match "/" r)
  10706. (concat string (substring r 0 (match-end 0)) f)
  10707. x))
  10708. rtn))
  10709. ((eq flag 'lambda)
  10710. ;; exact match?
  10711. (assoc string thetable)))))
  10712. args)))
  10713. ;;;; Dynamic blocks
  10714. (defun org-find-dblock (name)
  10715. "Find the first dynamic block with name NAME in the buffer.
  10716. If not found, stay at current position and return nil."
  10717. (let ((case-fold-search t) pos)
  10718. (save-excursion
  10719. (goto-char (point-min))
  10720. (setq pos (and (re-search-forward
  10721. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10722. (match-beginning 0))))
  10723. (if pos (goto-char pos))
  10724. pos))
  10725. (defconst org-dblock-start-re
  10726. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10727. "Matches the start line of a dynamic block, with parameters.")
  10728. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10729. "Matches the end of a dynamic block.")
  10730. (defun org-create-dblock (plist)
  10731. "Create a dynamic block section, with parameters taken from PLIST.
  10732. PLIST must contain a :name entry which is used as name of the block."
  10733. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10734. (end-of-line 1)
  10735. (newline))
  10736. (let ((col (current-column))
  10737. (name (plist-get plist :name)))
  10738. (insert "#+BEGIN: " name)
  10739. (while plist
  10740. (if (eq (car plist) :name)
  10741. (setq plist (cddr plist))
  10742. (insert " " (prin1-to-string (pop plist)))))
  10743. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10744. (beginning-of-line -2)))
  10745. (defun org-prepare-dblock ()
  10746. "Prepare dynamic block for refresh.
  10747. This empties the block, puts the cursor at the insert position and returns
  10748. the property list including an extra property :name with the block name."
  10749. (unless (looking-at org-dblock-start-re)
  10750. (user-error "Not at a dynamic block"))
  10751. (let* ((begdel (1+ (match-end 0)))
  10752. (name (org-no-properties (match-string 1)))
  10753. (params (append (list :name name)
  10754. (read (concat "(" (match-string 3) ")")))))
  10755. (save-excursion
  10756. (beginning-of-line 1)
  10757. (skip-chars-forward " \t")
  10758. (setq params (plist-put params :indentation-column (current-column))))
  10759. (unless (re-search-forward org-dblock-end-re nil t)
  10760. (error "Dynamic block not terminated"))
  10761. (setq params
  10762. (append params
  10763. (list :content (buffer-substring
  10764. begdel (match-beginning 0)))))
  10765. (delete-region begdel (match-beginning 0))
  10766. (goto-char begdel)
  10767. (open-line 1)
  10768. params))
  10769. (defun org-map-dblocks (&optional command)
  10770. "Apply COMMAND to all dynamic blocks in the current buffer.
  10771. If COMMAND is not given, use `org-update-dblock'."
  10772. (let ((cmd (or command 'org-update-dblock)))
  10773. (save-excursion
  10774. (goto-char (point-min))
  10775. (while (re-search-forward org-dblock-start-re nil t)
  10776. (goto-char (match-beginning 0))
  10777. (save-excursion
  10778. (condition-case nil
  10779. (funcall cmd)
  10780. (error (message "Error during update of dynamic block"))))
  10781. (unless (re-search-forward org-dblock-end-re nil t)
  10782. (error "Dynamic block not terminated"))))))
  10783. (defun org-dblock-update (&optional arg)
  10784. "User command for updating dynamic blocks.
  10785. Update the dynamic block at point. With prefix ARG, update all dynamic
  10786. blocks in the buffer."
  10787. (interactive "P")
  10788. (if arg
  10789. (org-update-all-dblocks)
  10790. (or (looking-at org-dblock-start-re)
  10791. (org-beginning-of-dblock))
  10792. (org-update-dblock)))
  10793. (defun org-update-dblock ()
  10794. "Update the dynamic block at point.
  10795. This means to empty the block, parse for parameters and then call
  10796. the correct writing function."
  10797. (interactive)
  10798. (save-window-excursion
  10799. (let* ((pos (point))
  10800. (line (org-current-line))
  10801. (params (org-prepare-dblock))
  10802. (name (plist-get params :name))
  10803. (indent (plist-get params :indentation-column))
  10804. (cmd (intern (concat "org-dblock-write:" name))))
  10805. (message "Updating dynamic block `%s' at line %d..." name line)
  10806. (funcall cmd params)
  10807. (message "Updating dynamic block `%s' at line %d...done" name line)
  10808. (goto-char pos)
  10809. (when (and indent (> indent 0))
  10810. (setq indent (make-string indent ?\ ))
  10811. (save-excursion
  10812. (org-beginning-of-dblock)
  10813. (forward-line 1)
  10814. (while (not (looking-at org-dblock-end-re))
  10815. (insert indent)
  10816. (beginning-of-line 2))
  10817. (when (looking-at org-dblock-end-re)
  10818. (and (looking-at "[ \t]+")
  10819. (replace-match ""))
  10820. (insert indent)))))))
  10821. (defun org-beginning-of-dblock ()
  10822. "Find the beginning of the dynamic block at point.
  10823. Error if there is no such block at point."
  10824. (let ((pos (point))
  10825. beg)
  10826. (end-of-line 1)
  10827. (if (and (re-search-backward org-dblock-start-re nil t)
  10828. (setq beg (match-beginning 0))
  10829. (re-search-forward org-dblock-end-re nil t)
  10830. (> (match-end 0) pos))
  10831. (goto-char beg)
  10832. (goto-char pos)
  10833. (error "Not in a dynamic block"))))
  10834. (defun org-update-all-dblocks ()
  10835. "Update all dynamic blocks in the buffer.
  10836. This function can be used in a hook."
  10837. (interactive)
  10838. (when (derived-mode-p 'org-mode)
  10839. (org-map-dblocks 'org-update-dblock)))
  10840. ;;;; Completion
  10841. (declare-function org-export-backend-name "org-export" (cl-x))
  10842. (declare-function org-export-backend-options "org-export" (cl-x))
  10843. (defun org-get-export-keywords ()
  10844. "Return a list of all currently understood export keywords.
  10845. Export keywords include options, block names, attributes and
  10846. keywords relative to each registered export back-end."
  10847. (let (keywords)
  10848. (dolist (backend
  10849. (org-bound-and-true-p org-export--registered-backends)
  10850. (delq nil keywords))
  10851. ;; Back-end name (for keywords, like #+LATEX:)
  10852. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10853. (dolist (option-entry (org-export-backend-options backend))
  10854. ;; Back-end options.
  10855. (push (nth 1 option-entry) keywords)))))
  10856. (defconst org-options-keywords
  10857. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10858. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10859. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10860. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10861. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10862. (defcustom org-structure-template-alist
  10863. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10864. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10865. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10866. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10867. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10868. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10869. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10870. "<literal style=\"latex\">\n?\n</literal>")
  10871. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10872. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10873. "<literal style=\"html\">\n?\n</literal>")
  10874. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10875. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10876. ("A" "#+ASCII: " "")
  10877. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10878. ("I" "#+INCLUDE: %file ?"
  10879. "<include file=%file markup=\"?\">"))
  10880. "Structure completion elements.
  10881. This is a list of abbreviation keys and values. The value gets inserted
  10882. if you type `<' followed by the key and then press the completion key,
  10883. usually `M-TAB'. %file will be replaced by a file name after prompting
  10884. for the file using completion. The cursor will be placed at the position
  10885. of the `?` in the template.
  10886. There are two templates for each key, the first uses the original Org syntax,
  10887. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10888. the default when the /org-mtags.el/ module has been loaded. See also the
  10889. variable `org-mtags-prefer-muse-templates'."
  10890. :group 'org-completion
  10891. :type '(repeat
  10892. (list
  10893. (string :tag "Key")
  10894. (string :tag "Template")
  10895. (string :tag "Muse Template"))))
  10896. (defun org-try-structure-completion ()
  10897. "Try to complete a structure template before point.
  10898. This looks for strings like \"<e\" on an otherwise empty line and
  10899. expands them."
  10900. (let ((l (buffer-substring (point-at-bol) (point)))
  10901. a)
  10902. (when (and (looking-at "[ \t]*$")
  10903. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10904. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10905. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10906. (match-beginning 1)) a)
  10907. t)))
  10908. (defun org-complete-expand-structure-template (start cell)
  10909. "Expand a structure template."
  10910. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10911. (rpl (nth (if musep 2 1) cell))
  10912. (ind ""))
  10913. (delete-region start (point))
  10914. (when (string-match "\\`#\\+" rpl)
  10915. (cond
  10916. ((bolp))
  10917. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10918. (setq ind (buffer-substring (point-at-bol) (point))))
  10919. (t (newline))))
  10920. (setq start (point))
  10921. (if (string-match "%file" rpl)
  10922. (setq rpl (replace-match
  10923. (concat
  10924. "\""
  10925. (save-match-data
  10926. (abbreviate-file-name (read-file-name "Include file: ")))
  10927. "\"")
  10928. t t rpl)))
  10929. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10930. (concat "\n" ind)))
  10931. (insert rpl)
  10932. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10933. ;;;; TODO, DEADLINE, Comments
  10934. (defun org-toggle-comment ()
  10935. "Change the COMMENT state of an entry."
  10936. (interactive)
  10937. (save-excursion
  10938. (org-back-to-heading)
  10939. (let (case-fold-search)
  10940. (cond
  10941. ((looking-at (format org-heading-keyword-regexp-format
  10942. org-comment-string))
  10943. (goto-char (match-end 1))
  10944. (looking-at (concat " +" org-comment-string))
  10945. (replace-match "" t t)
  10946. (when (eolp) (insert " ")))
  10947. ((looking-at org-outline-regexp)
  10948. (goto-char (match-end 0))
  10949. (insert org-comment-string " "))))))
  10950. (defvar org-last-todo-state-is-todo nil
  10951. "This is non-nil when the last TODO state change led to a TODO state.
  10952. If the last change removed the TODO tag or switched to DONE, then
  10953. this is nil.")
  10954. (defvar org-setting-tags nil) ; dynamically skipped
  10955. (defvar org-todo-setup-filter-hook nil
  10956. "Hook for functions that pre-filter todo specs.
  10957. Each function takes a todo spec and returns either nil or the spec
  10958. transformed into canonical form." )
  10959. (defvar org-todo-get-default-hook nil
  10960. "Hook for functions that get a default item for todo.
  10961. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10962. nil or a string to be used for the todo mark." )
  10963. (defvar org-agenda-headline-snapshot-before-repeat)
  10964. (defun org-current-effective-time ()
  10965. "Return current time adjusted for `org-extend-today-until' variable."
  10966. (let* ((ct (org-current-time))
  10967. (dct (decode-time ct))
  10968. (ct1
  10969. (cond
  10970. (org-use-last-clock-out-time-as-effective-time
  10971. (or (org-clock-get-last-clock-out-time) ct))
  10972. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10973. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10974. (t ct))))
  10975. ct1))
  10976. (defun org-todo-yesterday (&optional arg)
  10977. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10978. (interactive "P")
  10979. (if (eq major-mode 'org-agenda-mode)
  10980. (apply 'org-agenda-todo-yesterday arg)
  10981. (let* ((hour (third (decode-time
  10982. (org-current-time))))
  10983. (org-extend-today-until (1+ hour)))
  10984. (org-todo arg))))
  10985. (defvar org-block-entry-blocking ""
  10986. "First entry preventing the TODO state change.")
  10987. (defun org-todo (&optional arg)
  10988. "Change the TODO state of an item.
  10989. The state of an item is given by a keyword at the start of the heading,
  10990. like
  10991. *** TODO Write paper
  10992. *** DONE Call mom
  10993. The different keywords are specified in the variable `org-todo-keywords'.
  10994. By default the available states are \"TODO\" and \"DONE\".
  10995. So for this example: when the item starts with TODO, it is changed to DONE.
  10996. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10997. DONE are present, add TODO at the beginning of the heading.
  10998. With \\[universal-argument] prefix arg, use completion to determine the new \
  10999. state.
  11000. With numeric prefix arg, switch to that state.
  11001. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11002. keywords (nextset).
  11003. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11004. With a numeric prefix arg of 0, inhibit note taking for the change.
  11005. For calling through lisp, arg is also interpreted in the following way:
  11006. 'none -> empty state
  11007. \"\"(empty string) -> switch to empty state
  11008. 'done -> switch to DONE
  11009. 'nextset -> switch to the next set of keywords
  11010. 'previousset -> switch to the previous set of keywords
  11011. \"WAITING\" -> switch to the specified keyword, but only if it
  11012. really is a member of `org-todo-keywords'."
  11013. (interactive "P")
  11014. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11015. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11016. 'region-start-level 'region))
  11017. org-loop-over-headlines-in-active-region)
  11018. (org-map-entries
  11019. `(org-todo ,arg)
  11020. org-loop-over-headlines-in-active-region
  11021. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11022. (if (equal arg '(16)) (setq arg 'nextset))
  11023. (let ((org-blocker-hook org-blocker-hook)
  11024. commentp
  11025. case-fold-search)
  11026. (when (equal arg '(64))
  11027. (setq arg nil org-blocker-hook nil))
  11028. (when (and org-blocker-hook
  11029. (or org-inhibit-blocking
  11030. (org-entry-get nil "NOBLOCKING")))
  11031. (setq org-blocker-hook nil))
  11032. (save-excursion
  11033. (catch 'exit
  11034. (org-back-to-heading t)
  11035. (when (looking-at (concat "^\\*+ " org-comment-string))
  11036. (org-toggle-comment)
  11037. (setq commentp t))
  11038. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11039. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11040. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11041. (let* ((match-data (match-data))
  11042. (startpos (point-at-bol))
  11043. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11044. (org-log-done org-log-done)
  11045. (org-log-repeat org-log-repeat)
  11046. (org-todo-log-states org-todo-log-states)
  11047. (org-inhibit-logging
  11048. (if (equal arg 0)
  11049. (progn (setq arg nil) 'note) org-inhibit-logging))
  11050. (this (match-string 1))
  11051. (hl-pos (match-beginning 0))
  11052. (head (org-get-todo-sequence-head this))
  11053. (ass (assoc head org-todo-kwd-alist))
  11054. (interpret (nth 1 ass))
  11055. (done-word (nth 3 ass))
  11056. (final-done-word (nth 4 ass))
  11057. (org-last-state (or this ""))
  11058. (completion-ignore-case t)
  11059. (member (member this org-todo-keywords-1))
  11060. (tail (cdr member))
  11061. (org-state (cond
  11062. ((and org-todo-key-trigger
  11063. (or (and (equal arg '(4))
  11064. (eq org-use-fast-todo-selection 'prefix))
  11065. (and (not arg) org-use-fast-todo-selection
  11066. (not (eq org-use-fast-todo-selection
  11067. 'prefix)))))
  11068. ;; Use fast selection
  11069. (org-fast-todo-selection))
  11070. ((and (equal arg '(4))
  11071. (or (not org-use-fast-todo-selection)
  11072. (not org-todo-key-trigger)))
  11073. ;; Read a state with completion
  11074. (org-icompleting-read
  11075. "State: " (mapcar 'list org-todo-keywords-1)
  11076. nil t))
  11077. ((eq arg 'right)
  11078. (if this
  11079. (if tail (car tail) nil)
  11080. (car org-todo-keywords-1)))
  11081. ((eq arg 'left)
  11082. (if (equal member org-todo-keywords-1)
  11083. nil
  11084. (if this
  11085. (nth (- (length org-todo-keywords-1)
  11086. (length tail) 2)
  11087. org-todo-keywords-1)
  11088. (org-last org-todo-keywords-1))))
  11089. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11090. (setq arg nil))) ; hack to fall back to cycling
  11091. (arg
  11092. ;; user or caller requests a specific state
  11093. (cond
  11094. ((equal arg "") nil)
  11095. ((eq arg 'none) nil)
  11096. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11097. ((eq arg 'nextset)
  11098. (or (car (cdr (member head org-todo-heads)))
  11099. (car org-todo-heads)))
  11100. ((eq arg 'previousset)
  11101. (let ((org-todo-heads (reverse org-todo-heads)))
  11102. (or (car (cdr (member head org-todo-heads)))
  11103. (car org-todo-heads))))
  11104. ((car (member arg org-todo-keywords-1)))
  11105. ((stringp arg)
  11106. (user-error "State `%s' not valid in this file" arg))
  11107. ((nth (1- (prefix-numeric-value arg))
  11108. org-todo-keywords-1))))
  11109. ((null member) (or head (car org-todo-keywords-1)))
  11110. ((equal this final-done-word) nil) ;; -> make empty
  11111. ((null tail) nil) ;; -> first entry
  11112. ((memq interpret '(type priority))
  11113. (if (eq this-command last-command)
  11114. (car tail)
  11115. (if (> (length tail) 0)
  11116. (or done-word (car org-done-keywords))
  11117. nil)))
  11118. (t
  11119. (car tail))))
  11120. (org-state (or
  11121. (run-hook-with-args-until-success
  11122. 'org-todo-get-default-hook org-state org-last-state)
  11123. org-state))
  11124. (next (if org-state (concat " " org-state " ") " "))
  11125. (change-plist (list :type 'todo-state-change :from this :to org-state
  11126. :position startpos))
  11127. dolog now-done-p)
  11128. (when org-blocker-hook
  11129. (setq org-last-todo-state-is-todo
  11130. (not (member this org-done-keywords)))
  11131. (unless (save-excursion
  11132. (save-match-data
  11133. (org-with-wide-buffer
  11134. (run-hook-with-args-until-failure
  11135. 'org-blocker-hook change-plist))))
  11136. (if (org-called-interactively-p 'interactive)
  11137. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11138. this org-state org-block-entry-blocking)
  11139. ;; fail silently
  11140. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11141. this org-state org-block-entry-blocking)
  11142. (throw 'exit nil))))
  11143. (store-match-data match-data)
  11144. (replace-match next t t)
  11145. (unless (pos-visible-in-window-p hl-pos)
  11146. (message "TODO state changed to %s" (org-trim next)))
  11147. (unless head
  11148. (setq head (org-get-todo-sequence-head org-state)
  11149. ass (assoc head org-todo-kwd-alist)
  11150. interpret (nth 1 ass)
  11151. done-word (nth 3 ass)
  11152. final-done-word (nth 4 ass)))
  11153. (when (memq arg '(nextset previousset))
  11154. (message "Keyword-Set %d/%d: %s"
  11155. (- (length org-todo-sets) -1
  11156. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11157. (length org-todo-sets)
  11158. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11159. (setq org-last-todo-state-is-todo
  11160. (not (member org-state org-done-keywords)))
  11161. (setq now-done-p (and (member org-state org-done-keywords)
  11162. (not (member this org-done-keywords))))
  11163. (and logging (org-local-logging logging))
  11164. (when (and (or org-todo-log-states org-log-done)
  11165. (not (eq org-inhibit-logging t))
  11166. (not (memq arg '(nextset previousset))))
  11167. ;; we need to look at recording a time and note
  11168. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11169. (nth 2 (assoc this org-todo-log-states))))
  11170. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11171. (setq dolog 'time))
  11172. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11173. (and org-state
  11174. (member org-state org-not-done-keywords)
  11175. (not (member this org-not-done-keywords))))
  11176. ;; This is now a todo state and was not one before
  11177. ;; If there was a CLOSED time stamp, get rid of it.
  11178. (org-add-planning-info nil nil 'closed))
  11179. (when (and now-done-p org-log-done)
  11180. ;; It is now done, and it was not done before
  11181. (org-add-planning-info 'closed (org-current-effective-time))
  11182. (if (and (not dolog) (eq 'note org-log-done))
  11183. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11184. (when (and org-state dolog)
  11185. ;; This is a non-nil state, and we need to log it
  11186. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11187. ;; Fixup tag positioning
  11188. (org-todo-trigger-tag-changes org-state)
  11189. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11190. (when org-provide-todo-statistics
  11191. (org-update-parent-todo-statistics))
  11192. (run-hooks 'org-after-todo-state-change-hook)
  11193. (if (and arg (not (member org-state org-done-keywords)))
  11194. (setq head (org-get-todo-sequence-head org-state)))
  11195. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11196. ;; Do we need to trigger a repeat?
  11197. (when now-done-p
  11198. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11199. ;; This is for the agenda, take a snapshot of the headline.
  11200. (save-match-data
  11201. (setq org-agenda-headline-snapshot-before-repeat
  11202. (org-get-heading))))
  11203. (org-auto-repeat-maybe org-state))
  11204. ;; Fixup cursor location if close to the keyword
  11205. (if (and (outline-on-heading-p)
  11206. (not (bolp))
  11207. (save-excursion (beginning-of-line 1)
  11208. (looking-at org-todo-line-regexp))
  11209. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11210. (progn
  11211. (goto-char (or (match-end 2) (match-end 1)))
  11212. (and (looking-at " ") (just-one-space))))
  11213. (when org-trigger-hook
  11214. (save-excursion
  11215. (run-hook-with-args 'org-trigger-hook change-plist)))
  11216. (when commentp (org-toggle-comment))))))))
  11217. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11218. "Block turning an entry into a TODO, using the hierarchy.
  11219. This checks whether the current task should be blocked from state
  11220. changes. Such blocking occurs when:
  11221. 1. The task has children which are not all in a completed state.
  11222. 2. A task has a parent with the property :ORDERED:, and there
  11223. are siblings prior to the current task with incomplete
  11224. status.
  11225. 3. The parent of the task is blocked because it has siblings that should
  11226. be done first, or is child of a block grandparent TODO entry."
  11227. (if (not org-enforce-todo-dependencies)
  11228. t ; if locally turned off don't block
  11229. (catch 'dont-block
  11230. ;; If this is not a todo state change, or if this entry is already DONE,
  11231. ;; do not block
  11232. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11233. (member (plist-get change-plist :from)
  11234. (cons 'done org-done-keywords))
  11235. (member (plist-get change-plist :to)
  11236. (cons 'todo org-not-done-keywords))
  11237. (not (plist-get change-plist :to)))
  11238. (throw 'dont-block t))
  11239. ;; If this task has children, and any are undone, it's blocked
  11240. (save-excursion
  11241. (org-back-to-heading t)
  11242. (let ((this-level (funcall outline-level)))
  11243. (outline-next-heading)
  11244. (let ((child-level (funcall outline-level)))
  11245. (while (and (not (eobp))
  11246. (> child-level this-level))
  11247. ;; this todo has children, check whether they are all
  11248. ;; completed
  11249. (if (and (not (org-entry-is-done-p))
  11250. (org-entry-is-todo-p))
  11251. (progn (setq org-block-entry-blocking (org-get-heading))
  11252. (throw 'dont-block nil)))
  11253. (outline-next-heading)
  11254. (setq child-level (funcall outline-level))))))
  11255. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11256. ;; any previous siblings are undone, it's blocked
  11257. (save-excursion
  11258. (org-back-to-heading t)
  11259. (let* ((pos (point))
  11260. (parent-pos (and (org-up-heading-safe) (point))))
  11261. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11262. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11263. (forward-line 1)
  11264. (re-search-forward org-not-done-heading-regexp pos t))
  11265. (setq org-block-entry-blocking (match-string 0))
  11266. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11267. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11268. (while t
  11269. (goto-char parent-pos)
  11270. (if (not (looking-at org-not-done-heading-regexp))
  11271. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11272. (setq pos (point))
  11273. (setq parent-pos (and (org-up-heading-safe) (point)))
  11274. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11275. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11276. (forward-line 1)
  11277. (re-search-forward org-not-done-heading-regexp pos t)
  11278. (setq org-block-entry-blocking (org-get-heading)))
  11279. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11280. (defcustom org-track-ordered-property-with-tag nil
  11281. "Should the ORDERED property also be shown as a tag?
  11282. The ORDERED property decides if an entry should require subtasks to be
  11283. completed in sequence. Since a property is not very visible, setting
  11284. this option means that toggling the ORDERED property with the command
  11285. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11286. not relevant for the behavior, but it makes things more visible.
  11287. Note that toggling the tag with tags commands will not change the property
  11288. and therefore not influence behavior!
  11289. This can be t, meaning the tag ORDERED should be used, It can also be a
  11290. string to select a different tag for this task."
  11291. :group 'org-todo
  11292. :type '(choice
  11293. (const :tag "No tracking" nil)
  11294. (const :tag "Track with ORDERED tag" t)
  11295. (string :tag "Use other tag")))
  11296. (defun org-toggle-ordered-property ()
  11297. "Toggle the ORDERED property of the current entry.
  11298. For better visibility, you can track the value of this property with a tag.
  11299. See variable `org-track-ordered-property-with-tag'."
  11300. (interactive)
  11301. (let* ((t1 org-track-ordered-property-with-tag)
  11302. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11303. (save-excursion
  11304. (org-back-to-heading)
  11305. (if (org-entry-get nil "ORDERED")
  11306. (progn
  11307. (org-delete-property "ORDERED" "PROPERTIES")
  11308. (and tag (org-toggle-tag tag 'off))
  11309. (message "Subtasks can be completed in arbitrary order"))
  11310. (org-entry-put nil "ORDERED" "t")
  11311. (and tag (org-toggle-tag tag 'on))
  11312. (message "Subtasks must be completed in sequence")))))
  11313. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11314. (defun org-block-todo-from-checkboxes (change-plist)
  11315. "Block turning an entry into a TODO, using checkboxes.
  11316. This checks whether the current task should be blocked from state
  11317. changes because there are unchecked boxes in this entry."
  11318. (if (not org-enforce-todo-checkbox-dependencies)
  11319. t ; if locally turned off don't block
  11320. (catch 'dont-block
  11321. ;; If this is not a todo state change, or if this entry is already DONE,
  11322. ;; do not block
  11323. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11324. (member (plist-get change-plist :from)
  11325. (cons 'done org-done-keywords))
  11326. (member (plist-get change-plist :to)
  11327. (cons 'todo org-not-done-keywords))
  11328. (not (plist-get change-plist :to)))
  11329. (throw 'dont-block t))
  11330. ;; If this task has checkboxes that are not checked, it's blocked
  11331. (save-excursion
  11332. (org-back-to-heading t)
  11333. (let ((beg (point)) end)
  11334. (outline-next-heading)
  11335. (setq end (point))
  11336. (goto-char beg)
  11337. (if (org-list-search-forward
  11338. (concat (org-item-beginning-re)
  11339. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11340. "\\[[- ]\\]")
  11341. end t)
  11342. (progn
  11343. (if (boundp 'org-blocked-by-checkboxes)
  11344. (setq org-blocked-by-checkboxes t))
  11345. (throw 'dont-block nil)))))
  11346. t))) ; do not block
  11347. (defun org-entry-blocked-p ()
  11348. "Is the current entry blocked?"
  11349. (org-with-silent-modifications
  11350. (if (org-entry-get nil "NOBLOCKING")
  11351. nil ;; Never block this entry
  11352. (not (run-hook-with-args-until-failure
  11353. 'org-blocker-hook
  11354. (list :type 'todo-state-change
  11355. :position (point)
  11356. :from 'todo
  11357. :to 'done))))))
  11358. (defun org-update-statistics-cookies (all)
  11359. "Update the statistics cookie, either from TODO or from checkboxes.
  11360. This should be called with the cursor in a line with a statistics cookie."
  11361. (interactive "P")
  11362. (if all
  11363. (progn
  11364. (org-update-checkbox-count 'all)
  11365. (org-map-entries 'org-update-parent-todo-statistics))
  11366. (if (not (org-at-heading-p))
  11367. (org-update-checkbox-count)
  11368. (let ((pos (point-marker))
  11369. end l1 l2)
  11370. (ignore-errors (org-back-to-heading t))
  11371. (if (not (org-at-heading-p))
  11372. (org-update-checkbox-count)
  11373. (setq l1 (org-outline-level))
  11374. (setq end (save-excursion
  11375. (outline-next-heading)
  11376. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11377. (point)))
  11378. (if (and (save-excursion
  11379. (re-search-forward
  11380. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11381. (not (save-excursion (re-search-forward
  11382. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11383. (org-update-checkbox-count)
  11384. (if (and l2 (> l2 l1))
  11385. (progn
  11386. (goto-char end)
  11387. (org-update-parent-todo-statistics))
  11388. (goto-char pos)
  11389. (beginning-of-line 1)
  11390. (while (re-search-forward
  11391. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11392. (point-at-eol) t)
  11393. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11394. (goto-char pos)
  11395. (move-marker pos nil)))))
  11396. (defvar org-entry-property-inherited-from) ;; defined below
  11397. (defun org-update-parent-todo-statistics ()
  11398. "Update any statistics cookie in the parent of the current headline.
  11399. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11400. the entire subtree and this will travel up the hierarchy and update
  11401. statistics everywhere."
  11402. (let* ((prop (save-excursion (org-up-heading-safe)
  11403. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11404. (recursive (or (not org-hierarchical-todo-statistics)
  11405. (and prop (string-match "\\<recursive\\>" prop))))
  11406. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11407. 0))
  11408. (first t)
  11409. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11410. level ltoggle l1 new ndel
  11411. (cnt-all 0) (cnt-done 0) is-percent kwd
  11412. checkbox-beg ov ovs ove cookie-present)
  11413. (catch 'exit
  11414. (save-excursion
  11415. (beginning-of-line 1)
  11416. (setq ltoggle (funcall outline-level))
  11417. ;; Three situations are to consider:
  11418. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11419. ;; to the top-level ancestor on the headline;
  11420. ;; 2. If parent has "recursive" property, repeat up to the
  11421. ;; headline setting that property, taking inheritance into
  11422. ;; account;
  11423. ;; 3. Else, move up to direct parent and proceed only once.
  11424. (while (and (setq level (org-up-heading-safe))
  11425. (or recursive first)
  11426. (>= (point) lim))
  11427. (setq first nil cookie-present nil)
  11428. (unless (and level
  11429. (not (string-match
  11430. "\\<checkbox\\>"
  11431. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11432. "")))))
  11433. (throw 'exit nil))
  11434. (while (re-search-forward box-re (point-at-eol) t)
  11435. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11436. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11437. (save-match-data
  11438. (unless (outline-next-heading) (throw 'exit nil))
  11439. (while (and (looking-at org-complex-heading-regexp)
  11440. (> (setq l1 (length (match-string 1))) level))
  11441. (setq kwd (and (or recursive (= l1 ltoggle))
  11442. (match-string 2)))
  11443. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11444. (and (listp org-provide-todo-statistics)
  11445. (or (member kwd org-provide-todo-statistics)
  11446. (member kwd org-done-keywords))))
  11447. (setq cnt-all (1+ cnt-all))
  11448. (if (eq org-provide-todo-statistics t)
  11449. (and kwd (setq cnt-all (1+ cnt-all)))))
  11450. (and (member kwd org-done-keywords)
  11451. (setq cnt-done (1+ cnt-done)))
  11452. (outline-next-heading)))
  11453. (setq new
  11454. (if is-percent
  11455. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11456. (format "[%d/%d]" cnt-done cnt-all))
  11457. ndel (- (match-end 0) checkbox-beg))
  11458. ;; handle overlays when updating cookie from column view
  11459. (when (setq ov (car (overlays-at checkbox-beg)))
  11460. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11461. (delete-overlay ov))
  11462. (goto-char checkbox-beg)
  11463. (insert new)
  11464. (delete-region (point) (+ (point) ndel))
  11465. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11466. (when ov (move-overlay ov ovs ove)))
  11467. (when cookie-present
  11468. (run-hook-with-args 'org-after-todo-statistics-hook
  11469. cnt-done (- cnt-all cnt-done))))))
  11470. (run-hooks 'org-todo-statistics-hook)))
  11471. (defvar org-after-todo-statistics-hook nil
  11472. "Hook that is called after a TODO statistics cookie has been updated.
  11473. Each function is called with two arguments: the number of not-done entries
  11474. and the number of done entries.
  11475. For example, the following function, when added to this hook, will switch
  11476. an entry to DONE when all children are done, and back to TODO when new
  11477. entries are set to a TODO status. Note that this hook is only called
  11478. when there is a statistics cookie in the headline!
  11479. (defun org-summary-todo (n-done n-not-done)
  11480. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11481. (let (org-log-done org-log-states) ; turn off logging
  11482. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11483. ")
  11484. (defvar org-todo-statistics-hook nil
  11485. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11486. This hook runs even if there is no statistics cookie present, in which case
  11487. `org-after-todo-statistics-hook' would not run.")
  11488. (defun org-todo-trigger-tag-changes (state)
  11489. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11490. (let ((l org-todo-state-tags-triggers)
  11491. changes)
  11492. (when (or (not state) (equal state ""))
  11493. (setq changes (append changes (cdr (assoc "" l)))))
  11494. (when (and (stringp state) (> (length state) 0))
  11495. (setq changes (append changes (cdr (assoc state l)))))
  11496. (when (member state org-not-done-keywords)
  11497. (setq changes (append changes (cdr (assoc 'todo l)))))
  11498. (when (member state org-done-keywords)
  11499. (setq changes (append changes (cdr (assoc 'done l)))))
  11500. (dolist (c changes)
  11501. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11502. (defun org-local-logging (value)
  11503. "Get logging settings from a property VALUE."
  11504. (let* (words w a)
  11505. ;; directly set the variables, they are already local.
  11506. (setq org-log-done nil
  11507. org-log-repeat nil
  11508. org-todo-log-states nil)
  11509. (setq words (org-split-string value))
  11510. (while (setq w (pop words))
  11511. (cond
  11512. ((setq a (assoc w org-startup-options))
  11513. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11514. (set (nth 1 a) (nth 2 a))))
  11515. ((setq a (org-extract-log-state-settings w))
  11516. (and (member (car a) org-todo-keywords-1)
  11517. (push a org-todo-log-states)))))))
  11518. (defun org-get-todo-sequence-head (kwd)
  11519. "Return the head of the TODO sequence to which KWD belongs.
  11520. If KWD is not set, check if there is a text property remembering the
  11521. right sequence."
  11522. (let (p)
  11523. (cond
  11524. ((not kwd)
  11525. (or (get-text-property (point-at-bol) 'org-todo-head)
  11526. (progn
  11527. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11528. nil (point-at-eol)))
  11529. (get-text-property p 'org-todo-head))))
  11530. ((not (member kwd org-todo-keywords-1))
  11531. (car org-todo-keywords-1))
  11532. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11533. (defun org-fast-todo-selection ()
  11534. "Fast TODO keyword selection with single keys.
  11535. Returns the new TODO keyword, or nil if no state change should occur."
  11536. (let* ((fulltable org-todo-key-alist)
  11537. (done-keywords org-done-keywords) ;; needed for the faces.
  11538. (maxlen (apply 'max (mapcar
  11539. (lambda (x)
  11540. (if (stringp (car x)) (string-width (car x)) 0))
  11541. fulltable)))
  11542. (expert nil)
  11543. (fwidth (+ maxlen 3 1 3))
  11544. (ncol (/ (- (window-width) 4) fwidth))
  11545. tg cnt e c tbl
  11546. groups ingroup)
  11547. (save-excursion
  11548. (save-window-excursion
  11549. (if expert
  11550. (set-buffer (get-buffer-create " *Org todo*"))
  11551. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11552. (erase-buffer)
  11553. (org-set-local 'org-done-keywords done-keywords)
  11554. (setq tbl fulltable cnt 0)
  11555. (while (setq e (pop tbl))
  11556. (cond
  11557. ((equal e '(:startgroup))
  11558. (push '() groups) (setq ingroup t)
  11559. (when (not (= cnt 0))
  11560. (setq cnt 0)
  11561. (insert "\n"))
  11562. (insert "{ "))
  11563. ((equal e '(:endgroup))
  11564. (setq ingroup nil cnt 0)
  11565. (insert "}\n"))
  11566. ((equal e '(:newline))
  11567. (when (not (= cnt 0))
  11568. (setq cnt 0)
  11569. (insert "\n")
  11570. (setq e (car tbl))
  11571. (while (equal (car tbl) '(:newline))
  11572. (insert "\n")
  11573. (setq tbl (cdr tbl)))))
  11574. (t
  11575. (setq tg (car e) c (cdr e))
  11576. (if ingroup (push tg (car groups)))
  11577. (setq tg (org-add-props tg nil 'face
  11578. (org-get-todo-face tg)))
  11579. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11580. (insert "[" c "] " tg (make-string
  11581. (- fwidth 4 (length tg)) ?\ ))
  11582. (when (= (setq cnt (1+ cnt)) ncol)
  11583. (insert "\n")
  11584. (if ingroup (insert " "))
  11585. (setq cnt 0)))))
  11586. (insert "\n")
  11587. (goto-char (point-min))
  11588. (if (not expert) (org-fit-window-to-buffer))
  11589. (message "[a-z..]:Set [SPC]:clear")
  11590. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11591. (cond
  11592. ((or (= c ?\C-g)
  11593. (and (= c ?q) (not (rassoc c fulltable))))
  11594. (setq quit-flag t))
  11595. ((= c ?\ ) nil)
  11596. ((setq e (rassoc c fulltable) tg (car e))
  11597. tg)
  11598. (t (setq quit-flag t)))))))
  11599. (defun org-entry-is-todo-p ()
  11600. (member (org-get-todo-state) org-not-done-keywords))
  11601. (defun org-entry-is-done-p ()
  11602. (member (org-get-todo-state) org-done-keywords))
  11603. (defun org-get-todo-state ()
  11604. "Return the TODO keyword of the current subtree."
  11605. (save-excursion
  11606. (org-back-to-heading t)
  11607. (and (looking-at org-todo-line-regexp)
  11608. (match-end 2)
  11609. (match-string 2))))
  11610. (defun org-at-date-range-p (&optional inactive-ok)
  11611. "Is the cursor inside a date range?"
  11612. (interactive)
  11613. (save-excursion
  11614. (catch 'exit
  11615. (let ((pos (point)))
  11616. (skip-chars-backward "^[<\r\n")
  11617. (skip-chars-backward "<[")
  11618. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11619. (>= (match-end 0) pos)
  11620. (throw 'exit t))
  11621. (skip-chars-backward "^<[\r\n")
  11622. (skip-chars-backward "<[")
  11623. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11624. (>= (match-end 0) pos)
  11625. (throw 'exit t)))
  11626. nil)))
  11627. (defun org-get-repeat (&optional tagline)
  11628. "Check if there is a deadline/schedule with repeater in this entry."
  11629. (save-match-data
  11630. (save-excursion
  11631. (org-back-to-heading t)
  11632. (and (re-search-forward (if tagline
  11633. (concat tagline "\\s-*" org-repeat-re)
  11634. org-repeat-re)
  11635. (org-entry-end-position) t)
  11636. (match-string-no-properties 1)))))
  11637. (defvar org-last-changed-timestamp)
  11638. (defvar org-last-inserted-timestamp)
  11639. (defvar org-log-post-message)
  11640. (defvar org-log-note-purpose)
  11641. (defvar org-log-note-how)
  11642. (defvar org-log-note-extra)
  11643. (defun org-auto-repeat-maybe (done-word)
  11644. "Check if the current headline contains a repeated deadline/schedule.
  11645. If yes, set TODO state back to what it was and change the base date
  11646. of repeating deadline/scheduled time stamps to new date.
  11647. This function is run automatically after each state change to a DONE state."
  11648. ;; last-state is dynamically scoped into this function
  11649. (let* ((repeat (org-get-repeat))
  11650. (aa (assoc org-last-state org-todo-kwd-alist))
  11651. (interpret (nth 1 aa))
  11652. (head (nth 2 aa))
  11653. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11654. (msg "Entry repeats: ")
  11655. (org-log-done nil)
  11656. (org-todo-log-states nil)
  11657. re type n what ts time to-state)
  11658. (when repeat
  11659. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11660. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11661. org-todo-repeat-to-state))
  11662. (unless (and to-state (member to-state org-todo-keywords-1))
  11663. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11664. (org-todo to-state)
  11665. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11666. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11667. (org-time-stamp-format t t))))
  11668. (when org-log-repeat
  11669. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11670. (memq 'org-add-log-note post-command-hook))
  11671. ;; OK, we are already setup for some record
  11672. (if (eq org-log-repeat 'note)
  11673. ;; make sure we take a note, not only a time stamp
  11674. (setq org-log-note-how 'note))
  11675. ;; Set up for taking a record
  11676. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11677. org-last-state
  11678. 'findpos org-log-repeat)))
  11679. (org-back-to-heading t)
  11680. (org-add-planning-info nil nil 'closed)
  11681. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11682. org-deadline-time-regexp "\\)\\|\\("
  11683. org-ts-regexp "\\)"))
  11684. (while (re-search-forward
  11685. re (save-excursion (outline-next-heading) (point)) t)
  11686. (setq type (if (match-end 1) org-scheduled-string
  11687. (if (match-end 3) org-deadline-string "Plain:"))
  11688. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11689. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11690. (setq n (string-to-number (match-string 2 ts))
  11691. what (match-string 3 ts))
  11692. (if (equal what "w") (setq n (* n 7) what "d"))
  11693. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11694. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11695. ;; Preparation, see if we need to modify the start date for the change
  11696. (when (match-end 1)
  11697. (setq time (save-match-data (org-time-string-to-time ts)))
  11698. (cond
  11699. ((equal (match-string 1 ts) ".")
  11700. ;; Shift starting date to today
  11701. (org-timestamp-change
  11702. (- (org-today) (time-to-days time))
  11703. 'day))
  11704. ((equal (match-string 1 ts) "+")
  11705. (let ((nshiftmax 10) (nshift 0))
  11706. (while (or (= nshift 0)
  11707. (<= (time-to-days time)
  11708. (time-to-days (current-time))))
  11709. (when (= (incf nshift) nshiftmax)
  11710. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11711. (error "Abort")))
  11712. (org-timestamp-change n (cdr (assoc what whata)))
  11713. (org-at-timestamp-p t)
  11714. (setq ts (match-string 1))
  11715. (setq time (save-match-data (org-time-string-to-time ts)))))
  11716. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11717. ;; rematch, so that we have everything in place for the real shift
  11718. (org-at-timestamp-p t)
  11719. (setq ts (match-string 1))
  11720. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11721. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11722. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11723. (setq org-log-post-message msg)
  11724. (message "%s" msg))))
  11725. (defun org-show-todo-tree (arg)
  11726. "Make a compact tree which shows all headlines marked with TODO.
  11727. The tree will show the lines where the regexp matches, and all higher
  11728. headlines above the match.
  11729. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11730. With a numeric prefix N, construct a sparse tree for the Nth element
  11731. of `org-todo-keywords-1'."
  11732. (interactive "P")
  11733. (let ((case-fold-search nil)
  11734. (kwd-re
  11735. (cond ((null arg) org-not-done-regexp)
  11736. ((equal arg '(4))
  11737. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11738. (mapcar 'list org-todo-keywords-1))))
  11739. (concat "\\("
  11740. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11741. "\\)\\>")))
  11742. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11743. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11744. org-todo-keywords-1)))
  11745. (t (user-error "Invalid prefix argument: %s" arg)))))
  11746. (message "%d TODO entries found"
  11747. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11748. (defun org-deadline (arg &optional time)
  11749. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11750. With one universal prefix argument, remove any deadline from the item.
  11751. With two universal prefix arguments, prompt for a warning delay.
  11752. With argument TIME, set the deadline at the corresponding date. TIME
  11753. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11754. (interactive "P")
  11755. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11756. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11757. 'region-start-level 'region))
  11758. org-loop-over-headlines-in-active-region)
  11759. (org-map-entries
  11760. `(org-deadline ',arg ,time)
  11761. org-loop-over-headlines-in-active-region
  11762. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11763. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11764. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11765. (repeater (and old-date
  11766. (string-match
  11767. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11768. old-date)
  11769. (match-string 1 old-date))))
  11770. (cond
  11771. ((equal arg '(4))
  11772. (when (and old-date org-log-redeadline)
  11773. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11774. org-log-redeadline))
  11775. (org-remove-timestamp-with-keyword org-deadline-string)
  11776. (message "Item no longer has a deadline."))
  11777. ((equal arg '(16))
  11778. (save-excursion
  11779. (if (re-search-forward
  11780. org-deadline-time-regexp
  11781. (save-excursion (outline-next-heading) (point)) t)
  11782. (let* ((rpl0 (match-string 1))
  11783. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11784. (replace-match
  11785. (concat org-deadline-string
  11786. " <" rpl
  11787. (format " -%dd"
  11788. (abs
  11789. (- (time-to-days
  11790. (save-match-data
  11791. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11792. (time-to-days old-date-time))))
  11793. ">") t t))
  11794. (user-error "No deadline information to update"))))
  11795. (t
  11796. (org-add-planning-info 'deadline time 'closed)
  11797. (when (and old-date org-log-redeadline
  11798. (not (equal old-date
  11799. (substring org-last-inserted-timestamp 1 -1))))
  11800. (org-add-log-setup 'redeadline nil old-date 'findpos
  11801. org-log-redeadline))
  11802. (when repeater
  11803. (save-excursion
  11804. (org-back-to-heading t)
  11805. (when (re-search-forward (concat org-deadline-string " "
  11806. org-last-inserted-timestamp)
  11807. (save-excursion
  11808. (outline-next-heading) (point)) t)
  11809. (goto-char (1- (match-end 0)))
  11810. (insert " " repeater)
  11811. (setq org-last-inserted-timestamp
  11812. (concat (substring org-last-inserted-timestamp 0 -1)
  11813. " " repeater
  11814. (substring org-last-inserted-timestamp -1))))))
  11815. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11816. (defun org-schedule (arg &optional time)
  11817. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11818. With one universal prefix argument, remove any scheduling date from the item.
  11819. With two universal prefix arguments, prompt for a delay cookie.
  11820. With argument TIME, scheduled at the corresponding date. TIME can
  11821. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11822. (interactive "P")
  11823. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11824. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11825. 'region-start-level 'region))
  11826. org-loop-over-headlines-in-active-region)
  11827. (org-map-entries
  11828. `(org-schedule ',arg ,time)
  11829. org-loop-over-headlines-in-active-region
  11830. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11831. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11832. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11833. (repeater (and old-date
  11834. (string-match
  11835. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11836. old-date)
  11837. (match-string 1 old-date))))
  11838. (cond
  11839. ((equal arg '(4))
  11840. (progn
  11841. (when (and old-date org-log-reschedule)
  11842. (org-add-log-setup 'delschedule nil old-date 'findpos
  11843. org-log-reschedule))
  11844. (org-remove-timestamp-with-keyword org-scheduled-string)
  11845. (message "Item is no longer scheduled.")))
  11846. ((equal arg '(16))
  11847. (save-excursion
  11848. (if (re-search-forward
  11849. org-scheduled-time-regexp
  11850. (save-excursion (outline-next-heading) (point)) t)
  11851. (let* ((rpl0 (match-string 1))
  11852. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11853. (replace-match
  11854. (concat org-scheduled-string
  11855. " <" rpl
  11856. (format " -%dd"
  11857. (abs
  11858. (- (time-to-days
  11859. (save-match-data
  11860. (org-read-date nil t nil "Delay until" old-date-time)))
  11861. (time-to-days old-date-time))))
  11862. ">") t t))
  11863. (user-error "No scheduled information to update"))))
  11864. (t
  11865. (org-add-planning-info 'scheduled time 'closed)
  11866. (when (and old-date org-log-reschedule
  11867. (not (equal old-date
  11868. (substring org-last-inserted-timestamp 1 -1))))
  11869. (org-add-log-setup 'reschedule nil old-date 'findpos
  11870. org-log-reschedule))
  11871. (when repeater
  11872. (save-excursion
  11873. (org-back-to-heading t)
  11874. (when (re-search-forward (concat org-scheduled-string " "
  11875. org-last-inserted-timestamp)
  11876. (save-excursion
  11877. (outline-next-heading) (point)) t)
  11878. (goto-char (1- (match-end 0)))
  11879. (insert " " repeater)
  11880. (setq org-last-inserted-timestamp
  11881. (concat (substring org-last-inserted-timestamp 0 -1)
  11882. " " repeater
  11883. (substring org-last-inserted-timestamp -1))))))
  11884. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11885. (defun org-get-scheduled-time (pom &optional inherit)
  11886. "Get the scheduled time as a time tuple, of a format suitable
  11887. for calling org-schedule with, or if there is no scheduling,
  11888. returns nil."
  11889. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11890. (when time
  11891. (apply 'encode-time (org-parse-time-string time)))))
  11892. (defun org-get-deadline-time (pom &optional inherit)
  11893. "Get the deadline as a time tuple, of a format suitable for
  11894. calling org-deadline with, or if there is no scheduling, returns
  11895. nil."
  11896. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11897. (when time
  11898. (apply 'encode-time (org-parse-time-string time)))))
  11899. (defun org-remove-timestamp-with-keyword (keyword)
  11900. "Remove all time stamps with KEYWORD in the current entry."
  11901. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11902. beg)
  11903. (save-excursion
  11904. (org-back-to-heading t)
  11905. (setq beg (point))
  11906. (outline-next-heading)
  11907. (while (re-search-backward re beg t)
  11908. (replace-match "")
  11909. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11910. (equal (char-before) ?\ ))
  11911. (backward-delete-char 1)
  11912. (if (string-match "^[ \t]*$" (buffer-substring
  11913. (point-at-bol) (point-at-eol)))
  11914. (delete-region (point-at-bol)
  11915. (min (point-max) (1+ (point-at-eol))))))))))
  11916. (defun org-add-planning-info (what &optional time &rest remove)
  11917. "Insert new timestamp with keyword in the line directly after the headline.
  11918. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11919. If non is given, the user is prompted for a date.
  11920. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11921. be removed."
  11922. (interactive)
  11923. (let (org-time-was-given org-end-time-was-given ts
  11924. end default-time default-input)
  11925. (catch 'exit
  11926. (when (and (memq what '(scheduled deadline))
  11927. (or (not time)
  11928. (and (stringp time)
  11929. (string-match "^[-+]+[0-9]" time))))
  11930. ;; Try to get a default date/time from existing timestamp
  11931. (save-excursion
  11932. (org-back-to-heading t)
  11933. (setq end (save-excursion (outline-next-heading) (point)))
  11934. (when (re-search-forward (if (eq what 'scheduled)
  11935. org-scheduled-time-regexp
  11936. org-deadline-time-regexp)
  11937. end t)
  11938. (setq ts (match-string 1)
  11939. default-time
  11940. (apply 'encode-time (org-parse-time-string ts))
  11941. default-input (and ts (org-get-compact-tod ts))))))
  11942. (when what
  11943. (setq time
  11944. (if (stringp time)
  11945. ;; This is a string (relative or absolute), set proper date
  11946. (apply 'encode-time
  11947. (org-read-date-analyze
  11948. time default-time (decode-time default-time)))
  11949. ;; If necessary, get the time from the user
  11950. (or time (org-read-date nil 'to-time nil nil
  11951. default-time default-input)))))
  11952. (when (and org-insert-labeled-timestamps-at-point
  11953. (member what '(scheduled deadline)))
  11954. (insert
  11955. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11956. (org-insert-time-stamp time org-time-was-given
  11957. nil nil nil (list org-end-time-was-given))
  11958. (setq what nil))
  11959. (save-excursion
  11960. (save-restriction
  11961. (let (col list elt ts buffer-invisibility-spec)
  11962. (org-back-to-heading t)
  11963. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11964. (goto-char (match-end 1))
  11965. (setq col (current-column))
  11966. (goto-char (match-end 0))
  11967. (if (eobp) (insert "\n") (forward-char 1))
  11968. (when (and (not what)
  11969. (not (looking-at
  11970. (concat "[ \t]*"
  11971. org-keyword-time-not-clock-regexp))))
  11972. ;; Nothing to add, nothing to remove...... :-)
  11973. (throw 'exit nil))
  11974. (if (and (not (looking-at org-outline-regexp))
  11975. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11976. "[^\r\n]*"))
  11977. (not (equal (match-string 1) org-clock-string)))
  11978. (narrow-to-region (match-beginning 0) (match-end 0))
  11979. (insert-before-markers "\n")
  11980. (backward-char 1)
  11981. (narrow-to-region (point) (point))
  11982. (and org-adapt-indentation (org-indent-to-column col)))
  11983. ;; Check if we have to remove something.
  11984. (setq list (cons what remove))
  11985. (while list
  11986. (setq elt (pop list))
  11987. (when (or (and (eq elt 'scheduled)
  11988. (re-search-forward org-scheduled-time-regexp nil t))
  11989. (and (eq elt 'deadline)
  11990. (re-search-forward org-deadline-time-regexp nil t))
  11991. (and (eq elt 'closed)
  11992. (re-search-forward org-closed-time-regexp nil t)))
  11993. (replace-match "")
  11994. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11995. (and (looking-at "[ \t]+") (replace-match ""))
  11996. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11997. (when what
  11998. (insert
  11999. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  12000. (cond ((eq what 'scheduled) org-scheduled-string)
  12001. ((eq what 'deadline) org-deadline-string)
  12002. ((eq what 'closed) org-closed-string))
  12003. " ")
  12004. (setq ts (org-insert-time-stamp
  12005. time
  12006. (or org-time-was-given
  12007. (and (eq what 'closed) org-log-done-with-time))
  12008. (eq what 'closed)
  12009. nil nil (list org-end-time-was-given)))
  12010. (insert
  12011. (if (not (or (bolp) (eq (char-before) ?\ )
  12012. (memq (char-after) '(32 10))
  12013. (eobp))) " " ""))
  12014. (end-of-line 1))
  12015. (goto-char (point-min))
  12016. (widen)
  12017. (if (and (looking-at "[ \t]*\n")
  12018. (equal (char-before) ?\n))
  12019. (delete-region (1- (point)) (point-at-eol)))
  12020. ts))))))
  12021. (defvar org-log-note-marker (make-marker))
  12022. (defvar org-log-note-purpose nil)
  12023. (defvar org-log-note-state nil)
  12024. (defvar org-log-note-previous-state nil)
  12025. (defvar org-log-note-how nil)
  12026. (defvar org-log-note-extra nil)
  12027. (defvar org-log-note-window-configuration nil)
  12028. (defvar org-log-note-return-to (make-marker))
  12029. (defvar org-log-note-effective-time nil
  12030. "Remembered current time so that dynamically scoped
  12031. `org-extend-today-until' affects tha timestamps in state change
  12032. log")
  12033. (defvar org-log-post-message nil
  12034. "Message to be displayed after a log note has been stored.
  12035. The auto-repeater uses this.")
  12036. (defun org-add-note ()
  12037. "Add a note to the current entry.
  12038. This is done in the same way as adding a state change note."
  12039. (interactive)
  12040. (org-add-log-setup 'note nil nil 'findpos nil))
  12041. (defvar org-property-end-re)
  12042. (defun org-add-log-setup (&optional purpose state prev-state
  12043. findpos how extra)
  12044. "Set up the post command hook to take a note.
  12045. If this is about to TODO state change, the new state is expected in STATE.
  12046. When FINDPOS is non-nil, find the correct position for the note in
  12047. the current entry. If not, assume that it can be inserted at point.
  12048. HOW is an indicator what kind of note should be created.
  12049. EXTRA is additional text that will be inserted into the notes buffer."
  12050. (let* ((org-log-into-drawer (org-log-into-drawer))
  12051. (drawer (cond ((stringp org-log-into-drawer)
  12052. org-log-into-drawer)
  12053. (org-log-into-drawer "LOGBOOK"))))
  12054. (save-restriction
  12055. (save-excursion
  12056. (when findpos
  12057. (org-back-to-heading t)
  12058. (narrow-to-region (point) (save-excursion
  12059. (outline-next-heading) (point)))
  12060. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12061. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12062. "[^\r\n]*\\)?"))
  12063. (goto-char (match-end 0))
  12064. (cond
  12065. (drawer
  12066. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12067. nil t)
  12068. (progn
  12069. (goto-char (match-end 0))
  12070. (or org-log-states-order-reversed
  12071. (and (re-search-forward org-property-end-re nil t)
  12072. (goto-char (1- (match-beginning 0))))))
  12073. (insert "\n:" drawer ":\n:END:")
  12074. (beginning-of-line 0)
  12075. (org-indent-line)
  12076. (beginning-of-line 2)
  12077. (org-indent-line)
  12078. (end-of-line 0)))
  12079. ((and org-log-state-notes-insert-after-drawers
  12080. (save-excursion
  12081. (forward-line) (looking-at org-drawer-regexp)))
  12082. (forward-line)
  12083. (while (looking-at org-drawer-regexp)
  12084. (goto-char (match-end 0))
  12085. (re-search-forward org-property-end-re (point-max) t)
  12086. (forward-line))
  12087. (forward-line -1)))
  12088. (unless org-log-states-order-reversed
  12089. (and (= (char-after) ?\n) (forward-char 1))
  12090. (org-skip-over-state-notes)
  12091. (skip-chars-backward " \t\n\r")))
  12092. (move-marker org-log-note-marker (point))
  12093. (setq org-log-note-purpose purpose
  12094. org-log-note-state state
  12095. org-log-note-previous-state prev-state
  12096. org-log-note-how how
  12097. org-log-note-extra extra
  12098. org-log-note-effective-time (org-current-effective-time))
  12099. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12100. (defun org-skip-over-state-notes ()
  12101. "Skip past the list of State notes in an entry."
  12102. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12103. (when (ignore-errors (goto-char (org-in-item-p)))
  12104. (let* ((struct (org-list-struct))
  12105. (prevs (org-list-prevs-alist struct)))
  12106. (while (looking-at "[ \t]*- State")
  12107. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12108. (org-list-get-item-end (point) struct)))))))
  12109. (defun org-add-log-note (&optional purpose)
  12110. "Pop up a window for taking a note, and add this note later at point."
  12111. (remove-hook 'post-command-hook 'org-add-log-note)
  12112. (setq org-log-note-window-configuration (current-window-configuration))
  12113. (delete-other-windows)
  12114. (move-marker org-log-note-return-to (point))
  12115. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12116. (goto-char org-log-note-marker)
  12117. (org-switch-to-buffer-other-window "*Org Note*")
  12118. (erase-buffer)
  12119. (if (memq org-log-note-how '(time state))
  12120. (let (current-prefix-arg) (org-store-log-note))
  12121. (let ((org-inhibit-startup t)) (org-mode))
  12122. (insert (format "# Insert note for %s.
  12123. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12124. (cond
  12125. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12126. ((eq org-log-note-purpose 'done) "closed todo item")
  12127. ((eq org-log-note-purpose 'state)
  12128. (format "state change from \"%s\" to \"%s\""
  12129. (or org-log-note-previous-state "")
  12130. (or org-log-note-state "")))
  12131. ((eq org-log-note-purpose 'reschedule)
  12132. "rescheduling")
  12133. ((eq org-log-note-purpose 'delschedule)
  12134. "no longer scheduled")
  12135. ((eq org-log-note-purpose 'redeadline)
  12136. "changing deadline")
  12137. ((eq org-log-note-purpose 'deldeadline)
  12138. "removing deadline")
  12139. ((eq org-log-note-purpose 'refile)
  12140. "refiling")
  12141. ((eq org-log-note-purpose 'note)
  12142. "this entry")
  12143. (t (error "This should not happen")))))
  12144. (if org-log-note-extra (insert org-log-note-extra))
  12145. (org-set-local 'org-finish-function 'org-store-log-note)
  12146. (run-hooks 'org-log-buffer-setup-hook)))
  12147. (defvar org-note-abort nil) ; dynamically scoped
  12148. (defun org-store-log-note ()
  12149. "Finish taking a log note, and insert it to where it belongs."
  12150. (let ((txt (buffer-string)))
  12151. (kill-buffer (current-buffer))
  12152. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12153. lines ind bul)
  12154. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12155. (setq txt (replace-match "" t t txt)))
  12156. (if (string-match "\\s-+\\'" txt)
  12157. (setq txt (replace-match "" t t txt)))
  12158. (setq lines (org-split-string txt "\n"))
  12159. (when (and note (string-match "\\S-" note))
  12160. (setq note
  12161. (org-replace-escapes
  12162. note
  12163. (list (cons "%u" (user-login-name))
  12164. (cons "%U" user-full-name)
  12165. (cons "%t" (format-time-string
  12166. (org-time-stamp-format 'long 'inactive)
  12167. org-log-note-effective-time))
  12168. (cons "%T" (format-time-string
  12169. (org-time-stamp-format 'long nil)
  12170. org-log-note-effective-time))
  12171. (cons "%d" (format-time-string
  12172. (org-time-stamp-format nil 'inactive)
  12173. org-log-note-effective-time))
  12174. (cons "%D" (format-time-string
  12175. (org-time-stamp-format nil nil)
  12176. org-log-note-effective-time))
  12177. (cons "%s" (if org-log-note-state
  12178. (concat "\"" org-log-note-state "\"")
  12179. ""))
  12180. (cons "%S" (if org-log-note-previous-state
  12181. (concat "\"" org-log-note-previous-state "\"")
  12182. "\"\"")))))
  12183. (if lines (setq note (concat note " \\\\")))
  12184. (push note lines))
  12185. (when (or current-prefix-arg org-note-abort)
  12186. (when org-log-into-drawer
  12187. (org-remove-empty-drawer-at
  12188. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12189. org-log-note-marker))
  12190. (setq lines nil))
  12191. (when lines
  12192. (with-current-buffer (marker-buffer org-log-note-marker)
  12193. (save-excursion
  12194. (goto-char org-log-note-marker)
  12195. (move-marker org-log-note-marker nil)
  12196. (end-of-line 1)
  12197. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12198. (setq ind (save-excursion
  12199. (if (ignore-errors (goto-char (org-in-item-p)))
  12200. (let ((struct (org-list-struct)))
  12201. (org-list-get-ind
  12202. (org-list-get-top-point struct) struct))
  12203. (skip-chars-backward " \r\t\n")
  12204. (cond
  12205. ((and (org-at-heading-p)
  12206. org-adapt-indentation)
  12207. (1+ (org-current-level)))
  12208. ((org-at-heading-p) 0)
  12209. (t (org-get-indentation))))))
  12210. (setq bul (org-list-bullet-string "-"))
  12211. (org-indent-line-to ind)
  12212. (insert bul (pop lines))
  12213. (let ((ind-body (+ (length bul) ind)))
  12214. (while lines
  12215. (insert "\n")
  12216. (org-indent-line-to ind-body)
  12217. (insert (pop lines))))
  12218. (message "Note stored")
  12219. (org-back-to-heading t)
  12220. (org-cycle-hide-drawers 'children))
  12221. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12222. ;; from within `org-add-log-note' because `buffer-undo-list'
  12223. ;; is then modified outside of `org-with-remote-undo'.
  12224. (when (eq this-command 'org-agenda-todo)
  12225. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12226. ;; Don't add undo information when called from `org-agenda-todo'
  12227. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12228. (set-window-configuration org-log-note-window-configuration)
  12229. (with-current-buffer (marker-buffer org-log-note-return-to)
  12230. (goto-char org-log-note-return-to))
  12231. (move-marker org-log-note-return-to nil)
  12232. (and org-log-post-message (message "%s" org-log-post-message))))
  12233. (defun org-remove-empty-drawer-at (drawer pos)
  12234. "Remove an empty drawer DRAWER at position POS.
  12235. POS may also be a marker."
  12236. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12237. (save-excursion
  12238. (save-restriction
  12239. (widen)
  12240. (goto-char pos)
  12241. (if (org-in-regexp
  12242. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12243. (replace-match ""))))))
  12244. (defvar org-ts-type nil)
  12245. (defun org-sparse-tree (&optional arg type)
  12246. "Create a sparse tree, prompt for the details.
  12247. This command can create sparse trees. You first need to select the type
  12248. of match used to create the tree:
  12249. t Show all TODO entries.
  12250. T Show entries with a specific TODO keyword.
  12251. m Show entries selected by a tags/property match.
  12252. p Enter a property name and its value (both with completion on existing
  12253. names/values) and show entries with that property.
  12254. r Show entries matching a regular expression (`/' can be used as well).
  12255. b Show deadlines and scheduled items before a date.
  12256. a Show deadlines and scheduled items after a date.
  12257. d Show deadlines due within `org-deadline-warning-days'.
  12258. D Show deadlines and scheduled items between a date range."
  12259. (interactive "P")
  12260. (let (ans kwd value ts-type)
  12261. (setq type (or type org-sparse-tree-default-date-type))
  12262. (setq org-ts-type type)
  12263. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  12264. (cond ((eq type 'all) "all timestamps")
  12265. ((eq type 'scheduled) "only scheduled")
  12266. ((eq type 'deadline) "only deadline")
  12267. ((eq type 'active) "only active timestamps")
  12268. ((eq type 'inactive) "only inactive timestamps")
  12269. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12270. ((eq type 'closed) "with a closed time-stamp")
  12271. (t "scheduled/deadline")))
  12272. (setq ans (read-char-exclusive))
  12273. (cond
  12274. ((equal ans ?c)
  12275. (org-sparse-tree
  12276. arg (cadr (member type '(scheduled-or-deadline
  12277. all scheduled deadline active inactive closed)))))
  12278. ((equal ans ?d)
  12279. (call-interactively 'org-check-deadlines))
  12280. ((equal ans ?b)
  12281. (call-interactively 'org-check-before-date))
  12282. ((equal ans ?a)
  12283. (call-interactively 'org-check-after-date))
  12284. ((equal ans ?D)
  12285. (call-interactively 'org-check-dates-range))
  12286. ((equal ans ?t)
  12287. (call-interactively 'org-show-todo-tree))
  12288. ((equal ans ?T)
  12289. (org-show-todo-tree '(4)))
  12290. ((member ans '(?T ?m))
  12291. (call-interactively 'org-match-sparse-tree))
  12292. ((member ans '(?p ?P))
  12293. (setq kwd (org-icompleting-read "Property: "
  12294. (mapcar 'list (org-buffer-property-keys))))
  12295. (setq value (org-icompleting-read "Value: "
  12296. (mapcar 'list (org-property-values kwd))))
  12297. (unless (string-match "\\`{.*}\\'" value)
  12298. (setq value (concat "\"" value "\"")))
  12299. (org-match-sparse-tree arg (concat kwd "=" value)))
  12300. ((member ans '(?r ?R ?/))
  12301. (call-interactively 'org-occur))
  12302. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12303. (defvar org-occur-highlights nil
  12304. "List of overlays used for occur matches.")
  12305. (make-variable-buffer-local 'org-occur-highlights)
  12306. (defvar org-occur-parameters nil
  12307. "Parameters of the active org-occur calls.
  12308. This is a list, each call to org-occur pushes as cons cell,
  12309. containing the regular expression and the callback, onto the list.
  12310. The list can contain several entries if `org-occur' has been called
  12311. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12312. will only contain one set of parameters. When the highlights are
  12313. removed (for example with `C-c C-c', or with the next edit (depending
  12314. on `org-remove-highlights-with-change'), this variable is emptied
  12315. as well.")
  12316. (make-variable-buffer-local 'org-occur-parameters)
  12317. (defun org-occur (regexp &optional keep-previous callback)
  12318. "Make a compact tree which shows all matches of REGEXP.
  12319. The tree will show the lines where the regexp matches, and all higher
  12320. headlines above the match. It will also show the heading after the match,
  12321. to make sure editing the matching entry is easy.
  12322. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12323. call to `org-occur' will be kept, to allow stacking of calls to this
  12324. command.
  12325. If CALLBACK is non-nil, it is a function which is called to confirm
  12326. that the match should indeed be shown."
  12327. (interactive "sRegexp: \nP")
  12328. (when (equal regexp "")
  12329. (user-error "Regexp cannot be empty"))
  12330. (unless keep-previous
  12331. (org-remove-occur-highlights nil nil t))
  12332. (push (cons regexp callback) org-occur-parameters)
  12333. (let ((cnt 0))
  12334. (save-excursion
  12335. (goto-char (point-min))
  12336. (if (or (not keep-previous) ; do not want to keep
  12337. (not org-occur-highlights)) ; no previous matches
  12338. ;; hide everything
  12339. (org-overview))
  12340. (while (re-search-forward regexp nil t)
  12341. (when (or (not callback)
  12342. (save-match-data (funcall callback)))
  12343. (setq cnt (1+ cnt))
  12344. (when org-highlight-sparse-tree-matches
  12345. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12346. (org-show-context 'occur-tree))))
  12347. (when org-remove-highlights-with-change
  12348. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12349. nil 'local))
  12350. (unless org-sparse-tree-open-archived-trees
  12351. (org-hide-archived-subtrees (point-min) (point-max)))
  12352. (run-hooks 'org-occur-hook)
  12353. (if (org-called-interactively-p 'interactive)
  12354. (message "%d match(es) for regexp %s" cnt regexp))
  12355. cnt))
  12356. (defun org-occur-next-match (&optional n reset)
  12357. "Function for `next-error-function' to find sparse tree matches.
  12358. N is the number of matches to move, when negative move backwards.
  12359. RESET is entirely ignored - this function always goes back to the
  12360. starting point when no match is found."
  12361. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12362. (search-func (if (< n 0)
  12363. 'previous-single-char-property-change
  12364. 'next-single-char-property-change))
  12365. (n (abs n))
  12366. (pos (point))
  12367. p1)
  12368. (catch 'exit
  12369. (while (setq p1 (funcall search-func (point) 'org-type))
  12370. (when (equal p1 limit)
  12371. (goto-char pos)
  12372. (error "No more matches"))
  12373. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12374. (setq n (1- n))
  12375. (when (= n 0)
  12376. (goto-char p1)
  12377. (throw 'exit (point))))
  12378. (goto-char p1))
  12379. (goto-char p1)
  12380. (error "No more matches"))))
  12381. (defun org-show-context (&optional key)
  12382. "Make sure point and context are visible.
  12383. How much context is shown depends upon the variables
  12384. `org-show-hierarchy-above', `org-show-following-heading',
  12385. `org-show-entry-below' and `org-show-siblings'."
  12386. (let ((heading-p (org-at-heading-p t))
  12387. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12388. (following-p (org-get-alist-option org-show-following-heading key))
  12389. (entry-p (org-get-alist-option org-show-entry-below key))
  12390. (siblings-p (org-get-alist-option org-show-siblings key)))
  12391. ;; Show heading or entry text
  12392. (if (and heading-p (not entry-p))
  12393. (org-flag-heading nil) ; only show the heading
  12394. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12395. (org-show-hidden-entry))) ; show entire entry
  12396. (when following-p
  12397. ;; Show next sibling, or heading below text
  12398. (save-excursion
  12399. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12400. (org-flag-heading nil))))
  12401. (when siblings-p (org-show-siblings))
  12402. (when hierarchy-p
  12403. ;; show all higher headings, possibly with siblings
  12404. (save-excursion
  12405. (while (and (condition-case nil
  12406. (progn (org-up-heading-all 1) t)
  12407. (error nil))
  12408. (not (bobp)))
  12409. (org-flag-heading nil)
  12410. (when siblings-p (org-show-siblings)))))
  12411. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12412. (defvar org-reveal-start-hook nil
  12413. "Hook run before revealing a location.")
  12414. (defun org-reveal (&optional siblings)
  12415. "Show current entry, hierarchy above it, and the following headline.
  12416. This can be used to show a consistent set of context around locations
  12417. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12418. not t for the search context.
  12419. With optional argument SIBLINGS, on each level of the hierarchy all
  12420. siblings are shown. This repairs the tree structure to what it would
  12421. look like when opened with hierarchical calls to `org-cycle'.
  12422. With double optional argument \\[universal-argument] \\[universal-argument], \
  12423. go to the parent and show the
  12424. entire tree."
  12425. (interactive "P")
  12426. (run-hooks 'org-reveal-start-hook)
  12427. (let ((org-show-hierarchy-above t)
  12428. (org-show-following-heading t)
  12429. (org-show-siblings (if siblings t org-show-siblings)))
  12430. (org-show-context nil))
  12431. (when (equal siblings '(16))
  12432. (save-excursion
  12433. (when (org-up-heading-safe)
  12434. (org-show-subtree)
  12435. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12436. (defun org-highlight-new-match (beg end)
  12437. "Highlight from BEG to END and mark the highlight is an occur headline."
  12438. (let ((ov (make-overlay beg end)))
  12439. (overlay-put ov 'face 'secondary-selection)
  12440. (overlay-put ov 'org-type 'org-occur)
  12441. (push ov org-occur-highlights)))
  12442. (defun org-remove-occur-highlights (&optional beg end noremove)
  12443. "Remove the occur highlights from the buffer.
  12444. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12445. from the `before-change-functions' in the current buffer."
  12446. (interactive)
  12447. (unless org-inhibit-highlight-removal
  12448. (mapc 'delete-overlay org-occur-highlights)
  12449. (setq org-occur-highlights nil)
  12450. (setq org-occur-parameters nil)
  12451. (unless noremove
  12452. (remove-hook 'before-change-functions
  12453. 'org-remove-occur-highlights 'local))))
  12454. ;;;; Priorities
  12455. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12456. "Regular expression matching the priority indicator.")
  12457. (defvar org-remove-priority-next-time nil)
  12458. (defun org-priority-up ()
  12459. "Increase the priority of the current item."
  12460. (interactive)
  12461. (org-priority 'up))
  12462. (defun org-priority-down ()
  12463. "Decrease the priority of the current item."
  12464. (interactive)
  12465. (org-priority 'down))
  12466. (defun org-priority (&optional action show)
  12467. "Change the priority of an item.
  12468. ACTION can be `set', `up', `down', or a character."
  12469. (interactive "P")
  12470. (if (equal action '(4))
  12471. (org-show-priority)
  12472. (unless org-enable-priority-commands
  12473. (user-error "Priority commands are disabled"))
  12474. (setq action (or action 'set))
  12475. (let (current new news have remove)
  12476. (save-excursion
  12477. (org-back-to-heading t)
  12478. (if (looking-at org-priority-regexp)
  12479. (setq current (string-to-char (match-string 2))
  12480. have t))
  12481. (cond
  12482. ((eq action 'remove)
  12483. (setq remove t new ?\ ))
  12484. ((or (eq action 'set)
  12485. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12486. (if (not (eq action 'set))
  12487. (setq new action)
  12488. (message "Priority %c-%c, SPC to remove: "
  12489. org-highest-priority org-lowest-priority)
  12490. (save-match-data
  12491. (setq new (read-char-exclusive))))
  12492. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12493. (= (upcase org-lowest-priority) org-lowest-priority))
  12494. (setq new (upcase new)))
  12495. (cond ((equal new ?\ ) (setq remove t))
  12496. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12497. (user-error "Priority must be between `%c' and `%c'"
  12498. org-highest-priority org-lowest-priority))))
  12499. ((eq action 'up)
  12500. (setq new (if have
  12501. (1- current) ; normal cycling
  12502. ;; last priority was empty
  12503. (if (eq last-command this-command)
  12504. org-lowest-priority ; wrap around empty to lowest
  12505. ;; default
  12506. (if org-priority-start-cycle-with-default
  12507. org-default-priority
  12508. (1- org-default-priority))))))
  12509. ((eq action 'down)
  12510. (setq new (if have
  12511. (1+ current) ; normal cycling
  12512. ;; last priority was empty
  12513. (if (eq last-command this-command)
  12514. org-highest-priority ; wrap around empty to highest
  12515. ;; default
  12516. (if org-priority-start-cycle-with-default
  12517. org-default-priority
  12518. (1+ org-default-priority))))))
  12519. (t (user-error "Invalid action")))
  12520. (if (or (< (upcase new) org-highest-priority)
  12521. (> (upcase new) org-lowest-priority))
  12522. (if (and (memq action '(up down))
  12523. (not have) (not (eq last-command this-command)))
  12524. ;; `new' is from default priority
  12525. (error
  12526. "The default can not be set, see `org-default-priority' why")
  12527. ;; normal cycling: `new' is beyond highest/lowest priority
  12528. ;; and is wrapped around to the empty priority
  12529. (setq remove t)))
  12530. (setq news (format "%c" new))
  12531. (if have
  12532. (if remove
  12533. (replace-match "" t t nil 1)
  12534. (replace-match news t t nil 2))
  12535. (if remove
  12536. (user-error "No priority cookie found in line")
  12537. (let ((case-fold-search nil))
  12538. (looking-at org-todo-line-regexp))
  12539. (if (match-end 2)
  12540. (progn
  12541. (goto-char (match-end 2))
  12542. (insert " [#" news "]"))
  12543. (goto-char (match-beginning 3))
  12544. (insert "[#" news "] "))))
  12545. (org-preserve-lc (org-set-tags nil 'align)))
  12546. (if remove
  12547. (message "Priority removed")
  12548. (message "Priority of current item set to %s" news)))))
  12549. (defun org-show-priority ()
  12550. "Show the priority of the current item.
  12551. This priority is composed of the main priority given with the [#A] cookies,
  12552. and by additional input from the age of a schedules or deadline entry."
  12553. (interactive)
  12554. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12555. (org-get-at-bol 'priority)
  12556. (save-excursion
  12557. (save-match-data
  12558. (beginning-of-line)
  12559. (and (looking-at org-heading-regexp)
  12560. (org-get-priority (match-string 0))))))))
  12561. (message "Priority is %d" (if pri pri -1000))))
  12562. (defun org-get-priority (s)
  12563. "Find priority cookie and return priority."
  12564. (save-match-data
  12565. (if (functionp org-get-priority-function)
  12566. (funcall org-get-priority-function)
  12567. (if (not (string-match org-priority-regexp s))
  12568. (* 1000 (- org-lowest-priority org-default-priority))
  12569. (* 1000 (- org-lowest-priority
  12570. (string-to-char (match-string 2 s))))))))
  12571. ;;;; Tags
  12572. (defvar org-agenda-archives-mode)
  12573. (defvar org-map-continue-from nil
  12574. "Position from where mapping should continue.
  12575. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12576. (defvar org-scanner-tags nil
  12577. "The current tag list while the tags scanner is running.")
  12578. (defvar org-trust-scanner-tags nil
  12579. "Should `org-get-tags-at' use the tags for the scanner.
  12580. This is for internal dynamical scoping only.
  12581. When this is non-nil, the function `org-get-tags-at' will return the value
  12582. of `org-scanner-tags' instead of building the list by itself. This
  12583. can lead to large speed-ups when the tags scanner is used in a file with
  12584. many entries, and when the list of tags is retrieved, for example to
  12585. obtain a list of properties. Building the tags list for each entry in such
  12586. a file becomes an N^2 operation - but with this variable set, it scales
  12587. as N.")
  12588. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12589. "Sca headline tags with inheritance and produce output ACTION.
  12590. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12591. or `agenda' to produce an entry list for an agenda view. It can also be
  12592. a Lisp form or a function that should be called at each matched headline, in
  12593. this case the return value is a list of all return values from these calls.
  12594. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12595. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12596. only lines with a not-done TODO keyword are included in the output.
  12597. This should be the same variable that was scoped into
  12598. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12599. START-LEVEL can be a string with asterisks, reducing the scope to
  12600. headlines matching this string."
  12601. (require 'org-agenda)
  12602. (let* ((re (concat "^"
  12603. (if start-level
  12604. ;; Get the correct level to match
  12605. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12606. org-outline-regexp)
  12607. " *\\(\\<\\("
  12608. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12609. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12610. (props (list 'face 'default
  12611. 'done-face 'org-agenda-done
  12612. 'undone-face 'default
  12613. 'mouse-face 'highlight
  12614. 'org-not-done-regexp org-not-done-regexp
  12615. 'org-todo-regexp org-todo-regexp
  12616. 'org-complex-heading-regexp org-complex-heading-regexp
  12617. 'help-echo
  12618. (format "mouse-2 or RET jump to org file %s"
  12619. (abbreviate-file-name
  12620. (or (buffer-file-name (buffer-base-buffer))
  12621. (buffer-name (buffer-base-buffer)))))))
  12622. (org-map-continue-from nil)
  12623. lspos tags tags-list
  12624. (tags-alist (list (cons 0 org-file-tags)))
  12625. (llast 0) rtn rtn1 level category i txt
  12626. todo marker entry priority)
  12627. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12628. (setq action (list 'lambda nil action)))
  12629. (save-excursion
  12630. (goto-char (point-min))
  12631. (when (eq action 'sparse-tree)
  12632. (org-overview)
  12633. (org-remove-occur-highlights))
  12634. (while (let (case-fold-search)
  12635. (re-search-forward re nil t))
  12636. (setq org-map-continue-from nil)
  12637. (catch :skip
  12638. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12639. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12640. (goto-char (setq lspos (match-beginning 0)))
  12641. (setq level (org-reduced-level (org-outline-level))
  12642. category (org-get-category))
  12643. (setq i llast llast level)
  12644. ;; remove tag lists from same and sublevels
  12645. (while (>= i level)
  12646. (when (setq entry (assoc i tags-alist))
  12647. (setq tags-alist (delete entry tags-alist)))
  12648. (setq i (1- i)))
  12649. ;; add the next tags
  12650. (when tags
  12651. (setq tags (org-split-string tags ":")
  12652. tags-alist
  12653. (cons (cons level tags) tags-alist)))
  12654. ;; compile tags for current headline
  12655. (setq tags-list
  12656. (if org-use-tag-inheritance
  12657. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12658. tags)
  12659. org-scanner-tags tags-list)
  12660. (when org-use-tag-inheritance
  12661. (setcdr (car tags-alist)
  12662. (mapcar (lambda (x)
  12663. (setq x (copy-sequence x))
  12664. (org-add-prop-inherited x))
  12665. (cdar tags-alist))))
  12666. (when (and tags org-use-tag-inheritance
  12667. (or (not (eq t org-use-tag-inheritance))
  12668. org-tags-exclude-from-inheritance))
  12669. ;; selective inheritance, remove uninherited ones
  12670. (setcdr (car tags-alist)
  12671. (org-remove-uninherited-tags (cdar tags-alist))))
  12672. (when (and
  12673. ;; eval matcher only when the todo condition is OK
  12674. (and (or (not todo-only) (member todo org-not-done-keywords))
  12675. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12676. (eval matcher)))
  12677. ;; Call the skipper, but return t if it does not skip,
  12678. ;; so that the `and' form continues evaluating
  12679. (progn
  12680. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12681. t)
  12682. ;; Check if timestamps are deselecting this entry
  12683. (or (not todo-only)
  12684. (and (member todo org-not-done-keywords)
  12685. (or (not org-agenda-tags-todo-honor-ignore-options)
  12686. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12687. ;; select this headline
  12688. (cond
  12689. ((eq action 'sparse-tree)
  12690. (and org-highlight-sparse-tree-matches
  12691. (org-get-heading) (match-end 0)
  12692. (org-highlight-new-match
  12693. (match-beginning 1) (match-end 1)))
  12694. (org-show-context 'tags-tree))
  12695. ((eq action 'agenda)
  12696. (setq txt (org-agenda-format-item
  12697. ""
  12698. (concat
  12699. (if (eq org-tags-match-list-sublevels 'indented)
  12700. (make-string (1- level) ?.) "")
  12701. (org-get-heading))
  12702. level category
  12703. tags-list)
  12704. priority (org-get-priority txt))
  12705. (goto-char lspos)
  12706. (setq marker (org-agenda-new-marker))
  12707. (org-add-props txt props
  12708. 'org-marker marker 'org-hd-marker marker 'org-category category
  12709. 'todo-state todo
  12710. 'priority priority 'type "tagsmatch")
  12711. (push txt rtn))
  12712. ((functionp action)
  12713. (setq org-map-continue-from nil)
  12714. (save-excursion
  12715. (setq rtn1 (funcall action))
  12716. (push rtn1 rtn)))
  12717. (t (user-error "Invalid action")))
  12718. ;; if we are to skip sublevels, jump to end of subtree
  12719. (unless org-tags-match-list-sublevels
  12720. (org-end-of-subtree t)
  12721. (backward-char 1))))
  12722. ;; Get the correct position from where to continue
  12723. (if org-map-continue-from
  12724. (goto-char org-map-continue-from)
  12725. (and (= (point) lspos) (end-of-line 1)))))
  12726. (when (and (eq action 'sparse-tree)
  12727. (not org-sparse-tree-open-archived-trees))
  12728. (org-hide-archived-subtrees (point-min) (point-max)))
  12729. (nreverse rtn)))
  12730. (defun org-remove-uninherited-tags (tags)
  12731. "Remove all tags that are not inherited from the list TAGS."
  12732. (cond
  12733. ((eq org-use-tag-inheritance t)
  12734. (if org-tags-exclude-from-inheritance
  12735. (org-delete-all org-tags-exclude-from-inheritance tags)
  12736. tags))
  12737. ((not org-use-tag-inheritance) nil)
  12738. ((stringp org-use-tag-inheritance)
  12739. (delq nil (mapcar
  12740. (lambda (x)
  12741. (if (and (string-match org-use-tag-inheritance x)
  12742. (not (member x org-tags-exclude-from-inheritance)))
  12743. x nil))
  12744. tags)))
  12745. ((listp org-use-tag-inheritance)
  12746. (delq nil (mapcar
  12747. (lambda (x)
  12748. (if (member x org-use-tag-inheritance) x nil))
  12749. tags)))))
  12750. (defun org-match-sparse-tree (&optional todo-only match)
  12751. "Create a sparse tree according to tags string MATCH.
  12752. MATCH can contain positive and negative selection of tags, like
  12753. \"+WORK+URGENT-WITHBOSS\".
  12754. If optional argument TODO-ONLY is non-nil, only select lines that are
  12755. also TODO lines."
  12756. (interactive "P")
  12757. (org-agenda-prepare-buffers (list (current-buffer)))
  12758. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12759. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12760. (defvar org-cached-props nil)
  12761. (defun org-cached-entry-get (pom property)
  12762. (if (or (eq t org-use-property-inheritance)
  12763. (and (stringp org-use-property-inheritance)
  12764. (string-match org-use-property-inheritance property))
  12765. (and (listp org-use-property-inheritance)
  12766. (member property org-use-property-inheritance)))
  12767. ;; Caching is not possible, check it directly
  12768. (org-entry-get pom property 'inherit)
  12769. ;; Get all properties, so that we can do complicated checks easily
  12770. (cdr (assoc property (or org-cached-props
  12771. (setq org-cached-props
  12772. (org-entry-properties pom)))))))
  12773. (defun org-global-tags-completion-table (&optional files)
  12774. "Return the list of all tags in all agenda buffer/files.
  12775. Optional FILES argument is a list of files which can be used
  12776. instead of the agenda files."
  12777. (save-excursion
  12778. (org-uniquify
  12779. (delq nil
  12780. (apply 'append
  12781. (mapcar
  12782. (lambda (file)
  12783. (set-buffer (find-file-noselect file))
  12784. (append (org-get-buffer-tags)
  12785. (mapcar (lambda (x) (if (stringp (car-safe x))
  12786. (list (car-safe x)) nil))
  12787. org-tag-alist)))
  12788. (if (and files (car files))
  12789. files
  12790. (org-agenda-files))))))))
  12791. (defun org-make-tags-matcher (match)
  12792. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12793. The variable `todo-only' is scoped dynamically into this function.
  12794. It will be set to t if the matcher restricts matching to TODO entries,
  12795. otherwise will not be touched.
  12796. Returns a cons of the selection string MATCH and the constructed
  12797. lisp form implementing the matcher. The matcher is to be evaluated
  12798. at an Org entry, with point on the headline, and returns t if the
  12799. entry matches the selection string MATCH. The returned lisp form
  12800. references two variables with information about the entry, which
  12801. must be bound around the form's evaluation: todo, the TODO keyword
  12802. at the entry (or nil of none); and tags-list, the list of all tags
  12803. at the entry including inherited ones. Additionally, the category
  12804. of the entry (if any) must be specified as the text property
  12805. 'org-category on the headline.
  12806. See also `org-scan-tags'.
  12807. "
  12808. (declare (special todo-only))
  12809. (unless (boundp 'todo-only)
  12810. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12811. (unless match
  12812. ;; Get a new match request, with completion against the global
  12813. ;; tags table and the local tags in current buffer
  12814. (let ((org-last-tags-completion-table
  12815. (org-uniquify
  12816. (delq nil (append (org-get-buffer-tags)
  12817. (org-global-tags-completion-table))))))
  12818. (setq match (org-completing-read-no-i
  12819. "Match: " 'org-tags-completion-function nil nil nil
  12820. 'org-tags-history))))
  12821. ;; Parse the string and create a lisp form
  12822. (let ((match0 match)
  12823. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12824. minus tag mm
  12825. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12826. orterms term orlist re-p str-p level-p level-op time-p
  12827. prop-p pn pv po gv rest)
  12828. ;; Expand group tags
  12829. (setq match (org-tags-expand match))
  12830. (if (string-match "/+" match)
  12831. ;; match contains also a todo-matching request
  12832. (progn
  12833. (setq tagsmatch (substring match 0 (match-beginning 0))
  12834. todomatch (substring match (match-end 0)))
  12835. (if (string-match "^!" todomatch)
  12836. (setq todo-only t todomatch (substring todomatch 1)))
  12837. (if (string-match "^\\s-*$" todomatch)
  12838. (setq todomatch nil)))
  12839. ;; only matching tags
  12840. (setq tagsmatch match todomatch nil))
  12841. ;; Make the tags matcher
  12842. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12843. (setq tagsmatcher t)
  12844. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12845. (while (setq term (pop orterms))
  12846. (while (and (equal (substring term -1) "\\") orterms)
  12847. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12848. (while (string-match re term)
  12849. (setq rest (substring term (match-end 0))
  12850. minus (and (match-end 1)
  12851. (equal (match-string 1 term) "-"))
  12852. tag (save-match-data (replace-regexp-in-string
  12853. "\\\\-" "-"
  12854. (match-string 2 term)))
  12855. re-p (equal (string-to-char tag) ?{)
  12856. level-p (match-end 4)
  12857. prop-p (match-end 5)
  12858. mm (cond
  12859. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12860. (level-p
  12861. (setq level-op (org-op-to-function (match-string 3 term)))
  12862. `(,level-op level ,(string-to-number
  12863. (match-string 4 term))))
  12864. (prop-p
  12865. (setq pn (match-string 5 term)
  12866. po (match-string 6 term)
  12867. pv (match-string 7 term)
  12868. re-p (equal (string-to-char pv) ?{)
  12869. str-p (equal (string-to-char pv) ?\")
  12870. time-p (save-match-data
  12871. (string-match "^\"[[<].*[]>]\"$" pv))
  12872. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12873. (if time-p (setq pv (org-matcher-time pv)))
  12874. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12875. (cond
  12876. ((equal pn "CATEGORY")
  12877. (setq gv '(get-text-property (point) 'org-category)))
  12878. ((equal pn "TODO")
  12879. (setq gv 'todo))
  12880. (t
  12881. (setq gv `(org-cached-entry-get nil ,pn))))
  12882. (if re-p
  12883. (if (eq po 'org<>)
  12884. `(not (string-match ,pv (or ,gv "")))
  12885. `(string-match ,pv (or ,gv "")))
  12886. (if str-p
  12887. `(,po (or ,gv "") ,pv)
  12888. `(,po (string-to-number (or ,gv ""))
  12889. ,(string-to-number pv) ))))
  12890. (t `(member ,tag tags-list)))
  12891. mm (if minus (list 'not mm) mm)
  12892. term rest)
  12893. (push mm tagsmatcher))
  12894. (push (if (> (length tagsmatcher) 1)
  12895. (cons 'and tagsmatcher)
  12896. (car tagsmatcher))
  12897. orlist)
  12898. (setq tagsmatcher nil))
  12899. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12900. (setq tagsmatcher
  12901. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12902. ;; Make the todo matcher
  12903. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12904. (setq todomatcher t)
  12905. (setq orterms (org-split-string todomatch "|") orlist nil)
  12906. (while (setq term (pop orterms))
  12907. (while (string-match re term)
  12908. (setq minus (and (match-end 1)
  12909. (equal (match-string 1 term) "-"))
  12910. kwd (match-string 2 term)
  12911. re-p (equal (string-to-char kwd) ?{)
  12912. term (substring term (match-end 0))
  12913. mm (if re-p
  12914. `(string-match ,(substring kwd 1 -1) todo)
  12915. (list 'equal 'todo kwd))
  12916. mm (if minus (list 'not mm) mm))
  12917. (push mm todomatcher))
  12918. (push (if (> (length todomatcher) 1)
  12919. (cons 'and todomatcher)
  12920. (car todomatcher))
  12921. orlist)
  12922. (setq todomatcher nil))
  12923. (setq todomatcher (if (> (length orlist) 1)
  12924. (cons 'or orlist) (car orlist))))
  12925. ;; Return the string and lisp forms of the matcher
  12926. (setq matcher (if todomatcher
  12927. (list 'and tagsmatcher todomatcher)
  12928. tagsmatcher))
  12929. (when todo-only
  12930. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12931. matcher)))
  12932. (cons match0 matcher)))
  12933. (defun org-tags-expand (match &optional single-as-list downcased)
  12934. "Expand group tags in MATCH.
  12935. This replaces every group tag in MATCH with a regexp tag search.
  12936. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12937. will be replaced like this:
  12938. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12939. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12940. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12941. Replacing by a regexp preserves the structure of the match.
  12942. E.g., this expansion
  12943. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12944. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12945. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12946. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12947. assumed to be a single group tag, and the function will return
  12948. the list of tags in this group.
  12949. When DOWNCASE is non-nil, expand downcased TAGS."
  12950. (if org-group-tags
  12951. (let* ((case-fold-search t)
  12952. (stable org-mode-syntax-table)
  12953. (tal (or org-tag-groups-alist-for-agenda
  12954. org-tag-groups-alist))
  12955. (tal (if downcased
  12956. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12957. (tml (mapcar 'car tal))
  12958. (rtnmatch match) rpl)
  12959. ;; @ and _ are allowed as word-components in tags
  12960. (modify-syntax-entry ?@ "w" stable)
  12961. (modify-syntax-entry ?_ "w" stable)
  12962. (while (and tml
  12963. (with-syntax-table stable
  12964. (string-match
  12965. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12966. (regexp-opt tml) "\\>\\)") rtnmatch)))
  12967. (let* ((dir (match-string 1 rtnmatch))
  12968. (tag (match-string 2 rtnmatch))
  12969. (tag (if downcased (downcase tag) tag)))
  12970. (setq tml (delete tag tml))
  12971. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12972. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12973. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12974. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12975. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12976. (if single-as-list
  12977. (or (reverse rpl) (list rtnmatch))
  12978. rtnmatch))
  12979. (if single-as-list (list (if downcased (downcase match) match))
  12980. match)))
  12981. (defun org-op-to-function (op &optional stringp)
  12982. "Turn an operator into the appropriate function."
  12983. (setq op
  12984. (cond
  12985. ((equal op "<" ) '(< string< org-time<))
  12986. ((equal op ">" ) '(> org-string> org-time>))
  12987. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12988. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12989. ((member op '("=" "==")) '(= string= org-time=))
  12990. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12991. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12992. (defun org<> (a b) (not (= a b)))
  12993. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12994. (defun org-string>= (a b) (not (string< a b)))
  12995. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12996. (defun org-string<> (a b) (not (string= a b)))
  12997. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12998. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12999. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13000. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13001. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13002. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13003. (defun org-2ft (s)
  13004. "Convert S to a floating point time.
  13005. If S is already a number, just return it. If it is a string, parse
  13006. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13007. (cond
  13008. ((numberp s) s)
  13009. ((stringp s)
  13010. (condition-case nil
  13011. (float-time (apply 'encode-time (org-parse-time-string s)))
  13012. (error 0.)))
  13013. (t 0.)))
  13014. (defun org-time-today ()
  13015. "Time in seconds today at 0:00.
  13016. Returns the float number of seconds since the beginning of the
  13017. epoch to the beginning of today (00:00)."
  13018. (float-time (apply 'encode-time
  13019. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13020. (defun org-matcher-time (s)
  13021. "Interpret a time comparison value."
  13022. (save-match-data
  13023. (cond
  13024. ((string= s "<now>") (float-time))
  13025. ((string= s "<today>") (org-time-today))
  13026. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13027. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13028. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13029. (+ (org-time-today)
  13030. (* (string-to-number (match-string 1 s))
  13031. (cdr (assoc (match-string 2 s)
  13032. '(("d" . 86400.0) ("w" . 604800.0)
  13033. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13034. (t (org-2ft s)))))
  13035. (defun org-match-any-p (re list)
  13036. "Does re match any element of list?"
  13037. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13038. (delq nil list))
  13039. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13040. (defvar org-tags-overlay (make-overlay 1 1))
  13041. (org-detach-overlay org-tags-overlay)
  13042. (defun org-get-local-tags-at (&optional pos)
  13043. "Get a list of tags defined in the current headline."
  13044. (org-get-tags-at pos 'local))
  13045. (defun org-get-local-tags ()
  13046. "Get a list of tags defined in the current headline."
  13047. (org-get-tags-at nil 'local))
  13048. (defun org-get-tags-at (&optional pos local)
  13049. "Get a list of all headline tags applicable at POS.
  13050. POS defaults to point. If tags are inherited, the list contains
  13051. the targets in the same sequence as the headlines appear, i.e.
  13052. the tags of the current headline come last.
  13053. When LOCAL is non-nil, only return tags from the current headline,
  13054. ignore inherited ones."
  13055. (interactive)
  13056. (if (and org-trust-scanner-tags
  13057. (or (not pos) (equal pos (point)))
  13058. (not local))
  13059. org-scanner-tags
  13060. (let (tags ltags lastpos parent)
  13061. (save-excursion
  13062. (save-restriction
  13063. (widen)
  13064. (goto-char (or pos (point)))
  13065. (save-match-data
  13066. (catch 'done
  13067. (condition-case nil
  13068. (progn
  13069. (org-back-to-heading t)
  13070. (while (not (equal lastpos (point)))
  13071. (setq lastpos (point))
  13072. (when (looking-at
  13073. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13074. (setq ltags (org-split-string
  13075. (org-match-string-no-properties 1) ":"))
  13076. (when parent
  13077. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13078. (setq tags (append
  13079. (if parent
  13080. (org-remove-uninherited-tags ltags)
  13081. ltags)
  13082. tags)))
  13083. (or org-use-tag-inheritance (throw 'done t))
  13084. (if local (throw 'done t))
  13085. (or (org-up-heading-safe) (error nil))
  13086. (setq parent t)))
  13087. (error nil)))))
  13088. (if local
  13089. tags
  13090. (reverse (delete-dups
  13091. (reverse (append
  13092. (org-remove-uninherited-tags
  13093. org-file-tags) tags)))))))))
  13094. (defun org-add-prop-inherited (s)
  13095. (add-text-properties 0 (length s) '(inherited t) s)
  13096. s)
  13097. (defun org-toggle-tag (tag &optional onoff)
  13098. "Toggle the tag TAG for the current line.
  13099. If ONOFF is `on' or `off', don't toggle but set to this state."
  13100. (let (res current)
  13101. (save-excursion
  13102. (org-back-to-heading t)
  13103. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13104. (point-at-eol) t)
  13105. (progn
  13106. (setq current (match-string 1))
  13107. (replace-match ""))
  13108. (setq current ""))
  13109. (setq current (nreverse (org-split-string current ":")))
  13110. (cond
  13111. ((eq onoff 'on)
  13112. (setq res t)
  13113. (or (member tag current) (push tag current)))
  13114. ((eq onoff 'off)
  13115. (or (not (member tag current)) (setq current (delete tag current))))
  13116. (t (if (member tag current)
  13117. (setq current (delete tag current))
  13118. (setq res t)
  13119. (push tag current))))
  13120. (end-of-line 1)
  13121. (if current
  13122. (progn
  13123. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13124. (org-set-tags nil t))
  13125. (delete-horizontal-space))
  13126. (run-hooks 'org-after-tags-change-hook))
  13127. res))
  13128. (defun org-align-tags-here (to-col)
  13129. ;; Assumes that this is a headline
  13130. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13131. (beginning-of-line 1)
  13132. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13133. (< pos (match-beginning 2)))
  13134. (progn
  13135. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13136. (goto-char (match-beginning 1))
  13137. (insert " ")
  13138. (delete-region (point) (1+ (match-beginning 2)))
  13139. (setq ncol (max (current-column)
  13140. (1+ col)
  13141. (if (> to-col 0)
  13142. to-col
  13143. (- (abs to-col) tags-l))))
  13144. (setq p (point))
  13145. (insert (make-string (- ncol (current-column)) ?\ ))
  13146. (setq ncol (current-column))
  13147. (when indent-tabs-mode (tabify p (point-at-eol)))
  13148. (org-move-to-column (min ncol col) t))
  13149. (goto-char pos))))
  13150. (defun org-set-tags-command (&optional arg just-align)
  13151. "Call the set-tags command for the current entry."
  13152. (interactive "P")
  13153. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13154. (org-set-tags arg just-align)
  13155. (save-excursion
  13156. (unless (and (org-region-active-p)
  13157. org-loop-over-headlines-in-active-region)
  13158. (org-back-to-heading t))
  13159. (org-set-tags arg just-align))))
  13160. (defun org-set-tags-to (data)
  13161. "Set the tags of the current entry to DATA, replacing the current tags.
  13162. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13163. If DATA is nil or the empty string, any tags will be removed."
  13164. (interactive "sTags: ")
  13165. (setq data
  13166. (cond
  13167. ((eq data nil) "")
  13168. ((equal data "") "")
  13169. ((stringp data)
  13170. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13171. ":"))
  13172. ((listp data)
  13173. (concat ":" (mapconcat 'identity data ":") ":"))))
  13174. (when data
  13175. (save-excursion
  13176. (org-back-to-heading t)
  13177. (when (looking-at org-complex-heading-regexp)
  13178. (if (match-end 5)
  13179. (progn
  13180. (goto-char (match-beginning 5))
  13181. (insert data)
  13182. (delete-region (point) (point-at-eol))
  13183. (org-set-tags nil 'align))
  13184. (goto-char (point-at-eol))
  13185. (insert " " data)
  13186. (org-set-tags nil 'align)))
  13187. (beginning-of-line 1)
  13188. (if (looking-at ".*?\\([ \t]+\\)$")
  13189. (delete-region (match-beginning 1) (match-end 1))))))
  13190. (defun org-align-all-tags ()
  13191. "Align the tags i all headings."
  13192. (interactive)
  13193. (save-excursion
  13194. (or (ignore-errors (org-back-to-heading t))
  13195. (outline-next-heading))
  13196. (if (org-at-heading-p)
  13197. (org-set-tags t)
  13198. (message "No headings"))))
  13199. (defvar org-indent-indentation-per-level)
  13200. (defun org-set-tags (&optional arg just-align)
  13201. "Set the tags for the current headline.
  13202. With prefix ARG, realign all tags in headings in the current buffer."
  13203. (interactive "P")
  13204. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13205. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13206. 'region-start-level 'region))
  13207. org-loop-over-headlines-in-active-region)
  13208. (org-map-entries
  13209. ;; We don't use ARG and JUST-ALIGN here these args are not
  13210. ;; useful when looping over headlines
  13211. `(org-set-tags)
  13212. org-loop-over-headlines-in-active-region
  13213. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13214. (let* ((re org-outline-regexp-bol)
  13215. (current (unless arg (org-get-tags-string)))
  13216. (col (current-column))
  13217. (org-setting-tags t)
  13218. table current-tags inherited-tags ; computed below when needed
  13219. tags p0 c0 c1 rpl di tc level)
  13220. (if arg
  13221. (save-excursion
  13222. (goto-char (point-min))
  13223. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13224. (while (re-search-forward re nil t)
  13225. (org-set-tags nil t)
  13226. (end-of-line 1)))
  13227. (message "All tags realigned to column %d" org-tags-column))
  13228. (if just-align
  13229. (setq tags current)
  13230. ;; Get a new set of tags from the user
  13231. (save-excursion
  13232. (setq table (append org-tag-persistent-alist
  13233. (or org-tag-alist (org-get-buffer-tags))
  13234. (and
  13235. org-complete-tags-always-offer-all-agenda-tags
  13236. (org-global-tags-completion-table
  13237. (org-agenda-files))))
  13238. org-last-tags-completion-table table
  13239. current-tags (org-split-string current ":")
  13240. inherited-tags (nreverse
  13241. (nthcdr (length current-tags)
  13242. (nreverse (org-get-tags-at))))
  13243. tags
  13244. (if (or (eq t org-use-fast-tag-selection)
  13245. (and org-use-fast-tag-selection
  13246. (delq nil (mapcar 'cdr table))))
  13247. (org-fast-tag-selection
  13248. current-tags inherited-tags table
  13249. (if org-fast-tag-selection-include-todo
  13250. org-todo-key-alist))
  13251. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13252. (org-trim
  13253. (org-icompleting-read "Tags: "
  13254. 'org-tags-completion-function
  13255. nil nil current 'org-tags-history))))))
  13256. (while (string-match "[-+&]+" tags)
  13257. ;; No boolean logic, just a list
  13258. (setq tags (replace-match ":" t t tags))))
  13259. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13260. (if org-tags-sort-function
  13261. (setq tags (mapconcat 'identity
  13262. (sort (org-split-string
  13263. tags (org-re "[^[:alnum:]_@#%]+"))
  13264. org-tags-sort-function) ":")))
  13265. (if (string-match "\\`[\t ]*\\'" tags)
  13266. (setq tags "")
  13267. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13268. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13269. ;; Insert new tags at the correct column
  13270. (beginning-of-line 1)
  13271. (setq level (or (and (looking-at org-outline-regexp)
  13272. (- (match-end 0) (point) 1))
  13273. 1))
  13274. (cond
  13275. ((and (equal current "") (equal tags "")))
  13276. ((re-search-forward
  13277. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13278. (point-at-eol) t)
  13279. (if (equal tags "")
  13280. (setq rpl "")
  13281. (goto-char (match-beginning 0))
  13282. (setq c0 (current-column)
  13283. ;; compute offset for the case of org-indent-mode active
  13284. di (if org-indent-mode
  13285. (* (1- org-indent-indentation-per-level) (1- level))
  13286. 0)
  13287. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13288. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13289. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13290. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13291. (replace-match rpl t t)
  13292. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13293. tags)
  13294. (t (error "Tags alignment failed")))
  13295. (org-move-to-column col)
  13296. (unless just-align
  13297. (run-hooks 'org-after-tags-change-hook))))))
  13298. (defun org-change-tag-in-region (beg end tag off)
  13299. "Add or remove TAG for each entry in the region.
  13300. This works in the agenda, and also in an org-mode buffer."
  13301. (interactive
  13302. (list (region-beginning) (region-end)
  13303. (let ((org-last-tags-completion-table
  13304. (if (derived-mode-p 'org-mode)
  13305. (org-uniquify
  13306. (delq nil (append (org-get-buffer-tags)
  13307. (org-global-tags-completion-table))))
  13308. (org-global-tags-completion-table))))
  13309. (org-icompleting-read
  13310. "Tag: " 'org-tags-completion-function nil nil nil
  13311. 'org-tags-history))
  13312. (progn
  13313. (message "[s]et or [r]emove? ")
  13314. (equal (read-char-exclusive) ?r))))
  13315. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13316. (let ((agendap (equal major-mode 'org-agenda-mode))
  13317. l1 l2 m buf pos newhead (cnt 0))
  13318. (goto-char end)
  13319. (setq l2 (1- (org-current-line)))
  13320. (goto-char beg)
  13321. (setq l1 (org-current-line))
  13322. (loop for l from l1 to l2 do
  13323. (org-goto-line l)
  13324. (setq m (get-text-property (point) 'org-hd-marker))
  13325. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13326. (and agendap m))
  13327. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13328. pos (if agendap m (point)))
  13329. (with-current-buffer buf
  13330. (save-excursion
  13331. (save-restriction
  13332. (goto-char pos)
  13333. (setq cnt (1+ cnt))
  13334. (org-toggle-tag tag (if off 'off 'on))
  13335. (setq newhead (org-get-heading)))))
  13336. (and agendap (org-agenda-change-all-lines newhead m))))
  13337. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13338. (defun org-tags-completion-function (string predicate &optional flag)
  13339. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13340. (confirm (lambda (x) (stringp (car x)))))
  13341. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13342. (setq s1 (match-string 1 string)
  13343. s2 (match-string 2 string))
  13344. (setq s1 "" s2 string))
  13345. (cond
  13346. ((eq flag nil)
  13347. ;; try completion
  13348. (setq rtn (try-completion s2 ctable confirm))
  13349. (if (stringp rtn)
  13350. (setq rtn
  13351. (concat s1 s2 (substring rtn (length s2))
  13352. (if (and org-add-colon-after-tag-completion
  13353. (assoc rtn ctable))
  13354. ":" ""))))
  13355. rtn)
  13356. ((eq flag t)
  13357. ;; all-completions
  13358. (all-completions s2 ctable confirm))
  13359. ((eq flag 'lambda)
  13360. ;; exact match?
  13361. (assoc s2 ctable)))))
  13362. (defun org-fast-tag-insert (kwd tags face &optional end)
  13363. "Insert KDW, and the TAGS, the latter with face FACE.
  13364. Also insert END."
  13365. (insert (format "%-12s" (concat kwd ":"))
  13366. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13367. (or end "")))
  13368. (defun org-fast-tag-show-exit (flag)
  13369. (save-excursion
  13370. (org-goto-line 3)
  13371. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13372. (replace-match ""))
  13373. (when flag
  13374. (end-of-line 1)
  13375. (org-move-to-column (- (window-width) 19) t)
  13376. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13377. (defun org-set-current-tags-overlay (current prefix)
  13378. "Add an overlay to CURRENT tag with PREFIX."
  13379. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13380. (if (featurep 'xemacs)
  13381. (org-overlay-display org-tags-overlay (concat prefix s)
  13382. 'secondary-selection)
  13383. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13384. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13385. (defvar org-last-tag-selection-key nil)
  13386. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13387. "Fast tag selection with single keys.
  13388. CURRENT is the current list of tags in the headline, INHERITED is the
  13389. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13390. possibly with grouping information. TODO-TABLE is a similar table with
  13391. TODO keywords, should these have keys assigned to them.
  13392. If the keys are nil, a-z are automatically assigned.
  13393. Returns the new tags string, or nil to not change the current settings."
  13394. (let* ((fulltable (append table todo-table))
  13395. (maxlen (apply 'max (mapcar
  13396. (lambda (x)
  13397. (if (stringp (car x)) (string-width (car x)) 0))
  13398. fulltable)))
  13399. (buf (current-buffer))
  13400. (expert (eq org-fast-tag-selection-single-key 'expert))
  13401. (buffer-tags nil)
  13402. (fwidth (+ maxlen 3 1 3))
  13403. (ncol (/ (- (window-width) 4) fwidth))
  13404. (i-face 'org-done)
  13405. (c-face 'org-todo)
  13406. tg cnt e c char c1 c2 ntable tbl rtn
  13407. ov-start ov-end ov-prefix
  13408. (exit-after-next org-fast-tag-selection-single-key)
  13409. (done-keywords org-done-keywords)
  13410. groups ingroup)
  13411. (save-excursion
  13412. (beginning-of-line 1)
  13413. (if (looking-at
  13414. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13415. (setq ov-start (match-beginning 1)
  13416. ov-end (match-end 1)
  13417. ov-prefix "")
  13418. (setq ov-start (1- (point-at-eol))
  13419. ov-end (1+ ov-start))
  13420. (skip-chars-forward "^\n\r")
  13421. (setq ov-prefix
  13422. (concat
  13423. (buffer-substring (1- (point)) (point))
  13424. (if (> (current-column) org-tags-column)
  13425. " "
  13426. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13427. (move-overlay org-tags-overlay ov-start ov-end)
  13428. (save-window-excursion
  13429. (if expert
  13430. (set-buffer (get-buffer-create " *Org tags*"))
  13431. (delete-other-windows)
  13432. (split-window-vertically)
  13433. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13434. (erase-buffer)
  13435. (org-set-local 'org-done-keywords done-keywords)
  13436. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13437. (org-fast-tag-insert "Current" current c-face "\n\n")
  13438. (org-fast-tag-show-exit exit-after-next)
  13439. (org-set-current-tags-overlay current ov-prefix)
  13440. (setq tbl fulltable char ?a cnt 0)
  13441. (while (setq e (pop tbl))
  13442. (cond
  13443. ((equal (car e) :startgroup)
  13444. (push '() groups) (setq ingroup t)
  13445. (when (not (= cnt 0))
  13446. (setq cnt 0)
  13447. (insert "\n"))
  13448. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13449. ((equal (car e) :endgroup)
  13450. (setq ingroup nil cnt 0)
  13451. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13452. ((equal e '(:newline))
  13453. (when (not (= cnt 0))
  13454. (setq cnt 0)
  13455. (insert "\n")
  13456. (setq e (car tbl))
  13457. (while (equal (car tbl) '(:newline))
  13458. (insert "\n")
  13459. (setq tbl (cdr tbl)))))
  13460. ((equal e '(:grouptags)) nil)
  13461. (t
  13462. (setq tg (copy-sequence (car e)) c2 nil)
  13463. (if (cdr e)
  13464. (setq c (cdr e))
  13465. ;; automatically assign a character.
  13466. (setq c1 (string-to-char
  13467. (downcase (substring
  13468. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13469. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13470. (while (or (rassoc char ntable) (rassoc char table))
  13471. (setq char (1+ char)))
  13472. (setq c2 c1))
  13473. (setq c (or c2 char)))
  13474. (if ingroup (push tg (car groups)))
  13475. (setq tg (org-add-props tg nil 'face
  13476. (cond
  13477. ((not (assoc tg table))
  13478. (org-get-todo-face tg))
  13479. ((member tg current) c-face)
  13480. ((member tg inherited) i-face))))
  13481. (if (equal (caar tbl) :grouptags)
  13482. (org-add-props tg nil 'face 'org-tag-group))
  13483. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13484. (insert "[" c "] " tg (make-string
  13485. (- fwidth 4 (length tg)) ?\ ))
  13486. (push (cons tg c) ntable)
  13487. (when (= (setq cnt (1+ cnt)) ncol)
  13488. (insert "\n")
  13489. (if ingroup (insert " "))
  13490. (setq cnt 0)))))
  13491. (setq ntable (nreverse ntable))
  13492. (insert "\n")
  13493. (goto-char (point-min))
  13494. (if (not expert) (org-fit-window-to-buffer))
  13495. (setq rtn
  13496. (catch 'exit
  13497. (while t
  13498. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13499. (if (not groups) "no " "")
  13500. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13501. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13502. (setq org-last-tag-selection-key c)
  13503. (cond
  13504. ((= c ?\r) (throw 'exit t))
  13505. ((= c ?!)
  13506. (setq groups (not groups))
  13507. (goto-char (point-min))
  13508. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13509. ((= c ?\C-c)
  13510. (if (not expert)
  13511. (org-fast-tag-show-exit
  13512. (setq exit-after-next (not exit-after-next)))
  13513. (setq expert nil)
  13514. (delete-other-windows)
  13515. (set-window-buffer (split-window-vertically) " *Org tags*")
  13516. (org-switch-to-buffer-other-window " *Org tags*")
  13517. (org-fit-window-to-buffer)))
  13518. ((or (= c ?\C-g)
  13519. (and (= c ?q) (not (rassoc c ntable))))
  13520. (org-detach-overlay org-tags-overlay)
  13521. (setq quit-flag t))
  13522. ((= c ?\ )
  13523. (setq current nil)
  13524. (if exit-after-next (setq exit-after-next 'now)))
  13525. ((= c ?\t)
  13526. (condition-case nil
  13527. (setq tg (org-icompleting-read
  13528. "Tag: "
  13529. (or buffer-tags
  13530. (with-current-buffer buf
  13531. (org-get-buffer-tags)))))
  13532. (quit (setq tg "")))
  13533. (when (string-match "\\S-" tg)
  13534. (add-to-list 'buffer-tags (list tg))
  13535. (if (member tg current)
  13536. (setq current (delete tg current))
  13537. (push tg current)))
  13538. (if exit-after-next (setq exit-after-next 'now)))
  13539. ((setq e (rassoc c todo-table) tg (car e))
  13540. (with-current-buffer buf
  13541. (save-excursion (org-todo tg)))
  13542. (if exit-after-next (setq exit-after-next 'now)))
  13543. ((setq e (rassoc c ntable) tg (car e))
  13544. (if (member tg current)
  13545. (setq current (delete tg current))
  13546. (loop for g in groups do
  13547. (if (member tg g)
  13548. (mapc (lambda (x)
  13549. (setq current (delete x current)))
  13550. g)))
  13551. (push tg current))
  13552. (if exit-after-next (setq exit-after-next 'now))))
  13553. ;; Create a sorted list
  13554. (setq current
  13555. (sort current
  13556. (lambda (a b)
  13557. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13558. (if (eq exit-after-next 'now) (throw 'exit t))
  13559. (goto-char (point-min))
  13560. (beginning-of-line 2)
  13561. (delete-region (point) (point-at-eol))
  13562. (org-fast-tag-insert "Current" current c-face)
  13563. (org-set-current-tags-overlay current ov-prefix)
  13564. (while (re-search-forward
  13565. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13566. (setq tg (match-string 1))
  13567. (add-text-properties
  13568. (match-beginning 1) (match-end 1)
  13569. (list 'face
  13570. (cond
  13571. ((member tg current) c-face)
  13572. ((member tg inherited) i-face)
  13573. (t (get-text-property (match-beginning 1) 'face))))))
  13574. (goto-char (point-min)))))
  13575. (org-detach-overlay org-tags-overlay)
  13576. (if rtn
  13577. (mapconcat 'identity current ":")
  13578. nil))))
  13579. (defun org-get-tags-string ()
  13580. "Get the TAGS string in the current headline."
  13581. (unless (org-at-heading-p t)
  13582. (user-error "Not on a heading"))
  13583. (save-excursion
  13584. (beginning-of-line 1)
  13585. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13586. (org-match-string-no-properties 1)
  13587. "")))
  13588. (defun org-get-tags ()
  13589. "Get the list of tags specified in the current headline."
  13590. (org-split-string (org-get-tags-string) ":"))
  13591. (defun org-get-buffer-tags ()
  13592. "Get a table of all tags used in the buffer, for completion."
  13593. (let (tags)
  13594. (save-excursion
  13595. (goto-char (point-min))
  13596. (while (re-search-forward
  13597. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13598. (when (equal (char-after (point-at-bol 0)) ?*)
  13599. (mapc (lambda (x) (add-to-list 'tags x))
  13600. (org-split-string (org-match-string-no-properties 1) ":")))))
  13601. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13602. (mapcar 'list tags)))
  13603. ;;;; The mapping API
  13604. (defun org-map-entries (func &optional match scope &rest skip)
  13605. "Call FUNC at each headline selected by MATCH in SCOPE.
  13606. FUNC is a function or a lisp form. The function will be called without
  13607. arguments, with the cursor positioned at the beginning of the headline.
  13608. The return values of all calls to the function will be collected and
  13609. returned as a list.
  13610. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13611. does not need to preserve point. After evaluation, the cursor will be
  13612. moved to the end of the line (presumably of the headline of the
  13613. processed entry) and search continues from there. Under some
  13614. circumstances, this may not produce the wanted results. For example,
  13615. if you have removed (e.g. archived) the current (sub)tree it could
  13616. mean that the next entry will be skipped entirely. In such cases, you
  13617. can specify the position from where search should continue by making
  13618. FUNC set the variable `org-map-continue-from' to the desired buffer
  13619. position.
  13620. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13621. Only headlines that are matched by this query will be considered during
  13622. the iteration. When MATCH is nil or t, all headlines will be
  13623. visited by the iteration.
  13624. SCOPE determines the scope of this command. It can be any of:
  13625. nil The current buffer, respecting the restriction if any
  13626. tree The subtree started with the entry at point
  13627. region The entries within the active region, if any
  13628. region-start-level
  13629. The entries within the active region, but only those at
  13630. the same level than the first one.
  13631. file The current buffer, without restriction
  13632. file-with-archives
  13633. The current buffer, and any archives associated with it
  13634. agenda All agenda files
  13635. agenda-with-archives
  13636. All agenda files with any archive files associated with them
  13637. \(file1 file2 ...)
  13638. If this is a list, all files in the list will be scanned
  13639. The remaining args are treated as settings for the skipping facilities of
  13640. the scanner. The following items can be given here:
  13641. archive skip trees with the archive tag
  13642. comment skip trees with the COMMENT keyword
  13643. function or Emacs Lisp form:
  13644. will be used as value for `org-agenda-skip-function', so
  13645. whenever the function returns a position, FUNC will not be
  13646. called for that entry and search will continue from the
  13647. position returned
  13648. If your function needs to retrieve the tags including inherited tags
  13649. at the *current* entry, you can use the value of the variable
  13650. `org-scanner-tags' which will be much faster than getting the value
  13651. with `org-get-tags-at'. If your function gets properties with
  13652. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13653. to t around the call to `org-entry-properties' to get the same speedup.
  13654. Note that if your function moves around to retrieve tags and properties at
  13655. a *different* entry, you cannot use these techniques."
  13656. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13657. (not (org-region-active-p)))
  13658. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13659. (org-agenda-skip-archived-trees (memq 'archive skip))
  13660. (org-agenda-skip-comment-trees (memq 'comment skip))
  13661. (org-agenda-skip-function
  13662. (car (org-delete-all '(comment archive) skip)))
  13663. (org-tags-match-list-sublevels t)
  13664. (start-level (eq scope 'region-start-level))
  13665. matcher file res
  13666. org-todo-keywords-for-agenda
  13667. org-done-keywords-for-agenda
  13668. org-todo-keyword-alist-for-agenda
  13669. org-drawers-for-agenda
  13670. org-tag-alist-for-agenda
  13671. todo-only)
  13672. (cond
  13673. ((eq match t) (setq matcher t))
  13674. ((eq match nil) (setq matcher t))
  13675. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13676. (save-window-excursion
  13677. (save-restriction
  13678. (cond ((eq scope 'tree)
  13679. (org-back-to-heading t)
  13680. (org-narrow-to-subtree)
  13681. (setq scope nil))
  13682. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13683. (org-region-active-p))
  13684. ;; If needed, set start-level to a string like "2"
  13685. (when start-level
  13686. (save-excursion
  13687. (goto-char (region-beginning))
  13688. (unless (org-at-heading-p) (outline-next-heading))
  13689. (setq start-level (org-current-level))))
  13690. (narrow-to-region (region-beginning)
  13691. (save-excursion
  13692. (goto-char (region-end))
  13693. (unless (and (bolp) (org-at-heading-p))
  13694. (outline-next-heading))
  13695. (point)))
  13696. (setq scope nil)))
  13697. (if (not scope)
  13698. (progn
  13699. (org-agenda-prepare-buffers
  13700. (list (buffer-file-name (current-buffer))))
  13701. (setq res (org-scan-tags func matcher todo-only start-level)))
  13702. ;; Get the right scope
  13703. (cond
  13704. ((and scope (listp scope) (symbolp (car scope)))
  13705. (setq scope (eval scope)))
  13706. ((eq scope 'agenda)
  13707. (setq scope (org-agenda-files t)))
  13708. ((eq scope 'agenda-with-archives)
  13709. (setq scope (org-agenda-files t))
  13710. (setq scope (org-add-archive-files scope)))
  13711. ((eq scope 'file)
  13712. (setq scope (list (buffer-file-name))))
  13713. ((eq scope 'file-with-archives)
  13714. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13715. (org-agenda-prepare-buffers scope)
  13716. (while (setq file (pop scope))
  13717. (with-current-buffer (org-find-base-buffer-visiting file)
  13718. (save-excursion
  13719. (save-restriction
  13720. (widen)
  13721. (goto-char (point-min))
  13722. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13723. res)))
  13724. ;;;; Properties
  13725. ;;; Setting and retrieving properties
  13726. (defconst org-special-properties
  13727. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13728. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13729. "The special properties valid in Org-mode.
  13730. These are properties that are not defined in the property drawer,
  13731. but in some other way.")
  13732. (defconst org-default-properties
  13733. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13734. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13735. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13736. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13737. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13738. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13739. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13740. "Some properties that are used by Org-mode for various purposes.
  13741. Being in this list makes sure that they are offered for completion.")
  13742. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13743. "Regular expression matching the first line of a property drawer.")
  13744. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13745. "Regular expression matching the last line of a property drawer.")
  13746. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13747. "Regular expression matching the first line of a property drawer.")
  13748. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13749. "Regular expression matching the first line of a property drawer.")
  13750. (defconst org-property-drawer-re
  13751. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13752. org-property-end-re "\\)\n?")
  13753. "Matches an entire property drawer.")
  13754. (defconst org-clock-drawer-re
  13755. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13756. org-property-end-re "\\)\n?")
  13757. "Matches an entire clock drawer.")
  13758. (defun org-property-action ()
  13759. "Do an action on properties."
  13760. (interactive)
  13761. (let (c)
  13762. (org-at-property-p)
  13763. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13764. (setq c (read-char-exclusive))
  13765. (cond
  13766. ((equal c ?s)
  13767. (call-interactively 'org-set-property))
  13768. ((equal c ?d)
  13769. (call-interactively 'org-delete-property))
  13770. ((equal c ?D)
  13771. (call-interactively 'org-delete-property-globally))
  13772. ((equal c ?c)
  13773. (call-interactively 'org-compute-property-at-point))
  13774. (t (user-error "No such property action %c" c)))))
  13775. (defun org-inc-effort ()
  13776. "Increment the value of the effort property in the current entry."
  13777. (interactive)
  13778. (org-set-effort nil t))
  13779. (defvar org-clock-effort) ;; Defined in org-clock.el
  13780. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13781. (defun org-set-effort (&optional value increment)
  13782. "Set the effort property of the current entry.
  13783. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13784. 10th allowed value.
  13785. When INCREMENT is non-nil, set the property to the next allowed value."
  13786. (interactive "P")
  13787. (if (equal value 0) (setq value 10))
  13788. (let* ((completion-ignore-case t)
  13789. (prop org-effort-property)
  13790. (cur (org-entry-get nil prop))
  13791. (allowed (org-property-get-allowed-values nil prop 'table))
  13792. (existing (mapcar 'list (org-property-values prop)))
  13793. (heading (nth 4 (org-heading-components)))
  13794. rpl
  13795. (val (cond
  13796. ((stringp value) value)
  13797. ((and allowed (integerp value))
  13798. (or (car (nth (1- value) allowed))
  13799. (car (org-last allowed))))
  13800. ((and allowed increment)
  13801. (or (caadr (member (list cur) allowed))
  13802. (user-error "Allowed effort values are not set")))
  13803. (allowed
  13804. (message "Select 1-9,0, [RET%s]: %s"
  13805. (if cur (concat "=" cur) "")
  13806. (mapconcat 'car allowed " "))
  13807. (setq rpl (read-char-exclusive))
  13808. (if (equal rpl ?\r)
  13809. cur
  13810. (setq rpl (- rpl ?0))
  13811. (if (equal rpl 0) (setq rpl 10))
  13812. (if (and (> rpl 0) (<= rpl (length allowed)))
  13813. (car (nth (1- rpl) allowed))
  13814. (org-completing-read "Effort: " allowed nil))))
  13815. (t
  13816. (let (org-completion-use-ido org-completion-use-iswitchb)
  13817. (org-completing-read
  13818. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13819. (concat "[" cur "]") "")
  13820. ": ")
  13821. existing nil nil "" nil cur))))))
  13822. (unless (equal (org-entry-get nil prop) val)
  13823. (org-entry-put nil prop val))
  13824. (save-excursion
  13825. (org-back-to-heading t)
  13826. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13827. (when (string= heading org-clock-current-task)
  13828. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13829. (org-clock-update-mode-line))
  13830. (message "%s is now %s" prop val)))
  13831. (defun org-at-property-p ()
  13832. "Is cursor inside a property drawer?"
  13833. (save-excursion
  13834. (when (equal 'node-property (car (org-element-at-point)))
  13835. (beginning-of-line 1)
  13836. (looking-at org-property-re))))
  13837. (defun org-get-property-block (&optional beg end force)
  13838. "Return the (beg . end) range of the body of the property drawer.
  13839. BEG and END are the beginning and end of the current subtree, or of
  13840. the part before the first headline. If they are not given, they will
  13841. be found. If the drawer does not exist and FORCE is non-nil, create
  13842. the drawer."
  13843. (catch 'exit
  13844. (save-excursion
  13845. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13846. (progn (org-back-to-heading t) (point))))
  13847. (end (or end (and (not (outline-next-heading)) (point-max))
  13848. (point))))
  13849. (goto-char beg)
  13850. (if (re-search-forward org-property-start-re end t)
  13851. (setq beg (1+ (match-end 0)))
  13852. (if force
  13853. (save-excursion
  13854. (org-insert-property-drawer)
  13855. (setq end (progn (outline-next-heading) (point))))
  13856. (throw 'exit nil))
  13857. (goto-char beg)
  13858. (if (re-search-forward org-property-start-re end t)
  13859. (setq beg (1+ (match-end 0)))))
  13860. (if (re-search-forward org-property-end-re end t)
  13861. (setq end (match-beginning 0))
  13862. (or force (throw 'exit nil))
  13863. (goto-char beg)
  13864. (setq end beg)
  13865. (org-indent-line)
  13866. (insert ":END:\n"))
  13867. (cons beg end)))))
  13868. (defun org-entry-properties (&optional pom which specific)
  13869. "Get all properties of the entry at point-or-marker POM.
  13870. This includes the TODO keyword, the tags, time strings for deadline,
  13871. scheduled, and clocking, and any additional properties defined in the
  13872. entry. The return value is an alist, keys may occur multiple times
  13873. if the property key was used several times.
  13874. POM may also be nil, in which case the current entry is used.
  13875. If WHICH is nil or `all', get all properties. If WHICH is
  13876. `special' or `standard', only get that subclass. If WHICH
  13877. is a string only get exactly this property. SPECIFIC can be a string, the
  13878. specific property we are interested in. Specifying it can speed
  13879. things up because then unnecessary parsing is avoided."
  13880. (setq which (or which 'all))
  13881. (org-with-point-at pom
  13882. (let ((clockstr (substring org-clock-string 0 -1))
  13883. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13884. (case-fold-search nil)
  13885. beg end range props sum-props key key1 value string clocksum clocksumt)
  13886. (save-excursion
  13887. (when (condition-case nil
  13888. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13889. (error nil))
  13890. (setq beg (point))
  13891. (setq sum-props (get-text-property (point) 'org-summaries))
  13892. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13893. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13894. (outline-next-heading)
  13895. (setq end (point))
  13896. (when (memq which '(all special))
  13897. ;; Get the special properties, like TODO and tags
  13898. (goto-char beg)
  13899. (when (and (or (not specific) (string= specific "TODO"))
  13900. (looking-at org-todo-line-regexp) (match-end 2))
  13901. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13902. (when (and (or (not specific) (string= specific "PRIORITY"))
  13903. (looking-at org-priority-regexp))
  13904. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13905. (when (or (not specific) (string= specific "FILE"))
  13906. (push (cons "FILE" buffer-file-name) props))
  13907. (when (and (or (not specific) (string= specific "TAGS"))
  13908. (setq value (org-get-tags-string))
  13909. (string-match "\\S-" value))
  13910. (push (cons "TAGS" value) props))
  13911. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13912. (setq value (org-get-tags-at)))
  13913. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13914. ":"))
  13915. props))
  13916. (when (or (not specific) (string= specific "BLOCKED"))
  13917. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13918. (when (or (not specific)
  13919. (member specific
  13920. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13921. "TIMESTAMP" "TIMESTAMP_IA")))
  13922. (catch 'match
  13923. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13924. (setq key (if (match-end 1)
  13925. (substring (org-match-string-no-properties 1)
  13926. 0 -1))
  13927. string (if (equal key clockstr)
  13928. (org-trim
  13929. (buffer-substring-no-properties
  13930. (match-beginning 3) (goto-char
  13931. (point-at-eol))))
  13932. (substring (org-match-string-no-properties 3)
  13933. 1 -1)))
  13934. ;; Get the correct property name from the key. This is
  13935. ;; necessary if the user has configured time keywords.
  13936. (setq key1 (concat key ":"))
  13937. (cond
  13938. ((not key)
  13939. (setq key
  13940. (if (= (char-after (match-beginning 3)) ?\[)
  13941. "TIMESTAMP_IA" "TIMESTAMP")))
  13942. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13943. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13944. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13945. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13946. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13947. (progn
  13948. (push (cons key string) props)
  13949. ;; no need to search further if match is found
  13950. (throw 'match t))
  13951. (when (or (equal key "CLOCK") (not (assoc key props)))
  13952. (push (cons key string) props)))))))
  13953. (when (memq which '(all standard))
  13954. ;; Get the standard properties, like :PROP: ...
  13955. (setq range (org-get-property-block beg end))
  13956. (when range
  13957. (goto-char (car range))
  13958. (while (re-search-forward org-property-re
  13959. (cdr range) t)
  13960. (setq key (org-match-string-no-properties 2)
  13961. value (org-trim (or (org-match-string-no-properties 3) "")))
  13962. (unless (member key excluded)
  13963. (push (cons key (or value "")) props)))))
  13964. (if clocksum
  13965. (push (cons "CLOCKSUM"
  13966. (org-columns-number-to-string (/ (float clocksum) 60.)
  13967. 'add_times))
  13968. props))
  13969. (if clocksumt
  13970. (push (cons "CLOCKSUM_T"
  13971. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13972. 'add_times))
  13973. props))
  13974. (unless (assoc "CATEGORY" props)
  13975. (push (cons "CATEGORY" (org-get-category)) props))
  13976. (append sum-props (nreverse props)))))))
  13977. (defun org-entry-get (pom property &optional inherit literal-nil)
  13978. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13979. If INHERIT is non-nil and the entry does not have the property,
  13980. then also check higher levels of the hierarchy.
  13981. If INHERIT is the symbol `selective', use inheritance only if the setting
  13982. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13983. If the property is present but empty, the return value is the empty string.
  13984. If the property is not present at all, nil is returned.
  13985. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13986. do not interpret it as the list atom nil. This is used for inheritance
  13987. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13988. (org-with-point-at pom
  13989. (if (and inherit (if (eq inherit 'selective)
  13990. (org-property-inherit-p property)
  13991. t))
  13992. (org-entry-get-with-inheritance property literal-nil)
  13993. (if (member property org-special-properties)
  13994. ;; We need a special property. Use `org-entry-properties' to
  13995. ;; retrieve it, but specify the wanted property
  13996. (cdr (assoc property (org-entry-properties nil 'special property)))
  13997. (let ((range (org-get-property-block)))
  13998. (when (and range (not (eq (car range) (cdr range))))
  13999. (let* ((props (list (or (assoc property org-file-properties)
  14000. (assoc property org-global-properties)
  14001. (assoc property org-global-properties-fixed))))
  14002. (ap (lambda (key)
  14003. (when (re-search-forward
  14004. (org-re-property key) (cdr range) t)
  14005. (setq props
  14006. (org-update-property-plist
  14007. key
  14008. (if (match-end 3)
  14009. (org-match-string-no-properties 3) "")
  14010. props)))))
  14011. val)
  14012. (goto-char (car range))
  14013. (funcall ap property)
  14014. (goto-char (car range))
  14015. (while (funcall ap (concat property "+")))
  14016. (setq val (cdr (assoc property props)))
  14017. (when val (if literal-nil val (org-not-nil val))))))))))
  14018. (defun org-property-or-variable-value (var &optional inherit)
  14019. "Check if there is a property fixing the value of VAR.
  14020. If yes, return this value. If not, return the current value of the variable."
  14021. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14022. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14023. (read prop)
  14024. (symbol-value var))))
  14025. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14026. "Delete the property PROPERTY from entry at point-or-marker POM.
  14027. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14028. an empty drawer to delete."
  14029. (org-with-point-at pom
  14030. (if (member property org-special-properties)
  14031. nil ; cannot delete these properties.
  14032. (let ((range (org-get-property-block)))
  14033. (if (and range
  14034. (goto-char (car range))
  14035. (re-search-forward
  14036. (org-re-property property)
  14037. (cdr range) t))
  14038. (progn
  14039. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14040. (and delete-empty-drawer
  14041. (org-remove-empty-drawer-at
  14042. delete-empty-drawer (car range)))
  14043. t)
  14044. nil)))))
  14045. ;; Multi-values properties are properties that contain multiple values
  14046. ;; These values are assumed to be single words, separated by whitespace.
  14047. (defun org-entry-add-to-multivalued-property (pom property value)
  14048. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14049. (let* ((old (org-entry-get pom property))
  14050. (values (and old (org-split-string old "[ \t]"))))
  14051. (setq value (org-entry-protect-space value))
  14052. (unless (member value values)
  14053. (setq values (append values (list value)))
  14054. (org-entry-put pom property
  14055. (mapconcat 'identity values " ")))))
  14056. (defun org-entry-remove-from-multivalued-property (pom property value)
  14057. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14058. (let* ((old (org-entry-get pom property))
  14059. (values (and old (org-split-string old "[ \t]"))))
  14060. (setq value (org-entry-protect-space value))
  14061. (when (member value values)
  14062. (setq values (delete value values))
  14063. (org-entry-put pom property
  14064. (mapconcat 'identity values " ")))))
  14065. (defun org-entry-member-in-multivalued-property (pom property value)
  14066. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14067. (let* ((old (org-entry-get pom property))
  14068. (values (and old (org-split-string old "[ \t]"))))
  14069. (setq value (org-entry-protect-space value))
  14070. (member value values)))
  14071. (defun org-entry-get-multivalued-property (pom property)
  14072. "Return a list of values in a multivalued property."
  14073. (let* ((value (org-entry-get pom property))
  14074. (values (and value (org-split-string value "[ \t]"))))
  14075. (mapcar 'org-entry-restore-space values)))
  14076. (defun org-entry-put-multivalued-property (pom property &rest values)
  14077. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14078. VALUES should be a list of strings. Spaces will be protected."
  14079. (org-entry-put pom property
  14080. (mapconcat 'org-entry-protect-space values " "))
  14081. (let* ((value (org-entry-get pom property))
  14082. (values (and value (org-split-string value "[ \t]"))))
  14083. (mapcar 'org-entry-restore-space values)))
  14084. (defun org-entry-protect-space (s)
  14085. "Protect spaces and newline in string S."
  14086. (while (string-match " " s)
  14087. (setq s (replace-match "%20" t t s)))
  14088. (while (string-match "\n" s)
  14089. (setq s (replace-match "%0A" t t s)))
  14090. s)
  14091. (defun org-entry-restore-space (s)
  14092. "Restore spaces and newline in string S."
  14093. (while (string-match "%20" s)
  14094. (setq s (replace-match " " t t s)))
  14095. (while (string-match "%0A" s)
  14096. (setq s (replace-match "\n" t t s)))
  14097. s)
  14098. (defvar org-entry-property-inherited-from (make-marker)
  14099. "Marker pointing to the entry from where a property was inherited.
  14100. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14101. location of the entry where the inheritance search matched. If there was
  14102. no match, the marker will point nowhere.
  14103. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14104. is set.")
  14105. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14106. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14107. The search will stop at the first ancestor which has the property defined.
  14108. If the value found is \"nil\", return nil to show that the property
  14109. should be considered as undefined (this is the meaning of nil here).
  14110. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14111. (move-marker org-entry-property-inherited-from nil)
  14112. (let (tmp)
  14113. (save-excursion
  14114. (save-restriction
  14115. (widen)
  14116. (catch 'ex
  14117. (while t
  14118. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14119. (or (ignore-errors (org-back-to-heading t))
  14120. (goto-char (point-min)))
  14121. (move-marker org-entry-property-inherited-from (point))
  14122. (throw 'ex tmp))
  14123. (or (ignore-errors (org-up-heading-safe))
  14124. (throw 'ex nil))))))
  14125. (setq tmp (or tmp
  14126. (cdr (assoc property org-file-properties))
  14127. (cdr (assoc property org-global-properties))
  14128. (cdr (assoc property org-global-properties-fixed))))
  14129. (if literal-nil tmp (org-not-nil tmp))))
  14130. (defvar org-property-changed-functions nil
  14131. "Hook called when the value of a property has changed.
  14132. Each hook function should accept two arguments, the name of the property
  14133. and the new value.")
  14134. (defun org-entry-put (pom property value)
  14135. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14136. (org-with-point-at pom
  14137. (org-back-to-heading t)
  14138. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14139. range)
  14140. (cond
  14141. ((equal property "TODO")
  14142. (when (and (stringp value) (string-match "\\S-" value)
  14143. (not (member value org-todo-keywords-1)))
  14144. (user-error "\"%s\" is not a valid TODO state" value))
  14145. (if (or (not value)
  14146. (not (string-match "\\S-" value)))
  14147. (setq value 'none))
  14148. (org-todo value)
  14149. (org-set-tags nil 'align))
  14150. ((equal property "PRIORITY")
  14151. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14152. (string-to-char value) ?\ ))
  14153. (org-set-tags nil 'align))
  14154. ((equal property "CLOCKSUM")
  14155. (if (not (re-search-forward
  14156. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14157. (error "Cannot find a clock log")
  14158. (goto-char (- (match-end 1) 2))
  14159. (cond
  14160. ((eq value 'earlier) (org-timestamp-down))
  14161. ((eq value 'later) (org-timestamp-up)))
  14162. (org-clock-sum-current-item)))
  14163. ((equal property "SCHEDULED")
  14164. (if (re-search-forward org-scheduled-time-regexp end t)
  14165. (cond
  14166. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14167. ((eq value 'later) (org-timestamp-change 1 'day))
  14168. (t (call-interactively 'org-schedule)))
  14169. (call-interactively 'org-schedule)))
  14170. ((equal property "DEADLINE")
  14171. (if (re-search-forward org-deadline-time-regexp end t)
  14172. (cond
  14173. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14174. ((eq value 'later) (org-timestamp-change 1 'day))
  14175. (t (call-interactively 'org-deadline)))
  14176. (call-interactively 'org-deadline)))
  14177. ((member property org-special-properties)
  14178. (error "The %s property can not yet be set with `org-entry-put'"
  14179. property))
  14180. (t ; a non-special property
  14181. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14182. (setq range (org-get-property-block beg end 'force))
  14183. (goto-char (car range))
  14184. (if (re-search-forward
  14185. (org-re-property property) (cdr range) t)
  14186. (progn
  14187. (delete-region (match-beginning 0) (match-end 0))
  14188. (goto-char (match-beginning 0)))
  14189. (goto-char (cdr range))
  14190. (insert "\n")
  14191. (backward-char 1)
  14192. (org-indent-line))
  14193. (insert ":" property ":")
  14194. (and value (insert " " value))
  14195. (org-indent-line)))))
  14196. (run-hook-with-args 'org-property-changed-functions property value)))
  14197. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14198. "Get all property keys in the current buffer.
  14199. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14200. like tags and TODO state.
  14201. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14202. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14203. and others.
  14204. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14205. formats in the current buffer."
  14206. (let (rtn range cfmt s p)
  14207. (save-excursion
  14208. (save-restriction
  14209. (widen)
  14210. (goto-char (point-min))
  14211. (while (re-search-forward org-property-start-re nil t)
  14212. (setq range (org-get-property-block))
  14213. (goto-char (car range))
  14214. (while (re-search-forward org-property-re
  14215. (cdr range) t)
  14216. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14217. (outline-next-heading))))
  14218. (when include-specials
  14219. (setq rtn (append org-special-properties rtn)))
  14220. (when include-defaults
  14221. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14222. (add-to-list 'rtn org-effort-property))
  14223. (when include-columns
  14224. (save-excursion
  14225. (save-restriction
  14226. (widen)
  14227. (goto-char (point-min))
  14228. (while (re-search-forward
  14229. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14230. nil t)
  14231. (setq cfmt (match-string 2) s 0)
  14232. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14233. cfmt s)
  14234. (setq s (match-end 0)
  14235. p (match-string 1 cfmt))
  14236. (unless (or (equal p "ITEM")
  14237. (member p org-special-properties))
  14238. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14239. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14240. (defun org-property-values (key)
  14241. "Return a list of all values of property KEY in the current buffer."
  14242. (save-excursion
  14243. (save-restriction
  14244. (widen)
  14245. (goto-char (point-min))
  14246. (let ((re (org-re-property key))
  14247. values)
  14248. (while (re-search-forward re nil t)
  14249. (add-to-list 'values (org-trim (match-string 3))))
  14250. (delete "" values)))))
  14251. (defun org-insert-property-drawer ()
  14252. "Insert a property drawer into the current entry."
  14253. (org-back-to-heading t)
  14254. (looking-at org-outline-regexp)
  14255. (let ((indent (if org-adapt-indentation
  14256. (- (match-end 0) (match-beginning 0))
  14257. 0))
  14258. (beg (point))
  14259. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14260. end hiddenp)
  14261. (outline-next-heading)
  14262. (setq end (point))
  14263. (goto-char beg)
  14264. (while (re-search-forward re end t))
  14265. (setq hiddenp (outline-invisible-p))
  14266. (end-of-line 1)
  14267. (and (equal (char-after) ?\n) (forward-char 1))
  14268. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14269. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14270. ;; just skip this line
  14271. (beginning-of-line 2)
  14272. ;; Drawer start, find the end
  14273. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14274. (beginning-of-line 1)))
  14275. (org-skip-over-state-notes)
  14276. (skip-chars-backward " \t\n\r")
  14277. (if (eq (char-before) ?*) (forward-char 1))
  14278. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14279. (beginning-of-line 0)
  14280. (org-indent-to-column indent)
  14281. (beginning-of-line 2)
  14282. (org-indent-to-column indent)
  14283. (beginning-of-line 0)
  14284. (if hiddenp
  14285. (save-excursion
  14286. (org-back-to-heading t)
  14287. (hide-entry))
  14288. (org-flag-drawer t))))
  14289. (defun org-insert-drawer (&optional arg drawer)
  14290. "Insert a drawer at point.
  14291. Optional argument DRAWER, when non-nil, is a string representing
  14292. drawer's name. Otherwise, the user is prompted for a name.
  14293. If a region is active, insert the drawer around that region
  14294. instead.
  14295. Point is left between drawer's boundaries."
  14296. (interactive "P")
  14297. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14298. "LOGBOOK"))
  14299. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14300. ;; internally by Org. They are meant to be inserted
  14301. ;; automatically.
  14302. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14303. ;; Remove system drawers from list. Note: For some reason,
  14304. ;; `org-completing-read' ignores the predicate while
  14305. ;; `completing-read' handles it fine.
  14306. (drawer (if arg "PROPERTIES"
  14307. (or drawer
  14308. (completing-read
  14309. "Drawer: " org-drawers
  14310. (lambda (d) (not (member d system-drawers))))))))
  14311. (cond
  14312. ;; With C-u, fall back on `org-insert-property-drawer'
  14313. (arg (org-insert-property-drawer))
  14314. ;; With an active region, insert a drawer at point.
  14315. ((not (org-region-active-p))
  14316. (progn
  14317. (unless (bolp) (insert "\n"))
  14318. (insert (format ":%s:\n\n:END:\n" drawer))
  14319. (forward-line -2)))
  14320. ;; Otherwise, insert the drawer at point
  14321. (t
  14322. (let ((rbeg (region-beginning))
  14323. (rend (copy-marker (region-end))))
  14324. (unwind-protect
  14325. (progn
  14326. (goto-char rbeg)
  14327. (beginning-of-line)
  14328. (when (save-excursion
  14329. (re-search-forward org-outline-regexp-bol rend t))
  14330. (user-error "Drawers cannot contain headlines"))
  14331. ;; Position point at the beginning of the first
  14332. ;; non-blank line in region. Insert drawer's opening
  14333. ;; there, then indent it.
  14334. (org-skip-whitespace)
  14335. (beginning-of-line)
  14336. (insert ":" drawer ":\n")
  14337. (forward-line -1)
  14338. (indent-for-tab-command)
  14339. ;; Move point to the beginning of the first blank line
  14340. ;; after the last non-blank line in region. Insert
  14341. ;; drawer's closing, then indent it.
  14342. (goto-char rend)
  14343. (skip-chars-backward " \r\t\n")
  14344. (insert "\n:END:")
  14345. (deactivate-mark t)
  14346. (indent-for-tab-command)
  14347. (unless (eolp) (insert "\n")))
  14348. ;; Clear marker, whatever the outcome of insertion is.
  14349. (set-marker rend nil)))))))
  14350. (defvar org-property-set-functions-alist nil
  14351. "Property set function alist.
  14352. Each entry should have the following format:
  14353. (PROPERTY . READ-FUNCTION)
  14354. The read function will be called with the same argument as
  14355. `org-completing-read'.")
  14356. (defun org-set-property-function (property)
  14357. "Get the function that should be used to set PROPERTY.
  14358. This is computed according to `org-property-set-functions-alist'."
  14359. (or (cdr (assoc property org-property-set-functions-alist))
  14360. 'org-completing-read))
  14361. (defun org-read-property-value (property)
  14362. "Read PROPERTY value from user."
  14363. (let* ((completion-ignore-case t)
  14364. (allowed (org-property-get-allowed-values nil property 'table))
  14365. (cur (org-entry-get nil property))
  14366. (prompt (concat property " value"
  14367. (if (and cur (string-match "\\S-" cur))
  14368. (concat " [" cur "]") "") ": "))
  14369. (set-function (org-set-property-function property))
  14370. (val (if allowed
  14371. (funcall set-function prompt allowed nil
  14372. (not (get-text-property 0 'org-unrestricted
  14373. (caar allowed))))
  14374. (let (org-completion-use-ido org-completion-use-iswitchb)
  14375. (funcall set-function prompt
  14376. (mapcar 'list (org-property-values property))
  14377. nil nil "" nil cur)))))
  14378. (if (equal val "")
  14379. cur
  14380. val)))
  14381. (defvar org-last-set-property nil)
  14382. (defvar org-last-set-property-value nil)
  14383. (defun org-read-property-name ()
  14384. "Read a property name."
  14385. (let* ((completion-ignore-case t)
  14386. (keys (org-buffer-property-keys nil t t))
  14387. (default-prop (or (save-excursion
  14388. (save-match-data
  14389. (beginning-of-line)
  14390. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14391. (null (string= (match-string 1) "END"))
  14392. (match-string 1))))
  14393. org-last-set-property))
  14394. (property (org-icompleting-read
  14395. (concat "Property"
  14396. (if default-prop (concat " [" default-prop "]") "")
  14397. ": ")
  14398. (mapcar 'list keys)
  14399. nil nil nil nil
  14400. default-prop)))
  14401. (if (member property keys)
  14402. property
  14403. (or (cdr (assoc (downcase property)
  14404. (mapcar (lambda (x) (cons (downcase x) x))
  14405. keys)))
  14406. property))))
  14407. (defun org-set-property-and-value (use-last)
  14408. "Allow to set [PROPERTY]: [value] direction from prompt.
  14409. When use-default, don't even ask, just use the last
  14410. \"[PROPERTY]: [value]\" string from the history."
  14411. (interactive "P")
  14412. (let* ((completion-ignore-case t)
  14413. (pv (or (and use-last org-last-set-property-value)
  14414. (org-completing-read
  14415. "Enter a \"[Property]: [value]\" pair: "
  14416. nil nil nil nil nil
  14417. org-last-set-property-value)))
  14418. prop val)
  14419. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14420. (setq prop (match-string 1 pv)
  14421. val (match-string 2 pv))
  14422. (org-set-property prop val))))
  14423. (defun org-set-property (property value)
  14424. "In the current entry, set PROPERTY to VALUE.
  14425. When called interactively, this will prompt for a property name, offering
  14426. completion on existing and default properties. And then it will prompt
  14427. for a value, offering completion either on allowed values (via an inherited
  14428. xxx_ALL property) or on existing values in other instances of this property
  14429. in the current file."
  14430. (interactive (list nil nil))
  14431. (let* ((property (or property (org-read-property-name)))
  14432. (value (or value (org-read-property-value property)))
  14433. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14434. (setq org-last-set-property property)
  14435. (setq org-last-set-property-value (concat property ": " value))
  14436. ;; Possibly postprocess the inserted value:
  14437. (when fn (setq value (funcall fn value)))
  14438. (unless (equal (org-entry-get nil property) value)
  14439. (org-entry-put nil property value))))
  14440. (defun org-delete-property (property &optional delete-empty-drawer)
  14441. "In the current entry, delete PROPERTY.
  14442. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14443. an empty drawer to delete."
  14444. (interactive
  14445. (let* ((completion-ignore-case t)
  14446. (prop (org-icompleting-read "Property: "
  14447. (org-entry-properties nil 'standard))))
  14448. (list prop)))
  14449. (message "Property %s %s" property
  14450. (if (org-entry-delete nil property delete-empty-drawer)
  14451. "deleted"
  14452. "was not present in the entry")))
  14453. (defun org-delete-property-globally (property)
  14454. "Remove PROPERTY globally, from all entries."
  14455. (interactive
  14456. (let* ((completion-ignore-case t)
  14457. (prop (org-icompleting-read
  14458. "Globally remove property: "
  14459. (mapcar 'list (org-buffer-property-keys)))))
  14460. (list prop)))
  14461. (save-excursion
  14462. (save-restriction
  14463. (widen)
  14464. (goto-char (point-min))
  14465. (let ((cnt 0))
  14466. (while (re-search-forward
  14467. (org-re-property property)
  14468. nil t)
  14469. (setq cnt (1+ cnt))
  14470. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14471. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14472. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14473. (defun org-compute-property-at-point ()
  14474. "Compute the property at point.
  14475. This looks for an enclosing column format, extracts the operator and
  14476. then applies it to the property in the column format's scope."
  14477. (interactive)
  14478. (unless (org-at-property-p)
  14479. (user-error "Not at a property"))
  14480. (let ((prop (org-match-string-no-properties 2)))
  14481. (org-columns-get-format-and-top-level)
  14482. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14483. (user-error "No operator defined for property %s" prop))
  14484. (org-columns-compute prop)))
  14485. (defvar org-property-allowed-value-functions nil
  14486. "Hook for functions supplying allowed values for a specific property.
  14487. The functions must take a single argument, the name of the property, and
  14488. return a flat list of allowed values. If \":ETC\" is one of
  14489. the values, this means that these values are intended as defaults for
  14490. completion, but that other values should be allowed too.
  14491. The functions must return nil if they are not responsible for this
  14492. property.")
  14493. (defun org-property-get-allowed-values (pom property &optional table)
  14494. "Get allowed values for the property PROPERTY.
  14495. When TABLE is non-nil, return an alist that can directly be used for
  14496. completion."
  14497. (let (vals)
  14498. (cond
  14499. ((equal property "TODO")
  14500. (setq vals (org-with-point-at pom
  14501. (append org-todo-keywords-1 '("")))))
  14502. ((equal property "PRIORITY")
  14503. (let ((n org-lowest-priority))
  14504. (while (>= n org-highest-priority)
  14505. (push (char-to-string n) vals)
  14506. (setq n (1- n)))))
  14507. ((member property org-special-properties))
  14508. ((setq vals (run-hook-with-args-until-success
  14509. 'org-property-allowed-value-functions property)))
  14510. (t
  14511. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14512. (when (and vals (string-match "\\S-" vals))
  14513. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14514. (setq vals (mapcar (lambda (x)
  14515. (cond ((stringp x) x)
  14516. ((numberp x) (number-to-string x))
  14517. ((symbolp x) (symbol-name x))
  14518. (t "???")))
  14519. vals)))))
  14520. (when (member ":ETC" vals)
  14521. (setq vals (remove ":ETC" vals))
  14522. (org-add-props (car vals) '(org-unrestricted t)))
  14523. (if table (mapcar 'list vals) vals)))
  14524. (defun org-property-previous-allowed-value (&optional previous)
  14525. "Switch to the next allowed value for this property."
  14526. (interactive)
  14527. (org-property-next-allowed-value t))
  14528. (defun org-property-next-allowed-value (&optional previous)
  14529. "Switch to the next allowed value for this property."
  14530. (interactive)
  14531. (unless (org-at-property-p)
  14532. (user-error "Not at a property"))
  14533. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14534. (key (match-string 2))
  14535. (value (match-string 3))
  14536. (allowed (or (org-property-get-allowed-values (point) key)
  14537. (and (member value '("[ ]" "[-]" "[X]"))
  14538. '("[ ]" "[X]"))))
  14539. (heading (save-match-data (nth 4 (org-heading-components))))
  14540. nval)
  14541. (unless allowed
  14542. (user-error "Allowed values for this property have not been defined"))
  14543. (if previous (setq allowed (reverse allowed)))
  14544. (if (member value allowed)
  14545. (setq nval (car (cdr (member value allowed)))))
  14546. (setq nval (or nval (car allowed)))
  14547. (if (equal nval value)
  14548. (user-error "Only one allowed value for this property"))
  14549. (org-at-property-p)
  14550. (replace-match (concat " :" key ": " nval) t t)
  14551. (org-indent-line)
  14552. (beginning-of-line 1)
  14553. (skip-chars-forward " \t")
  14554. (when (equal prop org-effort-property)
  14555. (save-excursion
  14556. (org-back-to-heading t)
  14557. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14558. (when (string= org-clock-current-task heading)
  14559. (setq org-clock-effort nval)
  14560. (org-clock-update-mode-line)))
  14561. (run-hook-with-args 'org-property-changed-functions key nval)))
  14562. (defun org-find-olp (path &optional this-buffer)
  14563. "Return a marker pointing to the entry at outline path OLP.
  14564. If anything goes wrong, throw an error.
  14565. You can wrap this call to catch the error like this:
  14566. (condition-case msg
  14567. (org-mobile-locate-entry (match-string 4))
  14568. (error (nth 1 msg)))
  14569. The return value will then be either a string with the error message,
  14570. or a marker if everything is OK.
  14571. If THIS-BUFFER is set, the outline path does not contain a file,
  14572. only headings."
  14573. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14574. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14575. (level 1)
  14576. (lmin 1)
  14577. (lmax 1)
  14578. limit re end found pos heading cnt flevel)
  14579. (unless buffer (error "File not found :%s" file))
  14580. (with-current-buffer buffer
  14581. (save-excursion
  14582. (save-restriction
  14583. (widen)
  14584. (setq limit (point-max))
  14585. (goto-char (point-min))
  14586. (while (setq heading (pop path))
  14587. (setq re (format org-complex-heading-regexp-format
  14588. (regexp-quote heading)))
  14589. (setq cnt 0 pos (point))
  14590. (while (re-search-forward re end t)
  14591. (setq level (- (match-end 1) (match-beginning 1)))
  14592. (if (and (>= level lmin) (<= level lmax))
  14593. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14594. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14595. lmax heading))
  14596. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14597. lmax heading))
  14598. (goto-char found)
  14599. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14600. (setq end (save-excursion (org-end-of-subtree t t))))
  14601. (when (org-at-heading-p)
  14602. (point-marker)))))))
  14603. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14604. "Find node HEADING in BUFFER.
  14605. Return a marker to the heading if it was found, or nil if not.
  14606. If POS-ONLY is set, return just the position instead of a marker.
  14607. The heading text must match exact, but it may have a TODO keyword,
  14608. a priority cookie and tags in the standard locations."
  14609. (with-current-buffer (or buffer (current-buffer))
  14610. (save-excursion
  14611. (save-restriction
  14612. (widen)
  14613. (goto-char (point-min))
  14614. (let (case-fold-search)
  14615. (if (re-search-forward
  14616. (format org-complex-heading-regexp-format
  14617. (regexp-quote heading)) nil t)
  14618. (if pos-only
  14619. (match-beginning 0)
  14620. (move-marker (make-marker) (match-beginning 0)))))))))
  14621. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14622. "Find Org node headline HEADING in all .org files in directory DIR.
  14623. When the target headline is found, return a marker to this location."
  14624. (let ((files (directory-files (or dir default-directory)
  14625. nil "\\`[^.#].*\\.org\\'"))
  14626. file visiting m buffer)
  14627. (catch 'found
  14628. (while (setq file (pop files))
  14629. (message "trying %s" file)
  14630. (setq visiting (org-find-base-buffer-visiting file))
  14631. (setq buffer (or visiting (find-file-noselect file)))
  14632. (setq m (org-find-exact-headline-in-buffer
  14633. heading buffer))
  14634. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14635. (and m (throw 'found m))))))
  14636. (defun org-find-entry-with-id (ident)
  14637. "Locate the entry that contains the ID property with exact value IDENT.
  14638. IDENT can be a string, a symbol or a number, this function will search for
  14639. the string representation of it.
  14640. Return the position where this entry starts, or nil if there is no such entry."
  14641. (interactive "sID: ")
  14642. (let ((id (cond
  14643. ((stringp ident) ident)
  14644. ((symbol-name ident) (symbol-name ident))
  14645. ((numberp ident) (number-to-string ident))
  14646. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14647. (case-fold-search nil))
  14648. (save-excursion
  14649. (save-restriction
  14650. (widen)
  14651. (goto-char (point-min))
  14652. (when (re-search-forward
  14653. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14654. nil t)
  14655. (org-back-to-heading t)
  14656. (point))))))
  14657. ;;;; Timestamps
  14658. (defvar org-last-changed-timestamp nil)
  14659. (defvar org-last-inserted-timestamp nil
  14660. "The last time stamp inserted with `org-insert-time-stamp'.")
  14661. (defvar org-time-was-given) ; dynamically scoped parameter
  14662. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14663. (defvar org-ts-what) ; dynamically scoped parameter
  14664. (defun org-time-stamp (arg &optional inactive)
  14665. "Prompt for a date/time and insert a time stamp.
  14666. If the user specifies a time like HH:MM or if this command is
  14667. called with at least one prefix argument, the time stamp contains
  14668. the date and the time. Otherwise, only the date is be included.
  14669. All parts of a date not specified by the user is filled in from
  14670. the current date/time. So if you just press return without
  14671. typing anything, the time stamp will represent the current
  14672. date/time.
  14673. If there is already a timestamp at the cursor, it will be
  14674. modified.
  14675. With two universal prefix arguments, insert an active timestamp
  14676. with the current time without prompting the user."
  14677. (interactive "P")
  14678. (let* ((ts nil)
  14679. (default-time
  14680. ;; Default time is either today, or, when entering a range,
  14681. ;; the range start.
  14682. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14683. (save-excursion
  14684. (re-search-backward
  14685. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14686. (- (point) 20) t)))
  14687. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14688. (current-time)))
  14689. (default-input (and ts (org-get-compact-tod ts)))
  14690. (repeater (save-excursion
  14691. (save-match-data
  14692. (beginning-of-line)
  14693. (when (re-search-forward
  14694. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14695. (save-excursion (progn (end-of-line) (point))) t)
  14696. (match-string 0)))))
  14697. org-time-was-given org-end-time-was-given time)
  14698. (cond
  14699. ((and (org-at-timestamp-p t)
  14700. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14701. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14702. (insert "--")
  14703. (setq time (let ((this-command this-command))
  14704. (org-read-date arg 'totime nil nil
  14705. default-time default-input inactive)))
  14706. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14707. ((org-at-timestamp-p t)
  14708. (setq time (let ((this-command this-command))
  14709. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14710. (when (org-at-timestamp-p t) ; just to get the match data
  14711. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14712. (replace-match "")
  14713. (setq org-last-changed-timestamp
  14714. (org-insert-time-stamp
  14715. time (or org-time-was-given arg)
  14716. inactive nil nil (list org-end-time-was-given)))
  14717. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14718. (setq org-last-changed-timestamp
  14719. (concat (substring org-last-inserted-timestamp 0 -1)
  14720. " " repeater ">"))))
  14721. (message "Timestamp updated"))
  14722. ((equal arg '(16))
  14723. (org-insert-time-stamp (current-time) t))
  14724. (t
  14725. (setq time (let ((this-command this-command))
  14726. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14727. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14728. nil nil (list org-end-time-was-given))))))
  14729. ;; FIXME: can we use this for something else, like computing time differences?
  14730. (defun org-get-compact-tod (s)
  14731. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14732. (let* ((t1 (match-string 1 s))
  14733. (h1 (string-to-number (match-string 2 s)))
  14734. (m1 (string-to-number (match-string 3 s)))
  14735. (t2 (and (match-end 4) (match-string 5 s)))
  14736. (h2 (and t2 (string-to-number (match-string 6 s))))
  14737. (m2 (and t2 (string-to-number (match-string 7 s))))
  14738. dh dm)
  14739. (if (not t2)
  14740. t1
  14741. (setq dh (- h2 h1) dm (- m2 m1))
  14742. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14743. (concat t1 "+" (number-to-string dh)
  14744. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14745. (defun org-time-stamp-inactive (&optional arg)
  14746. "Insert an inactive time stamp.
  14747. An inactive time stamp is enclosed in square brackets instead of angle
  14748. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14749. does not link to the calendar and cannot be changed with the S-cursor keys.
  14750. So these are more for recording a certain time/date."
  14751. (interactive "P")
  14752. (org-time-stamp arg 'inactive))
  14753. (defvar org-date-ovl (make-overlay 1 1))
  14754. (overlay-put org-date-ovl 'face 'org-date-selected)
  14755. (org-detach-overlay org-date-ovl)
  14756. (defvar org-ans1) ; dynamically scoped parameter
  14757. (defvar org-ans2) ; dynamically scoped parameter
  14758. (defvar org-plain-time-of-day-regexp) ; defined below
  14759. (defvar org-overriding-default-time nil) ; dynamically scoped
  14760. (defvar org-read-date-overlay nil)
  14761. (defvar org-dcst nil) ; dynamically scoped
  14762. (defvar org-read-date-history nil)
  14763. (defvar org-read-date-final-answer nil)
  14764. (defvar org-read-date-analyze-futurep nil)
  14765. (defvar org-read-date-analyze-forced-year nil)
  14766. (defvar org-read-date-inactive)
  14767. (defvar org-read-date-minibuffer-local-map
  14768. (let ((map (make-sparse-keymap)))
  14769. (set-keymap-parent map minibuffer-local-map)
  14770. (org-defkey map (kbd ".")
  14771. (lambda () (interactive)
  14772. ;; Are we at the beginning of the prompt?
  14773. (if (looking-back "^[^:]+: ")
  14774. (org-eval-in-calendar '(calendar-goto-today))
  14775. (insert "."))))
  14776. (org-defkey map (kbd "C-.")
  14777. (lambda () (interactive)
  14778. (org-eval-in-calendar '(calendar-goto-today))))
  14779. (org-defkey map [(meta shift left)]
  14780. (lambda () (interactive)
  14781. (org-eval-in-calendar '(calendar-backward-month 1))))
  14782. (org-defkey map [(meta shift right)]
  14783. (lambda () (interactive)
  14784. (org-eval-in-calendar '(calendar-forward-month 1))))
  14785. (org-defkey map [(meta shift up)]
  14786. (lambda () (interactive)
  14787. (org-eval-in-calendar '(calendar-backward-year 1))))
  14788. (org-defkey map [(meta shift down)]
  14789. (lambda () (interactive)
  14790. (org-eval-in-calendar '(calendar-forward-year 1))))
  14791. (org-defkey map [?\e (shift left)]
  14792. (lambda () (interactive)
  14793. (org-eval-in-calendar '(calendar-backward-month 1))))
  14794. (org-defkey map [?\e (shift right)]
  14795. (lambda () (interactive)
  14796. (org-eval-in-calendar '(calendar-forward-month 1))))
  14797. (org-defkey map [?\e (shift up)]
  14798. (lambda () (interactive)
  14799. (org-eval-in-calendar '(calendar-backward-year 1))))
  14800. (org-defkey map [?\e (shift down)]
  14801. (lambda () (interactive)
  14802. (org-eval-in-calendar '(calendar-forward-year 1))))
  14803. (org-defkey map [(shift up)]
  14804. (lambda () (interactive)
  14805. (org-eval-in-calendar '(calendar-backward-week 1))))
  14806. (org-defkey map [(shift down)]
  14807. (lambda () (interactive)
  14808. (org-eval-in-calendar '(calendar-forward-week 1))))
  14809. (org-defkey map [(shift left)]
  14810. (lambda () (interactive)
  14811. (org-eval-in-calendar '(calendar-backward-day 1))))
  14812. (org-defkey map [(shift right)]
  14813. (lambda () (interactive)
  14814. (org-eval-in-calendar '(calendar-forward-day 1))))
  14815. (org-defkey map "!"
  14816. (lambda () (interactive)
  14817. (org-eval-in-calendar '(diary-view-entries))
  14818. (message "")))
  14819. (org-defkey map ">"
  14820. (lambda () (interactive)
  14821. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14822. (org-defkey map "<"
  14823. (lambda () (interactive)
  14824. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14825. (org-defkey map "\C-v"
  14826. (lambda () (interactive)
  14827. (org-eval-in-calendar
  14828. '(calendar-scroll-left-three-months 1))))
  14829. (org-defkey map "\M-v"
  14830. (lambda () (interactive)
  14831. (org-eval-in-calendar
  14832. '(calendar-scroll-right-three-months 1))))
  14833. map)
  14834. "Keymap for minibuffer commands when using `org-read-date'.")
  14835. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14836. default-time default-input inactive)
  14837. "Read a date, possibly a time, and make things smooth for the user.
  14838. The prompt will suggest to enter an ISO date, but you can also enter anything
  14839. which will at least partially be understood by `parse-time-string'.
  14840. Unrecognized parts of the date will default to the current day, month, year,
  14841. hour and minute. If this command is called to replace a timestamp at point,
  14842. or to enter the second timestamp of a range, the default time is taken
  14843. from the existing stamp. Furthermore, the command prefers the future,
  14844. so if you are giving a date where the year is not given, and the day-month
  14845. combination is already past in the current year, it will assume you
  14846. mean next year. For details, see the manual. A few examples:
  14847. 3-2-5 --> 2003-02-05
  14848. feb 15 --> currentyear-02-15
  14849. 2/15 --> currentyear-02-15
  14850. sep 12 9 --> 2009-09-12
  14851. 12:45 --> today 12:45
  14852. 22 sept 0:34 --> currentyear-09-22 0:34
  14853. 12 --> currentyear-currentmonth-12
  14854. Fri --> nearest Friday after today
  14855. -Tue --> last Tuesday
  14856. etc.
  14857. Furthermore you can specify a relative date by giving, as the *first* thing
  14858. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14859. change in days weeks, months, years.
  14860. With a single plus or minus, the date is relative to today. With a double
  14861. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14862. +4d --> four days from today
  14863. +4 --> same as above
  14864. +2w --> two weeks from today
  14865. ++5 --> five days from default date
  14866. The function understands only English month and weekday abbreviations.
  14867. While prompting, a calendar is popped up - you can also select the
  14868. date with the mouse (button 1). The calendar shows a period of three
  14869. months. To scroll it to other months, use the keys `>' and `<'.
  14870. If you don't like the calendar, turn it off with
  14871. \(setq org-read-date-popup-calendar nil)
  14872. With optional argument TO-TIME, the date will immediately be converted
  14873. to an internal time.
  14874. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14875. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14876. still enter a time, and this function will inform the calling routine
  14877. about this change. The calling routine may then choose to change the
  14878. format used to insert the time stamp into the buffer to include the time.
  14879. With optional argument FROM-STRING, read from this string instead from
  14880. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14881. the time/date that is used for everything that is not specified by the
  14882. user."
  14883. (require 'parse-time)
  14884. (let* ((org-time-stamp-rounding-minutes
  14885. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14886. (org-dcst org-display-custom-times)
  14887. (ct (org-current-time))
  14888. (org-def (or org-overriding-default-time default-time ct))
  14889. (org-defdecode (decode-time org-def))
  14890. (dummy (progn
  14891. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14892. (setcar (nthcdr 2 org-defdecode) -1)
  14893. (setcar (nthcdr 1 org-defdecode) 59)
  14894. (setq org-def (apply 'encode-time org-defdecode)
  14895. org-defdecode (decode-time org-def)))))
  14896. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14897. (calendar-frame-setup nil)
  14898. (calendar-setup nil)
  14899. (calendar-move-hook nil)
  14900. (calendar-view-diary-initially-flag nil)
  14901. (calendar-view-holidays-initially-flag nil)
  14902. (timestr (format-time-string
  14903. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14904. (prompt (concat (if prompt (concat prompt " ") "")
  14905. (format "Date+time [%s]: " timestr)))
  14906. ans (org-ans0 "") org-ans1 org-ans2 final)
  14907. (cond
  14908. (from-string (setq ans from-string))
  14909. (org-read-date-popup-calendar
  14910. (save-excursion
  14911. (save-window-excursion
  14912. (calendar)
  14913. (org-eval-in-calendar '(setq cursor-type nil) t)
  14914. (unwind-protect
  14915. (progn
  14916. (calendar-forward-day (- (time-to-days org-def)
  14917. (calendar-absolute-from-gregorian
  14918. (calendar-current-date))))
  14919. (org-eval-in-calendar nil t)
  14920. (let* ((old-map (current-local-map))
  14921. (map (copy-keymap calendar-mode-map))
  14922. (minibuffer-local-map
  14923. (copy-keymap org-read-date-minibuffer-local-map)))
  14924. (org-defkey map (kbd "RET") 'org-calendar-select)
  14925. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14926. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14927. (unwind-protect
  14928. (progn
  14929. (use-local-map map)
  14930. (setq org-read-date-inactive inactive)
  14931. (add-hook 'post-command-hook 'org-read-date-display)
  14932. (setq org-ans0 (read-string prompt default-input
  14933. 'org-read-date-history nil))
  14934. ;; org-ans0: from prompt
  14935. ;; org-ans1: from mouse click
  14936. ;; org-ans2: from calendar motion
  14937. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14938. (remove-hook 'post-command-hook 'org-read-date-display)
  14939. (use-local-map old-map)
  14940. (when org-read-date-overlay
  14941. (delete-overlay org-read-date-overlay)
  14942. (setq org-read-date-overlay nil)))))
  14943. (bury-buffer "*Calendar*")))))
  14944. (t ; Naked prompt only
  14945. (unwind-protect
  14946. (setq ans (read-string prompt default-input
  14947. 'org-read-date-history timestr))
  14948. (when org-read-date-overlay
  14949. (delete-overlay org-read-date-overlay)
  14950. (setq org-read-date-overlay nil)))))
  14951. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14952. (when org-read-date-analyze-forced-year
  14953. (message "Year was forced into %s"
  14954. (if org-read-date-force-compatible-dates
  14955. "compatible range (1970-2037)"
  14956. "range representable on this machine"))
  14957. (ding))
  14958. ;; One round trip to get rid of 34th of August and stuff like that....
  14959. (setq final (decode-time (apply 'encode-time final)))
  14960. (setq org-read-date-final-answer ans)
  14961. (if to-time
  14962. (apply 'encode-time final)
  14963. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14964. (format "%04d-%02d-%02d %02d:%02d"
  14965. (nth 5 final) (nth 4 final) (nth 3 final)
  14966. (nth 2 final) (nth 1 final))
  14967. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14968. (defvar org-def)
  14969. (defvar org-defdecode)
  14970. (defvar org-with-time)
  14971. (defun org-read-date-display ()
  14972. "Display the current date prompt interpretation in the minibuffer."
  14973. (when org-read-date-display-live
  14974. (when org-read-date-overlay
  14975. (delete-overlay org-read-date-overlay))
  14976. (when (minibufferp (current-buffer))
  14977. (save-excursion
  14978. (end-of-line 1)
  14979. (while (not (equal (buffer-substring
  14980. (max (point-min) (- (point) 4)) (point))
  14981. " "))
  14982. (insert " ")))
  14983. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14984. " " (or org-ans1 org-ans2)))
  14985. (org-end-time-was-given nil)
  14986. (f (org-read-date-analyze ans org-def org-defdecode))
  14987. (fmts (if org-dcst
  14988. org-time-stamp-custom-formats
  14989. org-time-stamp-formats))
  14990. (fmt (if (or org-with-time
  14991. (and (boundp 'org-time-was-given) org-time-was-given))
  14992. (cdr fmts)
  14993. (car fmts)))
  14994. (txt (format-time-string fmt (apply 'encode-time f)))
  14995. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14996. (txt (concat "=> " txt)))
  14997. (when (and org-end-time-was-given
  14998. (string-match org-plain-time-of-day-regexp txt))
  14999. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15000. org-end-time-was-given
  15001. (substring txt (match-end 0)))))
  15002. (when org-read-date-analyze-futurep
  15003. (setq txt (concat txt " (=>F)")))
  15004. (setq org-read-date-overlay
  15005. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15006. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15007. (defun org-read-date-analyze (ans org-def org-defdecode)
  15008. "Analyze the combined answer of the date prompt."
  15009. ;; FIXME: cleanup and comment
  15010. (let ((nowdecode (decode-time (current-time)))
  15011. delta deltan deltaw deltadef year month day
  15012. hour minute second wday pm h2 m2 tl wday1
  15013. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15014. (setq org-read-date-analyze-futurep nil
  15015. org-read-date-analyze-forced-year nil)
  15016. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15017. (setq ans "+0"))
  15018. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15019. (setq ans (replace-match "" t t ans)
  15020. deltan (car delta)
  15021. deltaw (nth 1 delta)
  15022. deltadef (nth 2 delta)))
  15023. ;; Check if there is an iso week date in there. If yes, store the
  15024. ;; info and postpone interpreting it until the rest of the parsing
  15025. ;; is done.
  15026. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15027. (setq iso-year (if (match-end 1)
  15028. (org-small-year-to-year
  15029. (string-to-number (match-string 1 ans))))
  15030. iso-weekday (if (match-end 3)
  15031. (string-to-number (match-string 3 ans)))
  15032. iso-week (string-to-number (match-string 2 ans)))
  15033. (setq ans (replace-match "" t t ans)))
  15034. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15035. (when (string-match
  15036. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15037. (setq year (if (match-end 2)
  15038. (string-to-number (match-string 2 ans))
  15039. (progn (setq kill-year t)
  15040. (string-to-number (format-time-string "%Y"))))
  15041. month (string-to-number (match-string 3 ans))
  15042. day (string-to-number (match-string 4 ans)))
  15043. (if (< year 100) (setq year (+ 2000 year)))
  15044. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15045. t nil ans)))
  15046. ;; Help matching dotted european dates
  15047. (when (string-match
  15048. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15049. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15050. (setq kill-year t)
  15051. (string-to-number (format-time-string "%Y")))
  15052. day (string-to-number (match-string 1 ans))
  15053. month (string-to-number (match-string 2 ans))
  15054. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15055. t nil ans)))
  15056. ;; Help matching american dates, like 5/30 or 5/30/7
  15057. (when (string-match
  15058. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15059. (setq year (if (match-end 4)
  15060. (string-to-number (match-string 4 ans))
  15061. (progn (setq kill-year t)
  15062. (string-to-number (format-time-string "%Y"))))
  15063. month (string-to-number (match-string 1 ans))
  15064. day (string-to-number (match-string 2 ans)))
  15065. (if (< year 100) (setq year (+ 2000 year)))
  15066. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15067. t nil ans)))
  15068. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15069. ;; If there is a time with am/pm, and *no* time without it, we convert
  15070. ;; so that matching will be successful.
  15071. (loop for i from 1 to 2 do ; twice, for end time as well
  15072. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15073. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15074. (setq hour (string-to-number (match-string 1 ans))
  15075. minute (if (match-end 3)
  15076. (string-to-number (match-string 3 ans))
  15077. 0)
  15078. pm (equal ?p
  15079. (string-to-char (downcase (match-string 4 ans)))))
  15080. (if (and (= hour 12) (not pm))
  15081. (setq hour 0)
  15082. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15083. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15084. t t ans))))
  15085. ;; Check if a time range is given as a duration
  15086. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15087. (setq hour (string-to-number (match-string 1 ans))
  15088. h2 (+ hour (string-to-number (match-string 3 ans)))
  15089. minute (string-to-number (match-string 2 ans))
  15090. m2 (+ minute (if (match-end 5) (string-to-number
  15091. (match-string 5 ans))0)))
  15092. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15093. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15094. t t ans)))
  15095. ;; Check if there is a time range
  15096. (when (boundp 'org-end-time-was-given)
  15097. (setq org-time-was-given nil)
  15098. (when (and (string-match org-plain-time-of-day-regexp ans)
  15099. (match-end 8))
  15100. (setq org-end-time-was-given (match-string 8 ans))
  15101. (setq ans (concat (substring ans 0 (match-beginning 7))
  15102. (substring ans (match-end 7))))))
  15103. (setq tl (parse-time-string ans)
  15104. day (or (nth 3 tl) (nth 3 org-defdecode))
  15105. month (or (nth 4 tl)
  15106. (if (and org-read-date-prefer-future
  15107. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15108. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15109. (nth 4 org-defdecode)))
  15110. year (or (and (not kill-year) (nth 5 tl))
  15111. (if (and org-read-date-prefer-future
  15112. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15113. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15114. (nth 5 org-defdecode)))
  15115. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15116. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15117. second (or (nth 0 tl) 0)
  15118. wday (nth 6 tl))
  15119. (when (and (eq org-read-date-prefer-future 'time)
  15120. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15121. (equal day (nth 3 nowdecode))
  15122. (equal month (nth 4 nowdecode))
  15123. (equal year (nth 5 nowdecode))
  15124. (nth 2 tl)
  15125. (or (< (nth 2 tl) (nth 2 nowdecode))
  15126. (and (= (nth 2 tl) (nth 2 nowdecode))
  15127. (nth 1 tl)
  15128. (< (nth 1 tl) (nth 1 nowdecode)))))
  15129. (setq day (1+ day)
  15130. futurep t))
  15131. ;; Special date definitions below
  15132. (cond
  15133. (iso-week
  15134. ;; There was an iso week
  15135. (require 'cal-iso)
  15136. (setq futurep nil)
  15137. (setq year (or iso-year year)
  15138. day (or iso-weekday wday 1)
  15139. wday nil ; to make sure that the trigger below does not match
  15140. iso-date (calendar-gregorian-from-absolute
  15141. (calendar-absolute-from-iso
  15142. (list iso-week day year))))
  15143. ; FIXME: Should we also push ISO weeks into the future?
  15144. ; (when (and org-read-date-prefer-future
  15145. ; (not iso-year)
  15146. ; (< (calendar-absolute-from-gregorian iso-date)
  15147. ; (time-to-days (current-time))))
  15148. ; (setq year (1+ year)
  15149. ; iso-date (calendar-gregorian-from-absolute
  15150. ; (calendar-absolute-from-iso
  15151. ; (list iso-week day year)))))
  15152. (setq month (car iso-date)
  15153. year (nth 2 iso-date)
  15154. day (nth 1 iso-date)))
  15155. (deltan
  15156. (setq futurep nil)
  15157. (unless deltadef
  15158. (let ((now (decode-time (current-time))))
  15159. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15160. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15161. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15162. ((equal deltaw "m") (setq month (+ month deltan)))
  15163. ((equal deltaw "y") (setq year (+ year deltan)))))
  15164. ((and wday (not (nth 3 tl)))
  15165. ;; Weekday was given, but no day, so pick that day in the week
  15166. ;; on or after the derived date.
  15167. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15168. (unless (equal wday wday1)
  15169. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15170. (if (and (boundp 'org-time-was-given)
  15171. (nth 2 tl))
  15172. (setq org-time-was-given t))
  15173. (if (< year 100) (setq year (+ 2000 year)))
  15174. ;; Check of the date is representable
  15175. (if org-read-date-force-compatible-dates
  15176. (progn
  15177. (if (< year 1970)
  15178. (setq year 1970 org-read-date-analyze-forced-year t))
  15179. (if (> year 2037)
  15180. (setq year 2037 org-read-date-analyze-forced-year t)))
  15181. (condition-case nil
  15182. (ignore (encode-time second minute hour day month year))
  15183. (error
  15184. (setq year (nth 5 org-defdecode))
  15185. (setq org-read-date-analyze-forced-year t))))
  15186. (setq org-read-date-analyze-futurep futurep)
  15187. (list second minute hour day month year)))
  15188. (defvar parse-time-weekdays)
  15189. (defun org-read-date-get-relative (s today default)
  15190. "Check string S for special relative date string.
  15191. TODAY and DEFAULT are internal times, for today and for a default.
  15192. Return shift list (N what def-flag)
  15193. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15194. N is the number of WHATs to shift.
  15195. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15196. the DEFAULT date rather than TODAY."
  15197. (require 'parse-time)
  15198. (when (and
  15199. (string-match
  15200. (concat
  15201. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15202. "\\([0-9]+\\)?"
  15203. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15204. "\\([ \t]\\|$\\)") s)
  15205. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15206. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15207. (string-to-char (substring (match-string 1 s) -1))
  15208. ?+))
  15209. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15210. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15211. (what (if (match-end 3) (match-string 3 s) "d"))
  15212. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15213. (date (if rel default today))
  15214. (wday (nth 6 (decode-time date)))
  15215. delta)
  15216. (if wday1
  15217. (progn
  15218. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15219. (if (= delta 0) (setq delta 7))
  15220. (if (= dir ?-)
  15221. (progn
  15222. (setq delta (- delta 7))
  15223. (if (= delta 0) (setq delta -7))))
  15224. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15225. (list delta "d" rel))
  15226. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15227. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15228. "Turn a user-specified date into the internal representation.
  15229. The internal representation needed by the calendar is (month day year).
  15230. This is a wrapper to handle the brain-dead convention in calendar that
  15231. user function argument order change dependent on argument order."
  15232. (if (boundp 'calendar-date-style)
  15233. (cond
  15234. ((eq calendar-date-style 'american)
  15235. (list arg1 arg2 arg3))
  15236. ((eq calendar-date-style 'european)
  15237. (list arg2 arg1 arg3))
  15238. ((eq calendar-date-style 'iso)
  15239. (list arg2 arg3 arg1)))
  15240. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15241. (if (org-bound-and-true-p european-calendar-style)
  15242. (list arg2 arg1 arg3)
  15243. (list arg1 arg2 arg3)))))
  15244. (defun org-eval-in-calendar (form &optional keepdate)
  15245. "Eval FORM in the calendar window and return to current window.
  15246. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15247. otherwise stick to the current value of `org-ans2'."
  15248. (let ((sf (selected-frame))
  15249. (sw (selected-window)))
  15250. (select-window (get-buffer-window "*Calendar*" t))
  15251. (eval form)
  15252. (when (and (not keepdate) (calendar-cursor-to-date))
  15253. (let* ((date (calendar-cursor-to-date))
  15254. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15255. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15256. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15257. (select-window sw)
  15258. (org-select-frame-set-input-focus sf)))
  15259. (defun org-calendar-select ()
  15260. "Return to `org-read-date' with the date currently selected.
  15261. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15262. (interactive)
  15263. (when (calendar-cursor-to-date)
  15264. (let* ((date (calendar-cursor-to-date))
  15265. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15266. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15267. (if (active-minibuffer-window) (exit-minibuffer))))
  15268. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15269. "Insert a date stamp for the date given by the internal TIME.
  15270. WITH-HM means use the stamp format that includes the time of the day.
  15271. INACTIVE means use square brackets instead of angular ones, so that the
  15272. stamp will not contribute to the agenda.
  15273. PRE and POST are optional strings to be inserted before and after the
  15274. stamp.
  15275. The command returns the inserted time stamp."
  15276. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15277. stamp)
  15278. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15279. (insert-before-markers (or pre ""))
  15280. (when (listp extra)
  15281. (setq extra (car extra))
  15282. (if (and (stringp extra)
  15283. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15284. (setq extra (format "-%02d:%02d"
  15285. (string-to-number (match-string 1 extra))
  15286. (string-to-number (match-string 2 extra))))
  15287. (setq extra nil)))
  15288. (when extra
  15289. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15290. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15291. (insert-before-markers (or post ""))
  15292. (setq org-last-inserted-timestamp stamp)))
  15293. (defun org-toggle-time-stamp-overlays ()
  15294. "Toggle the use of custom time stamp formats."
  15295. (interactive)
  15296. (setq org-display-custom-times (not org-display-custom-times))
  15297. (unless org-display-custom-times
  15298. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15299. (while (setq p (next-single-property-change p 'display))
  15300. (if (and (get-text-property p 'display)
  15301. (eq (get-text-property p 'face) 'org-date))
  15302. (remove-text-properties
  15303. p (setq p (next-single-property-change p 'display))
  15304. '(display t))))
  15305. (set-buffer-modified-p bmp)))
  15306. (if (featurep 'xemacs)
  15307. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15308. (org-restart-font-lock)
  15309. (setq org-table-may-need-update t)
  15310. (if org-display-custom-times
  15311. (message "Time stamps are overlaid with custom format")
  15312. (message "Time stamp overlays removed")))
  15313. (defun org-display-custom-time (beg end)
  15314. "Overlay modified time stamp format over timestamp between BEG and END."
  15315. (let* ((ts (buffer-substring beg end))
  15316. t1 w1 with-hm tf time str w2 (off 0))
  15317. (save-match-data
  15318. (setq t1 (org-parse-time-string ts t))
  15319. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15320. (setq off (- (match-end 0) (match-beginning 0)))))
  15321. (setq end (- end off))
  15322. (setq w1 (- end beg)
  15323. with-hm (and (nth 1 t1) (nth 2 t1))
  15324. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15325. time (org-fix-decoded-time t1)
  15326. str (org-add-props
  15327. (format-time-string
  15328. (substring tf 1 -1) (apply 'encode-time time))
  15329. nil 'mouse-face 'highlight)
  15330. w2 (length str))
  15331. (if (not (= w2 w1))
  15332. (add-text-properties (1+ beg) (+ 2 beg)
  15333. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15334. (if (featurep 'xemacs)
  15335. (progn
  15336. (put-text-property beg end 'invisible t)
  15337. (put-text-property beg end 'end-glyph (make-glyph str)))
  15338. (put-text-property beg end 'display str))))
  15339. (defun org-translate-time (string)
  15340. "Translate all timestamps in STRING to custom format.
  15341. But do this only if the variable `org-display-custom-times' is set."
  15342. (when org-display-custom-times
  15343. (save-match-data
  15344. (let* ((start 0)
  15345. (re org-ts-regexp-both)
  15346. t1 with-hm inactive tf time str beg end)
  15347. (while (setq start (string-match re string start))
  15348. (setq beg (match-beginning 0)
  15349. end (match-end 0)
  15350. t1 (save-match-data
  15351. (org-parse-time-string (substring string beg end) t))
  15352. with-hm (and (nth 1 t1) (nth 2 t1))
  15353. inactive (equal (substring string beg (1+ beg)) "[")
  15354. tf (funcall (if with-hm 'cdr 'car)
  15355. org-time-stamp-custom-formats)
  15356. time (org-fix-decoded-time t1)
  15357. str (format-time-string
  15358. (concat
  15359. (if inactive "[" "<") (substring tf 1 -1)
  15360. (if inactive "]" ">"))
  15361. (apply 'encode-time time))
  15362. string (replace-match str t t string)
  15363. start (+ start (length str)))))))
  15364. string)
  15365. (defun org-fix-decoded-time (time)
  15366. "Set 0 instead of nil for the first 6 elements of time.
  15367. Don't touch the rest."
  15368. (let ((n 0))
  15369. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15370. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15371. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15372. "Difference between TIMESTAMP-STRING and now in days.
  15373. If SECONDS is non-nil, return the difference in seconds."
  15374. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15375. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15376. (funcall fdiff (current-time)))))
  15377. (defun org-deadline-close (timestamp-string &optional ndays)
  15378. "Is the time in TIMESTAMP-STRING close to the current date?"
  15379. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15380. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15381. (not (org-entry-is-done-p))))
  15382. (defun org-get-wdays (ts &optional delay zero-delay)
  15383. "Get the deadline lead time appropriate for timestring TS.
  15384. When DELAY is non-nil, get the delay time for scheduled items
  15385. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15386. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15387. don't try to find the delay cookie in the scheduled timestamp."
  15388. (let ((tv (if delay org-scheduled-delay-days
  15389. org-deadline-warning-days)))
  15390. (cond
  15391. ((or (and delay (< tv 0))
  15392. (and delay zero-delay (<= tv 0))
  15393. (and (not delay) (<= tv 0)))
  15394. ;; Enforce this value no matter what
  15395. (- tv))
  15396. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15397. ;; lead time is specified.
  15398. (floor (* (string-to-number (match-string 1 ts))
  15399. (cdr (assoc (match-string 2 ts)
  15400. '(("d" . 1) ("w" . 7)
  15401. ("m" . 30.4) ("y" . 365.25)
  15402. ("h" . 0.041667)))))))
  15403. ;; go for the default.
  15404. (t tv))))
  15405. (defun org-calendar-select-mouse (ev)
  15406. "Return to `org-read-date' with the date currently selected.
  15407. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15408. (interactive "e")
  15409. (mouse-set-point ev)
  15410. (when (calendar-cursor-to-date)
  15411. (let* ((date (calendar-cursor-to-date))
  15412. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15413. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15414. (if (active-minibuffer-window) (exit-minibuffer))))
  15415. (defun org-check-deadlines (ndays)
  15416. "Check if there are any deadlines due or past due.
  15417. A deadline is considered due if it happens within `org-deadline-warning-days'
  15418. days from today's date. If the deadline appears in an entry marked DONE,
  15419. it is not shown. The prefix arg NDAYS can be used to test that many
  15420. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15421. (interactive "P")
  15422. (let* ((org-warn-days
  15423. (cond
  15424. ((equal ndays '(4)) 100000)
  15425. (ndays (prefix-numeric-value ndays))
  15426. (t (abs org-deadline-warning-days))))
  15427. (case-fold-search nil)
  15428. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15429. (callback
  15430. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15431. (message "%d deadlines past-due or due within %d days"
  15432. (org-occur regexp nil callback)
  15433. org-warn-days)))
  15434. (defsubst org-re-timestamp (type)
  15435. "Return a regexp for timestamp TYPE.
  15436. Allowed values for TYPE are:
  15437. all: all timestamps
  15438. active: only active timestamps (<...>)
  15439. inactive: only inactive timestamps ([...])
  15440. scheduled: only scheduled timestamps
  15441. deadline: only deadline timestamps
  15442. closed: only closed time-stamps
  15443. When TYPE is nil, fall back on returning a regexp that matches
  15444. both scheduled and deadline timestamps."
  15445. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15446. ((eq type 'active) org-ts-regexp)
  15447. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15448. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15449. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15450. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15451. ((eq type 'scheduled-or-deadline)
  15452. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15453. (defun org-check-before-date (date)
  15454. "Check if there are deadlines or scheduled entries before DATE."
  15455. (interactive (list (org-read-date)))
  15456. (let ((case-fold-search nil)
  15457. (regexp (org-re-timestamp org-ts-type))
  15458. (callback
  15459. (lambda () (time-less-p
  15460. (org-time-string-to-time (match-string 1))
  15461. (org-time-string-to-time date)))))
  15462. (message "%d entries before %s"
  15463. (org-occur regexp nil callback) date)))
  15464. (defun org-check-after-date (date)
  15465. "Check if there are deadlines or scheduled entries after DATE."
  15466. (interactive (list (org-read-date)))
  15467. (let ((case-fold-search nil)
  15468. (regexp (org-re-timestamp org-ts-type))
  15469. (callback
  15470. (lambda () (not
  15471. (time-less-p
  15472. (org-time-string-to-time (match-string 1))
  15473. (org-time-string-to-time date))))))
  15474. (message "%d entries after %s"
  15475. (org-occur regexp nil callback) date)))
  15476. (defun org-check-dates-range (start-date end-date)
  15477. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15478. (interactive (list (org-read-date nil nil nil "Range starts")
  15479. (org-read-date nil nil nil "Range end")))
  15480. (let ((case-fold-search nil)
  15481. (regexp (org-re-timestamp org-ts-type))
  15482. (callback
  15483. (lambda ()
  15484. (let ((match (match-string 1)))
  15485. (and
  15486. (not (time-less-p
  15487. (org-time-string-to-time match)
  15488. (org-time-string-to-time start-date)))
  15489. (time-less-p
  15490. (org-time-string-to-time match)
  15491. (org-time-string-to-time end-date)))))))
  15492. (message "%d entries between %s and %s"
  15493. (org-occur regexp nil callback) start-date end-date)))
  15494. (defun org-evaluate-time-range (&optional to-buffer)
  15495. "Evaluate a time range by computing the difference between start and end.
  15496. Normally the result is just printed in the echo area, but with prefix arg
  15497. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15498. If the time range is actually in a table, the result is inserted into the
  15499. next column.
  15500. For time difference computation, a year is assumed to be exactly 365
  15501. days in order to avoid rounding problems."
  15502. (interactive "P")
  15503. (or
  15504. (org-clock-update-time-maybe)
  15505. (save-excursion
  15506. (unless (org-at-date-range-p t)
  15507. (goto-char (point-at-bol))
  15508. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15509. (if (not (org-at-date-range-p t))
  15510. (user-error "Not at a time-stamp range, and none found in current line")))
  15511. (let* ((ts1 (match-string 1))
  15512. (ts2 (match-string 2))
  15513. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15514. (match-end (match-end 0))
  15515. (time1 (org-time-string-to-time ts1))
  15516. (time2 (org-time-string-to-time ts2))
  15517. (t1 (org-float-time time1))
  15518. (t2 (org-float-time time2))
  15519. (diff (abs (- t2 t1)))
  15520. (negative (< (- t2 t1) 0))
  15521. ;; (ys (floor (* 365 24 60 60)))
  15522. (ds (* 24 60 60))
  15523. (hs (* 60 60))
  15524. (fy "%dy %dd %02d:%02d")
  15525. (fy1 "%dy %dd")
  15526. (fd "%dd %02d:%02d")
  15527. (fd1 "%dd")
  15528. (fh "%02d:%02d")
  15529. y d h m align)
  15530. (if havetime
  15531. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15532. y 0
  15533. d (floor (/ diff ds)) diff (mod diff ds)
  15534. h (floor (/ diff hs)) diff (mod diff hs)
  15535. m (floor (/ diff 60)))
  15536. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15537. y 0
  15538. d (floor (+ (/ diff ds) 0.5))
  15539. h 0 m 0))
  15540. (if (not to-buffer)
  15541. (message "%s" (org-make-tdiff-string y d h m))
  15542. (if (org-at-table-p)
  15543. (progn
  15544. (goto-char match-end)
  15545. (setq align t)
  15546. (and (looking-at " *|") (goto-char (match-end 0))))
  15547. (goto-char match-end))
  15548. (if (looking-at
  15549. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15550. (replace-match ""))
  15551. (if negative (insert " -"))
  15552. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15553. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15554. (insert " " (format fh h m))))
  15555. (if align (org-table-align))
  15556. (message "Time difference inserted")))))
  15557. (defun org-make-tdiff-string (y d h m)
  15558. (let ((fmt "")
  15559. (l nil))
  15560. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15561. l (push y l)))
  15562. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15563. l (push d l)))
  15564. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15565. l (push h l)))
  15566. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15567. l (push m l)))
  15568. (apply 'format fmt (nreverse l))))
  15569. (defun org-time-string-to-time (s &optional buffer pos)
  15570. "Convert a timestamp string into internal time."
  15571. (condition-case errdata
  15572. (apply 'encode-time (org-parse-time-string s))
  15573. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15574. s (if (not (and buffer pos))
  15575. ""
  15576. (format " at %d in buffer `%s'" pos buffer))
  15577. (cdr errdata)))))
  15578. (defun org-time-string-to-seconds (s)
  15579. "Convert a timestamp string to a number of seconds."
  15580. (org-float-time (org-time-string-to-time s)))
  15581. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15582. "Convert a time stamp to an absolute day number.
  15583. If there is a specifier for a cyclic time stamp, get the closest
  15584. date to DAYNR.
  15585. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15586. The variable `date' is bound by the calendar when this is called."
  15587. (cond
  15588. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15589. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15590. daynr
  15591. (+ daynr 1000)))
  15592. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15593. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15594. (time-to-days (current-time))) (match-string 0 s)
  15595. prefer show-all))
  15596. (t (time-to-days
  15597. (condition-case errdata
  15598. (apply 'encode-time (org-parse-time-string s))
  15599. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15600. s (if (not (and buffer pos))
  15601. ""
  15602. (format " at %d in buffer `%s'" pos buffer))
  15603. (cdr errdata))))))))
  15604. (defun org-days-to-iso-week (days)
  15605. "Return the iso week number."
  15606. (require 'cal-iso)
  15607. (car (calendar-iso-from-absolute days)))
  15608. (defun org-small-year-to-year (year)
  15609. "Convert 2-digit years into 4-digit years.
  15610. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15611. The year 2000 cannot be abbreviated. Any year larger than 99
  15612. is returned unchanged."
  15613. (if (< year 38)
  15614. (setq year (+ 2000 year))
  15615. (if (< year 100)
  15616. (setq year (+ 1900 year))))
  15617. year)
  15618. (defun org-time-from-absolute (d)
  15619. "Return the time corresponding to date D.
  15620. D may be an absolute day number, or a calendar-type list (month day year)."
  15621. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15622. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15623. (defun org-calendar-holiday ()
  15624. "List of holidays, for Diary display in Org-mode."
  15625. (require 'holidays)
  15626. (let ((hl (funcall
  15627. (if (fboundp 'calendar-check-holidays)
  15628. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15629. (if hl (mapconcat 'identity hl "; "))))
  15630. (defun org-diary-sexp-entry (sexp entry date)
  15631. "Process a SEXP diary ENTRY for DATE."
  15632. (require 'diary-lib)
  15633. (let ((result (if calendar-debug-sexp
  15634. (let ((stack-trace-on-error t))
  15635. (eval (car (read-from-string sexp))))
  15636. (condition-case nil
  15637. (eval (car (read-from-string sexp)))
  15638. (error
  15639. (beep)
  15640. (message "Bad sexp at line %d in %s: %s"
  15641. (org-current-line)
  15642. (buffer-file-name) sexp)
  15643. (sleep-for 2))))))
  15644. (cond ((stringp result) (split-string result "; "))
  15645. ((and (consp result)
  15646. (not (consp (cdr result)))
  15647. (stringp (cdr result))) (cdr result))
  15648. ((and (consp result)
  15649. (stringp (car result))) result)
  15650. (result entry))))
  15651. (defun org-diary-to-ical-string (frombuf)
  15652. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15653. This uses the icalendar.el library."
  15654. (let* ((tmpdir (if (featurep 'xemacs)
  15655. (temp-directory)
  15656. temporary-file-directory))
  15657. (tmpfile (make-temp-name
  15658. (expand-file-name "orgics" tmpdir)))
  15659. buf rtn b e)
  15660. (with-current-buffer frombuf
  15661. (icalendar-export-region (point-min) (point-max) tmpfile)
  15662. (setq buf (find-buffer-visiting tmpfile))
  15663. (set-buffer buf)
  15664. (goto-char (point-min))
  15665. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15666. (setq b (match-beginning 0)))
  15667. (goto-char (point-max))
  15668. (if (re-search-backward "^END:VEVENT" nil t)
  15669. (setq e (match-end 0)))
  15670. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15671. (kill-buffer buf)
  15672. (delete-file tmpfile)
  15673. rtn))
  15674. (defun org-closest-date (start current change prefer show-all)
  15675. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15676. When PREFER is `past', return a date that is either CURRENT or past.
  15677. When PREFER is `future', return a date that is either CURRENT or future.
  15678. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15679. ;; Make the proper lists from the dates
  15680. (catch 'exit
  15681. (let ((a1 '(("h" . hour)
  15682. ("d" . day)
  15683. ("w" . week)
  15684. ("m" . month)
  15685. ("y" . year)))
  15686. (shour (nth 2 (org-parse-time-string start)))
  15687. dn dw sday cday n1 n2 n0
  15688. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15689. (setq start (org-date-to-gregorian start)
  15690. current (org-date-to-gregorian
  15691. (if show-all
  15692. current
  15693. (time-to-days (current-time))))
  15694. sday (calendar-absolute-from-gregorian start)
  15695. cday (calendar-absolute-from-gregorian current))
  15696. (if (<= cday sday) (throw 'exit sday))
  15697. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15698. (setq dn (string-to-number (match-string 1 change))
  15699. dw (cdr (assoc (match-string 2 change) a1)))
  15700. (user-error "Invalid change specifier: %s" change))
  15701. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15702. (cond
  15703. ((eq dw 'hour)
  15704. (let ((missing-hours
  15705. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15706. dn)))
  15707. (setq n1 (if (zerop missing-hours) cday
  15708. (- cday (1+ (floor (/ missing-hours 24)))))
  15709. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15710. ((eq dw 'day)
  15711. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15712. n2 (+ n1 dn)))
  15713. ((eq dw 'year)
  15714. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15715. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15716. (setq date1 (list m d y1)
  15717. n1 (calendar-absolute-from-gregorian date1)
  15718. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15719. n2 (calendar-absolute-from-gregorian date2)))
  15720. ((eq dw 'month)
  15721. ;; approx number of month between the two dates
  15722. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15723. ;; How often does dn fit in there?
  15724. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15725. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15726. m (+ m nm)
  15727. ny (floor (/ m 12))
  15728. y (+ y ny)
  15729. m (- m (* ny 12)))
  15730. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15731. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15732. (setq m2 (+ m dn) y2 y)
  15733. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15734. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15735. (while (<= n2 cday)
  15736. (setq n1 n2 m m2 y y2)
  15737. (setq m2 (+ m dn) y2 y)
  15738. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15739. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15740. ;; Make sure n1 is the earlier date
  15741. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15742. (if show-all
  15743. (cond
  15744. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15745. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15746. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15747. (cond
  15748. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15749. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15750. (t (if (= cday n1) n1 n2)))))))
  15751. (defun org-date-to-gregorian (date)
  15752. "Turn any specification of DATE into a Gregorian date for the calendar."
  15753. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15754. ((and (listp date) (= (length date) 3)) date)
  15755. ((stringp date)
  15756. (setq date (org-parse-time-string date))
  15757. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15758. ((listp date)
  15759. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15760. (defun org-parse-time-string (s &optional nodefault)
  15761. "Parse the standard Org-mode time string.
  15762. This should be a lot faster than the normal `parse-time-string'.
  15763. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15764. hour and minute fields will be nil if not given."
  15765. (cond ((string-match org-ts-regexp0 s)
  15766. (list 0
  15767. (if (or (match-beginning 8) (not nodefault))
  15768. (string-to-number (or (match-string 8 s) "0")))
  15769. (if (or (match-beginning 7) (not nodefault))
  15770. (string-to-number (or (match-string 7 s) "0")))
  15771. (string-to-number (match-string 4 s))
  15772. (string-to-number (match-string 3 s))
  15773. (string-to-number (match-string 2 s))
  15774. nil nil nil))
  15775. ((string-match "^<[^>]+>$" s)
  15776. (decode-time (seconds-to-time (org-matcher-time s))))
  15777. (t (error "Not a standard Org-mode time string: %s" s))))
  15778. (defun org-timestamp-up (&optional arg)
  15779. "Increase the date item at the cursor by one.
  15780. If the cursor is on the year, change the year. If it is on the month,
  15781. the day or the time, change that.
  15782. With prefix ARG, change by that many units."
  15783. (interactive "p")
  15784. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15785. (defun org-timestamp-down (&optional arg)
  15786. "Decrease the date item at the cursor by one.
  15787. If the cursor is on the year, change the year. If it is on the month,
  15788. the day or the time, change that.
  15789. With prefix ARG, change by that many units."
  15790. (interactive "p")
  15791. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15792. (defun org-timestamp-up-day (&optional arg)
  15793. "Increase the date in the time stamp by one day.
  15794. With prefix ARG, change that many days."
  15795. (interactive "p")
  15796. (if (and (not (org-at-timestamp-p t))
  15797. (org-at-heading-p))
  15798. (org-todo 'up)
  15799. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15800. (defun org-timestamp-down-day (&optional arg)
  15801. "Decrease the date in the time stamp by one day.
  15802. With prefix ARG, change that many days."
  15803. (interactive "p")
  15804. (if (and (not (org-at-timestamp-p t))
  15805. (org-at-heading-p))
  15806. (org-todo 'down)
  15807. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15808. (defun org-at-timestamp-p (&optional inactive-ok)
  15809. "Determine if the cursor is in or at a timestamp."
  15810. (interactive)
  15811. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15812. (pos (point))
  15813. (ans (or (looking-at tsr)
  15814. (save-excursion
  15815. (skip-chars-backward "^[<\n\r\t")
  15816. (if (> (point) (point-min)) (backward-char 1))
  15817. (and (looking-at tsr)
  15818. (> (- (match-end 0) pos) -1))))))
  15819. (and ans
  15820. (boundp 'org-ts-what)
  15821. (setq org-ts-what
  15822. (cond
  15823. ((= pos (match-beginning 0)) 'bracket)
  15824. ;; Point is considered to be "on the bracket" whether
  15825. ;; it's really on it or right after it.
  15826. ((= pos (1- (match-end 0))) 'bracket)
  15827. ((= pos (match-end 0)) 'after)
  15828. ((org-pos-in-match-range pos 2) 'year)
  15829. ((org-pos-in-match-range pos 3) 'month)
  15830. ((org-pos-in-match-range pos 7) 'hour)
  15831. ((org-pos-in-match-range pos 8) 'minute)
  15832. ((or (org-pos-in-match-range pos 4)
  15833. (org-pos-in-match-range pos 5)) 'day)
  15834. ((and (> pos (or (match-end 8) (match-end 5)))
  15835. (< pos (match-end 0)))
  15836. (- pos (or (match-end 8) (match-end 5))))
  15837. (t 'day))))
  15838. ans))
  15839. (defun org-toggle-timestamp-type ()
  15840. "Toggle the type (<active> or [inactive]) of a time stamp."
  15841. (interactive)
  15842. (when (org-at-timestamp-p t)
  15843. (let ((beg (match-beginning 0)) (end (match-end 0))
  15844. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15845. (save-excursion
  15846. (goto-char beg)
  15847. (while (re-search-forward "[][<>]" end t)
  15848. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15849. t t)))
  15850. (message "Timestamp is now %sactive"
  15851. (if (equal (char-after beg) ?<) "" "in")))))
  15852. (defun org-at-clock-log-p nil
  15853. "Is the cursor on the clock log line?"
  15854. (save-excursion
  15855. (move-beginning-of-line 1)
  15856. (looking-at "^[ \t]*CLOCK:")))
  15857. (defvar org-clock-history) ; defined in org-clock.el
  15858. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15859. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15860. "Change the date in the time stamp at point.
  15861. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15862. `year', `minute', `second'. If WHAT is not given, the cursor position
  15863. in the timestamp determines what will be changed.
  15864. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15865. (let ((origin (point)) origin-cat
  15866. with-hm inactive
  15867. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15868. org-ts-what
  15869. extra rem
  15870. ts time time0 fixnext clrgx)
  15871. (if (not (org-at-timestamp-p t))
  15872. (user-error "Not at a timestamp"))
  15873. (if (and (not what) (eq org-ts-what 'bracket))
  15874. (org-toggle-timestamp-type)
  15875. ;; Point isn't on brackets. Remember the part of the time-stamp
  15876. ;; the point was in. Indeed, size of time-stamps may change,
  15877. ;; but point must be kept in the same category nonetheless.
  15878. (setq origin-cat org-ts-what)
  15879. (if (and (not what) (not (eq org-ts-what 'day))
  15880. org-display-custom-times
  15881. (get-text-property (point) 'display)
  15882. (not (get-text-property (1- (point)) 'display)))
  15883. (setq org-ts-what 'day))
  15884. (setq org-ts-what (or what org-ts-what)
  15885. inactive (= (char-after (match-beginning 0)) ?\[)
  15886. ts (match-string 0))
  15887. (replace-match "")
  15888. (when (string-match
  15889. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15890. ts)
  15891. (setq extra (match-string 1 ts))
  15892. (if suppress-tmp-delay
  15893. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15894. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15895. (setq with-hm t))
  15896. (setq time0 (org-parse-time-string ts))
  15897. (when (and updown
  15898. (eq org-ts-what 'minute)
  15899. (not current-prefix-arg))
  15900. ;; This looks like s-up and s-down. Change by one rounding step.
  15901. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15902. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15903. (setcar (cdr time0) (+ (nth 1 time0)
  15904. (if (> n 0) (- rem) (- dm rem))))))
  15905. (setq time
  15906. (encode-time (or (car time0) 0)
  15907. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15908. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15909. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15910. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15911. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15912. (nthcdr 6 time0)))
  15913. (when (and (member org-ts-what '(hour minute))
  15914. extra
  15915. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15916. (setq extra (org-modify-ts-extra
  15917. extra
  15918. (if (eq org-ts-what 'hour) 2 5)
  15919. n dm)))
  15920. (when (integerp org-ts-what)
  15921. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15922. (if (eq what 'calendar)
  15923. (let ((cal-date (org-get-date-from-calendar)))
  15924. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15925. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15926. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15927. (setcar time0 (or (car time0) 0))
  15928. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15929. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15930. (setq time (apply 'encode-time time0))))
  15931. ;; Insert the new time-stamp, and ensure point stays in the same
  15932. ;; category as before (i.e. not after the last position in that
  15933. ;; category).
  15934. (let ((pos (point)))
  15935. ;; Stay before inserted string. `save-excursion' is of no use.
  15936. (setq org-last-changed-timestamp
  15937. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15938. (goto-char pos))
  15939. (save-match-data
  15940. (looking-at org-ts-regexp3)
  15941. (goto-char (cond
  15942. ;; `day' category ends before `hour' if any, or at
  15943. ;; the end of the day name.
  15944. ((eq origin-cat 'day)
  15945. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15946. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15947. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15948. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15949. ;; `year' and `month' have both fixed size: point
  15950. ;; couldn't have moved into another part.
  15951. (t origin))))
  15952. ;; Update clock if on a CLOCK line.
  15953. (org-clock-update-time-maybe)
  15954. ;; Maybe adjust the closest clock in `org-clock-history'
  15955. (when org-clock-adjust-closest
  15956. (if (not (and (org-at-clock-log-p)
  15957. (< 1 (length (delq nil (mapcar 'marker-position
  15958. org-clock-history))))))
  15959. (message "No clock to adjust")
  15960. (cond ((save-excursion ; fix previous clock?
  15961. (re-search-backward org-ts-regexp0 nil t)
  15962. (org-looking-back (concat org-clock-string " \\[")))
  15963. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15964. ((save-excursion ; fix next clock?
  15965. (re-search-backward org-ts-regexp0 nil t)
  15966. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15967. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15968. (save-window-excursion
  15969. ;; Find closest clock to point, adjust the previous/next one in history
  15970. (let* ((p (save-excursion (org-back-to-heading t)))
  15971. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15972. (clfixnth
  15973. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15974. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15975. (if (not clfixpos)
  15976. (message "No clock to adjust")
  15977. (save-excursion
  15978. (org-goto-marker-or-bmk clfixpos)
  15979. (org-show-subtree)
  15980. (when (re-search-forward clrgx nil t)
  15981. (goto-char (match-beginning 1))
  15982. (let (org-clock-adjust-closest)
  15983. (org-timestamp-change n org-ts-what updown))
  15984. (message "Clock adjusted in %s for heading: %s"
  15985. (file-name-nondirectory (buffer-file-name))
  15986. (org-get-heading t t)))))))))
  15987. ;; Try to recenter the calendar window, if any.
  15988. (if (and org-calendar-follow-timestamp-change
  15989. (get-buffer-window "*Calendar*" t)
  15990. (memq org-ts-what '(day month year)))
  15991. (org-recenter-calendar (time-to-days time))))))
  15992. (defun org-modify-ts-extra (s pos n dm)
  15993. "Change the different parts of the lead-time and repeat fields in timestamp."
  15994. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15995. ng h m new rem)
  15996. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15997. (cond
  15998. ((or (org-pos-in-match-range pos 2)
  15999. (org-pos-in-match-range pos 3))
  16000. (setq m (string-to-number (match-string 3 s))
  16001. h (string-to-number (match-string 2 s)))
  16002. (if (org-pos-in-match-range pos 2)
  16003. (setq h (+ h n))
  16004. (setq n (* dm (org-no-warnings (signum n))))
  16005. (when (not (= 0 (setq rem (% m dm))))
  16006. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16007. (setq m (+ m n)))
  16008. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16009. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16010. (setq h (min 24 (max 0 h)))
  16011. (setq ng 1 new (format "-%02d:%02d" h m)))
  16012. ((org-pos-in-match-range pos 6)
  16013. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16014. ((org-pos-in-match-range pos 5)
  16015. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16016. ((org-pos-in-match-range pos 9)
  16017. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16018. ((org-pos-in-match-range pos 8)
  16019. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16020. (when ng
  16021. (setq s (concat
  16022. (substring s 0 (match-beginning ng))
  16023. new
  16024. (substring s (match-end ng))))))
  16025. s))
  16026. (defun org-recenter-calendar (date)
  16027. "If the calendar is visible, recenter it to DATE."
  16028. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16029. (when cwin
  16030. (let ((calendar-move-hook nil))
  16031. (with-selected-window cwin
  16032. (calendar-goto-date (if (listp date) date
  16033. (calendar-gregorian-from-absolute date))))))))
  16034. (defun org-goto-calendar (&optional arg)
  16035. "Go to the Emacs calendar at the current date.
  16036. If there is a time stamp in the current line, go to that date.
  16037. A prefix ARG can be used to force the current date."
  16038. (interactive "P")
  16039. (let ((tsr org-ts-regexp) diff
  16040. (calendar-move-hook nil)
  16041. (calendar-view-holidays-initially-flag nil)
  16042. (calendar-view-diary-initially-flag nil))
  16043. (if (or (org-at-timestamp-p)
  16044. (save-excursion
  16045. (beginning-of-line 1)
  16046. (looking-at (concat ".*" tsr))))
  16047. (let ((d1 (time-to-days (current-time)))
  16048. (d2 (time-to-days
  16049. (org-time-string-to-time (match-string 1)))))
  16050. (setq diff (- d2 d1))))
  16051. (calendar)
  16052. (calendar-goto-today)
  16053. (if (and diff (not arg)) (calendar-forward-day diff))))
  16054. (defun org-get-date-from-calendar ()
  16055. "Return a list (month day year) of date at point in calendar."
  16056. (with-current-buffer "*Calendar*"
  16057. (save-match-data
  16058. (calendar-cursor-to-date))))
  16059. (defun org-date-from-calendar ()
  16060. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16061. If there is already a time stamp at the cursor position, update it."
  16062. (interactive)
  16063. (if (org-at-timestamp-p t)
  16064. (org-timestamp-change 0 'calendar)
  16065. (let ((cal-date (org-get-date-from-calendar)))
  16066. (org-insert-time-stamp
  16067. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16068. (defcustom org-effort-durations
  16069. `(("h" . 60)
  16070. ("d" . ,(* 60 8))
  16071. ("w" . ,(* 60 8 5))
  16072. ("m" . ,(* 60 8 5 4))
  16073. ("y" . ,(* 60 8 5 40)))
  16074. "Conversion factor to minutes for an effort modifier.
  16075. Each entry has the form (MODIFIER . MINUTES).
  16076. In an effort string, a number followed by MODIFIER is multiplied
  16077. by the specified number of MINUTES to obtain an effort in
  16078. minutes.
  16079. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16080. effort string \"2hours\" is equivalent to 120 minutes."
  16081. :group 'org-agenda
  16082. :version "24.1"
  16083. :type '(alist :key-type (string :tag "Modifier")
  16084. :value-type (number :tag "Minutes")))
  16085. (defun org-minutes-to-clocksum-string (m)
  16086. "Format number of minutes as a clocksum string.
  16087. The format is determined by `org-time-clocksum-format',
  16088. `org-time-clocksum-use-fractional' and
  16089. `org-time-clocksum-fractional-format' and
  16090. `org-time-clocksum-use-effort-durations'."
  16091. (let ((clocksum "")
  16092. (m (round m)) ; Don't allow fractions of minutes
  16093. h d w mo y fmt n)
  16094. (setq h (if org-time-clocksum-use-effort-durations
  16095. (cdr (assoc "h" org-effort-durations)) 60)
  16096. d (if org-time-clocksum-use-effort-durations
  16097. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16098. w (if org-time-clocksum-use-effort-durations
  16099. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16100. mo (if org-time-clocksum-use-effort-durations
  16101. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16102. y (if org-time-clocksum-use-effort-durations
  16103. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16104. ;; fractional format
  16105. (if org-time-clocksum-use-fractional
  16106. (cond
  16107. ;; single format string
  16108. ((stringp org-time-clocksum-fractional-format)
  16109. (format org-time-clocksum-fractional-format (/ m (float h))))
  16110. ;; choice of fractional formats for different time units
  16111. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16112. (> (/ (truncate m) (* y d h)) 0))
  16113. (format fmt (/ m (* y d (float h)))))
  16114. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16115. (> (/ (truncate m) (* mo d h)) 0))
  16116. (format fmt (/ m (* mo d (float h)))))
  16117. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16118. (> (/ (truncate m) (* w d h)) 0))
  16119. (format fmt (/ m (* w d (float h)))))
  16120. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16121. (> (/ (truncate m) (* d h)) 0))
  16122. (format fmt (/ m (* d (float h)))))
  16123. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16124. (> (/ (truncate m) h) 0))
  16125. (format fmt (/ m (float h))))
  16126. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16127. (format fmt m))
  16128. ;; fall back to smallest time unit with a format
  16129. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16130. (format fmt (/ m (float h))))
  16131. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16132. (format fmt (/ m (* d (float h)))))
  16133. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16134. (format fmt (/ m (* w d (float h)))))
  16135. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16136. (format fmt (/ m (* mo d (float h)))))
  16137. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16138. (format fmt (/ m (* y d (float h))))))
  16139. ;; standard (non-fractional) format, with single format string
  16140. (if (stringp org-time-clocksum-format)
  16141. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16142. ;; separate formats components
  16143. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16144. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16145. (plist-get org-time-clocksum-format :require-years))
  16146. (setq clocksum (concat clocksum (format fmt n))
  16147. m (- m (* n y d h))))
  16148. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16149. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16150. (plist-get org-time-clocksum-format :require-months))
  16151. (setq clocksum (concat clocksum (format fmt n))
  16152. m (- m (* n mo d h))))
  16153. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16154. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16155. (plist-get org-time-clocksum-format :require-weeks))
  16156. (setq clocksum (concat clocksum (format fmt n))
  16157. m (- m (* n w d h))))
  16158. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16159. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16160. (plist-get org-time-clocksum-format :require-days))
  16161. (setq clocksum (concat clocksum (format fmt n))
  16162. m (- m (* n d h))))
  16163. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16164. (or (> (setq n (/ (truncate m) h)) 0)
  16165. (plist-get org-time-clocksum-format :require-hours))
  16166. (setq clocksum (concat clocksum (format fmt n))
  16167. m (- m (* n h))))
  16168. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16169. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16170. (setq clocksum (concat clocksum (format fmt m))))
  16171. ;; return formatted time duration
  16172. clocksum))))
  16173. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16174. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16175. "Org mode version 8.0")
  16176. (defun org-hours-to-clocksum-string (n)
  16177. (org-minutes-to-clocksum-string (* n 60)))
  16178. (defun org-hh:mm-string-to-minutes (s)
  16179. "Convert a string H:MM to a number of minutes.
  16180. If the string is just a number, interpret it as minutes.
  16181. In fact, the first hh:mm or number in the string will be taken,
  16182. there can be extra stuff in the string.
  16183. If no number is found, the return value is 0."
  16184. (cond
  16185. ((integerp s) s)
  16186. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16187. (+ (* (string-to-number (match-string 1 s)) 60)
  16188. (string-to-number (match-string 2 s))))
  16189. ((string-match "\\([0-9]+\\)" s)
  16190. (string-to-number (match-string 1 s)))
  16191. (t 0)))
  16192. (defcustom org-image-actual-width t
  16193. "Should we use the actual width of images when inlining them?
  16194. When set to `t', always use the image width.
  16195. When set to a number, use imagemagick (when available) to set
  16196. the image's width to this value.
  16197. When set to a number in a list, try to get the width from any
  16198. #+ATTR.* keyword if it matches a width specification like
  16199. #+ATTR_HTML: :width 300px
  16200. and fall back on that number if none is found.
  16201. When set to nil, try to get the width from an #+ATTR.* keyword
  16202. and fall back on the original width if none is found.
  16203. This requires Emacs >= 24.1, build with imagemagick support."
  16204. :group 'org-appearance
  16205. :version "24.4"
  16206. :package-version '(Org . "8.0")
  16207. :type '(choice
  16208. (const :tag "Use the image width" t)
  16209. (integer :tag "Use a number of pixels")
  16210. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16211. (const :tag "Use #+ATTR* or don't resize" nil)))
  16212. (defcustom org-agenda-inhibit-startup nil
  16213. "Inhibit startup when preparing agenda buffers.
  16214. When this variable is `t' (the default), the initialization of
  16215. the Org agenda buffers is inhibited: e.g. the visibility state
  16216. is not set, the tables are not re-aligned, etc."
  16217. :type 'boolean
  16218. :version "24.3"
  16219. :group 'org-agenda)
  16220. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16221. "Convert a duration string S to minutes.
  16222. A bare number is interpreted as minutes, modifiers can be set by
  16223. customizing `org-effort-durations' (which see).
  16224. Entries containing a colon are interpreted as H:MM by
  16225. `org-hh:mm-string-to-minutes'."
  16226. (let ((result 0)
  16227. (re (concat "\\([0-9.]+\\) *\\("
  16228. (regexp-opt (mapcar 'car org-effort-durations))
  16229. "\\)")))
  16230. (while (string-match re s)
  16231. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16232. (string-to-number (match-string 1 s))))
  16233. (setq s (replace-match "" nil t s)))
  16234. (setq result (floor result))
  16235. (incf result (org-hh:mm-string-to-minutes s))
  16236. (if output-to-string (number-to-string result) result)))
  16237. ;;;; Files
  16238. (defun org-save-all-org-buffers ()
  16239. "Save all Org-mode buffers without user confirmation."
  16240. (interactive)
  16241. (message "Saving all Org-mode buffers...")
  16242. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16243. (when (featurep 'org-id) (org-id-locations-save))
  16244. (message "Saving all Org-mode buffers... done"))
  16245. (defun org-revert-all-org-buffers ()
  16246. "Revert all Org-mode buffers.
  16247. Prompt for confirmation when there are unsaved changes.
  16248. Be sure you know what you are doing before letting this function
  16249. overwrite your changes.
  16250. This function is useful in a setup where one tracks org files
  16251. with a version control system, to revert on one machine after pulling
  16252. changes from another. I believe the procedure must be like this:
  16253. 1. M-x org-save-all-org-buffers
  16254. 2. Pull changes from the other machine, resolve conflicts
  16255. 3. M-x org-revert-all-org-buffers"
  16256. (interactive)
  16257. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16258. (user-error "Abort"))
  16259. (save-excursion
  16260. (save-window-excursion
  16261. (mapc
  16262. (lambda (b)
  16263. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16264. (with-current-buffer b buffer-file-name))
  16265. (org-pop-to-buffer-same-window b)
  16266. (revert-buffer t 'no-confirm)))
  16267. (buffer-list))
  16268. (when (and (featurep 'org-id) org-id-track-globally)
  16269. (org-id-locations-load)))))
  16270. ;;;; Agenda files
  16271. ;;;###autoload
  16272. (defun org-switchb (&optional arg)
  16273. "Switch between Org buffers.
  16274. With one prefix argument, restrict available buffers to files.
  16275. With two prefix arguments, restrict available buffers to agenda files.
  16276. Defaults to `iswitchb' for buffer name completion.
  16277. Set `org-completion-use-ido' to make it use ido instead."
  16278. (interactive "P")
  16279. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16280. ((equal arg '(16)) (org-buffer-list 'agenda))
  16281. (t (org-buffer-list))))
  16282. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16283. (org-completion-use-ido org-completion-use-ido))
  16284. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16285. (setq org-completion-use-iswitchb t))
  16286. (org-pop-to-buffer-same-window
  16287. (org-icompleting-read "Org buffer: "
  16288. (mapcar 'list (mapcar 'buffer-name blist))
  16289. nil t))))
  16290. ;;; Define some older names previously used for this functionality
  16291. ;;;###autoload
  16292. (defalias 'org-ido-switchb 'org-switchb)
  16293. ;;;###autoload
  16294. (defalias 'org-iswitchb 'org-switchb)
  16295. (defun org-buffer-list (&optional predicate exclude-tmp)
  16296. "Return a list of Org buffers.
  16297. PREDICATE can be `export', `files' or `agenda'.
  16298. export restrict the list to Export buffers.
  16299. files restrict the list to buffers visiting Org files.
  16300. agenda restrict the list to buffers visiting agenda files.
  16301. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16302. (let* ((bfn nil)
  16303. (agenda-files (and (eq predicate 'agenda)
  16304. (mapcar 'file-truename (org-agenda-files t))))
  16305. (filter
  16306. (cond
  16307. ((eq predicate 'files)
  16308. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16309. ((eq predicate 'export)
  16310. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16311. ((eq predicate 'agenda)
  16312. (lambda (b)
  16313. (with-current-buffer b
  16314. (and (derived-mode-p 'org-mode)
  16315. (setq bfn (buffer-file-name b))
  16316. (member (file-truename bfn) agenda-files)))))
  16317. (t (lambda (b) (with-current-buffer b
  16318. (or (derived-mode-p 'org-mode)
  16319. (string-match "\*Org .*Export"
  16320. (buffer-name b)))))))))
  16321. (delq nil
  16322. (mapcar
  16323. (lambda(b)
  16324. (if (and (funcall filter b)
  16325. (or (not exclude-tmp)
  16326. (not (string-match "tmp" (buffer-name b)))))
  16327. b
  16328. nil))
  16329. (buffer-list)))))
  16330. (defun org-agenda-files (&optional unrestricted archives)
  16331. "Get the list of agenda files.
  16332. Optional UNRESTRICTED means return the full list even if a restriction
  16333. is currently in place.
  16334. When ARCHIVES is t, include all archive files that are really being
  16335. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16336. `org-agenda-archives-mode' is t."
  16337. (let ((files
  16338. (cond
  16339. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16340. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16341. ((listp org-agenda-files) org-agenda-files)
  16342. (t (error "Invalid value of `org-agenda-files'")))))
  16343. (setq files (apply 'append
  16344. (mapcar (lambda (f)
  16345. (if (file-directory-p f)
  16346. (directory-files
  16347. f t org-agenda-file-regexp)
  16348. (list f)))
  16349. files)))
  16350. (when org-agenda-skip-unavailable-files
  16351. (setq files (delq nil
  16352. (mapcar (function
  16353. (lambda (file)
  16354. (and (file-readable-p file) file)))
  16355. files))))
  16356. (when (or (eq archives t)
  16357. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16358. (setq files (org-add-archive-files files)))
  16359. files))
  16360. (defun org-agenda-file-p (&optional file)
  16361. "Return non-nil, if FILE is an agenda file.
  16362. If FILE is omitted, use the file associated with the current
  16363. buffer."
  16364. (member (or file (buffer-file-name))
  16365. (org-agenda-files t)))
  16366. (defun org-edit-agenda-file-list ()
  16367. "Edit the list of agenda files.
  16368. Depending on setup, this either uses customize to edit the variable
  16369. `org-agenda-files', or it visits the file that is holding the list. In the
  16370. latter case, the buffer is set up in a way that saving it automatically kills
  16371. the buffer and restores the previous window configuration."
  16372. (interactive)
  16373. (if (stringp org-agenda-files)
  16374. (let ((cw (current-window-configuration)))
  16375. (find-file org-agenda-files)
  16376. (org-set-local 'org-window-configuration cw)
  16377. (org-add-hook 'after-save-hook
  16378. (lambda ()
  16379. (set-window-configuration
  16380. (prog1 org-window-configuration
  16381. (kill-buffer (current-buffer))))
  16382. (org-install-agenda-files-menu)
  16383. (message "New agenda file list installed"))
  16384. nil 'local)
  16385. (message "%s" (substitute-command-keys
  16386. "Edit list and finish with \\[save-buffer]")))
  16387. (customize-variable 'org-agenda-files)))
  16388. (defun org-store-new-agenda-file-list (list)
  16389. "Set new value for the agenda file list and save it correctly."
  16390. (if (stringp org-agenda-files)
  16391. (let ((fe (org-read-agenda-file-list t)) b u)
  16392. (while (setq b (find-buffer-visiting org-agenda-files))
  16393. (kill-buffer b))
  16394. (with-temp-file org-agenda-files
  16395. (insert
  16396. (mapconcat
  16397. (lambda (f) ;; Keep un-expanded entries.
  16398. (if (setq u (assoc f fe))
  16399. (cdr u)
  16400. f))
  16401. list "\n")
  16402. "\n")))
  16403. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16404. (org-insert-mode-line-in-empty-file nil))
  16405. (setq org-agenda-files list)
  16406. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16407. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16408. "Read the list of agenda files from a file.
  16409. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16410. filenames, used by `org-store-new-agenda-file-list' to write back
  16411. un-expanded file names."
  16412. (when (file-directory-p org-agenda-files)
  16413. (error "`org-agenda-files' cannot be a single directory"))
  16414. (when (stringp org-agenda-files)
  16415. (with-temp-buffer
  16416. (insert-file-contents org-agenda-files)
  16417. (mapcar
  16418. (lambda (f)
  16419. (let ((e (expand-file-name (substitute-in-file-name f)
  16420. org-directory)))
  16421. (if pair-with-expansion
  16422. (cons e f)
  16423. e)))
  16424. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16425. ;;;###autoload
  16426. (defun org-cycle-agenda-files ()
  16427. "Cycle through the files in `org-agenda-files'.
  16428. If the current buffer visits an agenda file, find the next one in the list.
  16429. If the current buffer does not, find the first agenda file."
  16430. (interactive)
  16431. (let* ((fs (org-agenda-files t))
  16432. (files (append fs (list (car fs))))
  16433. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16434. file)
  16435. (unless files (user-error "No agenda files"))
  16436. (catch 'exit
  16437. (while (setq file (pop files))
  16438. (if (equal (file-truename file) tcf)
  16439. (when (car files)
  16440. (find-file (car files))
  16441. (throw 'exit t))))
  16442. (find-file (car fs)))
  16443. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16444. (defun org-agenda-file-to-front (&optional to-end)
  16445. "Move/add the current file to the top of the agenda file list.
  16446. If the file is not present in the list, it is added to the front. If it is
  16447. present, it is moved there. With optional argument TO-END, add/move to the
  16448. end of the list."
  16449. (interactive "P")
  16450. (let ((org-agenda-skip-unavailable-files nil)
  16451. (file-alist (mapcar (lambda (x)
  16452. (cons (file-truename x) x))
  16453. (org-agenda-files t)))
  16454. (ctf (file-truename
  16455. (or buffer-file-name
  16456. (user-error "Please save the current buffer to a file"))))
  16457. x had)
  16458. (setq x (assoc ctf file-alist) had x)
  16459. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16460. (if to-end
  16461. (setq file-alist (append (delq x file-alist) (list x)))
  16462. (setq file-alist (cons x (delq x file-alist))))
  16463. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16464. (org-install-agenda-files-menu)
  16465. (message "File %s to %s of agenda file list"
  16466. (if had "moved" "added") (if to-end "end" "front"))))
  16467. (defun org-remove-file (&optional file)
  16468. "Remove current file from the list of files in variable `org-agenda-files'.
  16469. These are the files which are being checked for agenda entries.
  16470. Optional argument FILE means use this file instead of the current."
  16471. (interactive)
  16472. (let* ((org-agenda-skip-unavailable-files nil)
  16473. (file (or file buffer-file-name
  16474. (user-error "Current buffer does not visit a file")))
  16475. (true-file (file-truename file))
  16476. (afile (abbreviate-file-name file))
  16477. (files (delq nil (mapcar
  16478. (lambda (x)
  16479. (if (equal true-file
  16480. (file-truename x))
  16481. nil x))
  16482. (org-agenda-files t)))))
  16483. (if (not (= (length files) (length (org-agenda-files t))))
  16484. (progn
  16485. (org-store-new-agenda-file-list files)
  16486. (org-install-agenda-files-menu)
  16487. (message "Removed file: %s" afile))
  16488. (message "File was not in list: %s (not removed)" afile))))
  16489. (defun org-file-menu-entry (file)
  16490. (vector file (list 'find-file file) t))
  16491. (defun org-check-agenda-file (file)
  16492. "Make sure FILE exists. If not, ask user what to do."
  16493. (when (not (file-exists-p file))
  16494. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16495. (abbreviate-file-name file))
  16496. (let ((r (downcase (read-char-exclusive))))
  16497. (cond
  16498. ((equal r ?r)
  16499. (org-remove-file file)
  16500. (throw 'nextfile t))
  16501. (t (error "Abort"))))))
  16502. (defun org-get-agenda-file-buffer (file)
  16503. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16504. it to the list of buffers which might be released later."
  16505. (let ((buf (org-find-base-buffer-visiting file)))
  16506. (if buf
  16507. buf ; just return it
  16508. ;; Make a new buffer and remember it
  16509. (setq buf (find-file-noselect file))
  16510. (if buf (push buf org-agenda-new-buffers))
  16511. buf)))
  16512. (defun org-release-buffers (blist)
  16513. "Release all buffers in list, asking the user for confirmation when needed.
  16514. When a buffer is unmodified, it is just killed. When modified, it is saved
  16515. \(if the user agrees) and then killed."
  16516. (let (buf file)
  16517. (while (setq buf (pop blist))
  16518. (setq file (buffer-file-name buf))
  16519. (when (and (buffer-modified-p buf)
  16520. file
  16521. (y-or-n-p (format "Save file %s? " file)))
  16522. (with-current-buffer buf (save-buffer)))
  16523. (kill-buffer buf))))
  16524. (defun org-agenda-prepare-buffers (files)
  16525. "Create buffers for all agenda files, protect archived trees and comments."
  16526. (interactive)
  16527. (let ((pa '(:org-archived t))
  16528. (pc '(:org-comment t))
  16529. (pall '(:org-archived t :org-comment t))
  16530. (inhibit-read-only t)
  16531. (org-inhibit-startup org-agenda-inhibit-startup)
  16532. (rea (concat ":" org-archive-tag ":"))
  16533. file re)
  16534. (setq org-tag-alist-for-agenda nil
  16535. org-tag-groups-alist-for-agenda nil)
  16536. (save-excursion
  16537. (save-restriction
  16538. (while (setq file (pop files))
  16539. (catch 'nextfile
  16540. (if (bufferp file)
  16541. (set-buffer file)
  16542. (org-check-agenda-file file)
  16543. (set-buffer (org-get-agenda-file-buffer file)))
  16544. (widen)
  16545. (org-set-regexps-and-options-for-tags)
  16546. (goto-char (point-min))
  16547. (let ((case-fold-search t))
  16548. (when (search-forward "#+setupfile" nil t)
  16549. ;; Don't set all regexps and options systematically as
  16550. ;; this is only run for setting agenda tags from setup
  16551. ;; file
  16552. (org-set-regexps-and-options)))
  16553. (org-refresh-category-properties)
  16554. (org-refresh-properties org-effort-property 'org-effort)
  16555. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16556. (setq org-todo-keywords-for-agenda
  16557. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16558. (setq org-done-keywords-for-agenda
  16559. (append org-done-keywords-for-agenda org-done-keywords))
  16560. (setq org-todo-keyword-alist-for-agenda
  16561. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16562. (setq org-drawers-for-agenda
  16563. (append org-drawers-for-agenda org-drawers))
  16564. (setq org-tag-alist-for-agenda
  16565. (org-uniquify
  16566. (append org-tag-alist-for-agenda
  16567. org-tag-alist
  16568. org-tag-persistent-alist)))
  16569. (if org-group-tags
  16570. (setq org-tag-groups-alist-for-agenda
  16571. (org-uniquify-alist
  16572. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16573. (org-with-silent-modifications
  16574. (save-excursion
  16575. (remove-text-properties (point-min) (point-max) pall)
  16576. (when org-agenda-skip-archived-trees
  16577. (goto-char (point-min))
  16578. (while (re-search-forward rea nil t)
  16579. (if (org-at-heading-p t)
  16580. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16581. (goto-char (point-min))
  16582. (setq re (format org-heading-keyword-regexp-format
  16583. org-comment-string))
  16584. (while (re-search-forward re nil t)
  16585. (add-text-properties
  16586. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16587. (setq org-todo-keywords-for-agenda
  16588. (org-uniquify org-todo-keywords-for-agenda))
  16589. (setq org-todo-keyword-alist-for-agenda
  16590. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16591. ;;;; CDLaTeX minor mode
  16592. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16593. "Keymap for the minor `org-cdlatex-mode'.")
  16594. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16595. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16596. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16597. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16598. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16599. (defvar org-cdlatex-texmathp-advice-is-done nil
  16600. "Flag remembering if we have applied the advice to texmathp already.")
  16601. (define-minor-mode org-cdlatex-mode
  16602. "Toggle the minor `org-cdlatex-mode'.
  16603. This mode supports entering LaTeX environment and math in LaTeX fragments
  16604. in Org-mode.
  16605. \\{org-cdlatex-mode-map}"
  16606. nil " OCDL" nil
  16607. (when org-cdlatex-mode
  16608. (require 'cdlatex)
  16609. (run-hooks 'cdlatex-mode-hook)
  16610. (cdlatex-compute-tables))
  16611. (unless org-cdlatex-texmathp-advice-is-done
  16612. (setq org-cdlatex-texmathp-advice-is-done t)
  16613. (defadvice texmathp (around org-math-always-on activate)
  16614. "Always return t in org-mode buffers.
  16615. This is because we want to insert math symbols without dollars even outside
  16616. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16617. an embedded LaTeX fragment, let texmathp do its job.
  16618. \\[org-cdlatex-mode-map]"
  16619. (interactive)
  16620. (let (p)
  16621. (cond
  16622. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16623. ((eq this-command 'cdlatex-math-symbol)
  16624. (setq ad-return-value t
  16625. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16626. (t
  16627. (let ((p (org-inside-LaTeX-fragment-p)))
  16628. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16629. (setq ad-return-value t
  16630. texmathp-why '("Org-mode embedded math" . 0))
  16631. (if p ad-do-it)))))))))
  16632. (defun turn-on-org-cdlatex ()
  16633. "Unconditionally turn on `org-cdlatex-mode'."
  16634. (org-cdlatex-mode 1))
  16635. (defun org-try-cdlatex-tab ()
  16636. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16637. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16638. - inside a LaTeX fragment, or
  16639. - after the first word in a line, where an abbreviation expansion could
  16640. insert a LaTeX environment."
  16641. (when org-cdlatex-mode
  16642. (cond
  16643. ;; Before any word on the line: No expansion possible.
  16644. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16645. ;; Just after first word on the line: Expand it. Make sure it
  16646. ;; cannot happen on headlines, though.
  16647. ((save-excursion
  16648. (skip-chars-backward "a-zA-Z0-9*")
  16649. (skip-chars-backward " \t")
  16650. (and (bolp) (not (org-at-heading-p))))
  16651. (cdlatex-tab) t)
  16652. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16653. (defun org-cdlatex-underscore-caret (&optional arg)
  16654. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16655. Revert to the normal definition outside of these fragments."
  16656. (interactive "P")
  16657. (if (org-inside-LaTeX-fragment-p)
  16658. (call-interactively 'cdlatex-sub-superscript)
  16659. (let (org-cdlatex-mode)
  16660. (call-interactively (key-binding (vector last-input-event))))))
  16661. (defun org-cdlatex-math-modify (&optional arg)
  16662. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16663. Revert to the normal definition outside of these fragments."
  16664. (interactive "P")
  16665. (if (org-inside-LaTeX-fragment-p)
  16666. (call-interactively 'cdlatex-math-modify)
  16667. (let (org-cdlatex-mode)
  16668. (call-interactively (key-binding (vector last-input-event))))))
  16669. ;;;; LaTeX fragments
  16670. (defvar org-latex-regexps
  16671. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16672. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16673. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16674. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16675. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16676. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16677. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16678. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16679. "Regular expressions for matching embedded LaTeX.")
  16680. (defun org-inside-LaTeX-fragment-p ()
  16681. "Test if point is inside a LaTeX fragment.
  16682. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16683. sequence appearing also before point.
  16684. Even though the matchers for math are configurable, this function assumes
  16685. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16686. delimiters are skipped when they have been removed by customization.
  16687. The return value is nil, or a cons cell with the delimiter and the
  16688. position of this delimiter.
  16689. This function does a reasonably good job, but can locally be fooled by
  16690. for example currency specifications. For example it will assume being in
  16691. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16692. fragments that are properly closed, but during editing, we have to live
  16693. with the uncertainty caused by missing closing delimiters. This function
  16694. looks only before point, not after."
  16695. (catch 'exit
  16696. (let ((pos (point))
  16697. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16698. (lim (progn
  16699. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16700. (point)))
  16701. dd-on str (start 0) m re)
  16702. (goto-char pos)
  16703. (when dodollar
  16704. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16705. re (nth 1 (assoc "$" org-latex-regexps)))
  16706. (while (string-match re str start)
  16707. (cond
  16708. ((= (match-end 0) (length str))
  16709. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16710. ((= (match-end 0) (- (length str) 5))
  16711. (throw 'exit nil))
  16712. (t (setq start (match-end 0))))))
  16713. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16714. (goto-char pos)
  16715. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16716. (and (match-beginning 2) (throw 'exit nil))
  16717. ;; count $$
  16718. (while (re-search-backward "\\$\\$" lim t)
  16719. (setq dd-on (not dd-on)))
  16720. (goto-char pos)
  16721. (if dd-on (cons "$$" m))))))
  16722. (defun org-inside-latex-macro-p ()
  16723. "Is point inside a LaTeX macro or its arguments?"
  16724. (save-match-data
  16725. (org-in-regexp
  16726. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16727. (defvar org-latex-fragment-image-overlays nil
  16728. "List of overlays carrying the images of latex fragments.")
  16729. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16730. (defun org-remove-latex-fragment-image-overlays ()
  16731. "Remove all overlays with LaTeX fragment images in current buffer."
  16732. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16733. (setq org-latex-fragment-image-overlays nil))
  16734. (defun org-preview-latex-fragment (&optional subtree)
  16735. "Preview the LaTeX fragment at point, or all locally or globally.
  16736. If the cursor is in a LaTeX fragment, create the image and overlay
  16737. it over the source code. If there is no fragment at point, display
  16738. all fragments in the current text, from one headline to the next. With
  16739. prefix SUBTREE, display all fragments in the current subtree. With a
  16740. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16741. the cursor is before the first headline,
  16742. display all fragments in the buffer.
  16743. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16744. (interactive "P")
  16745. (unless buffer-file-name
  16746. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16747. (when (display-graphic-p)
  16748. (org-remove-latex-fragment-image-overlays)
  16749. (save-excursion
  16750. (save-restriction
  16751. (let (beg end at msg)
  16752. (cond
  16753. ((or (equal subtree '(16))
  16754. (not (save-excursion
  16755. (re-search-backward org-outline-regexp-bol nil t))))
  16756. (setq beg (point-min) end (point-max)
  16757. msg "Creating images for buffer...%s"))
  16758. ((equal subtree '(4))
  16759. (org-back-to-heading)
  16760. (setq beg (point) end (org-end-of-subtree t)
  16761. msg "Creating images for subtree...%s"))
  16762. (t
  16763. (if (setq at (org-inside-LaTeX-fragment-p))
  16764. (goto-char (max (point-min) (- (cdr at) 2)))
  16765. (org-back-to-heading))
  16766. (setq beg (point) end (progn (outline-next-heading) (point))
  16767. msg (if at "Creating image...%s"
  16768. "Creating images for entry...%s"))))
  16769. (message msg "")
  16770. (narrow-to-region beg end)
  16771. (goto-char beg)
  16772. (org-format-latex
  16773. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16774. (file-name-nondirectory
  16775. buffer-file-name)))
  16776. default-directory 'overlays msg at 'forbuffer
  16777. org-latex-create-formula-image-program)
  16778. (message msg "done. Use `C-c C-c' to remove images."))))))
  16779. (defun org-format-latex (prefix &optional dir overlays msg at
  16780. forbuffer processing-type)
  16781. "Replace LaTeX fragments with links to an image, and produce images.
  16782. Some of the options can be changed using the variable
  16783. `org-format-latex-options'."
  16784. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16785. (let* ((prefixnodir (file-name-nondirectory prefix))
  16786. (absprefix (expand-file-name prefix dir))
  16787. (todir (file-name-directory absprefix))
  16788. (opt org-format-latex-options)
  16789. (optnew org-format-latex-options)
  16790. (matchers (plist-get opt :matchers))
  16791. (re-list org-latex-regexps)
  16792. (cnt 0) txt hash link beg end re e checkdir
  16793. string
  16794. m n block-type block linkfile movefile ov)
  16795. ;; Check the different regular expressions
  16796. (while (setq e (pop re-list))
  16797. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16798. block (if block-type "\n\n" ""))
  16799. (when (member m matchers)
  16800. (goto-char (point-min))
  16801. (while (re-search-forward re nil t)
  16802. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16803. (or (not overlays)
  16804. (not (eq (get-char-property (match-beginning n)
  16805. 'org-overlay-type)
  16806. 'org-latex-overlay))))
  16807. (cond
  16808. ((eq processing-type 'verbatim))
  16809. ((eq processing-type 'mathjax)
  16810. ;; Prepare for MathJax processing.
  16811. (setq string (match-string n))
  16812. (when (member m '("$" "$1"))
  16813. (save-excursion
  16814. (delete-region (match-beginning n) (match-end n))
  16815. (goto-char (match-beginning n))
  16816. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16817. ((or (eq processing-type 'dvipng)
  16818. (eq processing-type 'imagemagick))
  16819. ;; Process to an image.
  16820. (setq txt (match-string n)
  16821. beg (match-beginning n) end (match-end n)
  16822. cnt (1+ cnt))
  16823. (let ((face (face-at-point))
  16824. (fg (plist-get opt :foreground))
  16825. (bg (plist-get opt :background))
  16826. ;; Ensure full list is printed.
  16827. print-length print-level)
  16828. (when forbuffer
  16829. ;; Get the colors from the face at point.
  16830. (goto-char beg)
  16831. (when (eq fg 'auto)
  16832. (setq fg (face-attribute face :foreground nil 'default)))
  16833. (when (eq bg 'auto)
  16834. (setq bg (face-attribute face :background nil 'default)))
  16835. (setq optnew (copy-sequence opt))
  16836. (plist-put optnew :foreground fg)
  16837. (plist-put optnew :background bg))
  16838. (setq hash (sha1 (prin1-to-string
  16839. (list org-format-latex-header
  16840. org-latex-default-packages-alist
  16841. org-latex-packages-alist
  16842. org-format-latex-options
  16843. forbuffer txt fg bg)))
  16844. linkfile (format "%s_%s.png" prefix hash)
  16845. movefile (format "%s_%s.png" absprefix hash)))
  16846. (setq link (concat block "[[file:" linkfile "]]" block))
  16847. (if msg (message msg cnt))
  16848. (goto-char beg)
  16849. (unless checkdir ; Ensure the directory exists.
  16850. (setq checkdir t)
  16851. (or (file-directory-p todir) (make-directory todir t)))
  16852. (unless (file-exists-p movefile)
  16853. (org-create-formula-image
  16854. txt movefile optnew forbuffer processing-type))
  16855. (if overlays
  16856. (progn
  16857. (mapc (lambda (o)
  16858. (if (eq (overlay-get o 'org-overlay-type)
  16859. 'org-latex-overlay)
  16860. (delete-overlay o)))
  16861. (overlays-in beg end))
  16862. (setq ov (make-overlay beg end))
  16863. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16864. (if (featurep 'xemacs)
  16865. (progn
  16866. (overlay-put ov 'invisible t)
  16867. (overlay-put
  16868. ov 'end-glyph
  16869. (make-glyph (vector 'png :file movefile))))
  16870. (overlay-put
  16871. ov 'display
  16872. (list 'image :type 'png :file movefile :ascent 'center)))
  16873. (push ov org-latex-fragment-image-overlays)
  16874. (goto-char end))
  16875. (delete-region beg end)
  16876. (insert (org-add-props link
  16877. (list 'org-latex-src
  16878. (replace-regexp-in-string
  16879. "\"" "" txt)
  16880. 'org-latex-src-embed-type
  16881. (if block-type 'paragraph 'character))))))
  16882. ((eq processing-type 'mathml)
  16883. ;; Process to MathML
  16884. (unless (save-match-data (org-format-latex-mathml-available-p))
  16885. (user-error "LaTeX to MathML converter not configured"))
  16886. (setq txt (match-string n)
  16887. beg (match-beginning n) end (match-end n)
  16888. cnt (1+ cnt))
  16889. (if msg (message msg cnt))
  16890. (goto-char beg)
  16891. (delete-region beg end)
  16892. (insert (org-format-latex-as-mathml
  16893. txt block-type prefix dir)))
  16894. (t
  16895. (error "Unknown conversion type %s for LaTeX fragments"
  16896. processing-type)))))))))
  16897. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16898. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16899. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16900. sucessful, return the portion between \"<math...> </math>\"
  16901. elements otherwise return nil. When MATHML-FILE is specified,
  16902. write the results in to that file. When invoked as an
  16903. interactive command, prompt for LATEX-FRAG, with initial value
  16904. set to the current active region and echo the results for user
  16905. inspection."
  16906. (interactive (list (let ((frag (when (org-region-active-p)
  16907. (buffer-substring-no-properties
  16908. (region-beginning) (region-end)))))
  16909. (read-string "LaTeX Fragment: " frag nil frag))))
  16910. (unless latex-frag (error "Invalid LaTeX fragment"))
  16911. (let* ((tmp-in-file (file-relative-name
  16912. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16913. (ignore (write-region latex-frag nil tmp-in-file))
  16914. (tmp-out-file (file-relative-name
  16915. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16916. (cmd (format-spec
  16917. org-latex-to-mathml-convert-command
  16918. `((?j . ,(shell-quote-argument
  16919. (expand-file-name org-latex-to-mathml-jar-file)))
  16920. (?I . ,(shell-quote-argument tmp-in-file))
  16921. (?o . ,(shell-quote-argument tmp-out-file)))))
  16922. mathml shell-command-output)
  16923. (when (org-called-interactively-p 'any)
  16924. (unless (org-format-latex-mathml-available-p)
  16925. (user-error "LaTeX to MathML converter not configured")))
  16926. (message "Running %s" cmd)
  16927. (setq shell-command-output (shell-command-to-string cmd))
  16928. (setq mathml
  16929. (when (file-readable-p tmp-out-file)
  16930. (with-current-buffer (find-file-noselect tmp-out-file t)
  16931. (goto-char (point-min))
  16932. (when (re-search-forward
  16933. (concat
  16934. (regexp-quote
  16935. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16936. "\\(.\\|\n\\)*"
  16937. (regexp-quote "</math>")) nil t)
  16938. (prog1 (match-string 0) (kill-buffer))))))
  16939. (cond
  16940. (mathml
  16941. (setq mathml
  16942. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16943. (when mathml-file
  16944. (write-region mathml nil mathml-file))
  16945. (when (org-called-interactively-p 'any)
  16946. (message mathml)))
  16947. ((message "LaTeX to MathML conversion failed")
  16948. (message shell-command-output)))
  16949. (delete-file tmp-in-file)
  16950. (when (file-exists-p tmp-out-file)
  16951. (delete-file tmp-out-file))
  16952. mathml))
  16953. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16954. prefix &optional dir)
  16955. "Use `org-create-math-formula' but check local cache first."
  16956. (let* ((absprefix (expand-file-name prefix dir))
  16957. (print-length nil) (print-level nil)
  16958. (formula-id (concat
  16959. "formula-"
  16960. (sha1
  16961. (prin1-to-string
  16962. (list latex-frag
  16963. org-latex-to-mathml-convert-command)))))
  16964. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16965. (formula-cache-dir (file-name-directory formula-cache)))
  16966. (unless (file-directory-p formula-cache-dir)
  16967. (make-directory formula-cache-dir t))
  16968. (unless (file-exists-p formula-cache)
  16969. (org-create-math-formula latex-frag formula-cache))
  16970. (if (file-exists-p formula-cache)
  16971. ;; Successful conversion. Return the link to MathML file.
  16972. (org-add-props
  16973. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16974. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16975. 'org-latex-src-embed-type (if latex-frag-type
  16976. 'paragraph 'character)))
  16977. ;; Failed conversion. Return the LaTeX fragment verbatim
  16978. latex-frag)))
  16979. (defun org-create-formula-image (string tofile options buffer &optional type)
  16980. "Create an image from LaTeX source using dvipng or convert.
  16981. This function calls either `org-create-formula-image-with-dvipng'
  16982. or `org-create-formula-image-with-imagemagick' depending on the
  16983. value of `org-latex-create-formula-image-program' or on the value
  16984. of the optional TYPE variable.
  16985. Note: ultimately these two function should be combined as they
  16986. share a good deal of logic."
  16987. (org-check-external-command
  16988. "latex" "needed to convert LaTeX fragments to images")
  16989. (funcall
  16990. (case (or type org-latex-create-formula-image-program)
  16991. ('dvipng
  16992. (org-check-external-command
  16993. "dvipng" "needed to convert LaTeX fragments to images")
  16994. #'org-create-formula-image-with-dvipng)
  16995. ('imagemagick
  16996. (org-check-external-command
  16997. "convert" "you need to install imagemagick")
  16998. #'org-create-formula-image-with-imagemagick)
  16999. (t (error
  17000. "Invalid value of `org-latex-create-formula-image-program'")))
  17001. string tofile options buffer))
  17002. (declare-function org-export-get-backend "ox" (name))
  17003. (declare-function org-export--get-global-options "ox" (&optional backend))
  17004. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17005. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17006. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17007. (defun org-create-formula--latex-header ()
  17008. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17009. (let ((info (org-combine-plists (org-export--get-global-options
  17010. (org-export-get-backend 'latex))
  17011. (org-export--get-inbuffer-options
  17012. (org-export-get-backend 'latex)))))
  17013. (org-latex-guess-babel-language
  17014. (org-latex-guess-inputenc
  17015. (org-splice-latex-header
  17016. org-format-latex-header
  17017. org-latex-default-packages-alist
  17018. org-latex-packages-alist t
  17019. (plist-get info :latex-header)))
  17020. info)))
  17021. ;; This function borrows from Ganesh Swami's latex2png.el
  17022. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17023. "This calls dvipng."
  17024. (require 'ox-latex)
  17025. (let* ((tmpdir (if (featurep 'xemacs)
  17026. (temp-directory)
  17027. temporary-file-directory))
  17028. (texfilebase (make-temp-name
  17029. (expand-file-name "orgtex" tmpdir)))
  17030. (texfile (concat texfilebase ".tex"))
  17031. (dvifile (concat texfilebase ".dvi"))
  17032. (pngfile (concat texfilebase ".png"))
  17033. (fnh (if (featurep 'xemacs)
  17034. (font-height (face-font 'default))
  17035. (face-attribute 'default :height nil)))
  17036. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17037. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17038. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17039. "Black"))
  17040. (bg (or (plist-get options (if buffer :background :html-background))
  17041. "Transparent")))
  17042. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17043. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17044. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17045. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17046. (let ((latex-header (org-create-formula--latex-header)))
  17047. (with-temp-file texfile
  17048. (insert latex-header)
  17049. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17050. (let ((dir default-directory))
  17051. (condition-case nil
  17052. (progn
  17053. (cd tmpdir)
  17054. (call-process "latex" nil nil nil texfile))
  17055. (error nil))
  17056. (cd dir))
  17057. (if (not (file-exists-p dvifile))
  17058. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17059. (condition-case nil
  17060. (if (featurep 'xemacs)
  17061. (call-process "dvipng" nil nil nil
  17062. "-fg" fg "-bg" bg
  17063. "-T" "tight"
  17064. "-o" pngfile
  17065. dvifile)
  17066. (call-process "dvipng" nil nil nil
  17067. "-fg" fg "-bg" bg
  17068. "-D" dpi
  17069. ;;"-x" scale "-y" scale
  17070. "-T" "tight"
  17071. "-o" pngfile
  17072. dvifile))
  17073. (error nil))
  17074. (if (not (file-exists-p pngfile))
  17075. (if org-format-latex-signal-error
  17076. (error "Failed to create png file from %s" texfile)
  17077. (message "Failed to create png file from %s" texfile)
  17078. nil)
  17079. ;; Use the requested file name and clean up
  17080. (copy-file pngfile tofile 'replace)
  17081. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17082. (if (file-exists-p (concat texfilebase e))
  17083. (delete-file (concat texfilebase e))))
  17084. pngfile))))
  17085. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17086. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17087. "This calls convert, which is included into imagemagick."
  17088. (require 'ox-latex)
  17089. (let* ((tmpdir (if (featurep 'xemacs)
  17090. (temp-directory)
  17091. temporary-file-directory))
  17092. (texfilebase (make-temp-name
  17093. (expand-file-name "orgtex" tmpdir)))
  17094. (texfile (concat texfilebase ".tex"))
  17095. (pdffile (concat texfilebase ".pdf"))
  17096. (pngfile (concat texfilebase ".png"))
  17097. (fnh (if (featurep 'xemacs)
  17098. (font-height (face-font 'default))
  17099. (face-attribute 'default :height nil)))
  17100. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17101. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17102. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17103. "black"))
  17104. (bg (or (plist-get options (if buffer :background :html-background))
  17105. "white")))
  17106. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17107. (setq fg (org-latex-color-format fg)))
  17108. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17109. (setq bg (org-latex-color-format
  17110. (if (string= bg "Transparent") "white" bg))))
  17111. (let ((latex-header (org-create-formula--latex-header)))
  17112. (with-temp-file texfile
  17113. (insert latex-header)
  17114. (insert "\n\\begin{document}\n"
  17115. "\\definecolor{fg}{rgb}{" fg "}\n"
  17116. "\\definecolor{bg}{rgb}{" bg "}\n"
  17117. "\n\\pagecolor{bg}\n"
  17118. "\n{\\color{fg}\n"
  17119. string
  17120. "\n}\n"
  17121. "\n\\end{document}\n")))
  17122. (org-latex-compile texfile t)
  17123. (if (not (file-exists-p pdffile))
  17124. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17125. (condition-case nil
  17126. (if (featurep 'xemacs)
  17127. (call-process "convert" nil nil nil
  17128. "-density" "96"
  17129. "-trim"
  17130. "-antialias"
  17131. pdffile
  17132. "-quality" "100"
  17133. ;; "-sharpen" "0x1.0"
  17134. pngfile)
  17135. (call-process "convert" nil nil nil
  17136. "-density" dpi
  17137. "-trim"
  17138. "-antialias"
  17139. pdffile
  17140. "-quality" "100"
  17141. ;; "-sharpen" "0x1.0"
  17142. pngfile))
  17143. (error nil))
  17144. (if (not (file-exists-p pngfile))
  17145. (if org-format-latex-signal-error
  17146. (error "Failed to create png file from %s" texfile)
  17147. (message "Failed to create png file from %s" texfile)
  17148. nil)
  17149. ;; Use the requested file name and clean up
  17150. (copy-file pngfile tofile 'replace)
  17151. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17152. (if (file-exists-p (concat texfilebase e))
  17153. (delete-file (concat texfilebase e))))
  17154. pngfile))))
  17155. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17156. "Fill a LaTeX header template TPL.
  17157. In the template, the following place holders will be recognized:
  17158. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17159. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17160. [PACKAGES] \\usepackage statements for PKG
  17161. [NO-PACKAGES] do not include PKG
  17162. [EXTRA] the string EXTRA
  17163. [NO-EXTRA] do not include EXTRA
  17164. For backward compatibility, if both the positive and the negative place
  17165. holder is missing, the positive one (without the \"NO-\") will be
  17166. assumed to be present at the end of the template.
  17167. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17168. EXTRA is a string.
  17169. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17170. (let (rpl (end ""))
  17171. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17172. (setq rpl (if (or (match-end 1) (not def-pkg))
  17173. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17174. tpl (replace-match rpl t t tpl))
  17175. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17176. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17177. (setq rpl (if (or (match-end 1) (not pkg))
  17178. "" (org-latex-packages-to-string pkg snippets-p t))
  17179. tpl (replace-match rpl t t tpl))
  17180. (if pkg (setq end
  17181. (concat end "\n"
  17182. (org-latex-packages-to-string pkg snippets-p)))))
  17183. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17184. (setq rpl (if (or (match-end 1) (not extra))
  17185. "" (concat extra "\n"))
  17186. tpl (replace-match rpl t t tpl))
  17187. (if (and extra (string-match "\\S-" extra))
  17188. (setq end (concat end "\n" extra))))
  17189. (if (string-match "\\S-" end)
  17190. (concat tpl "\n" end)
  17191. tpl)))
  17192. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17193. "Turn an alist of packages into a string with the \\usepackage macros."
  17194. (setq pkg (mapconcat (lambda(p)
  17195. (cond
  17196. ((stringp p) p)
  17197. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17198. (format "%% Package %s omitted" (cadr p)))
  17199. ((equal "" (car p))
  17200. (format "\\usepackage{%s}" (cadr p)))
  17201. (t
  17202. (format "\\usepackage[%s]{%s}"
  17203. (car p) (cadr p)))))
  17204. pkg
  17205. "\n"))
  17206. (if newline (concat pkg "\n") pkg))
  17207. (defun org-dvipng-color (attr)
  17208. "Return a RGB color specification for dvipng."
  17209. (apply 'format "rgb %s %s %s"
  17210. (mapcar 'org-normalize-color
  17211. (if (featurep 'xemacs)
  17212. (color-rgb-components
  17213. (face-property 'default
  17214. (cond ((eq attr :foreground) 'foreground)
  17215. ((eq attr :background) 'background))))
  17216. (color-values (face-attribute 'default attr nil))))))
  17217. (defun org-dvipng-color-format (color-name)
  17218. "Convert COLOR-NAME to a RGB color value for dvipng."
  17219. (apply 'format "rgb %s %s %s"
  17220. (mapcar 'org-normalize-color
  17221. (color-values color-name))))
  17222. (defun org-latex-color (attr)
  17223. "Return a RGB color for the LaTeX color package."
  17224. (apply 'format "%s,%s,%s"
  17225. (mapcar 'org-normalize-color
  17226. (if (featurep 'xemacs)
  17227. (color-rgb-components
  17228. (face-property 'default
  17229. (cond ((eq attr :foreground) 'foreground)
  17230. ((eq attr :background) 'background))))
  17231. (color-values (face-attribute 'default attr nil))))))
  17232. (defun org-latex-color-format (color-name)
  17233. "Convert COLOR-NAME to a RGB color value."
  17234. (apply 'format "%s,%s,%s"
  17235. (mapcar 'org-normalize-color
  17236. (color-values color-name))))
  17237. (defun org-normalize-color (value)
  17238. "Return string to be used as color value for an RGB component."
  17239. (format "%g" (/ value 65535.0)))
  17240. ;; Image display
  17241. (defvar org-inline-image-overlays nil)
  17242. (make-variable-buffer-local 'org-inline-image-overlays)
  17243. (defun org-toggle-inline-images (&optional include-linked)
  17244. "Toggle the display of inline images.
  17245. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17246. (interactive "P")
  17247. (if org-inline-image-overlays
  17248. (progn
  17249. (org-remove-inline-images)
  17250. (message "Inline image display turned off"))
  17251. (org-display-inline-images include-linked)
  17252. (if (and (org-called-interactively-p)
  17253. org-inline-image-overlays)
  17254. (message "%d images displayed inline"
  17255. (length org-inline-image-overlays))
  17256. (message "No images to display inline"))))
  17257. (defun org-redisplay-inline-images ()
  17258. "Refresh the display of inline images."
  17259. (interactive)
  17260. (if (not org-inline-image-overlays)
  17261. (org-toggle-inline-images)
  17262. (org-toggle-inline-images)
  17263. (org-toggle-inline-images)))
  17264. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17265. "Display inline images.
  17266. Normally only links without a description part are inlined, because this
  17267. is how it will work for export. When INCLUDE-LINKED is set, also links
  17268. with a description part will be inlined. This can be nice for a quick
  17269. look at those images, but it does not reflect what exported files will look
  17270. like.
  17271. When REFRESH is set, refresh existing images between BEG and END.
  17272. This will create new image displays only if necessary.
  17273. BEG and END default to the buffer boundaries."
  17274. (interactive "P")
  17275. (when (display-graphic-p)
  17276. (unless refresh
  17277. (org-remove-inline-images)
  17278. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17279. (save-excursion
  17280. (save-restriction
  17281. (widen)
  17282. (setq beg (or beg (point-min)) end (or end (point-max)))
  17283. (goto-char beg)
  17284. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17285. (substring (org-image-file-name-regexp) 0 -2)
  17286. "\\)\\]" (if include-linked "" "\\]")))
  17287. (case-fold-search t)
  17288. old file ov img type attrwidth width)
  17289. (while (re-search-forward re end t)
  17290. (setq old (get-char-property-and-overlay (match-beginning 1)
  17291. 'org-image-overlay)
  17292. file (expand-file-name
  17293. (concat (or (match-string 3) "") (match-string 4))))
  17294. (when (image-type-available-p 'imagemagick)
  17295. (setq attrwidth (if (or (listp org-image-actual-width)
  17296. (null org-image-actual-width))
  17297. (save-excursion
  17298. (save-match-data
  17299. (when (re-search-backward
  17300. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17301. (save-excursion
  17302. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17303. (string-to-number (match-string 1))))))
  17304. width (cond ((eq org-image-actual-width t) nil)
  17305. ((null org-image-actual-width) attrwidth)
  17306. ((numberp org-image-actual-width)
  17307. org-image-actual-width)
  17308. ((listp org-image-actual-width)
  17309. (or attrwidth (car org-image-actual-width))))
  17310. type (if width 'imagemagick)))
  17311. (when (file-exists-p file)
  17312. (if (and (car-safe old) refresh)
  17313. (image-refresh (overlay-get (cdr old) 'display))
  17314. (setq img (save-match-data (create-image file type nil :width width)))
  17315. (when img
  17316. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17317. (overlay-put ov 'display img)
  17318. (overlay-put ov 'face 'default)
  17319. (overlay-put ov 'org-image-overlay t)
  17320. (overlay-put ov 'modification-hooks
  17321. (list 'org-display-inline-remove-overlay))
  17322. (push ov org-inline-image-overlays))))))))))
  17323. (define-obsolete-function-alias
  17324. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17325. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17326. "Remove inline-display overlay if a corresponding region is modified."
  17327. (let ((inhibit-modification-hooks t))
  17328. (when (and ov after)
  17329. (delete ov org-inline-image-overlays)
  17330. (delete-overlay ov))))
  17331. (defun org-remove-inline-images ()
  17332. "Remove inline display of images."
  17333. (interactive)
  17334. (mapc 'delete-overlay org-inline-image-overlays)
  17335. (setq org-inline-image-overlays nil))
  17336. ;;;; Key bindings
  17337. ;; Outline functions from `outline-mode-prefix-map'
  17338. ;; that can be remapped in Org:
  17339. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17340. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17341. (define-key org-mode-map [remap outline-forward-same-level]
  17342. 'org-forward-heading-same-level)
  17343. (define-key org-mode-map [remap outline-backward-same-level]
  17344. 'org-backward-heading-same-level)
  17345. (define-key org-mode-map [remap show-branches]
  17346. 'org-kill-note-or-show-branches)
  17347. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17348. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17349. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17350. ;; Outline functions from `outline-mode-prefix-map' that can not
  17351. ;; be remapped in Org:
  17352. ;;
  17353. ;; - the column "key binding" shows whether the Outline function is still
  17354. ;; available in Org mode on the same key that it has been bound to in
  17355. ;; Outline mode:
  17356. ;; - "overridden": key used for a different functionality in Org mode
  17357. ;; - else: key still bound to the same Outline function in Org mode
  17358. ;;
  17359. ;; | Outline function | key binding | Org replacement |
  17360. ;; |------------------------------------+-------------+-----------------------|
  17361. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17362. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17363. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17364. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17365. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17366. ;; | `show-entry' | overridden | no replacement |
  17367. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17368. ;; | `show-branches' | `C-c C-k' | still same function |
  17369. ;; | `show-subtree' | overridden | visibility cycling |
  17370. ;; | `show-all' | overridden | no replacement |
  17371. ;; | `hide-subtree' | overridden | visibility cycling |
  17372. ;; | `hide-body' | overridden | no replacement |
  17373. ;; | `hide-entry' | overridden | visibility cycling |
  17374. ;; | `hide-leaves' | overridden | no replacement |
  17375. ;; | `hide-sublevels' | overridden | no replacement |
  17376. ;; | `hide-other' | overridden | no replacement |
  17377. ;; Make `C-c C-x' a prefix key
  17378. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17379. ;; TAB key with modifiers
  17380. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17381. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17382. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17383. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17384. ;; The following line is necessary under Suse GNU/Linux
  17385. (unless (featurep 'xemacs)
  17386. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17387. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17388. (define-key org-mode-map [backtab] 'org-shifttab)
  17389. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17390. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17391. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17392. ;; Cursor keys with modifiers
  17393. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17394. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17395. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17396. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17397. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17398. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17399. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17400. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17401. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17402. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17403. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17404. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17405. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17406. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17407. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17408. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17409. ;; Babel keys
  17410. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17411. (mapc (lambda (pair)
  17412. (define-key org-babel-map (car pair) (cdr pair)))
  17413. org-babel-key-bindings)
  17414. ;;; Extra keys for tty access.
  17415. ;; We only set them when really needed because otherwise the
  17416. ;; menus don't show the simple keys
  17417. (when (or org-use-extra-keys
  17418. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17419. (not window-system))
  17420. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17421. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17422. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17423. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17424. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17425. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17426. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17427. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17428. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17429. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17430. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17431. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17432. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17433. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17434. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17435. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17436. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17437. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17438. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17439. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17440. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17441. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17442. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17443. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17444. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17445. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17446. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17447. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17448. ;; All the other keys
  17449. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17450. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17451. (if (boundp 'narrow-map)
  17452. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17453. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17454. (if (boundp 'narrow-map)
  17455. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17456. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17457. (if (boundp 'narrow-map)
  17458. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17459. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17460. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17461. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17462. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17463. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17464. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17465. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17466. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17467. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17468. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17469. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17470. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17471. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17472. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17473. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17474. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17475. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17476. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17477. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17478. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17479. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17480. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17481. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17482. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17483. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17484. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17485. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17486. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17487. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17488. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17489. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17490. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17491. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17492. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17493. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17494. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17495. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17496. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17497. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17498. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17499. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17500. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17501. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17502. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17503. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17504. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17505. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17506. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17507. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17508. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17509. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17510. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17511. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17512. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17513. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17514. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17515. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17516. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17517. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17518. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17519. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17520. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-element)
  17521. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-element)
  17522. (org-defkey org-mode-map "\C-m" 'org-return)
  17523. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17524. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17525. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17526. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17527. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17528. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17529. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17530. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17531. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17532. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17533. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17534. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17535. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17536. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17537. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17538. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17539. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17540. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17541. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17542. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17543. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17544. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17545. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17546. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17547. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17548. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17549. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17550. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17551. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17552. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17553. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17554. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17555. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17556. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17557. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17558. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17559. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17560. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17561. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17562. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17563. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17564. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17565. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17566. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17567. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17568. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17569. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17570. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17571. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17572. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17573. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17574. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17575. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17576. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17577. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17578. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17579. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17580. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17581. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17582. (when (featurep 'xemacs)
  17583. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17584. (defconst org-speed-commands-default
  17585. '(
  17586. ("Outline Navigation")
  17587. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17588. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17589. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17590. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17591. ("F" . org-next-block)
  17592. ("B" . org-previous-block)
  17593. ("u" . (org-speed-move-safe 'outline-up-heading))
  17594. ("j" . org-goto)
  17595. ("g" . (org-refile t))
  17596. ("Outline Visibility")
  17597. ("c" . org-cycle)
  17598. ("C" . org-shifttab)
  17599. (" " . org-display-outline-path)
  17600. ("s" . org-narrow-to-subtree)
  17601. ("=" . org-columns)
  17602. ("Outline Structure Editing")
  17603. ("U" . org-shiftmetaup)
  17604. ("D" . org-shiftmetadown)
  17605. ("r" . org-metaright)
  17606. ("l" . org-metaleft)
  17607. ("R" . org-shiftmetaright)
  17608. ("L" . org-shiftmetaleft)
  17609. ("i" . (progn (forward-char 1) (call-interactively
  17610. 'org-insert-heading-respect-content)))
  17611. ("^" . org-sort)
  17612. ("w" . org-refile)
  17613. ("a" . org-archive-subtree-default-with-confirmation)
  17614. ("@" . org-mark-subtree)
  17615. ("#" . org-toggle-comment)
  17616. ("Clock Commands")
  17617. ("I" . org-clock-in)
  17618. ("O" . org-clock-out)
  17619. ("Meta Data Editing")
  17620. ("t" . org-todo)
  17621. ("," . (org-priority))
  17622. ("0" . (org-priority ?\ ))
  17623. ("1" . (org-priority ?A))
  17624. ("2" . (org-priority ?B))
  17625. ("3" . (org-priority ?C))
  17626. (":" . org-set-tags-command)
  17627. ("e" . org-set-effort)
  17628. ("E" . org-inc-effort)
  17629. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17630. (org-entry-put (point) "APPT_WARNTIME" m)))
  17631. ("Agenda Views etc")
  17632. ("v" . org-agenda)
  17633. ("/" . org-sparse-tree)
  17634. ("Misc")
  17635. ("o" . org-open-at-point)
  17636. ("?" . org-speed-command-help)
  17637. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17638. (">" . (org-agenda-remove-restriction-lock))
  17639. )
  17640. "The default speed commands.")
  17641. (defun org-print-speed-command (e)
  17642. (if (> (length (car e)) 1)
  17643. (progn
  17644. (princ "\n")
  17645. (princ (car e))
  17646. (princ "\n")
  17647. (princ (make-string (length (car e)) ?-))
  17648. (princ "\n"))
  17649. (princ (car e))
  17650. (princ " ")
  17651. (if (symbolp (cdr e))
  17652. (princ (symbol-name (cdr e)))
  17653. (prin1 (cdr e)))
  17654. (princ "\n")))
  17655. (defun org-speed-command-help ()
  17656. "Show the available speed commands."
  17657. (interactive)
  17658. (if (not org-use-speed-commands)
  17659. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17660. (with-output-to-temp-buffer "*Help*"
  17661. (princ "User-defined Speed commands\n===========================\n")
  17662. (mapc 'org-print-speed-command org-speed-commands-user)
  17663. (princ "\n")
  17664. (princ "Built-in Speed commands\n=======================\n")
  17665. (mapc 'org-print-speed-command org-speed-commands-default))
  17666. (with-current-buffer "*Help*"
  17667. (setq truncate-lines t))))
  17668. (defun org-speed-move-safe (cmd)
  17669. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17670. If not, return to the original position and throw an error."
  17671. (interactive)
  17672. (let ((pos (point)))
  17673. (call-interactively cmd)
  17674. (unless (and (bolp) (org-at-heading-p))
  17675. (goto-char pos)
  17676. (error "Boundary reached while executing %s" cmd))))
  17677. (defvar org-self-insert-command-undo-counter 0)
  17678. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17679. (defvar org-speed-command nil)
  17680. (define-obsolete-function-alias
  17681. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17682. (defun org-speed-command-activate (keys)
  17683. "Hook for activating single-letter speed commands.
  17684. `org-speed-commands-default' specifies a minimal command set.
  17685. Use `org-speed-commands-user' for further customization."
  17686. (when (or (and (bolp) (looking-at org-outline-regexp))
  17687. (and (functionp org-use-speed-commands)
  17688. (funcall org-use-speed-commands)))
  17689. (cdr (assoc keys (append org-speed-commands-user
  17690. org-speed-commands-default)))))
  17691. (define-obsolete-function-alias
  17692. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17693. (defun org-babel-speed-command-activate (keys)
  17694. "Hook for activating single-letter code block commands."
  17695. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17696. (cdr (assoc keys org-babel-key-bindings))))
  17697. (defcustom org-speed-command-hook
  17698. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17699. "Hook for activating speed commands at strategic locations.
  17700. Hook functions are called in sequence until a valid handler is
  17701. found.
  17702. Each hook takes a single argument, a user-pressed command key
  17703. which is also a `self-insert-command' from the global map.
  17704. Within the hook, examine the cursor position and the command key
  17705. and return nil or a valid handler as appropriate. Handler could
  17706. be one of an interactive command, a function, or a form.
  17707. Set `org-use-speed-commands' to non-nil value to enable this
  17708. hook. The default setting is `org-speed-command-activate'."
  17709. :group 'org-structure
  17710. :version "24.1"
  17711. :type 'hook)
  17712. (defun org-self-insert-command (N)
  17713. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17714. If the cursor is in a table looking at whitespace, the whitespace is
  17715. overwritten, and the table is not marked as requiring realignment."
  17716. (interactive "p")
  17717. (org-check-before-invisible-edit 'insert)
  17718. (cond
  17719. ((and org-use-speed-commands
  17720. (setq org-speed-command
  17721. (run-hook-with-args-until-success
  17722. 'org-speed-command-hook (this-command-keys))))
  17723. (cond
  17724. ((commandp org-speed-command)
  17725. (setq this-command org-speed-command)
  17726. (call-interactively org-speed-command))
  17727. ((functionp org-speed-command)
  17728. (funcall org-speed-command))
  17729. ((and org-speed-command (listp org-speed-command))
  17730. (eval org-speed-command))
  17731. (t (let (org-use-speed-commands)
  17732. (call-interactively 'org-self-insert-command)))))
  17733. ((and
  17734. (org-table-p)
  17735. (progn
  17736. ;; check if we blank the field, and if that triggers align
  17737. (and (featurep 'org-table) org-table-auto-blank-field
  17738. (member last-command
  17739. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17740. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17741. ;; got extra space, this field does not determine column width
  17742. (let (org-table-may-need-update) (org-table-blank-field))
  17743. ;; no extra space, this field may determine column width
  17744. (org-table-blank-field)))
  17745. t)
  17746. (eq N 1)
  17747. (looking-at "[^|\n]* |"))
  17748. (let (org-table-may-need-update)
  17749. (goto-char (1- (match-end 0)))
  17750. (backward-delete-char 1)
  17751. (goto-char (match-beginning 0))
  17752. (self-insert-command N)))
  17753. (t
  17754. (setq org-table-may-need-update t)
  17755. (self-insert-command N)
  17756. (org-fix-tags-on-the-fly)
  17757. (if org-self-insert-cluster-for-undo
  17758. (if (not (eq last-command 'org-self-insert-command))
  17759. (setq org-self-insert-command-undo-counter 1)
  17760. (if (>= org-self-insert-command-undo-counter 20)
  17761. (setq org-self-insert-command-undo-counter 1)
  17762. (and (> org-self-insert-command-undo-counter 0)
  17763. buffer-undo-list (listp buffer-undo-list)
  17764. (not (cadr buffer-undo-list)) ; remove nil entry
  17765. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17766. (setq org-self-insert-command-undo-counter
  17767. (1+ org-self-insert-command-undo-counter))))))))
  17768. (defun org-check-before-invisible-edit (kind)
  17769. "Check is editing if kind KIND would be dangerous with invisible text around.
  17770. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17771. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17772. (if (and org-catch-invisible-edits
  17773. (or (not (boundp 'visible-mode)) (not visible-mode))
  17774. (or (get-char-property (point) 'invisible)
  17775. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17776. ;; OK, we need to take a closer look
  17777. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17778. (invisible-before-point (if (bobp) nil (get-char-property
  17779. (1- (point)) 'invisible)))
  17780. (border-and-ok-direction
  17781. (or
  17782. ;; Check if we are acting predictably before invisible text
  17783. (and invisible-at-point (not invisible-before-point)
  17784. (memq kind '(insert delete-backward)))
  17785. ;; Check if we are acting predictably after invisible text
  17786. ;; This works not well, and I have turned it off. It seems
  17787. ;; better to always show and stop after invisible text.
  17788. ;; (and (not invisible-at-point) invisible-before-point
  17789. ;; (memq kind '(insert delete)))
  17790. )))
  17791. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17792. (memq invisible-before-point '(outline org-hide-block t)))
  17793. (if (eq org-catch-invisible-edits 'error)
  17794. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17795. (if (and org-custom-properties-overlays
  17796. (y-or-n-p "Display invisible properties in this buffer? "))
  17797. (org-toggle-custom-properties-visibility)
  17798. ;; Make the area visible
  17799. (save-excursion
  17800. (if invisible-before-point
  17801. (goto-char (previous-single-char-property-change
  17802. (point) 'invisible)))
  17803. (org-cycle))
  17804. (cond
  17805. ((eq org-catch-invisible-edits 'show)
  17806. ;; That's it, we do the edit after showing
  17807. (message
  17808. "Unfolding invisible region around point before editing")
  17809. (sit-for 1))
  17810. ((and (eq org-catch-invisible-edits 'smart)
  17811. border-and-ok-direction)
  17812. (message "Unfolding invisible region around point before editing"))
  17813. (t
  17814. ;; Don't do the edit, make the user repeat it in full visibility
  17815. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17816. (defun org-fix-tags-on-the-fly ()
  17817. (when (and (equal (char-after (point-at-bol)) ?*)
  17818. (org-at-heading-p))
  17819. (org-align-tags-here org-tags-column)))
  17820. (defun org-delete-backward-char (N)
  17821. "Like `delete-backward-char', insert whitespace at field end in tables.
  17822. When deleting backwards, in tables this function will insert whitespace in
  17823. front of the next \"|\" separator, to keep the table aligned. The table will
  17824. still be marked for re-alignment if the field did fill the entire column,
  17825. because, in this case the deletion might narrow the column."
  17826. (interactive "p")
  17827. (save-match-data
  17828. (org-check-before-invisible-edit 'delete-backward)
  17829. (if (and (org-table-p)
  17830. (eq N 1)
  17831. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17832. (looking-at ".*?|"))
  17833. (let ((pos (point))
  17834. (noalign (looking-at "[^|\n\r]* |"))
  17835. (c org-table-may-need-update))
  17836. (backward-delete-char N)
  17837. (if (not overwrite-mode)
  17838. (progn
  17839. (skip-chars-forward "^|")
  17840. (insert " ")
  17841. (goto-char (1- pos))))
  17842. ;; noalign: if there were two spaces at the end, this field
  17843. ;; does not determine the width of the column.
  17844. (if noalign (setq org-table-may-need-update c)))
  17845. (backward-delete-char N)
  17846. (org-fix-tags-on-the-fly))))
  17847. (defun org-delete-char (N)
  17848. "Like `delete-char', but insert whitespace at field end in tables.
  17849. When deleting characters, in tables this function will insert whitespace in
  17850. front of the next \"|\" separator, to keep the table aligned. The table will
  17851. still be marked for re-alignment if the field did fill the entire column,
  17852. because, in this case the deletion might narrow the column."
  17853. (interactive "p")
  17854. (save-match-data
  17855. (org-check-before-invisible-edit 'delete)
  17856. (if (and (org-table-p)
  17857. (not (bolp))
  17858. (not (= (char-after) ?|))
  17859. (eq N 1))
  17860. (if (looking-at ".*?|")
  17861. (let ((pos (point))
  17862. (noalign (looking-at "[^|\n\r]* |"))
  17863. (c org-table-may-need-update))
  17864. (replace-match
  17865. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17866. (goto-char pos)
  17867. ;; noalign: if there were two spaces at the end, this field
  17868. ;; does not determine the width of the column.
  17869. (if noalign (setq org-table-may-need-update c)))
  17870. (delete-char N))
  17871. (delete-char N)
  17872. (org-fix-tags-on-the-fly))))
  17873. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17874. (put 'org-self-insert-command 'delete-selection t)
  17875. (put 'orgtbl-self-insert-command 'delete-selection t)
  17876. (put 'org-delete-char 'delete-selection 'supersede)
  17877. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17878. (put 'org-yank 'delete-selection 'yank)
  17879. ;; Make `flyspell-mode' delay after some commands
  17880. (put 'org-self-insert-command 'flyspell-delayed t)
  17881. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17882. (put 'org-delete-char 'flyspell-delayed t)
  17883. (put 'org-delete-backward-char 'flyspell-delayed t)
  17884. ;; Make pabbrev-mode expand after org-mode commands
  17885. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17886. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17887. ;; How to do this: Measure non-white length of current string
  17888. ;; If equal to column width, we should realign.
  17889. (defun org-remap (map &rest commands)
  17890. "In MAP, remap the functions given in COMMANDS.
  17891. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17892. (let (new old)
  17893. (while commands
  17894. (setq old (pop commands) new (pop commands))
  17895. (if (fboundp 'command-remapping)
  17896. (org-defkey map (vector 'remap old) new)
  17897. (substitute-key-definition old new map global-map)))))
  17898. (defun org-transpose-words ()
  17899. "Transpose words for Org.
  17900. This uses the `org-mode-transpose-word-syntax-table' syntax
  17901. table, which interprets characters in `org-emphasis-alist' as
  17902. word constituants."
  17903. (interactive)
  17904. (with-syntax-table org-mode-transpose-word-syntax-table
  17905. (call-interactively 'transpose-words)))
  17906. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17907. (when (eq org-enable-table-editor 'optimized)
  17908. ;; If the user wants maximum table support, we need to hijack
  17909. ;; some standard editing functions
  17910. (org-remap org-mode-map
  17911. 'self-insert-command 'org-self-insert-command
  17912. 'delete-char 'org-delete-char
  17913. 'delete-backward-char 'org-delete-backward-char)
  17914. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17915. (defvar org-ctrl-c-ctrl-c-hook nil
  17916. "Hook for functions attaching themselves to `C-c C-c'.
  17917. This can be used to add additional functionality to the C-c C-c
  17918. key which executes context-dependent commands. This hook is run
  17919. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17920. run after the last test.
  17921. Each function will be called with no arguments. The function
  17922. must check if the context is appropriate for it to act. If yes,
  17923. it should do its thing and then return a non-nil value. If the
  17924. context is wrong, just do nothing and return nil.")
  17925. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17926. "Hook for functions attaching themselves to `C-c C-c'.
  17927. This can be used to add additional functionality to the C-c C-c
  17928. key which executes context-dependent commands. This hook is run
  17929. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17930. before the first test.
  17931. Each function will be called with no arguments. The function
  17932. must check if the context is appropriate for it to act. If yes,
  17933. it should do its thing and then return a non-nil value. If the
  17934. context is wrong, just do nothing and return nil.")
  17935. (defvar org-tab-first-hook nil
  17936. "Hook for functions to attach themselves to TAB.
  17937. See `org-ctrl-c-ctrl-c-hook' for more information.
  17938. This hook runs as the first action when TAB is pressed, even before
  17939. `org-cycle' messes around with the `outline-regexp' to cater for
  17940. inline tasks and plain list item folding.
  17941. If any function in this hook returns t, any other actions that
  17942. would have been caused by TAB (such as table field motion or visibility
  17943. cycling) will not occur.")
  17944. (defvar org-tab-after-check-for-table-hook nil
  17945. "Hook for functions to attach themselves to TAB.
  17946. See `org-ctrl-c-ctrl-c-hook' for more information.
  17947. This hook runs after it has been established that the cursor is not in a
  17948. table, but before checking if the cursor is in a headline or if global cycling
  17949. should be done.
  17950. If any function in this hook returns t, not other actions like visibility
  17951. cycling will be done.")
  17952. (defvar org-tab-after-check-for-cycling-hook nil
  17953. "Hook for functions to attach themselves to TAB.
  17954. See `org-ctrl-c-ctrl-c-hook' for more information.
  17955. This hook runs after it has been established that not table field motion and
  17956. not visibility should be done because of current context. This is probably
  17957. the place where a package like yasnippets can hook in.")
  17958. (defvar org-tab-before-tab-emulation-hook nil
  17959. "Hook for functions to attach themselves to TAB.
  17960. See `org-ctrl-c-ctrl-c-hook' for more information.
  17961. This hook runs after every other options for TAB have been exhausted, but
  17962. before indentation and \t insertion takes place.")
  17963. (defvar org-metaleft-hook nil
  17964. "Hook for functions attaching themselves to `M-left'.
  17965. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17966. (defvar org-metaright-hook nil
  17967. "Hook for functions attaching themselves to `M-right'.
  17968. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17969. (defvar org-metaup-hook nil
  17970. "Hook for functions attaching themselves to `M-up'.
  17971. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17972. (defvar org-metadown-hook nil
  17973. "Hook for functions attaching themselves to `M-down'.
  17974. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17975. (defvar org-shiftmetaleft-hook nil
  17976. "Hook for functions attaching themselves to `M-S-left'.
  17977. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17978. (defvar org-shiftmetaright-hook nil
  17979. "Hook for functions attaching themselves to `M-S-right'.
  17980. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17981. (defvar org-shiftmetaup-hook nil
  17982. "Hook for functions attaching themselves to `M-S-up'.
  17983. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17984. (defvar org-shiftmetadown-hook nil
  17985. "Hook for functions attaching themselves to `M-S-down'.
  17986. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17987. (defvar org-metareturn-hook nil
  17988. "Hook for functions attaching themselves to `M-RET'.
  17989. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17990. (defvar org-shiftup-hook nil
  17991. "Hook for functions attaching themselves to `S-up'.
  17992. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17993. (defvar org-shiftup-final-hook nil
  17994. "Hook for functions attaching themselves to `S-up'.
  17995. This one runs after all other options except shift-select have been excluded.
  17996. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17997. (defvar org-shiftdown-hook nil
  17998. "Hook for functions attaching themselves to `S-down'.
  17999. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18000. (defvar org-shiftdown-final-hook nil
  18001. "Hook for functions attaching themselves to `S-down'.
  18002. This one runs after all other options except shift-select have been excluded.
  18003. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18004. (defvar org-shiftleft-hook nil
  18005. "Hook for functions attaching themselves to `S-left'.
  18006. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18007. (defvar org-shiftleft-final-hook nil
  18008. "Hook for functions attaching themselves to `S-left'.
  18009. This one runs after all other options except shift-select have been excluded.
  18010. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18011. (defvar org-shiftright-hook nil
  18012. "Hook for functions attaching themselves to `S-right'.
  18013. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18014. (defvar org-shiftright-final-hook nil
  18015. "Hook for functions attaching themselves to `S-right'.
  18016. This one runs after all other options except shift-select have been excluded.
  18017. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18018. (defun org-modifier-cursor-error ()
  18019. "Throw an error, a modified cursor command was applied in wrong context."
  18020. (user-error "This command is active in special context like tables, headlines or items"))
  18021. (defun org-shiftselect-error ()
  18022. "Throw an error because Shift-Cursor command was applied in wrong context."
  18023. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18024. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18025. (user-error "This command works only in special context like headlines or timestamps")))
  18026. (defun org-call-for-shift-select (cmd)
  18027. (let ((this-command-keys-shift-translated t))
  18028. (call-interactively cmd)))
  18029. (defun org-shifttab (&optional arg)
  18030. "Global visibility cycling or move to previous table field.
  18031. Call `org-table-previous-field' within a table.
  18032. When ARG is nil, cycle globally through visibility states.
  18033. When ARG is a numeric prefix, show contents of this level."
  18034. (interactive "P")
  18035. (cond
  18036. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18037. ((integerp arg)
  18038. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18039. (message "Content view to level: %d" arg)
  18040. (org-content (prefix-numeric-value arg2))
  18041. (org-cycle-show-empty-lines t)
  18042. (setq org-cycle-global-status 'overview)))
  18043. (t (call-interactively 'org-global-cycle))))
  18044. (defun org-shiftmetaleft ()
  18045. "Promote subtree or delete table column.
  18046. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18047. `org-table-delete-column', depending on context. See the
  18048. individual commands for more information."
  18049. (interactive)
  18050. (cond
  18051. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18052. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18053. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18054. ((if (not (org-region-active-p)) (org-at-item-p)
  18055. (save-excursion (goto-char (region-beginning))
  18056. (org-at-item-p)))
  18057. (call-interactively 'org-outdent-item-tree))
  18058. (t (org-modifier-cursor-error))))
  18059. (defun org-shiftmetaright ()
  18060. "Demote subtree or insert table column.
  18061. Calls `org-demote-subtree', `org-indent-item-tree', or
  18062. `org-table-insert-column', depending on context. See the
  18063. individual commands for more information."
  18064. (interactive)
  18065. (cond
  18066. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18067. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18068. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18069. ((if (not (org-region-active-p)) (org-at-item-p)
  18070. (save-excursion (goto-char (region-beginning))
  18071. (org-at-item-p)))
  18072. (call-interactively 'org-indent-item-tree))
  18073. (t (org-modifier-cursor-error))))
  18074. (defun org-shiftmetaup (&optional arg)
  18075. "Move subtree up or kill table row.
  18076. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18077. `org-move-item-up' or `org-timestamp-up', depending on context.
  18078. See the individual commands for more information."
  18079. (interactive "P")
  18080. (cond
  18081. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18082. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18083. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18084. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18085. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18086. (call-interactively 'org-timestamp-up)))
  18087. (t (call-interactively 'org-drag-line-backward))))
  18088. (defun org-shiftmetadown (&optional arg)
  18089. "Move subtree down or insert table row.
  18090. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18091. `org-move-item-down' or `org-timestamp-up', depending on context.
  18092. See the individual commands for more information."
  18093. (interactive "P")
  18094. (cond
  18095. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18096. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18097. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18098. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18099. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18100. (call-interactively 'org-timestamp-down)))
  18101. (t (call-interactively 'org-drag-line-forward))))
  18102. (defsubst org-hidden-tree-error ()
  18103. (user-error
  18104. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18105. (defun org-metaleft (&optional arg)
  18106. "Promote heading or move table column to left.
  18107. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18108. With no specific context, calls the Emacs default `backward-word'.
  18109. See the individual commands for more information."
  18110. (interactive "P")
  18111. (cond
  18112. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18113. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18114. ((org-with-limited-levels
  18115. (or (org-at-heading-p)
  18116. (and (org-region-active-p)
  18117. (save-excursion
  18118. (goto-char (region-beginning))
  18119. (org-at-heading-p)))))
  18120. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18121. (call-interactively 'org-do-promote))
  18122. ;; At an inline task.
  18123. ((org-at-heading-p)
  18124. (call-interactively 'org-inlinetask-promote))
  18125. ((or (org-at-item-p)
  18126. (and (org-region-active-p)
  18127. (save-excursion
  18128. (goto-char (region-beginning))
  18129. (org-at-item-p))))
  18130. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18131. (call-interactively 'org-outdent-item))
  18132. (t (call-interactively 'backward-word))))
  18133. (defun org-metaright (&optional arg)
  18134. "Demote a subtree, a list item or move table column to right.
  18135. In front of a drawer or a block keyword, indent it correctly.
  18136. With no specific context, calls the Emacs default `forward-word'.
  18137. See the individual commands for more information."
  18138. (interactive "P")
  18139. (cond
  18140. ((run-hook-with-args-until-success 'org-metaright-hook))
  18141. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18142. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18143. ((org-at-block-p) (call-interactively 'org-indent-block))
  18144. ((org-with-limited-levels
  18145. (or (org-at-heading-p)
  18146. (and (org-region-active-p)
  18147. (save-excursion
  18148. (goto-char (region-beginning))
  18149. (org-at-heading-p)))))
  18150. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18151. (call-interactively 'org-do-demote))
  18152. ;; At an inline task.
  18153. ((org-at-heading-p)
  18154. (call-interactively 'org-inlinetask-demote))
  18155. ((or (org-at-item-p)
  18156. (and (org-region-active-p)
  18157. (save-excursion
  18158. (goto-char (region-beginning))
  18159. (org-at-item-p))))
  18160. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18161. (call-interactively 'org-indent-item))
  18162. (t (call-interactively 'forward-word))))
  18163. (defun org-check-for-hidden (what)
  18164. "Check if there are hidden headlines/items in the current visual line.
  18165. WHAT can be either `headlines' or `items'. If the current line is
  18166. an outline or item heading and it has a folded subtree below it,
  18167. this function returns t, nil otherwise."
  18168. (let ((re (cond
  18169. ((eq what 'headlines) org-outline-regexp-bol)
  18170. ((eq what 'items) (org-item-beginning-re))
  18171. (t (error "This should not happen"))))
  18172. beg end)
  18173. (save-excursion
  18174. (catch 'exit
  18175. (unless (org-region-active-p)
  18176. (setq beg (point-at-bol))
  18177. (beginning-of-line 2)
  18178. (while (and (not (eobp)) ;; this is like `next-line'
  18179. (get-char-property (1- (point)) 'invisible))
  18180. (beginning-of-line 2))
  18181. (setq end (point))
  18182. (goto-char beg)
  18183. (goto-char (point-at-eol))
  18184. (setq end (max end (point)))
  18185. (while (re-search-forward re end t)
  18186. (if (get-char-property (match-beginning 0) 'invisible)
  18187. (throw 'exit t))))
  18188. nil))))
  18189. (defun org-metaup (&optional arg)
  18190. "Move subtree up or move table row up.
  18191. Calls `org-move-subtree-up' or `org-table-move-row' or
  18192. `org-move-item-up', depending on context. See the individual commands
  18193. for more information."
  18194. (interactive "P")
  18195. (cond
  18196. ((run-hook-with-args-until-success 'org-metaup-hook))
  18197. ((org-region-active-p)
  18198. (let* ((a (min (region-beginning) (region-end)))
  18199. (b (1- (max (region-beginning) (region-end))))
  18200. (c (save-excursion (goto-char a)
  18201. (move-beginning-of-line 0)))
  18202. (d (save-excursion (goto-char a)
  18203. (move-end-of-line 0) (point))))
  18204. (transpose-regions a b c d)
  18205. (goto-char c)))
  18206. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18207. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18208. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18209. (t (org-drag-element-backward))))
  18210. (defun org-metadown (&optional arg)
  18211. "Move subtree down or move table row down.
  18212. Calls `org-move-subtree-down' or `org-table-move-row' or
  18213. `org-move-item-down', depending on context. See the individual
  18214. commands for more information."
  18215. (interactive "P")
  18216. (cond
  18217. ((run-hook-with-args-until-success 'org-metadown-hook))
  18218. ((org-region-active-p)
  18219. (let* ((a (min (region-beginning) (region-end)))
  18220. (b (max (region-beginning) (region-end)))
  18221. (c (save-excursion (goto-char b)
  18222. (move-beginning-of-line 1)))
  18223. (d (save-excursion (goto-char b)
  18224. (move-end-of-line 1) (1+ (point)))))
  18225. (transpose-regions a b c d)
  18226. (goto-char d)))
  18227. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18228. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18229. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18230. (t (org-drag-element-forward))))
  18231. (defun org-shiftup (&optional arg)
  18232. "Increase item in timestamp or increase priority of current headline.
  18233. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18234. depending on context. See the individual commands for more information."
  18235. (interactive "P")
  18236. (cond
  18237. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18238. ((and org-support-shift-select (org-region-active-p))
  18239. (org-call-for-shift-select 'previous-line))
  18240. ((org-at-timestamp-p t)
  18241. (call-interactively (if org-edit-timestamp-down-means-later
  18242. 'org-timestamp-down 'org-timestamp-up)))
  18243. ((and (not (eq org-support-shift-select 'always))
  18244. org-enable-priority-commands
  18245. (org-at-heading-p))
  18246. (call-interactively 'org-priority-up))
  18247. ((and (not org-support-shift-select) (org-at-item-p))
  18248. (call-interactively 'org-previous-item))
  18249. ((org-clocktable-try-shift 'up arg))
  18250. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18251. (org-support-shift-select
  18252. (org-call-for-shift-select 'previous-line))
  18253. (t (org-shiftselect-error))))
  18254. (defun org-shiftdown (&optional arg)
  18255. "Decrease item in timestamp or decrease priority of current headline.
  18256. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18257. depending on context. See the individual commands for more information."
  18258. (interactive "P")
  18259. (cond
  18260. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18261. ((and org-support-shift-select (org-region-active-p))
  18262. (org-call-for-shift-select 'next-line))
  18263. ((org-at-timestamp-p t)
  18264. (call-interactively (if org-edit-timestamp-down-means-later
  18265. 'org-timestamp-up 'org-timestamp-down)))
  18266. ((and (not (eq org-support-shift-select 'always))
  18267. org-enable-priority-commands
  18268. (org-at-heading-p))
  18269. (call-interactively 'org-priority-down))
  18270. ((and (not org-support-shift-select) (org-at-item-p))
  18271. (call-interactively 'org-next-item))
  18272. ((org-clocktable-try-shift 'down arg))
  18273. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18274. (org-support-shift-select
  18275. (org-call-for-shift-select 'next-line))
  18276. (t (org-shiftselect-error))))
  18277. (defun org-shiftright (&optional arg)
  18278. "Cycle the thing at point or in the current line, depending on context.
  18279. Depending on context, this does one of the following:
  18280. - switch a timestamp at point one day into the future
  18281. - on a headline, switch to the next TODO keyword.
  18282. - on an item, switch entire list to the next bullet type
  18283. - on a property line, switch to the next allowed value
  18284. - on a clocktable definition line, move time block into the future"
  18285. (interactive "P")
  18286. (cond
  18287. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18288. ((and org-support-shift-select (org-region-active-p))
  18289. (org-call-for-shift-select 'forward-char))
  18290. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18291. ((and (not (eq org-support-shift-select 'always))
  18292. (org-at-heading-p))
  18293. (let ((org-inhibit-logging
  18294. (not org-treat-S-cursor-todo-selection-as-state-change))
  18295. (org-inhibit-blocking
  18296. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18297. (org-call-with-arg 'org-todo 'right)))
  18298. ((or (and org-support-shift-select
  18299. (not (eq org-support-shift-select 'always))
  18300. (org-at-item-bullet-p))
  18301. (and (not org-support-shift-select) (org-at-item-p)))
  18302. (org-call-with-arg 'org-cycle-list-bullet nil))
  18303. ((and (not (eq org-support-shift-select 'always))
  18304. (org-at-property-p))
  18305. (call-interactively 'org-property-next-allowed-value))
  18306. ((org-clocktable-try-shift 'right arg))
  18307. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18308. (org-support-shift-select
  18309. (org-call-for-shift-select 'forward-char))
  18310. (t (org-shiftselect-error))))
  18311. (defun org-shiftleft (&optional arg)
  18312. "Cycle the thing at point or in the current line, depending on context.
  18313. Depending on context, this does one of the following:
  18314. - switch a timestamp at point one day into the past
  18315. - on a headline, switch to the previous TODO keyword.
  18316. - on an item, switch entire list to the previous bullet type
  18317. - on a property line, switch to the previous allowed value
  18318. - on a clocktable definition line, move time block into the past"
  18319. (interactive "P")
  18320. (cond
  18321. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18322. ((and org-support-shift-select (org-region-active-p))
  18323. (org-call-for-shift-select 'backward-char))
  18324. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18325. ((and (not (eq org-support-shift-select 'always))
  18326. (org-at-heading-p))
  18327. (let ((org-inhibit-logging
  18328. (not org-treat-S-cursor-todo-selection-as-state-change))
  18329. (org-inhibit-blocking
  18330. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18331. (org-call-with-arg 'org-todo 'left)))
  18332. ((or (and org-support-shift-select
  18333. (not (eq org-support-shift-select 'always))
  18334. (org-at-item-bullet-p))
  18335. (and (not org-support-shift-select) (org-at-item-p)))
  18336. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18337. ((and (not (eq org-support-shift-select 'always))
  18338. (org-at-property-p))
  18339. (call-interactively 'org-property-previous-allowed-value))
  18340. ((org-clocktable-try-shift 'left arg))
  18341. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18342. (org-support-shift-select
  18343. (org-call-for-shift-select 'backward-char))
  18344. (t (org-shiftselect-error))))
  18345. (defun org-shiftcontrolright ()
  18346. "Switch to next TODO set."
  18347. (interactive)
  18348. (cond
  18349. ((and org-support-shift-select (org-region-active-p))
  18350. (org-call-for-shift-select 'forward-word))
  18351. ((and (not (eq org-support-shift-select 'always))
  18352. (org-at-heading-p))
  18353. (org-call-with-arg 'org-todo 'nextset))
  18354. (org-support-shift-select
  18355. (org-call-for-shift-select 'forward-word))
  18356. (t (org-shiftselect-error))))
  18357. (defun org-shiftcontrolleft ()
  18358. "Switch to previous TODO set."
  18359. (interactive)
  18360. (cond
  18361. ((and org-support-shift-select (org-region-active-p))
  18362. (org-call-for-shift-select 'backward-word))
  18363. ((and (not (eq org-support-shift-select 'always))
  18364. (org-at-heading-p))
  18365. (org-call-with-arg 'org-todo 'previousset))
  18366. (org-support-shift-select
  18367. (org-call-for-shift-select 'backward-word))
  18368. (t (org-shiftselect-error))))
  18369. (defun org-shiftcontrolup (&optional n)
  18370. "Change timestamps synchronously up in CLOCK log lines.
  18371. Optional argument N tells to change by that many units."
  18372. (interactive "P")
  18373. (cond ((and (not org-support-shift-select)
  18374. (org-at-clock-log-p)
  18375. (org-at-timestamp-p t))
  18376. (org-clock-timestamps-up n))
  18377. (t (org-shiftselect-error))))
  18378. (defun org-shiftcontroldown (&optional n)
  18379. "Change timestamps synchronously down in CLOCK log lines.
  18380. Optional argument N tells to change by that many units."
  18381. (interactive "P")
  18382. (cond ((and (not org-support-shift-select)
  18383. (org-at-clock-log-p)
  18384. (org-at-timestamp-p t))
  18385. (org-clock-timestamps-down n))
  18386. (t (org-shiftselect-error))))
  18387. (defun org-ctrl-c-ret ()
  18388. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18389. (interactive)
  18390. (cond
  18391. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18392. (t (call-interactively 'org-insert-heading))))
  18393. (defun org-find-visible ()
  18394. (let ((s (point)))
  18395. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18396. (get-char-property s 'invisible)))
  18397. s))
  18398. (defun org-find-invisible ()
  18399. (let ((s (point)))
  18400. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18401. (not (get-char-property s 'invisible))))
  18402. s))
  18403. (defun org-copy-visible (beg end)
  18404. "Copy the visible parts of the region."
  18405. (interactive "r")
  18406. (let (snippets s)
  18407. (save-excursion
  18408. (save-restriction
  18409. (narrow-to-region beg end)
  18410. (setq s (goto-char (point-min)))
  18411. (while (not (= (point) (point-max)))
  18412. (goto-char (org-find-invisible))
  18413. (push (buffer-substring s (point)) snippets)
  18414. (setq s (goto-char (org-find-visible))))))
  18415. (kill-new (apply 'concat (nreverse snippets)))))
  18416. (defun org-copy-special ()
  18417. "Copy region in table or copy current subtree.
  18418. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18419. See the individual commands for more information."
  18420. (interactive)
  18421. (call-interactively
  18422. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18423. (defun org-cut-special ()
  18424. "Cut region in table or cut current subtree.
  18425. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18426. See the individual commands for more information."
  18427. (interactive)
  18428. (call-interactively
  18429. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18430. (defun org-paste-special (arg)
  18431. "Paste rectangular region into table, or past subtree relative to level.
  18432. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18433. See the individual commands for more information."
  18434. (interactive "P")
  18435. (if (org-at-table-p)
  18436. (org-table-paste-rectangle)
  18437. (org-paste-subtree arg)))
  18438. (defsubst org-in-fixed-width-region-p ()
  18439. "Is point in a fixed-width region?"
  18440. (save-match-data
  18441. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18442. (defun org-edit-special (&optional arg)
  18443. "Call a special editor for the element at point.
  18444. When at a table, call the formula editor with `org-table-edit-formulas'.
  18445. When in a source code block, call `org-edit-src-code'.
  18446. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18447. When at an #+INCLUDE keyword, visit the included file.
  18448. On a link, call `ffap' to visit the link at point.
  18449. Otherwise, return a user error."
  18450. (interactive "P")
  18451. (let ((element (org-element-at-point)))
  18452. (assert (not buffer-read-only) nil
  18453. "Buffer is read-only: %s" (buffer-name))
  18454. (case (org-element-type element)
  18455. (src-block
  18456. (if (not arg) (org-edit-src-code)
  18457. (let* ((info (org-babel-get-src-block-info))
  18458. (lang (nth 0 info))
  18459. (params (nth 2 info))
  18460. (session (cdr (assq :session params))))
  18461. (if (not session) (org-edit-src-code)
  18462. ;; At a src-block with a session and function called with
  18463. ;; an ARG: switch to the buffer related to the inferior
  18464. ;; process.
  18465. (switch-to-buffer
  18466. (funcall (intern (concat "org-babel-prep-session:" lang))
  18467. session params))))))
  18468. (keyword
  18469. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18470. (find-file
  18471. (org-remove-double-quotes
  18472. (car (org-split-string (org-element-property :value element)))))
  18473. (user-error "No special environment to edit here")))
  18474. (table
  18475. (if (eq (org-element-property :type element) 'table.el)
  18476. (org-edit-src-code)
  18477. (call-interactively 'org-table-edit-formulas)))
  18478. ;; Only Org tables contain `table-row' type elements.
  18479. (table-row (call-interactively 'org-table-edit-formulas))
  18480. ((example-block export-block) (org-edit-src-code))
  18481. (fixed-width (org-edit-fixed-width-region))
  18482. (otherwise
  18483. ;; No notable element at point. Though, we may be at a link,
  18484. ;; which is an object. Thus, scan deeper.
  18485. (if (eq (org-element-type (org-element-context element)) 'link)
  18486. (call-interactively 'ffap)
  18487. (user-error "No special environment to edit here"))))))
  18488. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18489. (defun org-ctrl-c-ctrl-c (&optional arg)
  18490. "Set tags in headline, or update according to changed information at point.
  18491. This command does many different things, depending on context:
  18492. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18493. this is what we do.
  18494. - If the cursor is on a statistics cookie, update it.
  18495. - If the cursor is in a headline, prompt for tags and insert them
  18496. into the current line, aligned to `org-tags-column'. When called
  18497. with prefix arg, realign all tags in the current buffer.
  18498. - If the cursor is in one of the special #+KEYWORD lines, this
  18499. triggers scanning the buffer for these lines and updating the
  18500. information.
  18501. - If the cursor is inside a table, realign the table. This command
  18502. works even if the automatic table editor has been turned off.
  18503. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18504. the entire table.
  18505. - If the cursor is at a footnote reference or definition, jump to
  18506. the corresponding definition or references, respectively.
  18507. - If the cursor is a the beginning of a dynamic block, update it.
  18508. - If the current buffer is a capture buffer, close note and file it.
  18509. - If the cursor is on a <<<target>>>, update radio targets and
  18510. corresponding links in this buffer.
  18511. - If the cursor is on a numbered item in a plain list, renumber the
  18512. ordered list.
  18513. - If the cursor is on a checkbox, toggle it.
  18514. - If the cursor is on a code block, evaluate it. The variable
  18515. `org-confirm-babel-evaluate' can be used to control prompting
  18516. before code block evaluation, by default every code block
  18517. evaluation requires confirmation. Code block evaluation can be
  18518. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18519. (interactive "P")
  18520. (cond
  18521. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18522. org-occur-highlights
  18523. org-latex-fragment-image-overlays)
  18524. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18525. (org-remove-occur-highlights)
  18526. (org-remove-latex-fragment-image-overlays)
  18527. (message "Temporary highlights/overlays removed from current buffer"))
  18528. ((and (local-variable-p 'org-finish-function (current-buffer))
  18529. (fboundp org-finish-function))
  18530. (funcall org-finish-function))
  18531. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18532. (t
  18533. (let* ((context (org-element-context)) (type (org-element-type context)))
  18534. ;; Test if point is within a blank line.
  18535. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18536. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18537. (user-error "C-c C-c can do nothing useful at this location"))
  18538. ;; For convenience: at the first line of a paragraph on the
  18539. ;; same line as an item, apply function on that item instead.
  18540. (when (eq type 'paragraph)
  18541. (let ((parent (org-element-property :parent context)))
  18542. (when (and (eq (org-element-type parent) 'item)
  18543. (= (point-at-bol) (org-element-property :begin parent)))
  18544. (setq context parent type 'item))))
  18545. ;; Act according to type of element or object at point.
  18546. (case type
  18547. (clock (org-clock-update-time-maybe))
  18548. (dynamic-block
  18549. (save-excursion
  18550. (goto-char (org-element-property :post-affiliated context))
  18551. (org-update-dblock)))
  18552. (footnote-definition
  18553. (goto-char (org-element-property :post-affiliated context))
  18554. (call-interactively 'org-footnote-action))
  18555. (footnote-reference (call-interactively 'org-footnote-action))
  18556. ((headline inlinetask)
  18557. (save-excursion (goto-char (org-element-property :begin context))
  18558. (call-interactively 'org-set-tags)))
  18559. (item
  18560. ;; At an item: a double C-u set checkbox to "[-]"
  18561. ;; unconditionally, whereas a single one will toggle its
  18562. ;; presence. Without an universal argument, if the item
  18563. ;; has a checkbox, toggle it. Otherwise repair the list.
  18564. (let* ((box (org-element-property :checkbox context))
  18565. (struct (org-element-property :structure context))
  18566. (old-struct (copy-tree struct))
  18567. (parents (org-list-parents-alist struct))
  18568. (prevs (org-list-prevs-alist struct))
  18569. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18570. (org-list-set-checkbox
  18571. (org-element-property :begin context) struct
  18572. (cond ((equal arg '(16)) "[-]")
  18573. ((and (not box) (equal arg '(4))) "[ ]")
  18574. ((or (not box) (equal arg '(4))) nil)
  18575. ((eq box 'on) "[ ]")
  18576. (t "[X]")))
  18577. ;; Mimic `org-list-write-struct' but with grabbing
  18578. ;; a return value from `org-list-struct-fix-box'.
  18579. (org-list-struct-fix-ind struct parents 2)
  18580. (org-list-struct-fix-item-end struct)
  18581. (org-list-struct-fix-bul struct prevs)
  18582. (org-list-struct-fix-ind struct parents)
  18583. (let ((block-item
  18584. (org-list-struct-fix-box struct parents prevs orderedp)))
  18585. (if (and box (equal struct old-struct))
  18586. (if (equal arg '(16))
  18587. (message "Checkboxes already reset")
  18588. (user-error "Cannot toggle this checkbox: %s"
  18589. (if (eq box 'on)
  18590. "all subitems checked"
  18591. "unchecked subitems")))
  18592. (org-list-struct-apply-struct struct old-struct)
  18593. (org-update-checkbox-count-maybe))
  18594. (when block-item
  18595. (message "Checkboxes were removed due to empty box at line %d"
  18596. (org-current-line block-item))))))
  18597. (keyword
  18598. (let ((org-inhibit-startup-visibility-stuff t)
  18599. (org-startup-align-all-tables nil))
  18600. (when (boundp 'org-table-coordinate-overlays)
  18601. (mapc 'delete-overlay org-table-coordinate-overlays)
  18602. (setq org-table-coordinate-overlays nil))
  18603. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18604. (message "Local setup has been refreshed"))
  18605. (plain-list
  18606. ;; At a plain list, with a double C-u argument, set
  18607. ;; checkboxes of each item to "[-]", whereas a single one
  18608. ;; will toggle their presence according to the state of the
  18609. ;; first item in the list. Without an argument, repair the
  18610. ;; list.
  18611. (let* ((begin (org-element-property :contents-begin context))
  18612. (beginm (move-marker (make-marker) begin))
  18613. (struct (org-element-property :structure context))
  18614. (old-struct (copy-tree struct))
  18615. (first-box (save-excursion
  18616. (goto-char begin)
  18617. (looking-at org-list-full-item-re)
  18618. (match-string-no-properties 3)))
  18619. (new-box (cond ((equal arg '(16)) "[-]")
  18620. ((equal arg '(4)) (unless first-box "[ ]"))
  18621. ((equal first-box "[X]") "[ ]")
  18622. (t "[X]"))))
  18623. (cond
  18624. (arg
  18625. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18626. (org-list-get-all-items
  18627. begin struct (org-list-prevs-alist struct))))
  18628. ((and first-box (eq (point) begin))
  18629. ;; For convenience, when point is at bol on the first
  18630. ;; item of the list and no argument is provided, simply
  18631. ;; toggle checkbox of that item, if any.
  18632. (org-list-set-checkbox begin struct new-box)))
  18633. (org-list-write-struct
  18634. struct (org-list-parents-alist struct) old-struct)
  18635. (org-update-checkbox-count-maybe)
  18636. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18637. ((property-drawer node-property)
  18638. (call-interactively 'org-property-action))
  18639. ((radio-target target)
  18640. (call-interactively 'org-update-radio-target-regexp))
  18641. (statistics-cookie
  18642. (call-interactively 'org-update-statistics-cookies))
  18643. ((table table-cell table-row)
  18644. ;; At a table, recalculate every field and align it. Also
  18645. ;; send the table if necessary. If the table has
  18646. ;; a `table.el' type, just give up. At a table row or
  18647. ;; cell, maybe recalculate line but always align table.
  18648. (if (eq (org-element-property :type context) 'table.el)
  18649. (message "Use C-c ' to edit table.el tables")
  18650. (let ((org-enable-table-editor t))
  18651. (if (or (eq type 'table)
  18652. ;; Check if point is at a TBLFM line.
  18653. (and (eq type 'table-row)
  18654. (= (point) (org-element-property :end context))))
  18655. (save-excursion
  18656. (if (org-at-TBLFM-p)
  18657. (progn (require 'org-table)
  18658. (org-table-calc-current-TBLFM))
  18659. (goto-char (org-element-property :contents-begin context))
  18660. (org-call-with-arg 'org-table-recalculate (or arg t))
  18661. (orgtbl-send-table 'maybe)))
  18662. (org-table-maybe-eval-formula)
  18663. (cond (arg (call-interactively 'org-table-recalculate))
  18664. ((org-table-maybe-recalculate-line))
  18665. (t (org-table-align)))))))
  18666. (timestamp (org-timestamp-change 0 'day))
  18667. (otherwise
  18668. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18669. (user-error
  18670. "C-c C-c can do nothing useful at this location")))))))))
  18671. (defun org-mode-restart ()
  18672. "Restart Org-mode, to scan again for special lines.
  18673. Also updates the keyword regular expressions."
  18674. (interactive)
  18675. (org-mode)
  18676. (message "Org-mode restarted"))
  18677. (defun org-kill-note-or-show-branches ()
  18678. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18679. (interactive)
  18680. (if (not org-finish-function)
  18681. (progn
  18682. (hide-subtree)
  18683. (call-interactively 'show-branches))
  18684. (let ((org-note-abort t))
  18685. (funcall org-finish-function))))
  18686. (defun org-open-line (n)
  18687. "Insert a new row in tables, call `open-line' elsewhere."
  18688. (interactive "*p")
  18689. (if (org-at-table-p)
  18690. (org-table-insert-row)
  18691. (open-line n)))
  18692. (defun org-return (&optional indent)
  18693. "Goto next table row or insert a newline.
  18694. Calls `org-table-next-row' or `newline', depending on context.
  18695. See the individual commands for more information."
  18696. (interactive)
  18697. (let (org-ts-what)
  18698. (cond
  18699. ((or (bobp) (org-in-src-block-p))
  18700. (if indent (newline-and-indent) (newline)))
  18701. ((org-at-table-p)
  18702. (org-table-justify-field-maybe)
  18703. (call-interactively 'org-table-next-row))
  18704. ;; when `newline-and-indent' is called within a list, make sure
  18705. ;; text moved stays inside the item.
  18706. ((and (org-in-item-p) indent)
  18707. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18708. (progn
  18709. (save-match-data (newline))
  18710. (org-indent-line-to (length (match-string 0))))
  18711. (let ((ind (org-get-indentation)))
  18712. (newline)
  18713. (if (org-looking-back org-list-end-re)
  18714. (org-indent-line)
  18715. (org-indent-line-to ind)))))
  18716. ((and org-return-follows-link
  18717. (org-at-timestamp-p t)
  18718. (not (eq org-ts-what 'after)))
  18719. (org-follow-timestamp-link))
  18720. ((and org-return-follows-link
  18721. (let ((tprop (get-text-property (point) 'face)))
  18722. (or (eq tprop 'org-link)
  18723. (and (listp tprop) (memq 'org-link tprop)))))
  18724. (call-interactively 'org-open-at-point))
  18725. ((and (org-at-heading-p)
  18726. (looking-at
  18727. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18728. (org-show-entry)
  18729. (end-of-line 1)
  18730. (newline))
  18731. (t (if indent (newline-and-indent) (newline))))))
  18732. (defun org-return-indent ()
  18733. "Goto next table row or insert a newline and indent.
  18734. Calls `org-table-next-row' or `newline-and-indent', depending on
  18735. context. See the individual commands for more information."
  18736. (interactive)
  18737. (org-return t))
  18738. (defun org-ctrl-c-star ()
  18739. "Compute table, or change heading status of lines.
  18740. Calls `org-table-recalculate' or `org-toggle-heading',
  18741. depending on context."
  18742. (interactive)
  18743. (cond
  18744. ((org-at-table-p)
  18745. (call-interactively 'org-table-recalculate))
  18746. (t
  18747. ;; Convert all lines in region to list items
  18748. (call-interactively 'org-toggle-heading))))
  18749. (defun org-ctrl-c-minus ()
  18750. "Insert separator line in table or modify bullet status of line.
  18751. Also turns a plain line or a region of lines into list items.
  18752. Calls `org-table-insert-hline', `org-toggle-item', or
  18753. `org-cycle-list-bullet', depending on context."
  18754. (interactive)
  18755. (cond
  18756. ((org-at-table-p)
  18757. (call-interactively 'org-table-insert-hline))
  18758. ((org-region-active-p)
  18759. (call-interactively 'org-toggle-item))
  18760. ((org-in-item-p)
  18761. (call-interactively 'org-cycle-list-bullet))
  18762. (t
  18763. (call-interactively 'org-toggle-item))))
  18764. (defun org-toggle-item (arg)
  18765. "Convert headings or normal lines to items, items to normal lines.
  18766. If there is no active region, only the current line is considered.
  18767. If the first non blank line in the region is a headline, convert
  18768. all headlines to items, shifting text accordingly.
  18769. If it is an item, convert all items to normal lines.
  18770. If it is normal text, change region into a list of items.
  18771. With a prefix argument ARG, change the region in a single item."
  18772. (interactive "P")
  18773. (let ((shift-text
  18774. (function
  18775. ;; Shift text in current section to IND, from point to END.
  18776. ;; The function leaves point to END line.
  18777. (lambda (ind end)
  18778. (let ((min-i 1000) (end (copy-marker end)))
  18779. ;; First determine the minimum indentation (MIN-I) of
  18780. ;; the text.
  18781. (save-excursion
  18782. (catch 'exit
  18783. (while (< (point) end)
  18784. (let ((i (org-get-indentation)))
  18785. (cond
  18786. ;; Skip blank lines and inline tasks.
  18787. ((looking-at "^[ \t]*$"))
  18788. ((looking-at org-outline-regexp-bol))
  18789. ;; We can't find less than 0 indentation.
  18790. ((zerop i) (throw 'exit (setq min-i 0)))
  18791. ((< i min-i) (setq min-i i))))
  18792. (forward-line))))
  18793. ;; Then indent each line so that a line indented to
  18794. ;; MIN-I becomes indented to IND. Ignore blank lines
  18795. ;; and inline tasks in the process.
  18796. (let ((delta (- ind min-i)))
  18797. (while (< (point) end)
  18798. (unless (or (looking-at "^[ \t]*$")
  18799. (looking-at org-outline-regexp-bol))
  18800. (org-indent-line-to (+ (org-get-indentation) delta)))
  18801. (forward-line)))))))
  18802. (skip-blanks
  18803. (function
  18804. ;; Return beginning of first non-blank line, starting from
  18805. ;; line at POS.
  18806. (lambda (pos)
  18807. (save-excursion
  18808. (goto-char pos)
  18809. (skip-chars-forward " \r\t\n")
  18810. (point-at-bol)))))
  18811. beg end)
  18812. ;; Determine boundaries of changes.
  18813. (if (org-region-active-p)
  18814. (setq beg (funcall skip-blanks (region-beginning))
  18815. end (copy-marker (region-end)))
  18816. (setq beg (funcall skip-blanks (point-at-bol))
  18817. end (copy-marker (point-at-eol))))
  18818. ;; Depending on the starting line, choose an action on the text
  18819. ;; between BEG and END.
  18820. (org-with-limited-levels
  18821. (save-excursion
  18822. (goto-char beg)
  18823. (cond
  18824. ;; Case 1. Start at an item: de-itemize. Note that it only
  18825. ;; happens when a region is active: `org-ctrl-c-minus'
  18826. ;; would call `org-cycle-list-bullet' otherwise.
  18827. ((org-at-item-p)
  18828. (while (< (point) end)
  18829. (when (org-at-item-p)
  18830. (skip-chars-forward " \t")
  18831. (delete-region (point) (match-end 0)))
  18832. (forward-line)))
  18833. ;; Case 2. Start at an heading: convert to items.
  18834. ((org-at-heading-p)
  18835. (let* ((bul (org-list-bullet-string "-"))
  18836. (bul-len (length bul))
  18837. ;; Indentation of the first heading. It should be
  18838. ;; relative to the indentation of its parent, if any.
  18839. (start-ind (save-excursion
  18840. (cond
  18841. ((not org-adapt-indentation) 0)
  18842. ((not (outline-previous-heading)) 0)
  18843. (t (length (match-string 0))))))
  18844. ;; Level of first heading. Further headings will be
  18845. ;; compared to it to determine hierarchy in the list.
  18846. (ref-level (org-reduced-level (org-outline-level))))
  18847. (while (< (point) end)
  18848. (let* ((level (org-reduced-level (org-outline-level)))
  18849. (delta (max 0 (- level ref-level))))
  18850. ;; If current headline is less indented than the first
  18851. ;; one, set it as reference, in order to preserve
  18852. ;; subtrees.
  18853. (when (< level ref-level) (setq ref-level level))
  18854. (replace-match bul t t)
  18855. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18856. ;; Ensure all text down to END (or SECTION-END) belongs
  18857. ;; to the newly created item.
  18858. (let ((section-end (save-excursion
  18859. (or (outline-next-heading) (point)))))
  18860. (forward-line)
  18861. (funcall shift-text
  18862. (+ start-ind (* (1+ delta) bul-len))
  18863. (min end section-end)))))))
  18864. ;; Case 3. Normal line with ARG: make the first line of region
  18865. ;; an item, and shift indentation of others lines to
  18866. ;; set them as item's body.
  18867. (arg (let* ((bul (org-list-bullet-string "-"))
  18868. (bul-len (length bul))
  18869. (ref-ind (org-get-indentation)))
  18870. (skip-chars-forward " \t")
  18871. (insert bul)
  18872. (forward-line)
  18873. (while (< (point) end)
  18874. ;; Ensure that lines less indented than first one
  18875. ;; still get included in item body.
  18876. (funcall shift-text
  18877. (+ ref-ind bul-len)
  18878. (min end (save-excursion (or (outline-next-heading)
  18879. (point)))))
  18880. (forward-line))))
  18881. ;; Case 4. Normal line without ARG: turn each non-item line
  18882. ;; into an item.
  18883. (t
  18884. (while (< (point) end)
  18885. (unless (or (org-at-heading-p) (org-at-item-p))
  18886. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18887. (replace-match
  18888. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18889. (forward-line))))))))
  18890. (defun org-toggle-heading (&optional nstars)
  18891. "Convert headings to normal text, or items or text to headings.
  18892. If there is no active region, only convert the current line.
  18893. With a \\[universal-argument] prefix, convert the whole list at
  18894. point into heading.
  18895. In a region:
  18896. - If the first non blank line is a headline, remove the stars
  18897. from all headlines in the region.
  18898. - If it is a normal line, turn each and every normal line (i.e.,
  18899. not an heading or an item) in the region into headings. If you
  18900. want to convert only the first line of this region, use one
  18901. universal prefix argument.
  18902. - If it is a plain list item, turn all plain list items into headings.
  18903. When converting a line into a heading, the number of stars is chosen
  18904. such that the lines become children of the current entry. However,
  18905. when a numeric prefix argument is given, its value determines the
  18906. number of stars to add."
  18907. (interactive "P")
  18908. (let ((skip-blanks
  18909. (function
  18910. ;; Return beginning of first non-blank line, starting from
  18911. ;; line at POS.
  18912. (lambda (pos)
  18913. (save-excursion
  18914. (goto-char pos)
  18915. (while (org-at-comment-p) (forward-line))
  18916. (skip-chars-forward " \r\t\n")
  18917. (point-at-bol)))))
  18918. beg end toggled)
  18919. ;; Determine boundaries of changes. If a universal prefix has
  18920. ;; been given, put the list in a region. If region ends at a bol,
  18921. ;; do not consider the last line to be in the region.
  18922. (when (and current-prefix-arg (org-at-item-p))
  18923. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18924. (org-mark-element))
  18925. (if (org-region-active-p)
  18926. (setq beg (funcall skip-blanks (region-beginning))
  18927. end (copy-marker (save-excursion
  18928. (goto-char (region-end))
  18929. (if (bolp) (point) (point-at-eol)))))
  18930. (setq beg (funcall skip-blanks (point-at-bol))
  18931. end (copy-marker (point-at-eol))))
  18932. ;; Ensure inline tasks don't count as headings.
  18933. (org-with-limited-levels
  18934. (save-excursion
  18935. (goto-char beg)
  18936. (cond
  18937. ;; Case 1. Started at an heading: de-star headings.
  18938. ((org-at-heading-p)
  18939. (while (< (point) end)
  18940. (when (org-at-heading-p t)
  18941. (looking-at org-outline-regexp) (replace-match "")
  18942. (setq toggled t))
  18943. (forward-line)))
  18944. ;; Case 2. Started at an item: change items into headlines.
  18945. ;; One star will be added by `org-list-to-subtree'.
  18946. ((org-at-item-p)
  18947. (let* ((stars (make-string
  18948. ;; subtract the star that will be added again by
  18949. ;; `org-list-to-subtree'
  18950. (if (numberp nstars) (1- nstars)
  18951. (or (org-current-level) 0))
  18952. ?*))
  18953. (add-stars
  18954. (cond (nstars "") ; stars from prefix only
  18955. ((equal stars "") "") ; before first heading
  18956. (org-odd-levels-only "*") ; inside heading, odd
  18957. (t "")))) ; inside heading, oddeven
  18958. (while (< (point) end)
  18959. (when (org-at-item-p)
  18960. ;; Pay attention to cases when region ends before list.
  18961. (let* ((struct (org-list-struct))
  18962. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18963. (save-restriction
  18964. (narrow-to-region (point) list-end)
  18965. (insert
  18966. (org-list-to-subtree
  18967. (org-list-parse-list t)
  18968. '(:istart (concat stars add-stars (funcall get-stars depth))
  18969. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18970. (setq toggled t))
  18971. (forward-line))))
  18972. ;; Case 3. Started at normal text: make every line an heading,
  18973. ;; skipping headlines and items.
  18974. (t (let* ((stars
  18975. (make-string
  18976. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18977. (add-stars
  18978. (cond (nstars "") ; stars from prefix only
  18979. ((equal stars "") "*") ; before first heading
  18980. (org-odd-levels-only "**") ; inside heading, odd
  18981. (t "*"))) ; inside heading, oddeven
  18982. (rpl (concat stars add-stars " "))
  18983. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18984. (while (< (point) (if (equal nstars '(4)) lend end))
  18985. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18986. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18987. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18988. (forward-line)))))))
  18989. (unless toggled (message "Cannot toggle heading from here"))))
  18990. (defun org-meta-return (&optional arg)
  18991. "Insert a new heading or wrap a region in a table.
  18992. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18993. See the individual commands for more information."
  18994. (interactive "P")
  18995. (org-check-before-invisible-edit 'insert)
  18996. (cond
  18997. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18998. ((or (org-at-drawer-p) (org-in-drawer-p) (org-at-property-p))
  18999. (newline-and-indent))
  19000. ((org-at-table-p)
  19001. (call-interactively 'org-table-wrap-region))
  19002. (t (call-interactively 'org-insert-heading))))
  19003. ;;; Menu entries
  19004. (defsubst org-in-subtree-not-table-p ()
  19005. "Are we in a subtree and not in a table?"
  19006. (and (not (org-before-first-heading-p))
  19007. (not (org-at-table-p))))
  19008. ;; Define the Org-mode menus
  19009. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19010. '("Tbl"
  19011. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19012. ["Next Field" org-cycle (org-at-table-p)]
  19013. ["Previous Field" org-shifttab (org-at-table-p)]
  19014. ["Next Row" org-return (org-at-table-p)]
  19015. "--"
  19016. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19017. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19018. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19019. "--"
  19020. ("Column"
  19021. ["Move Column Left" org-metaleft (org-at-table-p)]
  19022. ["Move Column Right" org-metaright (org-at-table-p)]
  19023. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19024. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19025. ("Row"
  19026. ["Move Row Up" org-metaup (org-at-table-p)]
  19027. ["Move Row Down" org-metadown (org-at-table-p)]
  19028. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19029. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19030. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19031. "--"
  19032. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19033. ("Rectangle"
  19034. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19035. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19036. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19037. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19038. "--"
  19039. ("Calculate"
  19040. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19041. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19042. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19043. "--"
  19044. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19045. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19046. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19047. "--"
  19048. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19049. "--"
  19050. ["Sum Column/Rectangle" org-table-sum
  19051. (or (org-at-table-p) (org-region-active-p))]
  19052. ["Which Column?" org-table-current-column (org-at-table-p)])
  19053. ["Debug Formulas"
  19054. org-table-toggle-formula-debugger
  19055. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19056. ["Show Col/Row Numbers"
  19057. org-table-toggle-coordinate-overlays
  19058. :style toggle
  19059. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19060. "--"
  19061. ["Create" org-table-create (and (not (org-at-table-p))
  19062. org-enable-table-editor)]
  19063. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19064. ["Import from File" org-table-import (not (org-at-table-p))]
  19065. ["Export to File" org-table-export (org-at-table-p)]
  19066. "--"
  19067. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19068. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19069. '("Org"
  19070. ("Show/Hide"
  19071. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19072. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19073. ["Sparse Tree..." org-sparse-tree t]
  19074. ["Reveal Context" org-reveal t]
  19075. ["Show All" show-all t]
  19076. "--"
  19077. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19078. "--"
  19079. ["New Heading" org-insert-heading t]
  19080. ("Navigate Headings"
  19081. ["Up" outline-up-heading t]
  19082. ["Next" outline-next-visible-heading t]
  19083. ["Previous" outline-previous-visible-heading t]
  19084. ["Next Same Level" outline-forward-same-level t]
  19085. ["Previous Same Level" outline-backward-same-level t]
  19086. "--"
  19087. ["Jump" org-goto t])
  19088. ("Edit Structure"
  19089. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19090. "--"
  19091. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19092. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19093. "--"
  19094. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19095. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19096. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19097. "--"
  19098. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19099. "--"
  19100. ["Copy visible text" org-copy-visible t]
  19101. "--"
  19102. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19103. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19104. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19105. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19106. "--"
  19107. ["Sort Region/Children" org-sort t]
  19108. "--"
  19109. ["Convert to odd levels" org-convert-to-odd-levels t]
  19110. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19111. ("Editing"
  19112. ["Emphasis..." org-emphasize t]
  19113. ["Edit Source Example" org-edit-special t]
  19114. "--"
  19115. ["Footnote new/jump" org-footnote-action t]
  19116. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19117. ("Archive"
  19118. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19119. "--"
  19120. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19121. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19122. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19123. )
  19124. "--"
  19125. ("Hyperlinks"
  19126. ["Store Link (Global)" org-store-link t]
  19127. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19128. ["Insert Link" org-insert-link t]
  19129. ["Follow Link" org-open-at-point t]
  19130. "--"
  19131. ["Next link" org-next-link t]
  19132. ["Previous link" org-previous-link t]
  19133. "--"
  19134. ["Descriptive Links"
  19135. org-toggle-link-display
  19136. :style radio
  19137. :selected org-descriptive-links
  19138. ]
  19139. ["Literal Links"
  19140. org-toggle-link-display
  19141. :style radio
  19142. :selected (not org-descriptive-links)])
  19143. "--"
  19144. ("TODO Lists"
  19145. ["TODO/DONE/-" org-todo t]
  19146. ("Select keyword"
  19147. ["Next keyword" org-shiftright (org-at-heading-p)]
  19148. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19149. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19150. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19151. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19152. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19153. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19154. "--"
  19155. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19156. :selected org-enforce-todo-dependencies :style toggle :active t]
  19157. "Settings for tree at point"
  19158. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19159. :selected (org-entry-get nil "ORDERED")
  19160. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19161. ["Do Children parallel" org-toggle-ordered-property :style radio
  19162. :selected (not (org-entry-get nil "ORDERED"))
  19163. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19164. "--"
  19165. ["Set Priority" org-priority t]
  19166. ["Priority Up" org-shiftup t]
  19167. ["Priority Down" org-shiftdown t]
  19168. "--"
  19169. ["Get news from all feeds" org-feed-update-all t]
  19170. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19171. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19172. ("TAGS and Properties"
  19173. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19174. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19175. "--"
  19176. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19177. ["Column view of properties" org-columns t]
  19178. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19179. ("Dates and Scheduling"
  19180. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19181. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19182. ("Change Date"
  19183. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19184. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19185. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19186. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19187. ["Compute Time Range" org-evaluate-time-range t]
  19188. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19189. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19190. "--"
  19191. ["Custom time format" org-toggle-time-stamp-overlays
  19192. :style radio :selected org-display-custom-times]
  19193. "--"
  19194. ["Goto Calendar" org-goto-calendar t]
  19195. ["Date from Calendar" org-date-from-calendar t]
  19196. "--"
  19197. ["Start/Restart Timer" org-timer-start t]
  19198. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19199. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19200. ["Insert Timer String" org-timer t]
  19201. ["Insert Timer Item" org-timer-item t])
  19202. ("Logging work"
  19203. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19204. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19205. ["Clock out" org-clock-out t]
  19206. ["Clock cancel" org-clock-cancel t]
  19207. "--"
  19208. ["Mark as default task" org-clock-mark-default-task t]
  19209. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19210. ["Goto running clock" org-clock-goto t]
  19211. "--"
  19212. ["Display times" org-clock-display t]
  19213. ["Create clock table" org-clock-report t]
  19214. "--"
  19215. ["Record DONE time"
  19216. (progn (setq org-log-done (not org-log-done))
  19217. (message "Switching to %s will %s record a timestamp"
  19218. (car org-done-keywords)
  19219. (if org-log-done "automatically" "not")))
  19220. :style toggle :selected org-log-done])
  19221. "--"
  19222. ["Agenda Command..." org-agenda t]
  19223. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19224. ("File List for Agenda")
  19225. ("Special views current file"
  19226. ["TODO Tree" org-show-todo-tree t]
  19227. ["Check Deadlines" org-check-deadlines t]
  19228. ["Timeline" org-timeline t]
  19229. ["Tags/Property tree" org-match-sparse-tree t])
  19230. "--"
  19231. ["Export/Publish..." org-export-dispatch t]
  19232. ("LaTeX"
  19233. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19234. :selected org-cdlatex-mode]
  19235. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19236. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19237. ["Modify math symbol" org-cdlatex-math-modify
  19238. (org-inside-LaTeX-fragment-p)]
  19239. ["Insert citation" org-reftex-citation t]
  19240. "--"
  19241. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19242. "--"
  19243. ("MobileOrg"
  19244. ["Push Files and Views" org-mobile-push t]
  19245. ["Get Captured and Flagged" org-mobile-pull t]
  19246. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19247. "--"
  19248. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19249. "--"
  19250. ("Documentation"
  19251. ["Show Version" org-version t]
  19252. ["Info Documentation" org-info t])
  19253. ("Customize"
  19254. ["Browse Org Group" org-customize t]
  19255. "--"
  19256. ["Expand This Menu" org-create-customize-menu
  19257. (fboundp 'customize-menu-create)])
  19258. ["Send bug report" org-submit-bug-report t]
  19259. "--"
  19260. ("Refresh/Reload"
  19261. ["Refresh setup current buffer" org-mode-restart t]
  19262. ["Reload Org (after update)" org-reload t]
  19263. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19264. ))
  19265. (defun org-info (&optional node)
  19266. "Read documentation for Org-mode in the info system.
  19267. With optional NODE, go directly to that node."
  19268. (interactive)
  19269. (info (format "(org)%s" (or node ""))))
  19270. ;;;###autoload
  19271. (defun org-submit-bug-report ()
  19272. "Submit a bug report on Org-mode via mail.
  19273. Don't hesitate to report any problems or inaccurate documentation.
  19274. If you don't have setup sending mail from (X)Emacs, please copy the
  19275. output buffer into your mail program, as it gives us important
  19276. information about your Org-mode version and configuration."
  19277. (interactive)
  19278. (require 'reporter)
  19279. (org-load-modules-maybe)
  19280. (org-require-autoloaded-modules)
  19281. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19282. (reporter-submit-bug-report
  19283. "emacs-orgmode@gnu.org"
  19284. (org-version nil 'full)
  19285. (let (list)
  19286. (save-window-excursion
  19287. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19288. (delete-other-windows)
  19289. (erase-buffer)
  19290. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19291. We would like to add your full Org-mode and Outline configuration to the
  19292. bug report. This greatly simplifies the work of the maintainer and
  19293. other experts on the mailing list.
  19294. HOWEVER, some variables you have customized may contain private
  19295. information. The names of customers, colleagues, or friends, might
  19296. appear in the form of file names, tags, todo states, or search strings.
  19297. If you answer yes to the prompt, you might want to check and remove
  19298. such private information before sending the email.")
  19299. (add-text-properties (point-min) (point-max) '(face org-warning))
  19300. (when (yes-or-no-p "Include your Org-mode configuration ")
  19301. (mapatoms
  19302. (lambda (v)
  19303. (and (boundp v)
  19304. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19305. (or (and (symbol-value v)
  19306. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19307. (and
  19308. (get v 'custom-type) (get v 'standard-value)
  19309. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19310. (push v list)))))
  19311. (kill-buffer (get-buffer "*Warn about privacy*"))
  19312. list))
  19313. nil nil
  19314. "Remember to cover the basics, that is, what you expected to happen and
  19315. what in fact did happen. You don't know how to make a good report? See
  19316. http://orgmode.org/manual/Feedback.html#Feedback
  19317. Your bug report will be posted to the Org-mode mailing list.
  19318. ------------------------------------------------------------------------")
  19319. (save-excursion
  19320. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19321. (replace-match "\\1Bug: \\3 [\\2]")))))
  19322. (defun org-install-agenda-files-menu ()
  19323. (let ((bl (buffer-list)))
  19324. (save-excursion
  19325. (while bl
  19326. (set-buffer (pop bl))
  19327. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19328. (when (derived-mode-p 'org-mode)
  19329. (easy-menu-change
  19330. '("Org") "File List for Agenda"
  19331. (append
  19332. (list
  19333. ["Edit File List" (org-edit-agenda-file-list) t]
  19334. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19335. ["Remove Current File from List" org-remove-file t]
  19336. ["Cycle through agenda files" org-cycle-agenda-files t]
  19337. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19338. "--")
  19339. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19340. ;;;; Documentation
  19341. (defun org-require-autoloaded-modules ()
  19342. (interactive)
  19343. (mapc 'require
  19344. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19345. org-remember org-table org-timer)))
  19346. ;;;###autoload
  19347. (defun org-reload (&optional uncompiled)
  19348. "Reload all org lisp files.
  19349. With prefix arg UNCOMPILED, load the uncompiled versions."
  19350. (interactive "P")
  19351. (require 'loadhist)
  19352. (let* ((org-dir (org-find-library-dir "org"))
  19353. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19354. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19355. (remove-re (mapconcat 'identity
  19356. (mapcar (lambda (f) (concat "^" f "$"))
  19357. (list (if (featurep 'xemacs)
  19358. "org-colview"
  19359. "org-colview-xemacs")
  19360. "org" "org-loaddefs" "org-version"))
  19361. "\\|"))
  19362. (feats (delete-dups
  19363. (mapcar 'file-name-sans-extension
  19364. (mapcar 'file-name-nondirectory
  19365. (delq nil
  19366. (mapcar 'feature-file
  19367. features))))))
  19368. (lfeat (append
  19369. (sort
  19370. (setq feats
  19371. (delq nil (mapcar
  19372. (lambda (f)
  19373. (if (and (string-match feature-re f)
  19374. (not (string-match remove-re f)))
  19375. f nil))
  19376. feats)))
  19377. 'string-lessp)
  19378. (list "org-version" "org")))
  19379. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19380. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19381. load-uncore load-misses)
  19382. (setq load-misses
  19383. (delq 't
  19384. (mapcar (lambda (f)
  19385. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19386. (and (string= org-dir contrib-dir)
  19387. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19388. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19389. (add-to-list 'load-uncore f 'append)
  19390. 't)
  19391. f))
  19392. lfeat)))
  19393. (if load-uncore
  19394. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19395. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19396. (if load-misses
  19397. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19398. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19399. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19400. ;;;###autoload
  19401. (defun org-customize ()
  19402. "Call the customize function with org as argument."
  19403. (interactive)
  19404. (org-load-modules-maybe)
  19405. (org-require-autoloaded-modules)
  19406. (customize-browse 'org))
  19407. (defun org-create-customize-menu ()
  19408. "Create a full customization menu for Org-mode, insert it into the menu."
  19409. (interactive)
  19410. (org-load-modules-maybe)
  19411. (org-require-autoloaded-modules)
  19412. (if (fboundp 'customize-menu-create)
  19413. (progn
  19414. (easy-menu-change
  19415. '("Org") "Customize"
  19416. `(["Browse Org group" org-customize t]
  19417. "--"
  19418. ,(customize-menu-create 'org)
  19419. ["Set" Custom-set t]
  19420. ["Save" Custom-save t]
  19421. ["Reset to Current" Custom-reset-current t]
  19422. ["Reset to Saved" Custom-reset-saved t]
  19423. ["Reset to Standard Settings" Custom-reset-standard t]))
  19424. (message "\"Org\"-menu now contains full customization menu"))
  19425. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19426. ;;;; Miscellaneous stuff
  19427. ;;; Generally useful functions
  19428. (defun org-get-at-bol (property)
  19429. "Get text property PROPERTY at beginning of line."
  19430. (get-text-property (point-at-bol) property))
  19431. (defun org-find-text-property-in-string (prop s)
  19432. "Return the first non-nil value of property PROP in string S."
  19433. (or (get-text-property 0 prop s)
  19434. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19435. prop s)))
  19436. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19437. "Display the given MESSAGE as a warning."
  19438. (if (fboundp 'display-warning)
  19439. (display-warning 'org message
  19440. (if (featurep 'xemacs) 'warning :warning))
  19441. (let ((buf (get-buffer-create "*Org warnings*")))
  19442. (with-current-buffer buf
  19443. (goto-char (point-max))
  19444. (insert "Warning (Org): " message)
  19445. (unless (bolp)
  19446. (newline)))
  19447. (display-buffer buf)
  19448. (sit-for 0))))
  19449. (defun org-eval (form)
  19450. "Eval FORM and return result."
  19451. (condition-case error
  19452. (eval form)
  19453. (error (format "%%![Error: %s]" error))))
  19454. (defun org-in-clocktable-p ()
  19455. "Check if the cursor is in a clocktable."
  19456. (let ((pos (point)) start)
  19457. (save-excursion
  19458. (end-of-line 1)
  19459. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19460. (setq start (match-beginning 0))
  19461. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19462. (>= (match-end 0) pos)
  19463. start))))
  19464. (defun org-in-commented-line ()
  19465. "Is point in a line starting with `#'?"
  19466. (equal (char-after (point-at-bol)) ?#))
  19467. (defun org-in-indented-comment-line ()
  19468. "Is point in a line starting with `#' after some white space?"
  19469. (save-excursion
  19470. (save-match-data
  19471. (goto-char (point-at-bol))
  19472. (looking-at "[ \t]*#"))))
  19473. (defun org-in-verbatim-emphasis ()
  19474. (save-match-data
  19475. (and (org-in-regexp org-emph-re 2)
  19476. (>= (point) (match-beginning 3))
  19477. (<= (point) (match-end 4))
  19478. (member (match-string 3) '("=" "~")))))
  19479. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19480. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19481. (if (and marker (marker-buffer marker)
  19482. (buffer-live-p (marker-buffer marker)))
  19483. (progn
  19484. (org-pop-to-buffer-same-window (marker-buffer marker))
  19485. (if (or (> marker (point-max)) (< marker (point-min)))
  19486. (widen))
  19487. (goto-char marker)
  19488. (org-show-context 'org-goto))
  19489. (if bookmark
  19490. (bookmark-jump bookmark)
  19491. (error "Cannot find location"))))
  19492. (defun org-quote-csv-field (s)
  19493. "Quote field for inclusion in CSV material."
  19494. (if (string-match "[\",]" s)
  19495. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19496. s))
  19497. (defun org-force-self-insert (N)
  19498. "Needed to enforce self-insert under remapping."
  19499. (interactive "p")
  19500. (self-insert-command N))
  19501. (defun org-string-width (s)
  19502. "Compute width of string, ignoring invisible characters.
  19503. This ignores character with invisibility property `org-link', and also
  19504. characters with property `org-cwidth', because these will become invisible
  19505. upon the next fontification round."
  19506. (let (b l)
  19507. (when (or (eq t buffer-invisibility-spec)
  19508. (assq 'org-link buffer-invisibility-spec))
  19509. (while (setq b (text-property-any 0 (length s)
  19510. 'invisible 'org-link s))
  19511. (setq s (concat (substring s 0 b)
  19512. (substring s (or (next-single-property-change
  19513. b 'invisible s) (length s)))))))
  19514. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19515. (setq s (concat (substring s 0 b)
  19516. (substring s (or (next-single-property-change
  19517. b 'org-cwidth s) (length s))))))
  19518. (setq l (string-width s) b -1)
  19519. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19520. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19521. l))
  19522. (defun org-shorten-string (s maxlength)
  19523. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19524. If the string is shorter or has length MAXLENGTH, just return the
  19525. original string. If it is longer, the functions finds a space in the
  19526. string, breaks this string off at that locations and adds three dots
  19527. as ellipsis. Including the ellipsis, the string will not be longer
  19528. than MAXLENGTH. If finding a good breaking point in the string does
  19529. not work, the string is just chopped off in the middle of a word
  19530. if necessary."
  19531. (if (<= (length s) maxlength)
  19532. s
  19533. (let* ((n (max (- maxlength 4) 1))
  19534. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19535. (if (string-match re s)
  19536. (concat (match-string 1 s) "...")
  19537. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19538. (defun org-get-indentation (&optional line)
  19539. "Get the indentation of the current line, interpreting tabs.
  19540. When LINE is given, assume it represents a line and compute its indentation."
  19541. (if line
  19542. (if (string-match "^ *" (org-remove-tabs line))
  19543. (match-end 0))
  19544. (save-excursion
  19545. (beginning-of-line 1)
  19546. (skip-chars-forward " \t")
  19547. (current-column))))
  19548. (defun org-get-string-indentation (s)
  19549. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19550. (let ((n -1) (i 0) (w tab-width) c)
  19551. (catch 'exit
  19552. (while (< (setq n (1+ n)) (length s))
  19553. (setq c (aref s n))
  19554. (cond ((= c ?\ ) (setq i (1+ i)))
  19555. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19556. (t (throw 'exit t)))))
  19557. i))
  19558. (defun org-remove-tabs (s &optional width)
  19559. "Replace tabulators in S with spaces.
  19560. Assumes that s is a single line, starting in column 0."
  19561. (setq width (or width tab-width))
  19562. (while (string-match "\t" s)
  19563. (setq s (replace-match
  19564. (make-string
  19565. (- (* width (/ (+ (match-beginning 0) width) width))
  19566. (match-beginning 0)) ?\ )
  19567. t t s)))
  19568. s)
  19569. (defun org-fix-indentation (line ind)
  19570. "Fix indentation in LINE.
  19571. IND is a cons cell with target and minimum indentation.
  19572. If the current indentation in LINE is smaller than the minimum,
  19573. leave it alone. If it is larger than ind, set it to the target."
  19574. (let* ((l (org-remove-tabs line))
  19575. (i (org-get-indentation l))
  19576. (i1 (car ind)) (i2 (cdr ind)))
  19577. (if (>= i i2) (setq l (substring line i2)))
  19578. (if (> i1 0)
  19579. (concat (make-string i1 ?\ ) l)
  19580. l)))
  19581. (defun org-remove-indentation (code &optional n)
  19582. "Remove the maximum common indentation from the lines in CODE.
  19583. N may optionally be the number of spaces to remove."
  19584. (with-temp-buffer
  19585. (insert code)
  19586. (org-do-remove-indentation n)
  19587. (buffer-string)))
  19588. (defun org-do-remove-indentation (&optional n)
  19589. "Remove the maximum common indentation from the buffer."
  19590. (untabify (point-min) (point-max))
  19591. (let ((min 10000) re)
  19592. (if n
  19593. (setq min n)
  19594. (goto-char (point-min))
  19595. (while (re-search-forward "^ *[^ \n]" nil t)
  19596. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19597. (unless (or (= min 0) (= min 10000))
  19598. (setq re (format "^ \\{%d\\}" min))
  19599. (goto-char (point-min))
  19600. (while (re-search-forward re nil t)
  19601. (replace-match "")
  19602. (end-of-line 1))
  19603. min)))
  19604. (defun org-fill-template (template alist)
  19605. "Find each %key of ALIST in TEMPLATE and replace it."
  19606. (let ((case-fold-search nil)
  19607. entry key value)
  19608. (setq alist (sort (copy-sequence alist)
  19609. (lambda (a b) (< (length (car a)) (length (car b))))))
  19610. (while (setq entry (pop alist))
  19611. (setq template
  19612. (replace-regexp-in-string
  19613. (concat "%" (regexp-quote (car entry)))
  19614. (or (cdr entry) "") template t t)))
  19615. template))
  19616. (defun org-base-buffer (buffer)
  19617. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19618. (if (not buffer)
  19619. buffer
  19620. (or (buffer-base-buffer buffer)
  19621. buffer)))
  19622. (defun org-trim (s)
  19623. "Remove whitespace at beginning and end of string."
  19624. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19625. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19626. s)
  19627. (defun org-wrap (string &optional width lines)
  19628. "Wrap string to either a number of lines, or a width in characters.
  19629. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19630. that costs. If there is a word longer than WIDTH, the text is actually
  19631. wrapped to the length of that word.
  19632. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19633. many lines, whatever width that takes.
  19634. The return value is a list of lines, without newlines at the end."
  19635. (let* ((words (org-split-string string "[ \t\n]+"))
  19636. (maxword (apply 'max (mapcar 'org-string-width words)))
  19637. w ll)
  19638. (cond (width
  19639. (org-do-wrap words (max maxword width)))
  19640. (lines
  19641. (setq w maxword)
  19642. (setq ll (org-do-wrap words maxword))
  19643. (if (<= (length ll) lines)
  19644. ll
  19645. (setq ll words)
  19646. (while (> (length ll) lines)
  19647. (setq w (1+ w))
  19648. (setq ll (org-do-wrap words w)))
  19649. ll))
  19650. (t (error "Cannot wrap this")))))
  19651. (defun org-do-wrap (words width)
  19652. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19653. (let (lines line)
  19654. (while words
  19655. (setq line (pop words))
  19656. (while (and words (< (+ (length line) (length (car words))) width))
  19657. (setq line (concat line " " (pop words))))
  19658. (setq lines (push line lines)))
  19659. (nreverse lines)))
  19660. (defun org-split-string (string &optional separators)
  19661. "Splits STRING into substrings at SEPARATORS.
  19662. No empty strings are returned if there are matches at the beginning
  19663. and end of string."
  19664. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19665. (start 0)
  19666. notfirst
  19667. (list nil))
  19668. (while (and (string-match rexp string
  19669. (if (and notfirst
  19670. (= start (match-beginning 0))
  19671. (< start (length string)))
  19672. (1+ start) start))
  19673. (< (match-beginning 0) (length string)))
  19674. (setq notfirst t)
  19675. (or (eq (match-beginning 0) 0)
  19676. (and (eq (match-beginning 0) (match-end 0))
  19677. (eq (match-beginning 0) start))
  19678. (setq list
  19679. (cons (substring string start (match-beginning 0))
  19680. list)))
  19681. (setq start (match-end 0)))
  19682. (or (eq start (length string))
  19683. (setq list
  19684. (cons (substring string start)
  19685. list)))
  19686. (nreverse list)))
  19687. (defun org-quote-vert (s)
  19688. "Replace \"|\" with \"\\vert\"."
  19689. (while (string-match "|" s)
  19690. (setq s (replace-match "\\vert" t t s)))
  19691. s)
  19692. (defun org-uuidgen-p (s)
  19693. "Is S an ID created by UUIDGEN?"
  19694. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19695. (defun org-in-src-block-p (&optional inside)
  19696. "Whether point is in a code source block.
  19697. When INSIDE is non-nil, don't consider we are within a src block
  19698. when point is at #+BEGIN_SRC or #+END_SRC."
  19699. (let ((case-fold-search t) ov)
  19700. (or (and (setq ov (overlays-at (point)))
  19701. (memq 'org-block-background
  19702. (overlay-properties (car ov))))
  19703. (and (not inside)
  19704. (save-match-data
  19705. (save-excursion
  19706. (beginning-of-line)
  19707. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19708. (defun org-context ()
  19709. "Return a list of contexts of the current cursor position.
  19710. If several contexts apply, all are returned.
  19711. Each context entry is a list with a symbol naming the context, and
  19712. two positions indicating start and end of the context. Possible
  19713. contexts are:
  19714. :headline anywhere in a headline
  19715. :headline-stars on the leading stars in a headline
  19716. :todo-keyword on a TODO keyword (including DONE) in a headline
  19717. :tags on the TAGS in a headline
  19718. :priority on the priority cookie in a headline
  19719. :item on the first line of a plain list item
  19720. :item-bullet on the bullet/number of a plain list item
  19721. :checkbox on the checkbox in a plain list item
  19722. :table in an org-mode table
  19723. :table-special on a special filed in a table
  19724. :table-table in a table.el table
  19725. :clocktable in a clocktable
  19726. :src-block in a source block
  19727. :link on a hyperlink
  19728. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19729. :target on a <<target>>
  19730. :radio-target on a <<<radio-target>>>
  19731. :latex-fragment on a LaTeX fragment
  19732. :latex-preview on a LaTeX fragment with overlaid preview image
  19733. This function expects the position to be visible because it uses font-lock
  19734. faces as a help to recognize the following contexts: :table-special, :link,
  19735. and :keyword."
  19736. (let* ((f (get-text-property (point) 'face))
  19737. (faces (if (listp f) f (list f)))
  19738. (case-fold-search t)
  19739. (p (point)) clist o)
  19740. ;; First the large context
  19741. (cond
  19742. ((org-at-heading-p t)
  19743. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19744. (when (progn
  19745. (beginning-of-line 1)
  19746. (looking-at org-todo-line-tags-regexp))
  19747. (push (org-point-in-group p 1 :headline-stars) clist)
  19748. (push (org-point-in-group p 2 :todo-keyword) clist)
  19749. (push (org-point-in-group p 4 :tags) clist))
  19750. (goto-char p)
  19751. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19752. (if (looking-at "\\[#[A-Z0-9]\\]")
  19753. (push (org-point-in-group p 0 :priority) clist)))
  19754. ((org-at-item-p)
  19755. (push (org-point-in-group p 2 :item-bullet) clist)
  19756. (push (list :item (point-at-bol)
  19757. (save-excursion (org-end-of-item) (point)))
  19758. clist)
  19759. (and (org-at-item-checkbox-p)
  19760. (push (org-point-in-group p 0 :checkbox) clist)))
  19761. ((org-at-table-p)
  19762. (push (list :table (org-table-begin) (org-table-end)) clist)
  19763. (if (memq 'org-formula faces)
  19764. (push (list :table-special
  19765. (previous-single-property-change p 'face)
  19766. (next-single-property-change p 'face)) clist)))
  19767. ((org-at-table-p 'any)
  19768. (push (list :table-table) clist)))
  19769. (goto-char p)
  19770. (let ((case-fold-search t))
  19771. ;; New the "medium" contexts: clocktables, source blocks
  19772. (cond ((org-in-clocktable-p)
  19773. (push (list :clocktable
  19774. (and (or (looking-at "#\\+BEGIN: clocktable")
  19775. (search-backward "#+BEGIN: clocktable" nil t))
  19776. (match-beginning 0))
  19777. (and (re-search-forward "#\\+END:?" nil t)
  19778. (match-end 0))) clist))
  19779. ((org-in-src-block-p)
  19780. (push (list :src-block
  19781. (and (or (looking-at "#\\+BEGIN_SRC")
  19782. (search-backward "#+BEGIN_SRC" nil t))
  19783. (match-beginning 0))
  19784. (and (search-forward "#+END_SRC" nil t)
  19785. (match-beginning 0))) clist))))
  19786. (goto-char p)
  19787. ;; Now the small context
  19788. (cond
  19789. ((org-at-timestamp-p)
  19790. (push (org-point-in-group p 0 :timestamp) clist))
  19791. ((memq 'org-link faces)
  19792. (push (list :link
  19793. (previous-single-property-change p 'face)
  19794. (next-single-property-change p 'face)) clist))
  19795. ((memq 'org-special-keyword faces)
  19796. (push (list :keyword
  19797. (previous-single-property-change p 'face)
  19798. (next-single-property-change p 'face)) clist))
  19799. ((org-at-target-p)
  19800. (push (org-point-in-group p 0 :target) clist)
  19801. (goto-char (1- (match-beginning 0)))
  19802. (if (looking-at org-radio-target-regexp)
  19803. (push (org-point-in-group p 0 :radio-target) clist))
  19804. (goto-char p))
  19805. ((setq o (car (delq nil
  19806. (mapcar
  19807. (lambda (x)
  19808. (if (memq x org-latex-fragment-image-overlays) x))
  19809. (overlays-at (point))))))
  19810. (push (list :latex-fragment
  19811. (overlay-start o) (overlay-end o)) clist)
  19812. (push (list :latex-preview
  19813. (overlay-start o) (overlay-end o)) clist))
  19814. ((org-inside-LaTeX-fragment-p)
  19815. ;; FIXME: positions wrong.
  19816. (push (list :latex-fragment (point) (point)) clist)))
  19817. (setq clist (nreverse (delq nil clist)))
  19818. clist))
  19819. ;; FIXME: Compare with at-regexp-p Do we need both?
  19820. (defun org-in-regexp (re &optional nlines visually)
  19821. "Check if point is inside a match of regexp.
  19822. Normally only the current line is checked, but you can include NLINES extra
  19823. lines both before and after point into the search.
  19824. If VISUALLY is set, require that the cursor is not after the match but
  19825. really on, so that the block visually is on the match."
  19826. (catch 'exit
  19827. (let ((pos (point))
  19828. (eol (point-at-eol (+ 1 (or nlines 0))))
  19829. (inc (if visually 1 0)))
  19830. (save-excursion
  19831. (beginning-of-line (- 1 (or nlines 0)))
  19832. (while (re-search-forward re eol t)
  19833. (if (and (<= (match-beginning 0) pos)
  19834. (>= (+ inc (match-end 0)) pos))
  19835. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19836. (defun org-at-regexp-p (regexp)
  19837. "Is point inside a match of REGEXP in the current line?"
  19838. (catch 'exit
  19839. (save-excursion
  19840. (let ((pos (point)) (end (point-at-eol)))
  19841. (beginning-of-line 1)
  19842. (while (re-search-forward regexp end t)
  19843. (if (and (<= (match-beginning 0) pos)
  19844. (>= (match-end 0) pos))
  19845. (throw 'exit t)))
  19846. nil))))
  19847. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19848. "Non-nil when point is between matches of START-RE and END-RE.
  19849. Also return a non-nil value when point is on one of the matches.
  19850. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19851. buffer positions. Default values are the positions of headlines
  19852. surrounding the point.
  19853. The functions returns a cons cell whose car (resp. cdr) is the
  19854. position before START-RE (resp. after END-RE)."
  19855. (save-match-data
  19856. (let ((pos (point))
  19857. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19858. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19859. beg end)
  19860. (save-excursion
  19861. ;; Point is on a block when on START-RE or if START-RE can be
  19862. ;; found before it...
  19863. (and (or (org-at-regexp-p start-re)
  19864. (re-search-backward start-re limit-up t))
  19865. (setq beg (match-beginning 0))
  19866. ;; ... and END-RE after it...
  19867. (goto-char (match-end 0))
  19868. (re-search-forward end-re limit-down t)
  19869. (> (setq end (match-end 0)) pos)
  19870. ;; ... without another START-RE in-between.
  19871. (goto-char (match-beginning 0))
  19872. (not (re-search-backward start-re (1+ beg) t))
  19873. ;; Return value.
  19874. (cons beg end))))))
  19875. (defun org-in-block-p (names)
  19876. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19877. NAMES is a list of strings containing names of blocks.
  19878. Return first block name matched, or nil. Beware that in case of
  19879. nested blocks, the returned name may not belong to the closest
  19880. block from point."
  19881. (save-match-data
  19882. (catch 'exit
  19883. (let ((case-fold-search t)
  19884. (lim-up (save-excursion (outline-previous-heading)))
  19885. (lim-down (save-excursion (outline-next-heading))))
  19886. (mapc (lambda (name)
  19887. (let ((n (regexp-quote name)))
  19888. (when (org-between-regexps-p
  19889. (concat "^[ \t]*#\\+begin_" n)
  19890. (concat "^[ \t]*#\\+end_" n)
  19891. lim-up lim-down)
  19892. (throw 'exit n))))
  19893. names))
  19894. nil)))
  19895. (defun org-in-drawer-p ()
  19896. "Is point within a drawer?"
  19897. (save-match-data
  19898. (let ((case-fold-search t)
  19899. (lim-up (save-excursion (outline-previous-heading)))
  19900. (lim-down (save-excursion (outline-next-heading))))
  19901. (org-between-regexps-p
  19902. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  19903. "^[ \t]*:end:.*$"
  19904. lim-up lim-down))))
  19905. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19906. "Call `multi-occur' with buffers for all agenda files."
  19907. (interactive "sOrg-files matching: \np")
  19908. (let* ((files (org-agenda-files))
  19909. (tnames (mapcar 'file-truename files))
  19910. (extra org-agenda-text-search-extra-files)
  19911. f)
  19912. (when (eq (car extra) 'agenda-archives)
  19913. (setq extra (cdr extra))
  19914. (setq files (org-add-archive-files files)))
  19915. (while (setq f (pop extra))
  19916. (unless (member (file-truename f) tnames)
  19917. (add-to-list 'files f 'append)
  19918. (add-to-list 'tnames (file-truename f) 'append)))
  19919. (multi-occur
  19920. (mapcar (lambda (x)
  19921. (with-current-buffer
  19922. (or (get-file-buffer x) (find-file-noselect x))
  19923. (widen)
  19924. (current-buffer)))
  19925. files)
  19926. regexp)))
  19927. (if (boundp 'occur-mode-find-occurrence-hook)
  19928. ;; Emacs 23
  19929. (add-hook 'occur-mode-find-occurrence-hook
  19930. (lambda ()
  19931. (when (derived-mode-p 'org-mode)
  19932. (org-reveal))))
  19933. ;; Emacs 22
  19934. (defadvice occur-mode-goto-occurrence
  19935. (after org-occur-reveal activate)
  19936. (and (derived-mode-p 'org-mode) (org-reveal)))
  19937. (defadvice occur-mode-goto-occurrence-other-window
  19938. (after org-occur-reveal activate)
  19939. (and (derived-mode-p 'org-mode) (org-reveal)))
  19940. (defadvice occur-mode-display-occurrence
  19941. (after org-occur-reveal activate)
  19942. (when (derived-mode-p 'org-mode)
  19943. (let ((pos (occur-mode-find-occurrence)))
  19944. (with-current-buffer (marker-buffer pos)
  19945. (save-excursion
  19946. (goto-char pos)
  19947. (org-reveal)))))))
  19948. (defun org-occur-link-in-agenda-files ()
  19949. "Create a link and search for it in the agendas.
  19950. The link is not stored in `org-stored-links', it is just created
  19951. for the search purpose."
  19952. (interactive)
  19953. (let ((link (condition-case nil
  19954. (org-store-link nil)
  19955. (error "Unable to create a link to here"))))
  19956. (org-occur-in-agenda-files (regexp-quote link))))
  19957. (defun org-reverse-string (string)
  19958. "Return the reverse of STRING."
  19959. (apply 'string (reverse (string-to-list string))))
  19960. (defsubst org-uniquify (list)
  19961. "Non-destructively remove duplicate elements from LIST."
  19962. (let ((res (copy-sequence list))) (delete-dups res)))
  19963. (defun org-uniquify-alist (alist)
  19964. "Merge elements of ALIST with the same key.
  19965. For example, in this alist:
  19966. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19967. => '((a 1 3) (b 2))
  19968. merge (a 1) and (a 3) into (a 1 3).
  19969. The function returns the new ALIST."
  19970. (let (rtn)
  19971. (mapc
  19972. (lambda (e)
  19973. (let (n)
  19974. (if (not (assoc (car e) rtn))
  19975. (push e rtn)
  19976. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19977. (setq rtn (assq-delete-all (car e) rtn))
  19978. (push n rtn))))
  19979. alist)
  19980. rtn))
  19981. (defun org-delete-all (elts list)
  19982. "Remove all elements in ELTS from LIST."
  19983. (while elts
  19984. (setq list (delete (pop elts) list)))
  19985. list)
  19986. (defun org-count (cl-item cl-seq)
  19987. "Count the number of occurrences of ITEM in SEQ.
  19988. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19989. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19990. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19991. (while (< cl-start cl-end)
  19992. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19993. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19994. (setq cl-start (1+ cl-start)))
  19995. cl-count))
  19996. (defun org-remove-if (predicate seq)
  19997. "Remove everything from SEQ that fulfills PREDICATE."
  19998. (let (res e)
  19999. (while seq
  20000. (setq e (pop seq))
  20001. (if (not (funcall predicate e)) (push e res)))
  20002. (nreverse res)))
  20003. (defun org-remove-if-not (predicate seq)
  20004. "Remove everything from SEQ that does not fulfill PREDICATE."
  20005. (let (res e)
  20006. (while seq
  20007. (setq e (pop seq))
  20008. (if (funcall predicate e) (push e res)))
  20009. (nreverse res)))
  20010. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20011. "Reduce two-argument FUNCTION across SEQ.
  20012. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20013. \":initial-value\" removed."
  20014. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20015. (cadr (memq :initial-value cl-keys)))
  20016. (cl-seq (pop cl-seq))
  20017. (t (funcall cl-func)))))
  20018. (while cl-seq
  20019. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20020. cl-accum))
  20021. (defun org-every (pred seq)
  20022. "Return true if PREDICATE is true of every element of SEQ.
  20023. Adapted from `every' in cl.el."
  20024. (catch 'org-every
  20025. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20026. t))
  20027. (defun org-some (pred seq)
  20028. "Return true if PREDICATE is true of any element of SEQ.
  20029. Adapted from `some' in cl.el."
  20030. (catch 'org-some
  20031. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20032. nil))
  20033. (defun org-back-over-empty-lines ()
  20034. "Move backwards over whitespace, to the beginning of the first empty line.
  20035. Returns the number of empty lines passed."
  20036. (let ((pos (point)))
  20037. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20038. (skip-chars-backward " \t\n\r")
  20039. (unless (eobp)
  20040. (forward-line -1)))
  20041. (beginning-of-line 2)
  20042. (goto-char (min (point) pos))
  20043. (count-lines (point) pos)))
  20044. (defun org-skip-whitespace ()
  20045. (skip-chars-forward " \t\n\r"))
  20046. (defun org-point-in-group (point group &optional context)
  20047. "Check if POINT is in match-group GROUP.
  20048. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20049. match. If the match group does not exist or point is not inside it,
  20050. return nil."
  20051. (and (match-beginning group)
  20052. (>= point (match-beginning group))
  20053. (<= point (match-end group))
  20054. (if context
  20055. (list context (match-beginning group) (match-end group))
  20056. t)))
  20057. (defun org-switch-to-buffer-other-window (&rest args)
  20058. "Switch to buffer in a second window on the current frame.
  20059. In particular, do not allow pop-up frames.
  20060. Returns the newly created buffer."
  20061. (org-no-popups
  20062. (apply 'switch-to-buffer-other-window args)))
  20063. (defun org-combine-plists (&rest plists)
  20064. "Create a single property list from all plists in PLISTS.
  20065. The process starts by copying the first list, and then setting properties
  20066. from the other lists. Settings in the last list are the most significant
  20067. ones and overrule settings in the other lists."
  20068. (let ((rtn (copy-sequence (pop plists)))
  20069. p v ls)
  20070. (while plists
  20071. (setq ls (pop plists))
  20072. (while ls
  20073. (setq p (pop ls) v (pop ls))
  20074. (setq rtn (plist-put rtn p v))))
  20075. rtn))
  20076. (defun org-replace-escapes (string table)
  20077. "Replace %-escapes in STRING with values in TABLE.
  20078. TABLE is an association list with keys like \"%a\" and string values.
  20079. The sequences in STRING may contain normal field width and padding information,
  20080. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20081. so values can contain further %-escapes if they are define later in TABLE."
  20082. (let ((tbl (copy-alist table))
  20083. (case-fold-search nil)
  20084. (pchg 0)
  20085. e re rpl)
  20086. (while (setq e (pop tbl))
  20087. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20088. (when (and (cdr e) (string-match re (cdr e)))
  20089. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20090. (safe "SREF"))
  20091. (add-text-properties 0 3 (list 'sref sref) safe)
  20092. (setcdr e (replace-match safe t t (cdr e)))))
  20093. (while (string-match re string)
  20094. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20095. (cdr e)))
  20096. (setq string (replace-match rpl t t string))))
  20097. (while (setq pchg (next-property-change pchg string))
  20098. (let ((sref (get-text-property pchg 'sref string)))
  20099. (when (and sref (string-match "SREF" string pchg))
  20100. (setq string (replace-match sref t t string)))))
  20101. string))
  20102. (defun org-sublist (list start end)
  20103. "Return a section of LIST, from START to END.
  20104. Counting starts at 1."
  20105. (let (rtn (c start))
  20106. (setq list (nthcdr (1- start) list))
  20107. (while (and list (<= c end))
  20108. (push (pop list) rtn)
  20109. (setq c (1+ c)))
  20110. (nreverse rtn)))
  20111. (defun org-find-base-buffer-visiting (file)
  20112. "Like `find-buffer-visiting' but always return the base buffer and
  20113. not an indirect buffer."
  20114. (let ((buf (or (get-file-buffer file)
  20115. (find-buffer-visiting file))))
  20116. (if buf
  20117. (or (buffer-base-buffer buf) buf)
  20118. nil)))
  20119. (defun org-image-file-name-regexp (&optional extensions)
  20120. "Return regexp matching the file names of images.
  20121. If EXTENSIONS is given, only match these."
  20122. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20123. (image-file-name-regexp)
  20124. (let ((image-file-name-extensions
  20125. (or extensions
  20126. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20127. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20128. (concat "\\."
  20129. (regexp-opt (nconc (mapcar 'upcase
  20130. image-file-name-extensions)
  20131. image-file-name-extensions)
  20132. t)
  20133. "\\'"))))
  20134. (defun org-file-image-p (file &optional extensions)
  20135. "Return non-nil if FILE is an image."
  20136. (save-match-data
  20137. (string-match (org-image-file-name-regexp extensions) file)))
  20138. (defun org-get-cursor-date (&optional with-time)
  20139. "Return the date at cursor in as a time.
  20140. This works in the calendar and in the agenda, anywhere else it just
  20141. returns the current time.
  20142. If WITH-TIME is non-nil, returns the time of the event at point (in
  20143. the agenda) or the current time of the day."
  20144. (let (date day defd tp tm hod mod)
  20145. (when with-time
  20146. (setq tp (get-text-property (point) 'time))
  20147. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20148. (setq hod (string-to-number (match-string 1 tp))
  20149. mod (string-to-number (match-string 2 tp))))
  20150. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20151. mod (nth 1 (decode-time (current-time))))))
  20152. (cond
  20153. ((eq major-mode 'calendar-mode)
  20154. (setq date (calendar-cursor-to-date)
  20155. defd (encode-time 0 (or mod 0) (or hod 0)
  20156. (nth 1 date) (nth 0 date) (nth 2 date))))
  20157. ((eq major-mode 'org-agenda-mode)
  20158. (setq day (get-text-property (point) 'day))
  20159. (if day
  20160. (setq date (calendar-gregorian-from-absolute day)
  20161. defd (encode-time 0 (or mod 0) (or hod 0)
  20162. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20163. (or defd (current-time))))
  20164. (defun org-mark-subtree (&optional up)
  20165. "Mark the current subtree.
  20166. This puts point at the start of the current subtree, and mark at
  20167. the end. If a numeric prefix UP is given, move up into the
  20168. hierarchy of headlines by UP levels before marking the subtree."
  20169. (interactive "P")
  20170. (org-with-limited-levels
  20171. (cond ((org-at-heading-p) (beginning-of-line))
  20172. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20173. (t (outline-previous-visible-heading 1))))
  20174. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20175. (if (org-called-interactively-p 'any)
  20176. (call-interactively 'org-mark-element)
  20177. (org-mark-element)))
  20178. ;;; Indentation
  20179. (defun org-indent-line ()
  20180. "Indent line depending on context."
  20181. (interactive)
  20182. (let* ((pos (point))
  20183. (itemp (org-at-item-p))
  20184. (case-fold-search t)
  20185. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20186. (inline-task-p (and (featurep 'org-inlinetask)
  20187. (org-inlinetask-in-task-p)))
  20188. (inline-re (and inline-task-p
  20189. (org-inlinetask-outline-regexp)))
  20190. column)
  20191. (if (and orgstruct-is-++ (eq pos (point)))
  20192. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20193. (indent-according-to-mode))
  20194. (beginning-of-line 1)
  20195. (cond
  20196. ;; Headings
  20197. ((looking-at org-outline-regexp) (setq column 0))
  20198. ;; Footnote definition
  20199. ((looking-at org-footnote-definition-re) (setq column 0))
  20200. ;; Literal examples
  20201. ((looking-at "[ \t]*:\\( \\|$\\)")
  20202. (setq column (org-get-indentation))) ; do nothing
  20203. ;; Lists
  20204. ((ignore-errors (goto-char (org-in-item-p)))
  20205. (setq column (if itemp
  20206. (org-get-indentation)
  20207. (org-list-item-body-column (point))))
  20208. (goto-char pos))
  20209. ;; Drawers
  20210. ((and (looking-at "[ \t]*:END:")
  20211. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20212. (save-excursion
  20213. (goto-char (1- (match-beginning 1)))
  20214. (setq column (current-column))))
  20215. ;; Special blocks
  20216. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20217. (save-excursion
  20218. (re-search-backward
  20219. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20220. (setq column (org-get-indentation (match-string 0))))
  20221. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20222. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20223. (save-excursion
  20224. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20225. (setq column
  20226. (cond ((equal (downcase (match-string 1)) "src")
  20227. ;; src blocks: let `org-edit-src-exit' handle them
  20228. (org-get-indentation))
  20229. ((equal (downcase (match-string 1)) "example")
  20230. (max (org-get-indentation)
  20231. (org-get-indentation (match-string 0))))
  20232. (t
  20233. (org-get-indentation (match-string 0))))))
  20234. ;; This line has nothing special, look at the previous relevant
  20235. ;; line to compute indentation
  20236. (t
  20237. (beginning-of-line 0)
  20238. (while (and (not (bobp))
  20239. (not (looking-at org-table-line-regexp))
  20240. (not (looking-at org-drawer-regexp))
  20241. ;; When point started in an inline task, do not move
  20242. ;; above task starting line.
  20243. (not (and inline-task-p (looking-at inline-re)))
  20244. ;; Skip drawers, blocks, empty lines, verbatim,
  20245. ;; comments, tables, footnotes definitions, lists,
  20246. ;; inline tasks.
  20247. (or (and (looking-at "[ \t]*:END:")
  20248. (re-search-backward org-drawer-regexp nil t))
  20249. (and (looking-at "[ \t]*#\\+end_")
  20250. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20251. (looking-at "[ \t]*[\n:#|]")
  20252. (looking-at org-footnote-definition-re)
  20253. (and (not inline-task-p)
  20254. (featurep 'org-inlinetask)
  20255. (org-inlinetask-in-task-p)
  20256. (or (org-inlinetask-goto-beginning) t))))
  20257. (beginning-of-line 0))
  20258. (cond
  20259. ;; There was a list item above.
  20260. ((save-excursion
  20261. (and (ignore-errors (goto-char (org-in-item-p)))
  20262. (goto-char
  20263. (org-list-get-top-point (org-list-struct)))))
  20264. (looking-at org-list-full-item-re)
  20265. (setq column (length (match-string 0))))
  20266. ;; There was an heading above.
  20267. ((looking-at "\\*+[ \t]+")
  20268. (if (not org-adapt-indentation)
  20269. (setq column 0)
  20270. (goto-char (match-end 0))
  20271. (setq column (current-column))))
  20272. ;; A drawer had started and is unfinished
  20273. ((looking-at org-drawer-regexp)
  20274. (goto-char (1- (match-beginning 1)))
  20275. (setq column (current-column)))
  20276. ;; Else, nothing noticeable found: get indentation and go on.
  20277. (t (setq column (org-get-indentation))))))
  20278. ;; Now apply indentation and move cursor accordingly
  20279. (goto-char pos)
  20280. (if (<= (current-column) (current-indentation))
  20281. (org-indent-line-to column)
  20282. (save-excursion (org-indent-line-to column)))
  20283. ;; Special polishing for properties, see `org-property-format'
  20284. (setq column (current-column))
  20285. (beginning-of-line 1)
  20286. (if (looking-at org-property-re)
  20287. (replace-match (concat (match-string 4)
  20288. (format org-property-format
  20289. (match-string 1) (match-string 3)))
  20290. t t))
  20291. (org-move-to-column column))))
  20292. (defun org-indent-drawer ()
  20293. "Indent the drawer at point."
  20294. (interactive)
  20295. (let ((p (point))
  20296. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20297. (match-end 0)))
  20298. (folded
  20299. (save-excursion
  20300. (end-of-line)
  20301. (when (overlays-at (point))
  20302. (member 'invisible (overlay-properties
  20303. (car (overlays-at (point)))))))))
  20304. (when folded (org-cycle))
  20305. (indent-for-tab-command)
  20306. (while (and (move-beginning-of-line 2) (< (point) e))
  20307. (indent-for-tab-command))
  20308. (goto-char p)
  20309. (when folded (org-cycle)))
  20310. (message "Drawer at point indented"))
  20311. (defun org-indent-block ()
  20312. "Indent the block at point."
  20313. (interactive)
  20314. (let ((p (point))
  20315. (case-fold-search t)
  20316. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20317. (match-end 0)))
  20318. (folded
  20319. (save-excursion
  20320. (end-of-line)
  20321. (when (overlays-at (point))
  20322. (member 'invisible (overlay-properties
  20323. (car (overlays-at (point)))))))))
  20324. (when folded (org-cycle))
  20325. (indent-for-tab-command)
  20326. (while (and (move-beginning-of-line 2) (< (point) e))
  20327. (indent-for-tab-command))
  20328. (goto-char p)
  20329. (when folded (org-cycle)))
  20330. (message "Block at point indented"))
  20331. (defun org-indent-region (start end)
  20332. "Indent region."
  20333. (interactive "r")
  20334. (save-excursion
  20335. (let ((line-end (org-current-line end)))
  20336. (goto-char start)
  20337. (while (< (org-current-line) line-end)
  20338. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20339. (t (call-interactively 'org-indent-line)))
  20340. (move-beginning-of-line 2)))))
  20341. ;;; Filling
  20342. ;; We use our own fill-paragraph and auto-fill functions.
  20343. ;; `org-fill-paragraph' relies on adaptive filling and context
  20344. ;; checking. Appropriate `fill-prefix' is computed with
  20345. ;; `org-adaptive-fill-function'.
  20346. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20347. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20348. ;; `org-adaptive-fill-function' value. Internally,
  20349. ;; `org-comment-line-break-function' breaks the line.
  20350. ;; `org-setup-filling' installs filling and auto-filling related
  20351. ;; variables during `org-mode' initialization.
  20352. (defvar org-element-paragraph-separate) ; org-element.el
  20353. (defun org-setup-filling ()
  20354. (require 'org-element)
  20355. ;; Prevent auto-fill from inserting unwanted new items.
  20356. (when (boundp 'fill-nobreak-predicate)
  20357. (org-set-local
  20358. 'fill-nobreak-predicate
  20359. (org-uniquify
  20360. (append fill-nobreak-predicate
  20361. '(org-fill-line-break-nobreak-p
  20362. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20363. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20364. (org-set-local 'paragraph-start paragraph-ending)
  20365. (org-set-local 'paragraph-separate paragraph-ending))
  20366. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20367. (org-set-local 'auto-fill-inhibit-regexp nil)
  20368. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20369. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20370. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20371. (defun org-fill-line-break-nobreak-p ()
  20372. "Non-nil when a new line at point would create an Org line break."
  20373. (save-excursion
  20374. (skip-chars-backward "[ \t]")
  20375. (skip-chars-backward "\\\\")
  20376. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20377. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20378. "Non-nil when a new line at point would split a timestamp."
  20379. (and (org-at-timestamp-p t)
  20380. (not (looking-at org-ts-regexp-both))))
  20381. (declare-function message-in-body-p "message" ())
  20382. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20383. (defun org-adaptive-fill-function ()
  20384. "Compute a fill prefix for the current line.
  20385. Return fill prefix, as a string, or nil if current line isn't
  20386. meant to be filled."
  20387. (let (prefix)
  20388. (catch 'exit
  20389. (when (derived-mode-p 'message-mode)
  20390. (save-excursion
  20391. (beginning-of-line)
  20392. (cond ((or (not (message-in-body-p))
  20393. (looking-at orgtbl-line-start-regexp))
  20394. (throw 'exit nil))
  20395. ((looking-at message-cite-prefix-regexp)
  20396. (throw 'exit (match-string-no-properties 0)))
  20397. ((looking-at org-outline-regexp)
  20398. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20399. (org-with-wide-buffer
  20400. (let* ((p (line-beginning-position))
  20401. (element (save-excursion
  20402. (beginning-of-line)
  20403. (or (ignore-errors (org-element-at-point))
  20404. (user-error "An element cannot be parsed line %d"
  20405. (line-number-at-pos (point))))))
  20406. (type (org-element-type element))
  20407. (post-affiliated (org-element-property :post-affiliated element)))
  20408. (unless (and post-affiliated (< p post-affiliated))
  20409. (case type
  20410. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20411. (footnote-definition "")
  20412. ((item plain-list)
  20413. (make-string (org-list-item-body-column
  20414. (or post-affiliated
  20415. (org-element-property :begin element)))
  20416. ? ))
  20417. (paragraph
  20418. ;; Fill prefix is usually the same as the current line,
  20419. ;; except if the paragraph is at the beginning of an item.
  20420. (let ((parent (org-element-property :parent element)))
  20421. (cond ((eq (org-element-type parent) 'item)
  20422. (make-string (org-list-item-body-column
  20423. (org-element-property :begin parent))
  20424. ? ))
  20425. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20426. (match-string 0))
  20427. (t ""))))
  20428. (comment-block
  20429. ;; Only fill contents if P is within block boundaries.
  20430. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20431. (forward-line)
  20432. (point)))
  20433. (cend (save-excursion
  20434. (goto-char (org-element-property :end element))
  20435. (skip-chars-backward " \r\t\n")
  20436. (line-beginning-position))))
  20437. (when (and (>= p cbeg) (< p cend))
  20438. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20439. (match-string 0)
  20440. "")))))))))))
  20441. (declare-function message-goto-body "message" ())
  20442. (defvar message-cite-prefix-regexp) ; From message.el
  20443. (defun org-fill-paragraph (&optional justify)
  20444. "Fill element at point, when applicable.
  20445. This function only applies to comment blocks, comments, example
  20446. blocks and paragraphs. Also, as a special case, re-align table
  20447. when point is at one.
  20448. If JUSTIFY is non-nil (interactively, with prefix argument),
  20449. justify as well. If `sentence-end-double-space' is non-nil, then
  20450. period followed by one space does not end a sentence, so don't
  20451. break a line there. The variable `fill-column' controls the
  20452. width for filling.
  20453. For convenience, when point is at a plain list, an item or
  20454. a footnote definition, try to fill the first paragraph within."
  20455. (interactive)
  20456. (if (and (derived-mode-p 'message-mode)
  20457. (or (not (message-in-body-p))
  20458. (save-excursion (move-beginning-of-line 1)
  20459. (looking-at message-cite-prefix-regexp))))
  20460. ;; First ensure filling is correct in message-mode.
  20461. (let ((fill-paragraph-function
  20462. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20463. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20464. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20465. (paragraph-separate
  20466. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20467. (fill-paragraph nil))
  20468. (with-syntax-table org-mode-transpose-word-syntax-table
  20469. ;; Move to end of line in order to get the first paragraph
  20470. ;; within a plain list or a footnote definition.
  20471. (let ((element (save-excursion
  20472. (end-of-line)
  20473. (or (ignore-errors (org-element-at-point))
  20474. (user-error "An element cannot be parsed line %d"
  20475. (line-number-at-pos (point)))))))
  20476. ;; First check if point is in a blank line at the beginning of
  20477. ;; the buffer. In that case, ignore filling.
  20478. (case (org-element-type element)
  20479. ;; Use major mode filling function is src blocks.
  20480. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20481. ;; Align Org tables, leave table.el tables as-is.
  20482. (table-row (org-table-align) t)
  20483. (table
  20484. (when (eq (org-element-property :type element) 'org)
  20485. (save-excursion
  20486. (goto-char (org-element-property :post-affiliated element))
  20487. (org-table-align)))
  20488. t)
  20489. (paragraph
  20490. ;; Paragraphs may contain `line-break' type objects.
  20491. (let ((beg (max (point-min)
  20492. (org-element-property :contents-begin element)))
  20493. (end (min (point-max)
  20494. (org-element-property :contents-end element))))
  20495. ;; Do nothing if point is at an affiliated keyword.
  20496. (if (< (line-end-position) beg) t
  20497. (when (derived-mode-p 'message-mode)
  20498. ;; In `message-mode', do not fill following citation
  20499. ;; in current paragraph nor text before message body.
  20500. (let ((body-start (save-excursion (message-goto-body))))
  20501. (when body-start (setq beg (max body-start beg))))
  20502. (when (save-excursion
  20503. (re-search-forward
  20504. (concat "^" message-cite-prefix-regexp) end t))
  20505. (setq end (match-beginning 0))))
  20506. ;; Fill paragraph, taking line breaks into account.
  20507. ;; For that, slice the paragraph using line breaks as
  20508. ;; separators, and fill the parts in reverse order to
  20509. ;; avoid messing with markers.
  20510. (save-excursion
  20511. (goto-char end)
  20512. (mapc
  20513. (lambda (pos)
  20514. (fill-region-as-paragraph pos (point) justify)
  20515. (goto-char pos))
  20516. ;; Find the list of ending positions for line breaks
  20517. ;; in the current paragraph. Add paragraph
  20518. ;; beginning to include first slice.
  20519. (nreverse
  20520. (cons beg
  20521. (org-element-map
  20522. (org-element--parse-objects
  20523. beg end nil (org-element-restriction 'paragraph))
  20524. 'line-break
  20525. (lambda (lb) (org-element-property :end lb)))))))
  20526. t)))
  20527. ;; Contents of `comment-block' type elements should be
  20528. ;; filled as plain text, but only if point is within block
  20529. ;; markers.
  20530. (comment-block
  20531. (let* ((case-fold-search t)
  20532. (beg (save-excursion
  20533. (goto-char (org-element-property :begin element))
  20534. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20535. (forward-line)
  20536. (point)))
  20537. (end (save-excursion
  20538. (goto-char (org-element-property :end element))
  20539. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20540. (line-beginning-position))))
  20541. (if (or (< (point) beg) (> (point) end)) t
  20542. (fill-region-as-paragraph
  20543. (save-excursion (end-of-line)
  20544. (re-search-backward "^[ \t]*$" beg 'move)
  20545. (line-beginning-position))
  20546. (save-excursion (beginning-of-line)
  20547. (re-search-forward "^[ \t]*$" end 'move)
  20548. (line-beginning-position))
  20549. justify))))
  20550. ;; Fill comments.
  20551. (comment
  20552. (let ((begin (org-element-property :post-affiliated element))
  20553. (end (org-element-property :end element)))
  20554. (when (and (>= (point) begin) (<= (point) end))
  20555. (let ((begin (save-excursion
  20556. (end-of-line)
  20557. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  20558. (progn (forward-line) (point))
  20559. begin)))
  20560. (end (save-excursion
  20561. (end-of-line)
  20562. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  20563. (1- (line-beginning-position))
  20564. (skip-chars-backward " \r\t\n")
  20565. (line-end-position)))))
  20566. ;; Do not fill comments when at a blank line or at
  20567. ;; affiliated keywords.
  20568. (let ((fill-prefix (save-excursion
  20569. (beginning-of-line)
  20570. (looking-at "[ \t]*#")
  20571. (concat (match-string 0) " "))))
  20572. (when (> end begin)
  20573. (save-excursion
  20574. (fill-region-as-paragraph begin end justify))))))
  20575. t))
  20576. ;; Ignore every other element.
  20577. (otherwise t))))))
  20578. (defun org-auto-fill-function ()
  20579. "Auto-fill function."
  20580. ;; Check if auto-filling is meaningful.
  20581. (let ((fc (current-fill-column)))
  20582. (when (and fc (> (current-column) fc))
  20583. (let* ((fill-prefix (org-adaptive-fill-function))
  20584. ;; Enforce empty fill prefix, if required. Otherwise, it
  20585. ;; will be computed again.
  20586. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20587. (when fill-prefix (do-auto-fill))))))
  20588. (defun org-comment-line-break-function (&optional soft)
  20589. "Break line at point and indent, continuing comment if within one.
  20590. The inserted newline is marked hard if variable
  20591. `use-hard-newlines' is true, unless optional argument SOFT is
  20592. non-nil."
  20593. (if soft (insert-and-inherit ?\n) (newline 1))
  20594. (save-excursion (forward-char -1) (delete-horizontal-space))
  20595. (delete-horizontal-space)
  20596. (indent-to-left-margin)
  20597. (insert-before-markers-and-inherit fill-prefix))
  20598. ;;; Comments
  20599. ;; Org comments syntax is quite complex. It requires the entire line
  20600. ;; to be just a comment. Also, even with the right syntax at the
  20601. ;; beginning of line, some some elements (i.e. verse-block or
  20602. ;; example-block) don't accept comments. Usual Emacs comment commands
  20603. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20604. ;; Org still relies on `comment-dwim', but cannot trust
  20605. ;; `comment-only-p'. So, `comment-region-function' and
  20606. ;; `uncomment-region-function' both point
  20607. ;; to`org-comment-or-uncomment-region'. Eventually,
  20608. ;; `org-insert-comment' takes care of insertion of comments at the
  20609. ;; beginning of line.
  20610. ;; `org-setup-comments-handling' install comments related variables
  20611. ;; during `org-mode' initialization.
  20612. (defun org-setup-comments-handling ()
  20613. (interactive)
  20614. (org-set-local 'comment-use-syntax nil)
  20615. (org-set-local 'comment-start "# ")
  20616. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20617. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20618. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20619. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20620. (defun org-insert-comment ()
  20621. "Insert an empty comment above current line.
  20622. If the line is empty, insert comment at its beginning."
  20623. (beginning-of-line)
  20624. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20625. (org-indent-line)
  20626. (insert "# "))
  20627. (defvar comment-empty-lines) ; From newcomment.el.
  20628. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20629. "Comment or uncomment each non-blank line in the region.
  20630. Uncomment each non-blank line between BEG and END if it only
  20631. contains commented lines. Otherwise, comment them."
  20632. (save-restriction
  20633. ;; Restrict region
  20634. (narrow-to-region (save-excursion (goto-char beg)
  20635. (skip-chars-forward " \r\t\n" end)
  20636. (line-beginning-position))
  20637. (save-excursion (goto-char end)
  20638. (skip-chars-backward " \r\t\n" beg)
  20639. (line-end-position)))
  20640. (let ((uncommentp
  20641. ;; UNCOMMENTP is non-nil when every non blank line between
  20642. ;; BEG and END is a comment.
  20643. (save-excursion
  20644. (goto-char (point-min))
  20645. (while (and (not (eobp))
  20646. (let ((element (org-element-at-point)))
  20647. (and (eq (org-element-type element) 'comment)
  20648. (goto-char (min (point-max)
  20649. (org-element-property
  20650. :end element)))))))
  20651. (eobp))))
  20652. (if uncommentp
  20653. ;; Only blank lines and comments in region: uncomment it.
  20654. (save-excursion
  20655. (goto-char (point-min))
  20656. (while (not (eobp))
  20657. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20658. (replace-match "" nil nil nil 1))
  20659. (forward-line)))
  20660. ;; Comment each line in region.
  20661. (let ((min-indent (point-max)))
  20662. ;; First find the minimum indentation across all lines.
  20663. (save-excursion
  20664. (goto-char (point-min))
  20665. (while (and (not (eobp)) (not (zerop min-indent)))
  20666. (unless (looking-at "[ \t]*$")
  20667. (setq min-indent (min min-indent (current-indentation))))
  20668. (forward-line)))
  20669. ;; Then loop over all lines.
  20670. (save-excursion
  20671. (goto-char (point-min))
  20672. (while (not (eobp))
  20673. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20674. ;; Don't get fooled by invisible text (e.g. link path)
  20675. ;; when moving to column MIN-INDENT.
  20676. (let ((buffer-invisibility-spec nil))
  20677. (org-move-to-column min-indent t))
  20678. (insert comment-start))
  20679. (forward-line))))))))
  20680. ;;; Planning
  20681. ;; This section contains tools to operate on timestamp objects, as
  20682. ;; returned by, e.g. `org-element-context'.
  20683. (defun org-timestamp-has-time-p (timestamp)
  20684. "Non-nil when TIMESTAMP has a time specified."
  20685. (org-element-property :hour-start timestamp))
  20686. (defun org-timestamp-format (timestamp format &optional end utc)
  20687. "Format a TIMESTAMP element into a string.
  20688. FORMAT is a format specifier to be passed to
  20689. `format-time-string'.
  20690. When optional argument END is non-nil, use end of date-range or
  20691. time-range, if possible.
  20692. When optional argument UTC is non-nil, time will be expressed as
  20693. Universal Time."
  20694. (format-time-string
  20695. format
  20696. (apply 'encode-time
  20697. (cons 0
  20698. (mapcar
  20699. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20700. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20701. '(:minute-start :hour-start :day-start :month-start
  20702. :year-start)))))
  20703. utc))
  20704. (defun org-timestamp-split-range (timestamp &optional end)
  20705. "Extract a timestamp object from a date or time range.
  20706. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20707. the end of the range. Otherwise, extract its start.
  20708. Return a new timestamp object sharing the same parent as
  20709. TIMESTAMP."
  20710. (let ((type (org-element-property :type timestamp)))
  20711. (if (memq type '(active inactive diary)) timestamp
  20712. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20713. ;; Set new type.
  20714. (org-element-put-property
  20715. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20716. ;; Copy start properties over end properties if END is
  20717. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20718. (let ((p-alist '((:minute-start . :minute-end)
  20719. (:hour-start . :hour-end)
  20720. (:day-start . :day-end)
  20721. (:month-start . :month-end)
  20722. (:year-start . :year-end))))
  20723. (dolist (p-cell p-alist)
  20724. (org-element-put-property
  20725. split-ts
  20726. (funcall (if end 'car 'cdr) p-cell)
  20727. (org-element-property
  20728. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20729. ;; Eventually refresh `:raw-value'.
  20730. (org-element-put-property split-ts :raw-value nil)
  20731. (org-element-put-property
  20732. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20733. (defun org-timestamp-translate (timestamp &optional boundary)
  20734. "Apply `org-translate-time' on a TIMESTAMP object.
  20735. When optional argument BOUNDARY is non-nil, it is either the
  20736. symbol `start' or `end'. In this case, only translate the
  20737. starting or ending part of TIMESTAMP if it is a date or time
  20738. range. Otherwise, translate both parts."
  20739. (if (and (not boundary)
  20740. (memq (org-element-property :type timestamp)
  20741. '(active-range inactive-range)))
  20742. (concat
  20743. (org-translate-time
  20744. (org-element-property :raw-value
  20745. (org-timestamp-split-range timestamp)))
  20746. "--"
  20747. (org-translate-time
  20748. (org-element-property :raw-value
  20749. (org-timestamp-split-range timestamp t))))
  20750. (org-translate-time
  20751. (org-element-property
  20752. :raw-value
  20753. (if (not boundary) timestamp
  20754. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20755. ;;; Other stuff.
  20756. (defun org-toggle-fixed-width-section (arg)
  20757. "Toggle the fixed-width export.
  20758. If there is no active region, the QUOTE keyword at the current headline is
  20759. inserted or removed. When present, it causes the text between this headline
  20760. and the next to be exported as fixed-width text, and unmodified.
  20761. If there is an active region, this command adds or removes a colon as the
  20762. first character of this line. If the first character of a line is a colon,
  20763. this line is also exported in fixed-width font."
  20764. (interactive "P")
  20765. (let* ((cc 0)
  20766. (regionp (org-region-active-p))
  20767. (beg (if regionp (region-beginning) (point)))
  20768. (end (if regionp (region-end)))
  20769. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20770. (case-fold-search nil)
  20771. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20772. off)
  20773. (if regionp
  20774. (save-excursion
  20775. (goto-char beg)
  20776. (setq cc (current-column))
  20777. (beginning-of-line 1)
  20778. (setq off (looking-at re))
  20779. (while (> nlines 0)
  20780. (setq nlines (1- nlines))
  20781. (beginning-of-line 1)
  20782. (cond
  20783. (arg
  20784. (org-move-to-column cc t)
  20785. (insert ": \n")
  20786. (forward-line -1))
  20787. ((and off (looking-at re))
  20788. (replace-match "" t t nil 1))
  20789. ((not off) (org-move-to-column cc t) (insert ": ")))
  20790. (forward-line 1)))
  20791. (save-excursion
  20792. (org-back-to-heading)
  20793. (cond
  20794. ((looking-at (format org-heading-keyword-regexp-format
  20795. org-quote-string))
  20796. (goto-char (match-end 1))
  20797. (looking-at (concat " +" org-quote-string))
  20798. (replace-match "" t t)
  20799. (when (eolp) (insert " ")))
  20800. ((looking-at org-outline-regexp)
  20801. (goto-char (match-end 0))
  20802. (insert org-quote-string " ")))))))
  20803. (defun org-reftex-citation ()
  20804. "Use reftex-citation to insert a citation into the buffer.
  20805. This looks for a line like
  20806. #+BIBLIOGRAPHY: foo plain option:-d
  20807. and derives from it that foo.bib is the bibliography file relevant
  20808. for this document. It then installs the necessary environment for RefTeX
  20809. to work in this buffer and calls `reftex-citation' to insert a citation
  20810. into the buffer.
  20811. Export of such citations to both LaTeX and HTML is handled by the contributed
  20812. package ox-bibtex by Taru Karttunen."
  20813. (interactive)
  20814. (let ((reftex-docstruct-symbol 'rds)
  20815. (reftex-cite-format "\\cite{%l}")
  20816. rds bib)
  20817. (save-excursion
  20818. (save-restriction
  20819. (widen)
  20820. (let ((case-fold-search t)
  20821. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20822. (if (not (save-excursion
  20823. (or (re-search-forward re nil t)
  20824. (re-search-backward re nil t))))
  20825. (error "No bibliography defined in file")
  20826. (setq bib (concat (match-string 1) ".bib")
  20827. rds (list (list 'bib bib)))))))
  20828. (call-interactively 'reftex-citation)))
  20829. ;;;; Functions extending outline functionality
  20830. (defun org-beginning-of-line (&optional arg)
  20831. "Go to the beginning of the current line. If that is invisible, continue
  20832. to a visible line beginning. This makes the function of C-a more intuitive.
  20833. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20834. first attempt, and only move to after the tags when the cursor is already
  20835. beyond the end of the headline."
  20836. (interactive "P")
  20837. (let ((pos (point))
  20838. (special (if (consp org-special-ctrl-a/e)
  20839. (car org-special-ctrl-a/e)
  20840. org-special-ctrl-a/e))
  20841. refpos)
  20842. (if (org-bound-and-true-p visual-line-mode)
  20843. (beginning-of-visual-line 1)
  20844. (beginning-of-line 1))
  20845. (if (and arg (fboundp 'move-beginning-of-line))
  20846. (call-interactively 'move-beginning-of-line)
  20847. (if (bobp)
  20848. nil
  20849. (backward-char 1)
  20850. (if (org-truely-invisible-p)
  20851. (while (and (not (bobp)) (org-truely-invisible-p))
  20852. (backward-char 1)
  20853. (beginning-of-line 1))
  20854. (forward-char 1))))
  20855. (when special
  20856. (cond
  20857. ((and (looking-at org-complex-heading-regexp)
  20858. (= (char-after (match-end 1)) ?\ ))
  20859. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20860. (point-at-eol)))
  20861. (goto-char
  20862. (if (eq special t)
  20863. (cond ((> pos refpos) refpos)
  20864. ((= pos (point)) refpos)
  20865. (t (point)))
  20866. (cond ((> pos (point)) (point))
  20867. ((not (eq last-command this-command)) (point))
  20868. (t refpos)))))
  20869. ((org-at-item-p)
  20870. ;; Being at an item and not looking at an the item means point
  20871. ;; was previously moved to beginning of a visual line, which
  20872. ;; doesn't contain the item. Therefore, do nothing special,
  20873. ;; just stay here.
  20874. (when (looking-at org-list-full-item-re)
  20875. ;; Set special position at first white space character after
  20876. ;; bullet, and check-box, if any.
  20877. (let ((after-bullet
  20878. (let ((box (match-end 3)))
  20879. (if (not box) (match-end 1)
  20880. (let ((after (char-after box)))
  20881. (if (and after (= after ? )) (1+ box) box))))))
  20882. ;; Special case: Move point to special position when
  20883. ;; currently after it or at beginning of line.
  20884. (if (eq special t)
  20885. (when (or (> pos after-bullet) (= (point) pos))
  20886. (goto-char after-bullet))
  20887. ;; Reversed case: Move point to special position when
  20888. ;; point was already at beginning of line and command is
  20889. ;; repeated.
  20890. (when (and (= (point) pos) (eq last-command this-command))
  20891. (goto-char after-bullet))))))))
  20892. (org-no-warnings
  20893. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20894. (setq disable-point-adjustment
  20895. (or (not (invisible-p (point)))
  20896. (not (invisible-p (max (point-min) (1- (point))))))))
  20897. (defun org-end-of-line (&optional arg)
  20898. "Go to the end of the line.
  20899. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20900. tags on the first attempt, and only move to after the tags when
  20901. the cursor is already beyond the end of the headline."
  20902. (interactive "P")
  20903. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20904. org-special-ctrl-a/e))
  20905. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20906. 'end-of-visual-line)
  20907. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20908. (t 'end-of-line))))
  20909. (if (or (not special) arg) (call-interactively move-fun)
  20910. (let* ((element (save-excursion (beginning-of-line)
  20911. (org-element-at-point)))
  20912. (type (org-element-type element)))
  20913. (cond
  20914. ((memq type '(headline inlinetask))
  20915. (let ((pos (point)))
  20916. (beginning-of-line 1)
  20917. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20918. (if (eq special t)
  20919. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20920. (goto-char (match-beginning 1))
  20921. (goto-char (match-end 0)))
  20922. (if (or (< pos (match-end 0))
  20923. (not (eq this-command last-command)))
  20924. (goto-char (match-end 0))
  20925. (goto-char (match-beginning 1))))
  20926. (call-interactively move-fun))))
  20927. ((org-element-property :hiddenp element)
  20928. ;; If element is hidden, `move-end-of-line' would put point
  20929. ;; after it. Use `end-of-line' to stay on current line.
  20930. (call-interactively 'end-of-line))
  20931. (t (call-interactively move-fun)))))
  20932. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20933. (setq disable-point-adjustment
  20934. (or (not (invisible-p (point)))
  20935. (not (invisible-p (max (point-min) (1- (point))))))))
  20936. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20937. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20938. (defun org-backward-sentence (&optional arg)
  20939. "Go to beginning of sentence, or beginning of table field.
  20940. This will call `backward-sentence' or `org-table-beginning-of-field',
  20941. depending on context."
  20942. (interactive "P")
  20943. (cond
  20944. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20945. (t (call-interactively 'backward-sentence))))
  20946. (defun org-forward-sentence (&optional arg)
  20947. "Go to end of sentence, or end of table field.
  20948. This will call `forward-sentence' or `org-table-end-of-field',
  20949. depending on context."
  20950. (interactive "P")
  20951. (cond
  20952. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20953. (t (call-interactively 'forward-sentence))))
  20954. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20955. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20956. (defun org-kill-line (&optional arg)
  20957. "Kill line, to tags or end of line."
  20958. (interactive "P")
  20959. (cond
  20960. ((or (not org-special-ctrl-k)
  20961. (bolp)
  20962. (not (org-at-heading-p)))
  20963. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20964. org-ctrl-k-protect-subtree)
  20965. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20966. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20967. (user-error "C-k aborted as it would kill a hidden subtree")))
  20968. (call-interactively
  20969. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20970. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20971. (kill-region (point) (match-beginning 1))
  20972. (org-set-tags nil t))
  20973. (t (kill-region (point) (point-at-eol)))))
  20974. (define-key org-mode-map "\C-k" 'org-kill-line)
  20975. (defun org-yank (&optional arg)
  20976. "Yank. If the kill is a subtree, treat it specially.
  20977. This command will look at the current kill and check if is a single
  20978. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20979. cursor is at the beginning of a line or after the stars of a currently
  20980. empty headline, then the yank is handled specially. How exactly depends
  20981. on the value of the following variables, both set by default.
  20982. org-yank-folded-subtrees
  20983. When set, the subtree(s) will be folded after insertion, but only
  20984. if doing so would now swallow text after the yanked text.
  20985. org-yank-adjusted-subtrees
  20986. When set, the subtree will be promoted or demoted in order to
  20987. fit into the local outline tree structure, which means that the level
  20988. will be adjusted so that it becomes the smaller one of the two
  20989. *visible* surrounding headings.
  20990. Any prefix to this command will cause `yank' to be called directly with
  20991. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20992. will just
  20993. plainly yank the text as it is.
  20994. \[1] The test checks if the first non-white line is a heading
  20995. and if there are no other headings with fewer stars."
  20996. (interactive "P")
  20997. (org-yank-generic 'yank arg))
  20998. (defun org-yank-generic (command arg)
  20999. "Perform some yank-like command.
  21000. This function implements the behavior described in the `org-yank'
  21001. documentation. However, it has been generalized to work for any
  21002. interactive command with similar behavior."
  21003. ;; pretend to be command COMMAND
  21004. (setq this-command command)
  21005. (if arg
  21006. (call-interactively command)
  21007. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21008. (and (org-kill-is-subtree-p)
  21009. (or (bolp)
  21010. (and (looking-at "[ \t]*$")
  21011. (string-match
  21012. "\\`\\*+\\'"
  21013. (buffer-substring (point-at-bol) (point)))))))
  21014. swallowp)
  21015. (cond
  21016. ((and subtreep org-yank-folded-subtrees)
  21017. (let ((beg (point))
  21018. end)
  21019. (if (and subtreep org-yank-adjusted-subtrees)
  21020. (org-paste-subtree nil nil 'for-yank)
  21021. (call-interactively command))
  21022. (setq end (point))
  21023. (goto-char beg)
  21024. (when (and (bolp) subtreep
  21025. (not (setq swallowp
  21026. (org-yank-folding-would-swallow-text beg end))))
  21027. (org-with-limited-levels
  21028. (or (looking-at org-outline-regexp)
  21029. (re-search-forward org-outline-regexp-bol end t))
  21030. (while (and (< (point) end) (looking-at org-outline-regexp))
  21031. (hide-subtree)
  21032. (org-cycle-show-empty-lines 'folded)
  21033. (condition-case nil
  21034. (outline-forward-same-level 1)
  21035. (error (goto-char end))))))
  21036. (when swallowp
  21037. (message
  21038. "Inserted text not folded because that would swallow text"))
  21039. (goto-char end)
  21040. (skip-chars-forward " \t\n\r")
  21041. (beginning-of-line 1)
  21042. (push-mark beg 'nomsg)))
  21043. ((and subtreep org-yank-adjusted-subtrees)
  21044. (let ((beg (point-at-bol)))
  21045. (org-paste-subtree nil nil 'for-yank)
  21046. (push-mark beg 'nomsg)))
  21047. (t
  21048. (call-interactively command))))))
  21049. (defun org-yank-folding-would-swallow-text (beg end)
  21050. "Would hide-subtree at BEG swallow any text after END?"
  21051. (let (level)
  21052. (org-with-limited-levels
  21053. (save-excursion
  21054. (goto-char beg)
  21055. (when (or (looking-at org-outline-regexp)
  21056. (re-search-forward org-outline-regexp-bol end t))
  21057. (setq level (org-outline-level)))
  21058. (goto-char end)
  21059. (skip-chars-forward " \t\r\n\v\f")
  21060. (if (or (eobp)
  21061. (and (bolp) (looking-at org-outline-regexp)
  21062. (<= (org-outline-level) level)))
  21063. nil ; Nothing would be swallowed
  21064. t))))) ; something would swallow
  21065. (define-key org-mode-map "\C-y" 'org-yank)
  21066. (defun org-truely-invisible-p ()
  21067. "Check if point is at a character currently not visible.
  21068. This version does not only check the character property, but also
  21069. `visible-mode'."
  21070. ;; Early versions of noutline don't have `outline-invisible-p'.
  21071. (if (org-bound-and-true-p visible-mode)
  21072. nil
  21073. (outline-invisible-p)))
  21074. (defun org-invisible-p2 ()
  21075. "Check if point is at a character currently not visible."
  21076. (save-excursion
  21077. (if (and (eolp) (not (bobp))) (backward-char 1))
  21078. ;; Early versions of noutline don't have `outline-invisible-p'.
  21079. (outline-invisible-p)))
  21080. (defun org-back-to-heading (&optional invisible-ok)
  21081. "Call `outline-back-to-heading', but provide a better error message."
  21082. (condition-case nil
  21083. (outline-back-to-heading invisible-ok)
  21084. (error (error "Before first headline at position %d in buffer %s"
  21085. (point) (current-buffer)))))
  21086. (defun org-before-first-heading-p ()
  21087. "Before first heading?"
  21088. (save-excursion
  21089. (end-of-line)
  21090. (null (re-search-backward org-outline-regexp-bol nil t))))
  21091. (defun org-at-heading-p (&optional ignored)
  21092. (outline-on-heading-p t))
  21093. ;; Compatibility alias with Org versions < 7.8.03
  21094. (defalias 'org-on-heading-p 'org-at-heading-p)
  21095. (defun org-at-comment-p nil
  21096. "Is cursor in a line starting with a # character?"
  21097. (save-excursion
  21098. (beginning-of-line)
  21099. (looking-at "^#")))
  21100. (defun org-at-drawer-p nil
  21101. "Is cursor at a drawer keyword?"
  21102. (save-excursion
  21103. (move-beginning-of-line 1)
  21104. (looking-at org-drawer-regexp)))
  21105. (defun org-at-block-p nil
  21106. "Is cursor at a block keyword?"
  21107. (save-excursion
  21108. (move-beginning-of-line 1)
  21109. (looking-at org-block-regexp)))
  21110. (defun org-point-at-end-of-empty-headline ()
  21111. "If point is at the end of an empty headline, return t, else nil.
  21112. If the heading only contains a TODO keyword, it is still still considered
  21113. empty."
  21114. (and (looking-at "[ \t]*$")
  21115. (when org-todo-line-regexp
  21116. (save-excursion
  21117. (beginning-of-line 1)
  21118. (let ((case-fold-search nil))
  21119. (looking-at org-todo-line-regexp)
  21120. (string= (match-string 3) ""))))))
  21121. (defun org-at-heading-or-item-p ()
  21122. (or (org-at-heading-p) (org-at-item-p)))
  21123. (defun org-at-target-p ()
  21124. (or (org-in-regexp org-radio-target-regexp)
  21125. (org-in-regexp org-target-regexp)))
  21126. ;; Compatibility alias with Org versions < 7.8.03
  21127. (defalias 'org-on-target-p 'org-at-target-p)
  21128. (defun org-up-heading-all (arg)
  21129. "Move to the heading line of which the present line is a subheading.
  21130. This function considers both visible and invisible heading lines.
  21131. With argument, move up ARG levels."
  21132. (if (fboundp 'outline-up-heading-all)
  21133. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21134. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21135. (defun org-up-heading-safe ()
  21136. "Move to the heading line of which the present line is a subheading.
  21137. This version will not throw an error. It will return the level of the
  21138. headline found, or nil if no higher level is found.
  21139. Also, this function will be a lot faster than `outline-up-heading',
  21140. because it relies on stars being the outline starters. This can really
  21141. make a significant difference in outlines with very many siblings."
  21142. (let (start-level re)
  21143. (org-back-to-heading t)
  21144. (setq start-level (funcall outline-level))
  21145. (if (equal start-level 1)
  21146. nil
  21147. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21148. (if (re-search-backward re nil t)
  21149. (funcall outline-level)))))
  21150. (defun org-first-sibling-p ()
  21151. "Is this heading the first child of its parents?"
  21152. (interactive)
  21153. (let ((re org-outline-regexp-bol)
  21154. level l)
  21155. (unless (org-at-heading-p t)
  21156. (user-error "Not at a heading"))
  21157. (setq level (funcall outline-level))
  21158. (save-excursion
  21159. (if (not (re-search-backward re nil t))
  21160. t
  21161. (setq l (funcall outline-level))
  21162. (< l level)))))
  21163. (defun org-goto-sibling (&optional previous)
  21164. "Goto the next sibling, even if it is invisible.
  21165. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21166. when a sibling was found. When none is found, return nil and don't
  21167. move point."
  21168. (let ((fun (if previous 're-search-backward 're-search-forward))
  21169. (pos (point))
  21170. (re org-outline-regexp-bol)
  21171. level l)
  21172. (when (condition-case nil (org-back-to-heading t) (error nil))
  21173. (setq level (funcall outline-level))
  21174. (catch 'exit
  21175. (or previous (forward-char 1))
  21176. (while (funcall fun re nil t)
  21177. (setq l (funcall outline-level))
  21178. (when (< l level) (goto-char pos) (throw 'exit nil))
  21179. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21180. (goto-char pos)
  21181. nil))))
  21182. (defun org-show-siblings ()
  21183. "Show all siblings of the current headline."
  21184. (save-excursion
  21185. (while (org-goto-sibling) (org-flag-heading nil)))
  21186. (save-excursion
  21187. (while (org-goto-sibling 'previous)
  21188. (org-flag-heading nil))))
  21189. (defun org-goto-first-child ()
  21190. "Goto the first child, even if it is invisible.
  21191. Return t when a child was found. Otherwise don't move point and
  21192. return nil."
  21193. (let (level (pos (point)) (re org-outline-regexp-bol))
  21194. (when (condition-case nil (org-back-to-heading t) (error nil))
  21195. (setq level (outline-level))
  21196. (forward-char 1)
  21197. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21198. (progn (goto-char (match-beginning 0)) t)
  21199. (goto-char pos) nil))))
  21200. (defun org-show-hidden-entry ()
  21201. "Show an entry where even the heading is hidden."
  21202. (save-excursion
  21203. (org-show-entry)))
  21204. (defun org-flag-heading (flag &optional entry)
  21205. "Flag the current heading. FLAG non-nil means make invisible.
  21206. When ENTRY is non-nil, show the entire entry."
  21207. (save-excursion
  21208. (org-back-to-heading t)
  21209. ;; Check if we should show the entire entry
  21210. (if entry
  21211. (progn
  21212. (org-show-entry)
  21213. (save-excursion
  21214. (and (outline-next-heading)
  21215. (org-flag-heading nil))))
  21216. (outline-flag-region (max (point-min) (1- (point)))
  21217. (save-excursion (outline-end-of-heading) (point))
  21218. flag))))
  21219. (defun org-get-next-sibling ()
  21220. "Move to next heading of the same level, and return point.
  21221. If there is no such heading, return nil.
  21222. This is like outline-next-sibling, but invisible headings are ok."
  21223. (let ((level (funcall outline-level)))
  21224. (outline-next-heading)
  21225. (while (and (not (eobp)) (> (funcall outline-level) level))
  21226. (outline-next-heading))
  21227. (if (or (eobp) (< (funcall outline-level) level))
  21228. nil
  21229. (point))))
  21230. (defun org-get-last-sibling ()
  21231. "Move to previous heading of the same level, and return point.
  21232. If there is no such heading, return nil."
  21233. (let ((opoint (point))
  21234. (level (funcall outline-level)))
  21235. (outline-previous-heading)
  21236. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21237. (while (and (> (funcall outline-level) level)
  21238. (not (bobp)))
  21239. (outline-previous-heading))
  21240. (if (< (funcall outline-level) level)
  21241. nil
  21242. (point)))))
  21243. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21244. "Goto to the end of a subtree."
  21245. ;; This contains an exact copy of the original function, but it uses
  21246. ;; `org-back-to-heading', to make it work also in invisible
  21247. ;; trees. And is uses an invisible-ok argument.
  21248. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21249. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21250. ;; with many children and grandchildren etc, this can be much faster
  21251. ;; than the outline version.
  21252. (org-back-to-heading invisible-ok)
  21253. (let ((first t)
  21254. (level (funcall outline-level)))
  21255. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21256. ;; A true heading (not a plain list item), in Org-mode
  21257. ;; This means we can easily find the end by looking
  21258. ;; only for the right number of stars. Using a regexp to do
  21259. ;; this is so much faster than using a Lisp loop.
  21260. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21261. (forward-char 1)
  21262. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21263. ;; something else, do it the slow way
  21264. (while (and (not (eobp))
  21265. (or first (> (funcall outline-level) level)))
  21266. (setq first nil)
  21267. (outline-next-heading)))
  21268. (unless to-heading
  21269. (if (memq (preceding-char) '(?\n ?\^M))
  21270. (progn
  21271. ;; Go to end of line before heading
  21272. (forward-char -1)
  21273. (if (memq (preceding-char) '(?\n ?\^M))
  21274. ;; leave blank line before heading
  21275. (forward-char -1))))))
  21276. (point))
  21277. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21278. "Use Org version in org-mode, for dramatic speed-up."
  21279. (if (derived-mode-p 'org-mode)
  21280. (progn
  21281. (org-end-of-subtree nil t)
  21282. (unless (eobp) (backward-char 1)))
  21283. ad-do-it))
  21284. (defun org-end-of-meta-data-and-drawers ()
  21285. "Jump to the first text after meta data and drawers in the current entry.
  21286. This will move over empty lines, lines with planning time stamps,
  21287. clocking lines, and drawers."
  21288. (org-back-to-heading t)
  21289. (let ((end (save-excursion (outline-next-heading) (point)))
  21290. (re (concat "\\(" org-drawer-regexp "\\)"
  21291. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21292. (forward-line 1)
  21293. (while (re-search-forward re end t)
  21294. (if (not (match-end 1))
  21295. ;; empty or planning line
  21296. (forward-line 1)
  21297. ;; a drawer, find the end
  21298. (re-search-forward "^[ \t]*:END:" end 'move)
  21299. (forward-line 1)))
  21300. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21301. (point)))
  21302. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21303. "Move forward to the ARG'th subheading at same level as this one.
  21304. Stop at the first and last subheadings of a superior heading.
  21305. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21306. non-nil it will also look at invisible ones."
  21307. (interactive "p")
  21308. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21309. (if (and arg (< arg 0))
  21310. (goto-char (point-min))
  21311. (outline-next-heading))
  21312. (org-at-heading-p)
  21313. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21314. (f (if (and arg (< arg 0))
  21315. 're-search-backward
  21316. 're-search-forward))
  21317. (count (if arg (abs arg) 1))
  21318. (result (point)))
  21319. (while (and (prog1 (> count 0)
  21320. (forward-char (if (and arg (< arg 0)) -1 1)))
  21321. (funcall f org-outline-regexp-bol nil 'move))
  21322. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21323. (cond ((< l level) (setq count 0))
  21324. ((and (= l level)
  21325. (or invisible-ok
  21326. (progn
  21327. (goto-char (line-beginning-position))
  21328. (not (outline-invisible-p)))))
  21329. (setq count (1- count))
  21330. (when (eq l level)
  21331. (setq result (point)))))))
  21332. (goto-char result))
  21333. (beginning-of-line 1)))
  21334. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21335. "Move backward to the ARG'th subheading at same level as this one.
  21336. Stop at the first and last subheadings of a superior heading."
  21337. (interactive "p")
  21338. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21339. (defun org-next-block (arg &optional backward block-regexp)
  21340. "Jump to the next block.
  21341. With a prefix argument ARG, jump forward ARG many source blocks.
  21342. When BACKWARD is non-nil, jump to the previous block.
  21343. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21344. (interactive "p")
  21345. (let ((re (or block-regexp org-block-regexp))
  21346. (re-search-fn (or (and backward 're-search-backward)
  21347. 're-search-forward)))
  21348. (if (looking-at re) (forward-char 1))
  21349. (condition-case nil
  21350. (funcall re-search-fn re nil nil arg)
  21351. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21352. (goto-char (match-beginning 0)) (org-show-context)))
  21353. (defun org-previous-block (arg &optional block-regexp)
  21354. "Jump to the previous block.
  21355. With a prefix argument ARG, jump backward ARG many source blocks.
  21356. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21357. (interactive "p")
  21358. (org-next-block arg t block-regexp))
  21359. (defun org-forward-element ()
  21360. "Move forward by one element.
  21361. Move to the next element at the same level, when possible."
  21362. (interactive)
  21363. (cond ((eobp) (user-error "Cannot move further down"))
  21364. ((org-with-limited-levels (org-at-heading-p))
  21365. (let ((origin (point)))
  21366. (goto-char (org-end-of-subtree nil t))
  21367. (unless (org-with-limited-levels (org-at-heading-p))
  21368. (goto-char origin)
  21369. (user-error "Cannot move further down"))))
  21370. (t
  21371. (let* ((elem (org-element-at-point))
  21372. (end (org-element-property :end elem))
  21373. (parent (org-element-property :parent elem)))
  21374. (cond ((and parent (= (org-element-property :contents-end parent) end))
  21375. (goto-char (org-element-property :end parent)))
  21376. ((integer-or-marker-p end) (goto-char end))
  21377. (t (message "No element at point")))))))
  21378. (defun org-backward-element ()
  21379. "Move backward by one element.
  21380. Move to the previous element at the same level, when possible."
  21381. (interactive)
  21382. (cond ((bobp) (user-error "Cannot move further up"))
  21383. ((org-with-limited-levels (org-at-heading-p))
  21384. ;; At a headline, move to the previous one, if any, or stay
  21385. ;; here.
  21386. (let ((origin (point)))
  21387. (org-with-limited-levels (org-backward-heading-same-level 1))
  21388. ;; When current headline has no sibling above, move to its
  21389. ;; parent.
  21390. (when (= (point) origin)
  21391. (or (org-with-limited-levels (org-up-heading-safe))
  21392. (progn (goto-char origin)
  21393. (user-error "Cannot move further up"))))))
  21394. (t
  21395. (let* ((trail (org-element-at-point 'keep-trail))
  21396. (elem (car trail))
  21397. (prev-elem (nth 1 trail))
  21398. (beg (org-element-property :begin elem)))
  21399. (cond
  21400. ;; Move to beginning of current element if point isn't
  21401. ;; there already.
  21402. ((null beg) (message "No element at point"))
  21403. ((/= (point) beg) (goto-char beg))
  21404. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21405. ((org-before-first-heading-p) (goto-char (point-min)))
  21406. (t (org-back-to-heading)))))))
  21407. (defun org-up-element ()
  21408. "Move to upper element."
  21409. (interactive)
  21410. (if (org-with-limited-levels (org-at-heading-p))
  21411. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21412. (let* ((elem (org-element-at-point))
  21413. (parent (org-element-property :parent elem)))
  21414. (if parent (goto-char (org-element-property :begin parent))
  21415. (if (org-with-limited-levels (org-before-first-heading-p))
  21416. (user-error "No surrounding element")
  21417. (org-with-limited-levels (org-back-to-heading)))))))
  21418. (defvar org-element-greater-elements)
  21419. (defun org-down-element ()
  21420. "Move to inner element."
  21421. (interactive)
  21422. (let ((element (org-element-at-point)))
  21423. (cond
  21424. ((memq (org-element-type element) '(plain-list table))
  21425. (goto-char (org-element-property :contents-begin element))
  21426. (forward-char))
  21427. ((memq (org-element-type element) org-element-greater-elements)
  21428. ;; If contents are hidden, first disclose them.
  21429. (when (org-element-property :hiddenp element) (org-cycle))
  21430. (goto-char (or (org-element-property :contents-begin element)
  21431. (user-error "No content for this element"))))
  21432. (t (user-error "No inner element")))))
  21433. (defun org-drag-element-backward ()
  21434. "Move backward element at point."
  21435. (interactive)
  21436. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21437. (let* ((trail (org-element-at-point 'keep-trail))
  21438. (elem (car trail))
  21439. (prev-elem (nth 1 trail)))
  21440. ;; Error out if no previous element or previous element is
  21441. ;; a parent of the current one.
  21442. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21443. (user-error "Cannot drag element backward")
  21444. (let ((pos (point)))
  21445. (org-element-swap-A-B prev-elem elem)
  21446. (goto-char (+ (org-element-property :begin prev-elem)
  21447. (- pos (org-element-property :begin elem)))))))))
  21448. (defun org-drag-element-forward ()
  21449. "Move forward element at point."
  21450. (interactive)
  21451. (let* ((pos (point))
  21452. (elem (org-element-at-point)))
  21453. (when (= (point-max) (org-element-property :end elem))
  21454. (user-error "Cannot drag element forward"))
  21455. (goto-char (org-element-property :end elem))
  21456. (let ((next-elem (org-element-at-point)))
  21457. (when (or (org-element-nested-p elem next-elem)
  21458. (and (eq (org-element-type next-elem) 'headline)
  21459. (not (eq (org-element-type elem) 'headline))))
  21460. (goto-char pos)
  21461. (user-error "Cannot drag element forward"))
  21462. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21463. ;; body's length (without final blanks) and by the length of
  21464. ;; blanks between ELEM and NEXT-ELEM.
  21465. (let ((size-next (- (save-excursion
  21466. (goto-char (org-element-property :end next-elem))
  21467. (skip-chars-backward " \r\t\n")
  21468. (forward-line)
  21469. ;; Small correction if buffer doesn't end
  21470. ;; with a newline character.
  21471. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21472. (org-element-property :begin next-elem)))
  21473. (size-blank (- (org-element-property :end elem)
  21474. (save-excursion
  21475. (goto-char (org-element-property :end elem))
  21476. (skip-chars-backward " \r\t\n")
  21477. (forward-line)
  21478. (point)))))
  21479. (org-element-swap-A-B elem next-elem)
  21480. (goto-char (+ pos size-next size-blank))))))
  21481. (defun org-drag-line-forward (arg)
  21482. "Drag the line at point ARG lines forward."
  21483. (interactive "p")
  21484. (dotimes (n (abs arg))
  21485. (let ((c (current-column)))
  21486. (if (< 0 arg)
  21487. (progn
  21488. (beginning-of-line 2)
  21489. (transpose-lines 1)
  21490. (beginning-of-line 0))
  21491. (transpose-lines 1)
  21492. (beginning-of-line -1))
  21493. (org-move-to-column c))))
  21494. (defun org-drag-line-backward (arg)
  21495. "Drag the line at point ARG lines backward."
  21496. (interactive "p")
  21497. (org-drag-line-forward (- arg)))
  21498. (defun org-mark-element ()
  21499. "Put point at beginning of this element, mark at end.
  21500. Interactively, if this command is repeated or (in Transient Mark
  21501. mode) if the mark is active, it marks the next element after the
  21502. ones already marked."
  21503. (interactive)
  21504. (let (deactivate-mark)
  21505. (if (and (org-called-interactively-p 'any)
  21506. (or (and (eq last-command this-command) (mark t))
  21507. (and transient-mark-mode mark-active)))
  21508. (set-mark
  21509. (save-excursion
  21510. (goto-char (mark))
  21511. (goto-char (org-element-property :end (org-element-at-point)))))
  21512. (let ((element (org-element-at-point)))
  21513. (end-of-line)
  21514. (push-mark (org-element-property :end element) t t)
  21515. (goto-char (org-element-property :begin element))))))
  21516. (defun org-narrow-to-element ()
  21517. "Narrow buffer to current element."
  21518. (interactive)
  21519. (let ((elem (org-element-at-point)))
  21520. (cond
  21521. ((eq (car elem) 'headline)
  21522. (narrow-to-region
  21523. (org-element-property :begin elem)
  21524. (org-element-property :end elem)))
  21525. ((memq (car elem) org-element-greater-elements)
  21526. (narrow-to-region
  21527. (org-element-property :contents-begin elem)
  21528. (org-element-property :contents-end elem)))
  21529. (t
  21530. (narrow-to-region
  21531. (org-element-property :begin elem)
  21532. (org-element-property :end elem))))))
  21533. (defun org-transpose-element ()
  21534. "Transpose current and previous elements, keeping blank lines between.
  21535. Point is moved after both elements."
  21536. (interactive)
  21537. (org-skip-whitespace)
  21538. (let ((end (org-element-property :end (org-element-at-point))))
  21539. (org-drag-element-backward)
  21540. (goto-char end)))
  21541. (defun org-unindent-buffer ()
  21542. "Un-indent the visible part of the buffer.
  21543. Relative indentation (between items, inside blocks, etc.) isn't
  21544. modified."
  21545. (interactive)
  21546. (unless (eq major-mode 'org-mode)
  21547. (user-error "Cannot un-indent a buffer not in Org mode"))
  21548. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21549. unindent-tree ; For byte-compiler.
  21550. (unindent-tree
  21551. (function
  21552. (lambda (contents)
  21553. (mapc
  21554. (lambda (element)
  21555. (if (memq (org-element-type element) '(headline section))
  21556. (funcall unindent-tree (org-element-contents element))
  21557. (save-excursion
  21558. (save-restriction
  21559. (narrow-to-region
  21560. (org-element-property :begin element)
  21561. (org-element-property :end element))
  21562. (org-do-remove-indentation)))))
  21563. (reverse contents))))))
  21564. (funcall unindent-tree (org-element-contents parse-tree))))
  21565. (defun org-show-subtree ()
  21566. "Show everything after this heading at deeper levels."
  21567. (interactive)
  21568. (outline-flag-region
  21569. (point)
  21570. (save-excursion
  21571. (org-end-of-subtree t t))
  21572. nil))
  21573. (defun org-show-entry ()
  21574. "Show the body directly following this heading.
  21575. Show the heading too, if it is currently invisible."
  21576. (interactive)
  21577. (save-excursion
  21578. (condition-case nil
  21579. (progn
  21580. (org-back-to-heading t)
  21581. (outline-flag-region
  21582. (max (point-min) (1- (point)))
  21583. (save-excursion
  21584. (if (re-search-forward
  21585. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21586. (match-beginning 1)
  21587. (point-max)))
  21588. nil)
  21589. (org-cycle-hide-drawers 'children))
  21590. (error nil))))
  21591. (defun org-make-options-regexp (kwds &optional extra)
  21592. "Make a regular expression for keyword lines."
  21593. (concat
  21594. "^#\\+\\("
  21595. (mapconcat 'regexp-quote kwds "\\|")
  21596. (if extra (concat "\\|" extra))
  21597. "\\):[ \t]*\\(.*\\)"))
  21598. ;; Make isearch reveal the necessary context
  21599. (defun org-isearch-end ()
  21600. "Reveal context after isearch exits."
  21601. (when isearch-success ; only if search was successful
  21602. (if (featurep 'xemacs)
  21603. ;; Under XEmacs, the hook is run in the correct place,
  21604. ;; we directly show the context.
  21605. (org-show-context 'isearch)
  21606. ;; In Emacs the hook runs *before* restoring the overlays.
  21607. ;; So we have to use a one-time post-command-hook to do this.
  21608. ;; (Emacs 22 has a special variable, see function `org-mode')
  21609. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21610. isearch-mode-end-hook-quit)
  21611. ;; Only when the isearch was not quitted.
  21612. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21613. 'append 'local)))
  21614. (org-fix-ellipsis-at-bol)))
  21615. (defun org-isearch-post-command ()
  21616. "Remove self from hook, and show context."
  21617. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21618. (org-show-context 'isearch))
  21619. ;;;; Integration with and fixes for other packages
  21620. ;;; Imenu support
  21621. (defvar org-imenu-markers nil
  21622. "All markers currently used by Imenu.")
  21623. (make-variable-buffer-local 'org-imenu-markers)
  21624. (defun org-imenu-new-marker (&optional pos)
  21625. "Return a new marker for use by Imenu, and remember the marker."
  21626. (let ((m (make-marker)))
  21627. (move-marker m (or pos (point)))
  21628. (push m org-imenu-markers)
  21629. m))
  21630. (defun org-imenu-get-tree ()
  21631. "Produce the index for Imenu."
  21632. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21633. (setq org-imenu-markers nil)
  21634. (let* ((n org-imenu-depth)
  21635. (re (concat "^" (org-get-limited-outline-regexp)))
  21636. (subs (make-vector (1+ n) nil))
  21637. (last-level 0)
  21638. m level head0 head)
  21639. (save-excursion
  21640. (save-restriction
  21641. (widen)
  21642. (goto-char (point-max))
  21643. (while (re-search-backward re nil t)
  21644. (setq level (org-reduced-level (funcall outline-level)))
  21645. (when (and (<= level n)
  21646. (looking-at org-complex-heading-regexp)
  21647. (setq head0 (org-match-string-no-properties 4)))
  21648. (setq head (org-link-display-format head0)
  21649. m (org-imenu-new-marker))
  21650. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21651. (if (>= level last-level)
  21652. (push (cons head m) (aref subs level))
  21653. (push (cons head (aref subs (1+ level))) (aref subs level))
  21654. (loop for i from (1+ level) to n do (aset subs i nil)))
  21655. (setq last-level level)))))
  21656. (aref subs 1)))
  21657. (eval-after-load "imenu"
  21658. '(progn
  21659. (add-hook 'imenu-after-jump-hook
  21660. (lambda ()
  21661. (if (derived-mode-p 'org-mode)
  21662. (org-show-context 'org-goto))))))
  21663. (defun org-link-display-format (link)
  21664. "Replace a link with its the description.
  21665. If there is no description, use the link target."
  21666. (save-match-data
  21667. (if (string-match org-bracket-link-analytic-regexp link)
  21668. (replace-match (if (match-end 5)
  21669. (match-string 5 link)
  21670. (concat (match-string 1 link)
  21671. (match-string 3 link)))
  21672. nil t link)
  21673. link)))
  21674. (defun org-toggle-link-display ()
  21675. "Toggle the literal or descriptive display of links."
  21676. (interactive)
  21677. (if org-descriptive-links
  21678. (progn (org-remove-from-invisibility-spec '(org-link))
  21679. (org-restart-font-lock)
  21680. (setq org-descriptive-links nil))
  21681. (progn (add-to-invisibility-spec '(org-link))
  21682. (org-restart-font-lock)
  21683. (setq org-descriptive-links t))))
  21684. ;; Speedbar support
  21685. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21686. "Overlay marking the agenda restriction line in speedbar.")
  21687. (overlay-put org-speedbar-restriction-lock-overlay
  21688. 'face 'org-agenda-restriction-lock)
  21689. (overlay-put org-speedbar-restriction-lock-overlay
  21690. 'help-echo "Agendas are currently limited to this item.")
  21691. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21692. (defun org-speedbar-set-agenda-restriction ()
  21693. "Restrict future agenda commands to the location at point in speedbar.
  21694. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21695. (interactive)
  21696. (require 'org-agenda)
  21697. (let (p m tp np dir txt)
  21698. (cond
  21699. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21700. 'org-imenu t))
  21701. (setq m (get-text-property p 'org-imenu-marker))
  21702. (with-current-buffer (marker-buffer m)
  21703. (goto-char m)
  21704. (org-agenda-set-restriction-lock 'subtree)))
  21705. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21706. 'speedbar-function 'speedbar-find-file))
  21707. (setq tp (previous-single-property-change
  21708. (1+ p) 'speedbar-function)
  21709. np (next-single-property-change
  21710. tp 'speedbar-function)
  21711. dir (speedbar-line-directory)
  21712. txt (buffer-substring-no-properties (or tp (point-min))
  21713. (or np (point-max))))
  21714. (with-current-buffer (find-file-noselect
  21715. (let ((default-directory dir))
  21716. (expand-file-name txt)))
  21717. (unless (derived-mode-p 'org-mode)
  21718. (user-error "Cannot restrict to non-Org-mode file"))
  21719. (org-agenda-set-restriction-lock 'file)))
  21720. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21721. (move-overlay org-speedbar-restriction-lock-overlay
  21722. (point-at-bol) (point-at-eol))
  21723. (setq current-prefix-arg nil)
  21724. (org-agenda-maybe-redo)))
  21725. (defvar speedbar-file-key-map)
  21726. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  21727. (eval-after-load "speedbar"
  21728. '(progn
  21729. (speedbar-add-supported-extension ".org")
  21730. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21731. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21732. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21733. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21734. (add-hook 'speedbar-visiting-tag-hook
  21735. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21736. ;;; Fixes and Hacks for problems with other packages
  21737. ;; Make flyspell not check words in links, to not mess up our keymap
  21738. (defvar org-element-affiliated-keywords) ; From org-element.el
  21739. (defvar org-element-block-name-alist) ; From org-element.el
  21740. (defun org-mode-flyspell-verify ()
  21741. "Don't let flyspell put overlays at active buttons, or on
  21742. {todo,all-time,additional-option-like}-keywords."
  21743. (require 'org-element) ; For `org-element-affiliated-keywords'
  21744. (let ((pos (max (1- (point)) (point-min)))
  21745. (word (thing-at-point 'word)))
  21746. (and (not (get-text-property pos 'keymap))
  21747. (not (get-text-property pos 'org-no-flyspell))
  21748. (not (member word org-todo-keywords-1))
  21749. (not (member word org-all-time-keywords))
  21750. (not (member word org-options-keywords))
  21751. (not (member word (mapcar 'car org-startup-options)))
  21752. (not (member-ignore-case word org-element-affiliated-keywords))
  21753. (not (member-ignore-case word (org-get-export-keywords)))
  21754. (not (member-ignore-case
  21755. word (mapcar 'car org-element-block-name-alist)))
  21756. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  21757. (not (org-in-src-block-p)))))
  21758. (defun org-remove-flyspell-overlays-in (beg end)
  21759. "Remove flyspell overlays in region."
  21760. (and (org-bound-and-true-p flyspell-mode)
  21761. (fboundp 'flyspell-delete-region-overlays)
  21762. (flyspell-delete-region-overlays beg end))
  21763. (add-text-properties beg end '(org-no-flyspell t)))
  21764. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21765. (eval-after-load "bookmark"
  21766. '(if (boundp 'bookmark-after-jump-hook)
  21767. ;; We can use the hook
  21768. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21769. ;; Hook not available, use advice
  21770. (defadvice bookmark-jump (after org-make-visible activate)
  21771. "Make the position visible."
  21772. (org-bookmark-jump-unhide))))
  21773. ;; Make sure saveplace shows the location if it was hidden
  21774. (eval-after-load "saveplace"
  21775. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21776. "Make the position visible."
  21777. (org-bookmark-jump-unhide)))
  21778. ;; Make sure ecb shows the location if it was hidden
  21779. (eval-after-load "ecb"
  21780. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21781. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21782. (if (derived-mode-p 'org-mode)
  21783. (org-show-context))))
  21784. (defun org-bookmark-jump-unhide ()
  21785. "Unhide the current position, to show the bookmark location."
  21786. (and (derived-mode-p 'org-mode)
  21787. (or (outline-invisible-p)
  21788. (save-excursion (goto-char (max (point-min) (1- (point))))
  21789. (outline-invisible-p)))
  21790. (org-show-context 'bookmark-jump)))
  21791. ;; Make session.el ignore our circular variable
  21792. (defvar session-globals-exclude)
  21793. (eval-after-load "session"
  21794. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21795. ;;;; Finish up
  21796. (provide 'org)
  21797. (run-hooks 'org-load-hook)
  21798. ;;; org.el ends here