org.el 897 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733
  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: Grab links and url from various mac Applications" org-mac-link)
  381. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  382. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  383. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  384. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  385. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  386. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  387. (const :tag "C registry: A registry for Org-mode links" org-registry)
  388. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  389. (const :tag "C secretary: Team management with org-mode" org-secretary)
  390. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  391. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  392. (const :tag "C track: Keep up with Org-mode development" org-track)
  393. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  394. (const :tag "C vm: Links to VM folders/messages" org-vm)
  395. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  396. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  397. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  398. (defvar org-export--registered-backends) ; From ox.el.
  399. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  400. (declare-function org-export-backend-name "ox" (backend))
  401. (defcustom org-export-backends '(ascii html icalendar latex)
  402. "List of export back-ends that should be always available.
  403. If a description starts with <C>, the file is not part of Emacs
  404. and loading it will require that you have downloaded and properly
  405. installed the Org mode distribution.
  406. Unlike to `org-modules', libraries in this list will not be
  407. loaded along with Org, but only once the export framework is
  408. needed.
  409. This variable needs to be set before org.el is loaded. If you
  410. need to make a change while Emacs is running, use the customize
  411. interface or run the following code, where VAL stands for the new
  412. value of the variable, after updating it:
  413. \(progn
  414. \(setq org-export--registered-backends
  415. \(org-remove-if-not
  416. \(lambda (backend)
  417. \(let ((name (org-export-backend-name backend)))
  418. \(or (memq name val)
  419. \(catch 'parentp
  420. \(dolist (b val)
  421. \(and (org-export-derived-backend-p b name)
  422. \(throw 'parentp t)))))))
  423. org-export--registered-backends))
  424. \(let ((new-list (mapcar 'org-export-backend-name
  425. org-export--registered-backends)))
  426. \(dolist (backend val)
  427. \(cond
  428. \((not (load (format \"ox-%s\" backend) t t))
  429. \(message \"Problems while trying to load export back-end `%s'\"
  430. backend))
  431. \((not (memq backend new-list)) (push backend new-list))))
  432. \(set-default 'org-export-backends new-list)))
  433. Adding a back-end to this list will also pull the back-end it
  434. depends on, if any."
  435. :group 'org
  436. :group 'org-export
  437. :version "24.4"
  438. :package-version '(Org . "8.0")
  439. :initialize 'custom-initialize-set
  440. :set (lambda (var val)
  441. (if (not (featurep 'ox)) (set-default var val)
  442. ;; Any back-end not required anymore (not present in VAL and not
  443. ;; a parent of any back-end in the new value) is removed from the
  444. ;; list of registered back-ends.
  445. (setq org-export--registered-backends
  446. (org-remove-if-not
  447. (lambda (backend)
  448. (let ((name (org-export-backend-name backend)))
  449. (or (memq name val)
  450. (catch 'parentp
  451. (dolist (b val)
  452. (and (org-export-derived-backend-p b name)
  453. (throw 'parentp t)))))))
  454. org-export--registered-backends))
  455. ;; Now build NEW-LIST of both new back-ends and required
  456. ;; parents.
  457. (let ((new-list (mapcar 'org-export-backend-name
  458. org-export--registered-backends)))
  459. (dolist (backend val)
  460. (cond
  461. ((not (load (format "ox-%s" backend) t t))
  462. (message "Problems while trying to load export back-end `%s'"
  463. backend))
  464. ((not (memq backend new-list)) (push backend new-list))))
  465. ;; Set VAR to that list with fixed dependencies.
  466. (set-default var new-list))))
  467. :type '(set :greedy t
  468. (const :tag " ascii Export buffer to ASCII format" ascii)
  469. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  470. (const :tag " html Export buffer to HTML format" html)
  471. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  472. (const :tag " latex Export buffer to LaTeX format" latex)
  473. (const :tag " man Export buffer to MAN format" man)
  474. (const :tag " md Export buffer to Markdown format" md)
  475. (const :tag " odt Export buffer to ODT format" odt)
  476. (const :tag " org Export buffer to Org format" org)
  477. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  478. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  479. (const :tag "C deck Export buffer to deck.js presentations" deck)
  480. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  481. (const :tag "C groff Export buffer to Groff format" groff)
  482. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  483. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  484. (const :tag "C s5 Export buffer to s5 presentations" s5)
  485. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  486. (eval-after-load 'ox
  487. '(mapc
  488. (lambda (backend)
  489. (condition-case nil (require (intern (format "ox-%s" backend)))
  490. (error (message "Problems while trying to load export back-end `%s'"
  491. backend))))
  492. org-export-backends))
  493. (defcustom org-support-shift-select nil
  494. "Non-nil means make shift-cursor commands select text when possible.
  495. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  496. start selecting a region, or enlarge regions started in this way.
  497. In Org-mode, in special contexts, these same keys are used for
  498. other purposes, important enough to compete with shift selection.
  499. Org tries to balance these needs by supporting `shift-select-mode'
  500. outside these special contexts, under control of this variable.
  501. The default of this variable is nil, to avoid confusing behavior. Shifted
  502. cursor keys will then execute Org commands in the following contexts:
  503. - on a headline, changing TODO state (left/right) and priority (up/down)
  504. - on a time stamp, changing the time
  505. - in a plain list item, changing the bullet type
  506. - in a property definition line, switching between allowed values
  507. - in the BEGIN line of a clock table (changing the time block).
  508. Outside these contexts, the commands will throw an error.
  509. When this variable is t and the cursor is not in a special
  510. context, Org-mode will support shift-selection for making and
  511. enlarging regions. To make this more effective, the bullet
  512. cycling will no longer happen anywhere in an item line, but only
  513. if the cursor is exactly on the bullet.
  514. If you set this variable to the symbol `always', then the keys
  515. will not be special in headlines, property lines, and item lines,
  516. to make shift selection work there as well. If this is what you
  517. want, you can use the following alternative commands: `C-c C-t'
  518. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  519. can be used to switch TODO sets, `C-c -' to cycle item bullet
  520. types, and properties can be edited by hand or in column view.
  521. However, when the cursor is on a timestamp, shift-cursor commands
  522. will still edit the time stamp - this is just too good to give up.
  523. XEmacs user should have this variable set to nil, because
  524. `shift-select-mode' is in Emacs 23 or later only."
  525. :group 'org
  526. :type '(choice
  527. (const :tag "Never" nil)
  528. (const :tag "When outside special context" t)
  529. (const :tag "Everywhere except timestamps" always)))
  530. (defcustom org-loop-over-headlines-in-active-region nil
  531. "Shall some commands act upon headlines in the active region?
  532. When set to `t', some commands will be performed in all headlines
  533. within the active region.
  534. When set to `start-level', some commands will be performed in all
  535. headlines within the active region, provided that these headlines
  536. are of the same level than the first one.
  537. When set to a string, those commands will be performed on the
  538. matching headlines within the active region. Such string must be
  539. a tags/property/todo match as it is used in the agenda tags view.
  540. The list of commands is: `org-schedule', `org-deadline',
  541. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  542. `org-archive-to-archive-sibling'. The archiving commands skip
  543. already archived entries."
  544. :type '(choice (const :tag "Don't loop" nil)
  545. (const :tag "All headlines in active region" t)
  546. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  547. (string :tag "Tags/Property/Todo matcher"))
  548. :version "24.1"
  549. :group 'org-todo
  550. :group 'org-archive)
  551. (defgroup org-startup nil
  552. "Options concerning startup of Org-mode."
  553. :tag "Org Startup"
  554. :group 'org)
  555. (defcustom org-startup-folded t
  556. "Non-nil means entering Org-mode will switch to OVERVIEW.
  557. This can also be configured on a per-file basis by adding one of
  558. the following lines anywhere in the buffer:
  559. #+STARTUP: fold (or `overview', this is equivalent)
  560. #+STARTUP: nofold (or `showall', this is equivalent)
  561. #+STARTUP: content
  562. #+STARTUP: showeverything
  563. By default, this option is ignored when Org opens agenda files
  564. for the first time. If you want the agenda to honor the startup
  565. option, set `org-agenda-inhibit-startup' to nil."
  566. :group 'org-startup
  567. :type '(choice
  568. (const :tag "nofold: show all" nil)
  569. (const :tag "fold: overview" t)
  570. (const :tag "content: all headlines" content)
  571. (const :tag "show everything, even drawers" showeverything)))
  572. (defcustom org-startup-truncated t
  573. "Non-nil means entering Org-mode will set `truncate-lines'.
  574. This is useful since some lines containing links can be very long and
  575. uninteresting. Also tables look terrible when wrapped."
  576. :group 'org-startup
  577. :type 'boolean)
  578. (defcustom org-startup-indented nil
  579. "Non-nil means turn on `org-indent-mode' on startup.
  580. This can also be configured on a per-file basis by adding one of
  581. the following lines anywhere in the buffer:
  582. #+STARTUP: indent
  583. #+STARTUP: noindent"
  584. :group 'org-structure
  585. :type '(choice
  586. (const :tag "Not" nil)
  587. (const :tag "Globally (slow on startup in large files)" t)))
  588. (defcustom org-use-sub-superscripts t
  589. "Non-nil means interpret \"_\" and \"^\" for display.
  590. When this option is turned on, you can use TeX-like syntax for sub- and
  591. superscripts. Several characters after \"_\" or \"^\" will be
  592. considered as a single item - so grouping with {} is normally not
  593. needed. For example, the following things will be parsed as single
  594. sub- or superscripts.
  595. 10^24 or 10^tau several digits will be considered 1 item.
  596. 10^-12 or 10^-tau a leading sign with digits or a word
  597. x^2-y^3 will be read as x^2 - y^3, because items are
  598. terminated by almost any nonword/nondigit char.
  599. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  600. Still, ambiguity is possible - so when in doubt use {} to enclose
  601. the sub/superscript. If you set this variable to the symbol
  602. `{}', the braces are *required* in order to trigger
  603. interpretations as sub/superscript. This can be helpful in
  604. documents that need \"_\" frequently in plain text."
  605. :group 'org-startup
  606. :version "24.1"
  607. :type '(choice
  608. (const :tag "Always interpret" t)
  609. (const :tag "Only with braces" {})
  610. (const :tag "Never interpret" nil)))
  611. (defcustom org-startup-with-beamer-mode nil
  612. "Non-nil means turn on `org-beamer-mode' on startup.
  613. This can also be configured on a per-file basis by adding one of
  614. the following lines anywhere in the buffer:
  615. #+STARTUP: beamer"
  616. :group 'org-startup
  617. :version "24.1"
  618. :type 'boolean)
  619. (defcustom org-startup-align-all-tables nil
  620. "Non-nil means align all tables when visiting a file.
  621. This is useful when the column width in tables is forced with <N> cookies
  622. in table fields. Such tables will look correct only after the first re-align.
  623. This can also be configured on a per-file basis by adding one of
  624. the following lines anywhere in the buffer:
  625. #+STARTUP: align
  626. #+STARTUP: noalign"
  627. :group 'org-startup
  628. :type 'boolean)
  629. (defcustom org-startup-with-inline-images nil
  630. "Non-nil means show inline images when loading a new Org file.
  631. This can also be configured on a per-file basis by adding one of
  632. the following lines anywhere in the buffer:
  633. #+STARTUP: inlineimages
  634. #+STARTUP: noinlineimages"
  635. :group 'org-startup
  636. :version "24.1"
  637. :type 'boolean)
  638. (defcustom org-startup-with-latex-preview nil
  639. "Non-nil means preview LaTeX fragments when loading a new Org file.
  640. This can also be configured on a per-file basis by adding one of
  641. the followinglines anywhere in the buffer:
  642. #+STARTUP: latexpreview
  643. #+STARTUP: nolatexpreview"
  644. :group 'org-startup
  645. :version "24.4"
  646. :package-version '(Org . "8.0")
  647. :type 'boolean)
  648. (defcustom org-insert-mode-line-in-empty-file nil
  649. "Non-nil means insert the first line setting Org-mode in empty files.
  650. When the function `org-mode' is called interactively in an empty file, this
  651. normally means that the file name does not automatically trigger Org-mode.
  652. To ensure that the file will always be in Org-mode in the future, a
  653. line enforcing Org-mode will be inserted into the buffer, if this option
  654. has been set."
  655. :group 'org-startup
  656. :type 'boolean)
  657. (defcustom org-replace-disputed-keys nil
  658. "Non-nil means use alternative key bindings for some keys.
  659. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  660. These keys are also used by other packages like shift-selection-mode'
  661. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  662. If you want to use Org-mode together with one of these other modes,
  663. or more generally if you would like to move some Org-mode commands to
  664. other keys, set this variable and configure the keys with the variable
  665. `org-disputed-keys'.
  666. This option is only relevant at load-time of Org-mode, and must be set
  667. *before* org.el is loaded. Changing it requires a restart of Emacs to
  668. become effective."
  669. :group 'org-startup
  670. :type 'boolean)
  671. (defcustom org-use-extra-keys nil
  672. "Non-nil means use extra key sequence definitions for certain commands.
  673. This happens automatically if you run XEmacs or if `window-system'
  674. is nil. This variable lets you do the same manually. You must
  675. set it before loading org.
  676. Example: on Carbon Emacs 22 running graphically, with an external
  677. keyboard on a Powerbook, the default way of setting M-left might
  678. not work for either Alt or ESC. Setting this variable will make
  679. it work for ESC."
  680. :group 'org-startup
  681. :type 'boolean)
  682. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  683. (defcustom org-disputed-keys
  684. '(([(shift up)] . [(meta p)])
  685. ([(shift down)] . [(meta n)])
  686. ([(shift left)] . [(meta -)])
  687. ([(shift right)] . [(meta +)])
  688. ([(control shift right)] . [(meta shift +)])
  689. ([(control shift left)] . [(meta shift -)]))
  690. "Keys for which Org-mode and other modes compete.
  691. This is an alist, cars are the default keys, second element specifies
  692. the alternative to use when `org-replace-disputed-keys' is t.
  693. Keys can be specified in any syntax supported by `define-key'.
  694. The value of this option takes effect only at Org-mode's startup,
  695. therefore you'll have to restart Emacs to apply it after changing."
  696. :group 'org-startup
  697. :type 'alist)
  698. (defun org-key (key)
  699. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  700. Or return the original if not disputed.
  701. Also apply the translations defined in `org-xemacs-key-equivalents'."
  702. (when org-replace-disputed-keys
  703. (let* ((nkey (key-description key))
  704. (x (org-find-if (lambda (x)
  705. (equal (key-description (car x)) nkey))
  706. org-disputed-keys)))
  707. (setq key (if x (cdr x) key))))
  708. (when (featurep 'xemacs)
  709. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  710. key)
  711. (defun org-find-if (predicate seq)
  712. (catch 'exit
  713. (while seq
  714. (if (funcall predicate (car seq))
  715. (throw 'exit (car seq))
  716. (pop seq)))))
  717. (defun org-defkey (keymap key def)
  718. "Define a key, possibly translated, as returned by `org-key'."
  719. (define-key keymap (org-key key) def))
  720. (defcustom org-ellipsis nil
  721. "The ellipsis to use in the Org-mode outline.
  722. When nil, just use the standard three dots. When a string, use that instead,
  723. When a face, use the standard 3 dots, but with the specified face.
  724. The change affects only Org-mode (which will then use its own display table).
  725. Changing this requires executing `M-x org-mode' in a buffer to become
  726. effective."
  727. :group 'org-startup
  728. :type '(choice (const :tag "Default" nil)
  729. (face :tag "Face" :value org-warning)
  730. (string :tag "String" :value "...#")))
  731. (defvar org-display-table nil
  732. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  733. (defgroup org-keywords nil
  734. "Keywords in Org-mode."
  735. :tag "Org Keywords"
  736. :group 'org)
  737. (defcustom org-deadline-string "DEADLINE:"
  738. "String to mark deadline entries.
  739. A deadline is this string, followed by a time stamp. Should be a word,
  740. terminated by a colon. You can insert a schedule keyword and
  741. a timestamp with \\[org-deadline].
  742. Changes become only effective after restarting Emacs."
  743. :group 'org-keywords
  744. :type 'string)
  745. (defcustom org-scheduled-string "SCHEDULED:"
  746. "String to mark scheduled TODO entries.
  747. A schedule is this string, followed by a time stamp. Should be a word,
  748. terminated by a colon. You can insert a schedule keyword and
  749. a timestamp with \\[org-schedule].
  750. Changes become only effective after restarting Emacs."
  751. :group 'org-keywords
  752. :type 'string)
  753. (defcustom org-closed-string "CLOSED:"
  754. "String used as the prefix for timestamps logging closing a TODO entry."
  755. :group 'org-keywords
  756. :type 'string)
  757. (defcustom org-clock-string "CLOCK:"
  758. "String used as prefix for timestamps clocking work hours on an item."
  759. :group 'org-keywords
  760. :type 'string)
  761. (defcustom org-closed-keep-when-no-todo nil
  762. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  763. :group 'org-todo
  764. :group 'org-keywords
  765. :version "24.4"
  766. :package-version '(Org . "8.0")
  767. :type 'boolean)
  768. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  769. org-scheduled-string "\\|"
  770. org-deadline-string "\\|"
  771. org-closed-string "\\|"
  772. org-clock-string "\\)")
  773. "Matches a line with planning or clock info.")
  774. (defcustom org-comment-string "COMMENT"
  775. "Entries starting with this keyword will never be exported.
  776. An entry can be toggled between COMMENT and normal with
  777. \\[org-toggle-comment].
  778. Changes become only effective after restarting Emacs."
  779. :group 'org-keywords
  780. :type 'string)
  781. (defcustom org-quote-string "QUOTE"
  782. "Entries starting with this keyword will be exported in fixed-width font.
  783. Quoting applies only to the text in the entry following the headline, and does
  784. not extend beyond the next headline, even if that is lower level.
  785. An entry can be toggled between QUOTE and normal with
  786. \\[org-toggle-fixed-width-section]."
  787. :group 'org-keywords
  788. :type 'string)
  789. (defconst org-repeat-re
  790. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  791. "Regular expression for specifying repeated events.
  792. After a match, group 1 contains the repeat expression.")
  793. (defgroup org-structure nil
  794. "Options concerning the general structure of Org-mode files."
  795. :tag "Org Structure"
  796. :group 'org)
  797. (defgroup org-reveal-location nil
  798. "Options about how to make context of a location visible."
  799. :tag "Org Reveal Location"
  800. :group 'org-structure)
  801. (defconst org-context-choice
  802. '(choice
  803. (const :tag "Always" t)
  804. (const :tag "Never" nil)
  805. (repeat :greedy t :tag "Individual contexts"
  806. (cons
  807. (choice :tag "Context"
  808. (const agenda)
  809. (const org-goto)
  810. (const occur-tree)
  811. (const tags-tree)
  812. (const link-search)
  813. (const mark-goto)
  814. (const bookmark-jump)
  815. (const isearch)
  816. (const default))
  817. (boolean))))
  818. "Contexts for the reveal options.")
  819. (defcustom org-show-hierarchy-above '((default . t))
  820. "Non-nil means show full hierarchy when revealing a location.
  821. Org-mode often shows locations in an org-mode file which might have
  822. been invisible before. When this is set, the hierarchy of headings
  823. above the exposed location is shown.
  824. Turning this off for example for sparse trees makes them very compact.
  825. Instead of t, this can also be an alist specifying this option for different
  826. contexts. Valid contexts are
  827. agenda when exposing an entry from the agenda
  828. org-goto when using the command `org-goto' on key C-c C-j
  829. occur-tree when using the command `org-occur' on key C-c /
  830. tags-tree when constructing a sparse tree based on tags matches
  831. link-search when exposing search matches associated with a link
  832. mark-goto when exposing the jump goal of a mark
  833. bookmark-jump when exposing a bookmark location
  834. isearch when exiting from an incremental search
  835. default default for all contexts not set explicitly"
  836. :group 'org-reveal-location
  837. :type org-context-choice)
  838. (defcustom org-show-following-heading '((default . nil))
  839. "Non-nil means show following heading when revealing a location.
  840. Org-mode often shows locations in an org-mode file which might have
  841. been invisible before. When this is set, the heading following the
  842. match is shown.
  843. Turning this off for example for sparse trees makes them very compact,
  844. but makes it harder to edit the location of the match. In such a case,
  845. use the command \\[org-reveal] to show more context.
  846. Instead of t, this can also be an alist specifying this option for different
  847. contexts. See `org-show-hierarchy-above' for valid contexts."
  848. :group 'org-reveal-location
  849. :type org-context-choice)
  850. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  851. "Non-nil means show all sibling heading when revealing a location.
  852. Org-mode often shows locations in an org-mode file which might have
  853. been invisible before. When this is set, the sibling of the current entry
  854. heading are all made visible. If `org-show-hierarchy-above' is t,
  855. the same happens on each level of the hierarchy above the current entry.
  856. By default this is on for the isearch context, off for all other contexts.
  857. Turning this off for example for sparse trees makes them very compact,
  858. but makes it harder to edit the location of the match. In such a case,
  859. use the command \\[org-reveal] to show more context.
  860. Instead of t, this can also be an alist specifying this option for different
  861. contexts. See `org-show-hierarchy-above' for valid contexts."
  862. :group 'org-reveal-location
  863. :type org-context-choice
  864. :version "24.4"
  865. :package-version '(Org . "8.0"))
  866. (defcustom org-show-entry-below '((default . nil))
  867. "Non-nil means show the entry below a headline when revealing a location.
  868. Org-mode often shows locations in an org-mode file which might have
  869. been invisible before. When this is set, the text below the headline that is
  870. exposed is also shown.
  871. By default this is off for all contexts.
  872. Instead of t, this can also be an alist specifying this option for different
  873. contexts. See `org-show-hierarchy-above' for valid contexts."
  874. :group 'org-reveal-location
  875. :type org-context-choice)
  876. (defcustom org-indirect-buffer-display 'other-window
  877. "How should indirect tree buffers be displayed?
  878. This applies to indirect buffers created with the commands
  879. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  880. Valid values are:
  881. current-window Display in the current window
  882. other-window Just display in another window.
  883. dedicated-frame Create one new frame, and re-use it each time.
  884. new-frame Make a new frame each time. Note that in this case
  885. previously-made indirect buffers are kept, and you need to
  886. kill these buffers yourself."
  887. :group 'org-structure
  888. :group 'org-agenda-windows
  889. :type '(choice
  890. (const :tag "In current window" current-window)
  891. (const :tag "In current frame, other window" other-window)
  892. (const :tag "Each time a new frame" new-frame)
  893. (const :tag "One dedicated frame" dedicated-frame)))
  894. (defcustom org-use-speed-commands nil
  895. "Non-nil means activate single letter commands at beginning of a headline.
  896. This may also be a function to test for appropriate locations where speed
  897. commands should be active."
  898. :group 'org-structure
  899. :type '(choice
  900. (const :tag "Never" nil)
  901. (const :tag "At beginning of headline stars" t)
  902. (function)))
  903. (defcustom org-speed-commands-user nil
  904. "Alist of additional speed commands.
  905. This list will be checked before `org-speed-commands-default'
  906. when the variable `org-use-speed-commands' is non-nil
  907. and when the cursor is at the beginning of a headline.
  908. The car if each entry is a string with a single letter, which must
  909. be assigned to `self-insert-command' in the global map.
  910. The cdr is either a command to be called interactively, a function
  911. to be called, or a form to be evaluated.
  912. An entry that is just a list with a single string will be interpreted
  913. as a descriptive headline that will be added when listing the speed
  914. commands in the Help buffer using the `?' speed command."
  915. :group 'org-structure
  916. :type '(repeat :value ("k" . ignore)
  917. (choice :value ("k" . ignore)
  918. (list :tag "Descriptive Headline" (string :tag "Headline"))
  919. (cons :tag "Letter and Command"
  920. (string :tag "Command letter")
  921. (choice
  922. (function)
  923. (sexp))))))
  924. (defcustom org-bookmark-names-plist
  925. '(:last-capture "org-capture-last-stored"
  926. :last-refile "org-refile-last-stored"
  927. :last-capture-marker "org-capture-last-stored-marker")
  928. "Names for bookmarks automatically set by some Org commands.
  929. This can provide strings as names for a number of bookmakrs Org sets
  930. automatically. The following keys are currently implemented:
  931. :last-capture
  932. :last-capture-marker
  933. :last-refile
  934. When a key does not show up in the property list, the corresponding bookmark
  935. is not set."
  936. :group 'org-structure
  937. :type 'plist)
  938. (defgroup org-cycle nil
  939. "Options concerning visibility cycling in Org-mode."
  940. :tag "Org Cycle"
  941. :group 'org-structure)
  942. (defcustom org-cycle-skip-children-state-if-no-children t
  943. "Non-nil means skip CHILDREN state in entries that don't have any."
  944. :group 'org-cycle
  945. :type 'boolean)
  946. (defcustom org-cycle-max-level nil
  947. "Maximum level which should still be subject to visibility cycling.
  948. Levels higher than this will, for cycling, be treated as text, not a headline.
  949. When `org-odd-levels-only' is set, a value of N in this variable actually
  950. means 2N-1 stars as the limiting headline.
  951. When nil, cycle all levels.
  952. Note that the limiting level of cycling is also influenced by
  953. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  954. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  955. than its value."
  956. :group 'org-cycle
  957. :type '(choice
  958. (const :tag "No limit" nil)
  959. (integer :tag "Maximum level")))
  960. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  961. "Names of drawers. Drawers are not opened by cycling on the headline above.
  962. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  963. this:
  964. :DRAWERNAME:
  965. .....
  966. :END:
  967. The drawer \"PROPERTIES\" is special for capturing properties through
  968. the property API.
  969. Drawers can be defined on the per-file basis with a line like:
  970. #+DRAWERS: HIDDEN STATE PROPERTIES"
  971. :group 'org-structure
  972. :group 'org-cycle
  973. :type '(repeat (string :tag "Drawer Name")))
  974. (defcustom org-hide-block-startup nil
  975. "Non-nil means entering Org-mode will fold all blocks.
  976. This can also be set in on a per-file basis with
  977. #+STARTUP: hideblocks
  978. #+STARTUP: showblocks"
  979. :group 'org-startup
  980. :group 'org-cycle
  981. :type 'boolean)
  982. (defcustom org-cycle-global-at-bob nil
  983. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  984. This makes it possible to do global cycling without having to use S-TAB or
  985. \\[universal-argument] TAB. For this special case to work, the first line
  986. of the buffer must not be a headline -- it may be empty or some other text.
  987. When used in this way, `org-cycle-hook' is disabled temporarily to make
  988. sure the cursor stays at the beginning of the buffer. When this option is
  989. nil, don't do anything special at the beginning of the buffer."
  990. :group 'org-cycle
  991. :type 'boolean)
  992. (defcustom org-cycle-level-after-item/entry-creation t
  993. "Non-nil means cycle entry level or item indentation in new empty entries.
  994. When the cursor is at the end of an empty headline, i.e., with only stars
  995. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  996. and then all possible ancestor states, before returning to the original state.
  997. This makes data entry extremely fast: M-RET to create a new headline,
  998. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  999. When the cursor is at the end of an empty plain list item, one TAB will
  1000. make it a subitem, two or more tabs will back up to make this an item
  1001. higher up in the item hierarchy."
  1002. :group 'org-cycle
  1003. :type 'boolean)
  1004. (defcustom org-cycle-emulate-tab t
  1005. "Where should `org-cycle' emulate TAB.
  1006. nil Never
  1007. white Only in completely white lines
  1008. whitestart Only at the beginning of lines, before the first non-white char
  1009. t Everywhere except in headlines
  1010. exc-hl-bol Everywhere except at the start of a headline
  1011. If TAB is used in a place where it does not emulate TAB, the current subtree
  1012. visibility is cycled."
  1013. :group 'org-cycle
  1014. :type '(choice (const :tag "Never" nil)
  1015. (const :tag "Only in completely white lines" white)
  1016. (const :tag "Before first char in a line" whitestart)
  1017. (const :tag "Everywhere except in headlines" t)
  1018. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1019. (defcustom org-cycle-separator-lines 2
  1020. "Number of empty lines needed to keep an empty line between collapsed trees.
  1021. If you leave an empty line between the end of a subtree and the following
  1022. headline, this empty line is hidden when the subtree is folded.
  1023. Org-mode will leave (exactly) one empty line visible if the number of
  1024. empty lines is equal or larger to the number given in this variable.
  1025. So the default 2 means at least 2 empty lines after the end of a subtree
  1026. are needed to produce free space between a collapsed subtree and the
  1027. following headline.
  1028. If the number is negative, and the number of empty lines is at least -N,
  1029. all empty lines are shown.
  1030. Special case: when 0, never leave empty lines in collapsed view."
  1031. :group 'org-cycle
  1032. :type 'integer)
  1033. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1034. (defcustom org-pre-cycle-hook nil
  1035. "Hook that is run before visibility cycling is happening.
  1036. The function(s) in this hook must accept a single argument which indicates
  1037. the new state that will be set right after running this hook. The
  1038. argument is a symbol. Before a global state change, it can have the values
  1039. `overview', `content', or `all'. Before a local state change, it can have
  1040. the values `folded', `children', or `subtree'."
  1041. :group 'org-cycle
  1042. :type 'hook)
  1043. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1044. org-cycle-hide-drawers
  1045. org-cycle-hide-inline-tasks
  1046. org-cycle-show-empty-lines
  1047. org-optimize-window-after-visibility-change)
  1048. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1049. The function(s) in this hook must accept a single argument which indicates
  1050. the new state that was set by the most recent `org-cycle' command. The
  1051. argument is a symbol. After a global state change, it can have the values
  1052. `overview', `contents', or `all'. After a local state change, it can have
  1053. the values `folded', `children', or `subtree'."
  1054. :group 'org-cycle
  1055. :type 'hook)
  1056. (defgroup org-edit-structure nil
  1057. "Options concerning structure editing in Org-mode."
  1058. :tag "Org Edit Structure"
  1059. :group 'org-structure)
  1060. (defcustom org-odd-levels-only nil
  1061. "Non-nil means skip even levels and only use odd levels for the outline.
  1062. This has the effect that two stars are being added/taken away in
  1063. promotion/demotion commands. It also influences how levels are
  1064. handled by the exporters.
  1065. Changing it requires restart of `font-lock-mode' to become effective
  1066. for fontification also in regions already fontified.
  1067. You may also set this on a per-file basis by adding one of the following
  1068. lines to the buffer:
  1069. #+STARTUP: odd
  1070. #+STARTUP: oddeven"
  1071. :group 'org-edit-structure
  1072. :group 'org-appearance
  1073. :type 'boolean)
  1074. (defcustom org-adapt-indentation t
  1075. "Non-nil means adapt indentation to outline node level.
  1076. When this variable is set, Org assumes that you write outlines by
  1077. indenting text in each node to align with the headline (after the stars).
  1078. The following issues are influenced by this variable:
  1079. - When this is set and the *entire* text in an entry is indented, the
  1080. indentation is increased by one space in a demotion command, and
  1081. decreased by one in a promotion command. If any line in the entry
  1082. body starts with text at column 0, indentation is not changed at all.
  1083. - Property drawers and planning information is inserted indented when
  1084. this variable s set. When nil, they will not be indented.
  1085. - TAB indents a line relative to context. The lines below a headline
  1086. will be indented when this variable is set.
  1087. Note that this is all about true indentation, by adding and removing
  1088. space characters. See also `org-indent.el' which does level-dependent
  1089. indentation in a virtual way, i.e. at display time in Emacs."
  1090. :group 'org-edit-structure
  1091. :type 'boolean)
  1092. (defcustom org-special-ctrl-a/e nil
  1093. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1094. When t, `C-a' will bring back the cursor to the beginning of the
  1095. headline text, i.e. after the stars and after a possible TODO
  1096. keyword. In an item, this will be the position after bullet and
  1097. check-box, if any. When the cursor is already at that position,
  1098. another `C-a' will bring it to the beginning of the line.
  1099. `C-e' will jump to the end of the headline, ignoring the presence
  1100. of tags in the headline. A second `C-e' will then jump to the
  1101. true end of the line, after any tags. This also means that, when
  1102. this variable is non-nil, `C-e' also will never jump beyond the
  1103. end of the heading of a folded section, i.e. not after the
  1104. ellipses.
  1105. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1106. normally, going to the true line boundary first. Only a directly
  1107. following, identical keypress will bring the cursor to the
  1108. special positions.
  1109. This may also be a cons cell where the behavior for `C-a' and
  1110. `C-e' is set separately."
  1111. :group 'org-edit-structure
  1112. :type '(choice
  1113. (const :tag "off" nil)
  1114. (const :tag "on: after stars/bullet and before tags first" t)
  1115. (const :tag "reversed: true line boundary first" reversed)
  1116. (cons :tag "Set C-a and C-e separately"
  1117. (choice :tag "Special C-a"
  1118. (const :tag "off" nil)
  1119. (const :tag "on: after stars/bullet first" t)
  1120. (const :tag "reversed: before stars/bullet first" reversed))
  1121. (choice :tag "Special C-e"
  1122. (const :tag "off" nil)
  1123. (const :tag "on: before tags first" t)
  1124. (const :tag "reversed: after tags first" reversed)))))
  1125. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1126. (defcustom org-special-ctrl-k nil
  1127. "Non-nil means `C-k' will behave specially in headlines.
  1128. When nil, `C-k' will call the default `kill-line' command.
  1129. When t, the following will happen while the cursor is in the headline:
  1130. - When the cursor is at the beginning of a headline, kill the entire
  1131. line and possible the folded subtree below the line.
  1132. - When in the middle of the headline text, kill the headline up to the tags.
  1133. - When after the headline text, kill the tags."
  1134. :group 'org-edit-structure
  1135. :type 'boolean)
  1136. (defcustom org-ctrl-k-protect-subtree nil
  1137. "Non-nil means, do not delete a hidden subtree with C-k.
  1138. When set to the symbol `error', simply throw an error when C-k is
  1139. used to kill (part-of) a headline that has hidden text behind it.
  1140. Any other non-nil value will result in a query to the user, if it is
  1141. OK to kill that hidden subtree. When nil, kill without remorse."
  1142. :group 'org-edit-structure
  1143. :version "24.1"
  1144. :type '(choice
  1145. (const :tag "Do not protect hidden subtrees" nil)
  1146. (const :tag "Protect hidden subtrees with a security query" t)
  1147. (const :tag "Never kill a hidden subtree with C-k" error)))
  1148. (defcustom org-catch-invisible-edits nil
  1149. "Check if in invisible region before inserting or deleting a character.
  1150. Valid values are:
  1151. nil Do not check, so just do invisible edits.
  1152. error Throw an error and do nothing.
  1153. show Make point visible, and do the requested edit.
  1154. show-and-error Make point visible, then throw an error and abort the edit.
  1155. smart Make point visible, and do insertion/deletion if it is
  1156. adjacent to visible text and the change feels predictable.
  1157. Never delete a previously invisible character or add in the
  1158. middle or right after an invisible region. Basically, this
  1159. allows insertion and backward-delete right before ellipses.
  1160. FIXME: maybe in this case we should not even show?"
  1161. :group 'org-edit-structure
  1162. :version "24.1"
  1163. :type '(choice
  1164. (const :tag "Do not check" nil)
  1165. (const :tag "Throw error when trying to edit" error)
  1166. (const :tag "Unhide, but do not do the edit" show-and-error)
  1167. (const :tag "Show invisible part and do the edit" show)
  1168. (const :tag "Be smart and do the right thing" smart)))
  1169. (defcustom org-yank-folded-subtrees t
  1170. "Non-nil means when yanking subtrees, fold them.
  1171. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1172. it starts with a heading and all other headings in it are either children
  1173. or siblings, then fold all the subtrees. However, do this only if no
  1174. text after the yank would be swallowed into a folded tree by this action."
  1175. :group 'org-edit-structure
  1176. :type 'boolean)
  1177. (defcustom org-yank-adjusted-subtrees nil
  1178. "Non-nil means when yanking subtrees, adjust the level.
  1179. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1180. this function for details."
  1181. :group 'org-edit-structure
  1182. :type 'boolean)
  1183. (defcustom org-M-RET-may-split-line '((default . t))
  1184. "Non-nil means M-RET will split the line at the cursor position.
  1185. When nil, it will go to the end of the line before making a
  1186. new line.
  1187. You may also set this option in a different way for different
  1188. contexts. Valid contexts are:
  1189. headline when creating a new headline
  1190. item when creating a new item
  1191. table in a table field
  1192. default the value to be used for all contexts not explicitly
  1193. customized"
  1194. :group 'org-structure
  1195. :group 'org-table
  1196. :type '(choice
  1197. (const :tag "Always" t)
  1198. (const :tag "Never" nil)
  1199. (repeat :greedy t :tag "Individual contexts"
  1200. (cons
  1201. (choice :tag "Context"
  1202. (const headline)
  1203. (const item)
  1204. (const table)
  1205. (const default))
  1206. (boolean)))))
  1207. (defcustom org-insert-heading-respect-content nil
  1208. "Non-nil means insert new headings after the current subtree.
  1209. When nil, the new heading is created directly after the current line.
  1210. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1211. this variable on for the duration of the command."
  1212. :group 'org-structure
  1213. :type 'boolean)
  1214. (defcustom org-blank-before-new-entry '((heading . auto)
  1215. (plain-list-item . auto))
  1216. "Should `org-insert-heading' leave a blank line before new heading/item?
  1217. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1218. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1219. which case Org will look at the surrounding headings/items and try to
  1220. make an intelligent decision whether to insert a blank line or not.
  1221. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1222. the setting here is ignored and no empty line is inserted to avoid breaking
  1223. the list structure."
  1224. :group 'org-edit-structure
  1225. :type '(list
  1226. (cons (const heading)
  1227. (choice (const :tag "Never" nil)
  1228. (const :tag "Always" t)
  1229. (const :tag "Auto" auto)))
  1230. (cons (const plain-list-item)
  1231. (choice (const :tag "Never" nil)
  1232. (const :tag "Always" t)
  1233. (const :tag "Auto" auto)))))
  1234. (defcustom org-insert-heading-hook nil
  1235. "Hook being run after inserting a new heading."
  1236. :group 'org-edit-structure
  1237. :type 'hook)
  1238. (defcustom org-enable-fixed-width-editor t
  1239. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1240. This currently only means they are never auto-wrapped.
  1241. When nil, such lines will be treated like ordinary lines.
  1242. See also the QUOTE keyword."
  1243. :group 'org-edit-structure
  1244. :type 'boolean)
  1245. (defcustom org-goto-auto-isearch t
  1246. "Non-nil means typing characters in `org-goto' starts incremental search.
  1247. When nil, you can use these keybindings to navigate the buffer:
  1248. q Quit the org-goto interface
  1249. n Go to the next visible heading
  1250. p Go to the previous visible heading
  1251. f Go one heading forward on same level
  1252. b Go one heading backward on same level
  1253. u Go one heading up"
  1254. :group 'org-edit-structure
  1255. :type 'boolean)
  1256. (defgroup org-sparse-trees nil
  1257. "Options concerning sparse trees in Org-mode."
  1258. :tag "Org Sparse Trees"
  1259. :group 'org-structure)
  1260. (defcustom org-highlight-sparse-tree-matches t
  1261. "Non-nil means highlight all matches that define a sparse tree.
  1262. The highlights will automatically disappear the next time the buffer is
  1263. changed by an edit command."
  1264. :group 'org-sparse-trees
  1265. :type 'boolean)
  1266. (defcustom org-remove-highlights-with-change t
  1267. "Non-nil means any change to the buffer will remove temporary highlights.
  1268. Such highlights are created by `org-occur' and `org-clock-display'.
  1269. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1270. The highlights created by `org-preview-latex-fragment' always need
  1271. `C-c C-c' to be removed."
  1272. :group 'org-sparse-trees
  1273. :group 'org-time
  1274. :type 'boolean)
  1275. (defcustom org-occur-hook '(org-first-headline-recenter)
  1276. "Hook that is run after `org-occur' has constructed a sparse tree.
  1277. This can be used to recenter the window to show as much of the structure
  1278. as possible."
  1279. :group 'org-sparse-trees
  1280. :type 'hook)
  1281. (defgroup org-imenu-and-speedbar nil
  1282. "Options concerning imenu and speedbar in Org-mode."
  1283. :tag "Org Imenu and Speedbar"
  1284. :group 'org-structure)
  1285. (defcustom org-imenu-depth 2
  1286. "The maximum level for Imenu access to Org-mode headlines.
  1287. This also applied for speedbar access."
  1288. :group 'org-imenu-and-speedbar
  1289. :type 'integer)
  1290. (defgroup org-table nil
  1291. "Options concerning tables in Org-mode."
  1292. :tag "Org Table"
  1293. :group 'org)
  1294. (defcustom org-enable-table-editor 'optimized
  1295. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1296. When nil, such lines will be treated like ordinary lines.
  1297. When equal to the symbol `optimized', the table editor will be optimized to
  1298. do the following:
  1299. - Automatic overwrite mode in front of whitespace in table fields.
  1300. This makes the structure of the table stay in tact as long as the edited
  1301. field does not exceed the column width.
  1302. - Minimize the number of realigns. Normally, the table is aligned each time
  1303. TAB or RET are pressed to move to another field. With optimization this
  1304. happens only if changes to a field might have changed the column width.
  1305. Optimization requires replacing the functions `self-insert-command',
  1306. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1307. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1308. very good at guessing when a re-align will be necessary, but you can always
  1309. force one with \\[org-ctrl-c-ctrl-c].
  1310. If you would like to use the optimized version in Org-mode, but the
  1311. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1312. This variable can be used to turn on and off the table editor during a session,
  1313. but in order to toggle optimization, a restart is required.
  1314. See also the variable `org-table-auto-blank-field'."
  1315. :group 'org-table
  1316. :type '(choice
  1317. (const :tag "off" nil)
  1318. (const :tag "on" t)
  1319. (const :tag "on, optimized" optimized)))
  1320. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1321. (version<= emacs-version "24.1"))
  1322. "Non-nil means cluster self-insert commands for undo when possible.
  1323. If this is set, then, like in the Emacs command loop, 20 consecutive
  1324. characters will be undone together.
  1325. This is configurable, because there is some impact on typing performance."
  1326. :group 'org-table
  1327. :type 'boolean)
  1328. (defcustom org-table-tab-recognizes-table.el t
  1329. "Non-nil means TAB will automatically notice a table.el table.
  1330. When it sees such a table, it moves point into it and - if necessary -
  1331. calls `table-recognize-table'."
  1332. :group 'org-table-editing
  1333. :type 'boolean)
  1334. (defgroup org-link nil
  1335. "Options concerning links in Org-mode."
  1336. :tag "Org Link"
  1337. :group 'org)
  1338. (defvar org-link-abbrev-alist-local nil
  1339. "Buffer-local version of `org-link-abbrev-alist', which see.
  1340. The value of this is taken from the #+LINK lines.")
  1341. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1342. (defcustom org-link-abbrev-alist nil
  1343. "Alist of link abbreviations.
  1344. The car of each element is a string, to be replaced at the start of a link.
  1345. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1346. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1347. [[linkkey:tag][description]]
  1348. The 'linkkey' must be a word word, starting with a letter, followed
  1349. by letters, numbers, '-' or '_'.
  1350. If REPLACE is a string, the tag will simply be appended to create the link.
  1351. If the string contains \"%s\", the tag will be inserted there. If the string
  1352. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1353. at that point (see the function `url-hexify-string'). If the string contains
  1354. the specifier \"%(my-function)\", then the custom function `my-function' will
  1355. be invoked: this function takes the tag as its only argument and must return
  1356. a string.
  1357. REPLACE may also be a function that will be called with the tag as the
  1358. only argument to create the link, which should be returned as a string.
  1359. See the manual for examples."
  1360. :group 'org-link
  1361. :type '(repeat
  1362. (cons
  1363. (string :tag "Protocol")
  1364. (choice
  1365. (string :tag "Format")
  1366. (function)))))
  1367. (defcustom org-descriptive-links t
  1368. "Non-nil means Org will display descriptive links.
  1369. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1370. \"Org Website\", hiding the link itself and just displaying its
  1371. description. When set to `nil', Org will display the full links
  1372. literally.
  1373. You can interactively set the value of this variable by calling
  1374. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1375. :group 'org-link
  1376. :type 'boolean)
  1377. (defcustom org-link-file-path-type 'adaptive
  1378. "How the path name in file links should be stored.
  1379. Valid values are:
  1380. relative Relative to the current directory, i.e. the directory of the file
  1381. into which the link is being inserted.
  1382. absolute Absolute path, if possible with ~ for home directory.
  1383. noabbrev Absolute path, no abbreviation of home directory.
  1384. adaptive Use relative path for files in the current directory and sub-
  1385. directories of it. For other files, use an absolute path."
  1386. :group 'org-link
  1387. :type '(choice
  1388. (const relative)
  1389. (const absolute)
  1390. (const noabbrev)
  1391. (const adaptive)))
  1392. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1393. "Types of links that should be activated in Org-mode files.
  1394. This is a list of symbols, each leading to the activation of a certain link
  1395. type. In principle, it does not hurt to turn on most link types - there may
  1396. be a small gain when turning off unused link types. The types are:
  1397. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1398. angle Links in angular brackets that may contain whitespace like
  1399. <bbdb:Carsten Dominik>.
  1400. plain Plain links in normal text, no whitespace, like http://google.com.
  1401. radio Text that is matched by a radio target, see manual for details.
  1402. tag Tag settings in a headline (link to tag search).
  1403. date Time stamps (link to calendar).
  1404. footnote Footnote labels.
  1405. Changing this variable requires a restart of Emacs to become effective."
  1406. :group 'org-link
  1407. :type '(set :greedy t
  1408. (const :tag "Double bracket links" bracket)
  1409. (const :tag "Angular bracket links" angle)
  1410. (const :tag "Plain text links" plain)
  1411. (const :tag "Radio target matches" radio)
  1412. (const :tag "Tags" tag)
  1413. (const :tag "Timestamps" date)
  1414. (const :tag "Footnotes" footnote)))
  1415. (defcustom org-make-link-description-function nil
  1416. "Function to use for generating link descriptions from links.
  1417. When nil, the link location will be used. This function must take
  1418. two parameters: the first one is the link, the second one is the
  1419. description generated by `org-insert-link'. The function should
  1420. return the description to use."
  1421. :group 'org-link
  1422. :type '(choice (const nil) (function)))
  1423. (defgroup org-link-store nil
  1424. "Options concerning storing links in Org-mode."
  1425. :tag "Org Store Link"
  1426. :group 'org-link)
  1427. (defcustom org-url-hexify-p t
  1428. "When non-nil, hexify URL when creating a link."
  1429. :type 'boolean
  1430. :version "24.3"
  1431. :group 'org-link-store)
  1432. (defcustom org-email-link-description-format "Email %c: %.30s"
  1433. "Format of the description part of a link to an email or usenet message.
  1434. The following %-escapes will be replaced by corresponding information:
  1435. %F full \"From\" field
  1436. %f name, taken from \"From\" field, address if no name
  1437. %T full \"To\" field
  1438. %t first name in \"To\" field, address if no name
  1439. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1440. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1441. %s subject
  1442. %d date
  1443. %m message-id.
  1444. You may use normal field width specification between the % and the letter.
  1445. This is for example useful to limit the length of the subject.
  1446. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1447. :group 'org-link-store
  1448. :type 'string)
  1449. (defcustom org-from-is-user-regexp
  1450. (let (r1 r2)
  1451. (when (and user-mail-address (not (string= user-mail-address "")))
  1452. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1453. (when (and user-full-name (not (string= user-full-name "")))
  1454. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1455. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1456. "Regexp matched against the \"From:\" header of an email or usenet message.
  1457. It should match if the message is from the user him/herself."
  1458. :group 'org-link-store
  1459. :type 'regexp)
  1460. (defcustom org-context-in-file-links t
  1461. "Non-nil means file links from `org-store-link' contain context.
  1462. A search string will be added to the file name with :: as separator and
  1463. used to find the context when the link is activated by the command
  1464. `org-open-at-point'. When this option is t, the entire active region
  1465. will be placed in the search string of the file link. If set to a
  1466. positive integer, only the first n lines of context will be stored.
  1467. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1468. negates this setting for the duration of the command."
  1469. :group 'org-link-store
  1470. :type '(choice boolean integer))
  1471. (defcustom org-keep-stored-link-after-insertion nil
  1472. "Non-nil means keep link in list for entire session.
  1473. The command `org-store-link' adds a link pointing to the current
  1474. location to an internal list. These links accumulate during a session.
  1475. The command `org-insert-link' can be used to insert links into any
  1476. Org-mode file (offering completion for all stored links). When this
  1477. option is nil, every link which has been inserted once using \\[org-insert-link]
  1478. will be removed from the list, to make completing the unused links
  1479. more efficient."
  1480. :group 'org-link-store
  1481. :type 'boolean)
  1482. (defgroup org-link-follow nil
  1483. "Options concerning following links in Org-mode."
  1484. :tag "Org Follow Link"
  1485. :group 'org-link)
  1486. (defcustom org-link-translation-function nil
  1487. "Function to translate links with different syntax to Org syntax.
  1488. This can be used to translate links created for example by the Planner
  1489. or emacs-wiki packages to Org syntax.
  1490. The function must accept two parameters, a TYPE containing the link
  1491. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1492. which is everything after the link protocol. It should return a cons
  1493. with possibly modified values of type and path.
  1494. Org contains a function for this, so if you set this variable to
  1495. `org-translate-link-from-planner', you should be able follow many
  1496. links created by planner."
  1497. :group 'org-link-follow
  1498. :type '(choice (const nil) (function)))
  1499. (defcustom org-follow-link-hook nil
  1500. "Hook that is run after a link has been followed."
  1501. :group 'org-link-follow
  1502. :type 'hook)
  1503. (defcustom org-tab-follows-link nil
  1504. "Non-nil means on links TAB will follow the link.
  1505. Needs to be set before org.el is loaded.
  1506. This really should not be used, it does not make sense, and the
  1507. implementation is bad."
  1508. :group 'org-link-follow
  1509. :type 'boolean)
  1510. (defcustom org-return-follows-link nil
  1511. "Non-nil means on links RET will follow the link.
  1512. In tables, the special behavior of RET has precedence."
  1513. :group 'org-link-follow
  1514. :type 'boolean)
  1515. (defcustom org-mouse-1-follows-link
  1516. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1517. "Non-nil means mouse-1 on a link will follow the link.
  1518. A longer mouse click will still set point. Does not work on XEmacs.
  1519. Needs to be set before org.el is loaded."
  1520. :group 'org-link-follow
  1521. :type 'boolean)
  1522. (defcustom org-mark-ring-length 4
  1523. "Number of different positions to be recorded in the ring.
  1524. Changing this requires a restart of Emacs to work correctly."
  1525. :group 'org-link-follow
  1526. :type 'integer)
  1527. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1528. "Non-nil means internal links in Org files must exactly match a headline.
  1529. When nil, the link search tries to match a phrase with all words
  1530. in the search text."
  1531. :group 'org-link-follow
  1532. :version "24.1"
  1533. :type '(choice
  1534. (const :tag "Use fuzzy text search" nil)
  1535. (const :tag "Match only exact headline" t)
  1536. (const :tag "Match exact headline or query to create it"
  1537. query-to-create)))
  1538. (defcustom org-link-frame-setup
  1539. '((vm . vm-visit-folder-other-frame)
  1540. (vm-imap . vm-visit-imap-folder-other-frame)
  1541. (gnus . org-gnus-no-new-news)
  1542. (file . find-file-other-window)
  1543. (wl . wl-other-frame))
  1544. "Setup the frame configuration for following links.
  1545. When following a link with Emacs, it may often be useful to display
  1546. this link in another window or frame. This variable can be used to
  1547. set this up for the different types of links.
  1548. For VM, use any of
  1549. `vm-visit-folder'
  1550. `vm-visit-folder-other-window'
  1551. `vm-visit-folder-other-frame'
  1552. For Gnus, use any of
  1553. `gnus'
  1554. `gnus-other-frame'
  1555. `org-gnus-no-new-news'
  1556. For FILE, use any of
  1557. `find-file'
  1558. `find-file-other-window'
  1559. `find-file-other-frame'
  1560. For Wanderlust use any of
  1561. `wl'
  1562. `wl-other-frame'
  1563. For the calendar, use the variable `calendar-setup'.
  1564. For BBDB, it is currently only possible to display the matches in
  1565. another window."
  1566. :group 'org-link-follow
  1567. :type '(list
  1568. (cons (const vm)
  1569. (choice
  1570. (const vm-visit-folder)
  1571. (const vm-visit-folder-other-window)
  1572. (const vm-visit-folder-other-frame)))
  1573. (cons (const vm-imap)
  1574. (choice
  1575. (const vm-visit-imap-folder)
  1576. (const vm-visit-imap-folder-other-window)
  1577. (const vm-visit-imap-folder-other-frame)))
  1578. (cons (const gnus)
  1579. (choice
  1580. (const gnus)
  1581. (const gnus-other-frame)
  1582. (const org-gnus-no-new-news)))
  1583. (cons (const file)
  1584. (choice
  1585. (const find-file)
  1586. (const find-file-other-window)
  1587. (const find-file-other-frame)))
  1588. (cons (const wl)
  1589. (choice
  1590. (const wl)
  1591. (const wl-other-frame)))))
  1592. (defcustom org-display-internal-link-with-indirect-buffer nil
  1593. "Non-nil means use indirect buffer to display infile links.
  1594. Activating internal links (from one location in a file to another location
  1595. in the same file) normally just jumps to the location. When the link is
  1596. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1597. is displayed in
  1598. another window. When this option is set, the other window actually displays
  1599. an indirect buffer clone of the current buffer, to avoid any visibility
  1600. changes to the current buffer."
  1601. :group 'org-link-follow
  1602. :type 'boolean)
  1603. (defcustom org-open-non-existing-files nil
  1604. "Non-nil means `org-open-file' will open non-existing files.
  1605. When nil, an error will be generated.
  1606. This variable applies only to external applications because they
  1607. might choke on non-existing files. If the link is to a file that
  1608. will be opened in Emacs, the variable is ignored."
  1609. :group 'org-link-follow
  1610. :type 'boolean)
  1611. (defcustom org-open-directory-means-index-dot-org nil
  1612. "Non-nil means a link to a directory really means to index.org.
  1613. When nil, following a directory link will run dired or open a finder/explorer
  1614. window on that directory."
  1615. :group 'org-link-follow
  1616. :type 'boolean)
  1617. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1618. "Function and arguments to call for following mailto links.
  1619. This is a list with the first element being a Lisp function, and the
  1620. remaining elements being arguments to the function. In string arguments,
  1621. %a will be replaced by the address, and %s will be replaced by the subject
  1622. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1623. :group 'org-link-follow
  1624. :type '(choice
  1625. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1626. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1627. (const :tag "message-mail" (message-mail "%a" "%s"))
  1628. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1629. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1630. "Non-nil means ask for confirmation before executing shell links.
  1631. Shell links can be dangerous: just think about a link
  1632. [[shell:rm -rf ~/*][Google Search]]
  1633. This link would show up in your Org-mode document as \"Google Search\",
  1634. but really it would remove your entire home directory.
  1635. Therefore we advise against setting this variable to nil.
  1636. Just change it to `y-or-n-p' if you want to confirm with a
  1637. single keystroke rather than having to type \"yes\"."
  1638. :group 'org-link-follow
  1639. :type '(choice
  1640. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1641. (const :tag "with y-or-n (faster)" y-or-n-p)
  1642. (const :tag "no confirmation (dangerous)" nil)))
  1643. (put 'org-confirm-shell-link-function
  1644. 'safe-local-variable
  1645. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1646. (defcustom org-confirm-shell-link-not-regexp ""
  1647. "A regexp to skip confirmation for shell links."
  1648. :group 'org-link-follow
  1649. :version "24.1"
  1650. :type 'regexp)
  1651. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1652. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1653. Elisp links can be dangerous: just think about a link
  1654. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1655. This link would show up in your Org-mode document as \"Google Search\",
  1656. but really it would remove your entire home directory.
  1657. Therefore we advise against setting this variable to nil.
  1658. Just change it to `y-or-n-p' if you want to confirm with a
  1659. single keystroke rather than having to type \"yes\"."
  1660. :group 'org-link-follow
  1661. :type '(choice
  1662. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1663. (const :tag "with y-or-n (faster)" y-or-n-p)
  1664. (const :tag "no confirmation (dangerous)" nil)))
  1665. (put 'org-confirm-shell-link-function
  1666. 'safe-local-variable
  1667. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1668. (defcustom org-confirm-elisp-link-not-regexp ""
  1669. "A regexp to skip confirmation for Elisp links."
  1670. :group 'org-link-follow
  1671. :version "24.1"
  1672. :type 'regexp)
  1673. (defconst org-file-apps-defaults-gnu
  1674. '((remote . emacs)
  1675. (system . mailcap)
  1676. (t . mailcap))
  1677. "Default file applications on a UNIX or GNU/Linux system.
  1678. See `org-file-apps'.")
  1679. (defconst org-file-apps-defaults-macosx
  1680. '((remote . emacs)
  1681. (t . "open %s")
  1682. (system . "open %s")
  1683. ("ps.gz" . "gv %s")
  1684. ("eps.gz" . "gv %s")
  1685. ("dvi" . "xdvi %s")
  1686. ("fig" . "xfig %s"))
  1687. "Default file applications on a MacOS X system.
  1688. The system \"open\" is known as a default, but we use X11 applications
  1689. for some files for which the OS does not have a good default.
  1690. See `org-file-apps'.")
  1691. (defconst org-file-apps-defaults-windowsnt
  1692. (list
  1693. '(remote . emacs)
  1694. (cons t
  1695. (list (if (featurep 'xemacs)
  1696. 'mswindows-shell-execute
  1697. 'w32-shell-execute)
  1698. "open" 'file))
  1699. (cons 'system
  1700. (list (if (featurep 'xemacs)
  1701. 'mswindows-shell-execute
  1702. 'w32-shell-execute)
  1703. "open" 'file)))
  1704. "Default file applications on a Windows NT system.
  1705. The system \"open\" is used for most files.
  1706. See `org-file-apps'.")
  1707. (defcustom org-file-apps
  1708. '((auto-mode . emacs)
  1709. ("\\.mm\\'" . default)
  1710. ("\\.x?html?\\'" . default)
  1711. ("\\.pdf\\'" . default))
  1712. "External applications for opening `file:path' items in a document.
  1713. Org-mode uses system defaults for different file types, but
  1714. you can use this variable to set the application for a given file
  1715. extension. The entries in this list are cons cells where the car identifies
  1716. files and the cdr the corresponding command. Possible values for the
  1717. file identifier are
  1718. \"string\" A string as a file identifier can be interpreted in different
  1719. ways, depending on its contents:
  1720. - Alphanumeric characters only:
  1721. Match links with this file extension.
  1722. Example: (\"pdf\" . \"evince %s\")
  1723. to open PDFs with evince.
  1724. - Regular expression: Match links where the
  1725. filename matches the regexp. If you want to
  1726. use groups here, use shy groups.
  1727. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1728. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1729. to open *.html and *.xhtml with firefox.
  1730. - Regular expression which contains (non-shy) groups:
  1731. Match links where the whole link, including \"::\", and
  1732. anything after that, matches the regexp.
  1733. In a custom command string, %1, %2, etc. are replaced with
  1734. the parts of the link that were matched by the groups.
  1735. For backwards compatibility, if a command string is given
  1736. that does not use any of the group matches, this case is
  1737. handled identically to the second one (i.e. match against
  1738. file name only).
  1739. In a custom lisp form, you can access the group matches with
  1740. (match-string n link).
  1741. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1742. to open [[file:document.pdf::5]] with evince at page 5.
  1743. `directory' Matches a directory
  1744. `remote' Matches a remote file, accessible through tramp or efs.
  1745. Remote files most likely should be visited through Emacs
  1746. because external applications cannot handle such paths.
  1747. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1748. so all files Emacs knows how to handle. Using this with
  1749. command `emacs' will open most files in Emacs. Beware that this
  1750. will also open html files inside Emacs, unless you add
  1751. (\"html\" . default) to the list as well.
  1752. t Default for files not matched by any of the other options.
  1753. `system' The system command to open files, like `open' on Windows
  1754. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1755. that will be selected if you call `C-c C-o' with a double
  1756. \\[universal-argument] \\[universal-argument] prefix.
  1757. Possible values for the command are:
  1758. `emacs' The file will be visited by the current Emacs process.
  1759. `default' Use the default application for this file type, which is the
  1760. association for t in the list, most likely in the system-specific
  1761. part.
  1762. This can be used to overrule an unwanted setting in the
  1763. system-specific variable.
  1764. `system' Use the system command for opening files, like \"open\".
  1765. This command is specified by the entry whose car is `system'.
  1766. Most likely, the system-specific version of this variable
  1767. does define this command, but you can overrule/replace it
  1768. here.
  1769. string A command to be executed by a shell; %s will be replaced
  1770. by the path to the file.
  1771. sexp A Lisp form which will be evaluated. The file path will
  1772. be available in the Lisp variable `file'.
  1773. For more examples, see the system specific constants
  1774. `org-file-apps-defaults-macosx'
  1775. `org-file-apps-defaults-windowsnt'
  1776. `org-file-apps-defaults-gnu'."
  1777. :group 'org-link-follow
  1778. :type '(repeat
  1779. (cons (choice :value ""
  1780. (string :tag "Extension")
  1781. (const :tag "System command to open files" system)
  1782. (const :tag "Default for unrecognized files" t)
  1783. (const :tag "Remote file" remote)
  1784. (const :tag "Links to a directory" directory)
  1785. (const :tag "Any files that have Emacs modes"
  1786. auto-mode))
  1787. (choice :value ""
  1788. (const :tag "Visit with Emacs" emacs)
  1789. (const :tag "Use default" default)
  1790. (const :tag "Use the system command" system)
  1791. (string :tag "Command")
  1792. (sexp :tag "Lisp form")))))
  1793. (defcustom org-doi-server-url "http://dx.doi.org/"
  1794. "The URL of the DOI server."
  1795. :type 'string
  1796. :version "24.3"
  1797. :group 'org-link-follow)
  1798. (defgroup org-refile nil
  1799. "Options concerning refiling entries in Org-mode."
  1800. :tag "Org Refile"
  1801. :group 'org)
  1802. (defcustom org-directory "~/org"
  1803. "Directory with org files.
  1804. This is just a default location to look for Org files. There is no need
  1805. at all to put your files into this directory. It is only used in the
  1806. following situations:
  1807. 1. When a capture template specifies a target file that is not an
  1808. absolute path. The path will then be interpreted relative to
  1809. `org-directory'
  1810. 2. When a capture note is filed away in an interactive way (when exiting the
  1811. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1812. with `org-directory' as the default path."
  1813. :group 'org-refile
  1814. :group 'org-remember
  1815. :group 'org-capture
  1816. :type 'directory)
  1817. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1818. "Default target for storing notes.
  1819. Used as a fall back file for org-remember.el and org-capture.el, for
  1820. templates that do not specify a target file."
  1821. :group 'org-refile
  1822. :group 'org-remember
  1823. :group 'org-capture
  1824. :type '(choice
  1825. (const :tag "Default from remember-data-file" nil)
  1826. file))
  1827. (defcustom org-goto-interface 'outline
  1828. "The default interface to be used for `org-goto'.
  1829. Allowed values are:
  1830. outline The interface shows an outline of the relevant file
  1831. and the correct heading is found by moving through
  1832. the outline or by searching with incremental search.
  1833. outline-path-completion Headlines in the current buffer are offered via
  1834. completion. This is the interface also used by
  1835. the refile command."
  1836. :group 'org-refile
  1837. :type '(choice
  1838. (const :tag "Outline" outline)
  1839. (const :tag "Outline-path-completion" outline-path-completion)))
  1840. (defcustom org-goto-max-level 5
  1841. "Maximum target level when running `org-goto' with refile interface."
  1842. :group 'org-refile
  1843. :type 'integer)
  1844. (defcustom org-reverse-note-order nil
  1845. "Non-nil means store new notes at the beginning of a file or entry.
  1846. When nil, new notes will be filed to the end of a file or entry.
  1847. This can also be a list with cons cells of regular expressions that
  1848. are matched against file names, and values."
  1849. :group 'org-remember
  1850. :group 'org-capture
  1851. :group 'org-refile
  1852. :type '(choice
  1853. (const :tag "Reverse always" t)
  1854. (const :tag "Reverse never" nil)
  1855. (repeat :tag "By file name regexp"
  1856. (cons regexp boolean))))
  1857. (defcustom org-log-refile nil
  1858. "Information to record when a task is refiled.
  1859. Possible values are:
  1860. nil Don't add anything
  1861. time Add a time stamp to the task
  1862. note Prompt for a note and add it with template `org-log-note-headings'
  1863. This option can also be set with on a per-file-basis with
  1864. #+STARTUP: nologrefile
  1865. #+STARTUP: logrefile
  1866. #+STARTUP: lognoterefile
  1867. You can have local logging settings for a subtree by setting the LOGGING
  1868. property to one or more of these keywords.
  1869. When bulk-refiling from the agenda, the value `note' is forbidden and
  1870. will temporarily be changed to `time'."
  1871. :group 'org-refile
  1872. :group 'org-progress
  1873. :version "24.1"
  1874. :type '(choice
  1875. (const :tag "No logging" nil)
  1876. (const :tag "Record timestamp" time)
  1877. (const :tag "Record timestamp with note." note)))
  1878. (defcustom org-refile-targets nil
  1879. "Targets for refiling entries with \\[org-refile].
  1880. This is a list of cons cells. Each cell contains:
  1881. - a specification of the files to be considered, either a list of files,
  1882. or a symbol whose function or variable value will be used to retrieve
  1883. a file name or a list of file names. If you use `org-agenda-files' for
  1884. that, all agenda files will be scanned for targets. Nil means consider
  1885. headings in the current buffer.
  1886. - A specification of how to find candidate refile targets. This may be
  1887. any of:
  1888. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1889. This tag has to be present in all target headlines, inheritance will
  1890. not be considered.
  1891. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1892. todo keyword.
  1893. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1894. headlines that are refiling targets.
  1895. - a cons cell (:level . N). Any headline of level N is considered a target.
  1896. Note that, when `org-odd-levels-only' is set, level corresponds to
  1897. order in hierarchy, not to the number of stars.
  1898. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1899. Note that, when `org-odd-levels-only' is set, level corresponds to
  1900. order in hierarchy, not to the number of stars.
  1901. Each element of this list generates a set of possible targets.
  1902. The union of these sets is presented (with completion) to
  1903. the user by `org-refile'.
  1904. You can set the variable `org-refile-target-verify-function' to a function
  1905. to verify each headline found by the simple criteria above.
  1906. When this variable is nil, all top-level headlines in the current buffer
  1907. are used, equivalent to the value `((nil . (:level . 1))'."
  1908. :group 'org-refile
  1909. :type '(repeat
  1910. (cons
  1911. (choice :value org-agenda-files
  1912. (const :tag "All agenda files" org-agenda-files)
  1913. (const :tag "Current buffer" nil)
  1914. (function) (variable) (file))
  1915. (choice :tag "Identify target headline by"
  1916. (cons :tag "Specific tag" (const :value :tag) (string))
  1917. (cons :tag "TODO keyword" (const :value :todo) (string))
  1918. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1919. (cons :tag "Level number" (const :value :level) (integer))
  1920. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1921. (defcustom org-refile-target-verify-function nil
  1922. "Function to verify if the headline at point should be a refile target.
  1923. The function will be called without arguments, with point at the
  1924. beginning of the headline. It should return t and leave point
  1925. where it is if the headline is a valid target for refiling.
  1926. If the target should not be selected, the function must return nil.
  1927. In addition to this, it may move point to a place from where the search
  1928. should be continued. For example, the function may decide that the entire
  1929. subtree of the current entry should be excluded and move point to the end
  1930. of the subtree."
  1931. :group 'org-refile
  1932. :type '(choice
  1933. (const nil)
  1934. (function)))
  1935. (defcustom org-refile-use-cache nil
  1936. "Non-nil means cache refile targets to speed up the process.
  1937. The cache for a particular file will be updated automatically when
  1938. the buffer has been killed, or when any of the marker used for flagging
  1939. refile targets no longer points at a live buffer.
  1940. If you have added new entries to a buffer that might themselves be targets,
  1941. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1942. find that easier, `C-u C-u C-u C-c C-w'."
  1943. :group 'org-refile
  1944. :version "24.1"
  1945. :type 'boolean)
  1946. (defcustom org-refile-use-outline-path nil
  1947. "Non-nil means provide refile targets as paths.
  1948. So a level 3 headline will be available as level1/level2/level3.
  1949. When the value is `file', also include the file name (without directory)
  1950. into the path. In this case, you can also stop the completion after
  1951. the file name, to get entries inserted as top level in the file.
  1952. When `full-file-path', include the full file path."
  1953. :group 'org-refile
  1954. :type '(choice
  1955. (const :tag "Not" nil)
  1956. (const :tag "Yes" t)
  1957. (const :tag "Start with file name" file)
  1958. (const :tag "Start with full file path" full-file-path)))
  1959. (defcustom org-outline-path-complete-in-steps t
  1960. "Non-nil means complete the outline path in hierarchical steps.
  1961. When Org-mode uses the refile interface to select an outline path
  1962. \(see variable `org-refile-use-outline-path'), the completion of
  1963. the path can be done is a single go, or if can be done in steps down
  1964. the headline hierarchy. Going in steps is probably the best if you
  1965. do not use a special completion package like `ido' or `icicles'.
  1966. However, when using these packages, going in one step can be very
  1967. fast, while still showing the whole path to the entry."
  1968. :group 'org-refile
  1969. :type 'boolean)
  1970. (defcustom org-refile-allow-creating-parent-nodes nil
  1971. "Non-nil means allow to create new nodes as refile targets.
  1972. New nodes are then created by adding \"/new node name\" to the completion
  1973. of an existing node. When the value of this variable is `confirm',
  1974. new node creation must be confirmed by the user (recommended)
  1975. When nil, the completion must match an existing entry.
  1976. Note that, if the new heading is not seen by the criteria
  1977. listed in `org-refile-targets', multiple instances of the same
  1978. heading would be created by trying again to file under the new
  1979. heading."
  1980. :group 'org-refile
  1981. :type '(choice
  1982. (const :tag "Never" nil)
  1983. (const :tag "Always" t)
  1984. (const :tag "Prompt for confirmation" confirm)))
  1985. (defcustom org-refile-active-region-within-subtree nil
  1986. "Non-nil means also refile active region within a subtree.
  1987. By default `org-refile' doesn't allow refiling regions if they
  1988. don't contain a set of subtrees, but it might be convenient to
  1989. do so sometimes: in that case, the first line of the region is
  1990. converted to a headline before refiling."
  1991. :group 'org-refile
  1992. :version "24.1"
  1993. :type 'boolean)
  1994. (defgroup org-todo nil
  1995. "Options concerning TODO items in Org-mode."
  1996. :tag "Org TODO"
  1997. :group 'org)
  1998. (defgroup org-progress nil
  1999. "Options concerning Progress logging in Org-mode."
  2000. :tag "Org Progress"
  2001. :group 'org-time)
  2002. (defvar org-todo-interpretation-widgets
  2003. '((:tag "Sequence (cycling hits every state)" sequence)
  2004. (:tag "Type (cycling directly to DONE)" type))
  2005. "The available interpretation symbols for customizing `org-todo-keywords'.
  2006. Interested libraries should add to this list.")
  2007. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2008. "List of TODO entry keyword sequences and their interpretation.
  2009. \\<org-mode-map>This is a list of sequences.
  2010. Each sequence starts with a symbol, either `sequence' or `type',
  2011. indicating if the keywords should be interpreted as a sequence of
  2012. action steps, or as different types of TODO items. The first
  2013. keywords are states requiring action - these states will select a headline
  2014. for inclusion into the global TODO list Org-mode produces. If one of
  2015. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2016. signify that no further action is necessary. If \"|\" is not found,
  2017. the last keyword is treated as the only DONE state of the sequence.
  2018. The command \\[org-todo] cycles an entry through these states, and one
  2019. additional state where no keyword is present. For details about this
  2020. cycling, see the manual.
  2021. TODO keywords and interpretation can also be set on a per-file basis with
  2022. the special #+SEQ_TODO and #+TYP_TODO lines.
  2023. Each keyword can optionally specify a character for fast state selection
  2024. \(in combination with the variable `org-use-fast-todo-selection')
  2025. and specifiers for state change logging, using the same syntax that
  2026. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2027. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2028. indicates to record a time stamp each time this state is selected.
  2029. Each keyword may also specify if a timestamp or a note should be
  2030. recorded when entering or leaving the state, by adding additional
  2031. characters in the parenthesis after the keyword. This looks like this:
  2032. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2033. record only the time of the state change. With X and Y being either
  2034. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2035. Y when leaving the state if and only if the *target* state does not
  2036. define X. You may omit any of the fast-selection key or X or /Y,
  2037. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2038. For backward compatibility, this variable may also be just a list
  2039. of keywords. In this case the interpretation (sequence or type) will be
  2040. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2041. :group 'org-todo
  2042. :group 'org-keywords
  2043. :type '(choice
  2044. (repeat :tag "Old syntax, just keywords"
  2045. (string :tag "Keyword"))
  2046. (repeat :tag "New syntax"
  2047. (cons
  2048. (choice
  2049. :tag "Interpretation"
  2050. ;;Quick and dirty way to see
  2051. ;;`org-todo-interpretations'. This takes the
  2052. ;;place of item arguments
  2053. :convert-widget
  2054. (lambda (widget)
  2055. (widget-put widget
  2056. :args (mapcar
  2057. #'(lambda (x)
  2058. (widget-convert
  2059. (cons 'const x)))
  2060. org-todo-interpretation-widgets))
  2061. widget))
  2062. (repeat
  2063. (string :tag "Keyword"))))))
  2064. (defvar org-todo-keywords-1 nil
  2065. "All TODO and DONE keywords active in a buffer.")
  2066. (make-variable-buffer-local 'org-todo-keywords-1)
  2067. (defvar org-todo-keywords-for-agenda nil)
  2068. (defvar org-done-keywords-for-agenda nil)
  2069. (defvar org-drawers-for-agenda nil)
  2070. (defvar org-todo-keyword-alist-for-agenda nil)
  2071. (defvar org-tag-alist-for-agenda nil
  2072. "Alist of all tags from all agenda files.")
  2073. (defvar org-tag-groups-alist-for-agenda nil
  2074. "Alist of all groups tags from all current agenda files.")
  2075. (defvar org-tag-groups-alist nil)
  2076. (make-variable-buffer-local 'org-tag-groups-alist)
  2077. (defvar org-agenda-contributing-files nil)
  2078. (defvar org-not-done-keywords nil)
  2079. (make-variable-buffer-local 'org-not-done-keywords)
  2080. (defvar org-done-keywords nil)
  2081. (make-variable-buffer-local 'org-done-keywords)
  2082. (defvar org-todo-heads nil)
  2083. (make-variable-buffer-local 'org-todo-heads)
  2084. (defvar org-todo-sets nil)
  2085. (make-variable-buffer-local 'org-todo-sets)
  2086. (defvar org-todo-log-states nil)
  2087. (make-variable-buffer-local 'org-todo-log-states)
  2088. (defvar org-todo-kwd-alist nil)
  2089. (make-variable-buffer-local 'org-todo-kwd-alist)
  2090. (defvar org-todo-key-alist nil)
  2091. (make-variable-buffer-local 'org-todo-key-alist)
  2092. (defvar org-todo-key-trigger nil)
  2093. (make-variable-buffer-local 'org-todo-key-trigger)
  2094. (defcustom org-todo-interpretation 'sequence
  2095. "Controls how TODO keywords are interpreted.
  2096. This variable is in principle obsolete and is only used for
  2097. backward compatibility, if the interpretation of todo keywords is
  2098. not given already in `org-todo-keywords'. See that variable for
  2099. more information."
  2100. :group 'org-todo
  2101. :group 'org-keywords
  2102. :type '(choice (const sequence)
  2103. (const type)))
  2104. (defcustom org-use-fast-todo-selection t
  2105. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2106. This variable describes if and under what circumstances the cycling
  2107. mechanism for TODO keywords will be replaced by a single-key, direct
  2108. selection scheme.
  2109. When nil, fast selection is never used.
  2110. When the symbol `prefix', it will be used when `org-todo' is called
  2111. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2112. `C-u t' in an agenda buffer.
  2113. When t, fast selection is used by default. In this case, the prefix
  2114. argument forces cycling instead.
  2115. In all cases, the special interface is only used if access keys have
  2116. actually been assigned by the user, i.e. if keywords in the configuration
  2117. are followed by a letter in parenthesis, like TODO(t)."
  2118. :group 'org-todo
  2119. :type '(choice
  2120. (const :tag "Never" nil)
  2121. (const :tag "By default" t)
  2122. (const :tag "Only with C-u C-c C-t" prefix)))
  2123. (defcustom org-provide-todo-statistics t
  2124. "Non-nil means update todo statistics after insert and toggle.
  2125. ALL-HEADLINES means update todo statistics by including headlines
  2126. with no TODO keyword as well, counting them as not done.
  2127. A list of TODO keywords means the same, but skip keywords that are
  2128. not in this list.
  2129. When this is set, todo statistics is updated in the parent of the
  2130. current entry each time a todo state is changed."
  2131. :group 'org-todo
  2132. :type '(choice
  2133. (const :tag "Yes, only for TODO entries" t)
  2134. (const :tag "Yes, including all entries" 'all-headlines)
  2135. (repeat :tag "Yes, for TODOs in this list"
  2136. (string :tag "TODO keyword"))
  2137. (other :tag "No TODO statistics" nil)))
  2138. (defcustom org-hierarchical-todo-statistics t
  2139. "Non-nil means TODO statistics covers just direct children.
  2140. When nil, all entries in the subtree are considered.
  2141. This has only an effect if `org-provide-todo-statistics' is set.
  2142. To set this to nil for only a single subtree, use a COOKIE_DATA
  2143. property and include the word \"recursive\" into the value."
  2144. :group 'org-todo
  2145. :type 'boolean)
  2146. (defcustom org-after-todo-state-change-hook nil
  2147. "Hook which is run after the state of a TODO item was changed.
  2148. The new state (a string with a TODO keyword, or nil) is available in the
  2149. Lisp variable `org-state'."
  2150. :group 'org-todo
  2151. :type 'hook)
  2152. (defvar org-blocker-hook nil
  2153. "Hook for functions that are allowed to block a state change.
  2154. Functions in this hook should not modify the buffer.
  2155. Each function gets as its single argument a property list,
  2156. see `org-trigger-hook' for more information about this list.
  2157. If any of the functions in this hook returns nil, the state change
  2158. is blocked.")
  2159. (defvar org-trigger-hook nil
  2160. "Hook for functions that are triggered by a state change.
  2161. Each function gets as its single argument a property list with at
  2162. least the following elements:
  2163. (:type type-of-change :position pos-at-entry-start
  2164. :from old-state :to new-state)
  2165. Depending on the type, more properties may be present.
  2166. This mechanism is currently implemented for:
  2167. TODO state changes
  2168. ------------------
  2169. :type todo-state-change
  2170. :from previous state (keyword as a string), or nil, or a symbol
  2171. 'todo' or 'done', to indicate the general type of state.
  2172. :to new state, like in :from")
  2173. (defcustom org-enforce-todo-dependencies nil
  2174. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2175. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2176. be blocked if any prior sibling is not yet done.
  2177. Finally, if the parent is blocked because of ordered siblings of its own,
  2178. the child will also be blocked."
  2179. :set (lambda (var val)
  2180. (set var val)
  2181. (if val
  2182. (add-hook 'org-blocker-hook
  2183. 'org-block-todo-from-children-or-siblings-or-parent)
  2184. (remove-hook 'org-blocker-hook
  2185. 'org-block-todo-from-children-or-siblings-or-parent)))
  2186. :group 'org-todo
  2187. :type 'boolean)
  2188. (defcustom org-enforce-todo-checkbox-dependencies nil
  2189. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2190. When this is nil, checkboxes have no influence on switching TODO states.
  2191. When non-nil, you first need to check off all check boxes before the TODO
  2192. entry can be switched to DONE.
  2193. This variable needs to be set before org.el is loaded, and you need to
  2194. restart Emacs after a change to make the change effective. The only way
  2195. to change is while Emacs is running is through the customize interface."
  2196. :set (lambda (var val)
  2197. (set var val)
  2198. (if val
  2199. (add-hook 'org-blocker-hook
  2200. 'org-block-todo-from-checkboxes)
  2201. (remove-hook 'org-blocker-hook
  2202. 'org-block-todo-from-checkboxes)))
  2203. :group 'org-todo
  2204. :type 'boolean)
  2205. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2206. "Non-nil means inserting a TODO heading is treated as state change.
  2207. So when the command \\[org-insert-todo-heading] is used, state change
  2208. logging will apply if appropriate. When nil, the new TODO item will
  2209. be inserted directly, and no logging will take place."
  2210. :group 'org-todo
  2211. :type 'boolean)
  2212. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2213. "Non-nil means switching TODO states with S-cursor counts as state change.
  2214. This is the default behavior. However, setting this to nil allows a
  2215. convenient way to select a TODO state and bypass any logging associated
  2216. with that."
  2217. :group 'org-todo
  2218. :type 'boolean)
  2219. (defcustom org-todo-state-tags-triggers nil
  2220. "Tag changes that should be triggered by TODO state changes.
  2221. This is a list. Each entry is
  2222. (state-change (tag . flag) .......)
  2223. State-change can be a string with a state, and empty string to indicate the
  2224. state that has no TODO keyword, or it can be one of the symbols `todo'
  2225. or `done', meaning any not-done or done state, respectively."
  2226. :group 'org-todo
  2227. :group 'org-tags
  2228. :type '(repeat
  2229. (cons (choice :tag "When changing to"
  2230. (const :tag "Not-done state" todo)
  2231. (const :tag "Done state" done)
  2232. (string :tag "State"))
  2233. (repeat
  2234. (cons :tag "Tag action"
  2235. (string :tag "Tag")
  2236. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2237. (defcustom org-log-done nil
  2238. "Information to record when a task moves to the DONE state.
  2239. Possible values are:
  2240. nil Don't add anything, just change the keyword
  2241. time Add a time stamp to the task
  2242. note Prompt for a note and add it with template `org-log-note-headings'
  2243. This option can also be set with on a per-file-basis with
  2244. #+STARTUP: nologdone
  2245. #+STARTUP: logdone
  2246. #+STARTUP: lognotedone
  2247. You can have local logging settings for a subtree by setting the LOGGING
  2248. property to one or more of these keywords."
  2249. :group 'org-todo
  2250. :group 'org-progress
  2251. :type '(choice
  2252. (const :tag "No logging" nil)
  2253. (const :tag "Record CLOSED timestamp" time)
  2254. (const :tag "Record CLOSED timestamp with note." note)))
  2255. ;; Normalize old uses of org-log-done.
  2256. (cond
  2257. ((eq org-log-done t) (setq org-log-done 'time))
  2258. ((and (listp org-log-done) (memq 'done org-log-done))
  2259. (setq org-log-done 'note)))
  2260. (defcustom org-log-reschedule nil
  2261. "Information to record when the scheduling date of a tasks is modified.
  2262. Possible values are:
  2263. nil Don't add anything, just change the date
  2264. time Add a time stamp to the task
  2265. note Prompt for a note and add it with template `org-log-note-headings'
  2266. This option can also be set with on a per-file-basis with
  2267. #+STARTUP: nologreschedule
  2268. #+STARTUP: logreschedule
  2269. #+STARTUP: lognotereschedule"
  2270. :group 'org-todo
  2271. :group 'org-progress
  2272. :type '(choice
  2273. (const :tag "No logging" nil)
  2274. (const :tag "Record timestamp" time)
  2275. (const :tag "Record timestamp with note." note)))
  2276. (defcustom org-log-redeadline nil
  2277. "Information to record when the deadline date of a tasks is modified.
  2278. Possible values are:
  2279. nil Don't add anything, just change the date
  2280. time Add a time stamp to the task
  2281. note Prompt for a note and add it with template `org-log-note-headings'
  2282. This option can also be set with on a per-file-basis with
  2283. #+STARTUP: nologredeadline
  2284. #+STARTUP: logredeadline
  2285. #+STARTUP: lognoteredeadline
  2286. You can have local logging settings for a subtree by setting the LOGGING
  2287. property to one or more of these keywords."
  2288. :group 'org-todo
  2289. :group 'org-progress
  2290. :type '(choice
  2291. (const :tag "No logging" nil)
  2292. (const :tag "Record timestamp" time)
  2293. (const :tag "Record timestamp with note." note)))
  2294. (defcustom org-log-note-clock-out nil
  2295. "Non-nil means record a note when clocking out of an item.
  2296. This can also be configured on a per-file basis by adding one of
  2297. the following lines anywhere in the buffer:
  2298. #+STARTUP: lognoteclock-out
  2299. #+STARTUP: nolognoteclock-out"
  2300. :group 'org-todo
  2301. :group 'org-progress
  2302. :type 'boolean)
  2303. (defcustom org-log-done-with-time t
  2304. "Non-nil means the CLOSED time stamp will contain date and time.
  2305. When nil, only the date will be recorded."
  2306. :group 'org-progress
  2307. :type 'boolean)
  2308. (defcustom org-log-note-headings
  2309. '((done . "CLOSING NOTE %t")
  2310. (state . "State %-12s from %-12S %t")
  2311. (note . "Note taken on %t")
  2312. (reschedule . "Rescheduled from %S on %t")
  2313. (delschedule . "Not scheduled, was %S on %t")
  2314. (redeadline . "New deadline from %S on %t")
  2315. (deldeadline . "Removed deadline, was %S on %t")
  2316. (refile . "Refiled on %t")
  2317. (clock-out . ""))
  2318. "Headings for notes added to entries.
  2319. The value is an alist, with the car being a symbol indicating the note
  2320. context, and the cdr is the heading to be used. The heading may also be the
  2321. empty string.
  2322. %t in the heading will be replaced by a time stamp.
  2323. %T will be an active time stamp instead the default inactive one
  2324. %d will be replaced by a short-format time stamp.
  2325. %D will be replaced by an active short-format time stamp.
  2326. %s will be replaced by the new TODO state, in double quotes.
  2327. %S will be replaced by the old TODO state, in double quotes.
  2328. %u will be replaced by the user name.
  2329. %U will be replaced by the full user name.
  2330. In fact, it is not a good idea to change the `state' entry, because
  2331. agenda log mode depends on the format of these entries."
  2332. :group 'org-todo
  2333. :group 'org-progress
  2334. :type '(list :greedy t
  2335. (cons (const :tag "Heading when closing an item" done) string)
  2336. (cons (const :tag
  2337. "Heading when changing todo state (todo sequence only)"
  2338. state) string)
  2339. (cons (const :tag "Heading when just taking a note" note) string)
  2340. (cons (const :tag "Heading when clocking out" clock-out) string)
  2341. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2342. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2343. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2344. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2345. (cons (const :tag "Heading when refiling" refile) string)))
  2346. (unless (assq 'note org-log-note-headings)
  2347. (push '(note . "%t") org-log-note-headings))
  2348. (defcustom org-log-into-drawer nil
  2349. "Non-nil means insert state change notes and time stamps into a drawer.
  2350. When nil, state changes notes will be inserted after the headline and
  2351. any scheduling and clock lines, but not inside a drawer.
  2352. The value of this variable should be the name of the drawer to use.
  2353. LOGBOOK is proposed as the default drawer for this purpose, you can
  2354. also set this to a string to define the drawer of your choice.
  2355. A value of t is also allowed, representing \"LOGBOOK\".
  2356. A value of t or nil can also be set with on a per-file-basis with
  2357. #+STARTUP: logdrawer
  2358. #+STARTUP: nologdrawer
  2359. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2360. will be ignored.
  2361. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2362. a subtree."
  2363. :group 'org-todo
  2364. :group 'org-progress
  2365. :type '(choice
  2366. (const :tag "Not into a drawer" nil)
  2367. (const :tag "LOGBOOK" t)
  2368. (string :tag "Other")))
  2369. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2370. (defun org-log-into-drawer ()
  2371. "Return the value of `org-log-into-drawer', but let properties overrule.
  2372. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2373. used instead of the default value."
  2374. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2375. (cond
  2376. ((not p) org-log-into-drawer)
  2377. ((equal p "nil") nil)
  2378. ((equal p "t") "LOGBOOK")
  2379. (t p))))
  2380. (defcustom org-log-state-notes-insert-after-drawers nil
  2381. "Non-nil means insert state change notes after any drawers in entry.
  2382. Only the drawers that *immediately* follow the headline and the
  2383. deadline/scheduled line are skipped.
  2384. When nil, insert notes right after the heading and perhaps the line
  2385. with deadline/scheduling if present.
  2386. This variable will have no effect if `org-log-into-drawer' is
  2387. set."
  2388. :group 'org-todo
  2389. :group 'org-progress
  2390. :type 'boolean)
  2391. (defcustom org-log-states-order-reversed t
  2392. "Non-nil means the latest state note will be directly after heading.
  2393. When nil, the state change notes will be ordered according to time.
  2394. This option can also be set with on a per-file-basis with
  2395. #+STARTUP: logstatesreversed
  2396. #+STARTUP: nologstatesreversed"
  2397. :group 'org-todo
  2398. :group 'org-progress
  2399. :type 'boolean)
  2400. (defcustom org-todo-repeat-to-state nil
  2401. "The TODO state to which a repeater should return the repeating task.
  2402. By default this is the first task in a TODO sequence, or the previous state
  2403. in a TODO_TYP set. But you can specify another task here.
  2404. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2405. :group 'org-todo
  2406. :version "24.1"
  2407. :type '(choice (const :tag "Head of sequence" nil)
  2408. (string :tag "Specific state")))
  2409. (defcustom org-log-repeat 'time
  2410. "Non-nil means record moving through the DONE state when triggering repeat.
  2411. An auto-repeating task is immediately switched back to TODO when
  2412. marked DONE. If you are not logging state changes (by adding \"@\"
  2413. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2414. record a closing note, there will be no record of the task moving
  2415. through DONE. This variable forces taking a note anyway.
  2416. nil Don't force a record
  2417. time Record a time stamp
  2418. note Prompt for a note and add it with template `org-log-note-headings'
  2419. This option can also be set with on a per-file-basis with
  2420. #+STARTUP: nologrepeat
  2421. #+STARTUP: logrepeat
  2422. #+STARTUP: lognoterepeat
  2423. You can have local logging settings for a subtree by setting the LOGGING
  2424. property to one or more of these keywords."
  2425. :group 'org-todo
  2426. :group 'org-progress
  2427. :type '(choice
  2428. (const :tag "Don't force a record" nil)
  2429. (const :tag "Force recording the DONE state" time)
  2430. (const :tag "Force recording a note with the DONE state" note)))
  2431. (defgroup org-priorities nil
  2432. "Priorities in Org-mode."
  2433. :tag "Org Priorities"
  2434. :group 'org-todo)
  2435. (defcustom org-enable-priority-commands t
  2436. "Non-nil means priority commands are active.
  2437. When nil, these commands will be disabled, so that you never accidentally
  2438. set a priority."
  2439. :group 'org-priorities
  2440. :type 'boolean)
  2441. (defcustom org-highest-priority ?A
  2442. "The highest priority of TODO items. A character like ?A, ?B etc.
  2443. Must have a smaller ASCII number than `org-lowest-priority'."
  2444. :group 'org-priorities
  2445. :type 'character)
  2446. (defcustom org-lowest-priority ?C
  2447. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2448. Must have a larger ASCII number than `org-highest-priority'."
  2449. :group 'org-priorities
  2450. :type 'character)
  2451. (defcustom org-default-priority ?B
  2452. "The default priority of TODO items.
  2453. This is the priority an item gets if no explicit priority is given.
  2454. When starting to cycle on an empty priority the first step in the cycle
  2455. depends on `org-priority-start-cycle-with-default'. The resulting first
  2456. step priority must not exceed the range from `org-highest-priority' to
  2457. `org-lowest-priority' which means that `org-default-priority' has to be
  2458. in this range exclusive or inclusive the range boundaries. Else the
  2459. first step refuses to set the default and the second will fall back
  2460. to (depending on the command used) the highest or lowest priority."
  2461. :group 'org-priorities
  2462. :type 'character)
  2463. (defcustom org-priority-start-cycle-with-default t
  2464. "Non-nil means start with default priority when starting to cycle.
  2465. When this is nil, the first step in the cycle will be (depending on the
  2466. command used) one higher or lower than the default priority.
  2467. See also `org-default-priority'."
  2468. :group 'org-priorities
  2469. :type 'boolean)
  2470. (defcustom org-get-priority-function nil
  2471. "Function to extract the priority from a string.
  2472. The string is normally the headline. If this is nil Org computes the
  2473. priority from the priority cookie like [#A] in the headline. It returns
  2474. an integer, increasing by 1000 for each priority level.
  2475. The user can set a different function here, which should take a string
  2476. as an argument and return the numeric priority."
  2477. :group 'org-priorities
  2478. :version "24.1"
  2479. :type '(choice
  2480. (const nil)
  2481. (function)))
  2482. (defgroup org-time nil
  2483. "Options concerning time stamps and deadlines in Org-mode."
  2484. :tag "Org Time"
  2485. :group 'org)
  2486. (defcustom org-insert-labeled-timestamps-at-point nil
  2487. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2488. When nil, these labeled time stamps are forces into the second line of an
  2489. entry, just after the headline. When scheduling from the global TODO list,
  2490. the time stamp will always be forced into the second line."
  2491. :group 'org-time
  2492. :type 'boolean)
  2493. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2494. "Formats for `format-time-string' which are used for time stamps.
  2495. It is not recommended to change this constant.")
  2496. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2497. "Number of minutes to round time stamps to.
  2498. These are two values, the first applies when first creating a time stamp.
  2499. The second applies when changing it with the commands `S-up' and `S-down'.
  2500. When changing the time stamp, this means that it will change in steps
  2501. of N minutes, as given by the second value.
  2502. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2503. numbers should be factors of 60, so for example 5, 10, 15.
  2504. When this is larger than 1, you can still force an exact time stamp by using
  2505. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2506. and by using a prefix arg to `S-up/down' to specify the exact number
  2507. of minutes to shift."
  2508. :group 'org-time
  2509. :get #'(lambda (var) ; Make sure both elements are there
  2510. (if (integerp (default-value var))
  2511. (list (default-value var) 5)
  2512. (default-value var)))
  2513. :type '(list
  2514. (integer :tag "when inserting times")
  2515. (integer :tag "when modifying times")))
  2516. ;; Normalize old customizations of this variable.
  2517. (when (integerp org-time-stamp-rounding-minutes)
  2518. (setq org-time-stamp-rounding-minutes
  2519. (list org-time-stamp-rounding-minutes
  2520. org-time-stamp-rounding-minutes)))
  2521. (defcustom org-display-custom-times nil
  2522. "Non-nil means overlay custom formats over all time stamps.
  2523. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2524. To turn this on on a per-file basis, insert anywhere in the file:
  2525. #+STARTUP: customtime"
  2526. :group 'org-time
  2527. :set 'set-default
  2528. :type 'sexp)
  2529. (make-variable-buffer-local 'org-display-custom-times)
  2530. (defcustom org-time-stamp-custom-formats
  2531. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2532. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2533. These are overlaid over the default ISO format if the variable
  2534. `org-display-custom-times' is set. Time like %H:%M should be at the
  2535. end of the second format. The custom formats are also honored by export
  2536. commands, if custom time display is turned on at the time of export."
  2537. :group 'org-time
  2538. :type 'sexp)
  2539. (defun org-time-stamp-format (&optional long inactive)
  2540. "Get the right format for a time string."
  2541. (let ((f (if long (cdr org-time-stamp-formats)
  2542. (car org-time-stamp-formats))))
  2543. (if inactive
  2544. (concat "[" (substring f 1 -1) "]")
  2545. f)))
  2546. (defcustom org-time-clocksum-format
  2547. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2548. "The format string used when creating CLOCKSUM lines.
  2549. This is also used when Org mode generates a time duration.
  2550. The value can be a single format string containing two
  2551. %-sequences, which will be filled with the number of hours and
  2552. minutes in that order.
  2553. Alternatively, the value can be a plist associating any of the
  2554. keys :years, :months, :weeks, :days, :hours or :minutes with
  2555. format strings. The time duration is formatted using only the
  2556. time components that are needed and concatenating the results.
  2557. If a time unit in absent, it falls back to the next smallest
  2558. unit.
  2559. The keys :require-years, :require-months, :require-days,
  2560. :require-weeks, :require-hours, :require-minutes are also
  2561. meaningful. A non-nil value for these keys indicates that the
  2562. corresponding time component should always be included, even if
  2563. its value is 0.
  2564. For example,
  2565. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2566. :require-minutes t)
  2567. means durations longer than a day will be expressed in days,
  2568. hours and minutes, and durations less than a day will always be
  2569. expressed in hours and minutes (even for durations less than an
  2570. hour).
  2571. The value
  2572. \(:days \"%dd\" :minutes \"%dm\")
  2573. means durations longer than a day will be expressed in days and
  2574. minutes, and durations less than a day will be expressed entirely
  2575. in minutes (even for durations longer than an hour)."
  2576. :group 'org-time
  2577. :group 'org-clock
  2578. :version "24.4"
  2579. :package-version '(Org . "8.0")
  2580. :type '(choice (string :tag "Format string")
  2581. (set :tag "Plist"
  2582. (group :inline t (const :tag "Years" :years)
  2583. (string :tag "Format string"))
  2584. (group :inline t
  2585. (const :tag "Always show years" :require-years)
  2586. (const t))
  2587. (group :inline t (const :tag "Months" :months)
  2588. (string :tag "Format string"))
  2589. (group :inline t
  2590. (const :tag "Always show months" :require-months)
  2591. (const t))
  2592. (group :inline t (const :tag "Weeks" :weeks)
  2593. (string :tag "Format string"))
  2594. (group :inline t
  2595. (const :tag "Always show weeks" :require-weeks)
  2596. (const t))
  2597. (group :inline t (const :tag "Days" :days)
  2598. (string :tag "Format string"))
  2599. (group :inline t
  2600. (const :tag "Always show days" :require-days)
  2601. (const t))
  2602. (group :inline t (const :tag "Hours" :hours)
  2603. (string :tag "Format string"))
  2604. (group :inline t
  2605. (const :tag "Always show hours" :require-hours)
  2606. (const t))
  2607. (group :inline t (const :tag "Minutes" :minutes)
  2608. (string :tag "Format string"))
  2609. (group :inline t
  2610. (const :tag "Always show minutes" :require-minutes)
  2611. (const t)))))
  2612. (defcustom org-time-clocksum-use-fractional nil
  2613. "When non-nil, \\[org-clock-display] uses fractional times.
  2614. See `org-time-clocksum-format' for more on time clock formats."
  2615. :group 'org-time
  2616. :group 'org-clock
  2617. :version "24.3"
  2618. :type 'boolean)
  2619. (defcustom org-time-clocksum-use-effort-durations nil
  2620. "When non-nil, \\[org-clock-display] uses effort durations.
  2621. E.g. by default, one day is considered to be a 8 hours effort,
  2622. so a task that has been clocked for 16 hours will be displayed
  2623. as during 2 days in the clock display or in the clocktable.
  2624. See `org-effort-durations' on how to set effort durations
  2625. and `org-time-clocksum-format' for more on time clock formats."
  2626. :group 'org-time
  2627. :group 'org-clock
  2628. :version "24.4"
  2629. :package-version '(Org . "8.0")
  2630. :type 'boolean)
  2631. (defcustom org-time-clocksum-fractional-format "%.2f"
  2632. "The format string used when creating CLOCKSUM lines,
  2633. or when Org mode generates a time duration, if
  2634. `org-time-clocksum-use-fractional' is enabled.
  2635. The value can be a single format string containing one
  2636. %-sequence, which will be filled with the number of hours as
  2637. a float.
  2638. Alternatively, the value can be a plist associating any of the
  2639. keys :years, :months, :weeks, :days, :hours or :minutes with
  2640. a format string. The time duration is formatted using the
  2641. largest time unit which gives a non-zero integer part. If all
  2642. specified formats have zero integer part, the smallest time unit
  2643. is used."
  2644. :group 'org-time
  2645. :type '(choice (string :tag "Format string")
  2646. (set (group :inline t (const :tag "Years" :years)
  2647. (string :tag "Format string"))
  2648. (group :inline t (const :tag "Months" :months)
  2649. (string :tag "Format string"))
  2650. (group :inline t (const :tag "Weeks" :weeks)
  2651. (string :tag "Format string"))
  2652. (group :inline t (const :tag "Days" :days)
  2653. (string :tag "Format string"))
  2654. (group :inline t (const :tag "Hours" :hours)
  2655. (string :tag "Format string"))
  2656. (group :inline t (const :tag "Minutes" :minutes)
  2657. (string :tag "Format string")))))
  2658. (defcustom org-deadline-warning-days 14
  2659. "Number of days before expiration during which a deadline becomes active.
  2660. This variable governs the display in sparse trees and in the agenda.
  2661. When 0 or negative, it means use this number (the absolute value of it)
  2662. even if a deadline has a different individual lead time specified.
  2663. Custom commands can set this variable in the options section."
  2664. :group 'org-time
  2665. :group 'org-agenda-daily/weekly
  2666. :type 'integer)
  2667. (defcustom org-scheduled-delay-days 0
  2668. "Number of days before a scheduled item becomes active.
  2669. This variable governs the display in sparse trees and in the agenda.
  2670. The default value (i.e. 0) means: don't delay scheduled item.
  2671. When negative, it means use this number (the absolute value of it)
  2672. even if a scheduled item has a different individual delay time
  2673. specified.
  2674. Custom commands can set this variable in the options section."
  2675. :group 'org-time
  2676. :group 'org-agenda-daily/weekly
  2677. :version "24.4"
  2678. :package-version '(Org . "8.0")
  2679. :type 'integer)
  2680. (defcustom org-read-date-prefer-future t
  2681. "Non-nil means assume future for incomplete date input from user.
  2682. This affects the following situations:
  2683. 1. The user gives a month but not a year.
  2684. For example, if it is April and you enter \"feb 2\", this will be read
  2685. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2686. 2. The user gives a day, but no month.
  2687. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2688. read this as the third of *next* month. However, if you enter \"17\",
  2689. it will be considered as *this* month.
  2690. If you set this variable to the symbol `time', then also the following
  2691. will work:
  2692. 3. If the user gives a time.
  2693. If the time is before now, it will be interpreted as tomorrow.
  2694. Currently none of this works for ISO week specifications.
  2695. When this option is nil, the current day, month and year will always be
  2696. used as defaults.
  2697. See also `org-agenda-jump-prefer-future'."
  2698. :group 'org-time
  2699. :type '(choice
  2700. (const :tag "Never" nil)
  2701. (const :tag "Check month and day" t)
  2702. (const :tag "Check month, day, and time" time)))
  2703. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2704. "Should the agenda jump command prefer the future for incomplete dates?
  2705. The default is to do the same as configured in `org-read-date-prefer-future'.
  2706. But you can also set a deviating value here.
  2707. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2708. :group 'org-agenda
  2709. :group 'org-time
  2710. :version "24.1"
  2711. :type '(choice
  2712. (const :tag "Use org-read-date-prefer-future"
  2713. org-read-date-prefer-future)
  2714. (const :tag "Never" nil)
  2715. (const :tag "Always" t)))
  2716. (defcustom org-read-date-force-compatible-dates t
  2717. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2718. Depending on the system Emacs is running on, certain dates cannot
  2719. be represented with the type used internally to represent time.
  2720. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2721. correctly. Some systems allow for earlier dates, some for later,
  2722. some for both. One way to find out it to insert any date into an
  2723. Org buffer, putting the cursor on the year and hitting S-up and
  2724. S-down to test the range.
  2725. When this variable is set to t, the date/time prompt will not let
  2726. you specify dates outside the 1970-2037 range, so it is certain that
  2727. these dates will work in whatever version of Emacs you are
  2728. running, and also that you can move a file from one Emacs implementation
  2729. to another. WHenever Org is forcing the year for you, it will display
  2730. a message and beep.
  2731. When this variable is nil, Org will check if the date is
  2732. representable in the specific Emacs implementation you are using.
  2733. If not, it will force a year, usually the current year, and beep
  2734. to remind you. Currently this setting is not recommended because
  2735. the likelihood that you will open your Org files in an Emacs that
  2736. has limited date range is not negligible.
  2737. A workaround for this problem is to use diary sexp dates for time
  2738. stamps outside of this range."
  2739. :group 'org-time
  2740. :version "24.1"
  2741. :type 'boolean)
  2742. (defcustom org-read-date-display-live t
  2743. "Non-nil means display current interpretation of date prompt live.
  2744. This display will be in an overlay, in the minibuffer."
  2745. :group 'org-time
  2746. :type 'boolean)
  2747. (defcustom org-read-date-popup-calendar t
  2748. "Non-nil means pop up a calendar when prompting for a date.
  2749. In the calendar, the date can be selected with mouse-1. However, the
  2750. minibuffer will also be active, and you can simply enter the date as well.
  2751. When nil, only the minibuffer will be available."
  2752. :group 'org-time
  2753. :type 'boolean)
  2754. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2755. 'org-read-date-popup-calendar)
  2756. (make-obsolete-variable
  2757. 'org-read-date-minibuffer-setup-hook
  2758. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2759. (defcustom org-read-date-minibuffer-setup-hook nil
  2760. "Hook to be used to set up keys for the date/time interface.
  2761. Add key definitions to `minibuffer-local-map', which will be a
  2762. temporary copy.
  2763. WARNING: This option is obsolete, you should use
  2764. `org-read-date-minibuffer-local-map' to set up keys."
  2765. :group 'org-time
  2766. :type 'hook)
  2767. (defcustom org-extend-today-until 0
  2768. "The hour when your day really ends. Must be an integer.
  2769. This has influence for the following applications:
  2770. - When switching the agenda to \"today\". It it is still earlier than
  2771. the time given here, the day recognized as TODAY is actually yesterday.
  2772. - When a date is read from the user and it is still before the time given
  2773. here, the current date and time will be assumed to be yesterday, 23:59.
  2774. Also, timestamps inserted in capture templates follow this rule.
  2775. IMPORTANT: This is a feature whose implementation is and likely will
  2776. remain incomplete. Really, it is only here because past midnight seems to
  2777. be the favorite working time of John Wiegley :-)"
  2778. :group 'org-time
  2779. :type 'integer)
  2780. (defcustom org-use-effective-time nil
  2781. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2782. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2783. \"effective time\" of any timestamps between midnight and 8am will be
  2784. 23:59 of the previous day."
  2785. :group 'org-time
  2786. :version "24.1"
  2787. :type 'boolean)
  2788. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2789. "When non-nil, use the last clock out time for `org-todo'.
  2790. Note that this option has precedence over the combined use of
  2791. `org-use-effective-time' and `org-extend-today-until'."
  2792. :group 'org-time
  2793. :version "24.4"
  2794. :package-version '(Org . "8.0")
  2795. :type 'boolean)
  2796. (defcustom org-edit-timestamp-down-means-later nil
  2797. "Non-nil means S-down will increase the time in a time stamp.
  2798. When nil, S-up will increase."
  2799. :group 'org-time
  2800. :type 'boolean)
  2801. (defcustom org-calendar-follow-timestamp-change t
  2802. "Non-nil means make the calendar window follow timestamp changes.
  2803. When a timestamp is modified and the calendar window is visible, it will be
  2804. moved to the new date."
  2805. :group 'org-time
  2806. :type 'boolean)
  2807. (defgroup org-tags nil
  2808. "Options concerning tags in Org-mode."
  2809. :tag "Org Tags"
  2810. :group 'org)
  2811. (defcustom org-tag-alist nil
  2812. "List of tags allowed in Org-mode files.
  2813. When this list is nil, Org-mode will base TAG input on what is already in the
  2814. buffer.
  2815. The value of this variable is an alist, the car of each entry must be a
  2816. keyword as a string, the cdr may be a character that is used to select
  2817. that tag through the fast-tag-selection interface.
  2818. See the manual for details."
  2819. :group 'org-tags
  2820. :type '(repeat
  2821. (choice
  2822. (cons (string :tag "Tag name")
  2823. (character :tag "Access char"))
  2824. (list :tag "Start radio group"
  2825. (const :startgroup)
  2826. (option (string :tag "Group description")))
  2827. (list :tag "Group tags delimiter"
  2828. (const :grouptags))
  2829. (list :tag "End radio group"
  2830. (const :endgroup)
  2831. (option (string :tag "Group description")))
  2832. (const :tag "New line" (:newline)))))
  2833. (defcustom org-tag-persistent-alist nil
  2834. "List of tags that will always appear in all Org-mode files.
  2835. This is in addition to any in buffer settings or customizations
  2836. of `org-tag-alist'.
  2837. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2838. The value of this variable is an alist, the car of each entry must be a
  2839. keyword as a string, the cdr may be a character that is used to select
  2840. that tag through the fast-tag-selection interface.
  2841. See the manual for details.
  2842. To disable these tags on a per-file basis, insert anywhere in the file:
  2843. #+STARTUP: noptag"
  2844. :group 'org-tags
  2845. :type '(repeat
  2846. (choice
  2847. (cons (string :tag "Tag name")
  2848. (character :tag "Access char"))
  2849. (const :tag "Start radio group" (:startgroup))
  2850. (const :tag "Group tags delimiter" (:grouptags))
  2851. (const :tag "End radio group" (:endgroup))
  2852. (const :tag "New line" (:newline)))))
  2853. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2854. "If non-nil, always offer completion for all tags of all agenda files.
  2855. Instead of customizing this variable directly, you might want to
  2856. set it locally for capture buffers, because there no list of
  2857. tags in that file can be created dynamically (there are none).
  2858. (add-hook 'org-capture-mode-hook
  2859. (lambda ()
  2860. (set (make-local-variable
  2861. 'org-complete-tags-always-offer-all-agenda-tags)
  2862. t)))"
  2863. :group 'org-tags
  2864. :version "24.1"
  2865. :type 'boolean)
  2866. (defvar org-file-tags nil
  2867. "List of tags that can be inherited by all entries in the file.
  2868. The tags will be inherited if the variable `org-use-tag-inheritance'
  2869. says they should be.
  2870. This variable is populated from #+FILETAGS lines.")
  2871. (defcustom org-use-fast-tag-selection 'auto
  2872. "Non-nil means use fast tag selection scheme.
  2873. This is a special interface to select and deselect tags with single keys.
  2874. When nil, fast selection is never used.
  2875. When the symbol `auto', fast selection is used if and only if selection
  2876. characters for tags have been configured, either through the variable
  2877. `org-tag-alist' or through a #+TAGS line in the buffer.
  2878. When t, fast selection is always used and selection keys are assigned
  2879. automatically if necessary."
  2880. :group 'org-tags
  2881. :type '(choice
  2882. (const :tag "Always" t)
  2883. (const :tag "Never" nil)
  2884. (const :tag "When selection characters are configured" auto)))
  2885. (defcustom org-fast-tag-selection-single-key nil
  2886. "Non-nil means fast tag selection exits after first change.
  2887. When nil, you have to press RET to exit it.
  2888. During fast tag selection, you can toggle this flag with `C-c'.
  2889. This variable can also have the value `expert'. In this case, the window
  2890. displaying the tags menu is not even shown, until you press C-c again."
  2891. :group 'org-tags
  2892. :type '(choice
  2893. (const :tag "No" nil)
  2894. (const :tag "Yes" t)
  2895. (const :tag "Expert" expert)))
  2896. (defvar org-fast-tag-selection-include-todo nil
  2897. "Non-nil means fast tags selection interface will also offer TODO states.
  2898. This is an undocumented feature, you should not rely on it.")
  2899. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2900. "The column to which tags should be indented in a headline.
  2901. If this number is positive, it specifies the column. If it is negative,
  2902. it means that the tags should be flushright to that column. For example,
  2903. -80 works well for a normal 80 character screen.
  2904. When 0, place tags directly after headline text, with only one space in
  2905. between."
  2906. :group 'org-tags
  2907. :type 'integer)
  2908. (defcustom org-auto-align-tags t
  2909. "Non-nil keeps tags aligned when modifying headlines.
  2910. Some operations (i.e. demoting) change the length of a headline and
  2911. therefore shift the tags around. With this option turned on, after
  2912. each such operation the tags are again aligned to `org-tags-column'."
  2913. :group 'org-tags
  2914. :type 'boolean)
  2915. (defcustom org-use-tag-inheritance t
  2916. "Non-nil means tags in levels apply also for sublevels.
  2917. When nil, only the tags directly given in a specific line apply there.
  2918. This may also be a list of tags that should be inherited, or a regexp that
  2919. matches tags that should be inherited. Additional control is possible
  2920. with the variable `org-tags-exclude-from-inheritance' which gives an
  2921. explicit list of tags to be excluded from inheritance, even if the value of
  2922. `org-use-tag-inheritance' would select it for inheritance.
  2923. If this option is t, a match early-on in a tree can lead to a large
  2924. number of matches in the subtree when constructing the agenda or creating
  2925. a sparse tree. If you only want to see the first match in a tree during
  2926. a search, check out the variable `org-tags-match-list-sublevels'."
  2927. :group 'org-tags
  2928. :type '(choice
  2929. (const :tag "Not" nil)
  2930. (const :tag "Always" t)
  2931. (repeat :tag "Specific tags" (string :tag "Tag"))
  2932. (regexp :tag "Tags matched by regexp")))
  2933. (defcustom org-tags-exclude-from-inheritance nil
  2934. "List of tags that should never be inherited.
  2935. This is a way to exclude a few tags from inheritance. For way to do
  2936. the opposite, to actively allow inheritance for selected tags,
  2937. see the variable `org-use-tag-inheritance'."
  2938. :group 'org-tags
  2939. :type '(repeat (string :tag "Tag")))
  2940. (defun org-tag-inherit-p (tag)
  2941. "Check if TAG is one that should be inherited."
  2942. (cond
  2943. ((member tag org-tags-exclude-from-inheritance) nil)
  2944. ((eq org-use-tag-inheritance t) t)
  2945. ((not org-use-tag-inheritance) nil)
  2946. ((stringp org-use-tag-inheritance)
  2947. (string-match org-use-tag-inheritance tag))
  2948. ((listp org-use-tag-inheritance)
  2949. (member tag org-use-tag-inheritance))
  2950. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2951. (defcustom org-tags-match-list-sublevels t
  2952. "Non-nil means list also sublevels of headlines matching a search.
  2953. This variable applies to tags/property searches, and also to stuck
  2954. projects because this search is based on a tags match as well.
  2955. When set to the symbol `indented', sublevels are indented with
  2956. leading dots.
  2957. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2958. the sublevels of a headline matching a tag search often also match
  2959. the same search. Listing all of them can create very long lists.
  2960. Setting this variable to nil causes subtrees of a match to be skipped.
  2961. This variable is semi-obsolete and probably should always be true. It
  2962. is better to limit inheritance to certain tags using the variables
  2963. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2964. :group 'org-tags
  2965. :type '(choice
  2966. (const :tag "No, don't list them" nil)
  2967. (const :tag "Yes, do list them" t)
  2968. (const :tag "List them, indented with leading dots" indented)))
  2969. (defcustom org-tags-sort-function nil
  2970. "When set, tags are sorted using this function as a comparator."
  2971. :group 'org-tags
  2972. :type '(choice
  2973. (const :tag "No sorting" nil)
  2974. (const :tag "Alphabetical" string<)
  2975. (const :tag "Reverse alphabetical" string>)
  2976. (function :tag "Custom function" nil)))
  2977. (defvar org-tags-history nil
  2978. "History of minibuffer reads for tags.")
  2979. (defvar org-last-tags-completion-table nil
  2980. "The last used completion table for tags.")
  2981. (defvar org-after-tags-change-hook nil
  2982. "Hook that is run after the tags in a line have changed.")
  2983. (defgroup org-properties nil
  2984. "Options concerning properties in Org-mode."
  2985. :tag "Org Properties"
  2986. :group 'org)
  2987. (defcustom org-property-format "%-10s %s"
  2988. "How property key/value pairs should be formatted by `indent-line'.
  2989. When `indent-line' hits a property definition, it will format the line
  2990. according to this format, mainly to make sure that the values are
  2991. lined-up with respect to each other."
  2992. :group 'org-properties
  2993. :type 'string)
  2994. (defcustom org-properties-postprocess-alist nil
  2995. "Alist of properties and functions to adjust inserted values.
  2996. Elements of this alist must be of the form
  2997. ([string] [function])
  2998. where [string] must be a property name and [function] must be a
  2999. lambda expression: this lambda expression must take one argument,
  3000. the value to adjust, and return the new value as a string.
  3001. For example, this element will allow the property \"Remaining\"
  3002. to be updated wrt the relation between the \"Effort\" property
  3003. and the clock summary:
  3004. ((\"Remaining\" (lambda(value)
  3005. (let ((clocksum (org-clock-sum-current-item))
  3006. (effort (org-duration-string-to-minutes
  3007. (org-entry-get (point) \"Effort\"))))
  3008. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3009. :group 'org-properties
  3010. :version "24.1"
  3011. :type '(alist :key-type (string :tag "Property")
  3012. :value-type (function :tag "Function")))
  3013. (defcustom org-use-property-inheritance nil
  3014. "Non-nil means properties apply also for sublevels.
  3015. This setting is chiefly used during property searches. Turning it on can
  3016. cause significant overhead when doing a search, which is why it is not
  3017. on by default.
  3018. When nil, only the properties directly given in the current entry count.
  3019. When t, every property is inherited. The value may also be a list of
  3020. properties that should have inheritance, or a regular expression matching
  3021. properties that should be inherited.
  3022. However, note that some special properties use inheritance under special
  3023. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3024. and the properties ending in \"_ALL\" when they are used as descriptor
  3025. for valid values of a property.
  3026. Note for programmers:
  3027. When querying an entry with `org-entry-get', you can control if inheritance
  3028. should be used. By default, `org-entry-get' looks only at the local
  3029. properties. You can request inheritance by setting the inherit argument
  3030. to t (to force inheritance) or to `selective' (to respect the setting
  3031. in this variable)."
  3032. :group 'org-properties
  3033. :type '(choice
  3034. (const :tag "Not" nil)
  3035. (const :tag "Always" t)
  3036. (repeat :tag "Specific properties" (string :tag "Property"))
  3037. (regexp :tag "Properties matched by regexp")))
  3038. (defun org-property-inherit-p (property)
  3039. "Check if PROPERTY is one that should be inherited."
  3040. (cond
  3041. ((eq org-use-property-inheritance t) t)
  3042. ((not org-use-property-inheritance) nil)
  3043. ((stringp org-use-property-inheritance)
  3044. (string-match org-use-property-inheritance property))
  3045. ((listp org-use-property-inheritance)
  3046. (member property org-use-property-inheritance))
  3047. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3048. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3049. "The default column format, if no other format has been defined.
  3050. This variable can be set on the per-file basis by inserting a line
  3051. #+COLUMNS: %25ITEM ....."
  3052. :group 'org-properties
  3053. :type 'string)
  3054. (defcustom org-columns-ellipses ".."
  3055. "The ellipses to be used when a field in column view is truncated.
  3056. When this is the empty string, as many characters as possible are shown,
  3057. but then there will be no visual indication that the field has been truncated.
  3058. When this is a string of length N, the last N characters of a truncated
  3059. field are replaced by this string. If the column is narrower than the
  3060. ellipses string, only part of the ellipses string will be shown."
  3061. :group 'org-properties
  3062. :type 'string)
  3063. (defcustom org-columns-modify-value-for-display-function nil
  3064. "Function that modifies values for display in column view.
  3065. For example, it can be used to cut out a certain part from a time stamp.
  3066. The function must take 2 arguments:
  3067. column-title The title of the column (*not* the property name)
  3068. value The value that should be modified.
  3069. The function should return the value that should be displayed,
  3070. or nil if the normal value should be used."
  3071. :group 'org-properties
  3072. :type '(choice (const nil) (function)))
  3073. (defcustom org-effort-property "Effort"
  3074. "The property that is being used to keep track of effort estimates.
  3075. Effort estimates given in this property need to have the format H:MM."
  3076. :group 'org-properties
  3077. :group 'org-progress
  3078. :type '(string :tag "Property"))
  3079. (defconst org-global-properties-fixed
  3080. '(("VISIBILITY_ALL" . "folded children content all")
  3081. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3082. "List of property/value pairs that can be inherited by any entry.
  3083. These are fixed values, for the preset properties. The user variable
  3084. that can be used to add to this list is `org-global-properties'.
  3085. The entries in this list are cons cells where the car is a property
  3086. name and cdr is a string with the value. If the value represents
  3087. multiple items like an \"_ALL\" property, separate the items by
  3088. spaces.")
  3089. (defcustom org-global-properties nil
  3090. "List of property/value pairs that can be inherited by any entry.
  3091. This list will be combined with the constant `org-global-properties-fixed'.
  3092. The entries in this list are cons cells where the car is a property
  3093. name and cdr is a string with the value.
  3094. You can set buffer-local values for the same purpose in the variable
  3095. `org-file-properties' this by adding lines like
  3096. #+PROPERTY: NAME VALUE"
  3097. :group 'org-properties
  3098. :type '(repeat
  3099. (cons (string :tag "Property")
  3100. (string :tag "Value"))))
  3101. (defvar org-file-properties nil
  3102. "List of property/value pairs that can be inherited by any entry.
  3103. Valid for the current buffer.
  3104. This variable is populated from #+PROPERTY lines.")
  3105. (make-variable-buffer-local 'org-file-properties)
  3106. (defgroup org-agenda nil
  3107. "Options concerning agenda views in Org-mode."
  3108. :tag "Org Agenda"
  3109. :group 'org)
  3110. (defvar org-category nil
  3111. "Variable used by org files to set a category for agenda display.
  3112. Such files should use a file variable to set it, for example
  3113. # -*- mode: org; org-category: \"ELisp\"
  3114. or contain a special line
  3115. #+CATEGORY: ELisp
  3116. If the file does not specify a category, then file's base name
  3117. is used instead.")
  3118. (make-variable-buffer-local 'org-category)
  3119. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3120. (defcustom org-agenda-files nil
  3121. "The files to be used for agenda display.
  3122. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3123. \\[org-remove-file]. You can also use customize to edit the list.
  3124. If an entry is a directory, all files in that directory that are matched by
  3125. `org-agenda-file-regexp' will be part of the file list.
  3126. If the value of the variable is not a list but a single file name, then
  3127. the list of agenda files is actually stored and maintained in that file, one
  3128. agenda file per line. In this file paths can be given relative to
  3129. `org-directory'. Tilde expansion and environment variable substitution
  3130. are also made."
  3131. :group 'org-agenda
  3132. :type '(choice
  3133. (repeat :tag "List of files and directories" file)
  3134. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3135. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3136. "Regular expression to match files for `org-agenda-files'.
  3137. If any element in the list in that variable contains a directory instead
  3138. of a normal file, all files in that directory that are matched by this
  3139. regular expression will be included."
  3140. :group 'org-agenda
  3141. :type 'regexp)
  3142. (defcustom org-agenda-text-search-extra-files nil
  3143. "List of extra files to be searched by text search commands.
  3144. These files will be searched in addition to the agenda files by the
  3145. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3146. Note that these files will only be searched for text search commands,
  3147. not for the other agenda views like todo lists, tag searches or the weekly
  3148. agenda. This variable is intended to list notes and possibly archive files
  3149. that should also be searched by these two commands.
  3150. In fact, if the first element in the list is the symbol `agenda-archives',
  3151. then all archive files of all agenda files will be added to the search
  3152. scope."
  3153. :group 'org-agenda
  3154. :type '(set :greedy t
  3155. (const :tag "Agenda Archives" agenda-archives)
  3156. (repeat :inline t (file))))
  3157. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3158. 'org-agenda-text-search-extra-files)
  3159. (defcustom org-agenda-skip-unavailable-files nil
  3160. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3161. A nil value means to remove them, after a query, from the list."
  3162. :group 'org-agenda
  3163. :type 'boolean)
  3164. (defcustom org-calendar-to-agenda-key [?c]
  3165. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3166. The command `org-calendar-goto-agenda' will be bound to this key. The
  3167. default is the character `c' because then `c' can be used to switch back and
  3168. forth between agenda and calendar."
  3169. :group 'org-agenda
  3170. :type 'sexp)
  3171. (defcustom org-calendar-insert-diary-entry-key [?i]
  3172. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3173. This option is irrelevant until `org-agenda-diary-file' has been configured
  3174. to point to an Org-mode file. When that is the case, the command
  3175. `org-agenda-diary-entry' will be bound to the key given here, by default
  3176. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3177. if you want to continue doing this, you need to change this to a different
  3178. key."
  3179. :group 'org-agenda
  3180. :type 'sexp)
  3181. (defcustom org-agenda-diary-file 'diary-file
  3182. "File to which to add new entries with the `i' key in agenda and calendar.
  3183. When this is the symbol `diary-file', the functionality in the Emacs
  3184. calendar will be used to add entries to the `diary-file'. But when this
  3185. points to a file, `org-agenda-diary-entry' will be used instead."
  3186. :group 'org-agenda
  3187. :type '(choice
  3188. (const :tag "The standard Emacs diary file" diary-file)
  3189. (file :tag "Special Org file diary entries")))
  3190. (eval-after-load "calendar"
  3191. '(progn
  3192. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3193. 'org-calendar-goto-agenda)
  3194. (add-hook 'calendar-mode-hook
  3195. (lambda ()
  3196. (unless (eq org-agenda-diary-file 'diary-file)
  3197. (define-key calendar-mode-map
  3198. org-calendar-insert-diary-entry-key
  3199. 'org-agenda-diary-entry))))))
  3200. (defgroup org-latex nil
  3201. "Options for embedding LaTeX code into Org-mode."
  3202. :tag "Org LaTeX"
  3203. :group 'org)
  3204. (defcustom org-format-latex-options
  3205. '(:foreground default :background default :scale 1.0
  3206. :html-foreground "Black" :html-background "Transparent"
  3207. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3208. "Options for creating images from LaTeX fragments.
  3209. This is a property list with the following properties:
  3210. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3211. `default' means use the foreground of the default face.
  3212. `auto' means use the foreground from the text face.
  3213. :background the background color, or \"Transparent\".
  3214. `default' means use the background of the default face.
  3215. `auto' means use the background from the text face.
  3216. :scale a scaling factor for the size of the images, to get more pixels
  3217. :html-foreground, :html-background, :html-scale
  3218. the same numbers for HTML export.
  3219. :matchers a list indicating which matchers should be used to
  3220. find LaTeX fragments. Valid members of this list are:
  3221. \"begin\" find environments
  3222. \"$1\" find single characters surrounded by $.$
  3223. \"$\" find math expressions surrounded by $...$
  3224. \"$$\" find math expressions surrounded by $$....$$
  3225. \"\\(\" find math expressions surrounded by \\(...\\)
  3226. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3227. :group 'org-latex
  3228. :type 'plist)
  3229. (defcustom org-format-latex-signal-error t
  3230. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3231. When nil, just push out a message."
  3232. :group 'org-latex
  3233. :version "24.1"
  3234. :type 'boolean)
  3235. (defcustom org-latex-to-mathml-jar-file nil
  3236. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3237. Use this to specify additional executable file say a jar file.
  3238. When using MathToWeb as the converter, specify the full-path to
  3239. your mathtoweb.jar file."
  3240. :group 'org-latex
  3241. :version "24.1"
  3242. :type '(choice
  3243. (const :tag "None" nil)
  3244. (file :tag "JAR file" :must-match t)))
  3245. (defcustom org-latex-to-mathml-convert-command nil
  3246. "Command to convert LaTeX fragments to MathML.
  3247. Replace format-specifiers in the command as noted below and use
  3248. `shell-command' to convert LaTeX to MathML.
  3249. %j: Executable file in fully expanded form as specified by
  3250. `org-latex-to-mathml-jar-file'.
  3251. %I: Input LaTeX file in fully expanded form
  3252. %o: Output MathML file
  3253. This command is used by `org-create-math-formula'.
  3254. When using MathToWeb as the converter, set this to
  3255. \"java -jar %j -unicode -force -df %o %I\"."
  3256. :group 'org-latex
  3257. :version "24.1"
  3258. :type '(choice
  3259. (const :tag "None" nil)
  3260. (string :tag "\nShell command")))
  3261. (defcustom org-latex-create-formula-image-program 'dvipng
  3262. "Program to convert LaTeX fragments with.
  3263. dvipng Process the LaTeX fragments to dvi file, then convert
  3264. dvi files to png files using dvipng.
  3265. This will also include processing of non-math environments.
  3266. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3267. to convert pdf files to png files"
  3268. :group 'org-latex
  3269. :version "24.1"
  3270. :type '(choice
  3271. (const :tag "dvipng" dvipng)
  3272. (const :tag "imagemagick" imagemagick)))
  3273. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3274. "Path to store latex preview images.
  3275. A relative path here creates many directories relative to the
  3276. processed org files paths. An absolute path puts all preview
  3277. images at the same place."
  3278. :group 'org-latex
  3279. :version "24.3"
  3280. :type 'string)
  3281. (defun org-format-latex-mathml-available-p ()
  3282. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3283. (save-match-data
  3284. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3285. org-latex-to-mathml-convert-command)
  3286. (let ((executable (car (split-string
  3287. org-latex-to-mathml-convert-command))))
  3288. (when (executable-find executable)
  3289. (if (string-match
  3290. "%j" org-latex-to-mathml-convert-command)
  3291. (file-readable-p org-latex-to-mathml-jar-file)
  3292. t))))))
  3293. (defcustom org-format-latex-header "\\documentclass{article}
  3294. \\usepackage[usenames]{color}
  3295. \\usepackage{amsmath}
  3296. \\usepackage[mathscr]{eucal}
  3297. \[PACKAGES]
  3298. \[DEFAULT-PACKAGES]
  3299. \\pagestyle{empty} % do not remove
  3300. % The settings below are copied from fullpage.sty
  3301. \\setlength{\\textwidth}{\\paperwidth}
  3302. \\addtolength{\\textwidth}{-3cm}
  3303. \\setlength{\\oddsidemargin}{1.5cm}
  3304. \\addtolength{\\oddsidemargin}{-2.54cm}
  3305. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3306. \\setlength{\\textheight}{\\paperheight}
  3307. \\addtolength{\\textheight}{-\\headheight}
  3308. \\addtolength{\\textheight}{-\\headsep}
  3309. \\addtolength{\\textheight}{-\\footskip}
  3310. \\addtolength{\\textheight}{-3cm}
  3311. \\setlength{\\topmargin}{1.5cm}
  3312. \\addtolength{\\topmargin}{-2.54cm}"
  3313. "The document header used for processing LaTeX fragments.
  3314. It is imperative that this header make sure that no page number
  3315. appears on the page. The package defined in the variables
  3316. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3317. will either replace the placeholder \"[PACKAGES]\" in this
  3318. header, or they will be appended."
  3319. :group 'org-latex
  3320. :type 'string)
  3321. (defun org-set-packages-alist (var val)
  3322. "Set the packages alist and make sure it has 3 elements per entry."
  3323. (set var (mapcar (lambda (x)
  3324. (if (and (consp x) (= (length x) 2))
  3325. (list (car x) (nth 1 x) t)
  3326. x))
  3327. val)))
  3328. (defun org-get-packages-alist (var)
  3329. "Get the packages alist and make sure it has 3 elements per entry."
  3330. (mapcar (lambda (x)
  3331. (if (and (consp x) (= (length x) 2))
  3332. (list (car x) (nth 1 x) t)
  3333. x))
  3334. (default-value var)))
  3335. (defcustom org-latex-default-packages-alist
  3336. '(("AUTO" "inputenc" t)
  3337. ("T1" "fontenc" t)
  3338. ("" "fixltx2e" nil)
  3339. ("" "graphicx" t)
  3340. ("" "longtable" nil)
  3341. ("" "float" nil)
  3342. ("" "wrapfig" nil)
  3343. ("normalem" "ulem" t)
  3344. ("" "textcomp" t)
  3345. ("" "marvosym" t)
  3346. ("" "wasysym" t)
  3347. ("" "latexsym" t)
  3348. ("" "amssymb" t)
  3349. ("" "amstext" nil)
  3350. ("" "hyperref" nil)
  3351. "\\tolerance=1000")
  3352. "Alist of default packages to be inserted in the header.
  3353. Change this only if one of the packages here causes an
  3354. incompatibility with another package you are using.
  3355. The packages in this list are needed by one part or another of
  3356. Org mode to function properly:
  3357. - inputenc, fontenc: for basic font and character selection
  3358. - amstext: for subscript and superscript
  3359. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3360. symbols used for interpreting the entities in `org-entities'.
  3361. You can skip some of these packages if you don't use any of the
  3362. symbols in it.
  3363. - ulem: for underline and strike-through
  3364. - graphicx: for including images
  3365. - float, wrapfig: for figure placement
  3366. - longtable: for long tables
  3367. - hyperref: for cross references
  3368. Therefore you should not modify this variable unless you know
  3369. what you are doing. The one reason to change it anyway is that
  3370. you might be loading some other package that conflicts with one
  3371. of the default packages. Each cell is of the format
  3372. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3373. the package also needs to be included when compiling LaTeX
  3374. snippets into images for inclusion into non-LaTeX output."
  3375. :group 'org-latex
  3376. :group 'org-export-latex
  3377. :set 'org-set-packages-alist
  3378. :get 'org-get-packages-alist
  3379. :version "24.1"
  3380. :type '(repeat
  3381. (choice
  3382. (list :tag "options/package pair"
  3383. (string :tag "options")
  3384. (string :tag "package")
  3385. (boolean :tag "Snippet"))
  3386. (string :tag "A line of LaTeX"))))
  3387. (defcustom org-latex-packages-alist nil
  3388. "Alist of packages to be inserted in every LaTeX header.
  3389. These will be inserted after `org-latex-default-packages-alist'.
  3390. Each cell is of the format:
  3391. \(\"options\" \"package\" snippet-flag)
  3392. SNIPPET-FLAG, when t, indicates that this package is also needed
  3393. when turning LaTeX snippets into images for inclusion into
  3394. non-LaTeX output.
  3395. Make sure that you only list packages here which:
  3396. - you want in every file
  3397. - do not conflict with the setup in `org-format-latex-header'.
  3398. - do not conflict with the default packages in
  3399. `org-latex-default-packages-alist'."
  3400. :group 'org-latex
  3401. :group 'org-export-latex
  3402. :set 'org-set-packages-alist
  3403. :get 'org-get-packages-alist
  3404. :type '(repeat
  3405. (choice
  3406. (list :tag "options/package pair"
  3407. (string :tag "options")
  3408. (string :tag "package")
  3409. (boolean :tag "Snippet"))
  3410. (string :tag "A line of LaTeX"))))
  3411. (defgroup org-appearance nil
  3412. "Settings for Org-mode appearance."
  3413. :tag "Org Appearance"
  3414. :group 'org)
  3415. (defcustom org-level-color-stars-only nil
  3416. "Non-nil means fontify only the stars in each headline.
  3417. When nil, the entire headline is fontified.
  3418. Changing it requires restart of `font-lock-mode' to become effective
  3419. also in regions already fontified."
  3420. :group 'org-appearance
  3421. :type 'boolean)
  3422. (defcustom org-hide-leading-stars nil
  3423. "Non-nil means hide the first N-1 stars in a headline.
  3424. This works by using the face `org-hide' for these stars. This
  3425. face is white for a light background, and black for a dark
  3426. background. You may have to customize the face `org-hide' to
  3427. make this work.
  3428. Changing it requires restart of `font-lock-mode' to become effective
  3429. also in regions already fontified.
  3430. You may also set this on a per-file basis by adding one of the following
  3431. lines to the buffer:
  3432. #+STARTUP: hidestars
  3433. #+STARTUP: showstars"
  3434. :group 'org-appearance
  3435. :type 'boolean)
  3436. (defcustom org-hidden-keywords nil
  3437. "List of symbols corresponding to keywords to be hidden the org buffer.
  3438. For example, a value '(title) for this list will make the document's title
  3439. appear in the buffer without the initial #+TITLE: keyword."
  3440. :group 'org-appearance
  3441. :version "24.1"
  3442. :type '(set (const :tag "#+AUTHOR" author)
  3443. (const :tag "#+DATE" date)
  3444. (const :tag "#+EMAIL" email)
  3445. (const :tag "#+TITLE" title)))
  3446. (defcustom org-custom-properties nil
  3447. "List of properties (as strings) with a special meaning.
  3448. The default use of these custom properties is to let the user
  3449. hide them with `org-toggle-custom-properties-visibility'."
  3450. :group 'org-properties
  3451. :group 'org-appearance
  3452. :version "24.3"
  3453. :type '(repeat (string :tag "Property Name")))
  3454. (defcustom org-fontify-done-headline nil
  3455. "Non-nil means change the face of a headline if it is marked DONE.
  3456. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3457. When this is non-nil, the headline after the keyword is set to the
  3458. `org-headline-done' as an additional indication."
  3459. :group 'org-appearance
  3460. :type 'boolean)
  3461. (defcustom org-fontify-emphasized-text t
  3462. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3463. Changing this variable requires a restart of Emacs to take effect."
  3464. :group 'org-appearance
  3465. :type 'boolean)
  3466. (defcustom org-fontify-whole-heading-line nil
  3467. "Non-nil means fontify the whole line for headings.
  3468. This is useful when setting a background color for the
  3469. org-level-* faces."
  3470. :group 'org-appearance
  3471. :type 'boolean)
  3472. (defcustom org-highlight-latex-and-related nil
  3473. "Non-nil means highlight LaTeX related syntax in the buffer.
  3474. When non nil, the value should be a list containing any of the
  3475. following symbols:
  3476. `latex' Highlight LaTeX snippets and environments.
  3477. `script' Highlight subscript and superscript.
  3478. `entities' Highlight entities."
  3479. :group 'org-appearance
  3480. :version "24.4"
  3481. :package-version '(Org . "8.0")
  3482. :type '(choice
  3483. (const :tag "No highlighting" nil)
  3484. (set :greedy t :tag "Highlight"
  3485. (const :tag "LaTeX snippets and environments" latex)
  3486. (const :tag "Subscript and superscript" script)
  3487. (const :tag "Entities" entities))))
  3488. (defcustom org-hide-emphasis-markers nil
  3489. "Non-nil mean font-lock should hide the emphasis marker characters."
  3490. :group 'org-appearance
  3491. :type 'boolean)
  3492. (defcustom org-pretty-entities nil
  3493. "Non-nil means show entities as UTF8 characters.
  3494. When nil, the \\name form remains in the buffer."
  3495. :group 'org-appearance
  3496. :version "24.1"
  3497. :type 'boolean)
  3498. (defcustom org-pretty-entities-include-sub-superscripts t
  3499. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3500. :group 'org-appearance
  3501. :version "24.1"
  3502. :type 'boolean)
  3503. (defvar org-emph-re nil
  3504. "Regular expression for matching emphasis.
  3505. After a match, the match groups contain these elements:
  3506. 0 The match of the full regular expression, including the characters
  3507. before and after the proper match
  3508. 1 The character before the proper match, or empty at beginning of line
  3509. 2 The proper match, including the leading and trailing markers
  3510. 3 The leading marker like * or /, indicating the type of highlighting
  3511. 4 The text between the emphasis markers, not including the markers
  3512. 5 The character after the match, empty at the end of a line")
  3513. (defvar org-verbatim-re nil
  3514. "Regular expression for matching verbatim text.")
  3515. (defvar org-emphasis-regexp-components) ; defined just below
  3516. (defvar org-emphasis-alist) ; defined just below
  3517. (defun org-set-emph-re (var val)
  3518. "Set variable and compute the emphasis regular expression."
  3519. (set var val)
  3520. (when (and (boundp 'org-emphasis-alist)
  3521. (boundp 'org-emphasis-regexp-components)
  3522. org-emphasis-alist org-emphasis-regexp-components)
  3523. (let* ((e org-emphasis-regexp-components)
  3524. (pre (car e))
  3525. (post (nth 1 e))
  3526. (border (nth 2 e))
  3527. (body (nth 3 e))
  3528. (nl (nth 4 e))
  3529. (body1 (concat body "*?"))
  3530. (markers (mapconcat 'car org-emphasis-alist ""))
  3531. (vmarkers (mapconcat
  3532. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3533. org-emphasis-alist "")))
  3534. ;; make sure special characters appear at the right position in the class
  3535. (if (string-match "\\^" markers)
  3536. (setq markers (concat (replace-match "" t t markers) "^")))
  3537. (if (string-match "-" markers)
  3538. (setq markers (concat (replace-match "" t t markers) "-")))
  3539. (if (string-match "\\^" vmarkers)
  3540. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3541. (if (string-match "-" vmarkers)
  3542. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3543. (if (> nl 0)
  3544. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3545. (int-to-string nl) "\\}")))
  3546. ;; Make the regexp
  3547. (setq org-emph-re
  3548. (concat "\\([" pre "]\\|^\\)"
  3549. "\\("
  3550. "\\([" markers "]\\)"
  3551. "\\("
  3552. "[^" border "]\\|"
  3553. "[^" border "]"
  3554. body1
  3555. "[^" border "]"
  3556. "\\)"
  3557. "\\3\\)"
  3558. "\\([" post "]\\|$\\)"))
  3559. (setq org-verbatim-re
  3560. (concat "\\([" pre "]\\|^\\)"
  3561. "\\("
  3562. "\\([" vmarkers "]\\)"
  3563. "\\("
  3564. "[^" border "]\\|"
  3565. "[^" border "]"
  3566. body1
  3567. "[^" border "]"
  3568. "\\)"
  3569. "\\3\\)"
  3570. "\\([" post "]\\|$\\)")))))
  3571. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3572. ;; set this option proved cumbersome. See this message/thread:
  3573. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3574. (defvar org-emphasis-regexp-components
  3575. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3576. "Components used to build the regular expression for emphasis.
  3577. This is a list with five entries. Terminology: In an emphasis string
  3578. like \" *strong word* \", we call the initial space PREMATCH, the final
  3579. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3580. and \"trong wor\" is the body. The different components in this variable
  3581. specify what is allowed/forbidden in each part:
  3582. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3583. post Chars allowed as postmatch. End of line will be allowed too.
  3584. border The chars *forbidden* as border characters.
  3585. body-regexp A regexp like \".\" to match a body character. Don't use
  3586. non-shy groups here, and don't allow newline here.
  3587. newline The maximum number of newlines allowed in an emphasis exp.
  3588. You need to reload Org or to restart Emacs after customizing this.")
  3589. (defcustom org-emphasis-alist
  3590. `(("*" bold)
  3591. ("/" italic)
  3592. ("_" underline)
  3593. ("=" org-code verbatim)
  3594. ("~" org-verbatim verbatim)
  3595. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3596. "Alist of characters and faces to emphasize text.
  3597. Text starting and ending with a special character will be emphasized,
  3598. for example *bold*, _underlined_ and /italic/. This variable sets the
  3599. marker characters and the face to be used by font-lock for highlighting
  3600. in Org-mode Emacs buffers.
  3601. You need to reload Org or to restart Emacs after customizing this."
  3602. :group 'org-appearance
  3603. :set 'org-set-emph-re
  3604. :version "24.4"
  3605. :package-version '(Org . "8.0")
  3606. :type '(repeat
  3607. (list
  3608. (string :tag "Marker character")
  3609. (choice
  3610. (face :tag "Font-lock-face")
  3611. (plist :tag "Face property list"))
  3612. (option (const verbatim)))))
  3613. (defvar org-protecting-blocks
  3614. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3615. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3616. This is needed for font-lock setup.")
  3617. ;;; Miscellaneous options
  3618. (defgroup org-completion nil
  3619. "Completion in Org-mode."
  3620. :tag "Org Completion"
  3621. :group 'org)
  3622. (defcustom org-completion-use-ido nil
  3623. "Non-nil means use ido completion wherever possible.
  3624. Note that `ido-mode' must be active for this variable to be relevant.
  3625. If you decide to turn this variable on, you might well want to turn off
  3626. `org-outline-path-complete-in-steps'.
  3627. See also `org-completion-use-iswitchb'."
  3628. :group 'org-completion
  3629. :type 'boolean)
  3630. (defcustom org-completion-use-iswitchb nil
  3631. "Non-nil means use iswitchb completion wherever possible.
  3632. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3633. If you decide to turn this variable on, you might well want to turn off
  3634. `org-outline-path-complete-in-steps'.
  3635. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3636. :group 'org-completion
  3637. :type 'boolean)
  3638. (defcustom org-completion-fallback-command 'hippie-expand
  3639. "The expansion command called by \\[pcomplete] in normal context.
  3640. Normal means, no org-mode-specific context."
  3641. :group 'org-completion
  3642. :type 'function)
  3643. ;;; Functions and variables from their packages
  3644. ;; Declared here to avoid compiler warnings
  3645. ;; XEmacs only
  3646. (defvar outline-mode-menu-heading)
  3647. (defvar outline-mode-menu-show)
  3648. (defvar outline-mode-menu-hide)
  3649. (defvar zmacs-regions) ; XEmacs regions
  3650. ;; Emacs only
  3651. (defvar mark-active)
  3652. ;; Various packages
  3653. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3654. (declare-function calendar-forward-day "cal-move" (arg))
  3655. (declare-function calendar-goto-date "cal-move" (date))
  3656. (declare-function calendar-goto-today "cal-move" ())
  3657. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3658. (defvar calc-embedded-close-formula)
  3659. (defvar calc-embedded-open-formula)
  3660. (declare-function cdlatex-tab "ext:cdlatex" ())
  3661. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3662. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3663. (defvar font-lock-unfontify-region-function)
  3664. (declare-function iswitchb-read-buffer "iswitchb"
  3665. (prompt &optional default require-match start matches-set))
  3666. (defvar iswitchb-temp-buflist)
  3667. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3668. (defvar org-agenda-tags-todo-honor-ignore-options)
  3669. (declare-function org-agenda-skip "org-agenda" ())
  3670. (declare-function
  3671. org-agenda-format-item "org-agenda"
  3672. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3673. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3674. (declare-function org-agenda-change-all-lines "org-agenda"
  3675. (newhead hdmarker &optional fixface just-this))
  3676. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3677. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3678. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3679. (beg end))
  3680. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3681. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3682. "org-agenda" (&optional end))
  3683. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3684. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3685. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3686. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3687. (declare-function org-indent-mode "org-indent" (&optional arg))
  3688. (declare-function parse-time-string "parse-time" (string))
  3689. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3690. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3691. (defvar remember-data-file)
  3692. (defvar texmathp-why)
  3693. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3694. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3695. (defvar org-latex-regexps)
  3696. ;;; Autoload and prepare some org modules
  3697. ;; Some table stuff that needs to be defined here, because it is used
  3698. ;; by the functions setting up org-mode or checking for table context.
  3699. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3700. "Detect an org-type or table-type table.")
  3701. (defconst org-table-line-regexp "^[ \t]*|"
  3702. "Detect an org-type table line.")
  3703. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3704. "Detect an org-type table line.")
  3705. (defconst org-table-hline-regexp "^[ \t]*|-"
  3706. "Detect an org-type table hline.")
  3707. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3708. "Detect a table-type table hline.")
  3709. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3710. "Detect the first line outside a table when searching from within it.
  3711. This works for both table types.")
  3712. ;; Autoload the functions in org-table.el that are needed by functions here.
  3713. (eval-and-compile
  3714. (org-autoload "org-table"
  3715. '(org-table-begin org-table-blank-field org-table-end)))
  3716. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3717. "Detect a #+TBLFM line.")
  3718. ;;;###autoload
  3719. (defun turn-on-orgtbl ()
  3720. "Unconditionally turn on `orgtbl-mode'."
  3721. (require 'org-table)
  3722. (orgtbl-mode 1))
  3723. (defun org-at-table-p (&optional table-type)
  3724. "Return t if the cursor is inside an org-type table.
  3725. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3726. (if org-enable-table-editor
  3727. (save-excursion
  3728. (beginning-of-line 1)
  3729. (looking-at (if table-type org-table-any-line-regexp
  3730. org-table-line-regexp)))
  3731. nil))
  3732. (defsubst org-table-p () (org-at-table-p))
  3733. (defun org-at-table.el-p ()
  3734. "Return t if and only if we are at a table.el table."
  3735. (and (org-at-table-p 'any)
  3736. (save-excursion
  3737. (goto-char (org-table-begin 'any))
  3738. (looking-at org-table1-hline-regexp))))
  3739. (defun org-table-recognize-table.el ()
  3740. "If there is a table.el table nearby, recognize it and move into it."
  3741. (if org-table-tab-recognizes-table.el
  3742. (if (org-at-table.el-p)
  3743. (progn
  3744. (beginning-of-line 1)
  3745. (if (looking-at org-table-dataline-regexp)
  3746. nil
  3747. (if (looking-at org-table1-hline-regexp)
  3748. (progn
  3749. (beginning-of-line 2)
  3750. (if (looking-at org-table-any-border-regexp)
  3751. (beginning-of-line -1)))))
  3752. (if (re-search-forward "|" (org-table-end t) t)
  3753. (progn
  3754. (require 'table)
  3755. (if (table--at-cell-p (point))
  3756. t
  3757. (message "recognizing table.el table...")
  3758. (table-recognize-table)
  3759. (message "recognizing table.el table...done")))
  3760. (error "This should not happen"))
  3761. t)
  3762. nil)
  3763. nil))
  3764. (defun org-at-table-hline-p ()
  3765. "Return t if the cursor is inside a hline in a table."
  3766. (if org-enable-table-editor
  3767. (save-excursion
  3768. (beginning-of-line 1)
  3769. (looking-at org-table-hline-regexp))
  3770. nil))
  3771. (defun org-table-map-tables (function &optional quietly)
  3772. "Apply FUNCTION to the start of all tables in the buffer."
  3773. (save-excursion
  3774. (save-restriction
  3775. (widen)
  3776. (goto-char (point-min))
  3777. (while (re-search-forward org-table-any-line-regexp nil t)
  3778. (unless quietly
  3779. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3780. (beginning-of-line 1)
  3781. (when (and (looking-at org-table-line-regexp)
  3782. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3783. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3784. (save-excursion (funcall function))
  3785. (or (looking-at org-table-line-regexp)
  3786. (forward-char 1)))
  3787. (re-search-forward org-table-any-border-regexp nil 1))))
  3788. (unless quietly (message "Mapping tables: done")))
  3789. ;; Declare and autoload functions from org-agenda.el
  3790. (eval-and-compile
  3791. (org-autoload "org-agenda"
  3792. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3793. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3794. (declare-function org-clock-update-mode-line "org-clock" ())
  3795. (declare-function org-resolve-clocks "org-clock"
  3796. (&optional also-non-dangling-p prompt last-valid))
  3797. (defun org-at-TBLFM-p (&optional pos)
  3798. "Return t when point (or POS) is in #+TBLFM line."
  3799. (save-excursion
  3800. (let ((pos pos)))
  3801. (goto-char (or pos (point)))
  3802. (beginning-of-line 1)
  3803. (looking-at org-TBLFM-regexp)))
  3804. (defvar org-clock-start-time)
  3805. (defvar org-clock-marker (make-marker)
  3806. "Marker recording the last clock-in.")
  3807. (defvar org-clock-hd-marker (make-marker)
  3808. "Marker recording the last clock-in, but the headline position.")
  3809. (defvar org-clock-heading ""
  3810. "The heading of the current clock entry.")
  3811. (defun org-clock-is-active ()
  3812. "Return the buffer where the clock is currently running.
  3813. Return nil if no clock is running."
  3814. (marker-buffer org-clock-marker))
  3815. (eval-and-compile
  3816. (org-autoload "org-clock" '(org-clock-remove-overlays
  3817. org-clock-update-time-maybe
  3818. org-clocktable-shift)))
  3819. (defun org-check-running-clock ()
  3820. "Check if the current buffer contains the running clock.
  3821. If yes, offer to stop it and to save the buffer with the changes."
  3822. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3823. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3824. (buffer-name))))
  3825. (org-clock-out)
  3826. (when (y-or-n-p "Save changed buffer?")
  3827. (save-buffer))))
  3828. (defun org-clocktable-try-shift (dir n)
  3829. "Check if this line starts a clock table, if yes, shift the time block."
  3830. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3831. (org-clocktable-shift dir n)))
  3832. ;;;###autoload
  3833. (defun org-clock-persistence-insinuate ()
  3834. "Set up hooks for clock persistence."
  3835. (require 'org-clock)
  3836. (add-hook 'org-mode-hook 'org-clock-load)
  3837. (add-hook 'kill-emacs-hook 'org-clock-save))
  3838. ;; Define the variable already here, to make sure we have it.
  3839. (defvar org-indent-mode nil
  3840. "Non-nil if Org-Indent mode is enabled.
  3841. Use the command `org-indent-mode' to change this variable.")
  3842. ;; Autoload archiving code
  3843. ;; The stuff that is needed for cycling and tags has to be defined here.
  3844. (defgroup org-archive nil
  3845. "Options concerning archiving in Org-mode."
  3846. :tag "Org Archive"
  3847. :group 'org-structure)
  3848. (defcustom org-archive-location "%s_archive::"
  3849. "The location where subtrees should be archived.
  3850. The value of this variable is a string, consisting of two parts,
  3851. separated by a double-colon. The first part is a filename and
  3852. the second part is a headline.
  3853. When the filename is omitted, archiving happens in the same file.
  3854. %s in the filename will be replaced by the current file
  3855. name (without the directory part). Archiving to a different file
  3856. is useful to keep archived entries from contributing to the
  3857. Org-mode Agenda.
  3858. The archived entries will be filed as subtrees of the specified
  3859. headline. When the headline is omitted, the subtrees are simply
  3860. filed away at the end of the file, as top-level entries. Also in
  3861. the heading you can use %s to represent the file name, this can be
  3862. useful when using the same archive for a number of different files.
  3863. Here are a few examples:
  3864. \"%s_archive::\"
  3865. If the current file is Projects.org, archive in file
  3866. Projects.org_archive, as top-level trees. This is the default.
  3867. \"::* Archived Tasks\"
  3868. Archive in the current file, under the top-level headline
  3869. \"* Archived Tasks\".
  3870. \"~/org/archive.org::\"
  3871. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3872. \"~/org/archive.org::* From %s\"
  3873. Archive in file ~/org/archive.org (absolute path), under headlines
  3874. \"From FILENAME\" where file name is the current file name.
  3875. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3876. The \"datetree/\" string is special, signifying to archive
  3877. items to the datetree. Items are placed in either the CLOSED
  3878. date of the item, or the current date if there is no CLOSED date.
  3879. The heading will be a subentry to the current date. There doesn't
  3880. need to be a heading, but there always needs to be a slash after
  3881. datetree. For example, to store archived items directly in the
  3882. datetree, use \"~/org/datetree.org::datetree/\".
  3883. \"basement::** Finished Tasks\"
  3884. Archive in file ./basement (relative path), as level 3 trees
  3885. below the level 2 heading \"** Finished Tasks\".
  3886. You may set this option on a per-file basis by adding to the buffer a
  3887. line like
  3888. #+ARCHIVE: basement::** Finished Tasks
  3889. You may also define it locally for a subtree by setting an ARCHIVE property
  3890. in the entry. If such a property is found in an entry, or anywhere up
  3891. the hierarchy, it will be used."
  3892. :group 'org-archive
  3893. :type 'string)
  3894. (defcustom org-archive-tag "ARCHIVE"
  3895. "The tag that marks a subtree as archived.
  3896. An archived subtree does not open during visibility cycling, and does
  3897. not contribute to the agenda listings.
  3898. After changing this, font-lock must be restarted in the relevant buffers to
  3899. get the proper fontification."
  3900. :group 'org-archive
  3901. :group 'org-keywords
  3902. :type 'string)
  3903. (defcustom org-agenda-skip-archived-trees t
  3904. "Non-nil means the agenda will skip any items located in archived trees.
  3905. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3906. variable is no longer recommended, you should leave it at the value t.
  3907. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3908. :group 'org-archive
  3909. :group 'org-agenda-skip
  3910. :type 'boolean)
  3911. (defcustom org-columns-skip-archived-trees t
  3912. "Non-nil means ignore archived trees when creating column view."
  3913. :group 'org-archive
  3914. :group 'org-properties
  3915. :type 'boolean)
  3916. (defcustom org-cycle-open-archived-trees nil
  3917. "Non-nil means `org-cycle' will open archived trees.
  3918. An archived tree is a tree marked with the tag ARCHIVE.
  3919. When nil, archived trees will stay folded. You can still open them with
  3920. normal outline commands like `show-all', but not with the cycling commands."
  3921. :group 'org-archive
  3922. :group 'org-cycle
  3923. :type 'boolean)
  3924. (defcustom org-sparse-tree-open-archived-trees nil
  3925. "Non-nil means sparse tree construction shows matches in archived trees.
  3926. When nil, matches in these trees are highlighted, but the trees are kept in
  3927. collapsed state."
  3928. :group 'org-archive
  3929. :group 'org-sparse-trees
  3930. :type 'boolean)
  3931. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3932. "The default date type when building a sparse tree.
  3933. When this is nil, a date is a scheduled or a deadline timestamp.
  3934. Otherwise, these types are allowed:
  3935. all: all timestamps
  3936. active: only active timestamps (<...>)
  3937. inactive: only inactive timestamps (<...)
  3938. scheduled: only scheduled timestamps
  3939. deadline: only deadline timestamps"
  3940. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3941. (const :tag "All timestamps" all)
  3942. (const :tag "Only active timestamps" active)
  3943. (const :tag "Only inactive timestamps" inactive)
  3944. (const :tag "Only scheduled timestamps" scheduled)
  3945. (const :tag "Only deadline timestamps" deadline)
  3946. (const :tag "Only closed timestamps" closed))
  3947. :version "24.3"
  3948. :group 'org-sparse-trees)
  3949. (defun org-cycle-hide-archived-subtrees (state)
  3950. "Re-hide all archived subtrees after a visibility state change."
  3951. (when (and (not org-cycle-open-archived-trees)
  3952. (not (memq state '(overview folded))))
  3953. (save-excursion
  3954. (let* ((globalp (memq state '(contents all)))
  3955. (beg (if globalp (point-min) (point)))
  3956. (end (if globalp (point-max) (org-end-of-subtree t))))
  3957. (org-hide-archived-subtrees beg end)
  3958. (goto-char beg)
  3959. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3960. (message "%s" (substitute-command-keys
  3961. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3962. (defun org-force-cycle-archived ()
  3963. "Cycle subtree even if it is archived."
  3964. (interactive)
  3965. (setq this-command 'org-cycle)
  3966. (let ((org-cycle-open-archived-trees t))
  3967. (call-interactively 'org-cycle)))
  3968. (defun org-hide-archived-subtrees (beg end)
  3969. "Re-hide all archived subtrees after a visibility state change."
  3970. (save-excursion
  3971. (let* ((re (concat ":" org-archive-tag ":")))
  3972. (goto-char beg)
  3973. (while (re-search-forward re end t)
  3974. (when (org-at-heading-p)
  3975. (org-flag-subtree t)
  3976. (org-end-of-subtree t))))))
  3977. (declare-function outline-end-of-heading "outline" ())
  3978. (declare-function outline-flag-region "outline" (from to flag))
  3979. (defun org-flag-subtree (flag)
  3980. (save-excursion
  3981. (org-back-to-heading t)
  3982. (outline-end-of-heading)
  3983. (outline-flag-region (point)
  3984. (progn (org-end-of-subtree t) (point))
  3985. flag)))
  3986. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3987. (eval-and-compile
  3988. (org-autoload "org-archive"
  3989. '(org-add-archive-files)))
  3990. ;; Autoload Column View Code
  3991. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3992. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3993. (declare-function org-columns-compute "org-colview" (property))
  3994. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3995. '(org-columns-number-to-string
  3996. org-columns-get-format-and-top-level
  3997. org-columns-compute
  3998. org-columns-remove-overlays))
  3999. ;; Autoload ID code
  4000. (declare-function org-id-store-link "org-id")
  4001. (declare-function org-id-locations-load "org-id")
  4002. (declare-function org-id-locations-save "org-id")
  4003. (defvar org-id-track-globally)
  4004. (org-autoload "org-id"
  4005. '(org-id-new
  4006. org-id-copy
  4007. org-id-get-with-outline-path-completion
  4008. org-id-get-with-outline-drilling))
  4009. ;;; Variables for pre-computed regular expressions, all buffer local
  4010. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  4011. "Matches first line of a hidden block.")
  4012. (make-variable-buffer-local 'org-drawer-regexp)
  4013. (defvar org-todo-regexp nil
  4014. "Matches any of the TODO state keywords.")
  4015. (make-variable-buffer-local 'org-todo-regexp)
  4016. (defvar org-not-done-regexp nil
  4017. "Matches any of the TODO state keywords except the last one.")
  4018. (make-variable-buffer-local 'org-not-done-regexp)
  4019. (defvar org-not-done-heading-regexp nil
  4020. "Matches a TODO headline that is not done.")
  4021. (make-variable-buffer-local 'org-not-done-regexp)
  4022. (defvar org-todo-line-regexp nil
  4023. "Matches a headline and puts TODO state into group 2 if present.")
  4024. (make-variable-buffer-local 'org-todo-line-regexp)
  4025. (defvar org-complex-heading-regexp nil
  4026. "Matches a headline and puts everything into groups:
  4027. group 1: the stars
  4028. group 2: The todo keyword, maybe
  4029. group 3: Priority cookie
  4030. group 4: True headline
  4031. group 5: Tags")
  4032. (make-variable-buffer-local 'org-complex-heading-regexp)
  4033. (defvar org-complex-heading-regexp-format nil
  4034. "Printf format to make regexp to match an exact headline.
  4035. This regexp will match the headline of any node which has the
  4036. exact headline text that is put into the format, but may have any
  4037. TODO state, priority and tags.")
  4038. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4039. (defvar org-todo-line-tags-regexp nil
  4040. "Matches a headline and puts TODO state into group 2 if present.
  4041. Also put tags into group 4 if tags are present.")
  4042. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4043. (defvar org-ds-keyword-length 12
  4044. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4045. (make-variable-buffer-local 'org-ds-keyword-length)
  4046. (defvar org-deadline-regexp nil
  4047. "Matches the DEADLINE keyword.")
  4048. (make-variable-buffer-local 'org-deadline-regexp)
  4049. (defvar org-deadline-time-regexp nil
  4050. "Matches the DEADLINE keyword together with a time stamp.")
  4051. (make-variable-buffer-local 'org-deadline-time-regexp)
  4052. (defvar org-deadline-time-hour-regexp nil
  4053. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4054. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4055. (defvar org-deadline-line-regexp nil
  4056. "Matches the DEADLINE keyword and the rest of the line.")
  4057. (make-variable-buffer-local 'org-deadline-line-regexp)
  4058. (defvar org-scheduled-regexp nil
  4059. "Matches the SCHEDULED keyword.")
  4060. (make-variable-buffer-local 'org-scheduled-regexp)
  4061. (defvar org-scheduled-time-regexp nil
  4062. "Matches the SCHEDULED keyword together with a time stamp.")
  4063. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4064. (defvar org-scheduled-time-hour-regexp nil
  4065. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4066. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4067. (defvar org-closed-time-regexp nil
  4068. "Matches the CLOSED keyword together with a time stamp.")
  4069. (make-variable-buffer-local 'org-closed-time-regexp)
  4070. (defvar org-keyword-time-regexp nil
  4071. "Matches any of the 4 keywords, together with the time stamp.")
  4072. (make-variable-buffer-local 'org-keyword-time-regexp)
  4073. (defvar org-keyword-time-not-clock-regexp nil
  4074. "Matches any of the 3 keywords, together with the time stamp.")
  4075. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4076. (defvar org-maybe-keyword-time-regexp nil
  4077. "Matches a timestamp, possibly preceded by a keyword.")
  4078. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4079. (defvar org-all-time-keywords nil
  4080. "List of time keywords.")
  4081. (make-variable-buffer-local 'org-all-time-keywords)
  4082. (defconst org-plain-time-of-day-regexp
  4083. (concat
  4084. "\\(\\<[012]?[0-9]"
  4085. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4086. "\\(--?"
  4087. "\\(\\<[012]?[0-9]"
  4088. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4089. "\\)?")
  4090. "Regular expression to match a plain time or time range.
  4091. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4092. groups carry important information:
  4093. 0 the full match
  4094. 1 the first time, range or not
  4095. 8 the second time, if it is a range.")
  4096. (defconst org-plain-time-extension-regexp
  4097. (concat
  4098. "\\(\\<[012]?[0-9]"
  4099. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4100. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4101. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4102. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4103. groups carry important information:
  4104. 0 the full match
  4105. 7 hours of duration
  4106. 9 minutes of duration")
  4107. (defconst org-stamp-time-of-day-regexp
  4108. (concat
  4109. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4110. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4111. "\\(--?"
  4112. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4113. "Regular expression to match a timestamp time or time range.
  4114. After a match, the following groups carry important information:
  4115. 0 the full match
  4116. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4117. 2 the first time, range or not
  4118. 4 the second time, if it is a range.")
  4119. (defconst org-startup-options
  4120. '(("fold" org-startup-folded t)
  4121. ("overview" org-startup-folded t)
  4122. ("nofold" org-startup-folded nil)
  4123. ("showall" org-startup-folded nil)
  4124. ("showeverything" org-startup-folded showeverything)
  4125. ("content" org-startup-folded content)
  4126. ("indent" org-startup-indented t)
  4127. ("noindent" org-startup-indented nil)
  4128. ("hidestars" org-hide-leading-stars t)
  4129. ("showstars" org-hide-leading-stars nil)
  4130. ("odd" org-odd-levels-only t)
  4131. ("oddeven" org-odd-levels-only nil)
  4132. ("align" org-startup-align-all-tables t)
  4133. ("noalign" org-startup-align-all-tables nil)
  4134. ("inlineimages" org-startup-with-inline-images t)
  4135. ("noinlineimages" org-startup-with-inline-images nil)
  4136. ("latexpreview" org-startup-with-latex-preview t)
  4137. ("nolatexpreview" org-startup-with-latex-preview nil)
  4138. ("customtime" org-display-custom-times t)
  4139. ("logdone" org-log-done time)
  4140. ("lognotedone" org-log-done note)
  4141. ("nologdone" org-log-done nil)
  4142. ("lognoteclock-out" org-log-note-clock-out t)
  4143. ("nolognoteclock-out" org-log-note-clock-out nil)
  4144. ("logrepeat" org-log-repeat state)
  4145. ("lognoterepeat" org-log-repeat note)
  4146. ("logdrawer" org-log-into-drawer t)
  4147. ("nologdrawer" org-log-into-drawer nil)
  4148. ("logstatesreversed" org-log-states-order-reversed t)
  4149. ("nologstatesreversed" org-log-states-order-reversed nil)
  4150. ("nologrepeat" org-log-repeat nil)
  4151. ("logreschedule" org-log-reschedule time)
  4152. ("lognotereschedule" org-log-reschedule note)
  4153. ("nologreschedule" org-log-reschedule nil)
  4154. ("logredeadline" org-log-redeadline time)
  4155. ("lognoteredeadline" org-log-redeadline note)
  4156. ("nologredeadline" org-log-redeadline nil)
  4157. ("logrefile" org-log-refile time)
  4158. ("lognoterefile" org-log-refile note)
  4159. ("nologrefile" org-log-refile nil)
  4160. ("fninline" org-footnote-define-inline t)
  4161. ("nofninline" org-footnote-define-inline nil)
  4162. ("fnlocal" org-footnote-section nil)
  4163. ("fnauto" org-footnote-auto-label t)
  4164. ("fnprompt" org-footnote-auto-label nil)
  4165. ("fnconfirm" org-footnote-auto-label confirm)
  4166. ("fnplain" org-footnote-auto-label plain)
  4167. ("fnadjust" org-footnote-auto-adjust t)
  4168. ("nofnadjust" org-footnote-auto-adjust nil)
  4169. ("constcgs" constants-unit-system cgs)
  4170. ("constSI" constants-unit-system SI)
  4171. ("noptag" org-tag-persistent-alist nil)
  4172. ("hideblocks" org-hide-block-startup t)
  4173. ("nohideblocks" org-hide-block-startup nil)
  4174. ("beamer" org-startup-with-beamer-mode t)
  4175. ("entitiespretty" org-pretty-entities t)
  4176. ("entitiesplain" org-pretty-entities nil))
  4177. "Variable associated with STARTUP options for org-mode.
  4178. Each element is a list of three items: the startup options (as written
  4179. in the #+STARTUP line), the corresponding variable, and the value to set
  4180. this variable to if the option is found. An optional forth element PUSH
  4181. means to push this value onto the list in the variable.")
  4182. (defun org-update-property-plist (key val props)
  4183. "Update PROPS with KEY and VAL."
  4184. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4185. (key (if appending (substring key 0 (- (length key) 1)) key))
  4186. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4187. (previous (cdr (assoc key props))))
  4188. (if appending
  4189. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4190. (cons (cons key val) remainder))))
  4191. (defconst org-block-regexp
  4192. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4193. "Regular expression for hiding blocks.")
  4194. (defconst org-heading-keyword-regexp-format
  4195. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4196. "Printf format for a regexp matching a headline with some keyword.
  4197. This regexp will match the headline of any node which has the
  4198. exact keyword that is put into the format. The keyword isn't in
  4199. any group by default, but the stars and the body are.")
  4200. (defconst org-heading-keyword-maybe-regexp-format
  4201. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4202. "Printf format for a regexp matching a headline, possibly with some keyword.
  4203. This regexp can match any headline with the specified keyword, or
  4204. without a keyword. The keyword isn't in any group by default,
  4205. but the stars and the body are.")
  4206. (defcustom org-group-tags t
  4207. "When non-nil (the default), use group tags.
  4208. This can be turned on/off through `org-toggle-tags-groups'."
  4209. :group 'org-tags
  4210. :group 'org-startup
  4211. :type 'boolean)
  4212. (defun org-toggle-tags-groups ()
  4213. "Toggle support for group tags.
  4214. Support for group tags is controlled by the option
  4215. `org-group-tags', which is non-nil by default."
  4216. (interactive)
  4217. (setq org-group-tags (not org-group-tags))
  4218. (cond ((and (derived-mode-p 'org-agenda-mode)
  4219. org-group-tags)
  4220. (org-agenda-redo))
  4221. ((derived-mode-p 'org-mode)
  4222. (let ((org-inhibit-startup t)) (org-mode))))
  4223. (message "Groups tags support has been turned %s"
  4224. (if org-group-tags "on" "off")))
  4225. (defun org-set-regexps-and-options-for-tags ()
  4226. "Precompute variables used for tags."
  4227. (when (derived-mode-p 'org-mode)
  4228. (org-set-local 'org-file-tags nil)
  4229. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4230. (splitre "[ \t]+")
  4231. (start 0)
  4232. tags ftags key value)
  4233. (save-excursion
  4234. (save-restriction
  4235. (widen)
  4236. (goto-char (point-min))
  4237. (while (re-search-forward re nil t)
  4238. (setq key (upcase (org-match-string-no-properties 1))
  4239. value (org-match-string-no-properties 2))
  4240. (if (stringp value) (setq value (org-trim value)))
  4241. (cond
  4242. ((equal key "TAGS")
  4243. (setq tags (append tags (if tags '("\\n") nil)
  4244. (org-split-string value splitre))))
  4245. ((equal key "FILETAGS")
  4246. (when (string-match "\\S-" value)
  4247. (setq ftags
  4248. (append
  4249. ftags
  4250. (apply 'append
  4251. (mapcar (lambda (x) (org-split-string x ":"))
  4252. (org-split-string value)))))))))))
  4253. ;; Process the file tags.
  4254. (and ftags (org-set-local 'org-file-tags
  4255. (mapcar 'org-add-prop-inherited ftags)))
  4256. (org-set-local 'org-tag-groups-alist nil)
  4257. ;; Process the tags.
  4258. (when (and (not tags) org-tag-alist)
  4259. (setq tags
  4260. (mapcar
  4261. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4262. ((eq (car tg) :endgroup) "}")
  4263. ((eq (car tg) :grouptags) ":")
  4264. ((eq (car tg) :newline) "\n")
  4265. (t (concat (car tg)
  4266. (if (characterp (cdr tg))
  4267. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4268. org-tag-alist)))
  4269. (let (e tgs g)
  4270. (while (setq e (pop tags))
  4271. (cond
  4272. ((equal e "{")
  4273. (progn (push '(:startgroup) tgs)
  4274. (when (equal (nth 1 tags) ":")
  4275. (push (list (replace-regexp-in-string
  4276. "(.+)$" "" (nth 0 tags)))
  4277. org-tag-groups-alist)
  4278. (setq g 0))))
  4279. ((equal e ":") (push '(:grouptags) tgs))
  4280. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4281. ((equal e "\\n") (push '(:newline) tgs))
  4282. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4283. (push (cons (match-string 1 e)
  4284. (string-to-char (match-string 2 e))) tgs)
  4285. (if (and g (> g 0))
  4286. (setcar org-tag-groups-alist
  4287. (append (car org-tag-groups-alist)
  4288. (list (match-string 1 e)))))
  4289. (if g (setq g (1+ g))))
  4290. (t (push (list e) tgs)
  4291. (if (and g (> g 0))
  4292. (setcar org-tag-groups-alist
  4293. (append (car org-tag-groups-alist) (list e))))
  4294. (if g (setq g (1+ g))))))
  4295. (org-set-local 'org-tag-alist nil)
  4296. (while (setq e (pop tgs))
  4297. (or (and (stringp (car e))
  4298. (assoc (car e) org-tag-alist))
  4299. (push e org-tag-alist)))
  4300. ;; Return a list with tag variables
  4301. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4302. (defvar org-ota nil)
  4303. (defun org-set-regexps-and-options ()
  4304. "Precompute regular expressions used in the current buffer."
  4305. (when (derived-mode-p 'org-mode)
  4306. (org-set-local 'org-todo-kwd-alist nil)
  4307. (org-set-local 'org-todo-key-alist nil)
  4308. (org-set-local 'org-todo-key-trigger nil)
  4309. (org-set-local 'org-todo-keywords-1 nil)
  4310. (org-set-local 'org-done-keywords nil)
  4311. (org-set-local 'org-todo-heads nil)
  4312. (org-set-local 'org-todo-sets nil)
  4313. (org-set-local 'org-todo-log-states nil)
  4314. (org-set-local 'org-file-properties nil)
  4315. (let ((re (org-make-options-regexp
  4316. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4317. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4318. "SETUPFILE" "OPTIONS")
  4319. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4320. (splitre "[ \t]+")
  4321. (scripts org-use-sub-superscripts)
  4322. kwds kws0 kwsa key log value cat arch const links hw dws
  4323. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4324. (start 0))
  4325. (save-excursion
  4326. (save-restriction
  4327. (widen)
  4328. (goto-char (point-min))
  4329. (while
  4330. (or (and
  4331. ext-setup-or-nil
  4332. (not org-ota)
  4333. (let (ret)
  4334. (with-temp-buffer
  4335. (insert ext-setup-or-nil)
  4336. (let ((major-mode 'org-mode) org-ota)
  4337. (setq ret (save-match-data
  4338. (org-set-regexps-and-options-for-tags)))))
  4339. ;; Append setupfile tags to existing tags
  4340. (setq org-ota t)
  4341. (setq org-file-tags
  4342. (delq nil (append org-file-tags (nth 0 ret)))
  4343. org-tag-alist
  4344. (delq nil (append org-tag-alist (nth 1 ret)))
  4345. org-tag-groups-alist
  4346. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4347. (and ext-setup-or-nil
  4348. (string-match re ext-setup-or-nil start)
  4349. (setq start (match-end 0)))
  4350. (and (setq ext-setup-or-nil nil start 0)
  4351. (re-search-forward re nil t)))
  4352. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4353. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4354. (if (stringp value) (setq value (org-trim value)))
  4355. (cond
  4356. ((equal key "CATEGORY")
  4357. (setq cat value))
  4358. ((member key '("SEQ_TODO" "TODO"))
  4359. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4360. ((equal key "TYP_TODO")
  4361. (push (cons 'type (org-split-string value splitre)) kwds))
  4362. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4363. ;; general TODO-like setup
  4364. (push (cons (intern (downcase (match-string 1 key)))
  4365. (org-split-string value splitre)) kwds))
  4366. ((equal key "COLUMNS")
  4367. (org-set-local 'org-columns-default-format value))
  4368. ((equal key "LINK")
  4369. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4370. (push (cons (match-string 1 value)
  4371. (org-trim (match-string 2 value)))
  4372. links)))
  4373. ((equal key "PRIORITIES")
  4374. (setq prio (org-split-string value " +")))
  4375. ((equal key "PROPERTY")
  4376. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4377. (setq props (org-update-property-plist (match-string 1 value)
  4378. (match-string 2 value)
  4379. props))))
  4380. ((equal key "DRAWERS")
  4381. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4382. ((equal key "CONSTANTS")
  4383. (org-table-set-constants))
  4384. ((equal key "STARTUP")
  4385. (let ((opts (org-split-string value splitre))
  4386. l var val)
  4387. (while (setq l (pop opts))
  4388. (when (setq l (assoc l org-startup-options))
  4389. (setq var (nth 1 l) val (nth 2 l))
  4390. (if (not (nth 3 l))
  4391. (set (make-local-variable var) val)
  4392. (if (not (listp (symbol-value var)))
  4393. (set (make-local-variable var) nil))
  4394. (set (make-local-variable var) (symbol-value var))
  4395. (add-to-list var val))))))
  4396. ((equal key "ARCHIVE")
  4397. (setq arch value)
  4398. (remove-text-properties 0 (length arch)
  4399. '(face t fontified t) arch))
  4400. ((equal key "OPTIONS")
  4401. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4402. (setq scripts (read (match-string 2 value)))))
  4403. ((and (equal key "SETUPFILE")
  4404. ;; Prevent checking in Gnus messages
  4405. (not buffer-read-only))
  4406. (setq setup-contents (org-file-contents
  4407. (expand-file-name
  4408. (org-remove-double-quotes value))
  4409. 'noerror))
  4410. (if (not ext-setup-or-nil)
  4411. (setq ext-setup-or-nil setup-contents start 0)
  4412. (setq ext-setup-or-nil
  4413. (concat (substring ext-setup-or-nil 0 start)
  4414. "\n" setup-contents "\n"
  4415. (substring ext-setup-or-nil start)))))))
  4416. ;; search for property blocks
  4417. (goto-char (point-min))
  4418. (while (re-search-forward org-block-regexp nil t)
  4419. (when (equal "PROPERTY" (upcase (match-string 1)))
  4420. (setq value (replace-regexp-in-string
  4421. "[\n\r]" " " (match-string 4)))
  4422. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4423. (setq props (org-update-property-plist (match-string 1 value)
  4424. (match-string 2 value)
  4425. props)))))))
  4426. (org-set-local 'org-use-sub-superscripts scripts)
  4427. (when cat
  4428. (org-set-local 'org-category (intern cat))
  4429. (push (cons "CATEGORY" cat) props))
  4430. (when prio
  4431. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4432. (setq prio (mapcar 'string-to-char prio))
  4433. (org-set-local 'org-highest-priority (nth 0 prio))
  4434. (org-set-local 'org-lowest-priority (nth 1 prio))
  4435. (org-set-local 'org-default-priority (nth 2 prio)))
  4436. (and props (org-set-local 'org-file-properties (nreverse props)))
  4437. (and drawers (org-set-local 'org-drawers drawers))
  4438. (and arch (org-set-local 'org-archive-location arch))
  4439. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4440. ;; Process the TODO keywords
  4441. (unless kwds
  4442. ;; Use the global values as if they had been given locally.
  4443. (setq kwds (default-value 'org-todo-keywords))
  4444. (if (stringp (car kwds))
  4445. (setq kwds (list (cons org-todo-interpretation
  4446. (default-value 'org-todo-keywords)))))
  4447. (setq kwds (reverse kwds)))
  4448. (setq kwds (nreverse kwds))
  4449. (let (inter kws kw)
  4450. (while (setq kws (pop kwds))
  4451. (let ((kws (or
  4452. (run-hook-with-args-until-success
  4453. 'org-todo-setup-filter-hook kws)
  4454. kws)))
  4455. (setq inter (pop kws) sep (member "|" kws)
  4456. kws0 (delete "|" (copy-sequence kws))
  4457. kwsa nil
  4458. kws1 (mapcar
  4459. (lambda (x)
  4460. ;; 1 2
  4461. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4462. (progn
  4463. (setq kw (match-string 1 x)
  4464. key (and (match-end 2) (match-string 2 x))
  4465. log (org-extract-log-state-settings x))
  4466. (push (cons kw (and key (string-to-char key))) kwsa)
  4467. (and log (push log org-todo-log-states))
  4468. kw)
  4469. (error "Invalid TODO keyword %s" x)))
  4470. kws0)
  4471. kwsa (if kwsa (append '((:startgroup))
  4472. (nreverse kwsa)
  4473. '((:endgroup))))
  4474. hw (car kws1)
  4475. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4476. tail (list inter hw (car dws) (org-last dws))))
  4477. (add-to-list 'org-todo-heads hw 'append)
  4478. (push kws1 org-todo-sets)
  4479. (setq org-done-keywords (append org-done-keywords dws nil))
  4480. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4481. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4482. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4483. (setq org-todo-sets (nreverse org-todo-sets)
  4484. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4485. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4486. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4487. ;; Compute the regular expressions and other local variables.
  4488. ;; Using `org-outline-regexp-bol' would complicate them much,
  4489. ;; because of the fixed white space at the end of that string.
  4490. (if (not org-done-keywords)
  4491. (setq org-done-keywords (and org-todo-keywords-1
  4492. (list (org-last org-todo-keywords-1)))))
  4493. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4494. (length org-scheduled-string)
  4495. (length org-clock-string)
  4496. (length org-closed-string)))
  4497. org-drawer-regexp
  4498. (concat "^[ \t]*:\\("
  4499. (mapconcat 'regexp-quote org-drawers "\\|")
  4500. "\\):[ \t]*$")
  4501. org-not-done-keywords
  4502. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4503. org-todo-regexp
  4504. (concat "\\("
  4505. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4506. "\\)")
  4507. org-not-done-regexp
  4508. (concat "\\("
  4509. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4510. "\\)")
  4511. org-not-done-heading-regexp
  4512. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4513. org-todo-line-regexp
  4514. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4515. org-complex-heading-regexp
  4516. (concat "^\\(\\*+\\)"
  4517. "\\(?: +" org-todo-regexp "\\)?"
  4518. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4519. "\\(?: +\\(.*?\\)\\)??"
  4520. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4521. "[ \t]*$")
  4522. org-complex-heading-regexp-format
  4523. (concat "^\\(\\*+\\)"
  4524. "\\(?: +" org-todo-regexp "\\)?"
  4525. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4526. "\\(?: +"
  4527. ;; Stats cookies can be stuck to body.
  4528. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4529. "\\(%s\\)"
  4530. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4531. "\\)"
  4532. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4533. "[ \t]*$")
  4534. org-todo-line-tags-regexp
  4535. (concat "^\\(\\*+\\)"
  4536. "\\(?: +" org-todo-regexp "\\)?"
  4537. "\\(?: +\\(.*?\\)\\)??"
  4538. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4539. "[ \t]*$")
  4540. org-deadline-regexp (concat "\\<" org-deadline-string)
  4541. org-deadline-time-regexp
  4542. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4543. org-deadline-time-hour-regexp
  4544. (concat "\\<" org-deadline-string
  4545. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4546. org-deadline-line-regexp
  4547. (concat "\\<\\(" org-deadline-string "\\).*")
  4548. org-scheduled-regexp
  4549. (concat "\\<" org-scheduled-string)
  4550. org-scheduled-time-regexp
  4551. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4552. org-scheduled-time-hour-regexp
  4553. (concat "\\<" org-scheduled-string
  4554. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4555. org-closed-time-regexp
  4556. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4557. org-keyword-time-regexp
  4558. (concat "\\<\\(" org-scheduled-string
  4559. "\\|" org-deadline-string
  4560. "\\|" org-closed-string
  4561. "\\|" org-clock-string "\\)"
  4562. " *[[<]\\([^]>]+\\)[]>]")
  4563. org-keyword-time-not-clock-regexp
  4564. (concat "\\<\\(" org-scheduled-string
  4565. "\\|" org-deadline-string
  4566. "\\|" org-closed-string
  4567. "\\)"
  4568. " *[[<]\\([^]>]+\\)[]>]")
  4569. org-maybe-keyword-time-regexp
  4570. (concat "\\(\\<\\(" org-scheduled-string
  4571. "\\|" org-deadline-string
  4572. "\\|" org-closed-string
  4573. "\\|" org-clock-string "\\)\\)?"
  4574. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4575. org-all-time-keywords
  4576. (mapcar (lambda (w) (substring w 0 -1))
  4577. (list org-scheduled-string org-deadline-string
  4578. org-clock-string org-closed-string)))
  4579. (setq org-ota nil)
  4580. (org-compute-latex-and-related-regexp))))
  4581. (defun org-file-contents (file &optional noerror)
  4582. "Return the contents of FILE, as a string."
  4583. (if (or (not file)
  4584. (not (file-readable-p file)))
  4585. (if noerror
  4586. (message "Cannot read file \"%s\"" file)
  4587. (error "Cannot read file \"%s\"" file))
  4588. (with-temp-buffer
  4589. (insert-file-contents file)
  4590. (buffer-string))))
  4591. (defun org-extract-log-state-settings (x)
  4592. "Extract the log state setting from a TODO keyword string.
  4593. This will extract info from a string like \"WAIT(w@/!)\"."
  4594. (let (kw key log1 log2)
  4595. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4596. (setq kw (match-string 1 x)
  4597. key (and (match-end 2) (match-string 2 x))
  4598. log1 (and (match-end 3) (match-string 3 x))
  4599. log2 (and (match-end 4) (match-string 4 x)))
  4600. (and (or log1 log2)
  4601. (list kw
  4602. (and log1 (if (equal log1 "!") 'time 'note))
  4603. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4604. (defun org-remove-keyword-keys (list)
  4605. "Remove a pair of parenthesis at the end of each string in LIST."
  4606. (mapcar (lambda (x)
  4607. (if (string-match "(.*)$" x)
  4608. (substring x 0 (match-beginning 0))
  4609. x))
  4610. list))
  4611. (defun org-assign-fast-keys (alist)
  4612. "Assign fast keys to a keyword-key alist.
  4613. Respect keys that are already there."
  4614. (let (new e (alt ?0))
  4615. (while (setq e (pop alist))
  4616. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4617. (cdr e)) ;; Key already assigned.
  4618. (push e new)
  4619. (let ((clist (string-to-list (downcase (car e))))
  4620. (used (append new alist)))
  4621. (when (= (car clist) ?@)
  4622. (pop clist))
  4623. (while (and clist (rassoc (car clist) used))
  4624. (pop clist))
  4625. (unless clist
  4626. (while (rassoc alt used)
  4627. (incf alt)))
  4628. (push (cons (car e) (or (car clist) alt)) new))))
  4629. (nreverse new)))
  4630. ;;; Some variables used in various places
  4631. (defvar org-window-configuration nil
  4632. "Used in various places to store a window configuration.")
  4633. (defvar org-selected-window nil
  4634. "Used in various places to store a window configuration.")
  4635. (defvar org-finish-function nil
  4636. "Function to be called when `C-c C-c' is used.
  4637. This is for getting out of special buffers like capture.")
  4638. ;; FIXME: Occasionally check by commenting these, to make sure
  4639. ;; no other functions uses these, forgetting to let-bind them.
  4640. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4641. (defvar org-last-state)
  4642. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4643. ;; Defined somewhere in this file, but used before definition.
  4644. (defvar org-entities) ;; defined in org-entities.el
  4645. (defvar org-struct-menu)
  4646. (defvar org-org-menu)
  4647. (defvar org-tbl-menu)
  4648. ;;;; Define the Org-mode
  4649. ;; We use a before-change function to check if a table might need
  4650. ;; an update.
  4651. (defvar org-table-may-need-update t
  4652. "Indicates that a table might need an update.
  4653. This variable is set by `org-before-change-function'.
  4654. `org-table-align' sets it back to nil.")
  4655. (defun org-before-change-function (beg end)
  4656. "Every change indicates that a table might need an update."
  4657. (setq org-table-may-need-update t))
  4658. (defvar org-mode-map)
  4659. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4660. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4661. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4662. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4663. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4664. (defvar org-table-buffer-is-an nil)
  4665. (defvar bidi-paragraph-direction)
  4666. (defvar buffer-face-mode-face)
  4667. (require 'outline)
  4668. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4669. (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"))
  4670. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4671. ;; Other stuff we need.
  4672. (require 'time-date)
  4673. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4674. (require 'easymenu)
  4675. (require 'overlay)
  4676. ;; (require 'org-macs) moved higher up in the file before it is first used
  4677. (require 'org-entities)
  4678. ;; (require 'org-compat) moved higher up in the file before it is first used
  4679. (require 'org-faces)
  4680. (require 'org-list)
  4681. (require 'org-pcomplete)
  4682. (require 'org-src)
  4683. (require 'org-footnote)
  4684. (require 'org-macro)
  4685. ;; babel
  4686. (require 'ob)
  4687. ;;;###autoload
  4688. (define-derived-mode org-mode outline-mode "Org"
  4689. "Outline-based notes management and organizer, alias
  4690. \"Carsten's outline-mode for keeping track of everything.\"
  4691. Org-mode develops organizational tasks around a NOTES file which
  4692. contains information about projects as plain text. Org-mode is
  4693. implemented on top of outline-mode, which is ideal to keep the content
  4694. of large files well structured. It supports ToDo items, deadlines and
  4695. time stamps, which magically appear in the diary listing of the Emacs
  4696. calendar. Tables are easily created with a built-in table editor.
  4697. Plain text URL-like links connect to websites, emails (VM), Usenet
  4698. messages (Gnus), BBDB entries, and any files related to the project.
  4699. For printing and sharing of notes, an Org-mode file (or a part of it)
  4700. can be exported as a structured ASCII or HTML file.
  4701. The following commands are available:
  4702. \\{org-mode-map}"
  4703. ;; Get rid of Outline menus, they are not needed
  4704. ;; Need to do this here because define-derived-mode sets up
  4705. ;; the keymap so late. Still, it is a waste to call this each time
  4706. ;; we switch another buffer into org-mode.
  4707. (if (featurep 'xemacs)
  4708. (when (boundp 'outline-mode-menu-heading)
  4709. ;; Assume this is Greg's port, it uses easymenu
  4710. (easy-menu-remove outline-mode-menu-heading)
  4711. (easy-menu-remove outline-mode-menu-show)
  4712. (easy-menu-remove outline-mode-menu-hide))
  4713. (define-key org-mode-map [menu-bar headings] 'undefined)
  4714. (define-key org-mode-map [menu-bar hide] 'undefined)
  4715. (define-key org-mode-map [menu-bar show] 'undefined))
  4716. (org-load-modules-maybe)
  4717. (easy-menu-add org-org-menu)
  4718. (easy-menu-add org-tbl-menu)
  4719. (org-install-agenda-files-menu)
  4720. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4721. (add-to-invisibility-spec '(org-cwidth))
  4722. (add-to-invisibility-spec '(org-hide-block . t))
  4723. (when (featurep 'xemacs)
  4724. (org-set-local 'line-move-ignore-invisible t))
  4725. (org-set-local 'outline-regexp org-outline-regexp)
  4726. (org-set-local 'outline-level 'org-outline-level)
  4727. (setq bidi-paragraph-direction 'left-to-right)
  4728. (when (and org-ellipsis
  4729. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4730. (fboundp 'make-glyph-code))
  4731. (unless org-display-table
  4732. (setq org-display-table (make-display-table)))
  4733. (set-display-table-slot
  4734. org-display-table 4
  4735. (vconcat (mapcar
  4736. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4737. org-ellipsis)))
  4738. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4739. (setq buffer-display-table org-display-table))
  4740. (org-set-regexps-and-options-for-tags)
  4741. (org-set-regexps-and-options)
  4742. (org-set-font-lock-defaults)
  4743. (when (and org-tag-faces (not org-tags-special-faces-re))
  4744. ;; tag faces set outside customize.... force initialization.
  4745. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4746. ;; Calc embedded
  4747. (org-set-local 'calc-embedded-open-mode "# ")
  4748. ;; Modify a few syntax entries
  4749. (modify-syntax-entry ?@ "w")
  4750. (modify-syntax-entry ?\" "\"")
  4751. (if org-startup-truncated (setq truncate-lines t))
  4752. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4753. (org-set-local 'font-lock-unfontify-region-function
  4754. 'org-unfontify-region)
  4755. ;; Activate before-change-function
  4756. (org-set-local 'org-table-may-need-update t)
  4757. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4758. 'local)
  4759. ;; Check for running clock before killing a buffer
  4760. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4761. ;; Initialize macros templates.
  4762. (org-macro-initialize-templates)
  4763. ;; Initialize radio targets.
  4764. (org-update-radio-target-regexp)
  4765. ;; Indentation.
  4766. (org-set-local 'indent-line-function 'org-indent-line)
  4767. (org-set-local 'indent-region-function 'org-indent-region)
  4768. ;; Filling and auto-filling.
  4769. (org-setup-filling)
  4770. ;; Comments.
  4771. (org-setup-comments-handling)
  4772. ;; Beginning/end of defun
  4773. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4774. (org-set-local 'end-of-defun-function 'org-forward-element)
  4775. ;; Next error for sparse trees
  4776. (org-set-local 'next-error-function 'org-occur-next-match)
  4777. ;; Make sure dependence stuff works reliably, even for users who set it
  4778. ;; too late :-(
  4779. (if org-enforce-todo-dependencies
  4780. (add-hook 'org-blocker-hook
  4781. 'org-block-todo-from-children-or-siblings-or-parent)
  4782. (remove-hook 'org-blocker-hook
  4783. 'org-block-todo-from-children-or-siblings-or-parent))
  4784. (if org-enforce-todo-checkbox-dependencies
  4785. (add-hook 'org-blocker-hook
  4786. 'org-block-todo-from-checkboxes)
  4787. (remove-hook 'org-blocker-hook
  4788. 'org-block-todo-from-checkboxes))
  4789. ;; Align options lines
  4790. (org-set-local
  4791. 'align-mode-rules-list
  4792. '((org-in-buffer-settings
  4793. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4794. (modes . '(org-mode)))))
  4795. ;; Imenu
  4796. (org-set-local 'imenu-create-index-function
  4797. 'org-imenu-get-tree)
  4798. ;; Make isearch reveal context
  4799. (if (or (featurep 'xemacs)
  4800. (not (boundp 'outline-isearch-open-invisible-function)))
  4801. ;; Emacs 21 and XEmacs make use of the hook
  4802. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4803. ;; Emacs 22 deals with this through a special variable
  4804. (org-set-local 'outline-isearch-open-invisible-function
  4805. (lambda (&rest ignore) (org-show-context 'isearch)))
  4806. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4807. ;; Setup the pcomplete hooks
  4808. (set (make-local-variable 'pcomplete-command-completion-function)
  4809. 'org-pcomplete-initial)
  4810. (set (make-local-variable 'pcomplete-command-name-function)
  4811. 'org-command-at-point)
  4812. (set (make-local-variable 'pcomplete-default-completion-function)
  4813. 'ignore)
  4814. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4815. 'org-parse-arguments)
  4816. (set (make-local-variable 'pcomplete-termination-string) "")
  4817. (when (>= emacs-major-version 23)
  4818. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4819. ;; If empty file that did not turn on org-mode automatically, make it to.
  4820. (if (and org-insert-mode-line-in-empty-file
  4821. (org-called-interactively-p 'any)
  4822. (= (point-min) (point-max)))
  4823. (insert "# -*- mode: org -*-\n\n"))
  4824. (unless org-inhibit-startup
  4825. (org-unmodified
  4826. (and org-startup-with-beamer-mode (org-beamer-mode))
  4827. (when org-startup-align-all-tables
  4828. (org-table-map-tables 'org-table-align 'quietly))
  4829. (when org-startup-with-inline-images
  4830. (org-display-inline-images))
  4831. (when org-startup-with-latex-preview
  4832. (org-preview-latex-fragment))
  4833. (unless org-inhibit-startup-visibility-stuff
  4834. (org-set-startup-visibility))))
  4835. ;; Try to set org-hide correctly
  4836. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4837. ;; Update `customize-package-emacs-version-alist'
  4838. (add-to-list 'customize-package-emacs-version-alist
  4839. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4840. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4841. ("8.0" . "24.4")))
  4842. (defvar org-mode-transpose-word-syntax-table
  4843. (let ((st (make-syntax-table)))
  4844. (mapc (lambda(c) (modify-syntax-entry
  4845. (string-to-char (car c)) "w p" st))
  4846. org-emphasis-alist)
  4847. st))
  4848. (when (fboundp 'abbrev-table-put)
  4849. (abbrev-table-put org-mode-abbrev-table
  4850. :parents (list text-mode-abbrev-table)))
  4851. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4852. (defsubst org-fix-ellipsis-at-bol ()
  4853. (save-excursion (goto-char (window-start)) (recenter 0)))
  4854. (defun org-find-invisible-foreground ()
  4855. (let ((candidates (remove
  4856. "unspecified-bg"
  4857. (nconc
  4858. (list (face-background 'default)
  4859. (face-background 'org-default))
  4860. (mapcar
  4861. (lambda (alist)
  4862. (when (boundp alist)
  4863. (cdr (assoc 'background-color (symbol-value alist)))))
  4864. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4865. (list (face-foreground 'org-hide))))))
  4866. (car (remove nil candidates))))
  4867. (defun org-current-time (&optional rounding-minutes past)
  4868. "Current time, possibly rounded to ROUNDING-MINUTES.
  4869. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4870. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4871. the rounding returns a past time."
  4872. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4873. (car org-time-stamp-rounding-minutes)))
  4874. (time (decode-time)) res)
  4875. (if (< r 1)
  4876. (current-time)
  4877. (setq res
  4878. (apply 'encode-time
  4879. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4880. (nthcdr 2 time))))
  4881. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4882. (seconds-to-time (- (org-float-time res) (* r 60)))
  4883. res))))
  4884. (defun org-today ()
  4885. "Return today date, considering `org-extend-today-until'."
  4886. (time-to-days
  4887. (time-subtract (current-time)
  4888. (list 0 (* 3600 org-extend-today-until) 0))))
  4889. ;;;; Font-Lock stuff, including the activators
  4890. (defvar org-mouse-map (make-sparse-keymap))
  4891. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4892. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4893. (when org-mouse-1-follows-link
  4894. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4895. (when org-tab-follows-link
  4896. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4897. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4898. (require 'font-lock)
  4899. (defconst org-non-link-chars "]\t\n\r<>")
  4900. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4901. "shell" "elisp" "doi" "message"))
  4902. (defvar org-link-types-re nil
  4903. "Matches a link that has a url-like prefix like \"http:\"")
  4904. (defvar org-link-re-with-space nil
  4905. "Matches a link with spaces, optional angular brackets around it.")
  4906. (defvar org-link-re-with-space2 nil
  4907. "Matches a link with spaces, optional angular brackets around it.")
  4908. (defvar org-link-re-with-space3 nil
  4909. "Matches a link with spaces, only for internal part in bracket links.")
  4910. (defvar org-angle-link-re nil
  4911. "Matches link with angular brackets, spaces are allowed.")
  4912. (defvar org-plain-link-re nil
  4913. "Matches plain link, without spaces.")
  4914. (defvar org-bracket-link-regexp nil
  4915. "Matches a link in double brackets.")
  4916. (defvar org-bracket-link-analytic-regexp nil
  4917. "Regular expression used to analyze links.
  4918. Here is what the match groups contain after a match:
  4919. 1: http:
  4920. 2: http
  4921. 3: path
  4922. 4: [desc]
  4923. 5: desc")
  4924. (defvar org-bracket-link-analytic-regexp++ nil
  4925. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4926. (defvar org-any-link-re nil
  4927. "Regular expression matching any link.")
  4928. (defconst org-match-sexp-depth 3
  4929. "Number of stacked braces for sub/superscript matching.")
  4930. (defun org-create-multibrace-regexp (left right n)
  4931. "Create a regular expression which will match a balanced sexp.
  4932. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4933. as single character strings.
  4934. The regexp returned will match the entire expression including the
  4935. delimiters. It will also define a single group which contains the
  4936. match except for the outermost delimiters. The maximum depth of
  4937. stacked delimiters is N. Escaping delimiters is not possible."
  4938. (let* ((nothing (concat "[^" left right "]*?"))
  4939. (or "\\|")
  4940. (re nothing)
  4941. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4942. (while (> n 1)
  4943. (setq n (1- n)
  4944. re (concat re or next)
  4945. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4946. (concat left "\\(" re "\\)" right)))
  4947. (defvar org-match-substring-regexp
  4948. (concat
  4949. "\\(\\S-\\)\\([_^]\\)\\("
  4950. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4951. "\\|"
  4952. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4953. "\\|"
  4954. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4955. "The regular expression matching a sub- or superscript.")
  4956. (defvar org-match-substring-with-braces-regexp
  4957. (concat
  4958. "\\(\\S-\\)\\([_^]\\)\\("
  4959. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4960. "\\)")
  4961. "The regular expression matching a sub- or superscript, forcing braces.")
  4962. (defun org-make-link-regexps ()
  4963. "Update the link regular expressions.
  4964. This should be called after the variable `org-link-types' has changed."
  4965. (setq org-link-types-re
  4966. (concat
  4967. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4968. org-link-re-with-space
  4969. (concat
  4970. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4971. "\\([^" org-non-link-chars " ]"
  4972. "[^" org-non-link-chars "]*"
  4973. "[^" org-non-link-chars " ]\\)>?")
  4974. org-link-re-with-space2
  4975. (concat
  4976. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4977. "\\([^" org-non-link-chars " ]"
  4978. "[^\t\n\r]*"
  4979. "[^" org-non-link-chars " ]\\)>?")
  4980. org-link-re-with-space3
  4981. (concat
  4982. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4983. "\\([^" org-non-link-chars " ]"
  4984. "[^\t\n\r]*\\)")
  4985. org-angle-link-re
  4986. (concat
  4987. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4988. "\\([^" org-non-link-chars " ]"
  4989. "[^" org-non-link-chars "]*"
  4990. "\\)>")
  4991. org-plain-link-re
  4992. (concat
  4993. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4994. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4995. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4996. org-bracket-link-regexp
  4997. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4998. org-bracket-link-analytic-regexp
  4999. (concat
  5000. "\\[\\["
  5001. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  5002. "\\([^]]+\\)"
  5003. "\\]"
  5004. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5005. "\\]")
  5006. org-bracket-link-analytic-regexp++
  5007. (concat
  5008. "\\[\\["
  5009. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  5010. "\\([^]]+\\)"
  5011. "\\]"
  5012. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5013. "\\]")
  5014. org-any-link-re
  5015. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5016. org-angle-link-re "\\)\\|\\("
  5017. org-plain-link-re "\\)")))
  5018. (org-make-link-regexps)
  5019. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  5020. "Regular expression for fast time stamp matching.")
  5021. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  5022. "Regular expression for fast time stamp matching.")
  5023. (defconst org-ts-regexp0
  5024. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5025. "Regular expression matching time strings for analysis.
  5026. This one does not require the space after the date, so it can be used
  5027. on a string that terminates immediately after the date.")
  5028. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5029. "Regular expression matching time strings for analysis.")
  5030. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5031. "Regular expression matching time stamps, with groups.")
  5032. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5033. "Regular expression matching time stamps (also [..]), with groups.")
  5034. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5035. "Regular expression matching a time stamp range.")
  5036. (defconst org-tr-regexp-both
  5037. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5038. "Regular expression matching a time stamp range.")
  5039. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5040. org-ts-regexp "\\)?")
  5041. "Regular expression matching a time stamp or time stamp range.")
  5042. (defconst org-tsr-regexp-both
  5043. (concat org-ts-regexp-both "\\(--?-?"
  5044. org-ts-regexp-both "\\)?")
  5045. "Regular expression matching a time stamp or time stamp range.
  5046. The time stamps may be either active or inactive.")
  5047. (defvar org-emph-face nil)
  5048. (defun org-do-emphasis-faces (limit)
  5049. "Run through the buffer and add overlays to emphasized strings."
  5050. (let (rtn a)
  5051. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5052. (if (not (= (char-after (match-beginning 3))
  5053. (char-after (match-beginning 4))))
  5054. (progn
  5055. (setq rtn t)
  5056. (setq a (assoc (match-string 3) org-emphasis-alist))
  5057. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5058. 'face
  5059. (nth 1 a))
  5060. (and (nth 4 a)
  5061. (org-remove-flyspell-overlays-in
  5062. (match-beginning 0) (match-end 0)))
  5063. (add-text-properties (match-beginning 2) (match-end 2)
  5064. '(font-lock-multiline t org-emphasis t))
  5065. (when org-hide-emphasis-markers
  5066. (add-text-properties (match-end 4) (match-beginning 5)
  5067. '(invisible org-link))
  5068. (add-text-properties (match-beginning 3) (match-end 3)
  5069. '(invisible org-link)))))
  5070. (backward-char 1))
  5071. rtn))
  5072. (defun org-emphasize (&optional char)
  5073. "Insert or change an emphasis, i.e. a font like bold or italic.
  5074. If there is an active region, change that region to a new emphasis.
  5075. If there is no region, just insert the marker characters and position
  5076. the cursor between them.
  5077. CHAR should be the marker character. If it is a space, it means to
  5078. remove the emphasis of the selected region.
  5079. If CHAR is not given (for example in an interactive call) it will be
  5080. prompted for."
  5081. (interactive)
  5082. (let ((erc org-emphasis-regexp-components)
  5083. (prompt "")
  5084. (string "") beg end move c s)
  5085. (if (org-region-active-p)
  5086. (setq beg (region-beginning) end (region-end)
  5087. string (buffer-substring beg end))
  5088. (setq move t))
  5089. (unless char
  5090. (message "Emphasis marker or tag: [%s]"
  5091. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5092. (setq char (read-char-exclusive)))
  5093. (if (equal char ?\ )
  5094. (setq s "" move nil)
  5095. (unless (assoc (char-to-string char) org-emphasis-alist)
  5096. (user-error "No such emphasis marker: \"%c\"" char))
  5097. (setq s (char-to-string char)))
  5098. (while (and (> (length string) 1)
  5099. (equal (substring string 0 1) (substring string -1))
  5100. (assoc (substring string 0 1) org-emphasis-alist))
  5101. (setq string (substring string 1 -1)))
  5102. (setq string (concat s string s))
  5103. (if beg (delete-region beg end))
  5104. (unless (or (bolp)
  5105. (string-match (concat "[" (nth 0 erc) "\n]")
  5106. (char-to-string (char-before (point)))))
  5107. (insert " "))
  5108. (unless (or (eobp)
  5109. (string-match (concat "[" (nth 1 erc) "\n]")
  5110. (char-to-string (char-after (point)))))
  5111. (insert " ") (backward-char 1))
  5112. (insert string)
  5113. (and move (backward-char 1))))
  5114. (defconst org-nonsticky-props
  5115. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5116. (defsubst org-rear-nonsticky-at (pos)
  5117. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5118. (defun org-activate-plain-links (limit)
  5119. "Run through the buffer and add overlays to links."
  5120. (let (f hl)
  5121. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5122. (not (org-in-src-block-p)))
  5123. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5124. (setq f (get-text-property (match-beginning 0) 'face))
  5125. (setq hl (org-match-string-no-properties 0))
  5126. (if (or (eq f 'org-tag)
  5127. (and (listp f) (memq 'org-tag f)))
  5128. nil
  5129. (add-text-properties (match-beginning 0) (match-end 0)
  5130. (list 'mouse-face 'highlight
  5131. 'face 'org-link
  5132. 'htmlize-link `(:uri ,hl)
  5133. 'keymap org-mouse-map))
  5134. (org-rear-nonsticky-at (match-end 0)))
  5135. t)))
  5136. (defun org-activate-code (limit)
  5137. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5138. (progn
  5139. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5140. (remove-text-properties (match-beginning 0) (match-end 0)
  5141. '(display t invisible t intangible t))
  5142. t)))
  5143. (defcustom org-src-fontify-natively nil
  5144. "When non-nil, fontify code in code blocks."
  5145. :type 'boolean
  5146. :version "24.1"
  5147. :group 'org-appearance
  5148. :group 'org-babel)
  5149. (defcustom org-allow-promoting-top-level-subtree nil
  5150. "When non-nil, allow promoting a top level subtree.
  5151. The leading star of the top level headline will be replaced
  5152. by a #."
  5153. :type 'boolean
  5154. :version "24.1"
  5155. :group 'org-appearance)
  5156. (defun org-fontify-meta-lines-and-blocks (limit)
  5157. (condition-case nil
  5158. (org-fontify-meta-lines-and-blocks-1 limit)
  5159. (error (message "org-mode fontification error"))))
  5160. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5161. "Fontify #+ lines and blocks."
  5162. (let ((case-fold-search t))
  5163. (if (re-search-forward
  5164. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5165. limit t)
  5166. (let ((beg (match-beginning 0))
  5167. (block-start (match-end 0))
  5168. (block-end nil)
  5169. (lang (match-string 7))
  5170. (beg1 (line-beginning-position 2))
  5171. (dc1 (downcase (match-string 2)))
  5172. (dc3 (downcase (match-string 3)))
  5173. end end1 quoting block-type ovl)
  5174. (cond
  5175. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5176. ;; a single line of backend-specific content
  5177. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5178. (remove-text-properties (match-beginning 0) (match-end 0)
  5179. '(display t invisible t intangible t))
  5180. (add-text-properties (match-beginning 1) (match-end 3)
  5181. '(font-lock-fontified t face org-meta-line))
  5182. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5183. '(font-lock-fontified t face org-block))
  5184. ; for backend-specific code
  5185. t)
  5186. ((and (match-end 4) (equal dc3 "+begin"))
  5187. ;; Truly a block
  5188. (setq block-type (downcase (match-string 5))
  5189. quoting (member block-type org-protecting-blocks))
  5190. (when (re-search-forward
  5191. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5192. nil t) ;; on purpose, we look further than LIMIT
  5193. (setq end (min (point-max) (match-end 0))
  5194. end1 (min (point-max) (1- (match-beginning 0))))
  5195. (setq block-end (match-beginning 0))
  5196. (when quoting
  5197. (remove-text-properties beg end
  5198. '(display t invisible t intangible t)))
  5199. (add-text-properties
  5200. beg end
  5201. '(font-lock-fontified t font-lock-multiline t))
  5202. (add-text-properties beg beg1 '(face org-meta-line))
  5203. (add-text-properties end1 (min (point-max) (1+ end))
  5204. '(face org-meta-line)) ; for end_src
  5205. (cond
  5206. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5207. (org-src-font-lock-fontify-block lang block-start block-end)
  5208. ;; remove old background overlays
  5209. (mapc (lambda (ov)
  5210. (if (eq (overlay-get ov 'face) 'org-block-background)
  5211. (delete-overlay ov)))
  5212. (overlays-at (/ (+ beg1 block-end) 2)))
  5213. ;; add a background overlay
  5214. (setq ovl (make-overlay beg1 block-end))
  5215. (overlay-put ovl 'face 'org-block-background)
  5216. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5217. (quoting
  5218. (add-text-properties beg1 (min (point-max) (1+ end1))
  5219. '(face org-block))) ; end of source block
  5220. ((not org-fontify-quote-and-verse-blocks))
  5221. ((string= block-type "quote")
  5222. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5223. ((string= block-type "verse")
  5224. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5225. (add-text-properties beg beg1 '(face org-block-begin-line))
  5226. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5227. '(face org-block-end-line))
  5228. t))
  5229. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5230. (add-text-properties
  5231. beg (match-end 3)
  5232. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5233. '(font-lock-fontified t invisible t)
  5234. '(font-lock-fontified t face org-document-info-keyword)))
  5235. (add-text-properties
  5236. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5237. (if (string-equal dc1 "+title:")
  5238. '(font-lock-fontified t face org-document-title)
  5239. '(font-lock-fontified t face org-document-info))))
  5240. ((or (equal dc1 "+results")
  5241. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5242. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5243. "+call:" "+header:" "+headers:" "+name:"))
  5244. (and (match-end 4) (equal dc3 "+attr")))
  5245. (add-text-properties
  5246. beg (match-end 0)
  5247. '(font-lock-fontified t face org-meta-line))
  5248. t)
  5249. ((member dc3 '(" " ""))
  5250. (add-text-properties
  5251. beg (match-end 0)
  5252. '(font-lock-fontified t face font-lock-comment-face)))
  5253. ((not (member (char-after beg) '(?\ ?\t)))
  5254. ;; just any other in-buffer setting, but not indented
  5255. (add-text-properties
  5256. beg (match-end 0)
  5257. '(font-lock-fontified t face org-meta-line))
  5258. t)
  5259. (t nil))))))
  5260. (defun org-activate-angle-links (limit)
  5261. "Run through the buffer and add overlays to links."
  5262. (if (and (re-search-forward org-angle-link-re limit t)
  5263. (not (org-in-src-block-p)))
  5264. (progn
  5265. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5266. (add-text-properties (match-beginning 0) (match-end 0)
  5267. (list 'mouse-face 'highlight
  5268. 'keymap org-mouse-map))
  5269. (org-rear-nonsticky-at (match-end 0))
  5270. t)))
  5271. (defun org-activate-footnote-links (limit)
  5272. "Run through the buffer and add overlays to footnotes."
  5273. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5274. (when fn
  5275. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5276. (org-remove-flyspell-overlays-in beg end)
  5277. (add-text-properties beg end
  5278. (list 'mouse-face 'highlight
  5279. 'keymap org-mouse-map
  5280. 'help-echo
  5281. (if (= (point-at-bol) beg)
  5282. "Footnote definition"
  5283. "Footnote reference")
  5284. 'font-lock-fontified t
  5285. 'font-lock-multiline t
  5286. 'face 'org-footnote))))))
  5287. (defun org-activate-bracket-links (limit)
  5288. "Run through the buffer and add overlays to bracketed links."
  5289. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5290. (not (org-in-src-block-p)))
  5291. (let* ((hl (org-match-string-no-properties 1))
  5292. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5293. (ip (org-maybe-intangible
  5294. (list 'invisible 'org-link
  5295. 'keymap org-mouse-map 'mouse-face 'highlight
  5296. 'font-lock-multiline t 'help-echo help
  5297. 'htmlize-link `(:uri ,hl))))
  5298. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5299. 'font-lock-multiline t 'help-echo help
  5300. 'htmlize-link `(:uri ,hl))))
  5301. ;; We need to remove the invisible property here. Table narrowing
  5302. ;; may have made some of this invisible.
  5303. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5304. (remove-text-properties (match-beginning 0) (match-end 0)
  5305. '(invisible nil))
  5306. (if (match-end 3)
  5307. (progn
  5308. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5309. (org-rear-nonsticky-at (match-beginning 3))
  5310. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5311. (org-rear-nonsticky-at (match-end 3))
  5312. (add-text-properties (match-end 3) (match-end 0) ip)
  5313. (org-rear-nonsticky-at (match-end 0)))
  5314. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5315. (org-rear-nonsticky-at (match-beginning 1))
  5316. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5317. (org-rear-nonsticky-at (match-end 1))
  5318. (add-text-properties (match-end 1) (match-end 0) ip)
  5319. (org-rear-nonsticky-at (match-end 0)))
  5320. t)))
  5321. (defun org-activate-dates (limit)
  5322. "Run through the buffer and add overlays to dates."
  5323. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5324. (not (equal (char-before (match-beginning 0)) 91)))
  5325. (progn
  5326. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5327. (add-text-properties (match-beginning 0) (match-end 0)
  5328. (list 'mouse-face 'highlight
  5329. 'keymap org-mouse-map))
  5330. (org-rear-nonsticky-at (match-end 0))
  5331. (when org-display-custom-times
  5332. (if (match-end 3)
  5333. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5334. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5335. t)))
  5336. (defvar org-target-link-regexp nil
  5337. "Regular expression matching radio targets in plain text.")
  5338. (make-variable-buffer-local 'org-target-link-regexp)
  5339. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5340. "Regular expression matching a link target.")
  5341. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5342. "Regular expression matching a radio target.")
  5343. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5344. "Regular expression matching any target.")
  5345. (defun org-activate-target-links (limit)
  5346. "Run through the buffer and add overlays to target matches."
  5347. (when org-target-link-regexp
  5348. (let ((case-fold-search t))
  5349. (if (re-search-forward org-target-link-regexp limit t)
  5350. (progn
  5351. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5352. (add-text-properties (match-beginning 0) (match-end 0)
  5353. (list 'mouse-face 'highlight
  5354. 'keymap org-mouse-map
  5355. 'help-echo "Radio target link"
  5356. 'org-linked-text t))
  5357. (org-rear-nonsticky-at (match-end 0))
  5358. t)))))
  5359. (defun org-update-radio-target-regexp ()
  5360. "Find all radio targets in this file and update the regular expression."
  5361. (interactive)
  5362. (when (memq 'radio org-activate-links)
  5363. (setq org-target-link-regexp
  5364. (org-make-target-link-regexp (org-all-targets 'radio)))
  5365. (org-restart-font-lock)))
  5366. (defun org-hide-wide-columns (limit)
  5367. (let (s e)
  5368. (setq s (text-property-any (point) (or limit (point-max))
  5369. 'org-cwidth t))
  5370. (when s
  5371. (setq e (next-single-property-change s 'org-cwidth))
  5372. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5373. (goto-char e)
  5374. t)))
  5375. (defvar org-latex-and-related-regexp nil
  5376. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5377. (defvar org-match-substring-regexp)
  5378. (defvar org-match-substring-with-braces-regexp)
  5379. (defun org-compute-latex-and-related-regexp ()
  5380. "Compute regular expression for LaTeX, entities and sub/superscript.
  5381. Result depends on variable `org-highlight-latex-and-related'."
  5382. (org-set-local
  5383. 'org-latex-and-related-regexp
  5384. (let* ((re-sub
  5385. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5386. ((eq org-use-sub-superscripts '{})
  5387. (list org-match-substring-with-braces-regexp))
  5388. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5389. (re-latex
  5390. (when (memq 'latex org-highlight-latex-and-related)
  5391. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5392. (delq nil
  5393. (mapcar (lambda (x)
  5394. (and (member (car x) matchers) (nth 1 x)))
  5395. org-latex-regexps)))))
  5396. (re-entities
  5397. (when (memq 'entities org-highlight-latex-and-related)
  5398. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5399. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5400. (defun org-do-latex-and-related (limit)
  5401. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5402. LIMIT bounds the search for syntax to highlight. Stop at first
  5403. highlighted object, if any. Return t if some highlighting was
  5404. done, nil otherwise."
  5405. (when (org-string-nw-p org-latex-and-related-regexp)
  5406. (catch 'found
  5407. (while (re-search-forward org-latex-and-related-regexp limit t)
  5408. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5409. 'face))
  5410. '(org-code org-verbatim underline))
  5411. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5412. '(?_ ?^))
  5413. 1
  5414. 0)))
  5415. (font-lock-prepend-text-property
  5416. (+ offset (match-beginning 0)) (match-end 0)
  5417. 'face 'org-latex-and-related)
  5418. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5419. '(font-lock-multiline t)))
  5420. (throw 'found t)))
  5421. nil)))
  5422. (defun org-restart-font-lock ()
  5423. "Restart `font-lock-mode', to force refontification."
  5424. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5425. (font-lock-mode -1)
  5426. (font-lock-mode 1)))
  5427. (defun org-all-targets (&optional radio)
  5428. "Return a list of all targets in this file.
  5429. When optional argument RADIO is non-nil, only find radio
  5430. targets."
  5431. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5432. (save-excursion
  5433. (goto-char (point-min))
  5434. (while (re-search-forward re nil t)
  5435. ;; Make sure point is really within the object.
  5436. (backward-char)
  5437. (let ((obj (org-element-context)))
  5438. (when (memq (org-element-type obj) '(radio-target target))
  5439. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5440. rtn)))
  5441. (defun org-make-target-link-regexp (targets)
  5442. "Make regular expression matching all strings in TARGETS.
  5443. The regular expression finds the targets also if there is a line break
  5444. between words."
  5445. (and targets
  5446. (concat
  5447. "\\<\\("
  5448. (mapconcat
  5449. (lambda (x)
  5450. (setq x (regexp-quote x))
  5451. (while (string-match " +" x)
  5452. (setq x (replace-match "\\s-+" t t x)))
  5453. x)
  5454. targets
  5455. "\\|")
  5456. "\\)\\>")))
  5457. (defun org-activate-tags (limit)
  5458. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5459. (progn
  5460. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5461. (add-text-properties (match-beginning 1) (match-end 1)
  5462. (list 'mouse-face 'highlight
  5463. 'keymap org-mouse-map))
  5464. (org-rear-nonsticky-at (match-end 1))
  5465. t)))
  5466. (defun org-outline-level ()
  5467. "Compute the outline level of the heading at point.
  5468. If this is called at a normal headline, the level is the number of stars.
  5469. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5470. (save-excursion
  5471. (if (not (condition-case nil
  5472. (org-back-to-heading t)
  5473. (error nil)))
  5474. 0
  5475. (looking-at org-outline-regexp)
  5476. (1- (- (match-end 0) (match-beginning 0))))))
  5477. (defvar org-font-lock-keywords nil)
  5478. (defsubst org-re-property (property &optional literal)
  5479. "Return a regexp matching a PROPERTY line.
  5480. Match group 3 will be set to the value if it exists."
  5481. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5482. (if literal property (regexp-quote property))
  5483. "\\):\\)[ \t]+\\(?3:[^ \t\r\n].*?\\)\\(?5:[ \t]*\\)$"))
  5484. (defconst org-property-re
  5485. (org-re-property ".*?" 'literal)
  5486. "Regular expression matching a property line.
  5487. There are four matching groups:
  5488. 1: :PROPKEY: including the leading and trailing colon,
  5489. 2: PROPKEY without the leading and trailing colon,
  5490. 3: PROPVAL without leading or trailing spaces,
  5491. 4: the indentation of the current line,
  5492. 5: trailing whitespace.")
  5493. (defvar org-font-lock-hook nil
  5494. "Functions to be called for special font lock stuff.")
  5495. (defvar org-font-lock-set-keywords-hook nil
  5496. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5497. This is called after `org-font-lock-extra-keywords' is defined, but before
  5498. it is installed to be used by font lock. This can be useful if something
  5499. needs to be inserted at a specific position in the font-lock sequence.")
  5500. (defun org-font-lock-hook (limit)
  5501. "Run `org-font-lock-hook' within LIMIT."
  5502. (run-hook-with-args 'org-font-lock-hook limit))
  5503. (defun org-set-font-lock-defaults ()
  5504. "Set font lock defaults for the current buffer."
  5505. (let* ((em org-fontify-emphasized-text)
  5506. (lk org-activate-links)
  5507. (org-font-lock-extra-keywords
  5508. (list
  5509. ;; Call the hook
  5510. '(org-font-lock-hook)
  5511. ;; Headlines
  5512. `(,(if org-fontify-whole-heading-line
  5513. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5514. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5515. (1 (org-get-level-face 1))
  5516. (2 (org-get-level-face 2))
  5517. (3 (org-get-level-face 3)))
  5518. ;; Table lines
  5519. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5520. (1 'org-table t))
  5521. ;; Table internals
  5522. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5523. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5524. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5525. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5526. ;; Drawers
  5527. (list org-drawer-regexp '(0 'org-special-keyword t))
  5528. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5529. ;; Properties
  5530. (list org-property-re
  5531. '(1 'org-special-keyword t)
  5532. '(3 'org-property-value t))
  5533. ;; Links
  5534. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5535. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5536. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5537. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5538. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5539. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5540. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5541. ;; Targets.
  5542. (list org-any-target-regexp '(0 'org-target t))
  5543. ;; Diary sexps.
  5544. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5545. ;; Macro
  5546. '("{{{.+}}}" (0 'org-macro t))
  5547. '(org-hide-wide-columns (0 nil append))
  5548. ;; TODO keyword
  5549. (list (format org-heading-keyword-regexp-format
  5550. org-todo-regexp)
  5551. '(2 (org-get-todo-face 2) t))
  5552. ;; DONE
  5553. (if org-fontify-done-headline
  5554. (list (format org-heading-keyword-regexp-format
  5555. (concat
  5556. "\\(?:"
  5557. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5558. "\\)"))
  5559. '(2 'org-headline-done t))
  5560. nil)
  5561. ;; Priorities
  5562. '(org-font-lock-add-priority-faces)
  5563. ;; Tags
  5564. '(org-font-lock-add-tag-faces)
  5565. ;; Tags groups
  5566. (if (and org-group-tags org-tag-groups-alist)
  5567. (list (concat org-outline-regexp-bol ".+\\(:"
  5568. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5569. ":\\).*$")
  5570. '(1 'org-tag-group prepend)))
  5571. ;; Special keywords
  5572. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5573. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5574. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5575. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5576. ;; Emphasis
  5577. (if em
  5578. (if (featurep 'xemacs)
  5579. '(org-do-emphasis-faces (0 nil append))
  5580. '(org-do-emphasis-faces)))
  5581. ;; Checkboxes
  5582. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5583. 1 'org-checkbox prepend)
  5584. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5585. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5586. (0 (org-get-checkbox-statistics-face) t)))
  5587. ;; Description list items
  5588. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5589. 1 'org-list-dt prepend)
  5590. ;; ARCHIVEd headings
  5591. (list (concat
  5592. org-outline-regexp-bol
  5593. "\\(.*:" org-archive-tag ":.*\\)")
  5594. '(1 'org-archived prepend))
  5595. ;; Specials
  5596. '(org-do-latex-and-related)
  5597. '(org-fontify-entities)
  5598. '(org-raise-scripts)
  5599. ;; Code
  5600. '(org-activate-code (1 'org-code t))
  5601. ;; COMMENT
  5602. (list (format org-heading-keyword-regexp-format
  5603. (concat "\\("
  5604. org-comment-string "\\|" org-quote-string
  5605. "\\)"))
  5606. '(2 'org-special-keyword t))
  5607. ;; Blocks and meta lines
  5608. '(org-fontify-meta-lines-and-blocks))))
  5609. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5610. (run-hooks 'org-font-lock-set-keywords-hook)
  5611. ;; Now set the full font-lock-keywords
  5612. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5613. (org-set-local 'font-lock-defaults
  5614. '(org-font-lock-keywords t nil nil backward-paragraph))
  5615. (kill-local-variable 'font-lock-keywords) nil))
  5616. (defun org-toggle-pretty-entities ()
  5617. "Toggle the composition display of entities as UTF8 characters."
  5618. (interactive)
  5619. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5620. (org-restart-font-lock)
  5621. (if org-pretty-entities
  5622. (message "Entities are now displayed as UTF8 characters")
  5623. (save-restriction
  5624. (widen)
  5625. (org-decompose-region (point-min) (point-max))
  5626. (message "Entities are now displayed as plain text"))))
  5627. (defvar org-custom-properties-overlays nil
  5628. "List of overlays used for custom properties.")
  5629. (make-variable-buffer-local 'org-custom-properties-overlays)
  5630. (defun org-toggle-custom-properties-visibility ()
  5631. "Display or hide properties in `org-custom-properties'."
  5632. (interactive)
  5633. (if org-custom-properties-overlays
  5634. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5635. (setq org-custom-properties-overlays nil))
  5636. (unless (not org-custom-properties)
  5637. (save-excursion
  5638. (save-restriction
  5639. (widen)
  5640. (goto-char (point-min))
  5641. (while (re-search-forward org-property-re nil t)
  5642. (mapc (lambda(p)
  5643. (when (equal p (substring (match-string 1) 1 -1))
  5644. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5645. (overlay-put o 'invisible t)
  5646. (overlay-put o 'org-custom-property t)
  5647. (push o org-custom-properties-overlays))))
  5648. org-custom-properties)))))))
  5649. (defun org-fontify-entities (limit)
  5650. "Find an entity to fontify."
  5651. (let (ee)
  5652. (when org-pretty-entities
  5653. (catch 'match
  5654. (while (re-search-forward
  5655. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5656. limit t)
  5657. (if (and (not (org-in-indented-comment-line))
  5658. (setq ee (org-entity-get (match-string 1)))
  5659. (= (length (nth 6 ee)) 1))
  5660. (let*
  5661. ((end (if (equal (match-string 2) "{}")
  5662. (match-end 2)
  5663. (match-end 1))))
  5664. (add-text-properties
  5665. (match-beginning 0) end
  5666. (list 'font-lock-fontified t))
  5667. (compose-region (match-beginning 0) end
  5668. (nth 6 ee) nil)
  5669. (backward-char 1)
  5670. (throw 'match t))))
  5671. nil))))
  5672. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5673. "Fontify string S like in Org-mode."
  5674. (with-temp-buffer
  5675. (insert s)
  5676. (let ((org-odd-levels-only odd-levels))
  5677. (org-mode)
  5678. (font-lock-fontify-buffer)
  5679. (buffer-string))))
  5680. (defvar org-m nil)
  5681. (defvar org-l nil)
  5682. (defvar org-f nil)
  5683. (defun org-get-level-face (n)
  5684. "Get the right face for match N in font-lock matching of headlines."
  5685. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5686. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5687. (if org-cycle-level-faces
  5688. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5689. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5690. (cond
  5691. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5692. ((eq n 2) org-f)
  5693. (t (if org-level-color-stars-only nil org-f))))
  5694. (defun org-get-todo-face (kwd)
  5695. "Get the right face for a TODO keyword KWD.
  5696. If KWD is a number, get the corresponding match group."
  5697. (if (numberp kwd) (setq kwd (match-string kwd)))
  5698. (or (org-face-from-face-or-color
  5699. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5700. (and (member kwd org-done-keywords) 'org-done)
  5701. 'org-todo))
  5702. (defun org-face-from-face-or-color (context inherit face-or-color)
  5703. "Create a face list that inherits INHERIT, but sets the foreground color.
  5704. When FACE-OR-COLOR is not a string, just return it."
  5705. (if (stringp face-or-color)
  5706. (list :inherit inherit
  5707. (cdr (assoc context org-faces-easy-properties))
  5708. face-or-color)
  5709. face-or-color))
  5710. (defun org-font-lock-add-tag-faces (limit)
  5711. "Add the special tag faces."
  5712. (when (and org-tag-faces org-tags-special-faces-re)
  5713. (while (re-search-forward org-tags-special-faces-re limit t)
  5714. (add-text-properties (match-beginning 1) (match-end 1)
  5715. (list 'face (org-get-tag-face 1)
  5716. 'font-lock-fontified t))
  5717. (backward-char 1))))
  5718. (defun org-font-lock-add-priority-faces (limit)
  5719. "Add the special priority faces."
  5720. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5721. (when (save-match-data (org-at-heading-p))
  5722. (add-text-properties
  5723. (match-beginning 0) (match-end 0)
  5724. (list 'face (or (org-face-from-face-or-color
  5725. 'priority 'org-priority
  5726. (cdr (assoc (char-after (match-beginning 1))
  5727. org-priority-faces)))
  5728. 'org-priority)
  5729. 'font-lock-fontified t)))))
  5730. (defun org-get-tag-face (kwd)
  5731. "Get the right face for a TODO keyword KWD.
  5732. If KWD is a number, get the corresponding match group."
  5733. (if (numberp kwd) (setq kwd (match-string kwd)))
  5734. (or (org-face-from-face-or-color
  5735. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5736. 'org-tag))
  5737. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5738. "Remove fontification and activation overlays from links."
  5739. (font-lock-default-unfontify-region beg end)
  5740. (let* ((buffer-undo-list t)
  5741. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5742. (inhibit-modification-hooks t)
  5743. deactivate-mark buffer-file-name buffer-file-truename)
  5744. (org-decompose-region beg end)
  5745. (remove-text-properties beg end
  5746. '(mouse-face t keymap t org-linked-text t
  5747. invisible t intangible t
  5748. org-no-flyspell t org-emphasis t))
  5749. (org-remove-font-lock-display-properties beg end)))
  5750. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5751. ((raise 0.3) (height 0.7))
  5752. ((raise -0.5))
  5753. ((raise 0.5)))
  5754. "Display properties for showing superscripts and subscripts.")
  5755. (defun org-remove-font-lock-display-properties (beg end)
  5756. "Remove specific display properties that have been added by font lock.
  5757. The will remove the raise properties that are used to show superscripts
  5758. and subscripts."
  5759. (let (next prop)
  5760. (while (< beg end)
  5761. (setq next (next-single-property-change beg 'display nil end)
  5762. prop (get-text-property beg 'display))
  5763. (if (member prop org-script-display)
  5764. (put-text-property beg next 'display nil))
  5765. (setq beg next))))
  5766. (defun org-raise-scripts (limit)
  5767. "Add raise properties to sub/superscripts."
  5768. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5769. (if (re-search-forward
  5770. (if (eq org-use-sub-superscripts t)
  5771. org-match-substring-regexp
  5772. org-match-substring-with-braces-regexp)
  5773. limit t)
  5774. (let* ((pos (point)) table-p comment-p
  5775. (mpos (match-beginning 3))
  5776. (emph-p (get-text-property mpos 'org-emphasis))
  5777. (link-p (get-text-property mpos 'mouse-face))
  5778. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5779. (goto-char (point-at-bol))
  5780. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5781. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5782. (goto-char pos)
  5783. ;; Handle a_b^c
  5784. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5785. (if (or comment-p emph-p link-p keyw-p)
  5786. t
  5787. (put-text-property (match-beginning 3) (match-end 0)
  5788. 'display
  5789. (if (equal (char-after (match-beginning 2)) ?^)
  5790. (nth (if table-p 3 1) org-script-display)
  5791. (nth (if table-p 2 0) org-script-display)))
  5792. (add-text-properties (match-beginning 2) (match-end 2)
  5793. (list 'invisible t
  5794. 'org-dwidth t 'org-dwidth-n 1))
  5795. (if (and (eq (char-after (match-beginning 3)) ?{)
  5796. (eq (char-before (match-end 3)) ?}))
  5797. (progn
  5798. (add-text-properties
  5799. (match-beginning 3) (1+ (match-beginning 3))
  5800. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5801. (add-text-properties
  5802. (1- (match-end 3)) (match-end 3)
  5803. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5804. t)))))
  5805. ;;;; Visibility cycling, including org-goto and indirect buffer
  5806. ;;; Cycling
  5807. (defvar org-cycle-global-status nil)
  5808. (make-variable-buffer-local 'org-cycle-global-status)
  5809. (put 'org-cycle-global-status 'org-state t)
  5810. (defvar org-cycle-subtree-status nil)
  5811. (make-variable-buffer-local 'org-cycle-subtree-status)
  5812. (put 'org-cycle-subtree-status 'org-state t)
  5813. (defvar org-inlinetask-min-level)
  5814. ;;;###autoload
  5815. (defun org-cycle (&optional arg)
  5816. "TAB-action and visibility cycling for Org-mode.
  5817. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5818. is outline visibility cycling, but it also invokes other actions
  5819. in special contexts.
  5820. - When this function is called with a prefix argument, rotate the entire
  5821. buffer through 3 states (global cycling)
  5822. 1. OVERVIEW: Show only top-level headlines.
  5823. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5824. 3. SHOW ALL: Show everything.
  5825. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5826. determined by the variable `org-startup-folded', and by any VISIBILITY
  5827. properties in the buffer.
  5828. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5829. including any drawers.
  5830. - When inside a table, re-align the table and move to the next field.
  5831. - When point is at the beginning of a headline, rotate the subtree started
  5832. by this line through 3 different states (local cycling)
  5833. 1. FOLDED: Only the main headline is shown.
  5834. 2. CHILDREN: The main headline and the direct children are shown.
  5835. From this state, you can move to one of the children
  5836. and zoom in further.
  5837. 3. SUBTREE: Show the entire subtree, including body text.
  5838. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5839. - When point is at the beginning of an empty headline and the variable
  5840. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5841. of the headline by demoting and promoting it to likely levels. This
  5842. speeds up creation document structure by pressing TAB once or several
  5843. times right after creating a new headline.
  5844. - When there is a numeric prefix, go up to a heading with level ARG, do
  5845. a `show-subtree' and return to the previous cursor position. If ARG
  5846. is negative, go up that many levels.
  5847. - When point is not at the beginning of a headline, execute the global
  5848. binding for TAB, which is re-indenting the line. See the option
  5849. `org-cycle-emulate-tab' for details.
  5850. - Special case: if point is at the beginning of the buffer and there is
  5851. no headline in line 1, this function will act as if called with prefix arg
  5852. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5853. But only if also the variable `org-cycle-global-at-bob' is t."
  5854. (interactive "P")
  5855. (org-load-modules-maybe)
  5856. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5857. (and org-cycle-level-after-item/entry-creation
  5858. (or (org-cycle-level)
  5859. (org-cycle-item-indentation))))
  5860. (let* (message-log-max ; Don't populate the *Messages* buffer
  5861. (limit-level
  5862. (or org-cycle-max-level
  5863. (and (boundp 'org-inlinetask-min-level)
  5864. org-inlinetask-min-level
  5865. (1- org-inlinetask-min-level))))
  5866. (nstars (and limit-level
  5867. (if org-odd-levels-only
  5868. (and limit-level (1- (* limit-level 2)))
  5869. limit-level)))
  5870. (org-outline-regexp
  5871. (if (not (derived-mode-p 'org-mode))
  5872. outline-regexp
  5873. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5874. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5875. (not (looking-at org-outline-regexp))))
  5876. (org-cycle-hook
  5877. (if bob-special
  5878. (delq 'org-optimize-window-after-visibility-change
  5879. (copy-sequence org-cycle-hook))
  5880. org-cycle-hook))
  5881. (pos (point)))
  5882. (if (or bob-special (equal arg '(4)))
  5883. ;; special case: use global cycling
  5884. (setq arg t))
  5885. (cond
  5886. ((equal arg '(16))
  5887. (setq last-command 'dummy)
  5888. (org-set-startup-visibility)
  5889. (message "Startup visibility, plus VISIBILITY properties"))
  5890. ((equal arg '(64))
  5891. (show-all)
  5892. (message "Entire buffer visible, including drawers"))
  5893. ;; Table: enter it or move to the next field.
  5894. ((org-at-table-p 'any)
  5895. (if (org-at-table.el-p)
  5896. (message "Use C-c ' to edit table.el tables")
  5897. (if arg (org-table-edit-field t)
  5898. (org-table-justify-field-maybe)
  5899. (call-interactively 'org-table-next-field))))
  5900. ((run-hook-with-args-until-success
  5901. 'org-tab-after-check-for-table-hook))
  5902. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5903. ((eq arg t) (org-cycle-internal-global))
  5904. ;; Drawers: delegate to `org-flag-drawer'.
  5905. ((and org-drawers org-drawer-regexp
  5906. (save-excursion
  5907. (beginning-of-line 1)
  5908. (looking-at org-drawer-regexp)))
  5909. (org-flag-drawer ; toggle block visibility
  5910. (not (get-char-property (match-end 0) 'invisible))))
  5911. ;; Show-subtree, ARG levels up from here.
  5912. ((integerp arg)
  5913. (save-excursion
  5914. (org-back-to-heading)
  5915. (outline-up-heading (if (< arg 0) (- arg)
  5916. (- (funcall outline-level) arg)))
  5917. (org-show-subtree)))
  5918. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5919. ((and (featurep 'org-inlinetask)
  5920. (org-inlinetask-at-task-p)
  5921. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5922. (org-inlinetask-toggle-visibility))
  5923. ((org-try-cdlatex-tab))
  5924. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5925. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5926. (save-excursion (beginning-of-line 1)
  5927. (looking-at org-outline-regexp)))
  5928. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5929. (org-cycle-internal-local))
  5930. ;; From there: TAB emulation and template completion.
  5931. (buffer-read-only (org-back-to-heading))
  5932. ((run-hook-with-args-until-success
  5933. 'org-tab-after-check-for-cycling-hook))
  5934. ((org-try-structure-completion))
  5935. ((run-hook-with-args-until-success
  5936. 'org-tab-before-tab-emulation-hook))
  5937. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5938. (or (not (bolp))
  5939. (not (looking-at org-outline-regexp))))
  5940. (call-interactively (global-key-binding "\t")))
  5941. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5942. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5943. (or (and (eq org-cycle-emulate-tab 'white)
  5944. (= (match-end 0) (point-at-eol)))
  5945. (and (eq org-cycle-emulate-tab 'whitestart)
  5946. (>= (match-end 0) pos))))
  5947. t
  5948. (eq org-cycle-emulate-tab t))
  5949. (call-interactively (global-key-binding "\t")))
  5950. (t (save-excursion
  5951. (org-back-to-heading)
  5952. (org-cycle)))))))
  5953. (defun org-cycle-internal-global ()
  5954. "Do the global cycling action."
  5955. ;; Hack to avoid display of messages for .org attachments in Gnus
  5956. (let (message-log-max ; Don't populate the *Messages* buffer
  5957. (ga (string-match "\\*fontification" (buffer-name))))
  5958. (cond
  5959. ((and (eq last-command this-command)
  5960. (eq org-cycle-global-status 'overview))
  5961. ;; We just created the overview - now do table of contents
  5962. ;; This can be slow in very large buffers, so indicate action
  5963. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5964. (unless ga (message "CONTENTS..."))
  5965. (org-content)
  5966. (unless ga (message "CONTENTS...done"))
  5967. (setq org-cycle-global-status 'contents)
  5968. (run-hook-with-args 'org-cycle-hook 'contents))
  5969. ((and (eq last-command this-command)
  5970. (eq org-cycle-global-status 'contents))
  5971. ;; We just showed the table of contents - now show everything
  5972. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5973. (show-all)
  5974. (unless ga (message "SHOW ALL"))
  5975. (setq org-cycle-global-status 'all)
  5976. (run-hook-with-args 'org-cycle-hook 'all))
  5977. (t
  5978. ;; Default action: go to overview
  5979. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5980. (org-overview)
  5981. (unless ga (message "OVERVIEW"))
  5982. (setq org-cycle-global-status 'overview)
  5983. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5984. (defun org-cycle-internal-local ()
  5985. "Do the local cycling action."
  5986. (let (message-log-max ; Don't populate the *Messages* buffer
  5987. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5988. ;; First, determine end of headline (EOH), end of subtree or item
  5989. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5990. (save-excursion
  5991. (if (org-at-item-p)
  5992. (progn
  5993. (beginning-of-line)
  5994. (setq struct (org-list-struct))
  5995. (setq eoh (point-at-eol))
  5996. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5997. (setq has-children (org-list-has-child-p (point) struct)))
  5998. (org-back-to-heading)
  5999. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6000. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  6001. (setq has-children
  6002. (or (save-excursion
  6003. (let ((level (funcall outline-level)))
  6004. (outline-next-heading)
  6005. (and (org-at-heading-p t)
  6006. (> (funcall outline-level) level))))
  6007. (save-excursion
  6008. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6009. ;; Determine end invisible part of buffer (EOL)
  6010. (beginning-of-line 2)
  6011. ;; XEmacs doesn't have `next-single-char-property-change'
  6012. (if (featurep 'xemacs)
  6013. (while (and (not (eobp)) ;; this is like `next-line'
  6014. (get-char-property (1- (point)) 'invisible))
  6015. (beginning-of-line 2))
  6016. (while (and (not (eobp)) ;; this is like `next-line'
  6017. (get-char-property (1- (point)) 'invisible))
  6018. (goto-char (next-single-char-property-change (point) 'invisible))
  6019. (and (eolp) (beginning-of-line 2))))
  6020. (setq eol (point)))
  6021. ;; Find out what to do next and set `this-command'
  6022. (cond
  6023. ((= eos eoh)
  6024. ;; Nothing is hidden behind this heading
  6025. (unless (org-before-first-heading-p)
  6026. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6027. (message "EMPTY ENTRY")
  6028. (setq org-cycle-subtree-status nil)
  6029. (save-excursion
  6030. (goto-char eos)
  6031. (outline-next-heading)
  6032. (if (outline-invisible-p) (org-flag-heading nil))))
  6033. ((and (or (>= eol eos)
  6034. (not (string-match "\\S-" (buffer-substring eol eos))))
  6035. (or has-children
  6036. (not (setq children-skipped
  6037. org-cycle-skip-children-state-if-no-children))))
  6038. ;; Entire subtree is hidden in one line: children view
  6039. (unless (org-before-first-heading-p)
  6040. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6041. (if (org-at-item-p)
  6042. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6043. (org-show-entry)
  6044. (org-with-limited-levels (show-children))
  6045. ;; FIXME: This slows down the func way too much.
  6046. ;; How keep drawers hidden in subtree anyway?
  6047. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6048. ;; (org-cycle-hide-drawers 'subtree))
  6049. ;; Fold every list in subtree to top-level items.
  6050. (when (eq org-cycle-include-plain-lists 'integrate)
  6051. (save-excursion
  6052. (org-back-to-heading)
  6053. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6054. (beginning-of-line 1)
  6055. (let* ((struct (org-list-struct))
  6056. (prevs (org-list-prevs-alist struct))
  6057. (end (org-list-get-bottom-point struct)))
  6058. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6059. (org-list-get-all-items (point) struct prevs))
  6060. (goto-char (if (< end eos) end eos)))))))
  6061. (message "CHILDREN")
  6062. (save-excursion
  6063. (goto-char eos)
  6064. (outline-next-heading)
  6065. (if (outline-invisible-p) (org-flag-heading nil)))
  6066. (setq org-cycle-subtree-status 'children)
  6067. (unless (org-before-first-heading-p)
  6068. (run-hook-with-args 'org-cycle-hook 'children)))
  6069. ((or children-skipped
  6070. (and (eq last-command this-command)
  6071. (eq org-cycle-subtree-status 'children)))
  6072. ;; We just showed the children, or no children are there,
  6073. ;; now show everything.
  6074. (unless (org-before-first-heading-p)
  6075. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6076. (outline-flag-region eoh eos nil)
  6077. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6078. (setq org-cycle-subtree-status 'subtree)
  6079. (unless (org-before-first-heading-p)
  6080. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6081. (t
  6082. ;; Default action: hide the subtree.
  6083. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6084. (outline-flag-region eoh eos t)
  6085. (message "FOLDED")
  6086. (setq org-cycle-subtree-status 'folded)
  6087. (unless (org-before-first-heading-p)
  6088. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6089. ;;;###autoload
  6090. (defun org-global-cycle (&optional arg)
  6091. "Cycle the global visibility. For details see `org-cycle'.
  6092. With \\[universal-argument] prefix arg, switch to startup visibility.
  6093. With a numeric prefix, show all headlines up to that level."
  6094. (interactive "P")
  6095. (let ((org-cycle-include-plain-lists
  6096. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6097. (cond
  6098. ((integerp arg)
  6099. (show-all)
  6100. (hide-sublevels arg)
  6101. (setq org-cycle-global-status 'contents))
  6102. ((equal arg '(4))
  6103. (org-set-startup-visibility)
  6104. (message "Startup visibility, plus VISIBILITY properties."))
  6105. (t
  6106. (org-cycle '(4))))))
  6107. (defun org-set-startup-visibility ()
  6108. "Set the visibility required by startup options and properties."
  6109. (cond
  6110. ((eq org-startup-folded t)
  6111. (org-cycle '(4)))
  6112. ((eq org-startup-folded 'content)
  6113. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6114. (org-cycle '(4)) (org-cycle '(4)))))
  6115. (unless (eq org-startup-folded 'showeverything)
  6116. (if org-hide-block-startup (org-hide-block-all))
  6117. (org-set-visibility-according-to-property 'no-cleanup)
  6118. (org-cycle-hide-archived-subtrees 'all)
  6119. (org-cycle-hide-drawers 'all)
  6120. (org-cycle-show-empty-lines t)))
  6121. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6122. "Switch subtree visibilities according to :VISIBILITY: property."
  6123. (interactive)
  6124. (let (org-show-entry-below state)
  6125. (save-excursion
  6126. (goto-char (point-min))
  6127. (while (re-search-forward
  6128. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6129. nil t)
  6130. (setq state (match-string 1))
  6131. (save-excursion
  6132. (org-back-to-heading t)
  6133. (hide-subtree)
  6134. (org-reveal)
  6135. (cond
  6136. ((equal state '("fold" "folded"))
  6137. (hide-subtree))
  6138. ((equal state "children")
  6139. (org-show-hidden-entry)
  6140. (show-children))
  6141. ((equal state "content")
  6142. (save-excursion
  6143. (save-restriction
  6144. (org-narrow-to-subtree)
  6145. (org-content))))
  6146. ((member state '("all" "showall"))
  6147. (show-subtree)))))
  6148. (unless no-cleanup
  6149. (org-cycle-hide-archived-subtrees 'all)
  6150. (org-cycle-hide-drawers 'all)
  6151. (org-cycle-show-empty-lines 'all)))))
  6152. ;; This function uses outline-regexp instead of the more fundamental
  6153. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6154. ;; buffers, where outline-regexp is needed.
  6155. (defun org-overview ()
  6156. "Switch to overview mode, showing only top-level headlines.
  6157. Really, this shows all headlines with level equal or greater than the level
  6158. of the first headline in the buffer. This is important, because if the
  6159. first headline is not level one, then (hide-sublevels 1) gives confusing
  6160. results."
  6161. (interactive)
  6162. (let ((l (org-current-line))
  6163. (level (save-excursion
  6164. (goto-char (point-min))
  6165. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6166. (progn
  6167. (goto-char (match-beginning 0))
  6168. (funcall outline-level))))))
  6169. (and level (hide-sublevels level))
  6170. (recenter '(4))
  6171. (org-goto-line l)))
  6172. (defun org-content (&optional arg)
  6173. "Show all headlines in the buffer, like a table of contents.
  6174. With numerical argument N, show content up to level N."
  6175. (interactive "P")
  6176. (save-excursion
  6177. ;; Visit all headings and show their offspring
  6178. (and (integerp arg) (org-overview))
  6179. (goto-char (point-max))
  6180. (catch 'exit
  6181. (while (and (progn (condition-case nil
  6182. (outline-previous-visible-heading 1)
  6183. (error (goto-char (point-min))))
  6184. t)
  6185. (looking-at org-outline-regexp))
  6186. (if (integerp arg)
  6187. (show-children (1- arg))
  6188. (show-branches))
  6189. (if (bobp) (throw 'exit nil))))))
  6190. (defun org-optimize-window-after-visibility-change (state)
  6191. "Adjust the window after a change in outline visibility.
  6192. This function is the default value of the hook `org-cycle-hook'."
  6193. (when (get-buffer-window (current-buffer))
  6194. (cond
  6195. ((eq state 'content) nil)
  6196. ((eq state 'all) nil)
  6197. ((eq state 'folded) nil)
  6198. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6199. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6200. (defun org-remove-empty-overlays-at (pos)
  6201. "Remove outline overlays that do not contain non-white stuff."
  6202. (mapc
  6203. (lambda (o)
  6204. (and (eq 'outline (overlay-get o 'invisible))
  6205. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6206. (overlay-end o))))
  6207. (delete-overlay o)))
  6208. (overlays-at pos)))
  6209. (defun org-clean-visibility-after-subtree-move ()
  6210. "Fix visibility issues after moving a subtree."
  6211. ;; First, find a reasonable region to look at:
  6212. ;; Start two siblings above, end three below
  6213. (let* ((beg (save-excursion
  6214. (and (org-get-last-sibling)
  6215. (org-get-last-sibling))
  6216. (point)))
  6217. (end (save-excursion
  6218. (and (org-get-next-sibling)
  6219. (org-get-next-sibling)
  6220. (org-get-next-sibling))
  6221. (if (org-at-heading-p)
  6222. (point-at-eol)
  6223. (point))))
  6224. (level (looking-at "\\*+"))
  6225. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6226. (save-excursion
  6227. (save-restriction
  6228. (narrow-to-region beg end)
  6229. (when re
  6230. ;; Properly fold already folded siblings
  6231. (goto-char (point-min))
  6232. (while (re-search-forward re nil t)
  6233. (if (and (not (outline-invisible-p))
  6234. (save-excursion
  6235. (goto-char (point-at-eol)) (outline-invisible-p)))
  6236. (hide-entry))))
  6237. (org-cycle-show-empty-lines 'overview)
  6238. (org-cycle-hide-drawers 'overview)))))
  6239. (defun org-cycle-show-empty-lines (state)
  6240. "Show empty lines above all visible headlines.
  6241. The region to be covered depends on STATE when called through
  6242. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6243. entire buffer covered. Note that an empty line is only shown if there
  6244. are at least `org-cycle-separator-lines' empty lines before the headline."
  6245. (when (not (= org-cycle-separator-lines 0))
  6246. (save-excursion
  6247. (let* ((n (abs org-cycle-separator-lines))
  6248. (re (cond
  6249. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6250. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6251. (t (let ((ns (number-to-string (- n 2))))
  6252. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6253. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6254. beg end b e)
  6255. (cond
  6256. ((memq state '(overview contents t))
  6257. (setq beg (point-min) end (point-max)))
  6258. ((memq state '(children folded))
  6259. (setq beg (point) end (progn (org-end-of-subtree t t)
  6260. (beginning-of-line 2)
  6261. (point)))))
  6262. (when beg
  6263. (goto-char beg)
  6264. (while (re-search-forward re end t)
  6265. (unless (get-char-property (match-end 1) 'invisible)
  6266. (setq e (match-end 1))
  6267. (if (< org-cycle-separator-lines 0)
  6268. (setq b (save-excursion
  6269. (goto-char (match-beginning 0))
  6270. (org-back-over-empty-lines)
  6271. (if (save-excursion
  6272. (goto-char (max (point-min) (1- (point))))
  6273. (org-at-heading-p))
  6274. (1- (point))
  6275. (point))))
  6276. (setq b (match-beginning 1)))
  6277. (outline-flag-region b e nil)))))))
  6278. ;; Never hide empty lines at the end of the file.
  6279. (save-excursion
  6280. (goto-char (point-max))
  6281. (outline-previous-heading)
  6282. (outline-end-of-heading)
  6283. (if (and (looking-at "[ \t\n]+")
  6284. (= (match-end 0) (point-max)))
  6285. (outline-flag-region (point) (match-end 0) nil))))
  6286. (defun org-show-empty-lines-in-parent ()
  6287. "Move to the parent and re-show empty lines before visible headlines."
  6288. (save-excursion
  6289. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6290. (org-cycle-show-empty-lines context))))
  6291. (defun org-files-list ()
  6292. "Return `org-agenda-files' list, plus all open org-mode files.
  6293. This is useful for operations that need to scan all of a user's
  6294. open and agenda-wise Org files."
  6295. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6296. (dolist (buf (buffer-list))
  6297. (with-current-buffer buf
  6298. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6299. (let ((file (expand-file-name (buffer-file-name))))
  6300. (unless (member file files)
  6301. (push file files))))))
  6302. files))
  6303. (defsubst org-entry-beginning-position ()
  6304. "Return the beginning position of the current entry."
  6305. (save-excursion (outline-back-to-heading t) (point)))
  6306. (defsubst org-entry-end-position ()
  6307. "Return the end position of the current entry."
  6308. (save-excursion (outline-next-heading) (point)))
  6309. (defun org-cycle-hide-drawers (state)
  6310. "Re-hide all drawers after a visibility state change."
  6311. (when (and (derived-mode-p 'org-mode)
  6312. (not (memq state '(overview folded contents))))
  6313. (save-excursion
  6314. (let* ((globalp (memq state '(contents all)))
  6315. (beg (if globalp (point-min) (point)))
  6316. (end (if globalp (point-max)
  6317. (if (eq state 'children)
  6318. (save-excursion (outline-next-heading) (point))
  6319. (org-end-of-subtree t)))))
  6320. (goto-char beg)
  6321. (while (re-search-forward org-drawer-regexp end t)
  6322. (org-flag-drawer t))))))
  6323. (defun org-cycle-hide-inline-tasks (state)
  6324. "Re-hide inline task when switching to 'contents visibility state."
  6325. (when (and (eq state 'contents)
  6326. (boundp 'org-inlinetask-min-level)
  6327. org-inlinetask-min-level)
  6328. (hide-sublevels (1- org-inlinetask-min-level))))
  6329. (defun org-flag-drawer (flag)
  6330. "When FLAG is non-nil, hide the drawer we are within.
  6331. Otherwise make it visible."
  6332. (save-excursion
  6333. (beginning-of-line 1)
  6334. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6335. (let ((b (match-end 0)))
  6336. (if (re-search-forward
  6337. "^[ \t]*:END:"
  6338. (save-excursion (outline-next-heading) (point)) t)
  6339. (outline-flag-region b (point-at-eol) flag)
  6340. (user-error ":END: line missing at position %s" b))))))
  6341. (defun org-subtree-end-visible-p ()
  6342. "Is the end of the current subtree visible?"
  6343. (pos-visible-in-window-p
  6344. (save-excursion (org-end-of-subtree t) (point))))
  6345. (defun org-first-headline-recenter (&optional N)
  6346. "Move cursor to the first headline and recenter the headline.
  6347. Optional argument N means put the headline into the Nth line of the window."
  6348. (goto-char (point-min))
  6349. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6350. (beginning-of-line)
  6351. (recenter (prefix-numeric-value N))))
  6352. ;;; Saving and restoring visibility
  6353. (defun org-outline-overlay-data (&optional use-markers)
  6354. "Return a list of the locations of all outline overlays.
  6355. These are overlays with the `invisible' property value `outline'.
  6356. The return value is a list of cons cells, with start and stop
  6357. positions for each overlay.
  6358. If USE-MARKERS is set, return the positions as markers."
  6359. (let (beg end)
  6360. (save-excursion
  6361. (save-restriction
  6362. (widen)
  6363. (delq nil
  6364. (mapcar (lambda (o)
  6365. (when (eq (overlay-get o 'invisible) 'outline)
  6366. (setq beg (overlay-start o)
  6367. end (overlay-end o))
  6368. (and beg end (> end beg)
  6369. (if use-markers
  6370. (cons (move-marker (make-marker) beg)
  6371. (move-marker (make-marker) end))
  6372. (cons beg end)))))
  6373. (overlays-in (point-min) (point-max))))))))
  6374. (defun org-set-outline-overlay-data (data)
  6375. "Create visibility overlays for all positions in DATA.
  6376. DATA should have been made by `org-outline-overlay-data'."
  6377. (let (o)
  6378. (save-excursion
  6379. (save-restriction
  6380. (widen)
  6381. (show-all)
  6382. (mapc (lambda (c)
  6383. (outline-flag-region (car c) (cdr c) t))
  6384. data)))))
  6385. ;;; Folding of blocks
  6386. (defvar org-hide-block-overlays nil
  6387. "Overlays hiding blocks.")
  6388. (make-variable-buffer-local 'org-hide-block-overlays)
  6389. (defun org-block-map (function &optional start end)
  6390. "Call FUNCTION at the head of all source blocks in the current buffer.
  6391. Optional arguments START and END can be used to limit the range."
  6392. (let ((start (or start (point-min)))
  6393. (end (or end (point-max))))
  6394. (save-excursion
  6395. (goto-char start)
  6396. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6397. (save-excursion
  6398. (save-match-data
  6399. (goto-char (match-beginning 0))
  6400. (funcall function)))))))
  6401. (defun org-hide-block-toggle-all ()
  6402. "Toggle the visibility of all blocks in the current buffer."
  6403. (org-block-map #'org-hide-block-toggle))
  6404. (defun org-hide-block-all ()
  6405. "Fold all blocks in the current buffer."
  6406. (interactive)
  6407. (org-show-block-all)
  6408. (org-block-map #'org-hide-block-toggle-maybe))
  6409. (defun org-show-block-all ()
  6410. "Unfold all blocks in the current buffer."
  6411. (interactive)
  6412. (mapc 'delete-overlay org-hide-block-overlays)
  6413. (setq org-hide-block-overlays nil))
  6414. (defun org-hide-block-toggle-maybe ()
  6415. "Toggle visibility of block at point."
  6416. (interactive)
  6417. (let ((case-fold-search t))
  6418. (if (save-excursion
  6419. (beginning-of-line 1)
  6420. (looking-at org-block-regexp))
  6421. (progn (org-hide-block-toggle)
  6422. t) ;; to signal that we took action
  6423. nil))) ;; to signal that we did not
  6424. (defun org-hide-block-toggle (&optional force)
  6425. "Toggle the visibility of the current block."
  6426. (interactive)
  6427. (save-excursion
  6428. (beginning-of-line)
  6429. (if (re-search-forward org-block-regexp nil t)
  6430. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6431. (end (match-end 0)) ;; end of entire body
  6432. ov)
  6433. (if (memq t (mapcar (lambda (overlay)
  6434. (eq (overlay-get overlay 'invisible)
  6435. 'org-hide-block))
  6436. (overlays-at start)))
  6437. (if (or (not force) (eq force 'off))
  6438. (mapc (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. (overlays-at start)))
  6446. (setq ov (make-overlay start end))
  6447. (overlay-put ov 'invisible 'org-hide-block)
  6448. ;; make the block accessible to isearch
  6449. (overlay-put
  6450. ov 'isearch-open-invisible
  6451. (lambda (ov)
  6452. (when (member ov org-hide-block-overlays)
  6453. (setq org-hide-block-overlays
  6454. (delq ov org-hide-block-overlays)))
  6455. (when (eq (overlay-get ov 'invisible)
  6456. 'org-hide-block)
  6457. (delete-overlay ov))))
  6458. (push ov org-hide-block-overlays)))
  6459. (user-error "Not looking at a source block"))))
  6460. ;; org-tab-after-check-for-cycling-hook
  6461. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6462. ;; Remove overlays when changing major mode
  6463. (add-hook 'org-mode-hook
  6464. (lambda () (org-add-hook 'change-major-mode-hook
  6465. 'org-show-block-all 'append 'local)))
  6466. ;;; Org-goto
  6467. (defvar org-goto-window-configuration nil)
  6468. (defvar org-goto-marker nil)
  6469. (defvar org-goto-map)
  6470. (defun org-goto-map ()
  6471. "Set the keymap `org-goto'."
  6472. (setq org-goto-map
  6473. (let ((map (make-sparse-keymap)))
  6474. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6475. mouse-drag-region universal-argument org-occur))
  6476. cmd)
  6477. (while (setq cmd (pop cmds))
  6478. (substitute-key-definition cmd cmd map global-map)))
  6479. (suppress-keymap map)
  6480. (org-defkey map "\C-m" 'org-goto-ret)
  6481. (org-defkey map [(return)] 'org-goto-ret)
  6482. (org-defkey map [(left)] 'org-goto-left)
  6483. (org-defkey map [(right)] 'org-goto-right)
  6484. (org-defkey map [(control ?g)] 'org-goto-quit)
  6485. (org-defkey map "\C-i" 'org-cycle)
  6486. (org-defkey map [(tab)] 'org-cycle)
  6487. (org-defkey map [(down)] 'outline-next-visible-heading)
  6488. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6489. (if org-goto-auto-isearch
  6490. (if (fboundp 'define-key-after)
  6491. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6492. nil)
  6493. (org-defkey map "q" 'org-goto-quit)
  6494. (org-defkey map "n" 'outline-next-visible-heading)
  6495. (org-defkey map "p" 'outline-previous-visible-heading)
  6496. (org-defkey map "f" 'outline-forward-same-level)
  6497. (org-defkey map "b" 'outline-backward-same-level)
  6498. (org-defkey map "u" 'outline-up-heading))
  6499. (org-defkey map "/" 'org-occur)
  6500. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6501. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6502. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6503. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6504. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6505. map)))
  6506. (defconst org-goto-help
  6507. "Browse buffer copy, to find location or copy text.%s
  6508. RET=jump to location C-g=quit and return to previous location
  6509. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6510. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6511. (defun org-goto (&optional alternative-interface)
  6512. "Look up a different location in the current file, keeping current visibility.
  6513. When you want look-up or go to a different location in a
  6514. document, the fastest way is often to fold the entire buffer and
  6515. then dive into the tree. This method has the disadvantage, that
  6516. the previous location will be folded, which may not be what you
  6517. want.
  6518. This command works around this by showing a copy of the current
  6519. buffer in an indirect buffer, in overview mode. You can dive
  6520. into the tree in that copy, use org-occur and incremental search
  6521. to find a location. When pressing RET or `Q', the command
  6522. returns to the original buffer in which the visibility is still
  6523. unchanged. After RET it will also jump to the location selected
  6524. in the indirect buffer and expose the headline hierarchy above.
  6525. With a prefix argument, use the alternative interface: e.g. if
  6526. `org-goto-interface' is 'outline use 'outline-path-completion."
  6527. (interactive "P")
  6528. (org-goto-map)
  6529. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6530. (org-refile-use-outline-path t)
  6531. (org-refile-target-verify-function nil)
  6532. (interface
  6533. (if (not alternative-interface)
  6534. org-goto-interface
  6535. (if (eq org-goto-interface 'outline)
  6536. 'outline-path-completion
  6537. 'outline)))
  6538. (org-goto-start-pos (point))
  6539. (selected-point
  6540. (if (eq interface 'outline)
  6541. (car (org-get-location (current-buffer) org-goto-help))
  6542. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6543. (org-refile-check-position pa)
  6544. (nth 3 pa)))))
  6545. (if selected-point
  6546. (progn
  6547. (org-mark-ring-push org-goto-start-pos)
  6548. (goto-char selected-point)
  6549. (if (or (outline-invisible-p) (org-invisible-p2))
  6550. (org-show-context 'org-goto)))
  6551. (message "Quit"))))
  6552. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6553. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6554. (defvar org-goto-local-auto-isearch-map) ; defined below
  6555. (defun org-get-location (buf help)
  6556. "Let the user select a location in the Org-mode buffer BUF.
  6557. This function uses a recursive edit. It returns the selected position
  6558. or nil."
  6559. (org-no-popups
  6560. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6561. (isearch-hide-immediately nil)
  6562. (isearch-search-fun-function
  6563. (lambda () 'org-goto-local-search-headings))
  6564. (org-goto-selected-point org-goto-exit-command))
  6565. (save-excursion
  6566. (save-window-excursion
  6567. (delete-other-windows)
  6568. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6569. (org-pop-to-buffer-same-window
  6570. (condition-case nil
  6571. (make-indirect-buffer (current-buffer) "*org-goto*")
  6572. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6573. (with-output-to-temp-buffer "*Org Help*"
  6574. (princ (format help (if org-goto-auto-isearch
  6575. " Just type for auto-isearch."
  6576. " n/p/f/b/u to navigate, q to quit."))))
  6577. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6578. (setq buffer-read-only nil)
  6579. (let ((org-startup-truncated t)
  6580. (org-startup-folded nil)
  6581. (org-startup-align-all-tables nil))
  6582. (org-mode)
  6583. (org-overview))
  6584. (setq buffer-read-only t)
  6585. (if (and (boundp 'org-goto-start-pos)
  6586. (integer-or-marker-p org-goto-start-pos))
  6587. (let ((org-show-hierarchy-above t)
  6588. (org-show-siblings t)
  6589. (org-show-following-heading t))
  6590. (goto-char org-goto-start-pos)
  6591. (and (outline-invisible-p) (org-show-context)))
  6592. (goto-char (point-min)))
  6593. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6594. (message "Select location and press RET")
  6595. (use-local-map org-goto-map)
  6596. (recursive-edit)))
  6597. (kill-buffer "*org-goto*")
  6598. (cons org-goto-selected-point org-goto-exit-command))))
  6599. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6600. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6601. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6602. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6603. (defun org-goto-local-search-headings (string bound noerror)
  6604. "Search and make sure that any matches are in headlines."
  6605. (catch 'return
  6606. (while (if isearch-forward
  6607. (search-forward string bound noerror)
  6608. (search-backward string bound noerror))
  6609. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6610. (and (member :headline context)
  6611. (not (member :tags context))))
  6612. (throw 'return (point))))))
  6613. (defun org-goto-local-auto-isearch ()
  6614. "Start isearch."
  6615. (interactive)
  6616. (goto-char (point-min))
  6617. (let ((keys (this-command-keys)))
  6618. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6619. (isearch-mode t)
  6620. (isearch-process-search-char (string-to-char keys)))))
  6621. (defun org-goto-ret (&optional arg)
  6622. "Finish `org-goto' by going to the new location."
  6623. (interactive "P")
  6624. (setq org-goto-selected-point (point)
  6625. org-goto-exit-command 'return)
  6626. (throw 'exit nil))
  6627. (defun org-goto-left ()
  6628. "Finish `org-goto' by going to the new location."
  6629. (interactive)
  6630. (if (org-at-heading-p)
  6631. (progn
  6632. (beginning-of-line 1)
  6633. (setq org-goto-selected-point (point)
  6634. org-goto-exit-command 'left)
  6635. (throw 'exit nil))
  6636. (user-error "Not on a heading")))
  6637. (defun org-goto-right ()
  6638. "Finish `org-goto' by going to the new location."
  6639. (interactive)
  6640. (if (org-at-heading-p)
  6641. (progn
  6642. (setq org-goto-selected-point (point)
  6643. org-goto-exit-command 'right)
  6644. (throw 'exit nil))
  6645. (user-error "Not on a heading")))
  6646. (defun org-goto-quit ()
  6647. "Finish `org-goto' without cursor motion."
  6648. (interactive)
  6649. (setq org-goto-selected-point nil)
  6650. (setq org-goto-exit-command 'quit)
  6651. (throw 'exit nil))
  6652. ;;; Indirect buffer display of subtrees
  6653. (defvar org-indirect-dedicated-frame nil
  6654. "This is the frame being used for indirect tree display.")
  6655. (defvar org-last-indirect-buffer nil)
  6656. (defun org-tree-to-indirect-buffer (&optional arg)
  6657. "Create indirect buffer and narrow it to current subtree.
  6658. With a numerical prefix ARG, go up to this level and then take that tree.
  6659. If ARG is negative, go up that many levels.
  6660. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6661. indirect buffer previously made with this command, to avoid proliferation of
  6662. indirect buffers. However, when you call the command with a \
  6663. \\[universal-argument] prefix, or
  6664. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6665. is kept so that you can work with several indirect buffers at the same time.
  6666. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6667. \\[universal-argument] prefix also
  6668. requests that a new frame be made for the new buffer, so that the dedicated
  6669. frame is not changed."
  6670. (interactive "P")
  6671. (let ((cbuf (current-buffer))
  6672. (cwin (selected-window))
  6673. (pos (point))
  6674. beg end level heading ibuf)
  6675. (save-excursion
  6676. (org-back-to-heading t)
  6677. (when (numberp arg)
  6678. (setq level (org-outline-level))
  6679. (if (< arg 0) (setq arg (+ level arg)))
  6680. (while (> (setq level (org-outline-level)) arg)
  6681. (org-up-heading-safe)))
  6682. (setq beg (point)
  6683. heading (org-get-heading))
  6684. (org-end-of-subtree t t)
  6685. (if (org-at-heading-p) (backward-char 1))
  6686. (setq end (point)))
  6687. (if (and (buffer-live-p org-last-indirect-buffer)
  6688. (not (eq org-indirect-buffer-display 'new-frame))
  6689. (not arg))
  6690. (kill-buffer org-last-indirect-buffer))
  6691. (setq ibuf (org-get-indirect-buffer cbuf)
  6692. org-last-indirect-buffer ibuf)
  6693. (cond
  6694. ((or (eq org-indirect-buffer-display 'new-frame)
  6695. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6696. (select-frame (make-frame))
  6697. (delete-other-windows)
  6698. (org-pop-to-buffer-same-window ibuf)
  6699. (org-set-frame-title heading))
  6700. ((eq org-indirect-buffer-display 'dedicated-frame)
  6701. (raise-frame
  6702. (select-frame (or (and org-indirect-dedicated-frame
  6703. (frame-live-p org-indirect-dedicated-frame)
  6704. org-indirect-dedicated-frame)
  6705. (setq org-indirect-dedicated-frame (make-frame)))))
  6706. (delete-other-windows)
  6707. (org-pop-to-buffer-same-window ibuf)
  6708. (org-set-frame-title (concat "Indirect: " heading)))
  6709. ((eq org-indirect-buffer-display 'current-window)
  6710. (org-pop-to-buffer-same-window ibuf))
  6711. ((eq org-indirect-buffer-display 'other-window)
  6712. (pop-to-buffer ibuf))
  6713. (t (error "Invalid value")))
  6714. (if (featurep 'xemacs)
  6715. (save-excursion (org-mode) (turn-on-font-lock)))
  6716. (narrow-to-region beg end)
  6717. (show-all)
  6718. (goto-char pos)
  6719. (run-hook-with-args 'org-cycle-hook 'all)
  6720. (and (window-live-p cwin) (select-window cwin))))
  6721. (defun org-get-indirect-buffer (&optional buffer)
  6722. (setq buffer (or buffer (current-buffer)))
  6723. (let ((n 1) (base (buffer-name buffer)) bname)
  6724. (while (buffer-live-p
  6725. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6726. (setq n (1+ n)))
  6727. (condition-case nil
  6728. (make-indirect-buffer buffer bname 'clone)
  6729. (error (make-indirect-buffer buffer bname)))))
  6730. (defun org-set-frame-title (title)
  6731. "Set the title of the current frame to the string TITLE."
  6732. ;; FIXME: how to name a single frame in XEmacs???
  6733. (unless (featurep 'xemacs)
  6734. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6735. ;;;; Structure editing
  6736. ;;; Inserting headlines
  6737. (defun org-previous-line-empty-p (&optional next)
  6738. "Is the previous line a blank line?
  6739. When NEXT is non-nil, check the next line instead."
  6740. (save-excursion
  6741. (and (not (bobp))
  6742. (or (beginning-of-line (if next 2 0)) t)
  6743. (save-match-data
  6744. (looking-at "[ \t]*$")))))
  6745. (defun org-insert-heading (&optional arg invisible-ok)
  6746. "Insert a new heading or item with same depth at point.
  6747. If point is in a plain list and ARG is nil, create a new list item.
  6748. With one universal prefix argument, insert a heading even in lists.
  6749. With two universal prefix arguments, insert the heading at the end
  6750. of the parent subtree.
  6751. If point is at the beginning of a headline, insert a sibling before
  6752. the current headline. If point is not at the beginning, split the line
  6753. and create a new headline with the text in the current line after point
  6754. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6755. If point is at the beginning of a normal line, turn this line into
  6756. a heading.
  6757. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6758. This is important for non-interactive uses of the command."
  6759. (interactive "P")
  6760. (if (org-called-interactively-p 'any) (org-reveal))
  6761. (let ((itemp (org-in-item-p))
  6762. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6763. (respect-content (or org-insert-heading-respect-content
  6764. (equal arg '(16))))
  6765. (initial-content "")
  6766. (adjust-empty-lines t))
  6767. (cond
  6768. ((or (= (buffer-size) 0)
  6769. (and (not (save-excursion
  6770. (and (ignore-errors (org-back-to-heading invisible-ok))
  6771. (org-at-heading-p))))
  6772. (or arg (not itemp))))
  6773. ;; At beginning of buffer or so hight up that only a heading makes sense.
  6774. (insert
  6775. (if (org-previous-line-empty-p) "" "\n")
  6776. (if (org-in-src-block-p) ",* " "* "))
  6777. (run-hooks 'org-insert-heading-hook))
  6778. ((and itemp (not (equal arg '(4))))
  6779. ;; Insert an item
  6780. (org-insert-item))
  6781. (t
  6782. ;; Insert a heading
  6783. (save-restriction
  6784. (widen)
  6785. (let* ((empty-line-p nil)
  6786. (level nil)
  6787. (on-heading (org-at-heading-p))
  6788. ;; Get a level to fall back on
  6789. (fix-level
  6790. (save-excursion
  6791. (org-back-to-heading t)
  6792. (looking-at org-outline-regexp)
  6793. (make-string (1- (length (match-string 0))) ?*)))
  6794. (stars
  6795. (save-excursion
  6796. (condition-case nil
  6797. (progn
  6798. (org-back-to-heading invisible-ok)
  6799. (when (and (not on-heading)
  6800. (featurep 'org-inlinetask)
  6801. (integerp org-inlinetask-min-level)
  6802. (>= (length (match-string 0))
  6803. org-inlinetask-min-level))
  6804. ;; Find a heading level before the inline task
  6805. (while (and (setq level (org-up-heading-safe))
  6806. (>= level org-inlinetask-min-level)))
  6807. (if (org-at-heading-p)
  6808. (org-back-to-heading invisible-ok)
  6809. (error "This should not happen")))
  6810. (unless (and (save-excursion
  6811. (save-match-data
  6812. (org-backward-heading-same-level 1 invisible-ok))
  6813. (= (point) (match-beginning 0)))
  6814. (not (org-previous-line-empty-p t)))
  6815. (setq empty-line-p (org-previous-line-empty-p)))
  6816. (match-string 0))
  6817. (error (or fix-level "* ")))))
  6818. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6819. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6820. pos hide-previous previous-pos)
  6821. ;; If we insert after content, move there and clean up whitespace
  6822. (when respect-content
  6823. (org-end-of-subtree nil t)
  6824. (when (looking-at "^\\*")
  6825. (backward-char 1)
  6826. (insert "\n")))
  6827. ;; If we are splitting, grab the text that should be moved to the new headline
  6828. (when may-split
  6829. (if (org-on-heading-p)
  6830. ;; This is a heading, we split intelligently (keeping tags)
  6831. (let ((pos (point)))
  6832. (goto-char (point-at-bol))
  6833. (unless (looking-at org-complex-heading-regexp)
  6834. (error "This should not happen"))
  6835. (when (and (match-beginning 4)
  6836. (> pos (match-beginning 4))
  6837. (< pos (match-end 4)))
  6838. (setq initial-content (buffer-substring pos (match-end 4)))
  6839. (goto-char pos)
  6840. (delete-region (point) (match-end 4))
  6841. (if (looking-at "[ \t]*$")
  6842. (replace-match "")
  6843. (insert (make-string (length initial-content) ?\ )))
  6844. (setq initial-content (org-trim initial-content)))
  6845. (goto-char pos))
  6846. ;; a normal line
  6847. (setq initial-content (buffer-substring (point) (point-at-eol)))
  6848. (delete-region (point) (point-at-eol))
  6849. (setq initial-content (org-trim initial-content))))
  6850. ;; If we are at the beginning of the line, insert before it. Else after
  6851. (cond
  6852. ((and (bolp) (looking-at "[ \t]*$")))
  6853. ((and (bolp) (not (looking-at "[ \t]*$")))
  6854. (open-line 1))
  6855. (t
  6856. (goto-char (point-at-eol))
  6857. (insert "\n")))
  6858. ;; Insert the new heading
  6859. (insert stars)
  6860. (just-one-space)
  6861. (insert initial-content)
  6862. (if adjust-empty-lines (org-N-empty-lines-before-current (if empty-line-p 1 0)))
  6863. (run-hooks 'org-insert-heading-hook)))))))
  6864. (defun org-N-empty-lines-before-current (N)
  6865. "Make the number of empty lines before current exactly N.
  6866. So this will delete or add empty lines."
  6867. (save-excursion
  6868. (goto-char (point-at-bol))
  6869. (if (looking-back "\\s-+" nil 'greedy)
  6870. (replace-match ""))
  6871. (or (bobp) (insert "\n"))
  6872. (while (> N 0)
  6873. (insert "\n")
  6874. (setq N (1- N)))))
  6875. (defun org-get-heading (&optional no-tags no-todo)
  6876. "Return the heading of the current entry, without the stars.
  6877. When NO-TAGS is non-nil, don't include tags.
  6878. When NO-TODO is non-nil, don't include TODO keywords."
  6879. (save-excursion
  6880. (org-back-to-heading t)
  6881. (cond
  6882. ((and no-tags no-todo)
  6883. (looking-at org-complex-heading-regexp)
  6884. (match-string 4))
  6885. (no-tags
  6886. (looking-at (concat org-outline-regexp
  6887. "\\(.*?\\)"
  6888. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6889. (match-string 1))
  6890. (no-todo
  6891. (looking-at org-todo-line-regexp)
  6892. (match-string 3))
  6893. (t (looking-at org-heading-regexp)
  6894. (match-string 2)))))
  6895. (defvar orgstruct-mode) ; defined below
  6896. (defun org-heading-components ()
  6897. "Return the components of the current heading.
  6898. This is a list with the following elements:
  6899. - the level as an integer
  6900. - the reduced level, different if `org-odd-levels-only' is set.
  6901. - the TODO keyword, or nil
  6902. - the priority character, like ?A, or nil if no priority is given
  6903. - the headline text itself, or the tags string if no headline text
  6904. - the tags string, or nil."
  6905. (save-excursion
  6906. (org-back-to-heading t)
  6907. (if (let (case-fold-search)
  6908. (looking-at
  6909. (if orgstruct-mode
  6910. org-heading-regexp
  6911. org-complex-heading-regexp)))
  6912. (if orgstruct-mode
  6913. (list (length (match-string 1))
  6914. (org-reduced-level (length (match-string 1)))
  6915. nil
  6916. nil
  6917. (match-string 2)
  6918. nil)
  6919. (list (length (match-string 1))
  6920. (org-reduced-level (length (match-string 1)))
  6921. (org-match-string-no-properties 2)
  6922. (and (match-end 3) (aref (match-string 3) 2))
  6923. (org-match-string-no-properties 4)
  6924. (org-match-string-no-properties 5))))))
  6925. (defun org-get-entry ()
  6926. "Get the entry text, after heading, entire subtree."
  6927. (save-excursion
  6928. (org-back-to-heading t)
  6929. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6930. (defun org-insert-heading-after-current ()
  6931. "Insert a new heading with same level as current, after current subtree."
  6932. (interactive)
  6933. (org-back-to-heading)
  6934. (org-insert-heading)
  6935. (org-move-subtree-down)
  6936. (end-of-line 1))
  6937. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6938. "Insert heading with `org-insert-heading-respect-content' set to t."
  6939. (interactive "P")
  6940. (let ((org-insert-heading-respect-content t))
  6941. (org-insert-heading '(4) invisible-ok)))
  6942. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6943. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6944. (interactive "P")
  6945. (let ((org-insert-heading-respect-content t))
  6946. (org-insert-todo-heading force-state t)))
  6947. (defun org-insert-todo-heading (arg &optional force-heading)
  6948. "Insert a new heading with the same level and TODO state as current heading.
  6949. If the heading has no TODO state, or if the state is DONE, use the first
  6950. state (TODO by default). Also one prefix arg, force first state. With two
  6951. prefix args, force inserting at the end of the parent subtree."
  6952. (interactive "P")
  6953. (when (or force-heading (not (org-insert-item 'checkbox)))
  6954. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6955. force-heading))
  6956. (save-excursion
  6957. (org-back-to-heading)
  6958. (outline-previous-heading)
  6959. (looking-at org-todo-line-regexp))
  6960. (let*
  6961. ((new-mark-x
  6962. (if (or arg
  6963. (not (match-beginning 2))
  6964. (member (match-string 2) org-done-keywords))
  6965. (car org-todo-keywords-1)
  6966. (match-string 2)))
  6967. (new-mark
  6968. (or
  6969. (run-hook-with-args-until-success
  6970. 'org-todo-get-default-hook new-mark-x nil)
  6971. new-mark-x)))
  6972. (beginning-of-line 1)
  6973. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6974. (if org-treat-insert-todo-heading-as-state-change
  6975. (org-todo new-mark)
  6976. (insert new-mark " "))))
  6977. (when org-provide-todo-statistics
  6978. (org-update-parent-todo-statistics))))
  6979. (defun org-insert-subheading (arg)
  6980. "Insert a new subheading and demote it.
  6981. Works for outline headings and for plain lists alike."
  6982. (interactive "P")
  6983. (org-insert-heading arg)
  6984. (cond
  6985. ((org-at-heading-p) (org-do-demote))
  6986. ((org-at-item-p) (org-indent-item))))
  6987. (defun org-insert-todo-subheading (arg)
  6988. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6989. Works for outline headings and for plain lists alike."
  6990. (interactive "P")
  6991. (org-insert-todo-heading arg)
  6992. (cond
  6993. ((org-at-heading-p) (org-do-demote))
  6994. ((org-at-item-p) (org-indent-item))))
  6995. ;;; Promotion and Demotion
  6996. (defvar org-after-demote-entry-hook nil
  6997. "Hook run after an entry has been demoted.
  6998. The cursor will be at the beginning of the entry.
  6999. When a subtree is being demoted, the hook will be called for each node.")
  7000. (defvar org-after-promote-entry-hook nil
  7001. "Hook run after an entry has been promoted.
  7002. The cursor will be at the beginning of the entry.
  7003. When a subtree is being promoted, the hook will be called for each node.")
  7004. (defun org-promote-subtree ()
  7005. "Promote the entire subtree.
  7006. See also `org-promote'."
  7007. (interactive)
  7008. (save-excursion
  7009. (org-with-limited-levels (org-map-tree 'org-promote)))
  7010. (org-fix-position-after-promote))
  7011. (defun org-demote-subtree ()
  7012. "Demote the entire subtree. See `org-demote'.
  7013. See also `org-promote'."
  7014. (interactive)
  7015. (save-excursion
  7016. (org-with-limited-levels (org-map-tree 'org-demote)))
  7017. (org-fix-position-after-promote))
  7018. (defun org-do-promote ()
  7019. "Promote the current heading higher up the tree.
  7020. If the region is active in `transient-mark-mode', promote all headings
  7021. in the region."
  7022. (interactive)
  7023. (save-excursion
  7024. (if (org-region-active-p)
  7025. (org-map-region 'org-promote (region-beginning) (region-end))
  7026. (org-promote)))
  7027. (org-fix-position-after-promote))
  7028. (defun org-do-demote ()
  7029. "Demote the current heading lower down the tree.
  7030. If the region is active in `transient-mark-mode', demote all headings
  7031. in the region."
  7032. (interactive)
  7033. (save-excursion
  7034. (if (org-region-active-p)
  7035. (org-map-region 'org-demote (region-beginning) (region-end))
  7036. (org-demote)))
  7037. (org-fix-position-after-promote))
  7038. (defun org-fix-position-after-promote ()
  7039. "Make sure that after pro/demotion cursor position is right."
  7040. (let ((pos (point)))
  7041. (when (save-excursion
  7042. (beginning-of-line 1)
  7043. (looking-at org-todo-line-regexp)
  7044. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7045. (cond ((eobp) (insert " "))
  7046. ((eolp) (insert " "))
  7047. ((equal (char-after) ?\ ) (forward-char 1))))))
  7048. (defun org-current-level ()
  7049. "Return the level of the current entry, or nil if before the first headline.
  7050. The level is the number of stars at the beginning of the headline."
  7051. (save-excursion
  7052. (org-with-limited-levels
  7053. (if (ignore-errors (org-back-to-heading t))
  7054. (funcall outline-level)))))
  7055. (defun org-get-previous-line-level ()
  7056. "Return the outline depth of the last headline before the current line.
  7057. Returns 0 for the first headline in the buffer, and nil if before the
  7058. first headline."
  7059. (let ((current-level (org-current-level))
  7060. (prev-level (when (> (line-number-at-pos) 1)
  7061. (save-excursion
  7062. (beginning-of-line 0)
  7063. (org-current-level)))))
  7064. (cond ((null current-level) nil) ; Before first headline
  7065. ((null prev-level) 0) ; At first headline
  7066. (prev-level))))
  7067. (defun org-reduced-level (l)
  7068. "Compute the effective level of a heading.
  7069. This takes into account the setting of `org-odd-levels-only'."
  7070. (cond
  7071. ((zerop l) 0)
  7072. (org-odd-levels-only (1+ (floor (/ l 2))))
  7073. (t l)))
  7074. (defun org-level-increment ()
  7075. "Return the number of stars that will be added or removed at a
  7076. time to headlines when structure editing, based on the value of
  7077. `org-odd-levels-only'."
  7078. (if org-odd-levels-only 2 1))
  7079. (defun org-get-valid-level (level &optional change)
  7080. "Rectify a level change under the influence of `org-odd-levels-only'
  7081. LEVEL is a current level, CHANGE is by how much the level should be
  7082. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7083. even level numbers will become the next higher odd number."
  7084. (if org-odd-levels-only
  7085. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7086. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7087. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7088. (max 1 (+ level (or change 0)))))
  7089. (if (boundp 'define-obsolete-function-alias)
  7090. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7091. (define-obsolete-function-alias 'org-get-legal-level
  7092. 'org-get-valid-level)
  7093. (define-obsolete-function-alias 'org-get-legal-level
  7094. 'org-get-valid-level "23.1")))
  7095. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7096. ;; ̀org-with-limited-levels'
  7097. (defun org-promote ()
  7098. "Promote the current heading higher up the tree.
  7099. If the region is active in `transient-mark-mode', promote all headings
  7100. in the region."
  7101. (org-back-to-heading t)
  7102. (let* ((level (save-match-data (funcall outline-level)))
  7103. (after-change-functions (remove 'flyspell-after-change-function
  7104. after-change-functions))
  7105. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7106. (diff (abs (- level (length up-head) -1))))
  7107. (cond ((and (= level 1) org-called-with-limited-levels
  7108. org-allow-promoting-top-level-subtree)
  7109. (replace-match "# " nil t))
  7110. ((= level 1)
  7111. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7112. (t (replace-match up-head nil t)))
  7113. ;; Fixup tag positioning
  7114. (unless (= level 1)
  7115. (and org-auto-align-tags (org-set-tags nil t))
  7116. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7117. (run-hooks 'org-after-promote-entry-hook)))
  7118. (defun org-demote ()
  7119. "Demote the current heading lower down the tree.
  7120. If the region is active in `transient-mark-mode', demote all headings
  7121. in the region."
  7122. (org-back-to-heading t)
  7123. (let* ((level (save-match-data (funcall outline-level)))
  7124. (after-change-functions (remove 'flyspell-after-change-function
  7125. after-change-functions))
  7126. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7127. (diff (abs (- level (length down-head) -1))))
  7128. (replace-match down-head nil t)
  7129. ;; Fixup tag positioning
  7130. (and org-auto-align-tags (org-set-tags nil t))
  7131. (if org-adapt-indentation (org-fixup-indentation diff))
  7132. (run-hooks 'org-after-demote-entry-hook)))
  7133. (defun org-cycle-level ()
  7134. "Cycle the level of an empty headline through possible states.
  7135. This goes first to child, then to parent, level, then up the hierarchy.
  7136. After top level, it switches back to sibling level."
  7137. (interactive)
  7138. (let ((org-adapt-indentation nil))
  7139. (when (org-point-at-end-of-empty-headline)
  7140. (setq this-command 'org-cycle-level) ; Only needed for caching
  7141. (let ((cur-level (org-current-level))
  7142. (prev-level (org-get-previous-line-level)))
  7143. (cond
  7144. ;; If first headline in file, promote to top-level.
  7145. ((= prev-level 0)
  7146. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7147. do (org-do-promote)))
  7148. ;; If same level as prev, demote one.
  7149. ((= prev-level cur-level)
  7150. (org-do-demote))
  7151. ;; If parent is top-level, promote to top level if not already.
  7152. ((= prev-level 1)
  7153. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7154. do (org-do-promote)))
  7155. ;; If top-level, return to prev-level.
  7156. ((= cur-level 1)
  7157. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7158. do (org-do-demote)))
  7159. ;; If less than prev-level, promote one.
  7160. ((< cur-level prev-level)
  7161. (org-do-promote))
  7162. ;; If deeper than prev-level, promote until higher than
  7163. ;; prev-level.
  7164. ((> cur-level prev-level)
  7165. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7166. do (org-do-promote))))
  7167. t))))
  7168. (defun org-map-tree (fun)
  7169. "Call FUN for every heading underneath the current one."
  7170. (org-back-to-heading)
  7171. (let ((level (funcall outline-level)))
  7172. (save-excursion
  7173. (funcall fun)
  7174. (while (and (progn
  7175. (outline-next-heading)
  7176. (> (funcall outline-level) level))
  7177. (not (eobp)))
  7178. (funcall fun)))))
  7179. (defun org-map-region (fun beg end)
  7180. "Call FUN for every heading between BEG and END."
  7181. (let ((org-ignore-region t))
  7182. (save-excursion
  7183. (setq end (copy-marker end))
  7184. (goto-char beg)
  7185. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7186. (< (point) end))
  7187. (funcall fun))
  7188. (while (and (progn
  7189. (outline-next-heading)
  7190. (< (point) end))
  7191. (not (eobp)))
  7192. (funcall fun)))))
  7193. (defvar org-property-end-re) ; silence byte-compiler
  7194. (defun org-fixup-indentation (diff)
  7195. "Change the indentation in the current entry by DIFF.
  7196. However, if any line in the current entry has no indentation, or if it
  7197. would end up with no indentation after the change, nothing at all is done."
  7198. (save-excursion
  7199. (let ((end (save-excursion (outline-next-heading)
  7200. (point-marker)))
  7201. (prohibit (if (> diff 0)
  7202. "^\\S-"
  7203. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7204. col)
  7205. (unless (save-excursion (end-of-line 1)
  7206. (re-search-forward prohibit end t))
  7207. (while (and (< (point) end)
  7208. (re-search-forward "^[ \t]+" end t))
  7209. (goto-char (match-end 0))
  7210. (setq col (current-column))
  7211. (if (< diff 0) (replace-match ""))
  7212. (org-indent-to-column (+ diff col))))
  7213. (move-marker end nil))))
  7214. (defun org-convert-to-odd-levels ()
  7215. "Convert an org-mode file with all levels allowed to one with odd levels.
  7216. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7217. level 5 etc."
  7218. (interactive)
  7219. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7220. (let ((outline-level 'org-outline-level)
  7221. (org-odd-levels-only nil) n)
  7222. (save-excursion
  7223. (goto-char (point-min))
  7224. (while (re-search-forward "^\\*\\*+ " nil t)
  7225. (setq n (- (length (match-string 0)) 2))
  7226. (while (>= (setq n (1- n)) 0)
  7227. (org-demote))
  7228. (end-of-line 1))))))
  7229. (defun org-convert-to-oddeven-levels ()
  7230. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7231. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7232. file contains a section with an even level, conversion would
  7233. destroy the structure of the file. An error is signaled in this
  7234. case."
  7235. (interactive)
  7236. (goto-char (point-min))
  7237. ;; First check if there are no even levels
  7238. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7239. (org-show-context t)
  7240. (error "Not all levels are odd in this file. Conversion not possible"))
  7241. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7242. (let ((outline-regexp org-outline-regexp)
  7243. (outline-level 'org-outline-level)
  7244. (org-odd-levels-only nil) n)
  7245. (save-excursion
  7246. (goto-char (point-min))
  7247. (while (re-search-forward "^\\*\\*+ " nil t)
  7248. (setq n (/ (1- (length (match-string 0))) 2))
  7249. (while (>= (setq n (1- n)) 0)
  7250. (org-promote))
  7251. (end-of-line 1))))))
  7252. (defun org-tr-level (n)
  7253. "Make N odd if required."
  7254. (if org-odd-levels-only (1+ (/ n 2)) n))
  7255. ;;; Vertical tree motion, cutting and pasting of subtrees
  7256. (defun org-move-subtree-up (&optional arg)
  7257. "Move the current subtree up past ARG headlines of the same level."
  7258. (interactive "p")
  7259. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7260. (defun org-move-subtree-down (&optional arg)
  7261. "Move the current subtree down past ARG headlines of the same level."
  7262. (interactive "p")
  7263. (setq arg (prefix-numeric-value arg))
  7264. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7265. 'org-get-last-sibling))
  7266. (ins-point (make-marker))
  7267. (cnt (abs arg))
  7268. (col (current-column))
  7269. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7270. ;; Select the tree
  7271. (org-back-to-heading)
  7272. (setq beg0 (point))
  7273. (save-excursion
  7274. (setq ne-beg (org-back-over-empty-lines))
  7275. (setq beg (point)))
  7276. (save-match-data
  7277. (save-excursion (outline-end-of-heading)
  7278. (setq folded (outline-invisible-p)))
  7279. (outline-end-of-subtree))
  7280. (outline-next-heading)
  7281. (setq ne-end (org-back-over-empty-lines))
  7282. (setq end (point))
  7283. (goto-char beg0)
  7284. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7285. ;; include less whitespace
  7286. (save-excursion
  7287. (goto-char beg)
  7288. (forward-line (- ne-beg ne-end))
  7289. (setq beg (point))))
  7290. ;; Find insertion point, with error handling
  7291. (while (> cnt 0)
  7292. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7293. (progn (goto-char beg0)
  7294. (user-error "Cannot move past superior level or buffer limit")))
  7295. (setq cnt (1- cnt)))
  7296. (if (> arg 0)
  7297. ;; Moving forward - still need to move over subtree
  7298. (progn (org-end-of-subtree t t)
  7299. (save-excursion
  7300. (org-back-over-empty-lines)
  7301. (or (bolp) (newline)))))
  7302. (setq ne-ins (org-back-over-empty-lines))
  7303. (move-marker ins-point (point))
  7304. (setq txt (buffer-substring beg end))
  7305. (org-save-markers-in-region beg end)
  7306. (delete-region beg end)
  7307. (org-remove-empty-overlays-at beg)
  7308. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7309. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7310. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7311. (let ((bbb (point)))
  7312. (insert-before-markers txt)
  7313. (org-reinstall-markers-in-region bbb)
  7314. (move-marker ins-point bbb))
  7315. (or (bolp) (insert "\n"))
  7316. (setq ins-end (point))
  7317. (goto-char ins-point)
  7318. (org-skip-whitespace)
  7319. (when (and (< arg 0)
  7320. (org-first-sibling-p)
  7321. (> ne-ins ne-beg))
  7322. ;; Move whitespace back to beginning
  7323. (save-excursion
  7324. (goto-char ins-end)
  7325. (let ((kill-whole-line t))
  7326. (kill-line (- ne-ins ne-beg)) (point)))
  7327. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7328. (move-marker ins-point nil)
  7329. (if folded
  7330. (hide-subtree)
  7331. (org-show-entry)
  7332. (show-children)
  7333. (org-cycle-hide-drawers 'children))
  7334. (org-clean-visibility-after-subtree-move)
  7335. ;; move back to the initial column we were at
  7336. (move-to-column col)))
  7337. (defvar org-subtree-clip ""
  7338. "Clipboard for cut and paste of subtrees.
  7339. This is actually only a copy of the kill, because we use the normal kill
  7340. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7341. (defvar org-subtree-clip-folded nil
  7342. "Was the last copied subtree folded?
  7343. This is used to fold the tree back after pasting.")
  7344. (defun org-cut-subtree (&optional n)
  7345. "Cut the current subtree into the clipboard.
  7346. With prefix arg N, cut this many sequential subtrees.
  7347. This is a short-hand for marking the subtree and then cutting it."
  7348. (interactive "p")
  7349. (org-copy-subtree n 'cut))
  7350. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7351. "Copy the current subtree it in the clipboard.
  7352. With prefix arg N, copy this many sequential subtrees.
  7353. This is a short-hand for marking the subtree and then copying it.
  7354. If CUT is non-nil, actually cut the subtree.
  7355. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7356. of some markers in the region, even if CUT is non-nil. This is
  7357. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7358. (interactive "p")
  7359. (let (beg end folded (beg0 (point)))
  7360. (if (org-called-interactively-p 'any)
  7361. (org-back-to-heading nil) ; take what looks like a subtree
  7362. (org-back-to-heading t)) ; take what is really there
  7363. (setq beg (point))
  7364. (skip-chars-forward " \t\r\n")
  7365. (save-match-data
  7366. (if nosubtrees
  7367. (outline-next-heading)
  7368. (save-excursion (outline-end-of-heading)
  7369. (setq folded (outline-invisible-p)))
  7370. (condition-case nil
  7371. (org-forward-heading-same-level (1- n) t)
  7372. (error nil))
  7373. (org-end-of-subtree t t)))
  7374. (setq end (point))
  7375. (goto-char beg0)
  7376. (when (> end beg)
  7377. (setq org-subtree-clip-folded folded)
  7378. (when (or cut force-store-markers)
  7379. (org-save-markers-in-region beg end))
  7380. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7381. (setq org-subtree-clip (current-kill 0))
  7382. (message "%s: Subtree(s) with %d characters"
  7383. (if cut "Cut" "Copied")
  7384. (length org-subtree-clip)))))
  7385. (defun org-paste-subtree (&optional level tree for-yank)
  7386. "Paste the clipboard as a subtree, with modification of headline level.
  7387. The entire subtree is promoted or demoted in order to match a new headline
  7388. level.
  7389. If the cursor is at the beginning of a headline, the same level as
  7390. that headline is used to paste the tree.
  7391. If not, the new level is derived from the *visible* headings
  7392. before and after the insertion point, and taken to be the inferior headline
  7393. level of the two. So if the previous visible heading is level 3 and the
  7394. next is level 4 (or vice versa), level 4 will be used for insertion.
  7395. This makes sure that the subtree remains an independent subtree and does
  7396. not swallow low level entries.
  7397. You can also force a different level, either by using a numeric prefix
  7398. argument, or by inserting the heading marker by hand. For example, if the
  7399. cursor is after \"*****\", then the tree will be shifted to level 5.
  7400. If optional TREE is given, use this text instead of the kill ring.
  7401. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7402. move back over whitespace before inserting, and move point to the end of
  7403. the inserted text when done."
  7404. (interactive "P")
  7405. (setq tree (or tree (and kill-ring (current-kill 0))))
  7406. (unless (org-kill-is-subtree-p tree)
  7407. (user-error "%s"
  7408. (substitute-command-keys
  7409. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7410. (org-with-limited-levels
  7411. (let* ((visp (not (outline-invisible-p)))
  7412. (txt tree)
  7413. (^re_ "\\(\\*+\\)[ \t]*")
  7414. (old-level (if (string-match org-outline-regexp-bol txt)
  7415. (- (match-end 0) (match-beginning 0) 1)
  7416. -1))
  7417. (force-level (cond (level (prefix-numeric-value level))
  7418. ((and (looking-at "[ \t]*$")
  7419. (string-match
  7420. "^\\*+$" (buffer-substring
  7421. (point-at-bol) (point))))
  7422. (- (match-end 1) (match-beginning 1)))
  7423. ((and (bolp)
  7424. (looking-at org-outline-regexp))
  7425. (- (match-end 0) (point) 1))))
  7426. (previous-level (save-excursion
  7427. (condition-case nil
  7428. (progn
  7429. (outline-previous-visible-heading 1)
  7430. (if (looking-at ^re_)
  7431. (- (match-end 0) (match-beginning 0) 1)
  7432. 1))
  7433. (error 1))))
  7434. (next-level (save-excursion
  7435. (condition-case nil
  7436. (progn
  7437. (or (looking-at org-outline-regexp)
  7438. (outline-next-visible-heading 1))
  7439. (if (looking-at ^re_)
  7440. (- (match-end 0) (match-beginning 0) 1)
  7441. 1))
  7442. (error 1))))
  7443. (new-level (or force-level (max previous-level next-level)))
  7444. (shift (if (or (= old-level -1)
  7445. (= new-level -1)
  7446. (= old-level new-level))
  7447. 0
  7448. (- new-level old-level)))
  7449. (delta (if (> shift 0) -1 1))
  7450. (func (if (> shift 0) 'org-demote 'org-promote))
  7451. (org-odd-levels-only nil)
  7452. beg end newend)
  7453. ;; Remove the forced level indicator
  7454. (if force-level
  7455. (delete-region (point-at-bol) (point)))
  7456. ;; Paste
  7457. (beginning-of-line (if (bolp) 1 2))
  7458. (setq beg (point))
  7459. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7460. (insert-before-markers txt)
  7461. (unless (string-match "\n\\'" txt) (insert "\n"))
  7462. (setq newend (point))
  7463. (org-reinstall-markers-in-region beg)
  7464. (setq end (point))
  7465. (goto-char beg)
  7466. (skip-chars-forward " \t\n\r")
  7467. (setq beg (point))
  7468. (if (and (outline-invisible-p) visp)
  7469. (save-excursion (outline-show-heading)))
  7470. ;; Shift if necessary
  7471. (unless (= shift 0)
  7472. (save-restriction
  7473. (narrow-to-region beg end)
  7474. (while (not (= shift 0))
  7475. (org-map-region func (point-min) (point-max))
  7476. (setq shift (+ delta shift)))
  7477. (goto-char (point-min))
  7478. (setq newend (point-max))))
  7479. (when (or (org-called-interactively-p 'interactive) for-yank)
  7480. (message "Clipboard pasted as level %d subtree" new-level))
  7481. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7482. kill-ring
  7483. (eq org-subtree-clip (current-kill 0))
  7484. org-subtree-clip-folded)
  7485. ;; The tree was folded before it was killed/copied
  7486. (hide-subtree))
  7487. (and for-yank (goto-char newend)))))
  7488. (defun org-kill-is-subtree-p (&optional txt)
  7489. "Check if the current kill is an outline subtree, or a set of trees.
  7490. Returns nil if kill does not start with a headline, or if the first
  7491. headline level is not the largest headline level in the tree.
  7492. So this will actually accept several entries of equal levels as well,
  7493. which is OK for `org-paste-subtree'.
  7494. If optional TXT is given, check this string instead of the current kill."
  7495. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7496. (re (org-get-limited-outline-regexp))
  7497. (^re (concat "^" re))
  7498. (start-level (and kill
  7499. (string-match
  7500. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7501. kill)
  7502. (- (match-end 2) (match-beginning 2) 1)))
  7503. (start (1+ (or (match-beginning 2) -1))))
  7504. (if (not start-level)
  7505. (progn
  7506. nil) ;; does not even start with a heading
  7507. (catch 'exit
  7508. (while (setq start (string-match ^re kill (1+ start)))
  7509. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7510. (throw 'exit nil)))
  7511. t))))
  7512. (defvar org-markers-to-move nil
  7513. "Markers that should be moved with a cut-and-paste operation.
  7514. Those markers are stored together with their positions relative to
  7515. the start of the region.")
  7516. (defun org-save-markers-in-region (beg end)
  7517. "Check markers in region.
  7518. If these markers are between BEG and END, record their position relative
  7519. to BEG, so that after moving the block of text, we can put the markers back
  7520. into place.
  7521. This function gets called just before an entry or tree gets cut from the
  7522. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7523. called immediately, to move the markers with the entries."
  7524. (setq org-markers-to-move nil)
  7525. (when (featurep 'org-clock)
  7526. (org-clock-save-markers-for-cut-and-paste beg end))
  7527. (when (featurep 'org-agenda)
  7528. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7529. (defun org-check-and-save-marker (marker beg end)
  7530. "Check if MARKER is between BEG and END.
  7531. If yes, remember the marker and the distance to BEG."
  7532. (when (and (marker-buffer marker)
  7533. (equal (marker-buffer marker) (current-buffer)))
  7534. (if (and (>= marker beg) (< marker end))
  7535. (push (cons marker (- marker beg)) org-markers-to-move))))
  7536. (defun org-reinstall-markers-in-region (beg)
  7537. "Move all remembered markers to their position relative to BEG."
  7538. (mapc (lambda (x)
  7539. (move-marker (car x) (+ beg (cdr x))))
  7540. org-markers-to-move)
  7541. (setq org-markers-to-move nil))
  7542. (defun org-narrow-to-subtree ()
  7543. "Narrow buffer to the current subtree."
  7544. (interactive)
  7545. (save-excursion
  7546. (save-match-data
  7547. (org-with-limited-levels
  7548. (narrow-to-region
  7549. (progn (org-back-to-heading t) (point))
  7550. (progn (org-end-of-subtree t t)
  7551. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7552. (point)))))))
  7553. (defun org-narrow-to-block ()
  7554. "Narrow buffer to the current block."
  7555. (interactive)
  7556. (let* ((case-fold-search t)
  7557. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7558. "^[ \t]*#\\+end_.*")))
  7559. (if blockp
  7560. (narrow-to-region (car blockp) (cdr blockp))
  7561. (user-error "Not in a block"))))
  7562. (eval-when-compile
  7563. (defvar org-property-drawer-re))
  7564. (defvar org-property-start-re) ;; defined below
  7565. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7566. "Clone the task (subtree) at point N times.
  7567. The clones will be inserted as siblings.
  7568. In interactive use, the user will be prompted for the number of
  7569. clones to be produced. If the entry has a timestamp, the user
  7570. will also be prompted for a time shift, which may be a repeater
  7571. as used in time stamps, for example `+3d'. To disable this,
  7572. you can call the function with a universal prefix argument.
  7573. When a valid repeater is given and the entry contains any time
  7574. stamps, the clones will become a sequence in time, with time
  7575. stamps in the subtree shifted for each clone produced. If SHIFT
  7576. is nil or the empty string, time stamps will be left alone. The
  7577. ID property of the original subtree is removed.
  7578. If the original subtree did contain time stamps with a repeater,
  7579. the following will happen:
  7580. - the repeater will be removed in each clone
  7581. - an additional clone will be produced, with the current, unshifted
  7582. date(s) in the entry.
  7583. - the original entry will be placed *after* all the clones, with
  7584. repeater intact.
  7585. - the start days in the repeater in the original entry will be shifted
  7586. to past the last clone.
  7587. In this way you can spell out a number of instances of a repeating task,
  7588. and still retain the repeater to cover future instances of the task."
  7589. (interactive "nNumber of clones to produce: ")
  7590. (let ((shift
  7591. (or shift
  7592. (if (and (not (equal current-prefix-arg '(4)))
  7593. (save-excursion
  7594. (re-search-forward org-ts-regexp-both
  7595. (save-excursion
  7596. (org-end-of-subtree t)
  7597. (point)) t)))
  7598. (read-from-minibuffer
  7599. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7600. ""))) ;; No time shift
  7601. (n-no-remove -1)
  7602. (drawer-re org-drawer-regexp)
  7603. beg end template task idprop
  7604. shift-n shift-what doshift nmin nmax)
  7605. (if (not (and (integerp n) (> n 0)))
  7606. (error "Invalid number of replications %s" n))
  7607. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7608. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7609. shift)))
  7610. (error "Invalid shift specification %s" shift))
  7611. (when doshift
  7612. (setq shift-n (string-to-number (match-string 1 shift))
  7613. shift-what (cdr (assoc (match-string 2 shift)
  7614. '(("d" . day) ("w" . week)
  7615. ("m" . month) ("y" . year))))))
  7616. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7617. (setq nmin 1 nmax n)
  7618. (org-back-to-heading t)
  7619. (setq beg (point))
  7620. (setq idprop (org-entry-get nil "ID"))
  7621. (org-end-of-subtree t t)
  7622. (or (bolp) (insert "\n"))
  7623. (setq end (point))
  7624. (setq template (buffer-substring beg end))
  7625. (when (and doshift
  7626. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7627. (delete-region beg end)
  7628. (setq end beg)
  7629. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7630. (goto-char end)
  7631. (loop for n from nmin to nmax do
  7632. ;; prepare clone
  7633. (with-temp-buffer
  7634. (insert template)
  7635. (org-mode)
  7636. (goto-char (point-min))
  7637. (org-show-subtree)
  7638. (and idprop (if org-clone-delete-id
  7639. (org-entry-delete nil "ID")
  7640. (org-id-get-create t)))
  7641. (unless (= n 0)
  7642. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7643. (kill-whole-line))
  7644. (goto-char (point-min))
  7645. (while (re-search-forward drawer-re nil t)
  7646. (mapc (lambda (d)
  7647. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7648. (goto-char (point-min))
  7649. (when doshift
  7650. (while (re-search-forward org-ts-regexp-both nil t)
  7651. (org-timestamp-change (* n shift-n) shift-what))
  7652. (unless (= n n-no-remove)
  7653. (goto-char (point-min))
  7654. (while (re-search-forward org-ts-regexp nil t)
  7655. (save-excursion
  7656. (goto-char (match-beginning 0))
  7657. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7658. (delete-region (match-beginning 1) (match-end 1)))))))
  7659. (setq task (buffer-string)))
  7660. (insert task))
  7661. (goto-char beg)))
  7662. ;;; Outline Sorting
  7663. (defun org-sort (with-case)
  7664. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7665. Optional argument WITH-CASE means sort case-sensitively."
  7666. (interactive "P")
  7667. (cond
  7668. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7669. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7670. (t
  7671. (org-call-with-arg 'org-sort-entries with-case))))
  7672. (defun org-sort-remove-invisible (s)
  7673. "Remove invisible links from string S."
  7674. (remove-text-properties 0 (length s) org-rm-props s)
  7675. (while (string-match org-bracket-link-regexp s)
  7676. (setq s (replace-match (if (match-end 2)
  7677. (match-string 3 s)
  7678. (match-string 1 s)) t t s)))
  7679. (let ((st (format " %s " s)))
  7680. (while (string-match org-emph-re st)
  7681. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7682. (setq s (substring st 1 -1)))
  7683. s)
  7684. (defvar org-priority-regexp) ; defined later in the file
  7685. (defvar org-after-sorting-entries-or-items-hook nil
  7686. "Hook that is run after a bunch of entries or items have been sorted.
  7687. When children are sorted, the cursor is in the parent line when this
  7688. hook gets called. When a region or a plain list is sorted, the cursor
  7689. will be in the first entry of the sorted region/list.")
  7690. (defun org-sort-entries
  7691. (&optional with-case sorting-type getkey-func compare-func property)
  7692. "Sort entries on a certain level of an outline tree.
  7693. If there is an active region, the entries in the region are sorted.
  7694. Else, if the cursor is before the first entry, sort the top-level items.
  7695. Else, the children of the entry at point are sorted.
  7696. Sorting can be alphabetically, numerically, by date/time as given by
  7697. a time stamp, by a property, by priority order, or by a custom function.
  7698. The command prompts for the sorting type unless it has been given to the
  7699. function through the SORTING-TYPE argument, which needs to be a character,
  7700. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7701. precise meaning of each character:
  7702. n Numerically, by converting the beginning of the entry/item to a number.
  7703. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7704. o By order of TODO keywords.
  7705. t By date/time, either the first active time stamp in the entry, or, if
  7706. none exist, by the first inactive one.
  7707. s By the scheduled date/time.
  7708. d By deadline date/time.
  7709. c By creation time, which is assumed to be the first inactive time stamp
  7710. at the beginning of a line.
  7711. p By priority according to the cookie.
  7712. r By the value of a property.
  7713. Capital letters will reverse the sort order.
  7714. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7715. called with point at the beginning of the record. It must return either
  7716. a string or a number that should serve as the sorting key for that record.
  7717. Comparing entries ignores case by default. However, with an optional argument
  7718. WITH-CASE, the sorting considers case as well.
  7719. Sorting is done against the visible part of the headlines, it ignores hidden
  7720. links."
  7721. (interactive "P")
  7722. (let ((case-func (if with-case 'identity 'downcase))
  7723. (cmstr
  7724. ;; The clock marker is lost when using `sort-subr', let's
  7725. ;; store the clocking string.
  7726. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7727. (save-excursion
  7728. (goto-char org-clock-marker)
  7729. (looking-back "^.*") (match-string-no-properties 0))))
  7730. start beg end stars re re2
  7731. txt what tmp)
  7732. ;; Find beginning and end of region to sort
  7733. (cond
  7734. ((org-region-active-p)
  7735. ;; we will sort the region
  7736. (setq end (region-end)
  7737. what "region")
  7738. (goto-char (region-beginning))
  7739. (if (not (org-at-heading-p)) (outline-next-heading))
  7740. (setq start (point)))
  7741. ((or (org-at-heading-p)
  7742. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7743. ;; we will sort the children of the current headline
  7744. (org-back-to-heading)
  7745. (setq start (point)
  7746. end (progn (org-end-of-subtree t t)
  7747. (or (bolp) (insert "\n"))
  7748. (org-back-over-empty-lines)
  7749. (point))
  7750. what "children")
  7751. (goto-char start)
  7752. (show-subtree)
  7753. (outline-next-heading))
  7754. (t
  7755. ;; we will sort the top-level entries in this file
  7756. (goto-char (point-min))
  7757. (or (org-at-heading-p) (outline-next-heading))
  7758. (setq start (point))
  7759. (goto-char (point-max))
  7760. (beginning-of-line 1)
  7761. (when (looking-at ".*?\\S-")
  7762. ;; File ends in a non-white line
  7763. (end-of-line 1)
  7764. (insert "\n"))
  7765. (setq end (point-max))
  7766. (setq what "top-level")
  7767. (goto-char start)
  7768. (show-all)))
  7769. (setq beg (point))
  7770. (if (>= beg end) (user-error "Nothing to sort"))
  7771. (looking-at "\\(\\*+\\)")
  7772. (setq stars (match-string 1)
  7773. re (concat "^" (regexp-quote stars) " +")
  7774. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7775. txt (buffer-substring beg end))
  7776. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7777. (if (and (not (equal stars "*")) (string-match re2 txt))
  7778. (user-error "Region to sort contains a level above the first entry"))
  7779. (unless sorting-type
  7780. (message
  7781. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7782. [t]ime [s]cheduled [d]eadline [c]reated
  7783. A/N/P/R/O/F/T/S/D/C means reversed:"
  7784. what)
  7785. (setq sorting-type (read-char-exclusive))
  7786. (unless getkey-func
  7787. (and (= (downcase sorting-type) ?f)
  7788. (setq getkey-func
  7789. (org-icompleting-read "Sort using function: "
  7790. obarray 'fboundp t nil nil))
  7791. (setq getkey-func (intern getkey-func))))
  7792. (and (= (downcase sorting-type) ?r)
  7793. (not property)
  7794. (setq property
  7795. (org-icompleting-read "Property: "
  7796. (mapcar 'list (org-buffer-property-keys t))
  7797. nil t))))
  7798. (message "Sorting entries...")
  7799. (save-restriction
  7800. (narrow-to-region start end)
  7801. (let ((dcst (downcase sorting-type))
  7802. (case-fold-search nil)
  7803. (now (current-time)))
  7804. (sort-subr
  7805. (/= dcst sorting-type)
  7806. ;; This function moves to the beginning character of the "record" to
  7807. ;; be sorted.
  7808. (lambda nil
  7809. (if (re-search-forward re nil t)
  7810. (goto-char (match-beginning 0))
  7811. (goto-char (point-max))))
  7812. ;; This function moves to the last character of the "record" being
  7813. ;; sorted.
  7814. (lambda nil
  7815. (save-match-data
  7816. (condition-case nil
  7817. (outline-forward-same-level 1)
  7818. (error
  7819. (goto-char (point-max))))))
  7820. ;; This function returns the value that gets sorted against.
  7821. (lambda nil
  7822. (cond
  7823. ((= dcst ?n)
  7824. (if (looking-at org-complex-heading-regexp)
  7825. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7826. nil))
  7827. ((= dcst ?a)
  7828. (if (looking-at org-complex-heading-regexp)
  7829. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7830. nil))
  7831. ((= dcst ?t)
  7832. (let ((end (save-excursion (outline-next-heading) (point))))
  7833. (if (or (re-search-forward org-ts-regexp end t)
  7834. (re-search-forward org-ts-regexp-both end t))
  7835. (org-time-string-to-seconds (match-string 0))
  7836. (org-float-time now))))
  7837. ((= dcst ?c)
  7838. (let ((end (save-excursion (outline-next-heading) (point))))
  7839. (if (re-search-forward
  7840. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7841. end t)
  7842. (org-time-string-to-seconds (match-string 0))
  7843. (org-float-time now))))
  7844. ((= dcst ?s)
  7845. (let ((end (save-excursion (outline-next-heading) (point))))
  7846. (if (re-search-forward org-scheduled-time-regexp end t)
  7847. (org-time-string-to-seconds (match-string 1))
  7848. (org-float-time now))))
  7849. ((= dcst ?d)
  7850. (let ((end (save-excursion (outline-next-heading) (point))))
  7851. (if (re-search-forward org-deadline-time-regexp end t)
  7852. (org-time-string-to-seconds (match-string 1))
  7853. (org-float-time now))))
  7854. ((= dcst ?p)
  7855. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7856. (string-to-char (match-string 2))
  7857. org-default-priority))
  7858. ((= dcst ?r)
  7859. (or (org-entry-get nil property) ""))
  7860. ((= dcst ?o)
  7861. (if (looking-at org-complex-heading-regexp)
  7862. (- 9999 (length (member (match-string 2)
  7863. org-todo-keywords-1)))))
  7864. ((= dcst ?f)
  7865. (if getkey-func
  7866. (progn
  7867. (setq tmp (funcall getkey-func))
  7868. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7869. tmp)
  7870. (error "Invalid key function `%s'" getkey-func)))
  7871. (t (error "Invalid sorting type `%c'" sorting-type))))
  7872. nil
  7873. (cond
  7874. ((= dcst ?a) 'string<)
  7875. ((= dcst ?f) compare-func)
  7876. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7877. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7878. ;; Reset the clock marker if needed
  7879. (when cmstr
  7880. (save-excursion
  7881. (goto-char start)
  7882. (search-forward cmstr nil t)
  7883. (move-marker org-clock-marker (point))))
  7884. (message "Sorting entries...done")))
  7885. (defun org-do-sort (table what &optional with-case sorting-type)
  7886. "Sort TABLE of WHAT according to SORTING-TYPE.
  7887. The user will be prompted for the SORTING-TYPE if the call to this
  7888. function does not specify it. WHAT is only for the prompt, to indicate
  7889. what is being sorted. The sorting key will be extracted from
  7890. the car of the elements of the table.
  7891. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7892. (unless sorting-type
  7893. (message
  7894. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7895. what)
  7896. (setq sorting-type (read-char-exclusive)))
  7897. (let ((dcst (downcase sorting-type))
  7898. extractfun comparefun)
  7899. ;; Define the appropriate functions
  7900. (cond
  7901. ((= dcst ?n)
  7902. (setq extractfun 'string-to-number
  7903. comparefun (if (= dcst sorting-type) '< '>)))
  7904. ((= dcst ?a)
  7905. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7906. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7907. comparefun (if (= dcst sorting-type)
  7908. 'string<
  7909. (lambda (a b) (and (not (string< a b))
  7910. (not (string= a b)))))))
  7911. ((= dcst ?t)
  7912. (setq extractfun
  7913. (lambda (x)
  7914. (if (or (string-match org-ts-regexp x)
  7915. (string-match org-ts-regexp-both x))
  7916. (org-float-time
  7917. (org-time-string-to-time (match-string 0 x)))
  7918. 0))
  7919. comparefun (if (= dcst sorting-type) '< '>)))
  7920. (t (error "Invalid sorting type `%c'" sorting-type)))
  7921. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7922. table)
  7923. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7924. ;;; The orgstruct minor mode
  7925. ;; Define a minor mode which can be used in other modes in order to
  7926. ;; integrate the org-mode structure editing commands.
  7927. ;; This is really a hack, because the org-mode structure commands use
  7928. ;; keys which normally belong to the major mode. Here is how it
  7929. ;; works: The minor mode defines all the keys necessary to operate the
  7930. ;; structure commands, but wraps the commands into a function which
  7931. ;; tests if the cursor is currently at a headline or a plain list
  7932. ;; item. If that is the case, the structure command is used,
  7933. ;; temporarily setting many Org-mode variables like regular
  7934. ;; expressions for filling etc. However, when any of those keys is
  7935. ;; used at a different location, function uses `key-binding' to look
  7936. ;; up if the key has an associated command in another currently active
  7937. ;; keymap (minor modes, major mode, global), and executes that
  7938. ;; command. There might be problems if any of the keys is otherwise
  7939. ;; used as a prefix key.
  7940. (defcustom orgstruct-heading-prefix-regexp nil
  7941. "Regexp that matches the custom prefix of Org headlines in
  7942. orgstruct(++)-mode."
  7943. :group 'org
  7944. :version "24.4"
  7945. :package-version '(Org . "8.0")
  7946. :type 'string)
  7947. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7948. (defcustom orgstruct-setup-hook nil
  7949. "Hook run after orgstruct-mode-map is filled."
  7950. :group 'org
  7951. :version "24.4"
  7952. :package-version '(Org . "8.0")
  7953. :type 'hook)
  7954. (defvar orgstruct-initialized nil)
  7955. (defvar org-local-vars nil
  7956. "List of local variables, for use by `orgstruct-mode'.")
  7957. ;;;###autoload
  7958. (define-minor-mode orgstruct-mode
  7959. "Toggle the minor mode `orgstruct-mode'.
  7960. This mode is for using Org-mode structure commands in other
  7961. modes. The following keys behave as if Org-mode were active, if
  7962. the cursor is on a headline, or on a plain list item (both as
  7963. defined by Org-mode)."
  7964. nil " OrgStruct" (make-sparse-keymap)
  7965. (funcall (if orgstruct-mode
  7966. 'add-to-invisibility-spec
  7967. 'remove-from-invisibility-spec)
  7968. '(outline . t))
  7969. (when orgstruct-mode
  7970. (org-load-modules-maybe)
  7971. (unless orgstruct-initialized
  7972. (orgstruct-setup)
  7973. (setq orgstruct-initialized t))))
  7974. ;;;###autoload
  7975. (defun turn-on-orgstruct ()
  7976. "Unconditionally turn on `orgstruct-mode'."
  7977. (orgstruct-mode 1))
  7978. (defvar org-fb-vars nil)
  7979. (make-variable-buffer-local 'org-fb-vars)
  7980. (defun orgstruct++-mode (&optional arg)
  7981. "Toggle `orgstruct-mode', the enhanced version of it.
  7982. In addition to setting orgstruct-mode, this also exports all
  7983. indentation and autofilling variables from org-mode into the
  7984. buffer. It will also recognize item context in multiline items."
  7985. (interactive "P")
  7986. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7987. (if (< arg 1)
  7988. (progn (orgstruct-mode -1)
  7989. (mapc (lambda(v)
  7990. (org-set-local (car v)
  7991. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7992. org-fb-vars))
  7993. (orgstruct-mode 1)
  7994. (setq org-fb-vars nil)
  7995. (unless org-local-vars
  7996. (setq org-local-vars (org-get-local-variables)))
  7997. (let (var val)
  7998. (mapc
  7999. (lambda (x)
  8000. (when (string-match
  8001. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8002. (symbol-name (car x)))
  8003. (setq var (car x) val (nth 1 x))
  8004. (push (list var `(quote ,(eval var))) org-fb-vars)
  8005. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8006. org-local-vars)
  8007. (org-set-local 'orgstruct-is-++ t))))
  8008. (defvar orgstruct-is-++ nil
  8009. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8010. (make-variable-buffer-local 'orgstruct-is-++)
  8011. ;;;###autoload
  8012. (defun turn-on-orgstruct++ ()
  8013. "Unconditionally turn on `orgstruct++-mode'."
  8014. (orgstruct++-mode 1))
  8015. (defun orgstruct-error ()
  8016. "Error when there is no default binding for a structure key."
  8017. (interactive)
  8018. (funcall (if (fboundp 'user-error)
  8019. 'user-error
  8020. 'error)
  8021. "This key has no function outside structure elements"))
  8022. (defun orgstruct-setup ()
  8023. "Setup orgstruct keymap."
  8024. (dolist (cell '((org-demote . t)
  8025. (org-metaleft . t)
  8026. (org-metaright . t)
  8027. (org-promote . t)
  8028. (org-shiftmetaleft . t)
  8029. (org-shiftmetaright . t)
  8030. org-backward-element
  8031. org-backward-heading-same-level
  8032. org-ctrl-c-ret
  8033. org-ctrl-c-minus
  8034. org-ctrl-c-star
  8035. org-cycle
  8036. org-forward-heading-same-level
  8037. org-insert-heading
  8038. org-insert-heading-respect-content
  8039. org-kill-note-or-show-branches
  8040. org-mark-subtree
  8041. org-meta-return
  8042. org-metadown
  8043. org-metaup
  8044. org-narrow-to-subtree
  8045. org-promote-subtree
  8046. org-reveal
  8047. org-shiftdown
  8048. org-shiftleft
  8049. org-shiftmetadown
  8050. org-shiftmetaup
  8051. org-shiftright
  8052. org-shifttab
  8053. org-shifttab
  8054. org-shiftup
  8055. org-show-subtree
  8056. org-sort
  8057. org-up-element
  8058. outline-demote
  8059. outline-next-visible-heading
  8060. outline-previous-visible-heading
  8061. outline-promote
  8062. outline-up-heading
  8063. show-children))
  8064. (let ((f (or (car-safe cell) cell))
  8065. (disable-when-heading-prefix (cdr-safe cell)))
  8066. (when (fboundp f)
  8067. (let ((new-bindings))
  8068. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8069. (where-is-internal f outline-mode-map)))
  8070. (push binding new-bindings)
  8071. ;; TODO use local-function-key-map
  8072. (dolist (rep '(("<tab>" . "TAB")
  8073. ("<return>" . "RET")
  8074. ("<escape>" . "ESC")
  8075. ("<delete>" . "DEL")))
  8076. (setq binding (read-kbd-macro
  8077. (let ((case-fold-search))
  8078. (replace-regexp-in-string
  8079. (regexp-quote (cdr rep))
  8080. (car rep)
  8081. (key-description binding)))))
  8082. (pushnew binding new-bindings :test 'equal)))
  8083. (dolist (binding new-bindings)
  8084. (let ((key (lookup-key orgstruct-mode-map binding)))
  8085. (when (or (not key) (numberp key))
  8086. (condition-case nil
  8087. (org-defkey orgstruct-mode-map
  8088. binding
  8089. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8090. (error nil)))))))))
  8091. (run-hooks 'orgstruct-setup-hook))
  8092. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8093. "Create a function for binding in the structure minor mode.
  8094. FUN is the command to call inside a table. KEY is the key that
  8095. should be checked in for a command to execute outside of tables.
  8096. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8097. if `orgstruct-heading-prefix-regexp' is non-nil."
  8098. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8099. (let ((nname name)
  8100. (i 0))
  8101. (while (fboundp (intern nname))
  8102. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8103. (setq name (intern nname)))
  8104. (eval
  8105. (let ((bindings '((org-heading-regexp
  8106. (concat "^"
  8107. orgstruct-heading-prefix-regexp
  8108. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8109. (org-outline-regexp
  8110. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8111. (org-outline-regexp-bol
  8112. (concat "^" org-outline-regexp))
  8113. (outline-regexp org-outline-regexp)
  8114. (outline-heading-end-regexp "\n")
  8115. (outline-level 'org-outline-level)
  8116. (outline-heading-alist))))
  8117. `(defun ,name (arg)
  8118. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8119. "Outside of structure, run the binding of `"
  8120. (key-description key) "'."
  8121. (when disable-when-heading-prefix
  8122. (concat
  8123. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8124. "back to the default binding due to limitations of Org's implementation of\n"
  8125. "`" (symbol-name fun) "'.")))
  8126. (interactive "p")
  8127. (let* ((disable
  8128. ,(when disable-when-heading-prefix
  8129. '(and orgstruct-heading-prefix-regexp
  8130. (not (string= orgstruct-heading-prefix-regexp "")))))
  8131. (fallback
  8132. (or disable
  8133. (not
  8134. (let* ,bindings
  8135. (org-context-p 'headline 'item
  8136. ,(when (memq fun
  8137. '(org-insert-heading
  8138. org-insert-heading-respect-content
  8139. org-meta-return))
  8140. '(when orgstruct-is-++
  8141. 'item-body))))))))
  8142. (if fallback
  8143. (let* ((orgstruct-mode)
  8144. (binding
  8145. (loop with key = ,key
  8146. for rep in
  8147. '(nil
  8148. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8149. ("<\\([^>]*\\)return>" . "\\1RET")
  8150. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8151. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8152. do
  8153. (when rep
  8154. (setq key (read-kbd-macro
  8155. (let ((case-fold-search))
  8156. (replace-regexp-in-string
  8157. (car rep)
  8158. (cdr rep)
  8159. (key-description key))))))
  8160. thereis (key-binding key))))
  8161. (if (keymapp binding)
  8162. (set-temporary-overlay-map binding)
  8163. (let ((func (or binding
  8164. (unless disable
  8165. 'orgstruct-error))))
  8166. (when func
  8167. (call-interactively func)))))
  8168. (org-run-like-in-org-mode
  8169. (lambda ()
  8170. (interactive)
  8171. (let* ,bindings
  8172. (call-interactively ',fun)))))))))
  8173. name))
  8174. (defun org-contextualize-keys (alist contexts)
  8175. "Return valid elements in ALIST depending on CONTEXTS.
  8176. `org-agenda-custom-commands' or `org-capture-templates' are the
  8177. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8178. or `org-capture-templates-contexts' are the associated contexts
  8179. definitions."
  8180. (let ((contexts
  8181. ;; normalize contexts
  8182. (mapcar
  8183. (lambda(c) (cond ((listp (cadr c))
  8184. (list (car c) (car c) (cadr c)))
  8185. ((string= "" (cadr c))
  8186. (list (car c) (car c) (caddr c)))
  8187. (t c))) contexts))
  8188. (a alist) c r s)
  8189. ;; loop over all commands or templates
  8190. (while (setq c (pop a))
  8191. (let (vrules repl)
  8192. (cond
  8193. ((not (assoc (car c) contexts))
  8194. (push c r))
  8195. ((and (assoc (car c) contexts)
  8196. (setq vrules (org-contextualize-validate-key
  8197. (car c) contexts)))
  8198. (mapc (lambda (vr)
  8199. (when (not (equal (car vr) (cadr vr)))
  8200. (setq repl vr))) vrules)
  8201. (if (not repl) (push c r)
  8202. (push (cadr repl) s)
  8203. (push
  8204. (cons (car c)
  8205. (cdr (or (assoc (cadr repl) alist)
  8206. (error "Undefined key `%s' as contextual replacement for `%s'"
  8207. (cadr repl) (car c)))))
  8208. r))))))
  8209. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8210. (delq
  8211. nil
  8212. (delete-dups
  8213. (mapcar (lambda (x)
  8214. (let ((tpl (car x)))
  8215. (when (not (delq
  8216. nil
  8217. (mapcar (lambda(y)
  8218. (equal y tpl)) s))) x)))
  8219. (reverse r))))))
  8220. (defun org-contextualize-validate-key (key contexts)
  8221. "Check CONTEXTS for agenda or capture KEY."
  8222. (let (r rr res)
  8223. (while (setq r (pop contexts))
  8224. (mapc
  8225. (lambda (rr)
  8226. (when
  8227. (and (equal key (car r))
  8228. (if (functionp rr) (funcall rr)
  8229. (or (and (eq (car rr) 'in-file)
  8230. (buffer-file-name)
  8231. (string-match (cdr rr) (buffer-file-name)))
  8232. (and (eq (car rr) 'in-mode)
  8233. (string-match (cdr rr) (symbol-name major-mode)))
  8234. (and (eq (car rr) 'in-buffer)
  8235. (string-match (cdr rr) (buffer-name)))
  8236. (when (and (eq (car rr) 'not-in-file)
  8237. (buffer-file-name))
  8238. (not (string-match (cdr rr) (buffer-file-name))))
  8239. (when (eq (car rr) 'not-in-mode)
  8240. (not (string-match (cdr rr) (symbol-name major-mode))))
  8241. (when (eq (car rr) 'not-in-buffer)
  8242. (not (string-match (cdr rr) (buffer-name)))))))
  8243. (push r res)))
  8244. (car (last r))))
  8245. (delete-dups (delq nil res))))
  8246. (defun org-context-p (&rest contexts)
  8247. "Check if local context is any of CONTEXTS.
  8248. Possible values in the list of contexts are `table', `headline', and `item'."
  8249. (let ((pos (point)))
  8250. (goto-char (point-at-bol))
  8251. (prog1 (or (and (memq 'table contexts)
  8252. (looking-at "[ \t]*|"))
  8253. (and (memq 'headline contexts)
  8254. (looking-at org-outline-regexp))
  8255. (and (memq 'item contexts)
  8256. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8257. (and (memq 'item-body contexts)
  8258. (org-in-item-p)))
  8259. (goto-char pos))))
  8260. (defun org-get-local-variables ()
  8261. "Return a list of all local variables in an Org mode buffer."
  8262. (let (varlist)
  8263. (with-current-buffer (get-buffer-create "*Org tmp*")
  8264. (erase-buffer)
  8265. (org-mode)
  8266. (setq varlist (buffer-local-variables)))
  8267. (kill-buffer "*Org tmp*")
  8268. (delq nil
  8269. (mapcar
  8270. (lambda (x)
  8271. (setq x
  8272. (if (symbolp x)
  8273. (list x)
  8274. (list (car x) (cdr x))))
  8275. (if (and (not (get (car x) 'org-state))
  8276. (string-match
  8277. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8278. (symbol-name (car x))))
  8279. x nil))
  8280. varlist))))
  8281. (defun org-clone-local-variables (from-buffer &optional regexp)
  8282. "Clone local variables from FROM-BUFFER.
  8283. Optional argument REGEXP selects variables to clone."
  8284. (mapc
  8285. (lambda (pair)
  8286. (and (symbolp (car pair))
  8287. (or (null regexp)
  8288. (string-match regexp (symbol-name (car pair))))
  8289. (set (make-local-variable (car pair))
  8290. (cdr pair))))
  8291. (buffer-local-variables from-buffer)))
  8292. ;;;###autoload
  8293. (defun org-run-like-in-org-mode (cmd)
  8294. "Run a command, pretending that the current buffer is in Org-mode.
  8295. This will temporarily bind local variables that are typically bound in
  8296. Org-mode to the values they have in Org-mode, and then interactively
  8297. call CMD."
  8298. (org-load-modules-maybe)
  8299. (unless org-local-vars
  8300. (setq org-local-vars (org-get-local-variables)))
  8301. (let (binds)
  8302. (dolist (var org-local-vars)
  8303. (when (or (not (boundp (car var)))
  8304. (eq (symbol-value (car var))
  8305. (default-value (car var))))
  8306. (push (list (car var) `(quote ,(cadr var))) binds)))
  8307. (eval `(let ,binds
  8308. (call-interactively (quote ,cmd))))))
  8309. ;;;; Archiving
  8310. (defun org-get-category (&optional pos force-refresh)
  8311. "Get the category applying to position POS."
  8312. (save-match-data
  8313. (if force-refresh (org-refresh-category-properties))
  8314. (let ((pos (or pos (point))))
  8315. (or (get-text-property pos 'org-category)
  8316. (progn (org-refresh-category-properties)
  8317. (get-text-property pos 'org-category))))))
  8318. (defun org-refresh-category-properties ()
  8319. "Refresh category text properties in the buffer."
  8320. (let ((case-fold-search t)
  8321. (inhibit-read-only t)
  8322. (def-cat (cond
  8323. ((null org-category)
  8324. (if buffer-file-name
  8325. (file-name-sans-extension
  8326. (file-name-nondirectory buffer-file-name))
  8327. "???"))
  8328. ((symbolp org-category) (symbol-name org-category))
  8329. (t org-category)))
  8330. beg end cat pos optionp)
  8331. (org-with-silent-modifications
  8332. (save-excursion
  8333. (save-restriction
  8334. (widen)
  8335. (goto-char (point-min))
  8336. (put-text-property (point) (point-max) 'org-category def-cat)
  8337. (while (re-search-forward
  8338. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8339. (setq pos (match-end 0)
  8340. optionp (equal (char-after (match-beginning 0)) ?#)
  8341. cat (org-trim (match-string 2)))
  8342. (if optionp
  8343. (setq beg (point-at-bol) end (point-max))
  8344. (org-back-to-heading t)
  8345. (setq beg (point) end (org-end-of-subtree t t)))
  8346. (put-text-property beg end 'org-category cat)
  8347. (put-text-property beg end 'org-category-position beg)
  8348. (goto-char pos)))))))
  8349. (defun org-refresh-properties (dprop tprop)
  8350. "Refresh buffer text properties.
  8351. DPROP is the drawer property and TPROP is the corresponding text
  8352. property to set."
  8353. (let ((case-fold-search t)
  8354. (inhibit-read-only t) p)
  8355. (org-with-silent-modifications
  8356. (save-excursion
  8357. (save-restriction
  8358. (widen)
  8359. (goto-char (point-min))
  8360. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8361. (setq p (org-match-string-no-properties 1))
  8362. (save-excursion
  8363. (org-back-to-heading t)
  8364. (put-text-property
  8365. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8366. ;;;; Link Stuff
  8367. ;;; Link abbreviations
  8368. (defun org-link-expand-abbrev (link)
  8369. "Apply replacements as defined in `org-link-abbrev-alist'."
  8370. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8371. (let* ((key (match-string 1 link))
  8372. (as (or (assoc key org-link-abbrev-alist-local)
  8373. (assoc key org-link-abbrev-alist)))
  8374. (tag (and (match-end 2) (match-string 3 link)))
  8375. rpl)
  8376. (if (not as)
  8377. link
  8378. (setq rpl (cdr as))
  8379. (cond
  8380. ((symbolp rpl) (funcall rpl tag))
  8381. ((string-match "%(\\([^)]+\\))" rpl)
  8382. (replace-match
  8383. (save-match-data
  8384. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8385. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8386. ((string-match "%h" rpl)
  8387. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8388. (t (concat rpl tag)))))
  8389. link))
  8390. ;;; Storing and inserting links
  8391. (defvar org-insert-link-history nil
  8392. "Minibuffer history for links inserted with `org-insert-link'.")
  8393. (defvar org-stored-links nil
  8394. "Contains the links stored with `org-store-link'.")
  8395. (defvar org-store-link-plist nil
  8396. "Plist with info about the most recently link created with `org-store-link'.")
  8397. (defvar org-link-protocols nil
  8398. "Link protocols added to Org-mode using `org-add-link-type'.")
  8399. (defvar org-store-link-functions nil
  8400. "List of functions that are called to create and store a link.
  8401. Each function will be called in turn until one returns a non-nil
  8402. value. Each function should check if it is responsible for creating
  8403. this link (for example by looking at the major mode).
  8404. If not, it must exit and return nil.
  8405. If yes, it should return a non-nil value after a calling
  8406. `org-store-link-props' with a list of properties and values.
  8407. Special properties are:
  8408. :type The link prefix, like \"http\". This must be given.
  8409. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8410. This is obligatory as well.
  8411. :description Optional default description for the second pair
  8412. of brackets in an Org-mode link. The user can still change
  8413. this when inserting this link into an Org-mode buffer.
  8414. In addition to these, any additional properties can be specified
  8415. and then used in capture templates.")
  8416. (defun org-add-link-type (type &optional follow export)
  8417. "Add TYPE to the list of `org-link-types'.
  8418. Re-compute all regular expressions depending on `org-link-types'
  8419. FOLLOW and EXPORT are two functions.
  8420. FOLLOW should take the link path as the single argument and do whatever
  8421. is necessary to follow the link, for example find a file or display
  8422. a mail message.
  8423. EXPORT should format the link path for export to one of the export formats.
  8424. It should be a function accepting three arguments:
  8425. path the path of the link, the text after the prefix (like \"http:\")
  8426. desc the description of the link, if any, or a description added by
  8427. org-export-normalize-links if there is none
  8428. format the export format, a symbol like `html' or `latex' or `ascii'..
  8429. The function may use the FORMAT information to return different values
  8430. depending on the format. The return value will be put literally into
  8431. the exported file. If the return value is nil, this means Org should
  8432. do what it normally does with links which do not have EXPORT defined.
  8433. Org-mode has a built-in default for exporting links. If you are happy with
  8434. this default, there is no need to define an export function for the link
  8435. type. For a simple example of an export function, see `org-bbdb.el'."
  8436. (add-to-list 'org-link-types type t)
  8437. (org-make-link-regexps)
  8438. (if (assoc type org-link-protocols)
  8439. (setcdr (assoc type org-link-protocols) (list follow export))
  8440. (push (list type follow export) org-link-protocols)))
  8441. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8442. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8443. ;;;###autoload
  8444. (defun org-store-link (arg)
  8445. "\\<org-mode-map>Store an org-link to the current location.
  8446. This link is added to `org-stored-links' and can later be inserted
  8447. into an org-buffer with \\[org-insert-link].
  8448. For some link types, a prefix arg is interpreted.
  8449. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8450. For file links, arg negates `org-context-in-file-links'.
  8451. A double prefix arg force skipping storing functions that are not
  8452. part of Org's core.
  8453. A triple prefix arg force storing a link for each line in the
  8454. active region."
  8455. (interactive "P")
  8456. (org-load-modules-maybe)
  8457. (if (and (equal arg '(64)) (org-region-active-p))
  8458. (save-excursion
  8459. (let ((end (region-end)))
  8460. (goto-char (region-beginning))
  8461. (set-mark (point))
  8462. (while (< (point-at-eol) end)
  8463. (move-end-of-line 1) (activate-mark)
  8464. (let (current-prefix-arg)
  8465. (call-interactively 'org-store-link))
  8466. (move-beginning-of-line 2)
  8467. (set-mark (point)))))
  8468. (org-with-limited-levels
  8469. (setq org-store-link-plist nil)
  8470. (let (link cpltxt desc description search
  8471. txt custom-id agenda-link sfuns sfunsn)
  8472. (cond
  8473. ;; Store a link using an external link type
  8474. ((and (not (equal arg '(16)))
  8475. (setq sfuns
  8476. (delq
  8477. nil (mapcar (lambda (f)
  8478. (let (fs) (if (funcall f) (push f fs))))
  8479. org-store-link-functions))
  8480. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8481. (or (and (cdr sfuns)
  8482. (funcall (intern
  8483. (completing-read
  8484. "Which function for creating the link? "
  8485. sfunsn t (car sfunsn)))))
  8486. (funcall (caar sfuns)))
  8487. (setq link (plist-get org-store-link-plist :link)
  8488. desc (or (plist-get org-store-link-plist
  8489. :description) link))))
  8490. ;; Store a link from a source code buffer
  8491. ((org-src-edit-buffer-p)
  8492. (let (label gc)
  8493. (while (or (not label)
  8494. (save-excursion
  8495. (save-restriction
  8496. (widen)
  8497. (goto-char (point-min))
  8498. (re-search-forward
  8499. (regexp-quote (format org-coderef-label-format label))
  8500. nil t))))
  8501. (when label (message "Label exists already") (sit-for 2))
  8502. (setq label (read-string "Code line label: " label)))
  8503. (end-of-line 1)
  8504. (setq link (format org-coderef-label-format label))
  8505. (setq gc (- 79 (length link)))
  8506. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8507. (insert link)
  8508. (setq link (concat "(" label ")") desc nil)))
  8509. ;; We are in the agenda, link to referenced location
  8510. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8511. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8512. (get-text-property (point) 'org-marker))))
  8513. (when m
  8514. (org-with-point-at m
  8515. (setq agenda-link
  8516. (if (org-called-interactively-p 'any)
  8517. (call-interactively 'org-store-link)
  8518. (org-store-link nil)))))))
  8519. ((eq major-mode 'calendar-mode)
  8520. (let ((cd (calendar-cursor-to-date)))
  8521. (setq link
  8522. (format-time-string
  8523. (car org-time-stamp-formats)
  8524. (apply 'encode-time
  8525. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8526. nil nil nil))))
  8527. (org-store-link-props :type "calendar" :date cd)))
  8528. ((eq major-mode 'help-mode)
  8529. (setq link (concat "help:" (save-excursion
  8530. (goto-char (point-min))
  8531. (looking-at "^[^ ]+")
  8532. (match-string 0))))
  8533. (org-store-link-props :type "help"))
  8534. ((eq major-mode 'w3-mode)
  8535. (setq cpltxt (if (and (buffer-name)
  8536. (not (string-match "Untitled" (buffer-name))))
  8537. (buffer-name)
  8538. (url-view-url t))
  8539. link (url-view-url t))
  8540. (org-store-link-props :type "w3" :url (url-view-url t)))
  8541. ((eq major-mode 'image-mode)
  8542. (setq cpltxt (concat "file:"
  8543. (abbreviate-file-name buffer-file-name))
  8544. link cpltxt)
  8545. (org-store-link-props :type "image" :file buffer-file-name))
  8546. ;; In dired, store a link to the file of the current line
  8547. ((eq major-mode 'dired-mode)
  8548. (let ((file (dired-get-filename nil t)))
  8549. (setq file (if file
  8550. (abbreviate-file-name
  8551. (expand-file-name (dired-get-filename nil t)))
  8552. ;; otherwise, no file so use current directory.
  8553. default-directory))
  8554. (setq cpltxt (concat "file:" file)
  8555. link cpltxt)))
  8556. ((setq search (run-hook-with-args-until-success
  8557. 'org-create-file-search-functions))
  8558. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8559. "::" search))
  8560. (setq cpltxt (or description link)))
  8561. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8562. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8563. (cond
  8564. ;; Store a link using the target at point
  8565. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8566. (setq cpltxt
  8567. (concat "file:"
  8568. (abbreviate-file-name
  8569. (buffer-file-name (buffer-base-buffer)))
  8570. "::" (match-string 1))
  8571. link cpltxt))
  8572. ((and (featurep 'org-id)
  8573. (or (eq org-id-link-to-org-use-id t)
  8574. (and (org-called-interactively-p 'any)
  8575. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8576. (and (eq org-id-link-to-org-use-id
  8577. 'create-if-interactive-and-no-custom-id)
  8578. (not custom-id))))
  8579. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8580. ;; Store a link using the ID at point
  8581. (setq link (condition-case nil
  8582. (prog1 (org-id-store-link)
  8583. (setq desc (plist-get org-store-link-plist
  8584. :description)))
  8585. (error
  8586. ;; Probably before first headline, link only to file
  8587. (concat "file:"
  8588. (abbreviate-file-name
  8589. (buffer-file-name (buffer-base-buffer))))))))
  8590. (t
  8591. ;; Just link to current headline
  8592. (setq cpltxt (concat "file:"
  8593. (abbreviate-file-name
  8594. (buffer-file-name (buffer-base-buffer)))))
  8595. ;; Add a context search string
  8596. (when (org-xor org-context-in-file-links arg)
  8597. (let* ((ee (org-element-at-point))
  8598. (et (org-element-type ee))
  8599. (ev (plist-get (cadr ee) :value))
  8600. (ek (plist-get (cadr ee) :key))
  8601. (eok (and (stringp ek) (string-match "name" ek))))
  8602. (setq txt (cond
  8603. ((org-at-heading-p) nil)
  8604. ((and (eq et 'keyword) eok) ev)
  8605. ((org-region-active-p)
  8606. (buffer-substring (region-beginning) (region-end)))))
  8607. (when (or (null txt) (string-match "\\S-" txt))
  8608. (setq cpltxt
  8609. (concat cpltxt "::"
  8610. (condition-case nil
  8611. (org-make-org-heading-search-string txt)
  8612. (error "")))
  8613. desc (or (and (eq et 'keyword) eok ev)
  8614. (nth 4 (ignore-errors (org-heading-components)))
  8615. "NONE")))))
  8616. (if (string-match "::\\'" cpltxt)
  8617. (setq cpltxt (substring cpltxt 0 -2)))
  8618. (setq link cpltxt))))
  8619. ((buffer-file-name (buffer-base-buffer))
  8620. ;; Just link to this file here.
  8621. (setq cpltxt (concat "file:"
  8622. (abbreviate-file-name
  8623. (buffer-file-name (buffer-base-buffer)))))
  8624. ;; Add a context string.
  8625. (when (org-xor org-context-in-file-links arg)
  8626. (setq txt (if (org-region-active-p)
  8627. (buffer-substring (region-beginning) (region-end))
  8628. (buffer-substring (point-at-bol) (point-at-eol))))
  8629. ;; Only use search option if there is some text.
  8630. (when (string-match "\\S-" txt)
  8631. (setq cpltxt
  8632. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8633. desc "NONE")))
  8634. (setq link cpltxt))
  8635. ((org-called-interactively-p 'interactive)
  8636. (user-error "No method for storing a link from this buffer"))
  8637. (t (setq link nil)))
  8638. ;; We're done setting link and desc, clean up
  8639. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8640. (setq link (or link cpltxt)
  8641. desc (or desc cpltxt))
  8642. (cond ((equal desc "NONE") (setq desc nil))
  8643. ((string-match org-bracket-link-analytic-regexp desc)
  8644. (let ((d0 (match-string 3 desc))
  8645. (p0 (match-string 5 desc)))
  8646. (setq desc
  8647. (replace-regexp-in-string
  8648. org-bracket-link-regexp
  8649. (concat (or p0 d0)
  8650. (if (equal (length (match-string 0 desc))
  8651. (length desc)) "*" "")) desc)))))
  8652. ;; Return the link
  8653. (if (not (and (or (org-called-interactively-p 'any)
  8654. executing-kbd-macro) link))
  8655. (or agenda-link (and link (org-make-link-string link desc)))
  8656. (push (list link desc) org-stored-links)
  8657. (message "Stored: %s" (or desc link))
  8658. (when custom-id
  8659. (setq link (concat "file:" (abbreviate-file-name
  8660. (buffer-file-name)) "::#" custom-id))
  8661. (push (list link desc) org-stored-links)))))))
  8662. (defun org-store-link-props (&rest plist)
  8663. "Store link properties, extract names and addresses."
  8664. (let (x adr)
  8665. (when (setq x (plist-get plist :from))
  8666. (setq adr (mail-extract-address-components x))
  8667. (setq plist (plist-put plist :fromname (car adr)))
  8668. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8669. (when (setq x (plist-get plist :to))
  8670. (setq adr (mail-extract-address-components x))
  8671. (setq plist (plist-put plist :toname (car adr)))
  8672. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8673. (let ((from (plist-get plist :from))
  8674. (to (plist-get plist :to)))
  8675. (when (and from to org-from-is-user-regexp)
  8676. (setq plist
  8677. (plist-put plist :fromto
  8678. (if (string-match org-from-is-user-regexp from)
  8679. (concat "to %t")
  8680. (concat "from %f"))))))
  8681. (setq org-store-link-plist plist))
  8682. (defun org-add-link-props (&rest plist)
  8683. "Add these properties to the link property list."
  8684. (let (key value)
  8685. (while plist
  8686. (setq key (pop plist) value (pop plist))
  8687. (setq org-store-link-plist
  8688. (plist-put org-store-link-plist key value)))))
  8689. (defun org-email-link-description (&optional fmt)
  8690. "Return the description part of an email link.
  8691. This takes information from `org-store-link-plist' and formats it
  8692. according to FMT (default from `org-email-link-description-format')."
  8693. (setq fmt (or fmt org-email-link-description-format))
  8694. (let* ((p org-store-link-plist)
  8695. (to (plist-get p :toaddress))
  8696. (from (plist-get p :fromaddress))
  8697. (table
  8698. (list
  8699. (cons "%c" (plist-get p :fromto))
  8700. (cons "%F" (plist-get p :from))
  8701. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8702. (cons "%T" (plist-get p :to))
  8703. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8704. (cons "%s" (plist-get p :subject))
  8705. (cons "%d" (plist-get p :date))
  8706. (cons "%m" (plist-get p :message-id)))))
  8707. (when (string-match "%c" fmt)
  8708. ;; Check if the user wrote this message
  8709. (if (and org-from-is-user-regexp from to
  8710. (save-match-data (string-match org-from-is-user-regexp from)))
  8711. (setq fmt (replace-match "to %t" t t fmt))
  8712. (setq fmt (replace-match "from %f" t t fmt))))
  8713. (org-replace-escapes fmt table)))
  8714. (defun org-make-org-heading-search-string (&optional string)
  8715. "Make search string for the current headline or STRING."
  8716. (let ((s (or string
  8717. (and (derived-mode-p 'org-mode)
  8718. (save-excursion
  8719. (org-back-to-heading t)
  8720. (org-element-property :raw-value (org-element-at-point))))))
  8721. (lines org-context-in-file-links))
  8722. (or string (setq s (concat "*" s))) ; Add * for headlines
  8723. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8724. (when (and string (integerp lines) (> lines 0))
  8725. (let ((slines (org-split-string s "\n")))
  8726. (when (< lines (length slines))
  8727. (setq s (mapconcat
  8728. 'identity
  8729. (reverse (nthcdr (- (length slines) lines)
  8730. (reverse slines))) "\n")))))
  8731. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8732. (defun org-make-link-string (link &optional description)
  8733. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8734. (unless (string-match "\\S-" link)
  8735. (error "Empty link"))
  8736. (when (and description
  8737. (stringp description)
  8738. (not (string-match "\\S-" description)))
  8739. (setq description nil))
  8740. (when (stringp description)
  8741. ;; Remove brackets from the description, they are fatal.
  8742. (while (string-match "\\[" description)
  8743. (setq description (replace-match "{" t t description)))
  8744. (while (string-match "\\]" description)
  8745. (setq description (replace-match "}" t t description))))
  8746. (when (equal link description)
  8747. ;; No description needed, it is identical
  8748. (setq description nil))
  8749. (when (and (not description)
  8750. (not (string-match (org-image-file-name-regexp) link))
  8751. (not (equal link (org-link-escape link))))
  8752. (setq description (org-extract-attributes link)))
  8753. (setq link
  8754. (cond ((string-match (org-image-file-name-regexp) link) link)
  8755. ((string-match org-link-types-re link)
  8756. (concat (match-string 1 link)
  8757. (org-link-escape (substring link (match-end 1)))))
  8758. (t (org-link-escape link))))
  8759. (concat "[[" link "]"
  8760. (if description (concat "[" description "]") "")
  8761. "]"))
  8762. (defconst org-link-escape-chars
  8763. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8764. "List of characters that should be escaped in link.
  8765. This is the list that is used for internal purposes.")
  8766. (defconst org-link-escape-chars-browser
  8767. '(?\ ?\")
  8768. "List of escapes for characters that are problematic in links.
  8769. This is the list that is used before handing over to the browser.")
  8770. (defun org-link-escape (text &optional table merge)
  8771. "Return percent escaped representation of TEXT.
  8772. TEXT is a string with the text to escape.
  8773. Optional argument TABLE is a list with characters that should be
  8774. escaped. When nil, `org-link-escape-chars' is used.
  8775. If optional argument MERGE is set, merge TABLE into
  8776. `org-link-escape-chars'."
  8777. (cond
  8778. ((and table merge)
  8779. (mapc (lambda (defchr)
  8780. (unless (member defchr table)
  8781. (setq table (cons defchr table)))) org-link-escape-chars))
  8782. ((null table)
  8783. (setq table org-link-escape-chars)))
  8784. (mapconcat
  8785. (lambda (char)
  8786. (if (or (member char table)
  8787. (and (or (< char 32) (= char 37) (> char 126))
  8788. org-url-hexify-p))
  8789. (mapconcat (lambda (sequence-element)
  8790. (format "%%%.2X" sequence-element))
  8791. (or (encode-coding-char char 'utf-8)
  8792. (error "Unable to percent escape character: %s"
  8793. (char-to-string char))) "")
  8794. (char-to-string char))) text ""))
  8795. (defun org-link-unescape (str)
  8796. "Unhex hexified Unicode strings as returned from the JavaScript function
  8797. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8798. (unless (and (null str) (string= "" str))
  8799. (let ((pos 0) (case-fold-search t) unhexed)
  8800. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8801. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8802. (setq str (replace-match unhexed t t str))
  8803. (setq pos (+ pos (length unhexed))))))
  8804. str)
  8805. (defun org-link-unescape-compound (hex)
  8806. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8807. Note: this function also decodes single byte encodings like
  8808. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8809. (save-match-data
  8810. (let* ((bytes (cdr (split-string hex "%")))
  8811. (ret "")
  8812. (eat 0)
  8813. (sum 0))
  8814. (while bytes
  8815. (let* ((val (string-to-number (pop bytes) 16))
  8816. (shift-xor
  8817. (if (= 0 eat)
  8818. (cond
  8819. ((>= val 252) (cons 6 252))
  8820. ((>= val 248) (cons 5 248))
  8821. ((>= val 240) (cons 4 240))
  8822. ((>= val 224) (cons 3 224))
  8823. ((>= val 192) (cons 2 192))
  8824. (t (cons 0 0)))
  8825. (cons 6 128))))
  8826. (if (>= val 192) (setq eat (car shift-xor)))
  8827. (setq val (logxor val (cdr shift-xor)))
  8828. (setq sum (+ (lsh sum (car shift-xor)) val))
  8829. (if (> eat 0) (setq eat (- eat 1)))
  8830. (cond
  8831. ((= 0 eat) ;multi byte
  8832. (setq ret (concat ret (org-char-to-string sum)))
  8833. (setq sum 0))
  8834. ((not bytes) ; single byte(s)
  8835. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8836. )) ;; end (while bytes
  8837. ret )))
  8838. (defun org-link-unescape-single-byte-sequence (hex)
  8839. "Unhexify hex-encoded single byte character sequences."
  8840. (mapconcat (lambda (byte)
  8841. (char-to-string (string-to-number byte 16)))
  8842. (cdr (split-string hex "%")) ""))
  8843. (defun org-xor (a b)
  8844. "Exclusive or."
  8845. (if a (not b) b))
  8846. (defun org-fixup-message-id-for-http (s)
  8847. "Replace special characters in a message id, so it can be used in an http query."
  8848. (when (string-match "%" s)
  8849. (setq s (mapconcat (lambda (c)
  8850. (if (eq c ?%)
  8851. "%25"
  8852. (char-to-string c)))
  8853. s "")))
  8854. (while (string-match "<" s)
  8855. (setq s (replace-match "%3C" t t s)))
  8856. (while (string-match ">" s)
  8857. (setq s (replace-match "%3E" t t s)))
  8858. (while (string-match "@" s)
  8859. (setq s (replace-match "%40" t t s)))
  8860. s)
  8861. (defun org-link-prettify (link)
  8862. "Return a human-readable representation of LINK.
  8863. The car of LINK must be a raw link the cdr of LINK must be either
  8864. a link description or nil."
  8865. (let ((desc (or (cadr link) "<no description>")))
  8866. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8867. "<" (car link) ">")))
  8868. ;;;###autoload
  8869. (defun org-insert-link-global ()
  8870. "Insert a link like Org-mode does.
  8871. This command can be called in any mode to insert a link in Org-mode syntax."
  8872. (interactive)
  8873. (org-load-modules-maybe)
  8874. (org-run-like-in-org-mode 'org-insert-link))
  8875. (defun org-insert-all-links (&optional keep)
  8876. "Insert all links in `org-stored-links'."
  8877. (interactive "P")
  8878. (let ((links (copy-sequence org-stored-links)) l)
  8879. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8880. (insert "- ")
  8881. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8882. (insert "\n"))))
  8883. (defun org-link-fontify-links-to-this-file ()
  8884. "Fontify links to the current file in `org-stored-links'."
  8885. (let ((f (buffer-file-name)) a b)
  8886. (setq a (mapcar (lambda(l)
  8887. (let ((ll (car l)))
  8888. (when (and (string-match "^file:\\(.+\\)::" ll)
  8889. (equal f (expand-file-name (match-string 1 ll))))
  8890. ll)))
  8891. org-stored-links))
  8892. (when (featurep 'org-id)
  8893. (setq b (mapcar (lambda(l)
  8894. (let ((ll (car l)))
  8895. (when (and (string-match "^id:\\(.+\\)$" ll)
  8896. (equal f (expand-file-name
  8897. (or (org-id-find-id-file
  8898. (match-string 1 ll)) ""))))
  8899. ll)))
  8900. org-stored-links)))
  8901. (mapcar (lambda(l)
  8902. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8903. (delq nil (append a b)))))
  8904. (defvar org-link-links-in-this-file nil)
  8905. (defun org-insert-link (&optional complete-file link-location default-description)
  8906. "Insert a link. At the prompt, enter the link.
  8907. Completion can be used to insert any of the link protocol prefixes like
  8908. http or ftp in use.
  8909. The history can be used to select a link previously stored with
  8910. `org-store-link'. When the empty string is entered (i.e. if you just
  8911. press RET at the prompt), the link defaults to the most recently
  8912. stored link. As SPC triggers completion in the minibuffer, you need to
  8913. use M-SPC or C-q SPC to force the insertion of a space character.
  8914. You will also be prompted for a description, and if one is given, it will
  8915. be displayed in the buffer instead of the link.
  8916. If there is already a link at point, this command will allow you to edit link
  8917. and description parts.
  8918. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8919. be selected using completion. The path to the file will be relative to the
  8920. current directory if the file is in the current directory or a subdirectory.
  8921. Otherwise, the link will be the absolute path as completed in the minibuffer
  8922. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8923. option `org-link-file-path-type'.
  8924. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8925. the current directory or below.
  8926. With three \\[universal-argument] prefixes, negate the meaning of
  8927. `org-keep-stored-link-after-insertion'.
  8928. If `org-make-link-description-function' is non-nil, this function will be
  8929. called with the link target, and the result will be the default
  8930. link description.
  8931. If the LINK-LOCATION parameter is non-nil, this value will be
  8932. used as the link location instead of reading one interactively.
  8933. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8934. be used as the default description."
  8935. (interactive "P")
  8936. (let* ((wcf (current-window-configuration))
  8937. (origbuf (current-buffer))
  8938. (region (if (org-region-active-p)
  8939. (buffer-substring (region-beginning) (region-end))))
  8940. (remove (and region (list (region-beginning) (region-end))))
  8941. (desc region)
  8942. tmphist ; byte-compile incorrectly complains about this
  8943. (link link-location)
  8944. (abbrevs org-link-abbrev-alist-local)
  8945. entry file all-prefixes auto-desc)
  8946. (cond
  8947. (link-location) ; specified by arg, just use it.
  8948. ((org-in-regexp org-bracket-link-regexp 1)
  8949. ;; We do have a link at point, and we are going to edit it.
  8950. (setq remove (list (match-beginning 0) (match-end 0)))
  8951. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8952. (setq link (read-string "Link: "
  8953. (org-link-unescape
  8954. (org-match-string-no-properties 1)))))
  8955. ((or (org-in-regexp org-angle-link-re)
  8956. (org-in-regexp org-plain-link-re))
  8957. ;; Convert to bracket link
  8958. (setq remove (list (match-beginning 0) (match-end 0))
  8959. link (read-string "Link: "
  8960. (org-remove-angle-brackets (match-string 0)))))
  8961. ((member complete-file '((4) (16)))
  8962. ;; Completing read for file names.
  8963. (setq link (org-file-complete-link complete-file)))
  8964. (t
  8965. ;; Read link, with completion for stored links.
  8966. (org-link-fontify-links-to-this-file)
  8967. (org-switch-to-buffer-other-window "*Org Links*")
  8968. (with-current-buffer "*Org Links*"
  8969. (erase-buffer)
  8970. (insert "Insert a link.
  8971. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8972. (when org-stored-links
  8973. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8974. (insert (mapconcat 'org-link-prettify
  8975. (reverse org-stored-links) "\n")))
  8976. (goto-char (point-min)))
  8977. (let ((cw (selected-window)))
  8978. (select-window (get-buffer-window "*Org Links*" 'visible))
  8979. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8980. (unless (pos-visible-in-window-p (point-max))
  8981. (org-fit-window-to-buffer))
  8982. (and (window-live-p cw) (select-window cw)))
  8983. ;; Fake a link history, containing the stored links.
  8984. (setq tmphist (append (mapcar 'car org-stored-links)
  8985. org-insert-link-history))
  8986. (setq all-prefixes (append (mapcar 'car abbrevs)
  8987. (mapcar 'car org-link-abbrev-alist)
  8988. org-link-types))
  8989. (unwind-protect
  8990. (progn
  8991. (setq link
  8992. (org-completing-read
  8993. "Link: "
  8994. (append
  8995. (mapcar (lambda (x) (concat x ":"))
  8996. all-prefixes)
  8997. (mapcar 'car org-stored-links))
  8998. nil nil nil
  8999. 'tmphist
  9000. (caar org-stored-links)))
  9001. (if (not (string-match "\\S-" link))
  9002. (user-error "No link selected"))
  9003. (mapc (lambda(l)
  9004. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9005. org-stored-links)
  9006. (if (or (member link all-prefixes)
  9007. (and (equal ":" (substring link -1))
  9008. (member (substring link 0 -1) all-prefixes)
  9009. (setq link (substring link 0 -1))))
  9010. (setq link (with-current-buffer origbuf
  9011. (org-link-try-special-completion link)))))
  9012. (set-window-configuration wcf)
  9013. (kill-buffer "*Org Links*"))
  9014. (setq entry (assoc link org-stored-links))
  9015. (or entry (push link org-insert-link-history))
  9016. (setq desc (or desc (nth 1 entry)))))
  9017. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9018. (not org-keep-stored-link-after-insertion))
  9019. (setq org-stored-links (delq (assoc link org-stored-links)
  9020. org-stored-links)))
  9021. (if (string-match org-plain-link-re link)
  9022. ;; URL-like link, normalize the use of angular brackets.
  9023. (setq link (org-remove-angle-brackets link)))
  9024. ;; Check if we are linking to the current file with a search
  9025. ;; option If yes, simplify the link by using only the search
  9026. ;; option.
  9027. (when (and buffer-file-name
  9028. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9029. (let* ((path (match-string 1 link))
  9030. (case-fold-search nil)
  9031. (search (match-string 2 link)))
  9032. (save-match-data
  9033. (if (equal (file-truename buffer-file-name) (file-truename path))
  9034. ;; We are linking to this same file, with a search option
  9035. (setq link search)))))
  9036. ;; Check if we can/should use a relative path. If yes, simplify the link
  9037. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9038. (let* ((type (match-string 1 link))
  9039. (path (match-string 2 link))
  9040. (origpath path)
  9041. (case-fold-search nil))
  9042. (cond
  9043. ((or (eq org-link-file-path-type 'absolute)
  9044. (equal complete-file '(16)))
  9045. (setq path (abbreviate-file-name (expand-file-name path))))
  9046. ((eq org-link-file-path-type 'noabbrev)
  9047. (setq path (expand-file-name path)))
  9048. ((eq org-link-file-path-type 'relative)
  9049. (setq path (file-relative-name path)))
  9050. (t
  9051. (save-match-data
  9052. (if (string-match (concat "^" (regexp-quote
  9053. (expand-file-name
  9054. (file-name-as-directory
  9055. default-directory))))
  9056. (expand-file-name path))
  9057. ;; We are linking a file with relative path name.
  9058. (setq path (substring (expand-file-name path)
  9059. (match-end 0)))
  9060. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9061. (setq link (concat type path))
  9062. (if (equal desc origpath)
  9063. (setq desc path))))
  9064. (if org-make-link-description-function
  9065. (setq desc
  9066. (or (condition-case nil
  9067. (funcall org-make-link-description-function link desc)
  9068. (error (progn (message "Can't get link description from `%s'"
  9069. (symbol-name org-make-link-description-function))
  9070. (sit-for 2) nil)))
  9071. (read-string "Description: " default-description)))
  9072. (if default-description (setq desc default-description)
  9073. (setq desc (or (and auto-desc desc)
  9074. (read-string "Description: " desc)))))
  9075. (unless (string-match "\\S-" desc) (setq desc nil))
  9076. (if remove (apply 'delete-region remove))
  9077. (insert (org-make-link-string link desc))))
  9078. (defun org-link-try-special-completion (type)
  9079. "If there is completion support for link type TYPE, offer it."
  9080. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9081. (if (functionp fun)
  9082. (funcall fun)
  9083. (read-string "Link (no completion support): " (concat type ":")))))
  9084. (defun org-file-complete-link (&optional arg)
  9085. "Create a file link using completion."
  9086. (let (file link)
  9087. (setq file (org-iread-file-name "File: "))
  9088. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9089. (pwd1 (file-name-as-directory (abbreviate-file-name
  9090. (expand-file-name ".")))))
  9091. (cond
  9092. ((equal arg '(16))
  9093. (setq link (concat
  9094. "file:"
  9095. (abbreviate-file-name (expand-file-name file)))))
  9096. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9097. (setq link (concat "file:" (match-string 1 file))))
  9098. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9099. (expand-file-name file))
  9100. (setq link (concat
  9101. "file:" (match-string 1 (expand-file-name file)))))
  9102. (t (setq link (concat "file:" file)))))
  9103. link))
  9104. (defun org-iread-file-name (&rest args)
  9105. "Read-file-name using `ido-mode' speedup if available.
  9106. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9107. See `read-file-name' for a description of parameters."
  9108. (org-without-partial-completion
  9109. (if (and org-completion-use-ido
  9110. (fboundp 'ido-read-file-name)
  9111. (boundp 'ido-mode) ido-mode
  9112. (listp (second args)))
  9113. (let ((ido-enter-matching-directory nil))
  9114. (apply 'ido-read-file-name args))
  9115. (apply 'read-file-name args))))
  9116. (defun org-completing-read (&rest args)
  9117. "Completing-read with SPACE being a normal character."
  9118. (let ((enable-recursive-minibuffers t)
  9119. (minibuffer-local-completion-map
  9120. (copy-keymap minibuffer-local-completion-map)))
  9121. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9122. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9123. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9124. (apply 'org-icompleting-read args)))
  9125. (defun org-completing-read-no-i (&rest args)
  9126. (let (org-completion-use-ido org-completion-use-iswitchb)
  9127. (apply 'org-completing-read args)))
  9128. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9129. "Use iswitch as a completing-read replacement to choose from choices.
  9130. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9131. from."
  9132. (let* ((iswitchb-use-virtual-buffers nil)
  9133. (iswitchb-make-buflist-hook
  9134. (lambda ()
  9135. (setq iswitchb-temp-buflist choices))))
  9136. (iswitchb-read-buffer prompt)))
  9137. (defun org-icompleting-read (&rest args)
  9138. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9139. (org-without-partial-completion
  9140. (if (and org-completion-use-ido
  9141. (fboundp 'ido-completing-read)
  9142. (boundp 'ido-mode) ido-mode
  9143. (listp (second args)))
  9144. (let ((ido-enter-matching-directory nil))
  9145. (apply 'ido-completing-read (concat (car args))
  9146. (if (consp (car (nth 1 args)))
  9147. (mapcar 'car (nth 1 args))
  9148. (nth 1 args))
  9149. (cddr args)))
  9150. (if (and org-completion-use-iswitchb
  9151. (boundp 'iswitchb-mode) iswitchb-mode
  9152. (listp (second args)))
  9153. (apply 'org-iswitchb-completing-read (concat (car args))
  9154. (if (consp (car (nth 1 args)))
  9155. (mapcar 'car (nth 1 args))
  9156. (nth 1 args))
  9157. (cddr args))
  9158. (apply 'completing-read args)))))
  9159. (defun org-extract-attributes (s)
  9160. "Extract the attributes cookie from a string and set as text property."
  9161. (let (a attr (start 0) key value)
  9162. (save-match-data
  9163. (when (string-match "{{\\([^}]+\\)}}$" s)
  9164. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9165. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9166. (setq key (match-string 1 a) value (match-string 2 a)
  9167. start (match-end 0)
  9168. attr (plist-put attr (intern key) value))))
  9169. (org-add-props s nil 'org-attr attr))
  9170. s))
  9171. ;;; Opening/following a link
  9172. (defvar org-link-search-failed nil)
  9173. (defvar org-open-link-functions nil
  9174. "Hook for functions finding a plain text link.
  9175. These functions must take a single argument, the link content.
  9176. They will be called for links that look like [[link text][description]]
  9177. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9178. like a filename (e.g. \"./blue.png\").
  9179. These functions will be called *before* Org attempts to resolve the
  9180. link by doing text searches in the current buffer - so if you want a
  9181. link \"[[target]]\" to still find \"<<target>>\", your function should
  9182. handle this as a special case.
  9183. When the function does handle the link, it must return a non-nil value.
  9184. If it decides that it is not responsible for this link, it must return
  9185. nil to indicate that that Org-mode can continue with other options
  9186. like exact and fuzzy text search.")
  9187. (defun org-next-link (&optional search-backward)
  9188. "Move forward to the next link.
  9189. If the link is in hidden text, expose it."
  9190. (interactive "P")
  9191. (when (and org-link-search-failed (eq this-command last-command))
  9192. (goto-char (point-min))
  9193. (message "Link search wrapped back to beginning of buffer"))
  9194. (setq org-link-search-failed nil)
  9195. (let* ((pos (point))
  9196. (ct (org-context))
  9197. (a (assoc :link ct))
  9198. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9199. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9200. ((looking-at org-any-link-re)
  9201. ;; Don't stay stuck at link without an org-link face
  9202. (forward-char (if search-backward -1 1))))
  9203. (if (funcall srch-fun org-any-link-re nil t)
  9204. (progn
  9205. (goto-char (match-beginning 0))
  9206. (if (outline-invisible-p) (org-show-context)))
  9207. (goto-char pos)
  9208. (setq org-link-search-failed t)
  9209. (message "No further link found"))))
  9210. (defun org-previous-link ()
  9211. "Move backward to the previous link.
  9212. If the link is in hidden text, expose it."
  9213. (interactive)
  9214. (funcall 'org-next-link t))
  9215. (defun org-translate-link (s)
  9216. "Translate a link string if a translation function has been defined."
  9217. (if (and org-link-translation-function
  9218. (fboundp org-link-translation-function)
  9219. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9220. (progn
  9221. (setq s (funcall org-link-translation-function
  9222. (match-string 1 s) (match-string 2 s)))
  9223. (concat (car s) ":" (cdr s)))
  9224. s))
  9225. (defun org-translate-link-from-planner (type path)
  9226. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9227. This is still an experimental function, your mileage may vary."
  9228. (cond
  9229. ((member type '("http" "https" "news" "ftp"))
  9230. ;; standard Internet links are the same.
  9231. nil)
  9232. ((and (equal type "irc") (string-match "^//" path))
  9233. ;; Planner has two / at the beginning of an irc link, we have 1.
  9234. ;; We should have zero, actually....
  9235. (setq path (substring path 1)))
  9236. ((and (equal type "lisp") (string-match "^/" path))
  9237. ;; Planner has a slash, we do not.
  9238. (setq type "elisp" path (substring path 1)))
  9239. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9240. ;; A typical message link. Planner has the id after the final slash,
  9241. ;; we separate it with a hash mark
  9242. (setq path (concat (match-string 1 path) "#"
  9243. (org-remove-angle-brackets (match-string 2 path))))))
  9244. (cons type path))
  9245. (defun org-find-file-at-mouse (ev)
  9246. "Open file link or URL at mouse."
  9247. (interactive "e")
  9248. (mouse-set-point ev)
  9249. (org-open-at-point 'in-emacs))
  9250. (defun org-open-at-mouse (ev)
  9251. "Open file link or URL at mouse.
  9252. See the docstring of `org-open-file' for details."
  9253. (interactive "e")
  9254. (mouse-set-point ev)
  9255. (if (eq major-mode 'org-agenda-mode)
  9256. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9257. (org-open-at-point))
  9258. (defvar org-window-config-before-follow-link nil
  9259. "The window configuration before following a link.
  9260. This is saved in case the need arises to restore it.")
  9261. (defvar org-open-link-marker (make-marker)
  9262. "Marker pointing to the location where `org-open-at-point; was called.")
  9263. ;;;###autoload
  9264. (defun org-open-at-point-global ()
  9265. "Follow a link like Org-mode does.
  9266. This command can be called in any mode to follow a link that has
  9267. Org-mode syntax."
  9268. (interactive)
  9269. (org-run-like-in-org-mode 'org-open-at-point))
  9270. ;;;###autoload
  9271. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9272. "Open a link in the string S, as if it was in Org-mode."
  9273. (interactive "sLink: \nP")
  9274. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9275. (with-temp-buffer
  9276. (let ((org-inhibit-startup (not reference-buffer)))
  9277. (org-mode)
  9278. (insert s)
  9279. (goto-char (point-min))
  9280. (when reference-buffer
  9281. (setq org-link-abbrev-alist-local
  9282. (with-current-buffer reference-buffer
  9283. org-link-abbrev-alist-local)))
  9284. (org-open-at-point arg reference-buffer)))))
  9285. (defvar org-open-at-point-functions nil
  9286. "Hook that is run when following a link at point.
  9287. Functions in this hook must return t if they identify and follow
  9288. a link at point. If they don't find anything interesting at point,
  9289. they must return nil.")
  9290. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9291. (defun org-open-at-point (&optional arg reference-buffer)
  9292. "Open link at or after point.
  9293. If there is no link at point, this function will search forward up to
  9294. the end of the current line.
  9295. Normally, files will be opened by an appropriate application. If the
  9296. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9297. With a double prefix argument, try to open outside of Emacs, in the
  9298. application the system uses for this file type."
  9299. (interactive "P")
  9300. ;; if in a code block, then open the block's results
  9301. (unless (call-interactively #'org-babel-open-src-block-result)
  9302. (org-load-modules-maybe)
  9303. (move-marker org-open-link-marker (point))
  9304. (setq org-window-config-before-follow-link (current-window-configuration))
  9305. (org-remove-occur-highlights nil nil t)
  9306. (cond
  9307. ((and (org-at-heading-p)
  9308. (not (org-at-timestamp-p t))
  9309. (not (org-in-regexp
  9310. (concat org-plain-link-re "\\|"
  9311. org-bracket-link-regexp "\\|"
  9312. org-angle-link-re "\\|"
  9313. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9314. (not (get-text-property (point) 'org-linked-text)))
  9315. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9316. (lk0 (car lkall))
  9317. (lk (if (stringp lk0) (list lk0) lk0))
  9318. (lkend (cdr lkall)))
  9319. (mapcar (lambda(l)
  9320. (search-forward l nil lkend)
  9321. (goto-char (match-beginning 0))
  9322. (org-open-at-point))
  9323. lk))
  9324. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9325. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9326. ((and (org-at-timestamp-p t)
  9327. (not (org-in-regexp org-bracket-link-regexp)))
  9328. (org-follow-timestamp-link))
  9329. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9330. (not (org-in-regexp org-any-link-re)))
  9331. (org-footnote-action))
  9332. (t
  9333. (let (type path link line search (pos (point)))
  9334. (catch 'match
  9335. (save-excursion
  9336. (skip-chars-forward "^]\n\r")
  9337. (when (org-in-regexp org-bracket-link-regexp 1)
  9338. (setq link (org-extract-attributes
  9339. (org-link-unescape (org-match-string-no-properties 1))))
  9340. (while (string-match " *\n *" link)
  9341. (setq link (replace-match " " t t link)))
  9342. (setq link (org-link-expand-abbrev link))
  9343. (cond
  9344. ((or (file-name-absolute-p link)
  9345. (string-match "^\\.\\.?/" link))
  9346. (setq type "file" path link))
  9347. ((string-match org-link-re-with-space3 link)
  9348. (setq type (match-string 1 link) path (match-string 2 link)))
  9349. ((string-match "^help:+\\(.+\\)" link)
  9350. (setq type "help" path (match-string 1 link)))
  9351. (t (setq type "thisfile" path link)))
  9352. (throw 'match t)))
  9353. (when (get-text-property (point) 'org-linked-text)
  9354. (setq type "thisfile"
  9355. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9356. (1+ (point)) (point))
  9357. path (buffer-substring
  9358. (or (previous-single-property-change pos 'org-linked-text)
  9359. (point-min))
  9360. (or (next-single-property-change pos 'org-linked-text)
  9361. (point-max)))
  9362. ;; Ensure we will search for a <<<radio>>> link, not
  9363. ;; a simple reference like <<ref>>
  9364. path (concat "<" path))
  9365. (throw 'match t))
  9366. (save-excursion
  9367. (when (or (org-in-regexp org-angle-link-re)
  9368. (let ((match (org-in-regexp org-plain-link-re)))
  9369. ;; Check a plain link is not within a bracket link
  9370. (and match
  9371. (save-excursion
  9372. (progn
  9373. (goto-char (car match))
  9374. (not (org-in-regexp org-bracket-link-regexp))))))
  9375. (let ((line_ending (save-excursion (end-of-line) (point))))
  9376. ;; We are in a line before a plain or bracket link
  9377. (or (re-search-forward org-plain-link-re line_ending t)
  9378. (re-search-forward org-bracket-link-regexp line_ending t))))
  9379. (setq type (match-string 1)
  9380. path (org-link-unescape (match-string 2)))
  9381. (throw 'match t)))
  9382. (save-excursion
  9383. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9384. (setq type "tags"
  9385. path (match-string 1))
  9386. (while (string-match ":" path)
  9387. (setq path (replace-match "+" t t path)))
  9388. (throw 'match t)))
  9389. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9390. (setq type "tree-match"
  9391. path (match-string 1))
  9392. (throw 'match t)))
  9393. (unless path
  9394. (user-error "No link found"))
  9395. ;; switch back to reference buffer
  9396. ;; needed when if called in a temporary buffer through
  9397. ;; org-open-link-from-string
  9398. (with-current-buffer (or reference-buffer (current-buffer))
  9399. ;; Remove any trailing spaces in path
  9400. (if (string-match " +\\'" path)
  9401. (setq path (replace-match "" t t path)))
  9402. (if (and org-link-translation-function
  9403. (fboundp org-link-translation-function))
  9404. ;; Check if we need to translate the link
  9405. (let ((tmp (funcall org-link-translation-function type path)))
  9406. (setq type (car tmp) path (cdr tmp))))
  9407. (cond
  9408. ((assoc type org-link-protocols)
  9409. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9410. ((equal type "help")
  9411. (let ((f-or-v (intern path)))
  9412. (cond ((fboundp f-or-v)
  9413. (describe-function f-or-v))
  9414. ((boundp f-or-v)
  9415. (describe-variable f-or-v))
  9416. (t (error "Not a known function or variable")))))
  9417. ((equal type "mailto")
  9418. (let ((cmd (car org-link-mailto-program))
  9419. (args (cdr org-link-mailto-program)) args1
  9420. (address path) (subject "") a)
  9421. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9422. (setq address (match-string 1 path)
  9423. subject (org-link-escape (match-string 2 path))))
  9424. (while args
  9425. (cond
  9426. ((not (stringp (car args))) (push (pop args) args1))
  9427. (t (setq a (pop args))
  9428. (if (string-match "%a" a)
  9429. (setq a (replace-match address t t a)))
  9430. (if (string-match "%s" a)
  9431. (setq a (replace-match subject t t a)))
  9432. (push a args1))))
  9433. (apply cmd (nreverse args1))))
  9434. ((member type '("http" "https" "ftp" "news"))
  9435. (browse-url
  9436. (concat type ":"
  9437. (if (org-string-match-p
  9438. (concat "[[:nonascii:]"
  9439. org-link-escape-chars-browser "]")
  9440. path)
  9441. (org-link-escape path org-link-escape-chars-browser)
  9442. path))))
  9443. ((string= type "doi")
  9444. (browse-url
  9445. (concat org-doi-server-url
  9446. (if (org-string-match-p
  9447. (concat "[[:nonascii:]"
  9448. org-link-escape-chars-browser "]")
  9449. path)
  9450. (org-link-escape path org-link-escape-chars-browser)
  9451. path))))
  9452. ((member type '("message"))
  9453. (browse-url (concat type ":" path)))
  9454. ((string= type "tags")
  9455. (org-tags-view arg path))
  9456. ((string= type "tree-match")
  9457. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9458. ((string= type "file")
  9459. (if (string-match "::\\([0-9]+\\)\\'" path)
  9460. (setq line (string-to-number (match-string 1 path))
  9461. path (substring path 0 (match-beginning 0)))
  9462. (if (string-match "::\\(.+\\)\\'" path)
  9463. (setq search (match-string 1 path)
  9464. path (substring path 0 (match-beginning 0)))))
  9465. (if (string-match "[*?{]" (file-name-nondirectory path))
  9466. (dired path)
  9467. (org-open-file path arg line search)))
  9468. ((string= type "shell")
  9469. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9470. (cmd path))
  9471. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9472. (string-match org-confirm-shell-link-not-regexp cmd))
  9473. (not org-confirm-shell-link-function)
  9474. (funcall org-confirm-shell-link-function
  9475. (format "Execute \"%s\" in shell? "
  9476. (org-add-props cmd nil
  9477. 'face 'org-warning))))
  9478. (progn
  9479. (message "Executing %s" cmd)
  9480. (shell-command cmd buf)
  9481. (if (featurep 'midnight)
  9482. (setq clean-buffer-list-kill-buffer-names
  9483. (cons buf clean-buffer-list-kill-buffer-names))))
  9484. (error "Abort"))))
  9485. ((string= type "elisp")
  9486. (let ((cmd path))
  9487. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9488. (string-match org-confirm-elisp-link-not-regexp cmd))
  9489. (not org-confirm-elisp-link-function)
  9490. (funcall org-confirm-elisp-link-function
  9491. (format "Execute \"%s\" as elisp? "
  9492. (org-add-props cmd nil
  9493. 'face 'org-warning))))
  9494. (message "%s => %s" cmd
  9495. (if (equal (string-to-char cmd) ?\()
  9496. (eval (read cmd))
  9497. (call-interactively (read cmd))))
  9498. (error "Abort"))))
  9499. ((and (string= type "thisfile")
  9500. (run-hook-with-args-until-success
  9501. 'org-open-link-functions path)))
  9502. ((string= type "thisfile")
  9503. (if arg
  9504. (switch-to-buffer-other-window
  9505. (org-get-buffer-for-internal-link (current-buffer)))
  9506. (org-mark-ring-push))
  9507. (let ((cmd `(org-link-search
  9508. ,path
  9509. ,(cond ((equal arg '(4)) ''occur)
  9510. ((equal arg '(16)) ''org-occur))
  9511. ,pos)))
  9512. (condition-case nil (let ((org-link-search-inhibit-query t))
  9513. (eval cmd))
  9514. (error (progn (widen) (eval cmd))))))
  9515. (t (browse-url-at-point)))))))
  9516. (move-marker org-open-link-marker nil)
  9517. (run-hook-with-args 'org-follow-link-hook)))
  9518. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9519. "Offer links in the current entry and return the selected link.
  9520. If there is only one link, return it.
  9521. If NTH is an integer, return the NTH link found.
  9522. If ZERO is a string, check also this string for a link, and if
  9523. there is one, return it."
  9524. (with-current-buffer buffer
  9525. (save-excursion
  9526. (save-restriction
  9527. (widen)
  9528. (goto-char marker)
  9529. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9530. "\\(" org-angle-link-re "\\)\\|"
  9531. "\\(" org-plain-link-re "\\)"))
  9532. (cnt ?0)
  9533. (in-emacs (if (integerp nth) nil nth))
  9534. have-zero end links link c)
  9535. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9536. (push (match-string 0 zero) links)
  9537. (setq cnt (1- cnt) have-zero t))
  9538. (save-excursion
  9539. (org-back-to-heading t)
  9540. (setq end (save-excursion (outline-next-heading) (point)))
  9541. (while (re-search-forward re end t)
  9542. (push (match-string 0) links))
  9543. (setq links (org-uniquify (reverse links))))
  9544. (cond
  9545. ((null links)
  9546. (message "No links"))
  9547. ((equal (length links) 1)
  9548. (setq link (car links)))
  9549. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9550. (setq link (nth (if have-zero nth (1- nth)) links)))
  9551. (t ; we have to select a link
  9552. (save-excursion
  9553. (save-window-excursion
  9554. (delete-other-windows)
  9555. (with-output-to-temp-buffer "*Select Link*"
  9556. (mapc (lambda (l)
  9557. (if (not (string-match org-bracket-link-regexp l))
  9558. (princ (format "[%c] %s\n" (incf cnt)
  9559. (org-remove-angle-brackets l)))
  9560. (if (match-end 3)
  9561. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9562. (match-string 3 l) (match-string 1 l)))
  9563. (princ (format "[%c] %s\n" (incf cnt)
  9564. (match-string 1 l))))))
  9565. links))
  9566. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9567. (message "Select link to open, RET to open all:")
  9568. (setq c (read-char-exclusive))
  9569. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9570. (when (equal c ?q) (error "Abort"))
  9571. (if (equal c ?\C-m)
  9572. (setq link links)
  9573. (setq nth (- c ?0))
  9574. (if have-zero (setq nth (1+ nth)))
  9575. (unless (and (integerp nth) (>= (length links) nth))
  9576. (user-error "Invalid link selection"))
  9577. (setq link (nth (1- nth) links)))))
  9578. (cons link end))))))
  9579. ;; Add special file links that specify the way of opening
  9580. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9581. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9582. (defun org-open-file-with-system (path)
  9583. "Open file at PATH using the system way of opening it."
  9584. (org-open-file path 'system))
  9585. (defun org-open-file-with-emacs (path)
  9586. "Open file at PATH in Emacs."
  9587. (org-open-file path 'emacs))
  9588. ;;; File search
  9589. (defvar org-create-file-search-functions nil
  9590. "List of functions to construct the right search string for a file link.
  9591. These functions are called in turn with point at the location to
  9592. which the link should point.
  9593. A function in the hook should first test if it would like to
  9594. handle this file type, for example by checking the `major-mode'
  9595. or the file extension. If it decides not to handle this file, it
  9596. should just return nil to give other functions a chance. If it
  9597. does handle the file, it must return the search string to be used
  9598. when following the link. The search string will be part of the
  9599. file link, given after a double colon, and `org-open-at-point'
  9600. will automatically search for it. If special measures must be
  9601. taken to make the search successful, another function should be
  9602. added to the companion hook `org-execute-file-search-functions',
  9603. which see.
  9604. A function in this hook may also use `setq' to set the variable
  9605. `description' to provide a suggestion for the descriptive text to
  9606. be used for this link when it gets inserted into an Org-mode
  9607. buffer with \\[org-insert-link].")
  9608. (defvar org-execute-file-search-functions nil
  9609. "List of functions to execute a file search triggered by a link.
  9610. Functions added to this hook must accept a single argument, the
  9611. search string that was part of the file link, the part after the
  9612. double colon. The function must first check if it would like to
  9613. handle this search, for example by checking the `major-mode' or
  9614. the file extension. If it decides not to handle this search, it
  9615. should just return nil to give other functions a chance. If it
  9616. does handle the search, it must return a non-nil value to keep
  9617. other functions from trying.
  9618. Each function can access the current prefix argument through the
  9619. variable `current-prefix-arg'. Note that a single prefix is used
  9620. to force opening a link in Emacs, so it may be good to only use a
  9621. numeric or double prefix to guide the search function.
  9622. In case this is needed, a function in this hook can also restore
  9623. the window configuration before `org-open-at-point' was called using:
  9624. (set-window-configuration org-window-config-before-follow-link)")
  9625. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9626. (defun org-link-search (s &optional type avoid-pos stealth)
  9627. "Search for a link search option.
  9628. If S is surrounded by forward slashes, it is interpreted as a
  9629. regular expression. In org-mode files, this will create an `org-occur'
  9630. sparse tree. In ordinary files, `occur' will be used to list matches.
  9631. If the current buffer is in `dired-mode', grep will be used to search
  9632. in all files. If AVOID-POS is given, ignore matches near that position.
  9633. When optional argument STEALTH is non-nil, do not modify
  9634. visibility around point, thus ignoring
  9635. `org-show-hierarchy-above', `org-show-following-heading' and
  9636. `org-show-siblings' variables."
  9637. (let ((case-fold-search t)
  9638. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9639. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9640. (append '(("") (" ") ("\t") ("\n"))
  9641. org-emphasis-alist)
  9642. "\\|") "\\)"))
  9643. (pos (point))
  9644. (pre nil) (post nil)
  9645. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9646. (cond
  9647. ;; First check if there are any special search functions
  9648. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9649. ;; Now try the builtin stuff
  9650. ((and (equal (string-to-char s0) ?#)
  9651. (> (length s0) 1)
  9652. (save-excursion
  9653. (goto-char (point-min))
  9654. (and
  9655. (re-search-forward
  9656. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9657. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9658. (setq type 'dedicated
  9659. pos (match-beginning 0))))
  9660. ;; There is an exact target for this
  9661. (goto-char pos)
  9662. (org-back-to-heading t)))
  9663. ((save-excursion
  9664. (goto-char (point-min))
  9665. (and
  9666. (re-search-forward
  9667. (concat "<<" (regexp-quote s0) ">>") nil t)
  9668. (setq type 'dedicated
  9669. pos (match-beginning 0))))
  9670. ;; There is an exact target for this
  9671. (goto-char pos))
  9672. ((save-excursion
  9673. (goto-char (point-min))
  9674. (and
  9675. (re-search-forward
  9676. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9677. (setq type 'dedicated pos (match-beginning 0))))
  9678. ;; Found an element with a matching #+name affiliated keyword.
  9679. (goto-char pos))
  9680. ((and (string-match "^(\\(.*\\))$" s0)
  9681. (save-excursion
  9682. (goto-char (point-min))
  9683. (and
  9684. (re-search-forward
  9685. (concat "[^[]" (regexp-quote
  9686. (format org-coderef-label-format
  9687. (match-string 1 s0))))
  9688. nil t)
  9689. (setq type 'dedicated
  9690. pos (1+ (match-beginning 0))))))
  9691. ;; There is a coderef target for this
  9692. (goto-char pos))
  9693. ((string-match "^/\\(.*\\)/$" s)
  9694. ;; A regular expression
  9695. (cond
  9696. ((derived-mode-p 'org-mode)
  9697. (org-occur (match-string 1 s)))
  9698. (t (org-do-occur (match-string 1 s)))))
  9699. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9700. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9701. (goto-char (point-min))
  9702. (cond
  9703. ((let (case-fold-search)
  9704. (re-search-forward (format org-complex-heading-regexp-format
  9705. (regexp-quote s))
  9706. nil t))
  9707. ;; OK, found a match
  9708. (setq type 'dedicated)
  9709. (goto-char (match-beginning 0)))
  9710. ((and (not org-link-search-inhibit-query)
  9711. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9712. (y-or-n-p "No match - create this as a new heading? "))
  9713. (goto-char (point-max))
  9714. (or (bolp) (newline))
  9715. (insert "* " s "\n")
  9716. (beginning-of-line 0))
  9717. (t
  9718. (goto-char pos)
  9719. (error "No match"))))
  9720. (t
  9721. ;; A normal search string
  9722. (when (equal (string-to-char s) ?*)
  9723. ;; Anchor on headlines, post may include tags.
  9724. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9725. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9726. s (substring s 1)))
  9727. (remove-text-properties
  9728. 0 (length s)
  9729. '(face nil mouse-face nil keymap nil fontified nil) s)
  9730. ;; Make a series of regular expressions to find a match
  9731. (setq words (org-split-string s "[ \n\r\t]+")
  9732. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9733. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9734. "\\)" markers)
  9735. re2a_ (concat "\\(" (mapconcat 'downcase words
  9736. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9737. re2a (concat "[ \t\r\n]" re2a_)
  9738. re4_ (concat "\\(" (mapconcat 'downcase words
  9739. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9740. re4 (concat "[^a-zA-Z_]" re4_)
  9741. re1 (concat pre re2 post)
  9742. re3 (concat pre (if pre re4_ re4) post)
  9743. re5 (concat pre ".*" re4)
  9744. re2 (concat pre re2)
  9745. re2a (concat pre (if pre re2a_ re2a))
  9746. re4 (concat pre (if pre re4_ re4))
  9747. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9748. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9749. re5 "\\)"))
  9750. (cond
  9751. ((eq type 'org-occur) (org-occur reall))
  9752. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9753. (t (goto-char (point-min))
  9754. (setq type 'fuzzy)
  9755. (if (or (and (org-search-not-self 1 re0 nil t)
  9756. (setq type 'dedicated))
  9757. (org-search-not-self 1 re1 nil t)
  9758. (org-search-not-self 1 re2 nil t)
  9759. (org-search-not-self 1 re2a nil t)
  9760. (org-search-not-self 1 re3 nil t)
  9761. (org-search-not-self 1 re4 nil t)
  9762. (org-search-not-self 1 re5 nil t))
  9763. (goto-char (match-beginning 1))
  9764. (goto-char pos)
  9765. (error "No match"))))))
  9766. (and (derived-mode-p 'org-mode)
  9767. (not stealth)
  9768. (org-show-context 'link-search))
  9769. type))
  9770. (defun org-search-not-self (group &rest args)
  9771. "Execute `re-search-forward', but only accept matches that do not
  9772. enclose the position of `org-open-link-marker'."
  9773. (let ((m org-open-link-marker))
  9774. (catch 'exit
  9775. (while (apply 're-search-forward args)
  9776. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9777. (goto-char (match-end group))
  9778. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9779. (> (match-beginning 0) (marker-position m))
  9780. (< (match-end 0) (marker-position m)))
  9781. (save-match-data
  9782. (or (not (org-in-regexp
  9783. org-bracket-link-analytic-regexp 1))
  9784. (not (match-end 4)) ; no description
  9785. (and (<= (match-beginning 4) (point))
  9786. (>= (match-end 4) (point))))))
  9787. (throw 'exit (point))))))))
  9788. (defun org-get-buffer-for-internal-link (buffer)
  9789. "Return a buffer to be used for displaying the link target of internal links."
  9790. (cond
  9791. ((not org-display-internal-link-with-indirect-buffer)
  9792. buffer)
  9793. ((string-match "(Clone)$" (buffer-name buffer))
  9794. (message "Buffer is already a clone, not making another one")
  9795. ;; we also do not modify visibility in this case
  9796. buffer)
  9797. (t ; make a new indirect buffer for displaying the link
  9798. (let* ((bn (buffer-name buffer))
  9799. (ibn (concat bn "(Clone)"))
  9800. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9801. (with-current-buffer ib (org-overview))
  9802. ib))))
  9803. (defun org-do-occur (regexp &optional cleanup)
  9804. "Call the Emacs command `occur'.
  9805. If CLEANUP is non-nil, remove the printout of the regular expression
  9806. in the *Occur* buffer. This is useful if the regex is long and not useful
  9807. to read."
  9808. (occur regexp)
  9809. (when cleanup
  9810. (let ((cwin (selected-window)) win beg end)
  9811. (when (setq win (get-buffer-window "*Occur*"))
  9812. (select-window win))
  9813. (goto-char (point-min))
  9814. (when (re-search-forward "match[a-z]+" nil t)
  9815. (setq beg (match-end 0))
  9816. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9817. (setq end (1- (match-beginning 0)))))
  9818. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9819. (goto-char (point-min))
  9820. (select-window cwin))))
  9821. ;;; The mark ring for links jumps
  9822. (defvar org-mark-ring nil
  9823. "Mark ring for positions before jumps in Org-mode.")
  9824. (defvar org-mark-ring-last-goto nil
  9825. "Last position in the mark ring used to go back.")
  9826. ;; Fill and close the ring
  9827. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9828. (loop for i from 1 to org-mark-ring-length do
  9829. (push (make-marker) org-mark-ring))
  9830. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9831. org-mark-ring)
  9832. (defun org-mark-ring-push (&optional pos buffer)
  9833. "Put the current position or POS into the mark ring and rotate it."
  9834. (interactive)
  9835. (setq pos (or pos (point)))
  9836. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9837. (move-marker (car org-mark-ring)
  9838. (or pos (point))
  9839. (or buffer (current-buffer)))
  9840. (message "%s"
  9841. (substitute-command-keys
  9842. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9843. (defun org-mark-ring-goto (&optional n)
  9844. "Jump to the previous position in the mark ring.
  9845. With prefix arg N, jump back that many stored positions. When
  9846. called several times in succession, walk through the entire ring.
  9847. Org-mode commands jumping to a different position in the current file,
  9848. or to another Org-mode file, automatically push the old position
  9849. onto the ring."
  9850. (interactive "p")
  9851. (let (p m)
  9852. (if (eq last-command this-command)
  9853. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9854. (setq p org-mark-ring))
  9855. (setq org-mark-ring-last-goto p)
  9856. (setq m (car p))
  9857. (org-pop-to-buffer-same-window (marker-buffer m))
  9858. (goto-char m)
  9859. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9860. (defun org-remove-angle-brackets (s)
  9861. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9862. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9863. s)
  9864. (defun org-add-angle-brackets (s)
  9865. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9866. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9867. s)
  9868. (defun org-remove-double-quotes (s)
  9869. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9870. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9871. s)
  9872. ;;; Following specific links
  9873. (defun org-follow-timestamp-link ()
  9874. "Open an agenda view for the time-stamp date/range at point."
  9875. (cond
  9876. ((org-at-date-range-p t)
  9877. (let ((org-agenda-start-on-weekday)
  9878. (t1 (match-string 1))
  9879. (t2 (match-string 2)) tt1 tt2)
  9880. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9881. tt2 (time-to-days (org-time-string-to-time t2)))
  9882. (let ((org-agenda-buffer-tmp-name
  9883. (format "*Org Agenda(a:%s)"
  9884. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9885. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9886. ((org-at-timestamp-p t)
  9887. (let ((org-agenda-buffer-tmp-name
  9888. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9889. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9890. (substring (match-string 1) 0 10)))
  9891. 1)))
  9892. (t (error "This should not happen"))))
  9893. ;;; Following file links
  9894. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9895. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9896. (declare-function mailcap-mime-info
  9897. "mailcap" (string &optional request no-decode))
  9898. (defvar org-wait nil)
  9899. (defun org-open-file (path &optional in-emacs line search)
  9900. "Open the file at PATH.
  9901. First, this expands any special file name abbreviations. Then the
  9902. configuration variable `org-file-apps' is checked if it contains an
  9903. entry for this file type, and if yes, the corresponding command is launched.
  9904. If no application is found, Emacs simply visits the file.
  9905. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9906. With a double \\[universal-argument] \\[universal-argument] \
  9907. prefix arg, Org tries to avoid opening in Emacs
  9908. and to use an external application to visit the file.
  9909. Optional LINE specifies a line to go to, optional SEARCH a string
  9910. to search for. If LINE or SEARCH is given, the file will be
  9911. opened in Emacs, unless an entry from org-file-apps that makes
  9912. use of groups in a regexp matches.
  9913. If you want to change the way frames are used when following a
  9914. link, please customize `org-link-frame-setup'.
  9915. If the file does not exist, an error is thrown."
  9916. (let* ((file (if (equal path "")
  9917. buffer-file-name
  9918. (substitute-in-file-name (expand-file-name path))))
  9919. (file-apps (append org-file-apps (org-default-apps)))
  9920. (apps (org-remove-if
  9921. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9922. (apps-dlink (org-remove-if-not
  9923. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9924. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9925. (dirp (if remp nil (file-directory-p file)))
  9926. (file (if (and dirp org-open-directory-means-index-dot-org)
  9927. (concat (file-name-as-directory file) "index.org")
  9928. file))
  9929. (a-m-a-p (assq 'auto-mode apps))
  9930. (dfile (downcase file))
  9931. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9932. (link (cond ((and (eq line nil)
  9933. (eq search nil))
  9934. file)
  9935. (line
  9936. (concat file "::" (number-to-string line)))
  9937. (search
  9938. (concat file "::" search))))
  9939. (dlink (downcase link))
  9940. (old-buffer (current-buffer))
  9941. (old-pos (point))
  9942. (old-mode major-mode)
  9943. ext cmd link-match-data)
  9944. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9945. (setq ext (match-string 1 dfile))
  9946. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9947. (setq ext (match-string 1 dfile))))
  9948. (cond
  9949. ((member in-emacs '((16) system))
  9950. (setq cmd (cdr (assoc 'system apps))))
  9951. (in-emacs (setq cmd 'emacs))
  9952. (t
  9953. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9954. (and dirp (cdr (assoc 'directory apps)))
  9955. ; first, try matching against apps-dlink
  9956. ; if we get a match here, store the match data for later
  9957. (let ((match (assoc-default dlink apps-dlink
  9958. 'string-match)))
  9959. (if match
  9960. (progn (setq link-match-data (match-data))
  9961. match)
  9962. (progn (setq in-emacs (or in-emacs line search))
  9963. nil))) ; if we have no match in apps-dlink,
  9964. ; always open the file in emacs if line or search
  9965. ; is given (for backwards compatibility)
  9966. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9967. 'string-match)
  9968. (cdr (assoc ext apps))
  9969. (cdr (assoc t apps))))))
  9970. (when (eq cmd 'system)
  9971. (setq cmd (cdr (assoc 'system apps))))
  9972. (when (eq cmd 'default)
  9973. (setq cmd (cdr (assoc t apps))))
  9974. (when (eq cmd 'mailcap)
  9975. (require 'mailcap)
  9976. (mailcap-parse-mailcaps)
  9977. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9978. (command (mailcap-mime-info mime-type)))
  9979. (if (stringp command)
  9980. (setq cmd command)
  9981. (setq cmd 'emacs))))
  9982. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9983. (not (file-exists-p file))
  9984. (not org-open-non-existing-files))
  9985. (user-error "No such file: %s" file))
  9986. (cond
  9987. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9988. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9989. (while (string-match "['\"]%s['\"]" cmd)
  9990. (setq cmd (replace-match "%s" t t cmd)))
  9991. (while (string-match "%s" cmd)
  9992. (setq cmd (replace-match
  9993. (save-match-data
  9994. (shell-quote-argument
  9995. (convert-standard-filename file)))
  9996. t t cmd)))
  9997. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9998. (save-match-data
  9999. (let ((match-index 1)
  10000. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10001. (set-match-data link-match-data)
  10002. (while (<= match-index number-of-groups)
  10003. (let ((regex (concat "%" (number-to-string match-index)))
  10004. (replace-with (match-string match-index dlink)))
  10005. (while (string-match regex cmd)
  10006. (setq cmd (replace-match replace-with t t cmd))))
  10007. (setq match-index (+ match-index 1)))))
  10008. (save-window-excursion
  10009. (message "Running %s...done" cmd)
  10010. (start-process-shell-command cmd nil cmd)
  10011. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10012. ((or (stringp cmd)
  10013. (eq cmd 'emacs))
  10014. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10015. (widen)
  10016. (if line (org-goto-line line)
  10017. (if search (org-link-search search))))
  10018. ((consp cmd)
  10019. (let ((file (convert-standard-filename file)))
  10020. (save-match-data
  10021. (set-match-data link-match-data)
  10022. (eval cmd))))
  10023. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10024. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10025. (or (not (equal old-buffer (current-buffer)))
  10026. (not (equal old-pos (point))))
  10027. (org-mark-ring-push old-pos old-buffer))))
  10028. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10029. "This function returns non-nil if `entry' uses a regular
  10030. expression which should be matched against the whole link by
  10031. org-open-file.
  10032. It assumes that is the case when the entry uses a regular
  10033. expression which has at least one grouping construct and the
  10034. action is either a lisp form or a command string containing
  10035. '%1', i.e. using at least one subexpression match as a
  10036. parameter."
  10037. (let ((selector (car entry))
  10038. (action (cdr entry)))
  10039. (if (stringp selector)
  10040. (and (> (regexp-opt-depth selector) 0)
  10041. (or (and (stringp action)
  10042. (string-match "%[0-9]" action))
  10043. (consp action)))
  10044. nil)))
  10045. (defun org-default-apps ()
  10046. "Return the default applications for this operating system."
  10047. (cond
  10048. ((eq system-type 'darwin)
  10049. org-file-apps-defaults-macosx)
  10050. ((eq system-type 'windows-nt)
  10051. org-file-apps-defaults-windowsnt)
  10052. (t org-file-apps-defaults-gnu)))
  10053. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10054. "Convert extensions to regular expressions in the cars of LIST.
  10055. Also, weed out any non-string entries, because the return value is used
  10056. only for regexp matching.
  10057. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10058. point to the symbol `emacs', indicating that the file should
  10059. be opened in Emacs."
  10060. (append
  10061. (delq nil
  10062. (mapcar (lambda (x)
  10063. (if (not (stringp (car x)))
  10064. nil
  10065. (if (string-match "\\W" (car x))
  10066. x
  10067. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10068. list))
  10069. (if add-auto-mode
  10070. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10071. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10072. (defun org-file-remote-p (file)
  10073. "Test whether FILE specifies a location on a remote system.
  10074. Return non-nil if the location is indeed remote.
  10075. For example, the filename \"/user@host:/foo\" specifies a location
  10076. on the system \"/user@host:\"."
  10077. (cond ((fboundp 'file-remote-p)
  10078. (file-remote-p file))
  10079. ((fboundp 'tramp-handle-file-remote-p)
  10080. (tramp-handle-file-remote-p file))
  10081. ((and (boundp 'ange-ftp-name-format)
  10082. (string-match (car ange-ftp-name-format) file))
  10083. t)))
  10084. ;;;; Refiling
  10085. (defun org-get-org-file ()
  10086. "Read a filename, with default directory `org-directory'."
  10087. (let ((default (or org-default-notes-file remember-data-file)))
  10088. (read-file-name (format "File name [%s]: " default)
  10089. (file-name-as-directory org-directory)
  10090. default)))
  10091. (defun org-notes-order-reversed-p ()
  10092. "Check if the current file should receive notes in reversed order."
  10093. (cond
  10094. ((not org-reverse-note-order) nil)
  10095. ((eq t org-reverse-note-order) t)
  10096. ((not (listp org-reverse-note-order)) nil)
  10097. (t (catch 'exit
  10098. (let ((all org-reverse-note-order)
  10099. entry)
  10100. (while (setq entry (pop all))
  10101. (if (string-match (car entry) buffer-file-name)
  10102. (throw 'exit (cdr entry))))
  10103. nil)))))
  10104. (defvar org-refile-target-table nil
  10105. "The list of refile targets, created by `org-refile'.")
  10106. (defvar org-agenda-new-buffers nil
  10107. "Buffers created to visit agenda files.")
  10108. (defvar org-refile-cache nil
  10109. "Cache for refile targets.")
  10110. (defvar org-refile-markers nil
  10111. "All the markers used for caching refile locations.")
  10112. (defun org-refile-marker (pos)
  10113. "Get a new refile marker, but only if caching is in use."
  10114. (if (not org-refile-use-cache)
  10115. pos
  10116. (let ((m (make-marker)))
  10117. (move-marker m pos)
  10118. (push m org-refile-markers)
  10119. m)))
  10120. (defun org-refile-cache-clear ()
  10121. "Clear the refile cache and disable all the markers."
  10122. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10123. (setq org-refile-markers nil)
  10124. (setq org-refile-cache nil)
  10125. (message "Refile cache has been cleared"))
  10126. (defun org-refile-cache-check-set (set)
  10127. "Check if all the markers in the cache still have live buffers."
  10128. (let (marker)
  10129. (catch 'exit
  10130. (while (and set (setq marker (nth 3 (pop set))))
  10131. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10132. (when (and marker (null (marker-buffer marker)))
  10133. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10134. (sit-for 3)
  10135. (throw 'exit nil)))
  10136. t)))
  10137. (defun org-refile-cache-put (set &rest identifiers)
  10138. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10139. (let* ((key (sha1 (prin1-to-string identifiers)))
  10140. (entry (assoc key org-refile-cache)))
  10141. (if entry
  10142. (setcdr entry set)
  10143. (push (cons key set) org-refile-cache))))
  10144. (defun org-refile-cache-get (&rest identifiers)
  10145. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10146. (cond
  10147. ((not org-refile-cache) nil)
  10148. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10149. (t
  10150. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10151. org-refile-cache))))
  10152. (and set (org-refile-cache-check-set set) set)))))
  10153. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10154. "Produce a table with refile targets."
  10155. (let ((case-fold-search nil)
  10156. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10157. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10158. targets tgs txt re files f desc descre fast-path-p level pos0)
  10159. (message "Getting targets...")
  10160. (with-current-buffer (or default-buffer (current-buffer))
  10161. (while (setq entry (pop entries))
  10162. (setq files (car entry) desc (cdr entry))
  10163. (setq fast-path-p nil)
  10164. (cond
  10165. ((null files) (setq files (list (current-buffer))))
  10166. ((eq files 'org-agenda-files)
  10167. (setq files (org-agenda-files 'unrestricted)))
  10168. ((and (symbolp files) (fboundp files))
  10169. (setq files (funcall files)))
  10170. ((and (symbolp files) (boundp files))
  10171. (setq files (symbol-value files))))
  10172. (if (stringp files) (setq files (list files)))
  10173. (cond
  10174. ((eq (car desc) :tag)
  10175. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10176. ((eq (car desc) :todo)
  10177. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10178. ((eq (car desc) :regexp)
  10179. (setq descre (cdr desc)))
  10180. ((eq (car desc) :level)
  10181. (setq descre (concat "^\\*\\{" (number-to-string
  10182. (if org-odd-levels-only
  10183. (1- (* 2 (cdr desc)))
  10184. (cdr desc)))
  10185. "\\}[ \t]")))
  10186. ((eq (car desc) :maxlevel)
  10187. (setq fast-path-p t)
  10188. (setq descre (concat "^\\*\\{1," (number-to-string
  10189. (if org-odd-levels-only
  10190. (1- (* 2 (cdr desc)))
  10191. (cdr desc)))
  10192. "\\}[ \t]")))
  10193. (t (error "Bad refiling target description %s" desc)))
  10194. (while (setq f (pop files))
  10195. (with-current-buffer
  10196. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10197. (or
  10198. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10199. (progn
  10200. (if (bufferp f) (setq f (buffer-file-name
  10201. (buffer-base-buffer f))))
  10202. (setq f (and f (expand-file-name f)))
  10203. (if (eq org-refile-use-outline-path 'file)
  10204. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10205. (save-excursion
  10206. (save-restriction
  10207. (widen)
  10208. (goto-char (point-min))
  10209. (while (re-search-forward descre nil t)
  10210. (goto-char (setq pos0 (point-at-bol)))
  10211. (catch 'next
  10212. (when org-refile-target-verify-function
  10213. (save-match-data
  10214. (or (funcall org-refile-target-verify-function)
  10215. (throw 'next t))))
  10216. (when (and (looking-at org-complex-heading-regexp)
  10217. (not (member (match-string 4) excluded-entries))
  10218. (match-string 4))
  10219. (setq level (org-reduced-level
  10220. (- (match-end 1) (match-beginning 1)))
  10221. txt (org-link-display-format (match-string 4))
  10222. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10223. re (format org-complex-heading-regexp-format
  10224. (regexp-quote (match-string 4))))
  10225. (when org-refile-use-outline-path
  10226. (setq txt (mapconcat
  10227. 'org-protect-slash
  10228. (append
  10229. (if (eq org-refile-use-outline-path
  10230. 'file)
  10231. (list (file-name-nondirectory
  10232. (buffer-file-name
  10233. (buffer-base-buffer))))
  10234. (if (eq org-refile-use-outline-path
  10235. 'full-file-path)
  10236. (list (buffer-file-name
  10237. (buffer-base-buffer)))))
  10238. (org-get-outline-path fast-path-p
  10239. level txt)
  10240. (list txt))
  10241. "/")))
  10242. (push (list txt f re (org-refile-marker (point)))
  10243. tgs)))
  10244. (when (= (point) pos0)
  10245. ;; verification function has not moved point
  10246. (goto-char (point-at-eol))))))))
  10247. (when org-refile-use-cache
  10248. (org-refile-cache-put tgs (buffer-file-name) descre))
  10249. (setq targets (append tgs targets))))))
  10250. (message "Getting targets...done")
  10251. (nreverse targets)))
  10252. (defun org-protect-slash (s)
  10253. (while (string-match "/" s)
  10254. (setq s (replace-match "\\" t t s)))
  10255. s)
  10256. (defvar org-olpa (make-vector 20 nil))
  10257. (defun org-get-outline-path (&optional fastp level heading)
  10258. "Return the outline path to the current entry, as a list.
  10259. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10260. routine which makes outline path derivations for an entire file,
  10261. avoiding backtracing. Refile target collection makes use of that."
  10262. (if fastp
  10263. (progn
  10264. (if (> level 19)
  10265. (error "Outline path failure, more than 19 levels"))
  10266. (loop for i from level upto 19 do
  10267. (aset org-olpa i nil))
  10268. (prog1
  10269. (delq nil (append org-olpa nil))
  10270. (aset org-olpa level heading)))
  10271. (let (rtn case-fold-search)
  10272. (save-excursion
  10273. (save-restriction
  10274. (widen)
  10275. (while (org-up-heading-safe)
  10276. (when (looking-at org-complex-heading-regexp)
  10277. (push (org-trim
  10278. (replace-regexp-in-string
  10279. ;; Remove statistical/checkboxes cookies
  10280. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10281. (org-match-string-no-properties 4)))
  10282. rtn)))
  10283. rtn)))))
  10284. (defun org-format-outline-path (path &optional width prefix separator)
  10285. "Format the outline path PATH for display.
  10286. WIDTH is the maximum number of characters that is available.
  10287. PREFIX is a prefix to be included in the returned string,
  10288. such as the file name.
  10289. SEPARATOR is inserted between the different parts of the path,
  10290. the default is \"/\"."
  10291. (setq width (or width 79))
  10292. (if prefix (setq width (- width (length prefix))))
  10293. (if (not path)
  10294. (or prefix "")
  10295. (let* ((nsteps (length path))
  10296. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10297. (maxwidth (if (<= total-width width)
  10298. 10000 ;; everything fits
  10299. ;; we need to shorten the level headings
  10300. (/ (- width nsteps) nsteps)))
  10301. (org-odd-levels-only nil)
  10302. (n 0)
  10303. (total (1+ (length prefix))))
  10304. (setq maxwidth (max maxwidth 10))
  10305. (concat prefix
  10306. (if prefix (or separator "/"))
  10307. (mapconcat
  10308. (lambda (h)
  10309. (setq n (1+ n))
  10310. (if (and (= n nsteps) (< maxwidth 10000))
  10311. (setq maxwidth (- total-width total)))
  10312. (if (< (length h) maxwidth)
  10313. (progn (setq total (+ total (length h) 1)) h)
  10314. (setq h (substring h 0 (- maxwidth 2))
  10315. total (+ total maxwidth 1))
  10316. (if (string-match "[ \t]+\\'" h)
  10317. (setq h (substring h 0 (match-beginning 0))))
  10318. (setq h (concat h "..")))
  10319. (org-add-props h nil 'face
  10320. (nth (% (1- n) org-n-level-faces)
  10321. org-level-faces))
  10322. h)
  10323. path (or separator "/"))))))
  10324. (defun org-display-outline-path (&optional file current separator just-return-string)
  10325. "Display the current outline path in the echo area.
  10326. If FILE is non-nil, prepend the output with the file name.
  10327. If CURRENT is non-nil, append the current heading to the output.
  10328. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10329. the different parts of the path and defaults to \"/\".
  10330. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10331. (interactive "P")
  10332. (let* (case-fold-search
  10333. message-log-max ; Don't populate the *Messages* buffer
  10334. (bfn (buffer-file-name (buffer-base-buffer)))
  10335. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10336. res)
  10337. (if current (setq path (append path
  10338. (save-excursion
  10339. (org-back-to-heading t)
  10340. (if (looking-at org-complex-heading-regexp)
  10341. (list (match-string 4)))))))
  10342. (setq res
  10343. (org-format-outline-path
  10344. path
  10345. (1- (frame-width))
  10346. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10347. separator))
  10348. (if just-return-string
  10349. (org-no-properties res)
  10350. (let ((message-log-max nil))
  10351. (message "%s" res)))))
  10352. (defvar org-refile-history nil
  10353. "History for refiling operations.")
  10354. (defvar org-after-refile-insert-hook nil
  10355. "Hook run after `org-refile' has inserted its stuff at the new location.
  10356. Note that this is still *before* the stuff will be removed from
  10357. the *old* location.")
  10358. (defvar org-capture-last-stored-marker)
  10359. (defvar org-refile-keep nil
  10360. "Non-nil means `org-refile' will copy instead of refile.")
  10361. (defun org-copy ()
  10362. "Like `org-refile', but copy."
  10363. (interactive)
  10364. (let ((org-refile-keep t))
  10365. (funcall 'org-refile nil nil nil "Copy")))
  10366. (defun org-refile (&optional goto default-buffer rfloc msg)
  10367. "Move the entry or entries at point to another heading.
  10368. The list of target headings is compiled using the information in
  10369. `org-refile-targets', which see.
  10370. At the target location, the entry is filed as a subitem of the target
  10371. heading. Depending on `org-reverse-note-order', the new subitem will
  10372. either be the first or the last subitem.
  10373. If there is an active region, all entries in that region will be moved.
  10374. However, the region must fulfill the requirement that the first heading
  10375. is the first one sets the top-level of the moved text - at most siblings
  10376. below it are allowed.
  10377. With prefix arg GOTO, the command will only visit the target location
  10378. and not actually move anything.
  10379. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10380. go to the location where the last refiling operation has put the subtree.
  10381. With a numeric prefix argument of `2', refile to the running clock.
  10382. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10383. being set to `t' and copy to the target location, don't move it.
  10384. Beware that keeping refiled entries may result in duplicated ID
  10385. properties.
  10386. RFLOC can be a refile location obtained in a different way.
  10387. MSG is a string to replace \"Refile\" in the default prompt with
  10388. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10389. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10390. If you are using target caching (see `org-refile-use-cache'),
  10391. you have to clear the target cache in order to find new targets.
  10392. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10393. prefix argument (`C-u C-u C-u C-c C-w')."
  10394. (interactive "P")
  10395. (if (member goto '(0 (64)))
  10396. (org-refile-cache-clear)
  10397. (let* ((actionmsg (or msg "Refile"))
  10398. (cbuf (current-buffer))
  10399. (regionp (org-region-active-p))
  10400. (region-start (and regionp (region-beginning)))
  10401. (region-end (and regionp (region-end)))
  10402. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10403. (org-refile-keep (if (equal goto 3) t org-refile-keep))
  10404. pos it nbuf file re level reversed)
  10405. (setq last-command nil)
  10406. (when regionp
  10407. (goto-char region-start)
  10408. (or (bolp) (goto-char (point-at-bol)))
  10409. (setq region-start (point))
  10410. (unless (or (org-kill-is-subtree-p
  10411. (buffer-substring region-start region-end))
  10412. (prog1 org-refile-active-region-within-subtree
  10413. (let ((s (point-at-eol)))
  10414. (org-toggle-heading)
  10415. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10416. (user-error "The region is not a (sequence of) subtree(s)")))
  10417. (if (equal goto '(16))
  10418. (org-refile-goto-last-stored)
  10419. (when (or
  10420. (and (equal goto 2)
  10421. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10422. (prog1
  10423. (setq it (list (or org-clock-heading "running clock")
  10424. (buffer-file-name
  10425. (marker-buffer org-clock-hd-marker))
  10426. ""
  10427. (marker-position org-clock-hd-marker)))
  10428. (setq goto nil)))
  10429. (setq it (or rfloc
  10430. (let (heading-text)
  10431. (save-excursion
  10432. (unless goto
  10433. (org-back-to-heading t)
  10434. (setq heading-text
  10435. (nth 4 (org-heading-components))))
  10436. (org-refile-get-location
  10437. (cond (goto "Goto")
  10438. (regionp (concat actionmsg " region to"))
  10439. (t (concat actionmsg " subtree \""
  10440. heading-text "\" to")))
  10441. default-buffer
  10442. (and (not (equal '(4) goto))
  10443. org-refile-allow-creating-parent-nodes)
  10444. goto))))))
  10445. (setq file (nth 1 it)
  10446. re (nth 2 it)
  10447. pos (nth 3 it))
  10448. (if (and (not goto)
  10449. pos
  10450. (equal (buffer-file-name) file)
  10451. (if regionp
  10452. (and (>= pos region-start)
  10453. (<= pos region-end))
  10454. (and (>= pos (point))
  10455. (< pos (save-excursion
  10456. (org-end-of-subtree t t))))))
  10457. (error "Cannot refile to position inside the tree or region"))
  10458. (setq nbuf (or (find-buffer-visiting file)
  10459. (find-file-noselect file)))
  10460. (if (and goto (not (equal goto 3)))
  10461. (progn
  10462. (org-pop-to-buffer-same-window nbuf)
  10463. (goto-char pos)
  10464. (org-show-context 'org-goto))
  10465. (if regionp
  10466. (progn
  10467. (org-kill-new (buffer-substring region-start region-end))
  10468. (org-save-markers-in-region region-start region-end))
  10469. (org-copy-subtree 1 nil t))
  10470. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10471. (find-file-noselect file)))
  10472. (setq reversed (org-notes-order-reversed-p))
  10473. (save-excursion
  10474. (save-restriction
  10475. (widen)
  10476. (if pos
  10477. (progn
  10478. (goto-char pos)
  10479. (looking-at org-outline-regexp)
  10480. (setq level (org-get-valid-level (funcall outline-level) 1))
  10481. (goto-char
  10482. (if reversed
  10483. (or (outline-next-heading) (point-max))
  10484. (or (save-excursion (org-get-next-sibling))
  10485. (org-end-of-subtree t t)
  10486. (point-max)))))
  10487. (setq level 1)
  10488. (if (not reversed)
  10489. (goto-char (point-max))
  10490. (goto-char (point-min))
  10491. (or (outline-next-heading) (goto-char (point-max)))))
  10492. (if (not (bolp)) (newline))
  10493. (org-paste-subtree level)
  10494. (when org-log-refile
  10495. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10496. (unless (eq org-log-refile 'note)
  10497. (save-excursion (org-add-log-note))))
  10498. (and org-auto-align-tags
  10499. (let ((org-loop-over-headlines-in-active-region nil))
  10500. (org-set-tags nil t)))
  10501. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10502. :last-refile)))
  10503. (when bookmark-name
  10504. (with-demoted-errors
  10505. (bookmark-set bookmark-name))))
  10506. ;; If we are refiling for capture, make sure that the
  10507. ;; last-capture pointers point here
  10508. (when (org-bound-and-true-p org-refile-for-capture)
  10509. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10510. :last-capture-marker)))
  10511. (when bookmark-name
  10512. (with-demoted-errors
  10513. (bookmark-set bookmark-name))))
  10514. (move-marker org-capture-last-stored-marker (point)))
  10515. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10516. (run-hooks 'org-after-refile-insert-hook))))
  10517. (unless org-refile-keep
  10518. (if regionp
  10519. (delete-region (point) (+ (point) (- region-end region-start)))
  10520. (delete-region
  10521. (and (org-back-to-heading t) (point))
  10522. (min (buffer-size) (org-end-of-subtree t t) (point)))))
  10523. (when (featurep 'org-inlinetask)
  10524. (org-inlinetask-remove-END-maybe))
  10525. (setq org-markers-to-move nil)
  10526. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10527. (defun org-refile-goto-last-stored ()
  10528. "Go to the location where the last refile was stored."
  10529. (interactive)
  10530. (bookmark-jump "org-refile-last-stored")
  10531. (message "This is the location of the last refile"))
  10532. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10533. no-exclude)
  10534. "Prompt the user for a refile location, using PROMPT.
  10535. PROMPT should not be suffixed with a colon and a space, because
  10536. this function appends the default value from
  10537. `org-refile-history' automatically, if that is not empty.
  10538. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10539. this is used for the GOTO interface."
  10540. (let ((org-refile-targets org-refile-targets)
  10541. (org-refile-use-outline-path org-refile-use-outline-path)
  10542. excluded-entries)
  10543. (when (and (derived-mode-p 'org-mode)
  10544. (not org-refile-use-cache)
  10545. (not no-exclude))
  10546. (org-map-tree
  10547. (lambda()
  10548. (setq excluded-entries
  10549. (append excluded-entries (list (org-get-heading t t)))))))
  10550. (setq org-refile-target-table
  10551. (org-refile-get-targets default-buffer excluded-entries)))
  10552. (unless org-refile-target-table
  10553. (user-error "No refile targets"))
  10554. (let* ((cbuf (current-buffer))
  10555. (partial-completion-mode nil)
  10556. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10557. (cfunc (if (and org-refile-use-outline-path
  10558. org-outline-path-complete-in-steps)
  10559. 'org-olpath-completing-read
  10560. 'org-icompleting-read))
  10561. (extra (if org-refile-use-outline-path "/" ""))
  10562. (cbnex (concat (buffer-name) extra))
  10563. (filename (and cfn (expand-file-name cfn)))
  10564. (tbl (mapcar
  10565. (lambda (x)
  10566. (if (and (not (member org-refile-use-outline-path
  10567. '(file full-file-path)))
  10568. (not (equal filename (nth 1 x))))
  10569. (cons (concat (car x) extra " ("
  10570. (file-name-nondirectory (nth 1 x)) ")")
  10571. (cdr x))
  10572. (cons (concat (car x) extra) (cdr x))))
  10573. org-refile-target-table))
  10574. (completion-ignore-case t)
  10575. cdef
  10576. (prompt (concat prompt
  10577. (or (and (car org-refile-history)
  10578. (concat " (default " (car org-refile-history) ")"))
  10579. (and (assoc cbnex tbl) (setq cdef cbnex)
  10580. (concat " (default " cbnex ")"))) ": "))
  10581. pa answ parent-target child parent old-hist)
  10582. (setq old-hist org-refile-history)
  10583. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10584. nil 'org-refile-history (or cdef (car org-refile-history))))
  10585. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10586. (org-refile-check-position pa)
  10587. (if pa
  10588. (progn
  10589. (when (or (not org-refile-history)
  10590. (not (eq old-hist org-refile-history))
  10591. (not (equal (car pa) (car org-refile-history))))
  10592. (setq org-refile-history
  10593. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10594. org-refile-history
  10595. (cdr org-refile-history))))
  10596. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10597. (pop org-refile-history)))
  10598. pa)
  10599. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10600. (progn
  10601. (setq parent (match-string 1 answ)
  10602. child (match-string 2 answ))
  10603. (setq parent-target (or (assoc parent tbl)
  10604. (assoc (concat parent "/") tbl)))
  10605. (when (and parent-target
  10606. (or (eq new-nodes t)
  10607. (and (eq new-nodes 'confirm)
  10608. (y-or-n-p (format "Create new node \"%s\"? "
  10609. child)))))
  10610. (org-refile-new-child parent-target child)))
  10611. (user-error "Invalid target location")))))
  10612. (declare-function org-string-nw-p "org-macs" (s))
  10613. (defun org-refile-check-position (refile-pointer)
  10614. "Check if the refile pointer matches the headline to which it points."
  10615. (let* ((file (nth 1 refile-pointer))
  10616. (re (nth 2 refile-pointer))
  10617. (pos (nth 3 refile-pointer))
  10618. buffer)
  10619. (if (and (not (markerp pos)) (not file))
  10620. (user-error "Please save the buffer to a file before refiling")
  10621. (when (org-string-nw-p re)
  10622. (setq buffer (if (markerp pos)
  10623. (marker-buffer pos)
  10624. (or (find-buffer-visiting file)
  10625. (find-file-noselect file))))
  10626. (with-current-buffer buffer
  10627. (save-excursion
  10628. (save-restriction
  10629. (widen)
  10630. (goto-char pos)
  10631. (beginning-of-line 1)
  10632. (unless (org-looking-at-p re)
  10633. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10634. (defun org-refile-new-child (parent-target child)
  10635. "Use refile target PARENT-TARGET to add new CHILD below it."
  10636. (unless parent-target
  10637. (error "Cannot find parent for new node"))
  10638. (let ((file (nth 1 parent-target))
  10639. (pos (nth 3 parent-target))
  10640. level)
  10641. (with-current-buffer (or (find-buffer-visiting file)
  10642. (find-file-noselect file))
  10643. (save-excursion
  10644. (save-restriction
  10645. (widen)
  10646. (if pos
  10647. (goto-char pos)
  10648. (goto-char (point-max))
  10649. (if (not (bolp)) (newline)))
  10650. (when (looking-at org-outline-regexp)
  10651. (setq level (funcall outline-level))
  10652. (org-end-of-subtree t t))
  10653. (org-back-over-empty-lines)
  10654. (insert "\n" (make-string
  10655. (if pos (org-get-valid-level level 1) 1) ?*)
  10656. " " child "\n")
  10657. (beginning-of-line 0)
  10658. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10659. (defun org-olpath-completing-read (prompt collection &rest args)
  10660. "Read an outline path like a file name."
  10661. (let ((thetable collection)
  10662. (org-completion-use-ido nil) ; does not work with ido.
  10663. (org-completion-use-iswitchb nil)) ; or iswitchb
  10664. (apply
  10665. 'org-icompleting-read prompt
  10666. (lambda (string predicate &optional flag)
  10667. (let (rtn r f (l (length string)))
  10668. (cond
  10669. ((eq flag nil)
  10670. ;; try completion
  10671. (try-completion string thetable))
  10672. ((eq flag t)
  10673. ;; all-completions
  10674. (setq rtn (all-completions string thetable predicate))
  10675. (mapcar
  10676. (lambda (x)
  10677. (setq r (substring x l))
  10678. (if (string-match " ([^)]*)$" x)
  10679. (setq f (match-string 0 x))
  10680. (setq f ""))
  10681. (if (string-match "/" r)
  10682. (concat string (substring r 0 (match-end 0)) f)
  10683. x))
  10684. rtn))
  10685. ((eq flag 'lambda)
  10686. ;; exact match?
  10687. (assoc string thetable)))))
  10688. args)))
  10689. ;;;; Dynamic blocks
  10690. (defun org-find-dblock (name)
  10691. "Find the first dynamic block with name NAME in the buffer.
  10692. If not found, stay at current position and return nil."
  10693. (let ((case-fold-search t) pos)
  10694. (save-excursion
  10695. (goto-char (point-min))
  10696. (setq pos (and (re-search-forward
  10697. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10698. (match-beginning 0))))
  10699. (if pos (goto-char pos))
  10700. pos))
  10701. (defconst org-dblock-start-re
  10702. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10703. "Matches the start line of a dynamic block, with parameters.")
  10704. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10705. "Matches the end of a dynamic block.")
  10706. (defun org-create-dblock (plist)
  10707. "Create a dynamic block section, with parameters taken from PLIST.
  10708. PLIST must contain a :name entry which is used as name of the block."
  10709. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10710. (end-of-line 1)
  10711. (newline))
  10712. (let ((col (current-column))
  10713. (name (plist-get plist :name)))
  10714. (insert "#+BEGIN: " name)
  10715. (while plist
  10716. (if (eq (car plist) :name)
  10717. (setq plist (cddr plist))
  10718. (insert " " (prin1-to-string (pop plist)))))
  10719. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10720. (beginning-of-line -2)))
  10721. (defun org-prepare-dblock ()
  10722. "Prepare dynamic block for refresh.
  10723. This empties the block, puts the cursor at the insert position and returns
  10724. the property list including an extra property :name with the block name."
  10725. (unless (looking-at org-dblock-start-re)
  10726. (user-error "Not at a dynamic block"))
  10727. (let* ((begdel (1+ (match-end 0)))
  10728. (name (org-no-properties (match-string 1)))
  10729. (params (append (list :name name)
  10730. (read (concat "(" (match-string 3) ")")))))
  10731. (save-excursion
  10732. (beginning-of-line 1)
  10733. (skip-chars-forward " \t")
  10734. (setq params (plist-put params :indentation-column (current-column))))
  10735. (unless (re-search-forward org-dblock-end-re nil t)
  10736. (error "Dynamic block not terminated"))
  10737. (setq params
  10738. (append params
  10739. (list :content (buffer-substring
  10740. begdel (match-beginning 0)))))
  10741. (delete-region begdel (match-beginning 0))
  10742. (goto-char begdel)
  10743. (open-line 1)
  10744. params))
  10745. (defun org-map-dblocks (&optional command)
  10746. "Apply COMMAND to all dynamic blocks in the current buffer.
  10747. If COMMAND is not given, use `org-update-dblock'."
  10748. (let ((cmd (or command 'org-update-dblock)))
  10749. (save-excursion
  10750. (goto-char (point-min))
  10751. (while (re-search-forward org-dblock-start-re nil t)
  10752. (goto-char (match-beginning 0))
  10753. (save-excursion
  10754. (condition-case nil
  10755. (funcall cmd)
  10756. (error (message "Error during update of dynamic block"))))
  10757. (unless (re-search-forward org-dblock-end-re nil t)
  10758. (error "Dynamic block not terminated"))))))
  10759. (defun org-dblock-update (&optional arg)
  10760. "User command for updating dynamic blocks.
  10761. Update the dynamic block at point. With prefix ARG, update all dynamic
  10762. blocks in the buffer."
  10763. (interactive "P")
  10764. (if arg
  10765. (org-update-all-dblocks)
  10766. (or (looking-at org-dblock-start-re)
  10767. (org-beginning-of-dblock))
  10768. (org-update-dblock)))
  10769. (defun org-update-dblock ()
  10770. "Update the dynamic block at point.
  10771. This means to empty the block, parse for parameters and then call
  10772. the correct writing function."
  10773. (interactive)
  10774. (save-window-excursion
  10775. (let* ((pos (point))
  10776. (line (org-current-line))
  10777. (params (org-prepare-dblock))
  10778. (name (plist-get params :name))
  10779. (indent (plist-get params :indentation-column))
  10780. (cmd (intern (concat "org-dblock-write:" name))))
  10781. (message "Updating dynamic block `%s' at line %d..." name line)
  10782. (funcall cmd params)
  10783. (message "Updating dynamic block `%s' at line %d...done" name line)
  10784. (goto-char pos)
  10785. (when (and indent (> indent 0))
  10786. (setq indent (make-string indent ?\ ))
  10787. (save-excursion
  10788. (org-beginning-of-dblock)
  10789. (forward-line 1)
  10790. (while (not (looking-at org-dblock-end-re))
  10791. (insert indent)
  10792. (beginning-of-line 2))
  10793. (when (looking-at org-dblock-end-re)
  10794. (and (looking-at "[ \t]+")
  10795. (replace-match ""))
  10796. (insert indent)))))))
  10797. (defun org-beginning-of-dblock ()
  10798. "Find the beginning of the dynamic block at point.
  10799. Error if there is no such block at point."
  10800. (let ((pos (point))
  10801. beg)
  10802. (end-of-line 1)
  10803. (if (and (re-search-backward org-dblock-start-re nil t)
  10804. (setq beg (match-beginning 0))
  10805. (re-search-forward org-dblock-end-re nil t)
  10806. (> (match-end 0) pos))
  10807. (goto-char beg)
  10808. (goto-char pos)
  10809. (error "Not in a dynamic block"))))
  10810. (defun org-update-all-dblocks ()
  10811. "Update all dynamic blocks in the buffer.
  10812. This function can be used in a hook."
  10813. (interactive)
  10814. (when (derived-mode-p 'org-mode)
  10815. (org-map-dblocks 'org-update-dblock)))
  10816. ;;;; Completion
  10817. (declare-function org-export-backend-name "org-export" (cl-x))
  10818. (declare-function org-export-backend-options "org-export" (cl-x))
  10819. (defun org-get-export-keywords ()
  10820. "Return a list of all currently understood export keywords.
  10821. Export keywords include options, block names, attributes and
  10822. keywords relative to each registered export back-end."
  10823. (let (keywords)
  10824. (dolist (backend
  10825. (org-bound-and-true-p org-export--registered-backends)
  10826. (delq nil keywords))
  10827. ;; Back-end name (for keywords, like #+LATEX:)
  10828. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10829. (dolist (option-entry (org-export-backend-options backend))
  10830. ;; Back-end options.
  10831. (push (nth 1 option-entry) keywords)))))
  10832. (defconst org-options-keywords
  10833. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10834. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10835. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10836. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10837. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10838. (defcustom org-structure-template-alist
  10839. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10840. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10841. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10842. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10843. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10844. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10845. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10846. "<literal style=\"latex\">\n?\n</literal>")
  10847. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10848. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10849. "<literal style=\"html\">\n?\n</literal>")
  10850. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10851. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10852. ("A" "#+ASCII: " "")
  10853. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10854. ("I" "#+INCLUDE: %file ?"
  10855. "<include file=%file markup=\"?\">"))
  10856. "Structure completion elements.
  10857. This is a list of abbreviation keys and values. The value gets inserted
  10858. if you type `<' followed by the key and then press the completion key,
  10859. usually `M-TAB'. %file will be replaced by a file name after prompting
  10860. for the file using completion. The cursor will be placed at the position
  10861. of the `?` in the template.
  10862. There are two templates for each key, the first uses the original Org syntax,
  10863. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10864. the default when the /org-mtags.el/ module has been loaded. See also the
  10865. variable `org-mtags-prefer-muse-templates'."
  10866. :group 'org-completion
  10867. :type '(repeat
  10868. (list
  10869. (string :tag "Key")
  10870. (string :tag "Template")
  10871. (string :tag "Muse Template"))))
  10872. (defun org-try-structure-completion ()
  10873. "Try to complete a structure template before point.
  10874. This looks for strings like \"<e\" on an otherwise empty line and
  10875. expands them."
  10876. (let ((l (buffer-substring (point-at-bol) (point)))
  10877. a)
  10878. (when (and (looking-at "[ \t]*$")
  10879. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10880. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10881. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10882. (match-beginning 1)) a)
  10883. t)))
  10884. (defun org-complete-expand-structure-template (start cell)
  10885. "Expand a structure template."
  10886. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10887. (rpl (nth (if musep 2 1) cell))
  10888. (ind ""))
  10889. (delete-region start (point))
  10890. (when (string-match "\\`#\\+" rpl)
  10891. (cond
  10892. ((bolp))
  10893. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10894. (setq ind (buffer-substring (point-at-bol) (point))))
  10895. (t (newline))))
  10896. (setq start (point))
  10897. (if (string-match "%file" rpl)
  10898. (setq rpl (replace-match
  10899. (concat
  10900. "\""
  10901. (save-match-data
  10902. (abbreviate-file-name (read-file-name "Include file: ")))
  10903. "\"")
  10904. t t rpl)))
  10905. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10906. (concat "\n" ind)))
  10907. (insert rpl)
  10908. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10909. ;;;; TODO, DEADLINE, Comments
  10910. (defun org-toggle-comment ()
  10911. "Change the COMMENT state of an entry."
  10912. (interactive)
  10913. (save-excursion
  10914. (org-back-to-heading)
  10915. (let (case-fold-search)
  10916. (cond
  10917. ((looking-at (format org-heading-keyword-regexp-format
  10918. org-comment-string))
  10919. (goto-char (match-end 1))
  10920. (looking-at (concat " +" org-comment-string))
  10921. (replace-match "" t t)
  10922. (when (eolp) (insert " ")))
  10923. ((looking-at org-outline-regexp)
  10924. (goto-char (match-end 0))
  10925. (insert org-comment-string " "))))))
  10926. (defvar org-last-todo-state-is-todo nil
  10927. "This is non-nil when the last TODO state change led to a TODO state.
  10928. If the last change removed the TODO tag or switched to DONE, then
  10929. this is nil.")
  10930. (defvar org-setting-tags nil) ; dynamically skipped
  10931. (defvar org-todo-setup-filter-hook nil
  10932. "Hook for functions that pre-filter todo specs.
  10933. Each function takes a todo spec and returns either nil or the spec
  10934. transformed into canonical form." )
  10935. (defvar org-todo-get-default-hook nil
  10936. "Hook for functions that get a default item for todo.
  10937. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10938. nil or a string to be used for the todo mark." )
  10939. (defvar org-agenda-headline-snapshot-before-repeat)
  10940. (defun org-current-effective-time ()
  10941. "Return current time adjusted for `org-extend-today-until' variable."
  10942. (let* ((ct (org-current-time))
  10943. (dct (decode-time ct))
  10944. (ct1
  10945. (cond
  10946. (org-use-last-clock-out-time-as-effective-time
  10947. (or (org-clock-get-last-clock-out-time) ct))
  10948. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10949. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10950. (t ct))))
  10951. ct1))
  10952. (defun org-todo-yesterday (&optional arg)
  10953. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10954. (interactive "P")
  10955. (if (eq major-mode 'org-agenda-mode)
  10956. (apply 'org-agenda-todo-yesterday arg)
  10957. (let* ((hour (third (decode-time
  10958. (org-current-time))))
  10959. (org-extend-today-until (1+ hour)))
  10960. (org-todo arg))))
  10961. (defvar org-block-entry-blocking ""
  10962. "First entry preventing the TODO state change.")
  10963. (defun org-todo (&optional arg)
  10964. "Change the TODO state of an item.
  10965. The state of an item is given by a keyword at the start of the heading,
  10966. like
  10967. *** TODO Write paper
  10968. *** DONE Call mom
  10969. The different keywords are specified in the variable `org-todo-keywords'.
  10970. By default the available states are \"TODO\" and \"DONE\".
  10971. So for this example: when the item starts with TODO, it is changed to DONE.
  10972. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10973. DONE are present, add TODO at the beginning of the heading.
  10974. With \\[universal-argument] prefix arg, use completion to determine the new \
  10975. state.
  10976. With numeric prefix arg, switch to that state.
  10977. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10978. keywords (nextset).
  10979. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10980. With a numeric prefix arg of 0, inhibit note taking for the change.
  10981. For calling through lisp, arg is also interpreted in the following way:
  10982. 'none -> empty state
  10983. \"\"(empty string) -> switch to empty state
  10984. 'done -> switch to DONE
  10985. 'nextset -> switch to the next set of keywords
  10986. 'previousset -> switch to the previous set of keywords
  10987. \"WAITING\" -> switch to the specified keyword, but only if it
  10988. really is a member of `org-todo-keywords'."
  10989. (interactive "P")
  10990. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10991. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10992. 'region-start-level 'region))
  10993. org-loop-over-headlines-in-active-region)
  10994. (org-map-entries
  10995. `(org-todo ,arg)
  10996. org-loop-over-headlines-in-active-region
  10997. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10998. (if (equal arg '(16)) (setq arg 'nextset))
  10999. (let ((org-blocker-hook org-blocker-hook)
  11000. commentp
  11001. case-fold-search)
  11002. (when (equal arg '(64))
  11003. (setq arg nil org-blocker-hook nil))
  11004. (when (and org-blocker-hook
  11005. (or org-inhibit-blocking
  11006. (org-entry-get nil "NOBLOCKING")))
  11007. (setq org-blocker-hook nil))
  11008. (save-excursion
  11009. (catch 'exit
  11010. (org-back-to-heading t)
  11011. (when (looking-at (concat "^\\*+ " org-comment-string))
  11012. (org-toggle-comment)
  11013. (setq commentp t))
  11014. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11015. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11016. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11017. (let* ((match-data (match-data))
  11018. (startpos (point-at-bol))
  11019. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11020. (org-log-done org-log-done)
  11021. (org-log-repeat org-log-repeat)
  11022. (org-todo-log-states org-todo-log-states)
  11023. (org-inhibit-logging
  11024. (if (equal arg 0)
  11025. (progn (setq arg nil) 'note) org-inhibit-logging))
  11026. (this (match-string 1))
  11027. (hl-pos (match-beginning 0))
  11028. (head (org-get-todo-sequence-head this))
  11029. (ass (assoc head org-todo-kwd-alist))
  11030. (interpret (nth 1 ass))
  11031. (done-word (nth 3 ass))
  11032. (final-done-word (nth 4 ass))
  11033. (org-last-state (or this ""))
  11034. (completion-ignore-case t)
  11035. (member (member this org-todo-keywords-1))
  11036. (tail (cdr member))
  11037. (org-state (cond
  11038. ((and org-todo-key-trigger
  11039. (or (and (equal arg '(4))
  11040. (eq org-use-fast-todo-selection 'prefix))
  11041. (and (not arg) org-use-fast-todo-selection
  11042. (not (eq org-use-fast-todo-selection
  11043. 'prefix)))))
  11044. ;; Use fast selection
  11045. (org-fast-todo-selection))
  11046. ((and (equal arg '(4))
  11047. (or (not org-use-fast-todo-selection)
  11048. (not org-todo-key-trigger)))
  11049. ;; Read a state with completion
  11050. (org-icompleting-read
  11051. "State: " (mapcar 'list org-todo-keywords-1)
  11052. nil t))
  11053. ((eq arg 'right)
  11054. (if this
  11055. (if tail (car tail) nil)
  11056. (car org-todo-keywords-1)))
  11057. ((eq arg 'left)
  11058. (if (equal member org-todo-keywords-1)
  11059. nil
  11060. (if this
  11061. (nth (- (length org-todo-keywords-1)
  11062. (length tail) 2)
  11063. org-todo-keywords-1)
  11064. (org-last org-todo-keywords-1))))
  11065. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11066. (setq arg nil))) ; hack to fall back to cycling
  11067. (arg
  11068. ;; user or caller requests a specific state
  11069. (cond
  11070. ((equal arg "") nil)
  11071. ((eq arg 'none) nil)
  11072. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11073. ((eq arg 'nextset)
  11074. (or (car (cdr (member head org-todo-heads)))
  11075. (car org-todo-heads)))
  11076. ((eq arg 'previousset)
  11077. (let ((org-todo-heads (reverse org-todo-heads)))
  11078. (or (car (cdr (member head org-todo-heads)))
  11079. (car org-todo-heads))))
  11080. ((car (member arg org-todo-keywords-1)))
  11081. ((stringp arg)
  11082. (user-error "State `%s' not valid in this file" arg))
  11083. ((nth (1- (prefix-numeric-value arg))
  11084. org-todo-keywords-1))))
  11085. ((null member) (or head (car org-todo-keywords-1)))
  11086. ((equal this final-done-word) nil) ;; -> make empty
  11087. ((null tail) nil) ;; -> first entry
  11088. ((memq interpret '(type priority))
  11089. (if (eq this-command last-command)
  11090. (car tail)
  11091. (if (> (length tail) 0)
  11092. (or done-word (car org-done-keywords))
  11093. nil)))
  11094. (t
  11095. (car tail))))
  11096. (org-state (or
  11097. (run-hook-with-args-until-success
  11098. 'org-todo-get-default-hook org-state org-last-state)
  11099. org-state))
  11100. (next (if org-state (concat " " org-state " ") " "))
  11101. (change-plist (list :type 'todo-state-change :from this :to org-state
  11102. :position startpos))
  11103. dolog now-done-p)
  11104. (when org-blocker-hook
  11105. (setq org-last-todo-state-is-todo
  11106. (not (member this org-done-keywords)))
  11107. (unless (save-excursion
  11108. (save-match-data
  11109. (org-with-wide-buffer
  11110. (run-hook-with-args-until-failure
  11111. 'org-blocker-hook change-plist))))
  11112. (if (org-called-interactively-p 'interactive)
  11113. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11114. this org-state org-block-entry-blocking)
  11115. ;; fail silently
  11116. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11117. this org-state org-block-entry-blocking)
  11118. (throw 'exit nil))))
  11119. (store-match-data match-data)
  11120. (replace-match next t t)
  11121. (unless (pos-visible-in-window-p hl-pos)
  11122. (message "TODO state changed to %s" (org-trim next)))
  11123. (unless head
  11124. (setq head (org-get-todo-sequence-head org-state)
  11125. ass (assoc head org-todo-kwd-alist)
  11126. interpret (nth 1 ass)
  11127. done-word (nth 3 ass)
  11128. final-done-word (nth 4 ass)))
  11129. (when (memq arg '(nextset previousset))
  11130. (message "Keyword-Set %d/%d: %s"
  11131. (- (length org-todo-sets) -1
  11132. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11133. (length org-todo-sets)
  11134. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11135. (setq org-last-todo-state-is-todo
  11136. (not (member org-state org-done-keywords)))
  11137. (setq now-done-p (and (member org-state org-done-keywords)
  11138. (not (member this org-done-keywords))))
  11139. (and logging (org-local-logging logging))
  11140. (when (and (or org-todo-log-states org-log-done)
  11141. (not (eq org-inhibit-logging t))
  11142. (not (memq arg '(nextset previousset))))
  11143. ;; we need to look at recording a time and note
  11144. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11145. (nth 2 (assoc this org-todo-log-states))))
  11146. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11147. (setq dolog 'time))
  11148. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11149. (and org-state
  11150. (member org-state org-not-done-keywords)
  11151. (not (member this org-not-done-keywords))))
  11152. ;; This is now a todo state and was not one before
  11153. ;; If there was a CLOSED time stamp, get rid of it.
  11154. (org-add-planning-info nil nil 'closed))
  11155. (when (and now-done-p org-log-done)
  11156. ;; It is now done, and it was not done before
  11157. (org-add-planning-info 'closed (org-current-effective-time))
  11158. (if (and (not dolog) (eq 'note org-log-done))
  11159. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11160. (when (and org-state dolog)
  11161. ;; This is a non-nil state, and we need to log it
  11162. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11163. ;; Fixup tag positioning
  11164. (org-todo-trigger-tag-changes org-state)
  11165. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11166. (when org-provide-todo-statistics
  11167. (org-update-parent-todo-statistics))
  11168. (run-hooks 'org-after-todo-state-change-hook)
  11169. (if (and arg (not (member org-state org-done-keywords)))
  11170. (setq head (org-get-todo-sequence-head org-state)))
  11171. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11172. ;; Do we need to trigger a repeat?
  11173. (when now-done-p
  11174. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11175. ;; This is for the agenda, take a snapshot of the headline.
  11176. (save-match-data
  11177. (setq org-agenda-headline-snapshot-before-repeat
  11178. (org-get-heading))))
  11179. (org-auto-repeat-maybe org-state))
  11180. ;; Fixup cursor location if close to the keyword
  11181. (if (and (outline-on-heading-p)
  11182. (not (bolp))
  11183. (save-excursion (beginning-of-line 1)
  11184. (looking-at org-todo-line-regexp))
  11185. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11186. (progn
  11187. (goto-char (or (match-end 2) (match-end 1)))
  11188. (and (looking-at " ") (just-one-space))))
  11189. (when org-trigger-hook
  11190. (save-excursion
  11191. (run-hook-with-args 'org-trigger-hook change-plist)))
  11192. (when commentp (org-toggle-comment))))))))
  11193. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11194. "Block turning an entry into a TODO, using the hierarchy.
  11195. This checks whether the current task should be blocked from state
  11196. changes. Such blocking occurs when:
  11197. 1. The task has children which are not all in a completed state.
  11198. 2. A task has a parent with the property :ORDERED:, and there
  11199. are siblings prior to the current task with incomplete
  11200. status.
  11201. 3. The parent of the task is blocked because it has siblings that should
  11202. be done first, or is child of a block grandparent TODO entry."
  11203. (if (not org-enforce-todo-dependencies)
  11204. t ; if locally turned off don't block
  11205. (catch 'dont-block
  11206. ;; If this is not a todo state change, or if this entry is already DONE,
  11207. ;; do not block
  11208. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11209. (member (plist-get change-plist :from)
  11210. (cons 'done org-done-keywords))
  11211. (member (plist-get change-plist :to)
  11212. (cons 'todo org-not-done-keywords))
  11213. (not (plist-get change-plist :to)))
  11214. (throw 'dont-block t))
  11215. ;; If this task has children, and any are undone, it's blocked
  11216. (save-excursion
  11217. (org-back-to-heading t)
  11218. (let ((this-level (funcall outline-level)))
  11219. (outline-next-heading)
  11220. (let ((child-level (funcall outline-level)))
  11221. (while (and (not (eobp))
  11222. (> child-level this-level))
  11223. ;; this todo has children, check whether they are all
  11224. ;; completed
  11225. (if (and (not (org-entry-is-done-p))
  11226. (org-entry-is-todo-p))
  11227. (progn (setq org-block-entry-blocking (org-get-heading))
  11228. (throw 'dont-block nil)))
  11229. (outline-next-heading)
  11230. (setq child-level (funcall outline-level))))))
  11231. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11232. ;; any previous siblings are undone, it's blocked
  11233. (save-excursion
  11234. (org-back-to-heading t)
  11235. (let* ((pos (point))
  11236. (parent-pos (and (org-up-heading-safe) (point))))
  11237. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11238. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11239. (forward-line 1)
  11240. (re-search-forward org-not-done-heading-regexp pos t))
  11241. (setq org-block-entry-blocking (match-string 0))
  11242. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11243. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11244. (while t
  11245. (goto-char parent-pos)
  11246. (if (not (looking-at org-not-done-heading-regexp))
  11247. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11248. (setq pos (point))
  11249. (setq parent-pos (and (org-up-heading-safe) (point)))
  11250. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11251. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11252. (forward-line 1)
  11253. (re-search-forward org-not-done-heading-regexp pos t)
  11254. (setq org-block-entry-blocking (org-get-heading)))
  11255. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11256. (defcustom org-track-ordered-property-with-tag nil
  11257. "Should the ORDERED property also be shown as a tag?
  11258. The ORDERED property decides if an entry should require subtasks to be
  11259. completed in sequence. Since a property is not very visible, setting
  11260. this option means that toggling the ORDERED property with the command
  11261. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11262. not relevant for the behavior, but it makes things more visible.
  11263. Note that toggling the tag with tags commands will not change the property
  11264. and therefore not influence behavior!
  11265. This can be t, meaning the tag ORDERED should be used, It can also be a
  11266. string to select a different tag for this task."
  11267. :group 'org-todo
  11268. :type '(choice
  11269. (const :tag "No tracking" nil)
  11270. (const :tag "Track with ORDERED tag" t)
  11271. (string :tag "Use other tag")))
  11272. (defun org-toggle-ordered-property ()
  11273. "Toggle the ORDERED property of the current entry.
  11274. For better visibility, you can track the value of this property with a tag.
  11275. See variable `org-track-ordered-property-with-tag'."
  11276. (interactive)
  11277. (let* ((t1 org-track-ordered-property-with-tag)
  11278. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11279. (save-excursion
  11280. (org-back-to-heading)
  11281. (if (org-entry-get nil "ORDERED")
  11282. (progn
  11283. (org-delete-property "ORDERED" "PROPERTIES")
  11284. (and tag (org-toggle-tag tag 'off))
  11285. (message "Subtasks can be completed in arbitrary order"))
  11286. (org-entry-put nil "ORDERED" "t")
  11287. (and tag (org-toggle-tag tag 'on))
  11288. (message "Subtasks must be completed in sequence")))))
  11289. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11290. (defun org-block-todo-from-checkboxes (change-plist)
  11291. "Block turning an entry into a TODO, using checkboxes.
  11292. This checks whether the current task should be blocked from state
  11293. changes because there are unchecked boxes in this entry."
  11294. (if (not org-enforce-todo-checkbox-dependencies)
  11295. t ; if locally turned off don't block
  11296. (catch 'dont-block
  11297. ;; If this is not a todo state change, or if this entry is already DONE,
  11298. ;; do not block
  11299. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11300. (member (plist-get change-plist :from)
  11301. (cons 'done org-done-keywords))
  11302. (member (plist-get change-plist :to)
  11303. (cons 'todo org-not-done-keywords))
  11304. (not (plist-get change-plist :to)))
  11305. (throw 'dont-block t))
  11306. ;; If this task has checkboxes that are not checked, it's blocked
  11307. (save-excursion
  11308. (org-back-to-heading t)
  11309. (let ((beg (point)) end)
  11310. (outline-next-heading)
  11311. (setq end (point))
  11312. (goto-char beg)
  11313. (if (org-list-search-forward
  11314. (concat (org-item-beginning-re)
  11315. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11316. "\\[[- ]\\]")
  11317. end t)
  11318. (progn
  11319. (if (boundp 'org-blocked-by-checkboxes)
  11320. (setq org-blocked-by-checkboxes t))
  11321. (throw 'dont-block nil)))))
  11322. t))) ; do not block
  11323. (defun org-entry-blocked-p ()
  11324. "Is the current entry blocked?"
  11325. (org-with-silent-modifications
  11326. (if (org-entry-get nil "NOBLOCKING")
  11327. nil ;; Never block this entry
  11328. (not (run-hook-with-args-until-failure
  11329. 'org-blocker-hook
  11330. (list :type 'todo-state-change
  11331. :position (point)
  11332. :from 'todo
  11333. :to 'done))))))
  11334. (defun org-update-statistics-cookies (all)
  11335. "Update the statistics cookie, either from TODO or from checkboxes.
  11336. This should be called with the cursor in a line with a statistics cookie."
  11337. (interactive "P")
  11338. (if all
  11339. (progn
  11340. (org-update-checkbox-count 'all)
  11341. (org-map-entries 'org-update-parent-todo-statistics))
  11342. (if (not (org-at-heading-p))
  11343. (org-update-checkbox-count)
  11344. (let ((pos (point-marker))
  11345. end l1 l2)
  11346. (ignore-errors (org-back-to-heading t))
  11347. (if (not (org-at-heading-p))
  11348. (org-update-checkbox-count)
  11349. (setq l1 (org-outline-level))
  11350. (setq end (save-excursion
  11351. (outline-next-heading)
  11352. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11353. (point)))
  11354. (if (and (save-excursion
  11355. (re-search-forward
  11356. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11357. (not (save-excursion (re-search-forward
  11358. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11359. (org-update-checkbox-count)
  11360. (if (and l2 (> l2 l1))
  11361. (progn
  11362. (goto-char end)
  11363. (org-update-parent-todo-statistics))
  11364. (goto-char pos)
  11365. (beginning-of-line 1)
  11366. (while (re-search-forward
  11367. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11368. (point-at-eol) t)
  11369. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11370. (goto-char pos)
  11371. (move-marker pos nil)))))
  11372. (defvar org-entry-property-inherited-from) ;; defined below
  11373. (defun org-update-parent-todo-statistics ()
  11374. "Update any statistics cookie in the parent of the current headline.
  11375. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11376. the entire subtree and this will travel up the hierarchy and update
  11377. statistics everywhere."
  11378. (let* ((prop (save-excursion (org-up-heading-safe)
  11379. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11380. (recursive (or (not org-hierarchical-todo-statistics)
  11381. (and prop (string-match "\\<recursive\\>" prop))))
  11382. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11383. 0))
  11384. (first t)
  11385. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11386. level ltoggle l1 new ndel
  11387. (cnt-all 0) (cnt-done 0) is-percent kwd
  11388. checkbox-beg ov ovs ove cookie-present)
  11389. (catch 'exit
  11390. (save-excursion
  11391. (beginning-of-line 1)
  11392. (setq ltoggle (funcall outline-level))
  11393. ;; Three situations are to consider:
  11394. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11395. ;; to the top-level ancestor on the headline;
  11396. ;; 2. If parent has "recursive" property, repeat up to the
  11397. ;; headline setting that property, taking inheritance into
  11398. ;; account;
  11399. ;; 3. Else, move up to direct parent and proceed only once.
  11400. (while (and (setq level (org-up-heading-safe))
  11401. (or recursive first)
  11402. (>= (point) lim))
  11403. (setq first nil cookie-present nil)
  11404. (unless (and level
  11405. (not (string-match
  11406. "\\<checkbox\\>"
  11407. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11408. "")))))
  11409. (throw 'exit nil))
  11410. (while (re-search-forward box-re (point-at-eol) t)
  11411. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11412. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11413. (save-match-data
  11414. (unless (outline-next-heading) (throw 'exit nil))
  11415. (while (and (looking-at org-complex-heading-regexp)
  11416. (> (setq l1 (length (match-string 1))) level))
  11417. (setq kwd (and (or recursive (= l1 ltoggle))
  11418. (match-string 2)))
  11419. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11420. (and (listp org-provide-todo-statistics)
  11421. (or (member kwd org-provide-todo-statistics)
  11422. (member kwd org-done-keywords))))
  11423. (setq cnt-all (1+ cnt-all))
  11424. (if (eq org-provide-todo-statistics t)
  11425. (and kwd (setq cnt-all (1+ cnt-all)))))
  11426. (and (member kwd org-done-keywords)
  11427. (setq cnt-done (1+ cnt-done)))
  11428. (outline-next-heading)))
  11429. (setq new
  11430. (if is-percent
  11431. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11432. (format "[%d/%d]" cnt-done cnt-all))
  11433. ndel (- (match-end 0) checkbox-beg))
  11434. ;; handle overlays when updating cookie from column view
  11435. (when (setq ov (car (overlays-at checkbox-beg)))
  11436. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11437. (delete-overlay ov))
  11438. (goto-char checkbox-beg)
  11439. (insert new)
  11440. (delete-region (point) (+ (point) ndel))
  11441. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11442. (when ov (move-overlay ov ovs ove)))
  11443. (when cookie-present
  11444. (run-hook-with-args 'org-after-todo-statistics-hook
  11445. cnt-done (- cnt-all cnt-done))))))
  11446. (run-hooks 'org-todo-statistics-hook)))
  11447. (defvar org-after-todo-statistics-hook nil
  11448. "Hook that is called after a TODO statistics cookie has been updated.
  11449. Each function is called with two arguments: the number of not-done entries
  11450. and the number of done entries.
  11451. For example, the following function, when added to this hook, will switch
  11452. an entry to DONE when all children are done, and back to TODO when new
  11453. entries are set to a TODO status. Note that this hook is only called
  11454. when there is a statistics cookie in the headline!
  11455. (defun org-summary-todo (n-done n-not-done)
  11456. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11457. (let (org-log-done org-log-states) ; turn off logging
  11458. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11459. ")
  11460. (defvar org-todo-statistics-hook nil
  11461. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11462. This hook runs even if there is no statistics cookie present, in which case
  11463. `org-after-todo-statistics-hook' would not run.")
  11464. (defun org-todo-trigger-tag-changes (state)
  11465. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11466. (let ((l org-todo-state-tags-triggers)
  11467. changes)
  11468. (when (or (not state) (equal state ""))
  11469. (setq changes (append changes (cdr (assoc "" l)))))
  11470. (when (and (stringp state) (> (length state) 0))
  11471. (setq changes (append changes (cdr (assoc state l)))))
  11472. (when (member state org-not-done-keywords)
  11473. (setq changes (append changes (cdr (assoc 'todo l)))))
  11474. (when (member state org-done-keywords)
  11475. (setq changes (append changes (cdr (assoc 'done l)))))
  11476. (dolist (c changes)
  11477. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11478. (defun org-local-logging (value)
  11479. "Get logging settings from a property VALUE."
  11480. (let* (words w a)
  11481. ;; directly set the variables, they are already local.
  11482. (setq org-log-done nil
  11483. org-log-repeat nil
  11484. org-todo-log-states nil)
  11485. (setq words (org-split-string value))
  11486. (while (setq w (pop words))
  11487. (cond
  11488. ((setq a (assoc w org-startup-options))
  11489. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11490. (set (nth 1 a) (nth 2 a))))
  11491. ((setq a (org-extract-log-state-settings w))
  11492. (and (member (car a) org-todo-keywords-1)
  11493. (push a org-todo-log-states)))))))
  11494. (defun org-get-todo-sequence-head (kwd)
  11495. "Return the head of the TODO sequence to which KWD belongs.
  11496. If KWD is not set, check if there is a text property remembering the
  11497. right sequence."
  11498. (let (p)
  11499. (cond
  11500. ((not kwd)
  11501. (or (get-text-property (point-at-bol) 'org-todo-head)
  11502. (progn
  11503. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11504. nil (point-at-eol)))
  11505. (get-text-property p 'org-todo-head))))
  11506. ((not (member kwd org-todo-keywords-1))
  11507. (car org-todo-keywords-1))
  11508. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11509. (defun org-fast-todo-selection ()
  11510. "Fast TODO keyword selection with single keys.
  11511. Returns the new TODO keyword, or nil if no state change should occur."
  11512. (let* ((fulltable org-todo-key-alist)
  11513. (done-keywords org-done-keywords) ;; needed for the faces.
  11514. (maxlen (apply 'max (mapcar
  11515. (lambda (x)
  11516. (if (stringp (car x)) (string-width (car x)) 0))
  11517. fulltable)))
  11518. (expert nil)
  11519. (fwidth (+ maxlen 3 1 3))
  11520. (ncol (/ (- (window-width) 4) fwidth))
  11521. tg cnt e c tbl
  11522. groups ingroup)
  11523. (save-excursion
  11524. (save-window-excursion
  11525. (if expert
  11526. (set-buffer (get-buffer-create " *Org todo*"))
  11527. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11528. (erase-buffer)
  11529. (org-set-local 'org-done-keywords done-keywords)
  11530. (setq tbl fulltable cnt 0)
  11531. (while (setq e (pop tbl))
  11532. (cond
  11533. ((equal e '(:startgroup))
  11534. (push '() groups) (setq ingroup t)
  11535. (when (not (= cnt 0))
  11536. (setq cnt 0)
  11537. (insert "\n"))
  11538. (insert "{ "))
  11539. ((equal e '(:endgroup))
  11540. (setq ingroup nil cnt 0)
  11541. (insert "}\n"))
  11542. ((equal e '(:newline))
  11543. (when (not (= cnt 0))
  11544. (setq cnt 0)
  11545. (insert "\n")
  11546. (setq e (car tbl))
  11547. (while (equal (car tbl) '(:newline))
  11548. (insert "\n")
  11549. (setq tbl (cdr tbl)))))
  11550. (t
  11551. (setq tg (car e) c (cdr e))
  11552. (if ingroup (push tg (car groups)))
  11553. (setq tg (org-add-props tg nil 'face
  11554. (org-get-todo-face tg)))
  11555. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11556. (insert "[" c "] " tg (make-string
  11557. (- fwidth 4 (length tg)) ?\ ))
  11558. (when (= (setq cnt (1+ cnt)) ncol)
  11559. (insert "\n")
  11560. (if ingroup (insert " "))
  11561. (setq cnt 0)))))
  11562. (insert "\n")
  11563. (goto-char (point-min))
  11564. (if (not expert) (org-fit-window-to-buffer))
  11565. (message "[a-z..]:Set [SPC]:clear")
  11566. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11567. (cond
  11568. ((or (= c ?\C-g)
  11569. (and (= c ?q) (not (rassoc c fulltable))))
  11570. (setq quit-flag t))
  11571. ((= c ?\ ) nil)
  11572. ((setq e (rassoc c fulltable) tg (car e))
  11573. tg)
  11574. (t (setq quit-flag t)))))))
  11575. (defun org-entry-is-todo-p ()
  11576. (member (org-get-todo-state) org-not-done-keywords))
  11577. (defun org-entry-is-done-p ()
  11578. (member (org-get-todo-state) org-done-keywords))
  11579. (defun org-get-todo-state ()
  11580. "Return the TODO keyword of the current subtree."
  11581. (save-excursion
  11582. (org-back-to-heading t)
  11583. (and (looking-at org-todo-line-regexp)
  11584. (match-end 2)
  11585. (match-string 2))))
  11586. (defun org-at-date-range-p (&optional inactive-ok)
  11587. "Is the cursor inside a date range?"
  11588. (interactive)
  11589. (save-excursion
  11590. (catch 'exit
  11591. (let ((pos (point)))
  11592. (skip-chars-backward "^[<\r\n")
  11593. (skip-chars-backward "<[")
  11594. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11595. (>= (match-end 0) pos)
  11596. (throw 'exit t))
  11597. (skip-chars-backward "^<[\r\n")
  11598. (skip-chars-backward "<[")
  11599. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11600. (>= (match-end 0) pos)
  11601. (throw 'exit t)))
  11602. nil)))
  11603. (defun org-get-repeat (&optional tagline)
  11604. "Check if there is a deadline/schedule with repeater in this entry."
  11605. (save-match-data
  11606. (save-excursion
  11607. (org-back-to-heading t)
  11608. (and (re-search-forward (if tagline
  11609. (concat tagline "\\s-*" org-repeat-re)
  11610. org-repeat-re)
  11611. (org-entry-end-position) t)
  11612. (match-string-no-properties 1)))))
  11613. (defvar org-last-changed-timestamp)
  11614. (defvar org-last-inserted-timestamp)
  11615. (defvar org-log-post-message)
  11616. (defvar org-log-note-purpose)
  11617. (defvar org-log-note-how)
  11618. (defvar org-log-note-extra)
  11619. (defun org-auto-repeat-maybe (done-word)
  11620. "Check if the current headline contains a repeated deadline/schedule.
  11621. If yes, set TODO state back to what it was and change the base date
  11622. of repeating deadline/scheduled time stamps to new date.
  11623. This function is run automatically after each state change to a DONE state."
  11624. ;; last-state is dynamically scoped into this function
  11625. (let* ((repeat (org-get-repeat))
  11626. (aa (assoc org-last-state org-todo-kwd-alist))
  11627. (interpret (nth 1 aa))
  11628. (head (nth 2 aa))
  11629. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11630. (msg "Entry repeats: ")
  11631. (org-log-done nil)
  11632. (org-todo-log-states nil)
  11633. re type n what ts time to-state)
  11634. (when repeat
  11635. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11636. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11637. org-todo-repeat-to-state))
  11638. (unless (and to-state (member to-state org-todo-keywords-1))
  11639. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11640. (org-todo to-state)
  11641. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11642. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11643. (org-time-stamp-format t t))))
  11644. (when org-log-repeat
  11645. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11646. (memq 'org-add-log-note post-command-hook))
  11647. ;; OK, we are already setup for some record
  11648. (if (eq org-log-repeat 'note)
  11649. ;; make sure we take a note, not only a time stamp
  11650. (setq org-log-note-how 'note))
  11651. ;; Set up for taking a record
  11652. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11653. org-last-state
  11654. 'findpos org-log-repeat)))
  11655. (org-back-to-heading t)
  11656. (org-add-planning-info nil nil 'closed)
  11657. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11658. org-deadline-time-regexp "\\)\\|\\("
  11659. org-ts-regexp "\\)"))
  11660. (while (re-search-forward
  11661. re (save-excursion (outline-next-heading) (point)) t)
  11662. (setq type (if (match-end 1) org-scheduled-string
  11663. (if (match-end 3) org-deadline-string "Plain:"))
  11664. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11665. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11666. (setq n (string-to-number (match-string 2 ts))
  11667. what (match-string 3 ts))
  11668. (if (equal what "w") (setq n (* n 7) what "d"))
  11669. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11670. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11671. ;; Preparation, see if we need to modify the start date for the change
  11672. (when (match-end 1)
  11673. (setq time (save-match-data (org-time-string-to-time ts)))
  11674. (cond
  11675. ((equal (match-string 1 ts) ".")
  11676. ;; Shift starting date to today
  11677. (org-timestamp-change
  11678. (- (org-today) (time-to-days time))
  11679. 'day))
  11680. ((equal (match-string 1 ts) "+")
  11681. (let ((nshiftmax 10) (nshift 0))
  11682. (while (or (= nshift 0)
  11683. (<= (time-to-days time)
  11684. (time-to-days (current-time))))
  11685. (when (= (incf nshift) nshiftmax)
  11686. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11687. (error "Abort")))
  11688. (org-timestamp-change n (cdr (assoc what whata)))
  11689. (org-at-timestamp-p t)
  11690. (setq ts (match-string 1))
  11691. (setq time (save-match-data (org-time-string-to-time ts)))))
  11692. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11693. ;; rematch, so that we have everything in place for the real shift
  11694. (org-at-timestamp-p t)
  11695. (setq ts (match-string 1))
  11696. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11697. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11698. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11699. (setq org-log-post-message msg)
  11700. (message "%s" msg))))
  11701. (defun org-show-todo-tree (arg)
  11702. "Make a compact tree which shows all headlines marked with TODO.
  11703. The tree will show the lines where the regexp matches, and all higher
  11704. headlines above the match.
  11705. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11706. With a numeric prefix N, construct a sparse tree for the Nth element
  11707. of `org-todo-keywords-1'."
  11708. (interactive "P")
  11709. (let ((case-fold-search nil)
  11710. (kwd-re
  11711. (cond ((null arg) org-not-done-regexp)
  11712. ((equal arg '(4))
  11713. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11714. (mapcar 'list org-todo-keywords-1))))
  11715. (concat "\\("
  11716. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11717. "\\)\\>")))
  11718. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11719. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11720. org-todo-keywords-1)))
  11721. (t (user-error "Invalid prefix argument: %s" arg)))))
  11722. (message "%d TODO entries found"
  11723. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11724. (defun org-deadline (arg &optional time)
  11725. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11726. With one universal prefix argument, remove any deadline from the item.
  11727. With two universal prefix arguments, prompt for a warning delay.
  11728. With argument TIME, set the deadline at the corresponding date. TIME
  11729. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11730. (interactive "P")
  11731. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11732. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11733. 'region-start-level 'region))
  11734. org-loop-over-headlines-in-active-region)
  11735. (org-map-entries
  11736. `(org-deadline ',arg ,time)
  11737. org-loop-over-headlines-in-active-region
  11738. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11739. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11740. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11741. (repeater (and old-date
  11742. (string-match
  11743. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11744. old-date)
  11745. (match-string 1 old-date))))
  11746. (cond
  11747. ((equal arg '(4))
  11748. (when (and old-date org-log-redeadline)
  11749. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11750. org-log-redeadline))
  11751. (org-remove-timestamp-with-keyword org-deadline-string)
  11752. (message "Item no longer has a deadline."))
  11753. ((equal arg '(16))
  11754. (save-excursion
  11755. (if (re-search-forward
  11756. org-deadline-time-regexp
  11757. (save-excursion (outline-next-heading) (point)) t)
  11758. (let* ((rpl0 (match-string 1))
  11759. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11760. (replace-match
  11761. (concat org-deadline-string
  11762. " <" rpl
  11763. (format " -%dd"
  11764. (abs
  11765. (- (time-to-days
  11766. (save-match-data
  11767. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11768. (time-to-days old-date-time))))
  11769. ">") t t))
  11770. (user-error "No deadline information to update"))))
  11771. (t
  11772. (org-add-planning-info 'deadline time 'closed)
  11773. (when (and old-date org-log-redeadline
  11774. (not (equal old-date
  11775. (substring org-last-inserted-timestamp 1 -1))))
  11776. (org-add-log-setup 'redeadline nil old-date 'findpos
  11777. org-log-redeadline))
  11778. (when repeater
  11779. (save-excursion
  11780. (org-back-to-heading t)
  11781. (when (re-search-forward (concat org-deadline-string " "
  11782. org-last-inserted-timestamp)
  11783. (save-excursion
  11784. (outline-next-heading) (point)) t)
  11785. (goto-char (1- (match-end 0)))
  11786. (insert " " repeater)
  11787. (setq org-last-inserted-timestamp
  11788. (concat (substring org-last-inserted-timestamp 0 -1)
  11789. " " repeater
  11790. (substring org-last-inserted-timestamp -1))))))
  11791. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11792. (defun org-schedule (arg &optional time)
  11793. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11794. With one universal prefix argument, remove any scheduling date from the item.
  11795. With two universal prefix arguments, prompt for a delay cookie.
  11796. With argument TIME, scheduled at the corresponding date. TIME can
  11797. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11798. (interactive "P")
  11799. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11800. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11801. 'region-start-level 'region))
  11802. org-loop-over-headlines-in-active-region)
  11803. (org-map-entries
  11804. `(org-schedule ',arg ,time)
  11805. org-loop-over-headlines-in-active-region
  11806. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11807. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11808. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11809. (repeater (and old-date
  11810. (string-match
  11811. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11812. old-date)
  11813. (match-string 1 old-date))))
  11814. (cond
  11815. ((equal arg '(4))
  11816. (progn
  11817. (when (and old-date org-log-reschedule)
  11818. (org-add-log-setup 'delschedule nil old-date 'findpos
  11819. org-log-reschedule))
  11820. (org-remove-timestamp-with-keyword org-scheduled-string)
  11821. (message "Item is no longer scheduled.")))
  11822. ((equal arg '(16))
  11823. (save-excursion
  11824. (if (re-search-forward
  11825. org-scheduled-time-regexp
  11826. (save-excursion (outline-next-heading) (point)) t)
  11827. (let* ((rpl0 (match-string 1))
  11828. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11829. (replace-match
  11830. (concat org-scheduled-string
  11831. " <" rpl
  11832. (format " -%dd"
  11833. (abs
  11834. (- (time-to-days
  11835. (save-match-data
  11836. (org-read-date nil t nil "Delay until" old-date-time)))
  11837. (time-to-days old-date-time))))
  11838. ">") t t))
  11839. (user-error "No scheduled information to update"))))
  11840. (t
  11841. (org-add-planning-info 'scheduled time 'closed)
  11842. (when (and old-date org-log-reschedule
  11843. (not (equal old-date
  11844. (substring org-last-inserted-timestamp 1 -1))))
  11845. (org-add-log-setup 'reschedule nil old-date 'findpos
  11846. org-log-reschedule))
  11847. (when repeater
  11848. (save-excursion
  11849. (org-back-to-heading t)
  11850. (when (re-search-forward (concat org-scheduled-string " "
  11851. org-last-inserted-timestamp)
  11852. (save-excursion
  11853. (outline-next-heading) (point)) t)
  11854. (goto-char (1- (match-end 0)))
  11855. (insert " " repeater)
  11856. (setq org-last-inserted-timestamp
  11857. (concat (substring org-last-inserted-timestamp 0 -1)
  11858. " " repeater
  11859. (substring org-last-inserted-timestamp -1))))))
  11860. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11861. (defun org-get-scheduled-time (pom &optional inherit)
  11862. "Get the scheduled time as a time tuple, of a format suitable
  11863. for calling org-schedule with, or if there is no scheduling,
  11864. returns nil."
  11865. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11866. (when time
  11867. (apply 'encode-time (org-parse-time-string time)))))
  11868. (defun org-get-deadline-time (pom &optional inherit)
  11869. "Get the deadline as a time tuple, of a format suitable for
  11870. calling org-deadline with, or if there is no scheduling, returns
  11871. nil."
  11872. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11873. (when time
  11874. (apply 'encode-time (org-parse-time-string time)))))
  11875. (defun org-remove-timestamp-with-keyword (keyword)
  11876. "Remove all time stamps with KEYWORD in the current entry."
  11877. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11878. beg)
  11879. (save-excursion
  11880. (org-back-to-heading t)
  11881. (setq beg (point))
  11882. (outline-next-heading)
  11883. (while (re-search-backward re beg t)
  11884. (replace-match "")
  11885. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11886. (equal (char-before) ?\ ))
  11887. (backward-delete-char 1)
  11888. (if (string-match "^[ \t]*$" (buffer-substring
  11889. (point-at-bol) (point-at-eol)))
  11890. (delete-region (point-at-bol)
  11891. (min (point-max) (1+ (point-at-eol))))))))))
  11892. (defun org-add-planning-info (what &optional time &rest remove)
  11893. "Insert new timestamp with keyword in the line directly after the headline.
  11894. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11895. If non is given, the user is prompted for a date.
  11896. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11897. be removed."
  11898. (interactive)
  11899. (let (org-time-was-given org-end-time-was-given ts
  11900. end default-time default-input)
  11901. (catch 'exit
  11902. (when (and (memq what '(scheduled deadline))
  11903. (or (not time)
  11904. (and (stringp time)
  11905. (string-match "^[-+]+[0-9]" time))))
  11906. ;; Try to get a default date/time from existing timestamp
  11907. (save-excursion
  11908. (org-back-to-heading t)
  11909. (setq end (save-excursion (outline-next-heading) (point)))
  11910. (when (re-search-forward (if (eq what 'scheduled)
  11911. org-scheduled-time-regexp
  11912. org-deadline-time-regexp)
  11913. end t)
  11914. (setq ts (match-string 1)
  11915. default-time
  11916. (apply 'encode-time (org-parse-time-string ts))
  11917. default-input (and ts (org-get-compact-tod ts))))))
  11918. (when what
  11919. (setq time
  11920. (if (stringp time)
  11921. ;; This is a string (relative or absolute), set proper date
  11922. (apply 'encode-time
  11923. (org-read-date-analyze
  11924. time default-time (decode-time default-time)))
  11925. ;; If necessary, get the time from the user
  11926. (or time (org-read-date nil 'to-time nil nil
  11927. default-time default-input)))))
  11928. (when (and org-insert-labeled-timestamps-at-point
  11929. (member what '(scheduled deadline)))
  11930. (insert
  11931. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11932. (org-insert-time-stamp time org-time-was-given
  11933. nil nil nil (list org-end-time-was-given))
  11934. (setq what nil))
  11935. (save-excursion
  11936. (save-restriction
  11937. (let (col list elt ts buffer-invisibility-spec)
  11938. (org-back-to-heading t)
  11939. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11940. (goto-char (match-end 1))
  11941. (setq col (current-column))
  11942. (goto-char (match-end 0))
  11943. (if (eobp) (insert "\n") (forward-char 1))
  11944. (when (and (not what)
  11945. (not (looking-at
  11946. (concat "[ \t]*"
  11947. org-keyword-time-not-clock-regexp))))
  11948. ;; Nothing to add, nothing to remove...... :-)
  11949. (throw 'exit nil))
  11950. (if (and (not (looking-at org-outline-regexp))
  11951. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11952. "[^\r\n]*"))
  11953. (not (equal (match-string 1) org-clock-string)))
  11954. (narrow-to-region (match-beginning 0) (match-end 0))
  11955. (insert-before-markers "\n")
  11956. (backward-char 1)
  11957. (narrow-to-region (point) (point))
  11958. (and org-adapt-indentation (org-indent-to-column col)))
  11959. ;; Check if we have to remove something.
  11960. (setq list (cons what remove))
  11961. (while list
  11962. (setq elt (pop list))
  11963. (when (or (and (eq elt 'scheduled)
  11964. (re-search-forward org-scheduled-time-regexp nil t))
  11965. (and (eq elt 'deadline)
  11966. (re-search-forward org-deadline-time-regexp nil t))
  11967. (and (eq elt 'closed)
  11968. (re-search-forward org-closed-time-regexp nil t)))
  11969. (replace-match "")
  11970. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11971. (and (looking-at "[ \t]+") (replace-match ""))
  11972. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11973. (when what
  11974. (insert
  11975. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11976. (cond ((eq what 'scheduled) org-scheduled-string)
  11977. ((eq what 'deadline) org-deadline-string)
  11978. ((eq what 'closed) org-closed-string))
  11979. " ")
  11980. (setq ts (org-insert-time-stamp
  11981. time
  11982. (or org-time-was-given
  11983. (and (eq what 'closed) org-log-done-with-time))
  11984. (eq what 'closed)
  11985. nil nil (list org-end-time-was-given)))
  11986. (insert
  11987. (if (not (or (bolp) (eq (char-before) ?\ )
  11988. (memq (char-after) '(32 10))
  11989. (eobp))) " " ""))
  11990. (end-of-line 1))
  11991. (goto-char (point-min))
  11992. (widen)
  11993. (if (and (looking-at "[ \t]*\n")
  11994. (equal (char-before) ?\n))
  11995. (delete-region (1- (point)) (point-at-eol)))
  11996. ts))))))
  11997. (defvar org-log-note-marker (make-marker))
  11998. (defvar org-log-note-purpose nil)
  11999. (defvar org-log-note-state nil)
  12000. (defvar org-log-note-previous-state nil)
  12001. (defvar org-log-note-how nil)
  12002. (defvar org-log-note-extra nil)
  12003. (defvar org-log-note-window-configuration nil)
  12004. (defvar org-log-note-return-to (make-marker))
  12005. (defvar org-log-note-effective-time nil
  12006. "Remembered current time so that dynamically scoped
  12007. `org-extend-today-until' affects tha timestamps in state change
  12008. log")
  12009. (defvar org-log-post-message nil
  12010. "Message to be displayed after a log note has been stored.
  12011. The auto-repeater uses this.")
  12012. (defun org-add-note ()
  12013. "Add a note to the current entry.
  12014. This is done in the same way as adding a state change note."
  12015. (interactive)
  12016. (org-add-log-setup 'note nil nil 'findpos nil))
  12017. (defvar org-property-end-re)
  12018. (defun org-add-log-setup (&optional purpose state prev-state
  12019. findpos how extra)
  12020. "Set up the post command hook to take a note.
  12021. If this is about to TODO state change, the new state is expected in STATE.
  12022. When FINDPOS is non-nil, find the correct position for the note in
  12023. the current entry. If not, assume that it can be inserted at point.
  12024. HOW is an indicator what kind of note should be created.
  12025. EXTRA is additional text that will be inserted into the notes buffer."
  12026. (let* ((org-log-into-drawer (org-log-into-drawer))
  12027. (drawer (cond ((stringp org-log-into-drawer)
  12028. org-log-into-drawer)
  12029. (org-log-into-drawer "LOGBOOK"))))
  12030. (save-restriction
  12031. (save-excursion
  12032. (when findpos
  12033. (org-back-to-heading t)
  12034. (narrow-to-region (point) (save-excursion
  12035. (outline-next-heading) (point)))
  12036. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12037. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12038. "[^\r\n]*\\)?"))
  12039. (goto-char (match-end 0))
  12040. (cond
  12041. (drawer
  12042. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12043. nil t)
  12044. (progn
  12045. (goto-char (match-end 0))
  12046. (or org-log-states-order-reversed
  12047. (and (re-search-forward org-property-end-re nil t)
  12048. (goto-char (1- (match-beginning 0))))))
  12049. (insert "\n:" drawer ":\n:END:")
  12050. (beginning-of-line 0)
  12051. (org-indent-line)
  12052. (beginning-of-line 2)
  12053. (org-indent-line)
  12054. (end-of-line 0)))
  12055. ((and org-log-state-notes-insert-after-drawers
  12056. (save-excursion
  12057. (forward-line) (looking-at org-drawer-regexp)))
  12058. (forward-line)
  12059. (while (looking-at org-drawer-regexp)
  12060. (goto-char (match-end 0))
  12061. (re-search-forward org-property-end-re (point-max) t)
  12062. (forward-line))
  12063. (forward-line -1)))
  12064. (unless org-log-states-order-reversed
  12065. (and (= (char-after) ?\n) (forward-char 1))
  12066. (org-skip-over-state-notes)
  12067. (skip-chars-backward " \t\n\r")))
  12068. (move-marker org-log-note-marker (point))
  12069. (setq org-log-note-purpose purpose
  12070. org-log-note-state state
  12071. org-log-note-previous-state prev-state
  12072. org-log-note-how how
  12073. org-log-note-extra extra
  12074. org-log-note-effective-time (org-current-effective-time))
  12075. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12076. (defun org-skip-over-state-notes ()
  12077. "Skip past the list of State notes in an entry."
  12078. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12079. (when (ignore-errors (goto-char (org-in-item-p)))
  12080. (let* ((struct (org-list-struct))
  12081. (prevs (org-list-prevs-alist struct)))
  12082. (while (looking-at "[ \t]*- State")
  12083. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12084. (org-list-get-item-end (point) struct)))))))
  12085. (defun org-add-log-note (&optional purpose)
  12086. "Pop up a window for taking a note, and add this note later at point."
  12087. (remove-hook 'post-command-hook 'org-add-log-note)
  12088. (setq org-log-note-window-configuration (current-window-configuration))
  12089. (delete-other-windows)
  12090. (move-marker org-log-note-return-to (point))
  12091. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12092. (goto-char org-log-note-marker)
  12093. (org-switch-to-buffer-other-window "*Org Note*")
  12094. (erase-buffer)
  12095. (if (memq org-log-note-how '(time state))
  12096. (let (current-prefix-arg) (org-store-log-note))
  12097. (let ((org-inhibit-startup t)) (org-mode))
  12098. (insert (format "# Insert note for %s.
  12099. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12100. (cond
  12101. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12102. ((eq org-log-note-purpose 'done) "closed todo item")
  12103. ((eq org-log-note-purpose 'state)
  12104. (format "state change from \"%s\" to \"%s\""
  12105. (or org-log-note-previous-state "")
  12106. (or org-log-note-state "")))
  12107. ((eq org-log-note-purpose 'reschedule)
  12108. "rescheduling")
  12109. ((eq org-log-note-purpose 'delschedule)
  12110. "no longer scheduled")
  12111. ((eq org-log-note-purpose 'redeadline)
  12112. "changing deadline")
  12113. ((eq org-log-note-purpose 'deldeadline)
  12114. "removing deadline")
  12115. ((eq org-log-note-purpose 'refile)
  12116. "refiling")
  12117. ((eq org-log-note-purpose 'note)
  12118. "this entry")
  12119. (t (error "This should not happen")))))
  12120. (if org-log-note-extra (insert org-log-note-extra))
  12121. (org-set-local 'org-finish-function 'org-store-log-note)
  12122. (run-hooks 'org-log-buffer-setup-hook)))
  12123. (defvar org-note-abort nil) ; dynamically scoped
  12124. (defun org-store-log-note ()
  12125. "Finish taking a log note, and insert it to where it belongs."
  12126. (let ((txt (buffer-string)))
  12127. (kill-buffer (current-buffer))
  12128. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12129. lines ind bul)
  12130. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12131. (setq txt (replace-match "" t t txt)))
  12132. (if (string-match "\\s-+\\'" txt)
  12133. (setq txt (replace-match "" t t txt)))
  12134. (setq lines (org-split-string txt "\n"))
  12135. (when (and note (string-match "\\S-" note))
  12136. (setq note
  12137. (org-replace-escapes
  12138. note
  12139. (list (cons "%u" (user-login-name))
  12140. (cons "%U" user-full-name)
  12141. (cons "%t" (format-time-string
  12142. (org-time-stamp-format 'long 'inactive)
  12143. org-log-note-effective-time))
  12144. (cons "%T" (format-time-string
  12145. (org-time-stamp-format 'long nil)
  12146. org-log-note-effective-time))
  12147. (cons "%d" (format-time-string
  12148. (org-time-stamp-format nil 'inactive)
  12149. org-log-note-effective-time))
  12150. (cons "%D" (format-time-string
  12151. (org-time-stamp-format nil nil)
  12152. org-log-note-effective-time))
  12153. (cons "%s" (if org-log-note-state
  12154. (concat "\"" org-log-note-state "\"")
  12155. ""))
  12156. (cons "%S" (if org-log-note-previous-state
  12157. (concat "\"" org-log-note-previous-state "\"")
  12158. "\"\"")))))
  12159. (if lines (setq note (concat note " \\\\")))
  12160. (push note lines))
  12161. (when (or current-prefix-arg org-note-abort)
  12162. (when org-log-into-drawer
  12163. (org-remove-empty-drawer-at
  12164. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12165. org-log-note-marker))
  12166. (setq lines nil))
  12167. (when lines
  12168. (with-current-buffer (marker-buffer org-log-note-marker)
  12169. (save-excursion
  12170. (goto-char org-log-note-marker)
  12171. (move-marker org-log-note-marker nil)
  12172. (end-of-line 1)
  12173. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12174. (setq ind (save-excursion
  12175. (if (ignore-errors (goto-char (org-in-item-p)))
  12176. (let ((struct (org-list-struct)))
  12177. (org-list-get-ind
  12178. (org-list-get-top-point struct) struct))
  12179. (skip-chars-backward " \r\t\n")
  12180. (cond
  12181. ((and (org-at-heading-p)
  12182. org-adapt-indentation)
  12183. (1+ (org-current-level)))
  12184. ((org-at-heading-p) 0)
  12185. (t (org-get-indentation))))))
  12186. (setq bul (org-list-bullet-string "-"))
  12187. (org-indent-line-to ind)
  12188. (insert bul (pop lines))
  12189. (let ((ind-body (+ (length bul) ind)))
  12190. (while lines
  12191. (insert "\n")
  12192. (org-indent-line-to ind-body)
  12193. (insert (pop lines))))
  12194. (message "Note stored")
  12195. (org-back-to-heading t)
  12196. (org-cycle-hide-drawers 'children))
  12197. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12198. ;; from within `org-add-log-note' because `buffer-undo-list'
  12199. ;; is then modified outside of `org-with-remote-undo'.
  12200. (when (eq this-command 'org-agenda-todo)
  12201. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12202. ;; Don't add undo information when called from `org-agenda-todo'
  12203. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12204. (set-window-configuration org-log-note-window-configuration)
  12205. (with-current-buffer (marker-buffer org-log-note-return-to)
  12206. (goto-char org-log-note-return-to))
  12207. (move-marker org-log-note-return-to nil)
  12208. (and org-log-post-message (message "%s" org-log-post-message))))
  12209. (defun org-remove-empty-drawer-at (drawer pos)
  12210. "Remove an empty drawer DRAWER at position POS.
  12211. POS may also be a marker."
  12212. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12213. (save-excursion
  12214. (save-restriction
  12215. (widen)
  12216. (goto-char pos)
  12217. (if (org-in-regexp
  12218. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12219. (replace-match ""))))))
  12220. (defvar org-ts-type nil)
  12221. (defun org-sparse-tree (&optional arg type)
  12222. "Create a sparse tree, prompt for the details.
  12223. This command can create sparse trees. You first need to select the type
  12224. of match used to create the tree:
  12225. t Show all TODO entries.
  12226. T Show entries with a specific TODO keyword.
  12227. m Show entries selected by a tags/property match.
  12228. p Enter a property name and its value (both with completion on existing
  12229. names/values) and show entries with that property.
  12230. r Show entries matching a regular expression (`/' can be used as well).
  12231. b Show deadlines and scheduled items before a date.
  12232. a Show deadlines and scheduled items after a date.
  12233. d Show deadlines due within `org-deadline-warning-days'.
  12234. D Show deadlines and scheduled items between a date range."
  12235. (interactive "P")
  12236. (let (ans kwd value ts-type)
  12237. (setq type (or type org-sparse-tree-default-date-type))
  12238. (setq org-ts-type type)
  12239. (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"
  12240. (cond ((eq type 'all) "all timestamps")
  12241. ((eq type 'scheduled) "only scheduled")
  12242. ((eq type 'deadline) "only deadline")
  12243. ((eq type 'active) "only active timestamps")
  12244. ((eq type 'inactive) "only inactive timestamps")
  12245. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12246. ((eq type 'closed) "with a closed time-stamp")
  12247. (t "scheduled/deadline")))
  12248. (setq ans (read-char-exclusive))
  12249. (cond
  12250. ((equal ans ?c)
  12251. (org-sparse-tree
  12252. arg (cadr (member type '(scheduled-or-deadline
  12253. all scheduled deadline active inactive closed)))))
  12254. ((equal ans ?d)
  12255. (call-interactively 'org-check-deadlines))
  12256. ((equal ans ?b)
  12257. (call-interactively 'org-check-before-date))
  12258. ((equal ans ?a)
  12259. (call-interactively 'org-check-after-date))
  12260. ((equal ans ?D)
  12261. (call-interactively 'org-check-dates-range))
  12262. ((equal ans ?t)
  12263. (call-interactively 'org-show-todo-tree))
  12264. ((equal ans ?T)
  12265. (org-show-todo-tree '(4)))
  12266. ((member ans '(?T ?m))
  12267. (call-interactively 'org-match-sparse-tree))
  12268. ((member ans '(?p ?P))
  12269. (setq kwd (org-icompleting-read "Property: "
  12270. (mapcar 'list (org-buffer-property-keys))))
  12271. (setq value (org-icompleting-read "Value: "
  12272. (mapcar 'list (org-property-values kwd))))
  12273. (unless (string-match "\\`{.*}\\'" value)
  12274. (setq value (concat "\"" value "\"")))
  12275. (org-match-sparse-tree arg (concat kwd "=" value)))
  12276. ((member ans '(?r ?R ?/))
  12277. (call-interactively 'org-occur))
  12278. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12279. (defvar org-occur-highlights nil
  12280. "List of overlays used for occur matches.")
  12281. (make-variable-buffer-local 'org-occur-highlights)
  12282. (defvar org-occur-parameters nil
  12283. "Parameters of the active org-occur calls.
  12284. This is a list, each call to org-occur pushes as cons cell,
  12285. containing the regular expression and the callback, onto the list.
  12286. The list can contain several entries if `org-occur' has been called
  12287. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12288. will only contain one set of parameters. When the highlights are
  12289. removed (for example with `C-c C-c', or with the next edit (depending
  12290. on `org-remove-highlights-with-change'), this variable is emptied
  12291. as well.")
  12292. (make-variable-buffer-local 'org-occur-parameters)
  12293. (defun org-occur (regexp &optional keep-previous callback)
  12294. "Make a compact tree which shows all matches of REGEXP.
  12295. The tree will show the lines where the regexp matches, and all higher
  12296. headlines above the match. It will also show the heading after the match,
  12297. to make sure editing the matching entry is easy.
  12298. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12299. call to `org-occur' will be kept, to allow stacking of calls to this
  12300. command.
  12301. If CALLBACK is non-nil, it is a function which is called to confirm
  12302. that the match should indeed be shown."
  12303. (interactive "sRegexp: \nP")
  12304. (when (equal regexp "")
  12305. (user-error "Regexp cannot be empty"))
  12306. (unless keep-previous
  12307. (org-remove-occur-highlights nil nil t))
  12308. (push (cons regexp callback) org-occur-parameters)
  12309. (let ((cnt 0))
  12310. (save-excursion
  12311. (goto-char (point-min))
  12312. (if (or (not keep-previous) ; do not want to keep
  12313. (not org-occur-highlights)) ; no previous matches
  12314. ;; hide everything
  12315. (org-overview))
  12316. (while (re-search-forward regexp nil t)
  12317. (when (or (not callback)
  12318. (save-match-data (funcall callback)))
  12319. (setq cnt (1+ cnt))
  12320. (when org-highlight-sparse-tree-matches
  12321. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12322. (org-show-context 'occur-tree))))
  12323. (when org-remove-highlights-with-change
  12324. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12325. nil 'local))
  12326. (unless org-sparse-tree-open-archived-trees
  12327. (org-hide-archived-subtrees (point-min) (point-max)))
  12328. (run-hooks 'org-occur-hook)
  12329. (if (org-called-interactively-p 'interactive)
  12330. (message "%d match(es) for regexp %s" cnt regexp))
  12331. cnt))
  12332. (defun org-occur-next-match (&optional n reset)
  12333. "Function for `next-error-function' to find sparse tree matches.
  12334. N is the number of matches to move, when negative move backwards.
  12335. RESET is entirely ignored - this function always goes back to the
  12336. starting point when no match is found."
  12337. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12338. (search-func (if (< n 0)
  12339. 'previous-single-char-property-change
  12340. 'next-single-char-property-change))
  12341. (n (abs n))
  12342. (pos (point))
  12343. p1)
  12344. (catch 'exit
  12345. (while (setq p1 (funcall search-func (point) 'org-type))
  12346. (when (equal p1 limit)
  12347. (goto-char pos)
  12348. (error "No more matches"))
  12349. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12350. (setq n (1- n))
  12351. (when (= n 0)
  12352. (goto-char p1)
  12353. (throw 'exit (point))))
  12354. (goto-char p1))
  12355. (goto-char p1)
  12356. (error "No more matches"))))
  12357. (defun org-show-context (&optional key)
  12358. "Make sure point and context are visible.
  12359. How much context is shown depends upon the variables
  12360. `org-show-hierarchy-above', `org-show-following-heading',
  12361. `org-show-entry-below' and `org-show-siblings'."
  12362. (let ((heading-p (org-at-heading-p t))
  12363. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12364. (following-p (org-get-alist-option org-show-following-heading key))
  12365. (entry-p (org-get-alist-option org-show-entry-below key))
  12366. (siblings-p (org-get-alist-option org-show-siblings key)))
  12367. ;; Show heading or entry text
  12368. (if (and heading-p (not entry-p))
  12369. (org-flag-heading nil) ; only show the heading
  12370. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12371. (org-show-hidden-entry))) ; show entire entry
  12372. (when following-p
  12373. ;; Show next sibling, or heading below text
  12374. (save-excursion
  12375. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12376. (org-flag-heading nil))))
  12377. (when siblings-p (org-show-siblings))
  12378. (when hierarchy-p
  12379. ;; show all higher headings, possibly with siblings
  12380. (save-excursion
  12381. (while (and (condition-case nil
  12382. (progn (org-up-heading-all 1) t)
  12383. (error nil))
  12384. (not (bobp)))
  12385. (org-flag-heading nil)
  12386. (when siblings-p (org-show-siblings)))))
  12387. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12388. (defvar org-reveal-start-hook nil
  12389. "Hook run before revealing a location.")
  12390. (defun org-reveal (&optional siblings)
  12391. "Show current entry, hierarchy above it, and the following headline.
  12392. This can be used to show a consistent set of context around locations
  12393. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12394. not t for the search context.
  12395. With optional argument SIBLINGS, on each level of the hierarchy all
  12396. siblings are shown. This repairs the tree structure to what it would
  12397. look like when opened with hierarchical calls to `org-cycle'.
  12398. With double optional argument \\[universal-argument] \\[universal-argument], \
  12399. go to the parent and show the
  12400. entire tree."
  12401. (interactive "P")
  12402. (run-hooks 'org-reveal-start-hook)
  12403. (let ((org-show-hierarchy-above t)
  12404. (org-show-following-heading t)
  12405. (org-show-siblings (if siblings t org-show-siblings)))
  12406. (org-show-context nil))
  12407. (when (equal siblings '(16))
  12408. (save-excursion
  12409. (when (org-up-heading-safe)
  12410. (org-show-subtree)
  12411. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12412. (defun org-highlight-new-match (beg end)
  12413. "Highlight from BEG to END and mark the highlight is an occur headline."
  12414. (let ((ov (make-overlay beg end)))
  12415. (overlay-put ov 'face 'secondary-selection)
  12416. (overlay-put ov 'org-type 'org-occur)
  12417. (push ov org-occur-highlights)))
  12418. (defun org-remove-occur-highlights (&optional beg end noremove)
  12419. "Remove the occur highlights from the buffer.
  12420. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12421. from the `before-change-functions' in the current buffer."
  12422. (interactive)
  12423. (unless org-inhibit-highlight-removal
  12424. (mapc 'delete-overlay org-occur-highlights)
  12425. (setq org-occur-highlights nil)
  12426. (setq org-occur-parameters nil)
  12427. (unless noremove
  12428. (remove-hook 'before-change-functions
  12429. 'org-remove-occur-highlights 'local))))
  12430. ;;;; Priorities
  12431. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12432. "Regular expression matching the priority indicator.")
  12433. (defvar org-remove-priority-next-time nil)
  12434. (defun org-priority-up ()
  12435. "Increase the priority of the current item."
  12436. (interactive)
  12437. (org-priority 'up))
  12438. (defun org-priority-down ()
  12439. "Decrease the priority of the current item."
  12440. (interactive)
  12441. (org-priority 'down))
  12442. (defun org-priority (&optional action show)
  12443. "Change the priority of an item.
  12444. ACTION can be `set', `up', `down', or a character."
  12445. (interactive "P")
  12446. (if (equal action '(4))
  12447. (org-show-priority)
  12448. (unless org-enable-priority-commands
  12449. (user-error "Priority commands are disabled"))
  12450. (setq action (or action 'set))
  12451. (let (current new news have remove)
  12452. (save-excursion
  12453. (org-back-to-heading t)
  12454. (if (looking-at org-priority-regexp)
  12455. (setq current (string-to-char (match-string 2))
  12456. have t))
  12457. (cond
  12458. ((eq action 'remove)
  12459. (setq remove t new ?\ ))
  12460. ((or (eq action 'set)
  12461. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12462. (if (not (eq action 'set))
  12463. (setq new action)
  12464. (message "Priority %c-%c, SPC to remove: "
  12465. org-highest-priority org-lowest-priority)
  12466. (save-match-data
  12467. (setq new (read-char-exclusive))))
  12468. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12469. (= (upcase org-lowest-priority) org-lowest-priority))
  12470. (setq new (upcase new)))
  12471. (cond ((equal new ?\ ) (setq remove t))
  12472. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12473. (user-error "Priority must be between `%c' and `%c'"
  12474. org-highest-priority org-lowest-priority))))
  12475. ((eq action 'up)
  12476. (setq new (if have
  12477. (1- current) ; normal cycling
  12478. ;; last priority was empty
  12479. (if (eq last-command this-command)
  12480. org-lowest-priority ; wrap around empty to lowest
  12481. ;; default
  12482. (if org-priority-start-cycle-with-default
  12483. org-default-priority
  12484. (1- org-default-priority))))))
  12485. ((eq action 'down)
  12486. (setq new (if have
  12487. (1+ current) ; normal cycling
  12488. ;; last priority was empty
  12489. (if (eq last-command this-command)
  12490. org-highest-priority ; wrap around empty to highest
  12491. ;; default
  12492. (if org-priority-start-cycle-with-default
  12493. org-default-priority
  12494. (1+ org-default-priority))))))
  12495. (t (user-error "Invalid action")))
  12496. (if (or (< (upcase new) org-highest-priority)
  12497. (> (upcase new) org-lowest-priority))
  12498. (if (and (memq action '(up down))
  12499. (not have) (not (eq last-command this-command)))
  12500. ;; `new' is from default priority
  12501. (error
  12502. "The default can not be set, see `org-default-priority' why")
  12503. ;; normal cycling: `new' is beyond highest/lowest priority
  12504. ;; and is wrapped around to the empty priority
  12505. (setq remove t)))
  12506. (setq news (format "%c" new))
  12507. (if have
  12508. (if remove
  12509. (replace-match "" t t nil 1)
  12510. (replace-match news t t nil 2))
  12511. (if remove
  12512. (user-error "No priority cookie found in line")
  12513. (let ((case-fold-search nil))
  12514. (looking-at org-todo-line-regexp))
  12515. (if (match-end 2)
  12516. (progn
  12517. (goto-char (match-end 2))
  12518. (insert " [#" news "]"))
  12519. (goto-char (match-beginning 3))
  12520. (insert "[#" news "] "))))
  12521. (org-preserve-lc (org-set-tags nil 'align)))
  12522. (if remove
  12523. (message "Priority removed")
  12524. (message "Priority of current item set to %s" news)))))
  12525. (defun org-show-priority ()
  12526. "Show the priority of the current item.
  12527. This priority is composed of the main priority given with the [#A] cookies,
  12528. and by additional input from the age of a schedules or deadline entry."
  12529. (interactive)
  12530. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12531. (org-get-at-bol 'priority)
  12532. (save-excursion
  12533. (save-match-data
  12534. (beginning-of-line)
  12535. (and (looking-at org-heading-regexp)
  12536. (org-get-priority (match-string 0))))))))
  12537. (message "Priority is %d" (if pri pri -1000))))
  12538. (defun org-get-priority (s)
  12539. "Find priority cookie and return priority."
  12540. (save-match-data
  12541. (if (functionp org-get-priority-function)
  12542. (funcall org-get-priority-function)
  12543. (if (not (string-match org-priority-regexp s))
  12544. (* 1000 (- org-lowest-priority org-default-priority))
  12545. (* 1000 (- org-lowest-priority
  12546. (string-to-char (match-string 2 s))))))))
  12547. ;;;; Tags
  12548. (defvar org-agenda-archives-mode)
  12549. (defvar org-map-continue-from nil
  12550. "Position from where mapping should continue.
  12551. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12552. (defvar org-scanner-tags nil
  12553. "The current tag list while the tags scanner is running.")
  12554. (defvar org-trust-scanner-tags nil
  12555. "Should `org-get-tags-at' use the tags for the scanner.
  12556. This is for internal dynamical scoping only.
  12557. When this is non-nil, the function `org-get-tags-at' will return the value
  12558. of `org-scanner-tags' instead of building the list by itself. This
  12559. can lead to large speed-ups when the tags scanner is used in a file with
  12560. many entries, and when the list of tags is retrieved, for example to
  12561. obtain a list of properties. Building the tags list for each entry in such
  12562. a file becomes an N^2 operation - but with this variable set, it scales
  12563. as N.")
  12564. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12565. "Sca headline tags with inheritance and produce output ACTION.
  12566. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12567. or `agenda' to produce an entry list for an agenda view. It can also be
  12568. a Lisp form or a function that should be called at each matched headline, in
  12569. this case the return value is a list of all return values from these calls.
  12570. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12571. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12572. only lines with a not-done TODO keyword are included in the output.
  12573. This should be the same variable that was scoped into
  12574. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12575. START-LEVEL can be a string with asterisks, reducing the scope to
  12576. headlines matching this string."
  12577. (require 'org-agenda)
  12578. (let* ((re (concat "^"
  12579. (if start-level
  12580. ;; Get the correct level to match
  12581. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12582. org-outline-regexp)
  12583. " *\\(\\<\\("
  12584. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12585. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12586. (props (list 'face 'default
  12587. 'done-face 'org-agenda-done
  12588. 'undone-face 'default
  12589. 'mouse-face 'highlight
  12590. 'org-not-done-regexp org-not-done-regexp
  12591. 'org-todo-regexp org-todo-regexp
  12592. 'org-complex-heading-regexp org-complex-heading-regexp
  12593. 'help-echo
  12594. (format "mouse-2 or RET jump to org file %s"
  12595. (abbreviate-file-name
  12596. (or (buffer-file-name (buffer-base-buffer))
  12597. (buffer-name (buffer-base-buffer)))))))
  12598. (org-map-continue-from nil)
  12599. lspos tags tags-list
  12600. (tags-alist (list (cons 0 org-file-tags)))
  12601. (llast 0) rtn rtn1 level category i txt
  12602. todo marker entry priority)
  12603. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12604. (setq action (list 'lambda nil action)))
  12605. (save-excursion
  12606. (goto-char (point-min))
  12607. (when (eq action 'sparse-tree)
  12608. (org-overview)
  12609. (org-remove-occur-highlights))
  12610. (while (let (case-fold-search)
  12611. (re-search-forward re nil t))
  12612. (setq org-map-continue-from nil)
  12613. (catch :skip
  12614. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12615. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12616. (goto-char (setq lspos (match-beginning 0)))
  12617. (setq level (org-reduced-level (org-outline-level))
  12618. category (org-get-category))
  12619. (setq i llast llast level)
  12620. ;; remove tag lists from same and sublevels
  12621. (while (>= i level)
  12622. (when (setq entry (assoc i tags-alist))
  12623. (setq tags-alist (delete entry tags-alist)))
  12624. (setq i (1- i)))
  12625. ;; add the next tags
  12626. (when tags
  12627. (setq tags (org-split-string tags ":")
  12628. tags-alist
  12629. (cons (cons level tags) tags-alist)))
  12630. ;; compile tags for current headline
  12631. (setq tags-list
  12632. (if org-use-tag-inheritance
  12633. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12634. tags)
  12635. org-scanner-tags tags-list)
  12636. (when org-use-tag-inheritance
  12637. (setcdr (car tags-alist)
  12638. (mapcar (lambda (x)
  12639. (setq x (copy-sequence x))
  12640. (org-add-prop-inherited x))
  12641. (cdar tags-alist))))
  12642. (when (and tags org-use-tag-inheritance
  12643. (or (not (eq t org-use-tag-inheritance))
  12644. org-tags-exclude-from-inheritance))
  12645. ;; selective inheritance, remove uninherited ones
  12646. (setcdr (car tags-alist)
  12647. (org-remove-uninherited-tags (cdar tags-alist))))
  12648. (when (and
  12649. ;; eval matcher only when the todo condition is OK
  12650. (and (or (not todo-only) (member todo org-not-done-keywords))
  12651. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12652. (eval matcher)))
  12653. ;; Call the skipper, but return t if it does not skip,
  12654. ;; so that the `and' form continues evaluating
  12655. (progn
  12656. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12657. t)
  12658. ;; Check if timestamps are deselecting this entry
  12659. (or (not todo-only)
  12660. (and (member todo org-not-done-keywords)
  12661. (or (not org-agenda-tags-todo-honor-ignore-options)
  12662. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12663. ;; select this headline
  12664. (cond
  12665. ((eq action 'sparse-tree)
  12666. (and org-highlight-sparse-tree-matches
  12667. (org-get-heading) (match-end 0)
  12668. (org-highlight-new-match
  12669. (match-beginning 1) (match-end 1)))
  12670. (org-show-context 'tags-tree))
  12671. ((eq action 'agenda)
  12672. (setq txt (org-agenda-format-item
  12673. ""
  12674. (concat
  12675. (if (eq org-tags-match-list-sublevels 'indented)
  12676. (make-string (1- level) ?.) "")
  12677. (org-get-heading))
  12678. level category
  12679. tags-list)
  12680. priority (org-get-priority txt))
  12681. (goto-char lspos)
  12682. (setq marker (org-agenda-new-marker))
  12683. (org-add-props txt props
  12684. 'org-marker marker 'org-hd-marker marker 'org-category category
  12685. 'todo-state todo
  12686. 'priority priority 'type "tagsmatch")
  12687. (push txt rtn))
  12688. ((functionp action)
  12689. (setq org-map-continue-from nil)
  12690. (save-excursion
  12691. (setq rtn1 (funcall action))
  12692. (push rtn1 rtn)))
  12693. (t (user-error "Invalid action")))
  12694. ;; if we are to skip sublevels, jump to end of subtree
  12695. (unless org-tags-match-list-sublevels
  12696. (org-end-of-subtree t)
  12697. (backward-char 1))))
  12698. ;; Get the correct position from where to continue
  12699. (if org-map-continue-from
  12700. (goto-char org-map-continue-from)
  12701. (and (= (point) lspos) (end-of-line 1)))))
  12702. (when (and (eq action 'sparse-tree)
  12703. (not org-sparse-tree-open-archived-trees))
  12704. (org-hide-archived-subtrees (point-min) (point-max)))
  12705. (nreverse rtn)))
  12706. (defun org-remove-uninherited-tags (tags)
  12707. "Remove all tags that are not inherited from the list TAGS."
  12708. (cond
  12709. ((eq org-use-tag-inheritance t)
  12710. (if org-tags-exclude-from-inheritance
  12711. (org-delete-all org-tags-exclude-from-inheritance tags)
  12712. tags))
  12713. ((not org-use-tag-inheritance) nil)
  12714. ((stringp org-use-tag-inheritance)
  12715. (delq nil (mapcar
  12716. (lambda (x)
  12717. (if (and (string-match org-use-tag-inheritance x)
  12718. (not (member x org-tags-exclude-from-inheritance)))
  12719. x nil))
  12720. tags)))
  12721. ((listp org-use-tag-inheritance)
  12722. (delq nil (mapcar
  12723. (lambda (x)
  12724. (if (member x org-use-tag-inheritance) x nil))
  12725. tags)))))
  12726. (defun org-match-sparse-tree (&optional todo-only match)
  12727. "Create a sparse tree according to tags string MATCH.
  12728. MATCH can contain positive and negative selection of tags, like
  12729. \"+WORK+URGENT-WITHBOSS\".
  12730. If optional argument TODO-ONLY is non-nil, only select lines that are
  12731. also TODO lines."
  12732. (interactive "P")
  12733. (org-agenda-prepare-buffers (list (current-buffer)))
  12734. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12735. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12736. (defvar org-cached-props nil)
  12737. (defun org-cached-entry-get (pom property)
  12738. (if (or (eq t org-use-property-inheritance)
  12739. (and (stringp org-use-property-inheritance)
  12740. (string-match org-use-property-inheritance property))
  12741. (and (listp org-use-property-inheritance)
  12742. (member property org-use-property-inheritance)))
  12743. ;; Caching is not possible, check it directly
  12744. (org-entry-get pom property 'inherit)
  12745. ;; Get all properties, so that we can do complicated checks easily
  12746. (cdr (assoc property (or org-cached-props
  12747. (setq org-cached-props
  12748. (org-entry-properties pom)))))))
  12749. (defun org-global-tags-completion-table (&optional files)
  12750. "Return the list of all tags in all agenda buffer/files.
  12751. Optional FILES argument is a list of files which can be used
  12752. instead of the agenda files."
  12753. (save-excursion
  12754. (org-uniquify
  12755. (delq nil
  12756. (apply 'append
  12757. (mapcar
  12758. (lambda (file)
  12759. (set-buffer (find-file-noselect file))
  12760. (append (org-get-buffer-tags)
  12761. (mapcar (lambda (x) (if (stringp (car-safe x))
  12762. (list (car-safe x)) nil))
  12763. org-tag-alist)))
  12764. (if (and files (car files))
  12765. files
  12766. (org-agenda-files))))))))
  12767. (defun org-make-tags-matcher (match)
  12768. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12769. The variable `todo-only' is scoped dynamically into this function.
  12770. It will be set to t if the matcher restricts matching to TODO entries,
  12771. otherwise will not be touched.
  12772. Returns a cons of the selection string MATCH and the constructed
  12773. lisp form implementing the matcher. The matcher is to be evaluated
  12774. at an Org entry, with point on the headline, and returns t if the
  12775. entry matches the selection string MATCH. The returned lisp form
  12776. references two variables with information about the entry, which
  12777. must be bound around the form's evaluation: todo, the TODO keyword
  12778. at the entry (or nil of none); and tags-list, the list of all tags
  12779. at the entry including inherited ones. Additionally, the category
  12780. of the entry (if any) must be specified as the text property
  12781. 'org-category on the headline.
  12782. See also `org-scan-tags'.
  12783. "
  12784. (declare (special todo-only))
  12785. (unless (boundp 'todo-only)
  12786. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12787. (unless match
  12788. ;; Get a new match request, with completion against the global
  12789. ;; tags table and the local tags in current buffer
  12790. (let ((org-last-tags-completion-table
  12791. (org-uniquify
  12792. (delq nil (append (org-get-buffer-tags)
  12793. (org-global-tags-completion-table))))))
  12794. (setq match (org-completing-read-no-i
  12795. "Match: " 'org-tags-completion-function nil nil nil
  12796. 'org-tags-history))))
  12797. ;; Parse the string and create a lisp form
  12798. (let ((match0 match)
  12799. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12800. minus tag mm
  12801. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12802. orterms term orlist re-p str-p level-p level-op time-p
  12803. prop-p pn pv po gv rest)
  12804. ;; Expand group tags
  12805. (setq match (org-tags-expand match))
  12806. (if (string-match "/+" match)
  12807. ;; match contains also a todo-matching request
  12808. (progn
  12809. (setq tagsmatch (substring match 0 (match-beginning 0))
  12810. todomatch (substring match (match-end 0)))
  12811. (if (string-match "^!" todomatch)
  12812. (setq todo-only t todomatch (substring todomatch 1)))
  12813. (if (string-match "^\\s-*$" todomatch)
  12814. (setq todomatch nil)))
  12815. ;; only matching tags
  12816. (setq tagsmatch match todomatch nil))
  12817. ;; Make the tags matcher
  12818. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12819. (setq tagsmatcher t)
  12820. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12821. (while (setq term (pop orterms))
  12822. (while (and (equal (substring term -1) "\\") orterms)
  12823. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12824. (while (string-match re term)
  12825. (setq rest (substring term (match-end 0))
  12826. minus (and (match-end 1)
  12827. (equal (match-string 1 term) "-"))
  12828. tag (save-match-data (replace-regexp-in-string
  12829. "\\\\-" "-"
  12830. (match-string 2 term)))
  12831. re-p (equal (string-to-char tag) ?{)
  12832. level-p (match-end 4)
  12833. prop-p (match-end 5)
  12834. mm (cond
  12835. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12836. (level-p
  12837. (setq level-op (org-op-to-function (match-string 3 term)))
  12838. `(,level-op level ,(string-to-number
  12839. (match-string 4 term))))
  12840. (prop-p
  12841. (setq pn (match-string 5 term)
  12842. po (match-string 6 term)
  12843. pv (match-string 7 term)
  12844. re-p (equal (string-to-char pv) ?{)
  12845. str-p (equal (string-to-char pv) ?\")
  12846. time-p (save-match-data
  12847. (string-match "^\"[[<].*[]>]\"$" pv))
  12848. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12849. (if time-p (setq pv (org-matcher-time pv)))
  12850. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12851. (cond
  12852. ((equal pn "CATEGORY")
  12853. (setq gv '(get-text-property (point) 'org-category)))
  12854. ((equal pn "TODO")
  12855. (setq gv 'todo))
  12856. (t
  12857. (setq gv `(org-cached-entry-get nil ,pn))))
  12858. (if re-p
  12859. (if (eq po 'org<>)
  12860. `(not (string-match ,pv (or ,gv "")))
  12861. `(string-match ,pv (or ,gv "")))
  12862. (if str-p
  12863. `(,po (or ,gv "") ,pv)
  12864. `(,po (string-to-number (or ,gv ""))
  12865. ,(string-to-number pv) ))))
  12866. (t `(member ,tag tags-list)))
  12867. mm (if minus (list 'not mm) mm)
  12868. term rest)
  12869. (push mm tagsmatcher))
  12870. (push (if (> (length tagsmatcher) 1)
  12871. (cons 'and tagsmatcher)
  12872. (car tagsmatcher))
  12873. orlist)
  12874. (setq tagsmatcher nil))
  12875. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12876. (setq tagsmatcher
  12877. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12878. ;; Make the todo matcher
  12879. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12880. (setq todomatcher t)
  12881. (setq orterms (org-split-string todomatch "|") orlist nil)
  12882. (while (setq term (pop orterms))
  12883. (while (string-match re term)
  12884. (setq minus (and (match-end 1)
  12885. (equal (match-string 1 term) "-"))
  12886. kwd (match-string 2 term)
  12887. re-p (equal (string-to-char kwd) ?{)
  12888. term (substring term (match-end 0))
  12889. mm (if re-p
  12890. `(string-match ,(substring kwd 1 -1) todo)
  12891. (list 'equal 'todo kwd))
  12892. mm (if minus (list 'not mm) mm))
  12893. (push mm todomatcher))
  12894. (push (if (> (length todomatcher) 1)
  12895. (cons 'and todomatcher)
  12896. (car todomatcher))
  12897. orlist)
  12898. (setq todomatcher nil))
  12899. (setq todomatcher (if (> (length orlist) 1)
  12900. (cons 'or orlist) (car orlist))))
  12901. ;; Return the string and lisp forms of the matcher
  12902. (setq matcher (if todomatcher
  12903. (list 'and tagsmatcher todomatcher)
  12904. tagsmatcher))
  12905. (when todo-only
  12906. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12907. matcher)))
  12908. (cons match0 matcher)))
  12909. (defun org-tags-expand (match &optional single-as-list downcased)
  12910. "Expand group tags in MATCH.
  12911. This replaces every group tag in MATCH with a regexp tag search.
  12912. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12913. will be replaced like this:
  12914. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12915. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12916. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12917. Replacing by a regexp preserves the structure of the match.
  12918. E.g., this expansion
  12919. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12920. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12921. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12922. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12923. assumed to be a single group tag, and the function will return
  12924. the list of tags in this group.
  12925. When DOWNCASE is non-nil, expand downcased TAGS."
  12926. (if org-group-tags
  12927. (let* ((case-fold-search t)
  12928. (stable org-mode-syntax-table)
  12929. (tal (or org-tag-groups-alist-for-agenda
  12930. org-tag-groups-alist))
  12931. (tal (if downcased
  12932. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12933. (tml (mapcar 'car tal))
  12934. (rtnmatch match) rpl)
  12935. ;; @ and _ are allowed as word-components in tags
  12936. (modify-syntax-entry ?@ "w" stable)
  12937. (modify-syntax-entry ?_ "w" stable)
  12938. (while (and tml
  12939. (with-syntax-table stable
  12940. (string-match
  12941. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12942. (regexp-opt tml) "\\>\\)") rtnmatch)))
  12943. (let* ((dir (match-string 1 rtnmatch))
  12944. (tag (match-string 2 rtnmatch))
  12945. (tag (if downcased (downcase tag) tag)))
  12946. (setq tml (delete tag tml))
  12947. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12948. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12949. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12950. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12951. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12952. (if single-as-list
  12953. (or (reverse rpl) (list rtnmatch))
  12954. rtnmatch))
  12955. (if single-as-list (list (if downcased (downcase match) match))
  12956. match)))
  12957. (defun org-op-to-function (op &optional stringp)
  12958. "Turn an operator into the appropriate function."
  12959. (setq op
  12960. (cond
  12961. ((equal op "<" ) '(< string< org-time<))
  12962. ((equal op ">" ) '(> org-string> org-time>))
  12963. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12964. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12965. ((member op '("=" "==")) '(= string= org-time=))
  12966. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12967. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12968. (defun org<> (a b) (not (= a b)))
  12969. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12970. (defun org-string>= (a b) (not (string< a b)))
  12971. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12972. (defun org-string<> (a b) (not (string= a b)))
  12973. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12974. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12975. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12976. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12977. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12978. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12979. (defun org-2ft (s)
  12980. "Convert S to a floating point time.
  12981. If S is already a number, just return it. If it is a string, parse
  12982. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12983. (cond
  12984. ((numberp s) s)
  12985. ((stringp s)
  12986. (condition-case nil
  12987. (float-time (apply 'encode-time (org-parse-time-string s)))
  12988. (error 0.)))
  12989. (t 0.)))
  12990. (defun org-time-today ()
  12991. "Time in seconds today at 0:00.
  12992. Returns the float number of seconds since the beginning of the
  12993. epoch to the beginning of today (00:00)."
  12994. (float-time (apply 'encode-time
  12995. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12996. (defun org-matcher-time (s)
  12997. "Interpret a time comparison value."
  12998. (save-match-data
  12999. (cond
  13000. ((string= s "<now>") (float-time))
  13001. ((string= s "<today>") (org-time-today))
  13002. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13003. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13004. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13005. (+ (org-time-today)
  13006. (* (string-to-number (match-string 1 s))
  13007. (cdr (assoc (match-string 2 s)
  13008. '(("d" . 86400.0) ("w" . 604800.0)
  13009. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13010. (t (org-2ft s)))))
  13011. (defun org-match-any-p (re list)
  13012. "Does re match any element of list?"
  13013. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13014. (delq nil list))
  13015. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13016. (defvar org-tags-overlay (make-overlay 1 1))
  13017. (org-detach-overlay org-tags-overlay)
  13018. (defun org-get-local-tags-at (&optional pos)
  13019. "Get a list of tags defined in the current headline."
  13020. (org-get-tags-at pos 'local))
  13021. (defun org-get-local-tags ()
  13022. "Get a list of tags defined in the current headline."
  13023. (org-get-tags-at nil 'local))
  13024. (defun org-get-tags-at (&optional pos local)
  13025. "Get a list of all headline tags applicable at POS.
  13026. POS defaults to point. If tags are inherited, the list contains
  13027. the targets in the same sequence as the headlines appear, i.e.
  13028. the tags of the current headline come last.
  13029. When LOCAL is non-nil, only return tags from the current headline,
  13030. ignore inherited ones."
  13031. (interactive)
  13032. (if (and org-trust-scanner-tags
  13033. (or (not pos) (equal pos (point)))
  13034. (not local))
  13035. org-scanner-tags
  13036. (let (tags ltags lastpos parent)
  13037. (save-excursion
  13038. (save-restriction
  13039. (widen)
  13040. (goto-char (or pos (point)))
  13041. (save-match-data
  13042. (catch 'done
  13043. (condition-case nil
  13044. (progn
  13045. (org-back-to-heading t)
  13046. (while (not (equal lastpos (point)))
  13047. (setq lastpos (point))
  13048. (when (looking-at
  13049. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13050. (setq ltags (org-split-string
  13051. (org-match-string-no-properties 1) ":"))
  13052. (when parent
  13053. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13054. (setq tags (append
  13055. (if parent
  13056. (org-remove-uninherited-tags ltags)
  13057. ltags)
  13058. tags)))
  13059. (or org-use-tag-inheritance (throw 'done t))
  13060. (if local (throw 'done t))
  13061. (or (org-up-heading-safe) (error nil))
  13062. (setq parent t)))
  13063. (error nil)))))
  13064. (if local
  13065. tags
  13066. (reverse (delete-dups
  13067. (reverse (append
  13068. (org-remove-uninherited-tags
  13069. org-file-tags) tags)))))))))
  13070. (defun org-add-prop-inherited (s)
  13071. (add-text-properties 0 (length s) '(inherited t) s)
  13072. s)
  13073. (defun org-toggle-tag (tag &optional onoff)
  13074. "Toggle the tag TAG for the current line.
  13075. If ONOFF is `on' or `off', don't toggle but set to this state."
  13076. (let (res current)
  13077. (save-excursion
  13078. (org-back-to-heading t)
  13079. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13080. (point-at-eol) t)
  13081. (progn
  13082. (setq current (match-string 1))
  13083. (replace-match ""))
  13084. (setq current ""))
  13085. (setq current (nreverse (org-split-string current ":")))
  13086. (cond
  13087. ((eq onoff 'on)
  13088. (setq res t)
  13089. (or (member tag current) (push tag current)))
  13090. ((eq onoff 'off)
  13091. (or (not (member tag current)) (setq current (delete tag current))))
  13092. (t (if (member tag current)
  13093. (setq current (delete tag current))
  13094. (setq res t)
  13095. (push tag current))))
  13096. (end-of-line 1)
  13097. (if current
  13098. (progn
  13099. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13100. (org-set-tags nil t))
  13101. (delete-horizontal-space))
  13102. (run-hooks 'org-after-tags-change-hook))
  13103. res))
  13104. (defun org-align-tags-here (to-col)
  13105. ;; Assumes that this is a headline
  13106. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13107. (beginning-of-line 1)
  13108. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13109. (< pos (match-beginning 2)))
  13110. (progn
  13111. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13112. (goto-char (match-beginning 1))
  13113. (insert " ")
  13114. (delete-region (point) (1+ (match-beginning 2)))
  13115. (setq ncol (max (current-column)
  13116. (1+ col)
  13117. (if (> to-col 0)
  13118. to-col
  13119. (- (abs to-col) tags-l))))
  13120. (setq p (point))
  13121. (insert (make-string (- ncol (current-column)) ?\ ))
  13122. (setq ncol (current-column))
  13123. (when indent-tabs-mode (tabify p (point-at-eol)))
  13124. (org-move-to-column (min ncol col) t))
  13125. (goto-char pos))))
  13126. (defun org-set-tags-command (&optional arg just-align)
  13127. "Call the set-tags command for the current entry."
  13128. (interactive "P")
  13129. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13130. (org-set-tags arg just-align)
  13131. (save-excursion
  13132. (unless (and (org-region-active-p)
  13133. org-loop-over-headlines-in-active-region)
  13134. (org-back-to-heading t))
  13135. (org-set-tags arg just-align))))
  13136. (defun org-set-tags-to (data)
  13137. "Set the tags of the current entry to DATA, replacing the current tags.
  13138. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13139. If DATA is nil or the empty string, any tags will be removed."
  13140. (interactive "sTags: ")
  13141. (setq data
  13142. (cond
  13143. ((eq data nil) "")
  13144. ((equal data "") "")
  13145. ((stringp data)
  13146. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13147. ":"))
  13148. ((listp data)
  13149. (concat ":" (mapconcat 'identity data ":") ":"))))
  13150. (when data
  13151. (save-excursion
  13152. (org-back-to-heading t)
  13153. (when (looking-at org-complex-heading-regexp)
  13154. (if (match-end 5)
  13155. (progn
  13156. (goto-char (match-beginning 5))
  13157. (insert data)
  13158. (delete-region (point) (point-at-eol))
  13159. (org-set-tags nil 'align))
  13160. (goto-char (point-at-eol))
  13161. (insert " " data)
  13162. (org-set-tags nil 'align)))
  13163. (beginning-of-line 1)
  13164. (if (looking-at ".*?\\([ \t]+\\)$")
  13165. (delete-region (match-beginning 1) (match-end 1))))))
  13166. (defun org-align-all-tags ()
  13167. "Align the tags i all headings."
  13168. (interactive)
  13169. (save-excursion
  13170. (or (ignore-errors (org-back-to-heading t))
  13171. (outline-next-heading))
  13172. (if (org-at-heading-p)
  13173. (org-set-tags t)
  13174. (message "No headings"))))
  13175. (defvar org-indent-indentation-per-level)
  13176. (defun org-set-tags (&optional arg just-align)
  13177. "Set the tags for the current headline.
  13178. With prefix ARG, realign all tags in headings in the current buffer."
  13179. (interactive "P")
  13180. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13181. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13182. 'region-start-level 'region))
  13183. org-loop-over-headlines-in-active-region)
  13184. (org-map-entries
  13185. ;; We don't use ARG and JUST-ALIGN here these args are not
  13186. ;; useful when looping over headlines
  13187. `(org-set-tags)
  13188. org-loop-over-headlines-in-active-region
  13189. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13190. (let* ((re org-outline-regexp-bol)
  13191. (current (unless arg (org-get-tags-string)))
  13192. (col (current-column))
  13193. (org-setting-tags t)
  13194. table current-tags inherited-tags ; computed below when needed
  13195. tags p0 c0 c1 rpl di tc level)
  13196. (if arg
  13197. (save-excursion
  13198. (goto-char (point-min))
  13199. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13200. (while (re-search-forward re nil t)
  13201. (org-set-tags nil t)
  13202. (end-of-line 1)))
  13203. (message "All tags realigned to column %d" org-tags-column))
  13204. (if just-align
  13205. (setq tags current)
  13206. ;; Get a new set of tags from the user
  13207. (save-excursion
  13208. (setq table (append org-tag-persistent-alist
  13209. (or org-tag-alist (org-get-buffer-tags))
  13210. (and
  13211. org-complete-tags-always-offer-all-agenda-tags
  13212. (org-global-tags-completion-table
  13213. (org-agenda-files))))
  13214. org-last-tags-completion-table table
  13215. current-tags (org-split-string current ":")
  13216. inherited-tags (nreverse
  13217. (nthcdr (length current-tags)
  13218. (nreverse (org-get-tags-at))))
  13219. tags
  13220. (if (or (eq t org-use-fast-tag-selection)
  13221. (and org-use-fast-tag-selection
  13222. (delq nil (mapcar 'cdr table))))
  13223. (org-fast-tag-selection
  13224. current-tags inherited-tags table
  13225. (if org-fast-tag-selection-include-todo
  13226. org-todo-key-alist))
  13227. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13228. (org-trim
  13229. (org-icompleting-read "Tags: "
  13230. 'org-tags-completion-function
  13231. nil nil current 'org-tags-history))))))
  13232. (while (string-match "[-+&]+" tags)
  13233. ;; No boolean logic, just a list
  13234. (setq tags (replace-match ":" t t tags))))
  13235. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13236. (if org-tags-sort-function
  13237. (setq tags (mapconcat 'identity
  13238. (sort (org-split-string
  13239. tags (org-re "[^[:alnum:]_@#%]+"))
  13240. org-tags-sort-function) ":")))
  13241. (if (string-match "\\`[\t ]*\\'" tags)
  13242. (setq tags "")
  13243. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13244. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13245. ;; Insert new tags at the correct column
  13246. (beginning-of-line 1)
  13247. (setq level (or (and (looking-at org-outline-regexp)
  13248. (- (match-end 0) (point) 1))
  13249. 1))
  13250. (cond
  13251. ((and (equal current "") (equal tags "")))
  13252. ((re-search-forward
  13253. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13254. (point-at-eol) t)
  13255. (if (equal tags "")
  13256. (setq rpl "")
  13257. (goto-char (match-beginning 0))
  13258. (setq c0 (current-column)
  13259. ;; compute offset for the case of org-indent-mode active
  13260. di (if org-indent-mode
  13261. (* (1- org-indent-indentation-per-level) (1- level))
  13262. 0)
  13263. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13264. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13265. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13266. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13267. (replace-match rpl t t)
  13268. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13269. tags)
  13270. (t (error "Tags alignment failed")))
  13271. (org-move-to-column col)
  13272. (unless just-align
  13273. (run-hooks 'org-after-tags-change-hook))))))
  13274. (defun org-change-tag-in-region (beg end tag off)
  13275. "Add or remove TAG for each entry in the region.
  13276. This works in the agenda, and also in an org-mode buffer."
  13277. (interactive
  13278. (list (region-beginning) (region-end)
  13279. (let ((org-last-tags-completion-table
  13280. (if (derived-mode-p 'org-mode)
  13281. (org-uniquify
  13282. (delq nil (append (org-get-buffer-tags)
  13283. (org-global-tags-completion-table))))
  13284. (org-global-tags-completion-table))))
  13285. (org-icompleting-read
  13286. "Tag: " 'org-tags-completion-function nil nil nil
  13287. 'org-tags-history))
  13288. (progn
  13289. (message "[s]et or [r]emove? ")
  13290. (equal (read-char-exclusive) ?r))))
  13291. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13292. (let ((agendap (equal major-mode 'org-agenda-mode))
  13293. l1 l2 m buf pos newhead (cnt 0))
  13294. (goto-char end)
  13295. (setq l2 (1- (org-current-line)))
  13296. (goto-char beg)
  13297. (setq l1 (org-current-line))
  13298. (loop for l from l1 to l2 do
  13299. (org-goto-line l)
  13300. (setq m (get-text-property (point) 'org-hd-marker))
  13301. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13302. (and agendap m))
  13303. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13304. pos (if agendap m (point)))
  13305. (with-current-buffer buf
  13306. (save-excursion
  13307. (save-restriction
  13308. (goto-char pos)
  13309. (setq cnt (1+ cnt))
  13310. (org-toggle-tag tag (if off 'off 'on))
  13311. (setq newhead (org-get-heading)))))
  13312. (and agendap (org-agenda-change-all-lines newhead m))))
  13313. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13314. (defun org-tags-completion-function (string predicate &optional flag)
  13315. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13316. (confirm (lambda (x) (stringp (car x)))))
  13317. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13318. (setq s1 (match-string 1 string)
  13319. s2 (match-string 2 string))
  13320. (setq s1 "" s2 string))
  13321. (cond
  13322. ((eq flag nil)
  13323. ;; try completion
  13324. (setq rtn (try-completion s2 ctable confirm))
  13325. (if (stringp rtn)
  13326. (setq rtn
  13327. (concat s1 s2 (substring rtn (length s2))
  13328. (if (and org-add-colon-after-tag-completion
  13329. (assoc rtn ctable))
  13330. ":" ""))))
  13331. rtn)
  13332. ((eq flag t)
  13333. ;; all-completions
  13334. (all-completions s2 ctable confirm))
  13335. ((eq flag 'lambda)
  13336. ;; exact match?
  13337. (assoc s2 ctable)))))
  13338. (defun org-fast-tag-insert (kwd tags face &optional end)
  13339. "Insert KDW, and the TAGS, the latter with face FACE.
  13340. Also insert END."
  13341. (insert (format "%-12s" (concat kwd ":"))
  13342. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13343. (or end "")))
  13344. (defun org-fast-tag-show-exit (flag)
  13345. (save-excursion
  13346. (org-goto-line 3)
  13347. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13348. (replace-match ""))
  13349. (when flag
  13350. (end-of-line 1)
  13351. (org-move-to-column (- (window-width) 19) t)
  13352. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13353. (defun org-set-current-tags-overlay (current prefix)
  13354. "Add an overlay to CURRENT tag with PREFIX."
  13355. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13356. (if (featurep 'xemacs)
  13357. (org-overlay-display org-tags-overlay (concat prefix s)
  13358. 'secondary-selection)
  13359. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13360. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13361. (defvar org-last-tag-selection-key nil)
  13362. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13363. "Fast tag selection with single keys.
  13364. CURRENT is the current list of tags in the headline, INHERITED is the
  13365. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13366. possibly with grouping information. TODO-TABLE is a similar table with
  13367. TODO keywords, should these have keys assigned to them.
  13368. If the keys are nil, a-z are automatically assigned.
  13369. Returns the new tags string, or nil to not change the current settings."
  13370. (let* ((fulltable (append table todo-table))
  13371. (maxlen (apply 'max (mapcar
  13372. (lambda (x)
  13373. (if (stringp (car x)) (string-width (car x)) 0))
  13374. fulltable)))
  13375. (buf (current-buffer))
  13376. (expert (eq org-fast-tag-selection-single-key 'expert))
  13377. (buffer-tags nil)
  13378. (fwidth (+ maxlen 3 1 3))
  13379. (ncol (/ (- (window-width) 4) fwidth))
  13380. (i-face 'org-done)
  13381. (c-face 'org-todo)
  13382. tg cnt e c char c1 c2 ntable tbl rtn
  13383. ov-start ov-end ov-prefix
  13384. (exit-after-next org-fast-tag-selection-single-key)
  13385. (done-keywords org-done-keywords)
  13386. groups ingroup)
  13387. (save-excursion
  13388. (beginning-of-line 1)
  13389. (if (looking-at
  13390. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13391. (setq ov-start (match-beginning 1)
  13392. ov-end (match-end 1)
  13393. ov-prefix "")
  13394. (setq ov-start (1- (point-at-eol))
  13395. ov-end (1+ ov-start))
  13396. (skip-chars-forward "^\n\r")
  13397. (setq ov-prefix
  13398. (concat
  13399. (buffer-substring (1- (point)) (point))
  13400. (if (> (current-column) org-tags-column)
  13401. " "
  13402. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13403. (move-overlay org-tags-overlay ov-start ov-end)
  13404. (save-window-excursion
  13405. (if expert
  13406. (set-buffer (get-buffer-create " *Org tags*"))
  13407. (delete-other-windows)
  13408. (split-window-vertically)
  13409. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13410. (erase-buffer)
  13411. (org-set-local 'org-done-keywords done-keywords)
  13412. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13413. (org-fast-tag-insert "Current" current c-face "\n\n")
  13414. (org-fast-tag-show-exit exit-after-next)
  13415. (org-set-current-tags-overlay current ov-prefix)
  13416. (setq tbl fulltable char ?a cnt 0)
  13417. (while (setq e (pop tbl))
  13418. (cond
  13419. ((equal (car e) :startgroup)
  13420. (push '() groups) (setq ingroup t)
  13421. (when (not (= cnt 0))
  13422. (setq cnt 0)
  13423. (insert "\n"))
  13424. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13425. ((equal (car e) :endgroup)
  13426. (setq ingroup nil cnt 0)
  13427. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13428. ((equal e '(:newline))
  13429. (when (not (= cnt 0))
  13430. (setq cnt 0)
  13431. (insert "\n")
  13432. (setq e (car tbl))
  13433. (while (equal (car tbl) '(:newline))
  13434. (insert "\n")
  13435. (setq tbl (cdr tbl)))))
  13436. ((equal e '(:grouptags)) nil)
  13437. (t
  13438. (setq tg (copy-sequence (car e)) c2 nil)
  13439. (if (cdr e)
  13440. (setq c (cdr e))
  13441. ;; automatically assign a character.
  13442. (setq c1 (string-to-char
  13443. (downcase (substring
  13444. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13445. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13446. (while (or (rassoc char ntable) (rassoc char table))
  13447. (setq char (1+ char)))
  13448. (setq c2 c1))
  13449. (setq c (or c2 char)))
  13450. (if ingroup (push tg (car groups)))
  13451. (setq tg (org-add-props tg nil 'face
  13452. (cond
  13453. ((not (assoc tg table))
  13454. (org-get-todo-face tg))
  13455. ((member tg current) c-face)
  13456. ((member tg inherited) i-face))))
  13457. (if (equal (caar tbl) :grouptags)
  13458. (org-add-props tg nil 'face 'org-tag-group))
  13459. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13460. (insert "[" c "] " tg (make-string
  13461. (- fwidth 4 (length tg)) ?\ ))
  13462. (push (cons tg c) ntable)
  13463. (when (= (setq cnt (1+ cnt)) ncol)
  13464. (insert "\n")
  13465. (if ingroup (insert " "))
  13466. (setq cnt 0)))))
  13467. (setq ntable (nreverse ntable))
  13468. (insert "\n")
  13469. (goto-char (point-min))
  13470. (if (not expert) (org-fit-window-to-buffer))
  13471. (setq rtn
  13472. (catch 'exit
  13473. (while t
  13474. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13475. (if (not groups) "no " "")
  13476. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13477. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13478. (setq org-last-tag-selection-key c)
  13479. (cond
  13480. ((= c ?\r) (throw 'exit t))
  13481. ((= c ?!)
  13482. (setq groups (not groups))
  13483. (goto-char (point-min))
  13484. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13485. ((= c ?\C-c)
  13486. (if (not expert)
  13487. (org-fast-tag-show-exit
  13488. (setq exit-after-next (not exit-after-next)))
  13489. (setq expert nil)
  13490. (delete-other-windows)
  13491. (set-window-buffer (split-window-vertically) " *Org tags*")
  13492. (org-switch-to-buffer-other-window " *Org tags*")
  13493. (org-fit-window-to-buffer)))
  13494. ((or (= c ?\C-g)
  13495. (and (= c ?q) (not (rassoc c ntable))))
  13496. (org-detach-overlay org-tags-overlay)
  13497. (setq quit-flag t))
  13498. ((= c ?\ )
  13499. (setq current nil)
  13500. (if exit-after-next (setq exit-after-next 'now)))
  13501. ((= c ?\t)
  13502. (condition-case nil
  13503. (setq tg (org-icompleting-read
  13504. "Tag: "
  13505. (or buffer-tags
  13506. (with-current-buffer buf
  13507. (org-get-buffer-tags)))))
  13508. (quit (setq tg "")))
  13509. (when (string-match "\\S-" tg)
  13510. (add-to-list 'buffer-tags (list tg))
  13511. (if (member tg current)
  13512. (setq current (delete tg current))
  13513. (push tg current)))
  13514. (if exit-after-next (setq exit-after-next 'now)))
  13515. ((setq e (rassoc c todo-table) tg (car e))
  13516. (with-current-buffer buf
  13517. (save-excursion (org-todo tg)))
  13518. (if exit-after-next (setq exit-after-next 'now)))
  13519. ((setq e (rassoc c ntable) tg (car e))
  13520. (if (member tg current)
  13521. (setq current (delete tg current))
  13522. (loop for g in groups do
  13523. (if (member tg g)
  13524. (mapc (lambda (x)
  13525. (setq current (delete x current)))
  13526. g)))
  13527. (push tg current))
  13528. (if exit-after-next (setq exit-after-next 'now))))
  13529. ;; Create a sorted list
  13530. (setq current
  13531. (sort current
  13532. (lambda (a b)
  13533. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13534. (if (eq exit-after-next 'now) (throw 'exit t))
  13535. (goto-char (point-min))
  13536. (beginning-of-line 2)
  13537. (delete-region (point) (point-at-eol))
  13538. (org-fast-tag-insert "Current" current c-face)
  13539. (org-set-current-tags-overlay current ov-prefix)
  13540. (while (re-search-forward
  13541. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13542. (setq tg (match-string 1))
  13543. (add-text-properties
  13544. (match-beginning 1) (match-end 1)
  13545. (list 'face
  13546. (cond
  13547. ((member tg current) c-face)
  13548. ((member tg inherited) i-face)
  13549. (t (get-text-property (match-beginning 1) 'face))))))
  13550. (goto-char (point-min)))))
  13551. (org-detach-overlay org-tags-overlay)
  13552. (if rtn
  13553. (mapconcat 'identity current ":")
  13554. nil))))
  13555. (defun org-get-tags-string ()
  13556. "Get the TAGS string in the current headline."
  13557. (unless (org-at-heading-p t)
  13558. (user-error "Not on a heading"))
  13559. (save-excursion
  13560. (beginning-of-line 1)
  13561. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13562. (org-match-string-no-properties 1)
  13563. "")))
  13564. (defun org-get-tags ()
  13565. "Get the list of tags specified in the current headline."
  13566. (org-split-string (org-get-tags-string) ":"))
  13567. (defun org-get-buffer-tags ()
  13568. "Get a table of all tags used in the buffer, for completion."
  13569. (let (tags)
  13570. (save-excursion
  13571. (goto-char (point-min))
  13572. (while (re-search-forward
  13573. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13574. (when (equal (char-after (point-at-bol 0)) ?*)
  13575. (mapc (lambda (x) (add-to-list 'tags x))
  13576. (org-split-string (org-match-string-no-properties 1) ":")))))
  13577. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13578. (mapcar 'list tags)))
  13579. ;;;; The mapping API
  13580. (defun org-map-entries (func &optional match scope &rest skip)
  13581. "Call FUNC at each headline selected by MATCH in SCOPE.
  13582. FUNC is a function or a lisp form. The function will be called without
  13583. arguments, with the cursor positioned at the beginning of the headline.
  13584. The return values of all calls to the function will be collected and
  13585. returned as a list.
  13586. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13587. does not need to preserve point. After evaluation, the cursor will be
  13588. moved to the end of the line (presumably of the headline of the
  13589. processed entry) and search continues from there. Under some
  13590. circumstances, this may not produce the wanted results. For example,
  13591. if you have removed (e.g. archived) the current (sub)tree it could
  13592. mean that the next entry will be skipped entirely. In such cases, you
  13593. can specify the position from where search should continue by making
  13594. FUNC set the variable `org-map-continue-from' to the desired buffer
  13595. position.
  13596. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13597. Only headlines that are matched by this query will be considered during
  13598. the iteration. When MATCH is nil or t, all headlines will be
  13599. visited by the iteration.
  13600. SCOPE determines the scope of this command. It can be any of:
  13601. nil The current buffer, respecting the restriction if any
  13602. tree The subtree started with the entry at point
  13603. region The entries within the active region, if any
  13604. region-start-level
  13605. The entries within the active region, but only those at
  13606. the same level than the first one.
  13607. file The current buffer, without restriction
  13608. file-with-archives
  13609. The current buffer, and any archives associated with it
  13610. agenda All agenda files
  13611. agenda-with-archives
  13612. All agenda files with any archive files associated with them
  13613. \(file1 file2 ...)
  13614. If this is a list, all files in the list will be scanned
  13615. The remaining args are treated as settings for the skipping facilities of
  13616. the scanner. The following items can be given here:
  13617. archive skip trees with the archive tag
  13618. comment skip trees with the COMMENT keyword
  13619. function or Emacs Lisp form:
  13620. will be used as value for `org-agenda-skip-function', so
  13621. whenever the function returns a position, FUNC will not be
  13622. called for that entry and search will continue from the
  13623. position returned
  13624. If your function needs to retrieve the tags including inherited tags
  13625. at the *current* entry, you can use the value of the variable
  13626. `org-scanner-tags' which will be much faster than getting the value
  13627. with `org-get-tags-at'. If your function gets properties with
  13628. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13629. to t around the call to `org-entry-properties' to get the same speedup.
  13630. Note that if your function moves around to retrieve tags and properties at
  13631. a *different* entry, you cannot use these techniques."
  13632. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13633. (not (org-region-active-p)))
  13634. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13635. (org-agenda-skip-archived-trees (memq 'archive skip))
  13636. (org-agenda-skip-comment-trees (memq 'comment skip))
  13637. (org-agenda-skip-function
  13638. (car (org-delete-all '(comment archive) skip)))
  13639. (org-tags-match-list-sublevels t)
  13640. (start-level (eq scope 'region-start-level))
  13641. matcher file res
  13642. org-todo-keywords-for-agenda
  13643. org-done-keywords-for-agenda
  13644. org-todo-keyword-alist-for-agenda
  13645. org-drawers-for-agenda
  13646. org-tag-alist-for-agenda
  13647. todo-only)
  13648. (cond
  13649. ((eq match t) (setq matcher t))
  13650. ((eq match nil) (setq matcher t))
  13651. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13652. (save-window-excursion
  13653. (save-restriction
  13654. (cond ((eq scope 'tree)
  13655. (org-back-to-heading t)
  13656. (org-narrow-to-subtree)
  13657. (setq scope nil))
  13658. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13659. (org-region-active-p))
  13660. ;; If needed, set start-level to a string like "2"
  13661. (when start-level
  13662. (save-excursion
  13663. (goto-char (region-beginning))
  13664. (unless (org-at-heading-p) (outline-next-heading))
  13665. (setq start-level (org-current-level))))
  13666. (narrow-to-region (region-beginning)
  13667. (save-excursion
  13668. (goto-char (region-end))
  13669. (unless (and (bolp) (org-at-heading-p))
  13670. (outline-next-heading))
  13671. (point)))
  13672. (setq scope nil)))
  13673. (if (not scope)
  13674. (progn
  13675. (org-agenda-prepare-buffers
  13676. (list (buffer-file-name (current-buffer))))
  13677. (setq res (org-scan-tags func matcher todo-only start-level)))
  13678. ;; Get the right scope
  13679. (cond
  13680. ((and scope (listp scope) (symbolp (car scope)))
  13681. (setq scope (eval scope)))
  13682. ((eq scope 'agenda)
  13683. (setq scope (org-agenda-files t)))
  13684. ((eq scope 'agenda-with-archives)
  13685. (setq scope (org-agenda-files t))
  13686. (setq scope (org-add-archive-files scope)))
  13687. ((eq scope 'file)
  13688. (setq scope (list (buffer-file-name))))
  13689. ((eq scope 'file-with-archives)
  13690. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13691. (org-agenda-prepare-buffers scope)
  13692. (while (setq file (pop scope))
  13693. (with-current-buffer (org-find-base-buffer-visiting file)
  13694. (save-excursion
  13695. (save-restriction
  13696. (widen)
  13697. (goto-char (point-min))
  13698. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13699. res)))
  13700. ;;;; Properties
  13701. ;;; Setting and retrieving properties
  13702. (defconst org-special-properties
  13703. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13704. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13705. "The special properties valid in Org-mode.
  13706. These are properties that are not defined in the property drawer,
  13707. but in some other way.")
  13708. (defconst org-default-properties
  13709. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13710. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13711. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13712. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13713. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13714. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13715. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13716. "Some properties that are used by Org-mode for various purposes.
  13717. Being in this list makes sure that they are offered for completion.")
  13718. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13719. "Regular expression matching the first line of a property drawer.")
  13720. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13721. "Regular expression matching the last line of a property drawer.")
  13722. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13723. "Regular expression matching the first line of a property drawer.")
  13724. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13725. "Regular expression matching the first line of a property drawer.")
  13726. (defconst org-property-drawer-re
  13727. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13728. org-property-end-re "\\)\n?")
  13729. "Matches an entire property drawer.")
  13730. (defconst org-clock-drawer-re
  13731. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13732. org-property-end-re "\\)\n?")
  13733. "Matches an entire clock drawer.")
  13734. (defun org-property-action ()
  13735. "Do an action on properties."
  13736. (interactive)
  13737. (let (c)
  13738. (org-at-property-p)
  13739. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13740. (setq c (read-char-exclusive))
  13741. (cond
  13742. ((equal c ?s)
  13743. (call-interactively 'org-set-property))
  13744. ((equal c ?d)
  13745. (call-interactively 'org-delete-property))
  13746. ((equal c ?D)
  13747. (call-interactively 'org-delete-property-globally))
  13748. ((equal c ?c)
  13749. (call-interactively 'org-compute-property-at-point))
  13750. (t (user-error "No such property action %c" c)))))
  13751. (defun org-inc-effort ()
  13752. "Increment the value of the effort property in the current entry."
  13753. (interactive)
  13754. (org-set-effort nil t))
  13755. (defvar org-clock-effort) ;; Defined in org-clock.el
  13756. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13757. (defun org-set-effort (&optional value increment)
  13758. "Set the effort property of the current entry.
  13759. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13760. 10th allowed value.
  13761. When INCREMENT is non-nil, set the property to the next allowed value."
  13762. (interactive "P")
  13763. (if (equal value 0) (setq value 10))
  13764. (let* ((completion-ignore-case t)
  13765. (prop org-effort-property)
  13766. (cur (org-entry-get nil prop))
  13767. (allowed (org-property-get-allowed-values nil prop 'table))
  13768. (existing (mapcar 'list (org-property-values prop)))
  13769. (heading (nth 4 (org-heading-components)))
  13770. rpl
  13771. (val (cond
  13772. ((stringp value) value)
  13773. ((and allowed (integerp value))
  13774. (or (car (nth (1- value) allowed))
  13775. (car (org-last allowed))))
  13776. ((and allowed increment)
  13777. (or (caadr (member (list cur) allowed))
  13778. (user-error "Allowed effort values are not set")))
  13779. (allowed
  13780. (message "Select 1-9,0, [RET%s]: %s"
  13781. (if cur (concat "=" cur) "")
  13782. (mapconcat 'car allowed " "))
  13783. (setq rpl (read-char-exclusive))
  13784. (if (equal rpl ?\r)
  13785. cur
  13786. (setq rpl (- rpl ?0))
  13787. (if (equal rpl 0) (setq rpl 10))
  13788. (if (and (> rpl 0) (<= rpl (length allowed)))
  13789. (car (nth (1- rpl) allowed))
  13790. (org-completing-read "Effort: " allowed nil))))
  13791. (t
  13792. (let (org-completion-use-ido org-completion-use-iswitchb)
  13793. (org-completing-read
  13794. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13795. (concat "[" cur "]") "")
  13796. ": ")
  13797. existing nil nil "" nil cur))))))
  13798. (unless (equal (org-entry-get nil prop) val)
  13799. (org-entry-put nil prop val))
  13800. (save-excursion
  13801. (org-back-to-heading t)
  13802. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13803. (when (string= heading org-clock-current-task)
  13804. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13805. (org-clock-update-mode-line))
  13806. (message "%s is now %s" prop val)))
  13807. (defun org-at-property-p ()
  13808. "Is cursor inside a property drawer?"
  13809. (save-excursion
  13810. (when (equal 'node-property (car (org-element-at-point)))
  13811. (beginning-of-line 1)
  13812. (looking-at org-property-re))))
  13813. (defun org-get-property-block (&optional beg end force)
  13814. "Return the (beg . end) range of the body of the property drawer.
  13815. BEG and END are the beginning and end of the current subtree, or of
  13816. the part before the first headline. If they are not given, they will
  13817. be found. If the drawer does not exist and FORCE is non-nil, create
  13818. the drawer."
  13819. (catch 'exit
  13820. (save-excursion
  13821. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13822. (progn (org-back-to-heading t) (point))))
  13823. (end (or end (and (not (outline-next-heading)) (point-max))
  13824. (point))))
  13825. (goto-char beg)
  13826. (if (re-search-forward org-property-start-re end t)
  13827. (setq beg (1+ (match-end 0)))
  13828. (if force
  13829. (save-excursion
  13830. (org-insert-property-drawer)
  13831. (setq end (progn (outline-next-heading) (point))))
  13832. (throw 'exit nil))
  13833. (goto-char beg)
  13834. (if (re-search-forward org-property-start-re end t)
  13835. (setq beg (1+ (match-end 0)))))
  13836. (if (re-search-forward org-property-end-re end t)
  13837. (setq end (match-beginning 0))
  13838. (or force (throw 'exit nil))
  13839. (goto-char beg)
  13840. (setq end beg)
  13841. (org-indent-line)
  13842. (insert ":END:\n"))
  13843. (cons beg end)))))
  13844. (defun org-entry-properties (&optional pom which specific)
  13845. "Get all properties of the entry at point-or-marker POM.
  13846. This includes the TODO keyword, the tags, time strings for deadline,
  13847. scheduled, and clocking, and any additional properties defined in the
  13848. entry. The return value is an alist, keys may occur multiple times
  13849. if the property key was used several times.
  13850. POM may also be nil, in which case the current entry is used.
  13851. If WHICH is nil or `all', get all properties. If WHICH is
  13852. `special' or `standard', only get that subclass. If WHICH
  13853. is a string only get exactly this property. SPECIFIC can be a string, the
  13854. specific property we are interested in. Specifying it can speed
  13855. things up because then unnecessary parsing is avoided."
  13856. (setq which (or which 'all))
  13857. (org-with-point-at pom
  13858. (let ((clockstr (substring org-clock-string 0 -1))
  13859. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13860. (case-fold-search nil)
  13861. beg end range props sum-props key key1 value string clocksum clocksumt)
  13862. (save-excursion
  13863. (when (condition-case nil
  13864. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13865. (error nil))
  13866. (setq beg (point))
  13867. (setq sum-props (get-text-property (point) 'org-summaries))
  13868. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13869. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13870. (outline-next-heading)
  13871. (setq end (point))
  13872. (when (memq which '(all special))
  13873. ;; Get the special properties, like TODO and tags
  13874. (goto-char beg)
  13875. (when (and (or (not specific) (string= specific "TODO"))
  13876. (looking-at org-todo-line-regexp) (match-end 2))
  13877. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13878. (when (and (or (not specific) (string= specific "PRIORITY"))
  13879. (looking-at org-priority-regexp))
  13880. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13881. (when (or (not specific) (string= specific "FILE"))
  13882. (push (cons "FILE" buffer-file-name) props))
  13883. (when (and (or (not specific) (string= specific "TAGS"))
  13884. (setq value (org-get-tags-string))
  13885. (string-match "\\S-" value))
  13886. (push (cons "TAGS" value) props))
  13887. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13888. (setq value (org-get-tags-at)))
  13889. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13890. ":"))
  13891. props))
  13892. (when (or (not specific) (string= specific "BLOCKED"))
  13893. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13894. (when (or (not specific)
  13895. (member specific
  13896. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13897. "TIMESTAMP" "TIMESTAMP_IA")))
  13898. (catch 'match
  13899. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13900. (setq key (if (match-end 1)
  13901. (substring (org-match-string-no-properties 1)
  13902. 0 -1))
  13903. string (if (equal key clockstr)
  13904. (org-trim
  13905. (buffer-substring-no-properties
  13906. (match-beginning 3) (goto-char
  13907. (point-at-eol))))
  13908. (substring (org-match-string-no-properties 3)
  13909. 1 -1)))
  13910. ;; Get the correct property name from the key. This is
  13911. ;; necessary if the user has configured time keywords.
  13912. (setq key1 (concat key ":"))
  13913. (cond
  13914. ((not key)
  13915. (setq key
  13916. (if (= (char-after (match-beginning 3)) ?\[)
  13917. "TIMESTAMP_IA" "TIMESTAMP")))
  13918. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13919. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13920. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13921. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13922. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13923. (progn
  13924. (push (cons key string) props)
  13925. ;; no need to search further if match is found
  13926. (throw 'match t))
  13927. (when (or (equal key "CLOCK") (not (assoc key props)))
  13928. (push (cons key string) props)))))))
  13929. (when (memq which '(all standard))
  13930. ;; Get the standard properties, like :PROP: ...
  13931. (setq range (org-get-property-block beg end))
  13932. (when range
  13933. (goto-char (car range))
  13934. (while (re-search-forward org-property-re
  13935. (cdr range) t)
  13936. (setq key (org-match-string-no-properties 2)
  13937. value (org-trim (or (org-match-string-no-properties 3) "")))
  13938. (unless (member key excluded)
  13939. (push (cons key (or value "")) props)))))
  13940. (if clocksum
  13941. (push (cons "CLOCKSUM"
  13942. (org-columns-number-to-string (/ (float clocksum) 60.)
  13943. 'add_times))
  13944. props))
  13945. (if clocksumt
  13946. (push (cons "CLOCKSUM_T"
  13947. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13948. 'add_times))
  13949. props))
  13950. (unless (assoc "CATEGORY" props)
  13951. (push (cons "CATEGORY" (org-get-category)) props))
  13952. (append sum-props (nreverse props)))))))
  13953. (defun org-entry-get (pom property &optional inherit literal-nil)
  13954. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13955. If INHERIT is non-nil and the entry does not have the property,
  13956. then also check higher levels of the hierarchy.
  13957. If INHERIT is the symbol `selective', use inheritance only if the setting
  13958. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13959. If the property is present but empty, the return value is the empty string.
  13960. If the property is not present at all, nil is returned.
  13961. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13962. do not interpret it as the list atom nil. This is used for inheritance
  13963. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13964. (org-with-point-at pom
  13965. (if (and inherit (if (eq inherit 'selective)
  13966. (org-property-inherit-p property)
  13967. t))
  13968. (org-entry-get-with-inheritance property literal-nil)
  13969. (if (member property org-special-properties)
  13970. ;; We need a special property. Use `org-entry-properties' to
  13971. ;; retrieve it, but specify the wanted property
  13972. (cdr (assoc property (org-entry-properties nil 'special property)))
  13973. (let ((range (org-get-property-block)))
  13974. (when (and range (not (eq (car range) (cdr range))))
  13975. (let* ((props (list (or (assoc property org-file-properties)
  13976. (assoc property org-global-properties)
  13977. (assoc property org-global-properties-fixed))))
  13978. (ap (lambda (key)
  13979. (when (re-search-forward
  13980. (org-re-property key) (cdr range) t)
  13981. (setq props
  13982. (org-update-property-plist
  13983. key
  13984. (if (match-end 3)
  13985. (org-match-string-no-properties 3) "")
  13986. props)))))
  13987. val)
  13988. (goto-char (car range))
  13989. (funcall ap property)
  13990. (goto-char (car range))
  13991. (while (funcall ap (concat property "+")))
  13992. (setq val (cdr (assoc property props)))
  13993. (when val (if literal-nil val (org-not-nil val))))))))))
  13994. (defun org-property-or-variable-value (var &optional inherit)
  13995. "Check if there is a property fixing the value of VAR.
  13996. If yes, return this value. If not, return the current value of the variable."
  13997. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13998. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13999. (read prop)
  14000. (symbol-value var))))
  14001. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14002. "Delete the property PROPERTY from entry at point-or-marker POM.
  14003. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14004. an empty drawer to delete."
  14005. (org-with-point-at pom
  14006. (if (member property org-special-properties)
  14007. nil ; cannot delete these properties.
  14008. (let ((range (org-get-property-block)))
  14009. (if (and range
  14010. (goto-char (car range))
  14011. (re-search-forward
  14012. (org-re-property property)
  14013. (cdr range) t))
  14014. (progn
  14015. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14016. (and delete-empty-drawer
  14017. (org-remove-empty-drawer-at
  14018. delete-empty-drawer (car range)))
  14019. t)
  14020. nil)))))
  14021. ;; Multi-values properties are properties that contain multiple values
  14022. ;; These values are assumed to be single words, separated by whitespace.
  14023. (defun org-entry-add-to-multivalued-property (pom property value)
  14024. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14025. (let* ((old (org-entry-get pom property))
  14026. (values (and old (org-split-string old "[ \t]"))))
  14027. (setq value (org-entry-protect-space value))
  14028. (unless (member value values)
  14029. (setq values (append values (list value)))
  14030. (org-entry-put pom property
  14031. (mapconcat 'identity values " ")))))
  14032. (defun org-entry-remove-from-multivalued-property (pom property value)
  14033. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14034. (let* ((old (org-entry-get pom property))
  14035. (values (and old (org-split-string old "[ \t]"))))
  14036. (setq value (org-entry-protect-space value))
  14037. (when (member value values)
  14038. (setq values (delete value values))
  14039. (org-entry-put pom property
  14040. (mapconcat 'identity values " ")))))
  14041. (defun org-entry-member-in-multivalued-property (pom property value)
  14042. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14043. (let* ((old (org-entry-get pom property))
  14044. (values (and old (org-split-string old "[ \t]"))))
  14045. (setq value (org-entry-protect-space value))
  14046. (member value values)))
  14047. (defun org-entry-get-multivalued-property (pom property)
  14048. "Return a list of values in a multivalued property."
  14049. (let* ((value (org-entry-get pom property))
  14050. (values (and value (org-split-string value "[ \t]"))))
  14051. (mapcar 'org-entry-restore-space values)))
  14052. (defun org-entry-put-multivalued-property (pom property &rest values)
  14053. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14054. VALUES should be a list of strings. Spaces will be protected."
  14055. (org-entry-put pom property
  14056. (mapconcat 'org-entry-protect-space values " "))
  14057. (let* ((value (org-entry-get pom property))
  14058. (values (and value (org-split-string value "[ \t]"))))
  14059. (mapcar 'org-entry-restore-space values)))
  14060. (defun org-entry-protect-space (s)
  14061. "Protect spaces and newline in string S."
  14062. (while (string-match " " s)
  14063. (setq s (replace-match "%20" t t s)))
  14064. (while (string-match "\n" s)
  14065. (setq s (replace-match "%0A" t t s)))
  14066. s)
  14067. (defun org-entry-restore-space (s)
  14068. "Restore spaces and newline in string S."
  14069. (while (string-match "%20" s)
  14070. (setq s (replace-match " " t t s)))
  14071. (while (string-match "%0A" s)
  14072. (setq s (replace-match "\n" t t s)))
  14073. s)
  14074. (defvar org-entry-property-inherited-from (make-marker)
  14075. "Marker pointing to the entry from where a property was inherited.
  14076. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14077. location of the entry where the inheritance search matched. If there was
  14078. no match, the marker will point nowhere.
  14079. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14080. is set.")
  14081. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14082. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14083. The search will stop at the first ancestor which has the property defined.
  14084. If the value found is \"nil\", return nil to show that the property
  14085. should be considered as undefined (this is the meaning of nil here).
  14086. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14087. (move-marker org-entry-property-inherited-from nil)
  14088. (let (tmp)
  14089. (save-excursion
  14090. (save-restriction
  14091. (widen)
  14092. (catch 'ex
  14093. (while t
  14094. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14095. (or (ignore-errors (org-back-to-heading t))
  14096. (goto-char (point-min)))
  14097. (move-marker org-entry-property-inherited-from (point))
  14098. (throw 'ex tmp))
  14099. (or (ignore-errors (org-up-heading-safe))
  14100. (throw 'ex nil))))))
  14101. (setq tmp (or tmp
  14102. (cdr (assoc property org-file-properties))
  14103. (cdr (assoc property org-global-properties))
  14104. (cdr (assoc property org-global-properties-fixed))))
  14105. (if literal-nil tmp (org-not-nil tmp))))
  14106. (defvar org-property-changed-functions nil
  14107. "Hook called when the value of a property has changed.
  14108. Each hook function should accept two arguments, the name of the property
  14109. and the new value.")
  14110. (defun org-entry-put (pom property value)
  14111. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14112. (org-with-point-at pom
  14113. (org-back-to-heading t)
  14114. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14115. range)
  14116. (cond
  14117. ((equal property "TODO")
  14118. (when (and (stringp value) (string-match "\\S-" value)
  14119. (not (member value org-todo-keywords-1)))
  14120. (user-error "\"%s\" is not a valid TODO state" value))
  14121. (if (or (not value)
  14122. (not (string-match "\\S-" value)))
  14123. (setq value 'none))
  14124. (org-todo value)
  14125. (org-set-tags nil 'align))
  14126. ((equal property "PRIORITY")
  14127. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14128. (string-to-char value) ?\ ))
  14129. (org-set-tags nil 'align))
  14130. ((equal property "CLOCKSUM")
  14131. (if (not (re-search-forward
  14132. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14133. (error "Cannot find a clock log")
  14134. (goto-char (- (match-end 1) 2))
  14135. (cond
  14136. ((eq value 'earlier) (org-timestamp-down))
  14137. ((eq value 'later) (org-timestamp-up)))
  14138. (org-clock-sum-current-item)))
  14139. ((equal property "SCHEDULED")
  14140. (if (re-search-forward org-scheduled-time-regexp end t)
  14141. (cond
  14142. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14143. ((eq value 'later) (org-timestamp-change 1 'day))
  14144. (t (call-interactively 'org-schedule)))
  14145. (call-interactively 'org-schedule)))
  14146. ((equal property "DEADLINE")
  14147. (if (re-search-forward org-deadline-time-regexp end t)
  14148. (cond
  14149. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14150. ((eq value 'later) (org-timestamp-change 1 'day))
  14151. (t (call-interactively 'org-deadline)))
  14152. (call-interactively 'org-deadline)))
  14153. ((member property org-special-properties)
  14154. (error "The %s property can not yet be set with `org-entry-put'"
  14155. property))
  14156. (t ; a non-special property
  14157. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14158. (setq range (org-get-property-block beg end 'force))
  14159. (goto-char (car range))
  14160. (if (re-search-forward
  14161. (org-re-property property) (cdr range) t)
  14162. (progn
  14163. (delete-region (match-beginning 0) (match-end 0))
  14164. (goto-char (match-beginning 0)))
  14165. (goto-char (cdr range))
  14166. (insert "\n")
  14167. (backward-char 1)
  14168. (org-indent-line))
  14169. (insert ":" property ":")
  14170. (and value (insert " " value))
  14171. (org-indent-line)))))
  14172. (run-hook-with-args 'org-property-changed-functions property value)))
  14173. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14174. "Get all property keys in the current buffer.
  14175. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14176. like tags and TODO state.
  14177. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14178. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14179. and others.
  14180. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14181. formats in the current buffer."
  14182. (let (rtn range cfmt s p)
  14183. (save-excursion
  14184. (save-restriction
  14185. (widen)
  14186. (goto-char (point-min))
  14187. (while (re-search-forward org-property-start-re nil t)
  14188. (setq range (org-get-property-block))
  14189. (goto-char (car range))
  14190. (while (re-search-forward org-property-re
  14191. (cdr range) t)
  14192. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14193. (outline-next-heading))))
  14194. (when include-specials
  14195. (setq rtn (append org-special-properties rtn)))
  14196. (when include-defaults
  14197. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14198. (add-to-list 'rtn org-effort-property))
  14199. (when include-columns
  14200. (save-excursion
  14201. (save-restriction
  14202. (widen)
  14203. (goto-char (point-min))
  14204. (while (re-search-forward
  14205. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14206. nil t)
  14207. (setq cfmt (match-string 2) s 0)
  14208. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14209. cfmt s)
  14210. (setq s (match-end 0)
  14211. p (match-string 1 cfmt))
  14212. (unless (or (equal p "ITEM")
  14213. (member p org-special-properties))
  14214. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14215. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14216. (defun org-property-values (key)
  14217. "Return a list of all values of property KEY in the current buffer."
  14218. (save-excursion
  14219. (save-restriction
  14220. (widen)
  14221. (goto-char (point-min))
  14222. (let ((re (org-re-property key))
  14223. values)
  14224. (while (re-search-forward re nil t)
  14225. (add-to-list 'values (org-trim (match-string 3))))
  14226. (delete "" values)))))
  14227. (defun org-insert-property-drawer ()
  14228. "Insert a property drawer into the current entry."
  14229. (org-back-to-heading t)
  14230. (looking-at org-outline-regexp)
  14231. (let ((indent (if org-adapt-indentation
  14232. (- (match-end 0) (match-beginning 0))
  14233. 0))
  14234. (beg (point))
  14235. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14236. end hiddenp)
  14237. (outline-next-heading)
  14238. (setq end (point))
  14239. (goto-char beg)
  14240. (while (re-search-forward re end t))
  14241. (setq hiddenp (outline-invisible-p))
  14242. (end-of-line 1)
  14243. (and (equal (char-after) ?\n) (forward-char 1))
  14244. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14245. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14246. ;; just skip this line
  14247. (beginning-of-line 2)
  14248. ;; Drawer start, find the end
  14249. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14250. (beginning-of-line 1)))
  14251. (org-skip-over-state-notes)
  14252. (skip-chars-backward " \t\n\r")
  14253. (if (eq (char-before) ?*) (forward-char 1))
  14254. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14255. (beginning-of-line 0)
  14256. (org-indent-to-column indent)
  14257. (beginning-of-line 2)
  14258. (org-indent-to-column indent)
  14259. (beginning-of-line 0)
  14260. (if hiddenp
  14261. (save-excursion
  14262. (org-back-to-heading t)
  14263. (hide-entry))
  14264. (org-flag-drawer t))))
  14265. (defun org-insert-drawer (&optional arg drawer)
  14266. "Insert a drawer at point.
  14267. Optional argument DRAWER, when non-nil, is a string representing
  14268. drawer's name. Otherwise, the user is prompted for a name.
  14269. If a region is active, insert the drawer around that region
  14270. instead.
  14271. Point is left between drawer's boundaries."
  14272. (interactive "P")
  14273. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14274. "LOGBOOK"))
  14275. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14276. ;; internally by Org. They are meant to be inserted
  14277. ;; automatically.
  14278. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14279. ;; Remove system drawers from list. Note: For some reason,
  14280. ;; `org-completing-read' ignores the predicate while
  14281. ;; `completing-read' handles it fine.
  14282. (drawer (if arg "PROPERTIES"
  14283. (or drawer
  14284. (completing-read
  14285. "Drawer: " org-drawers
  14286. (lambda (d) (not (member d system-drawers))))))))
  14287. (cond
  14288. ;; With C-u, fall back on `org-insert-property-drawer'
  14289. (arg (org-insert-property-drawer))
  14290. ;; With an active region, insert a drawer at point.
  14291. ((not (org-region-active-p))
  14292. (progn
  14293. (unless (bolp) (insert "\n"))
  14294. (insert (format ":%s:\n\n:END:\n" drawer))
  14295. (forward-line -2)))
  14296. ;; Otherwise, insert the drawer at point
  14297. (t
  14298. (let ((rbeg (region-beginning))
  14299. (rend (copy-marker (region-end))))
  14300. (unwind-protect
  14301. (progn
  14302. (goto-char rbeg)
  14303. (beginning-of-line)
  14304. (when (save-excursion
  14305. (re-search-forward org-outline-regexp-bol rend t))
  14306. (user-error "Drawers cannot contain headlines"))
  14307. ;; Position point at the beginning of the first
  14308. ;; non-blank line in region. Insert drawer's opening
  14309. ;; there, then indent it.
  14310. (org-skip-whitespace)
  14311. (beginning-of-line)
  14312. (insert ":" drawer ":\n")
  14313. (forward-line -1)
  14314. (indent-for-tab-command)
  14315. ;; Move point to the beginning of the first blank line
  14316. ;; after the last non-blank line in region. Insert
  14317. ;; drawer's closing, then indent it.
  14318. (goto-char rend)
  14319. (skip-chars-backward " \r\t\n")
  14320. (insert "\n:END:")
  14321. (deactivate-mark t)
  14322. (indent-for-tab-command)
  14323. (unless (eolp) (insert "\n")))
  14324. ;; Clear marker, whatever the outcome of insertion is.
  14325. (set-marker rend nil)))))))
  14326. (defvar org-property-set-functions-alist nil
  14327. "Property set function alist.
  14328. Each entry should have the following format:
  14329. (PROPERTY . READ-FUNCTION)
  14330. The read function will be called with the same argument as
  14331. `org-completing-read'.")
  14332. (defun org-set-property-function (property)
  14333. "Get the function that should be used to set PROPERTY.
  14334. This is computed according to `org-property-set-functions-alist'."
  14335. (or (cdr (assoc property org-property-set-functions-alist))
  14336. 'org-completing-read))
  14337. (defun org-read-property-value (property)
  14338. "Read PROPERTY value from user."
  14339. (let* ((completion-ignore-case t)
  14340. (allowed (org-property-get-allowed-values nil property 'table))
  14341. (cur (org-entry-get nil property))
  14342. (prompt (concat property " value"
  14343. (if (and cur (string-match "\\S-" cur))
  14344. (concat " [" cur "]") "") ": "))
  14345. (set-function (org-set-property-function property))
  14346. (val (if allowed
  14347. (funcall set-function prompt allowed nil
  14348. (not (get-text-property 0 'org-unrestricted
  14349. (caar allowed))))
  14350. (let (org-completion-use-ido org-completion-use-iswitchb)
  14351. (funcall set-function prompt
  14352. (mapcar 'list (org-property-values property))
  14353. nil nil "" nil cur)))))
  14354. (if (equal val "")
  14355. cur
  14356. val)))
  14357. (defvar org-last-set-property nil)
  14358. (defvar org-last-set-property-value nil)
  14359. (defun org-read-property-name ()
  14360. "Read a property name."
  14361. (let* ((completion-ignore-case t)
  14362. (keys (org-buffer-property-keys nil t t))
  14363. (default-prop (or (save-excursion
  14364. (save-match-data
  14365. (beginning-of-line)
  14366. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14367. (null (string= (match-string 1) "END"))
  14368. (match-string 1))))
  14369. org-last-set-property))
  14370. (property (org-icompleting-read
  14371. (concat "Property"
  14372. (if default-prop (concat " [" default-prop "]") "")
  14373. ": ")
  14374. (mapcar 'list keys)
  14375. nil nil nil nil
  14376. default-prop)))
  14377. (if (member property keys)
  14378. property
  14379. (or (cdr (assoc (downcase property)
  14380. (mapcar (lambda (x) (cons (downcase x) x))
  14381. keys)))
  14382. property))))
  14383. (defun org-set-property-and-value (use-last)
  14384. "Allow to set [PROPERTY]: [value] direction from prompt.
  14385. When use-default, don't even ask, just use the last
  14386. \"[PROPERTY]: [value]\" string from the history."
  14387. (interactive "P")
  14388. (let* ((completion-ignore-case t)
  14389. (pv (or (and use-last org-last-set-property-value)
  14390. (org-completing-read
  14391. "Enter a \"[Property]: [value]\" pair: "
  14392. nil nil nil nil nil
  14393. org-last-set-property-value)))
  14394. prop val)
  14395. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14396. (setq prop (match-string 1 pv)
  14397. val (match-string 2 pv))
  14398. (org-set-property prop val))))
  14399. (defun org-set-property (property value)
  14400. "In the current entry, set PROPERTY to VALUE.
  14401. When called interactively, this will prompt for a property name, offering
  14402. completion on existing and default properties. And then it will prompt
  14403. for a value, offering completion either on allowed values (via an inherited
  14404. xxx_ALL property) or on existing values in other instances of this property
  14405. in the current file."
  14406. (interactive (list nil nil))
  14407. (let* ((property (or property (org-read-property-name)))
  14408. (value (or value (org-read-property-value property)))
  14409. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14410. (setq org-last-set-property property)
  14411. (setq org-last-set-property-value (concat property ": " value))
  14412. ;; Possibly postprocess the inserted value:
  14413. (when fn (setq value (funcall fn value)))
  14414. (unless (equal (org-entry-get nil property) value)
  14415. (org-entry-put nil property value))))
  14416. (defun org-delete-property (property &optional delete-empty-drawer)
  14417. "In the current entry, delete PROPERTY.
  14418. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14419. an empty drawer to delete."
  14420. (interactive
  14421. (let* ((completion-ignore-case t)
  14422. (prop (org-icompleting-read "Property: "
  14423. (org-entry-properties nil 'standard))))
  14424. (list prop)))
  14425. (message "Property %s %s" property
  14426. (if (org-entry-delete nil property delete-empty-drawer)
  14427. "deleted"
  14428. "was not present in the entry")))
  14429. (defun org-delete-property-globally (property)
  14430. "Remove PROPERTY globally, from all entries."
  14431. (interactive
  14432. (let* ((completion-ignore-case t)
  14433. (prop (org-icompleting-read
  14434. "Globally remove property: "
  14435. (mapcar 'list (org-buffer-property-keys)))))
  14436. (list prop)))
  14437. (save-excursion
  14438. (save-restriction
  14439. (widen)
  14440. (goto-char (point-min))
  14441. (let ((cnt 0))
  14442. (while (re-search-forward
  14443. (org-re-property property)
  14444. nil t)
  14445. (setq cnt (1+ cnt))
  14446. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14447. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14448. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14449. (defun org-compute-property-at-point ()
  14450. "Compute the property at point.
  14451. This looks for an enclosing column format, extracts the operator and
  14452. then applies it to the property in the column format's scope."
  14453. (interactive)
  14454. (unless (org-at-property-p)
  14455. (user-error "Not at a property"))
  14456. (let ((prop (org-match-string-no-properties 2)))
  14457. (org-columns-get-format-and-top-level)
  14458. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14459. (user-error "No operator defined for property %s" prop))
  14460. (org-columns-compute prop)))
  14461. (defvar org-property-allowed-value-functions nil
  14462. "Hook for functions supplying allowed values for a specific property.
  14463. The functions must take a single argument, the name of the property, and
  14464. return a flat list of allowed values. If \":ETC\" is one of
  14465. the values, this means that these values are intended as defaults for
  14466. completion, but that other values should be allowed too.
  14467. The functions must return nil if they are not responsible for this
  14468. property.")
  14469. (defun org-property-get-allowed-values (pom property &optional table)
  14470. "Get allowed values for the property PROPERTY.
  14471. When TABLE is non-nil, return an alist that can directly be used for
  14472. completion."
  14473. (let (vals)
  14474. (cond
  14475. ((equal property "TODO")
  14476. (setq vals (org-with-point-at pom
  14477. (append org-todo-keywords-1 '("")))))
  14478. ((equal property "PRIORITY")
  14479. (let ((n org-lowest-priority))
  14480. (while (>= n org-highest-priority)
  14481. (push (char-to-string n) vals)
  14482. (setq n (1- n)))))
  14483. ((member property org-special-properties))
  14484. ((setq vals (run-hook-with-args-until-success
  14485. 'org-property-allowed-value-functions property)))
  14486. (t
  14487. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14488. (when (and vals (string-match "\\S-" vals))
  14489. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14490. (setq vals (mapcar (lambda (x)
  14491. (cond ((stringp x) x)
  14492. ((numberp x) (number-to-string x))
  14493. ((symbolp x) (symbol-name x))
  14494. (t "???")))
  14495. vals)))))
  14496. (when (member ":ETC" vals)
  14497. (setq vals (remove ":ETC" vals))
  14498. (org-add-props (car vals) '(org-unrestricted t)))
  14499. (if table (mapcar 'list vals) vals)))
  14500. (defun org-property-previous-allowed-value (&optional previous)
  14501. "Switch to the next allowed value for this property."
  14502. (interactive)
  14503. (org-property-next-allowed-value t))
  14504. (defun org-property-next-allowed-value (&optional previous)
  14505. "Switch to the next allowed value for this property."
  14506. (interactive)
  14507. (unless (org-at-property-p)
  14508. (user-error "Not at a property"))
  14509. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14510. (key (match-string 2))
  14511. (value (match-string 3))
  14512. (allowed (or (org-property-get-allowed-values (point) key)
  14513. (and (member value '("[ ]" "[-]" "[X]"))
  14514. '("[ ]" "[X]"))))
  14515. (heading (save-match-data (nth 4 (org-heading-components))))
  14516. nval)
  14517. (unless allowed
  14518. (user-error "Allowed values for this property have not been defined"))
  14519. (if previous (setq allowed (reverse allowed)))
  14520. (if (member value allowed)
  14521. (setq nval (car (cdr (member value allowed)))))
  14522. (setq nval (or nval (car allowed)))
  14523. (if (equal nval value)
  14524. (user-error "Only one allowed value for this property"))
  14525. (org-at-property-p)
  14526. (replace-match (concat " :" key ": " nval) t t)
  14527. (org-indent-line)
  14528. (beginning-of-line 1)
  14529. (skip-chars-forward " \t")
  14530. (when (equal prop org-effort-property)
  14531. (save-excursion
  14532. (org-back-to-heading t)
  14533. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14534. (when (string= org-clock-current-task heading)
  14535. (setq org-clock-effort nval)
  14536. (org-clock-update-mode-line)))
  14537. (run-hook-with-args 'org-property-changed-functions key nval)))
  14538. (defun org-find-olp (path &optional this-buffer)
  14539. "Return a marker pointing to the entry at outline path OLP.
  14540. If anything goes wrong, throw an error.
  14541. You can wrap this call to catch the error like this:
  14542. (condition-case msg
  14543. (org-mobile-locate-entry (match-string 4))
  14544. (error (nth 1 msg)))
  14545. The return value will then be either a string with the error message,
  14546. or a marker if everything is OK.
  14547. If THIS-BUFFER is set, the outline path does not contain a file,
  14548. only headings."
  14549. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14550. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14551. (level 1)
  14552. (lmin 1)
  14553. (lmax 1)
  14554. limit re end found pos heading cnt flevel)
  14555. (unless buffer (error "File not found :%s" file))
  14556. (with-current-buffer buffer
  14557. (save-excursion
  14558. (save-restriction
  14559. (widen)
  14560. (setq limit (point-max))
  14561. (goto-char (point-min))
  14562. (while (setq heading (pop path))
  14563. (setq re (format org-complex-heading-regexp-format
  14564. (regexp-quote heading)))
  14565. (setq cnt 0 pos (point))
  14566. (while (re-search-forward re end t)
  14567. (setq level (- (match-end 1) (match-beginning 1)))
  14568. (if (and (>= level lmin) (<= level lmax))
  14569. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14570. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14571. lmax heading))
  14572. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14573. lmax heading))
  14574. (goto-char found)
  14575. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14576. (setq end (save-excursion (org-end-of-subtree t t))))
  14577. (when (org-at-heading-p)
  14578. (point-marker)))))))
  14579. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14580. "Find node HEADING in BUFFER.
  14581. Return a marker to the heading if it was found, or nil if not.
  14582. If POS-ONLY is set, return just the position instead of a marker.
  14583. The heading text must match exact, but it may have a TODO keyword,
  14584. a priority cookie and tags in the standard locations."
  14585. (with-current-buffer (or buffer (current-buffer))
  14586. (save-excursion
  14587. (save-restriction
  14588. (widen)
  14589. (goto-char (point-min))
  14590. (let (case-fold-search)
  14591. (if (re-search-forward
  14592. (format org-complex-heading-regexp-format
  14593. (regexp-quote heading)) nil t)
  14594. (if pos-only
  14595. (match-beginning 0)
  14596. (move-marker (make-marker) (match-beginning 0)))))))))
  14597. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14598. "Find Org node headline HEADING in all .org files in directory DIR.
  14599. When the target headline is found, return a marker to this location."
  14600. (let ((files (directory-files (or dir default-directory)
  14601. nil "\\`[^.#].*\\.org\\'"))
  14602. file visiting m buffer)
  14603. (catch 'found
  14604. (while (setq file (pop files))
  14605. (message "trying %s" file)
  14606. (setq visiting (org-find-base-buffer-visiting file))
  14607. (setq buffer (or visiting (find-file-noselect file)))
  14608. (setq m (org-find-exact-headline-in-buffer
  14609. heading buffer))
  14610. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14611. (and m (throw 'found m))))))
  14612. (defun org-find-entry-with-id (ident)
  14613. "Locate the entry that contains the ID property with exact value IDENT.
  14614. IDENT can be a string, a symbol or a number, this function will search for
  14615. the string representation of it.
  14616. Return the position where this entry starts, or nil if there is no such entry."
  14617. (interactive "sID: ")
  14618. (let ((id (cond
  14619. ((stringp ident) ident)
  14620. ((symbol-name ident) (symbol-name ident))
  14621. ((numberp ident) (number-to-string ident))
  14622. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14623. (case-fold-search nil))
  14624. (save-excursion
  14625. (save-restriction
  14626. (widen)
  14627. (goto-char (point-min))
  14628. (when (re-search-forward
  14629. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14630. nil t)
  14631. (org-back-to-heading t)
  14632. (point))))))
  14633. ;;;; Timestamps
  14634. (defvar org-last-changed-timestamp nil)
  14635. (defvar org-last-inserted-timestamp nil
  14636. "The last time stamp inserted with `org-insert-time-stamp'.")
  14637. (defvar org-time-was-given) ; dynamically scoped parameter
  14638. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14639. (defvar org-ts-what) ; dynamically scoped parameter
  14640. (defun org-time-stamp (arg &optional inactive)
  14641. "Prompt for a date/time and insert a time stamp.
  14642. If the user specifies a time like HH:MM or if this command is
  14643. called with at least one prefix argument, the time stamp contains
  14644. the date and the time. Otherwise, only the date is be included.
  14645. All parts of a date not specified by the user is filled in from
  14646. the current date/time. So if you just press return without
  14647. typing anything, the time stamp will represent the current
  14648. date/time.
  14649. If there is already a timestamp at the cursor, it will be
  14650. modified.
  14651. With two universal prefix arguments, insert an active timestamp
  14652. with the current time without prompting the user."
  14653. (interactive "P")
  14654. (let* ((ts nil)
  14655. (default-time
  14656. ;; Default time is either today, or, when entering a range,
  14657. ;; the range start.
  14658. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14659. (save-excursion
  14660. (re-search-backward
  14661. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14662. (- (point) 20) t)))
  14663. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14664. (current-time)))
  14665. (default-input (and ts (org-get-compact-tod ts)))
  14666. (repeater (save-excursion
  14667. (save-match-data
  14668. (beginning-of-line)
  14669. (when (re-search-forward
  14670. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14671. (save-excursion (progn (end-of-line) (point))) t)
  14672. (match-string 0)))))
  14673. org-time-was-given org-end-time-was-given time)
  14674. (cond
  14675. ((and (org-at-timestamp-p t)
  14676. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14677. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14678. (insert "--")
  14679. (setq time (let ((this-command this-command))
  14680. (org-read-date arg 'totime nil nil
  14681. default-time default-input inactive)))
  14682. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14683. ((org-at-timestamp-p t)
  14684. (setq time (let ((this-command this-command))
  14685. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14686. (when (org-at-timestamp-p t) ; just to get the match data
  14687. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14688. (replace-match "")
  14689. (setq org-last-changed-timestamp
  14690. (org-insert-time-stamp
  14691. time (or org-time-was-given arg)
  14692. inactive nil nil (list org-end-time-was-given)))
  14693. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14694. (setq org-last-changed-timestamp
  14695. (concat (substring org-last-inserted-timestamp 0 -1)
  14696. " " repeater ">"))))
  14697. (message "Timestamp updated"))
  14698. ((equal arg '(16))
  14699. (org-insert-time-stamp (current-time) t))
  14700. (t
  14701. (setq time (let ((this-command this-command))
  14702. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14703. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14704. nil nil (list org-end-time-was-given))))))
  14705. ;; FIXME: can we use this for something else, like computing time differences?
  14706. (defun org-get-compact-tod (s)
  14707. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14708. (let* ((t1 (match-string 1 s))
  14709. (h1 (string-to-number (match-string 2 s)))
  14710. (m1 (string-to-number (match-string 3 s)))
  14711. (t2 (and (match-end 4) (match-string 5 s)))
  14712. (h2 (and t2 (string-to-number (match-string 6 s))))
  14713. (m2 (and t2 (string-to-number (match-string 7 s))))
  14714. dh dm)
  14715. (if (not t2)
  14716. t1
  14717. (setq dh (- h2 h1) dm (- m2 m1))
  14718. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14719. (concat t1 "+" (number-to-string dh)
  14720. (and (/= 0 dm) (format ":%02d" dm)))))))
  14721. (defun org-time-stamp-inactive (&optional arg)
  14722. "Insert an inactive time stamp.
  14723. An inactive time stamp is enclosed in square brackets instead of angle
  14724. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14725. does not link to the calendar and cannot be changed with the S-cursor keys.
  14726. So these are more for recording a certain time/date."
  14727. (interactive "P")
  14728. (org-time-stamp arg 'inactive))
  14729. (defvar org-date-ovl (make-overlay 1 1))
  14730. (overlay-put org-date-ovl 'face 'org-date-selected)
  14731. (org-detach-overlay org-date-ovl)
  14732. (defvar org-ans1) ; dynamically scoped parameter
  14733. (defvar org-ans2) ; dynamically scoped parameter
  14734. (defvar org-plain-time-of-day-regexp) ; defined below
  14735. (defvar org-overriding-default-time nil) ; dynamically scoped
  14736. (defvar org-read-date-overlay nil)
  14737. (defvar org-dcst nil) ; dynamically scoped
  14738. (defvar org-read-date-history nil)
  14739. (defvar org-read-date-final-answer nil)
  14740. (defvar org-read-date-analyze-futurep nil)
  14741. (defvar org-read-date-analyze-forced-year nil)
  14742. (defvar org-read-date-inactive)
  14743. (defvar org-read-date-minibuffer-local-map
  14744. (let ((map (make-sparse-keymap)))
  14745. (set-keymap-parent map minibuffer-local-map)
  14746. (org-defkey map (kbd ".")
  14747. (lambda () (interactive)
  14748. ;; Are we at the beginning of the prompt?
  14749. (if (looking-back "^[^:]+: ")
  14750. (org-eval-in-calendar '(calendar-goto-today))
  14751. (insert "."))))
  14752. (org-defkey map (kbd "C-.")
  14753. (lambda () (interactive)
  14754. (org-eval-in-calendar '(calendar-goto-today))))
  14755. (org-defkey map [(meta shift left)]
  14756. (lambda () (interactive)
  14757. (org-eval-in-calendar '(calendar-backward-month 1))))
  14758. (org-defkey map [(meta shift right)]
  14759. (lambda () (interactive)
  14760. (org-eval-in-calendar '(calendar-forward-month 1))))
  14761. (org-defkey map [(meta shift up)]
  14762. (lambda () (interactive)
  14763. (org-eval-in-calendar '(calendar-backward-year 1))))
  14764. (org-defkey map [(meta shift down)]
  14765. (lambda () (interactive)
  14766. (org-eval-in-calendar '(calendar-forward-year 1))))
  14767. (org-defkey map [?\e (shift left)]
  14768. (lambda () (interactive)
  14769. (org-eval-in-calendar '(calendar-backward-month 1))))
  14770. (org-defkey map [?\e (shift right)]
  14771. (lambda () (interactive)
  14772. (org-eval-in-calendar '(calendar-forward-month 1))))
  14773. (org-defkey map [?\e (shift up)]
  14774. (lambda () (interactive)
  14775. (org-eval-in-calendar '(calendar-backward-year 1))))
  14776. (org-defkey map [?\e (shift down)]
  14777. (lambda () (interactive)
  14778. (org-eval-in-calendar '(calendar-forward-year 1))))
  14779. (org-defkey map [(shift up)]
  14780. (lambda () (interactive)
  14781. (org-eval-in-calendar '(calendar-backward-week 1))))
  14782. (org-defkey map [(shift down)]
  14783. (lambda () (interactive)
  14784. (org-eval-in-calendar '(calendar-forward-week 1))))
  14785. (org-defkey map [(shift left)]
  14786. (lambda () (interactive)
  14787. (org-eval-in-calendar '(calendar-backward-day 1))))
  14788. (org-defkey map [(shift right)]
  14789. (lambda () (interactive)
  14790. (org-eval-in-calendar '(calendar-forward-day 1))))
  14791. (org-defkey map "!"
  14792. (lambda () (interactive)
  14793. (org-eval-in-calendar '(diary-view-entries))
  14794. (message "")))
  14795. (org-defkey map ">"
  14796. (lambda () (interactive)
  14797. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14798. (org-defkey map "<"
  14799. (lambda () (interactive)
  14800. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14801. (org-defkey map "\C-v"
  14802. (lambda () (interactive)
  14803. (org-eval-in-calendar
  14804. '(calendar-scroll-left-three-months 1))))
  14805. (org-defkey map "\M-v"
  14806. (lambda () (interactive)
  14807. (org-eval-in-calendar
  14808. '(calendar-scroll-right-three-months 1))))
  14809. map)
  14810. "Keymap for minibuffer commands when using `org-read-date'.")
  14811. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14812. default-time default-input inactive)
  14813. "Read a date, possibly a time, and make things smooth for the user.
  14814. The prompt will suggest to enter an ISO date, but you can also enter anything
  14815. which will at least partially be understood by `parse-time-string'.
  14816. Unrecognized parts of the date will default to the current day, month, year,
  14817. hour and minute. If this command is called to replace a timestamp at point,
  14818. or to enter the second timestamp of a range, the default time is taken
  14819. from the existing stamp. Furthermore, the command prefers the future,
  14820. so if you are giving a date where the year is not given, and the day-month
  14821. combination is already past in the current year, it will assume you
  14822. mean next year. For details, see the manual. A few examples:
  14823. 3-2-5 --> 2003-02-05
  14824. feb 15 --> currentyear-02-15
  14825. 2/15 --> currentyear-02-15
  14826. sep 12 9 --> 2009-09-12
  14827. 12:45 --> today 12:45
  14828. 22 sept 0:34 --> currentyear-09-22 0:34
  14829. 12 --> currentyear-currentmonth-12
  14830. Fri --> nearest Friday after today
  14831. -Tue --> last Tuesday
  14832. etc.
  14833. Furthermore you can specify a relative date by giving, as the *first* thing
  14834. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14835. change in days weeks, months, years.
  14836. With a single plus or minus, the date is relative to today. With a double
  14837. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14838. +4d --> four days from today
  14839. +4 --> same as above
  14840. +2w --> two weeks from today
  14841. ++5 --> five days from default date
  14842. The function understands only English month and weekday abbreviations.
  14843. While prompting, a calendar is popped up - you can also select the
  14844. date with the mouse (button 1). The calendar shows a period of three
  14845. months. To scroll it to other months, use the keys `>' and `<'.
  14846. If you don't like the calendar, turn it off with
  14847. \(setq org-read-date-popup-calendar nil)
  14848. With optional argument TO-TIME, the date will immediately be converted
  14849. to an internal time.
  14850. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14851. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14852. still enter a time, and this function will inform the calling routine
  14853. about this change. The calling routine may then choose to change the
  14854. format used to insert the time stamp into the buffer to include the time.
  14855. With optional argument FROM-STRING, read from this string instead from
  14856. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14857. the time/date that is used for everything that is not specified by the
  14858. user."
  14859. (require 'parse-time)
  14860. (let* ((org-time-stamp-rounding-minutes
  14861. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14862. (org-dcst org-display-custom-times)
  14863. (ct (org-current-time))
  14864. (org-def (or org-overriding-default-time default-time ct))
  14865. (org-defdecode (decode-time org-def))
  14866. (dummy (progn
  14867. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14868. (setcar (nthcdr 2 org-defdecode) -1)
  14869. (setcar (nthcdr 1 org-defdecode) 59)
  14870. (setq org-def (apply 'encode-time org-defdecode)
  14871. org-defdecode (decode-time org-def)))))
  14872. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14873. (calendar-frame-setup nil)
  14874. (calendar-setup nil)
  14875. (calendar-move-hook nil)
  14876. (calendar-view-diary-initially-flag nil)
  14877. (calendar-view-holidays-initially-flag nil)
  14878. (timestr (format-time-string
  14879. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14880. (prompt (concat (if prompt (concat prompt " ") "")
  14881. (format "Date+time [%s]: " timestr)))
  14882. ans (org-ans0 "") org-ans1 org-ans2 final)
  14883. (cond
  14884. (from-string (setq ans from-string))
  14885. (org-read-date-popup-calendar
  14886. (save-excursion
  14887. (save-window-excursion
  14888. (calendar)
  14889. (org-eval-in-calendar '(setq cursor-type nil) t)
  14890. (unwind-protect
  14891. (progn
  14892. (calendar-forward-day (- (time-to-days org-def)
  14893. (calendar-absolute-from-gregorian
  14894. (calendar-current-date))))
  14895. (org-eval-in-calendar nil t)
  14896. (let* ((old-map (current-local-map))
  14897. (map (copy-keymap calendar-mode-map))
  14898. (minibuffer-local-map
  14899. (copy-keymap org-read-date-minibuffer-local-map)))
  14900. (org-defkey map (kbd "RET") 'org-calendar-select)
  14901. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14902. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14903. (unwind-protect
  14904. (progn
  14905. (use-local-map map)
  14906. (setq org-read-date-inactive inactive)
  14907. (add-hook 'post-command-hook 'org-read-date-display)
  14908. (setq org-ans0 (read-string prompt default-input
  14909. 'org-read-date-history nil))
  14910. ;; org-ans0: from prompt
  14911. ;; org-ans1: from mouse click
  14912. ;; org-ans2: from calendar motion
  14913. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14914. (remove-hook 'post-command-hook 'org-read-date-display)
  14915. (use-local-map old-map)
  14916. (when org-read-date-overlay
  14917. (delete-overlay org-read-date-overlay)
  14918. (setq org-read-date-overlay nil)))))
  14919. (bury-buffer "*Calendar*")))))
  14920. (t ; Naked prompt only
  14921. (unwind-protect
  14922. (setq ans (read-string prompt default-input
  14923. 'org-read-date-history timestr))
  14924. (when org-read-date-overlay
  14925. (delete-overlay org-read-date-overlay)
  14926. (setq org-read-date-overlay nil)))))
  14927. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14928. (when org-read-date-analyze-forced-year
  14929. (message "Year was forced into %s"
  14930. (if org-read-date-force-compatible-dates
  14931. "compatible range (1970-2037)"
  14932. "range representable on this machine"))
  14933. (ding))
  14934. ;; One round trip to get rid of 34th of August and stuff like that....
  14935. (setq final (decode-time (apply 'encode-time final)))
  14936. (setq org-read-date-final-answer ans)
  14937. (if to-time
  14938. (apply 'encode-time final)
  14939. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14940. (format "%04d-%02d-%02d %02d:%02d"
  14941. (nth 5 final) (nth 4 final) (nth 3 final)
  14942. (nth 2 final) (nth 1 final))
  14943. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14944. (defvar org-def)
  14945. (defvar org-defdecode)
  14946. (defvar org-with-time)
  14947. (defun org-read-date-display ()
  14948. "Display the current date prompt interpretation in the minibuffer."
  14949. (when org-read-date-display-live
  14950. (when org-read-date-overlay
  14951. (delete-overlay org-read-date-overlay))
  14952. (when (minibufferp (current-buffer))
  14953. (save-excursion
  14954. (end-of-line 1)
  14955. (while (not (equal (buffer-substring
  14956. (max (point-min) (- (point) 4)) (point))
  14957. " "))
  14958. (insert " ")))
  14959. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14960. " " (or org-ans1 org-ans2)))
  14961. (org-end-time-was-given nil)
  14962. (f (org-read-date-analyze ans org-def org-defdecode))
  14963. (fmts (if org-dcst
  14964. org-time-stamp-custom-formats
  14965. org-time-stamp-formats))
  14966. (fmt (if (or org-with-time
  14967. (and (boundp 'org-time-was-given) org-time-was-given))
  14968. (cdr fmts)
  14969. (car fmts)))
  14970. (txt (format-time-string fmt (apply 'encode-time f)))
  14971. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14972. (txt (concat "=> " txt)))
  14973. (when (and org-end-time-was-given
  14974. (string-match org-plain-time-of-day-regexp txt))
  14975. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14976. org-end-time-was-given
  14977. (substring txt (match-end 0)))))
  14978. (when org-read-date-analyze-futurep
  14979. (setq txt (concat txt " (=>F)")))
  14980. (setq org-read-date-overlay
  14981. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14982. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14983. (defun org-read-date-analyze (ans org-def org-defdecode)
  14984. "Analyze the combined answer of the date prompt."
  14985. ;; FIXME: cleanup and comment
  14986. (let ((nowdecode (decode-time (current-time)))
  14987. delta deltan deltaw deltadef year month day
  14988. hour minute second wday pm h2 m2 tl wday1
  14989. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14990. (setq org-read-date-analyze-futurep nil
  14991. org-read-date-analyze-forced-year nil)
  14992. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14993. (setq ans "+0"))
  14994. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14995. (setq ans (replace-match "" t t ans)
  14996. deltan (car delta)
  14997. deltaw (nth 1 delta)
  14998. deltadef (nth 2 delta)))
  14999. ;; Check if there is an iso week date in there. If yes, store the
  15000. ;; info and postpone interpreting it until the rest of the parsing
  15001. ;; is done.
  15002. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15003. (setq iso-year (if (match-end 1)
  15004. (org-small-year-to-year
  15005. (string-to-number (match-string 1 ans))))
  15006. iso-weekday (if (match-end 3)
  15007. (string-to-number (match-string 3 ans)))
  15008. iso-week (string-to-number (match-string 2 ans)))
  15009. (setq ans (replace-match "" t t ans)))
  15010. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15011. (when (string-match
  15012. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15013. (setq year (if (match-end 2)
  15014. (string-to-number (match-string 2 ans))
  15015. (progn (setq kill-year t)
  15016. (string-to-number (format-time-string "%Y"))))
  15017. month (string-to-number (match-string 3 ans))
  15018. day (string-to-number (match-string 4 ans)))
  15019. (if (< year 100) (setq year (+ 2000 year)))
  15020. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15021. t nil ans)))
  15022. ;; Help matching dotted european dates
  15023. (when (string-match
  15024. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15025. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15026. (setq kill-year t)
  15027. (string-to-number (format-time-string "%Y")))
  15028. day (string-to-number (match-string 1 ans))
  15029. month (string-to-number (match-string 2 ans))
  15030. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15031. t nil ans)))
  15032. ;; Help matching american dates, like 5/30 or 5/30/7
  15033. (when (string-match
  15034. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15035. (setq year (if (match-end 4)
  15036. (string-to-number (match-string 4 ans))
  15037. (progn (setq kill-year t)
  15038. (string-to-number (format-time-string "%Y"))))
  15039. month (string-to-number (match-string 1 ans))
  15040. day (string-to-number (match-string 2 ans)))
  15041. (if (< year 100) (setq year (+ 2000 year)))
  15042. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15043. t nil ans)))
  15044. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15045. ;; If there is a time with am/pm, and *no* time without it, we convert
  15046. ;; so that matching will be successful.
  15047. (loop for i from 1 to 2 do ; twice, for end time as well
  15048. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15049. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15050. (setq hour (string-to-number (match-string 1 ans))
  15051. minute (if (match-end 3)
  15052. (string-to-number (match-string 3 ans))
  15053. 0)
  15054. pm (equal ?p
  15055. (string-to-char (downcase (match-string 4 ans)))))
  15056. (if (and (= hour 12) (not pm))
  15057. (setq hour 0)
  15058. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15059. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15060. t t ans))))
  15061. ;; Check if a time range is given as a duration
  15062. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15063. (setq hour (string-to-number (match-string 1 ans))
  15064. h2 (+ hour (string-to-number (match-string 3 ans)))
  15065. minute (string-to-number (match-string 2 ans))
  15066. m2 (+ minute (if (match-end 5) (string-to-number
  15067. (match-string 5 ans))0)))
  15068. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15069. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15070. t t ans)))
  15071. ;; Check if there is a time range
  15072. (when (boundp 'org-end-time-was-given)
  15073. (setq org-time-was-given nil)
  15074. (when (and (string-match org-plain-time-of-day-regexp ans)
  15075. (match-end 8))
  15076. (setq org-end-time-was-given (match-string 8 ans))
  15077. (setq ans (concat (substring ans 0 (match-beginning 7))
  15078. (substring ans (match-end 7))))))
  15079. (setq tl (parse-time-string ans)
  15080. day (or (nth 3 tl) (nth 3 org-defdecode))
  15081. month (or (nth 4 tl)
  15082. (if (and org-read-date-prefer-future
  15083. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15084. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15085. (nth 4 org-defdecode)))
  15086. year (or (and (not kill-year) (nth 5 tl))
  15087. (if (and org-read-date-prefer-future
  15088. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15089. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15090. (nth 5 org-defdecode)))
  15091. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15092. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15093. second (or (nth 0 tl) 0)
  15094. wday (nth 6 tl))
  15095. (when (and (eq org-read-date-prefer-future 'time)
  15096. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15097. (equal day (nth 3 nowdecode))
  15098. (equal month (nth 4 nowdecode))
  15099. (equal year (nth 5 nowdecode))
  15100. (nth 2 tl)
  15101. (or (< (nth 2 tl) (nth 2 nowdecode))
  15102. (and (= (nth 2 tl) (nth 2 nowdecode))
  15103. (nth 1 tl)
  15104. (< (nth 1 tl) (nth 1 nowdecode)))))
  15105. (setq day (1+ day)
  15106. futurep t))
  15107. ;; Special date definitions below
  15108. (cond
  15109. (iso-week
  15110. ;; There was an iso week
  15111. (require 'cal-iso)
  15112. (setq futurep nil)
  15113. (setq year (or iso-year year)
  15114. day (or iso-weekday wday 1)
  15115. wday nil ; to make sure that the trigger below does not match
  15116. iso-date (calendar-gregorian-from-absolute
  15117. (calendar-absolute-from-iso
  15118. (list iso-week day year))))
  15119. ; FIXME: Should we also push ISO weeks into the future?
  15120. ; (when (and org-read-date-prefer-future
  15121. ; (not iso-year)
  15122. ; (< (calendar-absolute-from-gregorian iso-date)
  15123. ; (time-to-days (current-time))))
  15124. ; (setq year (1+ year)
  15125. ; iso-date (calendar-gregorian-from-absolute
  15126. ; (calendar-absolute-from-iso
  15127. ; (list iso-week day year)))))
  15128. (setq month (car iso-date)
  15129. year (nth 2 iso-date)
  15130. day (nth 1 iso-date)))
  15131. (deltan
  15132. (setq futurep nil)
  15133. (unless deltadef
  15134. (let ((now (decode-time (current-time))))
  15135. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15136. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15137. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15138. ((equal deltaw "m") (setq month (+ month deltan)))
  15139. ((equal deltaw "y") (setq year (+ year deltan)))))
  15140. ((and wday (not (nth 3 tl)))
  15141. ;; Weekday was given, but no day, so pick that day in the week
  15142. ;; on or after the derived date.
  15143. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15144. (unless (equal wday wday1)
  15145. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15146. (if (and (boundp 'org-time-was-given)
  15147. (nth 2 tl))
  15148. (setq org-time-was-given t))
  15149. (if (< year 100) (setq year (+ 2000 year)))
  15150. ;; Check of the date is representable
  15151. (if org-read-date-force-compatible-dates
  15152. (progn
  15153. (if (< year 1970)
  15154. (setq year 1970 org-read-date-analyze-forced-year t))
  15155. (if (> year 2037)
  15156. (setq year 2037 org-read-date-analyze-forced-year t)))
  15157. (condition-case nil
  15158. (ignore (encode-time second minute hour day month year))
  15159. (error
  15160. (setq year (nth 5 org-defdecode))
  15161. (setq org-read-date-analyze-forced-year t))))
  15162. (setq org-read-date-analyze-futurep futurep)
  15163. (list second minute hour day month year)))
  15164. (defvar parse-time-weekdays)
  15165. (defun org-read-date-get-relative (s today default)
  15166. "Check string S for special relative date string.
  15167. TODAY and DEFAULT are internal times, for today and for a default.
  15168. Return shift list (N what def-flag)
  15169. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15170. N is the number of WHATs to shift.
  15171. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15172. the DEFAULT date rather than TODAY."
  15173. (require 'parse-time)
  15174. (when (and
  15175. (string-match
  15176. (concat
  15177. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15178. "\\([0-9]+\\)?"
  15179. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15180. "\\([ \t]\\|$\\)") s)
  15181. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15182. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15183. (string-to-char (substring (match-string 1 s) -1))
  15184. ?+))
  15185. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15186. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15187. (what (if (match-end 3) (match-string 3 s) "d"))
  15188. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15189. (date (if rel default today))
  15190. (wday (nth 6 (decode-time date)))
  15191. delta)
  15192. (if wday1
  15193. (progn
  15194. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15195. (if (= delta 0) (setq delta 7))
  15196. (if (= dir ?-)
  15197. (progn
  15198. (setq delta (- delta 7))
  15199. (if (= delta 0) (setq delta -7))))
  15200. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15201. (list delta "d" rel))
  15202. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15203. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15204. "Turn a user-specified date into the internal representation.
  15205. The internal representation needed by the calendar is (month day year).
  15206. This is a wrapper to handle the brain-dead convention in calendar that
  15207. user function argument order change dependent on argument order."
  15208. (if (boundp 'calendar-date-style)
  15209. (cond
  15210. ((eq calendar-date-style 'american)
  15211. (list arg1 arg2 arg3))
  15212. ((eq calendar-date-style 'european)
  15213. (list arg2 arg1 arg3))
  15214. ((eq calendar-date-style 'iso)
  15215. (list arg2 arg3 arg1)))
  15216. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15217. (if (org-bound-and-true-p european-calendar-style)
  15218. (list arg2 arg1 arg3)
  15219. (list arg1 arg2 arg3)))))
  15220. (defun org-eval-in-calendar (form &optional keepdate)
  15221. "Eval FORM in the calendar window and return to current window.
  15222. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15223. otherwise stick to the current value of `org-ans2'."
  15224. (let ((sf (selected-frame))
  15225. (sw (selected-window)))
  15226. (select-window (get-buffer-window "*Calendar*" t))
  15227. (eval form)
  15228. (when (and (not keepdate) (calendar-cursor-to-date))
  15229. (let* ((date (calendar-cursor-to-date))
  15230. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15231. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15232. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15233. (select-window sw)
  15234. (org-select-frame-set-input-focus sf)))
  15235. (defun org-calendar-select ()
  15236. "Return to `org-read-date' with the date currently selected.
  15237. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15238. (interactive)
  15239. (when (calendar-cursor-to-date)
  15240. (let* ((date (calendar-cursor-to-date))
  15241. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15242. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15243. (if (active-minibuffer-window) (exit-minibuffer))))
  15244. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15245. "Insert a date stamp for the date given by the internal TIME.
  15246. WITH-HM means use the stamp format that includes the time of the day.
  15247. INACTIVE means use square brackets instead of angular ones, so that the
  15248. stamp will not contribute to the agenda.
  15249. PRE and POST are optional strings to be inserted before and after the
  15250. stamp.
  15251. The command returns the inserted time stamp."
  15252. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15253. stamp)
  15254. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15255. (insert-before-markers (or pre ""))
  15256. (when (listp extra)
  15257. (setq extra (car extra))
  15258. (if (and (stringp extra)
  15259. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15260. (setq extra (format "-%02d:%02d"
  15261. (string-to-number (match-string 1 extra))
  15262. (string-to-number (match-string 2 extra))))
  15263. (setq extra nil)))
  15264. (when extra
  15265. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15266. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15267. (insert-before-markers (or post ""))
  15268. (setq org-last-inserted-timestamp stamp)))
  15269. (defun org-toggle-time-stamp-overlays ()
  15270. "Toggle the use of custom time stamp formats."
  15271. (interactive)
  15272. (setq org-display-custom-times (not org-display-custom-times))
  15273. (unless org-display-custom-times
  15274. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15275. (while (setq p (next-single-property-change p 'display))
  15276. (if (and (get-text-property p 'display)
  15277. (eq (get-text-property p 'face) 'org-date))
  15278. (remove-text-properties
  15279. p (setq p (next-single-property-change p 'display))
  15280. '(display t))))
  15281. (set-buffer-modified-p bmp)))
  15282. (if (featurep 'xemacs)
  15283. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15284. (org-restart-font-lock)
  15285. (setq org-table-may-need-update t)
  15286. (if org-display-custom-times
  15287. (message "Time stamps are overlaid with custom format")
  15288. (message "Time stamp overlays removed")))
  15289. (defun org-display-custom-time (beg end)
  15290. "Overlay modified time stamp format over timestamp between BEG and END."
  15291. (let* ((ts (buffer-substring beg end))
  15292. t1 w1 with-hm tf time str w2 (off 0))
  15293. (save-match-data
  15294. (setq t1 (org-parse-time-string ts t))
  15295. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15296. (setq off (- (match-end 0) (match-beginning 0)))))
  15297. (setq end (- end off))
  15298. (setq w1 (- end beg)
  15299. with-hm (and (nth 1 t1) (nth 2 t1))
  15300. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15301. time (org-fix-decoded-time t1)
  15302. str (org-add-props
  15303. (format-time-string
  15304. (substring tf 1 -1) (apply 'encode-time time))
  15305. nil 'mouse-face 'highlight)
  15306. w2 (length str))
  15307. (if (not (= w2 w1))
  15308. (add-text-properties (1+ beg) (+ 2 beg)
  15309. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15310. (if (featurep 'xemacs)
  15311. (progn
  15312. (put-text-property beg end 'invisible t)
  15313. (put-text-property beg end 'end-glyph (make-glyph str)))
  15314. (put-text-property beg end 'display str))))
  15315. (defun org-translate-time (string)
  15316. "Translate all timestamps in STRING to custom format.
  15317. But do this only if the variable `org-display-custom-times' is set."
  15318. (when org-display-custom-times
  15319. (save-match-data
  15320. (let* ((start 0)
  15321. (re org-ts-regexp-both)
  15322. t1 with-hm inactive tf time str beg end)
  15323. (while (setq start (string-match re string start))
  15324. (setq beg (match-beginning 0)
  15325. end (match-end 0)
  15326. t1 (save-match-data
  15327. (org-parse-time-string (substring string beg end) t))
  15328. with-hm (and (nth 1 t1) (nth 2 t1))
  15329. inactive (equal (substring string beg (1+ beg)) "[")
  15330. tf (funcall (if with-hm 'cdr 'car)
  15331. org-time-stamp-custom-formats)
  15332. time (org-fix-decoded-time t1)
  15333. str (format-time-string
  15334. (concat
  15335. (if inactive "[" "<") (substring tf 1 -1)
  15336. (if inactive "]" ">"))
  15337. (apply 'encode-time time))
  15338. string (replace-match str t t string)
  15339. start (+ start (length str)))))))
  15340. string)
  15341. (defun org-fix-decoded-time (time)
  15342. "Set 0 instead of nil for the first 6 elements of time.
  15343. Don't touch the rest."
  15344. (let ((n 0))
  15345. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15346. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15347. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15348. "Difference between TIMESTAMP-STRING and now in days.
  15349. If SECONDS is non-nil, return the difference in seconds."
  15350. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15351. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15352. (funcall fdiff (current-time)))))
  15353. (defun org-deadline-close (timestamp-string &optional ndays)
  15354. "Is the time in TIMESTAMP-STRING close to the current date?"
  15355. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15356. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15357. (not (org-entry-is-done-p))))
  15358. (defun org-get-wdays (ts &optional delay zero-delay)
  15359. "Get the deadline lead time appropriate for timestring TS.
  15360. When DELAY is non-nil, get the delay time for scheduled items
  15361. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15362. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15363. don't try to find the delay cookie in the scheduled timestamp."
  15364. (let ((tv (if delay org-scheduled-delay-days
  15365. org-deadline-warning-days)))
  15366. (cond
  15367. ((or (and delay (< tv 0))
  15368. (and delay zero-delay (<= tv 0))
  15369. (and (not delay) (<= tv 0)))
  15370. ;; Enforce this value no matter what
  15371. (- tv))
  15372. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15373. ;; lead time is specified.
  15374. (floor (* (string-to-number (match-string 1 ts))
  15375. (cdr (assoc (match-string 2 ts)
  15376. '(("d" . 1) ("w" . 7)
  15377. ("m" . 30.4) ("y" . 365.25)
  15378. ("h" . 0.041667)))))))
  15379. ;; go for the default.
  15380. (t tv))))
  15381. (defun org-calendar-select-mouse (ev)
  15382. "Return to `org-read-date' with the date currently selected.
  15383. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15384. (interactive "e")
  15385. (mouse-set-point ev)
  15386. (when (calendar-cursor-to-date)
  15387. (let* ((date (calendar-cursor-to-date))
  15388. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15389. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15390. (if (active-minibuffer-window) (exit-minibuffer))))
  15391. (defun org-check-deadlines (ndays)
  15392. "Check if there are any deadlines due or past due.
  15393. A deadline is considered due if it happens within `org-deadline-warning-days'
  15394. days from today's date. If the deadline appears in an entry marked DONE,
  15395. it is not shown. The prefix arg NDAYS can be used to test that many
  15396. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15397. (interactive "P")
  15398. (let* ((org-warn-days
  15399. (cond
  15400. ((equal ndays '(4)) 100000)
  15401. (ndays (prefix-numeric-value ndays))
  15402. (t (abs org-deadline-warning-days))))
  15403. (case-fold-search nil)
  15404. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15405. (callback
  15406. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15407. (message "%d deadlines past-due or due within %d days"
  15408. (org-occur regexp nil callback)
  15409. org-warn-days)))
  15410. (defsubst org-re-timestamp (type)
  15411. "Return a regexp for timestamp TYPE.
  15412. Allowed values for TYPE are:
  15413. all: all timestamps
  15414. active: only active timestamps (<...>)
  15415. inactive: only inactive timestamps ([...])
  15416. scheduled: only scheduled timestamps
  15417. deadline: only deadline timestamps
  15418. closed: only closed time-stamps
  15419. When TYPE is nil, fall back on returning a regexp that matches
  15420. both scheduled and deadline timestamps."
  15421. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15422. ((eq type 'active) org-ts-regexp)
  15423. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15424. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15425. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15426. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15427. ((eq type 'scheduled-or-deadline)
  15428. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15429. (defun org-check-before-date (date)
  15430. "Check if there are deadlines or scheduled entries before DATE."
  15431. (interactive (list (org-read-date)))
  15432. (let ((case-fold-search nil)
  15433. (regexp (org-re-timestamp org-ts-type))
  15434. (callback
  15435. (lambda () (time-less-p
  15436. (org-time-string-to-time (match-string 1))
  15437. (org-time-string-to-time date)))))
  15438. (message "%d entries before %s"
  15439. (org-occur regexp nil callback) date)))
  15440. (defun org-check-after-date (date)
  15441. "Check if there are deadlines or scheduled entries after DATE."
  15442. (interactive (list (org-read-date)))
  15443. (let ((case-fold-search nil)
  15444. (regexp (org-re-timestamp org-ts-type))
  15445. (callback
  15446. (lambda () (not
  15447. (time-less-p
  15448. (org-time-string-to-time (match-string 1))
  15449. (org-time-string-to-time date))))))
  15450. (message "%d entries after %s"
  15451. (org-occur regexp nil callback) date)))
  15452. (defun org-check-dates-range (start-date end-date)
  15453. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15454. (interactive (list (org-read-date nil nil nil "Range starts")
  15455. (org-read-date nil nil nil "Range end")))
  15456. (let ((case-fold-search nil)
  15457. (regexp (org-re-timestamp org-ts-type))
  15458. (callback
  15459. (lambda ()
  15460. (let ((match (match-string 1)))
  15461. (and
  15462. (not (time-less-p
  15463. (org-time-string-to-time match)
  15464. (org-time-string-to-time start-date)))
  15465. (time-less-p
  15466. (org-time-string-to-time match)
  15467. (org-time-string-to-time end-date)))))))
  15468. (message "%d entries between %s and %s"
  15469. (org-occur regexp nil callback) start-date end-date)))
  15470. (defun org-evaluate-time-range (&optional to-buffer)
  15471. "Evaluate a time range by computing the difference between start and end.
  15472. Normally the result is just printed in the echo area, but with prefix arg
  15473. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15474. If the time range is actually in a table, the result is inserted into the
  15475. next column.
  15476. For time difference computation, a year is assumed to be exactly 365
  15477. days in order to avoid rounding problems."
  15478. (interactive "P")
  15479. (or
  15480. (org-clock-update-time-maybe)
  15481. (save-excursion
  15482. (unless (org-at-date-range-p t)
  15483. (goto-char (point-at-bol))
  15484. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15485. (if (not (org-at-date-range-p t))
  15486. (user-error "Not at a time-stamp range, and none found in current line")))
  15487. (let* ((ts1 (match-string 1))
  15488. (ts2 (match-string 2))
  15489. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15490. (match-end (match-end 0))
  15491. (time1 (org-time-string-to-time ts1))
  15492. (time2 (org-time-string-to-time ts2))
  15493. (t1 (org-float-time time1))
  15494. (t2 (org-float-time time2))
  15495. (diff (abs (- t2 t1)))
  15496. (negative (< (- t2 t1) 0))
  15497. ;; (ys (floor (* 365 24 60 60)))
  15498. (ds (* 24 60 60))
  15499. (hs (* 60 60))
  15500. (fy "%dy %dd %02d:%02d")
  15501. (fy1 "%dy %dd")
  15502. (fd "%dd %02d:%02d")
  15503. (fd1 "%dd")
  15504. (fh "%02d:%02d")
  15505. y d h m align)
  15506. (if havetime
  15507. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15508. y 0
  15509. d (floor (/ diff ds)) diff (mod diff ds)
  15510. h (floor (/ diff hs)) diff (mod diff hs)
  15511. m (floor (/ diff 60)))
  15512. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15513. y 0
  15514. d (floor (+ (/ diff ds) 0.5))
  15515. h 0 m 0))
  15516. (if (not to-buffer)
  15517. (message "%s" (org-make-tdiff-string y d h m))
  15518. (if (org-at-table-p)
  15519. (progn
  15520. (goto-char match-end)
  15521. (setq align t)
  15522. (and (looking-at " *|") (goto-char (match-end 0))))
  15523. (goto-char match-end))
  15524. (if (looking-at
  15525. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15526. (replace-match ""))
  15527. (if negative (insert " -"))
  15528. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15529. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15530. (insert " " (format fh h m))))
  15531. (if align (org-table-align))
  15532. (message "Time difference inserted")))))
  15533. (defun org-make-tdiff-string (y d h m)
  15534. (let ((fmt "")
  15535. (l nil))
  15536. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15537. l (push y l)))
  15538. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15539. l (push d l)))
  15540. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15541. l (push h l)))
  15542. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15543. l (push m l)))
  15544. (apply 'format fmt (nreverse l))))
  15545. (defun org-time-string-to-time (s &optional buffer pos)
  15546. "Convert a timestamp string into internal time."
  15547. (condition-case errdata
  15548. (apply 'encode-time (org-parse-time-string s))
  15549. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15550. s (if (not (and buffer pos))
  15551. ""
  15552. (format " at %d in buffer `%s'" pos buffer))
  15553. (cdr errdata)))))
  15554. (defun org-time-string-to-seconds (s)
  15555. "Convert a timestamp string to a number of seconds."
  15556. (org-float-time (org-time-string-to-time s)))
  15557. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15558. "Convert a time stamp to an absolute day number.
  15559. If there is a specifier for a cyclic time stamp, get the closest
  15560. date to DAYNR.
  15561. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15562. The variable `date' is bound by the calendar when this is called."
  15563. (cond
  15564. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15565. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15566. daynr
  15567. (+ daynr 1000)))
  15568. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15569. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15570. (time-to-days (current-time))) (match-string 0 s)
  15571. prefer show-all))
  15572. (t (time-to-days
  15573. (condition-case errdata
  15574. (apply 'encode-time (org-parse-time-string s))
  15575. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15576. s (if (not (and buffer pos))
  15577. ""
  15578. (format " at %d in buffer `%s'" pos buffer))
  15579. (cdr errdata))))))))
  15580. (defun org-days-to-iso-week (days)
  15581. "Return the iso week number."
  15582. (require 'cal-iso)
  15583. (car (calendar-iso-from-absolute days)))
  15584. (defun org-small-year-to-year (year)
  15585. "Convert 2-digit years into 4-digit years.
  15586. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15587. The year 2000 cannot be abbreviated. Any year larger than 99
  15588. is returned unchanged."
  15589. (if (< year 38)
  15590. (setq year (+ 2000 year))
  15591. (if (< year 100)
  15592. (setq year (+ 1900 year))))
  15593. year)
  15594. (defun org-time-from-absolute (d)
  15595. "Return the time corresponding to date D.
  15596. D may be an absolute day number, or a calendar-type list (month day year)."
  15597. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15598. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15599. (defun org-calendar-holiday ()
  15600. "List of holidays, for Diary display in Org-mode."
  15601. (require 'holidays)
  15602. (let ((hl (funcall
  15603. (if (fboundp 'calendar-check-holidays)
  15604. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15605. (if hl (mapconcat 'identity hl "; "))))
  15606. (defun org-diary-sexp-entry (sexp entry date)
  15607. "Process a SEXP diary ENTRY for DATE."
  15608. (require 'diary-lib)
  15609. (let ((result (if calendar-debug-sexp
  15610. (let ((stack-trace-on-error t))
  15611. (eval (car (read-from-string sexp))))
  15612. (condition-case nil
  15613. (eval (car (read-from-string sexp)))
  15614. (error
  15615. (beep)
  15616. (message "Bad sexp at line %d in %s: %s"
  15617. (org-current-line)
  15618. (buffer-file-name) sexp)
  15619. (sleep-for 2))))))
  15620. (cond ((stringp result) (split-string result "; "))
  15621. ((and (consp result)
  15622. (not (consp (cdr result)))
  15623. (stringp (cdr result))) (cdr result))
  15624. ((and (consp result)
  15625. (stringp (car result))) result)
  15626. (result entry))))
  15627. (defun org-diary-to-ical-string (frombuf)
  15628. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15629. This uses the icalendar.el library."
  15630. (let* ((tmpdir (if (featurep 'xemacs)
  15631. (temp-directory)
  15632. temporary-file-directory))
  15633. (tmpfile (make-temp-name
  15634. (expand-file-name "orgics" tmpdir)))
  15635. buf rtn b e)
  15636. (with-current-buffer frombuf
  15637. (icalendar-export-region (point-min) (point-max) tmpfile)
  15638. (setq buf (find-buffer-visiting tmpfile))
  15639. (set-buffer buf)
  15640. (goto-char (point-min))
  15641. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15642. (setq b (match-beginning 0)))
  15643. (goto-char (point-max))
  15644. (if (re-search-backward "^END:VEVENT" nil t)
  15645. (setq e (match-end 0)))
  15646. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15647. (kill-buffer buf)
  15648. (delete-file tmpfile)
  15649. rtn))
  15650. (defun org-closest-date (start current change prefer show-all)
  15651. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15652. When PREFER is `past', return a date that is either CURRENT or past.
  15653. When PREFER is `future', return a date that is either CURRENT or future.
  15654. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15655. ;; Make the proper lists from the dates
  15656. (catch 'exit
  15657. (let ((a1 '(("h" . hour)
  15658. ("d" . day)
  15659. ("w" . week)
  15660. ("m" . month)
  15661. ("y" . year)))
  15662. (shour (nth 2 (org-parse-time-string start)))
  15663. dn dw sday cday n1 n2 n0
  15664. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15665. (setq start (org-date-to-gregorian start)
  15666. current (org-date-to-gregorian
  15667. (if show-all
  15668. current
  15669. (time-to-days (current-time))))
  15670. sday (calendar-absolute-from-gregorian start)
  15671. cday (calendar-absolute-from-gregorian current))
  15672. (if (<= cday sday) (throw 'exit sday))
  15673. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15674. (setq dn (string-to-number (match-string 1 change))
  15675. dw (cdr (assoc (match-string 2 change) a1)))
  15676. (user-error "Invalid change specifier: %s" change))
  15677. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15678. (cond
  15679. ((eq dw 'hour)
  15680. (let ((missing-hours
  15681. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15682. dn)))
  15683. (setq n1 (if (zerop missing-hours) cday
  15684. (- cday (1+ (floor (/ missing-hours 24)))))
  15685. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15686. ((eq dw 'day)
  15687. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15688. n2 (+ n1 dn)))
  15689. ((eq dw 'year)
  15690. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15691. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15692. (setq date1 (list m d y1)
  15693. n1 (calendar-absolute-from-gregorian date1)
  15694. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15695. n2 (calendar-absolute-from-gregorian date2)))
  15696. ((eq dw 'month)
  15697. ;; approx number of month between the two dates
  15698. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15699. ;; How often does dn fit in there?
  15700. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15701. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15702. m (+ m nm)
  15703. ny (floor (/ m 12))
  15704. y (+ y ny)
  15705. m (- m (* ny 12)))
  15706. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15707. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15708. (setq m2 (+ m dn) y2 y)
  15709. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15710. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15711. (while (<= n2 cday)
  15712. (setq n1 n2 m m2 y y2)
  15713. (setq m2 (+ m dn) y2 y)
  15714. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15715. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15716. ;; Make sure n1 is the earlier date
  15717. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15718. (if show-all
  15719. (cond
  15720. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15721. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15722. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15723. (cond
  15724. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15725. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15726. (t (if (= cday n1) n1 n2)))))))
  15727. (defun org-date-to-gregorian (date)
  15728. "Turn any specification of DATE into a Gregorian date for the calendar."
  15729. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15730. ((and (listp date) (= (length date) 3)) date)
  15731. ((stringp date)
  15732. (setq date (org-parse-time-string date))
  15733. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15734. ((listp date)
  15735. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15736. (defun org-parse-time-string (s &optional nodefault)
  15737. "Parse the standard Org-mode time string.
  15738. This should be a lot faster than the normal `parse-time-string'.
  15739. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15740. hour and minute fields will be nil if not given."
  15741. (cond ((string-match org-ts-regexp0 s)
  15742. (list 0
  15743. (if (or (match-beginning 8) (not nodefault))
  15744. (string-to-number (or (match-string 8 s) "0")))
  15745. (if (or (match-beginning 7) (not nodefault))
  15746. (string-to-number (or (match-string 7 s) "0")))
  15747. (string-to-number (match-string 4 s))
  15748. (string-to-number (match-string 3 s))
  15749. (string-to-number (match-string 2 s))
  15750. nil nil nil))
  15751. ((string-match "^<[^>]+>$" s)
  15752. (decode-time (seconds-to-time (org-matcher-time s))))
  15753. (t (error "Not a standard Org-mode time string: %s" s))))
  15754. (defun org-timestamp-up (&optional arg)
  15755. "Increase the date item at the cursor by one.
  15756. If the cursor is on the year, change the year. If it is on the month,
  15757. the day or the time, change that.
  15758. With prefix ARG, change by that many units."
  15759. (interactive "p")
  15760. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15761. (defun org-timestamp-down (&optional arg)
  15762. "Decrease the date item at the cursor by one.
  15763. If the cursor is on the year, change the year. If it is on the month,
  15764. the day or the time, change that.
  15765. With prefix ARG, change by that many units."
  15766. (interactive "p")
  15767. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15768. (defun org-timestamp-up-day (&optional arg)
  15769. "Increase the date in the time stamp by one day.
  15770. With prefix ARG, change that many days."
  15771. (interactive "p")
  15772. (if (and (not (org-at-timestamp-p t))
  15773. (org-at-heading-p))
  15774. (org-todo 'up)
  15775. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15776. (defun org-timestamp-down-day (&optional arg)
  15777. "Decrease the date in the time stamp by one day.
  15778. With prefix ARG, change that many days."
  15779. (interactive "p")
  15780. (if (and (not (org-at-timestamp-p t))
  15781. (org-at-heading-p))
  15782. (org-todo 'down)
  15783. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15784. (defun org-at-timestamp-p (&optional inactive-ok)
  15785. "Determine if the cursor is in or at a timestamp."
  15786. (interactive)
  15787. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15788. (pos (point))
  15789. (ans (or (looking-at tsr)
  15790. (save-excursion
  15791. (skip-chars-backward "^[<\n\r\t")
  15792. (if (> (point) (point-min)) (backward-char 1))
  15793. (and (looking-at tsr)
  15794. (> (- (match-end 0) pos) -1))))))
  15795. (and ans
  15796. (boundp 'org-ts-what)
  15797. (setq org-ts-what
  15798. (cond
  15799. ((= pos (match-beginning 0)) 'bracket)
  15800. ;; Point is considered to be "on the bracket" whether
  15801. ;; it's really on it or right after it.
  15802. ((= pos (1- (match-end 0))) 'bracket)
  15803. ((= pos (match-end 0)) 'after)
  15804. ((org-pos-in-match-range pos 2) 'year)
  15805. ((org-pos-in-match-range pos 3) 'month)
  15806. ((org-pos-in-match-range pos 7) 'hour)
  15807. ((org-pos-in-match-range pos 8) 'minute)
  15808. ((or (org-pos-in-match-range pos 4)
  15809. (org-pos-in-match-range pos 5)) 'day)
  15810. ((and (> pos (or (match-end 8) (match-end 5)))
  15811. (< pos (match-end 0)))
  15812. (- pos (or (match-end 8) (match-end 5))))
  15813. (t 'day))))
  15814. ans))
  15815. (defun org-toggle-timestamp-type ()
  15816. "Toggle the type (<active> or [inactive]) of a time stamp."
  15817. (interactive)
  15818. (when (org-at-timestamp-p t)
  15819. (let ((beg (match-beginning 0)) (end (match-end 0))
  15820. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15821. (save-excursion
  15822. (goto-char beg)
  15823. (while (re-search-forward "[][<>]" end t)
  15824. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15825. t t)))
  15826. (message "Timestamp is now %sactive"
  15827. (if (equal (char-after beg) ?<) "" "in")))))
  15828. (defun org-at-clock-log-p nil
  15829. "Is the cursor on the clock log line?"
  15830. (save-excursion
  15831. (move-beginning-of-line 1)
  15832. (looking-at "^[ \t]*CLOCK:")))
  15833. (defvar org-clock-history) ; defined in org-clock.el
  15834. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15835. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15836. "Change the date in the time stamp at point.
  15837. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15838. `year', `minute', `second'. If WHAT is not given, the cursor position
  15839. in the timestamp determines what will be changed.
  15840. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15841. (let ((origin (point)) origin-cat
  15842. with-hm inactive
  15843. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15844. org-ts-what
  15845. extra rem
  15846. ts time time0 fixnext clrgx)
  15847. (if (not (org-at-timestamp-p t))
  15848. (user-error "Not at a timestamp"))
  15849. (if (and (not what) (eq org-ts-what 'bracket))
  15850. (org-toggle-timestamp-type)
  15851. ;; Point isn't on brackets. Remember the part of the time-stamp
  15852. ;; the point was in. Indeed, size of time-stamps may change,
  15853. ;; but point must be kept in the same category nonetheless.
  15854. (setq origin-cat org-ts-what)
  15855. (if (and (not what) (not (eq org-ts-what 'day))
  15856. org-display-custom-times
  15857. (get-text-property (point) 'display)
  15858. (not (get-text-property (1- (point)) 'display)))
  15859. (setq org-ts-what 'day))
  15860. (setq org-ts-what (or what org-ts-what)
  15861. inactive (= (char-after (match-beginning 0)) ?\[)
  15862. ts (match-string 0))
  15863. (replace-match "")
  15864. (when (string-match
  15865. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15866. ts)
  15867. (setq extra (match-string 1 ts))
  15868. (if suppress-tmp-delay
  15869. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15870. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15871. (setq with-hm t))
  15872. (setq time0 (org-parse-time-string ts))
  15873. (when (and updown
  15874. (eq org-ts-what 'minute)
  15875. (not current-prefix-arg))
  15876. ;; This looks like s-up and s-down. Change by one rounding step.
  15877. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15878. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15879. (setcar (cdr time0) (+ (nth 1 time0)
  15880. (if (> n 0) (- rem) (- dm rem))))))
  15881. (setq time
  15882. (encode-time (or (car time0) 0)
  15883. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15884. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15885. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15886. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15887. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15888. (nthcdr 6 time0)))
  15889. (when (and (member org-ts-what '(hour minute))
  15890. extra
  15891. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15892. (setq extra (org-modify-ts-extra
  15893. extra
  15894. (if (eq org-ts-what 'hour) 2 5)
  15895. n dm)))
  15896. (when (integerp org-ts-what)
  15897. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15898. (if (eq what 'calendar)
  15899. (let ((cal-date (org-get-date-from-calendar)))
  15900. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15901. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15902. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15903. (setcar time0 (or (car time0) 0))
  15904. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15905. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15906. (setq time (apply 'encode-time time0))))
  15907. ;; Insert the new time-stamp, and ensure point stays in the same
  15908. ;; category as before (i.e. not after the last position in that
  15909. ;; category).
  15910. (let ((pos (point)))
  15911. ;; Stay before inserted string. `save-excursion' is of no use.
  15912. (setq org-last-changed-timestamp
  15913. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15914. (goto-char pos))
  15915. (save-match-data
  15916. (looking-at org-ts-regexp3)
  15917. (goto-char (cond
  15918. ;; `day' category ends before `hour' if any, or at
  15919. ;; the end of the day name.
  15920. ((eq origin-cat 'day)
  15921. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15922. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15923. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15924. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15925. ;; `year' and `month' have both fixed size: point
  15926. ;; couldn't have moved into another part.
  15927. (t origin))))
  15928. ;; Update clock if on a CLOCK line.
  15929. (org-clock-update-time-maybe)
  15930. ;; Maybe adjust the closest clock in `org-clock-history'
  15931. (when org-clock-adjust-closest
  15932. (if (not (and (org-at-clock-log-p)
  15933. (< 1 (length (delq nil (mapcar 'marker-position
  15934. org-clock-history))))))
  15935. (message "No clock to adjust")
  15936. (cond ((save-excursion ; fix previous clock?
  15937. (re-search-backward org-ts-regexp0 nil t)
  15938. (org-looking-back (concat org-clock-string " \\[")))
  15939. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15940. ((save-excursion ; fix next clock?
  15941. (re-search-backward org-ts-regexp0 nil t)
  15942. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15943. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15944. (save-window-excursion
  15945. ;; Find closest clock to point, adjust the previous/next one in history
  15946. (let* ((p (save-excursion (org-back-to-heading t)))
  15947. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15948. (clfixnth
  15949. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15950. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15951. (if (not clfixpos)
  15952. (message "No clock to adjust")
  15953. (save-excursion
  15954. (org-goto-marker-or-bmk clfixpos)
  15955. (org-show-subtree)
  15956. (when (re-search-forward clrgx nil t)
  15957. (goto-char (match-beginning 1))
  15958. (let (org-clock-adjust-closest)
  15959. (org-timestamp-change n org-ts-what updown))
  15960. (message "Clock adjusted in %s for heading: %s"
  15961. (file-name-nondirectory (buffer-file-name))
  15962. (org-get-heading t t)))))))))
  15963. ;; Try to recenter the calendar window, if any.
  15964. (if (and org-calendar-follow-timestamp-change
  15965. (get-buffer-window "*Calendar*" t)
  15966. (memq org-ts-what '(day month year)))
  15967. (org-recenter-calendar (time-to-days time))))))
  15968. (defun org-modify-ts-extra (s pos n dm)
  15969. "Change the different parts of the lead-time and repeat fields in timestamp."
  15970. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15971. ng h m new rem)
  15972. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15973. (cond
  15974. ((or (org-pos-in-match-range pos 2)
  15975. (org-pos-in-match-range pos 3))
  15976. (setq m (string-to-number (match-string 3 s))
  15977. h (string-to-number (match-string 2 s)))
  15978. (if (org-pos-in-match-range pos 2)
  15979. (setq h (+ h n))
  15980. (setq n (* dm (org-no-warnings (signum n))))
  15981. (when (not (= 0 (setq rem (% m dm))))
  15982. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15983. (setq m (+ m n)))
  15984. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15985. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15986. (setq h (min 24 (max 0 h)))
  15987. (setq ng 1 new (format "-%02d:%02d" h m)))
  15988. ((org-pos-in-match-range pos 6)
  15989. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15990. ((org-pos-in-match-range pos 5)
  15991. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15992. ((org-pos-in-match-range pos 9)
  15993. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15994. ((org-pos-in-match-range pos 8)
  15995. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15996. (when ng
  15997. (setq s (concat
  15998. (substring s 0 (match-beginning ng))
  15999. new
  16000. (substring s (match-end ng))))))
  16001. s))
  16002. (defun org-recenter-calendar (date)
  16003. "If the calendar is visible, recenter it to DATE."
  16004. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16005. (when cwin
  16006. (let ((calendar-move-hook nil))
  16007. (with-selected-window cwin
  16008. (calendar-goto-date (if (listp date) date
  16009. (calendar-gregorian-from-absolute date))))))))
  16010. (defun org-goto-calendar (&optional arg)
  16011. "Go to the Emacs calendar at the current date.
  16012. If there is a time stamp in the current line, go to that date.
  16013. A prefix ARG can be used to force the current date."
  16014. (interactive "P")
  16015. (let ((tsr org-ts-regexp) diff
  16016. (calendar-move-hook nil)
  16017. (calendar-view-holidays-initially-flag nil)
  16018. (calendar-view-diary-initially-flag nil))
  16019. (if (or (org-at-timestamp-p)
  16020. (save-excursion
  16021. (beginning-of-line 1)
  16022. (looking-at (concat ".*" tsr))))
  16023. (let ((d1 (time-to-days (current-time)))
  16024. (d2 (time-to-days
  16025. (org-time-string-to-time (match-string 1)))))
  16026. (setq diff (- d2 d1))))
  16027. (calendar)
  16028. (calendar-goto-today)
  16029. (if (and diff (not arg)) (calendar-forward-day diff))))
  16030. (defun org-get-date-from-calendar ()
  16031. "Return a list (month day year) of date at point in calendar."
  16032. (with-current-buffer "*Calendar*"
  16033. (save-match-data
  16034. (calendar-cursor-to-date))))
  16035. (defun org-date-from-calendar ()
  16036. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16037. If there is already a time stamp at the cursor position, update it."
  16038. (interactive)
  16039. (if (org-at-timestamp-p t)
  16040. (org-timestamp-change 0 'calendar)
  16041. (let ((cal-date (org-get-date-from-calendar)))
  16042. (org-insert-time-stamp
  16043. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16044. (defcustom org-effort-durations
  16045. `(("h" . 60)
  16046. ("d" . ,(* 60 8))
  16047. ("w" . ,(* 60 8 5))
  16048. ("m" . ,(* 60 8 5 4))
  16049. ("y" . ,(* 60 8 5 40)))
  16050. "Conversion factor to minutes for an effort modifier.
  16051. Each entry has the form (MODIFIER . MINUTES).
  16052. In an effort string, a number followed by MODIFIER is multiplied
  16053. by the specified number of MINUTES to obtain an effort in
  16054. minutes.
  16055. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16056. effort string \"2hours\" is equivalent to 120 minutes."
  16057. :group 'org-agenda
  16058. :version "24.1"
  16059. :type '(alist :key-type (string :tag "Modifier")
  16060. :value-type (number :tag "Minutes")))
  16061. (defun org-minutes-to-clocksum-string (m)
  16062. "Format number of minutes as a clocksum string.
  16063. The format is determined by `org-time-clocksum-format',
  16064. `org-time-clocksum-use-fractional' and
  16065. `org-time-clocksum-fractional-format' and
  16066. `org-time-clocksum-use-effort-durations'."
  16067. (let ((clocksum "")
  16068. (m (round m)) ; Don't allow fractions of minutes
  16069. h d w mo y fmt n)
  16070. (setq h (if org-time-clocksum-use-effort-durations
  16071. (cdr (assoc "h" org-effort-durations)) 60)
  16072. d (if org-time-clocksum-use-effort-durations
  16073. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16074. w (if org-time-clocksum-use-effort-durations
  16075. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16076. mo (if org-time-clocksum-use-effort-durations
  16077. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16078. y (if org-time-clocksum-use-effort-durations
  16079. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16080. ;; fractional format
  16081. (if org-time-clocksum-use-fractional
  16082. (cond
  16083. ;; single format string
  16084. ((stringp org-time-clocksum-fractional-format)
  16085. (format org-time-clocksum-fractional-format (/ m (float h))))
  16086. ;; choice of fractional formats for different time units
  16087. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16088. (> (/ (truncate m) (* y d h)) 0))
  16089. (format fmt (/ m (* y d (float h)))))
  16090. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16091. (> (/ (truncate m) (* mo d h)) 0))
  16092. (format fmt (/ m (* mo d (float h)))))
  16093. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16094. (> (/ (truncate m) (* w d h)) 0))
  16095. (format fmt (/ m (* w d (float h)))))
  16096. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16097. (> (/ (truncate m) (* d h)) 0))
  16098. (format fmt (/ m (* d (float h)))))
  16099. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16100. (> (/ (truncate m) h) 0))
  16101. (format fmt (/ m (float h))))
  16102. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16103. (format fmt m))
  16104. ;; fall back to smallest time unit with a format
  16105. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16106. (format fmt (/ m (float h))))
  16107. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16108. (format fmt (/ m (* d (float h)))))
  16109. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16110. (format fmt (/ m (* w d (float h)))))
  16111. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16112. (format fmt (/ m (* mo d (float h)))))
  16113. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16114. (format fmt (/ m (* y d (float h))))))
  16115. ;; standard (non-fractional) format, with single format string
  16116. (if (stringp org-time-clocksum-format)
  16117. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16118. ;; separate formats components
  16119. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16120. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16121. (plist-get org-time-clocksum-format :require-years))
  16122. (setq clocksum (concat clocksum (format fmt n))
  16123. m (- m (* n y d h))))
  16124. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16125. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16126. (plist-get org-time-clocksum-format :require-months))
  16127. (setq clocksum (concat clocksum (format fmt n))
  16128. m (- m (* n mo d h))))
  16129. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16130. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16131. (plist-get org-time-clocksum-format :require-weeks))
  16132. (setq clocksum (concat clocksum (format fmt n))
  16133. m (- m (* n w d h))))
  16134. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16135. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16136. (plist-get org-time-clocksum-format :require-days))
  16137. (setq clocksum (concat clocksum (format fmt n))
  16138. m (- m (* n d h))))
  16139. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16140. (or (> (setq n (/ (truncate m) h)) 0)
  16141. (plist-get org-time-clocksum-format :require-hours))
  16142. (setq clocksum (concat clocksum (format fmt n))
  16143. m (- m (* n h))))
  16144. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16145. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16146. (setq clocksum (concat clocksum (format fmt m))))
  16147. ;; return formatted time duration
  16148. clocksum))))
  16149. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16150. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16151. "Org mode version 8.0")
  16152. (defun org-hours-to-clocksum-string (n)
  16153. (org-minutes-to-clocksum-string (* n 60)))
  16154. (defun org-hh:mm-string-to-minutes (s)
  16155. "Convert a string H:MM to a number of minutes.
  16156. If the string is just a number, interpret it as minutes.
  16157. In fact, the first hh:mm or number in the string will be taken,
  16158. there can be extra stuff in the string.
  16159. If no number is found, the return value is 0."
  16160. (cond
  16161. ((integerp s) s)
  16162. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16163. (+ (* (string-to-number (match-string 1 s)) 60)
  16164. (string-to-number (match-string 2 s))))
  16165. ((string-match "\\([0-9]+\\)" s)
  16166. (string-to-number (match-string 1 s)))
  16167. (t 0)))
  16168. (defcustom org-image-actual-width t
  16169. "Should we use the actual width of images when inlining them?
  16170. When set to `t', always use the image width.
  16171. When set to a number, use imagemagick (when available) to set
  16172. the image's width to this value.
  16173. When set to a number in a list, try to get the width from any
  16174. #+ATTR.* keyword if it matches a width specification like
  16175. #+ATTR_HTML: :width 300px
  16176. and fall back on that number if none is found.
  16177. When set to nil, try to get the width from an #+ATTR.* keyword
  16178. and fall back on the original width if none is found.
  16179. This requires Emacs >= 24.1, build with imagemagick support."
  16180. :group 'org-appearance
  16181. :version "24.4"
  16182. :package-version '(Org . "8.0")
  16183. :type '(choice
  16184. (const :tag "Use the image width" t)
  16185. (integer :tag "Use a number of pixels")
  16186. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16187. (const :tag "Use #+ATTR* or don't resize" nil)))
  16188. (defcustom org-agenda-inhibit-startup nil
  16189. "Inhibit startup when preparing agenda buffers.
  16190. When this variable is `t' (the default), the initialization of
  16191. the Org agenda buffers is inhibited: e.g. the visibility state
  16192. is not set, the tables are not re-aligned, etc."
  16193. :type 'boolean
  16194. :version "24.3"
  16195. :group 'org-agenda)
  16196. (defcustom org-agenda-ignore-drawer-properties nil
  16197. "Avoid updating text properties when building the agenda.
  16198. Properties are used to prepare buffers for effort estimates, appointments,
  16199. and subtree-local categories.
  16200. If you don't use these in the agenda, you can add them to this list and
  16201. agenda building will be a bit faster.
  16202. The value is a list, with zero or more of the symbols `effort', `appt',
  16203. or `category'."
  16204. :type '(set :greedy t
  16205. (const effort)
  16206. (const appt)
  16207. (const category))
  16208. :version "24.3"
  16209. :group 'org-agenda)
  16210. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16211. "Convert a duration string S to minutes.
  16212. A bare number is interpreted as minutes, modifiers can be set by
  16213. customizing `org-effort-durations' (which see).
  16214. Entries containing a colon are interpreted as H:MM by
  16215. `org-hh:mm-string-to-minutes'."
  16216. (let ((result 0)
  16217. (re (concat "\\([0-9.]+\\) *\\("
  16218. (regexp-opt (mapcar 'car org-effort-durations))
  16219. "\\)")))
  16220. (while (string-match re s)
  16221. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16222. (string-to-number (match-string 1 s))))
  16223. (setq s (replace-match "" nil t s)))
  16224. (setq result (floor result))
  16225. (incf result (org-hh:mm-string-to-minutes s))
  16226. (if output-to-string (number-to-string result) result)))
  16227. ;;;; Files
  16228. (defun org-save-all-org-buffers ()
  16229. "Save all Org-mode buffers without user confirmation."
  16230. (interactive)
  16231. (message "Saving all Org-mode buffers...")
  16232. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16233. (when (featurep 'org-id) (org-id-locations-save))
  16234. (message "Saving all Org-mode buffers... done"))
  16235. (defun org-revert-all-org-buffers ()
  16236. "Revert all Org-mode buffers.
  16237. Prompt for confirmation when there are unsaved changes.
  16238. Be sure you know what you are doing before letting this function
  16239. overwrite your changes.
  16240. This function is useful in a setup where one tracks org files
  16241. with a version control system, to revert on one machine after pulling
  16242. changes from another. I believe the procedure must be like this:
  16243. 1. M-x org-save-all-org-buffers
  16244. 2. Pull changes from the other machine, resolve conflicts
  16245. 3. M-x org-revert-all-org-buffers"
  16246. (interactive)
  16247. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16248. (user-error "Abort"))
  16249. (save-excursion
  16250. (save-window-excursion
  16251. (mapc
  16252. (lambda (b)
  16253. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16254. (with-current-buffer b buffer-file-name))
  16255. (org-pop-to-buffer-same-window b)
  16256. (revert-buffer t 'no-confirm)))
  16257. (buffer-list))
  16258. (when (and (featurep 'org-id) org-id-track-globally)
  16259. (org-id-locations-load)))))
  16260. ;;;; Agenda files
  16261. ;;;###autoload
  16262. (defun org-switchb (&optional arg)
  16263. "Switch between Org buffers.
  16264. With one prefix argument, restrict available buffers to files.
  16265. With two prefix arguments, restrict available buffers to agenda files.
  16266. Defaults to `iswitchb' for buffer name completion.
  16267. Set `org-completion-use-ido' to make it use ido instead."
  16268. (interactive "P")
  16269. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16270. ((equal arg '(16)) (org-buffer-list 'agenda))
  16271. (t (org-buffer-list))))
  16272. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16273. (org-completion-use-ido org-completion-use-ido))
  16274. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16275. (setq org-completion-use-iswitchb t))
  16276. (org-pop-to-buffer-same-window
  16277. (org-icompleting-read "Org buffer: "
  16278. (mapcar 'list (mapcar 'buffer-name blist))
  16279. nil t))))
  16280. ;;; Define some older names previously used for this functionality
  16281. ;;;###autoload
  16282. (defalias 'org-ido-switchb 'org-switchb)
  16283. ;;;###autoload
  16284. (defalias 'org-iswitchb 'org-switchb)
  16285. (defun org-buffer-list (&optional predicate exclude-tmp)
  16286. "Return a list of Org buffers.
  16287. PREDICATE can be `export', `files' or `agenda'.
  16288. export restrict the list to Export buffers.
  16289. files restrict the list to buffers visiting Org files.
  16290. agenda restrict the list to buffers visiting agenda files.
  16291. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16292. (let* ((bfn nil)
  16293. (agenda-files (and (eq predicate 'agenda)
  16294. (mapcar 'file-truename (org-agenda-files t))))
  16295. (filter
  16296. (cond
  16297. ((eq predicate 'files)
  16298. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16299. ((eq predicate 'export)
  16300. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16301. ((eq predicate 'agenda)
  16302. (lambda (b)
  16303. (with-current-buffer b
  16304. (and (derived-mode-p 'org-mode)
  16305. (setq bfn (buffer-file-name b))
  16306. (member (file-truename bfn) agenda-files)))))
  16307. (t (lambda (b) (with-current-buffer b
  16308. (or (derived-mode-p 'org-mode)
  16309. (string-match "\*Org .*Export"
  16310. (buffer-name b)))))))))
  16311. (delq nil
  16312. (mapcar
  16313. (lambda(b)
  16314. (if (and (funcall filter b)
  16315. (or (not exclude-tmp)
  16316. (not (string-match "tmp" (buffer-name b)))))
  16317. b
  16318. nil))
  16319. (buffer-list)))))
  16320. (defun org-agenda-files (&optional unrestricted archives)
  16321. "Get the list of agenda files.
  16322. Optional UNRESTRICTED means return the full list even if a restriction
  16323. is currently in place.
  16324. When ARCHIVES is t, include all archive files that are really being
  16325. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16326. `org-agenda-archives-mode' is t."
  16327. (let ((files
  16328. (cond
  16329. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16330. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16331. ((listp org-agenda-files) org-agenda-files)
  16332. (t (error "Invalid value of `org-agenda-files'")))))
  16333. (setq files (apply 'append
  16334. (mapcar (lambda (f)
  16335. (if (file-directory-p f)
  16336. (directory-files
  16337. f t org-agenda-file-regexp)
  16338. (list f)))
  16339. files)))
  16340. (when org-agenda-skip-unavailable-files
  16341. (setq files (delq nil
  16342. (mapcar (function
  16343. (lambda (file)
  16344. (and (file-readable-p file) file)))
  16345. files))))
  16346. (when (or (eq archives t)
  16347. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16348. (setq files (org-add-archive-files files)))
  16349. files))
  16350. (defun org-agenda-file-p (&optional file)
  16351. "Return non-nil, if FILE is an agenda file.
  16352. If FILE is omitted, use the file associated with the current
  16353. buffer."
  16354. (member (or file (buffer-file-name))
  16355. (org-agenda-files t)))
  16356. (defun org-edit-agenda-file-list ()
  16357. "Edit the list of agenda files.
  16358. Depending on setup, this either uses customize to edit the variable
  16359. `org-agenda-files', or it visits the file that is holding the list. In the
  16360. latter case, the buffer is set up in a way that saving it automatically kills
  16361. the buffer and restores the previous window configuration."
  16362. (interactive)
  16363. (if (stringp org-agenda-files)
  16364. (let ((cw (current-window-configuration)))
  16365. (find-file org-agenda-files)
  16366. (org-set-local 'org-window-configuration cw)
  16367. (org-add-hook 'after-save-hook
  16368. (lambda ()
  16369. (set-window-configuration
  16370. (prog1 org-window-configuration
  16371. (kill-buffer (current-buffer))))
  16372. (org-install-agenda-files-menu)
  16373. (message "New agenda file list installed"))
  16374. nil 'local)
  16375. (message "%s" (substitute-command-keys
  16376. "Edit list and finish with \\[save-buffer]")))
  16377. (customize-variable 'org-agenda-files)))
  16378. (defun org-store-new-agenda-file-list (list)
  16379. "Set new value for the agenda file list and save it correctly."
  16380. (if (stringp org-agenda-files)
  16381. (let ((fe (org-read-agenda-file-list t)) b u)
  16382. (while (setq b (find-buffer-visiting org-agenda-files))
  16383. (kill-buffer b))
  16384. (with-temp-file org-agenda-files
  16385. (insert
  16386. (mapconcat
  16387. (lambda (f) ;; Keep un-expanded entries.
  16388. (if (setq u (assoc f fe))
  16389. (cdr u)
  16390. f))
  16391. list "\n")
  16392. "\n")))
  16393. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16394. (org-insert-mode-line-in-empty-file nil))
  16395. (setq org-agenda-files list)
  16396. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16397. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16398. "Read the list of agenda files from a file.
  16399. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16400. filenames, used by `org-store-new-agenda-file-list' to write back
  16401. un-expanded file names."
  16402. (when (file-directory-p org-agenda-files)
  16403. (error "`org-agenda-files' cannot be a single directory"))
  16404. (when (stringp org-agenda-files)
  16405. (with-temp-buffer
  16406. (insert-file-contents org-agenda-files)
  16407. (mapcar
  16408. (lambda (f)
  16409. (let ((e (expand-file-name (substitute-in-file-name f)
  16410. org-directory)))
  16411. (if pair-with-expansion
  16412. (cons e f)
  16413. e)))
  16414. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16415. ;;;###autoload
  16416. (defun org-cycle-agenda-files ()
  16417. "Cycle through the files in `org-agenda-files'.
  16418. If the current buffer visits an agenda file, find the next one in the list.
  16419. If the current buffer does not, find the first agenda file."
  16420. (interactive)
  16421. (let* ((fs (org-agenda-files t))
  16422. (files (append fs (list (car fs))))
  16423. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16424. file)
  16425. (unless files (user-error "No agenda files"))
  16426. (catch 'exit
  16427. (while (setq file (pop files))
  16428. (if (equal (file-truename file) tcf)
  16429. (when (car files)
  16430. (find-file (car files))
  16431. (throw 'exit t))))
  16432. (find-file (car fs)))
  16433. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16434. (defun org-agenda-file-to-front (&optional to-end)
  16435. "Move/add the current file to the top of the agenda file list.
  16436. If the file is not present in the list, it is added to the front. If it is
  16437. present, it is moved there. With optional argument TO-END, add/move to the
  16438. end of the list."
  16439. (interactive "P")
  16440. (let ((org-agenda-skip-unavailable-files nil)
  16441. (file-alist (mapcar (lambda (x)
  16442. (cons (file-truename x) x))
  16443. (org-agenda-files t)))
  16444. (ctf (file-truename
  16445. (or buffer-file-name
  16446. (user-error "Please save the current buffer to a file"))))
  16447. x had)
  16448. (setq x (assoc ctf file-alist) had x)
  16449. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16450. (if to-end
  16451. (setq file-alist (append (delq x file-alist) (list x)))
  16452. (setq file-alist (cons x (delq x file-alist))))
  16453. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16454. (org-install-agenda-files-menu)
  16455. (message "File %s to %s of agenda file list"
  16456. (if had "moved" "added") (if to-end "end" "front"))))
  16457. (defun org-remove-file (&optional file)
  16458. "Remove current file from the list of files in variable `org-agenda-files'.
  16459. These are the files which are being checked for agenda entries.
  16460. Optional argument FILE means use this file instead of the current."
  16461. (interactive)
  16462. (let* ((org-agenda-skip-unavailable-files nil)
  16463. (file (or file buffer-file-name
  16464. (user-error "Current buffer does not visit a file")))
  16465. (true-file (file-truename file))
  16466. (afile (abbreviate-file-name file))
  16467. (files (delq nil (mapcar
  16468. (lambda (x)
  16469. (if (equal true-file
  16470. (file-truename x))
  16471. nil x))
  16472. (org-agenda-files t)))))
  16473. (if (not (= (length files) (length (org-agenda-files t))))
  16474. (progn
  16475. (org-store-new-agenda-file-list files)
  16476. (org-install-agenda-files-menu)
  16477. (message "Removed file: %s" afile))
  16478. (message "File was not in list: %s (not removed)" afile))))
  16479. (defun org-file-menu-entry (file)
  16480. (vector file (list 'find-file file) t))
  16481. (defun org-check-agenda-file (file)
  16482. "Make sure FILE exists. If not, ask user what to do."
  16483. (when (not (file-exists-p file))
  16484. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16485. (abbreviate-file-name file))
  16486. (let ((r (downcase (read-char-exclusive))))
  16487. (cond
  16488. ((equal r ?r)
  16489. (org-remove-file file)
  16490. (throw 'nextfile t))
  16491. (t (error "Abort"))))))
  16492. (defun org-get-agenda-file-buffer (file)
  16493. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16494. it to the list of buffers which might be released later."
  16495. (let ((buf (org-find-base-buffer-visiting file)))
  16496. (if buf
  16497. buf ; just return it
  16498. ;; Make a new buffer and remember it
  16499. (setq buf (find-file-noselect file))
  16500. (if buf (push buf org-agenda-new-buffers))
  16501. buf)))
  16502. (defun org-release-buffers (blist)
  16503. "Release all buffers in list, asking the user for confirmation when needed.
  16504. When a buffer is unmodified, it is just killed. When modified, it is saved
  16505. \(if the user agrees) and then killed."
  16506. (let (buf file)
  16507. (while (setq buf (pop blist))
  16508. (setq file (buffer-file-name buf))
  16509. (when (and (buffer-modified-p buf)
  16510. file
  16511. (y-or-n-p (format "Save file %s? " file)))
  16512. (with-current-buffer buf (save-buffer)))
  16513. (kill-buffer buf))))
  16514. (defun org-agenda-prepare-buffers (files)
  16515. "Create buffers for all agenda files, protect archived trees and comments."
  16516. (interactive)
  16517. (let ((pa '(:org-archived t))
  16518. (pc '(:org-comment t))
  16519. (pall '(:org-archived t :org-comment t))
  16520. (inhibit-read-only t)
  16521. (org-inhibit-startup org-agenda-inhibit-startup)
  16522. (rea (concat ":" org-archive-tag ":"))
  16523. file re)
  16524. (setq org-tag-alist-for-agenda nil
  16525. org-tag-groups-alist-for-agenda nil)
  16526. (save-excursion
  16527. (save-restriction
  16528. (while (setq file (pop files))
  16529. (catch 'nextfile
  16530. (if (bufferp file)
  16531. (set-buffer file)
  16532. (org-check-agenda-file file)
  16533. (set-buffer (org-get-agenda-file-buffer file)))
  16534. (widen)
  16535. (org-set-regexps-and-options-for-tags)
  16536. (goto-char (point-min))
  16537. (let ((case-fold-search t))
  16538. (when (search-forward "#+setupfile" nil t)
  16539. ;; Don't set all regexps and options systematically as
  16540. ;; this is only run for setting agenda tags from setup
  16541. ;; file
  16542. (org-set-regexps-and-options)))
  16543. (or (memq 'category org-agenda-ignore-drawer-properties)
  16544. (org-refresh-category-properties))
  16545. (or (memq 'effort org-agenda-ignore-drawer-properties)
  16546. (org-refresh-properties org-effort-property 'org-effort))
  16547. (or (memq 'appt org-agenda-ignore-drawer-properties)
  16548. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16549. (setq org-todo-keywords-for-agenda
  16550. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16551. (setq org-done-keywords-for-agenda
  16552. (append org-done-keywords-for-agenda org-done-keywords))
  16553. (setq org-todo-keyword-alist-for-agenda
  16554. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16555. (setq org-drawers-for-agenda
  16556. (append org-drawers-for-agenda org-drawers))
  16557. (setq org-tag-alist-for-agenda
  16558. (org-uniquify
  16559. (append org-tag-alist-for-agenda
  16560. org-tag-alist
  16561. org-tag-persistent-alist)))
  16562. (if org-group-tags
  16563. (setq org-tag-groups-alist-for-agenda
  16564. (org-uniquify-alist
  16565. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16566. (org-with-silent-modifications
  16567. (save-excursion
  16568. (remove-text-properties (point-min) (point-max) pall)
  16569. (when org-agenda-skip-archived-trees
  16570. (goto-char (point-min))
  16571. (while (re-search-forward rea nil t)
  16572. (if (org-at-heading-p t)
  16573. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16574. (goto-char (point-min))
  16575. (setq re (format org-heading-keyword-regexp-format
  16576. org-comment-string))
  16577. (while (re-search-forward re nil t)
  16578. (add-text-properties
  16579. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16580. (setq org-todo-keywords-for-agenda
  16581. (org-uniquify org-todo-keywords-for-agenda))
  16582. (setq org-todo-keyword-alist-for-agenda
  16583. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16584. ;;;; CDLaTeX minor mode
  16585. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16586. "Keymap for the minor `org-cdlatex-mode'.")
  16587. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16588. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16589. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16590. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16591. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16592. (defvar org-cdlatex-texmathp-advice-is-done nil
  16593. "Flag remembering if we have applied the advice to texmathp already.")
  16594. (define-minor-mode org-cdlatex-mode
  16595. "Toggle the minor `org-cdlatex-mode'.
  16596. This mode supports entering LaTeX environment and math in LaTeX fragments
  16597. in Org-mode.
  16598. \\{org-cdlatex-mode-map}"
  16599. nil " OCDL" nil
  16600. (when org-cdlatex-mode
  16601. (require 'cdlatex)
  16602. (run-hooks 'cdlatex-mode-hook)
  16603. (cdlatex-compute-tables))
  16604. (unless org-cdlatex-texmathp-advice-is-done
  16605. (setq org-cdlatex-texmathp-advice-is-done t)
  16606. (defadvice texmathp (around org-math-always-on activate)
  16607. "Always return t in org-mode buffers.
  16608. This is because we want to insert math symbols without dollars even outside
  16609. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16610. an embedded LaTeX fragment, let texmathp do its job.
  16611. \\[org-cdlatex-mode-map]"
  16612. (interactive)
  16613. (let (p)
  16614. (cond
  16615. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16616. ((eq this-command 'cdlatex-math-symbol)
  16617. (setq ad-return-value t
  16618. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16619. (t
  16620. (let ((p (org-inside-LaTeX-fragment-p)))
  16621. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16622. (setq ad-return-value t
  16623. texmathp-why '("Org-mode embedded math" . 0))
  16624. (if p ad-do-it)))))))))
  16625. (defun turn-on-org-cdlatex ()
  16626. "Unconditionally turn on `org-cdlatex-mode'."
  16627. (org-cdlatex-mode 1))
  16628. (defun org-try-cdlatex-tab ()
  16629. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16630. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16631. - inside a LaTeX fragment, or
  16632. - after the first word in a line, where an abbreviation expansion could
  16633. insert a LaTeX environment."
  16634. (when org-cdlatex-mode
  16635. (cond
  16636. ;; Before any word on the line: No expansion possible.
  16637. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16638. ;; Just after first word on the line: Expand it. Make sure it
  16639. ;; cannot happen on headlines, though.
  16640. ((save-excursion
  16641. (skip-chars-backward "a-zA-Z0-9*")
  16642. (skip-chars-backward " \t")
  16643. (and (bolp) (not (org-at-heading-p))))
  16644. (cdlatex-tab) t)
  16645. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16646. (defun org-cdlatex-underscore-caret (&optional arg)
  16647. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16648. Revert to the normal definition outside of these fragments."
  16649. (interactive "P")
  16650. (if (org-inside-LaTeX-fragment-p)
  16651. (call-interactively 'cdlatex-sub-superscript)
  16652. (let (org-cdlatex-mode)
  16653. (call-interactively (key-binding (vector last-input-event))))))
  16654. (defun org-cdlatex-math-modify (&optional arg)
  16655. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16656. Revert to the normal definition outside of these fragments."
  16657. (interactive "P")
  16658. (if (org-inside-LaTeX-fragment-p)
  16659. (call-interactively 'cdlatex-math-modify)
  16660. (let (org-cdlatex-mode)
  16661. (call-interactively (key-binding (vector last-input-event))))))
  16662. ;;;; LaTeX fragments
  16663. (defvar org-latex-regexps
  16664. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16665. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16666. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16667. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16668. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16669. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16670. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16671. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16672. "Regular expressions for matching embedded LaTeX.")
  16673. (defun org-inside-LaTeX-fragment-p ()
  16674. "Test if point is inside a LaTeX fragment.
  16675. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16676. sequence appearing also before point.
  16677. Even though the matchers for math are configurable, this function assumes
  16678. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16679. delimiters are skipped when they have been removed by customization.
  16680. The return value is nil, or a cons cell with the delimiter and the
  16681. position of this delimiter.
  16682. This function does a reasonably good job, but can locally be fooled by
  16683. for example currency specifications. For example it will assume being in
  16684. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16685. fragments that are properly closed, but during editing, we have to live
  16686. with the uncertainty caused by missing closing delimiters. This function
  16687. looks only before point, not after."
  16688. (catch 'exit
  16689. (let ((pos (point))
  16690. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16691. (lim (progn
  16692. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16693. (point)))
  16694. dd-on str (start 0) m re)
  16695. (goto-char pos)
  16696. (when dodollar
  16697. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16698. re (nth 1 (assoc "$" org-latex-regexps)))
  16699. (while (string-match re str start)
  16700. (cond
  16701. ((= (match-end 0) (length str))
  16702. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16703. ((= (match-end 0) (- (length str) 5))
  16704. (throw 'exit nil))
  16705. (t (setq start (match-end 0))))))
  16706. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16707. (goto-char pos)
  16708. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16709. (and (match-beginning 2) (throw 'exit nil))
  16710. ;; count $$
  16711. (while (re-search-backward "\\$\\$" lim t)
  16712. (setq dd-on (not dd-on)))
  16713. (goto-char pos)
  16714. (if dd-on (cons "$$" m))))))
  16715. (defun org-inside-latex-macro-p ()
  16716. "Is point inside a LaTeX macro or its arguments?"
  16717. (save-match-data
  16718. (org-in-regexp
  16719. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16720. (defvar org-latex-fragment-image-overlays nil
  16721. "List of overlays carrying the images of latex fragments.")
  16722. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16723. (defun org-remove-latex-fragment-image-overlays ()
  16724. "Remove all overlays with LaTeX fragment images in current buffer."
  16725. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16726. (setq org-latex-fragment-image-overlays nil))
  16727. (defun org-preview-latex-fragment (&optional subtree)
  16728. "Preview the LaTeX fragment at point, or all locally or globally.
  16729. If the cursor is in a LaTeX fragment, create the image and overlay
  16730. it over the source code. If there is no fragment at point, display
  16731. all fragments in the current text, from one headline to the next. With
  16732. prefix SUBTREE, display all fragments in the current subtree. With a
  16733. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16734. the cursor is before the first headline,
  16735. display all fragments in the buffer.
  16736. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16737. (interactive "P")
  16738. (unless buffer-file-name
  16739. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16740. (when (display-graphic-p)
  16741. (org-remove-latex-fragment-image-overlays)
  16742. (save-excursion
  16743. (save-restriction
  16744. (let (beg end at msg)
  16745. (cond
  16746. ((or (equal subtree '(16))
  16747. (not (save-excursion
  16748. (re-search-backward org-outline-regexp-bol nil t))))
  16749. (setq beg (point-min) end (point-max)
  16750. msg "Creating images for buffer...%s"))
  16751. ((equal subtree '(4))
  16752. (org-back-to-heading)
  16753. (setq beg (point) end (org-end-of-subtree t)
  16754. msg "Creating images for subtree...%s"))
  16755. (t
  16756. (if (setq at (org-inside-LaTeX-fragment-p))
  16757. (goto-char (max (point-min) (- (cdr at) 2)))
  16758. (org-back-to-heading))
  16759. (setq beg (point) end (progn (outline-next-heading) (point))
  16760. msg (if at "Creating image...%s"
  16761. "Creating images for entry...%s"))))
  16762. (message msg "")
  16763. (narrow-to-region beg end)
  16764. (goto-char beg)
  16765. (org-format-latex
  16766. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16767. (file-name-nondirectory
  16768. buffer-file-name)))
  16769. default-directory 'overlays msg at 'forbuffer
  16770. org-latex-create-formula-image-program)
  16771. (message msg "done. Use `C-c C-c' to remove images."))))))
  16772. (defun org-format-latex (prefix &optional dir overlays msg at
  16773. forbuffer processing-type)
  16774. "Replace LaTeX fragments with links to an image, and produce images.
  16775. Some of the options can be changed using the variable
  16776. `org-format-latex-options'."
  16777. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16778. (let* ((prefixnodir (file-name-nondirectory prefix))
  16779. (absprefix (expand-file-name prefix dir))
  16780. (todir (file-name-directory absprefix))
  16781. (opt org-format-latex-options)
  16782. (optnew org-format-latex-options)
  16783. (matchers (plist-get opt :matchers))
  16784. (re-list org-latex-regexps)
  16785. (cnt 0) txt hash link beg end re e checkdir
  16786. string
  16787. m n block-type block linkfile movefile ov)
  16788. ;; Check the different regular expressions
  16789. (while (setq e (pop re-list))
  16790. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16791. block (if block-type "\n\n" ""))
  16792. (when (member m matchers)
  16793. (goto-char (point-min))
  16794. (while (re-search-forward re nil t)
  16795. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16796. (or (not overlays)
  16797. (not (eq (get-char-property (match-beginning n)
  16798. 'org-overlay-type)
  16799. 'org-latex-overlay))))
  16800. (cond
  16801. ((eq processing-type 'verbatim))
  16802. ((eq processing-type 'mathjax)
  16803. ;; Prepare for MathJax processing.
  16804. (setq string (match-string n))
  16805. (when (member m '("$" "$1"))
  16806. (save-excursion
  16807. (delete-region (match-beginning n) (match-end n))
  16808. (goto-char (match-beginning n))
  16809. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16810. ((or (eq processing-type 'dvipng)
  16811. (eq processing-type 'imagemagick))
  16812. ;; Process to an image.
  16813. (setq txt (match-string n)
  16814. beg (match-beginning n) end (match-end n)
  16815. cnt (1+ cnt))
  16816. (let ((face (face-at-point))
  16817. (fg (plist-get opt :foreground))
  16818. (bg (plist-get opt :background))
  16819. ;; Ensure full list is printed.
  16820. print-length print-level)
  16821. (when forbuffer
  16822. ;; Get the colors from the face at point.
  16823. (goto-char beg)
  16824. (when (eq fg 'auto)
  16825. (setq fg (face-attribute face :foreground nil 'default)))
  16826. (when (eq bg 'auto)
  16827. (setq bg (face-attribute face :background nil 'default)))
  16828. (setq optnew (copy-sequence opt))
  16829. (plist-put optnew :foreground fg)
  16830. (plist-put optnew :background bg))
  16831. (setq hash (sha1 (prin1-to-string
  16832. (list org-format-latex-header
  16833. org-latex-default-packages-alist
  16834. org-latex-packages-alist
  16835. org-format-latex-options
  16836. forbuffer txt fg bg)))
  16837. linkfile (format "%s_%s.png" prefix hash)
  16838. movefile (format "%s_%s.png" absprefix hash)))
  16839. (setq link (concat block "[[file:" linkfile "]]" block))
  16840. (if msg (message msg cnt))
  16841. (goto-char beg)
  16842. (unless checkdir ; Ensure the directory exists.
  16843. (setq checkdir t)
  16844. (or (file-directory-p todir) (make-directory todir t)))
  16845. (unless (file-exists-p movefile)
  16846. (org-create-formula-image
  16847. txt movefile optnew forbuffer processing-type))
  16848. (if overlays
  16849. (progn
  16850. (mapc (lambda (o)
  16851. (if (eq (overlay-get o 'org-overlay-type)
  16852. 'org-latex-overlay)
  16853. (delete-overlay o)))
  16854. (overlays-in beg end))
  16855. (setq ov (make-overlay beg end))
  16856. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16857. (if (featurep 'xemacs)
  16858. (progn
  16859. (overlay-put ov 'invisible t)
  16860. (overlay-put
  16861. ov 'end-glyph
  16862. (make-glyph (vector 'png :file movefile))))
  16863. (overlay-put
  16864. ov 'display
  16865. (list 'image :type 'png :file movefile :ascent 'center)))
  16866. (push ov org-latex-fragment-image-overlays)
  16867. (goto-char end))
  16868. (delete-region beg end)
  16869. (insert (org-add-props link
  16870. (list 'org-latex-src
  16871. (replace-regexp-in-string
  16872. "\"" "" txt)
  16873. 'org-latex-src-embed-type
  16874. (if block-type 'paragraph 'character))))))
  16875. ((eq processing-type 'mathml)
  16876. ;; Process to MathML
  16877. (unless (save-match-data (org-format-latex-mathml-available-p))
  16878. (user-error "LaTeX to MathML converter not configured"))
  16879. (setq txt (match-string n)
  16880. beg (match-beginning n) end (match-end n)
  16881. cnt (1+ cnt))
  16882. (if msg (message msg cnt))
  16883. (goto-char beg)
  16884. (delete-region beg end)
  16885. (insert (org-format-latex-as-mathml
  16886. txt block-type prefix dir)))
  16887. (t
  16888. (error "Unknown conversion type %s for LaTeX fragments"
  16889. processing-type)))))))))
  16890. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16891. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16892. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16893. sucessful, return the portion between \"<math...> </math>\"
  16894. elements otherwise return nil. When MATHML-FILE is specified,
  16895. write the results in to that file. When invoked as an
  16896. interactive command, prompt for LATEX-FRAG, with initial value
  16897. set to the current active region and echo the results for user
  16898. inspection."
  16899. (interactive (list (let ((frag (when (org-region-active-p)
  16900. (buffer-substring-no-properties
  16901. (region-beginning) (region-end)))))
  16902. (read-string "LaTeX Fragment: " frag nil frag))))
  16903. (unless latex-frag (error "Invalid LaTeX fragment"))
  16904. (let* ((tmp-in-file (file-relative-name
  16905. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16906. (ignore (write-region latex-frag nil tmp-in-file))
  16907. (tmp-out-file (file-relative-name
  16908. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16909. (cmd (format-spec
  16910. org-latex-to-mathml-convert-command
  16911. `((?j . ,(shell-quote-argument
  16912. (expand-file-name org-latex-to-mathml-jar-file)))
  16913. (?I . ,(shell-quote-argument tmp-in-file))
  16914. (?o . ,(shell-quote-argument tmp-out-file)))))
  16915. mathml shell-command-output)
  16916. (when (org-called-interactively-p 'any)
  16917. (unless (org-format-latex-mathml-available-p)
  16918. (user-error "LaTeX to MathML converter not configured")))
  16919. (message "Running %s" cmd)
  16920. (setq shell-command-output (shell-command-to-string cmd))
  16921. (setq mathml
  16922. (when (file-readable-p tmp-out-file)
  16923. (with-current-buffer (find-file-noselect tmp-out-file t)
  16924. (goto-char (point-min))
  16925. (when (re-search-forward
  16926. (concat
  16927. (regexp-quote
  16928. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16929. "\\(.\\|\n\\)*"
  16930. (regexp-quote "</math>")) nil t)
  16931. (prog1 (match-string 0) (kill-buffer))))))
  16932. (cond
  16933. (mathml
  16934. (setq mathml
  16935. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16936. (when mathml-file
  16937. (write-region mathml nil mathml-file))
  16938. (when (org-called-interactively-p 'any)
  16939. (message mathml)))
  16940. ((message "LaTeX to MathML conversion failed")
  16941. (message shell-command-output)))
  16942. (delete-file tmp-in-file)
  16943. (when (file-exists-p tmp-out-file)
  16944. (delete-file tmp-out-file))
  16945. mathml))
  16946. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16947. prefix &optional dir)
  16948. "Use `org-create-math-formula' but check local cache first."
  16949. (let* ((absprefix (expand-file-name prefix dir))
  16950. (print-length nil) (print-level nil)
  16951. (formula-id (concat
  16952. "formula-"
  16953. (sha1
  16954. (prin1-to-string
  16955. (list latex-frag
  16956. org-latex-to-mathml-convert-command)))))
  16957. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16958. (formula-cache-dir (file-name-directory formula-cache)))
  16959. (unless (file-directory-p formula-cache-dir)
  16960. (make-directory formula-cache-dir t))
  16961. (unless (file-exists-p formula-cache)
  16962. (org-create-math-formula latex-frag formula-cache))
  16963. (if (file-exists-p formula-cache)
  16964. ;; Successful conversion. Return the link to MathML file.
  16965. (org-add-props
  16966. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16967. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16968. 'org-latex-src-embed-type (if latex-frag-type
  16969. 'paragraph 'character)))
  16970. ;; Failed conversion. Return the LaTeX fragment verbatim
  16971. latex-frag)))
  16972. (defun org-create-formula-image (string tofile options buffer &optional type)
  16973. "Create an image from LaTeX source using dvipng or convert.
  16974. This function calls either `org-create-formula-image-with-dvipng'
  16975. or `org-create-formula-image-with-imagemagick' depending on the
  16976. value of `org-latex-create-formula-image-program' or on the value
  16977. of the optional TYPE variable.
  16978. Note: ultimately these two function should be combined as they
  16979. share a good deal of logic."
  16980. (org-check-external-command
  16981. "latex" "needed to convert LaTeX fragments to images")
  16982. (funcall
  16983. (case (or type org-latex-create-formula-image-program)
  16984. ('dvipng
  16985. (org-check-external-command
  16986. "dvipng" "needed to convert LaTeX fragments to images")
  16987. #'org-create-formula-image-with-dvipng)
  16988. ('imagemagick
  16989. (org-check-external-command
  16990. "convert" "you need to install imagemagick")
  16991. #'org-create-formula-image-with-imagemagick)
  16992. (t (error
  16993. "Invalid value of `org-latex-create-formula-image-program'")))
  16994. string tofile options buffer))
  16995. (declare-function org-export-get-backend "ox" (name))
  16996. (declare-function org-export--get-global-options "ox" (&optional backend))
  16997. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16998. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  16999. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17000. (defun org-create-formula--latex-header ()
  17001. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17002. (let ((info (org-combine-plists (org-export--get-global-options
  17003. (org-export-get-backend 'latex))
  17004. (org-export--get-inbuffer-options
  17005. (org-export-get-backend 'latex)))))
  17006. (org-latex-guess-babel-language
  17007. (org-latex-guess-inputenc
  17008. (org-splice-latex-header
  17009. org-format-latex-header
  17010. org-latex-default-packages-alist
  17011. org-latex-packages-alist t
  17012. (plist-get info :latex-header)))
  17013. info)))
  17014. ;; This function borrows from Ganesh Swami's latex2png.el
  17015. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17016. "This calls dvipng."
  17017. (require 'ox-latex)
  17018. (let* ((tmpdir (if (featurep 'xemacs)
  17019. (temp-directory)
  17020. temporary-file-directory))
  17021. (texfilebase (make-temp-name
  17022. (expand-file-name "orgtex" tmpdir)))
  17023. (texfile (concat texfilebase ".tex"))
  17024. (dvifile (concat texfilebase ".dvi"))
  17025. (pngfile (concat texfilebase ".png"))
  17026. (fnh (if (featurep 'xemacs)
  17027. (font-height (face-font 'default))
  17028. (face-attribute 'default :height nil)))
  17029. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17030. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17031. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17032. "Black"))
  17033. (bg (or (plist-get options (if buffer :background :html-background))
  17034. "Transparent")))
  17035. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17036. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17037. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17038. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17039. (let ((latex-header (org-create-formula--latex-header)))
  17040. (with-temp-file texfile
  17041. (insert latex-header)
  17042. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17043. (let ((dir default-directory))
  17044. (condition-case nil
  17045. (progn
  17046. (cd tmpdir)
  17047. (call-process "latex" nil nil nil texfile))
  17048. (error nil))
  17049. (cd dir))
  17050. (if (not (file-exists-p dvifile))
  17051. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17052. (condition-case nil
  17053. (if (featurep 'xemacs)
  17054. (call-process "dvipng" nil nil nil
  17055. "-fg" fg "-bg" bg
  17056. "-T" "tight"
  17057. "-o" pngfile
  17058. dvifile)
  17059. (call-process "dvipng" nil nil nil
  17060. "-fg" fg "-bg" bg
  17061. "-D" dpi
  17062. ;;"-x" scale "-y" scale
  17063. "-T" "tight"
  17064. "-o" pngfile
  17065. dvifile))
  17066. (error nil))
  17067. (if (not (file-exists-p pngfile))
  17068. (if org-format-latex-signal-error
  17069. (error "Failed to create png file from %s" texfile)
  17070. (message "Failed to create png file from %s" texfile)
  17071. nil)
  17072. ;; Use the requested file name and clean up
  17073. (copy-file pngfile tofile 'replace)
  17074. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17075. (if (file-exists-p (concat texfilebase e))
  17076. (delete-file (concat texfilebase e))))
  17077. pngfile))))
  17078. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17079. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17080. "This calls convert, which is included into imagemagick."
  17081. (require 'ox-latex)
  17082. (let* ((tmpdir (if (featurep 'xemacs)
  17083. (temp-directory)
  17084. temporary-file-directory))
  17085. (texfilebase (make-temp-name
  17086. (expand-file-name "orgtex" tmpdir)))
  17087. (texfile (concat texfilebase ".tex"))
  17088. (pdffile (concat texfilebase ".pdf"))
  17089. (pngfile (concat texfilebase ".png"))
  17090. (fnh (if (featurep 'xemacs)
  17091. (font-height (face-font 'default))
  17092. (face-attribute 'default :height nil)))
  17093. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17094. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17095. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17096. "black"))
  17097. (bg (or (plist-get options (if buffer :background :html-background))
  17098. "white")))
  17099. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17100. (setq fg (org-latex-color-format fg)))
  17101. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17102. (setq bg (org-latex-color-format
  17103. (if (string= bg "Transparent") "white" bg))))
  17104. (let ((latex-header (org-create-formula--latex-header)))
  17105. (with-temp-file texfile
  17106. (insert latex-header)
  17107. (insert "\n\\begin{document}\n"
  17108. "\\definecolor{fg}{rgb}{" fg "}\n"
  17109. "\\definecolor{bg}{rgb}{" bg "}\n"
  17110. "\n\\pagecolor{bg}\n"
  17111. "\n{\\color{fg}\n"
  17112. string
  17113. "\n}\n"
  17114. "\n\\end{document}\n")))
  17115. (org-latex-compile texfile t)
  17116. (if (not (file-exists-p pdffile))
  17117. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17118. (condition-case nil
  17119. (if (featurep 'xemacs)
  17120. (call-process "convert" nil nil nil
  17121. "-density" "96"
  17122. "-trim"
  17123. "-antialias"
  17124. pdffile
  17125. "-quality" "100"
  17126. ;; "-sharpen" "0x1.0"
  17127. pngfile)
  17128. (call-process "convert" nil nil nil
  17129. "-density" dpi
  17130. "-trim"
  17131. "-antialias"
  17132. pdffile
  17133. "-quality" "100"
  17134. ;; "-sharpen" "0x1.0"
  17135. pngfile))
  17136. (error nil))
  17137. (if (not (file-exists-p pngfile))
  17138. (if org-format-latex-signal-error
  17139. (error "Failed to create png file from %s" texfile)
  17140. (message "Failed to create png file from %s" texfile)
  17141. nil)
  17142. ;; Use the requested file name and clean up
  17143. (copy-file pngfile tofile 'replace)
  17144. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17145. (if (file-exists-p (concat texfilebase e))
  17146. (delete-file (concat texfilebase e))))
  17147. pngfile))))
  17148. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17149. "Fill a LaTeX header template TPL.
  17150. In the template, the following place holders will be recognized:
  17151. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17152. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17153. [PACKAGES] \\usepackage statements for PKG
  17154. [NO-PACKAGES] do not include PKG
  17155. [EXTRA] the string EXTRA
  17156. [NO-EXTRA] do not include EXTRA
  17157. For backward compatibility, if both the positive and the negative place
  17158. holder is missing, the positive one (without the \"NO-\") will be
  17159. assumed to be present at the end of the template.
  17160. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17161. EXTRA is a string.
  17162. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17163. (let (rpl (end ""))
  17164. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17165. (setq rpl (if (or (match-end 1) (not def-pkg))
  17166. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17167. tpl (replace-match rpl t t tpl))
  17168. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17169. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17170. (setq rpl (if (or (match-end 1) (not pkg))
  17171. "" (org-latex-packages-to-string pkg snippets-p t))
  17172. tpl (replace-match rpl t t tpl))
  17173. (if pkg (setq end
  17174. (concat end "\n"
  17175. (org-latex-packages-to-string pkg snippets-p)))))
  17176. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17177. (setq rpl (if (or (match-end 1) (not extra))
  17178. "" (concat extra "\n"))
  17179. tpl (replace-match rpl t t tpl))
  17180. (if (and extra (string-match "\\S-" extra))
  17181. (setq end (concat end "\n" extra))))
  17182. (if (string-match "\\S-" end)
  17183. (concat tpl "\n" end)
  17184. tpl)))
  17185. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17186. "Turn an alist of packages into a string with the \\usepackage macros."
  17187. (setq pkg (mapconcat (lambda(p)
  17188. (cond
  17189. ((stringp p) p)
  17190. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17191. (format "%% Package %s omitted" (cadr p)))
  17192. ((equal "" (car p))
  17193. (format "\\usepackage{%s}" (cadr p)))
  17194. (t
  17195. (format "\\usepackage[%s]{%s}"
  17196. (car p) (cadr p)))))
  17197. pkg
  17198. "\n"))
  17199. (if newline (concat pkg "\n") pkg))
  17200. (defun org-dvipng-color (attr)
  17201. "Return a RGB color specification for dvipng."
  17202. (apply 'format "rgb %s %s %s"
  17203. (mapcar 'org-normalize-color
  17204. (if (featurep 'xemacs)
  17205. (color-rgb-components
  17206. (face-property 'default
  17207. (cond ((eq attr :foreground) 'foreground)
  17208. ((eq attr :background) 'background))))
  17209. (color-values (face-attribute 'default attr nil))))))
  17210. (defun org-dvipng-color-format (color-name)
  17211. "Convert COLOR-NAME to a RGB color value for dvipng."
  17212. (apply 'format "rgb %s %s %s"
  17213. (mapcar 'org-normalize-color
  17214. (color-values color-name))))
  17215. (defun org-latex-color (attr)
  17216. "Return a RGB color for the LaTeX color package."
  17217. (apply 'format "%s,%s,%s"
  17218. (mapcar 'org-normalize-color
  17219. (if (featurep 'xemacs)
  17220. (color-rgb-components
  17221. (face-property 'default
  17222. (cond ((eq attr :foreground) 'foreground)
  17223. ((eq attr :background) 'background))))
  17224. (color-values (face-attribute 'default attr nil))))))
  17225. (defun org-latex-color-format (color-name)
  17226. "Convert COLOR-NAME to a RGB color value."
  17227. (apply 'format "%s,%s,%s"
  17228. (mapcar 'org-normalize-color
  17229. (color-values color-name))))
  17230. (defun org-normalize-color (value)
  17231. "Return string to be used as color value for an RGB component."
  17232. (format "%g" (/ value 65535.0)))
  17233. ;; Image display
  17234. (defvar org-inline-image-overlays nil)
  17235. (make-variable-buffer-local 'org-inline-image-overlays)
  17236. (defun org-toggle-inline-images (&optional include-linked)
  17237. "Toggle the display of inline images.
  17238. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17239. (interactive "P")
  17240. (if org-inline-image-overlays
  17241. (progn
  17242. (org-remove-inline-images)
  17243. (message "Inline image display turned off"))
  17244. (org-display-inline-images include-linked)
  17245. (if (and (org-called-interactively-p)
  17246. org-inline-image-overlays)
  17247. (message "%d images displayed inline"
  17248. (length org-inline-image-overlays))
  17249. (message "No images to display inline"))))
  17250. (defun org-redisplay-inline-images ()
  17251. "Refresh the display of inline images."
  17252. (interactive)
  17253. (if (not org-inline-image-overlays)
  17254. (org-toggle-inline-images)
  17255. (org-toggle-inline-images)
  17256. (org-toggle-inline-images)))
  17257. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17258. "Display inline images.
  17259. Normally only links without a description part are inlined, because this
  17260. is how it will work for export. When INCLUDE-LINKED is set, also links
  17261. with a description part will be inlined. This can be nice for a quick
  17262. look at those images, but it does not reflect what exported files will look
  17263. like.
  17264. When REFRESH is set, refresh existing images between BEG and END.
  17265. This will create new image displays only if necessary.
  17266. BEG and END default to the buffer boundaries."
  17267. (interactive "P")
  17268. (when (display-graphic-p)
  17269. (unless refresh
  17270. (org-remove-inline-images)
  17271. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17272. (save-excursion
  17273. (save-restriction
  17274. (widen)
  17275. (setq beg (or beg (point-min)) end (or end (point-max)))
  17276. (goto-char beg)
  17277. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17278. (substring (org-image-file-name-regexp) 0 -2)
  17279. "\\)\\]" (if include-linked "" "\\]")))
  17280. (case-fold-search t)
  17281. old file ov img type attrwidth width)
  17282. (while (re-search-forward re end t)
  17283. (setq old (get-char-property-and-overlay (match-beginning 1)
  17284. 'org-image-overlay)
  17285. file (expand-file-name
  17286. (concat (or (match-string 3) "") (match-string 4))))
  17287. (when (image-type-available-p 'imagemagick)
  17288. (setq attrwidth (if (or (listp org-image-actual-width)
  17289. (null org-image-actual-width))
  17290. (save-excursion
  17291. (save-match-data
  17292. (when (re-search-backward
  17293. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17294. (save-excursion
  17295. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17296. (string-to-number (match-string 1))))))
  17297. width (cond ((eq org-image-actual-width t) nil)
  17298. ((null org-image-actual-width) attrwidth)
  17299. ((numberp org-image-actual-width)
  17300. org-image-actual-width)
  17301. ((listp org-image-actual-width)
  17302. (or attrwidth (car org-image-actual-width))))
  17303. type (if width 'imagemagick)))
  17304. (when (file-exists-p file)
  17305. (if (and (car-safe old) refresh)
  17306. (image-refresh (overlay-get (cdr old) 'display))
  17307. (setq img (save-match-data (create-image file type nil :width width)))
  17308. (when img
  17309. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17310. (overlay-put ov 'display img)
  17311. (overlay-put ov 'face 'default)
  17312. (overlay-put ov 'org-image-overlay t)
  17313. (overlay-put ov 'modification-hooks
  17314. (list 'org-display-inline-remove-overlay))
  17315. (push ov org-inline-image-overlays))))))))))
  17316. (define-obsolete-function-alias
  17317. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17318. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17319. "Remove inline-display overlay if a corresponding region is modified."
  17320. (let ((inhibit-modification-hooks t))
  17321. (when (and ov after)
  17322. (delete ov org-inline-image-overlays)
  17323. (delete-overlay ov))))
  17324. (defun org-remove-inline-images ()
  17325. "Remove inline display of images."
  17326. (interactive)
  17327. (mapc 'delete-overlay org-inline-image-overlays)
  17328. (setq org-inline-image-overlays nil))
  17329. ;;;; Key bindings
  17330. ;; Outline functions from `outline-mode-prefix-map'
  17331. ;; that can be remapped in Org:
  17332. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17333. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17334. (define-key org-mode-map [remap outline-forward-same-level]
  17335. 'org-forward-heading-same-level)
  17336. (define-key org-mode-map [remap outline-backward-same-level]
  17337. 'org-backward-heading-same-level)
  17338. (define-key org-mode-map [remap show-branches]
  17339. 'org-kill-note-or-show-branches)
  17340. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17341. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17342. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17343. ;; Outline functions from `outline-mode-prefix-map' that can not
  17344. ;; be remapped in Org:
  17345. ;;
  17346. ;; - the column "key binding" shows whether the Outline function is still
  17347. ;; available in Org mode on the same key that it has been bound to in
  17348. ;; Outline mode:
  17349. ;; - "overridden": key used for a different functionality in Org mode
  17350. ;; - else: key still bound to the same Outline function in Org mode
  17351. ;;
  17352. ;; | Outline function | key binding | Org replacement |
  17353. ;; |------------------------------------+-------------+-----------------------|
  17354. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17355. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17356. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17357. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17358. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17359. ;; | `show-entry' | overridden | no replacement |
  17360. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17361. ;; | `show-branches' | `C-c C-k' | still same function |
  17362. ;; | `show-subtree' | overridden | visibility cycling |
  17363. ;; | `show-all' | overridden | no replacement |
  17364. ;; | `hide-subtree' | overridden | visibility cycling |
  17365. ;; | `hide-body' | overridden | no replacement |
  17366. ;; | `hide-entry' | overridden | visibility cycling |
  17367. ;; | `hide-leaves' | overridden | no replacement |
  17368. ;; | `hide-sublevels' | overridden | no replacement |
  17369. ;; | `hide-other' | overridden | no replacement |
  17370. ;; Make `C-c C-x' a prefix key
  17371. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17372. ;; TAB key with modifiers
  17373. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17374. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17375. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17376. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17377. ;; The following line is necessary under Suse GNU/Linux
  17378. (unless (featurep 'xemacs)
  17379. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17380. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17381. (define-key org-mode-map [backtab] 'org-shifttab)
  17382. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17383. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17384. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17385. ;; Cursor keys with modifiers
  17386. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17387. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17388. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17389. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17390. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17391. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17392. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17393. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17394. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17395. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17396. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17397. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17398. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17399. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17400. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17401. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17402. ;; Babel keys
  17403. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17404. (mapc (lambda (pair)
  17405. (define-key org-babel-map (car pair) (cdr pair)))
  17406. org-babel-key-bindings)
  17407. ;;; Extra keys for tty access.
  17408. ;; We only set them when really needed because otherwise the
  17409. ;; menus don't show the simple keys
  17410. (when (or org-use-extra-keys
  17411. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17412. (not window-system))
  17413. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17414. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17415. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17416. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17417. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17418. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17419. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17420. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17421. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17422. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17423. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17424. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17425. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17426. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17427. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17428. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17429. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17430. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17431. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17432. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17433. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17434. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17435. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17436. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17437. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17438. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17439. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17440. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17441. ;; All the other keys
  17442. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17443. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17444. (if (boundp 'narrow-map)
  17445. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17446. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17447. (if (boundp 'narrow-map)
  17448. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17449. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17450. (if (boundp 'narrow-map)
  17451. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17452. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17453. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17454. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17455. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17456. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17457. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17458. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17459. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17460. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17461. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17462. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17463. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17464. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17465. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17466. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17467. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17468. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17469. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17470. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17471. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17472. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17473. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17474. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17475. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17476. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17477. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17478. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17479. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17480. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17481. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17482. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17483. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17484. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17485. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17486. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17487. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17488. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17489. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17490. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17491. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17492. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17493. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17494. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17495. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17496. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17497. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17498. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17499. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17500. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17501. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17502. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17503. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17504. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17505. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17506. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17507. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17508. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17509. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17510. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17511. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17512. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17513. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-element)
  17514. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-element)
  17515. (org-defkey org-mode-map "\C-m" 'org-return)
  17516. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17517. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17518. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17519. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17520. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17521. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17522. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17523. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17524. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17525. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17526. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17527. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17528. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17529. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17530. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17531. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17532. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17533. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17534. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17535. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17536. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17537. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17538. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17539. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17540. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17541. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17542. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17543. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17544. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17545. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17546. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17547. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17548. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17549. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17550. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17551. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17552. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17553. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17554. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17555. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17556. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17557. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17558. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17559. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17560. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17561. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17562. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17563. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17564. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17565. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17566. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17567. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17568. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17569. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17570. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17571. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17572. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17573. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17574. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17575. (when (featurep 'xemacs)
  17576. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17577. (defconst org-speed-commands-default
  17578. '(
  17579. ("Outline Navigation")
  17580. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17581. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17582. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17583. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17584. ("F" . org-next-block)
  17585. ("B" . org-previous-block)
  17586. ("u" . (org-speed-move-safe 'outline-up-heading))
  17587. ("j" . org-goto)
  17588. ("g" . (org-refile t))
  17589. ("Outline Visibility")
  17590. ("c" . org-cycle)
  17591. ("C" . org-shifttab)
  17592. (" " . org-display-outline-path)
  17593. ("s" . org-narrow-to-subtree)
  17594. ("=" . org-columns)
  17595. ("Outline Structure Editing")
  17596. ("U" . org-shiftmetaup)
  17597. ("D" . org-shiftmetadown)
  17598. ("r" . org-metaright)
  17599. ("l" . org-metaleft)
  17600. ("R" . org-shiftmetaright)
  17601. ("L" . org-shiftmetaleft)
  17602. ("i" . (progn (forward-char 1) (call-interactively
  17603. 'org-insert-heading-respect-content)))
  17604. ("^" . org-sort)
  17605. ("w" . org-refile)
  17606. ("a" . org-archive-subtree-default-with-confirmation)
  17607. ("@" . org-mark-subtree)
  17608. ("#" . org-toggle-comment)
  17609. ("Clock Commands")
  17610. ("I" . org-clock-in)
  17611. ("O" . org-clock-out)
  17612. ("Meta Data Editing")
  17613. ("t" . org-todo)
  17614. ("," . (org-priority))
  17615. ("0" . (org-priority ?\ ))
  17616. ("1" . (org-priority ?A))
  17617. ("2" . (org-priority ?B))
  17618. ("3" . (org-priority ?C))
  17619. (":" . org-set-tags-command)
  17620. ("e" . org-set-effort)
  17621. ("E" . org-inc-effort)
  17622. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17623. (org-entry-put (point) "APPT_WARNTIME" m)))
  17624. ("Agenda Views etc")
  17625. ("v" . org-agenda)
  17626. ("/" . org-sparse-tree)
  17627. ("Misc")
  17628. ("o" . org-open-at-point)
  17629. ("?" . org-speed-command-help)
  17630. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17631. (">" . (org-agenda-remove-restriction-lock))
  17632. )
  17633. "The default speed commands.")
  17634. (defun org-print-speed-command (e)
  17635. (if (> (length (car e)) 1)
  17636. (progn
  17637. (princ "\n")
  17638. (princ (car e))
  17639. (princ "\n")
  17640. (princ (make-string (length (car e)) ?-))
  17641. (princ "\n"))
  17642. (princ (car e))
  17643. (princ " ")
  17644. (if (symbolp (cdr e))
  17645. (princ (symbol-name (cdr e)))
  17646. (prin1 (cdr e)))
  17647. (princ "\n")))
  17648. (defun org-speed-command-help ()
  17649. "Show the available speed commands."
  17650. (interactive)
  17651. (if (not org-use-speed-commands)
  17652. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17653. (with-output-to-temp-buffer "*Help*"
  17654. (princ "User-defined Speed commands\n===========================\n")
  17655. (mapc 'org-print-speed-command org-speed-commands-user)
  17656. (princ "\n")
  17657. (princ "Built-in Speed commands\n=======================\n")
  17658. (mapc 'org-print-speed-command org-speed-commands-default))
  17659. (with-current-buffer "*Help*"
  17660. (setq truncate-lines t))))
  17661. (defun org-speed-move-safe (cmd)
  17662. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17663. If not, return to the original position and throw an error."
  17664. (interactive)
  17665. (let ((pos (point)))
  17666. (call-interactively cmd)
  17667. (unless (and (bolp) (org-at-heading-p))
  17668. (goto-char pos)
  17669. (error "Boundary reached while executing %s" cmd))))
  17670. (defvar org-self-insert-command-undo-counter 0)
  17671. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17672. (defvar org-speed-command nil)
  17673. (define-obsolete-function-alias
  17674. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17675. (defun org-speed-command-activate (keys)
  17676. "Hook for activating single-letter speed commands.
  17677. `org-speed-commands-default' specifies a minimal command set.
  17678. Use `org-speed-commands-user' for further customization."
  17679. (when (or (and (bolp) (looking-at org-outline-regexp))
  17680. (and (functionp org-use-speed-commands)
  17681. (funcall org-use-speed-commands)))
  17682. (cdr (assoc keys (append org-speed-commands-user
  17683. org-speed-commands-default)))))
  17684. (define-obsolete-function-alias
  17685. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17686. (defun org-babel-speed-command-activate (keys)
  17687. "Hook for activating single-letter code block commands."
  17688. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17689. (cdr (assoc keys org-babel-key-bindings))))
  17690. (defcustom org-speed-command-hook
  17691. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17692. "Hook for activating speed commands at strategic locations.
  17693. Hook functions are called in sequence until a valid handler is
  17694. found.
  17695. Each hook takes a single argument, a user-pressed command key
  17696. which is also a `self-insert-command' from the global map.
  17697. Within the hook, examine the cursor position and the command key
  17698. and return nil or a valid handler as appropriate. Handler could
  17699. be one of an interactive command, a function, or a form.
  17700. Set `org-use-speed-commands' to non-nil value to enable this
  17701. hook. The default setting is `org-speed-command-activate'."
  17702. :group 'org-structure
  17703. :version "24.1"
  17704. :type 'hook)
  17705. (defun org-self-insert-command (N)
  17706. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17707. If the cursor is in a table looking at whitespace, the whitespace is
  17708. overwritten, and the table is not marked as requiring realignment."
  17709. (interactive "p")
  17710. (org-check-before-invisible-edit 'insert)
  17711. (cond
  17712. ((and org-use-speed-commands
  17713. (setq org-speed-command
  17714. (run-hook-with-args-until-success
  17715. 'org-speed-command-hook (this-command-keys))))
  17716. (cond
  17717. ((commandp org-speed-command)
  17718. (setq this-command org-speed-command)
  17719. (call-interactively org-speed-command))
  17720. ((functionp org-speed-command)
  17721. (funcall org-speed-command))
  17722. ((and org-speed-command (listp org-speed-command))
  17723. (eval org-speed-command))
  17724. (t (let (org-use-speed-commands)
  17725. (call-interactively 'org-self-insert-command)))))
  17726. ((and
  17727. (org-table-p)
  17728. (progn
  17729. ;; check if we blank the field, and if that triggers align
  17730. (and (featurep 'org-table) org-table-auto-blank-field
  17731. (member last-command
  17732. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17733. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17734. ;; got extra space, this field does not determine column width
  17735. (let (org-table-may-need-update) (org-table-blank-field))
  17736. ;; no extra space, this field may determine column width
  17737. (org-table-blank-field)))
  17738. t)
  17739. (eq N 1)
  17740. (looking-at "[^|\n]* |"))
  17741. (let (org-table-may-need-update)
  17742. (goto-char (1- (match-end 0)))
  17743. (backward-delete-char 1)
  17744. (goto-char (match-beginning 0))
  17745. (self-insert-command N)))
  17746. (t
  17747. (setq org-table-may-need-update t)
  17748. (self-insert-command N)
  17749. (org-fix-tags-on-the-fly)
  17750. (if org-self-insert-cluster-for-undo
  17751. (if (not (eq last-command 'org-self-insert-command))
  17752. (setq org-self-insert-command-undo-counter 1)
  17753. (if (>= org-self-insert-command-undo-counter 20)
  17754. (setq org-self-insert-command-undo-counter 1)
  17755. (and (> org-self-insert-command-undo-counter 0)
  17756. buffer-undo-list (listp buffer-undo-list)
  17757. (not (cadr buffer-undo-list)) ; remove nil entry
  17758. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17759. (setq org-self-insert-command-undo-counter
  17760. (1+ org-self-insert-command-undo-counter))))))))
  17761. (defun org-check-before-invisible-edit (kind)
  17762. "Check is editing if kind KIND would be dangerous with invisible text around.
  17763. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17764. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17765. (if (and org-catch-invisible-edits
  17766. (or (not (boundp 'visible-mode)) (not visible-mode))
  17767. (or (get-char-property (point) 'invisible)
  17768. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17769. ;; OK, we need to take a closer look
  17770. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17771. (invisible-before-point (if (bobp) nil (get-char-property
  17772. (1- (point)) 'invisible)))
  17773. (border-and-ok-direction
  17774. (or
  17775. ;; Check if we are acting predictably before invisible text
  17776. (and invisible-at-point (not invisible-before-point)
  17777. (memq kind '(insert delete-backward)))
  17778. ;; Check if we are acting predictably after invisible text
  17779. ;; This works not well, and I have turned it off. It seems
  17780. ;; better to always show and stop after invisible text.
  17781. ;; (and (not invisible-at-point) invisible-before-point
  17782. ;; (memq kind '(insert delete)))
  17783. )))
  17784. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17785. (memq invisible-before-point '(outline org-hide-block t)))
  17786. (if (eq org-catch-invisible-edits 'error)
  17787. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17788. (if (and org-custom-properties-overlays
  17789. (y-or-n-p "Display invisible properties in this buffer? "))
  17790. (org-toggle-custom-properties-visibility)
  17791. ;; Make the area visible
  17792. (save-excursion
  17793. (if invisible-before-point
  17794. (goto-char (previous-single-char-property-change
  17795. (point) 'invisible)))
  17796. (org-cycle))
  17797. (cond
  17798. ((eq org-catch-invisible-edits 'show)
  17799. ;; That's it, we do the edit after showing
  17800. (message
  17801. "Unfolding invisible region around point before editing")
  17802. (sit-for 1))
  17803. ((and (eq org-catch-invisible-edits 'smart)
  17804. border-and-ok-direction)
  17805. (message "Unfolding invisible region around point before editing"))
  17806. (t
  17807. ;; Don't do the edit, make the user repeat it in full visibility
  17808. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17809. (defun org-fix-tags-on-the-fly ()
  17810. (when (and (equal (char-after (point-at-bol)) ?*)
  17811. (org-at-heading-p))
  17812. (org-align-tags-here org-tags-column)))
  17813. (defun org-delete-backward-char (N)
  17814. "Like `delete-backward-char', insert whitespace at field end in tables.
  17815. When deleting backwards, in tables this function will insert whitespace in
  17816. front of the next \"|\" separator, to keep the table aligned. The table will
  17817. still be marked for re-alignment if the field did fill the entire column,
  17818. because, in this case the deletion might narrow the column."
  17819. (interactive "p")
  17820. (save-match-data
  17821. (org-check-before-invisible-edit 'delete-backward)
  17822. (if (and (org-table-p)
  17823. (eq N 1)
  17824. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17825. (looking-at ".*?|"))
  17826. (let ((pos (point))
  17827. (noalign (looking-at "[^|\n\r]* |"))
  17828. (c org-table-may-need-update))
  17829. (backward-delete-char N)
  17830. (if (not overwrite-mode)
  17831. (progn
  17832. (skip-chars-forward "^|")
  17833. (insert " ")
  17834. (goto-char (1- pos))))
  17835. ;; noalign: if there were two spaces at the end, this field
  17836. ;; does not determine the width of the column.
  17837. (if noalign (setq org-table-may-need-update c)))
  17838. (backward-delete-char N)
  17839. (org-fix-tags-on-the-fly))))
  17840. (defun org-delete-char (N)
  17841. "Like `delete-char', but insert whitespace at field end in tables.
  17842. When deleting characters, in tables this function will insert whitespace in
  17843. front of the next \"|\" separator, to keep the table aligned. The table will
  17844. still be marked for re-alignment if the field did fill the entire column,
  17845. because, in this case the deletion might narrow the column."
  17846. (interactive "p")
  17847. (save-match-data
  17848. (org-check-before-invisible-edit 'delete)
  17849. (if (and (org-table-p)
  17850. (not (bolp))
  17851. (not (= (char-after) ?|))
  17852. (eq N 1))
  17853. (if (looking-at ".*?|")
  17854. (let ((pos (point))
  17855. (noalign (looking-at "[^|\n\r]* |"))
  17856. (c org-table-may-need-update))
  17857. (replace-match
  17858. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17859. (goto-char pos)
  17860. ;; noalign: if there were two spaces at the end, this field
  17861. ;; does not determine the width of the column.
  17862. (if noalign (setq org-table-may-need-update c)))
  17863. (delete-char N))
  17864. (delete-char N)
  17865. (org-fix-tags-on-the-fly))))
  17866. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17867. (put 'org-self-insert-command 'delete-selection t)
  17868. (put 'orgtbl-self-insert-command 'delete-selection t)
  17869. (put 'org-delete-char 'delete-selection 'supersede)
  17870. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17871. (put 'org-yank 'delete-selection 'yank)
  17872. ;; Make `flyspell-mode' delay after some commands
  17873. (put 'org-self-insert-command 'flyspell-delayed t)
  17874. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17875. (put 'org-delete-char 'flyspell-delayed t)
  17876. (put 'org-delete-backward-char 'flyspell-delayed t)
  17877. ;; Make pabbrev-mode expand after org-mode commands
  17878. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17879. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17880. ;; How to do this: Measure non-white length of current string
  17881. ;; If equal to column width, we should realign.
  17882. (defun org-remap (map &rest commands)
  17883. "In MAP, remap the functions given in COMMANDS.
  17884. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17885. (let (new old)
  17886. (while commands
  17887. (setq old (pop commands) new (pop commands))
  17888. (if (fboundp 'command-remapping)
  17889. (org-defkey map (vector 'remap old) new)
  17890. (substitute-key-definition old new map global-map)))))
  17891. (defun org-transpose-words ()
  17892. "Transpose words for Org.
  17893. This uses the `org-mode-transpose-word-syntax-table' syntax
  17894. table, which interprets characters in `org-emphasis-alist' as
  17895. word constituants."
  17896. (interactive)
  17897. (with-syntax-table org-mode-transpose-word-syntax-table
  17898. (call-interactively 'transpose-words)))
  17899. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17900. (when (eq org-enable-table-editor 'optimized)
  17901. ;; If the user wants maximum table support, we need to hijack
  17902. ;; some standard editing functions
  17903. (org-remap org-mode-map
  17904. 'self-insert-command 'org-self-insert-command
  17905. 'delete-char 'org-delete-char
  17906. 'delete-backward-char 'org-delete-backward-char)
  17907. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17908. (defvar org-ctrl-c-ctrl-c-hook nil
  17909. "Hook for functions attaching themselves to `C-c C-c'.
  17910. This can be used to add additional functionality to the C-c C-c
  17911. key which executes context-dependent commands. This hook is run
  17912. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17913. run after the last test.
  17914. Each function will be called with no arguments. The function
  17915. must check if the context is appropriate for it to act. If yes,
  17916. it should do its thing and then return a non-nil value. If the
  17917. context is wrong, just do nothing and return nil.")
  17918. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17919. "Hook for functions attaching themselves to `C-c C-c'.
  17920. This can be used to add additional functionality to the C-c C-c
  17921. key which executes context-dependent commands. This hook is run
  17922. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17923. before the first test.
  17924. Each function will be called with no arguments. The function
  17925. must check if the context is appropriate for it to act. If yes,
  17926. it should do its thing and then return a non-nil value. If the
  17927. context is wrong, just do nothing and return nil.")
  17928. (defvar org-tab-first-hook nil
  17929. "Hook for functions to attach themselves to TAB.
  17930. See `org-ctrl-c-ctrl-c-hook' for more information.
  17931. This hook runs as the first action when TAB is pressed, even before
  17932. `org-cycle' messes around with the `outline-regexp' to cater for
  17933. inline tasks and plain list item folding.
  17934. If any function in this hook returns t, any other actions that
  17935. would have been caused by TAB (such as table field motion or visibility
  17936. cycling) will not occur.")
  17937. (defvar org-tab-after-check-for-table-hook nil
  17938. "Hook for functions to attach themselves to TAB.
  17939. See `org-ctrl-c-ctrl-c-hook' for more information.
  17940. This hook runs after it has been established that the cursor is not in a
  17941. table, but before checking if the cursor is in a headline or if global cycling
  17942. should be done.
  17943. If any function in this hook returns t, not other actions like visibility
  17944. cycling will be done.")
  17945. (defvar org-tab-after-check-for-cycling-hook nil
  17946. "Hook for functions to attach themselves to TAB.
  17947. See `org-ctrl-c-ctrl-c-hook' for more information.
  17948. This hook runs after it has been established that not table field motion and
  17949. not visibility should be done because of current context. This is probably
  17950. the place where a package like yasnippets can hook in.")
  17951. (defvar org-tab-before-tab-emulation-hook nil
  17952. "Hook for functions to attach themselves to TAB.
  17953. See `org-ctrl-c-ctrl-c-hook' for more information.
  17954. This hook runs after every other options for TAB have been exhausted, but
  17955. before indentation and \t insertion takes place.")
  17956. (defvar org-metaleft-hook nil
  17957. "Hook for functions attaching themselves to `M-left'.
  17958. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17959. (defvar org-metaright-hook nil
  17960. "Hook for functions attaching themselves to `M-right'.
  17961. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17962. (defvar org-metaup-hook nil
  17963. "Hook for functions attaching themselves to `M-up'.
  17964. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17965. (defvar org-metadown-hook nil
  17966. "Hook for functions attaching themselves to `M-down'.
  17967. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17968. (defvar org-shiftmetaleft-hook nil
  17969. "Hook for functions attaching themselves to `M-S-left'.
  17970. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17971. (defvar org-shiftmetaright-hook nil
  17972. "Hook for functions attaching themselves to `M-S-right'.
  17973. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17974. (defvar org-shiftmetaup-hook nil
  17975. "Hook for functions attaching themselves to `M-S-up'.
  17976. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17977. (defvar org-shiftmetadown-hook nil
  17978. "Hook for functions attaching themselves to `M-S-down'.
  17979. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17980. (defvar org-metareturn-hook nil
  17981. "Hook for functions attaching themselves to `M-RET'.
  17982. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17983. (defvar org-shiftup-hook nil
  17984. "Hook for functions attaching themselves to `S-up'.
  17985. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17986. (defvar org-shiftup-final-hook nil
  17987. "Hook for functions attaching themselves to `S-up'.
  17988. This one runs after all other options except shift-select have been excluded.
  17989. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17990. (defvar org-shiftdown-hook nil
  17991. "Hook for functions attaching themselves to `S-down'.
  17992. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17993. (defvar org-shiftdown-final-hook nil
  17994. "Hook for functions attaching themselves to `S-down'.
  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-shiftleft-hook nil
  17998. "Hook for functions attaching themselves to `S-left'.
  17999. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18000. (defvar org-shiftleft-final-hook nil
  18001. "Hook for functions attaching themselves to `S-left'.
  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-shiftright-hook nil
  18005. "Hook for functions attaching themselves to `S-right'.
  18006. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18007. (defvar org-shiftright-final-hook nil
  18008. "Hook for functions attaching themselves to `S-right'.
  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. (defun org-modifier-cursor-error ()
  18012. "Throw an error, a modified cursor command was applied in wrong context."
  18013. (user-error "This command is active in special context like tables, headlines or items"))
  18014. (defun org-shiftselect-error ()
  18015. "Throw an error because Shift-Cursor command was applied in wrong context."
  18016. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18017. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18018. (user-error "This command works only in special context like headlines or timestamps")))
  18019. (defun org-call-for-shift-select (cmd)
  18020. (let ((this-command-keys-shift-translated t))
  18021. (call-interactively cmd)))
  18022. (defun org-shifttab (&optional arg)
  18023. "Global visibility cycling or move to previous table field.
  18024. Call `org-table-previous-field' within a table.
  18025. When ARG is nil, cycle globally through visibility states.
  18026. When ARG is a numeric prefix, show contents of this level."
  18027. (interactive "P")
  18028. (cond
  18029. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18030. ((integerp arg)
  18031. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18032. (message "Content view to level: %d" arg)
  18033. (org-content (prefix-numeric-value arg2))
  18034. (org-cycle-show-empty-lines t)
  18035. (setq org-cycle-global-status 'overview)))
  18036. (t (call-interactively 'org-global-cycle))))
  18037. (defun org-shiftmetaleft ()
  18038. "Promote subtree or delete table column.
  18039. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18040. `org-table-delete-column', depending on context. See the
  18041. individual commands for more information."
  18042. (interactive)
  18043. (cond
  18044. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18045. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18046. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18047. ((if (not (org-region-active-p)) (org-at-item-p)
  18048. (save-excursion (goto-char (region-beginning))
  18049. (org-at-item-p)))
  18050. (call-interactively 'org-outdent-item-tree))
  18051. (t (org-modifier-cursor-error))))
  18052. (defun org-shiftmetaright ()
  18053. "Demote subtree or insert table column.
  18054. Calls `org-demote-subtree', `org-indent-item-tree', or
  18055. `org-table-insert-column', depending on context. See the
  18056. individual commands for more information."
  18057. (interactive)
  18058. (cond
  18059. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18060. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18061. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18062. ((if (not (org-region-active-p)) (org-at-item-p)
  18063. (save-excursion (goto-char (region-beginning))
  18064. (org-at-item-p)))
  18065. (call-interactively 'org-indent-item-tree))
  18066. (t (org-modifier-cursor-error))))
  18067. (defun org-shiftmetaup (&optional arg)
  18068. "Move subtree up or kill table row.
  18069. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18070. `org-move-item-up' or `org-timestamp-up', depending on context.
  18071. See the individual commands for more information."
  18072. (interactive "P")
  18073. (cond
  18074. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18075. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18076. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18077. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18078. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18079. (call-interactively 'org-timestamp-up)))
  18080. (t (call-interactively 'org-drag-line-backward))))
  18081. (defun org-shiftmetadown (&optional arg)
  18082. "Move subtree down or insert table row.
  18083. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18084. `org-move-item-down' or `org-timestamp-up', depending on context.
  18085. See the individual commands for more information."
  18086. (interactive "P")
  18087. (cond
  18088. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18089. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18090. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18091. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18092. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18093. (call-interactively 'org-timestamp-down)))
  18094. (t (call-interactively 'org-drag-line-forward))))
  18095. (defsubst org-hidden-tree-error ()
  18096. (user-error
  18097. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18098. (defun org-metaleft (&optional arg)
  18099. "Promote heading or move table column to left.
  18100. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18101. With no specific context, calls the Emacs default `backward-word'.
  18102. See the individual commands for more information."
  18103. (interactive "P")
  18104. (cond
  18105. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18106. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18107. ((org-with-limited-levels
  18108. (or (org-at-heading-p)
  18109. (and (org-region-active-p)
  18110. (save-excursion
  18111. (goto-char (region-beginning))
  18112. (org-at-heading-p)))))
  18113. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18114. (call-interactively 'org-do-promote))
  18115. ;; At an inline task.
  18116. ((org-at-heading-p)
  18117. (call-interactively 'org-inlinetask-promote))
  18118. ((or (org-at-item-p)
  18119. (and (org-region-active-p)
  18120. (save-excursion
  18121. (goto-char (region-beginning))
  18122. (org-at-item-p))))
  18123. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18124. (call-interactively 'org-outdent-item))
  18125. (t (call-interactively 'backward-word))))
  18126. (defun org-metaright (&optional arg)
  18127. "Demote a subtree, a list item or move table column to right.
  18128. In front of a drawer or a block keyword, indent it correctly.
  18129. With no specific context, calls the Emacs default `forward-word'.
  18130. See the individual commands for more information."
  18131. (interactive "P")
  18132. (cond
  18133. ((run-hook-with-args-until-success 'org-metaright-hook))
  18134. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18135. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18136. ((org-at-block-p) (call-interactively 'org-indent-block))
  18137. ((org-with-limited-levels
  18138. (or (org-at-heading-p)
  18139. (and (org-region-active-p)
  18140. (save-excursion
  18141. (goto-char (region-beginning))
  18142. (org-at-heading-p)))))
  18143. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18144. (call-interactively 'org-do-demote))
  18145. ;; At an inline task.
  18146. ((org-at-heading-p)
  18147. (call-interactively 'org-inlinetask-demote))
  18148. ((or (org-at-item-p)
  18149. (and (org-region-active-p)
  18150. (save-excursion
  18151. (goto-char (region-beginning))
  18152. (org-at-item-p))))
  18153. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18154. (call-interactively 'org-indent-item))
  18155. (t (call-interactively 'forward-word))))
  18156. (defun org-check-for-hidden (what)
  18157. "Check if there are hidden headlines/items in the current visual line.
  18158. WHAT can be either `headlines' or `items'. If the current line is
  18159. an outline or item heading and it has a folded subtree below it,
  18160. this function returns t, nil otherwise."
  18161. (let ((re (cond
  18162. ((eq what 'headlines) org-outline-regexp-bol)
  18163. ((eq what 'items) (org-item-beginning-re))
  18164. (t (error "This should not happen"))))
  18165. beg end)
  18166. (save-excursion
  18167. (catch 'exit
  18168. (unless (org-region-active-p)
  18169. (setq beg (point-at-bol))
  18170. (beginning-of-line 2)
  18171. (while (and (not (eobp)) ;; this is like `next-line'
  18172. (get-char-property (1- (point)) 'invisible))
  18173. (beginning-of-line 2))
  18174. (setq end (point))
  18175. (goto-char beg)
  18176. (goto-char (point-at-eol))
  18177. (setq end (max end (point)))
  18178. (while (re-search-forward re end t)
  18179. (if (get-char-property (match-beginning 0) 'invisible)
  18180. (throw 'exit t))))
  18181. nil))))
  18182. (defun org-metaup (&optional arg)
  18183. "Move subtree up or move table row up.
  18184. Calls `org-move-subtree-up' or `org-table-move-row' or
  18185. `org-move-item-up', depending on context. See the individual commands
  18186. for more information."
  18187. (interactive "P")
  18188. (cond
  18189. ((run-hook-with-args-until-success 'org-metaup-hook))
  18190. ((org-region-active-p)
  18191. (let* ((a (min (region-beginning) (region-end)))
  18192. (b (1- (max (region-beginning) (region-end))))
  18193. (c (save-excursion (goto-char a)
  18194. (move-beginning-of-line 0)))
  18195. (d (save-excursion (goto-char a)
  18196. (move-end-of-line 0) (point))))
  18197. (transpose-regions a b c d)
  18198. (goto-char c)))
  18199. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18200. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18201. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18202. (t (org-drag-element-backward))))
  18203. (defun org-metadown (&optional arg)
  18204. "Move subtree down or move table row down.
  18205. Calls `org-move-subtree-down' or `org-table-move-row' or
  18206. `org-move-item-down', depending on context. See the individual
  18207. commands for more information."
  18208. (interactive "P")
  18209. (cond
  18210. ((run-hook-with-args-until-success 'org-metadown-hook))
  18211. ((org-region-active-p)
  18212. (let* ((a (min (region-beginning) (region-end)))
  18213. (b (max (region-beginning) (region-end)))
  18214. (c (save-excursion (goto-char b)
  18215. (move-beginning-of-line 1)))
  18216. (d (save-excursion (goto-char b)
  18217. (move-end-of-line 1) (1+ (point)))))
  18218. (transpose-regions a b c d)
  18219. (goto-char d)))
  18220. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18221. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18222. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18223. (t (org-drag-element-forward))))
  18224. (defun org-shiftup (&optional arg)
  18225. "Increase item in timestamp or increase priority of current headline.
  18226. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18227. depending on context. See the individual commands for more information."
  18228. (interactive "P")
  18229. (cond
  18230. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18231. ((and org-support-shift-select (org-region-active-p))
  18232. (org-call-for-shift-select 'previous-line))
  18233. ((org-at-timestamp-p t)
  18234. (call-interactively (if org-edit-timestamp-down-means-later
  18235. 'org-timestamp-down 'org-timestamp-up)))
  18236. ((and (not (eq org-support-shift-select 'always))
  18237. org-enable-priority-commands
  18238. (org-at-heading-p))
  18239. (call-interactively 'org-priority-up))
  18240. ((and (not org-support-shift-select) (org-at-item-p))
  18241. (call-interactively 'org-previous-item))
  18242. ((org-clocktable-try-shift 'up arg))
  18243. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18244. (org-support-shift-select
  18245. (org-call-for-shift-select 'previous-line))
  18246. (t (org-shiftselect-error))))
  18247. (defun org-shiftdown (&optional arg)
  18248. "Decrease item in timestamp or decrease priority of current headline.
  18249. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18250. depending on context. See the individual commands for more information."
  18251. (interactive "P")
  18252. (cond
  18253. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18254. ((and org-support-shift-select (org-region-active-p))
  18255. (org-call-for-shift-select 'next-line))
  18256. ((org-at-timestamp-p t)
  18257. (call-interactively (if org-edit-timestamp-down-means-later
  18258. 'org-timestamp-up 'org-timestamp-down)))
  18259. ((and (not (eq org-support-shift-select 'always))
  18260. org-enable-priority-commands
  18261. (org-at-heading-p))
  18262. (call-interactively 'org-priority-down))
  18263. ((and (not org-support-shift-select) (org-at-item-p))
  18264. (call-interactively 'org-next-item))
  18265. ((org-clocktable-try-shift 'down arg))
  18266. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18267. (org-support-shift-select
  18268. (org-call-for-shift-select 'next-line))
  18269. (t (org-shiftselect-error))))
  18270. (defun org-shiftright (&optional arg)
  18271. "Cycle the thing at point or in the current line, depending on context.
  18272. Depending on context, this does one of the following:
  18273. - switch a timestamp at point one day into the future
  18274. - on a headline, switch to the next TODO keyword.
  18275. - on an item, switch entire list to the next bullet type
  18276. - on a property line, switch to the next allowed value
  18277. - on a clocktable definition line, move time block into the future"
  18278. (interactive "P")
  18279. (cond
  18280. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18281. ((and org-support-shift-select (org-region-active-p))
  18282. (org-call-for-shift-select 'forward-char))
  18283. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18284. ((and (not (eq org-support-shift-select 'always))
  18285. (org-at-heading-p))
  18286. (let ((org-inhibit-logging
  18287. (not org-treat-S-cursor-todo-selection-as-state-change))
  18288. (org-inhibit-blocking
  18289. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18290. (org-call-with-arg 'org-todo 'right)))
  18291. ((or (and org-support-shift-select
  18292. (not (eq org-support-shift-select 'always))
  18293. (org-at-item-bullet-p))
  18294. (and (not org-support-shift-select) (org-at-item-p)))
  18295. (org-call-with-arg 'org-cycle-list-bullet nil))
  18296. ((and (not (eq org-support-shift-select 'always))
  18297. (org-at-property-p))
  18298. (call-interactively 'org-property-next-allowed-value))
  18299. ((org-clocktable-try-shift 'right arg))
  18300. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18301. (org-support-shift-select
  18302. (org-call-for-shift-select 'forward-char))
  18303. (t (org-shiftselect-error))))
  18304. (defun org-shiftleft (&optional arg)
  18305. "Cycle the thing at point or in the current line, depending on context.
  18306. Depending on context, this does one of the following:
  18307. - switch a timestamp at point one day into the past
  18308. - on a headline, switch to the previous TODO keyword.
  18309. - on an item, switch entire list to the previous bullet type
  18310. - on a property line, switch to the previous allowed value
  18311. - on a clocktable definition line, move time block into the past"
  18312. (interactive "P")
  18313. (cond
  18314. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18315. ((and org-support-shift-select (org-region-active-p))
  18316. (org-call-for-shift-select 'backward-char))
  18317. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18318. ((and (not (eq org-support-shift-select 'always))
  18319. (org-at-heading-p))
  18320. (let ((org-inhibit-logging
  18321. (not org-treat-S-cursor-todo-selection-as-state-change))
  18322. (org-inhibit-blocking
  18323. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18324. (org-call-with-arg 'org-todo 'left)))
  18325. ((or (and org-support-shift-select
  18326. (not (eq org-support-shift-select 'always))
  18327. (org-at-item-bullet-p))
  18328. (and (not org-support-shift-select) (org-at-item-p)))
  18329. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18330. ((and (not (eq org-support-shift-select 'always))
  18331. (org-at-property-p))
  18332. (call-interactively 'org-property-previous-allowed-value))
  18333. ((org-clocktable-try-shift 'left arg))
  18334. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18335. (org-support-shift-select
  18336. (org-call-for-shift-select 'backward-char))
  18337. (t (org-shiftselect-error))))
  18338. (defun org-shiftcontrolright ()
  18339. "Switch to next TODO set."
  18340. (interactive)
  18341. (cond
  18342. ((and org-support-shift-select (org-region-active-p))
  18343. (org-call-for-shift-select 'forward-word))
  18344. ((and (not (eq org-support-shift-select 'always))
  18345. (org-at-heading-p))
  18346. (org-call-with-arg 'org-todo 'nextset))
  18347. (org-support-shift-select
  18348. (org-call-for-shift-select 'forward-word))
  18349. (t (org-shiftselect-error))))
  18350. (defun org-shiftcontrolleft ()
  18351. "Switch to previous TODO set."
  18352. (interactive)
  18353. (cond
  18354. ((and org-support-shift-select (org-region-active-p))
  18355. (org-call-for-shift-select 'backward-word))
  18356. ((and (not (eq org-support-shift-select 'always))
  18357. (org-at-heading-p))
  18358. (org-call-with-arg 'org-todo 'previousset))
  18359. (org-support-shift-select
  18360. (org-call-for-shift-select 'backward-word))
  18361. (t (org-shiftselect-error))))
  18362. (defun org-shiftcontrolup (&optional n)
  18363. "Change timestamps synchronously up in CLOCK log lines.
  18364. Optional argument N tells to change by that many units."
  18365. (interactive "P")
  18366. (cond ((and (not org-support-shift-select)
  18367. (org-at-clock-log-p)
  18368. (org-at-timestamp-p t))
  18369. (org-clock-timestamps-up n))
  18370. (t (org-shiftselect-error))))
  18371. (defun org-shiftcontroldown (&optional n)
  18372. "Change timestamps synchronously down in CLOCK log lines.
  18373. Optional argument N tells to change by that many units."
  18374. (interactive "P")
  18375. (cond ((and (not org-support-shift-select)
  18376. (org-at-clock-log-p)
  18377. (org-at-timestamp-p t))
  18378. (org-clock-timestamps-down n))
  18379. (t (org-shiftselect-error))))
  18380. (defun org-ctrl-c-ret ()
  18381. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18382. (interactive)
  18383. (cond
  18384. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18385. (t (call-interactively 'org-insert-heading))))
  18386. (defun org-find-visible ()
  18387. (let ((s (point)))
  18388. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18389. (get-char-property s 'invisible)))
  18390. s))
  18391. (defun org-find-invisible ()
  18392. (let ((s (point)))
  18393. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18394. (not (get-char-property s 'invisible))))
  18395. s))
  18396. (defun org-copy-visible (beg end)
  18397. "Copy the visible parts of the region."
  18398. (interactive "r")
  18399. (let (snippets s)
  18400. (save-excursion
  18401. (save-restriction
  18402. (narrow-to-region beg end)
  18403. (setq s (goto-char (point-min)))
  18404. (while (not (= (point) (point-max)))
  18405. (goto-char (org-find-invisible))
  18406. (push (buffer-substring s (point)) snippets)
  18407. (setq s (goto-char (org-find-visible))))))
  18408. (kill-new (apply 'concat (nreverse snippets)))))
  18409. (defun org-copy-special ()
  18410. "Copy region in table or copy current subtree.
  18411. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18412. See the individual commands for more information."
  18413. (interactive)
  18414. (call-interactively
  18415. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18416. (defun org-cut-special ()
  18417. "Cut region in table or cut current subtree.
  18418. Calls `org-table-copy' or `org-cut-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-cut-region 'org-cut-subtree)))
  18423. (defun org-paste-special (arg)
  18424. "Paste rectangular region into table, or past subtree relative to level.
  18425. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18426. See the individual commands for more information."
  18427. (interactive "P")
  18428. (if (org-at-table-p)
  18429. (org-table-paste-rectangle)
  18430. (org-paste-subtree arg)))
  18431. (defsubst org-in-fixed-width-region-p ()
  18432. "Is point in a fixed-width region?"
  18433. (save-match-data
  18434. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18435. (defun org-edit-special (&optional arg)
  18436. "Call a special editor for the element at point.
  18437. When at a table, call the formula editor with `org-table-edit-formulas'.
  18438. When in a source code block, call `org-edit-src-code'.
  18439. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18440. When at an #+INCLUDE keyword, visit the included file.
  18441. On a link, call `ffap' to visit the link at point.
  18442. Otherwise, return a user error."
  18443. (interactive "P")
  18444. (let ((element (org-element-at-point)))
  18445. (assert (not buffer-read-only) nil
  18446. "Buffer is read-only: %s" (buffer-name))
  18447. (case (org-element-type element)
  18448. (src-block
  18449. (if (not arg) (org-edit-src-code)
  18450. (let* ((info (org-babel-get-src-block-info))
  18451. (lang (nth 0 info))
  18452. (params (nth 2 info))
  18453. (session (cdr (assq :session params))))
  18454. (if (not session) (org-edit-src-code)
  18455. ;; At a src-block with a session and function called with
  18456. ;; an ARG: switch to the buffer related to the inferior
  18457. ;; process.
  18458. (switch-to-buffer
  18459. (funcall (intern (concat "org-babel-prep-session:" lang))
  18460. session params))))))
  18461. (keyword
  18462. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18463. (find-file
  18464. (org-remove-double-quotes
  18465. (car (org-split-string (org-element-property :value element)))))
  18466. (user-error "No special environment to edit here")))
  18467. (table
  18468. (if (eq (org-element-property :type element) 'table.el)
  18469. (org-edit-src-code)
  18470. (call-interactively 'org-table-edit-formulas)))
  18471. ;; Only Org tables contain `table-row' type elements.
  18472. (table-row (call-interactively 'org-table-edit-formulas))
  18473. ((example-block export-block) (org-edit-src-code))
  18474. (fixed-width (org-edit-fixed-width-region))
  18475. (otherwise
  18476. ;; No notable element at point. Though, we may be at a link,
  18477. ;; which is an object. Thus, scan deeper.
  18478. (if (eq (org-element-type (org-element-context element)) 'link)
  18479. (call-interactively 'ffap)
  18480. (user-error "No special environment to edit here"))))))
  18481. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18482. (defun org-ctrl-c-ctrl-c (&optional arg)
  18483. "Set tags in headline, or update according to changed information at point.
  18484. This command does many different things, depending on context:
  18485. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18486. this is what we do.
  18487. - If the cursor is on a statistics cookie, update it.
  18488. - If the cursor is in a headline, prompt for tags and insert them
  18489. into the current line, aligned to `org-tags-column'. When called
  18490. with prefix arg, realign all tags in the current buffer.
  18491. - If the cursor is in one of the special #+KEYWORD lines, this
  18492. triggers scanning the buffer for these lines and updating the
  18493. information.
  18494. - If the cursor is inside a table, realign the table. This command
  18495. works even if the automatic table editor has been turned off.
  18496. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18497. the entire table.
  18498. - If the cursor is at a footnote reference or definition, jump to
  18499. the corresponding definition or references, respectively.
  18500. - If the cursor is a the beginning of a dynamic block, update it.
  18501. - If the current buffer is a capture buffer, close note and file it.
  18502. - If the cursor is on a <<<target>>>, update radio targets and
  18503. corresponding links in this buffer.
  18504. - If the cursor is on a numbered item in a plain list, renumber the
  18505. ordered list.
  18506. - If the cursor is on a checkbox, toggle it.
  18507. - If the cursor is on a code block, evaluate it. The variable
  18508. `org-confirm-babel-evaluate' can be used to control prompting
  18509. before code block evaluation, by default every code block
  18510. evaluation requires confirmation. Code block evaluation can be
  18511. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18512. (interactive "P")
  18513. (cond
  18514. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18515. org-occur-highlights
  18516. org-latex-fragment-image-overlays)
  18517. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18518. (org-remove-occur-highlights)
  18519. (org-remove-latex-fragment-image-overlays)
  18520. (message "Temporary highlights/overlays removed from current buffer"))
  18521. ((and (local-variable-p 'org-finish-function (current-buffer))
  18522. (fboundp org-finish-function))
  18523. (funcall org-finish-function))
  18524. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18525. (t
  18526. (let* ((context (org-element-context)) (type (org-element-type context)))
  18527. ;; Test if point is within a blank line.
  18528. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18529. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18530. (user-error "C-c C-c can do nothing useful at this location"))
  18531. ;; For convenience: at the first line of a paragraph on the
  18532. ;; same line as an item, apply function on that item instead.
  18533. (when (eq type 'paragraph)
  18534. (let ((parent (org-element-property :parent context)))
  18535. (when (and (eq (org-element-type parent) 'item)
  18536. (= (point-at-bol) (org-element-property :begin parent)))
  18537. (setq context parent type 'item))))
  18538. ;; When heading text is a link, treat the heading, not the link,
  18539. ;; as the current element
  18540. (when (eq type 'link)
  18541. (let ((parent (org-element-property :parent context)))
  18542. (when (and (eq (org-element-type parent) 'headline))
  18543. (setq context parent type 'headline))))
  18544. ;; Act according to type of element or object at point.
  18545. (case type
  18546. (clock (org-clock-update-time-maybe))
  18547. (dynamic-block
  18548. (save-excursion
  18549. (goto-char (org-element-property :post-affiliated context))
  18550. (org-update-dblock)))
  18551. (footnote-definition
  18552. (goto-char (org-element-property :post-affiliated context))
  18553. (call-interactively 'org-footnote-action))
  18554. (footnote-reference (call-interactively 'org-footnote-action))
  18555. ((headline inlinetask)
  18556. (save-excursion (goto-char (org-element-property :begin context))
  18557. (call-interactively 'org-set-tags)))
  18558. (item
  18559. ;; At an item: a double C-u set checkbox to "[-]"
  18560. ;; unconditionally, whereas a single one will toggle its
  18561. ;; presence. Without an universal argument, if the item
  18562. ;; has a checkbox, toggle it. Otherwise repair the list.
  18563. (let* ((box (org-element-property :checkbox context))
  18564. (struct (org-element-property :structure context))
  18565. (old-struct (copy-tree struct))
  18566. (parents (org-list-parents-alist struct))
  18567. (prevs (org-list-prevs-alist struct))
  18568. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18569. (org-list-set-checkbox
  18570. (org-element-property :begin context) struct
  18571. (cond ((equal arg '(16)) "[-]")
  18572. ((and (not box) (equal arg '(4))) "[ ]")
  18573. ((or (not box) (equal arg '(4))) nil)
  18574. ((eq box 'on) "[ ]")
  18575. (t "[X]")))
  18576. ;; Mimic `org-list-write-struct' but with grabbing
  18577. ;; a return value from `org-list-struct-fix-box'.
  18578. (org-list-struct-fix-ind struct parents 2)
  18579. (org-list-struct-fix-item-end struct)
  18580. (org-list-struct-fix-bul struct prevs)
  18581. (org-list-struct-fix-ind struct parents)
  18582. (let ((block-item
  18583. (org-list-struct-fix-box struct parents prevs orderedp)))
  18584. (if (and box (equal struct old-struct))
  18585. (if (equal arg '(16))
  18586. (message "Checkboxes already reset")
  18587. (user-error "Cannot toggle this checkbox: %s"
  18588. (if (eq box 'on)
  18589. "all subitems checked"
  18590. "unchecked subitems")))
  18591. (org-list-struct-apply-struct struct old-struct)
  18592. (org-update-checkbox-count-maybe))
  18593. (when block-item
  18594. (message "Checkboxes were removed due to empty box at line %d"
  18595. (org-current-line block-item))))))
  18596. (keyword
  18597. (let ((org-inhibit-startup-visibility-stuff t)
  18598. (org-startup-align-all-tables nil))
  18599. (when (boundp 'org-table-coordinate-overlays)
  18600. (mapc 'delete-overlay org-table-coordinate-overlays)
  18601. (setq org-table-coordinate-overlays nil))
  18602. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18603. (message "Local setup has been refreshed"))
  18604. (plain-list
  18605. ;; At a plain list, with a double C-u argument, set
  18606. ;; checkboxes of each item to "[-]", whereas a single one
  18607. ;; will toggle their presence according to the state of the
  18608. ;; first item in the list. Without an argument, repair the
  18609. ;; list.
  18610. (let* ((begin (org-element-property :contents-begin context))
  18611. (beginm (move-marker (make-marker) begin))
  18612. (struct (org-element-property :structure context))
  18613. (old-struct (copy-tree struct))
  18614. (first-box (save-excursion
  18615. (goto-char begin)
  18616. (looking-at org-list-full-item-re)
  18617. (match-string-no-properties 3)))
  18618. (new-box (cond ((equal arg '(16)) "[-]")
  18619. ((equal arg '(4)) (unless first-box "[ ]"))
  18620. ((equal first-box "[X]") "[ ]")
  18621. (t "[X]"))))
  18622. (cond
  18623. (arg
  18624. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18625. (org-list-get-all-items
  18626. begin struct (org-list-prevs-alist struct))))
  18627. ((and first-box (eq (point) begin))
  18628. ;; For convenience, when point is at bol on the first
  18629. ;; item of the list and no argument is provided, simply
  18630. ;; toggle checkbox of that item, if any.
  18631. (org-list-set-checkbox begin struct new-box)))
  18632. (org-list-write-struct
  18633. struct (org-list-parents-alist struct) old-struct)
  18634. (org-update-checkbox-count-maybe)
  18635. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18636. ((property-drawer node-property)
  18637. (call-interactively 'org-property-action))
  18638. ((radio-target target)
  18639. (call-interactively 'org-update-radio-target-regexp))
  18640. (statistics-cookie
  18641. (call-interactively 'org-update-statistics-cookies))
  18642. ((table table-cell table-row)
  18643. ;; At a table, recalculate every field and align it. Also
  18644. ;; send the table if necessary. If the table has
  18645. ;; a `table.el' type, just give up. At a table row or
  18646. ;; cell, maybe recalculate line but always align table.
  18647. (if (eq (org-element-property :type context) 'table.el)
  18648. (message "Use C-c ' to edit table.el tables")
  18649. (let ((org-enable-table-editor t))
  18650. (if (or (eq type 'table)
  18651. ;; Check if point is at a TBLFM line.
  18652. (and (eq type 'table-row)
  18653. (= (point) (org-element-property :end context))))
  18654. (save-excursion
  18655. (if (org-at-TBLFM-p)
  18656. (progn (require 'org-table)
  18657. (org-table-calc-current-TBLFM))
  18658. (goto-char (org-element-property :contents-begin context))
  18659. (org-call-with-arg 'org-table-recalculate (or arg t))
  18660. (orgtbl-send-table 'maybe)))
  18661. (org-table-maybe-eval-formula)
  18662. (cond (arg (call-interactively 'org-table-recalculate))
  18663. ((org-table-maybe-recalculate-line))
  18664. (t (org-table-align)))))))
  18665. (timestamp (org-timestamp-change 0 'day))
  18666. (otherwise
  18667. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18668. (user-error
  18669. "C-c C-c can do nothing useful at this location")))))))))
  18670. (defun org-mode-restart ()
  18671. "Restart Org-mode, to scan again for special lines.
  18672. Also updates the keyword regular expressions."
  18673. (interactive)
  18674. (org-mode)
  18675. (message "Org-mode restarted"))
  18676. (defun org-kill-note-or-show-branches ()
  18677. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18678. (interactive)
  18679. (if (not org-finish-function)
  18680. (progn
  18681. (hide-subtree)
  18682. (call-interactively 'show-branches))
  18683. (let ((org-note-abort t))
  18684. (funcall org-finish-function))))
  18685. (defun org-open-line (n)
  18686. "Insert a new row in tables, call `open-line' elsewhere."
  18687. (interactive "*p")
  18688. (if (org-at-table-p)
  18689. (org-table-insert-row)
  18690. (open-line n)))
  18691. (defun org-return (&optional indent)
  18692. "Goto next table row or insert a newline.
  18693. Calls `org-table-next-row' or `newline', depending on context.
  18694. See the individual commands for more information."
  18695. (interactive)
  18696. (let (org-ts-what)
  18697. (cond
  18698. ((or (bobp) (org-in-src-block-p))
  18699. (if indent (newline-and-indent) (newline)))
  18700. ((org-at-table-p)
  18701. (org-table-justify-field-maybe)
  18702. (call-interactively 'org-table-next-row))
  18703. ;; when `newline-and-indent' is called within a list, make sure
  18704. ;; text moved stays inside the item.
  18705. ((and (org-in-item-p) indent)
  18706. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18707. (progn
  18708. (save-match-data (newline))
  18709. (org-indent-line-to (length (match-string 0))))
  18710. (let ((ind (org-get-indentation)))
  18711. (newline)
  18712. (if (org-looking-back org-list-end-re)
  18713. (org-indent-line)
  18714. (org-indent-line-to ind)))))
  18715. ((and org-return-follows-link
  18716. (org-at-timestamp-p t)
  18717. (not (eq org-ts-what 'after)))
  18718. (org-follow-timestamp-link))
  18719. ((and org-return-follows-link
  18720. (let ((tprop (get-text-property (point) 'face)))
  18721. (or (eq tprop 'org-link)
  18722. (and (listp tprop) (memq 'org-link tprop)))))
  18723. (call-interactively 'org-open-at-point))
  18724. ((and (org-at-heading-p)
  18725. (looking-at
  18726. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18727. (org-show-entry)
  18728. (end-of-line 1)
  18729. (newline))
  18730. (t (if indent (newline-and-indent) (newline))))))
  18731. (defun org-return-indent ()
  18732. "Goto next table row or insert a newline and indent.
  18733. Calls `org-table-next-row' or `newline-and-indent', depending on
  18734. context. See the individual commands for more information."
  18735. (interactive)
  18736. (org-return t))
  18737. (defun org-ctrl-c-star ()
  18738. "Compute table, or change heading status of lines.
  18739. Calls `org-table-recalculate' or `org-toggle-heading',
  18740. depending on context."
  18741. (interactive)
  18742. (cond
  18743. ((org-at-table-p)
  18744. (call-interactively 'org-table-recalculate))
  18745. (t
  18746. ;; Convert all lines in region to list items
  18747. (call-interactively 'org-toggle-heading))))
  18748. (defun org-ctrl-c-minus ()
  18749. "Insert separator line in table or modify bullet status of line.
  18750. Also turns a plain line or a region of lines into list items.
  18751. Calls `org-table-insert-hline', `org-toggle-item', or
  18752. `org-cycle-list-bullet', depending on context."
  18753. (interactive)
  18754. (cond
  18755. ((org-at-table-p)
  18756. (call-interactively 'org-table-insert-hline))
  18757. ((org-region-active-p)
  18758. (call-interactively 'org-toggle-item))
  18759. ((org-in-item-p)
  18760. (call-interactively 'org-cycle-list-bullet))
  18761. (t
  18762. (call-interactively 'org-toggle-item))))
  18763. (defun org-toggle-item (arg)
  18764. "Convert headings or normal lines to items, items to normal lines.
  18765. If there is no active region, only the current line is considered.
  18766. If the first non blank line in the region is a headline, convert
  18767. all headlines to items, shifting text accordingly.
  18768. If it is an item, convert all items to normal lines.
  18769. If it is normal text, change region into a list of items.
  18770. With a prefix argument ARG, change the region in a single item."
  18771. (interactive "P")
  18772. (let ((shift-text
  18773. (function
  18774. ;; Shift text in current section to IND, from point to END.
  18775. ;; The function leaves point to END line.
  18776. (lambda (ind end)
  18777. (let ((min-i 1000) (end (copy-marker end)))
  18778. ;; First determine the minimum indentation (MIN-I) of
  18779. ;; the text.
  18780. (save-excursion
  18781. (catch 'exit
  18782. (while (< (point) end)
  18783. (let ((i (org-get-indentation)))
  18784. (cond
  18785. ;; Skip blank lines and inline tasks.
  18786. ((looking-at "^[ \t]*$"))
  18787. ((looking-at org-outline-regexp-bol))
  18788. ;; We can't find less than 0 indentation.
  18789. ((zerop i) (throw 'exit (setq min-i 0)))
  18790. ((< i min-i) (setq min-i i))))
  18791. (forward-line))))
  18792. ;; Then indent each line so that a line indented to
  18793. ;; MIN-I becomes indented to IND. Ignore blank lines
  18794. ;; and inline tasks in the process.
  18795. (let ((delta (- ind min-i)))
  18796. (while (< (point) end)
  18797. (unless (or (looking-at "^[ \t]*$")
  18798. (looking-at org-outline-regexp-bol))
  18799. (org-indent-line-to (+ (org-get-indentation) delta)))
  18800. (forward-line)))))))
  18801. (skip-blanks
  18802. (function
  18803. ;; Return beginning of first non-blank line, starting from
  18804. ;; line at POS.
  18805. (lambda (pos)
  18806. (save-excursion
  18807. (goto-char pos)
  18808. (skip-chars-forward " \r\t\n")
  18809. (point-at-bol)))))
  18810. beg end)
  18811. ;; Determine boundaries of changes.
  18812. (if (org-region-active-p)
  18813. (setq beg (funcall skip-blanks (region-beginning))
  18814. end (copy-marker (region-end)))
  18815. (setq beg (funcall skip-blanks (point-at-bol))
  18816. end (copy-marker (point-at-eol))))
  18817. ;; Depending on the starting line, choose an action on the text
  18818. ;; between BEG and END.
  18819. (org-with-limited-levels
  18820. (save-excursion
  18821. (goto-char beg)
  18822. (cond
  18823. ;; Case 1. Start at an item: de-itemize. Note that it only
  18824. ;; happens when a region is active: `org-ctrl-c-minus'
  18825. ;; would call `org-cycle-list-bullet' otherwise.
  18826. ((org-at-item-p)
  18827. (while (< (point) end)
  18828. (when (org-at-item-p)
  18829. (skip-chars-forward " \t")
  18830. (delete-region (point) (match-end 0)))
  18831. (forward-line)))
  18832. ;; Case 2. Start at an heading: convert to items.
  18833. ((org-at-heading-p)
  18834. (let* ((bul (org-list-bullet-string "-"))
  18835. (bul-len (length bul))
  18836. ;; Indentation of the first heading. It should be
  18837. ;; relative to the indentation of its parent, if any.
  18838. (start-ind (save-excursion
  18839. (cond
  18840. ((not org-adapt-indentation) 0)
  18841. ((not (outline-previous-heading)) 0)
  18842. (t (length (match-string 0))))))
  18843. ;; Level of first heading. Further headings will be
  18844. ;; compared to it to determine hierarchy in the list.
  18845. (ref-level (org-reduced-level (org-outline-level))))
  18846. (while (< (point) end)
  18847. (let* ((level (org-reduced-level (org-outline-level)))
  18848. (delta (max 0 (- level ref-level))))
  18849. ;; If current headline is less indented than the first
  18850. ;; one, set it as reference, in order to preserve
  18851. ;; subtrees.
  18852. (when (< level ref-level) (setq ref-level level))
  18853. (replace-match bul t t)
  18854. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18855. ;; Ensure all text down to END (or SECTION-END) belongs
  18856. ;; to the newly created item.
  18857. (let ((section-end (save-excursion
  18858. (or (outline-next-heading) (point)))))
  18859. (forward-line)
  18860. (funcall shift-text
  18861. (+ start-ind (* (1+ delta) bul-len))
  18862. (min end section-end)))))))
  18863. ;; Case 3. Normal line with ARG: make the first line of region
  18864. ;; an item, and shift indentation of others lines to
  18865. ;; set them as item's body.
  18866. (arg (let* ((bul (org-list-bullet-string "-"))
  18867. (bul-len (length bul))
  18868. (ref-ind (org-get-indentation)))
  18869. (skip-chars-forward " \t")
  18870. (insert bul)
  18871. (forward-line)
  18872. (while (< (point) end)
  18873. ;; Ensure that lines less indented than first one
  18874. ;; still get included in item body.
  18875. (funcall shift-text
  18876. (+ ref-ind bul-len)
  18877. (min end (save-excursion (or (outline-next-heading)
  18878. (point)))))
  18879. (forward-line))))
  18880. ;; Case 4. Normal line without ARG: turn each non-item line
  18881. ;; into an item.
  18882. (t
  18883. (while (< (point) end)
  18884. (unless (or (org-at-heading-p) (org-at-item-p))
  18885. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18886. (replace-match
  18887. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18888. (forward-line))))))))
  18889. (defun org-toggle-heading (&optional nstars)
  18890. "Convert headings to normal text, or items or text to headings.
  18891. If there is no active region, only convert the current line.
  18892. With a \\[universal-argument] prefix, convert the whole list at
  18893. point into heading.
  18894. In a region:
  18895. - If the first non blank line is a headline, remove the stars
  18896. from all headlines in the region.
  18897. - If it is a normal line, turn each and every normal line (i.e.,
  18898. not an heading or an item) in the region into headings. If you
  18899. want to convert only the first line of this region, use one
  18900. universal prefix argument.
  18901. - If it is a plain list item, turn all plain list items into headings.
  18902. When converting a line into a heading, the number of stars is chosen
  18903. such that the lines become children of the current entry. However,
  18904. when a numeric prefix argument is given, its value determines the
  18905. number of stars to add."
  18906. (interactive "P")
  18907. (let ((skip-blanks
  18908. (function
  18909. ;; Return beginning of first non-blank line, starting from
  18910. ;; line at POS.
  18911. (lambda (pos)
  18912. (save-excursion
  18913. (goto-char pos)
  18914. (while (org-at-comment-p) (forward-line))
  18915. (skip-chars-forward " \r\t\n")
  18916. (point-at-bol)))))
  18917. beg end toggled)
  18918. ;; Determine boundaries of changes. If a universal prefix has
  18919. ;; been given, put the list in a region. If region ends at a bol,
  18920. ;; do not consider the last line to be in the region.
  18921. (when (and current-prefix-arg (org-at-item-p))
  18922. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18923. (org-mark-element))
  18924. (if (org-region-active-p)
  18925. (setq beg (funcall skip-blanks (region-beginning))
  18926. end (copy-marker (save-excursion
  18927. (goto-char (region-end))
  18928. (if (bolp) (point) (point-at-eol)))))
  18929. (setq beg (funcall skip-blanks (point-at-bol))
  18930. end (copy-marker (point-at-eol))))
  18931. ;; Ensure inline tasks don't count as headings.
  18932. (org-with-limited-levels
  18933. (save-excursion
  18934. (goto-char beg)
  18935. (cond
  18936. ;; Case 1. Started at an heading: de-star headings.
  18937. ((org-at-heading-p)
  18938. (while (< (point) end)
  18939. (when (org-at-heading-p t)
  18940. (looking-at org-outline-regexp) (replace-match "")
  18941. (setq toggled t))
  18942. (forward-line)))
  18943. ;; Case 2. Started at an item: change items into headlines.
  18944. ;; One star will be added by `org-list-to-subtree'.
  18945. ((org-at-item-p)
  18946. (let* ((stars (make-string
  18947. ;; subtract the star that will be added again by
  18948. ;; `org-list-to-subtree'
  18949. (if (numberp nstars) (1- nstars)
  18950. (or (org-current-level) 0))
  18951. ?*))
  18952. (add-stars
  18953. (cond (nstars "") ; stars from prefix only
  18954. ((equal stars "") "") ; before first heading
  18955. (org-odd-levels-only "*") ; inside heading, odd
  18956. (t "")))) ; inside heading, oddeven
  18957. (while (< (point) end)
  18958. (when (org-at-item-p)
  18959. ;; Pay attention to cases when region ends before list.
  18960. (let* ((struct (org-list-struct))
  18961. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18962. (save-restriction
  18963. (narrow-to-region (point) list-end)
  18964. (insert
  18965. (org-list-to-subtree
  18966. (org-list-parse-list t)
  18967. '(:istart (concat stars add-stars (funcall get-stars depth))
  18968. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18969. (setq toggled t))
  18970. (forward-line))))
  18971. ;; Case 3. Started at normal text: make every line an heading,
  18972. ;; skipping headlines and items.
  18973. (t (let* ((stars
  18974. (make-string
  18975. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18976. (add-stars
  18977. (cond (nstars "") ; stars from prefix only
  18978. ((equal stars "") "*") ; before first heading
  18979. (org-odd-levels-only "**") ; inside heading, odd
  18980. (t "*"))) ; inside heading, oddeven
  18981. (rpl (concat stars add-stars " "))
  18982. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18983. (while (< (point) (if (equal nstars '(4)) lend end))
  18984. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18985. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18986. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18987. (forward-line)))))))
  18988. (unless toggled (message "Cannot toggle heading from here"))))
  18989. (defun org-meta-return (&optional arg)
  18990. "Insert a new heading or wrap a region in a table.
  18991. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18992. See the individual commands for more information."
  18993. (interactive "P")
  18994. (org-check-before-invisible-edit 'insert)
  18995. (cond
  18996. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18997. ((or (org-at-drawer-p) (org-in-drawer-p) (org-at-property-p))
  18998. (newline-and-indent))
  18999. ((org-at-table-p)
  19000. (call-interactively 'org-table-wrap-region))
  19001. (t (call-interactively 'org-insert-heading))))
  19002. ;;; Menu entries
  19003. (defsubst org-in-subtree-not-table-p ()
  19004. "Are we in a subtree and not in a table?"
  19005. (and (not (org-before-first-heading-p))
  19006. (not (org-at-table-p))))
  19007. ;; Define the Org-mode menus
  19008. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19009. '("Tbl"
  19010. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19011. ["Next Field" org-cycle (org-at-table-p)]
  19012. ["Previous Field" org-shifttab (org-at-table-p)]
  19013. ["Next Row" org-return (org-at-table-p)]
  19014. "--"
  19015. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19016. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19017. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19018. "--"
  19019. ("Column"
  19020. ["Move Column Left" org-metaleft (org-at-table-p)]
  19021. ["Move Column Right" org-metaright (org-at-table-p)]
  19022. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19023. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19024. ("Row"
  19025. ["Move Row Up" org-metaup (org-at-table-p)]
  19026. ["Move Row Down" org-metadown (org-at-table-p)]
  19027. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19028. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19029. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19030. "--"
  19031. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19032. ("Rectangle"
  19033. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19034. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19035. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19036. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19037. "--"
  19038. ("Calculate"
  19039. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19040. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19041. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19042. "--"
  19043. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19044. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19045. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19046. "--"
  19047. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19048. "--"
  19049. ["Sum Column/Rectangle" org-table-sum
  19050. (or (org-at-table-p) (org-region-active-p))]
  19051. ["Which Column?" org-table-current-column (org-at-table-p)])
  19052. ["Debug Formulas"
  19053. org-table-toggle-formula-debugger
  19054. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19055. ["Show Col/Row Numbers"
  19056. org-table-toggle-coordinate-overlays
  19057. :style toggle
  19058. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19059. "--"
  19060. ["Create" org-table-create (and (not (org-at-table-p))
  19061. org-enable-table-editor)]
  19062. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19063. ["Import from File" org-table-import (not (org-at-table-p))]
  19064. ["Export to File" org-table-export (org-at-table-p)]
  19065. "--"
  19066. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19067. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19068. '("Org"
  19069. ("Show/Hide"
  19070. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19071. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19072. ["Sparse Tree..." org-sparse-tree t]
  19073. ["Reveal Context" org-reveal t]
  19074. ["Show All" show-all t]
  19075. "--"
  19076. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19077. "--"
  19078. ["New Heading" org-insert-heading t]
  19079. ("Navigate Headings"
  19080. ["Up" outline-up-heading t]
  19081. ["Next" outline-next-visible-heading t]
  19082. ["Previous" outline-previous-visible-heading t]
  19083. ["Next Same Level" outline-forward-same-level t]
  19084. ["Previous Same Level" outline-backward-same-level t]
  19085. "--"
  19086. ["Jump" org-goto t])
  19087. ("Edit Structure"
  19088. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19089. "--"
  19090. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19091. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19092. "--"
  19093. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19094. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19095. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19096. "--"
  19097. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19098. "--"
  19099. ["Copy visible text" org-copy-visible t]
  19100. "--"
  19101. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19102. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19103. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19104. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19105. "--"
  19106. ["Sort Region/Children" org-sort t]
  19107. "--"
  19108. ["Convert to odd levels" org-convert-to-odd-levels t]
  19109. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19110. ("Editing"
  19111. ["Emphasis..." org-emphasize t]
  19112. ["Edit Source Example" org-edit-special t]
  19113. "--"
  19114. ["Footnote new/jump" org-footnote-action t]
  19115. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19116. ("Archive"
  19117. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19118. "--"
  19119. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19120. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19121. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19122. )
  19123. "--"
  19124. ("Hyperlinks"
  19125. ["Store Link (Global)" org-store-link t]
  19126. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19127. ["Insert Link" org-insert-link t]
  19128. ["Follow Link" org-open-at-point t]
  19129. "--"
  19130. ["Next link" org-next-link t]
  19131. ["Previous link" org-previous-link t]
  19132. "--"
  19133. ["Descriptive Links"
  19134. org-toggle-link-display
  19135. :style radio
  19136. :selected org-descriptive-links
  19137. ]
  19138. ["Literal Links"
  19139. org-toggle-link-display
  19140. :style radio
  19141. :selected (not org-descriptive-links)])
  19142. "--"
  19143. ("TODO Lists"
  19144. ["TODO/DONE/-" org-todo t]
  19145. ("Select keyword"
  19146. ["Next keyword" org-shiftright (org-at-heading-p)]
  19147. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19148. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19149. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19150. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19151. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19152. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19153. "--"
  19154. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19155. :selected org-enforce-todo-dependencies :style toggle :active t]
  19156. "Settings for tree at point"
  19157. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19158. :selected (org-entry-get nil "ORDERED")
  19159. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19160. ["Do Children parallel" org-toggle-ordered-property :style radio
  19161. :selected (not (org-entry-get nil "ORDERED"))
  19162. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19163. "--"
  19164. ["Set Priority" org-priority t]
  19165. ["Priority Up" org-shiftup t]
  19166. ["Priority Down" org-shiftdown t]
  19167. "--"
  19168. ["Get news from all feeds" org-feed-update-all t]
  19169. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19170. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19171. ("TAGS and Properties"
  19172. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19173. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19174. "--"
  19175. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19176. ["Column view of properties" org-columns t]
  19177. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19178. ("Dates and Scheduling"
  19179. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19180. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19181. ("Change Date"
  19182. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19183. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19184. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19185. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19186. ["Compute Time Range" org-evaluate-time-range t]
  19187. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19188. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19189. "--"
  19190. ["Custom time format" org-toggle-time-stamp-overlays
  19191. :style radio :selected org-display-custom-times]
  19192. "--"
  19193. ["Goto Calendar" org-goto-calendar t]
  19194. ["Date from Calendar" org-date-from-calendar t]
  19195. "--"
  19196. ["Start/Restart Timer" org-timer-start t]
  19197. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19198. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19199. ["Insert Timer String" org-timer t]
  19200. ["Insert Timer Item" org-timer-item t])
  19201. ("Logging work"
  19202. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19203. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19204. ["Clock out" org-clock-out t]
  19205. ["Clock cancel" org-clock-cancel t]
  19206. "--"
  19207. ["Mark as default task" org-clock-mark-default-task t]
  19208. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19209. ["Goto running clock" org-clock-goto t]
  19210. "--"
  19211. ["Display times" org-clock-display t]
  19212. ["Create clock table" org-clock-report t]
  19213. "--"
  19214. ["Record DONE time"
  19215. (progn (setq org-log-done (not org-log-done))
  19216. (message "Switching to %s will %s record a timestamp"
  19217. (car org-done-keywords)
  19218. (if org-log-done "automatically" "not")))
  19219. :style toggle :selected org-log-done])
  19220. "--"
  19221. ["Agenda Command..." org-agenda t]
  19222. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19223. ("File List for Agenda")
  19224. ("Special views current file"
  19225. ["TODO Tree" org-show-todo-tree t]
  19226. ["Check Deadlines" org-check-deadlines t]
  19227. ["Timeline" org-timeline t]
  19228. ["Tags/Property tree" org-match-sparse-tree t])
  19229. "--"
  19230. ["Export/Publish..." org-export-dispatch t]
  19231. ("LaTeX"
  19232. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19233. :selected org-cdlatex-mode]
  19234. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19235. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19236. ["Modify math symbol" org-cdlatex-math-modify
  19237. (org-inside-LaTeX-fragment-p)]
  19238. ["Insert citation" org-reftex-citation t]
  19239. "--"
  19240. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19241. "--"
  19242. ("MobileOrg"
  19243. ["Push Files and Views" org-mobile-push t]
  19244. ["Get Captured and Flagged" org-mobile-pull t]
  19245. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19246. "--"
  19247. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19248. "--"
  19249. ("Documentation"
  19250. ["Show Version" org-version t]
  19251. ["Info Documentation" org-info t])
  19252. ("Customize"
  19253. ["Browse Org Group" org-customize t]
  19254. "--"
  19255. ["Expand This Menu" org-create-customize-menu
  19256. (fboundp 'customize-menu-create)])
  19257. ["Send bug report" org-submit-bug-report t]
  19258. "--"
  19259. ("Refresh/Reload"
  19260. ["Refresh setup current buffer" org-mode-restart t]
  19261. ["Reload Org (after update)" org-reload t]
  19262. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19263. ))
  19264. (defun org-info (&optional node)
  19265. "Read documentation for Org-mode in the info system.
  19266. With optional NODE, go directly to that node."
  19267. (interactive)
  19268. (info (format "(org)%s" (or node ""))))
  19269. ;;;###autoload
  19270. (defun org-submit-bug-report ()
  19271. "Submit a bug report on Org-mode via mail.
  19272. Don't hesitate to report any problems or inaccurate documentation.
  19273. If you don't have setup sending mail from (X)Emacs, please copy the
  19274. output buffer into your mail program, as it gives us important
  19275. information about your Org-mode version and configuration."
  19276. (interactive)
  19277. (require 'reporter)
  19278. (org-load-modules-maybe)
  19279. (org-require-autoloaded-modules)
  19280. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19281. (reporter-submit-bug-report
  19282. "emacs-orgmode@gnu.org"
  19283. (org-version nil 'full)
  19284. (let (list)
  19285. (save-window-excursion
  19286. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19287. (delete-other-windows)
  19288. (erase-buffer)
  19289. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19290. We would like to add your full Org-mode and Outline configuration to the
  19291. bug report. This greatly simplifies the work of the maintainer and
  19292. other experts on the mailing list.
  19293. HOWEVER, some variables you have customized may contain private
  19294. information. The names of customers, colleagues, or friends, might
  19295. appear in the form of file names, tags, todo states, or search strings.
  19296. If you answer yes to the prompt, you might want to check and remove
  19297. such private information before sending the email.")
  19298. (add-text-properties (point-min) (point-max) '(face org-warning))
  19299. (when (yes-or-no-p "Include your Org-mode configuration ")
  19300. (mapatoms
  19301. (lambda (v)
  19302. (and (boundp v)
  19303. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19304. (or (and (symbol-value v)
  19305. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19306. (and
  19307. (get v 'custom-type) (get v 'standard-value)
  19308. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19309. (push v list)))))
  19310. (kill-buffer (get-buffer "*Warn about privacy*"))
  19311. list))
  19312. nil nil
  19313. "Remember to cover the basics, that is, what you expected to happen and
  19314. what in fact did happen. You don't know how to make a good report? See
  19315. http://orgmode.org/manual/Feedback.html#Feedback
  19316. Your bug report will be posted to the Org-mode mailing list.
  19317. ------------------------------------------------------------------------")
  19318. (save-excursion
  19319. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19320. (replace-match "\\1Bug: \\3 [\\2]")))))
  19321. (defun org-install-agenda-files-menu ()
  19322. (let ((bl (buffer-list)))
  19323. (save-excursion
  19324. (while bl
  19325. (set-buffer (pop bl))
  19326. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19327. (when (derived-mode-p 'org-mode)
  19328. (easy-menu-change
  19329. '("Org") "File List for Agenda"
  19330. (append
  19331. (list
  19332. ["Edit File List" (org-edit-agenda-file-list) t]
  19333. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19334. ["Remove Current File from List" org-remove-file t]
  19335. ["Cycle through agenda files" org-cycle-agenda-files t]
  19336. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19337. "--")
  19338. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19339. ;;;; Documentation
  19340. (defun org-require-autoloaded-modules ()
  19341. (interactive)
  19342. (mapc 'require
  19343. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19344. org-remember org-table org-timer)))
  19345. ;;;###autoload
  19346. (defun org-reload (&optional uncompiled)
  19347. "Reload all org lisp files.
  19348. With prefix arg UNCOMPILED, load the uncompiled versions."
  19349. (interactive "P")
  19350. (require 'loadhist)
  19351. (let* ((org-dir (org-find-library-dir "org"))
  19352. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19353. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19354. (remove-re (mapconcat 'identity
  19355. (mapcar (lambda (f) (concat "^" f "$"))
  19356. (list (if (featurep 'xemacs)
  19357. "org-colview"
  19358. "org-colview-xemacs")
  19359. "org" "org-loaddefs" "org-version"))
  19360. "\\|"))
  19361. (feats (delete-dups
  19362. (mapcar 'file-name-sans-extension
  19363. (mapcar 'file-name-nondirectory
  19364. (delq nil
  19365. (mapcar 'feature-file
  19366. features))))))
  19367. (lfeat (append
  19368. (sort
  19369. (setq feats
  19370. (delq nil (mapcar
  19371. (lambda (f)
  19372. (if (and (string-match feature-re f)
  19373. (not (string-match remove-re f)))
  19374. f nil))
  19375. feats)))
  19376. 'string-lessp)
  19377. (list "org-version" "org")))
  19378. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19379. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19380. load-uncore load-misses)
  19381. (setq load-misses
  19382. (delq 't
  19383. (mapcar (lambda (f)
  19384. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19385. (and (string= org-dir contrib-dir)
  19386. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19387. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19388. (add-to-list 'load-uncore f 'append)
  19389. 't)
  19390. f))
  19391. lfeat)))
  19392. (if load-uncore
  19393. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19394. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19395. (if load-misses
  19396. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19397. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19398. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19399. ;;;###autoload
  19400. (defun org-customize ()
  19401. "Call the customize function with org as argument."
  19402. (interactive)
  19403. (org-load-modules-maybe)
  19404. (org-require-autoloaded-modules)
  19405. (customize-browse 'org))
  19406. (defun org-create-customize-menu ()
  19407. "Create a full customization menu for Org-mode, insert it into the menu."
  19408. (interactive)
  19409. (org-load-modules-maybe)
  19410. (org-require-autoloaded-modules)
  19411. (if (fboundp 'customize-menu-create)
  19412. (progn
  19413. (easy-menu-change
  19414. '("Org") "Customize"
  19415. `(["Browse Org group" org-customize t]
  19416. "--"
  19417. ,(customize-menu-create 'org)
  19418. ["Set" Custom-set t]
  19419. ["Save" Custom-save t]
  19420. ["Reset to Current" Custom-reset-current t]
  19421. ["Reset to Saved" Custom-reset-saved t]
  19422. ["Reset to Standard Settings" Custom-reset-standard t]))
  19423. (message "\"Org\"-menu now contains full customization menu"))
  19424. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19425. ;;;; Miscellaneous stuff
  19426. ;;; Generally useful functions
  19427. (defun org-get-at-bol (property)
  19428. "Get text property PROPERTY at beginning of line."
  19429. (get-text-property (point-at-bol) property))
  19430. (defun org-find-text-property-in-string (prop s)
  19431. "Return the first non-nil value of property PROP in string S."
  19432. (or (get-text-property 0 prop s)
  19433. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19434. prop s)))
  19435. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19436. "Display the given MESSAGE as a warning."
  19437. (if (fboundp 'display-warning)
  19438. (display-warning 'org message
  19439. (if (featurep 'xemacs) 'warning :warning))
  19440. (let ((buf (get-buffer-create "*Org warnings*")))
  19441. (with-current-buffer buf
  19442. (goto-char (point-max))
  19443. (insert "Warning (Org): " message)
  19444. (unless (bolp)
  19445. (newline)))
  19446. (display-buffer buf)
  19447. (sit-for 0))))
  19448. (defun org-eval (form)
  19449. "Eval FORM and return result."
  19450. (condition-case error
  19451. (eval form)
  19452. (error (format "%%![Error: %s]" error))))
  19453. (defun org-in-clocktable-p ()
  19454. "Check if the cursor is in a clocktable."
  19455. (let ((pos (point)) start)
  19456. (save-excursion
  19457. (end-of-line 1)
  19458. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19459. (setq start (match-beginning 0))
  19460. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19461. (>= (match-end 0) pos)
  19462. start))))
  19463. (defun org-in-commented-line ()
  19464. "Is point in a line starting with `#'?"
  19465. (equal (char-after (point-at-bol)) ?#))
  19466. (defun org-in-indented-comment-line ()
  19467. "Is point in a line starting with `#' after some white space?"
  19468. (save-excursion
  19469. (save-match-data
  19470. (goto-char (point-at-bol))
  19471. (looking-at "[ \t]*#"))))
  19472. (defun org-in-verbatim-emphasis ()
  19473. (save-match-data
  19474. (and (org-in-regexp org-emph-re 2)
  19475. (>= (point) (match-beginning 3))
  19476. (<= (point) (match-end 4))
  19477. (member (match-string 3) '("=" "~")))))
  19478. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19479. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19480. (if (and marker (marker-buffer marker)
  19481. (buffer-live-p (marker-buffer marker)))
  19482. (progn
  19483. (org-pop-to-buffer-same-window (marker-buffer marker))
  19484. (if (or (> marker (point-max)) (< marker (point-min)))
  19485. (widen))
  19486. (goto-char marker)
  19487. (org-show-context 'org-goto))
  19488. (if bookmark
  19489. (bookmark-jump bookmark)
  19490. (error "Cannot find location"))))
  19491. (defun org-quote-csv-field (s)
  19492. "Quote field for inclusion in CSV material."
  19493. (if (string-match "[\",]" s)
  19494. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19495. s))
  19496. (defun org-force-self-insert (N)
  19497. "Needed to enforce self-insert under remapping."
  19498. (interactive "p")
  19499. (self-insert-command N))
  19500. (defun org-string-width (s)
  19501. "Compute width of string, ignoring invisible characters.
  19502. This ignores character with invisibility property `org-link', and also
  19503. characters with property `org-cwidth', because these will become invisible
  19504. upon the next fontification round."
  19505. (let (b l)
  19506. (when (or (eq t buffer-invisibility-spec)
  19507. (assq 'org-link buffer-invisibility-spec))
  19508. (while (setq b (text-property-any 0 (length s)
  19509. 'invisible 'org-link s))
  19510. (setq s (concat (substring s 0 b)
  19511. (substring s (or (next-single-property-change
  19512. b 'invisible s) (length s)))))))
  19513. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19514. (setq s (concat (substring s 0 b)
  19515. (substring s (or (next-single-property-change
  19516. b 'org-cwidth s) (length s))))))
  19517. (setq l (string-width s) b -1)
  19518. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19519. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19520. l))
  19521. (defun org-shorten-string (s maxlength)
  19522. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19523. If the string is shorter or has length MAXLENGTH, just return the
  19524. original string. If it is longer, the functions finds a space in the
  19525. string, breaks this string off at that locations and adds three dots
  19526. as ellipsis. Including the ellipsis, the string will not be longer
  19527. than MAXLENGTH. If finding a good breaking point in the string does
  19528. not work, the string is just chopped off in the middle of a word
  19529. if necessary."
  19530. (if (<= (length s) maxlength)
  19531. s
  19532. (let* ((n (max (- maxlength 4) 1))
  19533. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19534. (if (string-match re s)
  19535. (concat (match-string 1 s) "...")
  19536. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19537. (defun org-get-indentation (&optional line)
  19538. "Get the indentation of the current line, interpreting tabs.
  19539. When LINE is given, assume it represents a line and compute its indentation."
  19540. (if line
  19541. (if (string-match "^ *" (org-remove-tabs line))
  19542. (match-end 0))
  19543. (save-excursion
  19544. (beginning-of-line 1)
  19545. (skip-chars-forward " \t")
  19546. (current-column))))
  19547. (defun org-get-string-indentation (s)
  19548. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19549. (let ((n -1) (i 0) (w tab-width) c)
  19550. (catch 'exit
  19551. (while (< (setq n (1+ n)) (length s))
  19552. (setq c (aref s n))
  19553. (cond ((= c ?\ ) (setq i (1+ i)))
  19554. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19555. (t (throw 'exit t)))))
  19556. i))
  19557. (defun org-remove-tabs (s &optional width)
  19558. "Replace tabulators in S with spaces.
  19559. Assumes that s is a single line, starting in column 0."
  19560. (setq width (or width tab-width))
  19561. (while (string-match "\t" s)
  19562. (setq s (replace-match
  19563. (make-string
  19564. (- (* width (/ (+ (match-beginning 0) width) width))
  19565. (match-beginning 0)) ?\ )
  19566. t t s)))
  19567. s)
  19568. (defun org-fix-indentation (line ind)
  19569. "Fix indentation in LINE.
  19570. IND is a cons cell with target and minimum indentation.
  19571. If the current indentation in LINE is smaller than the minimum,
  19572. leave it alone. If it is larger than ind, set it to the target."
  19573. (let* ((l (org-remove-tabs line))
  19574. (i (org-get-indentation l))
  19575. (i1 (car ind)) (i2 (cdr ind)))
  19576. (if (>= i i2) (setq l (substring line i2)))
  19577. (if (> i1 0)
  19578. (concat (make-string i1 ?\ ) l)
  19579. l)))
  19580. (defun org-remove-indentation (code &optional n)
  19581. "Remove the maximum common indentation from the lines in CODE.
  19582. N may optionally be the number of spaces to remove."
  19583. (with-temp-buffer
  19584. (insert code)
  19585. (org-do-remove-indentation n)
  19586. (buffer-string)))
  19587. (defun org-do-remove-indentation (&optional n)
  19588. "Remove the maximum common indentation from the buffer."
  19589. (untabify (point-min) (point-max))
  19590. (let ((min 10000) re)
  19591. (if n
  19592. (setq min n)
  19593. (goto-char (point-min))
  19594. (while (re-search-forward "^ *[^ \n]" nil t)
  19595. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19596. (unless (or (= min 0) (= min 10000))
  19597. (setq re (format "^ \\{%d\\}" min))
  19598. (goto-char (point-min))
  19599. (while (re-search-forward re nil t)
  19600. (replace-match "")
  19601. (end-of-line 1))
  19602. min)))
  19603. (defun org-fill-template (template alist)
  19604. "Find each %key of ALIST in TEMPLATE and replace it."
  19605. (let ((case-fold-search nil)
  19606. entry key value)
  19607. (setq alist (sort (copy-sequence alist)
  19608. (lambda (a b) (< (length (car a)) (length (car b))))))
  19609. (while (setq entry (pop alist))
  19610. (setq template
  19611. (replace-regexp-in-string
  19612. (concat "%" (regexp-quote (car entry)))
  19613. (or (cdr entry) "") template t t)))
  19614. template))
  19615. (defun org-base-buffer (buffer)
  19616. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19617. (if (not buffer)
  19618. buffer
  19619. (or (buffer-base-buffer buffer)
  19620. buffer)))
  19621. (defun org-trim (s)
  19622. "Remove whitespace at beginning and end of string."
  19623. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19624. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19625. s)
  19626. (defun org-wrap (string &optional width lines)
  19627. "Wrap string to either a number of lines, or a width in characters.
  19628. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19629. that costs. If there is a word longer than WIDTH, the text is actually
  19630. wrapped to the length of that word.
  19631. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19632. many lines, whatever width that takes.
  19633. The return value is a list of lines, without newlines at the end."
  19634. (let* ((words (org-split-string string "[ \t\n]+"))
  19635. (maxword (apply 'max (mapcar 'org-string-width words)))
  19636. w ll)
  19637. (cond (width
  19638. (org-do-wrap words (max maxword width)))
  19639. (lines
  19640. (setq w maxword)
  19641. (setq ll (org-do-wrap words maxword))
  19642. (if (<= (length ll) lines)
  19643. ll
  19644. (setq ll words)
  19645. (while (> (length ll) lines)
  19646. (setq w (1+ w))
  19647. (setq ll (org-do-wrap words w)))
  19648. ll))
  19649. (t (error "Cannot wrap this")))))
  19650. (defun org-do-wrap (words width)
  19651. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19652. (let (lines line)
  19653. (while words
  19654. (setq line (pop words))
  19655. (while (and words (< (+ (length line) (length (car words))) width))
  19656. (setq line (concat line " " (pop words))))
  19657. (setq lines (push line lines)))
  19658. (nreverse lines)))
  19659. (defun org-split-string (string &optional separators)
  19660. "Splits STRING into substrings at SEPARATORS.
  19661. No empty strings are returned if there are matches at the beginning
  19662. and end of string."
  19663. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19664. (start 0)
  19665. notfirst
  19666. (list nil))
  19667. (while (and (string-match rexp string
  19668. (if (and notfirst
  19669. (= start (match-beginning 0))
  19670. (< start (length string)))
  19671. (1+ start) start))
  19672. (< (match-beginning 0) (length string)))
  19673. (setq notfirst t)
  19674. (or (eq (match-beginning 0) 0)
  19675. (and (eq (match-beginning 0) (match-end 0))
  19676. (eq (match-beginning 0) start))
  19677. (setq list
  19678. (cons (substring string start (match-beginning 0))
  19679. list)))
  19680. (setq start (match-end 0)))
  19681. (or (eq start (length string))
  19682. (setq list
  19683. (cons (substring string start)
  19684. list)))
  19685. (nreverse list)))
  19686. (defun org-quote-vert (s)
  19687. "Replace \"|\" with \"\\vert\"."
  19688. (while (string-match "|" s)
  19689. (setq s (replace-match "\\vert" t t s)))
  19690. s)
  19691. (defun org-uuidgen-p (s)
  19692. "Is S an ID created by UUIDGEN?"
  19693. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19694. (defun org-in-src-block-p (&optional inside)
  19695. "Whether point is in a code source block.
  19696. When INSIDE is non-nil, don't consider we are within a src block
  19697. when point is at #+BEGIN_SRC or #+END_SRC."
  19698. (let ((case-fold-search t) ov)
  19699. (or (and (setq ov (overlays-at (point)))
  19700. (memq 'org-block-background
  19701. (overlay-properties (car ov))))
  19702. (and (not inside)
  19703. (save-match-data
  19704. (save-excursion
  19705. (beginning-of-line)
  19706. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19707. (defun org-context ()
  19708. "Return a list of contexts of the current cursor position.
  19709. If several contexts apply, all are returned.
  19710. Each context entry is a list with a symbol naming the context, and
  19711. two positions indicating start and end of the context. Possible
  19712. contexts are:
  19713. :headline anywhere in a headline
  19714. :headline-stars on the leading stars in a headline
  19715. :todo-keyword on a TODO keyword (including DONE) in a headline
  19716. :tags on the TAGS in a headline
  19717. :priority on the priority cookie in a headline
  19718. :item on the first line of a plain list item
  19719. :item-bullet on the bullet/number of a plain list item
  19720. :checkbox on the checkbox in a plain list item
  19721. :table in an org-mode table
  19722. :table-special on a special filed in a table
  19723. :table-table in a table.el table
  19724. :clocktable in a clocktable
  19725. :src-block in a source block
  19726. :link on a hyperlink
  19727. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19728. :target on a <<target>>
  19729. :radio-target on a <<<radio-target>>>
  19730. :latex-fragment on a LaTeX fragment
  19731. :latex-preview on a LaTeX fragment with overlaid preview image
  19732. This function expects the position to be visible because it uses font-lock
  19733. faces as a help to recognize the following contexts: :table-special, :link,
  19734. and :keyword."
  19735. (let* ((f (get-text-property (point) 'face))
  19736. (faces (if (listp f) f (list f)))
  19737. (case-fold-search t)
  19738. (p (point)) clist o)
  19739. ;; First the large context
  19740. (cond
  19741. ((org-at-heading-p t)
  19742. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19743. (when (progn
  19744. (beginning-of-line 1)
  19745. (looking-at org-todo-line-tags-regexp))
  19746. (push (org-point-in-group p 1 :headline-stars) clist)
  19747. (push (org-point-in-group p 2 :todo-keyword) clist)
  19748. (push (org-point-in-group p 4 :tags) clist))
  19749. (goto-char p)
  19750. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19751. (if (looking-at "\\[#[A-Z0-9]\\]")
  19752. (push (org-point-in-group p 0 :priority) clist)))
  19753. ((org-at-item-p)
  19754. (push (org-point-in-group p 2 :item-bullet) clist)
  19755. (push (list :item (point-at-bol)
  19756. (save-excursion (org-end-of-item) (point)))
  19757. clist)
  19758. (and (org-at-item-checkbox-p)
  19759. (push (org-point-in-group p 0 :checkbox) clist)))
  19760. ((org-at-table-p)
  19761. (push (list :table (org-table-begin) (org-table-end)) clist)
  19762. (if (memq 'org-formula faces)
  19763. (push (list :table-special
  19764. (previous-single-property-change p 'face)
  19765. (next-single-property-change p 'face)) clist)))
  19766. ((org-at-table-p 'any)
  19767. (push (list :table-table) clist)))
  19768. (goto-char p)
  19769. (let ((case-fold-search t))
  19770. ;; New the "medium" contexts: clocktables, source blocks
  19771. (cond ((org-in-clocktable-p)
  19772. (push (list :clocktable
  19773. (and (or (looking-at "#\\+BEGIN: clocktable")
  19774. (search-backward "#+BEGIN: clocktable" nil t))
  19775. (match-beginning 0))
  19776. (and (re-search-forward "#\\+END:?" nil t)
  19777. (match-end 0))) clist))
  19778. ((org-in-src-block-p)
  19779. (push (list :src-block
  19780. (and (or (looking-at "#\\+BEGIN_SRC")
  19781. (search-backward "#+BEGIN_SRC" nil t))
  19782. (match-beginning 0))
  19783. (and (search-forward "#+END_SRC" nil t)
  19784. (match-beginning 0))) clist))))
  19785. (goto-char p)
  19786. ;; Now the small context
  19787. (cond
  19788. ((org-at-timestamp-p)
  19789. (push (org-point-in-group p 0 :timestamp) clist))
  19790. ((memq 'org-link faces)
  19791. (push (list :link
  19792. (previous-single-property-change p 'face)
  19793. (next-single-property-change p 'face)) clist))
  19794. ((memq 'org-special-keyword faces)
  19795. (push (list :keyword
  19796. (previous-single-property-change p 'face)
  19797. (next-single-property-change p 'face)) clist))
  19798. ((org-at-target-p)
  19799. (push (org-point-in-group p 0 :target) clist)
  19800. (goto-char (1- (match-beginning 0)))
  19801. (if (looking-at org-radio-target-regexp)
  19802. (push (org-point-in-group p 0 :radio-target) clist))
  19803. (goto-char p))
  19804. ((setq o (car (delq nil
  19805. (mapcar
  19806. (lambda (x)
  19807. (if (memq x org-latex-fragment-image-overlays) x))
  19808. (overlays-at (point))))))
  19809. (push (list :latex-fragment
  19810. (overlay-start o) (overlay-end o)) clist)
  19811. (push (list :latex-preview
  19812. (overlay-start o) (overlay-end o)) clist))
  19813. ((org-inside-LaTeX-fragment-p)
  19814. ;; FIXME: positions wrong.
  19815. (push (list :latex-fragment (point) (point)) clist)))
  19816. (setq clist (nreverse (delq nil clist)))
  19817. clist))
  19818. ;; FIXME: Compare with at-regexp-p Do we need both?
  19819. (defun org-in-regexp (re &optional nlines visually)
  19820. "Check if point is inside a match of regexp.
  19821. Normally only the current line is checked, but you can include NLINES extra
  19822. lines both before and after point into the search.
  19823. If VISUALLY is set, require that the cursor is not after the match but
  19824. really on, so that the block visually is on the match."
  19825. (catch 'exit
  19826. (let ((pos (point))
  19827. (eol (point-at-eol (+ 1 (or nlines 0))))
  19828. (inc (if visually 1 0)))
  19829. (save-excursion
  19830. (beginning-of-line (- 1 (or nlines 0)))
  19831. (while (re-search-forward re eol t)
  19832. (if (and (<= (match-beginning 0) pos)
  19833. (>= (+ inc (match-end 0)) pos))
  19834. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19835. (defun org-at-regexp-p (regexp)
  19836. "Is point inside a match of REGEXP in the current line?"
  19837. (catch 'exit
  19838. (save-excursion
  19839. (let ((pos (point)) (end (point-at-eol)))
  19840. (beginning-of-line 1)
  19841. (while (re-search-forward regexp end t)
  19842. (if (and (<= (match-beginning 0) pos)
  19843. (>= (match-end 0) pos))
  19844. (throw 'exit t)))
  19845. nil))))
  19846. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19847. "Non-nil when point is between matches of START-RE and END-RE.
  19848. Also return a non-nil value when point is on one of the matches.
  19849. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19850. buffer positions. Default values are the positions of headlines
  19851. surrounding the point.
  19852. The functions returns a cons cell whose car (resp. cdr) is the
  19853. position before START-RE (resp. after END-RE)."
  19854. (save-match-data
  19855. (let ((pos (point))
  19856. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19857. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19858. beg end)
  19859. (save-excursion
  19860. ;; Point is on a block when on START-RE or if START-RE can be
  19861. ;; found before it...
  19862. (and (or (org-at-regexp-p start-re)
  19863. (re-search-backward start-re limit-up t))
  19864. (setq beg (match-beginning 0))
  19865. ;; ... and END-RE after it...
  19866. (goto-char (match-end 0))
  19867. (re-search-forward end-re limit-down t)
  19868. (> (setq end (match-end 0)) pos)
  19869. ;; ... without another START-RE in-between.
  19870. (goto-char (match-beginning 0))
  19871. (not (re-search-backward start-re (1+ beg) t))
  19872. ;; Return value.
  19873. (cons beg end))))))
  19874. (defun org-in-block-p (names)
  19875. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19876. NAMES is a list of strings containing names of blocks.
  19877. Return first block name matched, or nil. Beware that in case of
  19878. nested blocks, the returned name may not belong to the closest
  19879. block from point."
  19880. (save-match-data
  19881. (catch 'exit
  19882. (let ((case-fold-search t)
  19883. (lim-up (save-excursion (outline-previous-heading)))
  19884. (lim-down (save-excursion (outline-next-heading))))
  19885. (mapc (lambda (name)
  19886. (let ((n (regexp-quote name)))
  19887. (when (org-between-regexps-p
  19888. (concat "^[ \t]*#\\+begin_" n)
  19889. (concat "^[ \t]*#\\+end_" n)
  19890. lim-up lim-down)
  19891. (throw 'exit n))))
  19892. names))
  19893. nil)))
  19894. (defun org-in-drawer-p ()
  19895. "Is point within a drawer?"
  19896. (save-match-data
  19897. (let ((case-fold-search t)
  19898. (lim-up (save-excursion (outline-previous-heading)))
  19899. (lim-down (save-excursion (outline-next-heading))))
  19900. (org-between-regexps-p
  19901. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  19902. "^[ \t]*:end:.*$"
  19903. lim-up lim-down))))
  19904. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19905. "Call `multi-occur' with buffers for all agenda files."
  19906. (interactive "sOrg-files matching: \np")
  19907. (let* ((files (org-agenda-files))
  19908. (tnames (mapcar 'file-truename files))
  19909. (extra org-agenda-text-search-extra-files)
  19910. f)
  19911. (when (eq (car extra) 'agenda-archives)
  19912. (setq extra (cdr extra))
  19913. (setq files (org-add-archive-files files)))
  19914. (while (setq f (pop extra))
  19915. (unless (member (file-truename f) tnames)
  19916. (add-to-list 'files f 'append)
  19917. (add-to-list 'tnames (file-truename f) 'append)))
  19918. (multi-occur
  19919. (mapcar (lambda (x)
  19920. (with-current-buffer
  19921. (or (get-file-buffer x) (find-file-noselect x))
  19922. (widen)
  19923. (current-buffer)))
  19924. files)
  19925. regexp)))
  19926. (if (boundp 'occur-mode-find-occurrence-hook)
  19927. ;; Emacs 23
  19928. (add-hook 'occur-mode-find-occurrence-hook
  19929. (lambda ()
  19930. (when (derived-mode-p 'org-mode)
  19931. (org-reveal))))
  19932. ;; Emacs 22
  19933. (defadvice occur-mode-goto-occurrence
  19934. (after org-occur-reveal activate)
  19935. (and (derived-mode-p 'org-mode) (org-reveal)))
  19936. (defadvice occur-mode-goto-occurrence-other-window
  19937. (after org-occur-reveal activate)
  19938. (and (derived-mode-p 'org-mode) (org-reveal)))
  19939. (defadvice occur-mode-display-occurrence
  19940. (after org-occur-reveal activate)
  19941. (when (derived-mode-p 'org-mode)
  19942. (let ((pos (occur-mode-find-occurrence)))
  19943. (with-current-buffer (marker-buffer pos)
  19944. (save-excursion
  19945. (goto-char pos)
  19946. (org-reveal)))))))
  19947. (defun org-occur-link-in-agenda-files ()
  19948. "Create a link and search for it in the agendas.
  19949. The link is not stored in `org-stored-links', it is just created
  19950. for the search purpose."
  19951. (interactive)
  19952. (let ((link (condition-case nil
  19953. (org-store-link nil)
  19954. (error "Unable to create a link to here"))))
  19955. (org-occur-in-agenda-files (regexp-quote link))))
  19956. (defun org-reverse-string (string)
  19957. "Return the reverse of STRING."
  19958. (apply 'string (reverse (string-to-list string))))
  19959. (defsubst org-uniquify (list)
  19960. "Non-destructively remove duplicate elements from LIST."
  19961. (let ((res (copy-sequence list))) (delete-dups res)))
  19962. (defun org-uniquify-alist (alist)
  19963. "Merge elements of ALIST with the same key.
  19964. For example, in this alist:
  19965. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19966. => '((a 1 3) (b 2))
  19967. merge (a 1) and (a 3) into (a 1 3).
  19968. The function returns the new ALIST."
  19969. (let (rtn)
  19970. (mapc
  19971. (lambda (e)
  19972. (let (n)
  19973. (if (not (assoc (car e) rtn))
  19974. (push e rtn)
  19975. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19976. (setq rtn (assq-delete-all (car e) rtn))
  19977. (push n rtn))))
  19978. alist)
  19979. rtn))
  19980. (defun org-delete-all (elts list)
  19981. "Remove all elements in ELTS from LIST."
  19982. (while elts
  19983. (setq list (delete (pop elts) list)))
  19984. list)
  19985. (defun org-count (cl-item cl-seq)
  19986. "Count the number of occurrences of ITEM in SEQ.
  19987. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19988. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19989. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19990. (while (< cl-start cl-end)
  19991. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19992. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19993. (setq cl-start (1+ cl-start)))
  19994. cl-count))
  19995. (defun org-remove-if (predicate seq)
  19996. "Remove everything from SEQ that fulfills PREDICATE."
  19997. (let (res e)
  19998. (while seq
  19999. (setq e (pop seq))
  20000. (if (not (funcall predicate e)) (push e res)))
  20001. (nreverse res)))
  20002. (defun org-remove-if-not (predicate seq)
  20003. "Remove everything from SEQ that does not fulfill PREDICATE."
  20004. (let (res e)
  20005. (while seq
  20006. (setq e (pop seq))
  20007. (if (funcall predicate e) (push e res)))
  20008. (nreverse res)))
  20009. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20010. "Reduce two-argument FUNCTION across SEQ.
  20011. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20012. \":initial-value\" removed."
  20013. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20014. (cadr (memq :initial-value cl-keys)))
  20015. (cl-seq (pop cl-seq))
  20016. (t (funcall cl-func)))))
  20017. (while cl-seq
  20018. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20019. cl-accum))
  20020. (defun org-every (pred seq)
  20021. "Return true if PREDICATE is true of every element of SEQ.
  20022. Adapted from `every' in cl.el."
  20023. (catch 'org-every
  20024. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20025. t))
  20026. (defun org-some (pred seq)
  20027. "Return true if PREDICATE is true of any element of SEQ.
  20028. Adapted from `some' in cl.el."
  20029. (catch 'org-some
  20030. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20031. nil))
  20032. (defun org-back-over-empty-lines ()
  20033. "Move backwards over whitespace, to the beginning of the first empty line.
  20034. Returns the number of empty lines passed."
  20035. (let ((pos (point)))
  20036. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20037. (skip-chars-backward " \t\n\r")
  20038. (unless (eobp)
  20039. (forward-line -1)))
  20040. (beginning-of-line 2)
  20041. (goto-char (min (point) pos))
  20042. (count-lines (point) pos)))
  20043. (defun org-skip-whitespace ()
  20044. (skip-chars-forward " \t\n\r"))
  20045. (defun org-point-in-group (point group &optional context)
  20046. "Check if POINT is in match-group GROUP.
  20047. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20048. match. If the match group does not exist or point is not inside it,
  20049. return nil."
  20050. (and (match-beginning group)
  20051. (>= point (match-beginning group))
  20052. (<= point (match-end group))
  20053. (if context
  20054. (list context (match-beginning group) (match-end group))
  20055. t)))
  20056. (defun org-switch-to-buffer-other-window (&rest args)
  20057. "Switch to buffer in a second window on the current frame.
  20058. In particular, do not allow pop-up frames.
  20059. Returns the newly created buffer."
  20060. (org-no-popups
  20061. (apply 'switch-to-buffer-other-window args)))
  20062. (defun org-combine-plists (&rest plists)
  20063. "Create a single property list from all plists in PLISTS.
  20064. The process starts by copying the first list, and then setting properties
  20065. from the other lists. Settings in the last list are the most significant
  20066. ones and overrule settings in the other lists."
  20067. (let ((rtn (copy-sequence (pop plists)))
  20068. p v ls)
  20069. (while plists
  20070. (setq ls (pop plists))
  20071. (while ls
  20072. (setq p (pop ls) v (pop ls))
  20073. (setq rtn (plist-put rtn p v))))
  20074. rtn))
  20075. (defun org-replace-escapes (string table)
  20076. "Replace %-escapes in STRING with values in TABLE.
  20077. TABLE is an association list with keys like \"%a\" and string values.
  20078. The sequences in STRING may contain normal field width and padding information,
  20079. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20080. so values can contain further %-escapes if they are define later in TABLE."
  20081. (let ((tbl (copy-alist table))
  20082. (case-fold-search nil)
  20083. (pchg 0)
  20084. e re rpl)
  20085. (while (setq e (pop tbl))
  20086. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20087. (when (and (cdr e) (string-match re (cdr e)))
  20088. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20089. (safe "SREF"))
  20090. (add-text-properties 0 3 (list 'sref sref) safe)
  20091. (setcdr e (replace-match safe t t (cdr e)))))
  20092. (while (string-match re string)
  20093. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20094. (cdr e)))
  20095. (setq string (replace-match rpl t t string))))
  20096. (while (setq pchg (next-property-change pchg string))
  20097. (let ((sref (get-text-property pchg 'sref string)))
  20098. (when (and sref (string-match "SREF" string pchg))
  20099. (setq string (replace-match sref t t string)))))
  20100. string))
  20101. (defun org-sublist (list start end)
  20102. "Return a section of LIST, from START to END.
  20103. Counting starts at 1."
  20104. (let (rtn (c start))
  20105. (setq list (nthcdr (1- start) list))
  20106. (while (and list (<= c end))
  20107. (push (pop list) rtn)
  20108. (setq c (1+ c)))
  20109. (nreverse rtn)))
  20110. (defun org-find-base-buffer-visiting (file)
  20111. "Like `find-buffer-visiting' but always return the base buffer and
  20112. not an indirect buffer."
  20113. (let ((buf (or (get-file-buffer file)
  20114. (find-buffer-visiting file))))
  20115. (if buf
  20116. (or (buffer-base-buffer buf) buf)
  20117. nil)))
  20118. (defun org-image-file-name-regexp (&optional extensions)
  20119. "Return regexp matching the file names of images.
  20120. If EXTENSIONS is given, only match these."
  20121. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20122. (image-file-name-regexp)
  20123. (let ((image-file-name-extensions
  20124. (or extensions
  20125. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20126. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20127. (concat "\\."
  20128. (regexp-opt (nconc (mapcar 'upcase
  20129. image-file-name-extensions)
  20130. image-file-name-extensions)
  20131. t)
  20132. "\\'"))))
  20133. (defun org-file-image-p (file &optional extensions)
  20134. "Return non-nil if FILE is an image."
  20135. (save-match-data
  20136. (string-match (org-image-file-name-regexp extensions) file)))
  20137. (defun org-get-cursor-date (&optional with-time)
  20138. "Return the date at cursor in as a time.
  20139. This works in the calendar and in the agenda, anywhere else it just
  20140. returns the current time.
  20141. If WITH-TIME is non-nil, returns the time of the event at point (in
  20142. the agenda) or the current time of the day."
  20143. (let (date day defd tp tm hod mod)
  20144. (when with-time
  20145. (setq tp (get-text-property (point) 'time))
  20146. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20147. (setq hod (string-to-number (match-string 1 tp))
  20148. mod (string-to-number (match-string 2 tp))))
  20149. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20150. mod (nth 1 (decode-time (current-time))))))
  20151. (cond
  20152. ((eq major-mode 'calendar-mode)
  20153. (setq date (calendar-cursor-to-date)
  20154. defd (encode-time 0 (or mod 0) (or hod 0)
  20155. (nth 1 date) (nth 0 date) (nth 2 date))))
  20156. ((eq major-mode 'org-agenda-mode)
  20157. (setq day (get-text-property (point) 'day))
  20158. (if day
  20159. (setq date (calendar-gregorian-from-absolute day)
  20160. defd (encode-time 0 (or mod 0) (or hod 0)
  20161. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20162. (or defd (current-time))))
  20163. (defun org-mark-subtree (&optional up)
  20164. "Mark the current subtree.
  20165. This puts point at the start of the current subtree, and mark at
  20166. the end. If a numeric prefix UP is given, move up into the
  20167. hierarchy of headlines by UP levels before marking the subtree."
  20168. (interactive "P")
  20169. (org-with-limited-levels
  20170. (cond ((org-at-heading-p) (beginning-of-line))
  20171. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20172. (t (outline-previous-visible-heading 1))))
  20173. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20174. (if (org-called-interactively-p 'any)
  20175. (call-interactively 'org-mark-element)
  20176. (org-mark-element)))
  20177. ;;; Indentation
  20178. (defun org-indent-line ()
  20179. "Indent line depending on context."
  20180. (interactive)
  20181. (let* ((pos (point))
  20182. (itemp (org-at-item-p))
  20183. (case-fold-search t)
  20184. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20185. (inline-task-p (and (featurep 'org-inlinetask)
  20186. (org-inlinetask-in-task-p)))
  20187. (inline-re (and inline-task-p
  20188. (org-inlinetask-outline-regexp)))
  20189. column)
  20190. (if (and orgstruct-is-++ (eq pos (point)))
  20191. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20192. (indent-according-to-mode))
  20193. (beginning-of-line 1)
  20194. (cond
  20195. ;; Headings
  20196. ((looking-at org-outline-regexp) (setq column 0))
  20197. ;; Footnote definition
  20198. ((looking-at org-footnote-definition-re) (setq column 0))
  20199. ;; Literal examples
  20200. ((looking-at "[ \t]*:\\( \\|$\\)")
  20201. (setq column (org-get-indentation))) ; do nothing
  20202. ;; Lists
  20203. ((ignore-errors (goto-char (org-in-item-p)))
  20204. (setq column (if itemp
  20205. (org-get-indentation)
  20206. (org-list-item-body-column (point))))
  20207. (goto-char pos))
  20208. ;; Drawers
  20209. ((and (looking-at "[ \t]*:END:")
  20210. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20211. (save-excursion
  20212. (goto-char (1- (match-beginning 1)))
  20213. (setq column (current-column))))
  20214. ;; Special blocks
  20215. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20216. (save-excursion
  20217. (re-search-backward
  20218. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20219. (setq column (org-get-indentation (match-string 0))))
  20220. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20221. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20222. (save-excursion
  20223. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20224. (setq column
  20225. (cond ((equal (downcase (match-string 1)) "src")
  20226. ;; src blocks: let `org-edit-src-exit' handle them
  20227. (org-get-indentation))
  20228. ((equal (downcase (match-string 1)) "example")
  20229. (max (org-get-indentation)
  20230. (org-get-indentation (match-string 0))))
  20231. (t
  20232. (org-get-indentation (match-string 0))))))
  20233. ;; This line has nothing special, look at the previous relevant
  20234. ;; line to compute indentation
  20235. (t
  20236. (beginning-of-line 0)
  20237. (while (and (not (bobp))
  20238. (not (looking-at org-table-line-regexp))
  20239. (not (looking-at org-drawer-regexp))
  20240. ;; When point started in an inline task, do not move
  20241. ;; above task starting line.
  20242. (not (and inline-task-p (looking-at inline-re)))
  20243. ;; Skip drawers, blocks, empty lines, verbatim,
  20244. ;; comments, tables, footnotes definitions, lists,
  20245. ;; inline tasks.
  20246. (or (and (looking-at "[ \t]*:END:")
  20247. (re-search-backward org-drawer-regexp nil t))
  20248. (and (looking-at "[ \t]*#\\+end_")
  20249. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20250. (looking-at "[ \t]*[\n:#|]")
  20251. (looking-at org-footnote-definition-re)
  20252. (and (not inline-task-p)
  20253. (featurep 'org-inlinetask)
  20254. (org-inlinetask-in-task-p)
  20255. (or (org-inlinetask-goto-beginning) t))))
  20256. (beginning-of-line 0))
  20257. (cond
  20258. ;; There was a list item above.
  20259. ((ignore-errors (goto-char (org-in-item-p)))
  20260. (goto-char (org-list-get-top-point (org-list-struct)))
  20261. (setq column (org-get-indentation)))
  20262. ;; There was an heading above.
  20263. ((looking-at "\\*+[ \t]+")
  20264. (if (not org-adapt-indentation)
  20265. (setq column 0)
  20266. (goto-char (match-end 0))
  20267. (setq column (current-column))))
  20268. ;; A drawer had started and is unfinished
  20269. ((looking-at org-drawer-regexp)
  20270. (goto-char (1- (match-beginning 1)))
  20271. (setq column (current-column)))
  20272. ;; Else, nothing noticeable found: get indentation and go on.
  20273. (t (setq column (org-get-indentation))))))
  20274. ;; Now apply indentation and move cursor accordingly
  20275. (goto-char pos)
  20276. (if (<= (current-column) (current-indentation))
  20277. (org-indent-line-to column)
  20278. (save-excursion (org-indent-line-to column)))
  20279. ;; Special polishing for properties, see `org-property-format'
  20280. (setq column (current-column))
  20281. (beginning-of-line 1)
  20282. (if (looking-at org-property-re)
  20283. (replace-match (concat (match-string 4)
  20284. (format org-property-format
  20285. (match-string 1) (match-string 3)))
  20286. t t))
  20287. (org-move-to-column column))))
  20288. (defun org-indent-drawer ()
  20289. "Indent the drawer at point."
  20290. (interactive)
  20291. (let ((p (point))
  20292. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20293. (match-end 0)))
  20294. (folded
  20295. (save-excursion
  20296. (end-of-line)
  20297. (when (overlays-at (point))
  20298. (member 'invisible (overlay-properties
  20299. (car (overlays-at (point)))))))))
  20300. (when folded (org-cycle))
  20301. (indent-for-tab-command)
  20302. (while (and (move-beginning-of-line 2) (< (point) e))
  20303. (indent-for-tab-command))
  20304. (goto-char p)
  20305. (when folded (org-cycle)))
  20306. (message "Drawer at point indented"))
  20307. (defun org-indent-block ()
  20308. "Indent the block at point."
  20309. (interactive)
  20310. (let ((p (point))
  20311. (case-fold-search t)
  20312. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20313. (match-end 0)))
  20314. (folded
  20315. (save-excursion
  20316. (end-of-line)
  20317. (when (overlays-at (point))
  20318. (member 'invisible (overlay-properties
  20319. (car (overlays-at (point)))))))))
  20320. (when folded (org-cycle))
  20321. (indent-for-tab-command)
  20322. (while (and (move-beginning-of-line 2) (< (point) e))
  20323. (indent-for-tab-command))
  20324. (goto-char p)
  20325. (when folded (org-cycle)))
  20326. (message "Block at point indented"))
  20327. (defun org-indent-region (start end)
  20328. "Indent region."
  20329. (interactive "r")
  20330. (save-excursion
  20331. (let ((line-end (org-current-line end)))
  20332. (goto-char start)
  20333. (while (< (org-current-line) line-end)
  20334. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20335. (t (call-interactively 'org-indent-line)))
  20336. (move-beginning-of-line 2)))))
  20337. ;;; Filling
  20338. ;; We use our own fill-paragraph and auto-fill functions.
  20339. ;; `org-fill-paragraph' relies on adaptive filling and context
  20340. ;; checking. Appropriate `fill-prefix' is computed with
  20341. ;; `org-adaptive-fill-function'.
  20342. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20343. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20344. ;; `org-adaptive-fill-function' value. Internally,
  20345. ;; `org-comment-line-break-function' breaks the line.
  20346. ;; `org-setup-filling' installs filling and auto-filling related
  20347. ;; variables during `org-mode' initialization.
  20348. (defvar org-element-paragraph-separate) ; org-element.el
  20349. (defun org-setup-filling ()
  20350. (require 'org-element)
  20351. ;; Prevent auto-fill from inserting unwanted new items.
  20352. (when (boundp 'fill-nobreak-predicate)
  20353. (org-set-local
  20354. 'fill-nobreak-predicate
  20355. (org-uniquify
  20356. (append fill-nobreak-predicate
  20357. '(org-fill-line-break-nobreak-p
  20358. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20359. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20360. (org-set-local 'paragraph-start paragraph-ending)
  20361. (org-set-local 'paragraph-separate paragraph-ending))
  20362. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20363. (org-set-local 'auto-fill-inhibit-regexp nil)
  20364. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20365. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20366. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20367. (defun org-fill-line-break-nobreak-p ()
  20368. "Non-nil when a new line at point would create an Org line break."
  20369. (save-excursion
  20370. (skip-chars-backward "[ \t]")
  20371. (skip-chars-backward "\\\\")
  20372. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20373. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20374. "Non-nil when a new line at point would split a timestamp."
  20375. (and (org-at-timestamp-p t)
  20376. (not (looking-at org-ts-regexp-both))))
  20377. (declare-function message-in-body-p "message" ())
  20378. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20379. (defun org-adaptive-fill-function ()
  20380. "Compute a fill prefix for the current line.
  20381. Return fill prefix, as a string, or nil if current line isn't
  20382. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20383. matches in paragraphs or comments, use it."
  20384. (let (prefix)
  20385. (catch 'exit
  20386. (when (derived-mode-p 'message-mode)
  20387. (save-excursion
  20388. (beginning-of-line)
  20389. (cond ((or (not (message-in-body-p))
  20390. (looking-at orgtbl-line-start-regexp))
  20391. (throw 'exit nil))
  20392. ((looking-at message-cite-prefix-regexp)
  20393. (throw 'exit (match-string-no-properties 0)))
  20394. ((looking-at org-outline-regexp)
  20395. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20396. (org-with-wide-buffer
  20397. (let* ((p (line-beginning-position))
  20398. (element (save-excursion
  20399. (beginning-of-line)
  20400. (or (ignore-errors (org-element-at-point))
  20401. (user-error "An element cannot be parsed line %d"
  20402. (line-number-at-pos (point))))))
  20403. (type (org-element-type element))
  20404. (post-affiliated (org-element-property :post-affiliated element)))
  20405. (unless (and post-affiliated (< p post-affiliated))
  20406. (case type
  20407. (comment
  20408. (save-excursion
  20409. (beginning-of-line)
  20410. (looking-at "[ \t]*#")
  20411. (goto-char (match-end 0))
  20412. (let ((comment-prefix (match-string 0)))
  20413. (if (looking-at adaptive-fill-regexp)
  20414. (concat comment-prefix (match-string 0))
  20415. comment-prefix))))
  20416. (footnote-definition "")
  20417. ((item plain-list)
  20418. (make-string (org-list-item-body-column
  20419. (or post-affiliated
  20420. (org-element-property :begin element)))
  20421. ? ))
  20422. (paragraph
  20423. ;; Fill prefix is usually the same as the current line,
  20424. ;; unless the paragraph is at the beginning of an item.
  20425. (let ((parent (org-element-property :parent element)))
  20426. (save-excursion
  20427. (beginning-of-line)
  20428. (cond ((eq (org-element-type parent) 'item)
  20429. (make-string (org-list-item-body-column
  20430. (org-element-property :begin parent))
  20431. ? ))
  20432. ((and adaptive-fill-regexp
  20433. ;; Locally disable
  20434. ;; `adaptive-fill-function' to let
  20435. ;; `fill-context-prefix' handle
  20436. ;; `adaptive-fill-regexp' variable.
  20437. (let (adaptive-fill-function)
  20438. (fill-context-prefix
  20439. post-affiliated
  20440. (org-element-property :end element)))))
  20441. ((looking-at "[ \t]+") (match-string 0))
  20442. (t "")))))
  20443. (comment-block
  20444. ;; Only fill contents if P is within block boundaries.
  20445. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20446. (forward-line)
  20447. (point)))
  20448. (cend (save-excursion
  20449. (goto-char (org-element-property :end element))
  20450. (skip-chars-backward " \r\t\n")
  20451. (line-beginning-position))))
  20452. (when (and (>= p cbeg) (< p cend))
  20453. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20454. (match-string 0)
  20455. "")))))))))))
  20456. (declare-function message-goto-body "message" ())
  20457. (defvar message-cite-prefix-regexp) ; From message.el
  20458. (defun org-fill-paragraph (&optional justify)
  20459. "Fill element at point, when applicable.
  20460. This function only applies to comment blocks, comments, example
  20461. blocks and paragraphs. Also, as a special case, re-align table
  20462. when point is at one.
  20463. If JUSTIFY is non-nil (interactively, with prefix argument),
  20464. justify as well. If `sentence-end-double-space' is non-nil, then
  20465. period followed by one space does not end a sentence, so don't
  20466. break a line there. The variable `fill-column' controls the
  20467. width for filling.
  20468. For convenience, when point is at a plain list, an item or
  20469. a footnote definition, try to fill the first paragraph within."
  20470. (interactive)
  20471. (if (and (derived-mode-p 'message-mode)
  20472. (or (not (message-in-body-p))
  20473. (save-excursion (move-beginning-of-line 1)
  20474. (looking-at message-cite-prefix-regexp))))
  20475. ;; First ensure filling is correct in message-mode.
  20476. (let ((fill-paragraph-function
  20477. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20478. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20479. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20480. (paragraph-separate
  20481. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20482. (fill-paragraph nil))
  20483. (with-syntax-table org-mode-transpose-word-syntax-table
  20484. ;; Move to end of line in order to get the first paragraph
  20485. ;; within a plain list or a footnote definition.
  20486. (let ((element (save-excursion
  20487. (end-of-line)
  20488. (or (ignore-errors (org-element-at-point))
  20489. (user-error "An element cannot be parsed line %d"
  20490. (line-number-at-pos (point)))))))
  20491. ;; First check if point is in a blank line at the beginning of
  20492. ;; the buffer. In that case, ignore filling.
  20493. (case (org-element-type element)
  20494. ;; Use major mode filling function is src blocks.
  20495. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20496. ;; Align Org tables, leave table.el tables as-is.
  20497. (table-row (org-table-align) t)
  20498. (table
  20499. (when (eq (org-element-property :type element) 'org)
  20500. (save-excursion
  20501. (goto-char (org-element-property :post-affiliated element))
  20502. (org-table-align)))
  20503. t)
  20504. (paragraph
  20505. ;; Paragraphs may contain `line-break' type objects.
  20506. (let ((beg (max (point-min)
  20507. (org-element-property :contents-begin element)))
  20508. (end (min (point-max)
  20509. (org-element-property :contents-end element))))
  20510. ;; Do nothing if point is at an affiliated keyword.
  20511. (if (< (line-end-position) beg) t
  20512. (when (derived-mode-p 'message-mode)
  20513. ;; In `message-mode', do not fill following citation
  20514. ;; in current paragraph nor text before message body.
  20515. (let ((body-start (save-excursion (message-goto-body))))
  20516. (when body-start (setq beg (max body-start beg))))
  20517. (when (save-excursion
  20518. (re-search-forward
  20519. (concat "^" message-cite-prefix-regexp) end t))
  20520. (setq end (match-beginning 0))))
  20521. ;; Fill paragraph, taking line breaks into account.
  20522. ;; For that, slice the paragraph using line breaks as
  20523. ;; separators, and fill the parts in reverse order to
  20524. ;; avoid messing with markers.
  20525. (save-excursion
  20526. (goto-char end)
  20527. (mapc
  20528. (lambda (pos)
  20529. (fill-region-as-paragraph pos (point) justify)
  20530. (goto-char pos))
  20531. ;; Find the list of ending positions for line breaks
  20532. ;; in the current paragraph. Add paragraph
  20533. ;; beginning to include first slice.
  20534. (nreverse
  20535. (cons beg
  20536. (org-element-map
  20537. (org-element--parse-objects
  20538. beg end nil (org-element-restriction 'paragraph))
  20539. 'line-break
  20540. (lambda (lb) (org-element-property :end lb)))))))
  20541. t)))
  20542. ;; Contents of `comment-block' type elements should be
  20543. ;; filled as plain text, but only if point is within block
  20544. ;; markers.
  20545. (comment-block
  20546. (let* ((case-fold-search t)
  20547. (beg (save-excursion
  20548. (goto-char (org-element-property :begin element))
  20549. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20550. (forward-line)
  20551. (point)))
  20552. (end (save-excursion
  20553. (goto-char (org-element-property :end element))
  20554. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20555. (line-beginning-position))))
  20556. (if (or (< (point) beg) (> (point) end)) t
  20557. (fill-region-as-paragraph
  20558. (save-excursion (end-of-line)
  20559. (re-search-backward "^[ \t]*$" beg 'move)
  20560. (line-beginning-position))
  20561. (save-excursion (beginning-of-line)
  20562. (re-search-forward "^[ \t]*$" end 'move)
  20563. (line-beginning-position))
  20564. justify))))
  20565. ;; Fill comments.
  20566. (comment
  20567. (let ((begin (org-element-property :post-affiliated element))
  20568. (end (org-element-property :end element)))
  20569. (when (and (>= (point) begin) (<= (point) end))
  20570. (let ((begin (save-excursion
  20571. (end-of-line)
  20572. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  20573. (progn (forward-line) (point))
  20574. begin)))
  20575. (end (save-excursion
  20576. (end-of-line)
  20577. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  20578. (1- (line-beginning-position))
  20579. (skip-chars-backward " \r\t\n")
  20580. (line-end-position)))))
  20581. ;; Do not fill comments when at a blank line.
  20582. (when (> end begin)
  20583. (let ((fill-prefix
  20584. (save-excursion
  20585. (beginning-of-line)
  20586. (looking-at "[ \t]*#")
  20587. (let ((comment-prefix (match-string 0)))
  20588. (goto-char (match-end 0))
  20589. (if (looking-at adaptive-fill-regexp)
  20590. (concat comment-prefix (match-string 0))
  20591. (concat comment-prefix " "))))))
  20592. (save-excursion
  20593. (fill-region-as-paragraph begin end justify))))))
  20594. t))
  20595. ;; Ignore every other element.
  20596. (otherwise t))))))
  20597. (defun org-auto-fill-function ()
  20598. "Auto-fill function."
  20599. ;; Check if auto-filling is meaningful.
  20600. (let ((fc (current-fill-column)))
  20601. (when (and fc (> (current-column) fc))
  20602. (let* ((fill-prefix (org-adaptive-fill-function))
  20603. ;; Enforce empty fill prefix, if required. Otherwise, it
  20604. ;; will be computed again.
  20605. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20606. (when fill-prefix (do-auto-fill))))))
  20607. (defun org-comment-line-break-function (&optional soft)
  20608. "Break line at point and indent, continuing comment if within one.
  20609. The inserted newline is marked hard if variable
  20610. `use-hard-newlines' is true, unless optional argument SOFT is
  20611. non-nil."
  20612. (if soft (insert-and-inherit ?\n) (newline 1))
  20613. (save-excursion (forward-char -1) (delete-horizontal-space))
  20614. (delete-horizontal-space)
  20615. (indent-to-left-margin)
  20616. (insert-before-markers-and-inherit fill-prefix))
  20617. ;;; Comments
  20618. ;; Org comments syntax is quite complex. It requires the entire line
  20619. ;; to be just a comment. Also, even with the right syntax at the
  20620. ;; beginning of line, some some elements (i.e. verse-block or
  20621. ;; example-block) don't accept comments. Usual Emacs comment commands
  20622. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20623. ;; Org still relies on `comment-dwim', but cannot trust
  20624. ;; `comment-only-p'. So, `comment-region-function' and
  20625. ;; `uncomment-region-function' both point
  20626. ;; to`org-comment-or-uncomment-region'. Eventually,
  20627. ;; `org-insert-comment' takes care of insertion of comments at the
  20628. ;; beginning of line.
  20629. ;; `org-setup-comments-handling' install comments related variables
  20630. ;; during `org-mode' initialization.
  20631. (defun org-setup-comments-handling ()
  20632. (interactive)
  20633. (org-set-local 'comment-use-syntax nil)
  20634. (org-set-local 'comment-start "# ")
  20635. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20636. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20637. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20638. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20639. (defun org-insert-comment ()
  20640. "Insert an empty comment above current line.
  20641. If the line is empty, insert comment at its beginning."
  20642. (beginning-of-line)
  20643. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20644. (org-indent-line)
  20645. (insert "# "))
  20646. (defvar comment-empty-lines) ; From newcomment.el.
  20647. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20648. "Comment or uncomment each non-blank line in the region.
  20649. Uncomment each non-blank line between BEG and END if it only
  20650. contains commented lines. Otherwise, comment them."
  20651. (save-restriction
  20652. ;; Restrict region
  20653. (narrow-to-region (save-excursion (goto-char beg)
  20654. (skip-chars-forward " \r\t\n" end)
  20655. (line-beginning-position))
  20656. (save-excursion (goto-char end)
  20657. (skip-chars-backward " \r\t\n" beg)
  20658. (line-end-position)))
  20659. (let ((uncommentp
  20660. ;; UNCOMMENTP is non-nil when every non blank line between
  20661. ;; BEG and END is a comment.
  20662. (save-excursion
  20663. (goto-char (point-min))
  20664. (while (and (not (eobp))
  20665. (let ((element (org-element-at-point)))
  20666. (and (eq (org-element-type element) 'comment)
  20667. (goto-char (min (point-max)
  20668. (org-element-property
  20669. :end element)))))))
  20670. (eobp))))
  20671. (if uncommentp
  20672. ;; Only blank lines and comments in region: uncomment it.
  20673. (save-excursion
  20674. (goto-char (point-min))
  20675. (while (not (eobp))
  20676. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20677. (replace-match "" nil nil nil 1))
  20678. (forward-line)))
  20679. ;; Comment each line in region.
  20680. (let ((min-indent (point-max)))
  20681. ;; First find the minimum indentation across all lines.
  20682. (save-excursion
  20683. (goto-char (point-min))
  20684. (while (and (not (eobp)) (not (zerop min-indent)))
  20685. (unless (looking-at "[ \t]*$")
  20686. (setq min-indent (min min-indent (current-indentation))))
  20687. (forward-line)))
  20688. ;; Then loop over all lines.
  20689. (save-excursion
  20690. (goto-char (point-min))
  20691. (while (not (eobp))
  20692. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20693. ;; Don't get fooled by invisible text (e.g. link path)
  20694. ;; when moving to column MIN-INDENT.
  20695. (let ((buffer-invisibility-spec nil))
  20696. (org-move-to-column min-indent t))
  20697. (insert comment-start))
  20698. (forward-line))))))))
  20699. ;;; Planning
  20700. ;; This section contains tools to operate on timestamp objects, as
  20701. ;; returned by, e.g. `org-element-context'.
  20702. (defun org-timestamp-has-time-p (timestamp)
  20703. "Non-nil when TIMESTAMP has a time specified."
  20704. (org-element-property :hour-start timestamp))
  20705. (defun org-timestamp-format (timestamp format &optional end utc)
  20706. "Format a TIMESTAMP element into a string.
  20707. FORMAT is a format specifier to be passed to
  20708. `format-time-string'.
  20709. When optional argument END is non-nil, use end of date-range or
  20710. time-range, if possible.
  20711. When optional argument UTC is non-nil, time will be expressed as
  20712. Universal Time."
  20713. (format-time-string
  20714. format
  20715. (apply 'encode-time
  20716. (cons 0
  20717. (mapcar
  20718. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20719. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20720. '(:minute-start :hour-start :day-start :month-start
  20721. :year-start)))))
  20722. utc))
  20723. (defun org-timestamp-split-range (timestamp &optional end)
  20724. "Extract a timestamp object from a date or time range.
  20725. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20726. the end of the range. Otherwise, extract its start.
  20727. Return a new timestamp object sharing the same parent as
  20728. TIMESTAMP."
  20729. (let ((type (org-element-property :type timestamp)))
  20730. (if (memq type '(active inactive diary)) timestamp
  20731. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20732. ;; Set new type.
  20733. (org-element-put-property
  20734. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20735. ;; Copy start properties over end properties if END is
  20736. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20737. (let ((p-alist '((:minute-start . :minute-end)
  20738. (:hour-start . :hour-end)
  20739. (:day-start . :day-end)
  20740. (:month-start . :month-end)
  20741. (:year-start . :year-end))))
  20742. (dolist (p-cell p-alist)
  20743. (org-element-put-property
  20744. split-ts
  20745. (funcall (if end 'car 'cdr) p-cell)
  20746. (org-element-property
  20747. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20748. ;; Eventually refresh `:raw-value'.
  20749. (org-element-put-property split-ts :raw-value nil)
  20750. (org-element-put-property
  20751. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20752. (defun org-timestamp-translate (timestamp &optional boundary)
  20753. "Apply `org-translate-time' on a TIMESTAMP object.
  20754. When optional argument BOUNDARY is non-nil, it is either the
  20755. symbol `start' or `end'. In this case, only translate the
  20756. starting or ending part of TIMESTAMP if it is a date or time
  20757. range. Otherwise, translate both parts."
  20758. (if (and (not boundary)
  20759. (memq (org-element-property :type timestamp)
  20760. '(active-range inactive-range)))
  20761. (concat
  20762. (org-translate-time
  20763. (org-element-property :raw-value
  20764. (org-timestamp-split-range timestamp)))
  20765. "--"
  20766. (org-translate-time
  20767. (org-element-property :raw-value
  20768. (org-timestamp-split-range timestamp t))))
  20769. (org-translate-time
  20770. (org-element-property
  20771. :raw-value
  20772. (if (not boundary) timestamp
  20773. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20774. ;;; Other stuff.
  20775. (defun org-toggle-fixed-width-section (arg)
  20776. "Toggle the fixed-width export.
  20777. If there is no active region, the QUOTE keyword at the current headline is
  20778. inserted or removed. When present, it causes the text between this headline
  20779. and the next to be exported as fixed-width text, and unmodified.
  20780. If there is an active region, this command adds or removes a colon as the
  20781. first character of this line. If the first character of a line is a colon,
  20782. this line is also exported in fixed-width font."
  20783. (interactive "P")
  20784. (let* ((cc 0)
  20785. (regionp (org-region-active-p))
  20786. (beg (if regionp (region-beginning) (point)))
  20787. (end (if regionp (region-end)))
  20788. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20789. (case-fold-search nil)
  20790. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20791. off)
  20792. (if regionp
  20793. (save-excursion
  20794. (goto-char beg)
  20795. (setq cc (current-column))
  20796. (beginning-of-line 1)
  20797. (setq off (looking-at re))
  20798. (while (> nlines 0)
  20799. (setq nlines (1- nlines))
  20800. (beginning-of-line 1)
  20801. (cond
  20802. (arg
  20803. (org-move-to-column cc t)
  20804. (insert ": \n")
  20805. (forward-line -1))
  20806. ((and off (looking-at re))
  20807. (replace-match "" t t nil 1))
  20808. ((not off) (org-move-to-column cc t) (insert ": ")))
  20809. (forward-line 1)))
  20810. (save-excursion
  20811. (org-back-to-heading)
  20812. (cond
  20813. ((looking-at (format org-heading-keyword-regexp-format
  20814. org-quote-string))
  20815. (goto-char (match-end 1))
  20816. (looking-at (concat " +" org-quote-string))
  20817. (replace-match "" t t)
  20818. (when (eolp) (insert " ")))
  20819. ((looking-at org-outline-regexp)
  20820. (goto-char (match-end 0))
  20821. (insert org-quote-string " ")))))))
  20822. (defun org-reftex-citation ()
  20823. "Use reftex-citation to insert a citation into the buffer.
  20824. This looks for a line like
  20825. #+BIBLIOGRAPHY: foo plain option:-d
  20826. and derives from it that foo.bib is the bibliography file relevant
  20827. for this document. It then installs the necessary environment for RefTeX
  20828. to work in this buffer and calls `reftex-citation' to insert a citation
  20829. into the buffer.
  20830. Export of such citations to both LaTeX and HTML is handled by the contributed
  20831. package ox-bibtex by Taru Karttunen."
  20832. (interactive)
  20833. (let ((reftex-docstruct-symbol 'rds)
  20834. (reftex-cite-format "\\cite{%l}")
  20835. rds bib)
  20836. (save-excursion
  20837. (save-restriction
  20838. (widen)
  20839. (let ((case-fold-search t)
  20840. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20841. (if (not (save-excursion
  20842. (or (re-search-forward re nil t)
  20843. (re-search-backward re nil t))))
  20844. (error "No bibliography defined in file")
  20845. (setq bib (concat (match-string 1) ".bib")
  20846. rds (list (list 'bib bib)))))))
  20847. (call-interactively 'reftex-citation)))
  20848. ;;;; Functions extending outline functionality
  20849. (defun org-beginning-of-line (&optional arg)
  20850. "Go to the beginning of the current line. If that is invisible, continue
  20851. to a visible line beginning. This makes the function of C-a more intuitive.
  20852. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20853. first attempt, and only move to after the tags when the cursor is already
  20854. beyond the end of the headline."
  20855. (interactive "P")
  20856. (let ((pos (point))
  20857. (special (if (consp org-special-ctrl-a/e)
  20858. (car org-special-ctrl-a/e)
  20859. org-special-ctrl-a/e))
  20860. refpos)
  20861. (if (org-bound-and-true-p visual-line-mode)
  20862. (beginning-of-visual-line 1)
  20863. (beginning-of-line 1))
  20864. (if (and arg (fboundp 'move-beginning-of-line))
  20865. (call-interactively 'move-beginning-of-line)
  20866. (if (bobp)
  20867. nil
  20868. (backward-char 1)
  20869. (if (org-truely-invisible-p)
  20870. (while (and (not (bobp)) (org-truely-invisible-p))
  20871. (backward-char 1)
  20872. (beginning-of-line 1))
  20873. (forward-char 1))))
  20874. (when special
  20875. (cond
  20876. ((and (looking-at org-complex-heading-regexp)
  20877. (= (char-after (match-end 1)) ?\ ))
  20878. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20879. (point-at-eol)))
  20880. (goto-char
  20881. (if (eq special t)
  20882. (cond ((> pos refpos) refpos)
  20883. ((= pos (point)) refpos)
  20884. (t (point)))
  20885. (cond ((> pos (point)) (point))
  20886. ((not (eq last-command this-command)) (point))
  20887. (t refpos)))))
  20888. ((org-at-item-p)
  20889. ;; Being at an item and not looking at an the item means point
  20890. ;; was previously moved to beginning of a visual line, which
  20891. ;; doesn't contain the item. Therefore, do nothing special,
  20892. ;; just stay here.
  20893. (when (looking-at org-list-full-item-re)
  20894. ;; Set special position at first white space character after
  20895. ;; bullet, and check-box, if any.
  20896. (let ((after-bullet
  20897. (let ((box (match-end 3)))
  20898. (if (not box) (match-end 1)
  20899. (let ((after (char-after box)))
  20900. (if (and after (= after ? )) (1+ box) box))))))
  20901. ;; Special case: Move point to special position when
  20902. ;; currently after it or at beginning of line.
  20903. (if (eq special t)
  20904. (when (or (> pos after-bullet) (= (point) pos))
  20905. (goto-char after-bullet))
  20906. ;; Reversed case: Move point to special position when
  20907. ;; point was already at beginning of line and command is
  20908. ;; repeated.
  20909. (when (and (= (point) pos) (eq last-command this-command))
  20910. (goto-char after-bullet))))))))
  20911. (org-no-warnings
  20912. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20913. (setq disable-point-adjustment
  20914. (or (not (invisible-p (point)))
  20915. (not (invisible-p (max (point-min) (1- (point))))))))
  20916. (defun org-end-of-line (&optional arg)
  20917. "Go to the end of the line.
  20918. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20919. tags on the first attempt, and only move to after the tags when
  20920. the cursor is already beyond the end of the headline."
  20921. (interactive "P")
  20922. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20923. org-special-ctrl-a/e))
  20924. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20925. 'end-of-visual-line)
  20926. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20927. (t 'end-of-line))))
  20928. (if (or (not special) arg) (call-interactively move-fun)
  20929. (let* ((element (save-excursion (beginning-of-line)
  20930. (org-element-at-point)))
  20931. (type (org-element-type element)))
  20932. (cond
  20933. ((memq type '(headline inlinetask))
  20934. (let ((pos (point)))
  20935. (beginning-of-line 1)
  20936. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20937. (if (eq special t)
  20938. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20939. (goto-char (match-beginning 1))
  20940. (goto-char (match-end 0)))
  20941. (if (or (< pos (match-end 0))
  20942. (not (eq this-command last-command)))
  20943. (goto-char (match-end 0))
  20944. (goto-char (match-beginning 1))))
  20945. (call-interactively move-fun))))
  20946. ((org-element-property :hiddenp element)
  20947. ;; If element is hidden, `move-end-of-line' would put point
  20948. ;; after it. Use `end-of-line' to stay on current line.
  20949. (call-interactively 'end-of-line))
  20950. (t (call-interactively move-fun)))))
  20951. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20952. (setq disable-point-adjustment
  20953. (or (not (invisible-p (point)))
  20954. (not (invisible-p (max (point-min) (1- (point))))))))
  20955. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20956. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20957. (defun org-backward-sentence (&optional arg)
  20958. "Go to beginning of sentence, or beginning of table field.
  20959. This will call `backward-sentence' or `org-table-beginning-of-field',
  20960. depending on context."
  20961. (interactive "P")
  20962. (cond
  20963. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20964. (t (call-interactively 'backward-sentence))))
  20965. (defun org-forward-sentence (&optional arg)
  20966. "Go to end of sentence, or end of table field.
  20967. This will call `forward-sentence' or `org-table-end-of-field',
  20968. depending on context."
  20969. (interactive "P")
  20970. (cond
  20971. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20972. (t (call-interactively 'forward-sentence))))
  20973. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20974. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20975. (defun org-kill-line (&optional arg)
  20976. "Kill line, to tags or end of line."
  20977. (interactive "P")
  20978. (cond
  20979. ((or (not org-special-ctrl-k)
  20980. (bolp)
  20981. (not (org-at-heading-p)))
  20982. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20983. org-ctrl-k-protect-subtree)
  20984. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20985. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20986. (user-error "C-k aborted as it would kill a hidden subtree")))
  20987. (call-interactively
  20988. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20989. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20990. (kill-region (point) (match-beginning 1))
  20991. (org-set-tags nil t))
  20992. (t (kill-region (point) (point-at-eol)))))
  20993. (define-key org-mode-map "\C-k" 'org-kill-line)
  20994. (defun org-yank (&optional arg)
  20995. "Yank. If the kill is a subtree, treat it specially.
  20996. This command will look at the current kill and check if is a single
  20997. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20998. cursor is at the beginning of a line or after the stars of a currently
  20999. empty headline, then the yank is handled specially. How exactly depends
  21000. on the value of the following variables, both set by default.
  21001. org-yank-folded-subtrees
  21002. When set, the subtree(s) will be folded after insertion, but only
  21003. if doing so would now swallow text after the yanked text.
  21004. org-yank-adjusted-subtrees
  21005. When set, the subtree will be promoted or demoted in order to
  21006. fit into the local outline tree structure, which means that the level
  21007. will be adjusted so that it becomes the smaller one of the two
  21008. *visible* surrounding headings.
  21009. Any prefix to this command will cause `yank' to be called directly with
  21010. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21011. will just
  21012. plainly yank the text as it is.
  21013. \[1] The test checks if the first non-white line is a heading
  21014. and if there are no other headings with fewer stars."
  21015. (interactive "P")
  21016. (org-yank-generic 'yank arg))
  21017. (defun org-yank-generic (command arg)
  21018. "Perform some yank-like command.
  21019. This function implements the behavior described in the `org-yank'
  21020. documentation. However, it has been generalized to work for any
  21021. interactive command with similar behavior."
  21022. ;; pretend to be command COMMAND
  21023. (setq this-command command)
  21024. (if arg
  21025. (call-interactively command)
  21026. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21027. (and (org-kill-is-subtree-p)
  21028. (or (bolp)
  21029. (and (looking-at "[ \t]*$")
  21030. (string-match
  21031. "\\`\\*+\\'"
  21032. (buffer-substring (point-at-bol) (point)))))))
  21033. swallowp)
  21034. (cond
  21035. ((and subtreep org-yank-folded-subtrees)
  21036. (let ((beg (point))
  21037. end)
  21038. (if (and subtreep org-yank-adjusted-subtrees)
  21039. (org-paste-subtree nil nil 'for-yank)
  21040. (call-interactively command))
  21041. (setq end (point))
  21042. (goto-char beg)
  21043. (when (and (bolp) subtreep
  21044. (not (setq swallowp
  21045. (org-yank-folding-would-swallow-text beg end))))
  21046. (org-with-limited-levels
  21047. (or (looking-at org-outline-regexp)
  21048. (re-search-forward org-outline-regexp-bol end t))
  21049. (while (and (< (point) end) (looking-at org-outline-regexp))
  21050. (hide-subtree)
  21051. (org-cycle-show-empty-lines 'folded)
  21052. (condition-case nil
  21053. (outline-forward-same-level 1)
  21054. (error (goto-char end))))))
  21055. (when swallowp
  21056. (message
  21057. "Inserted text not folded because that would swallow text"))
  21058. (goto-char end)
  21059. (skip-chars-forward " \t\n\r")
  21060. (beginning-of-line 1)
  21061. (push-mark beg 'nomsg)))
  21062. ((and subtreep org-yank-adjusted-subtrees)
  21063. (let ((beg (point-at-bol)))
  21064. (org-paste-subtree nil nil 'for-yank)
  21065. (push-mark beg 'nomsg)))
  21066. (t
  21067. (call-interactively command))))))
  21068. (defun org-yank-folding-would-swallow-text (beg end)
  21069. "Would hide-subtree at BEG swallow any text after END?"
  21070. (let (level)
  21071. (org-with-limited-levels
  21072. (save-excursion
  21073. (goto-char beg)
  21074. (when (or (looking-at org-outline-regexp)
  21075. (re-search-forward org-outline-regexp-bol end t))
  21076. (setq level (org-outline-level)))
  21077. (goto-char end)
  21078. (skip-chars-forward " \t\r\n\v\f")
  21079. (if (or (eobp)
  21080. (and (bolp) (looking-at org-outline-regexp)
  21081. (<= (org-outline-level) level)))
  21082. nil ; Nothing would be swallowed
  21083. t))))) ; something would swallow
  21084. (define-key org-mode-map "\C-y" 'org-yank)
  21085. (defun org-truely-invisible-p ()
  21086. "Check if point is at a character currently not visible.
  21087. This version does not only check the character property, but also
  21088. `visible-mode'."
  21089. ;; Early versions of noutline don't have `outline-invisible-p'.
  21090. (if (org-bound-and-true-p visible-mode)
  21091. nil
  21092. (outline-invisible-p)))
  21093. (defun org-invisible-p2 ()
  21094. "Check if point is at a character currently not visible."
  21095. (save-excursion
  21096. (if (and (eolp) (not (bobp))) (backward-char 1))
  21097. ;; Early versions of noutline don't have `outline-invisible-p'.
  21098. (outline-invisible-p)))
  21099. (defun org-back-to-heading (&optional invisible-ok)
  21100. "Call `outline-back-to-heading', but provide a better error message."
  21101. (condition-case nil
  21102. (outline-back-to-heading invisible-ok)
  21103. (error (error "Before first headline at position %d in buffer %s"
  21104. (point) (current-buffer)))))
  21105. (defun org-before-first-heading-p ()
  21106. "Before first heading?"
  21107. (save-excursion
  21108. (end-of-line)
  21109. (null (re-search-backward org-outline-regexp-bol nil t))))
  21110. (defun org-at-heading-p (&optional ignored)
  21111. (outline-on-heading-p t))
  21112. ;; Compatibility alias with Org versions < 7.8.03
  21113. (defalias 'org-on-heading-p 'org-at-heading-p)
  21114. (defun org-at-comment-p nil
  21115. "Is cursor in a line starting with a # character?"
  21116. (save-excursion
  21117. (beginning-of-line)
  21118. (looking-at "^#")))
  21119. (defun org-at-drawer-p nil
  21120. "Is cursor at a drawer keyword?"
  21121. (save-excursion
  21122. (move-beginning-of-line 1)
  21123. (looking-at org-drawer-regexp)))
  21124. (defun org-at-block-p nil
  21125. "Is cursor at a block keyword?"
  21126. (save-excursion
  21127. (move-beginning-of-line 1)
  21128. (looking-at org-block-regexp)))
  21129. (defun org-point-at-end-of-empty-headline ()
  21130. "If point is at the end of an empty headline, return t, else nil.
  21131. If the heading only contains a TODO keyword, it is still still considered
  21132. empty."
  21133. (and (looking-at "[ \t]*$")
  21134. (when org-todo-line-regexp
  21135. (save-excursion
  21136. (beginning-of-line 1)
  21137. (let ((case-fold-search nil))
  21138. (looking-at org-todo-line-regexp)
  21139. (string= (match-string 3) ""))))))
  21140. (defun org-at-heading-or-item-p ()
  21141. (or (org-at-heading-p) (org-at-item-p)))
  21142. (defun org-at-target-p ()
  21143. (or (org-in-regexp org-radio-target-regexp)
  21144. (org-in-regexp org-target-regexp)))
  21145. ;; Compatibility alias with Org versions < 7.8.03
  21146. (defalias 'org-on-target-p 'org-at-target-p)
  21147. (defun org-up-heading-all (arg)
  21148. "Move to the heading line of which the present line is a subheading.
  21149. This function considers both visible and invisible heading lines.
  21150. With argument, move up ARG levels."
  21151. (if (fboundp 'outline-up-heading-all)
  21152. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21153. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21154. (defun org-up-heading-safe ()
  21155. "Move to the heading line of which the present line is a subheading.
  21156. This version will not throw an error. It will return the level of the
  21157. headline found, or nil if no higher level is found.
  21158. Also, this function will be a lot faster than `outline-up-heading',
  21159. because it relies on stars being the outline starters. This can really
  21160. make a significant difference in outlines with very many siblings."
  21161. (let (start-level re)
  21162. (org-back-to-heading t)
  21163. (setq start-level (funcall outline-level))
  21164. (if (equal start-level 1)
  21165. nil
  21166. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21167. (if (re-search-backward re nil t)
  21168. (funcall outline-level)))))
  21169. (defun org-first-sibling-p ()
  21170. "Is this heading the first child of its parents?"
  21171. (interactive)
  21172. (let ((re org-outline-regexp-bol)
  21173. level l)
  21174. (unless (org-at-heading-p t)
  21175. (user-error "Not at a heading"))
  21176. (setq level (funcall outline-level))
  21177. (save-excursion
  21178. (if (not (re-search-backward re nil t))
  21179. t
  21180. (setq l (funcall outline-level))
  21181. (< l level)))))
  21182. (defun org-goto-sibling (&optional previous)
  21183. "Goto the next sibling, even if it is invisible.
  21184. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21185. when a sibling was found. When none is found, return nil and don't
  21186. move point."
  21187. (let ((fun (if previous 're-search-backward 're-search-forward))
  21188. (pos (point))
  21189. (re org-outline-regexp-bol)
  21190. level l)
  21191. (when (condition-case nil (org-back-to-heading t) (error nil))
  21192. (setq level (funcall outline-level))
  21193. (catch 'exit
  21194. (or previous (forward-char 1))
  21195. (while (funcall fun re nil t)
  21196. (setq l (funcall outline-level))
  21197. (when (< l level) (goto-char pos) (throw 'exit nil))
  21198. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21199. (goto-char pos)
  21200. nil))))
  21201. (defun org-show-siblings ()
  21202. "Show all siblings of the current headline."
  21203. (save-excursion
  21204. (while (org-goto-sibling) (org-flag-heading nil)))
  21205. (save-excursion
  21206. (while (org-goto-sibling 'previous)
  21207. (org-flag-heading nil))))
  21208. (defun org-goto-first-child ()
  21209. "Goto the first child, even if it is invisible.
  21210. Return t when a child was found. Otherwise don't move point and
  21211. return nil."
  21212. (let (level (pos (point)) (re org-outline-regexp-bol))
  21213. (when (condition-case nil (org-back-to-heading t) (error nil))
  21214. (setq level (outline-level))
  21215. (forward-char 1)
  21216. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21217. (progn (goto-char (match-beginning 0)) t)
  21218. (goto-char pos) nil))))
  21219. (defun org-show-hidden-entry ()
  21220. "Show an entry where even the heading is hidden."
  21221. (save-excursion
  21222. (org-show-entry)))
  21223. (defun org-flag-heading (flag &optional entry)
  21224. "Flag the current heading. FLAG non-nil means make invisible.
  21225. When ENTRY is non-nil, show the entire entry."
  21226. (save-excursion
  21227. (org-back-to-heading t)
  21228. ;; Check if we should show the entire entry
  21229. (if entry
  21230. (progn
  21231. (org-show-entry)
  21232. (save-excursion
  21233. (and (outline-next-heading)
  21234. (org-flag-heading nil))))
  21235. (outline-flag-region (max (point-min) (1- (point)))
  21236. (save-excursion (outline-end-of-heading) (point))
  21237. flag))))
  21238. (defun org-get-next-sibling ()
  21239. "Move to next heading of the same level, and return point.
  21240. If there is no such heading, return nil.
  21241. This is like outline-next-sibling, but invisible headings are ok."
  21242. (let ((level (funcall outline-level)))
  21243. (outline-next-heading)
  21244. (while (and (not (eobp)) (> (funcall outline-level) level))
  21245. (outline-next-heading))
  21246. (if (or (eobp) (< (funcall outline-level) level))
  21247. nil
  21248. (point))))
  21249. (defun org-get-last-sibling ()
  21250. "Move to previous heading of the same level, and return point.
  21251. If there is no such heading, return nil."
  21252. (let ((opoint (point))
  21253. (level (funcall outline-level)))
  21254. (outline-previous-heading)
  21255. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21256. (while (and (> (funcall outline-level) level)
  21257. (not (bobp)))
  21258. (outline-previous-heading))
  21259. (if (< (funcall outline-level) level)
  21260. nil
  21261. (point)))))
  21262. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21263. "Goto to the end of a subtree."
  21264. ;; This contains an exact copy of the original function, but it uses
  21265. ;; `org-back-to-heading', to make it work also in invisible
  21266. ;; trees. And is uses an invisible-ok argument.
  21267. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21268. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21269. ;; with many children and grandchildren etc, this can be much faster
  21270. ;; than the outline version.
  21271. (org-back-to-heading invisible-ok)
  21272. (let ((first t)
  21273. (level (funcall outline-level)))
  21274. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21275. ;; A true heading (not a plain list item), in Org-mode
  21276. ;; This means we can easily find the end by looking
  21277. ;; only for the right number of stars. Using a regexp to do
  21278. ;; this is so much faster than using a Lisp loop.
  21279. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21280. (forward-char 1)
  21281. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21282. ;; something else, do it the slow way
  21283. (while (and (not (eobp))
  21284. (or first (> (funcall outline-level) level)))
  21285. (setq first nil)
  21286. (outline-next-heading)))
  21287. (unless to-heading
  21288. (if (memq (preceding-char) '(?\n ?\^M))
  21289. (progn
  21290. ;; Go to end of line before heading
  21291. (forward-char -1)
  21292. (if (memq (preceding-char) '(?\n ?\^M))
  21293. ;; leave blank line before heading
  21294. (forward-char -1))))))
  21295. (point))
  21296. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21297. "Use Org version in org-mode, for dramatic speed-up."
  21298. (if (derived-mode-p 'org-mode)
  21299. (progn
  21300. (org-end-of-subtree nil t)
  21301. (unless (eobp) (backward-char 1)))
  21302. ad-do-it))
  21303. (defun org-end-of-meta-data-and-drawers ()
  21304. "Jump to the first text after meta data and drawers in the current entry.
  21305. This will move over empty lines, lines with planning time stamps,
  21306. clocking lines, and drawers."
  21307. (org-back-to-heading t)
  21308. (let ((end (save-excursion (outline-next-heading) (point)))
  21309. (re (concat "\\(" org-drawer-regexp "\\)"
  21310. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21311. (forward-line 1)
  21312. (while (re-search-forward re end t)
  21313. (if (not (match-end 1))
  21314. ;; empty or planning line
  21315. (forward-line 1)
  21316. ;; a drawer, find the end
  21317. (re-search-forward "^[ \t]*:END:" end 'move)
  21318. (forward-line 1)))
  21319. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21320. (point)))
  21321. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21322. "Move forward to the ARG'th subheading at same level as this one.
  21323. Stop at the first and last subheadings of a superior heading.
  21324. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21325. non-nil it will also look at invisible ones."
  21326. (interactive "p")
  21327. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21328. (if (and arg (< arg 0))
  21329. (goto-char (point-min))
  21330. (outline-next-heading))
  21331. (org-at-heading-p)
  21332. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21333. (f (if (and arg (< arg 0))
  21334. 're-search-backward
  21335. 're-search-forward))
  21336. (count (if arg (abs arg) 1))
  21337. (result (point)))
  21338. (while (and (prog1 (> count 0)
  21339. (forward-char (if (and arg (< arg 0)) -1 1)))
  21340. (funcall f org-outline-regexp-bol nil 'move))
  21341. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21342. (cond ((< l level) (setq count 0))
  21343. ((and (= l level)
  21344. (or invisible-ok
  21345. (progn
  21346. (goto-char (line-beginning-position))
  21347. (not (outline-invisible-p)))))
  21348. (setq count (1- count))
  21349. (when (eq l level)
  21350. (setq result (point)))))))
  21351. (goto-char result))
  21352. (beginning-of-line 1)))
  21353. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21354. "Move backward to the ARG'th subheading at same level as this one.
  21355. Stop at the first and last subheadings of a superior heading."
  21356. (interactive "p")
  21357. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21358. (defun org-next-block (arg &optional backward block-regexp)
  21359. "Jump to the next block.
  21360. With a prefix argument ARG, jump forward ARG many source blocks.
  21361. When BACKWARD is non-nil, jump to the previous block.
  21362. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21363. (interactive "p")
  21364. (let ((re (or block-regexp org-block-regexp))
  21365. (re-search-fn (or (and backward 're-search-backward)
  21366. 're-search-forward)))
  21367. (if (looking-at re) (forward-char 1))
  21368. (condition-case nil
  21369. (funcall re-search-fn re nil nil arg)
  21370. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21371. (goto-char (match-beginning 0)) (org-show-context)))
  21372. (defun org-previous-block (arg &optional block-regexp)
  21373. "Jump to the previous block.
  21374. With a prefix argument ARG, jump backward ARG many source blocks.
  21375. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21376. (interactive "p")
  21377. (org-next-block arg t block-regexp))
  21378. (defun org-forward-element ()
  21379. "Move forward by one element.
  21380. Move to the next element at the same level, when possible."
  21381. (interactive)
  21382. (cond ((eobp) (user-error "Cannot move further down"))
  21383. ((org-with-limited-levels (org-at-heading-p))
  21384. (let ((origin (point)))
  21385. (goto-char (org-end-of-subtree nil t))
  21386. (unless (org-with-limited-levels (org-at-heading-p))
  21387. (goto-char origin)
  21388. (user-error "Cannot move further down"))))
  21389. (t
  21390. (let* ((elem (org-element-at-point))
  21391. (end (org-element-property :end elem))
  21392. (parent (org-element-property :parent elem)))
  21393. (cond ((and parent (= (org-element-property :contents-end parent) end))
  21394. (goto-char (org-element-property :end parent)))
  21395. ((integer-or-marker-p end) (goto-char end))
  21396. (t (message "No element at point")))))))
  21397. (defun org-backward-element ()
  21398. "Move backward by one element.
  21399. Move to the previous element at the same level, when possible."
  21400. (interactive)
  21401. (cond ((bobp) (user-error "Cannot move further up"))
  21402. ((org-with-limited-levels (org-at-heading-p))
  21403. ;; At a headline, move to the previous one, if any, or stay
  21404. ;; here.
  21405. (let ((origin (point)))
  21406. (org-with-limited-levels (org-backward-heading-same-level 1))
  21407. ;; When current headline has no sibling above, move to its
  21408. ;; parent.
  21409. (when (= (point) origin)
  21410. (or (org-with-limited-levels (org-up-heading-safe))
  21411. (progn (goto-char origin)
  21412. (user-error "Cannot move further up"))))))
  21413. (t
  21414. (let* ((trail (org-element-at-point 'keep-trail))
  21415. (elem (car trail))
  21416. (prev-elem (nth 1 trail))
  21417. (beg (org-element-property :begin elem)))
  21418. (cond
  21419. ;; Move to beginning of current element if point isn't
  21420. ;; there already.
  21421. ((null beg) (message "No element at point"))
  21422. ((/= (point) beg) (goto-char beg))
  21423. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21424. ((org-before-first-heading-p) (goto-char (point-min)))
  21425. (t (org-back-to-heading)))))))
  21426. (defun org-up-element ()
  21427. "Move to upper element."
  21428. (interactive)
  21429. (if (org-with-limited-levels (org-at-heading-p))
  21430. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21431. (let* ((elem (org-element-at-point))
  21432. (parent (org-element-property :parent elem)))
  21433. (if parent (goto-char (org-element-property :begin parent))
  21434. (if (org-with-limited-levels (org-before-first-heading-p))
  21435. (user-error "No surrounding element")
  21436. (org-with-limited-levels (org-back-to-heading)))))))
  21437. (defvar org-element-greater-elements)
  21438. (defun org-down-element ()
  21439. "Move to inner element."
  21440. (interactive)
  21441. (let ((element (org-element-at-point)))
  21442. (cond
  21443. ((memq (org-element-type element) '(plain-list table))
  21444. (goto-char (org-element-property :contents-begin element))
  21445. (forward-char))
  21446. ((memq (org-element-type element) org-element-greater-elements)
  21447. ;; If contents are hidden, first disclose them.
  21448. (when (org-element-property :hiddenp element) (org-cycle))
  21449. (goto-char (or (org-element-property :contents-begin element)
  21450. (user-error "No content for this element"))))
  21451. (t (user-error "No inner element")))))
  21452. (defun org-drag-element-backward ()
  21453. "Move backward element at point."
  21454. (interactive)
  21455. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21456. (let* ((trail (org-element-at-point 'keep-trail))
  21457. (elem (car trail))
  21458. (prev-elem (nth 1 trail)))
  21459. ;; Error out if no previous element or previous element is
  21460. ;; a parent of the current one.
  21461. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21462. (user-error "Cannot drag element backward")
  21463. (let ((pos (point)))
  21464. (org-element-swap-A-B prev-elem elem)
  21465. (goto-char (+ (org-element-property :begin prev-elem)
  21466. (- pos (org-element-property :begin elem)))))))))
  21467. (defun org-drag-element-forward ()
  21468. "Move forward element at point."
  21469. (interactive)
  21470. (let* ((pos (point))
  21471. (elem (org-element-at-point)))
  21472. (when (= (point-max) (org-element-property :end elem))
  21473. (user-error "Cannot drag element forward"))
  21474. (goto-char (org-element-property :end elem))
  21475. (let ((next-elem (org-element-at-point)))
  21476. (when (or (org-element-nested-p elem next-elem)
  21477. (and (eq (org-element-type next-elem) 'headline)
  21478. (not (eq (org-element-type elem) 'headline))))
  21479. (goto-char pos)
  21480. (user-error "Cannot drag element forward"))
  21481. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21482. ;; body's length (without final blanks) and by the length of
  21483. ;; blanks between ELEM and NEXT-ELEM.
  21484. (let ((size-next (- (save-excursion
  21485. (goto-char (org-element-property :end next-elem))
  21486. (skip-chars-backward " \r\t\n")
  21487. (forward-line)
  21488. ;; Small correction if buffer doesn't end
  21489. ;; with a newline character.
  21490. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21491. (org-element-property :begin next-elem)))
  21492. (size-blank (- (org-element-property :end elem)
  21493. (save-excursion
  21494. (goto-char (org-element-property :end elem))
  21495. (skip-chars-backward " \r\t\n")
  21496. (forward-line)
  21497. (point)))))
  21498. (org-element-swap-A-B elem next-elem)
  21499. (goto-char (+ pos size-next size-blank))))))
  21500. (defun org-drag-line-forward (arg)
  21501. "Drag the line at point ARG lines forward."
  21502. (interactive "p")
  21503. (dotimes (n (abs arg))
  21504. (let ((c (current-column)))
  21505. (if (< 0 arg)
  21506. (progn
  21507. (beginning-of-line 2)
  21508. (transpose-lines 1)
  21509. (beginning-of-line 0))
  21510. (transpose-lines 1)
  21511. (beginning-of-line -1))
  21512. (org-move-to-column c))))
  21513. (defun org-drag-line-backward (arg)
  21514. "Drag the line at point ARG lines backward."
  21515. (interactive "p")
  21516. (org-drag-line-forward (- arg)))
  21517. (defun org-mark-element ()
  21518. "Put point at beginning of this element, mark at end.
  21519. Interactively, if this command is repeated or (in Transient Mark
  21520. mode) if the mark is active, it marks the next element after the
  21521. ones already marked."
  21522. (interactive)
  21523. (let (deactivate-mark)
  21524. (if (and (org-called-interactively-p 'any)
  21525. (or (and (eq last-command this-command) (mark t))
  21526. (and transient-mark-mode mark-active)))
  21527. (set-mark
  21528. (save-excursion
  21529. (goto-char (mark))
  21530. (goto-char (org-element-property :end (org-element-at-point)))))
  21531. (let ((element (org-element-at-point)))
  21532. (end-of-line)
  21533. (push-mark (org-element-property :end element) t t)
  21534. (goto-char (org-element-property :begin element))))))
  21535. (defun org-narrow-to-element ()
  21536. "Narrow buffer to current element."
  21537. (interactive)
  21538. (let ((elem (org-element-at-point)))
  21539. (cond
  21540. ((eq (car elem) 'headline)
  21541. (narrow-to-region
  21542. (org-element-property :begin elem)
  21543. (org-element-property :end elem)))
  21544. ((memq (car elem) org-element-greater-elements)
  21545. (narrow-to-region
  21546. (org-element-property :contents-begin elem)
  21547. (org-element-property :contents-end elem)))
  21548. (t
  21549. (narrow-to-region
  21550. (org-element-property :begin elem)
  21551. (org-element-property :end elem))))))
  21552. (defun org-transpose-element ()
  21553. "Transpose current and previous elements, keeping blank lines between.
  21554. Point is moved after both elements."
  21555. (interactive)
  21556. (org-skip-whitespace)
  21557. (let ((end (org-element-property :end (org-element-at-point))))
  21558. (org-drag-element-backward)
  21559. (goto-char end)))
  21560. (defun org-unindent-buffer ()
  21561. "Un-indent the visible part of the buffer.
  21562. Relative indentation (between items, inside blocks, etc.) isn't
  21563. modified."
  21564. (interactive)
  21565. (unless (eq major-mode 'org-mode)
  21566. (user-error "Cannot un-indent a buffer not in Org mode"))
  21567. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21568. unindent-tree ; For byte-compiler.
  21569. (unindent-tree
  21570. (function
  21571. (lambda (contents)
  21572. (mapc
  21573. (lambda (element)
  21574. (if (memq (org-element-type element) '(headline section))
  21575. (funcall unindent-tree (org-element-contents element))
  21576. (save-excursion
  21577. (save-restriction
  21578. (narrow-to-region
  21579. (org-element-property :begin element)
  21580. (org-element-property :end element))
  21581. (org-do-remove-indentation)))))
  21582. (reverse contents))))))
  21583. (funcall unindent-tree (org-element-contents parse-tree))))
  21584. (defun org-show-subtree ()
  21585. "Show everything after this heading at deeper levels."
  21586. (interactive)
  21587. (outline-flag-region
  21588. (point)
  21589. (save-excursion
  21590. (org-end-of-subtree t t))
  21591. nil))
  21592. (defun org-show-entry ()
  21593. "Show the body directly following this heading.
  21594. Show the heading too, if it is currently invisible."
  21595. (interactive)
  21596. (save-excursion
  21597. (condition-case nil
  21598. (progn
  21599. (org-back-to-heading t)
  21600. (outline-flag-region
  21601. (max (point-min) (1- (point)))
  21602. (save-excursion
  21603. (if (re-search-forward
  21604. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21605. (match-beginning 1)
  21606. (point-max)))
  21607. nil)
  21608. (org-cycle-hide-drawers 'children))
  21609. (error nil))))
  21610. (defun org-make-options-regexp (kwds &optional extra)
  21611. "Make a regular expression for keyword lines."
  21612. (concat
  21613. "^#\\+\\("
  21614. (mapconcat 'regexp-quote kwds "\\|")
  21615. (if extra (concat "\\|" extra))
  21616. "\\):[ \t]*\\(.*\\)"))
  21617. ;; Make isearch reveal the necessary context
  21618. (defun org-isearch-end ()
  21619. "Reveal context after isearch exits."
  21620. (when isearch-success ; only if search was successful
  21621. (if (featurep 'xemacs)
  21622. ;; Under XEmacs, the hook is run in the correct place,
  21623. ;; we directly show the context.
  21624. (org-show-context 'isearch)
  21625. ;; In Emacs the hook runs *before* restoring the overlays.
  21626. ;; So we have to use a one-time post-command-hook to do this.
  21627. ;; (Emacs 22 has a special variable, see function `org-mode')
  21628. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21629. isearch-mode-end-hook-quit)
  21630. ;; Only when the isearch was not quitted.
  21631. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21632. 'append 'local)))
  21633. (org-fix-ellipsis-at-bol)))
  21634. (defun org-isearch-post-command ()
  21635. "Remove self from hook, and show context."
  21636. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21637. (org-show-context 'isearch))
  21638. ;;;; Integration with and fixes for other packages
  21639. ;;; Imenu support
  21640. (defvar org-imenu-markers nil
  21641. "All markers currently used by Imenu.")
  21642. (make-variable-buffer-local 'org-imenu-markers)
  21643. (defun org-imenu-new-marker (&optional pos)
  21644. "Return a new marker for use by Imenu, and remember the marker."
  21645. (let ((m (make-marker)))
  21646. (move-marker m (or pos (point)))
  21647. (push m org-imenu-markers)
  21648. m))
  21649. (defun org-imenu-get-tree ()
  21650. "Produce the index for Imenu."
  21651. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21652. (setq org-imenu-markers nil)
  21653. (let* ((n org-imenu-depth)
  21654. (re (concat "^" (org-get-limited-outline-regexp)))
  21655. (subs (make-vector (1+ n) nil))
  21656. (last-level 0)
  21657. m level head0 head)
  21658. (save-excursion
  21659. (save-restriction
  21660. (widen)
  21661. (goto-char (point-max))
  21662. (while (re-search-backward re nil t)
  21663. (setq level (org-reduced-level (funcall outline-level)))
  21664. (when (and (<= level n)
  21665. (looking-at org-complex-heading-regexp)
  21666. (setq head0 (org-match-string-no-properties 4)))
  21667. (setq head (org-link-display-format head0)
  21668. m (org-imenu-new-marker))
  21669. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21670. (if (>= level last-level)
  21671. (push (cons head m) (aref subs level))
  21672. (push (cons head (aref subs (1+ level))) (aref subs level))
  21673. (loop for i from (1+ level) to n do (aset subs i nil)))
  21674. (setq last-level level)))))
  21675. (aref subs 1)))
  21676. (eval-after-load "imenu"
  21677. '(progn
  21678. (add-hook 'imenu-after-jump-hook
  21679. (lambda ()
  21680. (if (derived-mode-p 'org-mode)
  21681. (org-show-context 'org-goto))))))
  21682. (defun org-link-display-format (link)
  21683. "Replace a link with its the description.
  21684. If there is no description, use the link target."
  21685. (save-match-data
  21686. (if (string-match org-bracket-link-analytic-regexp link)
  21687. (replace-match (if (match-end 5)
  21688. (match-string 5 link)
  21689. (concat (match-string 1 link)
  21690. (match-string 3 link)))
  21691. nil t link)
  21692. link)))
  21693. (defun org-toggle-link-display ()
  21694. "Toggle the literal or descriptive display of links."
  21695. (interactive)
  21696. (if org-descriptive-links
  21697. (progn (org-remove-from-invisibility-spec '(org-link))
  21698. (org-restart-font-lock)
  21699. (setq org-descriptive-links nil))
  21700. (progn (add-to-invisibility-spec '(org-link))
  21701. (org-restart-font-lock)
  21702. (setq org-descriptive-links t))))
  21703. ;; Speedbar support
  21704. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21705. "Overlay marking the agenda restriction line in speedbar.")
  21706. (overlay-put org-speedbar-restriction-lock-overlay
  21707. 'face 'org-agenda-restriction-lock)
  21708. (overlay-put org-speedbar-restriction-lock-overlay
  21709. 'help-echo "Agendas are currently limited to this item.")
  21710. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21711. (defun org-speedbar-set-agenda-restriction ()
  21712. "Restrict future agenda commands to the location at point in speedbar.
  21713. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21714. (interactive)
  21715. (require 'org-agenda)
  21716. (let (p m tp np dir txt)
  21717. (cond
  21718. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21719. 'org-imenu t))
  21720. (setq m (get-text-property p 'org-imenu-marker))
  21721. (with-current-buffer (marker-buffer m)
  21722. (goto-char m)
  21723. (org-agenda-set-restriction-lock 'subtree)))
  21724. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21725. 'speedbar-function 'speedbar-find-file))
  21726. (setq tp (previous-single-property-change
  21727. (1+ p) 'speedbar-function)
  21728. np (next-single-property-change
  21729. tp 'speedbar-function)
  21730. dir (speedbar-line-directory)
  21731. txt (buffer-substring-no-properties (or tp (point-min))
  21732. (or np (point-max))))
  21733. (with-current-buffer (find-file-noselect
  21734. (let ((default-directory dir))
  21735. (expand-file-name txt)))
  21736. (unless (derived-mode-p 'org-mode)
  21737. (user-error "Cannot restrict to non-Org-mode file"))
  21738. (org-agenda-set-restriction-lock 'file)))
  21739. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21740. (move-overlay org-speedbar-restriction-lock-overlay
  21741. (point-at-bol) (point-at-eol))
  21742. (setq current-prefix-arg nil)
  21743. (org-agenda-maybe-redo)))
  21744. (defvar speedbar-file-key-map)
  21745. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  21746. (eval-after-load "speedbar"
  21747. '(progn
  21748. (speedbar-add-supported-extension ".org")
  21749. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21750. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21751. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21752. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21753. (add-hook 'speedbar-visiting-tag-hook
  21754. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21755. ;;; Fixes and Hacks for problems with other packages
  21756. ;; Make flyspell not check words in links, to not mess up our keymap
  21757. (defvar org-element-affiliated-keywords) ; From org-element.el
  21758. (defvar org-element-block-name-alist) ; From org-element.el
  21759. (defun org-mode-flyspell-verify ()
  21760. "Don't let flyspell put overlays at active buttons, or on
  21761. {todo,all-time,additional-option-like}-keywords."
  21762. (require 'org-element) ; For `org-element-affiliated-keywords'
  21763. (let ((pos (max (1- (point)) (point-min)))
  21764. (word (thing-at-point 'word)))
  21765. (and (not (get-text-property pos 'keymap))
  21766. (not (get-text-property pos 'org-no-flyspell))
  21767. (not (member word org-todo-keywords-1))
  21768. (not (member word org-all-time-keywords))
  21769. (not (member word org-options-keywords))
  21770. (not (member word (mapcar 'car org-startup-options)))
  21771. (not (member-ignore-case word org-element-affiliated-keywords))
  21772. (not (member-ignore-case word (org-get-export-keywords)))
  21773. (not (member-ignore-case
  21774. word (mapcar 'car org-element-block-name-alist)))
  21775. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  21776. (not (org-in-src-block-p)))))
  21777. (defun org-remove-flyspell-overlays-in (beg end)
  21778. "Remove flyspell overlays in region."
  21779. (and (org-bound-and-true-p flyspell-mode)
  21780. (fboundp 'flyspell-delete-region-overlays)
  21781. (flyspell-delete-region-overlays beg end))
  21782. (add-text-properties beg end '(org-no-flyspell t)))
  21783. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21784. (eval-after-load "bookmark"
  21785. '(if (boundp 'bookmark-after-jump-hook)
  21786. ;; We can use the hook
  21787. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21788. ;; Hook not available, use advice
  21789. (defadvice bookmark-jump (after org-make-visible activate)
  21790. "Make the position visible."
  21791. (org-bookmark-jump-unhide))))
  21792. ;; Make sure saveplace shows the location if it was hidden
  21793. (eval-after-load "saveplace"
  21794. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21795. "Make the position visible."
  21796. (org-bookmark-jump-unhide)))
  21797. ;; Make sure ecb shows the location if it was hidden
  21798. (eval-after-load "ecb"
  21799. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21800. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21801. (if (derived-mode-p 'org-mode)
  21802. (org-show-context))))
  21803. (defun org-bookmark-jump-unhide ()
  21804. "Unhide the current position, to show the bookmark location."
  21805. (and (derived-mode-p 'org-mode)
  21806. (or (outline-invisible-p)
  21807. (save-excursion (goto-char (max (point-min) (1- (point))))
  21808. (outline-invisible-p)))
  21809. (org-show-context 'bookmark-jump)))
  21810. ;; Make session.el ignore our circular variable
  21811. (defvar session-globals-exclude)
  21812. (eval-after-load "session"
  21813. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21814. ;;;; Finish up
  21815. (provide 'org)
  21816. (run-hooks 'org-load-hook)
  21817. ;;; org.el ends here