org.el 890 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550
  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: Bastien Guerry <bzg at gnu 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. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t t)
  74. (require 'org-macs)
  75. (require 'org-compat)
  76. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  77. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  78. ;;
  79. ;; In Org buffers, the value of `outline-regexp' is that of
  80. ;; `org-outline-regexp'. The only function still directly relying on
  81. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  82. ;; job when `orgstruct-mode' is active.
  83. (defvar org-outline-regexp "\\*+ "
  84. "Regexp to match Org headlines.")
  85. (defvar org-outline-regexp-bol "^\\*+ "
  86. "Regexp to match Org headlines.
  87. This is similar to `org-outline-regexp' but additionally makes
  88. sure that we are at the beginning of the line.")
  89. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  90. "Matches a headline, putting stars and text into groups.
  91. Stars are put in group 1 and the trimmed body in group 2.")
  92. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  93. (unless (boundp 'calendar-view-holidays-initially-flag)
  94. (org-defvaralias 'calendar-view-holidays-initially-flag
  95. 'view-calendar-holidays-initially))
  96. (unless (boundp 'calendar-view-diary-initially-flag)
  97. (org-defvaralias 'calendar-view-diary-initially-flag
  98. 'view-diary-entries-initially))
  99. (unless (boundp 'diary-fancy-buffer)
  100. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  101. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  102. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  103. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  104. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  105. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  106. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  107. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  108. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  109. (declare-function orgtbl-mode "org-table" (&optional arg))
  110. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  111. (declare-function org-beamer-mode "ox-beamer" ())
  112. (declare-function org-table-edit-field "org-table" (arg))
  113. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  114. (declare-function org-table-set-constants "org-table" ())
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-agenda-redo "org-agenda" (&optional all))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-restriction "org-element" (element))
  145. (declare-function org-element-type "org-element" (element))
  146. ;; load languages based on value of `org-babel-load-languages'
  147. (defvar org-babel-load-languages)
  148. ;;;###autoload
  149. (defun org-babel-do-load-languages (sym value)
  150. "Load the languages defined in `org-babel-load-languages'."
  151. (set-default sym value)
  152. (mapc (lambda (pair)
  153. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  154. (if active
  155. (progn
  156. (require (intern (concat "ob-" lang))))
  157. (progn
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-execute:" lang)))
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-expand-body:" lang)))))))
  162. org-babel-load-languages))
  163. ;;;###autoload
  164. (defun org-babel-load-file (file &optional compile)
  165. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  166. This function exports the source code using `org-babel-tangle'
  167. and then loads the resulting file using `load-file'. With prefix
  168. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  169. file to byte-code before it is loaded."
  170. (interactive "fFile to load: \nP")
  171. (require 'ob-core)
  172. (let* ((age (lambda (file)
  173. (float-time
  174. (time-subtract (current-time)
  175. (nth 5 (or (file-attributes (file-truename file))
  176. (file-attributes file)))))))
  177. (base-name (file-name-sans-extension file))
  178. (exported-file (concat base-name ".el")))
  179. ;; tangle if the org-mode file is newer than the elisp file
  180. (unless (and (file-exists-p exported-file)
  181. (> (funcall age file) (funcall age exported-file)))
  182. (org-babel-tangle-file file exported-file "emacs-lisp"))
  183. (message "%s %s"
  184. (if compile
  185. (progn (byte-compile-file exported-file 'load)
  186. "Compiled and loaded")
  187. (progn (load-file exported-file) "Loaded"))
  188. exported-file)))
  189. (defcustom org-babel-load-languages '((emacs-lisp . t))
  190. "Languages which can be evaluated in Org-mode buffers.
  191. This list can be used to load support for any of the languages
  192. below, note that each language will depend on a different set of
  193. system executables and/or Emacs modes. When a language is
  194. \"loaded\", then code blocks in that language can be evaluated
  195. with `org-babel-execute-src-block' bound by default to C-c
  196. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  197. be set to remove code block evaluation from the C-c C-c
  198. keybinding. By default only Emacs Lisp (which has no
  199. requirements) is loaded."
  200. :group 'org-babel
  201. :set 'org-babel-do-load-languages
  202. :version "24.1"
  203. :type '(alist :tag "Babel Languages"
  204. :key-type
  205. (choice
  206. (const :tag "Awk" awk)
  207. (const :tag "C" C)
  208. (const :tag "R" R)
  209. (const :tag "Asymptote" asymptote)
  210. (const :tag "Calc" calc)
  211. (const :tag "Clojure" clojure)
  212. (const :tag "CSS" css)
  213. (const :tag "Ditaa" ditaa)
  214. (const :tag "Dot" dot)
  215. (const :tag "Emacs Lisp" emacs-lisp)
  216. (const :tag "Fortran" fortran)
  217. (const :tag "Gnuplot" gnuplot)
  218. (const :tag "Haskell" haskell)
  219. (const :tag "IO" io)
  220. (const :tag "Java" java)
  221. (const :tag "Javascript" js)
  222. (const :tag "LaTeX" latex)
  223. (const :tag "Ledger" ledger)
  224. (const :tag "Lilypond" lilypond)
  225. (const :tag "Lisp" lisp)
  226. (const :tag "Makefile" makefile)
  227. (const :tag "Maxima" maxima)
  228. (const :tag "Matlab" matlab)
  229. (const :tag "Mscgen" mscgen)
  230. (const :tag "Ocaml" ocaml)
  231. (const :tag "Octave" octave)
  232. (const :tag "Org" org)
  233. (const :tag "Perl" perl)
  234. (const :tag "Pico Lisp" picolisp)
  235. (const :tag "PlantUML" plantuml)
  236. (const :tag "Python" python)
  237. (const :tag "Ruby" ruby)
  238. (const :tag "Sass" sass)
  239. (const :tag "Scala" scala)
  240. (const :tag "Scheme" scheme)
  241. (const :tag "Screen" screen)
  242. (const :tag "Shell Script" sh)
  243. (const :tag "Shen" shen)
  244. (const :tag "Sql" sql)
  245. (const :tag "Sqlite" sqlite))
  246. :value-type (boolean :tag "Activate" :value t)))
  247. ;;;; Customization variables
  248. (defcustom org-clone-delete-id nil
  249. "Remove ID property of clones of a subtree.
  250. When non-nil, clones of a subtree don't inherit the ID property.
  251. Otherwise they inherit the ID property with a new unique
  252. identifier."
  253. :type 'boolean
  254. :version "24.1"
  255. :group 'org-id)
  256. ;;; Version
  257. (org-check-version)
  258. ;;;###autoload
  259. (defun org-version (&optional here full message)
  260. "Show the org-mode version in the echo area.
  261. With prefix argument HERE, insert it at point.
  262. When FULL is non-nil, use a verbose version string.
  263. When MESSAGE is non-nil, display a message with the version."
  264. (interactive "P")
  265. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  266. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  267. (load-suffixes (list ".el"))
  268. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  269. (org-trash (or
  270. (and (fboundp 'org-release) (fboundp 'org-git-version))
  271. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  272. (load-suffixes save-load-suffixes)
  273. (org-version (org-release))
  274. (git-version (org-git-version))
  275. (version (format "Org-mode version %s (%s @ %s)"
  276. org-version
  277. git-version
  278. (if org-install-dir
  279. (if (string= org-dir org-install-dir)
  280. org-install-dir
  281. (concat "mixed installation! " org-install-dir " and " org-dir))
  282. "org-loaddefs.el can not be found!")))
  283. (_version (if full version org-version)))
  284. (if (org-called-interactively-p 'interactive)
  285. (if here
  286. (insert version)
  287. (message version))
  288. (if message (message _version))
  289. _version)))
  290. (defconst org-version (org-version))
  291. ;;; Compatibility constants
  292. ;;; The custom variables
  293. (defgroup org nil
  294. "Outline-based notes management and organizer."
  295. :tag "Org"
  296. :group 'outlines
  297. :group 'calendar)
  298. (defcustom org-mode-hook nil
  299. "Mode hook for Org-mode, run after the mode was turned on."
  300. :group 'org
  301. :type 'hook)
  302. (defcustom org-load-hook nil
  303. "Hook that is run after org.el has been loaded."
  304. :group 'org
  305. :type 'hook)
  306. (defcustom org-log-buffer-setup-hook nil
  307. "Hook that is run after an Org log buffer is created."
  308. :group 'org
  309. :version "24.1"
  310. :type 'hook)
  311. (defvar org-modules) ; defined below
  312. (defvar org-modules-loaded nil
  313. "Have the modules been loaded already?")
  314. (defun org-load-modules-maybe (&optional force)
  315. "Load all extensions listed in `org-modules'."
  316. (when (or force (not org-modules-loaded))
  317. (mapc (lambda (ext)
  318. (condition-case nil (require ext)
  319. (error (message "Problems while trying to load feature `%s'" ext))))
  320. org-modules)
  321. (setq org-modules-loaded t)))
  322. (defun org-set-modules (var value)
  323. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  324. (set var value)
  325. (when (featurep 'org)
  326. (org-load-modules-maybe 'force)))
  327. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  328. "Modules that should always be loaded together with org.el.
  329. If a description starts with <C>, the file is not part of Emacs
  330. and loading it will require that you have downloaded and properly
  331. installed the Org mode distribution.
  332. You can also use this system to load external packages (i.e. neither Org
  333. core modules, nor modules from the CONTRIB directory). Just add symbols
  334. to the end of the list. If the package is called org-xyz.el, then you need
  335. to add the symbol `xyz', and the package must have a call to:
  336. \(provide 'org-xyz)
  337. For export specific modules, see also `org-export-backends'."
  338. :group 'org
  339. :set 'org-set-modules
  340. :version "24.4"
  341. :package-version '(Org . "8.0")
  342. :type
  343. '(set :greedy t
  344. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  345. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  346. (const :tag " crypt: Encryption of subtrees" org-crypt)
  347. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  348. (const :tag " docview: Links to doc-view buffers" org-docview)
  349. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  350. (const :tag " habit: Track your consistency with habits" org-habit)
  351. (const :tag " id: Global IDs for identifying entries" org-id)
  352. (const :tag " info: Links to Info nodes" org-info)
  353. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  354. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  355. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  356. (const :tag " mouse: Additional mouse support" org-mouse)
  357. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  358. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  359. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  360. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  361. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  362. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  363. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  364. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  365. (const :tag "C collector: Collect properties into tables" org-collector)
  366. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  367. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  368. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  369. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  370. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  371. (const :tag "C eval: Include command output as text" org-eval)
  372. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  373. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  374. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  375. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  376. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  377. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  378. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  379. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  380. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  381. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  382. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  383. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  384. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  385. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  386. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  387. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  388. (const :tag "C registry: A registry for Org-mode links" org-registry)
  389. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  390. (const :tag "C secretary: Team management with org-mode" org-secretary)
  391. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  392. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  393. (const :tag "C track: Keep up with Org-mode development" org-track)
  394. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  395. (const :tag "C vm: Links to VM folders/messages" org-vm)
  396. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  397. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  398. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  399. (defvar org-export-registered-backends) ; From ox.el
  400. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  401. (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 VALUE stands for the
  412. new value of the variable, after updating it:
  413. \(progn
  414. \(setq org-export-registered-backends
  415. \(org-remove-if-not
  416. \(lambda (backend)
  417. \(or (memq backend val)
  418. \(catch 'parentp
  419. \(mapc
  420. \(lambda (b)
  421. \(and (org-export-derived-backend-p b (car backend))
  422. \(throw 'parentp t)))
  423. val)
  424. nil)))
  425. org-export-registered-backends))
  426. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  427. \(dolist (backend val)
  428. \(cond
  429. \((not (load (format \"ox-%s\" backend) t t))
  430. \(message \"Problems while trying to load export back-end `%s'\"
  431. backend))
  432. \((not (memq backend new-list)) (push backend new-list))))
  433. \(set-default var new-list)))
  434. Adding a back-end to this list will also pull the back-end it
  435. depends on, if any."
  436. :group 'org
  437. :group 'org-export
  438. :version "24.4"
  439. :package-version '(Org . "8.0")
  440. :initialize 'custom-initialize-set
  441. :set (lambda (var val)
  442. (if (not (featurep 'ox)) (set-default var val)
  443. ;; Any back-end not required anymore (not present in VAL and not
  444. ;; a parent of any back-end in the new value) is removed from the
  445. ;; list of registered back-ends.
  446. (setq org-export-registered-backends
  447. (org-remove-if-not
  448. (lambda (backend)
  449. (or (memq backend val)
  450. (catch 'parentp
  451. (mapc
  452. (lambda (b)
  453. (and (org-export-derived-backend-p b (car backend))
  454. (throw 'parentp t)))
  455. val)
  456. nil)))
  457. org-export-registered-backends))
  458. ;; Now build NEW-LIST of both new back-ends and required
  459. ;; parents.
  460. (let ((new-list (mapcar 'car org-export-registered-backends)))
  461. (dolist (backend val)
  462. (cond
  463. ((not (load (format "ox-%s" backend) t t))
  464. (message "Problems while trying to load export back-end `%s'"
  465. backend))
  466. ((not (memq backend new-list)) (push backend new-list))))
  467. ;; Set VAR to that list with fixed dependencies.
  468. (set-default var new-list))))
  469. :type '(set :greedy t
  470. (const :tag " ascii Export buffer to ASCII format" ascii)
  471. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  472. (const :tag " html Export buffer to HTML format" html)
  473. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  474. (const :tag " latex Export buffer to LaTeX format" latex)
  475. (const :tag " man Export buffer to MAN format" man)
  476. (const :tag " md Export buffer to Markdown format" md)
  477. (const :tag " odt Export buffer to ODT format" odt)
  478. (const :tag " org Export buffer to Org format" org)
  479. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  480. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  481. (const :tag "C deck Export buffer to deck.js presentations" deck)
  482. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  483. (const :tag "C groff Export buffer to Groff format" groff)
  484. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  485. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  486. (const :tag "C s5 Export buffer to s5 presentations" s5)
  487. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  488. (eval-after-load 'ox
  489. '(mapc
  490. (lambda (backend)
  491. (condition-case nil (require (intern (format "ox-%s" backend)))
  492. (error (message "Problems while trying to load export back-end `%s'"
  493. backend))))
  494. org-export-backends))
  495. (defcustom org-support-shift-select nil
  496. "Non-nil means make shift-cursor commands select text when possible.
  497. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  498. start selecting a region, or enlarge regions started in this way.
  499. In Org-mode, in special contexts, these same keys are used for
  500. other purposes, important enough to compete with shift selection.
  501. Org tries to balance these needs by supporting `shift-select-mode'
  502. outside these special contexts, under control of this variable.
  503. The default of this variable is nil, to avoid confusing behavior. Shifted
  504. cursor keys will then execute Org commands in the following contexts:
  505. - on a headline, changing TODO state (left/right) and priority (up/down)
  506. - on a time stamp, changing the time
  507. - in a plain list item, changing the bullet type
  508. - in a property definition line, switching between allowed values
  509. - in the BEGIN line of a clock table (changing the time block).
  510. Outside these contexts, the commands will throw an error.
  511. When this variable is t and the cursor is not in a special
  512. context, Org-mode will support shift-selection for making and
  513. enlarging regions. To make this more effective, the bullet
  514. cycling will no longer happen anywhere in an item line, but only
  515. if the cursor is exactly on the bullet.
  516. If you set this variable to the symbol `always', then the keys
  517. will not be special in headlines, property lines, and item lines,
  518. to make shift selection work there as well. If this is what you
  519. want, you can use the following alternative commands: `C-c C-t'
  520. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  521. can be used to switch TODO sets, `C-c -' to cycle item bullet
  522. types, and properties can be edited by hand or in column view.
  523. However, when the cursor is on a timestamp, shift-cursor commands
  524. will still edit the time stamp - this is just too good to give up.
  525. XEmacs user should have this variable set to nil, because
  526. `shift-select-mode' is in Emacs 23 or later only."
  527. :group 'org
  528. :type '(choice
  529. (const :tag "Never" nil)
  530. (const :tag "When outside special context" t)
  531. (const :tag "Everywhere except timestamps" always)))
  532. (defcustom org-loop-over-headlines-in-active-region nil
  533. "Shall some commands act upon headlines in the active region?
  534. When set to `t', some commands will be performed in all headlines
  535. within the active region.
  536. When set to `start-level', some commands will be performed in all
  537. headlines within the active region, provided that these headlines
  538. are of the same level than the first one.
  539. When set to a string, those commands will be performed on the
  540. matching headlines within the active region. Such string must be
  541. a tags/property/todo match as it is used in the agenda tags view.
  542. The list of commands is: `org-schedule', `org-deadline',
  543. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  544. `org-archive-to-archive-sibling'. The archiving commands skip
  545. already archived entries."
  546. :type '(choice (const :tag "Don't loop" nil)
  547. (const :tag "All headlines in active region" t)
  548. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  549. (string :tag "Tags/Property/Todo matcher"))
  550. :version "24.1"
  551. :group 'org-todo
  552. :group 'org-archive)
  553. (defgroup org-startup nil
  554. "Options concerning startup of Org-mode."
  555. :tag "Org Startup"
  556. :group 'org)
  557. (defcustom org-startup-folded t
  558. "Non-nil means entering Org-mode will switch to OVERVIEW.
  559. This can also be configured on a per-file basis by adding one of
  560. the following lines anywhere in the buffer:
  561. #+STARTUP: fold (or `overview', this is equivalent)
  562. #+STARTUP: nofold (or `showall', this is equivalent)
  563. #+STARTUP: content
  564. #+STARTUP: showeverything
  565. By default, this option is ignored when Org opens agenda files
  566. for the first time. If you want the agenda to honor the startup
  567. option, set `org-agenda-inhibit-startup' to nil."
  568. :group 'org-startup
  569. :type '(choice
  570. (const :tag "nofold: show all" nil)
  571. (const :tag "fold: overview" t)
  572. (const :tag "content: all headlines" content)
  573. (const :tag "show everything, even drawers" showeverything)))
  574. (defcustom org-startup-truncated t
  575. "Non-nil means entering Org-mode will set `truncate-lines'.
  576. This is useful since some lines containing links can be very long and
  577. uninteresting. Also tables look terrible when wrapped."
  578. :group 'org-startup
  579. :type 'boolean)
  580. (defcustom org-startup-indented nil
  581. "Non-nil means turn on `org-indent-mode' on startup.
  582. This can also be configured on a per-file basis by adding one of
  583. the following lines anywhere in the buffer:
  584. #+STARTUP: indent
  585. #+STARTUP: noindent"
  586. :group 'org-structure
  587. :type '(choice
  588. (const :tag "Not" nil)
  589. (const :tag "Globally (slow on startup in large files)" t)))
  590. (defcustom org-use-sub-superscripts t
  591. "Non-nil means interpret \"_\" and \"^\" for display.
  592. When this option is turned on, you can use TeX-like syntax for sub- and
  593. superscripts. Several characters after \"_\" or \"^\" will be
  594. considered as a single item - so grouping with {} is normally not
  595. needed. For example, the following things will be parsed as single
  596. sub- or superscripts.
  597. 10^24 or 10^tau several digits will be considered 1 item.
  598. 10^-12 or 10^-tau a leading sign with digits or a word
  599. x^2-y^3 will be read as x^2 - y^3, because items are
  600. terminated by almost any nonword/nondigit char.
  601. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  602. Still, ambiguity is possible - so when in doubt use {} to enclose
  603. the sub/superscript. If you set this variable to the symbol
  604. `{}', the braces are *required* in order to trigger
  605. interpretations as sub/superscript. This can be helpful in
  606. documents that need \"_\" frequently in plain text."
  607. :group 'org-startup
  608. :version "24.1"
  609. :type '(choice
  610. (const :tag "Always interpret" t)
  611. (const :tag "Only with braces" {})
  612. (const :tag "Never interpret" nil)))
  613. (defcustom org-startup-with-beamer-mode nil
  614. "Non-nil means turn on `org-beamer-mode' on startup.
  615. This can also be configured on a per-file basis by adding one of
  616. the following lines anywhere in the buffer:
  617. #+STARTUP: beamer"
  618. :group 'org-startup
  619. :version "24.1"
  620. :type 'boolean)
  621. (defcustom org-startup-align-all-tables nil
  622. "Non-nil means align all tables when visiting a file.
  623. This is useful when the column width in tables is forced with <N> cookies
  624. in table fields. Such tables will look correct only after the first re-align.
  625. This can also be configured on a per-file basis by adding one of
  626. the following lines anywhere in the buffer:
  627. #+STARTUP: align
  628. #+STARTUP: noalign"
  629. :group 'org-startup
  630. :type 'boolean)
  631. (defcustom org-startup-with-inline-images nil
  632. "Non-nil means show inline images when loading a new Org file.
  633. This can also be configured on a per-file basis by adding one of
  634. the following lines anywhere in the buffer:
  635. #+STARTUP: inlineimages
  636. #+STARTUP: noinlineimages"
  637. :group 'org-startup
  638. :version "24.1"
  639. :type 'boolean)
  640. (defcustom org-startup-with-latex-preview nil
  641. "Non-nil means preview LaTeX fragments when loading a new Org file.
  642. This can also be configured on a per-file basis by adding one of
  643. the followinglines anywhere in the buffer:
  644. #+STARTUP: latexpreview
  645. #+STARTUP: nolatexpreview"
  646. :group 'org-startup
  647. :version "24.4"
  648. :package-version '(Org . "8.0")
  649. :type 'boolean)
  650. (defcustom org-insert-mode-line-in-empty-file nil
  651. "Non-nil means insert the first line setting Org-mode in empty files.
  652. When the function `org-mode' is called interactively in an empty file, this
  653. normally means that the file name does not automatically trigger Org-mode.
  654. To ensure that the file will always be in Org-mode in the future, a
  655. line enforcing Org-mode will be inserted into the buffer, if this option
  656. has been set."
  657. :group 'org-startup
  658. :type 'boolean)
  659. (defcustom org-replace-disputed-keys nil
  660. "Non-nil means use alternative key bindings for some keys.
  661. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  662. These keys are also used by other packages like shift-selection-mode'
  663. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  664. If you want to use Org-mode together with one of these other modes,
  665. or more generally if you would like to move some Org-mode commands to
  666. other keys, set this variable and configure the keys with the variable
  667. `org-disputed-keys'.
  668. This option is only relevant at load-time of Org-mode, and must be set
  669. *before* org.el is loaded. Changing it requires a restart of Emacs to
  670. become effective."
  671. :group 'org-startup
  672. :type 'boolean)
  673. (defcustom org-use-extra-keys nil
  674. "Non-nil means use extra key sequence definitions for certain commands.
  675. This happens automatically if you run XEmacs or if `window-system'
  676. is nil. This variable lets you do the same manually. You must
  677. set it before loading org.
  678. Example: on Carbon Emacs 22 running graphically, with an external
  679. keyboard on a Powerbook, the default way of setting M-left might
  680. not work for either Alt or ESC. Setting this variable will make
  681. it work for ESC."
  682. :group 'org-startup
  683. :type 'boolean)
  684. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  685. (defcustom org-disputed-keys
  686. '(([(shift up)] . [(meta p)])
  687. ([(shift down)] . [(meta n)])
  688. ([(shift left)] . [(meta -)])
  689. ([(shift right)] . [(meta +)])
  690. ([(control shift right)] . [(meta shift +)])
  691. ([(control shift left)] . [(meta shift -)]))
  692. "Keys for which Org-mode and other modes compete.
  693. This is an alist, cars are the default keys, second element specifies
  694. the alternative to use when `org-replace-disputed-keys' is t.
  695. Keys can be specified in any syntax supported by `define-key'.
  696. The value of this option takes effect only at Org-mode's startup,
  697. therefore you'll have to restart Emacs to apply it after changing."
  698. :group 'org-startup
  699. :type 'alist)
  700. (defun org-key (key)
  701. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  702. Or return the original if not disputed.
  703. Also apply the translations defined in `org-xemacs-key-equivalents'."
  704. (when org-replace-disputed-keys
  705. (let* ((nkey (key-description key))
  706. (x (org-find-if (lambda (x)
  707. (equal (key-description (car x)) nkey))
  708. org-disputed-keys)))
  709. (setq key (if x (cdr x) key))))
  710. (when (featurep 'xemacs)
  711. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  712. key)
  713. (defun org-find-if (predicate seq)
  714. (catch 'exit
  715. (while seq
  716. (if (funcall predicate (car seq))
  717. (throw 'exit (car seq))
  718. (pop seq)))))
  719. (defun org-defkey (keymap key def)
  720. "Define a key, possibly translated, as returned by `org-key'."
  721. (define-key keymap (org-key key) def))
  722. (defcustom org-ellipsis nil
  723. "The ellipsis to use in the Org-mode outline.
  724. When nil, just use the standard three dots. When a string, use that instead,
  725. When a face, use the standard 3 dots, but with the specified face.
  726. The change affects only Org-mode (which will then use its own display table).
  727. Changing this requires executing `M-x org-mode' in a buffer to become
  728. effective."
  729. :group 'org-startup
  730. :type '(choice (const :tag "Default" nil)
  731. (face :tag "Face" :value org-warning)
  732. (string :tag "String" :value "...#")))
  733. (defvar org-display-table nil
  734. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  735. (defgroup org-keywords nil
  736. "Keywords in Org-mode."
  737. :tag "Org Keywords"
  738. :group 'org)
  739. (defcustom org-deadline-string "DEADLINE:"
  740. "String to mark deadline entries.
  741. A deadline is this string, followed by a time stamp. Should be a word,
  742. terminated by a colon. You can insert a schedule keyword and
  743. a timestamp with \\[org-deadline].
  744. Changes become only effective after restarting Emacs."
  745. :group 'org-keywords
  746. :type 'string)
  747. (defcustom org-scheduled-string "SCHEDULED:"
  748. "String to mark scheduled TODO entries.
  749. A schedule is this string, followed by a time stamp. Should be a word,
  750. terminated by a colon. You can insert a schedule keyword and
  751. a timestamp with \\[org-schedule].
  752. Changes become only effective after restarting Emacs."
  753. :group 'org-keywords
  754. :type 'string)
  755. (defcustom org-closed-string "CLOSED:"
  756. "String used as the prefix for timestamps logging closing a TODO entry."
  757. :group 'org-keywords
  758. :type 'string)
  759. (defcustom org-clock-string "CLOCK:"
  760. "String used as prefix for timestamps clocking work hours on an item."
  761. :group 'org-keywords
  762. :type 'string)
  763. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  764. org-scheduled-string "\\|"
  765. org-deadline-string "\\|"
  766. org-closed-string "\\|"
  767. org-clock-string "\\)")
  768. "Matches a line with planning or clock info.")
  769. (defcustom org-comment-string "COMMENT"
  770. "Entries starting with this keyword will never be exported.
  771. An entry can be toggled between COMMENT and normal with
  772. \\[org-toggle-comment].
  773. Changes become only effective after restarting Emacs."
  774. :group 'org-keywords
  775. :type 'string)
  776. (defcustom org-quote-string "QUOTE"
  777. "Entries starting with this keyword will be exported in fixed-width font.
  778. Quoting applies only to the text in the entry following the headline, and does
  779. not extend beyond the next headline, even if that is lower level.
  780. An entry can be toggled between QUOTE and normal with
  781. \\[org-toggle-fixed-width-section]."
  782. :group 'org-keywords
  783. :type 'string)
  784. (defconst org-repeat-re
  785. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  786. "Regular expression for specifying repeated events.
  787. After a match, group 1 contains the repeat expression.")
  788. (defgroup org-structure nil
  789. "Options concerning the general structure of Org-mode files."
  790. :tag "Org Structure"
  791. :group 'org)
  792. (defgroup org-reveal-location nil
  793. "Options about how to make context of a location visible."
  794. :tag "Org Reveal Location"
  795. :group 'org-structure)
  796. (defconst org-context-choice
  797. '(choice
  798. (const :tag "Always" t)
  799. (const :tag "Never" nil)
  800. (repeat :greedy t :tag "Individual contexts"
  801. (cons
  802. (choice :tag "Context"
  803. (const agenda)
  804. (const org-goto)
  805. (const occur-tree)
  806. (const tags-tree)
  807. (const link-search)
  808. (const mark-goto)
  809. (const bookmark-jump)
  810. (const isearch)
  811. (const default))
  812. (boolean))))
  813. "Contexts for the reveal options.")
  814. (defcustom org-show-hierarchy-above '((default . t))
  815. "Non-nil means show full hierarchy when revealing a location.
  816. Org-mode often shows locations in an org-mode file which might have
  817. been invisible before. When this is set, the hierarchy of headings
  818. above the exposed location is shown.
  819. Turning this off for example for sparse trees makes them very compact.
  820. Instead of t, this can also be an alist specifying this option for different
  821. contexts. Valid contexts are
  822. agenda when exposing an entry from the agenda
  823. org-goto when using the command `org-goto' on key C-c C-j
  824. occur-tree when using the command `org-occur' on key C-c /
  825. tags-tree when constructing a sparse tree based on tags matches
  826. link-search when exposing search matches associated with a link
  827. mark-goto when exposing the jump goal of a mark
  828. bookmark-jump when exposing a bookmark location
  829. isearch when exiting from an incremental search
  830. default default for all contexts not set explicitly"
  831. :group 'org-reveal-location
  832. :type org-context-choice)
  833. (defcustom org-show-following-heading '((default . nil))
  834. "Non-nil means show following heading when revealing a location.
  835. Org-mode often shows locations in an org-mode file which might have
  836. been invisible before. When this is set, the heading following the
  837. match is shown.
  838. Turning this off for example for sparse trees makes them very compact,
  839. but makes it harder to edit the location of the match. In such a case,
  840. use the command \\[org-reveal] to show more context.
  841. Instead of t, this can also be an alist specifying this option for different
  842. contexts. See `org-show-hierarchy-above' for valid contexts."
  843. :group 'org-reveal-location
  844. :type org-context-choice)
  845. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  846. "Non-nil means show all sibling heading when revealing a location.
  847. Org-mode often shows locations in an org-mode file which might have
  848. been invisible before. When this is set, the sibling of the current entry
  849. heading are all made visible. If `org-show-hierarchy-above' is t,
  850. the same happens on each level of the hierarchy above the current entry.
  851. By default this is on for the isearch context, off for all other contexts.
  852. Turning this off for example for sparse trees makes them very compact,
  853. but makes it harder to edit the location of the match. In such a case,
  854. use the command \\[org-reveal] to show more context.
  855. Instead of t, this can also be an alist specifying this option for different
  856. contexts. See `org-show-hierarchy-above' for valid contexts."
  857. :group 'org-reveal-location
  858. :type org-context-choice
  859. :version "24.4"
  860. :package-version '(Org . "8.0"))
  861. (defcustom org-show-entry-below '((default . nil))
  862. "Non-nil means show the entry below a headline when revealing a location.
  863. Org-mode often shows locations in an org-mode file which might have
  864. been invisible before. When this is set, the text below the headline that is
  865. exposed is also shown.
  866. By default this is off for all contexts.
  867. Instead of t, this can also be an alist specifying this option for different
  868. contexts. See `org-show-hierarchy-above' for valid contexts."
  869. :group 'org-reveal-location
  870. :type org-context-choice)
  871. (defcustom org-indirect-buffer-display 'other-window
  872. "How should indirect tree buffers be displayed?
  873. This applies to indirect buffers created with the commands
  874. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  875. Valid values are:
  876. current-window Display in the current window
  877. other-window Just display in another window.
  878. dedicated-frame Create one new frame, and re-use it each time.
  879. new-frame Make a new frame each time. Note that in this case
  880. previously-made indirect buffers are kept, and you need to
  881. kill these buffers yourself."
  882. :group 'org-structure
  883. :group 'org-agenda-windows
  884. :type '(choice
  885. (const :tag "In current window" current-window)
  886. (const :tag "In current frame, other window" other-window)
  887. (const :tag "Each time a new frame" new-frame)
  888. (const :tag "One dedicated frame" dedicated-frame)))
  889. (defcustom org-use-speed-commands nil
  890. "Non-nil means activate single letter commands at beginning of a headline.
  891. This may also be a function to test for appropriate locations where speed
  892. commands should be active."
  893. :group 'org-structure
  894. :type '(choice
  895. (const :tag "Never" nil)
  896. (const :tag "At beginning of headline stars" t)
  897. (function)))
  898. (defcustom org-speed-commands-user nil
  899. "Alist of additional speed commands.
  900. This list will be checked before `org-speed-commands-default'
  901. when the variable `org-use-speed-commands' is non-nil
  902. and when the cursor is at the beginning of a headline.
  903. The car if each entry is a string with a single letter, which must
  904. be assigned to `self-insert-command' in the global map.
  905. The cdr is either a command to be called interactively, a function
  906. to be called, or a form to be evaluated.
  907. An entry that is just a list with a single string will be interpreted
  908. as a descriptive headline that will be added when listing the speed
  909. commands in the Help buffer using the `?' speed command."
  910. :group 'org-structure
  911. :type '(repeat :value ("k" . ignore)
  912. (choice :value ("k" . ignore)
  913. (list :tag "Descriptive Headline" (string :tag "Headline"))
  914. (cons :tag "Letter and Command"
  915. (string :tag "Command letter")
  916. (choice
  917. (function)
  918. (sexp))))))
  919. (defgroup org-cycle nil
  920. "Options concerning visibility cycling in Org-mode."
  921. :tag "Org Cycle"
  922. :group 'org-structure)
  923. (defcustom org-cycle-skip-children-state-if-no-children t
  924. "Non-nil means skip CHILDREN state in entries that don't have any."
  925. :group 'org-cycle
  926. :type 'boolean)
  927. (defcustom org-cycle-max-level nil
  928. "Maximum level which should still be subject to visibility cycling.
  929. Levels higher than this will, for cycling, be treated as text, not a headline.
  930. When `org-odd-levels-only' is set, a value of N in this variable actually
  931. means 2N-1 stars as the limiting headline.
  932. When nil, cycle all levels.
  933. Note that the limiting level of cycling is also influenced by
  934. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  935. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  936. than its value."
  937. :group 'org-cycle
  938. :type '(choice
  939. (const :tag "No limit" nil)
  940. (integer :tag "Maximum level")))
  941. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  942. "Names of drawers. Drawers are not opened by cycling on the headline above.
  943. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  944. this:
  945. :DRAWERNAME:
  946. .....
  947. :END:
  948. The drawer \"PROPERTIES\" is special for capturing properties through
  949. the property API.
  950. Drawers can be defined on the per-file basis with a line like:
  951. #+DRAWERS: HIDDEN STATE PROPERTIES"
  952. :group 'org-structure
  953. :group 'org-cycle
  954. :type '(repeat (string :tag "Drawer Name")))
  955. (defcustom org-hide-block-startup nil
  956. "Non-nil means entering Org-mode will fold all blocks.
  957. This can also be set in on a per-file basis with
  958. #+STARTUP: hideblocks
  959. #+STARTUP: showblocks"
  960. :group 'org-startup
  961. :group 'org-cycle
  962. :type 'boolean)
  963. (defcustom org-cycle-global-at-bob nil
  964. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  965. This makes it possible to do global cycling without having to use S-TAB or
  966. \\[universal-argument] TAB. For this special case to work, the first line
  967. of the buffer must not be a headline -- it may be empty or some other text.
  968. When used in this way, `org-cycle-hook' is disabled temporarily to make
  969. sure the cursor stays at the beginning of the buffer. When this option is
  970. nil, don't do anything special at the beginning of the buffer."
  971. :group 'org-cycle
  972. :type 'boolean)
  973. (defcustom org-cycle-level-after-item/entry-creation t
  974. "Non-nil means cycle entry level or item indentation in new empty entries.
  975. When the cursor is at the end of an empty headline, i.e., with only stars
  976. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  977. and then all possible ancestor states, before returning to the original state.
  978. This makes data entry extremely fast: M-RET to create a new headline,
  979. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  980. When the cursor is at the end of an empty plain list item, one TAB will
  981. make it a subitem, two or more tabs will back up to make this an item
  982. higher up in the item hierarchy."
  983. :group 'org-cycle
  984. :type 'boolean)
  985. (defcustom org-cycle-emulate-tab t
  986. "Where should `org-cycle' emulate TAB.
  987. nil Never
  988. white Only in completely white lines
  989. whitestart Only at the beginning of lines, before the first non-white char
  990. t Everywhere except in headlines
  991. exc-hl-bol Everywhere except at the start of a headline
  992. If TAB is used in a place where it does not emulate TAB, the current subtree
  993. visibility is cycled."
  994. :group 'org-cycle
  995. :type '(choice (const :tag "Never" nil)
  996. (const :tag "Only in completely white lines" white)
  997. (const :tag "Before first char in a line" whitestart)
  998. (const :tag "Everywhere except in headlines" t)
  999. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  1000. ))
  1001. (defcustom org-cycle-separator-lines 2
  1002. "Number of empty lines needed to keep an empty line between collapsed trees.
  1003. If you leave an empty line between the end of a subtree and the following
  1004. headline, this empty line is hidden when the subtree is folded.
  1005. Org-mode will leave (exactly) one empty line visible if the number of
  1006. empty lines is equal or larger to the number given in this variable.
  1007. So the default 2 means at least 2 empty lines after the end of a subtree
  1008. are needed to produce free space between a collapsed subtree and the
  1009. following headline.
  1010. If the number is negative, and the number of empty lines is at least -N,
  1011. all empty lines are shown.
  1012. Special case: when 0, never leave empty lines in collapsed view."
  1013. :group 'org-cycle
  1014. :type 'integer)
  1015. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1016. (defcustom org-pre-cycle-hook nil
  1017. "Hook that is run before visibility cycling is happening.
  1018. The function(s) in this hook must accept a single argument which indicates
  1019. the new state that will be set right after running this hook. The
  1020. argument is a symbol. Before a global state change, it can have the values
  1021. `overview', `content', or `all'. Before a local state change, it can have
  1022. the values `folded', `children', or `subtree'."
  1023. :group 'org-cycle
  1024. :type 'hook)
  1025. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1026. org-cycle-hide-drawers
  1027. org-cycle-hide-inline-tasks
  1028. org-cycle-show-empty-lines
  1029. org-optimize-window-after-visibility-change)
  1030. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1031. The function(s) in this hook must accept a single argument which indicates
  1032. the new state that was set by the most recent `org-cycle' command. The
  1033. argument is a symbol. After a global state change, it can have the values
  1034. `overview', `contents', or `all'. After a local state change, it can have
  1035. the values `folded', `children', or `subtree'."
  1036. :group 'org-cycle
  1037. :type 'hook)
  1038. (defgroup org-edit-structure nil
  1039. "Options concerning structure editing in Org-mode."
  1040. :tag "Org Edit Structure"
  1041. :group 'org-structure)
  1042. (defcustom org-odd-levels-only nil
  1043. "Non-nil means skip even levels and only use odd levels for the outline.
  1044. This has the effect that two stars are being added/taken away in
  1045. promotion/demotion commands. It also influences how levels are
  1046. handled by the exporters.
  1047. Changing it requires restart of `font-lock-mode' to become effective
  1048. for fontification also in regions already fontified.
  1049. You may also set this on a per-file basis by adding one of the following
  1050. lines to the buffer:
  1051. #+STARTUP: odd
  1052. #+STARTUP: oddeven"
  1053. :group 'org-edit-structure
  1054. :group 'org-appearance
  1055. :type 'boolean)
  1056. (defcustom org-adapt-indentation t
  1057. "Non-nil means adapt indentation to outline node level.
  1058. When this variable is set, Org assumes that you write outlines by
  1059. indenting text in each node to align with the headline (after the stars).
  1060. The following issues are influenced by this variable:
  1061. - When this is set and the *entire* text in an entry is indented, the
  1062. indentation is increased by one space in a demotion command, and
  1063. decreased by one in a promotion command. If any line in the entry
  1064. body starts with text at column 0, indentation is not changed at all.
  1065. - Property drawers and planning information is inserted indented when
  1066. this variable s set. When nil, they will not be indented.
  1067. - TAB indents a line relative to context. The lines below a headline
  1068. will be indented when this variable is set.
  1069. Note that this is all about true indentation, by adding and removing
  1070. space characters. See also `org-indent.el' which does level-dependent
  1071. indentation in a virtual way, i.e. at display time in Emacs."
  1072. :group 'org-edit-structure
  1073. :type 'boolean)
  1074. (defcustom org-special-ctrl-a/e nil
  1075. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1076. When t, `C-a' will bring back the cursor to the beginning of the
  1077. headline text, i.e. after the stars and after a possible TODO
  1078. keyword. In an item, this will be the position after bullet and
  1079. check-box, if any. When the cursor is already at that position,
  1080. another `C-a' will bring it to the beginning of the line.
  1081. `C-e' will jump to the end of the headline, ignoring the presence
  1082. of tags in the headline. A second `C-e' will then jump to the
  1083. true end of the line, after any tags. This also means that, when
  1084. this variable is non-nil, `C-e' also will never jump beyond the
  1085. end of the heading of a folded section, i.e. not after the
  1086. ellipses.
  1087. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1088. normally, going to the true line boundary first. Only a directly
  1089. following, identical keypress will bring the cursor to the
  1090. special positions.
  1091. This may also be a cons cell where the behavior for `C-a' and
  1092. `C-e' is set separately."
  1093. :group 'org-edit-structure
  1094. :type '(choice
  1095. (const :tag "off" nil)
  1096. (const :tag "on: after stars/bullet and before tags first" t)
  1097. (const :tag "reversed: true line boundary first" reversed)
  1098. (cons :tag "Set C-a and C-e separately"
  1099. (choice :tag "Special C-a"
  1100. (const :tag "off" nil)
  1101. (const :tag "on: after stars/bullet first" t)
  1102. (const :tag "reversed: before stars/bullet first" reversed))
  1103. (choice :tag "Special C-e"
  1104. (const :tag "off" nil)
  1105. (const :tag "on: before tags first" t)
  1106. (const :tag "reversed: after tags first" reversed)))))
  1107. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1108. (defcustom org-special-ctrl-k nil
  1109. "Non-nil means `C-k' will behave specially in headlines.
  1110. When nil, `C-k' will call the default `kill-line' command.
  1111. When t, the following will happen while the cursor is in the headline:
  1112. - When the cursor is at the beginning of a headline, kill the entire
  1113. line and possible the folded subtree below the line.
  1114. - When in the middle of the headline text, kill the headline up to the tags.
  1115. - When after the headline text, kill the tags."
  1116. :group 'org-edit-structure
  1117. :type 'boolean)
  1118. (defcustom org-ctrl-k-protect-subtree nil
  1119. "Non-nil means, do not delete a hidden subtree with C-k.
  1120. When set to the symbol `error', simply throw an error when C-k is
  1121. used to kill (part-of) a headline that has hidden text behind it.
  1122. Any other non-nil value will result in a query to the user, if it is
  1123. OK to kill that hidden subtree. When nil, kill without remorse."
  1124. :group 'org-edit-structure
  1125. :version "24.1"
  1126. :type '(choice
  1127. (const :tag "Do not protect hidden subtrees" nil)
  1128. (const :tag "Protect hidden subtrees with a security query" t)
  1129. (const :tag "Never kill a hidden subtree with C-k" error)))
  1130. (defcustom org-catch-invisible-edits nil
  1131. "Check if in invisible region before inserting or deleting a character.
  1132. Valid values are:
  1133. nil Do not check, so just do invisible edits.
  1134. error Throw an error and do nothing.
  1135. show Make point visible, and do the requested edit.
  1136. show-and-error Make point visible, then throw an error and abort the edit.
  1137. smart Make point visible, and do insertion/deletion if it is
  1138. adjacent to visible text and the change feels predictable.
  1139. Never delete a previously invisible character or add in the
  1140. middle or right after an invisible region. Basically, this
  1141. allows insertion and backward-delete right before ellipses.
  1142. FIXME: maybe in this case we should not even show?"
  1143. :group 'org-edit-structure
  1144. :version "24.1"
  1145. :type '(choice
  1146. (const :tag "Do not check" nil)
  1147. (const :tag "Throw error when trying to edit" error)
  1148. (const :tag "Unhide, but do not do the edit" show-and-error)
  1149. (const :tag "Show invisible part and do the edit" show)
  1150. (const :tag "Be smart and do the right thing" smart)))
  1151. (defcustom org-yank-folded-subtrees t
  1152. "Non-nil means when yanking subtrees, fold them.
  1153. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1154. it starts with a heading and all other headings in it are either children
  1155. or siblings, then fold all the subtrees. However, do this only if no
  1156. text after the yank would be swallowed into a folded tree by this action."
  1157. :group 'org-edit-structure
  1158. :type 'boolean)
  1159. (defcustom org-yank-adjusted-subtrees nil
  1160. "Non-nil means when yanking subtrees, adjust the level.
  1161. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1162. this function for details."
  1163. :group 'org-edit-structure
  1164. :type 'boolean)
  1165. (defcustom org-M-RET-may-split-line '((default . t))
  1166. "Non-nil means M-RET will split the line at the cursor position.
  1167. When nil, it will go to the end of the line before making a
  1168. new line.
  1169. You may also set this option in a different way for different
  1170. contexts. Valid contexts are:
  1171. headline when creating a new headline
  1172. item when creating a new item
  1173. table in a table field
  1174. default the value to be used for all contexts not explicitly
  1175. customized"
  1176. :group 'org-structure
  1177. :group 'org-table
  1178. :type '(choice
  1179. (const :tag "Always" t)
  1180. (const :tag "Never" nil)
  1181. (repeat :greedy t :tag "Individual contexts"
  1182. (cons
  1183. (choice :tag "Context"
  1184. (const headline)
  1185. (const item)
  1186. (const table)
  1187. (const default))
  1188. (boolean)))))
  1189. (defcustom org-insert-heading-respect-content nil
  1190. "Non-nil means insert new headings after the current subtree.
  1191. When nil, the new heading is created directly after the current line.
  1192. The commands \\[org-insert-heading-respect-content] and
  1193. \\[org-insert-todo-heading-respect-content] turn this variable on
  1194. for the duration of the command."
  1195. :group 'org-structure
  1196. :type 'boolean)
  1197. (defcustom org-blank-before-new-entry '((heading . auto)
  1198. (plain-list-item . auto))
  1199. "Should `org-insert-heading' leave a blank line before new heading/item?
  1200. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1201. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1202. which case Org will look at the surrounding headings/items and try to
  1203. make an intelligent decision whether to insert a blank line or not.
  1204. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1205. the setting here is ignored and no empty line is inserted to avoid breaking
  1206. the list structure."
  1207. :group 'org-edit-structure
  1208. :type '(list
  1209. (cons (const heading)
  1210. (choice (const :tag "Never" nil)
  1211. (const :tag "Always" t)
  1212. (const :tag "Auto" auto)))
  1213. (cons (const plain-list-item)
  1214. (choice (const :tag "Never" nil)
  1215. (const :tag "Always" t)
  1216. (const :tag "Auto" auto)))))
  1217. (defcustom org-insert-heading-hook nil
  1218. "Hook being run after inserting a new heading."
  1219. :group 'org-edit-structure
  1220. :type 'hook)
  1221. (defcustom org-enable-fixed-width-editor t
  1222. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1223. This currently only means they are never auto-wrapped.
  1224. When nil, such lines will be treated like ordinary lines.
  1225. See also the QUOTE keyword."
  1226. :group 'org-edit-structure
  1227. :type 'boolean)
  1228. (defcustom org-goto-auto-isearch t
  1229. "Non-nil means typing characters in `org-goto' starts incremental search.
  1230. When nil, you can use these keybindings to navigate the buffer:
  1231. q Quit the org-goto interface
  1232. n Go to the next visible heading
  1233. p Go to the previous visible heading
  1234. f Go one heading forward on same level
  1235. b Go one heading backward on same level
  1236. u Go one heading up"
  1237. :group 'org-edit-structure
  1238. :type 'boolean)
  1239. (defgroup org-sparse-trees nil
  1240. "Options concerning sparse trees in Org-mode."
  1241. :tag "Org Sparse Trees"
  1242. :group 'org-structure)
  1243. (defcustom org-highlight-sparse-tree-matches t
  1244. "Non-nil means highlight all matches that define a sparse tree.
  1245. The highlights will automatically disappear the next time the buffer is
  1246. changed by an edit command."
  1247. :group 'org-sparse-trees
  1248. :type 'boolean)
  1249. (defcustom org-remove-highlights-with-change t
  1250. "Non-nil means any change to the buffer will remove temporary highlights.
  1251. Such highlights are created by `org-occur' and `org-clock-display'.
  1252. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1253. The highlights created by `org-preview-latex-fragment' always need
  1254. `C-c C-c' to be removed."
  1255. :group 'org-sparse-trees
  1256. :group 'org-time
  1257. :type 'boolean)
  1258. (defcustom org-occur-hook '(org-first-headline-recenter)
  1259. "Hook that is run after `org-occur' has constructed a sparse tree.
  1260. This can be used to recenter the window to show as much of the structure
  1261. as possible."
  1262. :group 'org-sparse-trees
  1263. :type 'hook)
  1264. (defgroup org-imenu-and-speedbar nil
  1265. "Options concerning imenu and speedbar in Org-mode."
  1266. :tag "Org Imenu and Speedbar"
  1267. :group 'org-structure)
  1268. (defcustom org-imenu-depth 2
  1269. "The maximum level for Imenu access to Org-mode headlines.
  1270. This also applied for speedbar access."
  1271. :group 'org-imenu-and-speedbar
  1272. :type 'integer)
  1273. (defgroup org-table nil
  1274. "Options concerning tables in Org-mode."
  1275. :tag "Org Table"
  1276. :group 'org)
  1277. (defcustom org-enable-table-editor 'optimized
  1278. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1279. When nil, such lines will be treated like ordinary lines.
  1280. When equal to the symbol `optimized', the table editor will be optimized to
  1281. do the following:
  1282. - Automatic overwrite mode in front of whitespace in table fields.
  1283. This makes the structure of the table stay in tact as long as the edited
  1284. field does not exceed the column width.
  1285. - Minimize the number of realigns. Normally, the table is aligned each time
  1286. TAB or RET are pressed to move to another field. With optimization this
  1287. happens only if changes to a field might have changed the column width.
  1288. Optimization requires replacing the functions `self-insert-command',
  1289. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1290. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1291. very good at guessing when a re-align will be necessary, but you can always
  1292. force one with \\[org-ctrl-c-ctrl-c].
  1293. If you would like to use the optimized version in Org-mode, but the
  1294. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1295. This variable can be used to turn on and off the table editor during a session,
  1296. but in order to toggle optimization, a restart is required.
  1297. See also the variable `org-table-auto-blank-field'."
  1298. :group 'org-table
  1299. :type '(choice
  1300. (const :tag "off" nil)
  1301. (const :tag "on" t)
  1302. (const :tag "on, optimized" optimized)))
  1303. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1304. (version<= emacs-version "24.1"))
  1305. "Non-nil means cluster self-insert commands for undo when possible.
  1306. If this is set, then, like in the Emacs command loop, 20 consecutive
  1307. characters will be undone together.
  1308. This is configurable, because there is some impact on typing performance."
  1309. :group 'org-table
  1310. :type 'boolean)
  1311. (defcustom org-table-tab-recognizes-table.el t
  1312. "Non-nil means TAB will automatically notice a table.el table.
  1313. When it sees such a table, it moves point into it and - if necessary -
  1314. calls `table-recognize-table'."
  1315. :group 'org-table-editing
  1316. :type 'boolean)
  1317. (defgroup org-link nil
  1318. "Options concerning links in Org-mode."
  1319. :tag "Org Link"
  1320. :group 'org)
  1321. (defvar org-link-abbrev-alist-local nil
  1322. "Buffer-local version of `org-link-abbrev-alist', which see.
  1323. The value of this is taken from the #+LINK lines.")
  1324. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1325. (defcustom org-link-abbrev-alist nil
  1326. "Alist of link abbreviations.
  1327. The car of each element is a string, to be replaced at the start of a link.
  1328. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1329. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1330. [[linkkey:tag][description]]
  1331. The 'linkkey' must be a word word, starting with a letter, followed
  1332. by letters, numbers, '-' or '_'.
  1333. If REPLACE is a string, the tag will simply be appended to create the link.
  1334. If the string contains \"%s\", the tag will be inserted there. If the string
  1335. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1336. at that point (see the function `url-hexify-string'). If the string contains
  1337. the specifier \"%(my-function)\", then the custom function `my-function' will
  1338. be invoked: this function takes the tag as its only argument and must return
  1339. a string.
  1340. REPLACE may also be a function that will be called with the tag as the
  1341. only argument to create the link, which should be returned as a string.
  1342. See the manual for examples."
  1343. :group 'org-link
  1344. :type '(repeat
  1345. (cons
  1346. (string :tag "Protocol")
  1347. (choice
  1348. (string :tag "Format")
  1349. (function)))))
  1350. (defcustom org-descriptive-links t
  1351. "Non-nil means Org will display descriptive links.
  1352. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1353. \"Org Website\", hiding the link itself and just displaying its
  1354. description. When set to `nil', Org will display the full links
  1355. literally.
  1356. You can interactively set the value of this variable by calling
  1357. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1358. :group 'org-link
  1359. :type 'boolean)
  1360. (defcustom org-link-file-path-type 'adaptive
  1361. "How the path name in file links should be stored.
  1362. Valid values are:
  1363. relative Relative to the current directory, i.e. the directory of the file
  1364. into which the link is being inserted.
  1365. absolute Absolute path, if possible with ~ for home directory.
  1366. noabbrev Absolute path, no abbreviation of home directory.
  1367. adaptive Use relative path for files in the current directory and sub-
  1368. directories of it. For other files, use an absolute path."
  1369. :group 'org-link
  1370. :type '(choice
  1371. (const relative)
  1372. (const absolute)
  1373. (const noabbrev)
  1374. (const adaptive)))
  1375. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1376. "Types of links that should be activated in Org-mode files.
  1377. This is a list of symbols, each leading to the activation of a certain link
  1378. type. In principle, it does not hurt to turn on most link types - there may
  1379. be a small gain when turning off unused link types. The types are:
  1380. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1381. angle Links in angular brackets that may contain whitespace like
  1382. <bbdb:Carsten Dominik>.
  1383. plain Plain links in normal text, no whitespace, like http://google.com.
  1384. radio Text that is matched by a radio target, see manual for details.
  1385. tag Tag settings in a headline (link to tag search).
  1386. date Time stamps (link to calendar).
  1387. footnote Footnote labels.
  1388. Changing this variable requires a restart of Emacs to become effective."
  1389. :group 'org-link
  1390. :type '(set :greedy t
  1391. (const :tag "Double bracket links" bracket)
  1392. (const :tag "Angular bracket links" angle)
  1393. (const :tag "Plain text links" plain)
  1394. (const :tag "Radio target matches" radio)
  1395. (const :tag "Tags" tag)
  1396. (const :tag "Timestamps" date)
  1397. (const :tag "Footnotes" footnote)))
  1398. (defcustom org-make-link-description-function nil
  1399. "Function to use for generating link descriptions from links.
  1400. When nil, the link location will be used. This function must take
  1401. two parameters: the first one is the link, the second one is the
  1402. description generated by `org-insert-link'. The function should
  1403. return the description to use."
  1404. :group 'org-link
  1405. :type 'function)
  1406. (defgroup org-link-store nil
  1407. "Options concerning storing links in Org-mode."
  1408. :tag "Org Store Link"
  1409. :group 'org-link)
  1410. (defcustom org-url-hexify-p t
  1411. "When non-nil, hexify URL when creating a link."
  1412. :type 'boolean
  1413. :version "24.3"
  1414. :group 'org-link-store)
  1415. (defcustom org-email-link-description-format "Email %c: %.30s"
  1416. "Format of the description part of a link to an email or usenet message.
  1417. The following %-escapes will be replaced by corresponding information:
  1418. %F full \"From\" field
  1419. %f name, taken from \"From\" field, address if no name
  1420. %T full \"To\" field
  1421. %t first name in \"To\" field, address if no name
  1422. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1423. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1424. %s subject
  1425. %d date
  1426. %m message-id.
  1427. You may use normal field width specification between the % and the letter.
  1428. This is for example useful to limit the length of the subject.
  1429. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1430. :group 'org-link-store
  1431. :type 'string)
  1432. (defcustom org-from-is-user-regexp
  1433. (let (r1 r2)
  1434. (when (and user-mail-address (not (string= user-mail-address "")))
  1435. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1436. (when (and user-full-name (not (string= user-full-name "")))
  1437. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1438. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1439. "Regexp matched against the \"From:\" header of an email or usenet message.
  1440. It should match if the message is from the user him/herself."
  1441. :group 'org-link-store
  1442. :type 'regexp)
  1443. (defcustom org-context-in-file-links t
  1444. "Non-nil means file links from `org-store-link' contain context.
  1445. A search string will be added to the file name with :: as separator and
  1446. used to find the context when the link is activated by the command
  1447. `org-open-at-point'. When this option is t, the entire active region
  1448. will be placed in the search string of the file link. If set to a
  1449. positive integer, only the first n lines of context will be stored.
  1450. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1451. negates this setting for the duration of the command."
  1452. :group 'org-link-store
  1453. :type '(choice boolean integer))
  1454. (defcustom org-keep-stored-link-after-insertion nil
  1455. "Non-nil means keep link in list for entire session.
  1456. The command `org-store-link' adds a link pointing to the current
  1457. location to an internal list. These links accumulate during a session.
  1458. The command `org-insert-link' can be used to insert links into any
  1459. Org-mode file (offering completion for all stored links). When this
  1460. option is nil, every link which has been inserted once using \\[org-insert-link]
  1461. will be removed from the list, to make completing the unused links
  1462. more efficient."
  1463. :group 'org-link-store
  1464. :type 'boolean)
  1465. (defgroup org-link-follow nil
  1466. "Options concerning following links in Org-mode."
  1467. :tag "Org Follow Link"
  1468. :group 'org-link)
  1469. (defcustom org-link-translation-function nil
  1470. "Function to translate links with different syntax to Org syntax.
  1471. This can be used to translate links created for example by the Planner
  1472. or emacs-wiki packages to Org syntax.
  1473. The function must accept two parameters, a TYPE containing the link
  1474. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1475. which is everything after the link protocol. It should return a cons
  1476. with possibly modified values of type and path.
  1477. Org contains a function for this, so if you set this variable to
  1478. `org-translate-link-from-planner', you should be able follow many
  1479. links created by planner."
  1480. :group 'org-link-follow
  1481. :type 'function)
  1482. (defcustom org-follow-link-hook nil
  1483. "Hook that is run after a link has been followed."
  1484. :group 'org-link-follow
  1485. :type 'hook)
  1486. (defcustom org-tab-follows-link nil
  1487. "Non-nil means on links TAB will follow the link.
  1488. Needs to be set before org.el is loaded.
  1489. This really should not be used, it does not make sense, and the
  1490. implementation is bad."
  1491. :group 'org-link-follow
  1492. :type 'boolean)
  1493. (defcustom org-return-follows-link nil
  1494. "Non-nil means on links RET will follow the link.
  1495. In tables, the special behavior of RET has precedence."
  1496. :group 'org-link-follow
  1497. :type 'boolean)
  1498. (defcustom org-mouse-1-follows-link
  1499. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1500. "Non-nil means mouse-1 on a link will follow the link.
  1501. A longer mouse click will still set point. Does not work on XEmacs.
  1502. Needs to be set before org.el is loaded."
  1503. :group 'org-link-follow
  1504. :type 'boolean)
  1505. (defcustom org-mark-ring-length 4
  1506. "Number of different positions to be recorded in the ring.
  1507. Changing this requires a restart of Emacs to work correctly."
  1508. :group 'org-link-follow
  1509. :type 'integer)
  1510. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1511. "Non-nil means internal links in Org files must exactly match a headline.
  1512. When nil, the link search tries to match a phrase with all words
  1513. in the search text."
  1514. :group 'org-link-follow
  1515. :version "24.1"
  1516. :type '(choice
  1517. (const :tag "Use fuzzy text search" nil)
  1518. (const :tag "Match only exact headline" t)
  1519. (const :tag "Match exact headline or query to create it"
  1520. query-to-create)))
  1521. (defcustom org-link-frame-setup
  1522. '((vm . vm-visit-folder-other-frame)
  1523. (vm-imap . vm-visit-imap-folder-other-frame)
  1524. (gnus . org-gnus-no-new-news)
  1525. (file . find-file-other-window)
  1526. (wl . wl-other-frame))
  1527. "Setup the frame configuration for following links.
  1528. When following a link with Emacs, it may often be useful to display
  1529. this link in another window or frame. This variable can be used to
  1530. set this up for the different types of links.
  1531. For VM, use any of
  1532. `vm-visit-folder'
  1533. `vm-visit-folder-other-window'
  1534. `vm-visit-folder-other-frame'
  1535. For Gnus, use any of
  1536. `gnus'
  1537. `gnus-other-frame'
  1538. `org-gnus-no-new-news'
  1539. For FILE, use any of
  1540. `find-file'
  1541. `find-file-other-window'
  1542. `find-file-other-frame'
  1543. For Wanderlust use any of
  1544. `wl'
  1545. `wl-other-frame'
  1546. For the calendar, use the variable `calendar-setup'.
  1547. For BBDB, it is currently only possible to display the matches in
  1548. another window."
  1549. :group 'org-link-follow
  1550. :type '(list
  1551. (cons (const vm)
  1552. (choice
  1553. (const vm-visit-folder)
  1554. (const vm-visit-folder-other-window)
  1555. (const vm-visit-folder-other-frame)))
  1556. (cons (const gnus)
  1557. (choice
  1558. (const gnus)
  1559. (const gnus-other-frame)
  1560. (const org-gnus-no-new-news)))
  1561. (cons (const file)
  1562. (choice
  1563. (const find-file)
  1564. (const find-file-other-window)
  1565. (const find-file-other-frame)))
  1566. (cons (const wl)
  1567. (choice
  1568. (const wl)
  1569. (const wl-other-frame)))))
  1570. (defcustom org-display-internal-link-with-indirect-buffer nil
  1571. "Non-nil means use indirect buffer to display infile links.
  1572. Activating internal links (from one location in a file to another location
  1573. in the same file) normally just jumps to the location. When the link is
  1574. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1575. is displayed in
  1576. another window. When this option is set, the other window actually displays
  1577. an indirect buffer clone of the current buffer, to avoid any visibility
  1578. changes to the current buffer."
  1579. :group 'org-link-follow
  1580. :type 'boolean)
  1581. (defcustom org-open-non-existing-files nil
  1582. "Non-nil means `org-open-file' will open non-existing files.
  1583. When nil, an error will be generated.
  1584. This variable applies only to external applications because they
  1585. might choke on non-existing files. If the link is to a file that
  1586. will be opened in Emacs, the variable is ignored."
  1587. :group 'org-link-follow
  1588. :type 'boolean)
  1589. (defcustom org-open-directory-means-index-dot-org nil
  1590. "Non-nil means a link to a directory really means to index.org.
  1591. When nil, following a directory link will run dired or open a finder/explorer
  1592. window on that directory."
  1593. :group 'org-link-follow
  1594. :type 'boolean)
  1595. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1596. "Function and arguments to call for following mailto links.
  1597. This is a list with the first element being a Lisp function, and the
  1598. remaining elements being arguments to the function. In string arguments,
  1599. %a will be replaced by the address, and %s will be replaced by the subject
  1600. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1601. :group 'org-link-follow
  1602. :type '(choice
  1603. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1604. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1605. (const :tag "message-mail" (message-mail "%a" "%s"))
  1606. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1607. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1608. "Non-nil means ask for confirmation before executing shell links.
  1609. Shell links can be dangerous: just think about a link
  1610. [[shell:rm -rf ~/*][Google Search]]
  1611. This link would show up in your Org-mode document as \"Google Search\",
  1612. but really it would remove your entire home directory.
  1613. Therefore we advise against setting this variable to nil.
  1614. Just change it to `y-or-n-p' if you want to confirm with a
  1615. single keystroke rather than having to type \"yes\"."
  1616. :group 'org-link-follow
  1617. :type '(choice
  1618. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1619. (const :tag "with y-or-n (faster)" y-or-n-p)
  1620. (const :tag "no confirmation (dangerous)" nil)))
  1621. (put 'org-confirm-shell-link-function
  1622. 'safe-local-variable
  1623. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1624. (defcustom org-confirm-shell-link-not-regexp ""
  1625. "A regexp to skip confirmation for shell links."
  1626. :group 'org-link-follow
  1627. :version "24.1"
  1628. :type 'regexp)
  1629. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1630. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1631. Elisp links can be dangerous: just think about a link
  1632. [[elisp:(shell-command \"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-elisp-link-not-regexp ""
  1647. "A regexp to skip confirmation for Elisp links."
  1648. :group 'org-link-follow
  1649. :version "24.1"
  1650. :type 'regexp)
  1651. (defconst org-file-apps-defaults-gnu
  1652. '((remote . emacs)
  1653. (system . mailcap)
  1654. (t . mailcap))
  1655. "Default file applications on a UNIX or GNU/Linux system.
  1656. See `org-file-apps'.")
  1657. (defconst org-file-apps-defaults-macosx
  1658. '((remote . emacs)
  1659. (t . "open %s")
  1660. (system . "open %s")
  1661. ("ps.gz" . "gv %s")
  1662. ("eps.gz" . "gv %s")
  1663. ("dvi" . "xdvi %s")
  1664. ("fig" . "xfig %s"))
  1665. "Default file applications on a MacOS X system.
  1666. The system \"open\" is known as a default, but we use X11 applications
  1667. for some files for which the OS does not have a good default.
  1668. See `org-file-apps'.")
  1669. (defconst org-file-apps-defaults-windowsnt
  1670. (list
  1671. '(remote . emacs)
  1672. (cons t
  1673. (list (if (featurep 'xemacs)
  1674. 'mswindows-shell-execute
  1675. 'w32-shell-execute)
  1676. "open" 'file))
  1677. (cons 'system
  1678. (list (if (featurep 'xemacs)
  1679. 'mswindows-shell-execute
  1680. 'w32-shell-execute)
  1681. "open" 'file)))
  1682. "Default file applications on a Windows NT system.
  1683. The system \"open\" is used for most files.
  1684. See `org-file-apps'.")
  1685. (defcustom org-file-apps
  1686. '(
  1687. (auto-mode . emacs)
  1688. ("\\.mm\\'" . default)
  1689. ("\\.x?html?\\'" . default)
  1690. ("\\.pdf\\'" . default)
  1691. )
  1692. "External applications for opening `file:path' items in a document.
  1693. Org-mode uses system defaults for different file types, but
  1694. you can use this variable to set the application for a given file
  1695. extension. The entries in this list are cons cells where the car identifies
  1696. files and the cdr the corresponding command. Possible values for the
  1697. file identifier are
  1698. \"string\" A string as a file identifier can be interpreted in different
  1699. ways, depending on its contents:
  1700. - Alphanumeric characters only:
  1701. Match links with this file extension.
  1702. Example: (\"pdf\" . \"evince %s\")
  1703. to open PDFs with evince.
  1704. - Regular expression: Match links where the
  1705. filename matches the regexp. If you want to
  1706. use groups here, use shy groups.
  1707. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1708. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1709. to open *.html and *.xhtml with firefox.
  1710. - Regular expression which contains (non-shy) groups:
  1711. Match links where the whole link, including \"::\", and
  1712. anything after that, matches the regexp.
  1713. In a custom command string, %1, %2, etc. are replaced with
  1714. the parts of the link that were matched by the groups.
  1715. For backwards compatibility, if a command string is given
  1716. that does not use any of the group matches, this case is
  1717. handled identically to the second one (i.e. match against
  1718. file name only).
  1719. In a custom lisp form, you can access the group matches with
  1720. (match-string n link).
  1721. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1722. to open [[file:document.pdf::5]] with evince at page 5.
  1723. `directory' Matches a directory
  1724. `remote' Matches a remote file, accessible through tramp or efs.
  1725. Remote files most likely should be visited through Emacs
  1726. because external applications cannot handle such paths.
  1727. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1728. so all files Emacs knows how to handle. Using this with
  1729. command `emacs' will open most files in Emacs. Beware that this
  1730. will also open html files inside Emacs, unless you add
  1731. (\"html\" . default) to the list as well.
  1732. t Default for files not matched by any of the other options.
  1733. `system' The system command to open files, like `open' on Windows
  1734. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1735. that will be selected if you call `C-c C-o' with a double
  1736. \\[universal-argument] \\[universal-argument] prefix.
  1737. Possible values for the command are:
  1738. `emacs' The file will be visited by the current Emacs process.
  1739. `default' Use the default application for this file type, which is the
  1740. association for t in the list, most likely in the system-specific
  1741. part.
  1742. This can be used to overrule an unwanted setting in the
  1743. system-specific variable.
  1744. `system' Use the system command for opening files, like \"open\".
  1745. This command is specified by the entry whose car is `system'.
  1746. Most likely, the system-specific version of this variable
  1747. does define this command, but you can overrule/replace it
  1748. here.
  1749. string A command to be executed by a shell; %s will be replaced
  1750. by the path to the file.
  1751. sexp A Lisp form which will be evaluated. The file path will
  1752. be available in the Lisp variable `file'.
  1753. For more examples, see the system specific constants
  1754. `org-file-apps-defaults-macosx'
  1755. `org-file-apps-defaults-windowsnt'
  1756. `org-file-apps-defaults-gnu'."
  1757. :group 'org-link-follow
  1758. :type '(repeat
  1759. (cons (choice :value ""
  1760. (string :tag "Extension")
  1761. (const :tag "System command to open files" system)
  1762. (const :tag "Default for unrecognized files" t)
  1763. (const :tag "Remote file" remote)
  1764. (const :tag "Links to a directory" directory)
  1765. (const :tag "Any files that have Emacs modes"
  1766. auto-mode))
  1767. (choice :value ""
  1768. (const :tag "Visit with Emacs" emacs)
  1769. (const :tag "Use default" default)
  1770. (const :tag "Use the system command" system)
  1771. (string :tag "Command")
  1772. (sexp :tag "Lisp form")))))
  1773. (defcustom org-doi-server-url "http://dx.doi.org/"
  1774. "The URL of the DOI server."
  1775. :type 'string
  1776. :version "24.3"
  1777. :group 'org-link-follow)
  1778. (defgroup org-refile nil
  1779. "Options concerning refiling entries in Org-mode."
  1780. :tag "Org Refile"
  1781. :group 'org)
  1782. (defcustom org-directory "~/org"
  1783. "Directory with org files.
  1784. This is just a default location to look for Org files. There is no need
  1785. at all to put your files into this directory. It is only used in the
  1786. following situations:
  1787. 1. When a capture template specifies a target file that is not an
  1788. absolute path. The path will then be interpreted relative to
  1789. `org-directory'
  1790. 2. When a capture note is filed away in an interactive way (when exiting the
  1791. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1792. with `org-directory' as the default path."
  1793. :group 'org-refile
  1794. :group 'org-remember
  1795. :group 'org-capture
  1796. :type 'directory)
  1797. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1798. "Default target for storing notes.
  1799. Used as a fall back file for org-remember.el and org-capture.el, for
  1800. templates that do not specify a target file."
  1801. :group 'org-refile
  1802. :group 'org-remember
  1803. :group 'org-capture
  1804. :type '(choice
  1805. (const :tag "Default from remember-data-file" nil)
  1806. file))
  1807. (defcustom org-goto-interface 'outline
  1808. "The default interface to be used for `org-goto'.
  1809. Allowed values are:
  1810. outline The interface shows an outline of the relevant file
  1811. and the correct heading is found by moving through
  1812. the outline or by searching with incremental search.
  1813. outline-path-completion Headlines in the current buffer are offered via
  1814. completion. This is the interface also used by
  1815. the refile command."
  1816. :group 'org-refile
  1817. :type '(choice
  1818. (const :tag "Outline" outline)
  1819. (const :tag "Outline-path-completion" outline-path-completion)))
  1820. (defcustom org-goto-max-level 5
  1821. "Maximum target level when running `org-goto' with refile interface."
  1822. :group 'org-refile
  1823. :type 'integer)
  1824. (defcustom org-reverse-note-order nil
  1825. "Non-nil means store new notes at the beginning of a file or entry.
  1826. When nil, new notes will be filed to the end of a file or entry.
  1827. This can also be a list with cons cells of regular expressions that
  1828. are matched against file names, and values."
  1829. :group 'org-remember
  1830. :group 'org-capture
  1831. :group 'org-refile
  1832. :type '(choice
  1833. (const :tag "Reverse always" t)
  1834. (const :tag "Reverse never" nil)
  1835. (repeat :tag "By file name regexp"
  1836. (cons regexp boolean))))
  1837. (defcustom org-log-refile nil
  1838. "Information to record when a task is refiled.
  1839. Possible values are:
  1840. nil Don't add anything
  1841. time Add a time stamp to the task
  1842. note Prompt for a note and add it with template `org-log-note-headings'
  1843. This option can also be set with on a per-file-basis with
  1844. #+STARTUP: nologrefile
  1845. #+STARTUP: logrefile
  1846. #+STARTUP: lognoterefile
  1847. You can have local logging settings for a subtree by setting the LOGGING
  1848. property to one or more of these keywords.
  1849. When bulk-refiling from the agenda, the value `note' is forbidden and
  1850. will temporarily be changed to `time'."
  1851. :group 'org-refile
  1852. :group 'org-progress
  1853. :version "24.1"
  1854. :type '(choice
  1855. (const :tag "No logging" nil)
  1856. (const :tag "Record timestamp" time)
  1857. (const :tag "Record timestamp with note." note)))
  1858. (defcustom org-refile-targets nil
  1859. "Targets for refiling entries with \\[org-refile].
  1860. This is a list of cons cells. Each cell contains:
  1861. - a specification of the files to be considered, either a list of files,
  1862. or a symbol whose function or variable value will be used to retrieve
  1863. a file name or a list of file names. If you use `org-agenda-files' for
  1864. that, all agenda files will be scanned for targets. Nil means consider
  1865. headings in the current buffer.
  1866. - A specification of how to find candidate refile targets. This may be
  1867. any of:
  1868. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1869. This tag has to be present in all target headlines, inheritance will
  1870. not be considered.
  1871. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1872. todo keyword.
  1873. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1874. headlines that are refiling targets.
  1875. - a cons cell (:level . N). Any headline of level N is considered a target.
  1876. Note that, when `org-odd-levels-only' is set, level corresponds to
  1877. order in hierarchy, not to the number of stars.
  1878. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1879. Note that, when `org-odd-levels-only' is set, level corresponds to
  1880. order in hierarchy, not to the number of stars.
  1881. Each element of this list generates a set of possible targets.
  1882. The union of these sets is presented (with completion) to
  1883. the user by `org-refile'.
  1884. You can set the variable `org-refile-target-verify-function' to a function
  1885. to verify each headline found by the simple criteria above.
  1886. When this variable is nil, all top-level headlines in the current buffer
  1887. are used, equivalent to the value `((nil . (:level . 1))'."
  1888. :group 'org-refile
  1889. :type '(repeat
  1890. (cons
  1891. (choice :value org-agenda-files
  1892. (const :tag "All agenda files" org-agenda-files)
  1893. (const :tag "Current buffer" nil)
  1894. (function) (variable) (file))
  1895. (choice :tag "Identify target headline by"
  1896. (cons :tag "Specific tag" (const :value :tag) (string))
  1897. (cons :tag "TODO keyword" (const :value :todo) (string))
  1898. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1899. (cons :tag "Level number" (const :value :level) (integer))
  1900. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1901. (defcustom org-refile-target-verify-function nil
  1902. "Function to verify if the headline at point should be a refile target.
  1903. The function will be called without arguments, with point at the
  1904. beginning of the headline. It should return t and leave point
  1905. where it is if the headline is a valid target for refiling.
  1906. If the target should not be selected, the function must return nil.
  1907. In addition to this, it may move point to a place from where the search
  1908. should be continued. For example, the function may decide that the entire
  1909. subtree of the current entry should be excluded and move point to the end
  1910. of the subtree."
  1911. :group 'org-refile
  1912. :type 'function)
  1913. (defcustom org-refile-use-cache nil
  1914. "Non-nil means cache refile targets to speed up the process.
  1915. The cache for a particular file will be updated automatically when
  1916. the buffer has been killed, or when any of the marker used for flagging
  1917. refile targets no longer points at a live buffer.
  1918. If you have added new entries to a buffer that might themselves be targets,
  1919. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1920. find that easier, `C-u C-u C-u C-c C-w'."
  1921. :group 'org-refile
  1922. :version "24.1"
  1923. :type 'boolean)
  1924. (defcustom org-refile-use-outline-path nil
  1925. "Non-nil means provide refile targets as paths.
  1926. So a level 3 headline will be available as level1/level2/level3.
  1927. When the value is `file', also include the file name (without directory)
  1928. into the path. In this case, you can also stop the completion after
  1929. the file name, to get entries inserted as top level in the file.
  1930. When `full-file-path', include the full file path."
  1931. :group 'org-refile
  1932. :type '(choice
  1933. (const :tag "Not" nil)
  1934. (const :tag "Yes" t)
  1935. (const :tag "Start with file name" file)
  1936. (const :tag "Start with full file path" full-file-path)))
  1937. (defcustom org-outline-path-complete-in-steps t
  1938. "Non-nil means complete the outline path in hierarchical steps.
  1939. When Org-mode uses the refile interface to select an outline path
  1940. \(see variable `org-refile-use-outline-path'), the completion of
  1941. the path can be done is a single go, or if can be done in steps down
  1942. the headline hierarchy. Going in steps is probably the best if you
  1943. do not use a special completion package like `ido' or `icicles'.
  1944. However, when using these packages, going in one step can be very
  1945. fast, while still showing the whole path to the entry."
  1946. :group 'org-refile
  1947. :type 'boolean)
  1948. (defcustom org-refile-allow-creating-parent-nodes nil
  1949. "Non-nil means allow to create new nodes as refile targets.
  1950. New nodes are then created by adding \"/new node name\" to the completion
  1951. of an existing node. When the value of this variable is `confirm',
  1952. new node creation must be confirmed by the user (recommended)
  1953. When nil, the completion must match an existing entry.
  1954. Note that, if the new heading is not seen by the criteria
  1955. listed in `org-refile-targets', multiple instances of the same
  1956. heading would be created by trying again to file under the new
  1957. heading."
  1958. :group 'org-refile
  1959. :type '(choice
  1960. (const :tag "Never" nil)
  1961. (const :tag "Always" t)
  1962. (const :tag "Prompt for confirmation" confirm)))
  1963. (defcustom org-refile-active-region-within-subtree nil
  1964. "Non-nil means also refile active region within a subtree.
  1965. By default `org-refile' doesn't allow refiling regions if they
  1966. don't contain a set of subtrees, but it might be convenient to
  1967. do so sometimes: in that case, the first line of the region is
  1968. converted to a headline before refiling."
  1969. :group 'org-refile
  1970. :version "24.1"
  1971. :type 'boolean)
  1972. (defgroup org-todo nil
  1973. "Options concerning TODO items in Org-mode."
  1974. :tag "Org TODO"
  1975. :group 'org)
  1976. (defgroup org-progress nil
  1977. "Options concerning Progress logging in Org-mode."
  1978. :tag "Org Progress"
  1979. :group 'org-time)
  1980. (defvar org-todo-interpretation-widgets
  1981. '((:tag "Sequence (cycling hits every state)" sequence)
  1982. (:tag "Type (cycling directly to DONE)" type))
  1983. "The available interpretation symbols for customizing `org-todo-keywords'.
  1984. Interested libraries should add to this list.")
  1985. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1986. "List of TODO entry keyword sequences and their interpretation.
  1987. \\<org-mode-map>This is a list of sequences.
  1988. Each sequence starts with a symbol, either `sequence' or `type',
  1989. indicating if the keywords should be interpreted as a sequence of
  1990. action steps, or as different types of TODO items. The first
  1991. keywords are states requiring action - these states will select a headline
  1992. for inclusion into the global TODO list Org-mode produces. If one of
  1993. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1994. signify that no further action is necessary. If \"|\" is not found,
  1995. the last keyword is treated as the only DONE state of the sequence.
  1996. The command \\[org-todo] cycles an entry through these states, and one
  1997. additional state where no keyword is present. For details about this
  1998. cycling, see the manual.
  1999. TODO keywords and interpretation can also be set on a per-file basis with
  2000. the special #+SEQ_TODO and #+TYP_TODO lines.
  2001. Each keyword can optionally specify a character for fast state selection
  2002. \(in combination with the variable `org-use-fast-todo-selection')
  2003. and specifiers for state change logging, using the same syntax that
  2004. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2005. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2006. indicates to record a time stamp each time this state is selected.
  2007. Each keyword may also specify if a timestamp or a note should be
  2008. recorded when entering or leaving the state, by adding additional
  2009. characters in the parenthesis after the keyword. This looks like this:
  2010. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2011. record only the time of the state change. With X and Y being either
  2012. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2013. Y when leaving the state if and only if the *target* state does not
  2014. define X. You may omit any of the fast-selection key or X or /Y,
  2015. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2016. For backward compatibility, this variable may also be just a list
  2017. of keywords. In this case the interpretation (sequence or type) will be
  2018. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2019. :group 'org-todo
  2020. :group 'org-keywords
  2021. :type '(choice
  2022. (repeat :tag "Old syntax, just keywords"
  2023. (string :tag "Keyword"))
  2024. (repeat :tag "New syntax"
  2025. (cons
  2026. (choice
  2027. :tag "Interpretation"
  2028. ;;Quick and dirty way to see
  2029. ;;`org-todo-interpretations'. This takes the
  2030. ;;place of item arguments
  2031. :convert-widget
  2032. (lambda (widget)
  2033. (widget-put widget
  2034. :args (mapcar
  2035. #'(lambda (x)
  2036. (widget-convert
  2037. (cons 'const x)))
  2038. org-todo-interpretation-widgets))
  2039. widget))
  2040. (repeat
  2041. (string :tag "Keyword"))))))
  2042. (defvar org-todo-keywords-1 nil
  2043. "All TODO and DONE keywords active in a buffer.")
  2044. (make-variable-buffer-local 'org-todo-keywords-1)
  2045. (defvar org-todo-keywords-for-agenda nil)
  2046. (defvar org-done-keywords-for-agenda nil)
  2047. (defvar org-drawers-for-agenda nil)
  2048. (defvar org-todo-keyword-alist-for-agenda nil)
  2049. (defvar org-tag-alist-for-agenda nil
  2050. "Alist of all tags from all agenda files.")
  2051. (defvar org-tag-groups-alist-for-agenda nil
  2052. "Alist of all groups tags from all current agenda files.")
  2053. (defvar org-tag-groups-alist nil)
  2054. (make-variable-buffer-local 'org-tag-groups-alist)
  2055. (defvar org-agenda-contributing-files nil)
  2056. (defvar org-not-done-keywords nil)
  2057. (make-variable-buffer-local 'org-not-done-keywords)
  2058. (defvar org-done-keywords nil)
  2059. (make-variable-buffer-local 'org-done-keywords)
  2060. (defvar org-todo-heads nil)
  2061. (make-variable-buffer-local 'org-todo-heads)
  2062. (defvar org-todo-sets nil)
  2063. (make-variable-buffer-local 'org-todo-sets)
  2064. (defvar org-todo-log-states nil)
  2065. (make-variable-buffer-local 'org-todo-log-states)
  2066. (defvar org-todo-kwd-alist nil)
  2067. (make-variable-buffer-local 'org-todo-kwd-alist)
  2068. (defvar org-todo-key-alist nil)
  2069. (make-variable-buffer-local 'org-todo-key-alist)
  2070. (defvar org-todo-key-trigger nil)
  2071. (make-variable-buffer-local 'org-todo-key-trigger)
  2072. (defcustom org-todo-interpretation 'sequence
  2073. "Controls how TODO keywords are interpreted.
  2074. This variable is in principle obsolete and is only used for
  2075. backward compatibility, if the interpretation of todo keywords is
  2076. not given already in `org-todo-keywords'. See that variable for
  2077. more information."
  2078. :group 'org-todo
  2079. :group 'org-keywords
  2080. :type '(choice (const sequence)
  2081. (const type)))
  2082. (defcustom org-use-fast-todo-selection t
  2083. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2084. This variable describes if and under what circumstances the cycling
  2085. mechanism for TODO keywords will be replaced by a single-key, direct
  2086. selection scheme.
  2087. When nil, fast selection is never used.
  2088. When the symbol `prefix', it will be used when `org-todo' is called
  2089. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2090. `C-u t' in an agenda buffer.
  2091. When t, fast selection is used by default. In this case, the prefix
  2092. argument forces cycling instead.
  2093. In all cases, the special interface is only used if access keys have
  2094. actually been assigned by the user, i.e. if keywords in the configuration
  2095. are followed by a letter in parenthesis, like TODO(t)."
  2096. :group 'org-todo
  2097. :type '(choice
  2098. (const :tag "Never" nil)
  2099. (const :tag "By default" t)
  2100. (const :tag "Only with C-u C-c C-t" prefix)))
  2101. (defcustom org-provide-todo-statistics t
  2102. "Non-nil means update todo statistics after insert and toggle.
  2103. ALL-HEADLINES means update todo statistics by including headlines
  2104. with no TODO keyword as well, counting them as not done.
  2105. A list of TODO keywords means the same, but skip keywords that are
  2106. not in this list.
  2107. When this is set, todo statistics is updated in the parent of the
  2108. current entry each time a todo state is changed."
  2109. :group 'org-todo
  2110. :type '(choice
  2111. (const :tag "Yes, only for TODO entries" t)
  2112. (const :tag "Yes, including all entries" 'all-headlines)
  2113. (repeat :tag "Yes, for TODOs in this list"
  2114. (string :tag "TODO keyword"))
  2115. (other :tag "No TODO statistics" nil)))
  2116. (defcustom org-hierarchical-todo-statistics t
  2117. "Non-nil means TODO statistics covers just direct children.
  2118. When nil, all entries in the subtree are considered.
  2119. This has only an effect if `org-provide-todo-statistics' is set.
  2120. To set this to nil for only a single subtree, use a COOKIE_DATA
  2121. property and include the word \"recursive\" into the value."
  2122. :group 'org-todo
  2123. :type 'boolean)
  2124. (defcustom org-after-todo-state-change-hook nil
  2125. "Hook which is run after the state of a TODO item was changed.
  2126. The new state (a string with a TODO keyword, or nil) is available in the
  2127. Lisp variable `org-state'."
  2128. :group 'org-todo
  2129. :type 'hook)
  2130. (defvar org-blocker-hook nil
  2131. "Hook for functions that are allowed to block a state change.
  2132. Functions in this hook should not modify the buffer.
  2133. Each function gets as its single argument a property list,
  2134. see `org-trigger-hook' for more information about this list.
  2135. If any of the functions in this hook returns nil, the state change
  2136. is blocked.")
  2137. (defvar org-trigger-hook nil
  2138. "Hook for functions that are triggered by a state change.
  2139. Each function gets as its single argument a property list with at
  2140. least the following elements:
  2141. (:type type-of-change :position pos-at-entry-start
  2142. :from old-state :to new-state)
  2143. Depending on the type, more properties may be present.
  2144. This mechanism is currently implemented for:
  2145. TODO state changes
  2146. ------------------
  2147. :type todo-state-change
  2148. :from previous state (keyword as a string), or nil, or a symbol
  2149. 'todo' or 'done', to indicate the general type of state.
  2150. :to new state, like in :from")
  2151. (defcustom org-enforce-todo-dependencies nil
  2152. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2153. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2154. be blocked if any prior sibling is not yet done.
  2155. Finally, if the parent is blocked because of ordered siblings of its own,
  2156. the child will also be blocked."
  2157. :set (lambda (var val)
  2158. (set var val)
  2159. (if val
  2160. (add-hook 'org-blocker-hook
  2161. 'org-block-todo-from-children-or-siblings-or-parent)
  2162. (remove-hook 'org-blocker-hook
  2163. 'org-block-todo-from-children-or-siblings-or-parent)))
  2164. :group 'org-todo
  2165. :type 'boolean)
  2166. (defcustom org-enforce-todo-checkbox-dependencies nil
  2167. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2168. When this is nil, checkboxes have no influence on switching TODO states.
  2169. When non-nil, you first need to check off all check boxes before the TODO
  2170. entry can be switched to DONE.
  2171. This variable needs to be set before org.el is loaded, and you need to
  2172. restart Emacs after a change to make the change effective. The only way
  2173. to change is while Emacs is running is through the customize interface."
  2174. :set (lambda (var val)
  2175. (set var val)
  2176. (if val
  2177. (add-hook 'org-blocker-hook
  2178. 'org-block-todo-from-checkboxes)
  2179. (remove-hook 'org-blocker-hook
  2180. 'org-block-todo-from-checkboxes)))
  2181. :group 'org-todo
  2182. :type 'boolean)
  2183. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2184. "Non-nil means inserting a TODO heading is treated as state change.
  2185. So when the command \\[org-insert-todo-heading] is used, state change
  2186. logging will apply if appropriate. When nil, the new TODO item will
  2187. be inserted directly, and no logging will take place."
  2188. :group 'org-todo
  2189. :type 'boolean)
  2190. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2191. "Non-nil means switching TODO states with S-cursor counts as state change.
  2192. This is the default behavior. However, setting this to nil allows a
  2193. convenient way to select a TODO state and bypass any logging associated
  2194. with that."
  2195. :group 'org-todo
  2196. :type 'boolean)
  2197. (defcustom org-todo-state-tags-triggers nil
  2198. "Tag changes that should be triggered by TODO state changes.
  2199. This is a list. Each entry is
  2200. (state-change (tag . flag) .......)
  2201. State-change can be a string with a state, and empty string to indicate the
  2202. state that has no TODO keyword, or it can be one of the symbols `todo'
  2203. or `done', meaning any not-done or done state, respectively."
  2204. :group 'org-todo
  2205. :group 'org-tags
  2206. :type '(repeat
  2207. (cons (choice :tag "When changing to"
  2208. (const :tag "Not-done state" todo)
  2209. (const :tag "Done state" done)
  2210. (string :tag "State"))
  2211. (repeat
  2212. (cons :tag "Tag action"
  2213. (string :tag "Tag")
  2214. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2215. (defcustom org-log-done nil
  2216. "Information to record when a task moves to the DONE state.
  2217. Possible values are:
  2218. nil Don't add anything, just change the keyword
  2219. time Add a time stamp to the task
  2220. note Prompt for a note and add it with template `org-log-note-headings'
  2221. This option can also be set with on a per-file-basis with
  2222. #+STARTUP: nologdone
  2223. #+STARTUP: logdone
  2224. #+STARTUP: lognotedone
  2225. You can have local logging settings for a subtree by setting the LOGGING
  2226. property to one or more of these keywords."
  2227. :group 'org-todo
  2228. :group 'org-progress
  2229. :type '(choice
  2230. (const :tag "No logging" nil)
  2231. (const :tag "Record CLOSED timestamp" time)
  2232. (const :tag "Record CLOSED timestamp with note." note)))
  2233. ;; Normalize old uses of org-log-done.
  2234. (cond
  2235. ((eq org-log-done t) (setq org-log-done 'time))
  2236. ((and (listp org-log-done) (memq 'done org-log-done))
  2237. (setq org-log-done 'note)))
  2238. (defcustom org-log-reschedule nil
  2239. "Information to record when the scheduling date of a tasks is modified.
  2240. Possible values are:
  2241. nil Don't add anything, just change the date
  2242. time Add a time stamp to the task
  2243. note Prompt for a note and add it with template `org-log-note-headings'
  2244. This option can also be set with on a per-file-basis with
  2245. #+STARTUP: nologreschedule
  2246. #+STARTUP: logreschedule
  2247. #+STARTUP: lognotereschedule"
  2248. :group 'org-todo
  2249. :group 'org-progress
  2250. :type '(choice
  2251. (const :tag "No logging" nil)
  2252. (const :tag "Record timestamp" time)
  2253. (const :tag "Record timestamp with note." note)))
  2254. (defcustom org-log-redeadline nil
  2255. "Information to record when the deadline date of a tasks is modified.
  2256. Possible values are:
  2257. nil Don't add anything, just change the date
  2258. time Add a time stamp to the task
  2259. note Prompt for a note and add it with template `org-log-note-headings'
  2260. This option can also be set with on a per-file-basis with
  2261. #+STARTUP: nologredeadline
  2262. #+STARTUP: logredeadline
  2263. #+STARTUP: lognoteredeadline
  2264. You can have local logging settings for a subtree by setting the LOGGING
  2265. property to one or more of these keywords."
  2266. :group 'org-todo
  2267. :group 'org-progress
  2268. :type '(choice
  2269. (const :tag "No logging" nil)
  2270. (const :tag "Record timestamp" time)
  2271. (const :tag "Record timestamp with note." note)))
  2272. (defcustom org-log-note-clock-out nil
  2273. "Non-nil means record a note when clocking out of an item.
  2274. This can also be configured on a per-file basis by adding one of
  2275. the following lines anywhere in the buffer:
  2276. #+STARTUP: lognoteclock-out
  2277. #+STARTUP: nolognoteclock-out"
  2278. :group 'org-todo
  2279. :group 'org-progress
  2280. :type 'boolean)
  2281. (defcustom org-log-done-with-time t
  2282. "Non-nil means the CLOSED time stamp will contain date and time.
  2283. When nil, only the date will be recorded."
  2284. :group 'org-progress
  2285. :type 'boolean)
  2286. (defcustom org-log-note-headings
  2287. '((done . "CLOSING NOTE %t")
  2288. (state . "State %-12s from %-12S %t")
  2289. (note . "Note taken on %t")
  2290. (reschedule . "Rescheduled from %S on %t")
  2291. (delschedule . "Not scheduled, was %S on %t")
  2292. (redeadline . "New deadline from %S on %t")
  2293. (deldeadline . "Removed deadline, was %S on %t")
  2294. (refile . "Refiled on %t")
  2295. (clock-out . ""))
  2296. "Headings for notes added to entries.
  2297. The value is an alist, with the car being a symbol indicating the note
  2298. context, and the cdr is the heading to be used. The heading may also be the
  2299. empty string.
  2300. %t in the heading will be replaced by a time stamp.
  2301. %T will be an active time stamp instead the default inactive one
  2302. %d will be replaced by a short-format time stamp.
  2303. %D will be replaced by an active short-format time stamp.
  2304. %s will be replaced by the new TODO state, in double quotes.
  2305. %S will be replaced by the old TODO state, in double quotes.
  2306. %u will be replaced by the user name.
  2307. %U will be replaced by the full user name.
  2308. In fact, it is not a good idea to change the `state' entry, because
  2309. agenda log mode depends on the format of these entries."
  2310. :group 'org-todo
  2311. :group 'org-progress
  2312. :type '(list :greedy t
  2313. (cons (const :tag "Heading when closing an item" done) string)
  2314. (cons (const :tag
  2315. "Heading when changing todo state (todo sequence only)"
  2316. state) string)
  2317. (cons (const :tag "Heading when just taking a note" note) string)
  2318. (cons (const :tag "Heading when clocking out" clock-out) string)
  2319. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2320. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2321. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2322. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2323. (cons (const :tag "Heading when refiling" refile) string)))
  2324. (unless (assq 'note org-log-note-headings)
  2325. (push '(note . "%t") org-log-note-headings))
  2326. (defcustom org-log-into-drawer nil
  2327. "Non-nil means insert state change notes and time stamps into a drawer.
  2328. When nil, state changes notes will be inserted after the headline and
  2329. any scheduling and clock lines, but not inside a drawer.
  2330. The value of this variable should be the name of the drawer to use.
  2331. LOGBOOK is proposed as the default drawer for this purpose, you can
  2332. also set this to a string to define the drawer of your choice.
  2333. A value of t is also allowed, representing \"LOGBOOK\".
  2334. A value of t or nil can also be set with on a per-file-basis with
  2335. #+STARTUP: logdrawer
  2336. #+STARTUP: nologdrawer
  2337. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2338. will be ignored.
  2339. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2340. a subtree."
  2341. :group 'org-todo
  2342. :group 'org-progress
  2343. :type '(choice
  2344. (const :tag "Not into a drawer" nil)
  2345. (const :tag "LOGBOOK" t)
  2346. (string :tag "Other")))
  2347. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2348. (defun org-log-into-drawer ()
  2349. "Return the value of `org-log-into-drawer', but let properties overrule.
  2350. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2351. used instead of the default value."
  2352. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2353. (cond
  2354. ((not p) org-log-into-drawer)
  2355. ((equal p "nil") nil)
  2356. ((equal p "t") "LOGBOOK")
  2357. (t p))))
  2358. (defcustom org-log-state-notes-insert-after-drawers nil
  2359. "Non-nil means insert state change notes after any drawers in entry.
  2360. Only the drawers that *immediately* follow the headline and the
  2361. deadline/scheduled line are skipped.
  2362. When nil, insert notes right after the heading and perhaps the line
  2363. with deadline/scheduling if present.
  2364. This variable will have no effect if `org-log-into-drawer' is
  2365. set."
  2366. :group 'org-todo
  2367. :group 'org-progress
  2368. :type 'boolean)
  2369. (defcustom org-log-states-order-reversed t
  2370. "Non-nil means the latest state note will be directly after heading.
  2371. When nil, the state change notes will be ordered according to time.
  2372. This option can also be set with on a per-file-basis with
  2373. #+STARTUP: logstatesreversed
  2374. #+STARTUP: nologstatesreversed"
  2375. :group 'org-todo
  2376. :group 'org-progress
  2377. :type 'boolean)
  2378. (defcustom org-todo-repeat-to-state nil
  2379. "The TODO state to which a repeater should return the repeating task.
  2380. By default this is the first task in a TODO sequence, or the previous state
  2381. in a TODO_TYP set. But you can specify another task here.
  2382. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2383. :group 'org-todo
  2384. :version "24.1"
  2385. :type '(choice (const :tag "Head of sequence" nil)
  2386. (string :tag "Specific state")))
  2387. (defcustom org-log-repeat 'time
  2388. "Non-nil means record moving through the DONE state when triggering repeat.
  2389. An auto-repeating task is immediately switched back to TODO when
  2390. marked DONE. If you are not logging state changes (by adding \"@\"
  2391. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2392. record a closing note, there will be no record of the task moving
  2393. through DONE. This variable forces taking a note anyway.
  2394. nil Don't force a record
  2395. time Record a time stamp
  2396. note Prompt for a note and add it with template `org-log-note-headings'
  2397. This option can also be set with on a per-file-basis with
  2398. #+STARTUP: nologrepeat
  2399. #+STARTUP: logrepeat
  2400. #+STARTUP: lognoterepeat
  2401. You can have local logging settings for a subtree by setting the LOGGING
  2402. property to one or more of these keywords."
  2403. :group 'org-todo
  2404. :group 'org-progress
  2405. :type '(choice
  2406. (const :tag "Don't force a record" nil)
  2407. (const :tag "Force recording the DONE state" time)
  2408. (const :tag "Force recording a note with the DONE state" note)))
  2409. (defgroup org-priorities nil
  2410. "Priorities in Org-mode."
  2411. :tag "Org Priorities"
  2412. :group 'org-todo)
  2413. (defcustom org-enable-priority-commands t
  2414. "Non-nil means priority commands are active.
  2415. When nil, these commands will be disabled, so that you never accidentally
  2416. set a priority."
  2417. :group 'org-priorities
  2418. :type 'boolean)
  2419. (defcustom org-highest-priority ?A
  2420. "The highest priority of TODO items. A character like ?A, ?B etc.
  2421. Must have a smaller ASCII number than `org-lowest-priority'."
  2422. :group 'org-priorities
  2423. :type 'character)
  2424. (defcustom org-lowest-priority ?C
  2425. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2426. Must have a larger ASCII number than `org-highest-priority'."
  2427. :group 'org-priorities
  2428. :type 'character)
  2429. (defcustom org-default-priority ?B
  2430. "The default priority of TODO items.
  2431. This is the priority an item gets if no explicit priority is given.
  2432. When starting to cycle on an empty priority the first step in the cycle
  2433. depends on `org-priority-start-cycle-with-default'. The resulting first
  2434. step priority must not exceed the range from `org-highest-priority' to
  2435. `org-lowest-priority' which means that `org-default-priority' has to be
  2436. in this range exclusive or inclusive the range boundaries. Else the
  2437. first step refuses to set the default and the second will fall back
  2438. to (depending on the command used) the highest or lowest priority."
  2439. :group 'org-priorities
  2440. :type 'character)
  2441. (defcustom org-priority-start-cycle-with-default t
  2442. "Non-nil means start with default priority when starting to cycle.
  2443. When this is nil, the first step in the cycle will be (depending on the
  2444. command used) one higher or lower than the default priority.
  2445. See also `org-default-priority'."
  2446. :group 'org-priorities
  2447. :type 'boolean)
  2448. (defcustom org-get-priority-function nil
  2449. "Function to extract the priority from a string.
  2450. The string is normally the headline. If this is nil Org computes the
  2451. priority from the priority cookie like [#A] in the headline. It returns
  2452. an integer, increasing by 1000 for each priority level.
  2453. The user can set a different function here, which should take a string
  2454. as an argument and return the numeric priority."
  2455. :group 'org-priorities
  2456. :version "24.1"
  2457. :type 'function)
  2458. (defgroup org-time nil
  2459. "Options concerning time stamps and deadlines in Org-mode."
  2460. :tag "Org Time"
  2461. :group 'org)
  2462. (defcustom org-insert-labeled-timestamps-at-point nil
  2463. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2464. When nil, these labeled time stamps are forces into the second line of an
  2465. entry, just after the headline. When scheduling from the global TODO list,
  2466. the time stamp will always be forced into the second line."
  2467. :group 'org-time
  2468. :type 'boolean)
  2469. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2470. "Formats for `format-time-string' which are used for time stamps.
  2471. It is not recommended to change this constant.")
  2472. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2473. "Number of minutes to round time stamps to.
  2474. These are two values, the first applies when first creating a time stamp.
  2475. The second applies when changing it with the commands `S-up' and `S-down'.
  2476. When changing the time stamp, this means that it will change in steps
  2477. of N minutes, as given by the second value.
  2478. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2479. numbers should be factors of 60, so for example 5, 10, 15.
  2480. When this is larger than 1, you can still force an exact time stamp by using
  2481. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2482. and by using a prefix arg to `S-up/down' to specify the exact number
  2483. of minutes to shift."
  2484. :group 'org-time
  2485. :get #'(lambda (var) ; Make sure both elements are there
  2486. (if (integerp (default-value var))
  2487. (list (default-value var) 5)
  2488. (default-value var)))
  2489. :type '(list
  2490. (integer :tag "when inserting times")
  2491. (integer :tag "when modifying times")))
  2492. ;; Normalize old customizations of this variable.
  2493. (when (integerp org-time-stamp-rounding-minutes)
  2494. (setq org-time-stamp-rounding-minutes
  2495. (list org-time-stamp-rounding-minutes
  2496. org-time-stamp-rounding-minutes)))
  2497. (defcustom org-display-custom-times nil
  2498. "Non-nil means overlay custom formats over all time stamps.
  2499. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2500. To turn this on on a per-file basis, insert anywhere in the file:
  2501. #+STARTUP: customtime"
  2502. :group 'org-time
  2503. :set 'set-default
  2504. :type 'sexp)
  2505. (make-variable-buffer-local 'org-display-custom-times)
  2506. (defcustom org-time-stamp-custom-formats
  2507. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2508. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2509. These are overlaid over the default ISO format if the variable
  2510. `org-display-custom-times' is set. Time like %H:%M should be at the
  2511. end of the second format. The custom formats are also honored by export
  2512. commands, if custom time display is turned on at the time of export."
  2513. :group 'org-time
  2514. :type 'sexp)
  2515. (defun org-time-stamp-format (&optional long inactive)
  2516. "Get the right format for a time string."
  2517. (let ((f (if long (cdr org-time-stamp-formats)
  2518. (car org-time-stamp-formats))))
  2519. (if inactive
  2520. (concat "[" (substring f 1 -1) "]")
  2521. f)))
  2522. (defcustom org-time-clocksum-format
  2523. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2524. "The format string used when creating CLOCKSUM lines.
  2525. This is also used when Org mode generates a time duration.
  2526. The value can be a single format string containing two
  2527. %-sequences, which will be filled with the number of hours and
  2528. minutes in that order.
  2529. Alternatively, the value can be a plist associating any of the
  2530. keys :years, :months, :weeks, :days, :hours or :minutes with
  2531. format strings. The time duration is formatted using only the
  2532. time components that are needed and concatenating the results.
  2533. If a time unit in absent, it falls back to the next smallest
  2534. unit.
  2535. The keys :require-years, :require-months, :require-days,
  2536. :require-weeks, :require-hours, :require-minutes are also
  2537. meaningful. A non-nil value for these keys indicates that the
  2538. corresponding time component should always be included, even if
  2539. its value is 0.
  2540. For example,
  2541. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2542. :require-minutes t)
  2543. means durations longer than a day will be expressed in days,
  2544. hours and minutes, and durations less than a day will always be
  2545. expressed in hours and minutes (even for durations less than an
  2546. hour).
  2547. The value
  2548. \(:days \"%dd\" :minutes \"%dm\")
  2549. means durations longer than a day will be expressed in days and
  2550. minutes, and durations less than a day will be expressed entirely
  2551. in minutes (even for durations longer than an hour)."
  2552. :group 'org-time
  2553. :group 'org-clock
  2554. :version "24.4"
  2555. :package-version '(Org . "8.0")
  2556. :type '(choice (string :tag "Format string")
  2557. (set :tag "Plist"
  2558. (group :inline t (const :tag "Years" :years)
  2559. (string :tag "Format string"))
  2560. (group :inline t
  2561. (const :tag "Always show years" :require-years)
  2562. (const t))
  2563. (group :inline t (const :tag "Months" :months)
  2564. (string :tag "Format string"))
  2565. (group :inline t
  2566. (const :tag "Always show months" :require-months)
  2567. (const t))
  2568. (group :inline t (const :tag "Weeks" :weeks)
  2569. (string :tag "Format string"))
  2570. (group :inline t
  2571. (const :tag "Always show weeks" :require-weeks)
  2572. (const t))
  2573. (group :inline t (const :tag "Days" :days)
  2574. (string :tag "Format string"))
  2575. (group :inline t
  2576. (const :tag "Always show days" :require-days)
  2577. (const t))
  2578. (group :inline t (const :tag "Hours" :hours)
  2579. (string :tag "Format string"))
  2580. (group :inline t
  2581. (const :tag "Always show hours" :require-hours)
  2582. (const t))
  2583. (group :inline t (const :tag "Minutes" :minutes)
  2584. (string :tag "Format string"))
  2585. (group :inline t
  2586. (const :tag "Always show minutes" :require-minutes)
  2587. (const t)))))
  2588. (defcustom org-time-clocksum-use-fractional nil
  2589. "When non-nil, \\[org-clock-display] uses fractional times.
  2590. See `org-time-clocksum-format' for more on time clock formats."
  2591. :group 'org-time
  2592. :group 'org-clock
  2593. :version "24.3"
  2594. :type 'boolean)
  2595. (defcustom org-time-clocksum-use-effort-durations nil
  2596. "When non-nil, \\[org-clock-display] uses effort durations.
  2597. E.g. by default, one day is considered to be a 8 hours effort,
  2598. so a task that has been clocked for 16 hours will be displayed
  2599. as during 2 days in the clock display or in the clocktable.
  2600. See `org-effort-durations' on how to set effort durations
  2601. and `org-time-clocksum-format' for more on time clock formats."
  2602. :group 'org-time
  2603. :group 'org-clock
  2604. :version "24.4"
  2605. :package-version '(Org . "8.0")
  2606. :type 'boolean)
  2607. (defcustom org-time-clocksum-fractional-format "%.2f"
  2608. "The format string used when creating CLOCKSUM lines,
  2609. or when Org mode generates a time duration, if
  2610. `org-time-clocksum-use-fractional' is enabled.
  2611. The value can be a single format string containing one
  2612. %-sequence, which will be filled with the number of hours as
  2613. a float.
  2614. Alternatively, the value can be a plist associating any of the
  2615. keys :years, :months, :weeks, :days, :hours or :minutes with
  2616. a format string. The time duration is formatted using the
  2617. largest time unit which gives a non-zero integer part. If all
  2618. specified formats have zero integer part, the smallest time unit
  2619. is used."
  2620. :group 'org-time
  2621. :type '(choice (string :tag "Format string")
  2622. (set (group :inline t (const :tag "Years" :years)
  2623. (string :tag "Format string"))
  2624. (group :inline t (const :tag "Months" :months)
  2625. (string :tag "Format string"))
  2626. (group :inline t (const :tag "Weeks" :weeks)
  2627. (string :tag "Format string"))
  2628. (group :inline t (const :tag "Days" :days)
  2629. (string :tag "Format string"))
  2630. (group :inline t (const :tag "Hours" :hours)
  2631. (string :tag "Format string"))
  2632. (group :inline t (const :tag "Minutes" :minutes)
  2633. (string :tag "Format string")))))
  2634. (defcustom org-deadline-warning-days 14
  2635. "Number of days before expiration during which a deadline becomes active.
  2636. This variable governs the display in sparse trees and in the agenda.
  2637. When 0 or negative, it means use this number (the absolute value of it)
  2638. even if a deadline has a different individual lead time specified.
  2639. Custom commands can set this variable in the options section."
  2640. :group 'org-time
  2641. :group 'org-agenda-daily/weekly
  2642. :type 'integer)
  2643. (defcustom org-scheduled-delay-days 0
  2644. "Number of days before a scheduled item becomes active.
  2645. This variable governs the display in sparse trees and in the agenda.
  2646. The default value (i.e. 0) means: don't delay scheduled item.
  2647. When negative, it means use this number (the absolute value of it)
  2648. even if a scheduled item has a different individual delay time
  2649. specified.
  2650. Custom commands can set this variable in the options section."
  2651. :group 'org-time
  2652. :group 'org-agenda-daily/weekly
  2653. :version "24.4"
  2654. :package-version '(Org . "8.0")
  2655. :type 'integer)
  2656. (defcustom org-read-date-prefer-future t
  2657. "Non-nil means assume future for incomplete date input from user.
  2658. This affects the following situations:
  2659. 1. The user gives a month but not a year.
  2660. For example, if it is April and you enter \"feb 2\", this will be read
  2661. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2662. 2. The user gives a day, but no month.
  2663. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2664. read this as the third of *next* month. However, if you enter \"17\",
  2665. it will be considered as *this* month.
  2666. If you set this variable to the symbol `time', then also the following
  2667. will work:
  2668. 3. If the user gives a time.
  2669. If the time is before now, it will be interpreted as tomorrow.
  2670. Currently none of this works for ISO week specifications.
  2671. When this option is nil, the current day, month and year will always be
  2672. used as defaults.
  2673. See also `org-agenda-jump-prefer-future'."
  2674. :group 'org-time
  2675. :type '(choice
  2676. (const :tag "Never" nil)
  2677. (const :tag "Check month and day" t)
  2678. (const :tag "Check month, day, and time" time)))
  2679. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2680. "Should the agenda jump command prefer the future for incomplete dates?
  2681. The default is to do the same as configured in `org-read-date-prefer-future'.
  2682. But you can also set a deviating value here.
  2683. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2684. :group 'org-agenda
  2685. :group 'org-time
  2686. :version "24.1"
  2687. :type '(choice
  2688. (const :tag "Use org-read-date-prefer-future"
  2689. org-read-date-prefer-future)
  2690. (const :tag "Never" nil)
  2691. (const :tag "Always" t)))
  2692. (defcustom org-read-date-force-compatible-dates t
  2693. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2694. Depending on the system Emacs is running on, certain dates cannot
  2695. be represented with the type used internally to represent time.
  2696. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2697. correctly. Some systems allow for earlier dates, some for later,
  2698. some for both. One way to find out it to insert any date into an
  2699. Org buffer, putting the cursor on the year and hitting S-up and
  2700. S-down to test the range.
  2701. When this variable is set to t, the date/time prompt will not let
  2702. you specify dates outside the 1970-2037 range, so it is certain that
  2703. these dates will work in whatever version of Emacs you are
  2704. running, and also that you can move a file from one Emacs implementation
  2705. to another. WHenever Org is forcing the year for you, it will display
  2706. a message and beep.
  2707. When this variable is nil, Org will check if the date is
  2708. representable in the specific Emacs implementation you are using.
  2709. If not, it will force a year, usually the current year, and beep
  2710. to remind you. Currently this setting is not recommended because
  2711. the likelihood that you will open your Org files in an Emacs that
  2712. has limited date range is not negligible.
  2713. A workaround for this problem is to use diary sexp dates for time
  2714. stamps outside of this range."
  2715. :group 'org-time
  2716. :version "24.1"
  2717. :type 'boolean)
  2718. (defcustom org-read-date-display-live t
  2719. "Non-nil means display current interpretation of date prompt live.
  2720. This display will be in an overlay, in the minibuffer."
  2721. :group 'org-time
  2722. :type 'boolean)
  2723. (defcustom org-read-date-popup-calendar t
  2724. "Non-nil means pop up a calendar when prompting for a date.
  2725. In the calendar, the date can be selected with mouse-1. However, the
  2726. minibuffer will also be active, and you can simply enter the date as well.
  2727. When nil, only the minibuffer will be available."
  2728. :group 'org-time
  2729. :type 'boolean)
  2730. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2731. 'org-read-date-popup-calendar)
  2732. (make-obsolete-variable
  2733. 'org-read-date-minibuffer-setup-hook
  2734. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2735. (defcustom org-read-date-minibuffer-setup-hook nil
  2736. "Hook to be used to set up keys for the date/time interface.
  2737. Add key definitions to `minibuffer-local-map', which will be a
  2738. temporary copy.
  2739. WARNING: This option is obsolete, you should use
  2740. `org-read-date-minibuffer-local-map' to set up keys."
  2741. :group 'org-time
  2742. :type 'hook)
  2743. (defcustom org-extend-today-until 0
  2744. "The hour when your day really ends. Must be an integer.
  2745. This has influence for the following applications:
  2746. - When switching the agenda to \"today\". It it is still earlier than
  2747. the time given here, the day recognized as TODAY is actually yesterday.
  2748. - When a date is read from the user and it is still before the time given
  2749. here, the current date and time will be assumed to be yesterday, 23:59.
  2750. Also, timestamps inserted in capture templates follow this rule.
  2751. IMPORTANT: This is a feature whose implementation is and likely will
  2752. remain incomplete. Really, it is only here because past midnight seems to
  2753. be the favorite working time of John Wiegley :-)"
  2754. :group 'org-time
  2755. :type 'integer)
  2756. (defcustom org-use-effective-time nil
  2757. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2758. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2759. \"effective time\" of any timestamps between midnight and 8am will be
  2760. 23:59 of the previous day."
  2761. :group 'org-time
  2762. :version "24.1"
  2763. :type 'boolean)
  2764. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2765. "When non-nil, use the last clock out time for `org-todo'.
  2766. Note that this option has precedence over the combined use of
  2767. `org-use-effective-time' and `org-extend-today-until'."
  2768. :group 'org-time
  2769. :version "24.4"
  2770. :package-version '(Org . "8.0")
  2771. :type 'boolean)
  2772. (defcustom org-edit-timestamp-down-means-later nil
  2773. "Non-nil means S-down will increase the time in a time stamp.
  2774. When nil, S-up will increase."
  2775. :group 'org-time
  2776. :type 'boolean)
  2777. (defcustom org-calendar-follow-timestamp-change t
  2778. "Non-nil means make the calendar window follow timestamp changes.
  2779. When a timestamp is modified and the calendar window is visible, it will be
  2780. moved to the new date."
  2781. :group 'org-time
  2782. :type 'boolean)
  2783. (defgroup org-tags nil
  2784. "Options concerning tags in Org-mode."
  2785. :tag "Org Tags"
  2786. :group 'org)
  2787. (defcustom org-tag-alist nil
  2788. "List of tags allowed in Org-mode files.
  2789. When this list is nil, Org-mode will base TAG input on what is already in the
  2790. buffer.
  2791. The value of this variable is an alist, the car of each entry must be a
  2792. keyword as a string, the cdr may be a character that is used to select
  2793. that tag through the fast-tag-selection interface.
  2794. See the manual for details."
  2795. :group 'org-tags
  2796. :type '(repeat
  2797. (choice
  2798. (cons (string :tag "Tag name")
  2799. (character :tag "Access char"))
  2800. (list :tag "Start radio group"
  2801. (const :startgroup)
  2802. (option (string :tag "Group description")))
  2803. (list :tag "Group tags delimiter"
  2804. (const :grouptags))
  2805. (list :tag "End radio group"
  2806. (const :endgroup)
  2807. (option (string :tag "Group description")))
  2808. (const :tag "New line" (:newline)))))
  2809. (defcustom org-tag-persistent-alist nil
  2810. "List of tags that will always appear in all Org-mode files.
  2811. This is in addition to any in buffer settings or customizations
  2812. of `org-tag-alist'.
  2813. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2814. The value of this variable is an alist, the car of each entry must be a
  2815. keyword as a string, the cdr may be a character that is used to select
  2816. that tag through the fast-tag-selection interface.
  2817. See the manual for details.
  2818. To disable these tags on a per-file basis, insert anywhere in the file:
  2819. #+STARTUP: noptag"
  2820. :group 'org-tags
  2821. :type '(repeat
  2822. (choice
  2823. (cons (string :tag "Tag name")
  2824. (character :tag "Access char"))
  2825. (const :tag "Start radio group" (:startgroup))
  2826. (const :tag "Group tags delimiter" (:grouptags))
  2827. (const :tag "End radio group" (:endgroup))
  2828. (const :tag "New line" (:newline)))))
  2829. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2830. "If non-nil, always offer completion for all tags of all agenda files.
  2831. Instead of customizing this variable directly, you might want to
  2832. set it locally for capture buffers, because there no list of
  2833. tags in that file can be created dynamically (there are none).
  2834. (add-hook 'org-capture-mode-hook
  2835. (lambda ()
  2836. (set (make-local-variable
  2837. 'org-complete-tags-always-offer-all-agenda-tags)
  2838. t)))"
  2839. :group 'org-tags
  2840. :version "24.1"
  2841. :type 'boolean)
  2842. (defvar org-file-tags nil
  2843. "List of tags that can be inherited by all entries in the file.
  2844. The tags will be inherited if the variable `org-use-tag-inheritance'
  2845. says they should be.
  2846. This variable is populated from #+FILETAGS lines.")
  2847. (defcustom org-use-fast-tag-selection 'auto
  2848. "Non-nil means use fast tag selection scheme.
  2849. This is a special interface to select and deselect tags with single keys.
  2850. When nil, fast selection is never used.
  2851. When the symbol `auto', fast selection is used if and only if selection
  2852. characters for tags have been configured, either through the variable
  2853. `org-tag-alist' or through a #+TAGS line in the buffer.
  2854. When t, fast selection is always used and selection keys are assigned
  2855. automatically if necessary."
  2856. :group 'org-tags
  2857. :type '(choice
  2858. (const :tag "Always" t)
  2859. (const :tag "Never" nil)
  2860. (const :tag "When selection characters are configured" 'auto)))
  2861. (defcustom org-fast-tag-selection-single-key nil
  2862. "Non-nil means fast tag selection exits after first change.
  2863. When nil, you have to press RET to exit it.
  2864. During fast tag selection, you can toggle this flag with `C-c'.
  2865. This variable can also have the value `expert'. In this case, the window
  2866. displaying the tags menu is not even shown, until you press C-c again."
  2867. :group 'org-tags
  2868. :type '(choice
  2869. (const :tag "No" nil)
  2870. (const :tag "Yes" t)
  2871. (const :tag "Expert" expert)))
  2872. (defvar org-fast-tag-selection-include-todo nil
  2873. "Non-nil means fast tags selection interface will also offer TODO states.
  2874. This is an undocumented feature, you should not rely on it.")
  2875. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2876. "The column to which tags should be indented in a headline.
  2877. If this number is positive, it specifies the column. If it is negative,
  2878. it means that the tags should be flushright to that column. For example,
  2879. -80 works well for a normal 80 character screen.
  2880. When 0, place tags directly after headline text, with only one space in
  2881. between."
  2882. :group 'org-tags
  2883. :type 'integer)
  2884. (defcustom org-auto-align-tags t
  2885. "Non-nil keeps tags aligned when modifying headlines.
  2886. Some operations (i.e. demoting) change the length of a headline and
  2887. therefore shift the tags around. With this option turned on, after
  2888. each such operation the tags are again aligned to `org-tags-column'."
  2889. :group 'org-tags
  2890. :type 'boolean)
  2891. (defcustom org-use-tag-inheritance t
  2892. "Non-nil means tags in levels apply also for sublevels.
  2893. When nil, only the tags directly given in a specific line apply there.
  2894. This may also be a list of tags that should be inherited, or a regexp that
  2895. matches tags that should be inherited. Additional control is possible
  2896. with the variable `org-tags-exclude-from-inheritance' which gives an
  2897. explicit list of tags to be excluded from inheritance, even if the value of
  2898. `org-use-tag-inheritance' would select it for inheritance.
  2899. If this option is t, a match early-on in a tree can lead to a large
  2900. number of matches in the subtree when constructing the agenda or creating
  2901. a sparse tree. If you only want to see the first match in a tree during
  2902. a search, check out the variable `org-tags-match-list-sublevels'."
  2903. :group 'org-tags
  2904. :type '(choice
  2905. (const :tag "Not" nil)
  2906. (const :tag "Always" t)
  2907. (repeat :tag "Specific tags" (string :tag "Tag"))
  2908. (regexp :tag "Tags matched by regexp")))
  2909. (defcustom org-tags-exclude-from-inheritance nil
  2910. "List of tags that should never be inherited.
  2911. This is a way to exclude a few tags from inheritance. For way to do
  2912. the opposite, to actively allow inheritance for selected tags,
  2913. see the variable `org-use-tag-inheritance'."
  2914. :group 'org-tags
  2915. :type '(repeat (string :tag "Tag")))
  2916. (defun org-tag-inherit-p (tag)
  2917. "Check if TAG is one that should be inherited."
  2918. (cond
  2919. ((member tag org-tags-exclude-from-inheritance) nil)
  2920. ((eq org-use-tag-inheritance t) t)
  2921. ((not org-use-tag-inheritance) nil)
  2922. ((stringp org-use-tag-inheritance)
  2923. (string-match org-use-tag-inheritance tag))
  2924. ((listp org-use-tag-inheritance)
  2925. (member tag org-use-tag-inheritance))
  2926. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2927. (defcustom org-tags-match-list-sublevels t
  2928. "Non-nil means list also sublevels of headlines matching a search.
  2929. This variable applies to tags/property searches, and also to stuck
  2930. projects because this search is based on a tags match as well.
  2931. When set to the symbol `indented', sublevels are indented with
  2932. leading dots.
  2933. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2934. the sublevels of a headline matching a tag search often also match
  2935. the same search. Listing all of them can create very long lists.
  2936. Setting this variable to nil causes subtrees of a match to be skipped.
  2937. This variable is semi-obsolete and probably should always be true. It
  2938. is better to limit inheritance to certain tags using the variables
  2939. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2940. :group 'org-tags
  2941. :type '(choice
  2942. (const :tag "No, don't list them" nil)
  2943. (const :tag "Yes, do list them" t)
  2944. (const :tag "List them, indented with leading dots" indented)))
  2945. (defcustom org-tags-sort-function nil
  2946. "When set, tags are sorted using this function as a comparator."
  2947. :group 'org-tags
  2948. :type '(choice
  2949. (const :tag "No sorting" nil)
  2950. (const :tag "Alphabetical" string<)
  2951. (const :tag "Reverse alphabetical" string>)
  2952. (function :tag "Custom function" nil)))
  2953. (defvar org-tags-history nil
  2954. "History of minibuffer reads for tags.")
  2955. (defvar org-last-tags-completion-table nil
  2956. "The last used completion table for tags.")
  2957. (defvar org-after-tags-change-hook nil
  2958. "Hook that is run after the tags in a line have changed.")
  2959. (defgroup org-properties nil
  2960. "Options concerning properties in Org-mode."
  2961. :tag "Org Properties"
  2962. :group 'org)
  2963. (defcustom org-property-format "%-10s %s"
  2964. "How property key/value pairs should be formatted by `indent-line'.
  2965. When `indent-line' hits a property definition, it will format the line
  2966. according to this format, mainly to make sure that the values are
  2967. lined-up with respect to each other."
  2968. :group 'org-properties
  2969. :type 'string)
  2970. (defcustom org-properties-postprocess-alist nil
  2971. "Alist of properties and functions to adjust inserted values.
  2972. Elements of this alist must be of the form
  2973. ([string] [function])
  2974. where [string] must be a property name and [function] must be a
  2975. lambda expression: this lambda expression must take one argument,
  2976. the value to adjust, and return the new value as a string.
  2977. For example, this element will allow the property \"Remaining\"
  2978. to be updated wrt the relation between the \"Effort\" property
  2979. and the clock summary:
  2980. ((\"Remaining\" (lambda(value)
  2981. (let ((clocksum (org-clock-sum-current-item))
  2982. (effort (org-duration-string-to-minutes
  2983. (org-entry-get (point) \"Effort\"))))
  2984. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2985. :group 'org-properties
  2986. :version "24.1"
  2987. :type '(alist :key-type (string :tag "Property")
  2988. :value-type (function :tag "Function")))
  2989. (defcustom org-use-property-inheritance nil
  2990. "Non-nil means properties apply also for sublevels.
  2991. This setting is chiefly used during property searches. Turning it on can
  2992. cause significant overhead when doing a search, which is why it is not
  2993. on by default.
  2994. When nil, only the properties directly given in the current entry count.
  2995. When t, every property is inherited. The value may also be a list of
  2996. properties that should have inheritance, or a regular expression matching
  2997. properties that should be inherited.
  2998. However, note that some special properties use inheritance under special
  2999. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3000. and the properties ending in \"_ALL\" when they are used as descriptor
  3001. for valid values of a property.
  3002. Note for programmers:
  3003. When querying an entry with `org-entry-get', you can control if inheritance
  3004. should be used. By default, `org-entry-get' looks only at the local
  3005. properties. You can request inheritance by setting the inherit argument
  3006. to t (to force inheritance) or to `selective' (to respect the setting
  3007. in this variable)."
  3008. :group 'org-properties
  3009. :type '(choice
  3010. (const :tag "Not" nil)
  3011. (const :tag "Always" t)
  3012. (repeat :tag "Specific properties" (string :tag "Property"))
  3013. (regexp :tag "Properties matched by regexp")))
  3014. (defun org-property-inherit-p (property)
  3015. "Check if PROPERTY is one that should be inherited."
  3016. (cond
  3017. ((eq org-use-property-inheritance t) t)
  3018. ((not org-use-property-inheritance) nil)
  3019. ((stringp org-use-property-inheritance)
  3020. (string-match org-use-property-inheritance property))
  3021. ((listp org-use-property-inheritance)
  3022. (member property org-use-property-inheritance))
  3023. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3024. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3025. "The default column format, if no other format has been defined.
  3026. This variable can be set on the per-file basis by inserting a line
  3027. #+COLUMNS: %25ITEM ....."
  3028. :group 'org-properties
  3029. :type 'string)
  3030. (defcustom org-columns-ellipses ".."
  3031. "The ellipses to be used when a field in column view is truncated.
  3032. When this is the empty string, as many characters as possible are shown,
  3033. but then there will be no visual indication that the field has been truncated.
  3034. When this is a string of length N, the last N characters of a truncated
  3035. field are replaced by this string. If the column is narrower than the
  3036. ellipses string, only part of the ellipses string will be shown."
  3037. :group 'org-properties
  3038. :type 'string)
  3039. (defcustom org-columns-modify-value-for-display-function nil
  3040. "Function that modifies values for display in column view.
  3041. For example, it can be used to cut out a certain part from a time stamp.
  3042. The function must take 2 arguments:
  3043. column-title The title of the column (*not* the property name)
  3044. value The value that should be modified.
  3045. The function should return the value that should be displayed,
  3046. or nil if the normal value should be used."
  3047. :group 'org-properties
  3048. :type 'function)
  3049. (defcustom org-effort-property "Effort"
  3050. "The property that is being used to keep track of effort estimates.
  3051. Effort estimates given in this property need to have the format H:MM."
  3052. :group 'org-properties
  3053. :group 'org-progress
  3054. :type '(string :tag "Property"))
  3055. (defconst org-global-properties-fixed
  3056. '(("VISIBILITY_ALL" . "folded children content all")
  3057. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3058. "List of property/value pairs that can be inherited by any entry.
  3059. These are fixed values, for the preset properties. The user variable
  3060. that can be used to add to this list is `org-global-properties'.
  3061. The entries in this list are cons cells where the car is a property
  3062. name and cdr is a string with the value. If the value represents
  3063. multiple items like an \"_ALL\" property, separate the items by
  3064. spaces.")
  3065. (defcustom org-global-properties nil
  3066. "List of property/value pairs that can be inherited by any entry.
  3067. This list will be combined with the constant `org-global-properties-fixed'.
  3068. The entries in this list are cons cells where the car is a property
  3069. name and cdr is a string with the value.
  3070. You can set buffer-local values for the same purpose in the variable
  3071. `org-file-properties' this by adding lines like
  3072. #+PROPERTY: NAME VALUE"
  3073. :group 'org-properties
  3074. :type '(repeat
  3075. (cons (string :tag "Property")
  3076. (string :tag "Value"))))
  3077. (defvar org-file-properties nil
  3078. "List of property/value pairs that can be inherited by any entry.
  3079. Valid for the current buffer.
  3080. This variable is populated from #+PROPERTY lines.")
  3081. (make-variable-buffer-local 'org-file-properties)
  3082. (defgroup org-agenda nil
  3083. "Options concerning agenda views in Org-mode."
  3084. :tag "Org Agenda"
  3085. :group 'org)
  3086. (defvar org-category nil
  3087. "Variable used by org files to set a category for agenda display.
  3088. Such files should use a file variable to set it, for example
  3089. # -*- mode: org; org-category: \"ELisp\"
  3090. or contain a special line
  3091. #+CATEGORY: ELisp
  3092. If the file does not specify a category, then file's base name
  3093. is used instead.")
  3094. (make-variable-buffer-local 'org-category)
  3095. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3096. (defcustom org-agenda-files nil
  3097. "The files to be used for agenda display.
  3098. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3099. \\[org-remove-file]. You can also use customize to edit the list.
  3100. If an entry is a directory, all files in that directory that are matched by
  3101. `org-agenda-file-regexp' will be part of the file list.
  3102. If the value of the variable is not a list but a single file name, then
  3103. the list of agenda files is actually stored and maintained in that file, one
  3104. agenda file per line. In this file paths can be given relative to
  3105. `org-directory'. Tilde expansion and environment variable substitution
  3106. are also made."
  3107. :group 'org-agenda
  3108. :type '(choice
  3109. (repeat :tag "List of files and directories" file)
  3110. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3111. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3112. "Regular expression to match files for `org-agenda-files'.
  3113. If any element in the list in that variable contains a directory instead
  3114. of a normal file, all files in that directory that are matched by this
  3115. regular expression will be included."
  3116. :group 'org-agenda
  3117. :type 'regexp)
  3118. (defcustom org-agenda-text-search-extra-files nil
  3119. "List of extra files to be searched by text search commands.
  3120. These files will be search in addition to the agenda files by the
  3121. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3122. Note that these files will only be searched for text search commands,
  3123. not for the other agenda views like todo lists, tag searches or the weekly
  3124. agenda. This variable is intended to list notes and possibly archive files
  3125. that should also be searched by these two commands.
  3126. In fact, if the first element in the list is the symbol `agenda-archives',
  3127. than all archive files of all agenda files will be added to the search
  3128. scope."
  3129. :group 'org-agenda
  3130. :type '(set :greedy t
  3131. (const :tag "Agenda Archives" agenda-archives)
  3132. (repeat :inline t (file))))
  3133. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3134. 'org-agenda-text-search-extra-files)
  3135. (defcustom org-agenda-skip-unavailable-files nil
  3136. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3137. A nil value means to remove them, after a query, from the list."
  3138. :group 'org-agenda
  3139. :type 'boolean)
  3140. (defcustom org-calendar-to-agenda-key [?c]
  3141. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3142. The command `org-calendar-goto-agenda' will be bound to this key. The
  3143. default is the character `c' because then `c' can be used to switch back and
  3144. forth between agenda and calendar."
  3145. :group 'org-agenda
  3146. :type 'sexp)
  3147. (defcustom org-calendar-insert-diary-entry-key [?i]
  3148. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3149. This option is irrelevant until `org-agenda-diary-file' has been configured
  3150. to point to an Org-mode file. When that is the case, the command
  3151. `org-agenda-diary-entry' will be bound to the key given here, by default
  3152. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3153. if you want to continue doing this, you need to change this to a different
  3154. key."
  3155. :group 'org-agenda
  3156. :type 'sexp)
  3157. (defcustom org-agenda-diary-file 'diary-file
  3158. "File to which to add new entries with the `i' key in agenda and calendar.
  3159. When this is the symbol `diary-file', the functionality in the Emacs
  3160. calendar will be used to add entries to the `diary-file'. But when this
  3161. points to a file, `org-agenda-diary-entry' will be used instead."
  3162. :group 'org-agenda
  3163. :type '(choice
  3164. (const :tag "The standard Emacs diary file" diary-file)
  3165. (file :tag "Special Org file diary entries")))
  3166. (eval-after-load "calendar"
  3167. '(progn
  3168. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3169. 'org-calendar-goto-agenda)
  3170. (add-hook 'calendar-mode-hook
  3171. (lambda ()
  3172. (unless (eq org-agenda-diary-file 'diary-file)
  3173. (define-key calendar-mode-map
  3174. org-calendar-insert-diary-entry-key
  3175. 'org-agenda-diary-entry))))))
  3176. (defgroup org-latex nil
  3177. "Options for embedding LaTeX code into Org-mode."
  3178. :tag "Org LaTeX"
  3179. :group 'org)
  3180. (defcustom org-format-latex-options
  3181. '(:foreground default :background default :scale 1.0
  3182. :html-foreground "Black" :html-background "Transparent"
  3183. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3184. "Options for creating images from LaTeX fragments.
  3185. This is a property list with the following properties:
  3186. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3187. `default' means use the foreground of the default face.
  3188. `auto' means use the foreground from the text face.
  3189. :background the background color, or \"Transparent\".
  3190. `default' means use the background of the default face.
  3191. `auto' means use the background from the text face.
  3192. :scale a scaling factor for the size of the images, to get more pixels
  3193. :html-foreground, :html-background, :html-scale
  3194. the same numbers for HTML export.
  3195. :matchers a list indicating which matchers should be used to
  3196. find LaTeX fragments. Valid members of this list are:
  3197. \"begin\" find environments
  3198. \"$1\" find single characters surrounded by $.$
  3199. \"$\" find math expressions surrounded by $...$
  3200. \"$$\" find math expressions surrounded by $$....$$
  3201. \"\\(\" find math expressions surrounded by \\(...\\)
  3202. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3203. :group 'org-latex
  3204. :type 'plist)
  3205. (defcustom org-format-latex-signal-error t
  3206. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3207. When nil, just push out a message."
  3208. :group 'org-latex
  3209. :version "24.1"
  3210. :type 'boolean)
  3211. (defcustom org-latex-to-mathml-jar-file nil
  3212. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3213. Use this to specify additional executable file say a jar file.
  3214. When using MathToWeb as the converter, specify the full-path to
  3215. your mathtoweb.jar file."
  3216. :group 'org-latex
  3217. :version "24.1"
  3218. :type '(choice
  3219. (const :tag "None" nil)
  3220. (file :tag "JAR file" :must-match t)))
  3221. (defcustom org-latex-to-mathml-convert-command nil
  3222. "Command to convert LaTeX fragments to MathML.
  3223. Replace format-specifiers in the command as noted below and use
  3224. `shell-command' to convert LaTeX to MathML.
  3225. %j: Executable file in fully expanded form as specified by
  3226. `org-latex-to-mathml-jar-file'.
  3227. %I: Input LaTeX file in fully expanded form
  3228. %o: Output MathML file
  3229. This command is used by `org-create-math-formula'.
  3230. When using MathToWeb as the converter, set this to
  3231. \"java -jar %j -unicode -force -df %o %I\"."
  3232. :group 'org-latex
  3233. :version "24.1"
  3234. :type '(choice
  3235. (const :tag "None" nil)
  3236. (string :tag "\nShell command")))
  3237. (defcustom org-latex-create-formula-image-program 'dvipng
  3238. "Program to convert LaTeX fragments with.
  3239. dvipng Process the LaTeX fragments to dvi file, then convert
  3240. dvi files to png files using dvipng.
  3241. This will also include processing of non-math environments.
  3242. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3243. to convert pdf files to png files"
  3244. :group 'org-latex
  3245. :version "24.1"
  3246. :type '(choice
  3247. (const :tag "dvipng" dvipng)
  3248. (const :tag "imagemagick" imagemagick)))
  3249. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3250. "Path to store latex preview images.
  3251. A relative path here creates many directories relative to the
  3252. processed org files paths. An absolute path puts all preview
  3253. images at the same place."
  3254. :group 'org-latex
  3255. :version "24.3"
  3256. :type 'string)
  3257. (defun org-format-latex-mathml-available-p ()
  3258. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3259. (save-match-data
  3260. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3261. org-latex-to-mathml-convert-command)
  3262. (let ((executable (car (split-string
  3263. org-latex-to-mathml-convert-command))))
  3264. (when (executable-find executable)
  3265. (if (string-match
  3266. "%j" org-latex-to-mathml-convert-command)
  3267. (file-readable-p org-latex-to-mathml-jar-file)
  3268. t))))))
  3269. (defcustom org-format-latex-header "\\documentclass{article}
  3270. \\usepackage[usenames]{color}
  3271. \\usepackage{amsmath}
  3272. \\usepackage[mathscr]{eucal}
  3273. \\pagestyle{empty} % do not remove
  3274. \[PACKAGES]
  3275. \[DEFAULT-PACKAGES]
  3276. % The settings below are copied from fullpage.sty
  3277. \\setlength{\\textwidth}{\\paperwidth}
  3278. \\addtolength{\\textwidth}{-3cm}
  3279. \\setlength{\\oddsidemargin}{1.5cm}
  3280. \\addtolength{\\oddsidemargin}{-2.54cm}
  3281. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3282. \\setlength{\\textheight}{\\paperheight}
  3283. \\addtolength{\\textheight}{-\\headheight}
  3284. \\addtolength{\\textheight}{-\\headsep}
  3285. \\addtolength{\\textheight}{-\\footskip}
  3286. \\addtolength{\\textheight}{-3cm}
  3287. \\setlength{\\topmargin}{1.5cm}
  3288. \\addtolength{\\topmargin}{-2.54cm}"
  3289. "The document header used for processing LaTeX fragments.
  3290. It is imperative that this header make sure that no page number
  3291. appears on the page. The package defined in the variables
  3292. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3293. will either replace the placeholder \"[PACKAGES]\" in this
  3294. header, or they will be appended."
  3295. :group 'org-latex
  3296. :type 'string)
  3297. (defun org-set-packages-alist (var val)
  3298. "Set the packages alist and make sure it has 3 elements per entry."
  3299. (set var (mapcar (lambda (x)
  3300. (if (and (consp x) (= (length x) 2))
  3301. (list (car x) (nth 1 x) t)
  3302. x))
  3303. val)))
  3304. (defun org-get-packages-alist (var)
  3305. "Get the packages alist and make sure it has 3 elements per entry."
  3306. (mapcar (lambda (x)
  3307. (if (and (consp x) (= (length x) 2))
  3308. (list (car x) (nth 1 x) t)
  3309. x))
  3310. (default-value var)))
  3311. (defcustom org-latex-default-packages-alist
  3312. '(("AUTO" "inputenc" t)
  3313. ("T1" "fontenc" t)
  3314. ("" "fixltx2e" nil)
  3315. ("" "graphicx" t)
  3316. ("" "longtable" nil)
  3317. ("" "float" nil)
  3318. ("" "wrapfig" nil)
  3319. ("" "soul" t)
  3320. ("" "textcomp" t)
  3321. ("" "marvosym" t)
  3322. ("" "wasysym" t)
  3323. ("" "latexsym" t)
  3324. ("" "amssymb" t)
  3325. ("" "hyperref" nil)
  3326. "\\tolerance=1000")
  3327. "Alist of default packages to be inserted in the header.
  3328. Change this only if one of the packages here causes an
  3329. incompatibility with another package you are using.
  3330. The packages in this list are needed by one part or another of
  3331. Org mode to function properly:
  3332. - inputenc, fontenc: for basic font and character selection
  3333. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3334. symbols used for interpreting the entities in `org-entities'.
  3335. You can skip some of these packages if you don't use any of the
  3336. symbols in it.
  3337. - graphicx: for including images
  3338. - float, wrapfig: for figure placement
  3339. - longtable: for long tables
  3340. - hyperref: for cross references
  3341. Therefore you should not modify this variable unless you know
  3342. what you are doing. The one reason to change it anyway is that
  3343. you might be loading some other package that conflicts with one
  3344. of the default packages. Each cell is of the format
  3345. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3346. the package also needs to be included when compiling LaTeX
  3347. snippets into images for inclusion into non-LaTeX output."
  3348. :group 'org-latex
  3349. :group 'org-export-latex
  3350. :set 'org-set-packages-alist
  3351. :get 'org-get-packages-alist
  3352. :version "24.1"
  3353. :type '(repeat
  3354. (choice
  3355. (list :tag "options/package pair"
  3356. (string :tag "options")
  3357. (string :tag "package")
  3358. (boolean :tag "Snippet"))
  3359. (string :tag "A line of LaTeX"))))
  3360. (defcustom org-latex-packages-alist nil
  3361. "Alist of packages to be inserted in every LaTeX header.
  3362. These will be inserted after `org-latex-default-packages-alist'.
  3363. Each cell is of the format:
  3364. \(\"options\" \"package\" snippet-flag)
  3365. SNIPPET-FLAG, when t, indicates that this package is also needed
  3366. when turning LaTeX snippets into images for inclusion into
  3367. non-LaTeX output.
  3368. Make sure that you only list packages here which:
  3369. - you want in every file
  3370. - do not conflict with the setup in `org-format-latex-header'.
  3371. - do not conflict with the default packages in
  3372. `org-latex-default-packages-alist'."
  3373. :group 'org-latex
  3374. :group 'org-export-latex
  3375. :set 'org-set-packages-alist
  3376. :get 'org-get-packages-alist
  3377. :type '(repeat
  3378. (choice
  3379. (list :tag "options/package pair"
  3380. (string :tag "options")
  3381. (string :tag "package")
  3382. (boolean :tag "Snippet"))
  3383. (string :tag "A line of LaTeX"))))
  3384. (defgroup org-appearance nil
  3385. "Settings for Org-mode appearance."
  3386. :tag "Org Appearance"
  3387. :group 'org)
  3388. (defcustom org-level-color-stars-only nil
  3389. "Non-nil means fontify only the stars in each headline.
  3390. When nil, the entire headline is fontified.
  3391. Changing it requires restart of `font-lock-mode' to become effective
  3392. also in regions already fontified."
  3393. :group 'org-appearance
  3394. :type 'boolean)
  3395. (defcustom org-hide-leading-stars nil
  3396. "Non-nil means hide the first N-1 stars in a headline.
  3397. This works by using the face `org-hide' for these stars. This
  3398. face is white for a light background, and black for a dark
  3399. background. You may have to customize the face `org-hide' to
  3400. make this work.
  3401. Changing it requires restart of `font-lock-mode' to become effective
  3402. also in regions already fontified.
  3403. You may also set this on a per-file basis by adding one of the following
  3404. lines to the buffer:
  3405. #+STARTUP: hidestars
  3406. #+STARTUP: showstars"
  3407. :group 'org-appearance
  3408. :type 'boolean)
  3409. (defcustom org-hidden-keywords nil
  3410. "List of symbols corresponding to keywords to be hidden the org buffer.
  3411. For example, a value '(title) for this list will make the document's title
  3412. appear in the buffer without the initial #+TITLE: keyword."
  3413. :group 'org-appearance
  3414. :version "24.1"
  3415. :type '(set (const :tag "#+AUTHOR" author)
  3416. (const :tag "#+DATE" date)
  3417. (const :tag "#+EMAIL" email)
  3418. (const :tag "#+TITLE" title)))
  3419. (defcustom org-custom-properties nil
  3420. "List of properties (as strings) with a special meaning.
  3421. The default use of these custom properties is to let the user
  3422. hide them with `org-toggle-custom-properties-visibility'."
  3423. :group 'org-properties
  3424. :group 'org-appearance
  3425. :version "24.3"
  3426. :type '(repeat (string :tag "Property Name")))
  3427. (defcustom org-fontify-done-headline nil
  3428. "Non-nil means change the face of a headline if it is marked DONE.
  3429. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3430. When this is non-nil, the headline after the keyword is set to the
  3431. `org-headline-done' as an additional indication."
  3432. :group 'org-appearance
  3433. :type 'boolean)
  3434. (defcustom org-fontify-emphasized-text t
  3435. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3436. Changing this variable requires a restart of Emacs to take effect."
  3437. :group 'org-appearance
  3438. :type 'boolean)
  3439. (defcustom org-fontify-whole-heading-line nil
  3440. "Non-nil means fontify the whole line for headings.
  3441. This is useful when setting a background color for the
  3442. org-level-* faces."
  3443. :group 'org-appearance
  3444. :type 'boolean)
  3445. (defcustom org-highlight-latex-and-related nil
  3446. "Non-nil means highlight LaTeX related syntax in the buffer.
  3447. When non nil, the value should be a list containing any of the
  3448. following symbols:
  3449. `latex' Highlight LaTeX snippets and environments.
  3450. `script' Highlight subscript and superscript.
  3451. `entities' Highlight entities."
  3452. :group 'org-appearance
  3453. :version "24.4"
  3454. :package-version '(Org . "8.0")
  3455. :type '(choice
  3456. (const :tag "No highlighting" nil)
  3457. (set :greedy t :tag "Highlight"
  3458. (const :tag "LaTeX snippets and environments" latex)
  3459. (const :tag "Subscript and superscript" script)
  3460. (const :tag "Entities" entities))))
  3461. (defcustom org-hide-emphasis-markers nil
  3462. "Non-nil mean font-lock should hide the emphasis marker characters."
  3463. :group 'org-appearance
  3464. :type 'boolean)
  3465. (defcustom org-pretty-entities nil
  3466. "Non-nil means show entities as UTF8 characters.
  3467. When nil, the \\name form remains in the buffer."
  3468. :group 'org-appearance
  3469. :version "24.1"
  3470. :type 'boolean)
  3471. (defcustom org-pretty-entities-include-sub-superscripts t
  3472. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3473. :group 'org-appearance
  3474. :version "24.1"
  3475. :type 'boolean)
  3476. (defvar org-emph-re nil
  3477. "Regular expression for matching emphasis.
  3478. After a match, the match groups contain these elements:
  3479. 0 The match of the full regular expression, including the characters
  3480. before and after the proper match
  3481. 1 The character before the proper match, or empty at beginning of line
  3482. 2 The proper match, including the leading and trailing markers
  3483. 3 The leading marker like * or /, indicating the type of highlighting
  3484. 4 The text between the emphasis markers, not including the markers
  3485. 5 The character after the match, empty at the end of a line")
  3486. (defvar org-verbatim-re nil
  3487. "Regular expression for matching verbatim text.")
  3488. (defvar org-emphasis-regexp-components) ; defined just below
  3489. (defvar org-emphasis-alist) ; defined just below
  3490. (defun org-set-emph-re (var val)
  3491. "Set variable and compute the emphasis regular expression."
  3492. (set var val)
  3493. (when (and (boundp 'org-emphasis-alist)
  3494. (boundp 'org-emphasis-regexp-components)
  3495. org-emphasis-alist org-emphasis-regexp-components)
  3496. (let* ((e org-emphasis-regexp-components)
  3497. (pre (car e))
  3498. (post (nth 1 e))
  3499. (border (nth 2 e))
  3500. (body (nth 3 e))
  3501. (nl (nth 4 e))
  3502. (body1 (concat body "*?"))
  3503. (markers (mapconcat 'car org-emphasis-alist ""))
  3504. (vmarkers (mapconcat
  3505. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3506. org-emphasis-alist "")))
  3507. ;; make sure special characters appear at the right position in the class
  3508. (if (string-match "\\^" markers)
  3509. (setq markers (concat (replace-match "" t t markers) "^")))
  3510. (if (string-match "-" markers)
  3511. (setq markers (concat (replace-match "" t t markers) "-")))
  3512. (if (string-match "\\^" vmarkers)
  3513. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3514. (if (string-match "-" vmarkers)
  3515. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3516. (if (> nl 0)
  3517. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3518. (int-to-string nl) "\\}")))
  3519. ;; Make the regexp
  3520. (setq org-emph-re
  3521. (concat "\\([" pre "]\\|^\\)"
  3522. "\\("
  3523. "\\([" markers "]\\)"
  3524. "\\("
  3525. "[^" border "]\\|"
  3526. "[^" border "]"
  3527. body1
  3528. "[^" border "]"
  3529. "\\)"
  3530. "\\3\\)"
  3531. "\\([" post "]\\|$\\)"))
  3532. (setq org-verbatim-re
  3533. (concat "\\([" pre "]\\|^\\)"
  3534. "\\("
  3535. "\\([" vmarkers "]\\)"
  3536. "\\("
  3537. "[^" border "]\\|"
  3538. "[^" border "]"
  3539. body1
  3540. "[^" border "]"
  3541. "\\)"
  3542. "\\3\\)"
  3543. "\\([" post "]\\|$\\)")))))
  3544. (defcustom org-emphasis-regexp-components
  3545. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3546. "Components used to build the regular expression for emphasis.
  3547. This is a list with five entries. Terminology: In an emphasis string
  3548. like \" *strong word* \", we call the initial space PREMATCH, the final
  3549. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3550. and \"trong wor\" is the body. The different components in this variable
  3551. specify what is allowed/forbidden in each part:
  3552. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3553. post Chars allowed as postmatch. End of line will be allowed too.
  3554. border The chars *forbidden* as border characters.
  3555. body-regexp A regexp like \".\" to match a body character. Don't use
  3556. non-shy groups here, and don't allow newline here.
  3557. newline The maximum number of newlines allowed in an emphasis exp.
  3558. Use customize to modify this, or restart Emacs after changing it."
  3559. :group 'org-appearance
  3560. :set 'org-set-emph-re
  3561. :type '(list
  3562. (sexp :tag "Allowed chars in pre ")
  3563. (sexp :tag "Allowed chars in post ")
  3564. (sexp :tag "Forbidden chars in border ")
  3565. (sexp :tag "Regexp for body ")
  3566. (integer :tag "number of newlines allowed")
  3567. (option (boolean :tag "Please ignore this button"))))
  3568. (defcustom org-emphasis-alist
  3569. `(("*" bold "<b>" "</b>")
  3570. ("/" italic "<i>" "</i>")
  3571. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3572. ("=" org-code "<code>" "</code>" verbatim)
  3573. ("~" org-verbatim "<code>" "</code>" verbatim)
  3574. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3575. "<del>" "</del>")
  3576. )
  3577. "Special syntax for emphasized text.
  3578. Text starting and ending with a special character will be emphasized, for
  3579. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3580. characters, the face to be used by font-lock for highlighting in Org-mode
  3581. Emacs buffers, and the HTML tags to be used for this.
  3582. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3583. Use customize to modify this, or restart Emacs after changing it."
  3584. :group 'org-appearance
  3585. :set 'org-set-emph-re
  3586. :type '(repeat
  3587. (list
  3588. (string :tag "Marker character")
  3589. (choice
  3590. (face :tag "Font-lock-face")
  3591. (plist :tag "Face property list"))
  3592. (string :tag "HTML start tag")
  3593. (string :tag "HTML end tag")
  3594. (option (const verbatim)))))
  3595. (defvar org-protecting-blocks
  3596. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3597. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3598. This is needed for font-lock setup.")
  3599. ;;; Miscellaneous options
  3600. (defgroup org-completion nil
  3601. "Completion in Org-mode."
  3602. :tag "Org Completion"
  3603. :group 'org)
  3604. (defcustom org-completion-use-ido nil
  3605. "Non-nil means use ido completion wherever possible.
  3606. Note that `ido-mode' must be active for this variable to be relevant.
  3607. If you decide to turn this variable on, you might well want to turn off
  3608. `org-outline-path-complete-in-steps'.
  3609. See also `org-completion-use-iswitchb'."
  3610. :group 'org-completion
  3611. :type 'boolean)
  3612. (defcustom org-completion-use-iswitchb nil
  3613. "Non-nil means use iswitchb completion wherever possible.
  3614. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3615. If you decide to turn this variable on, you might well want to turn off
  3616. `org-outline-path-complete-in-steps'.
  3617. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3618. :group 'org-completion
  3619. :type 'boolean)
  3620. (defcustom org-completion-fallback-command 'hippie-expand
  3621. "The expansion command called by \\[pcomplete] in normal context.
  3622. Normal means, no org-mode-specific context."
  3623. :group 'org-completion
  3624. :type 'function)
  3625. ;;; Functions and variables from their packages
  3626. ;; Declared here to avoid compiler warnings
  3627. ;; XEmacs only
  3628. (defvar outline-mode-menu-heading)
  3629. (defvar outline-mode-menu-show)
  3630. (defvar outline-mode-menu-hide)
  3631. (defvar zmacs-regions) ; XEmacs regions
  3632. ;; Emacs only
  3633. (defvar mark-active)
  3634. ;; Various packages
  3635. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3636. (declare-function calendar-forward-day "cal-move" (arg))
  3637. (declare-function calendar-goto-date "cal-move" (date))
  3638. (declare-function calendar-goto-today "cal-move" ())
  3639. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3640. (defvar calc-embedded-close-formula)
  3641. (defvar calc-embedded-open-formula)
  3642. (declare-function cdlatex-tab "ext:cdlatex" ())
  3643. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3644. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3645. (defvar font-lock-unfontify-region-function)
  3646. (declare-function iswitchb-read-buffer "iswitchb"
  3647. (prompt &optional default require-match start matches-set))
  3648. (defvar iswitchb-temp-buflist)
  3649. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3650. (defvar org-agenda-tags-todo-honor-ignore-options)
  3651. (declare-function org-agenda-skip "org-agenda" ())
  3652. (declare-function
  3653. org-agenda-format-item "org-agenda"
  3654. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3655. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3656. (declare-function org-agenda-change-all-lines "org-agenda"
  3657. (newhead hdmarker &optional fixface just-this))
  3658. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3659. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3660. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3661. (beg end))
  3662. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3663. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3664. "org-agenda" (&optional end))
  3665. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3666. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3667. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3668. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3669. (declare-function org-indent-mode "org-indent" (&optional arg))
  3670. (declare-function parse-time-string "parse-time" (string))
  3671. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3672. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3673. (defvar remember-data-file)
  3674. (defvar texmathp-why)
  3675. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3676. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3677. (defvar org-latex-regexps)
  3678. ;;; Autoload and prepare some org modules
  3679. ;; Some table stuff that needs to be defined here, because it is used
  3680. ;; by the functions setting up org-mode or checking for table context.
  3681. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3682. "Detect an org-type or table-type table.")
  3683. (defconst org-table-line-regexp "^[ \t]*|"
  3684. "Detect an org-type table line.")
  3685. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3686. "Detect an org-type table line.")
  3687. (defconst org-table-hline-regexp "^[ \t]*|-"
  3688. "Detect an org-type table hline.")
  3689. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3690. "Detect a table-type table hline.")
  3691. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3692. "Detect the first line outside a table when searching from within it.
  3693. This works for both table types.")
  3694. ;; Autoload the functions in org-table.el that are needed by functions here.
  3695. (eval-and-compile
  3696. (org-autoload "org-table"
  3697. '(org-table-begin org-table-blank-field org-table-end)))
  3698. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3699. "Detect a #+TBLFM line.")
  3700. ;;;###autoload
  3701. (defun turn-on-orgtbl ()
  3702. "Unconditionally turn on `orgtbl-mode'."
  3703. (require 'org-table)
  3704. (orgtbl-mode 1))
  3705. (defun org-at-table-p (&optional table-type)
  3706. "Return t if the cursor is inside an org-type table.
  3707. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3708. (if org-enable-table-editor
  3709. (save-excursion
  3710. (beginning-of-line 1)
  3711. (looking-at (if table-type org-table-any-line-regexp
  3712. org-table-line-regexp)))
  3713. nil))
  3714. (defsubst org-table-p () (org-at-table-p))
  3715. (defun org-at-table.el-p ()
  3716. "Return t if and only if we are at a table.el table."
  3717. (and (org-at-table-p 'any)
  3718. (save-excursion
  3719. (goto-char (org-table-begin 'any))
  3720. (looking-at org-table1-hline-regexp))))
  3721. (defun org-table-recognize-table.el ()
  3722. "If there is a table.el table nearby, recognize it and move into it."
  3723. (if org-table-tab-recognizes-table.el
  3724. (if (org-at-table.el-p)
  3725. (progn
  3726. (beginning-of-line 1)
  3727. (if (looking-at org-table-dataline-regexp)
  3728. nil
  3729. (if (looking-at org-table1-hline-regexp)
  3730. (progn
  3731. (beginning-of-line 2)
  3732. (if (looking-at org-table-any-border-regexp)
  3733. (beginning-of-line -1)))))
  3734. (if (re-search-forward "|" (org-table-end t) t)
  3735. (progn
  3736. (require 'table)
  3737. (if (table--at-cell-p (point))
  3738. t
  3739. (message "recognizing table.el table...")
  3740. (table-recognize-table)
  3741. (message "recognizing table.el table...done")))
  3742. (error "This should not happen"))
  3743. t)
  3744. nil)
  3745. nil))
  3746. (defun org-at-table-hline-p ()
  3747. "Return t if the cursor is inside a hline in a table."
  3748. (if org-enable-table-editor
  3749. (save-excursion
  3750. (beginning-of-line 1)
  3751. (looking-at org-table-hline-regexp))
  3752. nil))
  3753. (defvar org-table-clean-did-remove-column nil)
  3754. (defun org-table-map-tables (function &optional quietly)
  3755. "Apply FUNCTION to the start of all tables in the buffer."
  3756. (save-excursion
  3757. (save-restriction
  3758. (widen)
  3759. (goto-char (point-min))
  3760. (while (re-search-forward org-table-any-line-regexp nil t)
  3761. (unless quietly
  3762. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3763. (beginning-of-line 1)
  3764. (when (and (looking-at org-table-line-regexp)
  3765. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3766. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3767. (save-excursion (funcall function))
  3768. (or (looking-at org-table-line-regexp)
  3769. (forward-char 1)))
  3770. (re-search-forward org-table-any-border-regexp nil 1))))
  3771. (unless quietly (message "Mapping tables: done")))
  3772. ;; Declare and autoload functions from ox.el and al.
  3773. (declare-function org-export-get-environment "ox"
  3774. (&optional backend subtreep ext-plist))
  3775. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3776. ;; Declare and autoload functions from org-agenda.el
  3777. (eval-and-compile
  3778. (org-autoload "org-agenda"
  3779. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3780. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3781. (declare-function org-clock-update-mode-line "org-clock" ())
  3782. (declare-function org-resolve-clocks "org-clock"
  3783. (&optional also-non-dangling-p prompt last-valid))
  3784. (defun org-at-TBLFM-p (&optional pos)
  3785. "Return t when point (or POS) is in #+TBLFM line."
  3786. (save-excursion
  3787. (let ((pos pos)))
  3788. (goto-char (or pos (point)))
  3789. (beginning-of-line 1)
  3790. (looking-at org-TBLFM-regexp)))
  3791. (defvar org-clock-start-time)
  3792. (defvar org-clock-marker (make-marker)
  3793. "Marker recording the last clock-in.")
  3794. (defvar org-clock-hd-marker (make-marker)
  3795. "Marker recording the last clock-in, but the headline position.")
  3796. (defvar org-clock-heading ""
  3797. "The heading of the current clock entry.")
  3798. (defun org-clock-is-active ()
  3799. "Return non-nil if clock is currently running.
  3800. The return value is actually the clock marker."
  3801. (marker-buffer org-clock-marker))
  3802. (eval-and-compile
  3803. (org-autoload "org-clock" '(org-clock-remove-overlays
  3804. org-clock-update-time-maybe
  3805. org-clocktable-shift)))
  3806. (defun org-check-running-clock ()
  3807. "Check if the current buffer contains the running clock.
  3808. If yes, offer to stop it and to save the buffer with the changes."
  3809. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3810. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3811. (buffer-name))))
  3812. (org-clock-out)
  3813. (when (y-or-n-p "Save changed buffer?")
  3814. (save-buffer))))
  3815. (defun org-clocktable-try-shift (dir n)
  3816. "Check if this line starts a clock table, if yes, shift the time block."
  3817. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3818. (org-clocktable-shift dir n)))
  3819. ;;;###autoload
  3820. (defun org-clock-persistence-insinuate ()
  3821. "Set up hooks for clock persistence."
  3822. (require 'org-clock)
  3823. (add-hook 'org-mode-hook 'org-clock-load)
  3824. (add-hook 'kill-emacs-hook 'org-clock-save))
  3825. ;; Define the variable already here, to make sure we have it.
  3826. (defvar org-indent-mode nil
  3827. "Non-nil if Org-Indent mode is enabled.
  3828. Use the command `org-indent-mode' to change this variable.")
  3829. ;; Autoload archiving code
  3830. ;; The stuff that is needed for cycling and tags has to be defined here.
  3831. (defgroup org-archive nil
  3832. "Options concerning archiving in Org-mode."
  3833. :tag "Org Archive"
  3834. :group 'org-structure)
  3835. (defcustom org-archive-location "%s_archive::"
  3836. "The location where subtrees should be archived.
  3837. The value of this variable is a string, consisting of two parts,
  3838. separated by a double-colon. The first part is a filename and
  3839. the second part is a headline.
  3840. When the filename is omitted, archiving happens in the same file.
  3841. %s in the filename will be replaced by the current file
  3842. name (without the directory part). Archiving to a different file
  3843. is useful to keep archived entries from contributing to the
  3844. Org-mode Agenda.
  3845. The archived entries will be filed as subtrees of the specified
  3846. headline. When the headline is omitted, the subtrees are simply
  3847. filed away at the end of the file, as top-level entries. Also in
  3848. the heading you can use %s to represent the file name, this can be
  3849. useful when using the same archive for a number of different files.
  3850. Here are a few examples:
  3851. \"%s_archive::\"
  3852. If the current file is Projects.org, archive in file
  3853. Projects.org_archive, as top-level trees. This is the default.
  3854. \"::* Archived Tasks\"
  3855. Archive in the current file, under the top-level headline
  3856. \"* Archived Tasks\".
  3857. \"~/org/archive.org::\"
  3858. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3859. \"~/org/archive.org::* From %s\"
  3860. Archive in file ~/org/archive.org (absolute path), under headlines
  3861. \"From FILENAME\" where file name is the current file name.
  3862. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3863. The \"datetree/\" string is special, signifying to archive
  3864. items to the datetree. Items are placed in either the CLOSED
  3865. date of the item, or the current date if there is no CLOSED date.
  3866. The heading will be a subentry to the current date. There doesn't
  3867. need to be a heading, but there always needs to be a slash after
  3868. datetree. For example, to store archived items directly in the
  3869. datetree, use \"~/org/datetree.org::datetree/\".
  3870. \"basement::** Finished Tasks\"
  3871. Archive in file ./basement (relative path), as level 3 trees
  3872. below the level 2 heading \"** Finished Tasks\".
  3873. You may set this option on a per-file basis by adding to the buffer a
  3874. line like
  3875. #+ARCHIVE: basement::** Finished Tasks
  3876. You may also define it locally for a subtree by setting an ARCHIVE property
  3877. in the entry. If such a property is found in an entry, or anywhere up
  3878. the hierarchy, it will be used."
  3879. :group 'org-archive
  3880. :type 'string)
  3881. (defcustom org-archive-tag "ARCHIVE"
  3882. "The tag that marks a subtree as archived.
  3883. An archived subtree does not open during visibility cycling, and does
  3884. not contribute to the agenda listings.
  3885. After changing this, font-lock must be restarted in the relevant buffers to
  3886. get the proper fontification."
  3887. :group 'org-archive
  3888. :group 'org-keywords
  3889. :type 'string)
  3890. (defcustom org-agenda-skip-archived-trees t
  3891. "Non-nil means the agenda will skip any items located in archived trees.
  3892. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3893. variable is no longer recommended, you should leave it at the value t.
  3894. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3895. :group 'org-archive
  3896. :group 'org-agenda-skip
  3897. :type 'boolean)
  3898. (defcustom org-columns-skip-archived-trees t
  3899. "Non-nil means ignore archived trees when creating column view."
  3900. :group 'org-archive
  3901. :group 'org-properties
  3902. :type 'boolean)
  3903. (defcustom org-cycle-open-archived-trees nil
  3904. "Non-nil means `org-cycle' will open archived trees.
  3905. An archived tree is a tree marked with the tag ARCHIVE.
  3906. When nil, archived trees will stay folded. You can still open them with
  3907. normal outline commands like `show-all', but not with the cycling commands."
  3908. :group 'org-archive
  3909. :group 'org-cycle
  3910. :type 'boolean)
  3911. (defcustom org-sparse-tree-open-archived-trees nil
  3912. "Non-nil means sparse tree construction shows matches in archived trees.
  3913. When nil, matches in these trees are highlighted, but the trees are kept in
  3914. collapsed state."
  3915. :group 'org-archive
  3916. :group 'org-sparse-trees
  3917. :type 'boolean)
  3918. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3919. "The default date type when building a sparse tree.
  3920. When this is nil, a date is a scheduled or a deadline timestamp.
  3921. Otherwise, these types are allowed:
  3922. all: all timestamps
  3923. active: only active timestamps (<...>)
  3924. inactive: only inactive timestamps (<...)
  3925. scheduled: only scheduled timestamps
  3926. deadline: only deadline timestamps"
  3927. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3928. (const :tag "All timestamps" all)
  3929. (const :tag "Only active timestamps" active)
  3930. (const :tag "Only inactive timestamps" inactive)
  3931. (const :tag "Only scheduled timestamps" scheduled)
  3932. (const :tag "Only deadline timestamps" deadline)
  3933. (const :tag "Only closed timestamps" closed))
  3934. :version "24.3"
  3935. :group 'org-sparse-trees)
  3936. (defun org-cycle-hide-archived-subtrees (state)
  3937. "Re-hide all archived subtrees after a visibility state change."
  3938. (when (and (not org-cycle-open-archived-trees)
  3939. (not (memq state '(overview folded))))
  3940. (save-excursion
  3941. (let* ((globalp (memq state '(contents all)))
  3942. (beg (if globalp (point-min) (point)))
  3943. (end (if globalp (point-max) (org-end-of-subtree t))))
  3944. (org-hide-archived-subtrees beg end)
  3945. (goto-char beg)
  3946. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3947. (message "%s" (substitute-command-keys
  3948. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3949. (defun org-force-cycle-archived ()
  3950. "Cycle subtree even if it is archived."
  3951. (interactive)
  3952. (setq this-command 'org-cycle)
  3953. (let ((org-cycle-open-archived-trees t))
  3954. (call-interactively 'org-cycle)))
  3955. (defun org-hide-archived-subtrees (beg end)
  3956. "Re-hide all archived subtrees after a visibility state change."
  3957. (save-excursion
  3958. (let* ((re (concat ":" org-archive-tag ":")))
  3959. (goto-char beg)
  3960. (while (re-search-forward re end t)
  3961. (when (org-at-heading-p)
  3962. (org-flag-subtree t)
  3963. (org-end-of-subtree t))))))
  3964. (declare-function outline-end-of-heading "outline" ())
  3965. (declare-function outline-flag-region "outline" (from to flag))
  3966. (defun org-flag-subtree (flag)
  3967. (save-excursion
  3968. (org-back-to-heading t)
  3969. (outline-end-of-heading)
  3970. (outline-flag-region (point)
  3971. (progn (org-end-of-subtree t) (point))
  3972. flag)))
  3973. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3974. (eval-and-compile
  3975. (org-autoload "org-archive"
  3976. '(org-add-archive-files)))
  3977. ;; Autoload Column View Code
  3978. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3979. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3980. (declare-function org-columns-compute "org-colview" (property))
  3981. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3982. '(org-columns-number-to-string
  3983. org-columns-get-format-and-top-level
  3984. org-columns-compute
  3985. org-columns-remove-overlays))
  3986. ;; Autoload ID code
  3987. (declare-function org-id-store-link "org-id")
  3988. (declare-function org-id-locations-load "org-id")
  3989. (declare-function org-id-locations-save "org-id")
  3990. (defvar org-id-track-globally)
  3991. (org-autoload "org-id"
  3992. '(org-id-new
  3993. org-id-copy
  3994. org-id-get-with-outline-path-completion
  3995. org-id-get-with-outline-drilling))
  3996. ;;; Variables for pre-computed regular expressions, all buffer local
  3997. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3998. "Matches first line of a hidden block.")
  3999. (make-variable-buffer-local 'org-drawer-regexp)
  4000. (defvar org-todo-regexp nil
  4001. "Matches any of the TODO state keywords.")
  4002. (make-variable-buffer-local 'org-todo-regexp)
  4003. (defvar org-not-done-regexp nil
  4004. "Matches any of the TODO state keywords except the last one.")
  4005. (make-variable-buffer-local 'org-not-done-regexp)
  4006. (defvar org-not-done-heading-regexp nil
  4007. "Matches a TODO headline that is not done.")
  4008. (make-variable-buffer-local 'org-not-done-regexp)
  4009. (defvar org-todo-line-regexp nil
  4010. "Matches a headline and puts TODO state into group 2 if present.")
  4011. (make-variable-buffer-local 'org-todo-line-regexp)
  4012. (defvar org-complex-heading-regexp nil
  4013. "Matches a headline and puts everything into groups:
  4014. group 1: the stars
  4015. group 2: The todo keyword, maybe
  4016. group 3: Priority cookie
  4017. group 4: True headline
  4018. group 5: Tags")
  4019. (make-variable-buffer-local 'org-complex-heading-regexp)
  4020. (defvar org-complex-heading-regexp-format nil
  4021. "Printf format to make regexp to match an exact headline.
  4022. This regexp will match the headline of any node which has the
  4023. exact headline text that is put into the format, but may have any
  4024. TODO state, priority and tags.")
  4025. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4026. (defvar org-todo-line-tags-regexp nil
  4027. "Matches a headline and puts TODO state into group 2 if present.
  4028. Also put tags into group 4 if tags are present.")
  4029. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4030. (defvar org-ds-keyword-length 12
  4031. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4032. (make-variable-buffer-local 'org-ds-keyword-length)
  4033. (defvar org-deadline-regexp nil
  4034. "Matches the DEADLINE keyword.")
  4035. (make-variable-buffer-local 'org-deadline-regexp)
  4036. (defvar org-deadline-time-regexp nil
  4037. "Matches the DEADLINE keyword together with a time stamp.")
  4038. (make-variable-buffer-local 'org-deadline-time-regexp)
  4039. (defvar org-deadline-time-hour-regexp nil
  4040. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4041. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4042. (defvar org-deadline-line-regexp nil
  4043. "Matches the DEADLINE keyword and the rest of the line.")
  4044. (make-variable-buffer-local 'org-deadline-line-regexp)
  4045. (defvar org-scheduled-regexp nil
  4046. "Matches the SCHEDULED keyword.")
  4047. (make-variable-buffer-local 'org-scheduled-regexp)
  4048. (defvar org-scheduled-time-regexp nil
  4049. "Matches the SCHEDULED keyword together with a time stamp.")
  4050. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4051. (defvar org-scheduled-time-hour-regexp nil
  4052. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4053. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4054. (defvar org-closed-time-regexp nil
  4055. "Matches the CLOSED keyword together with a time stamp.")
  4056. (make-variable-buffer-local 'org-closed-time-regexp)
  4057. (defvar org-keyword-time-regexp nil
  4058. "Matches any of the 4 keywords, together with the time stamp.")
  4059. (make-variable-buffer-local 'org-keyword-time-regexp)
  4060. (defvar org-keyword-time-not-clock-regexp nil
  4061. "Matches any of the 3 keywords, together with the time stamp.")
  4062. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4063. (defvar org-maybe-keyword-time-regexp nil
  4064. "Matches a timestamp, possibly preceded by a keyword.")
  4065. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4066. (defvar org-all-time-keywords nil
  4067. "List of time keywords.")
  4068. (make-variable-buffer-local 'org-all-time-keywords)
  4069. (defconst org-plain-time-of-day-regexp
  4070. (concat
  4071. "\\(\\<[012]?[0-9]"
  4072. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4073. "\\(--?"
  4074. "\\(\\<[012]?[0-9]"
  4075. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4076. "\\)?")
  4077. "Regular expression to match a plain time or time range.
  4078. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4079. groups carry important information:
  4080. 0 the full match
  4081. 1 the first time, range or not
  4082. 8 the second time, if it is a range.")
  4083. (defconst org-plain-time-extension-regexp
  4084. (concat
  4085. "\\(\\<[012]?[0-9]"
  4086. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4087. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4088. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4089. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4090. groups carry important information:
  4091. 0 the full match
  4092. 7 hours of duration
  4093. 9 minutes of duration")
  4094. (defconst org-stamp-time-of-day-regexp
  4095. (concat
  4096. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4097. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4098. "\\(--?"
  4099. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4100. "Regular expression to match a timestamp time or time range.
  4101. After a match, the following groups carry important information:
  4102. 0 the full match
  4103. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4104. 2 the first time, range or not
  4105. 4 the second time, if it is a range.")
  4106. (defconst org-startup-options
  4107. '(("fold" org-startup-folded t)
  4108. ("overview" org-startup-folded t)
  4109. ("nofold" org-startup-folded nil)
  4110. ("showall" org-startup-folded nil)
  4111. ("showeverything" org-startup-folded showeverything)
  4112. ("content" org-startup-folded content)
  4113. ("indent" org-startup-indented t)
  4114. ("noindent" org-startup-indented nil)
  4115. ("hidestars" org-hide-leading-stars t)
  4116. ("showstars" org-hide-leading-stars nil)
  4117. ("odd" org-odd-levels-only t)
  4118. ("oddeven" org-odd-levels-only nil)
  4119. ("align" org-startup-align-all-tables t)
  4120. ("noalign" org-startup-align-all-tables nil)
  4121. ("inlineimages" org-startup-with-inline-images t)
  4122. ("noinlineimages" org-startup-with-inline-images nil)
  4123. ("latexpreview" org-startup-with-latex-preview t)
  4124. ("nolatexpreview" org-startup-with-latex-preview nil)
  4125. ("customtime" org-display-custom-times t)
  4126. ("logdone" org-log-done time)
  4127. ("lognotedone" org-log-done note)
  4128. ("nologdone" org-log-done nil)
  4129. ("lognoteclock-out" org-log-note-clock-out t)
  4130. ("nolognoteclock-out" org-log-note-clock-out nil)
  4131. ("logrepeat" org-log-repeat state)
  4132. ("lognoterepeat" org-log-repeat note)
  4133. ("logdrawer" org-log-into-drawer t)
  4134. ("nologdrawer" org-log-into-drawer nil)
  4135. ("logstatesreversed" org-log-states-order-reversed t)
  4136. ("nologstatesreversed" org-log-states-order-reversed nil)
  4137. ("nologrepeat" org-log-repeat nil)
  4138. ("logreschedule" org-log-reschedule time)
  4139. ("lognotereschedule" org-log-reschedule note)
  4140. ("nologreschedule" org-log-reschedule nil)
  4141. ("logredeadline" org-log-redeadline time)
  4142. ("lognoteredeadline" org-log-redeadline note)
  4143. ("nologredeadline" org-log-redeadline nil)
  4144. ("logrefile" org-log-refile time)
  4145. ("lognoterefile" org-log-refile note)
  4146. ("nologrefile" org-log-refile nil)
  4147. ("fninline" org-footnote-define-inline t)
  4148. ("nofninline" org-footnote-define-inline nil)
  4149. ("fnlocal" org-footnote-section nil)
  4150. ("fnauto" org-footnote-auto-label t)
  4151. ("fnprompt" org-footnote-auto-label nil)
  4152. ("fnconfirm" org-footnote-auto-label confirm)
  4153. ("fnplain" org-footnote-auto-label plain)
  4154. ("fnadjust" org-footnote-auto-adjust t)
  4155. ("nofnadjust" org-footnote-auto-adjust nil)
  4156. ("constcgs" constants-unit-system cgs)
  4157. ("constSI" constants-unit-system SI)
  4158. ("noptag" org-tag-persistent-alist nil)
  4159. ("hideblocks" org-hide-block-startup t)
  4160. ("nohideblocks" org-hide-block-startup nil)
  4161. ("beamer" org-startup-with-beamer-mode t)
  4162. ("entitiespretty" org-pretty-entities t)
  4163. ("entitiesplain" org-pretty-entities nil))
  4164. "Variable associated with STARTUP options for org-mode.
  4165. Each element is a list of three items: the startup options (as written
  4166. in the #+STARTUP line), the corresponding variable, and the value to set
  4167. this variable to if the option is found. An optional forth element PUSH
  4168. means to push this value onto the list in the variable.")
  4169. (defun org-update-property-plist (key val props)
  4170. "Update PROPS with KEY and VAL."
  4171. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4172. (key (if appending (substring key 0 (- (length key) 1)) key))
  4173. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4174. (previous (cdr (assoc key props))))
  4175. (if appending
  4176. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4177. (cons (cons key val) remainder))))
  4178. (defconst org-block-regexp
  4179. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4180. "Regular expression for hiding blocks.")
  4181. (defconst org-heading-keyword-regexp-format
  4182. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4183. "Printf format for a regexp matching a headline with some keyword.
  4184. This regexp will match the headline of any node which has the
  4185. exact keyword that is put into the format. The keyword isn't in
  4186. any group by default, but the stars and the body are.")
  4187. (defconst org-heading-keyword-maybe-regexp-format
  4188. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4189. "Printf format for a regexp matching a headline, possibly with some keyword.
  4190. This regexp can match any headline with the specified keyword, or
  4191. without a keyword. The keyword isn't in any group by default,
  4192. but the stars and the body are.")
  4193. (defcustom org-group-tags t
  4194. "When non-nil (the default), use group tags.
  4195. This can be turned on/off through `org-toggle-tags-groups'."
  4196. :group 'org-tags
  4197. :group 'org-startup
  4198. :type 'boolean)
  4199. (defun org-toggle-tags-groups ()
  4200. "Toggle support for group tags.
  4201. Support for group tags is controlled by the option
  4202. `org-group-tags', which is non-nil by default."
  4203. (interactive)
  4204. (setq org-group-tags (not org-group-tags))
  4205. (cond ((and (derived-mode-p 'org-agenda-mode)
  4206. org-group-tags)
  4207. (org-agenda-redo))
  4208. ((derived-mode-p 'org-mode)
  4209. (let ((org-inhibit-startup t)) (org-mode))))
  4210. (message "Groups tags support has been turned %s"
  4211. (if org-group-tags "on" "off")))
  4212. (defun org-set-regexps-and-options-for-tags ()
  4213. "Precompute regular expressions used for tags in the current buffer."
  4214. (when (derived-mode-p 'org-mode)
  4215. (org-set-local 'org-file-tags nil)
  4216. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4217. (splitre "[ \t]+")
  4218. (start 0)
  4219. tags ftags key value)
  4220. (save-excursion
  4221. (save-restriction
  4222. (widen)
  4223. (goto-char (point-min))
  4224. (while (re-search-forward re nil t)
  4225. (setq key (upcase (org-match-string-no-properties 1))
  4226. value (org-match-string-no-properties 2))
  4227. (if (stringp value) (setq value (org-trim value)))
  4228. (cond
  4229. ((equal key "TAGS")
  4230. (setq tags (append tags (if tags '("\\n") nil)
  4231. (org-split-string value splitre))))
  4232. ((equal key "FILETAGS")
  4233. (when (string-match "\\S-" value)
  4234. (setq ftags
  4235. (append
  4236. ftags
  4237. (apply 'append
  4238. (mapcar (lambda (x) (org-split-string x ":"))
  4239. (org-split-string value)))))))))))
  4240. ;; Process the file tags.
  4241. (and ftags (org-set-local 'org-file-tags
  4242. (mapcar 'org-add-prop-inherited ftags)))
  4243. (org-set-local 'org-tag-groups-alist nil)
  4244. ;; Process the tags.
  4245. ;; FIXME
  4246. (when tags
  4247. (let (e tgs g)
  4248. (while (setq e (pop tags))
  4249. (cond
  4250. ((equal e "{")
  4251. (progn (push '(:startgroup) tgs)
  4252. (when (equal (nth 1 tags) ":")
  4253. (push (list (replace-regexp-in-string
  4254. "(.+)$" "" (nth 0 tags)))
  4255. org-tag-groups-alist)
  4256. (setq g 0))))
  4257. ((equal e ":") (push '(:grouptags) tgs))
  4258. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4259. ((equal e "\\n") (push '(:newline) tgs))
  4260. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4261. (push (cons (match-string 1 e)
  4262. (string-to-char (match-string 2 e))) tgs)
  4263. (if (and g (> g 0))
  4264. (setcar org-tag-groups-alist
  4265. (append (car org-tag-groups-alist)
  4266. (list (match-string 1 e)))))
  4267. (if g (setq g (1+ g))))
  4268. (t (push (list e) tgs)
  4269. (if (and g (> g 0))
  4270. (setcar org-tag-groups-alist
  4271. (append (car org-tag-groups-alist) (list e))))
  4272. (if g (setq g (1+ g))))))
  4273. (org-set-local 'org-tag-alist nil)
  4274. (while (setq e (pop tgs))
  4275. (or (and (stringp (car e))
  4276. (assoc (car e) org-tag-alist))
  4277. (push e org-tag-alist))))))))
  4278. (defun org-set-regexps-and-options ()
  4279. "Precompute regular expressions used in the current buffer."
  4280. (when (derived-mode-p 'org-mode)
  4281. (org-set-local 'org-todo-kwd-alist nil)
  4282. (org-set-local 'org-todo-key-alist nil)
  4283. (org-set-local 'org-todo-key-trigger nil)
  4284. (org-set-local 'org-todo-keywords-1 nil)
  4285. (org-set-local 'org-done-keywords nil)
  4286. (org-set-local 'org-todo-heads nil)
  4287. (org-set-local 'org-todo-sets nil)
  4288. (org-set-local 'org-todo-log-states nil)
  4289. (org-set-local 'org-file-properties nil)
  4290. (let ((re (org-make-options-regexp
  4291. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4292. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4293. "SETUPFILE" "OPTIONS")
  4294. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4295. (splitre "[ \t]+")
  4296. (scripts org-use-sub-superscripts)
  4297. kwds kws0 kwsa key log value cat arch const links hw dws
  4298. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4299. (start 0))
  4300. (save-excursion
  4301. (save-restriction
  4302. (widen)
  4303. (goto-char (point-min))
  4304. (while (or (and ext-setup-or-nil
  4305. (let (ret)
  4306. (with-temp-buffer
  4307. (insert ext-setup-or-nil)
  4308. (let ((major-mode 'org-mode))
  4309. (org-set-regexps-and-options-for-tags)
  4310. (setq ret (list org-file-tags org-tag-alist
  4311. org-tag-groups-alist))))
  4312. (setq org-file-tags (nth 0 ret)
  4313. org-tag-alist (nth 1 ret)
  4314. org-tag-groups-alist (nth 2 ret))))
  4315. (and ext-setup-or-nil
  4316. (string-match re ext-setup-or-nil start)
  4317. (setq start (match-end 0)))
  4318. (and (setq ext-setup-or-nil nil start 0)
  4319. (re-search-forward re nil t)))
  4320. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4321. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4322. (if (stringp value) (setq value (org-trim value)))
  4323. (cond
  4324. ((equal key "CATEGORY")
  4325. (setq cat value))
  4326. ((member key '("SEQ_TODO" "TODO"))
  4327. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4328. ((equal key "TYP_TODO")
  4329. (push (cons 'type (org-split-string value splitre)) kwds))
  4330. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4331. ;; general TODO-like setup
  4332. (push (cons (intern (downcase (match-string 1 key)))
  4333. (org-split-string value splitre)) kwds))
  4334. ((equal key "COLUMNS")
  4335. (org-set-local 'org-columns-default-format value))
  4336. ((equal key "LINK")
  4337. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4338. (push (cons (match-string 1 value)
  4339. (org-trim (match-string 2 value)))
  4340. links)))
  4341. ((equal key "PRIORITIES")
  4342. (setq prio (org-split-string value " +")))
  4343. ((equal key "PROPERTY")
  4344. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4345. (setq props (org-update-property-plist (match-string 1 value)
  4346. (match-string 2 value)
  4347. props))))
  4348. ((equal key "DRAWERS")
  4349. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4350. ((equal key "CONSTANTS")
  4351. (org-table-set-constants))
  4352. ((equal key "STARTUP")
  4353. (let ((opts (org-split-string value splitre))
  4354. l var val)
  4355. (while (setq l (pop opts))
  4356. (when (setq l (assoc l org-startup-options))
  4357. (setq var (nth 1 l) val (nth 2 l))
  4358. (if (not (nth 3 l))
  4359. (set (make-local-variable var) val)
  4360. (if (not (listp (symbol-value var)))
  4361. (set (make-local-variable var) nil))
  4362. (set (make-local-variable var) (symbol-value var))
  4363. (add-to-list var val))))))
  4364. ((equal key "ARCHIVE")
  4365. (setq arch value)
  4366. (remove-text-properties 0 (length arch)
  4367. '(face t fontified t) arch))
  4368. ((equal key "OPTIONS")
  4369. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4370. (setq scripts (read (match-string 2 value)))))
  4371. ((and (equal key "SETUPFILE")
  4372. ;; Prevent checking in Gnus messages
  4373. (not buffer-read-only))
  4374. (setq setup-contents (org-file-contents
  4375. (expand-file-name
  4376. (org-remove-double-quotes value))
  4377. 'noerror))
  4378. (if (not ext-setup-or-nil)
  4379. (setq ext-setup-or-nil setup-contents start 0)
  4380. (setq ext-setup-or-nil
  4381. (concat (substring ext-setup-or-nil 0 start)
  4382. "\n" setup-contents "\n"
  4383. (substring ext-setup-or-nil start)))))))
  4384. ;; search for property blocks
  4385. (goto-char (point-min))
  4386. (while (re-search-forward org-block-regexp nil t)
  4387. (when (equal "PROPERTY" (upcase (match-string 1)))
  4388. (setq value (replace-regexp-in-string
  4389. "[\n\r]" " " (match-string 4)))
  4390. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4391. (setq props (org-update-property-plist (match-string 1 value)
  4392. (match-string 2 value)
  4393. props)))))))
  4394. (org-set-local 'org-use-sub-superscripts scripts)
  4395. (when cat
  4396. (org-set-local 'org-category (intern cat))
  4397. (push (cons "CATEGORY" cat) props))
  4398. (when prio
  4399. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4400. (setq prio (mapcar 'string-to-char prio))
  4401. (org-set-local 'org-highest-priority (nth 0 prio))
  4402. (org-set-local 'org-lowest-priority (nth 1 prio))
  4403. (org-set-local 'org-default-priority (nth 2 prio)))
  4404. (and props (org-set-local 'org-file-properties (nreverse props)))
  4405. (and drawers (org-set-local 'org-drawers drawers))
  4406. (and arch (org-set-local 'org-archive-location arch))
  4407. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4408. ;; Process the TODO keywords
  4409. (unless kwds
  4410. ;; Use the global values as if they had been given locally.
  4411. (setq kwds (default-value 'org-todo-keywords))
  4412. (if (stringp (car kwds))
  4413. (setq kwds (list (cons org-todo-interpretation
  4414. (default-value 'org-todo-keywords)))))
  4415. (setq kwds (reverse kwds)))
  4416. (setq kwds (nreverse kwds))
  4417. (let (inter kws kw)
  4418. (while (setq kws (pop kwds))
  4419. (let ((kws (or
  4420. (run-hook-with-args-until-success
  4421. 'org-todo-setup-filter-hook kws)
  4422. kws)))
  4423. (setq inter (pop kws) sep (member "|" kws)
  4424. kws0 (delete "|" (copy-sequence kws))
  4425. kwsa nil
  4426. kws1 (mapcar
  4427. (lambda (x)
  4428. ;; 1 2
  4429. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4430. (progn
  4431. (setq kw (match-string 1 x)
  4432. key (and (match-end 2) (match-string 2 x))
  4433. log (org-extract-log-state-settings x))
  4434. (push (cons kw (and key (string-to-char key))) kwsa)
  4435. (and log (push log org-todo-log-states))
  4436. kw)
  4437. (error "Invalid TODO keyword %s" x)))
  4438. kws0)
  4439. kwsa (if kwsa (append '((:startgroup))
  4440. (nreverse kwsa)
  4441. '((:endgroup))))
  4442. hw (car kws1)
  4443. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4444. tail (list inter hw (car dws) (org-last dws))))
  4445. (add-to-list 'org-todo-heads hw 'append)
  4446. (push kws1 org-todo-sets)
  4447. (setq org-done-keywords (append org-done-keywords dws nil))
  4448. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4449. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4450. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4451. (setq org-todo-sets (nreverse org-todo-sets)
  4452. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4453. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4454. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4455. ;; Compute the regular expressions and other local variables.
  4456. ;; Using `org-outline-regexp-bol' would complicate them much,
  4457. ;; because of the fixed white space at the end of that string.
  4458. (if (not org-done-keywords)
  4459. (setq org-done-keywords (and org-todo-keywords-1
  4460. (list (org-last org-todo-keywords-1)))))
  4461. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4462. (length org-scheduled-string)
  4463. (length org-clock-string)
  4464. (length org-closed-string)))
  4465. org-drawer-regexp
  4466. (concat "^[ \t]*:\\("
  4467. (mapconcat 'regexp-quote org-drawers "\\|")
  4468. "\\):[ \t]*$")
  4469. org-not-done-keywords
  4470. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4471. org-todo-regexp
  4472. (concat "\\("
  4473. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4474. "\\)")
  4475. org-not-done-regexp
  4476. (concat "\\("
  4477. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4478. "\\)")
  4479. org-not-done-heading-regexp
  4480. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4481. org-todo-line-regexp
  4482. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4483. org-complex-heading-regexp
  4484. (concat "^\\(\\*+\\)"
  4485. "\\(?: +" org-todo-regexp "\\)?"
  4486. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4487. "\\(?: +\\(.*?\\)\\)??"
  4488. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4489. "[ \t]*$")
  4490. org-complex-heading-regexp-format
  4491. (concat "^\\(\\*+\\)"
  4492. "\\(?: +" org-todo-regexp "\\)?"
  4493. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4494. "\\(?: +"
  4495. ;; Stats cookies can be stuck to body.
  4496. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4497. "\\(%s\\)"
  4498. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4499. "\\)"
  4500. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4501. "[ \t]*$")
  4502. org-todo-line-tags-regexp
  4503. (concat "^\\(\\*+\\)"
  4504. "\\(?: +" org-todo-regexp "\\)?"
  4505. "\\(?: +\\(.*?\\)\\)??"
  4506. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4507. "[ \t]*$")
  4508. org-deadline-regexp (concat "\\<" org-deadline-string)
  4509. org-deadline-time-regexp
  4510. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4511. org-deadline-time-hour-regexp
  4512. (concat "\\<" org-deadline-string
  4513. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4514. org-deadline-line-regexp
  4515. (concat "\\<\\(" org-deadline-string "\\).*")
  4516. org-scheduled-regexp
  4517. (concat "\\<" org-scheduled-string)
  4518. org-scheduled-time-regexp
  4519. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4520. org-scheduled-time-hour-regexp
  4521. (concat "\\<" org-scheduled-string
  4522. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4523. org-closed-time-regexp
  4524. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4525. org-keyword-time-regexp
  4526. (concat "\\<\\(" org-scheduled-string
  4527. "\\|" org-deadline-string
  4528. "\\|" org-closed-string
  4529. "\\|" org-clock-string "\\)"
  4530. " *[[<]\\([^]>]+\\)[]>]")
  4531. org-keyword-time-not-clock-regexp
  4532. (concat "\\<\\(" org-scheduled-string
  4533. "\\|" org-deadline-string
  4534. "\\|" org-closed-string
  4535. "\\)"
  4536. " *[[<]\\([^]>]+\\)[]>]")
  4537. org-maybe-keyword-time-regexp
  4538. (concat "\\(\\<\\(" org-scheduled-string
  4539. "\\|" org-deadline-string
  4540. "\\|" org-closed-string
  4541. "\\|" org-clock-string "\\)\\)?"
  4542. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4543. org-all-time-keywords
  4544. (mapcar (lambda (w) (substring w 0 -1))
  4545. (list org-scheduled-string org-deadline-string
  4546. org-clock-string org-closed-string)))
  4547. (org-compute-latex-and-related-regexp)
  4548. (org-set-font-lock-defaults))))
  4549. (defun org-file-contents (file &optional noerror)
  4550. "Return the contents of FILE, as a string."
  4551. (if (or (not file)
  4552. (not (file-readable-p file)))
  4553. (if noerror
  4554. (message "Cannot read file \"%s\"" file)
  4555. (error "Cannot read file \"%s\"" file))
  4556. (with-temp-buffer
  4557. (insert-file-contents file)
  4558. (buffer-string))))
  4559. (defun org-extract-log-state-settings (x)
  4560. "Extract the log state setting from a TODO keyword string.
  4561. This will extract info from a string like \"WAIT(w@/!)\"."
  4562. (let (kw key log1 log2)
  4563. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4564. (setq kw (match-string 1 x)
  4565. key (and (match-end 2) (match-string 2 x))
  4566. log1 (and (match-end 3) (match-string 3 x))
  4567. log2 (and (match-end 4) (match-string 4 x)))
  4568. (and (or log1 log2)
  4569. (list kw
  4570. (and log1 (if (equal log1 "!") 'time 'note))
  4571. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4572. (defun org-remove-keyword-keys (list)
  4573. "Remove a pair of parenthesis at the end of each string in LIST."
  4574. (mapcar (lambda (x)
  4575. (if (string-match "(.*)$" x)
  4576. (substring x 0 (match-beginning 0))
  4577. x))
  4578. list))
  4579. (defun org-assign-fast-keys (alist)
  4580. "Assign fast keys to a keyword-key alist.
  4581. Respect keys that are already there."
  4582. (let (new e (alt ?0))
  4583. (while (setq e (pop alist))
  4584. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4585. (cdr e)) ;; Key already assigned.
  4586. (push e new)
  4587. (let ((clist (string-to-list (downcase (car e))))
  4588. (used (append new alist)))
  4589. (when (= (car clist) ?@)
  4590. (pop clist))
  4591. (while (and clist (rassoc (car clist) used))
  4592. (pop clist))
  4593. (unless clist
  4594. (while (rassoc alt used)
  4595. (incf alt)))
  4596. (push (cons (car e) (or (car clist) alt)) new))))
  4597. (nreverse new)))
  4598. ;;; Some variables used in various places
  4599. (defvar org-window-configuration nil
  4600. "Used in various places to store a window configuration.")
  4601. (defvar org-selected-window nil
  4602. "Used in various places to store a window configuration.")
  4603. (defvar org-finish-function nil
  4604. "Function to be called when `C-c C-c' is used.
  4605. This is for getting out of special buffers like capture.")
  4606. ;; FIXME: Occasionally check by commenting these, to make sure
  4607. ;; no other functions uses these, forgetting to let-bind them.
  4608. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4609. (defvar org-last-state)
  4610. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4611. ;; Defined somewhere in this file, but used before definition.
  4612. (defvar org-entities) ;; defined in org-entities.el
  4613. (defvar org-struct-menu)
  4614. (defvar org-org-menu)
  4615. (defvar org-tbl-menu)
  4616. ;;;; Define the Org-mode
  4617. ;; We use a before-change function to check if a table might need
  4618. ;; an update.
  4619. (defvar org-table-may-need-update t
  4620. "Indicates that a table might need an update.
  4621. This variable is set by `org-before-change-function'.
  4622. `org-table-align' sets it back to nil.")
  4623. (defun org-before-change-function (beg end)
  4624. "Every change indicates that a table might need an update."
  4625. (setq org-table-may-need-update t))
  4626. (defvar org-mode-map)
  4627. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4628. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4629. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4630. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4631. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4632. (defvar org-table-buffer-is-an nil)
  4633. (defvar bidi-paragraph-direction)
  4634. (defvar buffer-face-mode-face)
  4635. (require 'outline)
  4636. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4637. (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"))
  4638. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4639. ;; Other stuff we need.
  4640. (require 'time-date)
  4641. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4642. (require 'easymenu)
  4643. (require 'overlay)
  4644. ;; (require 'org-macs) moved higher up in the file before it is first used
  4645. (require 'org-entities)
  4646. ;; (require 'org-compat) moved higher up in the file before it is first used
  4647. (require 'org-faces)
  4648. (require 'org-list)
  4649. (require 'org-pcomplete)
  4650. (require 'org-src)
  4651. (require 'org-footnote)
  4652. (require 'org-macro)
  4653. ;; babel
  4654. (require 'ob)
  4655. ;;;###autoload
  4656. (define-derived-mode org-mode outline-mode "Org"
  4657. "Outline-based notes management and organizer, alias
  4658. \"Carsten's outline-mode for keeping track of everything.\"
  4659. Org-mode develops organizational tasks around a NOTES file which
  4660. contains information about projects as plain text. Org-mode is
  4661. implemented on top of outline-mode, which is ideal to keep the content
  4662. of large files well structured. It supports ToDo items, deadlines and
  4663. time stamps, which magically appear in the diary listing of the Emacs
  4664. calendar. Tables are easily created with a built-in table editor.
  4665. Plain text URL-like links connect to websites, emails (VM), Usenet
  4666. messages (Gnus), BBDB entries, and any files related to the project.
  4667. For printing and sharing of notes, an Org-mode file (or a part of it)
  4668. can be exported as a structured ASCII or HTML file.
  4669. The following commands are available:
  4670. \\{org-mode-map}"
  4671. ;; Get rid of Outline menus, they are not needed
  4672. ;; Need to do this here because define-derived-mode sets up
  4673. ;; the keymap so late. Still, it is a waste to call this each time
  4674. ;; we switch another buffer into org-mode.
  4675. (if (featurep 'xemacs)
  4676. (when (boundp 'outline-mode-menu-heading)
  4677. ;; Assume this is Greg's port, it uses easymenu
  4678. (easy-menu-remove outline-mode-menu-heading)
  4679. (easy-menu-remove outline-mode-menu-show)
  4680. (easy-menu-remove outline-mode-menu-hide))
  4681. (define-key org-mode-map [menu-bar headings] 'undefined)
  4682. (define-key org-mode-map [menu-bar hide] 'undefined)
  4683. (define-key org-mode-map [menu-bar show] 'undefined))
  4684. (org-load-modules-maybe)
  4685. (easy-menu-add org-org-menu)
  4686. (easy-menu-add org-tbl-menu)
  4687. (org-install-agenda-files-menu)
  4688. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4689. (add-to-invisibility-spec '(org-cwidth))
  4690. (add-to-invisibility-spec '(org-hide-block . t))
  4691. (when (featurep 'xemacs)
  4692. (org-set-local 'line-move-ignore-invisible t))
  4693. (org-set-local 'outline-regexp org-outline-regexp)
  4694. (org-set-local 'outline-level 'org-outline-level)
  4695. (setq bidi-paragraph-direction 'left-to-right)
  4696. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4697. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4698. (when (and org-ellipsis
  4699. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4700. (fboundp 'make-glyph-code))
  4701. (unless org-display-table
  4702. (setq org-display-table (make-display-table)))
  4703. (set-display-table-slot
  4704. org-display-table 4
  4705. (vconcat (mapcar
  4706. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4707. org-ellipsis)))
  4708. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4709. (setq buffer-display-table org-display-table))
  4710. (org-set-regexps-and-options-for-tags)
  4711. (org-set-regexps-and-options)
  4712. (when (and org-tag-faces (not org-tags-special-faces-re))
  4713. ;; tag faces set outside customize.... force initialization.
  4714. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4715. ;; Calc embedded
  4716. (org-set-local 'calc-embedded-open-mode "# ")
  4717. ;; Modify a few syntax entries
  4718. (modify-syntax-entry ?@ "w")
  4719. (modify-syntax-entry ?\" "\"")
  4720. (if org-startup-truncated (setq truncate-lines t))
  4721. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4722. (org-set-local 'font-lock-unfontify-region-function
  4723. 'org-unfontify-region)
  4724. ;; Activate before-change-function
  4725. (org-set-local 'org-table-may-need-update t)
  4726. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4727. 'local)
  4728. ;; Check for running clock before killing a buffer
  4729. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4730. ;; Initialize macros templates.
  4731. (org-macro-initialize-templates)
  4732. ;; Initialize radio targets.
  4733. (org-update-radio-target-regexp)
  4734. ;; Indentation.
  4735. (org-set-local 'indent-line-function 'org-indent-line)
  4736. (org-set-local 'indent-region-function 'org-indent-region)
  4737. ;; Filling and auto-filling.
  4738. (org-setup-filling)
  4739. ;; Comments.
  4740. (org-setup-comments-handling)
  4741. ;; Beginning/end of defun
  4742. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4743. (org-set-local 'end-of-defun-function 'org-forward-element)
  4744. ;; Next error for sparse trees
  4745. (org-set-local 'next-error-function 'org-occur-next-match)
  4746. ;; Make sure dependence stuff works reliably, even for users who set it
  4747. ;; too late :-(
  4748. (if org-enforce-todo-dependencies
  4749. (add-hook 'org-blocker-hook
  4750. 'org-block-todo-from-children-or-siblings-or-parent)
  4751. (remove-hook 'org-blocker-hook
  4752. 'org-block-todo-from-children-or-siblings-or-parent))
  4753. (if org-enforce-todo-checkbox-dependencies
  4754. (add-hook 'org-blocker-hook
  4755. 'org-block-todo-from-checkboxes)
  4756. (remove-hook 'org-blocker-hook
  4757. 'org-block-todo-from-checkboxes))
  4758. ;; Align options lines
  4759. (org-set-local
  4760. 'align-mode-rules-list
  4761. '((org-in-buffer-settings
  4762. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4763. (modes . '(org-mode)))))
  4764. ;; Imenu
  4765. (org-set-local 'imenu-create-index-function
  4766. 'org-imenu-get-tree)
  4767. ;; Make isearch reveal context
  4768. (if (or (featurep 'xemacs)
  4769. (not (boundp 'outline-isearch-open-invisible-function)))
  4770. ;; Emacs 21 and XEmacs make use of the hook
  4771. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4772. ;; Emacs 22 deals with this through a special variable
  4773. (org-set-local 'outline-isearch-open-invisible-function
  4774. (lambda (&rest ignore) (org-show-context 'isearch)))
  4775. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4776. ;; Setup the pcomplete hooks
  4777. (set (make-local-variable 'pcomplete-command-completion-function)
  4778. 'org-pcomplete-initial)
  4779. (set (make-local-variable 'pcomplete-command-name-function)
  4780. 'org-command-at-point)
  4781. (set (make-local-variable 'pcomplete-default-completion-function)
  4782. 'ignore)
  4783. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4784. 'org-parse-arguments)
  4785. (set (make-local-variable 'pcomplete-termination-string) "")
  4786. (when (>= emacs-major-version 23)
  4787. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4788. ;; If empty file that did not turn on org-mode automatically, make it to.
  4789. (if (and org-insert-mode-line-in-empty-file
  4790. (org-called-interactively-p 'any)
  4791. (= (point-min) (point-max)))
  4792. (insert "# -*- mode: org -*-\n\n"))
  4793. (unless org-inhibit-startup
  4794. (org-unmodified
  4795. (and org-startup-with-beamer-mode (org-beamer-mode))
  4796. (when org-startup-align-all-tables
  4797. (org-table-map-tables 'org-table-align 'quietly))
  4798. (when org-startup-with-inline-images
  4799. (org-display-inline-images))
  4800. (when org-startup-with-latex-preview
  4801. (org-preview-latex-fragment))
  4802. (unless org-inhibit-startup-visibility-stuff
  4803. (org-set-startup-visibility))))
  4804. ;; Try to set org-hide correctly
  4805. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4806. ;; Update `customize-package-emacs-version-alist'
  4807. (add-to-list 'customize-package-emacs-version-alist
  4808. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4809. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4810. ("8.0" . "24.4")))
  4811. (defvar org-mode-transpose-word-syntax-table
  4812. (let ((st (make-syntax-table)))
  4813. (mapc (lambda(c) (modify-syntax-entry
  4814. (string-to-char (car c)) "w p" st))
  4815. org-emphasis-alist)
  4816. st))
  4817. (when (fboundp 'abbrev-table-put)
  4818. (abbrev-table-put org-mode-abbrev-table
  4819. :parents (list text-mode-abbrev-table)))
  4820. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4821. (defsubst org-fix-ellipsis-at-bol ()
  4822. (save-excursion (goto-char (window-start)) (recenter 0)))
  4823. (defun org-find-invisible-foreground ()
  4824. (let ((candidates (remove
  4825. "unspecified-bg"
  4826. (nconc
  4827. (list (face-background 'default)
  4828. (face-background 'org-default))
  4829. (mapcar
  4830. (lambda (alist)
  4831. (when (boundp alist)
  4832. (cdr (assoc 'background-color (symbol-value alist)))))
  4833. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4834. (list (face-foreground 'org-hide))))))
  4835. (car (remove nil candidates))))
  4836. (defun org-current-time (&optional rounding-minutes past)
  4837. "Current time, possibly rounded to ROUNDING-MINUTES.
  4838. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4839. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4840. the rounding returns a past time."
  4841. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4842. (car org-time-stamp-rounding-minutes)))
  4843. (time (decode-time)) res)
  4844. (if (< r 1)
  4845. (current-time)
  4846. (setq res
  4847. (apply 'encode-time
  4848. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4849. (nthcdr 2 time))))
  4850. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4851. (seconds-to-time (- (org-float-time res) (* r 60)))
  4852. res))))
  4853. (defun org-today ()
  4854. "Return today date, considering `org-extend-today-until'."
  4855. (time-to-days
  4856. (time-subtract (current-time)
  4857. (list 0 (* 3600 org-extend-today-until) 0))))
  4858. ;;;; Font-Lock stuff, including the activators
  4859. (defvar org-mouse-map (make-sparse-keymap))
  4860. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4861. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4862. (when org-mouse-1-follows-link
  4863. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4864. (when org-tab-follows-link
  4865. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4866. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4867. (require 'font-lock)
  4868. (defconst org-non-link-chars "]\t\n\r<>")
  4869. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4870. "shell" "elisp" "doi" "message"))
  4871. (defvar org-link-types-re nil
  4872. "Matches a link that has a url-like prefix like \"http:\"")
  4873. (defvar org-link-re-with-space nil
  4874. "Matches a link with spaces, optional angular brackets around it.")
  4875. (defvar org-link-re-with-space2 nil
  4876. "Matches a link with spaces, optional angular brackets around it.")
  4877. (defvar org-link-re-with-space3 nil
  4878. "Matches a link with spaces, only for internal part in bracket links.")
  4879. (defvar org-angle-link-re nil
  4880. "Matches link with angular brackets, spaces are allowed.")
  4881. (defvar org-plain-link-re nil
  4882. "Matches plain link, without spaces.")
  4883. (defvar org-bracket-link-regexp nil
  4884. "Matches a link in double brackets.")
  4885. (defvar org-bracket-link-analytic-regexp nil
  4886. "Regular expression used to analyze links.
  4887. Here is what the match groups contain after a match:
  4888. 1: http:
  4889. 2: http
  4890. 3: path
  4891. 4: [desc]
  4892. 5: desc")
  4893. (defvar org-bracket-link-analytic-regexp++ nil
  4894. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4895. (defvar org-any-link-re nil
  4896. "Regular expression matching any link.")
  4897. (defconst org-match-sexp-depth 3
  4898. "Number of stacked braces for sub/superscript matching.")
  4899. (defun org-create-multibrace-regexp (left right n)
  4900. "Create a regular expression which will match a balanced sexp.
  4901. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4902. as single character strings.
  4903. The regexp returned will match the entire expression including the
  4904. delimiters. It will also define a single group which contains the
  4905. match except for the outermost delimiters. The maximum depth of
  4906. stacked delimiters is N. Escaping delimiters is not possible."
  4907. (let* ((nothing (concat "[^" left right "]*?"))
  4908. (or "\\|")
  4909. (re nothing)
  4910. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4911. (while (> n 1)
  4912. (setq n (1- n)
  4913. re (concat re or next)
  4914. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4915. (concat left "\\(" re "\\)" right)))
  4916. (defvar org-match-substring-regexp
  4917. (concat
  4918. "\\(\\S-\\)\\([_^]\\)\\("
  4919. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4920. "\\|"
  4921. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4922. "\\|"
  4923. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4924. "The regular expression matching a sub- or superscript.")
  4925. (defvar org-match-substring-with-braces-regexp
  4926. (concat
  4927. "\\(\\S-\\)\\([_^]\\)\\("
  4928. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4929. "\\)")
  4930. "The regular expression matching a sub- or superscript, forcing braces.")
  4931. (defun org-make-link-regexps ()
  4932. "Update the link regular expressions.
  4933. This should be called after the variable `org-link-types' has changed."
  4934. (setq org-link-types-re
  4935. (concat
  4936. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4937. org-link-re-with-space
  4938. (concat
  4939. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4940. "\\([^" org-non-link-chars " ]"
  4941. "[^" org-non-link-chars "]*"
  4942. "[^" org-non-link-chars " ]\\)>?")
  4943. org-link-re-with-space2
  4944. (concat
  4945. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4946. "\\([^" org-non-link-chars " ]"
  4947. "[^\t\n\r]*"
  4948. "[^" org-non-link-chars " ]\\)>?")
  4949. org-link-re-with-space3
  4950. (concat
  4951. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4952. "\\([^" org-non-link-chars " ]"
  4953. "[^\t\n\r]*\\)")
  4954. org-angle-link-re
  4955. (concat
  4956. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4957. "\\([^" org-non-link-chars " ]"
  4958. "[^" org-non-link-chars "]*"
  4959. "\\)>")
  4960. org-plain-link-re
  4961. (concat
  4962. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4963. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4964. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4965. org-bracket-link-regexp
  4966. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4967. org-bracket-link-analytic-regexp
  4968. (concat
  4969. "\\[\\["
  4970. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4971. "\\([^]]+\\)"
  4972. "\\]"
  4973. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4974. "\\]")
  4975. org-bracket-link-analytic-regexp++
  4976. (concat
  4977. "\\[\\["
  4978. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4979. "\\([^]]+\\)"
  4980. "\\]"
  4981. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4982. "\\]")
  4983. org-any-link-re
  4984. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4985. org-angle-link-re "\\)\\|\\("
  4986. org-plain-link-re "\\)")))
  4987. (org-make-link-regexps)
  4988. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4989. "Regular expression for fast time stamp matching.")
  4990. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4991. "Regular expression for fast time stamp matching.")
  4992. (defconst org-ts-regexp0
  4993. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4994. "Regular expression matching time strings for analysis.
  4995. This one does not require the space after the date, so it can be used
  4996. on a string that terminates immediately after the date.")
  4997. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4998. "Regular expression matching time strings for analysis.")
  4999. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5000. "Regular expression matching time stamps, with groups.")
  5001. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5002. "Regular expression matching time stamps (also [..]), with groups.")
  5003. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5004. "Regular expression matching a time stamp range.")
  5005. (defconst org-tr-regexp-both
  5006. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5007. "Regular expression matching a time stamp range.")
  5008. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5009. org-ts-regexp "\\)?")
  5010. "Regular expression matching a time stamp or time stamp range.")
  5011. (defconst org-tsr-regexp-both
  5012. (concat org-ts-regexp-both "\\(--?-?"
  5013. org-ts-regexp-both "\\)?")
  5014. "Regular expression matching a time stamp or time stamp range.
  5015. The time stamps may be either active or inactive.")
  5016. (defvar org-emph-face nil)
  5017. (defun org-do-emphasis-faces (limit)
  5018. "Run through the buffer and add overlays to emphasized strings."
  5019. (let (rtn a)
  5020. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5021. (if (not (= (char-after (match-beginning 3))
  5022. (char-after (match-beginning 4))))
  5023. (progn
  5024. (setq rtn t)
  5025. (setq a (assoc (match-string 3) org-emphasis-alist))
  5026. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5027. 'face
  5028. (nth 1 a))
  5029. (and (nth 4 a)
  5030. (org-remove-flyspell-overlays-in
  5031. (match-beginning 0) (match-end 0)))
  5032. (add-text-properties (match-beginning 2) (match-end 2)
  5033. '(font-lock-multiline t org-emphasis t))
  5034. (when org-hide-emphasis-markers
  5035. (add-text-properties (match-end 4) (match-beginning 5)
  5036. '(invisible org-link))
  5037. (add-text-properties (match-beginning 3) (match-end 3)
  5038. '(invisible org-link)))))
  5039. (backward-char 1))
  5040. rtn))
  5041. (defun org-emphasize (&optional char)
  5042. "Insert or change an emphasis, i.e. a font like bold or italic.
  5043. If there is an active region, change that region to a new emphasis.
  5044. If there is no region, just insert the marker characters and position
  5045. the cursor between them.
  5046. CHAR should be either the marker character, or the first character of the
  5047. HTML tag associated with that emphasis. If CHAR is a space, the means
  5048. to remove the emphasis of the selected region.
  5049. If char is not given (for example in an interactive call) it
  5050. will be prompted for."
  5051. (interactive)
  5052. (let ((eal org-emphasis-alist) e det
  5053. (erc org-emphasis-regexp-components)
  5054. (prompt "")
  5055. (string "") beg end move tag c s)
  5056. (if (org-region-active-p)
  5057. (setq beg (region-beginning) end (region-end)
  5058. string (buffer-substring beg end))
  5059. (setq move t))
  5060. (while (setq e (pop eal))
  5061. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  5062. c (aref tag 0))
  5063. (push (cons c (string-to-char (car e))) det)
  5064. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  5065. (substring tag 1)))))
  5066. (setq det (nreverse det))
  5067. (unless char
  5068. (message "%s" (concat "Emphasis marker or tag:" prompt))
  5069. (setq char (read-char-exclusive)))
  5070. (setq char (or (cdr (assoc char det)) char))
  5071. (if (equal char ?\ )
  5072. (setq s "" move nil)
  5073. (unless (assoc (char-to-string char) org-emphasis-alist)
  5074. (error "No such emphasis marker: \"%c\"" char))
  5075. (setq s (char-to-string char)))
  5076. (while (and (> (length string) 1)
  5077. (equal (substring string 0 1) (substring string -1))
  5078. (assoc (substring string 0 1) org-emphasis-alist))
  5079. (setq string (substring string 1 -1)))
  5080. (setq string (concat s string s))
  5081. (if beg (delete-region beg end))
  5082. (unless (or (bolp)
  5083. (string-match (concat "[" (nth 0 erc) "\n]")
  5084. (char-to-string (char-before (point)))))
  5085. (insert " "))
  5086. (unless (or (eobp)
  5087. (string-match (concat "[" (nth 1 erc) "\n]")
  5088. (char-to-string (char-after (point)))))
  5089. (insert " ") (backward-char 1))
  5090. (insert string)
  5091. (and move (backward-char 1))))
  5092. (defconst org-nonsticky-props
  5093. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5094. (defsubst org-rear-nonsticky-at (pos)
  5095. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5096. (defun org-activate-plain-links (limit)
  5097. "Run through the buffer and add overlays to links."
  5098. (let (f hl)
  5099. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5100. (not (org-in-src-block-p)))
  5101. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5102. (setq f (get-text-property (match-beginning 0) 'face))
  5103. (setq hl (org-match-string-no-properties 0))
  5104. (if (or (eq f 'org-tag)
  5105. (and (listp f) (memq 'org-tag f)))
  5106. nil
  5107. (add-text-properties (match-beginning 0) (match-end 0)
  5108. (list 'mouse-face 'highlight
  5109. 'face 'org-link
  5110. 'htmlize-link `(:uri ,hl)
  5111. 'keymap org-mouse-map))
  5112. (org-rear-nonsticky-at (match-end 0)))
  5113. t)))
  5114. (defun org-activate-code (limit)
  5115. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5116. (progn
  5117. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5118. (remove-text-properties (match-beginning 0) (match-end 0)
  5119. '(display t invisible t intangible t))
  5120. t)))
  5121. (defcustom org-src-fontify-natively nil
  5122. "When non-nil, fontify code in code blocks."
  5123. :type 'boolean
  5124. :version "24.1"
  5125. :group 'org-appearance
  5126. :group 'org-babel)
  5127. (defcustom org-allow-promoting-top-level-subtree nil
  5128. "When non-nil, allow promoting a top level subtree.
  5129. The leading star of the top level headline will be replaced
  5130. by a #."
  5131. :type 'boolean
  5132. :version "24.1"
  5133. :group 'org-appearance)
  5134. (defun org-fontify-meta-lines-and-blocks (limit)
  5135. (condition-case nil
  5136. (org-fontify-meta-lines-and-blocks-1 limit)
  5137. (error (message "org-mode fontification error"))))
  5138. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5139. "Fontify #+ lines and blocks."
  5140. (let ((case-fold-search t))
  5141. (if (re-search-forward
  5142. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5143. limit t)
  5144. (let ((beg (match-beginning 0))
  5145. (block-start (match-end 0))
  5146. (block-end nil)
  5147. (lang (match-string 7))
  5148. (beg1 (line-beginning-position 2))
  5149. (dc1 (downcase (match-string 2)))
  5150. (dc3 (downcase (match-string 3)))
  5151. end end1 quoting block-type ovl)
  5152. (cond
  5153. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5154. ;; a single line of backend-specific content
  5155. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5156. (remove-text-properties (match-beginning 0) (match-end 0)
  5157. '(display t invisible t intangible t))
  5158. (add-text-properties (match-beginning 1) (match-end 3)
  5159. '(font-lock-fontified t face org-meta-line))
  5160. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5161. '(font-lock-fontified t face org-block))
  5162. ; for backend-specific code
  5163. t)
  5164. ((and (match-end 4) (equal dc3 "+begin"))
  5165. ;; Truly a block
  5166. (setq block-type (downcase (match-string 5))
  5167. quoting (member block-type org-protecting-blocks))
  5168. (when (re-search-forward
  5169. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5170. nil t) ;; on purpose, we look further than LIMIT
  5171. (setq end (min (point-max) (match-end 0))
  5172. end1 (min (point-max) (1- (match-beginning 0))))
  5173. (setq block-end (match-beginning 0))
  5174. (when quoting
  5175. (remove-text-properties beg end
  5176. '(display t invisible t intangible t)))
  5177. (add-text-properties
  5178. beg end
  5179. '(font-lock-fontified t font-lock-multiline t))
  5180. (add-text-properties beg beg1 '(face org-meta-line))
  5181. (add-text-properties end1 (min (point-max) (1+ end))
  5182. '(face org-meta-line)) ; for end_src
  5183. (cond
  5184. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5185. (org-src-font-lock-fontify-block lang block-start block-end)
  5186. ;; remove old background overlays
  5187. (mapc (lambda (ov)
  5188. (if (eq (overlay-get ov 'face) 'org-block-background)
  5189. (delete-overlay ov)))
  5190. (overlays-at (/ (+ beg1 block-end) 2)))
  5191. ;; add a background overlay
  5192. (setq ovl (make-overlay beg1 block-end))
  5193. (overlay-put ovl 'face 'org-block-background)
  5194. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5195. (quoting
  5196. (add-text-properties beg1 (min (point-max) (1+ end1))
  5197. '(face org-block))) ; end of source block
  5198. ((not org-fontify-quote-and-verse-blocks))
  5199. ((string= block-type "quote")
  5200. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5201. ((string= block-type "verse")
  5202. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5203. (add-text-properties beg beg1 '(face org-block-begin-line))
  5204. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5205. '(face org-block-end-line))
  5206. t))
  5207. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5208. (add-text-properties
  5209. beg (match-end 3)
  5210. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5211. '(font-lock-fontified t invisible t)
  5212. '(font-lock-fontified t face org-document-info-keyword)))
  5213. (add-text-properties
  5214. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5215. (if (string-equal dc1 "+title:")
  5216. '(font-lock-fontified t face org-document-title)
  5217. '(font-lock-fontified t face org-document-info))))
  5218. ((or (equal dc1 "+results")
  5219. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5220. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5221. "+call:" "+header:" "+headers:" "+name:"))
  5222. (and (match-end 4) (equal dc3 "+attr")))
  5223. (add-text-properties
  5224. beg (match-end 0)
  5225. '(font-lock-fontified t face org-meta-line))
  5226. t)
  5227. ((member dc3 '(" " ""))
  5228. (add-text-properties
  5229. beg (match-end 0)
  5230. '(font-lock-fontified t face font-lock-comment-face)))
  5231. ((not (member (char-after beg) '(?\ ?\t)))
  5232. ;; just any other in-buffer setting, but not indented
  5233. (add-text-properties
  5234. beg (match-end 0)
  5235. '(font-lock-fontified t face org-meta-line))
  5236. t)
  5237. (t nil))))))
  5238. (defun org-activate-angle-links (limit)
  5239. "Run through the buffer and add overlays to links."
  5240. (if (and (re-search-forward org-angle-link-re limit t)
  5241. (not (org-in-src-block-p)))
  5242. (progn
  5243. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5244. (add-text-properties (match-beginning 0) (match-end 0)
  5245. (list 'mouse-face 'highlight
  5246. 'keymap org-mouse-map))
  5247. (org-rear-nonsticky-at (match-end 0))
  5248. t)))
  5249. (defun org-activate-footnote-links (limit)
  5250. "Run through the buffer and add overlays to footnotes."
  5251. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5252. (when fn
  5253. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5254. (org-remove-flyspell-overlays-in beg end)
  5255. (add-text-properties beg end
  5256. (list 'mouse-face 'highlight
  5257. 'keymap org-mouse-map
  5258. 'help-echo
  5259. (if (= (point-at-bol) beg)
  5260. "Footnote definition"
  5261. "Footnote reference")
  5262. 'font-lock-fontified t
  5263. 'font-lock-multiline t
  5264. 'face 'org-footnote))))))
  5265. (defun org-activate-bracket-links (limit)
  5266. "Run through the buffer and add overlays to bracketed links."
  5267. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5268. (not (org-in-src-block-p)))
  5269. (let* ((hl (org-match-string-no-properties 1))
  5270. (help (concat "LINK: " hl))
  5271. ;; FIXME: Above we should remove the escapes. But that
  5272. ;; requires another match, protecting match data, a lot
  5273. ;; of overhead for font-lock.
  5274. (ip (org-maybe-intangible
  5275. (list 'invisible 'org-link
  5276. 'keymap org-mouse-map 'mouse-face 'highlight
  5277. 'font-lock-multiline t 'help-echo help
  5278. 'htmlize-link `(:uri ,hl))))
  5279. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5280. 'font-lock-multiline t 'help-echo help
  5281. 'htmlize-link `(:uri ,hl))))
  5282. ;; We need to remove the invisible property here. Table narrowing
  5283. ;; may have made some of this invisible.
  5284. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5285. (remove-text-properties (match-beginning 0) (match-end 0)
  5286. '(invisible nil))
  5287. (if (match-end 3)
  5288. (progn
  5289. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5290. (org-rear-nonsticky-at (match-beginning 3))
  5291. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5292. (org-rear-nonsticky-at (match-end 3))
  5293. (add-text-properties (match-end 3) (match-end 0) ip)
  5294. (org-rear-nonsticky-at (match-end 0)))
  5295. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5296. (org-rear-nonsticky-at (match-beginning 1))
  5297. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5298. (org-rear-nonsticky-at (match-end 1))
  5299. (add-text-properties (match-end 1) (match-end 0) ip)
  5300. (org-rear-nonsticky-at (match-end 0)))
  5301. t)))
  5302. (defun org-activate-dates (limit)
  5303. "Run through the buffer and add overlays to dates."
  5304. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5305. (not (equal (char-before (match-beginning 0)) 91)))
  5306. (progn
  5307. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5308. (add-text-properties (match-beginning 0) (match-end 0)
  5309. (list 'mouse-face 'highlight
  5310. 'keymap org-mouse-map))
  5311. (org-rear-nonsticky-at (match-end 0))
  5312. (when org-display-custom-times
  5313. (if (match-end 3)
  5314. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5315. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5316. t)))
  5317. (defvar org-target-link-regexp nil
  5318. "Regular expression matching radio targets in plain text.")
  5319. (make-variable-buffer-local 'org-target-link-regexp)
  5320. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5321. "Regular expression matching a link target.")
  5322. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5323. "Regular expression matching a radio target.")
  5324. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5325. "Regular expression matching any target.")
  5326. (defun org-activate-target-links (limit)
  5327. "Run through the buffer and add overlays to target matches."
  5328. (when org-target-link-regexp
  5329. (let ((case-fold-search t))
  5330. (if (re-search-forward org-target-link-regexp limit t)
  5331. (progn
  5332. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5333. (add-text-properties (match-beginning 0) (match-end 0)
  5334. (list 'mouse-face 'highlight
  5335. 'keymap org-mouse-map
  5336. 'help-echo "Radio target link"
  5337. 'org-linked-text t))
  5338. (org-rear-nonsticky-at (match-end 0))
  5339. t)))))
  5340. (defun org-update-radio-target-regexp ()
  5341. "Find all radio targets in this file and update the regular expression."
  5342. (interactive)
  5343. (when (memq 'radio org-activate-links)
  5344. (setq org-target-link-regexp
  5345. (org-make-target-link-regexp (org-all-targets 'radio)))
  5346. (org-restart-font-lock)))
  5347. (defun org-hide-wide-columns (limit)
  5348. (let (s e)
  5349. (setq s (text-property-any (point) (or limit (point-max))
  5350. 'org-cwidth t))
  5351. (when s
  5352. (setq e (next-single-property-change s 'org-cwidth))
  5353. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5354. (goto-char e)
  5355. t)))
  5356. (defvar org-latex-and-related-regexp nil
  5357. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5358. (defvar org-match-substring-regexp)
  5359. (defvar org-match-substring-with-braces-regexp)
  5360. (defun org-compute-latex-and-related-regexp ()
  5361. "Compute regular expression for LaTeX, entities and sub/superscript.
  5362. Result depends on variable `org-highlight-latex-and-related'."
  5363. (org-set-local
  5364. 'org-latex-and-related-regexp
  5365. (let* ((re-sub
  5366. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5367. ((eq org-use-sub-superscripts '{})
  5368. (list org-match-substring-with-braces-regexp))
  5369. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5370. (re-latex
  5371. (when (memq 'latex org-highlight-latex-and-related)
  5372. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5373. (delq nil
  5374. (mapcar (lambda (x)
  5375. (and (member (car x) matchers) (nth 1 x)))
  5376. org-latex-regexps)))))
  5377. (re-entities
  5378. (when (memq 'entities org-highlight-latex-and-related)
  5379. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5380. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5381. (defun org-do-latex-and-related (limit)
  5382. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5383. LIMIT bounds the search for syntax to highlight. Stop at first
  5384. highlighted object, if any. Return t if some highlighting was
  5385. done, nil otherwise."
  5386. (when (org-string-nw-p org-latex-and-related-regexp)
  5387. (catch 'found
  5388. (while (re-search-forward org-latex-and-related-regexp limit t)
  5389. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5390. 'face))
  5391. '(org-code org-verbatim underline))
  5392. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5393. '(?_ ?^))
  5394. 1
  5395. 0)))
  5396. (font-lock-prepend-text-property
  5397. (+ offset (match-beginning 0)) (match-end 0)
  5398. 'face 'org-latex-and-related)
  5399. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5400. '(font-lock-multiline t)))
  5401. (throw 'found t)))
  5402. nil)))
  5403. (defun org-restart-font-lock ()
  5404. "Restart `font-lock-mode', to force refontification."
  5405. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5406. (font-lock-mode -1)
  5407. (font-lock-mode 1)))
  5408. (defun org-all-targets (&optional radio)
  5409. "Return a list of all targets in this file.
  5410. When optional argument RADIO is non-nil, only find radio
  5411. targets."
  5412. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5413. (save-excursion
  5414. (goto-char (point-min))
  5415. (while (re-search-forward re nil t)
  5416. ;; Make sure point is really within the object.
  5417. (backward-char)
  5418. (let ((obj (org-element-context)))
  5419. (when (memq (org-element-type obj) '(radio-target target))
  5420. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5421. rtn)))
  5422. (defun org-make-target-link-regexp (targets)
  5423. "Make regular expression matching all strings in TARGETS.
  5424. The regular expression finds the targets also if there is a line break
  5425. between words."
  5426. (and targets
  5427. (concat
  5428. "\\<\\("
  5429. (mapconcat
  5430. (lambda (x)
  5431. (setq x (regexp-quote x))
  5432. (while (string-match " +" x)
  5433. (setq x (replace-match "\\s-+" t t x)))
  5434. x)
  5435. targets
  5436. "\\|")
  5437. "\\)\\>")))
  5438. (defun org-activate-tags (limit)
  5439. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5440. (progn
  5441. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5442. (add-text-properties (match-beginning 1) (match-end 1)
  5443. (list 'mouse-face 'highlight
  5444. 'keymap org-mouse-map))
  5445. (org-rear-nonsticky-at (match-end 1))
  5446. t)))
  5447. (defun org-outline-level ()
  5448. "Compute the outline level of the heading at point.
  5449. If this is called at a normal headline, the level is the number of stars.
  5450. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5451. (save-excursion
  5452. (if (not (condition-case nil
  5453. (org-back-to-heading t)
  5454. (error nil)))
  5455. 0
  5456. (looking-at org-outline-regexp)
  5457. (1- (- (match-end 0) (match-beginning 0))))))
  5458. (defvar org-font-lock-keywords nil)
  5459. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5460. "Regular expression matching a property line.")
  5461. (defvar org-font-lock-hook nil
  5462. "Functions to be called for special font lock stuff.")
  5463. (defvar org-font-lock-set-keywords-hook nil
  5464. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5465. This is called after `org-font-lock-extra-keywords' is defined, but before
  5466. it is installed to be used by font lock. This can be useful if something
  5467. needs to be inserted at a specific position in the font-lock sequence.")
  5468. (defun org-font-lock-hook (limit)
  5469. "Run `org-font-lock-hook' within LIMIT."
  5470. (run-hook-with-args 'org-font-lock-hook limit))
  5471. (defun org-set-font-lock-defaults ()
  5472. "Set font lock defaults for the current buffer."
  5473. (let* ((em org-fontify-emphasized-text)
  5474. (lk org-activate-links)
  5475. (org-font-lock-extra-keywords
  5476. (list
  5477. ;; Call the hook
  5478. '(org-font-lock-hook)
  5479. ;; Headlines
  5480. `(,(if org-fontify-whole-heading-line
  5481. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5482. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5483. (1 (org-get-level-face 1))
  5484. (2 (org-get-level-face 2))
  5485. (3 (org-get-level-face 3)))
  5486. ;; Table lines
  5487. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5488. (1 'org-table t))
  5489. ;; Table internals
  5490. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5491. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5492. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5493. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5494. ;; Drawers
  5495. (list org-drawer-regexp '(0 'org-special-keyword t))
  5496. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5497. ;; Properties
  5498. (list org-property-re
  5499. '(1 'org-special-keyword t)
  5500. '(3 'org-property-value t))
  5501. ;; Links
  5502. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5503. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5504. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5505. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5506. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5507. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5508. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5509. ;; Targets.
  5510. (list org-any-target-regexp '(0 'org-target t))
  5511. ;; Diary sexps.
  5512. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5513. ;; Macro
  5514. '("{{{.+}}}" (0 'org-macro t))
  5515. '(org-hide-wide-columns (0 nil append))
  5516. ;; TODO keyword
  5517. (list (format org-heading-keyword-regexp-format
  5518. org-todo-regexp)
  5519. '(2 (org-get-todo-face 2) t))
  5520. ;; DONE
  5521. (if org-fontify-done-headline
  5522. (list (format org-heading-keyword-regexp-format
  5523. (concat
  5524. "\\(?:"
  5525. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5526. "\\)"))
  5527. '(2 'org-headline-done t))
  5528. nil)
  5529. ;; Priorities
  5530. '(org-font-lock-add-priority-faces)
  5531. ;; Tags
  5532. '(org-font-lock-add-tag-faces)
  5533. ;; Tags groups
  5534. (if (and org-group-tags org-tag-groups-alist)
  5535. (list (concat org-outline-regexp-bol ".+\\(:"
  5536. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5537. ":\\).*$")
  5538. '(1 'org-tag-group prepend)))
  5539. ;; Special keywords
  5540. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5541. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5542. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5543. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5544. ;; Emphasis
  5545. (if em
  5546. (if (featurep 'xemacs)
  5547. '(org-do-emphasis-faces (0 nil append))
  5548. '(org-do-emphasis-faces)))
  5549. ;; Checkboxes
  5550. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5551. 1 'org-checkbox prepend)
  5552. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5553. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5554. (0 (org-get-checkbox-statistics-face) t)))
  5555. ;; Description list items
  5556. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5557. 1 'org-list-dt prepend)
  5558. ;; ARCHIVEd headings
  5559. (list (concat
  5560. org-outline-regexp-bol
  5561. "\\(.*:" org-archive-tag ":.*\\)")
  5562. '(1 'org-archived prepend))
  5563. ;; Specials
  5564. '(org-do-latex-and-related)
  5565. '(org-fontify-entities)
  5566. '(org-raise-scripts)
  5567. ;; Code
  5568. '(org-activate-code (1 'org-code t))
  5569. ;; COMMENT
  5570. (list (format org-heading-keyword-regexp-format
  5571. (concat "\\("
  5572. org-comment-string "\\|" org-quote-string
  5573. "\\)"))
  5574. '(2 'org-special-keyword t))
  5575. ;; Blocks and meta lines
  5576. '(org-fontify-meta-lines-and-blocks)
  5577. )))
  5578. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5579. (run-hooks 'org-font-lock-set-keywords-hook)
  5580. ;; Now set the full font-lock-keywords
  5581. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5582. (org-set-local 'font-lock-defaults
  5583. '(org-font-lock-keywords t nil nil backward-paragraph))
  5584. (kill-local-variable 'font-lock-keywords) nil))
  5585. (defun org-toggle-pretty-entities ()
  5586. "Toggle the composition display of entities as UTF8 characters."
  5587. (interactive)
  5588. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5589. (org-restart-font-lock)
  5590. (if org-pretty-entities
  5591. (message "Entities are now displayed as UTF8 characters")
  5592. (save-restriction
  5593. (widen)
  5594. (org-decompose-region (point-min) (point-max))
  5595. (message "Entities are now displayed as plain text"))))
  5596. (defvar org-custom-properties-overlays nil
  5597. "List of overlays used for custom properties.")
  5598. (make-variable-buffer-local 'org-custom-properties-overlays)
  5599. (defun org-toggle-custom-properties-visibility ()
  5600. "Display or hide properties in `org-custom-properties'."
  5601. (interactive)
  5602. (if org-custom-properties-overlays
  5603. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5604. (setq org-custom-properties-overlays nil))
  5605. (unless (not org-custom-properties)
  5606. (save-excursion
  5607. (save-restriction
  5608. (widen)
  5609. (goto-char (point-min))
  5610. (while (re-search-forward org-property-re nil t)
  5611. (mapc (lambda(p)
  5612. (when (equal p (substring (match-string 1) 1 -1))
  5613. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5614. (overlay-put o 'invisible t)
  5615. (overlay-put o 'org-custom-property t)
  5616. (push o org-custom-properties-overlays))))
  5617. org-custom-properties)))))))
  5618. (defun org-fontify-entities (limit)
  5619. "Find an entity to fontify."
  5620. (let (ee)
  5621. (when org-pretty-entities
  5622. (catch 'match
  5623. (while (re-search-forward
  5624. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5625. limit t)
  5626. (if (and (not (org-in-indented-comment-line))
  5627. (setq ee (org-entity-get (match-string 1)))
  5628. (= (length (nth 6 ee)) 1))
  5629. (let*
  5630. ((end (if (equal (match-string 2) "{}")
  5631. (match-end 2)
  5632. (match-end 1))))
  5633. (add-text-properties
  5634. (match-beginning 0) end
  5635. (list 'font-lock-fontified t))
  5636. (compose-region (match-beginning 0) end
  5637. (nth 6 ee) nil)
  5638. (backward-char 1)
  5639. (throw 'match t))))
  5640. nil))))
  5641. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5642. "Fontify string S like in Org-mode."
  5643. (with-temp-buffer
  5644. (insert s)
  5645. (let ((org-odd-levels-only odd-levels))
  5646. (org-mode)
  5647. (font-lock-fontify-buffer)
  5648. (buffer-string))))
  5649. (defvar org-m nil)
  5650. (defvar org-l nil)
  5651. (defvar org-f nil)
  5652. (defun org-get-level-face (n)
  5653. "Get the right face for match N in font-lock matching of headlines."
  5654. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5655. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5656. (if org-cycle-level-faces
  5657. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5658. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5659. (cond
  5660. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5661. ((eq n 2) org-f)
  5662. (t (if org-level-color-stars-only nil org-f))))
  5663. (defun org-get-todo-face (kwd)
  5664. "Get the right face for a TODO keyword KWD.
  5665. If KWD is a number, get the corresponding match group."
  5666. (if (numberp kwd) (setq kwd (match-string kwd)))
  5667. (or (org-face-from-face-or-color
  5668. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5669. (and (member kwd org-done-keywords) 'org-done)
  5670. 'org-todo))
  5671. (defun org-face-from-face-or-color (context inherit face-or-color)
  5672. "Create a face list that inherits INHERIT, but sets the foreground color.
  5673. When FACE-OR-COLOR is not a string, just return it."
  5674. (if (stringp face-or-color)
  5675. (list :inherit inherit
  5676. (cdr (assoc context org-faces-easy-properties))
  5677. face-or-color)
  5678. face-or-color))
  5679. (defun org-font-lock-add-tag-faces (limit)
  5680. "Add the special tag faces."
  5681. (when (and org-tag-faces org-tags-special-faces-re)
  5682. (while (re-search-forward org-tags-special-faces-re limit t)
  5683. (add-text-properties (match-beginning 1) (match-end 1)
  5684. (list 'face (org-get-tag-face 1)
  5685. 'font-lock-fontified t))
  5686. (backward-char 1))))
  5687. (defun org-font-lock-add-priority-faces (limit)
  5688. "Add the special priority faces."
  5689. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5690. (when (save-match-data (org-at-heading-p))
  5691. (add-text-properties
  5692. (match-beginning 0) (match-end 0)
  5693. (list 'face (or (org-face-from-face-or-color
  5694. 'priority 'org-priority
  5695. (cdr (assoc (char-after (match-beginning 1))
  5696. org-priority-faces)))
  5697. 'org-priority)
  5698. 'font-lock-fontified t)))))
  5699. (defun org-get-tag-face (kwd)
  5700. "Get the right face for a TODO keyword KWD.
  5701. If KWD is a number, get the corresponding match group."
  5702. (if (numberp kwd) (setq kwd (match-string kwd)))
  5703. (or (org-face-from-face-or-color
  5704. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5705. 'org-tag))
  5706. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5707. "Remove fontification and activation overlays from links."
  5708. (font-lock-default-unfontify-region beg end)
  5709. (let* ((buffer-undo-list t)
  5710. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5711. (inhibit-modification-hooks t)
  5712. deactivate-mark buffer-file-name buffer-file-truename)
  5713. (org-decompose-region beg end)
  5714. (remove-text-properties beg end
  5715. '(mouse-face t keymap t org-linked-text t
  5716. invisible t intangible t
  5717. org-no-flyspell t org-emphasis t))
  5718. (org-remove-font-lock-display-properties beg end)))
  5719. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5720. ((raise 0.3) (height 0.7))
  5721. ((raise -0.5))
  5722. ((raise 0.5)))
  5723. "Display properties for showing superscripts and subscripts.")
  5724. (defun org-remove-font-lock-display-properties (beg end)
  5725. "Remove specific display properties that have been added by font lock.
  5726. The will remove the raise properties that are used to show superscripts
  5727. and subscripts."
  5728. (let (next prop)
  5729. (while (< beg end)
  5730. (setq next (next-single-property-change beg 'display nil end)
  5731. prop (get-text-property beg 'display))
  5732. (if (member prop org-script-display)
  5733. (put-text-property beg next 'display nil))
  5734. (setq beg next))))
  5735. (defun org-raise-scripts (limit)
  5736. "Add raise properties to sub/superscripts."
  5737. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5738. (if (re-search-forward
  5739. (if (eq org-use-sub-superscripts t)
  5740. org-match-substring-regexp
  5741. org-match-substring-with-braces-regexp)
  5742. limit t)
  5743. (let* ((pos (point)) table-p comment-p
  5744. (mpos (match-beginning 3))
  5745. (emph-p (get-text-property mpos 'org-emphasis))
  5746. (link-p (get-text-property mpos 'mouse-face))
  5747. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5748. (goto-char (point-at-bol))
  5749. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5750. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5751. (goto-char pos)
  5752. ;; Handle a_b^c
  5753. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5754. (if (or comment-p emph-p link-p keyw-p)
  5755. t
  5756. (put-text-property (match-beginning 3) (match-end 0)
  5757. 'display
  5758. (if (equal (char-after (match-beginning 2)) ?^)
  5759. (nth (if table-p 3 1) org-script-display)
  5760. (nth (if table-p 2 0) org-script-display)))
  5761. (add-text-properties (match-beginning 2) (match-end 2)
  5762. (list 'invisible t
  5763. 'org-dwidth t 'org-dwidth-n 1))
  5764. (if (and (eq (char-after (match-beginning 3)) ?{)
  5765. (eq (char-before (match-end 3)) ?}))
  5766. (progn
  5767. (add-text-properties
  5768. (match-beginning 3) (1+ (match-beginning 3))
  5769. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5770. (add-text-properties
  5771. (1- (match-end 3)) (match-end 3)
  5772. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5773. t)))))
  5774. ;;;; Visibility cycling, including org-goto and indirect buffer
  5775. ;;; Cycling
  5776. (defvar org-cycle-global-status nil)
  5777. (make-variable-buffer-local 'org-cycle-global-status)
  5778. (put 'org-cycle-global-status 'org-state t)
  5779. (defvar org-cycle-subtree-status nil)
  5780. (make-variable-buffer-local 'org-cycle-subtree-status)
  5781. (put 'org-cycle-subtree-status 'org-state t)
  5782. (defvar org-inlinetask-min-level)
  5783. ;;;###autoload
  5784. (defun org-cycle (&optional arg)
  5785. "TAB-action and visibility cycling for Org-mode.
  5786. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5787. is outline visibility cycling, but it also invokes other actions
  5788. in special contexts.
  5789. - When this function is called with a prefix argument, rotate the entire
  5790. buffer through 3 states (global cycling)
  5791. 1. OVERVIEW: Show only top-level headlines.
  5792. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5793. 3. SHOW ALL: Show everything.
  5794. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5795. determined by the variable `org-startup-folded', and by any VISIBILITY
  5796. properties in the buffer.
  5797. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5798. including any drawers.
  5799. - When inside a table, re-align the table and move to the next field.
  5800. - When point is at the beginning of a headline, rotate the subtree started
  5801. by this line through 3 different states (local cycling)
  5802. 1. FOLDED: Only the main headline is shown.
  5803. 2. CHILDREN: The main headline and the direct children are shown.
  5804. From this state, you can move to one of the children
  5805. and zoom in further.
  5806. 3. SUBTREE: Show the entire subtree, including body text.
  5807. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5808. - When point is at the beginning of an empty headline and the variable
  5809. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5810. of the headline by demoting and promoting it to likely levels. This
  5811. speeds up creation document structure by pressing TAB once or several
  5812. times right after creating a new headline.
  5813. - When there is a numeric prefix, go up to a heading with level ARG, do
  5814. a `show-subtree' and return to the previous cursor position. If ARG
  5815. is negative, go up that many levels.
  5816. - When point is not at the beginning of a headline, execute the global
  5817. binding for TAB, which is re-indenting the line. See the option
  5818. `org-cycle-emulate-tab' for details.
  5819. - Special case: if point is at the beginning of the buffer and there is
  5820. no headline in line 1, this function will act as if called with prefix arg
  5821. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5822. But only if also the variable `org-cycle-global-at-bob' is t."
  5823. (interactive "P")
  5824. (org-load-modules-maybe)
  5825. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5826. (and org-cycle-level-after-item/entry-creation
  5827. (or (org-cycle-level)
  5828. (org-cycle-item-indentation))))
  5829. (let* (message-log-max ; Don't populate the *Messages* buffer
  5830. (limit-level
  5831. (or org-cycle-max-level
  5832. (and (boundp 'org-inlinetask-min-level)
  5833. org-inlinetask-min-level
  5834. (1- org-inlinetask-min-level))))
  5835. (nstars (and limit-level
  5836. (if org-odd-levels-only
  5837. (and limit-level (1- (* limit-level 2)))
  5838. limit-level)))
  5839. (org-outline-regexp
  5840. (if (not (derived-mode-p 'org-mode))
  5841. outline-regexp
  5842. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5843. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5844. (not (looking-at org-outline-regexp))))
  5845. (org-cycle-hook
  5846. (if bob-special
  5847. (delq 'org-optimize-window-after-visibility-change
  5848. (copy-sequence org-cycle-hook))
  5849. org-cycle-hook))
  5850. (pos (point)))
  5851. (if (or bob-special (equal arg '(4)))
  5852. ;; special case: use global cycling
  5853. (setq arg t))
  5854. (cond
  5855. ((equal arg '(16))
  5856. (setq last-command 'dummy)
  5857. (org-set-startup-visibility)
  5858. (message "Startup visibility, plus VISIBILITY properties"))
  5859. ((equal arg '(64))
  5860. (show-all)
  5861. (message "Entire buffer visible, including drawers"))
  5862. ;; Table: enter it or move to the next field.
  5863. ((org-at-table-p 'any)
  5864. (if (org-at-table.el-p)
  5865. (message "Use C-c ' to edit table.el tables")
  5866. (if arg (org-table-edit-field t)
  5867. (org-table-justify-field-maybe)
  5868. (call-interactively 'org-table-next-field))))
  5869. ((run-hook-with-args-until-success
  5870. 'org-tab-after-check-for-table-hook))
  5871. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5872. ((eq arg t) (org-cycle-internal-global))
  5873. ;; Drawers: delegate to `org-flag-drawer'.
  5874. ((and org-drawers org-drawer-regexp
  5875. (save-excursion
  5876. (beginning-of-line 1)
  5877. (looking-at org-drawer-regexp)))
  5878. (org-flag-drawer ; toggle block visibility
  5879. (not (get-char-property (match-end 0) 'invisible))))
  5880. ;; Show-subtree, ARG levels up from here.
  5881. ((integerp arg)
  5882. (save-excursion
  5883. (org-back-to-heading)
  5884. (outline-up-heading (if (< arg 0) (- arg)
  5885. (- (funcall outline-level) arg)))
  5886. (org-show-subtree)))
  5887. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5888. ((and (featurep 'org-inlinetask)
  5889. (org-inlinetask-at-task-p)
  5890. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5891. (org-inlinetask-toggle-visibility))
  5892. ((org-try-cdlatex-tab))
  5893. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5894. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5895. (save-excursion (beginning-of-line 1)
  5896. (looking-at org-outline-regexp)))
  5897. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5898. (org-cycle-internal-local))
  5899. ;; From there: TAB emulation and template completion.
  5900. (buffer-read-only (org-back-to-heading))
  5901. ((run-hook-with-args-until-success
  5902. 'org-tab-after-check-for-cycling-hook))
  5903. ((org-try-structure-completion))
  5904. ((run-hook-with-args-until-success
  5905. 'org-tab-before-tab-emulation-hook))
  5906. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5907. (or (not (bolp))
  5908. (not (looking-at org-outline-regexp))))
  5909. (call-interactively (global-key-binding "\t")))
  5910. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5911. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5912. (or (and (eq org-cycle-emulate-tab 'white)
  5913. (= (match-end 0) (point-at-eol)))
  5914. (and (eq org-cycle-emulate-tab 'whitestart)
  5915. (>= (match-end 0) pos))))
  5916. t
  5917. (eq org-cycle-emulate-tab t))
  5918. (call-interactively (global-key-binding "\t")))
  5919. (t (save-excursion
  5920. (org-back-to-heading)
  5921. (org-cycle)))))))
  5922. (defun org-cycle-internal-global ()
  5923. "Do the global cycling action."
  5924. ;; Hack to avoid display of messages for .org attachments in Gnus
  5925. (let (message-log-max ; Don't populate the *Messages* buffer
  5926. (ga (string-match "\\*fontification" (buffer-name))))
  5927. (cond
  5928. ((and (eq last-command this-command)
  5929. (eq org-cycle-global-status 'overview))
  5930. ;; We just created the overview - now do table of contents
  5931. ;; This can be slow in very large buffers, so indicate action
  5932. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5933. (unless ga (message "CONTENTS..."))
  5934. (org-content)
  5935. (unless ga (message "CONTENTS...done"))
  5936. (setq org-cycle-global-status 'contents)
  5937. (run-hook-with-args 'org-cycle-hook 'contents))
  5938. ((and (eq last-command this-command)
  5939. (eq org-cycle-global-status 'contents))
  5940. ;; We just showed the table of contents - now show everything
  5941. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5942. (show-all)
  5943. (unless ga (message "SHOW ALL"))
  5944. (setq org-cycle-global-status 'all)
  5945. (run-hook-with-args 'org-cycle-hook 'all))
  5946. (t
  5947. ;; Default action: go to overview
  5948. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5949. (org-overview)
  5950. (unless ga (message "OVERVIEW"))
  5951. (setq org-cycle-global-status 'overview)
  5952. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5953. (defun org-cycle-internal-local ()
  5954. "Do the local cycling action."
  5955. (let (message-log-max ; Don't populate the *Messages* buffer
  5956. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5957. ;; First, determine end of headline (EOH), end of subtree or item
  5958. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5959. (save-excursion
  5960. (if (org-at-item-p)
  5961. (progn
  5962. (beginning-of-line)
  5963. (setq struct (org-list-struct))
  5964. (setq eoh (point-at-eol))
  5965. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5966. (setq has-children (org-list-has-child-p (point) struct)))
  5967. (org-back-to-heading)
  5968. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5969. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5970. (setq has-children
  5971. (or (save-excursion
  5972. (let ((level (funcall outline-level)))
  5973. (outline-next-heading)
  5974. (and (org-at-heading-p t)
  5975. (> (funcall outline-level) level))))
  5976. (save-excursion
  5977. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5978. ;; Determine end invisible part of buffer (EOL)
  5979. (beginning-of-line 2)
  5980. ;; XEmacs doesn't have `next-single-char-property-change'
  5981. (if (featurep 'xemacs)
  5982. (while (and (not (eobp)) ;; this is like `next-line'
  5983. (get-char-property (1- (point)) 'invisible))
  5984. (beginning-of-line 2))
  5985. (while (and (not (eobp)) ;; this is like `next-line'
  5986. (get-char-property (1- (point)) 'invisible))
  5987. (goto-char (next-single-char-property-change (point) 'invisible))
  5988. (and (eolp) (beginning-of-line 2))))
  5989. (setq eol (point)))
  5990. ;; Find out what to do next and set `this-command'
  5991. (cond
  5992. ((= eos eoh)
  5993. ;; Nothing is hidden behind this heading
  5994. (unless (org-before-first-heading-p)
  5995. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5996. (message "EMPTY ENTRY")
  5997. (setq org-cycle-subtree-status nil)
  5998. (save-excursion
  5999. (goto-char eos)
  6000. (outline-next-heading)
  6001. (if (outline-invisible-p) (org-flag-heading nil))))
  6002. ((and (or (>= eol eos)
  6003. (not (string-match "\\S-" (buffer-substring eol eos))))
  6004. (or has-children
  6005. (not (setq children-skipped
  6006. org-cycle-skip-children-state-if-no-children))))
  6007. ;; Entire subtree is hidden in one line: children view
  6008. (unless (org-before-first-heading-p)
  6009. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6010. (if (org-at-item-p)
  6011. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6012. (org-show-entry)
  6013. (org-with-limited-levels (show-children))
  6014. ;; FIXME: This slows down the func way too much.
  6015. ;; How keep drawers hidden in subtree anyway?
  6016. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6017. ;; (org-cycle-hide-drawers 'subtree))
  6018. ;; Fold every list in subtree to top-level items.
  6019. (when (eq org-cycle-include-plain-lists 'integrate)
  6020. (save-excursion
  6021. (org-back-to-heading)
  6022. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6023. (beginning-of-line 1)
  6024. (let* ((struct (org-list-struct))
  6025. (prevs (org-list-prevs-alist struct))
  6026. (end (org-list-get-bottom-point struct)))
  6027. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6028. (org-list-get-all-items (point) struct prevs))
  6029. (goto-char (if (< end eos) end eos)))))))
  6030. (message "CHILDREN")
  6031. (save-excursion
  6032. (goto-char eos)
  6033. (outline-next-heading)
  6034. (if (outline-invisible-p) (org-flag-heading nil)))
  6035. (setq org-cycle-subtree-status 'children)
  6036. (unless (org-before-first-heading-p)
  6037. (run-hook-with-args 'org-cycle-hook 'children)))
  6038. ((or children-skipped
  6039. (and (eq last-command this-command)
  6040. (eq org-cycle-subtree-status 'children)))
  6041. ;; We just showed the children, or no children are there,
  6042. ;; now show everything.
  6043. (unless (org-before-first-heading-p)
  6044. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6045. (outline-flag-region eoh eos nil)
  6046. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6047. (setq org-cycle-subtree-status 'subtree)
  6048. (unless (org-before-first-heading-p)
  6049. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6050. (t
  6051. ;; Default action: hide the subtree.
  6052. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6053. (outline-flag-region eoh eos t)
  6054. (message "FOLDED")
  6055. (setq org-cycle-subtree-status 'folded)
  6056. (unless (org-before-first-heading-p)
  6057. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6058. ;;;###autoload
  6059. (defun org-global-cycle (&optional arg)
  6060. "Cycle the global visibility. For details see `org-cycle'.
  6061. With \\[universal-argument] prefix arg, switch to startup visibility.
  6062. With a numeric prefix, show all headlines up to that level."
  6063. (interactive "P")
  6064. (let ((org-cycle-include-plain-lists
  6065. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6066. (cond
  6067. ((integerp arg)
  6068. (show-all)
  6069. (hide-sublevels arg)
  6070. (setq org-cycle-global-status 'contents))
  6071. ((equal arg '(4))
  6072. (org-set-startup-visibility)
  6073. (message "Startup visibility, plus VISIBILITY properties."))
  6074. (t
  6075. (org-cycle '(4))))))
  6076. (defun org-set-startup-visibility ()
  6077. "Set the visibility required by startup options and properties."
  6078. (cond
  6079. ((eq org-startup-folded t)
  6080. (org-cycle '(4)))
  6081. ((eq org-startup-folded 'content)
  6082. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6083. (org-cycle '(4)) (org-cycle '(4)))))
  6084. (unless (eq org-startup-folded 'showeverything)
  6085. (if org-hide-block-startup (org-hide-block-all))
  6086. (org-set-visibility-according-to-property 'no-cleanup)
  6087. (org-cycle-hide-archived-subtrees 'all)
  6088. (org-cycle-hide-drawers 'all)
  6089. (org-cycle-show-empty-lines t)))
  6090. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6091. "Switch subtree visibilities according to :VISIBILITY: property."
  6092. (interactive)
  6093. (let (org-show-entry-below state)
  6094. (save-excursion
  6095. (goto-char (point-min))
  6096. (while (re-search-forward
  6097. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6098. nil t)
  6099. (setq state (match-string 1))
  6100. (save-excursion
  6101. (org-back-to-heading t)
  6102. (hide-subtree)
  6103. (org-reveal)
  6104. (cond
  6105. ((equal state '("fold" "folded"))
  6106. (hide-subtree))
  6107. ((equal state "children")
  6108. (org-show-hidden-entry)
  6109. (show-children))
  6110. ((equal state "content")
  6111. (save-excursion
  6112. (save-restriction
  6113. (org-narrow-to-subtree)
  6114. (org-content))))
  6115. ((member state '("all" "showall"))
  6116. (show-subtree)))))
  6117. (unless no-cleanup
  6118. (org-cycle-hide-archived-subtrees 'all)
  6119. (org-cycle-hide-drawers 'all)
  6120. (org-cycle-show-empty-lines 'all)))))
  6121. ;; This function uses outline-regexp instead of the more fundamental
  6122. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6123. ;; buffers, where outline-regexp is needed.
  6124. (defun org-overview ()
  6125. "Switch to overview mode, showing only top-level headlines.
  6126. Really, this shows all headlines with level equal or greater than the level
  6127. of the first headline in the buffer. This is important, because if the
  6128. first headline is not level one, then (hide-sublevels 1) gives confusing
  6129. results."
  6130. (interactive)
  6131. (let ((l (org-current-line))
  6132. (level (save-excursion
  6133. (goto-char (point-min))
  6134. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6135. (progn
  6136. (goto-char (match-beginning 0))
  6137. (funcall outline-level))))))
  6138. (and level (hide-sublevels level))
  6139. (recenter '(4))
  6140. (org-goto-line l)))
  6141. (defun org-content (&optional arg)
  6142. "Show all headlines in the buffer, like a table of contents.
  6143. With numerical argument N, show content up to level N."
  6144. (interactive "P")
  6145. (save-excursion
  6146. ;; Visit all headings and show their offspring
  6147. (and (integerp arg) (org-overview))
  6148. (goto-char (point-max))
  6149. (catch 'exit
  6150. (while (and (progn (condition-case nil
  6151. (outline-previous-visible-heading 1)
  6152. (error (goto-char (point-min))))
  6153. t)
  6154. (looking-at org-outline-regexp))
  6155. (if (integerp arg)
  6156. (show-children (1- arg))
  6157. (show-branches))
  6158. (if (bobp) (throw 'exit nil))))))
  6159. (defun org-optimize-window-after-visibility-change (state)
  6160. "Adjust the window after a change in outline visibility.
  6161. This function is the default value of the hook `org-cycle-hook'."
  6162. (when (get-buffer-window (current-buffer))
  6163. (cond
  6164. ((eq state 'content) nil)
  6165. ((eq state 'all) nil)
  6166. ((eq state 'folded) nil)
  6167. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6168. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6169. (defun org-remove-empty-overlays-at (pos)
  6170. "Remove outline overlays that do not contain non-white stuff."
  6171. (mapc
  6172. (lambda (o)
  6173. (and (eq 'outline (overlay-get o 'invisible))
  6174. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6175. (overlay-end o))))
  6176. (delete-overlay o)))
  6177. (overlays-at pos)))
  6178. (defun org-clean-visibility-after-subtree-move ()
  6179. "Fix visibility issues after moving a subtree."
  6180. ;; First, find a reasonable region to look at:
  6181. ;; Start two siblings above, end three below
  6182. (let* ((beg (save-excursion
  6183. (and (org-get-last-sibling)
  6184. (org-get-last-sibling))
  6185. (point)))
  6186. (end (save-excursion
  6187. (and (org-get-next-sibling)
  6188. (org-get-next-sibling)
  6189. (org-get-next-sibling))
  6190. (if (org-at-heading-p)
  6191. (point-at-eol)
  6192. (point))))
  6193. (level (looking-at "\\*+"))
  6194. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6195. (save-excursion
  6196. (save-restriction
  6197. (narrow-to-region beg end)
  6198. (when re
  6199. ;; Properly fold already folded siblings
  6200. (goto-char (point-min))
  6201. (while (re-search-forward re nil t)
  6202. (if (and (not (outline-invisible-p))
  6203. (save-excursion
  6204. (goto-char (point-at-eol)) (outline-invisible-p)))
  6205. (hide-entry))))
  6206. (org-cycle-show-empty-lines 'overview)
  6207. (org-cycle-hide-drawers 'overview)))))
  6208. (defun org-cycle-show-empty-lines (state)
  6209. "Show empty lines above all visible headlines.
  6210. The region to be covered depends on STATE when called through
  6211. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6212. entire buffer covered. Note that an empty line is only shown if there
  6213. are at least `org-cycle-separator-lines' empty lines before the headline."
  6214. (when (not (= org-cycle-separator-lines 0))
  6215. (save-excursion
  6216. (let* ((n (abs org-cycle-separator-lines))
  6217. (re (cond
  6218. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6219. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6220. (t (let ((ns (number-to-string (- n 2))))
  6221. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6222. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6223. beg end b e)
  6224. (cond
  6225. ((memq state '(overview contents t))
  6226. (setq beg (point-min) end (point-max)))
  6227. ((memq state '(children folded))
  6228. (setq beg (point) end (progn (org-end-of-subtree t t)
  6229. (beginning-of-line 2)
  6230. (point)))))
  6231. (when beg
  6232. (goto-char beg)
  6233. (while (re-search-forward re end t)
  6234. (unless (get-char-property (match-end 1) 'invisible)
  6235. (setq e (match-end 1))
  6236. (if (< org-cycle-separator-lines 0)
  6237. (setq b (save-excursion
  6238. (goto-char (match-beginning 0))
  6239. (org-back-over-empty-lines)
  6240. (if (save-excursion
  6241. (goto-char (max (point-min) (1- (point))))
  6242. (org-at-heading-p))
  6243. (1- (point))
  6244. (point))))
  6245. (setq b (match-beginning 1)))
  6246. (outline-flag-region b e nil)))))))
  6247. ;; Never hide empty lines at the end of the file.
  6248. (save-excursion
  6249. (goto-char (point-max))
  6250. (outline-previous-heading)
  6251. (outline-end-of-heading)
  6252. (if (and (looking-at "[ \t\n]+")
  6253. (= (match-end 0) (point-max)))
  6254. (outline-flag-region (point) (match-end 0) nil))))
  6255. (defun org-show-empty-lines-in-parent ()
  6256. "Move to the parent and re-show empty lines before visible headlines."
  6257. (save-excursion
  6258. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6259. (org-cycle-show-empty-lines context))))
  6260. (defun org-files-list ()
  6261. "Return `org-agenda-files' list, plus all open org-mode files.
  6262. This is useful for operations that need to scan all of a user's
  6263. open and agenda-wise Org files."
  6264. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6265. (dolist (buf (buffer-list))
  6266. (with-current-buffer buf
  6267. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6268. (let ((file (expand-file-name (buffer-file-name))))
  6269. (unless (member file files)
  6270. (push file files))))))
  6271. files))
  6272. (defsubst org-entry-beginning-position ()
  6273. "Return the beginning position of the current entry."
  6274. (save-excursion (outline-back-to-heading t) (point)))
  6275. (defsubst org-entry-end-position ()
  6276. "Return the end position of the current entry."
  6277. (save-excursion (outline-next-heading) (point)))
  6278. (defun org-cycle-hide-drawers (state)
  6279. "Re-hide all drawers after a visibility state change."
  6280. (when (and (derived-mode-p 'org-mode)
  6281. (not (memq state '(overview folded contents))))
  6282. (save-excursion
  6283. (let* ((globalp (memq state '(contents all)))
  6284. (beg (if globalp (point-min) (point)))
  6285. (end (if globalp (point-max)
  6286. (if (eq state 'children)
  6287. (save-excursion (outline-next-heading) (point))
  6288. (org-end-of-subtree t)))))
  6289. (goto-char beg)
  6290. (while (re-search-forward org-drawer-regexp end t)
  6291. (org-flag-drawer t))))))
  6292. (defun org-cycle-hide-inline-tasks (state)
  6293. "Re-hide inline task when switching to 'contents visibility state."
  6294. (when (and (eq state 'contents)
  6295. (boundp 'org-inlinetask-min-level)
  6296. org-inlinetask-min-level)
  6297. (hide-sublevels (1- org-inlinetask-min-level))))
  6298. (defun org-flag-drawer (flag)
  6299. "When FLAG is non-nil, hide the drawer we are within.
  6300. Otherwise make it visible."
  6301. (save-excursion
  6302. (beginning-of-line 1)
  6303. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6304. (let ((b (match-end 0)))
  6305. (if (re-search-forward
  6306. "^[ \t]*:END:"
  6307. (save-excursion (outline-next-heading) (point)) t)
  6308. (outline-flag-region b (point-at-eol) flag)
  6309. (error ":END: line missing at position %s" b))))))
  6310. (defun org-subtree-end-visible-p ()
  6311. "Is the end of the current subtree visible?"
  6312. (pos-visible-in-window-p
  6313. (save-excursion (org-end-of-subtree t) (point))))
  6314. (defun org-first-headline-recenter (&optional N)
  6315. "Move cursor to the first headline and recenter the headline.
  6316. Optional argument N means put the headline into the Nth line of the window."
  6317. (goto-char (point-min))
  6318. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6319. (beginning-of-line)
  6320. (recenter (prefix-numeric-value N))))
  6321. ;;; Saving and restoring visibility
  6322. (defun org-outline-overlay-data (&optional use-markers)
  6323. "Return a list of the locations of all outline overlays.
  6324. These are overlays with the `invisible' property value `outline'.
  6325. The return value is a list of cons cells, with start and stop
  6326. positions for each overlay.
  6327. If USE-MARKERS is set, return the positions as markers."
  6328. (let (beg end)
  6329. (save-excursion
  6330. (save-restriction
  6331. (widen)
  6332. (delq nil
  6333. (mapcar (lambda (o)
  6334. (when (eq (overlay-get o 'invisible) 'outline)
  6335. (setq beg (overlay-start o)
  6336. end (overlay-end o))
  6337. (and beg end (> end beg)
  6338. (if use-markers
  6339. (cons (move-marker (make-marker) beg)
  6340. (move-marker (make-marker) end))
  6341. (cons beg end)))))
  6342. (overlays-in (point-min) (point-max))))))))
  6343. (defun org-set-outline-overlay-data (data)
  6344. "Create visibility overlays for all positions in DATA.
  6345. DATA should have been made by `org-outline-overlay-data'."
  6346. (let (o)
  6347. (save-excursion
  6348. (save-restriction
  6349. (widen)
  6350. (show-all)
  6351. (mapc (lambda (c)
  6352. (outline-flag-region (car c) (cdr c) t))
  6353. data)))))
  6354. ;;; Folding of blocks
  6355. (defvar org-hide-block-overlays nil
  6356. "Overlays hiding blocks.")
  6357. (make-variable-buffer-local 'org-hide-block-overlays)
  6358. (defun org-block-map (function &optional start end)
  6359. "Call FUNCTION at the head of all source blocks in the current buffer.
  6360. Optional arguments START and END can be used to limit the range."
  6361. (let ((start (or start (point-min)))
  6362. (end (or end (point-max))))
  6363. (save-excursion
  6364. (goto-char start)
  6365. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6366. (save-excursion
  6367. (save-match-data
  6368. (goto-char (match-beginning 0))
  6369. (funcall function)))))))
  6370. (defun org-hide-block-toggle-all ()
  6371. "Toggle the visibility of all blocks in the current buffer."
  6372. (org-block-map #'org-hide-block-toggle))
  6373. (defun org-hide-block-all ()
  6374. "Fold all blocks in the current buffer."
  6375. (interactive)
  6376. (org-show-block-all)
  6377. (org-block-map #'org-hide-block-toggle-maybe))
  6378. (defun org-show-block-all ()
  6379. "Unfold all blocks in the current buffer."
  6380. (interactive)
  6381. (mapc 'delete-overlay org-hide-block-overlays)
  6382. (setq org-hide-block-overlays nil))
  6383. (defun org-hide-block-toggle-maybe ()
  6384. "Toggle visibility of block at point."
  6385. (interactive)
  6386. (let ((case-fold-search t))
  6387. (if (save-excursion
  6388. (beginning-of-line 1)
  6389. (looking-at org-block-regexp))
  6390. (progn (org-hide-block-toggle)
  6391. t) ;; to signal that we took action
  6392. nil))) ;; to signal that we did not
  6393. (defun org-hide-block-toggle (&optional force)
  6394. "Toggle the visibility of the current block."
  6395. (interactive)
  6396. (save-excursion
  6397. (beginning-of-line)
  6398. (if (re-search-forward org-block-regexp nil t)
  6399. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6400. (end (match-end 0)) ;; end of entire body
  6401. ov)
  6402. (if (memq t (mapcar (lambda (overlay)
  6403. (eq (overlay-get overlay 'invisible)
  6404. 'org-hide-block))
  6405. (overlays-at start)))
  6406. (if (or (not force) (eq force 'off))
  6407. (mapc (lambda (ov)
  6408. (when (member ov org-hide-block-overlays)
  6409. (setq org-hide-block-overlays
  6410. (delq ov org-hide-block-overlays)))
  6411. (when (eq (overlay-get ov 'invisible)
  6412. 'org-hide-block)
  6413. (delete-overlay ov)))
  6414. (overlays-at start)))
  6415. (setq ov (make-overlay start end))
  6416. (overlay-put ov 'invisible 'org-hide-block)
  6417. ;; make the block accessible to isearch
  6418. (overlay-put
  6419. ov 'isearch-open-invisible
  6420. (lambda (ov)
  6421. (when (member ov org-hide-block-overlays)
  6422. (setq org-hide-block-overlays
  6423. (delq ov org-hide-block-overlays)))
  6424. (when (eq (overlay-get ov 'invisible)
  6425. 'org-hide-block)
  6426. (delete-overlay ov))))
  6427. (push ov org-hide-block-overlays)))
  6428. (error "Not looking at a source block"))))
  6429. ;; org-tab-after-check-for-cycling-hook
  6430. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6431. ;; Remove overlays when changing major mode
  6432. (add-hook 'org-mode-hook
  6433. (lambda () (org-add-hook 'change-major-mode-hook
  6434. 'org-show-block-all 'append 'local)))
  6435. ;;; Org-goto
  6436. (defvar org-goto-window-configuration nil)
  6437. (defvar org-goto-marker nil)
  6438. (defvar org-goto-map)
  6439. (defun org-goto-map ()
  6440. "Set the keymap `org-goto'."
  6441. (setq org-goto-map
  6442. (let ((map (make-sparse-keymap)))
  6443. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6444. mouse-drag-region universal-argument org-occur))
  6445. cmd)
  6446. (while (setq cmd (pop cmds))
  6447. (substitute-key-definition cmd cmd map global-map)))
  6448. (suppress-keymap map)
  6449. (org-defkey map "\C-m" 'org-goto-ret)
  6450. (org-defkey map [(return)] 'org-goto-ret)
  6451. (org-defkey map [(left)] 'org-goto-left)
  6452. (org-defkey map [(right)] 'org-goto-right)
  6453. (org-defkey map [(control ?g)] 'org-goto-quit)
  6454. (org-defkey map "\C-i" 'org-cycle)
  6455. (org-defkey map [(tab)] 'org-cycle)
  6456. (org-defkey map [(down)] 'outline-next-visible-heading)
  6457. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6458. (if org-goto-auto-isearch
  6459. (if (fboundp 'define-key-after)
  6460. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6461. nil)
  6462. (org-defkey map "q" 'org-goto-quit)
  6463. (org-defkey map "n" 'outline-next-visible-heading)
  6464. (org-defkey map "p" 'outline-previous-visible-heading)
  6465. (org-defkey map "f" 'outline-forward-same-level)
  6466. (org-defkey map "b" 'outline-backward-same-level)
  6467. (org-defkey map "u" 'outline-up-heading))
  6468. (org-defkey map "/" 'org-occur)
  6469. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6470. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6471. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6472. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6473. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6474. map)))
  6475. (defconst org-goto-help
  6476. "Browse buffer copy, to find location or copy text.%s
  6477. RET=jump to location C-g=quit and return to previous location
  6478. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6479. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6480. (defun org-goto (&optional alternative-interface)
  6481. "Look up a different location in the current file, keeping current visibility.
  6482. When you want look-up or go to a different location in a
  6483. document, the fastest way is often to fold the entire buffer and
  6484. then dive into the tree. This method has the disadvantage, that
  6485. the previous location will be folded, which may not be what you
  6486. want.
  6487. This command works around this by showing a copy of the current
  6488. buffer in an indirect buffer, in overview mode. You can dive
  6489. into the tree in that copy, use org-occur and incremental search
  6490. to find a location. When pressing RET or `Q', the command
  6491. returns to the original buffer in which the visibility is still
  6492. unchanged. After RET it will also jump to the location selected
  6493. in the indirect buffer and expose the headline hierarchy above.
  6494. With a prefix argument, use the alternative interface: e.g. if
  6495. `org-goto-interface' is 'outline use 'outline-path-completion."
  6496. (interactive "P")
  6497. (org-goto-map)
  6498. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6499. (org-refile-use-outline-path t)
  6500. (org-refile-target-verify-function nil)
  6501. (interface
  6502. (if (not alternative-interface)
  6503. org-goto-interface
  6504. (if (eq org-goto-interface 'outline)
  6505. 'outline-path-completion
  6506. 'outline)))
  6507. (org-goto-start-pos (point))
  6508. (selected-point
  6509. (if (eq interface 'outline)
  6510. (car (org-get-location (current-buffer) org-goto-help))
  6511. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6512. (org-refile-check-position pa)
  6513. (nth 3 pa)))))
  6514. (if selected-point
  6515. (progn
  6516. (org-mark-ring-push org-goto-start-pos)
  6517. (goto-char selected-point)
  6518. (if (or (outline-invisible-p) (org-invisible-p2))
  6519. (org-show-context 'org-goto)))
  6520. (message "Quit"))))
  6521. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6522. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6523. (defvar org-goto-local-auto-isearch-map) ; defined below
  6524. (defun org-get-location (buf help)
  6525. "Let the user select a location in the Org-mode buffer BUF.
  6526. This function uses a recursive edit. It returns the selected position
  6527. or nil."
  6528. (org-no-popups
  6529. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6530. (isearch-hide-immediately nil)
  6531. (isearch-search-fun-function
  6532. (lambda () 'org-goto-local-search-headings))
  6533. (org-goto-selected-point org-goto-exit-command))
  6534. (save-excursion
  6535. (save-window-excursion
  6536. (delete-other-windows)
  6537. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6538. (org-pop-to-buffer-same-window
  6539. (condition-case nil
  6540. (make-indirect-buffer (current-buffer) "*org-goto*")
  6541. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6542. (with-output-to-temp-buffer "*Org Help*"
  6543. (princ (format help (if org-goto-auto-isearch
  6544. " Just type for auto-isearch."
  6545. " n/p/f/b/u to navigate, q to quit."))))
  6546. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6547. (setq buffer-read-only nil)
  6548. (let ((org-startup-truncated t)
  6549. (org-startup-folded nil)
  6550. (org-startup-align-all-tables nil))
  6551. (org-mode)
  6552. (org-overview))
  6553. (setq buffer-read-only t)
  6554. (if (and (boundp 'org-goto-start-pos)
  6555. (integer-or-marker-p org-goto-start-pos))
  6556. (let ((org-show-hierarchy-above t)
  6557. (org-show-siblings t)
  6558. (org-show-following-heading t))
  6559. (goto-char org-goto-start-pos)
  6560. (and (outline-invisible-p) (org-show-context)))
  6561. (goto-char (point-min)))
  6562. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6563. (message "Select location and press RET")
  6564. (use-local-map org-goto-map)
  6565. (recursive-edit)))
  6566. (kill-buffer "*org-goto*")
  6567. (cons org-goto-selected-point org-goto-exit-command))))
  6568. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6569. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6570. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6571. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6572. (defun org-goto-local-search-headings (string bound noerror)
  6573. "Search and make sure that any matches are in headlines."
  6574. (catch 'return
  6575. (while (if isearch-forward
  6576. (search-forward string bound noerror)
  6577. (search-backward string bound noerror))
  6578. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6579. (and (member :headline context)
  6580. (not (member :tags context))))
  6581. (throw 'return (point))))))
  6582. (defun org-goto-local-auto-isearch ()
  6583. "Start isearch."
  6584. (interactive)
  6585. (goto-char (point-min))
  6586. (let ((keys (this-command-keys)))
  6587. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6588. (isearch-mode t)
  6589. (isearch-process-search-char (string-to-char keys)))))
  6590. (defun org-goto-ret (&optional arg)
  6591. "Finish `org-goto' by going to the new location."
  6592. (interactive "P")
  6593. (setq org-goto-selected-point (point)
  6594. org-goto-exit-command 'return)
  6595. (throw 'exit nil))
  6596. (defun org-goto-left ()
  6597. "Finish `org-goto' by going to the new location."
  6598. (interactive)
  6599. (if (org-at-heading-p)
  6600. (progn
  6601. (beginning-of-line 1)
  6602. (setq org-goto-selected-point (point)
  6603. org-goto-exit-command 'left)
  6604. (throw 'exit nil))
  6605. (error "Not on a heading")))
  6606. (defun org-goto-right ()
  6607. "Finish `org-goto' by going to the new location."
  6608. (interactive)
  6609. (if (org-at-heading-p)
  6610. (progn
  6611. (setq org-goto-selected-point (point)
  6612. org-goto-exit-command 'right)
  6613. (throw 'exit nil))
  6614. (error "Not on a heading")))
  6615. (defun org-goto-quit ()
  6616. "Finish `org-goto' without cursor motion."
  6617. (interactive)
  6618. (setq org-goto-selected-point nil)
  6619. (setq org-goto-exit-command 'quit)
  6620. (throw 'exit nil))
  6621. ;;; Indirect buffer display of subtrees
  6622. (defvar org-indirect-dedicated-frame nil
  6623. "This is the frame being used for indirect tree display.")
  6624. (defvar org-last-indirect-buffer nil)
  6625. (defun org-tree-to-indirect-buffer (&optional arg)
  6626. "Create indirect buffer and narrow it to current subtree.
  6627. With a numerical prefix ARG, go up to this level and then take that tree.
  6628. If ARG is negative, go up that many levels.
  6629. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6630. indirect buffer previously made with this command, to avoid proliferation of
  6631. indirect buffers. However, when you call the command with a \
  6632. \\[universal-argument] prefix, or
  6633. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6634. is kept so that you can work with several indirect buffers at the same time.
  6635. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6636. \\[universal-argument] prefix also
  6637. requests that a new frame be made for the new buffer, so that the dedicated
  6638. frame is not changed."
  6639. (interactive "P")
  6640. (let ((cbuf (current-buffer))
  6641. (cwin (selected-window))
  6642. (pos (point))
  6643. beg end level heading ibuf)
  6644. (save-excursion
  6645. (org-back-to-heading t)
  6646. (when (numberp arg)
  6647. (setq level (org-outline-level))
  6648. (if (< arg 0) (setq arg (+ level arg)))
  6649. (while (> (setq level (org-outline-level)) arg)
  6650. (org-up-heading-safe)))
  6651. (setq beg (point)
  6652. heading (org-get-heading))
  6653. (org-end-of-subtree t t)
  6654. (if (org-at-heading-p) (backward-char 1))
  6655. (setq end (point)))
  6656. (if (and (buffer-live-p org-last-indirect-buffer)
  6657. (not (eq org-indirect-buffer-display 'new-frame))
  6658. (not arg))
  6659. (kill-buffer org-last-indirect-buffer))
  6660. (setq ibuf (org-get-indirect-buffer cbuf)
  6661. org-last-indirect-buffer ibuf)
  6662. (cond
  6663. ((or (eq org-indirect-buffer-display 'new-frame)
  6664. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6665. (select-frame (make-frame))
  6666. (delete-other-windows)
  6667. (org-pop-to-buffer-same-window ibuf)
  6668. (org-set-frame-title heading))
  6669. ((eq org-indirect-buffer-display 'dedicated-frame)
  6670. (raise-frame
  6671. (select-frame (or (and org-indirect-dedicated-frame
  6672. (frame-live-p org-indirect-dedicated-frame)
  6673. org-indirect-dedicated-frame)
  6674. (setq org-indirect-dedicated-frame (make-frame)))))
  6675. (delete-other-windows)
  6676. (org-pop-to-buffer-same-window ibuf)
  6677. (org-set-frame-title (concat "Indirect: " heading)))
  6678. ((eq org-indirect-buffer-display 'current-window)
  6679. (org-pop-to-buffer-same-window ibuf))
  6680. ((eq org-indirect-buffer-display 'other-window)
  6681. (pop-to-buffer ibuf))
  6682. (t (error "Invalid value")))
  6683. (if (featurep 'xemacs)
  6684. (save-excursion (org-mode) (turn-on-font-lock)))
  6685. (narrow-to-region beg end)
  6686. (show-all)
  6687. (goto-char pos)
  6688. (run-hook-with-args 'org-cycle-hook 'all)
  6689. (and (window-live-p cwin) (select-window cwin))))
  6690. (defun org-get-indirect-buffer (&optional buffer)
  6691. (setq buffer (or buffer (current-buffer)))
  6692. (let ((n 1) (base (buffer-name buffer)) bname)
  6693. (while (buffer-live-p
  6694. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6695. (setq n (1+ n)))
  6696. (condition-case nil
  6697. (make-indirect-buffer buffer bname 'clone)
  6698. (error (make-indirect-buffer buffer bname)))))
  6699. (defun org-set-frame-title (title)
  6700. "Set the title of the current frame to the string TITLE."
  6701. ;; FIXME: how to name a single frame in XEmacs???
  6702. (unless (featurep 'xemacs)
  6703. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6704. ;;;; Structure editing
  6705. ;;; Inserting headlines
  6706. (defun org-previous-line-empty-p (&optional next)
  6707. "Is the previous line a blank line?
  6708. When NEXT is non-nil, check the next line instead."
  6709. (save-excursion
  6710. (and (not (bobp))
  6711. (or (beginning-of-line (if next 2 0)) t)
  6712. (save-match-data
  6713. (looking-at "[ \t]*$")))))
  6714. (defun org-insert-heading (&optional arg invisible-ok)
  6715. "Insert a new heading or item with same depth at point.
  6716. If point is in a plain list and ARG is nil, create a new list item.
  6717. With one universal prefix argument, insert a heading even in lists.
  6718. With two universal prefix arguments, insert the heading at the end
  6719. of the parent subtree.
  6720. If point is at the beginning of a headline, insert a sibling before
  6721. the current headline. If point is not at the beginning, split the line
  6722. and create a new headline with the text in the current line after point
  6723. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6724. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6725. This is important for non-interactive uses of the command."
  6726. (interactive "P")
  6727. (cond
  6728. ((or (= (buffer-size) 0)
  6729. (and (not (save-excursion
  6730. (and (ignore-errors (org-back-to-heading invisible-ok))
  6731. (org-at-heading-p))))
  6732. (or arg (not (org-in-item-p)))))
  6733. (insert
  6734. (if (org-previous-line-empty-p) "" "\n")
  6735. (if (org-in-src-block-p) ",* " "* "))
  6736. (run-hooks 'org-insert-heading-hook))
  6737. ((or arg (not (org-insert-item
  6738. (save-excursion
  6739. (beginning-of-line)
  6740. (looking-at org-list-full-item-re)
  6741. (match-string 3)))))
  6742. (let (begn endn)
  6743. (when (org-buffer-narrowed-p)
  6744. (setq begn (point-min) endn (point-max))
  6745. (widen))
  6746. (let* ((empty-line-p nil)
  6747. (eops (equal arg '(16))) ; insert at end of parent subtree
  6748. (org-insert-heading-respect-content
  6749. (or (not (null arg)) org-insert-heading-respect-content))
  6750. (level nil)
  6751. (on-heading (org-at-heading-p))
  6752. (head (save-excursion
  6753. (condition-case nil
  6754. (progn
  6755. (org-back-to-heading invisible-ok)
  6756. (when (and (not on-heading)
  6757. (featurep 'org-inlinetask)
  6758. (integerp org-inlinetask-min-level)
  6759. (>= (length (match-string 0))
  6760. org-inlinetask-min-level))
  6761. ;; Find a heading level before the inline task
  6762. (while (and (setq level (org-up-heading-safe))
  6763. (>= level org-inlinetask-min-level)))
  6764. (if (org-at-heading-p)
  6765. (org-back-to-heading invisible-ok)
  6766. (error "This should not happen")))
  6767. (unless (and (save-excursion
  6768. (save-match-data
  6769. (org-backward-heading-same-level 1 invisible-ok))
  6770. (= (point) (match-beginning 0)))
  6771. (not (org-previous-line-empty-p t)))
  6772. (setq empty-line-p (org-previous-line-empty-p)))
  6773. (match-string 0))
  6774. (error "* "))))
  6775. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6776. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6777. pos hide-previous previous-pos)
  6778. (if ;; At the beginning of a heading, open a new line for insertiong
  6779. (and (bolp) (org-at-heading-p)
  6780. (not eops)
  6781. (or (bobp)
  6782. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6783. (open-line (if blank 2 1))
  6784. (save-excursion
  6785. (setq previous-pos (point-at-bol))
  6786. (end-of-line)
  6787. (setq hide-previous (outline-invisible-p)))
  6788. (and org-insert-heading-respect-content
  6789. (save-excursion
  6790. (while (outline-invisible-p)
  6791. (org-show-subtree)
  6792. (org-up-heading-safe))))
  6793. (let ((split
  6794. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6795. (save-excursion
  6796. (let ((p (point)))
  6797. (goto-char (point-at-bol))
  6798. (and (looking-at org-complex-heading-regexp)
  6799. (match-beginning 4)
  6800. (> p (match-beginning 4)))))))
  6801. tags pos)
  6802. (cond
  6803. ;; Insert a new line, possibly at end of parent subtree
  6804. (org-insert-heading-respect-content
  6805. (if (not eops)
  6806. (progn
  6807. (org-end-of-subtree nil t)
  6808. (and (looking-at "^\\*") (backward-char 1))
  6809. (while (and (not (bobp))
  6810. ;; Don't delete spaces in empty headlines
  6811. (not (looking-back org-outline-regexp))
  6812. (member (char-before) '(?\ ?\t ?\n)))
  6813. (backward-delete-char 1)))
  6814. (let ((p (point)))
  6815. (org-up-heading-safe)
  6816. (if (= p (point))
  6817. (goto-char (point-max))
  6818. (org-end-of-subtree nil t))))
  6819. (when (featurep 'org-inlinetask)
  6820. (while (and (not (eobp))
  6821. (looking-at "\\(\\*+\\)[ \t]+")
  6822. (>= (length (match-string 1))
  6823. org-inlinetask-min-level))
  6824. (org-end-of-subtree nil t)))
  6825. (or (bolp) (newline))
  6826. (or (org-previous-line-empty-p)
  6827. (and blank (newline)))
  6828. (if (or empty-line-p eops) (open-line 1)))
  6829. ;; Insert a headling containing text after point
  6830. ((org-at-heading-p)
  6831. (when hide-previous
  6832. (show-children)
  6833. (org-show-entry))
  6834. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6835. (setq tags (and (match-end 2) (match-string 2)))
  6836. (and (match-end 1)
  6837. (delete-region (match-beginning 1) (match-end 1)))
  6838. (setq pos (point-at-bol))
  6839. (or split (end-of-line 1))
  6840. (delete-horizontal-space)
  6841. (if (string-match "\\`\\*+\\'"
  6842. (buffer-substring (point-at-bol) (point)))
  6843. (insert " "))
  6844. (newline (if blank 2 1))
  6845. (when tags
  6846. (save-excursion
  6847. (goto-char pos)
  6848. (end-of-line 1)
  6849. (insert " " tags)
  6850. (org-set-tags nil 'align))))
  6851. (t
  6852. (or split (end-of-line 1))
  6853. (newline (if blank 2 1))))))
  6854. (insert head) (just-one-space)
  6855. (setq pos (point))
  6856. (end-of-line 1)
  6857. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6858. (when (and org-insert-heading-respect-content hide-previous)
  6859. (save-excursion
  6860. (goto-char previous-pos)
  6861. (hide-subtree)))
  6862. (when (and begn endn)
  6863. (narrow-to-region (min (point) begn) (max (point) endn)))
  6864. (run-hooks 'org-insert-heading-hook))))))
  6865. (defun org-get-heading (&optional no-tags no-todo)
  6866. "Return the heading of the current entry, without the stars.
  6867. When NO-TAGS is non-nil, don't include tags.
  6868. When NO-TODO is non-nil, don't include TODO keywords."
  6869. (save-excursion
  6870. (org-back-to-heading t)
  6871. (cond
  6872. ((and no-tags no-todo)
  6873. (looking-at org-complex-heading-regexp)
  6874. (match-string 4))
  6875. (no-tags
  6876. (looking-at (concat org-outline-regexp
  6877. "\\(.*?\\)"
  6878. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6879. (match-string 1))
  6880. (no-todo
  6881. (looking-at org-todo-line-regexp)
  6882. (match-string 3))
  6883. (t (looking-at org-heading-regexp)
  6884. (match-string 2)))))
  6885. (defvar orgstruct-mode) ; defined below
  6886. (defun org-heading-components ()
  6887. "Return the components of the current heading.
  6888. This is a list with the following elements:
  6889. - the level as an integer
  6890. - the reduced level, different if `org-odd-levels-only' is set.
  6891. - the TODO keyword, or nil
  6892. - the priority character, like ?A, or nil if no priority is given
  6893. - the headline text itself, or the tags string if no headline text
  6894. - the tags string, or nil."
  6895. (save-excursion
  6896. (org-back-to-heading t)
  6897. (if (let (case-fold-search)
  6898. (looking-at
  6899. (if orgstruct-mode
  6900. org-heading-regexp
  6901. org-complex-heading-regexp)))
  6902. (if orgstruct-mode
  6903. (list (length (match-string 1))
  6904. (org-reduced-level (length (match-string 1)))
  6905. nil
  6906. nil
  6907. (match-string 2)
  6908. nil)
  6909. (list (length (match-string 1))
  6910. (org-reduced-level (length (match-string 1)))
  6911. (org-match-string-no-properties 2)
  6912. (and (match-end 3) (aref (match-string 3) 2))
  6913. (org-match-string-no-properties 4)
  6914. (org-match-string-no-properties 5))))))
  6915. (defun org-get-entry ()
  6916. "Get the entry text, after heading, entire subtree."
  6917. (save-excursion
  6918. (org-back-to-heading t)
  6919. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6920. (defun org-insert-heading-after-current ()
  6921. "Insert a new heading with same level as current, after current subtree."
  6922. (interactive)
  6923. (org-back-to-heading)
  6924. (org-insert-heading)
  6925. (org-move-subtree-down)
  6926. (end-of-line 1))
  6927. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6928. "Insert heading with `org-insert-heading-respect-content' set to t."
  6929. (interactive "P")
  6930. (let ((org-insert-heading-respect-content t))
  6931. (org-insert-heading arg invisible-ok)))
  6932. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6933. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6934. (interactive "P")
  6935. (let ((org-insert-heading-respect-content t))
  6936. (org-insert-todo-heading force-state t)))
  6937. (defun org-insert-todo-heading (arg &optional force-heading)
  6938. "Insert a new heading with the same level and TODO state as current heading.
  6939. If the heading has no TODO state, or if the state is DONE, use the first
  6940. state (TODO by default). Also one prefix arg, force first state. With two
  6941. prefix args, force inserting at the end of the parent subtree."
  6942. (interactive "P")
  6943. (when (or force-heading (not (org-insert-item 'checkbox)))
  6944. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6945. force-heading))
  6946. (save-excursion
  6947. (org-back-to-heading)
  6948. (outline-previous-heading)
  6949. (looking-at org-todo-line-regexp))
  6950. (let*
  6951. ((new-mark-x
  6952. (if (or arg
  6953. (not (match-beginning 2))
  6954. (member (match-string 2) org-done-keywords))
  6955. (car org-todo-keywords-1)
  6956. (match-string 2)))
  6957. (new-mark
  6958. (or
  6959. (run-hook-with-args-until-success
  6960. 'org-todo-get-default-hook new-mark-x nil)
  6961. new-mark-x)))
  6962. (beginning-of-line 1)
  6963. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6964. (if org-treat-insert-todo-heading-as-state-change
  6965. (org-todo new-mark)
  6966. (insert new-mark " "))))
  6967. (when org-provide-todo-statistics
  6968. (org-update-parent-todo-statistics))))
  6969. (defun org-insert-subheading (arg)
  6970. "Insert a new subheading and demote it.
  6971. Works for outline headings and for plain lists alike."
  6972. (interactive "P")
  6973. (org-insert-heading arg)
  6974. (cond
  6975. ((org-at-heading-p) (org-do-demote))
  6976. ((org-at-item-p) (org-indent-item))))
  6977. (defun org-insert-todo-subheading (arg)
  6978. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6979. Works for outline headings and for plain lists alike."
  6980. (interactive "P")
  6981. (org-insert-todo-heading arg)
  6982. (cond
  6983. ((org-at-heading-p) (org-do-demote))
  6984. ((org-at-item-p) (org-indent-item))))
  6985. ;;; Promotion and Demotion
  6986. (defvar org-after-demote-entry-hook nil
  6987. "Hook run after an entry has been demoted.
  6988. The cursor will be at the beginning of the entry.
  6989. When a subtree is being demoted, the hook will be called for each node.")
  6990. (defvar org-after-promote-entry-hook nil
  6991. "Hook run after an entry has been promoted.
  6992. The cursor will be at the beginning of the entry.
  6993. When a subtree is being promoted, the hook will be called for each node.")
  6994. (defun org-promote-subtree ()
  6995. "Promote the entire subtree.
  6996. See also `org-promote'."
  6997. (interactive)
  6998. (save-excursion
  6999. (org-with-limited-levels (org-map-tree 'org-promote)))
  7000. (org-fix-position-after-promote))
  7001. (defun org-demote-subtree ()
  7002. "Demote the entire subtree. See `org-demote'.
  7003. See also `org-promote'."
  7004. (interactive)
  7005. (save-excursion
  7006. (org-with-limited-levels (org-map-tree 'org-demote)))
  7007. (org-fix-position-after-promote))
  7008. (defun org-do-promote ()
  7009. "Promote the current heading higher up the tree.
  7010. If the region is active in `transient-mark-mode', promote all headings
  7011. in the region."
  7012. (interactive)
  7013. (save-excursion
  7014. (if (org-region-active-p)
  7015. (org-map-region 'org-promote (region-beginning) (region-end))
  7016. (org-promote)))
  7017. (org-fix-position-after-promote))
  7018. (defun org-do-demote ()
  7019. "Demote the current heading lower down the tree.
  7020. If the region is active in `transient-mark-mode', demote all headings
  7021. in the region."
  7022. (interactive)
  7023. (save-excursion
  7024. (if (org-region-active-p)
  7025. (org-map-region 'org-demote (region-beginning) (region-end))
  7026. (org-demote)))
  7027. (org-fix-position-after-promote))
  7028. (defun org-fix-position-after-promote ()
  7029. "Make sure that after pro/demotion cursor position is right."
  7030. (let ((pos (point)))
  7031. (when (save-excursion
  7032. (beginning-of-line 1)
  7033. (looking-at org-todo-line-regexp)
  7034. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7035. (cond ((eobp) (insert " "))
  7036. ((eolp) (insert " "))
  7037. ((equal (char-after) ?\ ) (forward-char 1))))))
  7038. (defun org-current-level ()
  7039. "Return the level of the current entry, or nil if before the first headline.
  7040. The level is the number of stars at the beginning of the headline."
  7041. (save-excursion
  7042. (org-with-limited-levels
  7043. (if (ignore-errors (org-back-to-heading t))
  7044. (funcall outline-level)))))
  7045. (defun org-get-previous-line-level ()
  7046. "Return the outline depth of the last headline before the current line.
  7047. Returns 0 for the first headline in the buffer, and nil if before the
  7048. first headline."
  7049. (let ((current-level (org-current-level))
  7050. (prev-level (when (> (line-number-at-pos) 1)
  7051. (save-excursion
  7052. (beginning-of-line 0)
  7053. (org-current-level)))))
  7054. (cond ((null current-level) nil) ; Before first headline
  7055. ((null prev-level) 0) ; At first headline
  7056. (prev-level))))
  7057. (defun org-reduced-level (l)
  7058. "Compute the effective level of a heading.
  7059. This takes into account the setting of `org-odd-levels-only'."
  7060. (cond
  7061. ((zerop l) 0)
  7062. (org-odd-levels-only (1+ (floor (/ l 2))))
  7063. (t l)))
  7064. (defun org-level-increment ()
  7065. "Return the number of stars that will be added or removed at a
  7066. time to headlines when structure editing, based on the value of
  7067. `org-odd-levels-only'."
  7068. (if org-odd-levels-only 2 1))
  7069. (defun org-get-valid-level (level &optional change)
  7070. "Rectify a level change under the influence of `org-odd-levels-only'
  7071. LEVEL is a current level, CHANGE is by how much the level should be
  7072. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7073. even level numbers will become the next higher odd number."
  7074. (if org-odd-levels-only
  7075. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7076. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7077. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7078. (max 1 (+ level (or change 0)))))
  7079. (if (boundp 'define-obsolete-function-alias)
  7080. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7081. (define-obsolete-function-alias 'org-get-legal-level
  7082. 'org-get-valid-level)
  7083. (define-obsolete-function-alias 'org-get-legal-level
  7084. 'org-get-valid-level "23.1")))
  7085. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7086. ;; ̀org-with-limited-levels'
  7087. (defun org-promote ()
  7088. "Promote the current heading higher up the tree.
  7089. If the region is active in `transient-mark-mode', promote all headings
  7090. in the region."
  7091. (org-back-to-heading t)
  7092. (let* ((level (save-match-data (funcall outline-level)))
  7093. (after-change-functions (remove 'flyspell-after-change-function
  7094. after-change-functions))
  7095. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7096. (diff (abs (- level (length up-head) -1))))
  7097. (cond ((and (= level 1) org-called-with-limited-levels
  7098. org-allow-promoting-top-level-subtree)
  7099. (replace-match "# " nil t))
  7100. ((= level 1)
  7101. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  7102. (t (replace-match up-head nil t)))
  7103. ;; Fixup tag positioning
  7104. (unless (= level 1)
  7105. (and org-auto-align-tags (org-set-tags nil t))
  7106. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7107. (run-hooks 'org-after-promote-entry-hook)))
  7108. (defun org-demote ()
  7109. "Demote the current heading lower down the tree.
  7110. If the region is active in `transient-mark-mode', demote all headings
  7111. in the region."
  7112. (org-back-to-heading t)
  7113. (let* ((level (save-match-data (funcall outline-level)))
  7114. (after-change-functions (remove 'flyspell-after-change-function
  7115. after-change-functions))
  7116. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7117. (diff (abs (- level (length down-head) -1))))
  7118. (replace-match down-head nil t)
  7119. ;; Fixup tag positioning
  7120. (and org-auto-align-tags (org-set-tags nil t))
  7121. (if org-adapt-indentation (org-fixup-indentation diff))
  7122. (run-hooks 'org-after-demote-entry-hook)))
  7123. (defun org-cycle-level ()
  7124. "Cycle the level of an empty headline through possible states.
  7125. This goes first to child, then to parent, level, then up the hierarchy.
  7126. After top level, it switches back to sibling level."
  7127. (interactive)
  7128. (let ((org-adapt-indentation nil))
  7129. (when (org-point-at-end-of-empty-headline)
  7130. (setq this-command 'org-cycle-level) ; Only needed for caching
  7131. (let ((cur-level (org-current-level))
  7132. (prev-level (org-get-previous-line-level)))
  7133. (cond
  7134. ;; If first headline in file, promote to top-level.
  7135. ((= prev-level 0)
  7136. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7137. do (org-do-promote)))
  7138. ;; If same level as prev, demote one.
  7139. ((= prev-level cur-level)
  7140. (org-do-demote))
  7141. ;; If parent is top-level, promote to top level if not already.
  7142. ((= prev-level 1)
  7143. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7144. do (org-do-promote)))
  7145. ;; If top-level, return to prev-level.
  7146. ((= cur-level 1)
  7147. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7148. do (org-do-demote)))
  7149. ;; If less than prev-level, promote one.
  7150. ((< cur-level prev-level)
  7151. (org-do-promote))
  7152. ;; If deeper than prev-level, promote until higher than
  7153. ;; prev-level.
  7154. ((> cur-level prev-level)
  7155. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7156. do (org-do-promote))))
  7157. t))))
  7158. (defun org-map-tree (fun)
  7159. "Call FUN for every heading underneath the current one."
  7160. (org-back-to-heading)
  7161. (let ((level (funcall outline-level)))
  7162. (save-excursion
  7163. (funcall fun)
  7164. (while (and (progn
  7165. (outline-next-heading)
  7166. (> (funcall outline-level) level))
  7167. (not (eobp)))
  7168. (funcall fun)))))
  7169. (defun org-map-region (fun beg end)
  7170. "Call FUN for every heading between BEG and END."
  7171. (let ((org-ignore-region t))
  7172. (save-excursion
  7173. (setq end (copy-marker end))
  7174. (goto-char beg)
  7175. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7176. (< (point) end))
  7177. (funcall fun))
  7178. (while (and (progn
  7179. (outline-next-heading)
  7180. (< (point) end))
  7181. (not (eobp)))
  7182. (funcall fun)))))
  7183. (defvar org-property-end-re) ; silence byte-compiler
  7184. (defun org-fixup-indentation (diff)
  7185. "Change the indentation in the current entry by DIFF.
  7186. However, if any line in the current entry has no indentation, or if it
  7187. would end up with no indentation after the change, nothing at all is done."
  7188. (save-excursion
  7189. (let ((end (save-excursion (outline-next-heading)
  7190. (point-marker)))
  7191. (prohibit (if (> diff 0)
  7192. "^\\S-"
  7193. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7194. col)
  7195. (unless (save-excursion (end-of-line 1)
  7196. (re-search-forward prohibit end t))
  7197. (while (and (< (point) end)
  7198. (re-search-forward "^[ \t]+" end t))
  7199. (goto-char (match-end 0))
  7200. (setq col (current-column))
  7201. (if (< diff 0) (replace-match ""))
  7202. (org-indent-to-column (+ diff col))))
  7203. (move-marker end nil))))
  7204. (defun org-convert-to-odd-levels ()
  7205. "Convert an org-mode file with all levels allowed to one with odd levels.
  7206. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7207. level 5 etc."
  7208. (interactive)
  7209. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7210. (let ((outline-level 'org-outline-level)
  7211. (org-odd-levels-only nil) n)
  7212. (save-excursion
  7213. (goto-char (point-min))
  7214. (while (re-search-forward "^\\*\\*+ " nil t)
  7215. (setq n (- (length (match-string 0)) 2))
  7216. (while (>= (setq n (1- n)) 0)
  7217. (org-demote))
  7218. (end-of-line 1))))))
  7219. (defun org-convert-to-oddeven-levels ()
  7220. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7221. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7222. file contains a section with an even level, conversion would
  7223. destroy the structure of the file. An error is signaled in this
  7224. case."
  7225. (interactive)
  7226. (goto-char (point-min))
  7227. ;; First check if there are no even levels
  7228. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7229. (org-show-context t)
  7230. (error "Not all levels are odd in this file. Conversion not possible"))
  7231. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7232. (let ((outline-regexp org-outline-regexp)
  7233. (outline-level 'org-outline-level)
  7234. (org-odd-levels-only nil) n)
  7235. (save-excursion
  7236. (goto-char (point-min))
  7237. (while (re-search-forward "^\\*\\*+ " nil t)
  7238. (setq n (/ (1- (length (match-string 0))) 2))
  7239. (while (>= (setq n (1- n)) 0)
  7240. (org-promote))
  7241. (end-of-line 1))))))
  7242. (defun org-tr-level (n)
  7243. "Make N odd if required."
  7244. (if org-odd-levels-only (1+ (/ n 2)) n))
  7245. ;;; Vertical tree motion, cutting and pasting of subtrees
  7246. (defun org-move-subtree-up (&optional arg)
  7247. "Move the current subtree up past ARG headlines of the same level."
  7248. (interactive "p")
  7249. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7250. (defun org-move-subtree-down (&optional arg)
  7251. "Move the current subtree down past ARG headlines of the same level."
  7252. (interactive "p")
  7253. (setq arg (prefix-numeric-value arg))
  7254. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7255. 'org-get-last-sibling))
  7256. (ins-point (make-marker))
  7257. (cnt (abs arg))
  7258. (col (current-column))
  7259. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7260. ;; Select the tree
  7261. (org-back-to-heading)
  7262. (setq beg0 (point))
  7263. (save-excursion
  7264. (setq ne-beg (org-back-over-empty-lines))
  7265. (setq beg (point)))
  7266. (save-match-data
  7267. (save-excursion (outline-end-of-heading)
  7268. (setq folded (outline-invisible-p)))
  7269. (outline-end-of-subtree))
  7270. (outline-next-heading)
  7271. (setq ne-end (org-back-over-empty-lines))
  7272. (setq end (point))
  7273. (goto-char beg0)
  7274. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7275. ;; include less whitespace
  7276. (save-excursion
  7277. (goto-char beg)
  7278. (forward-line (- ne-beg ne-end))
  7279. (setq beg (point))))
  7280. ;; Find insertion point, with error handling
  7281. (while (> cnt 0)
  7282. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7283. (progn (goto-char beg0)
  7284. (user-error "Cannot move past superior level or buffer limit")))
  7285. (setq cnt (1- cnt)))
  7286. (if (> arg 0)
  7287. ;; Moving forward - still need to move over subtree
  7288. (progn (org-end-of-subtree t t)
  7289. (save-excursion
  7290. (org-back-over-empty-lines)
  7291. (or (bolp) (newline)))))
  7292. (setq ne-ins (org-back-over-empty-lines))
  7293. (move-marker ins-point (point))
  7294. (setq txt (buffer-substring beg end))
  7295. (org-save-markers-in-region beg end)
  7296. (delete-region beg end)
  7297. (org-remove-empty-overlays-at beg)
  7298. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7299. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7300. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7301. (let ((bbb (point)))
  7302. (insert-before-markers txt)
  7303. (org-reinstall-markers-in-region bbb)
  7304. (move-marker ins-point bbb))
  7305. (or (bolp) (insert "\n"))
  7306. (setq ins-end (point))
  7307. (goto-char ins-point)
  7308. (org-skip-whitespace)
  7309. (when (and (< arg 0)
  7310. (org-first-sibling-p)
  7311. (> ne-ins ne-beg))
  7312. ;; Move whitespace back to beginning
  7313. (save-excursion
  7314. (goto-char ins-end)
  7315. (let ((kill-whole-line t))
  7316. (kill-line (- ne-ins ne-beg)) (point)))
  7317. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7318. (move-marker ins-point nil)
  7319. (if folded
  7320. (hide-subtree)
  7321. (org-show-entry)
  7322. (show-children)
  7323. (org-cycle-hide-drawers 'children))
  7324. (org-clean-visibility-after-subtree-move)
  7325. ;; move back to the initial column we were at
  7326. (move-to-column col)))
  7327. (defvar org-subtree-clip ""
  7328. "Clipboard for cut and paste of subtrees.
  7329. This is actually only a copy of the kill, because we use the normal kill
  7330. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7331. (defvar org-subtree-clip-folded nil
  7332. "Was the last copied subtree folded?
  7333. This is used to fold the tree back after pasting.")
  7334. (defun org-cut-subtree (&optional n)
  7335. "Cut the current subtree into the clipboard.
  7336. With prefix arg N, cut this many sequential subtrees.
  7337. This is a short-hand for marking the subtree and then cutting it."
  7338. (interactive "p")
  7339. (org-copy-subtree n 'cut))
  7340. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7341. "Cut the current subtree into the clipboard.
  7342. With prefix arg N, cut this many sequential subtrees.
  7343. This is a short-hand for marking the subtree and then copying it.
  7344. If CUT is non-nil, actually cut the subtree.
  7345. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7346. of some markers in the region, even if CUT is non-nil. This is
  7347. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7348. (interactive "p")
  7349. (let (beg end folded (beg0 (point)))
  7350. (if (org-called-interactively-p 'any)
  7351. (org-back-to-heading nil) ; take what looks like a subtree
  7352. (org-back-to-heading t)) ; take what is really there
  7353. (setq beg (point))
  7354. (skip-chars-forward " \t\r\n")
  7355. (save-match-data
  7356. (if nosubtrees
  7357. (outline-next-heading)
  7358. (save-excursion (outline-end-of-heading)
  7359. (setq folded (outline-invisible-p)))
  7360. (condition-case nil
  7361. (org-forward-heading-same-level (1- n) t)
  7362. (error nil))
  7363. (org-end-of-subtree t t)))
  7364. (setq end (point))
  7365. (goto-char beg0)
  7366. (when (> end beg)
  7367. (setq org-subtree-clip-folded folded)
  7368. (when (or cut force-store-markers)
  7369. (org-save-markers-in-region beg end))
  7370. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7371. (setq org-subtree-clip (current-kill 0))
  7372. (message "%s: Subtree(s) with %d characters"
  7373. (if cut "Cut" "Copied")
  7374. (length org-subtree-clip)))))
  7375. (defun org-paste-subtree (&optional level tree for-yank)
  7376. "Paste the clipboard as a subtree, with modification of headline level.
  7377. The entire subtree is promoted or demoted in order to match a new headline
  7378. level.
  7379. If the cursor is at the beginning of a headline, the same level as
  7380. that headline is used to paste the tree.
  7381. If not, the new level is derived from the *visible* headings
  7382. before and after the insertion point, and taken to be the inferior headline
  7383. level of the two. So if the previous visible heading is level 3 and the
  7384. next is level 4 (or vice versa), level 4 will be used for insertion.
  7385. This makes sure that the subtree remains an independent subtree and does
  7386. not swallow low level entries.
  7387. You can also force a different level, either by using a numeric prefix
  7388. argument, or by inserting the heading marker by hand. For example, if the
  7389. cursor is after \"*****\", then the tree will be shifted to level 5.
  7390. If optional TREE is given, use this text instead of the kill ring.
  7391. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7392. move back over whitespace before inserting, and move point to the end of
  7393. the inserted text when done."
  7394. (interactive "P")
  7395. (setq tree (or tree (and kill-ring (current-kill 0))))
  7396. (unless (org-kill-is-subtree-p tree)
  7397. (error "%s"
  7398. (substitute-command-keys
  7399. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7400. (org-with-limited-levels
  7401. (let* ((visp (not (outline-invisible-p)))
  7402. (txt tree)
  7403. (^re_ "\\(\\*+\\)[ \t]*")
  7404. (old-level (if (string-match org-outline-regexp-bol txt)
  7405. (- (match-end 0) (match-beginning 0) 1)
  7406. -1))
  7407. (force-level (cond (level (prefix-numeric-value level))
  7408. ((and (looking-at "[ \t]*$")
  7409. (string-match
  7410. "^\\*+$" (buffer-substring
  7411. (point-at-bol) (point))))
  7412. (- (match-end 1) (match-beginning 1)))
  7413. ((and (bolp)
  7414. (looking-at org-outline-regexp))
  7415. (- (match-end 0) (point) 1))))
  7416. (previous-level (save-excursion
  7417. (condition-case nil
  7418. (progn
  7419. (outline-previous-visible-heading 1)
  7420. (if (looking-at ^re_)
  7421. (- (match-end 0) (match-beginning 0) 1)
  7422. 1))
  7423. (error 1))))
  7424. (next-level (save-excursion
  7425. (condition-case nil
  7426. (progn
  7427. (or (looking-at org-outline-regexp)
  7428. (outline-next-visible-heading 1))
  7429. (if (looking-at ^re_)
  7430. (- (match-end 0) (match-beginning 0) 1)
  7431. 1))
  7432. (error 1))))
  7433. (new-level (or force-level (max previous-level next-level)))
  7434. (shift (if (or (= old-level -1)
  7435. (= new-level -1)
  7436. (= old-level new-level))
  7437. 0
  7438. (- new-level old-level)))
  7439. (delta (if (> shift 0) -1 1))
  7440. (func (if (> shift 0) 'org-demote 'org-promote))
  7441. (org-odd-levels-only nil)
  7442. beg end newend)
  7443. ;; Remove the forced level indicator
  7444. (if force-level
  7445. (delete-region (point-at-bol) (point)))
  7446. ;; Paste
  7447. (beginning-of-line (if (bolp) 1 2))
  7448. (setq beg (point))
  7449. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7450. (insert-before-markers txt)
  7451. (unless (string-match "\n\\'" txt) (insert "\n"))
  7452. (setq newend (point))
  7453. (org-reinstall-markers-in-region beg)
  7454. (setq end (point))
  7455. (goto-char beg)
  7456. (skip-chars-forward " \t\n\r")
  7457. (setq beg (point))
  7458. (if (and (outline-invisible-p) visp)
  7459. (save-excursion (outline-show-heading)))
  7460. ;; Shift if necessary
  7461. (unless (= shift 0)
  7462. (save-restriction
  7463. (narrow-to-region beg end)
  7464. (while (not (= shift 0))
  7465. (org-map-region func (point-min) (point-max))
  7466. (setq shift (+ delta shift)))
  7467. (goto-char (point-min))
  7468. (setq newend (point-max))))
  7469. (when (or (org-called-interactively-p 'interactive) for-yank)
  7470. (message "Clipboard pasted as level %d subtree" new-level))
  7471. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7472. kill-ring
  7473. (eq org-subtree-clip (current-kill 0))
  7474. org-subtree-clip-folded)
  7475. ;; The tree was folded before it was killed/copied
  7476. (hide-subtree))
  7477. (and for-yank (goto-char newend)))))
  7478. (defun org-kill-is-subtree-p (&optional txt)
  7479. "Check if the current kill is an outline subtree, or a set of trees.
  7480. Returns nil if kill does not start with a headline, or if the first
  7481. headline level is not the largest headline level in the tree.
  7482. So this will actually accept several entries of equal levels as well,
  7483. which is OK for `org-paste-subtree'.
  7484. If optional TXT is given, check this string instead of the current kill."
  7485. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7486. (re (org-get-limited-outline-regexp))
  7487. (^re (concat "^" re))
  7488. (start-level (and kill
  7489. (string-match
  7490. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7491. kill)
  7492. (- (match-end 2) (match-beginning 2) 1)))
  7493. (start (1+ (or (match-beginning 2) -1))))
  7494. (if (not start-level)
  7495. (progn
  7496. nil) ;; does not even start with a heading
  7497. (catch 'exit
  7498. (while (setq start (string-match ^re kill (1+ start)))
  7499. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7500. (throw 'exit nil)))
  7501. t))))
  7502. (defvar org-markers-to-move nil
  7503. "Markers that should be moved with a cut-and-paste operation.
  7504. Those markers are stored together with their positions relative to
  7505. the start of the region.")
  7506. (defun org-save-markers-in-region (beg end)
  7507. "Check markers in region.
  7508. If these markers are between BEG and END, record their position relative
  7509. to BEG, so that after moving the block of text, we can put the markers back
  7510. into place.
  7511. This function gets called just before an entry or tree gets cut from the
  7512. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7513. called immediately, to move the markers with the entries."
  7514. (setq org-markers-to-move nil)
  7515. (when (featurep 'org-clock)
  7516. (org-clock-save-markers-for-cut-and-paste beg end))
  7517. (when (featurep 'org-agenda)
  7518. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7519. (defun org-check-and-save-marker (marker beg end)
  7520. "Check if MARKER is between BEG and END.
  7521. If yes, remember the marker and the distance to BEG."
  7522. (when (and (marker-buffer marker)
  7523. (equal (marker-buffer marker) (current-buffer)))
  7524. (if (and (>= marker beg) (< marker end))
  7525. (push (cons marker (- marker beg)) org-markers-to-move))))
  7526. (defun org-reinstall-markers-in-region (beg)
  7527. "Move all remembered markers to their position relative to BEG."
  7528. (mapc (lambda (x)
  7529. (move-marker (car x) (+ beg (cdr x))))
  7530. org-markers-to-move)
  7531. (setq org-markers-to-move nil))
  7532. (defun org-narrow-to-subtree ()
  7533. "Narrow buffer to the current subtree."
  7534. (interactive)
  7535. (save-excursion
  7536. (save-match-data
  7537. (org-with-limited-levels
  7538. (narrow-to-region
  7539. (progn (org-back-to-heading t) (point))
  7540. (progn (org-end-of-subtree t t)
  7541. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7542. (point)))))))
  7543. (defun org-narrow-to-block ()
  7544. "Narrow buffer to the current block."
  7545. (interactive)
  7546. (let* ((case-fold-search t)
  7547. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7548. "^[ \t]*#\\+end_.*")))
  7549. (if blockp
  7550. (narrow-to-region (car blockp) (cdr blockp))
  7551. (error "Not in a block"))))
  7552. (eval-when-compile
  7553. (defvar org-property-drawer-re))
  7554. (defvar org-property-start-re) ;; defined below
  7555. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7556. "Clone the task (subtree) at point N times.
  7557. The clones will be inserted as siblings.
  7558. In interactive use, the user will be prompted for the number of
  7559. clones to be produced. When called with a universal prefix argument
  7560. and if the entry has a timestamp, the user will also be prompted for
  7561. a time shift, which may be a repeater as used in time stamps, for
  7562. example `+3d'.
  7563. When a valid repeater is given and the entry contains any time
  7564. stamps, the clones will become a sequence in time, with time
  7565. stamps in the subtree shifted for each clone produced. If SHIFT
  7566. is nil or the empty string, time stamps will be left alone. The
  7567. ID property of the original subtree is removed.
  7568. If the original subtree did contain time stamps with a repeater,
  7569. the following will happen:
  7570. - the repeater will be removed in each clone
  7571. - an additional clone will be produced, with the current, unshifted
  7572. date(s) in the entry.
  7573. - the original entry will be placed *after* all the clones, with
  7574. repeater intact.
  7575. - the start days in the repeater in the original entry will be shifted
  7576. to past the last clone.
  7577. In this way you can spell out a number of instances of a repeating task,
  7578. and still retain the repeater to cover future instances of the task."
  7579. (interactive "nNumber of clones to produce: ")
  7580. (let ((shift
  7581. (or shift
  7582. (if (and (equal current-prefix-arg '(4))
  7583. (save-excursion
  7584. (re-search-forward org-ts-regexp-both
  7585. (save-excursion
  7586. (org-end-of-subtree t)
  7587. (point)) t)))
  7588. (read-from-minibuffer
  7589. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7590. ""))) ;; No time shift
  7591. (n-no-remove -1)
  7592. (drawer-re org-drawer-regexp)
  7593. beg end template task idprop
  7594. shift-n shift-what doshift nmin nmax)
  7595. (if (not (and (integerp n) (> n 0)))
  7596. (error "Invalid number of replications %s" n))
  7597. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7598. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7599. shift)))
  7600. (error "Invalid shift specification %s" shift))
  7601. (when doshift
  7602. (setq shift-n (string-to-number (match-string 1 shift))
  7603. shift-what (cdr (assoc (match-string 2 shift)
  7604. '(("d" . day) ("w" . week)
  7605. ("m" . month) ("y" . year))))))
  7606. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7607. (setq nmin 1 nmax n)
  7608. (org-back-to-heading t)
  7609. (setq beg (point))
  7610. (setq idprop (org-entry-get nil "ID"))
  7611. (org-end-of-subtree t t)
  7612. (or (bolp) (insert "\n"))
  7613. (setq end (point))
  7614. (setq template (buffer-substring beg end))
  7615. (when (and doshift
  7616. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7617. (delete-region beg end)
  7618. (setq end beg)
  7619. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7620. (goto-char end)
  7621. (loop for n from nmin to nmax do
  7622. ;; prepare clone
  7623. (with-temp-buffer
  7624. (insert template)
  7625. (org-mode)
  7626. (goto-char (point-min))
  7627. (org-show-subtree)
  7628. (and idprop (if org-clone-delete-id
  7629. (org-entry-delete nil "ID")
  7630. (org-id-get-create t)))
  7631. (unless (= n 0)
  7632. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7633. (kill-whole-line))
  7634. (goto-char (point-min))
  7635. (while (re-search-forward drawer-re nil t)
  7636. (mapc (lambda (d)
  7637. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7638. (goto-char (point-min))
  7639. (when doshift
  7640. (while (re-search-forward org-ts-regexp-both nil t)
  7641. (org-timestamp-change (* n shift-n) shift-what))
  7642. (unless (= n n-no-remove)
  7643. (goto-char (point-min))
  7644. (while (re-search-forward org-ts-regexp nil t)
  7645. (save-excursion
  7646. (goto-char (match-beginning 0))
  7647. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7648. (delete-region (match-beginning 1) (match-end 1)))))))
  7649. (setq task (buffer-string)))
  7650. (insert task))
  7651. (goto-char beg)))
  7652. ;;; Outline Sorting
  7653. (defun org-sort (with-case)
  7654. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7655. Optional argument WITH-CASE means sort case-sensitively."
  7656. (interactive "P")
  7657. (cond
  7658. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7659. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7660. (t
  7661. (org-call-with-arg 'org-sort-entries with-case))))
  7662. (defun org-sort-remove-invisible (s)
  7663. "Remove invisible links from string S."
  7664. (remove-text-properties 0 (length s) org-rm-props s)
  7665. (while (string-match org-bracket-link-regexp s)
  7666. (setq s (replace-match (if (match-end 2)
  7667. (match-string 3 s)
  7668. (match-string 1 s)) t t s)))
  7669. (let ((st (format " %s " s)))
  7670. (while (string-match org-emph-re st)
  7671. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7672. (setq s (substring st 1 -1)))
  7673. s)
  7674. (defvar org-priority-regexp) ; defined later in the file
  7675. (defvar org-after-sorting-entries-or-items-hook nil
  7676. "Hook that is run after a bunch of entries or items have been sorted.
  7677. When children are sorted, the cursor is in the parent line when this
  7678. hook gets called. When a region or a plain list is sorted, the cursor
  7679. will be in the first entry of the sorted region/list.")
  7680. (defun org-sort-entries
  7681. (&optional with-case sorting-type getkey-func compare-func property)
  7682. "Sort entries on a certain level of an outline tree.
  7683. If there is an active region, the entries in the region are sorted.
  7684. Else, if the cursor is before the first entry, sort the top-level items.
  7685. Else, the children of the entry at point are sorted.
  7686. Sorting can be alphabetically, numerically, by date/time as given by
  7687. a time stamp, by a property, by priority order, or by a custom function.
  7688. The command prompts for the sorting type unless it has been given to the
  7689. function through the SORTING-TYPE argument, which needs to be a character,
  7690. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7691. precise meaning of each character:
  7692. n Numerically, by converting the beginning of the entry/item to a number.
  7693. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7694. o By order of TODO keywords.
  7695. t By date/time, either the first active time stamp in the entry, or, if
  7696. none exist, by the first inactive one.
  7697. s By the scheduled date/time.
  7698. d By deadline date/time.
  7699. c By creation time, which is assumed to be the first inactive time stamp
  7700. at the beginning of a line.
  7701. p By priority according to the cookie.
  7702. r By the value of a property.
  7703. Capital letters will reverse the sort order.
  7704. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7705. called with point at the beginning of the record. It must return either
  7706. a string or a number that should serve as the sorting key for that record.
  7707. Comparing entries ignores case by default. However, with an optional argument
  7708. WITH-CASE, the sorting considers case as well.
  7709. Sorting is done against the visible part of the headlines, it ignores hidden
  7710. links."
  7711. (interactive "P")
  7712. (let ((case-func (if with-case 'identity 'downcase))
  7713. (cmstr
  7714. ;; The clock marker is lost when using `sort-subr', let's
  7715. ;; store the clocking string.
  7716. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7717. (save-excursion
  7718. (goto-char org-clock-marker)
  7719. (looking-back "^.*") (match-string-no-properties 0))))
  7720. start beg end stars re re2
  7721. txt what tmp)
  7722. ;; Find beginning and end of region to sort
  7723. (cond
  7724. ((org-region-active-p)
  7725. ;; we will sort the region
  7726. (setq end (region-end)
  7727. what "region")
  7728. (goto-char (region-beginning))
  7729. (if (not (org-at-heading-p)) (outline-next-heading))
  7730. (setq start (point)))
  7731. ((or (org-at-heading-p)
  7732. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7733. ;; we will sort the children of the current headline
  7734. (org-back-to-heading)
  7735. (setq start (point)
  7736. end (progn (org-end-of-subtree t t)
  7737. (or (bolp) (insert "\n"))
  7738. (org-back-over-empty-lines)
  7739. (point))
  7740. what "children")
  7741. (goto-char start)
  7742. (show-subtree)
  7743. (outline-next-heading))
  7744. (t
  7745. ;; we will sort the top-level entries in this file
  7746. (goto-char (point-min))
  7747. (or (org-at-heading-p) (outline-next-heading))
  7748. (setq start (point))
  7749. (goto-char (point-max))
  7750. (beginning-of-line 1)
  7751. (when (looking-at ".*?\\S-")
  7752. ;; File ends in a non-white line
  7753. (end-of-line 1)
  7754. (insert "\n"))
  7755. (setq end (point-max))
  7756. (setq what "top-level")
  7757. (goto-char start)
  7758. (show-all)))
  7759. (setq beg (point))
  7760. (if (>= beg end) (error "Nothing to sort"))
  7761. (looking-at "\\(\\*+\\)")
  7762. (setq stars (match-string 1)
  7763. re (concat "^" (regexp-quote stars) " +")
  7764. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7765. txt (buffer-substring beg end))
  7766. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7767. (if (and (not (equal stars "*")) (string-match re2 txt))
  7768. (error "Region to sort contains a level above the first entry"))
  7769. (unless sorting-type
  7770. (message
  7771. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7772. [t]ime [s]cheduled [d]eadline [c]reated
  7773. A/N/P/R/O/F/T/S/D/C means reversed:"
  7774. what)
  7775. (setq sorting-type (read-char-exclusive))
  7776. (unless getkey-func
  7777. (and (= (downcase sorting-type) ?f)
  7778. (setq getkey-func
  7779. (org-icompleting-read "Sort using function: "
  7780. obarray 'fboundp t nil nil))
  7781. (setq getkey-func (intern getkey-func))))
  7782. (and (= (downcase sorting-type) ?r)
  7783. (not property)
  7784. (setq property
  7785. (org-icompleting-read "Property: "
  7786. (mapcar 'list (org-buffer-property-keys t))
  7787. nil t))))
  7788. (message "Sorting entries...")
  7789. (save-restriction
  7790. (narrow-to-region start end)
  7791. (let ((dcst (downcase sorting-type))
  7792. (case-fold-search nil)
  7793. (now (current-time)))
  7794. (sort-subr
  7795. (/= dcst sorting-type)
  7796. ;; This function moves to the beginning character of the "record" to
  7797. ;; be sorted.
  7798. (lambda nil
  7799. (if (re-search-forward re nil t)
  7800. (goto-char (match-beginning 0))
  7801. (goto-char (point-max))))
  7802. ;; This function moves to the last character of the "record" being
  7803. ;; sorted.
  7804. (lambda nil
  7805. (save-match-data
  7806. (condition-case nil
  7807. (outline-forward-same-level 1)
  7808. (error
  7809. (goto-char (point-max))))))
  7810. ;; This function returns the value that gets sorted against.
  7811. (lambda nil
  7812. (cond
  7813. ((= dcst ?n)
  7814. (if (looking-at org-complex-heading-regexp)
  7815. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7816. nil))
  7817. ((= dcst ?a)
  7818. (if (looking-at org-complex-heading-regexp)
  7819. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7820. nil))
  7821. ((= dcst ?t)
  7822. (let ((end (save-excursion (outline-next-heading) (point))))
  7823. (if (or (re-search-forward org-ts-regexp end t)
  7824. (re-search-forward org-ts-regexp-both end t))
  7825. (org-time-string-to-seconds (match-string 0))
  7826. (org-float-time now))))
  7827. ((= dcst ?c)
  7828. (let ((end (save-excursion (outline-next-heading) (point))))
  7829. (if (re-search-forward
  7830. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7831. end t)
  7832. (org-time-string-to-seconds (match-string 0))
  7833. (org-float-time now))))
  7834. ((= dcst ?s)
  7835. (let ((end (save-excursion (outline-next-heading) (point))))
  7836. (if (re-search-forward org-scheduled-time-regexp end t)
  7837. (org-time-string-to-seconds (match-string 1))
  7838. (org-float-time now))))
  7839. ((= dcst ?d)
  7840. (let ((end (save-excursion (outline-next-heading) (point))))
  7841. (if (re-search-forward org-deadline-time-regexp end t)
  7842. (org-time-string-to-seconds (match-string 1))
  7843. (org-float-time now))))
  7844. ((= dcst ?p)
  7845. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7846. (string-to-char (match-string 2))
  7847. org-default-priority))
  7848. ((= dcst ?r)
  7849. (or (org-entry-get nil property) ""))
  7850. ((= dcst ?o)
  7851. (if (looking-at org-complex-heading-regexp)
  7852. (- 9999 (length (member (match-string 2)
  7853. org-todo-keywords-1)))))
  7854. ((= dcst ?f)
  7855. (if getkey-func
  7856. (progn
  7857. (setq tmp (funcall getkey-func))
  7858. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7859. tmp)
  7860. (error "Invalid key function `%s'" getkey-func)))
  7861. (t (error "Invalid sorting type `%c'" sorting-type))))
  7862. nil
  7863. (cond
  7864. ((= dcst ?a) 'string<)
  7865. ((= dcst ?f) compare-func)
  7866. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7867. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7868. ;; Reset the clock marker if needed
  7869. (when cmstr
  7870. (save-excursion
  7871. (goto-char start)
  7872. (search-forward cmstr nil t)
  7873. (move-marker org-clock-marker (point))))
  7874. (message "Sorting entries...done")))
  7875. (defun org-do-sort (table what &optional with-case sorting-type)
  7876. "Sort TABLE of WHAT according to SORTING-TYPE.
  7877. The user will be prompted for the SORTING-TYPE if the call to this
  7878. function does not specify it. WHAT is only for the prompt, to indicate
  7879. what is being sorted. The sorting key will be extracted from
  7880. the car of the elements of the table.
  7881. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7882. (unless sorting-type
  7883. (message
  7884. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7885. what)
  7886. (setq sorting-type (read-char-exclusive)))
  7887. (let ((dcst (downcase sorting-type))
  7888. extractfun comparefun)
  7889. ;; Define the appropriate functions
  7890. (cond
  7891. ((= dcst ?n)
  7892. (setq extractfun 'string-to-number
  7893. comparefun (if (= dcst sorting-type) '< '>)))
  7894. ((= dcst ?a)
  7895. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7896. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7897. comparefun (if (= dcst sorting-type)
  7898. 'string<
  7899. (lambda (a b) (and (not (string< a b))
  7900. (not (string= a b)))))))
  7901. ((= dcst ?t)
  7902. (setq extractfun
  7903. (lambda (x)
  7904. (if (or (string-match org-ts-regexp x)
  7905. (string-match org-ts-regexp-both x))
  7906. (org-float-time
  7907. (org-time-string-to-time (match-string 0 x)))
  7908. 0))
  7909. comparefun (if (= dcst sorting-type) '< '>)))
  7910. (t (error "Invalid sorting type `%c'" sorting-type)))
  7911. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7912. table)
  7913. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7914. ;;; The orgstruct minor mode
  7915. ;; Define a minor mode which can be used in other modes in order to
  7916. ;; integrate the org-mode structure editing commands.
  7917. ;; This is really a hack, because the org-mode structure commands use
  7918. ;; keys which normally belong to the major mode. Here is how it
  7919. ;; works: The minor mode defines all the keys necessary to operate the
  7920. ;; structure commands, but wraps the commands into a function which
  7921. ;; tests if the cursor is currently at a headline or a plain list
  7922. ;; item. If that is the case, the structure command is used,
  7923. ;; temporarily setting many Org-mode variables like regular
  7924. ;; expressions for filling etc. However, when any of those keys is
  7925. ;; used at a different location, function uses `key-binding' to look
  7926. ;; up if the key has an associated command in another currently active
  7927. ;; keymap (minor modes, major mode, global), and executes that
  7928. ;; command. There might be problems if any of the keys is otherwise
  7929. ;; used as a prefix key.
  7930. (defcustom orgstruct-heading-prefix-regexp nil
  7931. "Regexp that matches the custom prefix of Org headlines in
  7932. orgstruct(++)-mode."
  7933. :group 'org
  7934. :version "24.4"
  7935. :package-version '(Org . "8.0")
  7936. :type 'string)
  7937. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7938. (defcustom orgstruct-setup-hook nil
  7939. "Hook run after orgstruct-mode-map is filled."
  7940. :group 'org
  7941. :version "24.4"
  7942. :package-version '(Org . "8.0")
  7943. :type 'hook)
  7944. (defvar orgstruct-initialized nil)
  7945. (defvar org-local-vars nil
  7946. "List of local variables, for use by `orgstruct-mode'.")
  7947. ;;;###autoload
  7948. (define-minor-mode orgstruct-mode
  7949. "Toggle the minor mode `orgstruct-mode'.
  7950. This mode is for using Org-mode structure commands in other
  7951. modes. The following keys behave as if Org-mode were active, if
  7952. the cursor is on a headline, or on a plain list item (both as
  7953. defined by Org-mode)."
  7954. nil " OrgStruct" (make-sparse-keymap)
  7955. (funcall (if orgstruct-mode
  7956. 'add-to-invisibility-spec
  7957. 'remove-from-invisibility-spec)
  7958. '(outline . t))
  7959. (when orgstruct-mode
  7960. (org-load-modules-maybe)
  7961. (unless orgstruct-initialized
  7962. (orgstruct-setup)
  7963. (setq orgstruct-initialized t))))
  7964. ;;;###autoload
  7965. (defun turn-on-orgstruct ()
  7966. "Unconditionally turn on `orgstruct-mode'."
  7967. (orgstruct-mode 1))
  7968. (defvar org-fb-vars nil)
  7969. (make-variable-buffer-local 'org-fb-vars)
  7970. (defun orgstruct++-mode (&optional arg)
  7971. "Toggle `orgstruct-mode', the enhanced version of it.
  7972. In addition to setting orgstruct-mode, this also exports all
  7973. indentation and autofilling variables from org-mode into the
  7974. buffer. It will also recognize item context in multiline items."
  7975. (interactive "P")
  7976. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7977. (if (< arg 1)
  7978. (progn (orgstruct-mode -1)
  7979. (mapc (lambda(v)
  7980. (org-set-local (car v)
  7981. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7982. org-fb-vars))
  7983. (orgstruct-mode 1)
  7984. (setq org-fb-vars nil)
  7985. (let (var val)
  7986. (mapc
  7987. (lambda (x)
  7988. (when (string-match
  7989. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7990. (symbol-name (car x)))
  7991. (setq var (car x) val (nth 1 x))
  7992. (push (list var `(quote ,(eval var))) org-fb-vars)
  7993. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7994. org-local-vars)
  7995. (org-set-local 'orgstruct-is-++ t))))
  7996. (defvar orgstruct-is-++ nil
  7997. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7998. (make-variable-buffer-local 'orgstruct-is-++)
  7999. ;;;###autoload
  8000. (defun turn-on-orgstruct++ ()
  8001. "Unconditionally turn on `orgstruct++-mode'."
  8002. (orgstruct++-mode 1))
  8003. (defun orgstruct-error ()
  8004. "Error when there is no default binding for a structure key."
  8005. (interactive)
  8006. (funcall (if (fboundp 'user-error)
  8007. 'user-error
  8008. 'error)
  8009. "This key has no function outside structure elements"))
  8010. (defun orgstruct-setup ()
  8011. "Setup orgstruct keymap."
  8012. (dolist (cell '((org-demote . t)
  8013. (org-metaleft . t)
  8014. (org-metaright . t)
  8015. (org-promote . t)
  8016. (org-shiftmetaleft . t)
  8017. (org-shiftmetaright . t)
  8018. org-backward-element
  8019. org-backward-heading-same-level
  8020. org-ctrl-c-ret
  8021. org-ctrl-c-minus
  8022. org-ctrl-c-star
  8023. org-cycle
  8024. org-forward-heading-same-level
  8025. org-insert-heading
  8026. org-insert-heading-respect-content
  8027. org-kill-note-or-show-branches
  8028. org-mark-subtree
  8029. org-meta-return
  8030. org-metadown
  8031. org-metaup
  8032. org-narrow-to-subtree
  8033. org-promote-subtree
  8034. org-reveal
  8035. org-shiftdown
  8036. org-shiftleft
  8037. org-shiftmetadown
  8038. org-shiftmetaup
  8039. org-shiftright
  8040. org-shifttab
  8041. org-shifttab
  8042. org-shiftup
  8043. org-show-subtree
  8044. org-sort
  8045. org-up-element
  8046. outline-demote
  8047. outline-next-visible-heading
  8048. outline-previous-visible-heading
  8049. outline-promote
  8050. outline-up-heading
  8051. show-children))
  8052. (let ((f (or (car-safe cell) cell))
  8053. (disable-when-heading-prefix (cdr-safe cell)))
  8054. (when (fboundp f)
  8055. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8056. (where-is-internal f outline-mode-map)))
  8057. ;; TODO use local-function-key-map
  8058. (dolist (rep '(("<tab>" . "TAB")
  8059. ("<return>" . "RET")
  8060. ("<escape>" . "ESC")
  8061. ("<delete>" . "DEL")))
  8062. (setq binding (read-kbd-macro
  8063. (let ((case-fold-search))
  8064. (replace-regexp-in-string
  8065. (regexp-quote (cdr rep))
  8066. (car rep)
  8067. (key-description binding))))))
  8068. (let ((key (lookup-key orgstruct-mode-map binding)))
  8069. (when (or (not key) (numberp key))
  8070. (condition-case nil
  8071. (org-defkey orgstruct-mode-map
  8072. binding
  8073. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8074. (error nil))))))))
  8075. (run-hooks 'orgstruct-setup-hook))
  8076. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8077. "Create a function for binding in the structure minor mode.
  8078. FUN is the command to call inside a table. KEY is the key that
  8079. should be checked in for a command to execute outside of tables.
  8080. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8081. if `orgstruct-heading-prefix-regexp' is non-nil."
  8082. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8083. (let ((nname name)
  8084. (i 0))
  8085. (while (fboundp (intern nname))
  8086. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8087. (setq name (intern nname)))
  8088. (eval
  8089. (let ((bindings '((org-heading-regexp
  8090. (concat "^"
  8091. orgstruct-heading-prefix-regexp
  8092. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8093. (org-outline-regexp
  8094. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8095. (org-outline-regexp-bol
  8096. (concat "^" org-outline-regexp))
  8097. (outline-regexp org-outline-regexp)
  8098. (outline-heading-end-regexp "\n")
  8099. (outline-level 'org-outline-level)
  8100. (outline-heading-alist))))
  8101. `(defun ,name (arg)
  8102. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8103. "Outside of structure, run the binding of `"
  8104. (key-description key) "'."
  8105. (when disable-when-heading-prefix
  8106. (concat
  8107. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8108. "back to the default binding due to limitations of Org's implementation of\n"
  8109. "`" (symbol-name fun) "'.")))
  8110. (interactive "p")
  8111. (let* ((disable
  8112. ,(when disable-when-heading-prefix
  8113. '(and orgstruct-heading-prefix-regexp
  8114. (not (string= orgstruct-heading-prefix-regexp "")))))
  8115. (fallback
  8116. (or disable
  8117. (not
  8118. (let* ,bindings
  8119. (org-context-p 'headline 'item
  8120. ,(when (memq fun '(org-insert-heading))
  8121. '(when orgstruct-is-++
  8122. 'item-body))))))))
  8123. (if fallback
  8124. (let* ((orgstruct-mode)
  8125. (binding
  8126. (loop with key = ,key
  8127. for rep in
  8128. '(nil
  8129. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8130. ("<\\([^>]*\\)return>" . "\\1RET")
  8131. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8132. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8133. do
  8134. (when rep
  8135. (setq key (read-kbd-macro
  8136. (let ((case-fold-search))
  8137. (replace-regexp-in-string
  8138. (car rep)
  8139. (cdr rep)
  8140. (key-description key))))))
  8141. thereis (key-binding key))))
  8142. (if (keymapp binding)
  8143. (set-temporary-overlay-map binding)
  8144. (let ((func (or binding
  8145. (unless disable
  8146. 'orgstruct-error))))
  8147. (when func
  8148. (call-interactively func)))))
  8149. (org-run-like-in-org-mode
  8150. (lambda ()
  8151. (interactive)
  8152. (let* ,bindings
  8153. (call-interactively ',fun)))))))))
  8154. name))
  8155. (defun org-contextualize-keys (alist contexts)
  8156. "Return valid elements in ALIST depending on CONTEXTS.
  8157. `org-agenda-custom-commands' or `org-capture-templates' are the
  8158. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8159. or `org-capture-templates-contexts' are the associated contexts
  8160. definitions."
  8161. (let ((contexts
  8162. ;; normalize contexts
  8163. (mapcar
  8164. (lambda(c) (cond ((listp (cadr c))
  8165. (list (car c) (car c) (cadr c)))
  8166. ((string= "" (cadr c))
  8167. (list (car c) (car c) (caddr c)))
  8168. (t c))) contexts))
  8169. (a alist) c r s)
  8170. ;; loop over all commands or templates
  8171. (while (setq c (pop a))
  8172. (let (vrules repl)
  8173. (cond
  8174. ((not (assoc (car c) contexts))
  8175. (push c r))
  8176. ((and (assoc (car c) contexts)
  8177. (setq vrules (org-contextualize-validate-key
  8178. (car c) contexts)))
  8179. (mapc (lambda (vr)
  8180. (when (not (equal (car vr) (cadr vr)))
  8181. (setq repl vr))) vrules)
  8182. (if (not repl) (push c r)
  8183. (push (cadr repl) s)
  8184. (push
  8185. (cons (car c)
  8186. (cdr (or (assoc (cadr repl) alist)
  8187. (error "Undefined key `%s' as contextual replacement for `%s'"
  8188. (cadr repl) (car c)))))
  8189. r))))))
  8190. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8191. (delq
  8192. nil
  8193. (delete-dups
  8194. (mapcar (lambda (x)
  8195. (let ((tpl (car x)))
  8196. (when (not (delq
  8197. nil
  8198. (mapcar (lambda(y)
  8199. (equal y tpl)) s))) x)))
  8200. (reverse r))))))
  8201. (defun org-contextualize-validate-key (key contexts)
  8202. "Check CONTEXTS for agenda or capture KEY."
  8203. (let (r rr res)
  8204. (while (setq r (pop contexts))
  8205. (mapc
  8206. (lambda (rr)
  8207. (when
  8208. (and (equal key (car r))
  8209. (if (functionp rr) (funcall rr)
  8210. (or (and (eq (car rr) 'in-file)
  8211. (buffer-file-name)
  8212. (string-match (cdr rr) (buffer-file-name)))
  8213. (and (eq (car rr) 'in-mode)
  8214. (string-match (cdr rr) (symbol-name major-mode)))
  8215. (and (eq (car rr) 'in-buffer)
  8216. (string-match (cdr rr) (buffer-name)))
  8217. (when (and (eq (car rr) 'not-in-file)
  8218. (buffer-file-name))
  8219. (not (string-match (cdr rr) (buffer-file-name))))
  8220. (when (eq (car rr) 'not-in-mode)
  8221. (not (string-match (cdr rr) (symbol-name major-mode))))
  8222. (when (eq (car rr) 'not-in-buffer)
  8223. (not (string-match (cdr rr) (buffer-name)))))))
  8224. (push r res)))
  8225. (car (last r))))
  8226. (delete-dups (delq nil res))))
  8227. (defun org-context-p (&rest contexts)
  8228. "Check if local context is any of CONTEXTS.
  8229. Possible values in the list of contexts are `table', `headline', and `item'."
  8230. (let ((pos (point)))
  8231. (goto-char (point-at-bol))
  8232. (prog1 (or (and (memq 'table contexts)
  8233. (looking-at "[ \t]*|"))
  8234. (and (memq 'headline contexts)
  8235. (looking-at org-outline-regexp))
  8236. (and (memq 'item contexts)
  8237. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8238. (and (memq 'item-body contexts)
  8239. (org-in-item-p)))
  8240. (goto-char pos))))
  8241. (defun org-get-local-variables ()
  8242. "Return a list of all local variables in an Org mode buffer."
  8243. (let (varlist)
  8244. (with-current-buffer (get-buffer-create "*Org tmp*")
  8245. (erase-buffer)
  8246. (org-mode)
  8247. (setq varlist (buffer-local-variables)))
  8248. (kill-buffer "*Org tmp*")
  8249. (delq nil
  8250. (mapcar
  8251. (lambda (x)
  8252. (setq x
  8253. (if (symbolp x)
  8254. (list x)
  8255. (list (car x) (cdr x))))
  8256. (if (and (not (get (car x) 'org-state))
  8257. (string-match
  8258. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8259. (symbol-name (car x))))
  8260. x nil))
  8261. varlist))))
  8262. (defun org-clone-local-variables (from-buffer &optional regexp)
  8263. "Clone local variables from FROM-BUFFER.
  8264. Optional argument REGEXP selects variables to clone."
  8265. (mapc
  8266. (lambda (pair)
  8267. (and (symbolp (car pair))
  8268. (or (null regexp)
  8269. (string-match regexp (symbol-name (car pair))))
  8270. (set (make-local-variable (car pair))
  8271. (cdr pair))))
  8272. (buffer-local-variables from-buffer)))
  8273. ;;;###autoload
  8274. (defun org-run-like-in-org-mode (cmd)
  8275. "Run a command, pretending that the current buffer is in Org-mode.
  8276. This will temporarily bind local variables that are typically bound in
  8277. Org-mode to the values they have in Org-mode, and then interactively
  8278. call CMD."
  8279. (org-load-modules-maybe)
  8280. (unless org-local-vars
  8281. (setq org-local-vars (org-get-local-variables)))
  8282. (let (binds)
  8283. (dolist (var org-local-vars)
  8284. (when (or (not (boundp (car var)))
  8285. (eq (symbol-value (car var))
  8286. (default-value (car var))))
  8287. (push (list (car var) `(quote ,(cadr var))) binds)))
  8288. (eval `(let ,binds
  8289. (call-interactively (quote ,cmd))))))
  8290. ;;;; Archiving
  8291. (defun org-get-category (&optional pos force-refresh)
  8292. "Get the category applying to position POS."
  8293. (save-match-data
  8294. (if force-refresh (org-refresh-category-properties))
  8295. (let ((pos (or pos (point))))
  8296. (or (get-text-property pos 'org-category)
  8297. (progn (org-refresh-category-properties)
  8298. (get-text-property pos 'org-category))))))
  8299. (defun org-refresh-category-properties ()
  8300. "Refresh category text properties in the buffer."
  8301. (let ((case-fold-search t)
  8302. (inhibit-read-only t)
  8303. (def-cat (cond
  8304. ((null org-category)
  8305. (if buffer-file-name
  8306. (file-name-sans-extension
  8307. (file-name-nondirectory buffer-file-name))
  8308. "???"))
  8309. ((symbolp org-category) (symbol-name org-category))
  8310. (t org-category)))
  8311. beg end cat pos optionp)
  8312. (org-with-silent-modifications
  8313. (save-excursion
  8314. (save-restriction
  8315. (widen)
  8316. (goto-char (point-min))
  8317. (put-text-property (point) (point-max) 'org-category def-cat)
  8318. (while (re-search-forward
  8319. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8320. (setq pos (match-end 0)
  8321. optionp (equal (char-after (match-beginning 0)) ?#)
  8322. cat (org-trim (match-string 2)))
  8323. (if optionp
  8324. (setq beg (point-at-bol) end (point-max))
  8325. (org-back-to-heading t)
  8326. (setq beg (point) end (org-end-of-subtree t t)))
  8327. (put-text-property beg end 'org-category cat)
  8328. (put-text-property beg end 'org-category-position beg)
  8329. (goto-char pos)))))))
  8330. (defun org-refresh-properties (dprop tprop)
  8331. "Refresh buffer text properties.
  8332. DPROP is the drawer property and TPROP is the corresponding text
  8333. property to set."
  8334. (let ((case-fold-search t)
  8335. (inhibit-read-only t) p)
  8336. (org-with-silent-modifications
  8337. (save-excursion
  8338. (save-restriction
  8339. (widen)
  8340. (goto-char (point-min))
  8341. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8342. (setq p (org-match-string-no-properties 1))
  8343. (save-excursion
  8344. (org-back-to-heading t)
  8345. (put-text-property
  8346. (point-at-bol) (point-at-eol) tprop p))))))))
  8347. ;;;; Link Stuff
  8348. ;;; Link abbreviations
  8349. (defun org-link-expand-abbrev (link)
  8350. "Apply replacements as defined in `org-link-abbrev-alist'."
  8351. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8352. (let* ((key (match-string 1 link))
  8353. (as (or (assoc key org-link-abbrev-alist-local)
  8354. (assoc key org-link-abbrev-alist)))
  8355. (tag (and (match-end 2) (match-string 3 link)))
  8356. rpl)
  8357. (if (not as)
  8358. link
  8359. (setq rpl (cdr as))
  8360. (cond
  8361. ((symbolp rpl) (funcall rpl tag))
  8362. ((string-match "%(\\([^)]+\\))" rpl)
  8363. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8364. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8365. ((string-match "%h" rpl)
  8366. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8367. (t (concat rpl tag)))))
  8368. link))
  8369. ;;; Storing and inserting links
  8370. (defvar org-insert-link-history nil
  8371. "Minibuffer history for links inserted with `org-insert-link'.")
  8372. (defvar org-stored-links nil
  8373. "Contains the links stored with `org-store-link'.")
  8374. (defvar org-store-link-plist nil
  8375. "Plist with info about the most recently link created with `org-store-link'.")
  8376. (defvar org-link-protocols nil
  8377. "Link protocols added to Org-mode using `org-add-link-type'.")
  8378. (defvar org-store-link-functions nil
  8379. "List of functions that are called to create and store a link.
  8380. Each function will be called in turn until one returns a non-nil
  8381. value. Each function should check if it is responsible for creating
  8382. this link (for example by looking at the major mode).
  8383. If not, it must exit and return nil.
  8384. If yes, it should return a non-nil value after a calling
  8385. `org-store-link-props' with a list of properties and values.
  8386. Special properties are:
  8387. :type The link prefix, like \"http\". This must be given.
  8388. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8389. This is obligatory as well.
  8390. :description Optional default description for the second pair
  8391. of brackets in an Org-mode link. The user can still change
  8392. this when inserting this link into an Org-mode buffer.
  8393. In addition to these, any additional properties can be specified
  8394. and then used in capture templates.")
  8395. (defun org-add-link-type (type &optional follow export)
  8396. "Add TYPE to the list of `org-link-types'.
  8397. Re-compute all regular expressions depending on `org-link-types'
  8398. FOLLOW and EXPORT are two functions.
  8399. FOLLOW should take the link path as the single argument and do whatever
  8400. is necessary to follow the link, for example find a file or display
  8401. a mail message.
  8402. EXPORT should format the link path for export to one of the export formats.
  8403. It should be a function accepting three arguments:
  8404. path the path of the link, the text after the prefix (like \"http:\")
  8405. desc the description of the link, if any, or a description added by
  8406. org-export-normalize-links if there is none
  8407. format the export format, a symbol like `html' or `latex' or `ascii'..
  8408. The function may use the FORMAT information to return different values
  8409. depending on the format. The return value will be put literally into
  8410. the exported file. If the return value is nil, this means Org should
  8411. do what it normally does with links which do not have EXPORT defined.
  8412. Org-mode has a built-in default for exporting links. If you are happy with
  8413. this default, there is no need to define an export function for the link
  8414. type. For a simple example of an export function, see `org-bbdb.el'."
  8415. (add-to-list 'org-link-types type t)
  8416. (org-make-link-regexps)
  8417. (if (assoc type org-link-protocols)
  8418. (setcdr (assoc type org-link-protocols) (list follow export))
  8419. (push (list type follow export) org-link-protocols)))
  8420. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8421. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8422. ;;;###autoload
  8423. (defun org-store-link (arg)
  8424. "\\<org-mode-map>Store an org-link to the current location.
  8425. This link is added to `org-stored-links' and can later be inserted
  8426. into an org-buffer with \\[org-insert-link].
  8427. For some link types, a prefix arg is interpreted.
  8428. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8429. For file links, arg negates `org-context-in-file-links'.
  8430. A double prefix arg force skipping storing functions that are not
  8431. part of Org's core.
  8432. A triple prefix arg force storing a link for each line in the
  8433. active region."
  8434. (interactive "P")
  8435. (org-load-modules-maybe)
  8436. (if (and (equal arg '(64)) (org-region-active-p))
  8437. (save-excursion
  8438. (let ((end (region-end)))
  8439. (goto-char (region-beginning))
  8440. (set-mark (point))
  8441. (while (< (point-at-eol) end)
  8442. (move-end-of-line 1) (activate-mark)
  8443. (let (current-prefix-arg)
  8444. (call-interactively 'org-store-link))
  8445. (move-beginning-of-line 2)
  8446. (set-mark (point)))))
  8447. (org-with-limited-levels
  8448. (setq org-store-link-plist nil)
  8449. (let (link cpltxt desc description search
  8450. txt custom-id agenda-link sfuns sfunsn)
  8451. (cond
  8452. ;; Store a link using an external link type
  8453. ((and (not (equal arg '(16)))
  8454. (setq sfuns
  8455. (delq
  8456. nil (mapcar (lambda (f)
  8457. (let (fs) (if (funcall f) (push f fs))))
  8458. org-store-link-functions))
  8459. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8460. (or (and (cdr sfuns)
  8461. (funcall (intern
  8462. (completing-read
  8463. "Which function for creating the link? "
  8464. sfunsn t (car sfunsn)))))
  8465. (funcall (caar sfuns)))
  8466. (setq link (plist-get org-store-link-plist :link)
  8467. desc (or (plist-get org-store-link-plist
  8468. :description) link))))
  8469. ;; Store a link from a source code buffer
  8470. ((org-src-edit-buffer-p)
  8471. (let (label gc)
  8472. (while (or (not label)
  8473. (save-excursion
  8474. (save-restriction
  8475. (widen)
  8476. (goto-char (point-min))
  8477. (re-search-forward
  8478. (regexp-quote (format org-coderef-label-format label))
  8479. nil t))))
  8480. (when label (message "Label exists already") (sit-for 2))
  8481. (setq label (read-string "Code line label: " label)))
  8482. (end-of-line 1)
  8483. (setq link (format org-coderef-label-format label))
  8484. (setq gc (- 79 (length link)))
  8485. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8486. (insert link)
  8487. (setq link (concat "(" label ")") desc nil)))
  8488. ;; We are in the agenda, link to referenced location
  8489. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8490. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8491. (get-text-property (point) 'org-marker))))
  8492. (when m
  8493. (org-with-point-at m
  8494. (setq agenda-link
  8495. (if (org-called-interactively-p 'any)
  8496. (call-interactively 'org-store-link)
  8497. (org-store-link nil)))))))
  8498. ((eq major-mode 'calendar-mode)
  8499. (let ((cd (calendar-cursor-to-date)))
  8500. (setq link
  8501. (format-time-string
  8502. (car org-time-stamp-formats)
  8503. (apply 'encode-time
  8504. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8505. nil nil nil))))
  8506. (org-store-link-props :type "calendar" :date cd)))
  8507. ((eq major-mode 'help-mode)
  8508. (setq link (concat "help:" (save-excursion
  8509. (goto-char (point-min))
  8510. (looking-at "^[^ ]+")
  8511. (match-string 0))))
  8512. (org-store-link-props :type "help"))
  8513. ((eq major-mode 'w3-mode)
  8514. (setq cpltxt (if (and (buffer-name)
  8515. (not (string-match "Untitled" (buffer-name))))
  8516. (buffer-name)
  8517. (url-view-url t))
  8518. link (url-view-url t))
  8519. (org-store-link-props :type "w3" :url (url-view-url t)))
  8520. ((eq major-mode 'image-mode)
  8521. (setq cpltxt (concat "file:"
  8522. (abbreviate-file-name buffer-file-name))
  8523. link cpltxt)
  8524. (org-store-link-props :type "image" :file buffer-file-name))
  8525. ;; In dired, store a link to the file of the current line
  8526. ((eq major-mode 'dired-mode)
  8527. (let ((file (dired-get-filename nil t)))
  8528. (setq file (if file
  8529. (abbreviate-file-name
  8530. (expand-file-name (dired-get-filename nil t)))
  8531. ;; otherwise, no file so use current directory.
  8532. default-directory))
  8533. (setq cpltxt (concat "file:" file)
  8534. link cpltxt)))
  8535. ((setq search (run-hook-with-args-until-success
  8536. 'org-create-file-search-functions))
  8537. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8538. "::" search))
  8539. (setq cpltxt (or description link)))
  8540. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8541. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8542. (cond
  8543. ;; Store a link using the radio target at point
  8544. ((org-in-regexp "<<\\(.*?\\)>>")
  8545. (setq cpltxt
  8546. (concat "file:"
  8547. (abbreviate-file-name
  8548. (buffer-file-name (buffer-base-buffer)))
  8549. "::" (match-string 1))
  8550. link cpltxt))
  8551. ((and (featurep 'org-id)
  8552. (or (eq org-id-link-to-org-use-id t)
  8553. (and (org-called-interactively-p 'any)
  8554. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8555. (and (eq org-id-link-to-org-use-id
  8556. 'create-if-interactive-and-no-custom-id)
  8557. (not custom-id))))
  8558. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8559. ;; Store a link using the ID at point
  8560. (setq link (condition-case nil
  8561. (prog1 (org-id-store-link)
  8562. (setq desc (plist-get org-store-link-plist
  8563. :description)))
  8564. (error
  8565. ;; Probably before first headline, link only to file
  8566. (concat "file:"
  8567. (abbreviate-file-name
  8568. (buffer-file-name (buffer-base-buffer))))))))
  8569. (t
  8570. ;; Just link to current headline
  8571. (setq cpltxt (concat "file:"
  8572. (abbreviate-file-name
  8573. (buffer-file-name (buffer-base-buffer)))))
  8574. ;; Add a context search string
  8575. (when (org-xor org-context-in-file-links arg)
  8576. (let* ((ee (org-element-at-point))
  8577. (et (org-element-type ee))
  8578. (ev (plist-get (cadr ee) :value))
  8579. (ek (plist-get (cadr ee) :key))
  8580. (eok (and (stringp ek) (string-match "name" ek))))
  8581. (setq txt (cond
  8582. ((org-at-heading-p) nil)
  8583. ((and (eq et 'keyword) eok) ev)
  8584. ((org-region-active-p)
  8585. (buffer-substring (region-beginning) (region-end)))))
  8586. (when (or (null txt) (string-match "\\S-" txt))
  8587. (setq cpltxt
  8588. (concat cpltxt "::"
  8589. (condition-case nil
  8590. (org-make-org-heading-search-string txt)
  8591. (error "")))
  8592. desc (or (and (eq et 'keyword) eok ev)
  8593. (nth 4 (ignore-errors (org-heading-components)))
  8594. "NONE")))))
  8595. (if (string-match "::\\'" cpltxt)
  8596. (setq cpltxt (substring cpltxt 0 -2)))
  8597. (setq link cpltxt))))
  8598. ((buffer-file-name (buffer-base-buffer))
  8599. ;; Just link to this file here.
  8600. (setq cpltxt (concat "file:"
  8601. (abbreviate-file-name
  8602. (buffer-file-name (buffer-base-buffer)))))
  8603. ;; Add a context string.
  8604. (when (org-xor org-context-in-file-links arg)
  8605. (setq txt (if (org-region-active-p)
  8606. (buffer-substring (region-beginning) (region-end))
  8607. (buffer-substring (point-at-bol) (point-at-eol))))
  8608. ;; Only use search option if there is some text.
  8609. (when (string-match "\\S-" txt)
  8610. (setq cpltxt
  8611. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8612. desc "NONE")))
  8613. (setq link cpltxt))
  8614. ((org-called-interactively-p 'interactive)
  8615. (user-error "No method for storing a link from this buffer"))
  8616. (t (setq link nil)))
  8617. ;; We're done setting link and desc, clean up
  8618. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8619. (setq link (or link cpltxt)
  8620. desc (or desc cpltxt))
  8621. (cond ((equal desc "NONE") (setq desc nil))
  8622. ((string-match org-bracket-link-regexp desc)
  8623. (setq desc
  8624. (replace-regexp-in-string
  8625. org-bracket-link-regexp
  8626. (concat "\\3" (if (equal (length (match-string 0 desc))
  8627. (length desc)) "*" "")) desc))))
  8628. ;; Return the link
  8629. (if (not (and (or (org-called-interactively-p 'any)
  8630. executing-kbd-macro) link))
  8631. (or agenda-link (and link (org-make-link-string link desc)))
  8632. (push (list link desc) org-stored-links)
  8633. (message "Stored: %s" (or desc link))
  8634. (when custom-id
  8635. (setq link (concat "file:" (abbreviate-file-name
  8636. (buffer-file-name)) "::#" custom-id))
  8637. (push (list link desc) org-stored-links)))))))
  8638. (defun org-store-link-props (&rest plist)
  8639. "Store link properties, extract names and addresses."
  8640. (let (x adr)
  8641. (when (setq x (plist-get plist :from))
  8642. (setq adr (mail-extract-address-components x))
  8643. (setq plist (plist-put plist :fromname (car adr)))
  8644. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8645. (when (setq x (plist-get plist :to))
  8646. (setq adr (mail-extract-address-components x))
  8647. (setq plist (plist-put plist :toname (car adr)))
  8648. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8649. (let ((from (plist-get plist :from))
  8650. (to (plist-get plist :to)))
  8651. (when (and from to org-from-is-user-regexp)
  8652. (setq plist
  8653. (plist-put plist :fromto
  8654. (if (string-match org-from-is-user-regexp from)
  8655. (concat "to %t")
  8656. (concat "from %f"))))))
  8657. (setq org-store-link-plist plist))
  8658. (defun org-add-link-props (&rest plist)
  8659. "Add these properties to the link property list."
  8660. (let (key value)
  8661. (while plist
  8662. (setq key (pop plist) value (pop plist))
  8663. (setq org-store-link-plist
  8664. (plist-put org-store-link-plist key value)))))
  8665. (defun org-email-link-description (&optional fmt)
  8666. "Return the description part of an email link.
  8667. This takes information from `org-store-link-plist' and formats it
  8668. according to FMT (default from `org-email-link-description-format')."
  8669. (setq fmt (or fmt org-email-link-description-format))
  8670. (let* ((p org-store-link-plist)
  8671. (to (plist-get p :toaddress))
  8672. (from (plist-get p :fromaddress))
  8673. (table
  8674. (list
  8675. (cons "%c" (plist-get p :fromto))
  8676. (cons "%F" (plist-get p :from))
  8677. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8678. (cons "%T" (plist-get p :to))
  8679. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8680. (cons "%s" (plist-get p :subject))
  8681. (cons "%d" (plist-get p :date))
  8682. (cons "%m" (plist-get p :message-id)))))
  8683. (when (string-match "%c" fmt)
  8684. ;; Check if the user wrote this message
  8685. (if (and org-from-is-user-regexp from to
  8686. (save-match-data (string-match org-from-is-user-regexp from)))
  8687. (setq fmt (replace-match "to %t" t t fmt))
  8688. (setq fmt (replace-match "from %f" t t fmt))))
  8689. (org-replace-escapes fmt table)))
  8690. (defun org-make-org-heading-search-string (&optional string)
  8691. "Make search string for the current headline or STRING."
  8692. (let ((s (or string
  8693. (and (derived-mode-p 'org-mode)
  8694. (save-excursion
  8695. (org-back-to-heading t)
  8696. (org-element-property :raw-value (org-element-at-point))))))
  8697. (lines org-context-in-file-links))
  8698. (or string (setq s (concat "*" s))) ; Add * for headlines
  8699. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8700. (when (and string (integerp lines) (> lines 0))
  8701. (let ((slines (org-split-string s "\n")))
  8702. (when (< lines (length slines))
  8703. (setq s (mapconcat
  8704. 'identity
  8705. (reverse (nthcdr (- (length slines) lines)
  8706. (reverse slines))) "\n")))))
  8707. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8708. (defun org-make-link-string (link &optional description)
  8709. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8710. (unless (string-match "\\S-" link)
  8711. (error "Empty link"))
  8712. (when (and description
  8713. (stringp description)
  8714. (not (string-match "\\S-" description)))
  8715. (setq description nil))
  8716. (when (stringp description)
  8717. ;; Remove brackets from the description, they are fatal.
  8718. (while (string-match "\\[" description)
  8719. (setq description (replace-match "{" t t description)))
  8720. (while (string-match "\\]" description)
  8721. (setq description (replace-match "}" t t description))))
  8722. (when (equal link description)
  8723. ;; No description needed, it is identical
  8724. (setq description nil))
  8725. (when (and (not description)
  8726. (not (string-match (org-image-file-name-regexp) link))
  8727. (not (equal link (org-link-escape link))))
  8728. (setq description (org-extract-attributes link)))
  8729. (setq link
  8730. (cond ((string-match (org-image-file-name-regexp) link) link)
  8731. ((string-match org-link-types-re link)
  8732. (concat (match-string 1 link)
  8733. (org-link-escape (substring link (match-end 1)))))
  8734. (t (org-link-escape link))))
  8735. (concat "[[" link "]"
  8736. (if description (concat "[" description "]") "")
  8737. "]"))
  8738. (defconst org-link-escape-chars
  8739. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8740. "List of characters that should be escaped in link.
  8741. This is the list that is used for internal purposes.")
  8742. (defconst org-link-escape-chars-browser
  8743. '(?\ )
  8744. "List of escapes for characters that are problematic in links.
  8745. This is the list that is used before handing over to the browser.")
  8746. (defun org-link-escape (text &optional table merge)
  8747. "Return percent escaped representation of TEXT.
  8748. TEXT is a string with the text to escape.
  8749. Optional argument TABLE is a list with characters that should be
  8750. escaped. When nil, `org-link-escape-chars' is used.
  8751. If optional argument MERGE is set, merge TABLE into
  8752. `org-link-escape-chars'."
  8753. (cond
  8754. ((and table merge)
  8755. (mapc (lambda (defchr)
  8756. (unless (member defchr table)
  8757. (setq table (cons defchr table)))) org-link-escape-chars))
  8758. ((null table)
  8759. (setq table org-link-escape-chars)))
  8760. (mapconcat
  8761. (lambda (char)
  8762. (if (or (member char table)
  8763. (and (or (< char 32) (= char 37) (> char 126))
  8764. org-url-hexify-p))
  8765. (mapconcat (lambda (sequence-element)
  8766. (format "%%%.2X" sequence-element))
  8767. (or (encode-coding-char char 'utf-8)
  8768. (error "Unable to percent escape character: %s"
  8769. (char-to-string char))) "")
  8770. (char-to-string char))) text ""))
  8771. (defun org-link-unescape (str)
  8772. "Unhex hexified Unicode strings as returned from the JavaScript function
  8773. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8774. (unless (and (null str) (string= "" str))
  8775. (let ((pos 0) (case-fold-search t) unhexed)
  8776. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8777. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8778. (setq str (replace-match unhexed t t str))
  8779. (setq pos (+ pos (length unhexed))))))
  8780. str)
  8781. (defun org-link-unescape-compound (hex)
  8782. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8783. Note: this function also decodes single byte encodings like
  8784. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8785. (save-match-data
  8786. (let* ((bytes (cdr (split-string hex "%")))
  8787. (ret "")
  8788. (eat 0)
  8789. (sum 0))
  8790. (while bytes
  8791. (let* ((val (string-to-number (pop bytes) 16))
  8792. (shift-xor
  8793. (if (= 0 eat)
  8794. (cond
  8795. ((>= val 252) (cons 6 252))
  8796. ((>= val 248) (cons 5 248))
  8797. ((>= val 240) (cons 4 240))
  8798. ((>= val 224) (cons 3 224))
  8799. ((>= val 192) (cons 2 192))
  8800. (t (cons 0 0)))
  8801. (cons 6 128))))
  8802. (if (>= val 192) (setq eat (car shift-xor)))
  8803. (setq val (logxor val (cdr shift-xor)))
  8804. (setq sum (+ (lsh sum (car shift-xor)) val))
  8805. (if (> eat 0) (setq eat (- eat 1)))
  8806. (cond
  8807. ((= 0 eat) ;multi byte
  8808. (setq ret (concat ret (org-char-to-string sum)))
  8809. (setq sum 0))
  8810. ((not bytes) ; single byte(s)
  8811. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8812. )) ;; end (while bytes
  8813. ret )))
  8814. (defun org-link-unescape-single-byte-sequence (hex)
  8815. "Unhexify hex-encoded single byte character sequences."
  8816. (mapconcat (lambda (byte)
  8817. (char-to-string (string-to-number byte 16)))
  8818. (cdr (split-string hex "%")) ""))
  8819. (defun org-xor (a b)
  8820. "Exclusive or."
  8821. (if a (not b) b))
  8822. (defun org-fixup-message-id-for-http (s)
  8823. "Replace special characters in a message id, so it can be used in an http query."
  8824. (when (string-match "%" s)
  8825. (setq s (mapconcat (lambda (c)
  8826. (if (eq c ?%)
  8827. "%25"
  8828. (char-to-string c)))
  8829. s "")))
  8830. (while (string-match "<" s)
  8831. (setq s (replace-match "%3C" t t s)))
  8832. (while (string-match ">" s)
  8833. (setq s (replace-match "%3E" t t s)))
  8834. (while (string-match "@" s)
  8835. (setq s (replace-match "%40" t t s)))
  8836. s)
  8837. (defun org-link-prettify (link)
  8838. "Return a human-readable representation of LINK.
  8839. The car of LINK must be a raw link the cdr of LINK must be either
  8840. a link description or nil."
  8841. (let ((desc (or (cadr link) "<no description>")))
  8842. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8843. "<" (car link) ">")))
  8844. ;;;###autoload
  8845. (defun org-insert-link-global ()
  8846. "Insert a link like Org-mode does.
  8847. This command can be called in any mode to insert a link in Org-mode syntax."
  8848. (interactive)
  8849. (org-load-modules-maybe)
  8850. (org-run-like-in-org-mode 'org-insert-link))
  8851. (defun org-insert-all-links (&optional keep)
  8852. "Insert all links in `org-stored-links'."
  8853. (interactive "P")
  8854. (let ((links (copy-sequence org-stored-links)) l)
  8855. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8856. (insert "- ")
  8857. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8858. (insert "\n"))))
  8859. (defun org-link-fontify-links-to-this-file ()
  8860. "Fontify links to the current file in `org-stored-links'."
  8861. (let ((f (buffer-file-name)) a b)
  8862. (setq a (mapcar (lambda(l)
  8863. (let ((ll (car l)))
  8864. (when (and (string-match "^file:\\(.+\\)::" ll)
  8865. (equal f (expand-file-name (match-string 1 ll))))
  8866. ll)))
  8867. org-stored-links))
  8868. (when (featurep 'org-id)
  8869. (setq b (mapcar (lambda(l)
  8870. (let ((ll (car l)))
  8871. (when (and (string-match "^id:\\(.+\\)$" ll)
  8872. (equal f (expand-file-name
  8873. (or (org-id-find-id-file
  8874. (match-string 1 ll)) ""))))
  8875. ll)))
  8876. org-stored-links)))
  8877. (mapcar (lambda(l)
  8878. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8879. (delq nil (append a b)))))
  8880. (defvar org-link-links-in-this-file nil)
  8881. (defun org-insert-link (&optional complete-file link-location default-description)
  8882. "Insert a link. At the prompt, enter the link.
  8883. Completion can be used to insert any of the link protocol prefixes like
  8884. http or ftp in use.
  8885. The history can be used to select a link previously stored with
  8886. `org-store-link'. When the empty string is entered (i.e. if you just
  8887. press RET at the prompt), the link defaults to the most recently
  8888. stored link. As SPC triggers completion in the minibuffer, you need to
  8889. use M-SPC or C-q SPC to force the insertion of a space character.
  8890. You will also be prompted for a description, and if one is given, it will
  8891. be displayed in the buffer instead of the link.
  8892. If there is already a link at point, this command will allow you to edit link
  8893. and description parts.
  8894. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8895. be selected using completion. The path to the file will be relative to the
  8896. current directory if the file is in the current directory or a subdirectory.
  8897. Otherwise, the link will be the absolute path as completed in the minibuffer
  8898. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8899. option `org-link-file-path-type'.
  8900. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8901. the current directory or below.
  8902. With three \\[universal-argument] prefixes, negate the meaning of
  8903. `org-keep-stored-link-after-insertion'.
  8904. If `org-make-link-description-function' is non-nil, this function will be
  8905. called with the link target, and the result will be the default
  8906. link description.
  8907. If the LINK-LOCATION parameter is non-nil, this value will be
  8908. used as the link location instead of reading one interactively.
  8909. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8910. be used as the default description."
  8911. (interactive "P")
  8912. (let* ((wcf (current-window-configuration))
  8913. (origbuf (current-buffer))
  8914. (region (if (org-region-active-p)
  8915. (buffer-substring (region-beginning) (region-end))))
  8916. (remove (and region (list (region-beginning) (region-end))))
  8917. (desc region)
  8918. tmphist ; byte-compile incorrectly complains about this
  8919. (link link-location)
  8920. (abbrevs org-link-abbrev-alist-local)
  8921. entry file all-prefixes auto-desc)
  8922. (cond
  8923. (link-location) ; specified by arg, just use it.
  8924. ((org-in-regexp org-bracket-link-regexp 1)
  8925. ;; We do have a link at point, and we are going to edit it.
  8926. (setq remove (list (match-beginning 0) (match-end 0)))
  8927. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8928. (setq link (read-string "Link: "
  8929. (org-link-unescape
  8930. (org-match-string-no-properties 1)))))
  8931. ((or (org-in-regexp org-angle-link-re)
  8932. (org-in-regexp org-plain-link-re))
  8933. ;; Convert to bracket link
  8934. (setq remove (list (match-beginning 0) (match-end 0))
  8935. link (read-string "Link: "
  8936. (org-remove-angle-brackets (match-string 0)))))
  8937. ((member complete-file '((4) (16)))
  8938. ;; Completing read for file names.
  8939. (setq link (org-file-complete-link complete-file)))
  8940. (t
  8941. ;; Read link, with completion for stored links.
  8942. (org-link-fontify-links-to-this-file)
  8943. (org-switch-to-buffer-other-window "*Org Links*")
  8944. (with-current-buffer "*Org Links*"
  8945. (erase-buffer)
  8946. (insert "Insert a link.
  8947. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8948. (when org-stored-links
  8949. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8950. (insert (mapconcat 'org-link-prettify
  8951. (reverse org-stored-links) "\n")))
  8952. (goto-char (point-min)))
  8953. (let ((cw (selected-window)))
  8954. (select-window (get-buffer-window "*Org Links*" 'visible))
  8955. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8956. (unless (pos-visible-in-window-p (point-max))
  8957. (org-fit-window-to-buffer))
  8958. (and (window-live-p cw) (select-window cw)))
  8959. ;; Fake a link history, containing the stored links.
  8960. (setq tmphist (append (mapcar 'car org-stored-links)
  8961. org-insert-link-history))
  8962. (setq all-prefixes (append (mapcar 'car abbrevs)
  8963. (mapcar 'car org-link-abbrev-alist)
  8964. org-link-types))
  8965. (unwind-protect
  8966. (progn
  8967. (setq link
  8968. (org-completing-read
  8969. "Link: "
  8970. (append
  8971. (mapcar (lambda (x) (concat x ":"))
  8972. all-prefixes)
  8973. (mapcar 'car org-stored-links))
  8974. nil nil nil
  8975. 'tmphist
  8976. (caar org-stored-links)))
  8977. (if (not (string-match "\\S-" link))
  8978. (error "No link selected"))
  8979. (mapc (lambda(l)
  8980. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8981. org-stored-links)
  8982. (if (or (member link all-prefixes)
  8983. (and (equal ":" (substring link -1))
  8984. (member (substring link 0 -1) all-prefixes)
  8985. (setq link (substring link 0 -1))))
  8986. (setq link (with-current-buffer origbuf
  8987. (org-link-try-special-completion link)))))
  8988. (set-window-configuration wcf)
  8989. (kill-buffer "*Org Links*"))
  8990. (setq entry (assoc link org-stored-links))
  8991. (or entry (push link org-insert-link-history))
  8992. (setq desc (or desc (nth 1 entry)))))
  8993. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8994. (not org-keep-stored-link-after-insertion))
  8995. (setq org-stored-links (delq (assoc link org-stored-links)
  8996. org-stored-links)))
  8997. (if (string-match org-plain-link-re link)
  8998. ;; URL-like link, normalize the use of angular brackets.
  8999. (setq link (org-remove-angle-brackets link)))
  9000. ;; Check if we are linking to the current file with a search
  9001. ;; option If yes, simplify the link by using only the search
  9002. ;; option.
  9003. (when (and buffer-file-name
  9004. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9005. (let* ((path (match-string 1 link))
  9006. (case-fold-search nil)
  9007. (search (match-string 2 link)))
  9008. (save-match-data
  9009. (if (equal (file-truename buffer-file-name) (file-truename path))
  9010. ;; We are linking to this same file, with a search option
  9011. (setq link search)))))
  9012. ;; Check if we can/should use a relative path. If yes, simplify the link
  9013. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9014. (let* ((type (match-string 1 link))
  9015. (path (match-string 2 link))
  9016. (origpath path)
  9017. (case-fold-search nil))
  9018. (cond
  9019. ((or (eq org-link-file-path-type 'absolute)
  9020. (equal complete-file '(16)))
  9021. (setq path (abbreviate-file-name (expand-file-name path))))
  9022. ((eq org-link-file-path-type 'noabbrev)
  9023. (setq path (expand-file-name path)))
  9024. ((eq org-link-file-path-type 'relative)
  9025. (setq path (file-relative-name path)))
  9026. (t
  9027. (save-match-data
  9028. (if (string-match (concat "^" (regexp-quote
  9029. (expand-file-name
  9030. (file-name-as-directory
  9031. default-directory))))
  9032. (expand-file-name path))
  9033. ;; We are linking a file with relative path name.
  9034. (setq path (substring (expand-file-name path)
  9035. (match-end 0)))
  9036. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9037. (setq link (concat type path))
  9038. (if (equal desc origpath)
  9039. (setq desc path))))
  9040. (if org-make-link-description-function
  9041. (setq desc
  9042. (or (condition-case nil
  9043. (funcall org-make-link-description-function link desc)
  9044. (error (progn (message "Can't get link description from `%s'"
  9045. (symbol-name org-make-link-description-function))
  9046. (sit-for 2) nil)))
  9047. (read-string "Description: " default-description)))
  9048. (if default-description (setq desc default-description)
  9049. (setq desc (or (and auto-desc desc)
  9050. (read-string "Description: " desc)))))
  9051. (unless (string-match "\\S-" desc) (setq desc nil))
  9052. (if remove (apply 'delete-region remove))
  9053. (insert (org-make-link-string link desc))))
  9054. (defun org-link-try-special-completion (type)
  9055. "If there is completion support for link type TYPE, offer it."
  9056. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9057. (if (functionp fun)
  9058. (funcall fun)
  9059. (read-string "Link (no completion support): " (concat type ":")))))
  9060. (defun org-file-complete-link (&optional arg)
  9061. "Create a file link using completion."
  9062. (let (file link)
  9063. (setq file (org-iread-file-name "File: "))
  9064. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9065. (pwd1 (file-name-as-directory (abbreviate-file-name
  9066. (expand-file-name ".")))))
  9067. (cond
  9068. ((equal arg '(16))
  9069. (setq link (concat
  9070. "file:"
  9071. (abbreviate-file-name (expand-file-name file)))))
  9072. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9073. (setq link (concat "file:" (match-string 1 file))))
  9074. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9075. (expand-file-name file))
  9076. (setq link (concat
  9077. "file:" (match-string 1 (expand-file-name file)))))
  9078. (t (setq link (concat "file:" file)))))
  9079. link))
  9080. (defun org-iread-file-name (&rest args)
  9081. "Read-file-name using `ido-mode' speedup if available.
  9082. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9083. See `read-file-name' for a description of parameters."
  9084. (org-without-partial-completion
  9085. (if (and org-completion-use-ido
  9086. (fboundp 'ido-read-file-name)
  9087. (boundp 'ido-mode) ido-mode
  9088. (listp (second args)))
  9089. (let ((ido-enter-matching-directory nil))
  9090. (apply 'ido-read-file-name args))
  9091. (apply 'read-file-name args))))
  9092. (defun org-completing-read (&rest args)
  9093. "Completing-read with SPACE being a normal character."
  9094. (let ((enable-recursive-minibuffers t)
  9095. (minibuffer-local-completion-map
  9096. (copy-keymap minibuffer-local-completion-map)))
  9097. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9098. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9099. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9100. (apply 'org-icompleting-read args)))
  9101. (defun org-completing-read-no-i (&rest args)
  9102. (let (org-completion-use-ido org-completion-use-iswitchb)
  9103. (apply 'org-completing-read args)))
  9104. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9105. "Use iswitch as a completing-read replacement to choose from choices.
  9106. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9107. from."
  9108. (let* ((iswitchb-use-virtual-buffers nil)
  9109. (iswitchb-make-buflist-hook
  9110. (lambda ()
  9111. (setq iswitchb-temp-buflist choices))))
  9112. (iswitchb-read-buffer prompt)))
  9113. (defun org-icompleting-read (&rest args)
  9114. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9115. (org-without-partial-completion
  9116. (if (and org-completion-use-ido
  9117. (fboundp 'ido-completing-read)
  9118. (boundp 'ido-mode) ido-mode
  9119. (listp (second args)))
  9120. (let ((ido-enter-matching-directory nil))
  9121. (apply 'ido-completing-read (concat (car args))
  9122. (if (consp (car (nth 1 args)))
  9123. (mapcar 'car (nth 1 args))
  9124. (nth 1 args))
  9125. (cddr args)))
  9126. (if (and org-completion-use-iswitchb
  9127. (boundp 'iswitchb-mode) iswitchb-mode
  9128. (listp (second args)))
  9129. (apply 'org-iswitchb-completing-read (concat (car args))
  9130. (if (consp (car (nth 1 args)))
  9131. (mapcar 'car (nth 1 args))
  9132. (nth 1 args))
  9133. (cddr args))
  9134. (apply 'completing-read args)))))
  9135. (defun org-extract-attributes (s)
  9136. "Extract the attributes cookie from a string and set as text property."
  9137. (let (a attr (start 0) key value)
  9138. (save-match-data
  9139. (when (string-match "{{\\([^}]+\\)}}$" s)
  9140. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9141. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9142. (setq key (match-string 1 a) value (match-string 2 a)
  9143. start (match-end 0)
  9144. attr (plist-put attr (intern key) value))))
  9145. (org-add-props s nil 'org-attr attr))
  9146. s))
  9147. (defun org-extract-attributes-from-string (tag)
  9148. (let (key value attr)
  9149. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  9150. (setq key (match-string 1 tag) value (match-string 2 tag)
  9151. tag (replace-match "" t t tag)
  9152. attr (plist-put attr (intern key) value)))
  9153. (cons tag attr)))
  9154. (defun org-attributes-to-string (plist)
  9155. "Format a property list into an HTML attribute list."
  9156. (let ((s "") key value)
  9157. (while plist
  9158. (setq key (pop plist) value (pop plist))
  9159. (and value
  9160. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  9161. s))
  9162. ;;; Opening/following a link
  9163. (defvar org-link-search-failed nil)
  9164. (defvar org-open-link-functions nil
  9165. "Hook for functions finding a plain text link.
  9166. These functions must take a single argument, the link content.
  9167. They will be called for links that look like [[link text][description]]
  9168. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9169. like a filename (e.g. \"./blue.png\").
  9170. These functions will be called *before* Org attempts to resolve the
  9171. link by doing text searches in the current buffer - so if you want a
  9172. link \"[[target]]\" to still find \"<<target>>\", your function should
  9173. handle this as a special case.
  9174. When the function does handle the link, it must return a non-nil value.
  9175. If it decides that it is not responsible for this link, it must return
  9176. nil to indicate that that Org-mode can continue with other options
  9177. like exact and fuzzy text search.")
  9178. (defun org-next-link (&optional search-backward)
  9179. "Move forward to the next link.
  9180. If the link is in hidden text, expose it."
  9181. (interactive "P")
  9182. (when (and org-link-search-failed (eq this-command last-command))
  9183. (goto-char (point-min))
  9184. (message "Link search wrapped back to beginning of buffer"))
  9185. (setq org-link-search-failed nil)
  9186. (let* ((pos (point))
  9187. (ct (org-context))
  9188. (a (assoc :link ct))
  9189. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9190. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9191. ((looking-at org-any-link-re)
  9192. ;; Don't stay stuck at link without an org-link face
  9193. (forward-char (if search-backward -1 1))))
  9194. (if (funcall srch-fun org-any-link-re nil t)
  9195. (progn
  9196. (goto-char (match-beginning 0))
  9197. (if (outline-invisible-p) (org-show-context)))
  9198. (goto-char pos)
  9199. (setq org-link-search-failed t)
  9200. (message "No further link found"))))
  9201. (defun org-previous-link ()
  9202. "Move backward to the previous link.
  9203. If the link is in hidden text, expose it."
  9204. (interactive)
  9205. (funcall 'org-next-link t))
  9206. (defun org-translate-link (s)
  9207. "Translate a link string if a translation function has been defined."
  9208. (if (and org-link-translation-function
  9209. (fboundp org-link-translation-function)
  9210. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9211. (progn
  9212. (setq s (funcall org-link-translation-function
  9213. (match-string 1 s) (match-string 2 s)))
  9214. (concat (car s) ":" (cdr s)))
  9215. s))
  9216. (defun org-translate-link-from-planner (type path)
  9217. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9218. This is still an experimental function, your mileage may vary."
  9219. (cond
  9220. ((member type '("http" "https" "news" "ftp"))
  9221. ;; standard Internet links are the same.
  9222. nil)
  9223. ((and (equal type "irc") (string-match "^//" path))
  9224. ;; Planner has two / at the beginning of an irc link, we have 1.
  9225. ;; We should have zero, actually....
  9226. (setq path (substring path 1)))
  9227. ((and (equal type "lisp") (string-match "^/" path))
  9228. ;; Planner has a slash, we do not.
  9229. (setq type "elisp" path (substring path 1)))
  9230. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9231. ;; A typical message link. Planner has the id after the final slash,
  9232. ;; we separate it with a hash mark
  9233. (setq path (concat (match-string 1 path) "#"
  9234. (org-remove-angle-brackets (match-string 2 path)))))
  9235. )
  9236. (cons type path))
  9237. (defun org-find-file-at-mouse (ev)
  9238. "Open file link or URL at mouse."
  9239. (interactive "e")
  9240. (mouse-set-point ev)
  9241. (org-open-at-point 'in-emacs))
  9242. (defun org-open-at-mouse (ev)
  9243. "Open file link or URL at mouse.
  9244. See the docstring of `org-open-file' for details."
  9245. (interactive "e")
  9246. (mouse-set-point ev)
  9247. (if (eq major-mode 'org-agenda-mode)
  9248. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9249. (org-open-at-point))
  9250. (defvar org-window-config-before-follow-link nil
  9251. "The window configuration before following a link.
  9252. This is saved in case the need arises to restore it.")
  9253. (defvar org-open-link-marker (make-marker)
  9254. "Marker pointing to the location where `org-open-at-point; was called.")
  9255. ;;;###autoload
  9256. (defun org-open-at-point-global ()
  9257. "Follow a link like Org-mode does.
  9258. This command can be called in any mode to follow a link that has
  9259. Org-mode syntax."
  9260. (interactive)
  9261. (org-run-like-in-org-mode 'org-open-at-point))
  9262. ;;;###autoload
  9263. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9264. "Open a link in the string S, as if it was in Org-mode."
  9265. (interactive "sLink: \nP")
  9266. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9267. (with-temp-buffer
  9268. (let ((org-inhibit-startup (not reference-buffer)))
  9269. (org-mode)
  9270. (insert s)
  9271. (goto-char (point-min))
  9272. (when reference-buffer
  9273. (setq org-link-abbrev-alist-local
  9274. (with-current-buffer reference-buffer
  9275. org-link-abbrev-alist-local)))
  9276. (org-open-at-point arg reference-buffer)))))
  9277. (defvar org-open-at-point-functions nil
  9278. "Hook that is run when following a link at point.
  9279. Functions in this hook must return t if they identify and follow
  9280. a link at point. If they don't find anything interesting at point,
  9281. they must return nil.")
  9282. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9283. (defun org-open-at-point (&optional arg reference-buffer)
  9284. "Open link at or after point.
  9285. If there is no link at point, this function will search forward up to
  9286. the end of the current line.
  9287. Normally, files will be opened by an appropriate application. If the
  9288. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9289. With a double prefix argument, try to open outside of Emacs, in the
  9290. application the system uses for this file type."
  9291. (interactive "P")
  9292. ;; if in a code block, then open the block's results
  9293. (unless (call-interactively #'org-babel-open-src-block-result)
  9294. (org-load-modules-maybe)
  9295. (move-marker org-open-link-marker (point))
  9296. (setq org-window-config-before-follow-link (current-window-configuration))
  9297. (org-remove-occur-highlights nil nil t)
  9298. (cond
  9299. ((and (org-at-heading-p)
  9300. (not (org-at-timestamp-p t))
  9301. (not (org-in-regexp
  9302. (concat org-plain-link-re "\\|"
  9303. org-bracket-link-regexp "\\|"
  9304. org-angle-link-re "\\|"
  9305. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9306. (not (get-text-property (point) 'org-linked-text)))
  9307. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9308. (lk0 (car lkall))
  9309. (lk (if (stringp lk0) (list lk0) lk0))
  9310. (lkend (cdr lkall)))
  9311. (mapcar (lambda(l)
  9312. (search-forward l nil lkend)
  9313. (goto-char (match-beginning 0))
  9314. (org-open-at-point))
  9315. lk))
  9316. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9317. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9318. ((and (org-at-timestamp-p t)
  9319. (not (org-in-regexp org-bracket-link-regexp)))
  9320. (org-follow-timestamp-link))
  9321. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9322. (not (org-in-regexp org-any-link-re)))
  9323. (org-footnote-action))
  9324. (t
  9325. (let (type path link line search (pos (point)))
  9326. (catch 'match
  9327. (save-excursion
  9328. (skip-chars-forward "^]\n\r")
  9329. (when (org-in-regexp org-bracket-link-regexp 1)
  9330. (setq link (org-extract-attributes
  9331. (org-link-unescape (org-match-string-no-properties 1))))
  9332. (while (string-match " *\n *" link)
  9333. (setq link (replace-match " " t t link)))
  9334. (setq link (org-link-expand-abbrev link))
  9335. (cond
  9336. ((or (file-name-absolute-p link)
  9337. (string-match "^\\.\\.?/" link))
  9338. (setq type "file" path link))
  9339. ((string-match org-link-re-with-space3 link)
  9340. (setq type (match-string 1 link) path (match-string 2 link)))
  9341. ((string-match "^help:+\\(.+\\)" link)
  9342. (setq type "help" path (match-string 1 link)))
  9343. (t (setq type "thisfile" path link)))
  9344. (throw 'match t)))
  9345. (when (get-text-property (point) 'org-linked-text)
  9346. (setq type "thisfile"
  9347. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9348. (1+ (point)) (point))
  9349. path (buffer-substring
  9350. (or (previous-single-property-change pos 'org-linked-text)
  9351. (point-min))
  9352. (or (next-single-property-change pos 'org-linked-text)
  9353. (point-max))))
  9354. (throw 'match t))
  9355. (save-excursion
  9356. (when (or (org-in-regexp org-angle-link-re)
  9357. (let ((match (org-in-regexp org-plain-link-re)))
  9358. ;; Check a plain link is not within a bracket link
  9359. (and match
  9360. (save-excursion
  9361. (progn
  9362. (goto-char (car match))
  9363. (not (org-in-regexp org-bracket-link-regexp))))))
  9364. (let ((line_ending (save-excursion (end-of-line) (point))))
  9365. ;; We are in a line before a plain or bracket link
  9366. (or (re-search-forward org-plain-link-re line_ending t)
  9367. (re-search-forward org-bracket-link-regexp line_ending t))))
  9368. (setq type (match-string 1)
  9369. path (org-link-unescape (match-string 2)))
  9370. (throw 'match t)))
  9371. (save-excursion
  9372. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9373. (setq type "tags"
  9374. path (match-string 1))
  9375. (while (string-match ":" path)
  9376. (setq path (replace-match "+" t t path)))
  9377. (throw 'match t)))
  9378. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9379. (setq type "tree-match"
  9380. path (match-string 1))
  9381. (throw 'match t)))
  9382. (unless path
  9383. (user-error "No link found"))
  9384. ;; switch back to reference buffer
  9385. ;; needed when if called in a temporary buffer through
  9386. ;; org-open-link-from-string
  9387. (with-current-buffer (or reference-buffer (current-buffer))
  9388. ;; Remove any trailing spaces in path
  9389. (if (string-match " +\\'" path)
  9390. (setq path (replace-match "" t t path)))
  9391. (if (and org-link-translation-function
  9392. (fboundp org-link-translation-function))
  9393. ;; Check if we need to translate the link
  9394. (let ((tmp (funcall org-link-translation-function type path)))
  9395. (setq type (car tmp) path (cdr tmp))))
  9396. (cond
  9397. ((assoc type org-link-protocols)
  9398. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9399. ((equal type "help")
  9400. (let ((f-or-v (intern path)))
  9401. (cond ((fboundp f-or-v)
  9402. (describe-function f-or-v))
  9403. ((boundp f-or-v)
  9404. (describe-variable f-or-v))
  9405. (t (error "Not a known function or variable")))))
  9406. ((equal type "mailto")
  9407. (let ((cmd (car org-link-mailto-program))
  9408. (args (cdr org-link-mailto-program)) args1
  9409. (address path) (subject "") a)
  9410. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9411. (setq address (match-string 1 path)
  9412. subject (org-link-escape (match-string 2 path))))
  9413. (while args
  9414. (cond
  9415. ((not (stringp (car args))) (push (pop args) args1))
  9416. (t (setq a (pop args))
  9417. (if (string-match "%a" a)
  9418. (setq a (replace-match address t t a)))
  9419. (if (string-match "%s" a)
  9420. (setq a (replace-match subject t t a)))
  9421. (push a args1))))
  9422. (apply cmd (nreverse args1))))
  9423. ((member type '("http" "https" "ftp" "news"))
  9424. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9425. (org-link-escape
  9426. path org-link-escape-chars-browser)
  9427. path))))
  9428. ((string= type "doi")
  9429. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9430. (org-link-escape
  9431. path org-link-escape-chars-browser)
  9432. path))))
  9433. ((member type '("message"))
  9434. (browse-url (concat type ":" path)))
  9435. ((string= type "tags")
  9436. (org-tags-view arg path))
  9437. ((string= type "tree-match")
  9438. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9439. ((string= type "file")
  9440. (if (string-match "::\\([0-9]+\\)\\'" path)
  9441. (setq line (string-to-number (match-string 1 path))
  9442. path (substring path 0 (match-beginning 0)))
  9443. (if (string-match "::\\(.+\\)\\'" path)
  9444. (setq search (match-string 1 path)
  9445. path (substring path 0 (match-beginning 0)))))
  9446. (if (string-match "[*?{]" (file-name-nondirectory path))
  9447. (dired path)
  9448. (org-open-file path arg line search)))
  9449. ((string= type "shell")
  9450. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9451. (cmd path))
  9452. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9453. (string-match org-confirm-shell-link-not-regexp cmd))
  9454. (not org-confirm-shell-link-function)
  9455. (funcall org-confirm-shell-link-function
  9456. (format "Execute \"%s\" in shell? "
  9457. (org-add-props cmd nil
  9458. 'face 'org-warning))))
  9459. (progn
  9460. (message "Executing %s" cmd)
  9461. (shell-command cmd buf)
  9462. (if (featurep 'midnight)
  9463. (setq clean-buffer-list-kill-buffer-names
  9464. (cons buf clean-buffer-list-kill-buffer-names))))
  9465. (error "Abort"))))
  9466. ((string= type "elisp")
  9467. (let ((cmd path))
  9468. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9469. (string-match org-confirm-elisp-link-not-regexp cmd))
  9470. (not org-confirm-elisp-link-function)
  9471. (funcall org-confirm-elisp-link-function
  9472. (format "Execute \"%s\" as elisp? "
  9473. (org-add-props cmd nil
  9474. 'face 'org-warning))))
  9475. (message "%s => %s" cmd
  9476. (if (equal (string-to-char cmd) ?\()
  9477. (eval (read cmd))
  9478. (call-interactively (read cmd))))
  9479. (error "Abort"))))
  9480. ((and (string= type "thisfile")
  9481. (run-hook-with-args-until-success
  9482. 'org-open-link-functions path)))
  9483. ((string= type "thisfile")
  9484. (if arg
  9485. (switch-to-buffer-other-window
  9486. (org-get-buffer-for-internal-link (current-buffer)))
  9487. (org-mark-ring-push))
  9488. (let ((cmd `(org-link-search
  9489. ,path
  9490. ,(cond ((equal arg '(4)) ''occur)
  9491. ((equal arg '(16)) ''org-occur))
  9492. ,pos)))
  9493. (condition-case nil (let ((org-link-search-inhibit-query t))
  9494. (eval cmd))
  9495. (error (progn (widen) (eval cmd))))))
  9496. (t (browse-url-at-point)))))))
  9497. (move-marker org-open-link-marker nil)
  9498. (run-hook-with-args 'org-follow-link-hook)))
  9499. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9500. "Offer links in the current entry and return the selected link.
  9501. If there is only one link, return it.
  9502. If NTH is an integer, return the NTH link found.
  9503. If ZERO is a string, check also this string for a link, and if
  9504. there is one, return it."
  9505. (with-current-buffer buffer
  9506. (save-excursion
  9507. (save-restriction
  9508. (widen)
  9509. (goto-char marker)
  9510. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9511. "\\(" org-angle-link-re "\\)\\|"
  9512. "\\(" org-plain-link-re "\\)"))
  9513. (cnt ?0)
  9514. (in-emacs (if (integerp nth) nil nth))
  9515. have-zero end links link c)
  9516. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9517. (push (match-string 0 zero) links)
  9518. (setq cnt (1- cnt) have-zero t))
  9519. (save-excursion
  9520. (org-back-to-heading t)
  9521. (setq end (save-excursion (outline-next-heading) (point)))
  9522. (while (re-search-forward re end t)
  9523. (push (match-string 0) links))
  9524. (setq links (org-uniquify (reverse links))))
  9525. (cond
  9526. ((null links)
  9527. (message "No links"))
  9528. ((equal (length links) 1)
  9529. (setq link (car links)))
  9530. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9531. (setq link (nth (if have-zero nth (1- nth)) links)))
  9532. (t ; we have to select a link
  9533. (save-excursion
  9534. (save-window-excursion
  9535. (delete-other-windows)
  9536. (with-output-to-temp-buffer "*Select Link*"
  9537. (mapc (lambda (l)
  9538. (if (not (string-match org-bracket-link-regexp l))
  9539. (princ (format "[%c] %s\n" (incf cnt)
  9540. (org-remove-angle-brackets l)))
  9541. (if (match-end 3)
  9542. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9543. (match-string 3 l) (match-string 1 l)))
  9544. (princ (format "[%c] %s\n" (incf cnt)
  9545. (match-string 1 l))))))
  9546. links))
  9547. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9548. (message "Select link to open, RET to open all:")
  9549. (setq c (read-char-exclusive))
  9550. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9551. (when (equal c ?q) (error "Abort"))
  9552. (if (equal c ?\C-m)
  9553. (setq link links)
  9554. (setq nth (- c ?0))
  9555. (if have-zero (setq nth (1+ nth)))
  9556. (unless (and (integerp nth) (>= (length links) nth))
  9557. (error "Invalid link selection"))
  9558. (setq link (nth (1- nth) links)))))
  9559. (cons link end))))))
  9560. ;; Add special file links that specify the way of opening
  9561. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9562. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9563. (defun org-open-file-with-system (path)
  9564. "Open file at PATH using the system way of opening it."
  9565. (org-open-file path 'system))
  9566. (defun org-open-file-with-emacs (path)
  9567. "Open file at PATH in Emacs."
  9568. (org-open-file path 'emacs))
  9569. ;;; File search
  9570. (defvar org-create-file-search-functions nil
  9571. "List of functions to construct the right search string for a file link.
  9572. These functions are called in turn with point at the location to
  9573. which the link should point.
  9574. A function in the hook should first test if it would like to
  9575. handle this file type, for example by checking the `major-mode'
  9576. or the file extension. If it decides not to handle this file, it
  9577. should just return nil to give other functions a chance. If it
  9578. does handle the file, it must return the search string to be used
  9579. when following the link. The search string will be part of the
  9580. file link, given after a double colon, and `org-open-at-point'
  9581. will automatically search for it. If special measures must be
  9582. taken to make the search successful, another function should be
  9583. added to the companion hook `org-execute-file-search-functions',
  9584. which see.
  9585. A function in this hook may also use `setq' to set the variable
  9586. `description' to provide a suggestion for the descriptive text to
  9587. be used for this link when it gets inserted into an Org-mode
  9588. buffer with \\[org-insert-link].")
  9589. (defvar org-execute-file-search-functions nil
  9590. "List of functions to execute a file search triggered by a link.
  9591. Functions added to this hook must accept a single argument, the
  9592. search string that was part of the file link, the part after the
  9593. double colon. The function must first check if it would like to
  9594. handle this search, for example by checking the `major-mode' or
  9595. the file extension. If it decides not to handle this search, it
  9596. should just return nil to give other functions a chance. If it
  9597. does handle the search, it must return a non-nil value to keep
  9598. other functions from trying.
  9599. Each function can access the current prefix argument through the
  9600. variable `current-prefix-arg'. Note that a single prefix is used
  9601. to force opening a link in Emacs, so it may be good to only use a
  9602. numeric or double prefix to guide the search function.
  9603. In case this is needed, a function in this hook can also restore
  9604. the window configuration before `org-open-at-point' was called using:
  9605. (set-window-configuration org-window-config-before-follow-link)")
  9606. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9607. (defun org-link-search (s &optional type avoid-pos stealth)
  9608. "Search for a link search option.
  9609. If S is surrounded by forward slashes, it is interpreted as a
  9610. regular expression. In org-mode files, this will create an `org-occur'
  9611. sparse tree. In ordinary files, `occur' will be used to list matches.
  9612. If the current buffer is in `dired-mode', grep will be used to search
  9613. in all files. If AVOID-POS is given, ignore matches near that position.
  9614. When optional argument STEALTH is non-nil, do not modify
  9615. visibility around point, thus ignoring
  9616. `org-show-hierarchy-above', `org-show-following-heading' and
  9617. `org-show-siblings' variables."
  9618. (let ((case-fold-search t)
  9619. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9620. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9621. (append '(("") (" ") ("\t") ("\n"))
  9622. org-emphasis-alist)
  9623. "\\|") "\\)"))
  9624. (pos (point))
  9625. (pre nil) (post nil)
  9626. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9627. (cond
  9628. ;; First check if there are any special search functions
  9629. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9630. ;; Now try the builtin stuff
  9631. ((and (equal (string-to-char s0) ?#)
  9632. (> (length s0) 1)
  9633. (save-excursion
  9634. (goto-char (point-min))
  9635. (and
  9636. (re-search-forward
  9637. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9638. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9639. (setq type 'dedicated
  9640. pos (match-beginning 0))))
  9641. ;; There is an exact target for this
  9642. (goto-char pos)
  9643. (org-back-to-heading t)))
  9644. ((save-excursion
  9645. (goto-char (point-min))
  9646. (and
  9647. (re-search-forward
  9648. (concat "<<" (regexp-quote s0) ">>") nil t)
  9649. (setq type 'dedicated
  9650. pos (match-beginning 0))))
  9651. ;; There is an exact target for this
  9652. (goto-char pos))
  9653. ((save-excursion
  9654. (goto-char (point-min))
  9655. (and
  9656. (re-search-forward
  9657. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9658. (setq type 'dedicated pos (match-beginning 0))))
  9659. ;; Found an element with a matching #+name affiliated keyword.
  9660. (goto-char pos))
  9661. ((and (string-match "^(\\(.*\\))$" s0)
  9662. (save-excursion
  9663. (goto-char (point-min))
  9664. (and
  9665. (re-search-forward
  9666. (concat "[^[]" (regexp-quote
  9667. (format org-coderef-label-format
  9668. (match-string 1 s0))))
  9669. nil t)
  9670. (setq type 'dedicated
  9671. pos (1+ (match-beginning 0))))))
  9672. ;; There is a coderef target for this
  9673. (goto-char pos))
  9674. ((string-match "^/\\(.*\\)/$" s)
  9675. ;; A regular expression
  9676. (cond
  9677. ((derived-mode-p 'org-mode)
  9678. (org-occur (match-string 1 s)))
  9679. (t (org-do-occur (match-string 1 s)))))
  9680. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9681. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9682. (goto-char (point-min))
  9683. (cond
  9684. ((let (case-fold-search)
  9685. (re-search-forward (format org-complex-heading-regexp-format
  9686. (regexp-quote s))
  9687. nil t))
  9688. ;; OK, found a match
  9689. (setq type 'dedicated)
  9690. (goto-char (match-beginning 0)))
  9691. ((and (not org-link-search-inhibit-query)
  9692. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9693. (y-or-n-p "No match - create this as a new heading? "))
  9694. (goto-char (point-max))
  9695. (or (bolp) (newline))
  9696. (insert "* " s "\n")
  9697. (beginning-of-line 0))
  9698. (t
  9699. (goto-char pos)
  9700. (error "No match"))))
  9701. (t
  9702. ;; A normal search string
  9703. (when (equal (string-to-char s) ?*)
  9704. ;; Anchor on headlines, post may include tags.
  9705. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9706. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9707. s (substring s 1)))
  9708. (remove-text-properties
  9709. 0 (length s)
  9710. '(face nil mouse-face nil keymap nil fontified nil) s)
  9711. ;; Make a series of regular expressions to find a match
  9712. (setq words (org-split-string s "[ \n\r\t]+")
  9713. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9714. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9715. "\\)" markers)
  9716. re2a_ (concat "\\(" (mapconcat 'downcase words
  9717. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9718. re2a (concat "[ \t\r\n]" re2a_)
  9719. re4_ (concat "\\(" (mapconcat 'downcase words
  9720. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9721. re4 (concat "[^a-zA-Z_]" re4_)
  9722. re1 (concat pre re2 post)
  9723. re3 (concat pre (if pre re4_ re4) post)
  9724. re5 (concat pre ".*" re4)
  9725. re2 (concat pre re2)
  9726. re2a (concat pre (if pre re2a_ re2a))
  9727. re4 (concat pre (if pre re4_ re4))
  9728. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9729. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9730. re5 "\\)"
  9731. ))
  9732. (cond
  9733. ((eq type 'org-occur) (org-occur reall))
  9734. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9735. (t (goto-char (point-min))
  9736. (setq type 'fuzzy)
  9737. (if (or (and (org-search-not-self 1 re0 nil t)
  9738. (setq type 'dedicated))
  9739. (org-search-not-self 1 re1 nil t)
  9740. (org-search-not-self 1 re2 nil t)
  9741. (org-search-not-self 1 re2a nil t)
  9742. (org-search-not-self 1 re3 nil t)
  9743. (org-search-not-self 1 re4 nil t)
  9744. (org-search-not-self 1 re5 nil t))
  9745. (goto-char (match-beginning 1))
  9746. (goto-char pos)
  9747. (error "No match"))))))
  9748. (and (derived-mode-p 'org-mode)
  9749. (not stealth)
  9750. (org-show-context 'link-search))
  9751. type))
  9752. (defun org-search-not-self (group &rest args)
  9753. "Execute `re-search-forward', but only accept matches that do not
  9754. enclose the position of `org-open-link-marker'."
  9755. (let ((m org-open-link-marker))
  9756. (catch 'exit
  9757. (while (apply 're-search-forward args)
  9758. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9759. (goto-char (match-end group))
  9760. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9761. (> (match-beginning 0) (marker-position m))
  9762. (< (match-end 0) (marker-position m)))
  9763. (save-match-data
  9764. (or (not (org-in-regexp
  9765. org-bracket-link-analytic-regexp 1))
  9766. (not (match-end 4)) ; no description
  9767. (and (<= (match-beginning 4) (point))
  9768. (>= (match-end 4) (point))))))
  9769. (throw 'exit (point))))))))
  9770. (defun org-get-buffer-for-internal-link (buffer)
  9771. "Return a buffer to be used for displaying the link target of internal links."
  9772. (cond
  9773. ((not org-display-internal-link-with-indirect-buffer)
  9774. buffer)
  9775. ((string-match "(Clone)$" (buffer-name buffer))
  9776. (message "Buffer is already a clone, not making another one")
  9777. ;; we also do not modify visibility in this case
  9778. buffer)
  9779. (t ; make a new indirect buffer for displaying the link
  9780. (let* ((bn (buffer-name buffer))
  9781. (ibn (concat bn "(Clone)"))
  9782. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9783. (with-current-buffer ib (org-overview))
  9784. ib))))
  9785. (defun org-do-occur (regexp &optional cleanup)
  9786. "Call the Emacs command `occur'.
  9787. If CLEANUP is non-nil, remove the printout of the regular expression
  9788. in the *Occur* buffer. This is useful if the regex is long and not useful
  9789. to read."
  9790. (occur regexp)
  9791. (when cleanup
  9792. (let ((cwin (selected-window)) win beg end)
  9793. (when (setq win (get-buffer-window "*Occur*"))
  9794. (select-window win))
  9795. (goto-char (point-min))
  9796. (when (re-search-forward "match[a-z]+" nil t)
  9797. (setq beg (match-end 0))
  9798. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9799. (setq end (1- (match-beginning 0)))))
  9800. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9801. (goto-char (point-min))
  9802. (select-window cwin))))
  9803. ;;; The mark ring for links jumps
  9804. (defvar org-mark-ring nil
  9805. "Mark ring for positions before jumps in Org-mode.")
  9806. (defvar org-mark-ring-last-goto nil
  9807. "Last position in the mark ring used to go back.")
  9808. ;; Fill and close the ring
  9809. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9810. (loop for i from 1 to org-mark-ring-length do
  9811. (push (make-marker) org-mark-ring))
  9812. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9813. org-mark-ring)
  9814. (defun org-mark-ring-push (&optional pos buffer)
  9815. "Put the current position or POS into the mark ring and rotate it."
  9816. (interactive)
  9817. (setq pos (or pos (point)))
  9818. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9819. (move-marker (car org-mark-ring)
  9820. (or pos (point))
  9821. (or buffer (current-buffer)))
  9822. (message "%s"
  9823. (substitute-command-keys
  9824. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9825. (defun org-mark-ring-goto (&optional n)
  9826. "Jump to the previous position in the mark ring.
  9827. With prefix arg N, jump back that many stored positions. When
  9828. called several times in succession, walk through the entire ring.
  9829. Org-mode commands jumping to a different position in the current file,
  9830. or to another Org-mode file, automatically push the old position
  9831. onto the ring."
  9832. (interactive "p")
  9833. (let (p m)
  9834. (if (eq last-command this-command)
  9835. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9836. (setq p org-mark-ring))
  9837. (setq org-mark-ring-last-goto p)
  9838. (setq m (car p))
  9839. (org-pop-to-buffer-same-window (marker-buffer m))
  9840. (goto-char m)
  9841. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9842. (defun org-remove-angle-brackets (s)
  9843. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9844. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9845. s)
  9846. (defun org-add-angle-brackets (s)
  9847. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9848. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9849. s)
  9850. (defun org-remove-double-quotes (s)
  9851. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9852. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9853. s)
  9854. ;;; Following specific links
  9855. (defun org-follow-timestamp-link ()
  9856. "Open an agenda view for the time-stamp date/range at point."
  9857. (cond
  9858. ((org-at-date-range-p t)
  9859. (let ((org-agenda-start-on-weekday)
  9860. (t1 (match-string 1))
  9861. (t2 (match-string 2)) tt1 tt2)
  9862. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9863. tt2 (time-to-days (org-time-string-to-time t2)))
  9864. (let ((org-agenda-buffer-tmp-name
  9865. (format "*Org Agenda(a:%s)"
  9866. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9867. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9868. ((org-at-timestamp-p t)
  9869. (let ((org-agenda-buffer-tmp-name
  9870. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9871. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9872. (substring (match-string 1) 0 10)))
  9873. 1)))
  9874. (t (error "This should not happen"))))
  9875. ;;; Following file links
  9876. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9877. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9878. (declare-function mailcap-mime-info
  9879. "mailcap" (string &optional request no-decode))
  9880. (defvar org-wait nil)
  9881. (defun org-open-file (path &optional in-emacs line search)
  9882. "Open the file at PATH.
  9883. First, this expands any special file name abbreviations. Then the
  9884. configuration variable `org-file-apps' is checked if it contains an
  9885. entry for this file type, and if yes, the corresponding command is launched.
  9886. If no application is found, Emacs simply visits the file.
  9887. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9888. With a double \\[universal-argument] \\[universal-argument] \
  9889. prefix arg, Org tries to avoid opening in Emacs
  9890. and to use an external application to visit the file.
  9891. Optional LINE specifies a line to go to, optional SEARCH a string
  9892. to search for. If LINE or SEARCH is given, the file will be
  9893. opened in Emacs, unless an entry from org-file-apps that makes
  9894. use of groups in a regexp matches.
  9895. If you want to change the way frames are used when following a
  9896. link, please customize `org-link-frame-setup'.
  9897. If the file does not exist, an error is thrown."
  9898. (let* ((file (if (equal path "")
  9899. buffer-file-name
  9900. (substitute-in-file-name (expand-file-name path))))
  9901. (file-apps (append org-file-apps (org-default-apps)))
  9902. (apps (org-remove-if
  9903. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9904. (apps-dlink (org-remove-if-not
  9905. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9906. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9907. (dirp (if remp nil (file-directory-p file)))
  9908. (file (if (and dirp org-open-directory-means-index-dot-org)
  9909. (concat (file-name-as-directory file) "index.org")
  9910. file))
  9911. (a-m-a-p (assq 'auto-mode apps))
  9912. (dfile (downcase file))
  9913. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9914. (link (cond ((and (eq line nil)
  9915. (eq search nil))
  9916. file)
  9917. (line
  9918. (concat file "::" (number-to-string line)))
  9919. (search
  9920. (concat file "::" search))))
  9921. (dlink (downcase link))
  9922. (old-buffer (current-buffer))
  9923. (old-pos (point))
  9924. (old-mode major-mode)
  9925. ext cmd link-match-data)
  9926. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9927. (setq ext (match-string 1 dfile))
  9928. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9929. (setq ext (match-string 1 dfile))))
  9930. (cond
  9931. ((member in-emacs '((16) system))
  9932. (setq cmd (cdr (assoc 'system apps))))
  9933. (in-emacs (setq cmd 'emacs))
  9934. (t
  9935. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9936. (and dirp (cdr (assoc 'directory apps)))
  9937. ; first, try matching against apps-dlink
  9938. ; if we get a match here, store the match data for later
  9939. (let ((match (assoc-default dlink apps-dlink
  9940. 'string-match)))
  9941. (if match
  9942. (progn (setq link-match-data (match-data))
  9943. match)
  9944. (progn (setq in-emacs (or in-emacs line search))
  9945. nil))) ; if we have no match in apps-dlink,
  9946. ; always open the file in emacs if line or search
  9947. ; is given (for backwards compatibility)
  9948. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9949. 'string-match)
  9950. (cdr (assoc ext apps))
  9951. (cdr (assoc t apps))))))
  9952. (when (eq cmd 'system)
  9953. (setq cmd (cdr (assoc 'system apps))))
  9954. (when (eq cmd 'default)
  9955. (setq cmd (cdr (assoc t apps))))
  9956. (when (eq cmd 'mailcap)
  9957. (require 'mailcap)
  9958. (mailcap-parse-mailcaps)
  9959. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9960. (command (mailcap-mime-info mime-type)))
  9961. (if (stringp command)
  9962. (setq cmd command)
  9963. (setq cmd 'emacs))))
  9964. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9965. (not (file-exists-p file))
  9966. (not org-open-non-existing-files))
  9967. (error "No such file: %s" file))
  9968. (cond
  9969. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9970. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9971. (while (string-match "['\"]%s['\"]" cmd)
  9972. (setq cmd (replace-match "%s" t t cmd)))
  9973. (while (string-match "%s" cmd)
  9974. (setq cmd (replace-match
  9975. (save-match-data
  9976. (shell-quote-argument
  9977. (convert-standard-filename file)))
  9978. t t cmd)))
  9979. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9980. (save-match-data
  9981. (let ((match-index 1)
  9982. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9983. (set-match-data link-match-data)
  9984. (while (<= match-index number-of-groups)
  9985. (let ((regex (concat "%" (number-to-string match-index)))
  9986. (replace-with (match-string match-index dlink)))
  9987. (while (string-match regex cmd)
  9988. (setq cmd (replace-match replace-with t t cmd))))
  9989. (setq match-index (+ match-index 1)))))
  9990. (save-window-excursion
  9991. (message "Running %s...done" cmd)
  9992. (start-process-shell-command cmd nil cmd)
  9993. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9994. ((or (stringp cmd)
  9995. (eq cmd 'emacs))
  9996. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9997. (widen)
  9998. (if line (org-goto-line line)
  9999. (if search (org-link-search search))))
  10000. ((consp cmd)
  10001. (let ((file (convert-standard-filename file)))
  10002. (save-match-data
  10003. (set-match-data link-match-data)
  10004. (eval cmd))))
  10005. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10006. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10007. (or (not (equal old-buffer (current-buffer)))
  10008. (not (equal old-pos (point))))
  10009. (org-mark-ring-push old-pos old-buffer))))
  10010. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10011. "This function returns non-nil if `entry' uses a regular
  10012. expression which should be matched against the whole link by
  10013. org-open-file.
  10014. It assumes that is the case when the entry uses a regular
  10015. expression which has at least one grouping construct and the
  10016. action is either a lisp form or a command string containing
  10017. '%1', i.e. using at least one subexpression match as a
  10018. parameter."
  10019. (let ((selector (car entry))
  10020. (action (cdr entry)))
  10021. (if (stringp selector)
  10022. (and (> (regexp-opt-depth selector) 0)
  10023. (or (and (stringp action)
  10024. (string-match "%[0-9]" action))
  10025. (consp action)))
  10026. nil)))
  10027. (defun org-default-apps ()
  10028. "Return the default applications for this operating system."
  10029. (cond
  10030. ((eq system-type 'darwin)
  10031. org-file-apps-defaults-macosx)
  10032. ((eq system-type 'windows-nt)
  10033. org-file-apps-defaults-windowsnt)
  10034. (t org-file-apps-defaults-gnu)))
  10035. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10036. "Convert extensions to regular expressions in the cars of LIST.
  10037. Also, weed out any non-string entries, because the return value is used
  10038. only for regexp matching.
  10039. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10040. point to the symbol `emacs', indicating that the file should
  10041. be opened in Emacs."
  10042. (append
  10043. (delq nil
  10044. (mapcar (lambda (x)
  10045. (if (not (stringp (car x)))
  10046. nil
  10047. (if (string-match "\\W" (car x))
  10048. x
  10049. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10050. list))
  10051. (if add-auto-mode
  10052. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10053. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10054. (defun org-file-remote-p (file)
  10055. "Test whether FILE specifies a location on a remote system.
  10056. Return non-nil if the location is indeed remote.
  10057. For example, the filename \"/user@host:/foo\" specifies a location
  10058. on the system \"/user@host:\"."
  10059. (cond ((fboundp 'file-remote-p)
  10060. (file-remote-p file))
  10061. ((fboundp 'tramp-handle-file-remote-p)
  10062. (tramp-handle-file-remote-p file))
  10063. ((and (boundp 'ange-ftp-name-format)
  10064. (string-match (car ange-ftp-name-format) file))
  10065. t)))
  10066. ;;;; Refiling
  10067. (defun org-get-org-file ()
  10068. "Read a filename, with default directory `org-directory'."
  10069. (let ((default (or org-default-notes-file remember-data-file)))
  10070. (read-file-name (format "File name [%s]: " default)
  10071. (file-name-as-directory org-directory)
  10072. default)))
  10073. (defun org-notes-order-reversed-p ()
  10074. "Check if the current file should receive notes in reversed order."
  10075. (cond
  10076. ((not org-reverse-note-order) nil)
  10077. ((eq t org-reverse-note-order) t)
  10078. ((not (listp org-reverse-note-order)) nil)
  10079. (t (catch 'exit
  10080. (let ((all org-reverse-note-order)
  10081. entry)
  10082. (while (setq entry (pop all))
  10083. (if (string-match (car entry) buffer-file-name)
  10084. (throw 'exit (cdr entry))))
  10085. nil)))))
  10086. (defvar org-refile-target-table nil
  10087. "The list of refile targets, created by `org-refile'.")
  10088. (defvar org-agenda-new-buffers nil
  10089. "Buffers created to visit agenda files.")
  10090. (defvar org-refile-cache nil
  10091. "Cache for refile targets.")
  10092. (defvar org-refile-markers nil
  10093. "All the markers used for caching refile locations.")
  10094. (defun org-refile-marker (pos)
  10095. "Get a new refile marker, but only if caching is in use."
  10096. (if (not org-refile-use-cache)
  10097. pos
  10098. (let ((m (make-marker)))
  10099. (move-marker m pos)
  10100. (push m org-refile-markers)
  10101. m)))
  10102. (defun org-refile-cache-clear ()
  10103. "Clear the refile cache and disable all the markers."
  10104. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10105. (setq org-refile-markers nil)
  10106. (setq org-refile-cache nil)
  10107. (message "Refile cache has been cleared"))
  10108. (defun org-refile-cache-check-set (set)
  10109. "Check if all the markers in the cache still have live buffers."
  10110. (let (marker)
  10111. (catch 'exit
  10112. (while (and set (setq marker (nth 3 (pop set))))
  10113. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10114. (when (and marker (null (marker-buffer marker)))
  10115. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10116. (sit-for 3)
  10117. (throw 'exit nil)))
  10118. t)))
  10119. (defun org-refile-cache-put (set &rest identifiers)
  10120. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10121. (let* ((key (sha1 (prin1-to-string identifiers)))
  10122. (entry (assoc key org-refile-cache)))
  10123. (if entry
  10124. (setcdr entry set)
  10125. (push (cons key set) org-refile-cache))))
  10126. (defun org-refile-cache-get (&rest identifiers)
  10127. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10128. (cond
  10129. ((not org-refile-cache) nil)
  10130. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10131. (t
  10132. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10133. org-refile-cache))))
  10134. (and set (org-refile-cache-check-set set) set)))))
  10135. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10136. "Produce a table with refile targets."
  10137. (let ((case-fold-search nil)
  10138. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10139. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10140. targets tgs txt re files f desc descre fast-path-p level pos0)
  10141. (message "Getting targets...")
  10142. (with-current-buffer (or default-buffer (current-buffer))
  10143. (while (setq entry (pop entries))
  10144. (setq files (car entry) desc (cdr entry))
  10145. (setq fast-path-p nil)
  10146. (cond
  10147. ((null files) (setq files (list (current-buffer))))
  10148. ((eq files 'org-agenda-files)
  10149. (setq files (org-agenda-files 'unrestricted)))
  10150. ((and (symbolp files) (fboundp files))
  10151. (setq files (funcall files)))
  10152. ((and (symbolp files) (boundp files))
  10153. (setq files (symbol-value files))))
  10154. (if (stringp files) (setq files (list files)))
  10155. (cond
  10156. ((eq (car desc) :tag)
  10157. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10158. ((eq (car desc) :todo)
  10159. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10160. ((eq (car desc) :regexp)
  10161. (setq descre (cdr desc)))
  10162. ((eq (car desc) :level)
  10163. (setq descre (concat "^\\*\\{" (number-to-string
  10164. (if org-odd-levels-only
  10165. (1- (* 2 (cdr desc)))
  10166. (cdr desc)))
  10167. "\\}[ \t]")))
  10168. ((eq (car desc) :maxlevel)
  10169. (setq fast-path-p t)
  10170. (setq descre (concat "^\\*\\{1," (number-to-string
  10171. (if org-odd-levels-only
  10172. (1- (* 2 (cdr desc)))
  10173. (cdr desc)))
  10174. "\\}[ \t]")))
  10175. (t (error "Bad refiling target description %s" desc)))
  10176. (while (setq f (pop files))
  10177. (with-current-buffer
  10178. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10179. (or
  10180. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10181. (progn
  10182. (if (bufferp f) (setq f (buffer-file-name
  10183. (buffer-base-buffer f))))
  10184. (setq f (and f (expand-file-name f)))
  10185. (if (eq org-refile-use-outline-path 'file)
  10186. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10187. (save-excursion
  10188. (save-restriction
  10189. (widen)
  10190. (goto-char (point-min))
  10191. (while (re-search-forward descre nil t)
  10192. (goto-char (setq pos0 (point-at-bol)))
  10193. (catch 'next
  10194. (when org-refile-target-verify-function
  10195. (save-match-data
  10196. (or (funcall org-refile-target-verify-function)
  10197. (throw 'next t))))
  10198. (when (and (looking-at org-complex-heading-regexp)
  10199. (not (member (match-string 4) excluded-entries))
  10200. (match-string 4))
  10201. (setq level (org-reduced-level
  10202. (- (match-end 1) (match-beginning 1)))
  10203. txt (org-link-display-format (match-string 4))
  10204. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10205. re (format org-complex-heading-regexp-format
  10206. (regexp-quote (match-string 4))))
  10207. (when org-refile-use-outline-path
  10208. (setq txt (mapconcat
  10209. 'org-protect-slash
  10210. (append
  10211. (if (eq org-refile-use-outline-path
  10212. 'file)
  10213. (list (file-name-nondirectory
  10214. (buffer-file-name
  10215. (buffer-base-buffer))))
  10216. (if (eq org-refile-use-outline-path
  10217. 'full-file-path)
  10218. (list (buffer-file-name
  10219. (buffer-base-buffer)))))
  10220. (org-get-outline-path fast-path-p
  10221. level txt)
  10222. (list txt))
  10223. "/")))
  10224. (push (list txt f re (org-refile-marker (point)))
  10225. tgs)))
  10226. (when (= (point) pos0)
  10227. ;; verification function has not moved point
  10228. (goto-char (point-at-eol))))))))
  10229. (when org-refile-use-cache
  10230. (org-refile-cache-put tgs (buffer-file-name) descre))
  10231. (setq targets (append tgs targets))
  10232. ))))
  10233. (message "Getting targets...done")
  10234. (nreverse targets)))
  10235. (defun org-protect-slash (s)
  10236. (while (string-match "/" s)
  10237. (setq s (replace-match "\\" t t s)))
  10238. s)
  10239. (defvar org-olpa (make-vector 20 nil))
  10240. (defun org-get-outline-path (&optional fastp level heading)
  10241. "Return the outline path to the current entry, as a list.
  10242. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10243. routine which makes outline path derivations for an entire file,
  10244. avoiding backtracing. Refile target collection makes use of that."
  10245. (if fastp
  10246. (progn
  10247. (if (> level 19)
  10248. (error "Outline path failure, more than 19 levels"))
  10249. (loop for i from level upto 19 do
  10250. (aset org-olpa i nil))
  10251. (prog1
  10252. (delq nil (append org-olpa nil))
  10253. (aset org-olpa level heading)))
  10254. (let (rtn case-fold-search)
  10255. (save-excursion
  10256. (save-restriction
  10257. (widen)
  10258. (while (org-up-heading-safe)
  10259. (when (looking-at org-complex-heading-regexp)
  10260. (push (org-match-string-no-properties 4) rtn)))
  10261. rtn)))))
  10262. (defun org-format-outline-path (path &optional width prefix separator)
  10263. "Format the outline path PATH for display.
  10264. WIDTH is the maximum number of characters that is available.
  10265. PREFIX is a prefix to be included in the returned string,
  10266. such as the file name.
  10267. SEPARATOR is inserted between the different parts of the path,
  10268. the default is \"/\"."
  10269. (setq width (or width 79))
  10270. (if prefix (setq width (- width (length prefix))))
  10271. (if (not path)
  10272. (or prefix "")
  10273. (let* ((nsteps (length path))
  10274. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10275. (maxwidth (if (<= total-width width)
  10276. 10000 ;; everything fits
  10277. ;; we need to shorten the level headings
  10278. (/ (- width nsteps) nsteps)))
  10279. (org-odd-levels-only nil)
  10280. (n 0)
  10281. (total (1+ (length prefix))))
  10282. (setq maxwidth (max maxwidth 10))
  10283. (concat prefix
  10284. (if prefix (or separator "/"))
  10285. (mapconcat
  10286. (lambda (h)
  10287. (setq n (1+ n))
  10288. (if (and (= n nsteps) (< maxwidth 10000))
  10289. (setq maxwidth (- total-width total)))
  10290. (if (< (length h) maxwidth)
  10291. (progn (setq total (+ total (length h) 1)) h)
  10292. (setq h (substring h 0 (- maxwidth 2))
  10293. total (+ total maxwidth 1))
  10294. (if (string-match "[ \t]+\\'" h)
  10295. (setq h (substring h 0 (match-beginning 0))))
  10296. (setq h (concat h "..")))
  10297. (org-add-props h nil 'face
  10298. (nth (% (1- n) org-n-level-faces)
  10299. org-level-faces))
  10300. h)
  10301. path (or separator "/"))))))
  10302. (defun org-display-outline-path (&optional file current separator just-return-string)
  10303. "Display the current outline path in the echo area.
  10304. If FILE is non-nil, prepend the output with the file name.
  10305. If CURRENT is non-nil, append the current heading to the output.
  10306. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10307. the different parts of the path and defaults to \"/\".
  10308. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10309. (interactive "P")
  10310. (let* (case-fold-search
  10311. message-log-max ; Don't populate the *Messages* buffer
  10312. (bfn (buffer-file-name (buffer-base-buffer)))
  10313. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10314. res)
  10315. (if current (setq path (append path
  10316. (save-excursion
  10317. (org-back-to-heading t)
  10318. (if (looking-at org-complex-heading-regexp)
  10319. (list (match-string 4)))))))
  10320. (setq res
  10321. (org-format-outline-path
  10322. path
  10323. (1- (frame-width))
  10324. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10325. separator))
  10326. (if just-return-string
  10327. (org-no-properties res)
  10328. (message "%s" res))))
  10329. (defvar org-refile-history nil
  10330. "History for refiling operations.")
  10331. (defvar org-after-refile-insert-hook nil
  10332. "Hook run after `org-refile' has inserted its stuff at the new location.
  10333. Note that this is still *before* the stuff will be removed from
  10334. the *old* location.")
  10335. (defvar org-capture-last-stored-marker)
  10336. (defvar org-refile-keep nil
  10337. "Non-nil means `org-refile' will copy instead of refile.")
  10338. (defun org-copy ()
  10339. "Like `org-refile', but copy."
  10340. (interactive)
  10341. (let ((org-refile-keep t))
  10342. (funcall 'org-refile nil nil nil "Copy")))
  10343. (defun org-refile (&optional goto default-buffer rfloc msg)
  10344. "Move the entry or entries at point to another heading.
  10345. The list of target headings is compiled using the information in
  10346. `org-refile-targets', which see.
  10347. At the target location, the entry is filed as a subitem of the target
  10348. heading. Depending on `org-reverse-note-order', the new subitem will
  10349. either be the first or the last subitem.
  10350. If there is an active region, all entries in that region will be moved.
  10351. However, the region must fulfill the requirement that the first heading
  10352. is the first one sets the top-level of the moved text - at most siblings
  10353. below it are allowed.
  10354. With prefix arg GOTO, the command will only visit the target location
  10355. and not actually move anything.
  10356. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10357. go to the location where the last refiling operation has put the subtree.
  10358. With a prefix argument of `2', refile to the running clock.
  10359. RFLOC can be a refile location obtained in a different way.
  10360. MSG is a string to replace \"Refile\" in the default prompt with
  10361. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10362. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10363. If you are using target caching (see `org-refile-use-cache'),
  10364. you have to clear the target cache in order to find new targets.
  10365. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10366. prefix argument (`C-u C-u C-u C-c C-w')."
  10367. (interactive "P")
  10368. (if (member goto '(0 (64)))
  10369. (org-refile-cache-clear)
  10370. (let* ((actionmsg (or msg "Refile"))
  10371. (cbuf (current-buffer))
  10372. (regionp (org-region-active-p))
  10373. (region-start (and regionp (region-beginning)))
  10374. (region-end (and regionp (region-end)))
  10375. (region-length (and regionp (- region-end region-start)))
  10376. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10377. pos it nbuf file re level reversed)
  10378. (setq last-command nil)
  10379. (when regionp
  10380. (goto-char region-start)
  10381. (or (bolp) (goto-char (point-at-bol)))
  10382. (setq region-start (point))
  10383. (unless (or (org-kill-is-subtree-p
  10384. (buffer-substring region-start region-end))
  10385. (prog1 org-refile-active-region-within-subtree
  10386. (org-toggle-heading)))
  10387. (error "The region is not a (sequence of) subtree(s)")))
  10388. (if (equal goto '(16))
  10389. (org-refile-goto-last-stored)
  10390. (when (or
  10391. (and (equal goto 2)
  10392. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10393. (prog1
  10394. (setq it (list (or org-clock-heading "running clock")
  10395. (buffer-file-name
  10396. (marker-buffer org-clock-hd-marker))
  10397. ""
  10398. (marker-position org-clock-hd-marker)))
  10399. (setq goto nil)))
  10400. (setq it (or rfloc
  10401. (let (heading-text)
  10402. (save-excursion
  10403. (unless goto
  10404. (org-back-to-heading t)
  10405. (setq heading-text
  10406. (nth 4 (org-heading-components))))
  10407. (org-refile-get-location
  10408. (cond (goto "Goto")
  10409. (regionp (concat actionmsg " region to"))
  10410. (t (concat actionmsg " subtree \""
  10411. heading-text "\" to")))
  10412. default-buffer
  10413. (and (not (equal '(4) goto))
  10414. org-refile-allow-creating-parent-nodes)
  10415. goto))))))
  10416. (setq file (nth 1 it)
  10417. re (nth 2 it)
  10418. pos (nth 3 it))
  10419. (if (and (not goto)
  10420. pos
  10421. (equal (buffer-file-name) file)
  10422. (if regionp
  10423. (and (>= pos region-start)
  10424. (<= pos region-end))
  10425. (and (>= pos (point))
  10426. (< pos (save-excursion
  10427. (org-end-of-subtree t t))))))
  10428. (error "Cannot refile to position inside the tree or region"))
  10429. (setq nbuf (or (find-buffer-visiting file)
  10430. (find-file-noselect file)))
  10431. (if goto
  10432. (progn
  10433. (org-pop-to-buffer-same-window nbuf)
  10434. (goto-char pos)
  10435. (org-show-context 'org-goto))
  10436. (if regionp
  10437. (progn
  10438. (org-kill-new (buffer-substring region-start region-end))
  10439. (org-save-markers-in-region region-start region-end))
  10440. (org-copy-subtree 1 nil t))
  10441. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10442. (find-file-noselect file)))
  10443. (setq reversed (org-notes-order-reversed-p))
  10444. (save-excursion
  10445. (save-restriction
  10446. (widen)
  10447. (if pos
  10448. (progn
  10449. (goto-char pos)
  10450. (looking-at org-outline-regexp)
  10451. (setq level (org-get-valid-level (funcall outline-level) 1))
  10452. (goto-char
  10453. (if reversed
  10454. (or (outline-next-heading) (point-max))
  10455. (or (save-excursion (org-get-next-sibling))
  10456. (org-end-of-subtree t t)
  10457. (point-max)))))
  10458. (setq level 1)
  10459. (if (not reversed)
  10460. (goto-char (point-max))
  10461. (goto-char (point-min))
  10462. (or (outline-next-heading) (goto-char (point-max)))))
  10463. (if (not (bolp)) (newline))
  10464. (org-paste-subtree level)
  10465. (when org-log-refile
  10466. (org-add-log-setup 'refile nil nil 'findpos
  10467. org-log-refile)
  10468. (unless (eq org-log-refile 'note)
  10469. (save-excursion (org-add-log-note))))
  10470. (and org-auto-align-tags
  10471. (let ((org-loop-over-headlines-in-active-region nil))
  10472. (org-set-tags nil t)))
  10473. (with-demoted-errors
  10474. (bookmark-set "org-refile-last-stored"))
  10475. ;; If we are refiling for capture, make sure that the
  10476. ;; last-capture pointers point here
  10477. (when (org-bound-and-true-p org-refile-for-capture)
  10478. (with-demoted-errors
  10479. (bookmark-set "org-capture-last-stored-marker"))
  10480. (move-marker org-capture-last-stored-marker (point)))
  10481. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10482. (run-hooks 'org-after-refile-insert-hook))))
  10483. (unless org-refile-keep
  10484. (if regionp
  10485. (delete-region (point) (+ (point) region-length))
  10486. (org-cut-subtree)))
  10487. (when (featurep 'org-inlinetask)
  10488. (org-inlinetask-remove-END-maybe))
  10489. (setq org-markers-to-move nil)
  10490. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10491. (defun org-refile-goto-last-stored ()
  10492. "Go to the location where the last refile was stored."
  10493. (interactive)
  10494. (bookmark-jump "org-refile-last-stored")
  10495. (message "This is the location of the last refile"))
  10496. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10497. no-exclude)
  10498. "Prompt the user for a refile location, using PROMPT.
  10499. PROMPT should not be suffixed with a colon and a space, because
  10500. this function appends the default value from
  10501. `org-refile-history' automatically, if that is not empty.
  10502. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10503. this is used for the GOTO interface."
  10504. (let ((org-refile-targets org-refile-targets)
  10505. (org-refile-use-outline-path org-refile-use-outline-path)
  10506. excluded-entries)
  10507. (when (and (derived-mode-p 'org-mode)
  10508. (not org-refile-use-cache)
  10509. (not no-exclude))
  10510. (org-map-tree
  10511. (lambda()
  10512. (setq excluded-entries
  10513. (append excluded-entries (list (org-get-heading t t)))))))
  10514. (setq org-refile-target-table
  10515. (org-refile-get-targets default-buffer excluded-entries)))
  10516. (unless org-refile-target-table
  10517. (error "No refile targets"))
  10518. (let* ((cbuf (current-buffer))
  10519. (partial-completion-mode nil)
  10520. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10521. (cfunc (if (and org-refile-use-outline-path
  10522. org-outline-path-complete-in-steps)
  10523. 'org-olpath-completing-read
  10524. 'org-icompleting-read))
  10525. (extra (if org-refile-use-outline-path "/" ""))
  10526. (cbnex (concat (buffer-name) extra))
  10527. (filename (and cfn (expand-file-name cfn)))
  10528. (tbl (mapcar
  10529. (lambda (x)
  10530. (if (and (not (member org-refile-use-outline-path
  10531. '(file full-file-path)))
  10532. (not (equal filename (nth 1 x))))
  10533. (cons (concat (car x) extra " ("
  10534. (file-name-nondirectory (nth 1 x)) ")")
  10535. (cdr x))
  10536. (cons (concat (car x) extra) (cdr x))))
  10537. org-refile-target-table))
  10538. (completion-ignore-case t)
  10539. cdef
  10540. (prompt (concat prompt
  10541. (or (and (car org-refile-history)
  10542. (concat " (default " (car org-refile-history) ")"))
  10543. (and (assoc cbnex tbl) (setq cdef cbnex)
  10544. (concat " (default " cbnex ")"))) ": "))
  10545. pa answ parent-target child parent old-hist)
  10546. (setq old-hist org-refile-history)
  10547. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10548. nil 'org-refile-history (or cdef (car org-refile-history))))
  10549. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10550. (org-refile-check-position pa)
  10551. (if pa
  10552. (progn
  10553. (when (or (not org-refile-history)
  10554. (not (eq old-hist org-refile-history))
  10555. (not (equal (car pa) (car org-refile-history))))
  10556. (setq org-refile-history
  10557. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10558. org-refile-history
  10559. (cdr org-refile-history))))
  10560. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10561. (pop org-refile-history)))
  10562. pa)
  10563. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10564. (progn
  10565. (setq parent (match-string 1 answ)
  10566. child (match-string 2 answ))
  10567. (setq parent-target (or (assoc parent tbl)
  10568. (assoc (concat parent "/") tbl)))
  10569. (when (and parent-target
  10570. (or (eq new-nodes t)
  10571. (and (eq new-nodes 'confirm)
  10572. (y-or-n-p (format "Create new node \"%s\"? "
  10573. child)))))
  10574. (org-refile-new-child parent-target child)))
  10575. (error "Invalid target location")))))
  10576. (declare-function org-string-nw-p "org-macs" (s))
  10577. (defun org-refile-check-position (refile-pointer)
  10578. "Check if the refile pointer matches the headline to which it points."
  10579. (let* ((file (nth 1 refile-pointer))
  10580. (re (nth 2 refile-pointer))
  10581. (pos (nth 3 refile-pointer))
  10582. buffer)
  10583. (if (and (not (markerp pos)) (not file))
  10584. (error "Please save the buffer to a file before refiling")
  10585. (when (org-string-nw-p re)
  10586. (setq buffer (if (markerp pos)
  10587. (marker-buffer pos)
  10588. (or (find-buffer-visiting file)
  10589. (find-file-noselect file))))
  10590. (with-current-buffer buffer
  10591. (save-excursion
  10592. (save-restriction
  10593. (widen)
  10594. (goto-char pos)
  10595. (beginning-of-line 1)
  10596. (unless (org-looking-at-p re)
  10597. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10598. (defun org-refile-new-child (parent-target child)
  10599. "Use refile target PARENT-TARGET to add new CHILD below it."
  10600. (unless parent-target
  10601. (error "Cannot find parent for new node"))
  10602. (let ((file (nth 1 parent-target))
  10603. (pos (nth 3 parent-target))
  10604. level)
  10605. (with-current-buffer (or (find-buffer-visiting file)
  10606. (find-file-noselect file))
  10607. (save-excursion
  10608. (save-restriction
  10609. (widen)
  10610. (if pos
  10611. (goto-char pos)
  10612. (goto-char (point-max))
  10613. (if (not (bolp)) (newline)))
  10614. (when (looking-at org-outline-regexp)
  10615. (setq level (funcall outline-level))
  10616. (org-end-of-subtree t t))
  10617. (org-back-over-empty-lines)
  10618. (insert "\n" (make-string
  10619. (if pos (org-get-valid-level level 1) 1) ?*)
  10620. " " child "\n")
  10621. (beginning-of-line 0)
  10622. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10623. (defun org-olpath-completing-read (prompt collection &rest args)
  10624. "Read an outline path like a file name."
  10625. (let ((thetable collection)
  10626. (org-completion-use-ido nil) ; does not work with ido.
  10627. (org-completion-use-iswitchb nil)) ; or iswitchb
  10628. (apply
  10629. 'org-icompleting-read prompt
  10630. (lambda (string predicate &optional flag)
  10631. (let (rtn r f (l (length string)))
  10632. (cond
  10633. ((eq flag nil)
  10634. ;; try completion
  10635. (try-completion string thetable))
  10636. ((eq flag t)
  10637. ;; all-completions
  10638. (setq rtn (all-completions string thetable predicate))
  10639. (mapcar
  10640. (lambda (x)
  10641. (setq r (substring x l))
  10642. (if (string-match " ([^)]*)$" x)
  10643. (setq f (match-string 0 x))
  10644. (setq f ""))
  10645. (if (string-match "/" r)
  10646. (concat string (substring r 0 (match-end 0)) f)
  10647. x))
  10648. rtn))
  10649. ((eq flag 'lambda)
  10650. ;; exact match?
  10651. (assoc string thetable)))))
  10652. args)))
  10653. ;;;; Dynamic blocks
  10654. (defun org-find-dblock (name)
  10655. "Find the first dynamic block with name NAME in the buffer.
  10656. If not found, stay at current position and return nil."
  10657. (let ((case-fold-search t) pos)
  10658. (save-excursion
  10659. (goto-char (point-min))
  10660. (setq pos (and (re-search-forward
  10661. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10662. (match-beginning 0))))
  10663. (if pos (goto-char pos))
  10664. pos))
  10665. (defconst org-dblock-start-re
  10666. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10667. "Matches the start line of a dynamic block, with parameters.")
  10668. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10669. "Matches the end of a dynamic block.")
  10670. (defun org-create-dblock (plist)
  10671. "Create a dynamic block section, with parameters taken from PLIST.
  10672. PLIST must contain a :name entry which is used as name of the block."
  10673. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10674. (end-of-line 1)
  10675. (newline))
  10676. (let ((col (current-column))
  10677. (name (plist-get plist :name)))
  10678. (insert "#+BEGIN: " name)
  10679. (while plist
  10680. (if (eq (car plist) :name)
  10681. (setq plist (cddr plist))
  10682. (insert " " (prin1-to-string (pop plist)))))
  10683. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10684. (beginning-of-line -2)))
  10685. (defun org-prepare-dblock ()
  10686. "Prepare dynamic block for refresh.
  10687. This empties the block, puts the cursor at the insert position and returns
  10688. the property list including an extra property :name with the block name."
  10689. (unless (looking-at org-dblock-start-re)
  10690. (error "Not at a dynamic block"))
  10691. (let* ((begdel (1+ (match-end 0)))
  10692. (name (org-no-properties (match-string 1)))
  10693. (params (append (list :name name)
  10694. (read (concat "(" (match-string 3) ")")))))
  10695. (save-excursion
  10696. (beginning-of-line 1)
  10697. (skip-chars-forward " \t")
  10698. (setq params (plist-put params :indentation-column (current-column))))
  10699. (unless (re-search-forward org-dblock-end-re nil t)
  10700. (error "Dynamic block not terminated"))
  10701. (setq params
  10702. (append params
  10703. (list :content (buffer-substring
  10704. begdel (match-beginning 0)))))
  10705. (delete-region begdel (match-beginning 0))
  10706. (goto-char begdel)
  10707. (open-line 1)
  10708. params))
  10709. (defun org-map-dblocks (&optional command)
  10710. "Apply COMMAND to all dynamic blocks in the current buffer.
  10711. If COMMAND is not given, use `org-update-dblock'."
  10712. (let ((cmd (or command 'org-update-dblock)))
  10713. (save-excursion
  10714. (goto-char (point-min))
  10715. (while (re-search-forward org-dblock-start-re nil t)
  10716. (goto-char (match-beginning 0))
  10717. (save-excursion
  10718. (condition-case nil
  10719. (funcall cmd)
  10720. (error (message "Error during update of dynamic block"))))
  10721. (unless (re-search-forward org-dblock-end-re nil t)
  10722. (error "Dynamic block not terminated"))))))
  10723. (defun org-dblock-update (&optional arg)
  10724. "User command for updating dynamic blocks.
  10725. Update the dynamic block at point. With prefix ARG, update all dynamic
  10726. blocks in the buffer."
  10727. (interactive "P")
  10728. (if arg
  10729. (org-update-all-dblocks)
  10730. (or (looking-at org-dblock-start-re)
  10731. (org-beginning-of-dblock))
  10732. (org-update-dblock)))
  10733. (defun org-update-dblock ()
  10734. "Update the dynamic block at point.
  10735. This means to empty the block, parse for parameters and then call
  10736. the correct writing function."
  10737. (interactive)
  10738. (save-window-excursion
  10739. (let* ((pos (point))
  10740. (line (org-current-line))
  10741. (params (org-prepare-dblock))
  10742. (name (plist-get params :name))
  10743. (indent (plist-get params :indentation-column))
  10744. (cmd (intern (concat "org-dblock-write:" name))))
  10745. (message "Updating dynamic block `%s' at line %d..." name line)
  10746. (funcall cmd params)
  10747. (message "Updating dynamic block `%s' at line %d...done" name line)
  10748. (goto-char pos)
  10749. (when (and indent (> indent 0))
  10750. (setq indent (make-string indent ?\ ))
  10751. (save-excursion
  10752. (org-beginning-of-dblock)
  10753. (forward-line 1)
  10754. (while (not (looking-at org-dblock-end-re))
  10755. (insert indent)
  10756. (beginning-of-line 2))
  10757. (when (looking-at org-dblock-end-re)
  10758. (and (looking-at "[ \t]+")
  10759. (replace-match ""))
  10760. (insert indent)))))))
  10761. (defun org-beginning-of-dblock ()
  10762. "Find the beginning of the dynamic block at point.
  10763. Error if there is no such block at point."
  10764. (let ((pos (point))
  10765. beg)
  10766. (end-of-line 1)
  10767. (if (and (re-search-backward org-dblock-start-re nil t)
  10768. (setq beg (match-beginning 0))
  10769. (re-search-forward org-dblock-end-re nil t)
  10770. (> (match-end 0) pos))
  10771. (goto-char beg)
  10772. (goto-char pos)
  10773. (error "Not in a dynamic block"))))
  10774. (defun org-update-all-dblocks ()
  10775. "Update all dynamic blocks in the buffer.
  10776. This function can be used in a hook."
  10777. (interactive)
  10778. (when (derived-mode-p 'org-mode)
  10779. (org-map-dblocks 'org-update-dblock)))
  10780. ;;;; Completion
  10781. (defun org-get-export-keywords ()
  10782. "Return a list of all currently understood export keywords.
  10783. Export keywords include options, block names, attributes and
  10784. keywords relative to each registered export back-end."
  10785. (delq nil
  10786. (let (keywords)
  10787. (mapc
  10788. (lambda (back-end)
  10789. (let ((props (cdr back-end)))
  10790. ;; Back-end name (for keywords, like #+LATEX:)
  10791. (push (upcase (symbol-name (car back-end))) keywords)
  10792. ;; Back-end options.
  10793. (mapc (lambda (option) (push (cadr option) keywords))
  10794. (plist-get (cdr back-end) :options-alist))))
  10795. (org-bound-and-true-p org-export-registered-backends))
  10796. keywords)))
  10797. (defconst org-options-keywords
  10798. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10799. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10800. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10801. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10802. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10803. (defcustom org-structure-template-alist
  10804. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10805. "<src lang=\"?\">\n\n</src>")
  10806. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10807. "<example>\n?\n</example>")
  10808. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10809. "<quote>\n?\n</quote>")
  10810. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10811. "<verse>\n?\n</verse>")
  10812. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10813. "<verbatim>\n?\n</verbatim>")
  10814. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10815. "<center>\n?\n</center>")
  10816. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10817. "<literal style=\"latex\">\n?\n</literal>")
  10818. ("L" "#+LaTeX: "
  10819. "<literal style=\"latex\">?</literal>")
  10820. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10821. "<literal style=\"html\">\n?\n</literal>")
  10822. ("H" "#+HTML: "
  10823. "<literal style=\"html\">?</literal>")
  10824. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10825. ("A" "#+ASCII: ")
  10826. ("i" "#+INDEX: ?"
  10827. "#+INDEX: ?")
  10828. ("I" "#+INCLUDE: %file ?"
  10829. "<include file=%file markup=\"?\">"))
  10830. "Structure completion elements.
  10831. This is a list of abbreviation keys and values. The value gets inserted
  10832. if you type `<' followed by the key and then press the completion key,
  10833. usually `M-TAB'. %file will be replaced by a file name after prompting
  10834. for the file using completion. The cursor will be placed at the position
  10835. of the `?` in the template.
  10836. There are two templates for each key, the first uses the original Org syntax,
  10837. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10838. the default when the /org-mtags.el/ module has been loaded. See also the
  10839. variable `org-mtags-prefer-muse-templates'."
  10840. :group 'org-completion
  10841. :type '(repeat
  10842. (string :tag "Key")
  10843. (string :tag "Template")
  10844. (string :tag "Muse Template")))
  10845. (defun org-try-structure-completion ()
  10846. "Try to complete a structure template before point.
  10847. This looks for strings like \"<e\" on an otherwise empty line and
  10848. expands them."
  10849. (let ((l (buffer-substring (point-at-bol) (point)))
  10850. a)
  10851. (when (and (looking-at "[ \t]*$")
  10852. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10853. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10854. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10855. (match-beginning 1)) a)
  10856. t)))
  10857. (defun org-complete-expand-structure-template (start cell)
  10858. "Expand a structure template."
  10859. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10860. (rpl (nth (if musep 2 1) cell))
  10861. (ind ""))
  10862. (delete-region start (point))
  10863. (when (string-match "\\`#\\+" rpl)
  10864. (cond
  10865. ((bolp))
  10866. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10867. (setq ind (buffer-substring (point-at-bol) (point))))
  10868. (t (newline))))
  10869. (setq start (point))
  10870. (if (string-match "%file" rpl)
  10871. (setq rpl (replace-match
  10872. (concat
  10873. "\""
  10874. (save-match-data
  10875. (abbreviate-file-name (read-file-name "Include file: ")))
  10876. "\"")
  10877. t t rpl)))
  10878. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10879. (concat "\n" ind)))
  10880. (insert rpl)
  10881. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10882. ;;;; TODO, DEADLINE, Comments
  10883. (defun org-toggle-comment ()
  10884. "Change the COMMENT state of an entry."
  10885. (interactive)
  10886. (save-excursion
  10887. (org-back-to-heading)
  10888. (let (case-fold-search)
  10889. (cond
  10890. ((looking-at (format org-heading-keyword-regexp-format
  10891. org-comment-string))
  10892. (goto-char (match-end 1))
  10893. (looking-at (concat " +" org-comment-string))
  10894. (replace-match "" t t)
  10895. (when (eolp) (insert " ")))
  10896. ((looking-at org-outline-regexp)
  10897. (goto-char (match-end 0))
  10898. (insert org-comment-string " "))))))
  10899. (defvar org-last-todo-state-is-todo nil
  10900. "This is non-nil when the last TODO state change led to a TODO state.
  10901. If the last change removed the TODO tag or switched to DONE, then
  10902. this is nil.")
  10903. (defvar org-setting-tags nil) ; dynamically skipped
  10904. (defvar org-todo-setup-filter-hook nil
  10905. "Hook for functions that pre-filter todo specs.
  10906. Each function takes a todo spec and returns either nil or the spec
  10907. transformed into canonical form." )
  10908. (defvar org-todo-get-default-hook nil
  10909. "Hook for functions that get a default item for todo.
  10910. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10911. nil or a string to be used for the todo mark." )
  10912. (defvar org-agenda-headline-snapshot-before-repeat)
  10913. (defun org-current-effective-time ()
  10914. "Return current time adjusted for `org-extend-today-until' variable."
  10915. (let* ((ct (org-current-time))
  10916. (dct (decode-time ct))
  10917. (ct1
  10918. (cond
  10919. (org-use-last-clock-out-time-as-effective-time
  10920. (or (org-clock-get-last-clock-out-time) ct))
  10921. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10922. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10923. (t ct))))
  10924. ct1))
  10925. (defun org-todo-yesterday (&optional arg)
  10926. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10927. (interactive "P")
  10928. (if (eq major-mode 'org-agenda-mode)
  10929. (apply 'org-agenda-todo-yesterday arg)
  10930. (let* ((hour (third (decode-time
  10931. (org-current-time))))
  10932. (org-extend-today-until (1+ hour)))
  10933. (org-todo arg))))
  10934. (defvar org-block-entry-blocking ""
  10935. "First entry preventing the TODO state change.")
  10936. (defun org-todo (&optional arg)
  10937. "Change the TODO state of an item.
  10938. The state of an item is given by a keyword at the start of the heading,
  10939. like
  10940. *** TODO Write paper
  10941. *** DONE Call mom
  10942. The different keywords are specified in the variable `org-todo-keywords'.
  10943. By default the available states are \"TODO\" and \"DONE\".
  10944. So for this example: when the item starts with TODO, it is changed to DONE.
  10945. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10946. DONE are present, add TODO at the beginning of the heading.
  10947. With \\[universal-argument] prefix arg, use completion to determine the new \
  10948. state.
  10949. With numeric prefix arg, switch to that state.
  10950. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10951. keywords (nextset).
  10952. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10953. With a numeric prefix arg of 0, inhibit note taking for the change.
  10954. For calling through lisp, arg is also interpreted in the following way:
  10955. 'none -> empty state
  10956. \"\"(empty string) -> switch to empty state
  10957. 'done -> switch to DONE
  10958. 'nextset -> switch to the next set of keywords
  10959. 'previousset -> switch to the previous set of keywords
  10960. \"WAITING\" -> switch to the specified keyword, but only if it
  10961. really is a member of `org-todo-keywords'."
  10962. (interactive "P")
  10963. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10964. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10965. 'region-start-level 'region))
  10966. org-loop-over-headlines-in-active-region)
  10967. (org-map-entries
  10968. `(org-todo ,arg)
  10969. org-loop-over-headlines-in-active-region
  10970. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10971. (if (equal arg '(16)) (setq arg 'nextset))
  10972. (let ((org-blocker-hook org-blocker-hook)
  10973. commentp
  10974. case-fold-search)
  10975. (when (equal arg '(64))
  10976. (setq arg nil org-blocker-hook nil))
  10977. (when (and org-blocker-hook
  10978. (or org-inhibit-blocking
  10979. (org-entry-get nil "NOBLOCKING")))
  10980. (setq org-blocker-hook nil))
  10981. (save-excursion
  10982. (catch 'exit
  10983. (org-back-to-heading t)
  10984. (when (looking-at (concat "^\\*+ " org-comment-string))
  10985. (org-toggle-comment)
  10986. (setq commentp t))
  10987. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10988. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10989. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10990. (let* ((match-data (match-data))
  10991. (startpos (point-at-bol))
  10992. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10993. (org-log-done org-log-done)
  10994. (org-log-repeat org-log-repeat)
  10995. (org-todo-log-states org-todo-log-states)
  10996. (org-inhibit-logging
  10997. (if (equal arg 0)
  10998. (progn (setq arg nil) 'note) org-inhibit-logging))
  10999. (this (match-string 1))
  11000. (hl-pos (match-beginning 0))
  11001. (head (org-get-todo-sequence-head this))
  11002. (ass (assoc head org-todo-kwd-alist))
  11003. (interpret (nth 1 ass))
  11004. (done-word (nth 3 ass))
  11005. (final-done-word (nth 4 ass))
  11006. (org-last-state (or this ""))
  11007. (completion-ignore-case t)
  11008. (member (member this org-todo-keywords-1))
  11009. (tail (cdr member))
  11010. (org-state (cond
  11011. ((and org-todo-key-trigger
  11012. (or (and (equal arg '(4))
  11013. (eq org-use-fast-todo-selection 'prefix))
  11014. (and (not arg) org-use-fast-todo-selection
  11015. (not (eq org-use-fast-todo-selection
  11016. 'prefix)))))
  11017. ;; Use fast selection
  11018. (org-fast-todo-selection))
  11019. ((and (equal arg '(4))
  11020. (or (not org-use-fast-todo-selection)
  11021. (not org-todo-key-trigger)))
  11022. ;; Read a state with completion
  11023. (org-icompleting-read
  11024. "State: " (mapcar 'list org-todo-keywords-1)
  11025. nil t))
  11026. ((eq arg 'right)
  11027. (if this
  11028. (if tail (car tail) nil)
  11029. (car org-todo-keywords-1)))
  11030. ((eq arg 'left)
  11031. (if (equal member org-todo-keywords-1)
  11032. nil
  11033. (if this
  11034. (nth (- (length org-todo-keywords-1)
  11035. (length tail) 2)
  11036. org-todo-keywords-1)
  11037. (org-last org-todo-keywords-1))))
  11038. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11039. (setq arg nil))) ; hack to fall back to cycling
  11040. (arg
  11041. ;; user or caller requests a specific state
  11042. (cond
  11043. ((equal arg "") nil)
  11044. ((eq arg 'none) nil)
  11045. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11046. ((eq arg 'nextset)
  11047. (or (car (cdr (member head org-todo-heads)))
  11048. (car org-todo-heads)))
  11049. ((eq arg 'previousset)
  11050. (let ((org-todo-heads (reverse org-todo-heads)))
  11051. (or (car (cdr (member head org-todo-heads)))
  11052. (car org-todo-heads))))
  11053. ((car (member arg org-todo-keywords-1)))
  11054. ((stringp arg)
  11055. (error "State `%s' not valid in this file" arg))
  11056. ((nth (1- (prefix-numeric-value arg))
  11057. org-todo-keywords-1))))
  11058. ((null member) (or head (car org-todo-keywords-1)))
  11059. ((equal this final-done-word) nil) ;; -> make empty
  11060. ((null tail) nil) ;; -> first entry
  11061. ((memq interpret '(type priority))
  11062. (if (eq this-command last-command)
  11063. (car tail)
  11064. (if (> (length tail) 0)
  11065. (or done-word (car org-done-keywords))
  11066. nil)))
  11067. (t
  11068. (car tail))))
  11069. (org-state (or
  11070. (run-hook-with-args-until-success
  11071. 'org-todo-get-default-hook org-state org-last-state)
  11072. org-state))
  11073. (next (if org-state (concat " " org-state " ") " "))
  11074. (change-plist (list :type 'todo-state-change :from this :to org-state
  11075. :position startpos))
  11076. dolog now-done-p)
  11077. (when org-blocker-hook
  11078. (setq org-last-todo-state-is-todo
  11079. (not (member this org-done-keywords)))
  11080. (unless (save-excursion
  11081. (save-match-data
  11082. (org-with-wide-buffer
  11083. (run-hook-with-args-until-failure
  11084. 'org-blocker-hook change-plist))))
  11085. (if (org-called-interactively-p 'interactive)
  11086. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11087. this org-state org-block-entry-blocking)
  11088. ;; fail silently
  11089. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11090. this org-state org-block-entry-blocking)
  11091. (throw 'exit nil))))
  11092. (store-match-data match-data)
  11093. (replace-match next t t)
  11094. (unless (pos-visible-in-window-p hl-pos)
  11095. (message "TODO state changed to %s" (org-trim next)))
  11096. (unless head
  11097. (setq head (org-get-todo-sequence-head org-state)
  11098. ass (assoc head org-todo-kwd-alist)
  11099. interpret (nth 1 ass)
  11100. done-word (nth 3 ass)
  11101. final-done-word (nth 4 ass)))
  11102. (when (memq arg '(nextset previousset))
  11103. (message "Keyword-Set %d/%d: %s"
  11104. (- (length org-todo-sets) -1
  11105. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11106. (length org-todo-sets)
  11107. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11108. (setq org-last-todo-state-is-todo
  11109. (not (member org-state org-done-keywords)))
  11110. (setq now-done-p (and (member org-state org-done-keywords)
  11111. (not (member this org-done-keywords))))
  11112. (and logging (org-local-logging logging))
  11113. (when (and (or org-todo-log-states org-log-done)
  11114. (not (eq org-inhibit-logging t))
  11115. (not (memq arg '(nextset previousset))))
  11116. ;; we need to look at recording a time and note
  11117. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11118. (nth 2 (assoc this org-todo-log-states))))
  11119. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11120. (setq dolog 'time))
  11121. (when (and org-state
  11122. (member org-state org-not-done-keywords)
  11123. (not (member this org-not-done-keywords)))
  11124. ;; This is now a todo state and was not one before
  11125. ;; If there was a CLOSED time stamp, get rid of it.
  11126. (org-add-planning-info nil nil 'closed))
  11127. (when (and now-done-p org-log-done)
  11128. ;; It is now done, and it was not done before
  11129. (org-add-planning-info 'closed (org-current-effective-time))
  11130. (if (and (not dolog) (eq 'note org-log-done))
  11131. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11132. (when (and org-state dolog)
  11133. ;; This is a non-nil state, and we need to log it
  11134. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11135. ;; Fixup tag positioning
  11136. (org-todo-trigger-tag-changes org-state)
  11137. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11138. (when org-provide-todo-statistics
  11139. (org-update-parent-todo-statistics))
  11140. (run-hooks 'org-after-todo-state-change-hook)
  11141. (if (and arg (not (member org-state org-done-keywords)))
  11142. (setq head (org-get-todo-sequence-head org-state)))
  11143. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11144. ;; Do we need to trigger a repeat?
  11145. (when now-done-p
  11146. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11147. ;; This is for the agenda, take a snapshot of the headline.
  11148. (save-match-data
  11149. (setq org-agenda-headline-snapshot-before-repeat
  11150. (org-get-heading))))
  11151. (org-auto-repeat-maybe org-state))
  11152. ;; Fixup cursor location if close to the keyword
  11153. (if (and (outline-on-heading-p)
  11154. (not (bolp))
  11155. (save-excursion (beginning-of-line 1)
  11156. (looking-at org-todo-line-regexp))
  11157. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11158. (progn
  11159. (goto-char (or (match-end 2) (match-end 1)))
  11160. (and (looking-at " ") (just-one-space))))
  11161. (when org-trigger-hook
  11162. (save-excursion
  11163. (run-hook-with-args 'org-trigger-hook change-plist)))
  11164. (when commentp (org-toggle-comment))))))))
  11165. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11166. "Block turning an entry into a TODO, using the hierarchy.
  11167. This checks whether the current task should be blocked from state
  11168. changes. Such blocking occurs when:
  11169. 1. The task has children which are not all in a completed state.
  11170. 2. A task has a parent with the property :ORDERED:, and there
  11171. are siblings prior to the current task with incomplete
  11172. status.
  11173. 3. The parent of the task is blocked because it has siblings that should
  11174. be done first, or is child of a block grandparent TODO entry."
  11175. (if (not org-enforce-todo-dependencies)
  11176. t ; if locally turned off don't block
  11177. (catch 'dont-block
  11178. ;; If this is not a todo state change, or if this entry is already DONE,
  11179. ;; do not block
  11180. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11181. (member (plist-get change-plist :from)
  11182. (cons 'done org-done-keywords))
  11183. (member (plist-get change-plist :to)
  11184. (cons 'todo org-not-done-keywords))
  11185. (not (plist-get change-plist :to)))
  11186. (throw 'dont-block t))
  11187. ;; If this task has children, and any are undone, it's blocked
  11188. (save-excursion
  11189. (org-back-to-heading t)
  11190. (let ((this-level (funcall outline-level)))
  11191. (outline-next-heading)
  11192. (let ((child-level (funcall outline-level)))
  11193. (while (and (not (eobp))
  11194. (> child-level this-level))
  11195. ;; this todo has children, check whether they are all
  11196. ;; completed
  11197. (if (and (not (org-entry-is-done-p))
  11198. (org-entry-is-todo-p))
  11199. (progn (setq org-block-entry-blocking (org-get-heading))
  11200. (throw 'dont-block nil)))
  11201. (outline-next-heading)
  11202. (setq child-level (funcall outline-level))))))
  11203. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11204. ;; any previous siblings are undone, it's blocked
  11205. (save-excursion
  11206. (org-back-to-heading t)
  11207. (let* ((pos (point))
  11208. (parent-pos (and (org-up-heading-safe) (point))))
  11209. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11210. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11211. (forward-line 1)
  11212. (re-search-forward org-not-done-heading-regexp pos t))
  11213. (setq org-block-entry-blocking (match-string 0))
  11214. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11215. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11216. (while t
  11217. (goto-char parent-pos)
  11218. (if (not (looking-at org-not-done-heading-regexp))
  11219. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11220. (setq pos (point))
  11221. (setq parent-pos (and (org-up-heading-safe) (point)))
  11222. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11223. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11224. (forward-line 1)
  11225. (re-search-forward org-not-done-heading-regexp pos t)
  11226. (setq org-block-entry-blocking (org-get-heading)))
  11227. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11228. (defcustom org-track-ordered-property-with-tag nil
  11229. "Should the ORDERED property also be shown as a tag?
  11230. The ORDERED property decides if an entry should require subtasks to be
  11231. completed in sequence. Since a property is not very visible, setting
  11232. this option means that toggling the ORDERED property with the command
  11233. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11234. not relevant for the behavior, but it makes things more visible.
  11235. Note that toggling the tag with tags commands will not change the property
  11236. and therefore not influence behavior!
  11237. This can be t, meaning the tag ORDERED should be used, It can also be a
  11238. string to select a different tag for this task."
  11239. :group 'org-todo
  11240. :type '(choice
  11241. (const :tag "No tracking" nil)
  11242. (const :tag "Track with ORDERED tag" t)
  11243. (string :tag "Use other tag")))
  11244. (defun org-toggle-ordered-property ()
  11245. "Toggle the ORDERED property of the current entry.
  11246. For better visibility, you can track the value of this property with a tag.
  11247. See variable `org-track-ordered-property-with-tag'."
  11248. (interactive)
  11249. (let* ((t1 org-track-ordered-property-with-tag)
  11250. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11251. (save-excursion
  11252. (org-back-to-heading)
  11253. (if (org-entry-get nil "ORDERED")
  11254. (progn
  11255. (org-delete-property "ORDERED" "PROPERTIES")
  11256. (and tag (org-toggle-tag tag 'off))
  11257. (message "Subtasks can be completed in arbitrary order"))
  11258. (org-entry-put nil "ORDERED" "t")
  11259. (and tag (org-toggle-tag tag 'on))
  11260. (message "Subtasks must be completed in sequence")))))
  11261. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11262. (defun org-block-todo-from-checkboxes (change-plist)
  11263. "Block turning an entry into a TODO, using checkboxes.
  11264. This checks whether the current task should be blocked from state
  11265. changes because there are unchecked boxes in this entry."
  11266. (if (not org-enforce-todo-checkbox-dependencies)
  11267. t ; if locally turned off don't block
  11268. (catch 'dont-block
  11269. ;; If this is not a todo state change, or if this entry is already DONE,
  11270. ;; do not block
  11271. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11272. (member (plist-get change-plist :from)
  11273. (cons 'done org-done-keywords))
  11274. (member (plist-get change-plist :to)
  11275. (cons 'todo org-not-done-keywords))
  11276. (not (plist-get change-plist :to)))
  11277. (throw 'dont-block t))
  11278. ;; If this task has checkboxes that are not checked, it's blocked
  11279. (save-excursion
  11280. (org-back-to-heading t)
  11281. (let ((beg (point)) end)
  11282. (outline-next-heading)
  11283. (setq end (point))
  11284. (goto-char beg)
  11285. (if (org-list-search-forward
  11286. (concat (org-item-beginning-re)
  11287. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11288. "\\[[- ]\\]")
  11289. end t)
  11290. (progn
  11291. (if (boundp 'org-blocked-by-checkboxes)
  11292. (setq org-blocked-by-checkboxes t))
  11293. (throw 'dont-block nil)))))
  11294. t))) ; do not block
  11295. (defun org-entry-blocked-p ()
  11296. "Is the current entry blocked?"
  11297. (org-with-silent-modifications
  11298. (if (org-entry-get nil "NOBLOCKING")
  11299. nil ;; Never block this entry
  11300. (not (run-hook-with-args-until-failure
  11301. 'org-blocker-hook
  11302. (list :type 'todo-state-change
  11303. :position (point)
  11304. :from 'todo
  11305. :to 'done))))))
  11306. (defun org-update-statistics-cookies (all)
  11307. "Update the statistics cookie, either from TODO or from checkboxes.
  11308. This should be called with the cursor in a line with a statistics cookie."
  11309. (interactive "P")
  11310. (if all
  11311. (progn
  11312. (org-update-checkbox-count 'all)
  11313. (org-map-entries 'org-update-parent-todo-statistics))
  11314. (if (not (org-at-heading-p))
  11315. (org-update-checkbox-count)
  11316. (let ((pos (point-marker))
  11317. end l1 l2)
  11318. (ignore-errors (org-back-to-heading t))
  11319. (if (not (org-at-heading-p))
  11320. (org-update-checkbox-count)
  11321. (setq l1 (org-outline-level))
  11322. (setq end (save-excursion
  11323. (outline-next-heading)
  11324. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11325. (point)))
  11326. (if (and (save-excursion
  11327. (re-search-forward
  11328. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11329. (not (save-excursion (re-search-forward
  11330. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11331. (org-update-checkbox-count)
  11332. (if (and l2 (> l2 l1))
  11333. (progn
  11334. (goto-char end)
  11335. (org-update-parent-todo-statistics))
  11336. (goto-char pos)
  11337. (beginning-of-line 1)
  11338. (while (re-search-forward
  11339. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11340. (point-at-eol) t)
  11341. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11342. (goto-char pos)
  11343. (move-marker pos nil)))))
  11344. (defvar org-entry-property-inherited-from) ;; defined below
  11345. (defun org-update-parent-todo-statistics ()
  11346. "Update any statistics cookie in the parent of the current headline.
  11347. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11348. the entire subtree and this will travel up the hierarchy and update
  11349. statistics everywhere."
  11350. (let* ((prop (save-excursion (org-up-heading-safe)
  11351. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11352. (recursive (or (not org-hierarchical-todo-statistics)
  11353. (and prop (string-match "\\<recursive\\>" prop))))
  11354. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11355. 0))
  11356. (first t)
  11357. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11358. level ltoggle l1 new ndel
  11359. (cnt-all 0) (cnt-done 0) is-percent kwd
  11360. checkbox-beg ov ovs ove cookie-present)
  11361. (catch 'exit
  11362. (save-excursion
  11363. (beginning-of-line 1)
  11364. (setq ltoggle (funcall outline-level))
  11365. ;; Three situations are to consider:
  11366. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11367. ;; to the top-level ancestor on the headline;
  11368. ;; 2. If parent has "recursive" property, repeat up to the
  11369. ;; headline setting that property, taking inheritance into
  11370. ;; account;
  11371. ;; 3. Else, move up to direct parent and proceed only once.
  11372. (while (and (setq level (org-up-heading-safe))
  11373. (or recursive first)
  11374. (>= (point) lim))
  11375. (setq first nil cookie-present nil)
  11376. (unless (and level
  11377. (not (string-match
  11378. "\\<checkbox\\>"
  11379. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11380. "")))))
  11381. (throw 'exit nil))
  11382. (while (re-search-forward box-re (point-at-eol) t)
  11383. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11384. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11385. (save-match-data
  11386. (unless (outline-next-heading) (throw 'exit nil))
  11387. (while (and (looking-at org-complex-heading-regexp)
  11388. (> (setq l1 (length (match-string 1))) level))
  11389. (setq kwd (and (or recursive (= l1 ltoggle))
  11390. (match-string 2)))
  11391. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11392. (and (listp org-provide-todo-statistics)
  11393. (or (member kwd org-provide-todo-statistics)
  11394. (member kwd org-done-keywords))))
  11395. (setq cnt-all (1+ cnt-all))
  11396. (if (eq org-provide-todo-statistics t)
  11397. (and kwd (setq cnt-all (1+ cnt-all)))))
  11398. (and (member kwd org-done-keywords)
  11399. (setq cnt-done (1+ cnt-done)))
  11400. (outline-next-heading)))
  11401. (setq new
  11402. (if is-percent
  11403. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11404. (format "[%d/%d]" cnt-done cnt-all))
  11405. ndel (- (match-end 0) checkbox-beg))
  11406. ;; handle overlays when updating cookie from column view
  11407. (when (setq ov (car (overlays-at checkbox-beg)))
  11408. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11409. (delete-overlay ov))
  11410. (goto-char checkbox-beg)
  11411. (insert new)
  11412. (delete-region (point) (+ (point) ndel))
  11413. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11414. (when ov (move-overlay ov ovs ove)))
  11415. (when cookie-present
  11416. (run-hook-with-args 'org-after-todo-statistics-hook
  11417. cnt-done (- cnt-all cnt-done))))))
  11418. (run-hooks 'org-todo-statistics-hook)))
  11419. (defvar org-after-todo-statistics-hook nil
  11420. "Hook that is called after a TODO statistics cookie has been updated.
  11421. Each function is called with two arguments: the number of not-done entries
  11422. and the number of done entries.
  11423. For example, the following function, when added to this hook, will switch
  11424. an entry to DONE when all children are done, and back to TODO when new
  11425. entries are set to a TODO status. Note that this hook is only called
  11426. when there is a statistics cookie in the headline!
  11427. (defun org-summary-todo (n-done n-not-done)
  11428. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11429. (let (org-log-done org-log-states) ; turn off logging
  11430. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11431. ")
  11432. (defvar org-todo-statistics-hook nil
  11433. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11434. This hook runs even if there is no statistics cookie present, in which case
  11435. `org-after-todo-statistics-hook' would not run.")
  11436. (defun org-todo-trigger-tag-changes (state)
  11437. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11438. (let ((l org-todo-state-tags-triggers)
  11439. changes)
  11440. (when (or (not state) (equal state ""))
  11441. (setq changes (append changes (cdr (assoc "" l)))))
  11442. (when (and (stringp state) (> (length state) 0))
  11443. (setq changes (append changes (cdr (assoc state l)))))
  11444. (when (member state org-not-done-keywords)
  11445. (setq changes (append changes (cdr (assoc 'todo l)))))
  11446. (when (member state org-done-keywords)
  11447. (setq changes (append changes (cdr (assoc 'done l)))))
  11448. (dolist (c changes)
  11449. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11450. (defun org-local-logging (value)
  11451. "Get logging settings from a property VALUE."
  11452. (let* (words w a)
  11453. ;; directly set the variables, they are already local.
  11454. (setq org-log-done nil
  11455. org-log-repeat nil
  11456. org-todo-log-states nil)
  11457. (setq words (org-split-string value))
  11458. (while (setq w (pop words))
  11459. (cond
  11460. ((setq a (assoc w org-startup-options))
  11461. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11462. (set (nth 1 a) (nth 2 a))))
  11463. ((setq a (org-extract-log-state-settings w))
  11464. (and (member (car a) org-todo-keywords-1)
  11465. (push a org-todo-log-states)))))))
  11466. (defun org-get-todo-sequence-head (kwd)
  11467. "Return the head of the TODO sequence to which KWD belongs.
  11468. If KWD is not set, check if there is a text property remembering the
  11469. right sequence."
  11470. (let (p)
  11471. (cond
  11472. ((not kwd)
  11473. (or (get-text-property (point-at-bol) 'org-todo-head)
  11474. (progn
  11475. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11476. nil (point-at-eol)))
  11477. (get-text-property p 'org-todo-head))))
  11478. ((not (member kwd org-todo-keywords-1))
  11479. (car org-todo-keywords-1))
  11480. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11481. (defun org-fast-todo-selection ()
  11482. "Fast TODO keyword selection with single keys.
  11483. Returns the new TODO keyword, or nil if no state change should occur."
  11484. (let* ((fulltable org-todo-key-alist)
  11485. (done-keywords org-done-keywords) ;; needed for the faces.
  11486. (maxlen (apply 'max (mapcar
  11487. (lambda (x)
  11488. (if (stringp (car x)) (string-width (car x)) 0))
  11489. fulltable)))
  11490. (expert nil)
  11491. (fwidth (+ maxlen 3 1 3))
  11492. (ncol (/ (- (window-width) 4) fwidth))
  11493. tg cnt e c tbl
  11494. groups ingroup)
  11495. (save-excursion
  11496. (save-window-excursion
  11497. (if expert
  11498. (set-buffer (get-buffer-create " *Org todo*"))
  11499. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11500. (erase-buffer)
  11501. (org-set-local 'org-done-keywords done-keywords)
  11502. (setq tbl fulltable cnt 0)
  11503. (while (setq e (pop tbl))
  11504. (cond
  11505. ((equal e '(:startgroup))
  11506. (push '() groups) (setq ingroup t)
  11507. (when (not (= cnt 0))
  11508. (setq cnt 0)
  11509. (insert "\n"))
  11510. (insert "{ "))
  11511. ((equal e '(:endgroup))
  11512. (setq ingroup nil cnt 0)
  11513. (insert "}\n"))
  11514. ((equal e '(:newline))
  11515. (when (not (= cnt 0))
  11516. (setq cnt 0)
  11517. (insert "\n")
  11518. (setq e (car tbl))
  11519. (while (equal (car tbl) '(:newline))
  11520. (insert "\n")
  11521. (setq tbl (cdr tbl)))))
  11522. (t
  11523. (setq tg (car e) c (cdr e))
  11524. (if ingroup (push tg (car groups)))
  11525. (setq tg (org-add-props tg nil 'face
  11526. (org-get-todo-face tg)))
  11527. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11528. (insert "[" c "] " tg (make-string
  11529. (- fwidth 4 (length tg)) ?\ ))
  11530. (when (= (setq cnt (1+ cnt)) ncol)
  11531. (insert "\n")
  11532. (if ingroup (insert " "))
  11533. (setq cnt 0)))))
  11534. (insert "\n")
  11535. (goto-char (point-min))
  11536. (if (not expert) (org-fit-window-to-buffer))
  11537. (message "[a-z..]:Set [SPC]:clear")
  11538. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11539. (cond
  11540. ((or (= c ?\C-g)
  11541. (and (= c ?q) (not (rassoc c fulltable))))
  11542. (setq quit-flag t))
  11543. ((= c ?\ ) nil)
  11544. ((setq e (rassoc c fulltable) tg (car e))
  11545. tg)
  11546. (t (setq quit-flag t)))))))
  11547. (defun org-entry-is-todo-p ()
  11548. (member (org-get-todo-state) org-not-done-keywords))
  11549. (defun org-entry-is-done-p ()
  11550. (member (org-get-todo-state) org-done-keywords))
  11551. (defun org-get-todo-state ()
  11552. "Return the TODO keyword of the current subtree."
  11553. (save-excursion
  11554. (org-back-to-heading t)
  11555. (and (looking-at org-todo-line-regexp)
  11556. (match-end 2)
  11557. (match-string 2))))
  11558. (defun org-at-date-range-p (&optional inactive-ok)
  11559. "Is the cursor inside a date range?"
  11560. (interactive)
  11561. (save-excursion
  11562. (catch 'exit
  11563. (let ((pos (point)))
  11564. (skip-chars-backward "^[<\r\n")
  11565. (skip-chars-backward "<[")
  11566. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11567. (>= (match-end 0) pos)
  11568. (throw 'exit t))
  11569. (skip-chars-backward "^<[\r\n")
  11570. (skip-chars-backward "<[")
  11571. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11572. (>= (match-end 0) pos)
  11573. (throw 'exit t)))
  11574. nil)))
  11575. (defun org-get-repeat (&optional tagline)
  11576. "Check if there is a deadline/schedule with repeater in this entry."
  11577. (save-match-data
  11578. (save-excursion
  11579. (org-back-to-heading t)
  11580. (and (re-search-forward (if tagline
  11581. (concat tagline "\\s-*" org-repeat-re)
  11582. org-repeat-re)
  11583. (org-entry-end-position) t)
  11584. (match-string-no-properties 1)))))
  11585. (defvar org-last-changed-timestamp)
  11586. (defvar org-last-inserted-timestamp)
  11587. (defvar org-log-post-message)
  11588. (defvar org-log-note-purpose)
  11589. (defvar org-log-note-how)
  11590. (defvar org-log-note-extra)
  11591. (defun org-auto-repeat-maybe (done-word)
  11592. "Check if the current headline contains a repeated deadline/schedule.
  11593. If yes, set TODO state back to what it was and change the base date
  11594. of repeating deadline/scheduled time stamps to new date.
  11595. This function is run automatically after each state change to a DONE state."
  11596. ;; last-state is dynamically scoped into this function
  11597. (let* ((repeat (org-get-repeat))
  11598. (aa (assoc org-last-state org-todo-kwd-alist))
  11599. (interpret (nth 1 aa))
  11600. (head (nth 2 aa))
  11601. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11602. (msg "Entry repeats: ")
  11603. (org-log-done nil)
  11604. (org-todo-log-states nil)
  11605. re type n what ts time to-state)
  11606. (when repeat
  11607. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11608. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11609. org-todo-repeat-to-state))
  11610. (unless (and to-state (member to-state org-todo-keywords-1))
  11611. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11612. (org-todo to-state)
  11613. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11614. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11615. (org-time-stamp-format t t))))
  11616. (when org-log-repeat
  11617. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11618. (memq 'org-add-log-note post-command-hook))
  11619. ;; OK, we are already setup for some record
  11620. (if (eq org-log-repeat 'note)
  11621. ;; make sure we take a note, not only a time stamp
  11622. (setq org-log-note-how 'note))
  11623. ;; Set up for taking a record
  11624. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11625. org-last-state
  11626. 'findpos org-log-repeat)))
  11627. (org-back-to-heading t)
  11628. (org-add-planning-info nil nil 'closed)
  11629. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11630. org-deadline-time-regexp "\\)\\|\\("
  11631. org-ts-regexp "\\)"))
  11632. (while (re-search-forward
  11633. re (save-excursion (outline-next-heading) (point)) t)
  11634. (setq type (if (match-end 1) org-scheduled-string
  11635. (if (match-end 3) org-deadline-string "Plain:"))
  11636. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11637. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11638. (setq n (string-to-number (match-string 2 ts))
  11639. what (match-string 3 ts))
  11640. (if (equal what "w") (setq n (* n 7) what "d"))
  11641. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11642. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11643. ;; Preparation, see if we need to modify the start date for the change
  11644. (when (match-end 1)
  11645. (setq time (save-match-data (org-time-string-to-time ts)))
  11646. (cond
  11647. ((equal (match-string 1 ts) ".")
  11648. ;; Shift starting date to today
  11649. (org-timestamp-change
  11650. (- (org-today) (time-to-days time))
  11651. 'day))
  11652. ((equal (match-string 1 ts) "+")
  11653. (let ((nshiftmax 10) (nshift 0))
  11654. (while (or (= nshift 0)
  11655. (<= (time-to-days time)
  11656. (time-to-days (current-time))))
  11657. (when (= (incf nshift) nshiftmax)
  11658. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11659. (error "Abort")))
  11660. (org-timestamp-change n (cdr (assoc what whata)))
  11661. (org-at-timestamp-p t)
  11662. (setq ts (match-string 1))
  11663. (setq time (save-match-data (org-time-string-to-time ts)))))
  11664. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11665. ;; rematch, so that we have everything in place for the real shift
  11666. (org-at-timestamp-p t)
  11667. (setq ts (match-string 1))
  11668. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11669. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11670. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11671. (setq org-log-post-message msg)
  11672. (message "%s" msg))))
  11673. (defun org-show-todo-tree (arg)
  11674. "Make a compact tree which shows all headlines marked with TODO.
  11675. The tree will show the lines where the regexp matches, and all higher
  11676. headlines above the match.
  11677. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11678. With a numeric prefix N, construct a sparse tree for the Nth element
  11679. of `org-todo-keywords-1'."
  11680. (interactive "P")
  11681. (let ((case-fold-search nil)
  11682. (kwd-re
  11683. (cond ((null arg) org-not-done-regexp)
  11684. ((equal arg '(4))
  11685. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11686. (mapcar 'list org-todo-keywords-1))))
  11687. (concat "\\("
  11688. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11689. "\\)\\>")))
  11690. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11691. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11692. org-todo-keywords-1)))
  11693. (t (error "Invalid prefix argument: %s" arg)))))
  11694. (message "%d TODO entries found"
  11695. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11696. (defun org-deadline (arg &optional time)
  11697. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11698. With one universal prefix argument, remove any deadline from the item.
  11699. With two universal prefix arguments, prompt for a warning delay.
  11700. With argument TIME, set the deadline at the corresponding date. TIME
  11701. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11702. (interactive "P")
  11703. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11704. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11705. 'region-start-level 'region))
  11706. org-loop-over-headlines-in-active-region)
  11707. (org-map-entries
  11708. `(org-deadline ',arg ,time)
  11709. org-loop-over-headlines-in-active-region
  11710. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11711. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11712. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11713. (repeater (and old-date
  11714. (string-match
  11715. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11716. old-date)
  11717. (match-string 1 old-date))))
  11718. (cond
  11719. ((equal arg '(4))
  11720. (when (and old-date org-log-redeadline)
  11721. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11722. org-log-redeadline))
  11723. (org-remove-timestamp-with-keyword org-deadline-string)
  11724. (message "Item no longer has a deadline."))
  11725. ((equal arg '(16))
  11726. (save-excursion
  11727. (if (re-search-forward
  11728. org-deadline-time-regexp
  11729. (save-excursion (outline-next-heading) (point)) t)
  11730. (let* ((rpl0 (match-string 1))
  11731. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11732. (replace-match
  11733. (concat org-deadline-string
  11734. " <" rpl
  11735. (format " -%dd"
  11736. (abs
  11737. (- (time-to-days
  11738. (save-match-data
  11739. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11740. (time-to-days old-date-time))))
  11741. ">") t t))
  11742. (user-error "No deadline information to update"))))
  11743. (t
  11744. (org-add-planning-info 'deadline time 'closed)
  11745. (when (and old-date org-log-redeadline
  11746. (not (equal old-date
  11747. (substring org-last-inserted-timestamp 1 -1))))
  11748. (org-add-log-setup 'redeadline nil old-date 'findpos
  11749. org-log-redeadline))
  11750. (when repeater
  11751. (save-excursion
  11752. (org-back-to-heading t)
  11753. (when (re-search-forward (concat org-deadline-string " "
  11754. org-last-inserted-timestamp)
  11755. (save-excursion
  11756. (outline-next-heading) (point)) t)
  11757. (goto-char (1- (match-end 0)))
  11758. (insert " " repeater)
  11759. (setq org-last-inserted-timestamp
  11760. (concat (substring org-last-inserted-timestamp 0 -1)
  11761. " " repeater
  11762. (substring org-last-inserted-timestamp -1))))))
  11763. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11764. (defun org-schedule (arg &optional time)
  11765. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11766. With one universal prefix argument, remove any scheduling date from the item.
  11767. With two universal prefix arguments, prompt for a delay cookie.
  11768. With argument TIME, scheduled at the corresponding date. TIME can
  11769. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11770. (interactive "P")
  11771. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11772. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11773. 'region-start-level 'region))
  11774. org-loop-over-headlines-in-active-region)
  11775. (org-map-entries
  11776. `(org-schedule ',arg ,time)
  11777. org-loop-over-headlines-in-active-region
  11778. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11779. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11780. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11781. (repeater (and old-date
  11782. (string-match
  11783. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11784. old-date)
  11785. (match-string 1 old-date))))
  11786. (cond
  11787. ((equal arg '(4))
  11788. (progn
  11789. (when (and old-date org-log-reschedule)
  11790. (org-add-log-setup 'delschedule nil old-date 'findpos
  11791. org-log-reschedule))
  11792. (org-remove-timestamp-with-keyword org-scheduled-string)
  11793. (message "Item is no longer scheduled.")))
  11794. ((equal arg '(16))
  11795. (save-excursion
  11796. (if (re-search-forward
  11797. org-scheduled-time-regexp
  11798. (save-excursion (outline-next-heading) (point)) t)
  11799. (let* ((rpl0 (match-string 1))
  11800. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11801. (replace-match
  11802. (concat org-scheduled-string
  11803. " <" rpl
  11804. (format " -%dd"
  11805. (abs
  11806. (- (time-to-days
  11807. (save-match-data
  11808. (org-read-date nil t nil "Delay until" old-date-time)))
  11809. (time-to-days old-date-time))))
  11810. ">") t t))
  11811. (user-error "No scheduled information to update"))))
  11812. (t
  11813. (org-add-planning-info 'scheduled time 'closed)
  11814. (when (and old-date org-log-reschedule
  11815. (not (equal old-date
  11816. (substring org-last-inserted-timestamp 1 -1))))
  11817. (org-add-log-setup 'reschedule nil old-date 'findpos
  11818. org-log-reschedule))
  11819. (when repeater
  11820. (save-excursion
  11821. (org-back-to-heading t)
  11822. (when (re-search-forward (concat org-scheduled-string " "
  11823. org-last-inserted-timestamp)
  11824. (save-excursion
  11825. (outline-next-heading) (point)) t)
  11826. (goto-char (1- (match-end 0)))
  11827. (insert " " repeater)
  11828. (setq org-last-inserted-timestamp
  11829. (concat (substring org-last-inserted-timestamp 0 -1)
  11830. " " repeater
  11831. (substring org-last-inserted-timestamp -1))))))
  11832. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11833. (defun org-get-scheduled-time (pom &optional inherit)
  11834. "Get the scheduled time as a time tuple, of a format suitable
  11835. for calling org-schedule with, or if there is no scheduling,
  11836. returns nil."
  11837. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11838. (when time
  11839. (apply 'encode-time (org-parse-time-string time)))))
  11840. (defun org-get-deadline-time (pom &optional inherit)
  11841. "Get the deadline as a time tuple, of a format suitable for
  11842. calling org-deadline with, or if there is no scheduling, returns
  11843. nil."
  11844. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11845. (when time
  11846. (apply 'encode-time (org-parse-time-string time)))))
  11847. (defun org-remove-timestamp-with-keyword (keyword)
  11848. "Remove all time stamps with KEYWORD in the current entry."
  11849. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11850. beg)
  11851. (save-excursion
  11852. (org-back-to-heading t)
  11853. (setq beg (point))
  11854. (outline-next-heading)
  11855. (while (re-search-backward re beg t)
  11856. (replace-match "")
  11857. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11858. (equal (char-before) ?\ ))
  11859. (backward-delete-char 1)
  11860. (if (string-match "^[ \t]*$" (buffer-substring
  11861. (point-at-bol) (point-at-eol)))
  11862. (delete-region (point-at-bol)
  11863. (min (point-max) (1+ (point-at-eol))))))))))
  11864. (defun org-add-planning-info (what &optional time &rest remove)
  11865. "Insert new timestamp with keyword in the line directly after the headline.
  11866. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11867. If non is given, the user is prompted for a date.
  11868. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11869. be removed."
  11870. (interactive)
  11871. (let (org-time-was-given org-end-time-was-given ts
  11872. end default-time default-input)
  11873. (catch 'exit
  11874. (when (and (memq what '(scheduled deadline))
  11875. (or (not time)
  11876. (and (stringp time)
  11877. (string-match "^[-+]+[0-9]" time))))
  11878. ;; Try to get a default date/time from existing timestamp
  11879. (save-excursion
  11880. (org-back-to-heading t)
  11881. (setq end (save-excursion (outline-next-heading) (point)))
  11882. (when (re-search-forward (if (eq what 'scheduled)
  11883. org-scheduled-time-regexp
  11884. org-deadline-time-regexp)
  11885. end t)
  11886. (setq ts (match-string 1)
  11887. default-time
  11888. (apply 'encode-time (org-parse-time-string ts))
  11889. default-input (and ts (org-get-compact-tod ts))))))
  11890. (when what
  11891. (setq time
  11892. (if (stringp time)
  11893. ;; This is a string (relative or absolute), set proper date
  11894. (apply 'encode-time
  11895. (org-read-date-analyze
  11896. time default-time (decode-time default-time)))
  11897. ;; If necessary, get the time from the user
  11898. (or time (org-read-date nil 'to-time nil nil
  11899. default-time default-input)))))
  11900. (when (and org-insert-labeled-timestamps-at-point
  11901. (member what '(scheduled deadline)))
  11902. (insert
  11903. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11904. (org-insert-time-stamp time org-time-was-given
  11905. nil nil nil (list org-end-time-was-given))
  11906. (setq what nil))
  11907. (save-excursion
  11908. (save-restriction
  11909. (let (col list elt ts buffer-invisibility-spec)
  11910. (org-back-to-heading t)
  11911. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11912. (goto-char (match-end 1))
  11913. (setq col (current-column))
  11914. (goto-char (match-end 0))
  11915. (if (eobp) (insert "\n") (forward-char 1))
  11916. (when (and (not what)
  11917. (not (looking-at
  11918. (concat "[ \t]*"
  11919. org-keyword-time-not-clock-regexp))))
  11920. ;; Nothing to add, nothing to remove...... :-)
  11921. (throw 'exit nil))
  11922. (if (and (not (looking-at org-outline-regexp))
  11923. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11924. "[^\r\n]*"))
  11925. (not (equal (match-string 1) org-clock-string)))
  11926. (narrow-to-region (match-beginning 0) (match-end 0))
  11927. (insert-before-markers "\n")
  11928. (backward-char 1)
  11929. (narrow-to-region (point) (point))
  11930. (and org-adapt-indentation (org-indent-to-column col)))
  11931. ;; Check if we have to remove something.
  11932. (setq list (cons what remove))
  11933. (while list
  11934. (setq elt (pop list))
  11935. (when (or (and (eq elt 'scheduled)
  11936. (re-search-forward org-scheduled-time-regexp nil t))
  11937. (and (eq elt 'deadline)
  11938. (re-search-forward org-deadline-time-regexp nil t))
  11939. (and (eq elt 'closed)
  11940. (re-search-forward org-closed-time-regexp nil t)))
  11941. (replace-match "")
  11942. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11943. (and (looking-at "[ \t]+") (replace-match ""))
  11944. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11945. (when what
  11946. (insert
  11947. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11948. (cond ((eq what 'scheduled) org-scheduled-string)
  11949. ((eq what 'deadline) org-deadline-string)
  11950. ((eq what 'closed) org-closed-string))
  11951. " ")
  11952. (setq ts (org-insert-time-stamp
  11953. time
  11954. (or org-time-was-given
  11955. (and (eq what 'closed) org-log-done-with-time))
  11956. (eq what 'closed)
  11957. nil nil (list org-end-time-was-given)))
  11958. (insert
  11959. (if (not (or (bolp) (eq (char-before) ?\ )
  11960. (memq (char-after) '(32 10))
  11961. (eobp))) " " ""))
  11962. (end-of-line 1))
  11963. (goto-char (point-min))
  11964. (widen)
  11965. (if (and (looking-at "[ \t]*\n")
  11966. (equal (char-before) ?\n))
  11967. (delete-region (1- (point)) (point-at-eol)))
  11968. ts))))))
  11969. (defvar org-log-note-marker (make-marker))
  11970. (defvar org-log-note-purpose nil)
  11971. (defvar org-log-note-state nil)
  11972. (defvar org-log-note-previous-state nil)
  11973. (defvar org-log-note-how nil)
  11974. (defvar org-log-note-extra nil)
  11975. (defvar org-log-note-window-configuration nil)
  11976. (defvar org-log-note-return-to (make-marker))
  11977. (defvar org-log-note-effective-time nil
  11978. "Remembered current time so that dynamically scoped
  11979. `org-extend-today-until' affects tha timestamps in state change
  11980. log")
  11981. (defvar org-log-post-message nil
  11982. "Message to be displayed after a log note has been stored.
  11983. The auto-repeater uses this.")
  11984. (defun org-add-note ()
  11985. "Add a note to the current entry.
  11986. This is done in the same way as adding a state change note."
  11987. (interactive)
  11988. (org-add-log-setup 'note nil nil 'findpos nil))
  11989. (defvar org-property-end-re)
  11990. (defun org-add-log-setup (&optional purpose state prev-state
  11991. findpos how extra)
  11992. "Set up the post command hook to take a note.
  11993. If this is about to TODO state change, the new state is expected in STATE.
  11994. When FINDPOS is non-nil, find the correct position for the note in
  11995. the current entry. If not, assume that it can be inserted at point.
  11996. HOW is an indicator what kind of note should be created.
  11997. EXTRA is additional text that will be inserted into the notes buffer."
  11998. (let* ((org-log-into-drawer (org-log-into-drawer))
  11999. (drawer (cond ((stringp org-log-into-drawer)
  12000. org-log-into-drawer)
  12001. (org-log-into-drawer "LOGBOOK"))))
  12002. (save-restriction
  12003. (save-excursion
  12004. (when findpos
  12005. (org-back-to-heading t)
  12006. (narrow-to-region (point) (save-excursion
  12007. (outline-next-heading) (point)))
  12008. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12009. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12010. "[^\r\n]*\\)?"))
  12011. (goto-char (match-end 0))
  12012. (cond
  12013. (drawer
  12014. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12015. nil t)
  12016. (progn
  12017. (goto-char (match-end 0))
  12018. (or org-log-states-order-reversed
  12019. (and (re-search-forward org-property-end-re nil t)
  12020. (goto-char (1- (match-beginning 0))))))
  12021. (insert "\n:" drawer ":\n:END:")
  12022. (beginning-of-line 0)
  12023. (org-indent-line)
  12024. (beginning-of-line 2)
  12025. (org-indent-line)
  12026. (end-of-line 0)))
  12027. ((and org-log-state-notes-insert-after-drawers
  12028. (save-excursion
  12029. (forward-line) (looking-at org-drawer-regexp)))
  12030. (forward-line)
  12031. (while (looking-at org-drawer-regexp)
  12032. (goto-char (match-end 0))
  12033. (re-search-forward org-property-end-re (point-max) t)
  12034. (forward-line))
  12035. (forward-line -1)))
  12036. (unless org-log-states-order-reversed
  12037. (and (= (char-after) ?\n) (forward-char 1))
  12038. (org-skip-over-state-notes)
  12039. (skip-chars-backward " \t\n\r")))
  12040. (move-marker org-log-note-marker (point))
  12041. (setq org-log-note-purpose purpose
  12042. org-log-note-state state
  12043. org-log-note-previous-state prev-state
  12044. org-log-note-how how
  12045. org-log-note-extra extra
  12046. org-log-note-effective-time (org-current-effective-time))
  12047. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12048. (defun org-skip-over-state-notes ()
  12049. "Skip past the list of State notes in an entry."
  12050. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12051. (when (ignore-errors (goto-char (org-in-item-p)))
  12052. (let* ((struct (org-list-struct))
  12053. (prevs (org-list-prevs-alist struct)))
  12054. (while (looking-at "[ \t]*- State")
  12055. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12056. (org-list-get-item-end (point) struct)))))))
  12057. (defun org-add-log-note (&optional purpose)
  12058. "Pop up a window for taking a note, and add this note later at point."
  12059. (remove-hook 'post-command-hook 'org-add-log-note)
  12060. (setq org-log-note-window-configuration (current-window-configuration))
  12061. (delete-other-windows)
  12062. (move-marker org-log-note-return-to (point))
  12063. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12064. (goto-char org-log-note-marker)
  12065. (org-switch-to-buffer-other-window "*Org Note*")
  12066. (erase-buffer)
  12067. (if (memq org-log-note-how '(time state))
  12068. (let (current-prefix-arg) (org-store-log-note))
  12069. (let ((org-inhibit-startup t)) (org-mode))
  12070. (insert (format "# Insert note for %s.
  12071. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12072. (cond
  12073. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12074. ((eq org-log-note-purpose 'done) "closed todo item")
  12075. ((eq org-log-note-purpose 'state)
  12076. (format "state change from \"%s\" to \"%s\""
  12077. (or org-log-note-previous-state "")
  12078. (or org-log-note-state "")))
  12079. ((eq org-log-note-purpose 'reschedule)
  12080. "rescheduling")
  12081. ((eq org-log-note-purpose 'delschedule)
  12082. "no longer scheduled")
  12083. ((eq org-log-note-purpose 'redeadline)
  12084. "changing deadline")
  12085. ((eq org-log-note-purpose 'deldeadline)
  12086. "removing deadline")
  12087. ((eq org-log-note-purpose 'refile)
  12088. "refiling")
  12089. ((eq org-log-note-purpose 'note)
  12090. "this entry")
  12091. (t (error "This should not happen")))))
  12092. (if org-log-note-extra (insert org-log-note-extra))
  12093. (org-set-local 'org-finish-function 'org-store-log-note)
  12094. (run-hooks 'org-log-buffer-setup-hook)))
  12095. (defvar org-note-abort nil) ; dynamically scoped
  12096. (defun org-store-log-note ()
  12097. "Finish taking a log note, and insert it to where it belongs."
  12098. (let ((txt (buffer-string)))
  12099. (kill-buffer (current-buffer))
  12100. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12101. lines ind bul)
  12102. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12103. (setq txt (replace-match "" t t txt)))
  12104. (if (string-match "\\s-+\\'" txt)
  12105. (setq txt (replace-match "" t t txt)))
  12106. (setq lines (org-split-string txt "\n"))
  12107. (when (and note (string-match "\\S-" note))
  12108. (setq note
  12109. (org-replace-escapes
  12110. note
  12111. (list (cons "%u" (user-login-name))
  12112. (cons "%U" user-full-name)
  12113. (cons "%t" (format-time-string
  12114. (org-time-stamp-format 'long 'inactive)
  12115. org-log-note-effective-time))
  12116. (cons "%T" (format-time-string
  12117. (org-time-stamp-format 'long nil)
  12118. org-log-note-effective-time))
  12119. (cons "%d" (format-time-string
  12120. (org-time-stamp-format nil 'inactive)
  12121. org-log-note-effective-time))
  12122. (cons "%D" (format-time-string
  12123. (org-time-stamp-format nil nil)
  12124. org-log-note-effective-time))
  12125. (cons "%s" (if org-log-note-state
  12126. (concat "\"" org-log-note-state "\"")
  12127. ""))
  12128. (cons "%S" (if org-log-note-previous-state
  12129. (concat "\"" org-log-note-previous-state "\"")
  12130. "\"\"")))))
  12131. (if lines (setq note (concat note " \\\\")))
  12132. (push note lines))
  12133. (when (or current-prefix-arg org-note-abort)
  12134. (when org-log-into-drawer
  12135. (org-remove-empty-drawer-at
  12136. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12137. org-log-note-marker))
  12138. (setq lines nil))
  12139. (when lines
  12140. (with-current-buffer (marker-buffer org-log-note-marker)
  12141. (save-excursion
  12142. (goto-char org-log-note-marker)
  12143. (move-marker org-log-note-marker nil)
  12144. (end-of-line 1)
  12145. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12146. (setq ind (save-excursion
  12147. (if (ignore-errors (goto-char (org-in-item-p)))
  12148. (let ((struct (org-list-struct)))
  12149. (org-list-get-ind
  12150. (org-list-get-top-point struct) struct))
  12151. (skip-chars-backward " \r\t\n")
  12152. (cond
  12153. ((and (org-at-heading-p)
  12154. org-adapt-indentation)
  12155. (1+ (org-current-level)))
  12156. ((org-at-heading-p) 0)
  12157. (t (org-get-indentation))))))
  12158. (setq bul (org-list-bullet-string "-"))
  12159. (org-indent-line-to ind)
  12160. (insert bul (pop lines))
  12161. (let ((ind-body (+ (length bul) ind)))
  12162. (while lines
  12163. (insert "\n")
  12164. (org-indent-line-to ind-body)
  12165. (insert (pop lines))))
  12166. (message "Note stored")
  12167. (org-back-to-heading t)
  12168. (org-cycle-hide-drawers 'children))))))
  12169. (set-window-configuration org-log-note-window-configuration)
  12170. (with-current-buffer (marker-buffer org-log-note-return-to)
  12171. (goto-char org-log-note-return-to))
  12172. (move-marker org-log-note-return-to nil)
  12173. (and org-log-post-message (message "%s" org-log-post-message)))
  12174. (defun org-remove-empty-drawer-at (drawer pos)
  12175. "Remove an empty drawer DRAWER at position POS.
  12176. POS may also be a marker."
  12177. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12178. (save-excursion
  12179. (save-restriction
  12180. (widen)
  12181. (goto-char pos)
  12182. (if (org-in-regexp
  12183. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12184. (replace-match ""))))))
  12185. (defvar org-ts-type nil)
  12186. (defun org-sparse-tree (&optional arg type)
  12187. "Create a sparse tree, prompt for the details.
  12188. This command can create sparse trees. You first need to select the type
  12189. of match used to create the tree:
  12190. t Show all TODO entries.
  12191. T Show entries with a specific TODO keyword.
  12192. m Show entries selected by a tags/property match.
  12193. p Enter a property name and its value (both with completion on existing
  12194. names/values) and show entries with that property.
  12195. r Show entries matching a regular expression (`/' can be used as well).
  12196. b Show deadlines and scheduled items before a date.
  12197. a Show deadlines and scheduled items after a date.
  12198. d Show deadlines due within `org-deadline-warning-days'.
  12199. D Show deadlines and scheduled items between a date range."
  12200. (interactive "P")
  12201. (let (ans kwd value ts-type)
  12202. (setq type (or type org-sparse-tree-default-date-type))
  12203. (setq org-ts-type type)
  12204. (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"
  12205. (cond ((eq type 'all) "all timestamps")
  12206. ((eq type 'scheduled) "only scheduled")
  12207. ((eq type 'deadline) "only deadline")
  12208. ((eq type 'active) "only active timestamps")
  12209. ((eq type 'inactive) "only inactive timestamps")
  12210. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12211. ((eq type 'closed) "with a closed time-stamp")
  12212. (t "scheduled/deadline")))
  12213. (setq ans (read-char-exclusive))
  12214. (cond
  12215. ((equal ans ?c)
  12216. (org-sparse-tree
  12217. arg (cadr (member type '(scheduled-or-deadline
  12218. all scheduled deadline active inactive closed)))))
  12219. ((equal ans ?d)
  12220. (call-interactively 'org-check-deadlines))
  12221. ((equal ans ?b)
  12222. (call-interactively 'org-check-before-date))
  12223. ((equal ans ?a)
  12224. (call-interactively 'org-check-after-date))
  12225. ((equal ans ?D)
  12226. (call-interactively 'org-check-dates-range))
  12227. ((equal ans ?t)
  12228. (call-interactively 'org-show-todo-tree))
  12229. ((equal ans ?T)
  12230. (org-show-todo-tree '(4)))
  12231. ((member ans '(?T ?m))
  12232. (call-interactively 'org-match-sparse-tree))
  12233. ((member ans '(?p ?P))
  12234. (setq kwd (org-icompleting-read "Property: "
  12235. (mapcar 'list (org-buffer-property-keys))))
  12236. (setq value (org-icompleting-read "Value: "
  12237. (mapcar 'list (org-property-values kwd))))
  12238. (unless (string-match "\\`{.*}\\'" value)
  12239. (setq value (concat "\"" value "\"")))
  12240. (org-match-sparse-tree arg (concat kwd "=" value)))
  12241. ((member ans '(?r ?R ?/))
  12242. (call-interactively 'org-occur))
  12243. (t (error "No such sparse tree command \"%c\"" ans)))))
  12244. (defvar org-occur-highlights nil
  12245. "List of overlays used for occur matches.")
  12246. (make-variable-buffer-local 'org-occur-highlights)
  12247. (defvar org-occur-parameters nil
  12248. "Parameters of the active org-occur calls.
  12249. This is a list, each call to org-occur pushes as cons cell,
  12250. containing the regular expression and the callback, onto the list.
  12251. The list can contain several entries if `org-occur' has been called
  12252. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12253. will only contain one set of parameters. When the highlights are
  12254. removed (for example with `C-c C-c', or with the next edit (depending
  12255. on `org-remove-highlights-with-change'), this variable is emptied
  12256. as well.")
  12257. (make-variable-buffer-local 'org-occur-parameters)
  12258. (defun org-occur (regexp &optional keep-previous callback)
  12259. "Make a compact tree which shows all matches of REGEXP.
  12260. The tree will show the lines where the regexp matches, and all higher
  12261. headlines above the match. It will also show the heading after the match,
  12262. to make sure editing the matching entry is easy.
  12263. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12264. call to `org-occur' will be kept, to allow stacking of calls to this
  12265. command.
  12266. If CALLBACK is non-nil, it is a function which is called to confirm
  12267. that the match should indeed be shown."
  12268. (interactive "sRegexp: \nP")
  12269. (when (equal regexp "")
  12270. (error "Regexp cannot be empty"))
  12271. (unless keep-previous
  12272. (org-remove-occur-highlights nil nil t))
  12273. (push (cons regexp callback) org-occur-parameters)
  12274. (let ((cnt 0))
  12275. (save-excursion
  12276. (goto-char (point-min))
  12277. (if (or (not keep-previous) ; do not want to keep
  12278. (not org-occur-highlights)) ; no previous matches
  12279. ;; hide everything
  12280. (org-overview))
  12281. (while (re-search-forward regexp nil t)
  12282. (when (or (not callback)
  12283. (save-match-data (funcall callback)))
  12284. (setq cnt (1+ cnt))
  12285. (when org-highlight-sparse-tree-matches
  12286. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12287. (org-show-context 'occur-tree))))
  12288. (when org-remove-highlights-with-change
  12289. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12290. nil 'local))
  12291. (unless org-sparse-tree-open-archived-trees
  12292. (org-hide-archived-subtrees (point-min) (point-max)))
  12293. (run-hooks 'org-occur-hook)
  12294. (if (org-called-interactively-p 'interactive)
  12295. (message "%d match(es) for regexp %s" cnt regexp))
  12296. cnt))
  12297. (defun org-occur-next-match (&optional n reset)
  12298. "Function for `next-error-function' to find sparse tree matches.
  12299. N is the number of matches to move, when negative move backwards.
  12300. RESET is entirely ignored - this function always goes back to the
  12301. starting point when no match is found."
  12302. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12303. (search-func (if (< n 0)
  12304. 'previous-single-char-property-change
  12305. 'next-single-char-property-change))
  12306. (n (abs n))
  12307. (pos (point))
  12308. p1)
  12309. (catch 'exit
  12310. (while (setq p1 (funcall search-func (point) 'org-type))
  12311. (when (equal p1 limit)
  12312. (goto-char pos)
  12313. (error "No more matches"))
  12314. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12315. (setq n (1- n))
  12316. (when (= n 0)
  12317. (goto-char p1)
  12318. (throw 'exit (point))))
  12319. (goto-char p1))
  12320. (goto-char p1)
  12321. (error "No more matches"))))
  12322. (defun org-show-context (&optional key)
  12323. "Make sure point and context are visible.
  12324. How much context is shown depends upon the variables
  12325. `org-show-hierarchy-above', `org-show-following-heading',
  12326. `org-show-entry-below' and `org-show-siblings'."
  12327. (let ((heading-p (org-at-heading-p t))
  12328. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12329. (following-p (org-get-alist-option org-show-following-heading key))
  12330. (entry-p (org-get-alist-option org-show-entry-below key))
  12331. (siblings-p (org-get-alist-option org-show-siblings key)))
  12332. ;; Show heading or entry text
  12333. (if (and heading-p (not entry-p))
  12334. (org-flag-heading nil) ; only show the heading
  12335. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12336. (org-show-hidden-entry))) ; show entire entry
  12337. (when following-p
  12338. ;; Show next sibling, or heading below text
  12339. (save-excursion
  12340. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12341. (org-flag-heading nil))))
  12342. (when siblings-p (org-show-siblings))
  12343. (when hierarchy-p
  12344. ;; show all higher headings, possibly with siblings
  12345. (save-excursion
  12346. (while (and (condition-case nil
  12347. (progn (org-up-heading-all 1) t)
  12348. (error nil))
  12349. (not (bobp)))
  12350. (org-flag-heading nil)
  12351. (when siblings-p (org-show-siblings)))))
  12352. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12353. (defvar org-reveal-start-hook nil
  12354. "Hook run before revealing a location.")
  12355. (defun org-reveal (&optional siblings)
  12356. "Show current entry, hierarchy above it, and the following headline.
  12357. This can be used to show a consistent set of context around locations
  12358. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12359. not t for the search context.
  12360. With optional argument SIBLINGS, on each level of the hierarchy all
  12361. siblings are shown. This repairs the tree structure to what it would
  12362. look like when opened with hierarchical calls to `org-cycle'.
  12363. With double optional argument \\[universal-argument] \\[universal-argument], \
  12364. go to the parent and show the
  12365. entire tree."
  12366. (interactive "P")
  12367. (run-hooks 'org-reveal-start-hook)
  12368. (let ((org-show-hierarchy-above t)
  12369. (org-show-following-heading t)
  12370. (org-show-siblings (if siblings t org-show-siblings)))
  12371. (org-show-context nil))
  12372. (when (equal siblings '(16))
  12373. (save-excursion
  12374. (when (org-up-heading-safe)
  12375. (org-show-subtree)
  12376. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12377. (defun org-highlight-new-match (beg end)
  12378. "Highlight from BEG to END and mark the highlight is an occur headline."
  12379. (let ((ov (make-overlay beg end)))
  12380. (overlay-put ov 'face 'secondary-selection)
  12381. (overlay-put ov 'org-type 'org-occur)
  12382. (push ov org-occur-highlights)))
  12383. (defun org-remove-occur-highlights (&optional beg end noremove)
  12384. "Remove the occur highlights from the buffer.
  12385. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12386. from the `before-change-functions' in the current buffer."
  12387. (interactive)
  12388. (unless org-inhibit-highlight-removal
  12389. (mapc 'delete-overlay org-occur-highlights)
  12390. (setq org-occur-highlights nil)
  12391. (setq org-occur-parameters nil)
  12392. (unless noremove
  12393. (remove-hook 'before-change-functions
  12394. 'org-remove-occur-highlights 'local))))
  12395. ;;;; Priorities
  12396. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12397. "Regular expression matching the priority indicator.")
  12398. (defvar org-remove-priority-next-time nil)
  12399. (defun org-priority-up ()
  12400. "Increase the priority of the current item."
  12401. (interactive)
  12402. (org-priority 'up))
  12403. (defun org-priority-down ()
  12404. "Decrease the priority of the current item."
  12405. (interactive)
  12406. (org-priority 'down))
  12407. (defun org-priority (&optional action show)
  12408. "Change the priority of an item.
  12409. ACTION can be `set', `up', `down', or a character."
  12410. (interactive "P")
  12411. (if (equal action '(4))
  12412. (org-show-priority)
  12413. (unless org-enable-priority-commands
  12414. (error "Priority commands are disabled"))
  12415. (setq action (or action 'set))
  12416. (let (current new news have remove)
  12417. (save-excursion
  12418. (org-back-to-heading t)
  12419. (if (looking-at org-priority-regexp)
  12420. (setq current (string-to-char (match-string 2))
  12421. have t))
  12422. (cond
  12423. ((eq action 'remove)
  12424. (setq remove t new ?\ ))
  12425. ((or (eq action 'set)
  12426. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12427. (if (not (eq action 'set))
  12428. (setq new action)
  12429. (message "Priority %c-%c, SPC to remove: "
  12430. org-highest-priority org-lowest-priority)
  12431. (save-match-data
  12432. (setq new (read-char-exclusive))))
  12433. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12434. (= (upcase org-lowest-priority) org-lowest-priority))
  12435. (setq new (upcase new)))
  12436. (cond ((equal new ?\ ) (setq remove t))
  12437. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12438. (error "Priority must be between `%c' and `%c'"
  12439. org-highest-priority org-lowest-priority))))
  12440. ((eq action 'up)
  12441. (setq new (if have
  12442. (1- current) ; normal cycling
  12443. ;; last priority was empty
  12444. (if (eq last-command this-command)
  12445. org-lowest-priority ; wrap around empty to lowest
  12446. ;; default
  12447. (if org-priority-start-cycle-with-default
  12448. org-default-priority
  12449. (1- org-default-priority))))))
  12450. ((eq action 'down)
  12451. (setq new (if have
  12452. (1+ current) ; normal cycling
  12453. ;; last priority was empty
  12454. (if (eq last-command this-command)
  12455. org-highest-priority ; wrap around empty to highest
  12456. ;; default
  12457. (if org-priority-start-cycle-with-default
  12458. org-default-priority
  12459. (1+ org-default-priority))))))
  12460. (t (error "Invalid action")))
  12461. (if (or (< (upcase new) org-highest-priority)
  12462. (> (upcase new) org-lowest-priority))
  12463. (if (and (memq action '(up down))
  12464. (not have) (not (eq last-command this-command)))
  12465. ;; `new' is from default priority
  12466. (error
  12467. "The default can not be set, see `org-default-priority' why")
  12468. ;; normal cycling: `new' is beyond highest/lowest priority
  12469. ;; and is wrapped around to the empty priority
  12470. (setq remove t)))
  12471. (setq news (format "%c" new))
  12472. (if have
  12473. (if remove
  12474. (replace-match "" t t nil 1)
  12475. (replace-match news t t nil 2))
  12476. (if remove
  12477. (error "No priority cookie found in line")
  12478. (let ((case-fold-search nil))
  12479. (looking-at org-todo-line-regexp))
  12480. (if (match-end 2)
  12481. (progn
  12482. (goto-char (match-end 2))
  12483. (insert " [#" news "]"))
  12484. (goto-char (match-beginning 3))
  12485. (insert "[#" news "] "))))
  12486. (org-preserve-lc (org-set-tags nil 'align)))
  12487. (if remove
  12488. (message "Priority removed")
  12489. (message "Priority of current item set to %s" news)))))
  12490. (defun org-show-priority ()
  12491. "Show the priority of the current item.
  12492. This priority is composed of the main priority given with the [#A] cookies,
  12493. and by additional input from the age of a schedules or deadline entry."
  12494. (interactive)
  12495. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12496. (org-get-at-bol 'priority)
  12497. (save-excursion
  12498. (save-match-data
  12499. (beginning-of-line)
  12500. (and (looking-at org-heading-regexp)
  12501. (org-get-priority (match-string 0))))))))
  12502. (message "Priority is %d" (if pri pri -1000))))
  12503. (defun org-get-priority (s)
  12504. "Find priority cookie and return priority."
  12505. (save-match-data
  12506. (if (functionp org-get-priority-function)
  12507. (funcall org-get-priority-function)
  12508. (if (not (string-match org-priority-regexp s))
  12509. (* 1000 (- org-lowest-priority org-default-priority))
  12510. (* 1000 (- org-lowest-priority
  12511. (string-to-char (match-string 2 s))))))))
  12512. ;;;; Tags
  12513. (defvar org-agenda-archives-mode)
  12514. (defvar org-map-continue-from nil
  12515. "Position from where mapping should continue.
  12516. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12517. (defvar org-scanner-tags nil
  12518. "The current tag list while the tags scanner is running.")
  12519. (defvar org-trust-scanner-tags nil
  12520. "Should `org-get-tags-at' use the tags for the scanner.
  12521. This is for internal dynamical scoping only.
  12522. When this is non-nil, the function `org-get-tags-at' will return the value
  12523. of `org-scanner-tags' instead of building the list by itself. This
  12524. can lead to large speed-ups when the tags scanner is used in a file with
  12525. many entries, and when the list of tags is retrieved, for example to
  12526. obtain a list of properties. Building the tags list for each entry in such
  12527. a file becomes an N^2 operation - but with this variable set, it scales
  12528. as N.")
  12529. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12530. "Sca headline tags with inheritance and produce output ACTION.
  12531. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12532. or `agenda' to produce an entry list for an agenda view. It can also be
  12533. a Lisp form or a function that should be called at each matched headline, in
  12534. this case the return value is a list of all return values from these calls.
  12535. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12536. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12537. only lines with a not-done TODO keyword are included in the output.
  12538. This should be the same variable that was scoped into
  12539. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12540. START-LEVEL can be a string with asterisks, reducing the scope to
  12541. headlines matching this string."
  12542. (require 'org-agenda)
  12543. (let* ((re (concat "^"
  12544. (if start-level
  12545. ;; Get the correct level to match
  12546. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12547. org-outline-regexp)
  12548. " *\\(\\<\\("
  12549. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12550. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12551. (props (list 'face 'default
  12552. 'done-face 'org-agenda-done
  12553. 'undone-face 'default
  12554. 'mouse-face 'highlight
  12555. 'org-not-done-regexp org-not-done-regexp
  12556. 'org-todo-regexp org-todo-regexp
  12557. 'org-complex-heading-regexp org-complex-heading-regexp
  12558. 'help-echo
  12559. (format "mouse-2 or RET jump to org file %s"
  12560. (abbreviate-file-name
  12561. (or (buffer-file-name (buffer-base-buffer))
  12562. (buffer-name (buffer-base-buffer)))))))
  12563. (case-fold-search nil)
  12564. (org-map-continue-from nil)
  12565. lspos tags tags-list
  12566. (tags-alist (list (cons 0 org-file-tags)))
  12567. (llast 0) rtn rtn1 level category i txt
  12568. todo marker entry priority)
  12569. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12570. (setq action (list 'lambda nil action)))
  12571. (save-excursion
  12572. (goto-char (point-min))
  12573. (when (eq action 'sparse-tree)
  12574. (org-overview)
  12575. (org-remove-occur-highlights))
  12576. (while (re-search-forward re nil t)
  12577. (setq org-map-continue-from nil)
  12578. (catch :skip
  12579. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12580. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12581. (goto-char (setq lspos (match-beginning 0)))
  12582. (setq level (org-reduced-level (org-outline-level))
  12583. category (org-get-category))
  12584. (setq i llast llast level)
  12585. ;; remove tag lists from same and sublevels
  12586. (while (>= i level)
  12587. (when (setq entry (assoc i tags-alist))
  12588. (setq tags-alist (delete entry tags-alist)))
  12589. (setq i (1- i)))
  12590. ;; add the next tags
  12591. (when tags
  12592. (setq tags (org-split-string tags ":")
  12593. tags-alist
  12594. (cons (cons level tags) tags-alist)))
  12595. ;; compile tags for current headline
  12596. (setq tags-list
  12597. (if org-use-tag-inheritance
  12598. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12599. tags)
  12600. org-scanner-tags tags-list)
  12601. (when org-use-tag-inheritance
  12602. (setcdr (car tags-alist)
  12603. (mapcar (lambda (x)
  12604. (setq x (copy-sequence x))
  12605. (org-add-prop-inherited x))
  12606. (cdar tags-alist))))
  12607. (when (and tags org-use-tag-inheritance
  12608. (or (not (eq t org-use-tag-inheritance))
  12609. org-tags-exclude-from-inheritance))
  12610. ;; selective inheritance, remove uninherited ones
  12611. (setcdr (car tags-alist)
  12612. (org-remove-uninherited-tags (cdar tags-alist))))
  12613. (when (and
  12614. ;; eval matcher only when the todo condition is OK
  12615. (and (or (not todo-only) (member todo org-not-done-keywords))
  12616. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12617. (eval matcher)))
  12618. ;; Call the skipper, but return t if it does not skip,
  12619. ;; so that the `and' form continues evaluating
  12620. (progn
  12621. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12622. t)
  12623. ;; Check if timestamps are deselecting this entry
  12624. (or (not todo-only)
  12625. (and (member todo org-not-done-keywords)
  12626. (or (not org-agenda-tags-todo-honor-ignore-options)
  12627. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12628. ;; select this headline
  12629. (cond
  12630. ((eq action 'sparse-tree)
  12631. (and org-highlight-sparse-tree-matches
  12632. (org-get-heading) (match-end 0)
  12633. (org-highlight-new-match
  12634. (match-beginning 1) (match-end 1)))
  12635. (org-show-context 'tags-tree))
  12636. ((eq action 'agenda)
  12637. (setq txt (org-agenda-format-item
  12638. ""
  12639. (concat
  12640. (if (eq org-tags-match-list-sublevels 'indented)
  12641. (make-string (1- level) ?.) "")
  12642. (org-get-heading))
  12643. level category
  12644. tags-list)
  12645. priority (org-get-priority txt))
  12646. (goto-char lspos)
  12647. (setq marker (org-agenda-new-marker))
  12648. (org-add-props txt props
  12649. 'org-marker marker 'org-hd-marker marker 'org-category category
  12650. 'todo-state todo
  12651. 'priority priority 'type "tagsmatch")
  12652. (push txt rtn))
  12653. ((functionp action)
  12654. (setq org-map-continue-from nil)
  12655. (save-excursion
  12656. (setq rtn1 (funcall action))
  12657. (push rtn1 rtn)))
  12658. (t (error "Invalid action")))
  12659. ;; if we are to skip sublevels, jump to end of subtree
  12660. (unless org-tags-match-list-sublevels
  12661. (org-end-of-subtree t)
  12662. (backward-char 1))))
  12663. ;; Get the correct position from where to continue
  12664. (if org-map-continue-from
  12665. (goto-char org-map-continue-from)
  12666. (and (= (point) lspos) (end-of-line 1)))))
  12667. (when (and (eq action 'sparse-tree)
  12668. (not org-sparse-tree-open-archived-trees))
  12669. (org-hide-archived-subtrees (point-min) (point-max)))
  12670. (nreverse rtn)))
  12671. (defun org-remove-uninherited-tags (tags)
  12672. "Remove all tags that are not inherited from the list TAGS."
  12673. (cond
  12674. ((eq org-use-tag-inheritance t)
  12675. (if org-tags-exclude-from-inheritance
  12676. (org-delete-all org-tags-exclude-from-inheritance tags)
  12677. tags))
  12678. ((not org-use-tag-inheritance) nil)
  12679. ((stringp org-use-tag-inheritance)
  12680. (delq nil (mapcar
  12681. (lambda (x)
  12682. (if (and (string-match org-use-tag-inheritance x)
  12683. (not (member x org-tags-exclude-from-inheritance)))
  12684. x nil))
  12685. tags)))
  12686. ((listp org-use-tag-inheritance)
  12687. (delq nil (mapcar
  12688. (lambda (x)
  12689. (if (member x org-use-tag-inheritance) x nil))
  12690. tags)))))
  12691. (defun org-match-sparse-tree (&optional todo-only match)
  12692. "Create a sparse tree according to tags string MATCH.
  12693. MATCH can contain positive and negative selection of tags, like
  12694. \"+WORK+URGENT-WITHBOSS\".
  12695. If optional argument TODO-ONLY is non-nil, only select lines that are
  12696. also TODO lines."
  12697. (interactive "P")
  12698. (org-agenda-prepare-buffers (list (current-buffer)))
  12699. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12700. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12701. (defvar org-cached-props nil)
  12702. (defun org-cached-entry-get (pom property)
  12703. (if (or (eq t org-use-property-inheritance)
  12704. (and (stringp org-use-property-inheritance)
  12705. (string-match org-use-property-inheritance property))
  12706. (and (listp org-use-property-inheritance)
  12707. (member property org-use-property-inheritance)))
  12708. ;; Caching is not possible, check it directly
  12709. (org-entry-get pom property 'inherit)
  12710. ;; Get all properties, so that we can do complicated checks easily
  12711. (cdr (assoc property (or org-cached-props
  12712. (setq org-cached-props
  12713. (org-entry-properties pom)))))))
  12714. (defun org-global-tags-completion-table (&optional files)
  12715. "Return the list of all tags in all agenda buffer/files.
  12716. Optional FILES argument is a list of files which can be used
  12717. instead of the agenda files."
  12718. (save-excursion
  12719. (org-uniquify
  12720. (delq nil
  12721. (apply 'append
  12722. (mapcar
  12723. (lambda (file)
  12724. (set-buffer (find-file-noselect file))
  12725. (append (org-get-buffer-tags)
  12726. (mapcar (lambda (x) (if (stringp (car-safe x))
  12727. (list (car-safe x)) nil))
  12728. org-tag-alist)))
  12729. (if (and files (car files))
  12730. files
  12731. (org-agenda-files))))))))
  12732. (defun org-make-tags-matcher (match)
  12733. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12734. The variable `todo-only' is scoped dynamically into this function.
  12735. It will be set to t if the matcher restricts matching to TODO entries,
  12736. otherwise will not be touched.
  12737. Returns a cons of the selection string MATCH and the constructed
  12738. lisp form implementing the matcher. The matcher is to be evaluated
  12739. at an Org entry, with point on the headline, and returns t if the
  12740. entry matches the selection string MATCH. The returned lisp form
  12741. references two variables with information about the entry, which
  12742. must be bound around the form's evaluation: todo, the TODO keyword
  12743. at the entry (or nil of none); and tags-list, the list of all tags
  12744. at the entry including inherited ones. Additionally, the category
  12745. of the entry (if any) must be specified as the text property
  12746. 'org-category on the headline.
  12747. See also `org-scan-tags'.
  12748. "
  12749. (declare (special todo-only))
  12750. (unless (boundp 'todo-only)
  12751. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12752. (unless match
  12753. ;; Get a new match request, with completion
  12754. (let ((org-last-tags-completion-table
  12755. (org-global-tags-completion-table)))
  12756. (setq match (org-completing-read-no-i
  12757. "Match: " 'org-tags-completion-function nil nil nil
  12758. 'org-tags-history))))
  12759. ;; Parse the string and create a lisp form
  12760. (let ((match0 match)
  12761. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12762. minus tag mm
  12763. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12764. orterms term orlist re-p str-p level-p level-op time-p
  12765. prop-p pn pv po gv rest)
  12766. ;; Expand group tags
  12767. (setq match (org-tags-expand match))
  12768. (if (string-match "/+" match)
  12769. ;; match contains also a todo-matching request
  12770. (progn
  12771. (setq tagsmatch (substring match 0 (match-beginning 0))
  12772. todomatch (substring match (match-end 0)))
  12773. (if (string-match "^!" todomatch)
  12774. (setq todo-only t todomatch (substring todomatch 1)))
  12775. (if (string-match "^\\s-*$" todomatch)
  12776. (setq todomatch nil)))
  12777. ;; only matching tags
  12778. (setq tagsmatch match todomatch nil))
  12779. ;; Make the tags matcher
  12780. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12781. (setq tagsmatcher t)
  12782. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12783. (while (setq term (pop orterms))
  12784. (while (and (equal (substring term -1) "\\") orterms)
  12785. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12786. (while (string-match re term)
  12787. (setq rest (substring term (match-end 0))
  12788. minus (and (match-end 1)
  12789. (equal (match-string 1 term) "-"))
  12790. tag (save-match-data (replace-regexp-in-string
  12791. "\\\\-" "-"
  12792. (match-string 2 term)))
  12793. re-p (equal (string-to-char tag) ?{)
  12794. level-p (match-end 4)
  12795. prop-p (match-end 5)
  12796. mm (cond
  12797. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12798. (level-p
  12799. (setq level-op (org-op-to-function (match-string 3 term)))
  12800. `(,level-op level ,(string-to-number
  12801. (match-string 4 term))))
  12802. (prop-p
  12803. (setq pn (match-string 5 term)
  12804. po (match-string 6 term)
  12805. pv (match-string 7 term)
  12806. re-p (equal (string-to-char pv) ?{)
  12807. str-p (equal (string-to-char pv) ?\")
  12808. time-p (save-match-data
  12809. (string-match "^\"[[<].*[]>]\"$" pv))
  12810. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12811. (if time-p (setq pv (org-matcher-time pv)))
  12812. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12813. (cond
  12814. ((equal pn "CATEGORY")
  12815. (setq gv '(get-text-property (point) 'org-category)))
  12816. ((equal pn "TODO")
  12817. (setq gv 'todo))
  12818. (t
  12819. (setq gv `(org-cached-entry-get nil ,pn))))
  12820. (if re-p
  12821. (if (eq po 'org<>)
  12822. `(not (string-match ,pv (or ,gv "")))
  12823. `(string-match ,pv (or ,gv "")))
  12824. (if str-p
  12825. `(,po (or ,gv "") ,pv)
  12826. `(,po (string-to-number (or ,gv ""))
  12827. ,(string-to-number pv) ))))
  12828. (t `(member ,tag tags-list)))
  12829. mm (if minus (list 'not mm) mm)
  12830. term rest)
  12831. (push mm tagsmatcher))
  12832. (push (if (> (length tagsmatcher) 1)
  12833. (cons 'and tagsmatcher)
  12834. (car tagsmatcher))
  12835. orlist)
  12836. (setq tagsmatcher nil))
  12837. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12838. (setq tagsmatcher
  12839. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12840. ;; Make the todo matcher
  12841. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12842. (setq todomatcher t)
  12843. (setq orterms (org-split-string todomatch "|") orlist nil)
  12844. (while (setq term (pop orterms))
  12845. (while (string-match re term)
  12846. (setq minus (and (match-end 1)
  12847. (equal (match-string 1 term) "-"))
  12848. kwd (match-string 2 term)
  12849. re-p (equal (string-to-char kwd) ?{)
  12850. term (substring term (match-end 0))
  12851. mm (if re-p
  12852. `(string-match ,(substring kwd 1 -1) todo)
  12853. (list 'equal 'todo kwd))
  12854. mm (if minus (list 'not mm) mm))
  12855. (push mm todomatcher))
  12856. (push (if (> (length todomatcher) 1)
  12857. (cons 'and todomatcher)
  12858. (car todomatcher))
  12859. orlist)
  12860. (setq todomatcher nil))
  12861. (setq todomatcher (if (> (length orlist) 1)
  12862. (cons 'or orlist) (car orlist))))
  12863. ;; Return the string and lisp forms of the matcher
  12864. (setq matcher (if todomatcher
  12865. (list 'and tagsmatcher todomatcher)
  12866. tagsmatcher))
  12867. (when todo-only
  12868. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12869. matcher)))
  12870. (cons match0 matcher)))
  12871. (defun org-tags-expand (match &optional single-as-list downcased)
  12872. "Expand group tags in MATCH.
  12873. This replaces every group tag in MATCH with a regexp tag search.
  12874. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12875. will be replaced like this:
  12876. Work => {\(?:Work\|Lab\|Conf\}
  12877. +Work => +{\(?:Work\|Lab\|Conf\}
  12878. -Work => -{\(?:Work\|Lab\|Conf\}
  12879. Replacing by a regexp preserves the structure of the match.
  12880. E.g., this expansion
  12881. Work|Home => {\(?:Work\|Lab\|Conf\}|Home
  12882. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12883. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12884. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12885. assumed to be a single group tag, and the function will return
  12886. the list of tags in this group.
  12887. When DOWNCASE is non-nil, expand downcased TAGS."
  12888. (if org-group-tags
  12889. (let* ((case-fold-search t)
  12890. (stable org-mode-syntax-table)
  12891. (tal (or org-tag-groups-alist-for-agenda
  12892. org-tag-groups-alist))
  12893. (tal (if downcased (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12894. (tml (mapcar 'car tal))
  12895. (rtnmatch match) rpl)
  12896. ;; @ and _ are allowed as word-components in tags
  12897. (modify-syntax-entry ?@ "w" stable)
  12898. (modify-syntax-entry ?_ "w" stable)
  12899. (while (and tml (string-match
  12900. (concat "\\(?1:[+-]?\\)\\(?2:\\<" (regexp-opt tml) "\\>\\)")
  12901. rtnmatch))
  12902. (let* ((dir (match-string 1 rtnmatch))
  12903. (tag (match-string 2 rtnmatch))
  12904. (tag (if downcased (downcase tag) tag)))
  12905. (setq tml (delete tag tml))
  12906. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12907. (setq rtnmatch
  12908. (replace-match
  12909. (concat dir "{" (regexp-opt rpl) "}") t t rtnmatch))))
  12910. (if single-as-list
  12911. (or (reverse rpl) (list rtnmatch))
  12912. rtnmatch))
  12913. (if single-as-list (list (if downcased (downcase match) match))
  12914. match)))
  12915. (defun org-op-to-function (op &optional stringp)
  12916. "Turn an operator into the appropriate function."
  12917. (setq op
  12918. (cond
  12919. ((equal op "<" ) '(< string< org-time<))
  12920. ((equal op ">" ) '(> org-string> org-time>))
  12921. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12922. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12923. ((member op '("=" "==")) '(= string= org-time=))
  12924. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12925. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12926. (defun org<> (a b) (not (= a b)))
  12927. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12928. (defun org-string>= (a b) (not (string< a b)))
  12929. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12930. (defun org-string<> (a b) (not (string= a b)))
  12931. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12932. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12933. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12934. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12935. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12936. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12937. (defun org-2ft (s)
  12938. "Convert S to a floating point time.
  12939. If S is already a number, just return it. If it is a string, parse
  12940. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12941. (cond
  12942. ((numberp s) s)
  12943. ((stringp s)
  12944. (condition-case nil
  12945. (float-time (apply 'encode-time (org-parse-time-string s)))
  12946. (error 0.)))
  12947. (t 0.)))
  12948. (defun org-time-today ()
  12949. "Time in seconds today at 0:00.
  12950. Returns the float number of seconds since the beginning of the
  12951. epoch to the beginning of today (00:00)."
  12952. (float-time (apply 'encode-time
  12953. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12954. (defun org-matcher-time (s)
  12955. "Interpret a time comparison value."
  12956. (save-match-data
  12957. (cond
  12958. ((string= s "<now>") (float-time))
  12959. ((string= s "<today>") (org-time-today))
  12960. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12961. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12962. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12963. (+ (org-time-today)
  12964. (* (string-to-number (match-string 1 s))
  12965. (cdr (assoc (match-string 2 s)
  12966. '(("d" . 86400.0) ("w" . 604800.0)
  12967. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12968. (t (org-2ft s)))))
  12969. (defun org-match-any-p (re list)
  12970. "Does re match any element of list?"
  12971. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12972. (delq nil list))
  12973. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12974. (defvar org-tags-overlay (make-overlay 1 1))
  12975. (org-detach-overlay org-tags-overlay)
  12976. (defun org-get-local-tags-at (&optional pos)
  12977. "Get a list of tags defined in the current headline."
  12978. (org-get-tags-at pos 'local))
  12979. (defun org-get-local-tags ()
  12980. "Get a list of tags defined in the current headline."
  12981. (org-get-tags-at nil 'local))
  12982. (defun org-get-tags-at (&optional pos local)
  12983. "Get a list of all headline tags applicable at POS.
  12984. POS defaults to point. If tags are inherited, the list contains
  12985. the targets in the same sequence as the headlines appear, i.e.
  12986. the tags of the current headline come last.
  12987. When LOCAL is non-nil, only return tags from the current headline,
  12988. ignore inherited ones."
  12989. (interactive)
  12990. (if (and org-trust-scanner-tags
  12991. (or (not pos) (equal pos (point)))
  12992. (not local))
  12993. org-scanner-tags
  12994. (let (tags ltags lastpos parent)
  12995. (save-excursion
  12996. (save-restriction
  12997. (widen)
  12998. (goto-char (or pos (point)))
  12999. (save-match-data
  13000. (catch 'done
  13001. (condition-case nil
  13002. (progn
  13003. (org-back-to-heading t)
  13004. (while (not (equal lastpos (point)))
  13005. (setq lastpos (point))
  13006. (when (looking-at
  13007. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13008. (setq ltags (org-split-string
  13009. (org-match-string-no-properties 1) ":"))
  13010. (when parent
  13011. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13012. (setq tags (append
  13013. (if parent
  13014. (org-remove-uninherited-tags ltags)
  13015. ltags)
  13016. tags)))
  13017. (or org-use-tag-inheritance (throw 'done t))
  13018. (if local (throw 'done t))
  13019. (or (org-up-heading-safe) (error nil))
  13020. (setq parent t)))
  13021. (error nil)))))
  13022. (if local
  13023. tags
  13024. (reverse (delete-dups
  13025. (reverse (append
  13026. (org-remove-uninherited-tags
  13027. org-file-tags) tags)))))))))
  13028. (defun org-add-prop-inherited (s)
  13029. (add-text-properties 0 (length s) '(inherited t) s)
  13030. s)
  13031. (defun org-toggle-tag (tag &optional onoff)
  13032. "Toggle the tag TAG for the current line.
  13033. If ONOFF is `on' or `off', don't toggle but set to this state."
  13034. (let (res current)
  13035. (save-excursion
  13036. (org-back-to-heading t)
  13037. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13038. (point-at-eol) t)
  13039. (progn
  13040. (setq current (match-string 1))
  13041. (replace-match ""))
  13042. (setq current ""))
  13043. (setq current (nreverse (org-split-string current ":")))
  13044. (cond
  13045. ((eq onoff 'on)
  13046. (setq res t)
  13047. (or (member tag current) (push tag current)))
  13048. ((eq onoff 'off)
  13049. (or (not (member tag current)) (setq current (delete tag current))))
  13050. (t (if (member tag current)
  13051. (setq current (delete tag current))
  13052. (setq res t)
  13053. (push tag current))))
  13054. (end-of-line 1)
  13055. (if current
  13056. (progn
  13057. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13058. (org-set-tags nil t))
  13059. (delete-horizontal-space))
  13060. (run-hooks 'org-after-tags-change-hook))
  13061. res))
  13062. (defun org-align-tags-here (to-col)
  13063. ;; Assumes that this is a headline
  13064. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13065. (beginning-of-line 1)
  13066. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13067. (< pos (match-beginning 2)))
  13068. (progn
  13069. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13070. (goto-char (match-beginning 1))
  13071. (insert " ")
  13072. (delete-region (point) (1+ (match-beginning 2)))
  13073. (setq ncol (max (current-column)
  13074. (1+ col)
  13075. (if (> to-col 0)
  13076. to-col
  13077. (- (abs to-col) tags-l))))
  13078. (setq p (point))
  13079. (insert (make-string (- ncol (current-column)) ?\ ))
  13080. (setq ncol (current-column))
  13081. (when indent-tabs-mode (tabify p (point-at-eol)))
  13082. (org-move-to-column (min ncol col) t))
  13083. (goto-char pos))))
  13084. (defun org-set-tags-command (&optional arg just-align)
  13085. "Call the set-tags command for the current entry."
  13086. (interactive "P")
  13087. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13088. (org-set-tags arg just-align)
  13089. (save-excursion
  13090. (unless (and (org-region-active-p)
  13091. org-loop-over-headlines-in-active-region)
  13092. (org-back-to-heading t))
  13093. (org-set-tags arg just-align))))
  13094. (defun org-set-tags-to (data)
  13095. "Set the tags of the current entry to DATA, replacing the current tags.
  13096. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13097. If DATA is nil or the empty string, any tags will be removed."
  13098. (interactive "sTags: ")
  13099. (setq data
  13100. (cond
  13101. ((eq data nil) "")
  13102. ((equal data "") "")
  13103. ((stringp data)
  13104. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13105. ":"))
  13106. ((listp data)
  13107. (concat ":" (mapconcat 'identity data ":") ":"))))
  13108. (when data
  13109. (save-excursion
  13110. (org-back-to-heading t)
  13111. (when (looking-at org-complex-heading-regexp)
  13112. (if (match-end 5)
  13113. (progn
  13114. (goto-char (match-beginning 5))
  13115. (insert data)
  13116. (delete-region (point) (point-at-eol))
  13117. (org-set-tags nil 'align))
  13118. (goto-char (point-at-eol))
  13119. (insert " " data)
  13120. (org-set-tags nil 'align)))
  13121. (beginning-of-line 1)
  13122. (if (looking-at ".*?\\([ \t]+\\)$")
  13123. (delete-region (match-beginning 1) (match-end 1))))))
  13124. (defun org-align-all-tags ()
  13125. "Align the tags i all headings."
  13126. (interactive)
  13127. (save-excursion
  13128. (or (ignore-errors (org-back-to-heading t))
  13129. (outline-next-heading))
  13130. (if (org-at-heading-p)
  13131. (org-set-tags t)
  13132. (message "No headings"))))
  13133. (defvar org-indent-indentation-per-level)
  13134. (defun org-set-tags (&optional arg just-align)
  13135. "Set the tags for the current headline.
  13136. With prefix ARG, realign all tags in headings in the current buffer."
  13137. (interactive "P")
  13138. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13139. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13140. 'region-start-level 'region))
  13141. org-loop-over-headlines-in-active-region)
  13142. (org-map-entries
  13143. ;; We don't use ARG and JUST-ALIGN here these args are not
  13144. ;; useful when looping over headlines
  13145. `(org-set-tags)
  13146. org-loop-over-headlines-in-active-region
  13147. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13148. (let* ((re org-outline-regexp-bol)
  13149. (current (unless arg (org-get-tags-string)))
  13150. (col (current-column))
  13151. (org-setting-tags t)
  13152. table current-tags inherited-tags ; computed below when needed
  13153. tags p0 c0 c1 rpl di tc level)
  13154. (if arg
  13155. (save-excursion
  13156. (goto-char (point-min))
  13157. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13158. (while (re-search-forward re nil t)
  13159. (org-set-tags nil t)
  13160. (end-of-line 1)))
  13161. (message "All tags realigned to column %d" org-tags-column))
  13162. (if just-align
  13163. (setq tags current)
  13164. ;; Get a new set of tags from the user
  13165. (save-excursion
  13166. (setq table (append org-tag-persistent-alist
  13167. (or org-tag-alist (org-get-buffer-tags))
  13168. (and
  13169. org-complete-tags-always-offer-all-agenda-tags
  13170. (org-global-tags-completion-table
  13171. (org-agenda-files))))
  13172. org-last-tags-completion-table table
  13173. current-tags (org-split-string current ":")
  13174. inherited-tags (nreverse
  13175. (nthcdr (length current-tags)
  13176. (nreverse (org-get-tags-at))))
  13177. tags
  13178. (if (or (eq t org-use-fast-tag-selection)
  13179. (and org-use-fast-tag-selection
  13180. (delq nil (mapcar 'cdr table))))
  13181. (org-fast-tag-selection
  13182. current-tags inherited-tags table
  13183. (if org-fast-tag-selection-include-todo
  13184. org-todo-key-alist))
  13185. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13186. (org-trim
  13187. (org-icompleting-read "Tags: "
  13188. 'org-tags-completion-function
  13189. nil nil current 'org-tags-history))))))
  13190. (while (string-match "[-+&]+" tags)
  13191. ;; No boolean logic, just a list
  13192. (setq tags (replace-match ":" t t tags))))
  13193. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13194. (if org-tags-sort-function
  13195. (setq tags (mapconcat 'identity
  13196. (sort (org-split-string
  13197. tags (org-re "[^[:alnum:]_@#%]+"))
  13198. org-tags-sort-function) ":")))
  13199. (if (string-match "\\`[\t ]*\\'" tags)
  13200. (setq tags "")
  13201. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13202. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13203. ;; Insert new tags at the correct column
  13204. (beginning-of-line 1)
  13205. (setq level (or (and (looking-at org-outline-regexp)
  13206. (- (match-end 0) (point) 1))
  13207. 1))
  13208. (cond
  13209. ((and (equal current "") (equal tags "")))
  13210. ((re-search-forward
  13211. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13212. (point-at-eol) t)
  13213. (if (equal tags "")
  13214. (setq rpl "")
  13215. (goto-char (match-beginning 0))
  13216. (setq c0 (current-column)
  13217. ;; compute offset for the case of org-indent-mode active
  13218. di (if org-indent-mode
  13219. (* (1- org-indent-indentation-per-level) (1- level))
  13220. 0)
  13221. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13222. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13223. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13224. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13225. (replace-match rpl t t)
  13226. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13227. tags)
  13228. (t (error "Tags alignment failed")))
  13229. (org-move-to-column col)
  13230. (unless just-align
  13231. (run-hooks 'org-after-tags-change-hook))))))
  13232. (defun org-change-tag-in-region (beg end tag off)
  13233. "Add or remove TAG for each entry in the region.
  13234. This works in the agenda, and also in an org-mode buffer."
  13235. (interactive
  13236. (list (region-beginning) (region-end)
  13237. (let ((org-last-tags-completion-table
  13238. (if (derived-mode-p 'org-mode)
  13239. (org-get-buffer-tags)
  13240. (org-global-tags-completion-table))))
  13241. (org-icompleting-read
  13242. "Tag: " 'org-tags-completion-function nil nil nil
  13243. 'org-tags-history))
  13244. (progn
  13245. (message "[s]et or [r]emove? ")
  13246. (equal (read-char-exclusive) ?r))))
  13247. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13248. (let ((agendap (equal major-mode 'org-agenda-mode))
  13249. l1 l2 m buf pos newhead (cnt 0))
  13250. (goto-char end)
  13251. (setq l2 (1- (org-current-line)))
  13252. (goto-char beg)
  13253. (setq l1 (org-current-line))
  13254. (loop for l from l1 to l2 do
  13255. (org-goto-line l)
  13256. (setq m (get-text-property (point) 'org-hd-marker))
  13257. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13258. (and agendap m))
  13259. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13260. pos (if agendap m (point)))
  13261. (with-current-buffer buf
  13262. (save-excursion
  13263. (save-restriction
  13264. (goto-char pos)
  13265. (setq cnt (1+ cnt))
  13266. (org-toggle-tag tag (if off 'off 'on))
  13267. (setq newhead (org-get-heading)))))
  13268. (and agendap (org-agenda-change-all-lines newhead m))))
  13269. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13270. (defun org-tags-completion-function (string predicate &optional flag)
  13271. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13272. (confirm (lambda (x) (stringp (car x)))))
  13273. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13274. (setq s1 (match-string 1 string)
  13275. s2 (match-string 2 string))
  13276. (setq s1 "" s2 string))
  13277. (cond
  13278. ((eq flag nil)
  13279. ;; try completion
  13280. (setq rtn (try-completion s2 ctable confirm))
  13281. (if (stringp rtn)
  13282. (setq rtn
  13283. (concat s1 s2 (substring rtn (length s2))
  13284. (if (and org-add-colon-after-tag-completion
  13285. (assoc rtn ctable))
  13286. ":" ""))))
  13287. rtn)
  13288. ((eq flag t)
  13289. ;; all-completions
  13290. (all-completions s2 ctable confirm))
  13291. ((eq flag 'lambda)
  13292. ;; exact match?
  13293. (assoc s2 ctable)))))
  13294. (defun org-fast-tag-insert (kwd tags face &optional end)
  13295. "Insert KDW, and the TAGS, the latter with face FACE.
  13296. Also insert END."
  13297. (insert (format "%-12s" (concat kwd ":"))
  13298. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13299. (or end "")))
  13300. (defun org-fast-tag-show-exit (flag)
  13301. (save-excursion
  13302. (org-goto-line 3)
  13303. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13304. (replace-match ""))
  13305. (when flag
  13306. (end-of-line 1)
  13307. (org-move-to-column (- (window-width) 19) t)
  13308. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13309. (defun org-set-current-tags-overlay (current prefix)
  13310. "Add an overlay to CURRENT tag with PREFIX."
  13311. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13312. (if (featurep 'xemacs)
  13313. (org-overlay-display org-tags-overlay (concat prefix s)
  13314. 'secondary-selection)
  13315. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13316. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13317. (defvar org-last-tag-selection-key nil)
  13318. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13319. "Fast tag selection with single keys.
  13320. CURRENT is the current list of tags in the headline, INHERITED is the
  13321. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13322. possibly with grouping information. TODO-TABLE is a similar table with
  13323. TODO keywords, should these have keys assigned to them.
  13324. If the keys are nil, a-z are automatically assigned.
  13325. Returns the new tags string, or nil to not change the current settings."
  13326. (let* ((fulltable (append table todo-table))
  13327. (maxlen (apply 'max (mapcar
  13328. (lambda (x)
  13329. (if (stringp (car x)) (string-width (car x)) 0))
  13330. fulltable)))
  13331. (buf (current-buffer))
  13332. (expert (eq org-fast-tag-selection-single-key 'expert))
  13333. (buffer-tags nil)
  13334. (fwidth (+ maxlen 3 1 3))
  13335. (ncol (/ (- (window-width) 4) fwidth))
  13336. (i-face 'org-done)
  13337. (c-face 'org-todo)
  13338. tg cnt e c char c1 c2 ntable tbl rtn
  13339. ov-start ov-end ov-prefix
  13340. (exit-after-next org-fast-tag-selection-single-key)
  13341. (done-keywords org-done-keywords)
  13342. groups ingroup)
  13343. (save-excursion
  13344. (beginning-of-line 1)
  13345. (if (looking-at
  13346. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13347. (setq ov-start (match-beginning 1)
  13348. ov-end (match-end 1)
  13349. ov-prefix "")
  13350. (setq ov-start (1- (point-at-eol))
  13351. ov-end (1+ ov-start))
  13352. (skip-chars-forward "^\n\r")
  13353. (setq ov-prefix
  13354. (concat
  13355. (buffer-substring (1- (point)) (point))
  13356. (if (> (current-column) org-tags-column)
  13357. " "
  13358. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13359. (move-overlay org-tags-overlay ov-start ov-end)
  13360. (save-window-excursion
  13361. (if expert
  13362. (set-buffer (get-buffer-create " *Org tags*"))
  13363. (delete-other-windows)
  13364. (split-window-vertically)
  13365. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13366. (erase-buffer)
  13367. (org-set-local 'org-done-keywords done-keywords)
  13368. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13369. (org-fast-tag-insert "Current" current c-face "\n\n")
  13370. (org-fast-tag-show-exit exit-after-next)
  13371. (org-set-current-tags-overlay current ov-prefix)
  13372. (setq tbl fulltable char ?a cnt 0)
  13373. (while (setq e (pop tbl))
  13374. (cond
  13375. ((equal (car e) :startgroup)
  13376. (push '() groups) (setq ingroup t)
  13377. (when (not (= cnt 0))
  13378. (setq cnt 0)
  13379. (insert "\n"))
  13380. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13381. ((equal (car e) :endgroup)
  13382. (setq ingroup nil cnt 0)
  13383. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13384. ((equal e '(:newline))
  13385. (when (not (= cnt 0))
  13386. (setq cnt 0)
  13387. (insert "\n")
  13388. (setq e (car tbl))
  13389. (while (equal (car tbl) '(:newline))
  13390. (insert "\n")
  13391. (setq tbl (cdr tbl)))))
  13392. ((equal e '(:grouptags)) nil)
  13393. (t
  13394. (setq tg (copy-sequence (car e)) c2 nil)
  13395. (if (cdr e)
  13396. (setq c (cdr e))
  13397. ;; automatically assign a character.
  13398. (setq c1 (string-to-char
  13399. (downcase (substring
  13400. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13401. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13402. (while (or (rassoc char ntable) (rassoc char table))
  13403. (setq char (1+ char)))
  13404. (setq c2 c1))
  13405. (setq c (or c2 char)))
  13406. (if ingroup (push tg (car groups)))
  13407. (setq tg (org-add-props tg nil 'face
  13408. (cond
  13409. ((not (assoc tg table))
  13410. (org-get-todo-face tg))
  13411. ((member tg current) c-face)
  13412. ((member tg inherited) i-face))))
  13413. (if (equal (caar tbl) :grouptags)
  13414. (org-add-props tg nil 'face 'org-tag-group))
  13415. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13416. (insert "[" c "] " tg (make-string
  13417. (- fwidth 4 (length tg)) ?\ ))
  13418. (push (cons tg c) ntable)
  13419. (when (= (setq cnt (1+ cnt)) ncol)
  13420. (insert "\n")
  13421. (if ingroup (insert " "))
  13422. (setq cnt 0)))))
  13423. (setq ntable (nreverse ntable))
  13424. (insert "\n")
  13425. (goto-char (point-min))
  13426. (if (not expert) (org-fit-window-to-buffer))
  13427. (setq rtn
  13428. (catch 'exit
  13429. (while t
  13430. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13431. (if (not groups) "no " "")
  13432. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13433. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13434. (setq org-last-tag-selection-key c)
  13435. (cond
  13436. ((= c ?\r) (throw 'exit t))
  13437. ((= c ?!)
  13438. (setq groups (not groups))
  13439. (goto-char (point-min))
  13440. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13441. ((= c ?\C-c)
  13442. (if (not expert)
  13443. (org-fast-tag-show-exit
  13444. (setq exit-after-next (not exit-after-next)))
  13445. (setq expert nil)
  13446. (delete-other-windows)
  13447. (set-window-buffer (split-window-vertically) " *Org tags*")
  13448. (org-switch-to-buffer-other-window " *Org tags*")
  13449. (org-fit-window-to-buffer)))
  13450. ((or (= c ?\C-g)
  13451. (and (= c ?q) (not (rassoc c ntable))))
  13452. (org-detach-overlay org-tags-overlay)
  13453. (setq quit-flag t))
  13454. ((= c ?\ )
  13455. (setq current nil)
  13456. (if exit-after-next (setq exit-after-next 'now)))
  13457. ((= c ?\t)
  13458. (condition-case nil
  13459. (setq tg (org-icompleting-read
  13460. "Tag: "
  13461. (or buffer-tags
  13462. (with-current-buffer buf
  13463. (org-get-buffer-tags)))))
  13464. (quit (setq tg "")))
  13465. (when (string-match "\\S-" tg)
  13466. (add-to-list 'buffer-tags (list tg))
  13467. (if (member tg current)
  13468. (setq current (delete tg current))
  13469. (push tg current)))
  13470. (if exit-after-next (setq exit-after-next 'now)))
  13471. ((setq e (rassoc c todo-table) tg (car e))
  13472. (with-current-buffer buf
  13473. (save-excursion (org-todo tg)))
  13474. (if exit-after-next (setq exit-after-next 'now)))
  13475. ((setq e (rassoc c ntable) tg (car e))
  13476. (if (member tg current)
  13477. (setq current (delete tg current))
  13478. (loop for g in groups do
  13479. (if (member tg g)
  13480. (mapc (lambda (x)
  13481. (setq current (delete x current)))
  13482. g)))
  13483. (push tg current))
  13484. (if exit-after-next (setq exit-after-next 'now))))
  13485. ;; Create a sorted list
  13486. (setq current
  13487. (sort current
  13488. (lambda (a b)
  13489. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13490. (if (eq exit-after-next 'now) (throw 'exit t))
  13491. (goto-char (point-min))
  13492. (beginning-of-line 2)
  13493. (delete-region (point) (point-at-eol))
  13494. (org-fast-tag-insert "Current" current c-face)
  13495. (org-set-current-tags-overlay current ov-prefix)
  13496. (while (re-search-forward
  13497. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13498. (setq tg (match-string 1))
  13499. (add-text-properties
  13500. (match-beginning 1) (match-end 1)
  13501. (list 'face
  13502. (cond
  13503. ((member tg current) c-face)
  13504. ((member tg inherited) i-face)
  13505. (t (get-text-property (match-beginning 1) 'face))))))
  13506. (goto-char (point-min)))))
  13507. (org-detach-overlay org-tags-overlay)
  13508. (if rtn
  13509. (mapconcat 'identity current ":")
  13510. nil))))
  13511. (defun org-get-tags-string ()
  13512. "Get the TAGS string in the current headline."
  13513. (unless (org-at-heading-p t)
  13514. (error "Not on a heading"))
  13515. (save-excursion
  13516. (beginning-of-line 1)
  13517. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13518. (org-match-string-no-properties 1)
  13519. "")))
  13520. (defun org-get-tags ()
  13521. "Get the list of tags specified in the current headline."
  13522. (org-split-string (org-get-tags-string) ":"))
  13523. (defun org-get-buffer-tags ()
  13524. "Get a table of all tags used in the buffer, for completion."
  13525. (let (tags)
  13526. (save-excursion
  13527. (goto-char (point-min))
  13528. (while (re-search-forward
  13529. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13530. (when (equal (char-after (point-at-bol 0)) ?*)
  13531. (mapc (lambda (x) (add-to-list 'tags x))
  13532. (org-split-string (org-match-string-no-properties 1) ":")))))
  13533. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13534. (mapcar 'list tags)))
  13535. ;;;; The mapping API
  13536. (defun org-map-entries (func &optional match scope &rest skip)
  13537. "Call FUNC at each headline selected by MATCH in SCOPE.
  13538. FUNC is a function or a lisp form. The function will be called without
  13539. arguments, with the cursor positioned at the beginning of the headline.
  13540. The return values of all calls to the function will be collected and
  13541. returned as a list.
  13542. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13543. does not need to preserve point. After evaluation, the cursor will be
  13544. moved to the end of the line (presumably of the headline of the
  13545. processed entry) and search continues from there. Under some
  13546. circumstances, this may not produce the wanted results. For example,
  13547. if you have removed (e.g. archived) the current (sub)tree it could
  13548. mean that the next entry will be skipped entirely. In such cases, you
  13549. can specify the position from where search should continue by making
  13550. FUNC set the variable `org-map-continue-from' to the desired buffer
  13551. position.
  13552. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13553. Only headlines that are matched by this query will be considered during
  13554. the iteration. When MATCH is nil or t, all headlines will be
  13555. visited by the iteration.
  13556. SCOPE determines the scope of this command. It can be any of:
  13557. nil The current buffer, respecting the restriction if any
  13558. tree The subtree started with the entry at point
  13559. region The entries within the active region, if any
  13560. region-start-level
  13561. The entries within the active region, but only those at
  13562. the same level than the first one.
  13563. file The current buffer, without restriction
  13564. file-with-archives
  13565. The current buffer, and any archives associated with it
  13566. agenda All agenda files
  13567. agenda-with-archives
  13568. All agenda files with any archive files associated with them
  13569. \(file1 file2 ...)
  13570. If this is a list, all files in the list will be scanned
  13571. The remaining args are treated as settings for the skipping facilities of
  13572. the scanner. The following items can be given here:
  13573. archive skip trees with the archive tag
  13574. comment skip trees with the COMMENT keyword
  13575. function or Emacs Lisp form:
  13576. will be used as value for `org-agenda-skip-function', so
  13577. whenever the function returns a position, FUNC will not be
  13578. called for that entry and search will continue from the
  13579. position returned
  13580. If your function needs to retrieve the tags including inherited tags
  13581. at the *current* entry, you can use the value of the variable
  13582. `org-scanner-tags' which will be much faster than getting the value
  13583. with `org-get-tags-at'. If your function gets properties with
  13584. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13585. to t around the call to `org-entry-properties' to get the same speedup.
  13586. Note that if your function moves around to retrieve tags and properties at
  13587. a *different* entry, you cannot use these techniques."
  13588. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13589. (not (org-region-active-p)))
  13590. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13591. (org-agenda-skip-archived-trees (memq 'archive skip))
  13592. (org-agenda-skip-comment-trees (memq 'comment skip))
  13593. (org-agenda-skip-function
  13594. (car (org-delete-all '(comment archive) skip)))
  13595. (org-tags-match-list-sublevels t)
  13596. (start-level (eq scope 'region-start-level))
  13597. matcher file res
  13598. org-todo-keywords-for-agenda
  13599. org-done-keywords-for-agenda
  13600. org-todo-keyword-alist-for-agenda
  13601. org-drawers-for-agenda
  13602. org-tag-alist-for-agenda
  13603. todo-only)
  13604. (cond
  13605. ((eq match t) (setq matcher t))
  13606. ((eq match nil) (setq matcher t))
  13607. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13608. (save-window-excursion
  13609. (save-restriction
  13610. (cond ((eq scope 'tree)
  13611. (org-back-to-heading t)
  13612. (org-narrow-to-subtree)
  13613. (setq scope nil))
  13614. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13615. (org-region-active-p))
  13616. ;; If needed, set start-level to a string like "2"
  13617. (when start-level
  13618. (save-excursion
  13619. (goto-char (region-beginning))
  13620. (unless (org-at-heading-p) (outline-next-heading))
  13621. (setq start-level (org-current-level))))
  13622. (narrow-to-region (region-beginning)
  13623. (save-excursion
  13624. (goto-char (region-end))
  13625. (unless (and (bolp) (org-at-heading-p))
  13626. (outline-next-heading))
  13627. (point)))
  13628. (setq scope nil)))
  13629. (if (not scope)
  13630. (progn
  13631. (org-agenda-prepare-buffers
  13632. (list (buffer-file-name (current-buffer))))
  13633. (setq res (org-scan-tags func matcher todo-only start-level)))
  13634. ;; Get the right scope
  13635. (cond
  13636. ((and scope (listp scope) (symbolp (car scope)))
  13637. (setq scope (eval scope)))
  13638. ((eq scope 'agenda)
  13639. (setq scope (org-agenda-files t)))
  13640. ((eq scope 'agenda-with-archives)
  13641. (setq scope (org-agenda-files t))
  13642. (setq scope (org-add-archive-files scope)))
  13643. ((eq scope 'file)
  13644. (setq scope (list (buffer-file-name))))
  13645. ((eq scope 'file-with-archives)
  13646. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13647. (org-agenda-prepare-buffers scope)
  13648. (while (setq file (pop scope))
  13649. (with-current-buffer (org-find-base-buffer-visiting file)
  13650. (save-excursion
  13651. (save-restriction
  13652. (widen)
  13653. (goto-char (point-min))
  13654. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13655. res)))
  13656. ;;;; Properties
  13657. ;;; Setting and retrieving properties
  13658. (defconst org-special-properties
  13659. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13660. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13661. "The special properties valid in Org-mode.
  13662. These are properties that are not defined in the property drawer,
  13663. but in some other way.")
  13664. (defconst org-default-properties
  13665. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13666. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13667. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13668. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13669. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13670. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13671. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13672. "Some properties that are used by Org-mode for various purposes.
  13673. Being in this list makes sure that they are offered for completion.")
  13674. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13675. "Regular expression matching the first line of a property drawer.")
  13676. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13677. "Regular expression matching the last line of a property drawer.")
  13678. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13679. "Regular expression matching the first line of a property drawer.")
  13680. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13681. "Regular expression matching the first line of a property drawer.")
  13682. (defconst org-property-drawer-re
  13683. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13684. org-property-end-re "\\)\n?")
  13685. "Matches an entire property drawer.")
  13686. (defconst org-clock-drawer-re
  13687. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13688. org-property-end-re "\\)\n?")
  13689. "Matches an entire clock drawer.")
  13690. (defsubst org-re-property (property)
  13691. "Return a regexp matching a PROPERTY line.
  13692. Match group 1 will be set to the value."
  13693. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13694. (defsubst org-re-property-keyword (property)
  13695. "Return a regexp matching a PROPERTY line, possibly with no
  13696. value for the property."
  13697. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13698. (defun org-property-action ()
  13699. "Do an action on properties."
  13700. (interactive)
  13701. (let (c)
  13702. (org-at-property-p)
  13703. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13704. (setq c (read-char-exclusive))
  13705. (cond
  13706. ((equal c ?s)
  13707. (call-interactively 'org-set-property))
  13708. ((equal c ?d)
  13709. (call-interactively 'org-delete-property))
  13710. ((equal c ?D)
  13711. (call-interactively 'org-delete-property-globally))
  13712. ((equal c ?c)
  13713. (call-interactively 'org-compute-property-at-point))
  13714. (t (error "No such property action %c" c)))))
  13715. (defun org-inc-effort ()
  13716. "Increment the value of the effort property in the current entry."
  13717. (interactive)
  13718. (org-set-effort nil t))
  13719. (defvar org-clock-effort) ;; Defined in org-clock.el
  13720. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13721. (defun org-set-effort (&optional value increment)
  13722. "Set the effort property of the current entry.
  13723. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13724. 10th allowed value.
  13725. When INCREMENT is non-nil, set the property to the next allowed value."
  13726. (interactive "P")
  13727. (if (equal value 0) (setq value 10))
  13728. (let* ((completion-ignore-case t)
  13729. (prop org-effort-property)
  13730. (cur (org-entry-get nil prop))
  13731. (allowed (org-property-get-allowed-values nil prop 'table))
  13732. (existing (mapcar 'list (org-property-values prop)))
  13733. (heading (nth 4 (org-heading-components)))
  13734. rpl
  13735. (val (cond
  13736. ((stringp value) value)
  13737. ((and allowed (integerp value))
  13738. (or (car (nth (1- value) allowed))
  13739. (car (org-last allowed))))
  13740. ((and allowed increment)
  13741. (or (caadr (member (list cur) allowed))
  13742. (error "Allowed effort values are not set")))
  13743. (allowed
  13744. (message "Select 1-9,0, [RET%s]: %s"
  13745. (if cur (concat "=" cur) "")
  13746. (mapconcat 'car allowed " "))
  13747. (setq rpl (read-char-exclusive))
  13748. (if (equal rpl ?\r)
  13749. cur
  13750. (setq rpl (- rpl ?0))
  13751. (if (equal rpl 0) (setq rpl 10))
  13752. (if (and (> rpl 0) (<= rpl (length allowed)))
  13753. (car (nth (1- rpl) allowed))
  13754. (org-completing-read "Effort: " allowed nil))))
  13755. (t
  13756. (let (org-completion-use-ido org-completion-use-iswitchb)
  13757. (org-completing-read
  13758. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13759. (concat "[" cur "]") "")
  13760. ": ")
  13761. existing nil nil "" nil cur))))))
  13762. (unless (equal (org-entry-get nil prop) val)
  13763. (org-entry-put nil prop val))
  13764. (save-excursion
  13765. (org-back-to-heading t)
  13766. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13767. (when (string= heading org-clock-current-task)
  13768. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13769. (org-clock-update-mode-line))
  13770. (message "%s is now %s" prop val)))
  13771. (defun org-at-property-p ()
  13772. "Is cursor inside a property drawer?"
  13773. (save-excursion
  13774. (beginning-of-line 1)
  13775. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13776. (save-match-data ;; Used by calling procedures
  13777. (let ((p (point))
  13778. (range (unless (org-before-first-heading-p)
  13779. (org-get-property-block))))
  13780. (and range (<= (car range) p) (< p (cdr range))))))))
  13781. (defun org-get-property-block (&optional beg end force)
  13782. "Return the (beg . end) range of the body of the property drawer.
  13783. BEG and END are the beginning and end of the current subtree, or of
  13784. the part before the first headline. If they are not given, they will
  13785. be found. If the drawer does not exist and FORCE is non-nil, create
  13786. the drawer."
  13787. (catch 'exit
  13788. (save-excursion
  13789. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13790. (progn (org-back-to-heading t) (point))))
  13791. (end (or end (and (not (outline-next-heading)) (point-max))
  13792. (point))))
  13793. (goto-char beg)
  13794. (if (re-search-forward org-property-start-re end t)
  13795. (setq beg (1+ (match-end 0)))
  13796. (if force
  13797. (save-excursion
  13798. (org-insert-property-drawer)
  13799. (setq end (progn (outline-next-heading) (point))))
  13800. (throw 'exit nil))
  13801. (goto-char beg)
  13802. (if (re-search-forward org-property-start-re end t)
  13803. (setq beg (1+ (match-end 0)))))
  13804. (if (re-search-forward org-property-end-re end t)
  13805. (setq end (match-beginning 0))
  13806. (or force (throw 'exit nil))
  13807. (goto-char beg)
  13808. (setq end beg)
  13809. (org-indent-line)
  13810. (insert ":END:\n"))
  13811. (cons beg end)))))
  13812. (defun org-entry-properties (&optional pom which specific)
  13813. "Get all properties of the entry at point-or-marker POM.
  13814. This includes the TODO keyword, the tags, time strings for deadline,
  13815. scheduled, and clocking, and any additional properties defined in the
  13816. entry. The return value is an alist, keys may occur multiple times
  13817. if the property key was used several times.
  13818. POM may also be nil, in which case the current entry is used.
  13819. If WHICH is nil or `all', get all properties. If WHICH is
  13820. `special' or `standard', only get that subclass. If WHICH
  13821. is a string only get exactly this property. SPECIFIC can be a string, the
  13822. specific property we are interested in. Specifying it can speed
  13823. things up because then unnecessary parsing is avoided."
  13824. (setq which (or which 'all))
  13825. (org-with-point-at pom
  13826. (let ((clockstr (substring org-clock-string 0 -1))
  13827. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13828. (case-fold-search nil)
  13829. beg end range props sum-props key key1 value string clocksum clocksumt)
  13830. (save-excursion
  13831. (when (condition-case nil
  13832. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13833. (error nil))
  13834. (setq beg (point))
  13835. (setq sum-props (get-text-property (point) 'org-summaries))
  13836. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13837. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13838. (outline-next-heading)
  13839. (setq end (point))
  13840. (when (memq which '(all special))
  13841. ;; Get the special properties, like TODO and tags
  13842. (goto-char beg)
  13843. (when (and (or (not specific) (string= specific "TODO"))
  13844. (looking-at org-todo-line-regexp) (match-end 2))
  13845. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13846. (when (and (or (not specific) (string= specific "PRIORITY"))
  13847. (looking-at org-priority-regexp))
  13848. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13849. (when (or (not specific) (string= specific "FILE"))
  13850. (push (cons "FILE" buffer-file-name) props))
  13851. (when (and (or (not specific) (string= specific "TAGS"))
  13852. (setq value (org-get-tags-string))
  13853. (string-match "\\S-" value))
  13854. (push (cons "TAGS" value) props))
  13855. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13856. (setq value (org-get-tags-at)))
  13857. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13858. ":"))
  13859. props))
  13860. (when (or (not specific) (string= specific "BLOCKED"))
  13861. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13862. (when (or (not specific)
  13863. (member specific
  13864. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13865. "TIMESTAMP" "TIMESTAMP_IA")))
  13866. (catch 'match
  13867. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13868. (setq key (if (match-end 1)
  13869. (substring (org-match-string-no-properties 1)
  13870. 0 -1))
  13871. string (if (equal key clockstr)
  13872. (org-trim
  13873. (buffer-substring-no-properties
  13874. (match-beginning 3) (goto-char
  13875. (point-at-eol))))
  13876. (substring (org-match-string-no-properties 3)
  13877. 1 -1)))
  13878. ;; Get the correct property name from the key. This is
  13879. ;; necessary if the user has configured time keywords.
  13880. (setq key1 (concat key ":"))
  13881. (cond
  13882. ((not key)
  13883. (setq key
  13884. (if (= (char-after (match-beginning 3)) ?\[)
  13885. "TIMESTAMP_IA" "TIMESTAMP")))
  13886. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13887. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13888. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13889. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13890. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13891. (progn
  13892. (push (cons key string) props)
  13893. ;; no need to search further if match is found
  13894. (throw 'match t))
  13895. (when (or (equal key "CLOCK") (not (assoc key props)))
  13896. (push (cons key string) props)))))))
  13897. (when (memq which '(all standard))
  13898. ;; Get the standard properties, like :PROP: ...
  13899. (setq range (org-get-property-block beg end))
  13900. (when range
  13901. (goto-char (car range))
  13902. (while (re-search-forward
  13903. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13904. (cdr range) t)
  13905. (setq key (org-match-string-no-properties 1)
  13906. value (org-trim (or (org-match-string-no-properties 2) "")))
  13907. (unless (member key excluded)
  13908. (push (cons key (or value "")) props)))))
  13909. (if clocksum
  13910. (push (cons "CLOCKSUM"
  13911. (org-columns-number-to-string (/ (float clocksum) 60.)
  13912. 'add_times))
  13913. props))
  13914. (if clocksumt
  13915. (push (cons "CLOCKSUM_T"
  13916. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13917. 'add_times))
  13918. props))
  13919. (unless (assoc "CATEGORY" props)
  13920. (push (cons "CATEGORY" (org-get-category)) props))
  13921. (append sum-props (nreverse props)))))))
  13922. (defun org-entry-get (pom property &optional inherit literal-nil)
  13923. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13924. If INHERIT is non-nil and the entry does not have the property,
  13925. then also check higher levels of the hierarchy.
  13926. If INHERIT is the symbol `selective', use inheritance only if the setting
  13927. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13928. If the property is present but empty, the return value is the empty string.
  13929. If the property is not present at all, nil is returned.
  13930. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13931. do not interpret it as the list atom nil. This is used for inheritance
  13932. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13933. (org-with-point-at pom
  13934. (if (and inherit (if (eq inherit 'selective)
  13935. (org-property-inherit-p property)
  13936. t))
  13937. (org-entry-get-with-inheritance property literal-nil)
  13938. (if (member property org-special-properties)
  13939. ;; We need a special property. Use `org-entry-properties' to
  13940. ;; retrieve it, but specify the wanted property
  13941. (cdr (assoc property (org-entry-properties nil 'special property)))
  13942. (let ((range (org-get-property-block)))
  13943. (when (and range (not (eq (car range) (cdr range))))
  13944. (let* ((props (list (or (assoc property org-file-properties)
  13945. (assoc property org-global-properties)
  13946. (assoc property org-global-properties-fixed))))
  13947. (ap (lambda (key)
  13948. (when (re-search-forward
  13949. (org-re-property key) (cdr range) t)
  13950. (setq props
  13951. (org-update-property-plist
  13952. key
  13953. (if (match-end 1)
  13954. (org-match-string-no-properties 1) "")
  13955. props)))))
  13956. val)
  13957. (goto-char (car range))
  13958. (funcall ap property)
  13959. (goto-char (car range))
  13960. (while (funcall ap (concat property "+")))
  13961. (setq val (cdr (assoc property props)))
  13962. (when val (if literal-nil val (org-not-nil val))))))))))
  13963. (defun org-property-or-variable-value (var &optional inherit)
  13964. "Check if there is a property fixing the value of VAR.
  13965. If yes, return this value. If not, return the current value of the variable."
  13966. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13967. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13968. (read prop)
  13969. (symbol-value var))))
  13970. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13971. "Delete the property PROPERTY from entry at point-or-marker POM.
  13972. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13973. an empty drawer to delete."
  13974. (org-with-point-at pom
  13975. (if (member property org-special-properties)
  13976. nil ; cannot delete these properties.
  13977. (let ((range (org-get-property-block)))
  13978. (if (and range
  13979. (goto-char (car range))
  13980. (re-search-forward
  13981. (org-re-property property)
  13982. (cdr range) t))
  13983. (progn
  13984. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13985. (and delete-empty-drawer
  13986. (org-remove-empty-drawer-at
  13987. delete-empty-drawer (car range)))
  13988. t)
  13989. nil)))))
  13990. ;; Multi-values properties are properties that contain multiple values
  13991. ;; These values are assumed to be single words, separated by whitespace.
  13992. (defun org-entry-add-to-multivalued-property (pom property value)
  13993. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13994. (let* ((old (org-entry-get pom property))
  13995. (values (and old (org-split-string old "[ \t]"))))
  13996. (setq value (org-entry-protect-space value))
  13997. (unless (member value values)
  13998. (setq values (append values (list value)))
  13999. (org-entry-put pom property
  14000. (mapconcat 'identity values " ")))))
  14001. (defun org-entry-remove-from-multivalued-property (pom property value)
  14002. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14003. (let* ((old (org-entry-get pom property))
  14004. (values (and old (org-split-string old "[ \t]"))))
  14005. (setq value (org-entry-protect-space value))
  14006. (when (member value values)
  14007. (setq values (delete value values))
  14008. (org-entry-put pom property
  14009. (mapconcat 'identity values " ")))))
  14010. (defun org-entry-member-in-multivalued-property (pom property value)
  14011. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14012. (let* ((old (org-entry-get pom property))
  14013. (values (and old (org-split-string old "[ \t]"))))
  14014. (setq value (org-entry-protect-space value))
  14015. (member value values)))
  14016. (defun org-entry-get-multivalued-property (pom property)
  14017. "Return a list of values in a multivalued property."
  14018. (let* ((value (org-entry-get pom property))
  14019. (values (and value (org-split-string value "[ \t]"))))
  14020. (mapcar 'org-entry-restore-space values)))
  14021. (defun org-entry-put-multivalued-property (pom property &rest values)
  14022. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14023. VALUES should be a list of strings. Spaces will be protected."
  14024. (org-entry-put pom property
  14025. (mapconcat 'org-entry-protect-space values " "))
  14026. (let* ((value (org-entry-get pom property))
  14027. (values (and value (org-split-string value "[ \t]"))))
  14028. (mapcar 'org-entry-restore-space values)))
  14029. (defun org-entry-protect-space (s)
  14030. "Protect spaces and newline in string S."
  14031. (while (string-match " " s)
  14032. (setq s (replace-match "%20" t t s)))
  14033. (while (string-match "\n" s)
  14034. (setq s (replace-match "%0A" t t s)))
  14035. s)
  14036. (defun org-entry-restore-space (s)
  14037. "Restore spaces and newline in string S."
  14038. (while (string-match "%20" s)
  14039. (setq s (replace-match " " t t s)))
  14040. (while (string-match "%0A" s)
  14041. (setq s (replace-match "\n" t t s)))
  14042. s)
  14043. (defvar org-entry-property-inherited-from (make-marker)
  14044. "Marker pointing to the entry from where a property was inherited.
  14045. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14046. location of the entry where the inheritance search matched. If there was
  14047. no match, the marker will point nowhere.
  14048. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14049. is set.")
  14050. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14051. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14052. The search will stop at the first ancestor which has the property defined.
  14053. If the value found is \"nil\", return nil to show that the property
  14054. should be considered as undefined (this is the meaning of nil here).
  14055. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14056. (move-marker org-entry-property-inherited-from nil)
  14057. (let (tmp)
  14058. (save-excursion
  14059. (save-restriction
  14060. (widen)
  14061. (catch 'ex
  14062. (while t
  14063. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14064. (or (ignore-errors (org-back-to-heading t))
  14065. (goto-char (point-min)))
  14066. (move-marker org-entry-property-inherited-from (point))
  14067. (throw 'ex tmp))
  14068. (or (ignore-errors (org-up-heading-safe))
  14069. (throw 'ex nil))))))
  14070. (setq tmp (or tmp
  14071. (cdr (assoc property org-file-properties))
  14072. (cdr (assoc property org-global-properties))
  14073. (cdr (assoc property org-global-properties-fixed))))
  14074. (if literal-nil tmp (org-not-nil tmp))))
  14075. (defvar org-property-changed-functions nil
  14076. "Hook called when the value of a property has changed.
  14077. Each hook function should accept two arguments, the name of the property
  14078. and the new value.")
  14079. (defun org-entry-put (pom property value)
  14080. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14081. (org-with-point-at pom
  14082. (org-back-to-heading t)
  14083. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14084. range)
  14085. (cond
  14086. ((equal property "TODO")
  14087. (when (and (stringp value) (string-match "\\S-" value)
  14088. (not (member value org-todo-keywords-1)))
  14089. (error "\"%s\" is not a valid TODO state" value))
  14090. (if (or (not value)
  14091. (not (string-match "\\S-" value)))
  14092. (setq value 'none))
  14093. (org-todo value)
  14094. (org-set-tags nil 'align))
  14095. ((equal property "PRIORITY")
  14096. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14097. (string-to-char value) ?\ ))
  14098. (org-set-tags nil 'align))
  14099. ((equal property "CLOCKSUM")
  14100. (if (not (re-search-forward
  14101. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14102. (error "Cannot find a clock log")
  14103. (goto-char (- (match-end 1) 2))
  14104. (cond
  14105. ((eq value 'earlier) (org-timestamp-down))
  14106. ((eq value 'later) (org-timestamp-up)))
  14107. (org-clock-sum-current-item)))
  14108. ((equal property "SCHEDULED")
  14109. (if (re-search-forward org-scheduled-time-regexp end t)
  14110. (cond
  14111. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14112. ((eq value 'later) (org-timestamp-change 1 'day))
  14113. (t (call-interactively 'org-schedule)))
  14114. (call-interactively 'org-schedule)))
  14115. ((equal property "DEADLINE")
  14116. (if (re-search-forward org-deadline-time-regexp end t)
  14117. (cond
  14118. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14119. ((eq value 'later) (org-timestamp-change 1 'day))
  14120. (t (call-interactively 'org-deadline)))
  14121. (call-interactively 'org-deadline)))
  14122. ((member property org-special-properties)
  14123. (error "The %s property can not yet be set with `org-entry-put'"
  14124. property))
  14125. (t ; a non-special property
  14126. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14127. (setq range (org-get-property-block beg end 'force))
  14128. (goto-char (car range))
  14129. (if (re-search-forward
  14130. (org-re-property-keyword property) (cdr range) t)
  14131. (progn
  14132. (delete-region (match-beginning 0) (match-end 0))
  14133. (goto-char (match-beginning 0)))
  14134. (goto-char (cdr range))
  14135. (insert "\n")
  14136. (backward-char 1)
  14137. (org-indent-line))
  14138. (insert ":" property ":")
  14139. (and value (insert " " value))
  14140. (org-indent-line)))))
  14141. (run-hook-with-args 'org-property-changed-functions property value)))
  14142. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14143. "Get all property keys in the current buffer.
  14144. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14145. like tags and TODO state.
  14146. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14147. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14148. and others.
  14149. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14150. formats in the current buffer."
  14151. (let (rtn range cfmt s p)
  14152. (save-excursion
  14153. (save-restriction
  14154. (widen)
  14155. (goto-char (point-min))
  14156. (while (re-search-forward org-property-start-re nil t)
  14157. (setq range (org-get-property-block))
  14158. (goto-char (car range))
  14159. (while (re-search-forward
  14160. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14161. (cdr range) t)
  14162. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14163. (outline-next-heading))))
  14164. (when include-specials
  14165. (setq rtn (append org-special-properties rtn)))
  14166. (when include-defaults
  14167. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14168. (add-to-list 'rtn org-effort-property))
  14169. (when include-columns
  14170. (save-excursion
  14171. (save-restriction
  14172. (widen)
  14173. (goto-char (point-min))
  14174. (while (re-search-forward
  14175. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14176. nil t)
  14177. (setq cfmt (match-string 2) s 0)
  14178. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14179. cfmt s)
  14180. (setq s (match-end 0)
  14181. p (match-string 1 cfmt))
  14182. (unless (or (equal p "ITEM")
  14183. (member p org-special-properties))
  14184. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14185. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14186. (defun org-property-values (key)
  14187. "Return a list of all values of property KEY in the current buffer."
  14188. (save-excursion
  14189. (save-restriction
  14190. (widen)
  14191. (goto-char (point-min))
  14192. (let ((re (org-re-property key))
  14193. values)
  14194. (while (re-search-forward re nil t)
  14195. (add-to-list 'values (org-trim (match-string 1))))
  14196. (delete "" values)))))
  14197. (defun org-insert-property-drawer ()
  14198. "Insert a property drawer into the current entry."
  14199. (org-back-to-heading t)
  14200. (looking-at org-outline-regexp)
  14201. (let ((indent (if org-adapt-indentation
  14202. (- (match-end 0) (match-beginning 0))
  14203. 0))
  14204. (beg (point))
  14205. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14206. end hiddenp)
  14207. (outline-next-heading)
  14208. (setq end (point))
  14209. (goto-char beg)
  14210. (while (re-search-forward re end t))
  14211. (setq hiddenp (outline-invisible-p))
  14212. (end-of-line 1)
  14213. (and (equal (char-after) ?\n) (forward-char 1))
  14214. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14215. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14216. ;; just skip this line
  14217. (beginning-of-line 2)
  14218. ;; Drawer start, find the end
  14219. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14220. (beginning-of-line 1)))
  14221. (org-skip-over-state-notes)
  14222. (skip-chars-backward " \t\n\r")
  14223. (if (eq (char-before) ?*) (forward-char 1))
  14224. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14225. (beginning-of-line 0)
  14226. (org-indent-to-column indent)
  14227. (beginning-of-line 2)
  14228. (org-indent-to-column indent)
  14229. (beginning-of-line 0)
  14230. (if hiddenp
  14231. (save-excursion
  14232. (org-back-to-heading t)
  14233. (hide-entry))
  14234. (org-flag-drawer t))))
  14235. (defun org-insert-drawer (&optional arg drawer)
  14236. "Insert a drawer at point.
  14237. Optional argument DRAWER, when non-nil, is a string representing
  14238. drawer's name. Otherwise, the user is prompted for a name.
  14239. If a region is active, insert the drawer around that region
  14240. instead.
  14241. Point is left between drawer's boundaries."
  14242. (interactive "P")
  14243. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14244. "LOGBOOK"))
  14245. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14246. ;; internally by Org. They are meant to be inserted
  14247. ;; automatically.
  14248. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14249. ;; Remove system drawers from list. Note: For some reason,
  14250. ;; `org-completing-read' ignores the predicate while
  14251. ;; `completing-read' handles it fine.
  14252. (drawer (if arg "PROPERTIES"
  14253. (or drawer
  14254. (completing-read
  14255. "Drawer: " org-drawers
  14256. (lambda (d) (not (member d system-drawers))))))))
  14257. (cond
  14258. ;; With C-u, fall back on `org-insert-property-drawer'
  14259. (arg (org-insert-property-drawer))
  14260. ;; With an active region, insert a drawer at point.
  14261. ((not (org-region-active-p))
  14262. (progn
  14263. (unless (bolp) (insert "\n"))
  14264. (insert (format ":%s:\n\n:END:\n" drawer))
  14265. (forward-line -2)))
  14266. ;; Otherwise, insert the drawer at point
  14267. (t
  14268. (let ((rbeg (region-beginning))
  14269. (rend (copy-marker (region-end))))
  14270. (unwind-protect
  14271. (progn
  14272. (goto-char rbeg)
  14273. (beginning-of-line)
  14274. (when (save-excursion
  14275. (re-search-forward org-outline-regexp-bol rend t))
  14276. (error "Drawers cannot contain headlines"))
  14277. ;; Position point at the beginning of the first
  14278. ;; non-blank line in region. Insert drawer's opening
  14279. ;; there, then indent it.
  14280. (org-skip-whitespace)
  14281. (beginning-of-line)
  14282. (insert ":" drawer ":\n")
  14283. (forward-line -1)
  14284. (indent-for-tab-command)
  14285. ;; Move point to the beginning of the first blank line
  14286. ;; after the last non-blank line in region. Insert
  14287. ;; drawer's closing, then indent it.
  14288. (goto-char rend)
  14289. (skip-chars-backward " \r\t\n")
  14290. (insert "\n:END:")
  14291. (deactivate-mark t)
  14292. (indent-for-tab-command)
  14293. (unless (eolp) (insert "\n")))
  14294. ;; Clear marker, whatever the outcome of insertion is.
  14295. (set-marker rend nil)))))))
  14296. (defvar org-property-set-functions-alist nil
  14297. "Property set function alist.
  14298. Each entry should have the following format:
  14299. (PROPERTY . READ-FUNCTION)
  14300. The read function will be called with the same argument as
  14301. `org-completing-read'.")
  14302. (defun org-set-property-function (property)
  14303. "Get the function that should be used to set PROPERTY.
  14304. This is computed according to `org-property-set-functions-alist'."
  14305. (or (cdr (assoc property org-property-set-functions-alist))
  14306. 'org-completing-read))
  14307. (defun org-read-property-value (property)
  14308. "Read PROPERTY value from user."
  14309. (let* ((completion-ignore-case t)
  14310. (allowed (org-property-get-allowed-values nil property 'table))
  14311. (cur (org-entry-get nil property))
  14312. (prompt (concat property " value"
  14313. (if (and cur (string-match "\\S-" cur))
  14314. (concat " [" cur "]") "") ": "))
  14315. (set-function (org-set-property-function property))
  14316. (val (if allowed
  14317. (funcall set-function prompt allowed nil
  14318. (not (get-text-property 0 'org-unrestricted
  14319. (caar allowed))))
  14320. (let (org-completion-use-ido org-completion-use-iswitchb)
  14321. (funcall set-function prompt
  14322. (mapcar 'list (org-property-values property))
  14323. nil nil "" nil cur)))))
  14324. (if (equal val "")
  14325. cur
  14326. val)))
  14327. (defvar org-last-set-property nil)
  14328. (defvar org-last-set-property-value nil)
  14329. (defun org-read-property-name ()
  14330. "Read a property name."
  14331. (let* ((completion-ignore-case t)
  14332. (keys (org-buffer-property-keys nil t t))
  14333. (default-prop (or (save-excursion
  14334. (save-match-data
  14335. (beginning-of-line)
  14336. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14337. (null (string= (match-string 1) "END"))
  14338. (match-string 1))))
  14339. org-last-set-property))
  14340. (property (org-icompleting-read
  14341. (concat "Property"
  14342. (if default-prop (concat " [" default-prop "]") "")
  14343. ": ")
  14344. (mapcar 'list keys)
  14345. nil nil nil nil
  14346. default-prop)))
  14347. (if (member property keys)
  14348. property
  14349. (or (cdr (assoc (downcase property)
  14350. (mapcar (lambda (x) (cons (downcase x) x))
  14351. keys)))
  14352. property))))
  14353. (defun org-set-property-and-value (use-last)
  14354. "Allow to set [PROPERTY]: [value] direction from prompt.
  14355. When use-default, don't even ask, just use the last
  14356. \"[PROPERTY]: [value]\" string from the history."
  14357. (interactive "P")
  14358. (let* ((completion-ignore-case t)
  14359. (pv (or (and use-last org-last-set-property-value)
  14360. (org-completing-read
  14361. "Enter a \"[Property]: [value]\" pair: "
  14362. nil nil nil nil nil
  14363. org-last-set-property-value)))
  14364. prop val)
  14365. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14366. (setq prop (match-string 1 pv)
  14367. val (match-string 2 pv))
  14368. (org-set-property prop val))))
  14369. (defun org-set-property (property value)
  14370. "In the current entry, set PROPERTY to VALUE.
  14371. When called interactively, this will prompt for a property name, offering
  14372. completion on existing and default properties. And then it will prompt
  14373. for a value, offering completion either on allowed values (via an inherited
  14374. xxx_ALL property) or on existing values in other instances of this property
  14375. in the current file."
  14376. (interactive (list nil nil))
  14377. (let* ((property (or property (org-read-property-name)))
  14378. (value (or value (org-read-property-value property)))
  14379. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14380. (setq org-last-set-property property)
  14381. (setq org-last-set-property-value (concat property ": " value))
  14382. ;; Possibly postprocess the inserted value:
  14383. (when fn (setq value (funcall fn value)))
  14384. (unless (equal (org-entry-get nil property) value)
  14385. (org-entry-put nil property value))))
  14386. (defun org-delete-property (property &optional delete-empty-drawer)
  14387. "In the current entry, delete PROPERTY.
  14388. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14389. an empty drawer to delete."
  14390. (interactive
  14391. (let* ((completion-ignore-case t)
  14392. (prop (org-icompleting-read "Property: "
  14393. (org-entry-properties nil 'standard))))
  14394. (list prop)))
  14395. (message "Property %s %s" property
  14396. (if (org-entry-delete nil property delete-empty-drawer)
  14397. "deleted"
  14398. "was not present in the entry")))
  14399. (defun org-delete-property-globally (property)
  14400. "Remove PROPERTY globally, from all entries."
  14401. (interactive
  14402. (let* ((completion-ignore-case t)
  14403. (prop (org-icompleting-read
  14404. "Globally remove property: "
  14405. (mapcar 'list (org-buffer-property-keys)))))
  14406. (list prop)))
  14407. (save-excursion
  14408. (save-restriction
  14409. (widen)
  14410. (goto-char (point-min))
  14411. (let ((cnt 0))
  14412. (while (re-search-forward
  14413. (org-re-property property)
  14414. nil t)
  14415. (setq cnt (1+ cnt))
  14416. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14417. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14418. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14419. (defun org-compute-property-at-point ()
  14420. "Compute the property at point.
  14421. This looks for an enclosing column format, extracts the operator and
  14422. then applies it to the property in the column format's scope."
  14423. (interactive)
  14424. (unless (org-at-property-p)
  14425. (error "Not at a property"))
  14426. (let ((prop (org-match-string-no-properties 2)))
  14427. (org-columns-get-format-and-top-level)
  14428. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14429. (error "No operator defined for property %s" prop))
  14430. (org-columns-compute prop)))
  14431. (defvar org-property-allowed-value-functions nil
  14432. "Hook for functions supplying allowed values for a specific property.
  14433. The functions must take a single argument, the name of the property, and
  14434. return a flat list of allowed values. If \":ETC\" is one of
  14435. the values, this means that these values are intended as defaults for
  14436. completion, but that other values should be allowed too.
  14437. The functions must return nil if they are not responsible for this
  14438. property.")
  14439. (defun org-property-get-allowed-values (pom property &optional table)
  14440. "Get allowed values for the property PROPERTY.
  14441. When TABLE is non-nil, return an alist that can directly be used for
  14442. completion."
  14443. (let (vals)
  14444. (cond
  14445. ((equal property "TODO")
  14446. (setq vals (org-with-point-at pom
  14447. (append org-todo-keywords-1 '("")))))
  14448. ((equal property "PRIORITY")
  14449. (let ((n org-lowest-priority))
  14450. (while (>= n org-highest-priority)
  14451. (push (char-to-string n) vals)
  14452. (setq n (1- n)))))
  14453. ((member property org-special-properties))
  14454. ((setq vals (run-hook-with-args-until-success
  14455. 'org-property-allowed-value-functions property)))
  14456. (t
  14457. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14458. (when (and vals (string-match "\\S-" vals))
  14459. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14460. (setq vals (mapcar (lambda (x)
  14461. (cond ((stringp x) x)
  14462. ((numberp x) (number-to-string x))
  14463. ((symbolp x) (symbol-name x))
  14464. (t "???")))
  14465. vals)))))
  14466. (when (member ":ETC" vals)
  14467. (setq vals (remove ":ETC" vals))
  14468. (org-add-props (car vals) '(org-unrestricted t)))
  14469. (if table (mapcar 'list vals) vals)))
  14470. (defun org-property-previous-allowed-value (&optional previous)
  14471. "Switch to the next allowed value for this property."
  14472. (interactive)
  14473. (org-property-next-allowed-value t))
  14474. (defun org-property-next-allowed-value (&optional previous)
  14475. "Switch to the next allowed value for this property."
  14476. (interactive)
  14477. (unless (org-at-property-p)
  14478. (error "Not at a property"))
  14479. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14480. (key (match-string 2))
  14481. (value (match-string 3))
  14482. (allowed (or (org-property-get-allowed-values (point) key)
  14483. (and (member value '("[ ]" "[-]" "[X]"))
  14484. '("[ ]" "[X]"))))
  14485. (heading (save-match-data (nth 4 (org-heading-components))))
  14486. nval)
  14487. (unless allowed
  14488. (error "Allowed values for this property have not been defined"))
  14489. (if previous (setq allowed (reverse allowed)))
  14490. (if (member value allowed)
  14491. (setq nval (car (cdr (member value allowed)))))
  14492. (setq nval (or nval (car allowed)))
  14493. (if (equal nval value)
  14494. (error "Only one allowed value for this property"))
  14495. (org-at-property-p)
  14496. (replace-match (concat " :" key ": " nval) t t)
  14497. (org-indent-line)
  14498. (beginning-of-line 1)
  14499. (skip-chars-forward " \t")
  14500. (when (equal prop org-effort-property)
  14501. (save-excursion
  14502. (org-back-to-heading t)
  14503. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14504. (when (string= org-clock-current-task heading)
  14505. (setq org-clock-effort nval)
  14506. (org-clock-update-mode-line)))
  14507. (run-hook-with-args 'org-property-changed-functions key nval)))
  14508. (defun org-find-olp (path &optional this-buffer)
  14509. "Return a marker pointing to the entry at outline path OLP.
  14510. If anything goes wrong, throw an error.
  14511. You can wrap this call to catch the error like this:
  14512. (condition-case msg
  14513. (org-mobile-locate-entry (match-string 4))
  14514. (error (nth 1 msg)))
  14515. The return value will then be either a string with the error message,
  14516. or a marker if everything is OK.
  14517. If THIS-BUFFER is set, the outline path does not contain a file,
  14518. only headings."
  14519. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14520. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14521. (level 1)
  14522. (lmin 1)
  14523. (lmax 1)
  14524. limit re end found pos heading cnt flevel)
  14525. (unless buffer (error "File not found :%s" file))
  14526. (with-current-buffer buffer
  14527. (save-excursion
  14528. (save-restriction
  14529. (widen)
  14530. (setq limit (point-max))
  14531. (goto-char (point-min))
  14532. (while (setq heading (pop path))
  14533. (setq re (format org-complex-heading-regexp-format
  14534. (regexp-quote heading)))
  14535. (setq cnt 0 pos (point))
  14536. (while (re-search-forward re end t)
  14537. (setq level (- (match-end 1) (match-beginning 1)))
  14538. (if (and (>= level lmin) (<= level lmax))
  14539. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14540. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14541. lmax heading))
  14542. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14543. lmax heading))
  14544. (goto-char found)
  14545. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14546. (setq end (save-excursion (org-end-of-subtree t t))))
  14547. (when (org-at-heading-p)
  14548. (point-marker)))))))
  14549. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14550. "Find node HEADING in BUFFER.
  14551. Return a marker to the heading if it was found, or nil if not.
  14552. If POS-ONLY is set, return just the position instead of a marker.
  14553. The heading text must match exact, but it may have a TODO keyword,
  14554. a priority cookie and tags in the standard locations."
  14555. (with-current-buffer (or buffer (current-buffer))
  14556. (save-excursion
  14557. (save-restriction
  14558. (widen)
  14559. (goto-char (point-min))
  14560. (let (case-fold-search)
  14561. (if (re-search-forward
  14562. (format org-complex-heading-regexp-format
  14563. (regexp-quote heading)) nil t)
  14564. (if pos-only
  14565. (match-beginning 0)
  14566. (move-marker (make-marker) (match-beginning 0)))))))))
  14567. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14568. "Find Org node headline HEADING in all .org files in directory DIR.
  14569. When the target headline is found, return a marker to this location."
  14570. (let ((files (directory-files (or dir default-directory)
  14571. nil "\\`[^.#].*\\.org\\'"))
  14572. file visiting m buffer)
  14573. (catch 'found
  14574. (while (setq file (pop files))
  14575. (message "trying %s" file)
  14576. (setq visiting (org-find-base-buffer-visiting file))
  14577. (setq buffer (or visiting (find-file-noselect file)))
  14578. (setq m (org-find-exact-headline-in-buffer
  14579. heading buffer))
  14580. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14581. (and m (throw 'found m))))))
  14582. (defun org-find-entry-with-id (ident)
  14583. "Locate the entry that contains the ID property with exact value IDENT.
  14584. IDENT can be a string, a symbol or a number, this function will search for
  14585. the string representation of it.
  14586. Return the position where this entry starts, or nil if there is no such entry."
  14587. (interactive "sID: ")
  14588. (let ((id (cond
  14589. ((stringp ident) ident)
  14590. ((symbol-name ident) (symbol-name ident))
  14591. ((numberp ident) (number-to-string ident))
  14592. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14593. (case-fold-search nil))
  14594. (save-excursion
  14595. (save-restriction
  14596. (widen)
  14597. (goto-char (point-min))
  14598. (when (re-search-forward
  14599. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14600. nil t)
  14601. (org-back-to-heading t)
  14602. (point))))))
  14603. ;;;; Timestamps
  14604. (defvar org-last-changed-timestamp nil)
  14605. (defvar org-last-inserted-timestamp nil
  14606. "The last time stamp inserted with `org-insert-time-stamp'.")
  14607. (defvar org-time-was-given) ; dynamically scoped parameter
  14608. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14609. (defvar org-ts-what) ; dynamically scoped parameter
  14610. (defun org-time-stamp (arg &optional inactive)
  14611. "Prompt for a date/time and insert a time stamp.
  14612. If the user specifies a time like HH:MM or if this command is
  14613. called with at least one prefix argument, the time stamp contains
  14614. the date and the time. Otherwise, only the date is be included.
  14615. All parts of a date not specified by the user is filled in from
  14616. the current date/time. So if you just press return without
  14617. typing anything, the time stamp will represent the current
  14618. date/time.
  14619. If there is already a timestamp at the cursor, it will be
  14620. modified.
  14621. With two universal prefix arguments, insert an active timestamp
  14622. with the current time without prompting the user."
  14623. (interactive "P")
  14624. (let* ((ts nil)
  14625. (default-time
  14626. ;; Default time is either today, or, when entering a range,
  14627. ;; the range start.
  14628. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14629. (save-excursion
  14630. (re-search-backward
  14631. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14632. (- (point) 20) t)))
  14633. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14634. (current-time)))
  14635. (default-input (and ts (org-get-compact-tod ts)))
  14636. (repeater (save-excursion
  14637. (save-match-data
  14638. (beginning-of-line)
  14639. (when (re-search-forward
  14640. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14641. (save-excursion (progn (end-of-line) (point))) t)
  14642. (match-string 0)))))
  14643. org-time-was-given org-end-time-was-given time)
  14644. (cond
  14645. ((and (org-at-timestamp-p t)
  14646. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14647. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14648. (insert "--")
  14649. (setq time (let ((this-command this-command))
  14650. (org-read-date arg 'totime nil nil
  14651. default-time default-input inactive)))
  14652. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14653. ((org-at-timestamp-p t)
  14654. (setq time (let ((this-command this-command))
  14655. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14656. (when (org-at-timestamp-p t) ; just to get the match data
  14657. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14658. (replace-match "")
  14659. (setq org-last-changed-timestamp
  14660. (org-insert-time-stamp
  14661. time (or org-time-was-given arg)
  14662. inactive nil nil (list org-end-time-was-given)))
  14663. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14664. (setq org-last-changed-timestamp
  14665. (concat (substring org-last-inserted-timestamp 0 -1)
  14666. " " repeater ">"))))
  14667. (message "Timestamp updated"))
  14668. ((equal arg '(16))
  14669. (org-insert-time-stamp (current-time) t))
  14670. (t
  14671. (setq time (let ((this-command this-command))
  14672. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14673. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14674. nil nil (list org-end-time-was-given))))))
  14675. ;; FIXME: can we use this for something else, like computing time differences?
  14676. (defun org-get-compact-tod (s)
  14677. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14678. (let* ((t1 (match-string 1 s))
  14679. (h1 (string-to-number (match-string 2 s)))
  14680. (m1 (string-to-number (match-string 3 s)))
  14681. (t2 (and (match-end 4) (match-string 5 s)))
  14682. (h2 (and t2 (string-to-number (match-string 6 s))))
  14683. (m2 (and t2 (string-to-number (match-string 7 s))))
  14684. dh dm)
  14685. (if (not t2)
  14686. t1
  14687. (setq dh (- h2 h1) dm (- m2 m1))
  14688. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14689. (concat t1 "+" (number-to-string dh)
  14690. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14691. (defun org-time-stamp-inactive (&optional arg)
  14692. "Insert an inactive time stamp.
  14693. An inactive time stamp is enclosed in square brackets instead of angle
  14694. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14695. does not link to the calendar and cannot be changed with the S-cursor keys.
  14696. So these are more for recording a certain time/date."
  14697. (interactive "P")
  14698. (org-time-stamp arg 'inactive))
  14699. (defvar org-date-ovl (make-overlay 1 1))
  14700. (overlay-put org-date-ovl 'face 'org-date-selected)
  14701. (org-detach-overlay org-date-ovl)
  14702. (defvar org-ans1) ; dynamically scoped parameter
  14703. (defvar org-ans2) ; dynamically scoped parameter
  14704. (defvar org-plain-time-of-day-regexp) ; defined below
  14705. (defvar org-overriding-default-time nil) ; dynamically scoped
  14706. (defvar org-read-date-overlay nil)
  14707. (defvar org-dcst nil) ; dynamically scoped
  14708. (defvar org-read-date-history nil)
  14709. (defvar org-read-date-final-answer nil)
  14710. (defvar org-read-date-analyze-futurep nil)
  14711. (defvar org-read-date-analyze-forced-year nil)
  14712. (defvar org-read-date-inactive)
  14713. (defvar org-read-date-minibuffer-local-map
  14714. (let ((map (make-sparse-keymap)))
  14715. (set-keymap-parent map minibuffer-local-map)
  14716. (org-defkey map (kbd ".")
  14717. (lambda () (interactive)
  14718. (org-eval-in-calendar '(calendar-goto-today))))
  14719. (org-defkey map [(meta shift left)]
  14720. (lambda () (interactive)
  14721. (org-eval-in-calendar '(calendar-backward-month 1))))
  14722. (org-defkey map [(meta shift right)]
  14723. (lambda () (interactive)
  14724. (org-eval-in-calendar '(calendar-forward-month 1))))
  14725. (org-defkey map [(meta shift up)]
  14726. (lambda () (interactive)
  14727. (org-eval-in-calendar '(calendar-backward-year 1))))
  14728. (org-defkey map [(meta shift down)]
  14729. (lambda () (interactive)
  14730. (org-eval-in-calendar '(calendar-forward-year 1))))
  14731. (org-defkey map [?\e (shift left)]
  14732. (lambda () (interactive)
  14733. (org-eval-in-calendar '(calendar-backward-month 1))))
  14734. (org-defkey map [?\e (shift right)]
  14735. (lambda () (interactive)
  14736. (org-eval-in-calendar '(calendar-forward-month 1))))
  14737. (org-defkey map [?\e (shift up)]
  14738. (lambda () (interactive)
  14739. (org-eval-in-calendar '(calendar-backward-year 1))))
  14740. (org-defkey map [?\e (shift down)]
  14741. (lambda () (interactive)
  14742. (org-eval-in-calendar '(calendar-forward-year 1))))
  14743. (org-defkey map [(shift up)]
  14744. (lambda () (interactive)
  14745. (org-eval-in-calendar '(calendar-backward-week 1))))
  14746. (org-defkey map [(shift down)]
  14747. (lambda () (interactive)
  14748. (org-eval-in-calendar '(calendar-forward-week 1))))
  14749. (org-defkey map [(shift left)]
  14750. (lambda () (interactive)
  14751. (org-eval-in-calendar '(calendar-backward-day 1))))
  14752. (org-defkey map [(shift right)]
  14753. (lambda () (interactive)
  14754. (org-eval-in-calendar '(calendar-forward-day 1))))
  14755. (org-defkey map "!"
  14756. (lambda () (interactive)
  14757. (org-eval-in-calendar '(diary-view-entries))
  14758. (message "")))
  14759. (org-defkey map ">"
  14760. (lambda () (interactive)
  14761. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14762. (org-defkey map "<"
  14763. (lambda () (interactive)
  14764. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14765. (org-defkey map "\C-v"
  14766. (lambda () (interactive)
  14767. (org-eval-in-calendar
  14768. '(calendar-scroll-left-three-months 1))))
  14769. (org-defkey map "\M-v"
  14770. (lambda () (interactive)
  14771. (org-eval-in-calendar
  14772. '(calendar-scroll-right-three-months 1))))
  14773. map)
  14774. "Keymap for minibuffer commands when using `org-read-date'.")
  14775. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14776. default-time default-input inactive)
  14777. "Read a date, possibly a time, and make things smooth for the user.
  14778. The prompt will suggest to enter an ISO date, but you can also enter anything
  14779. which will at least partially be understood by `parse-time-string'.
  14780. Unrecognized parts of the date will default to the current day, month, year,
  14781. hour and minute. If this command is called to replace a timestamp at point,
  14782. or to enter the second timestamp of a range, the default time is taken
  14783. from the existing stamp. Furthermore, the command prefers the future,
  14784. so if you are giving a date where the year is not given, and the day-month
  14785. combination is already past in the current year, it will assume you
  14786. mean next year. For details, see the manual. A few examples:
  14787. 3-2-5 --> 2003-02-05
  14788. feb 15 --> currentyear-02-15
  14789. 2/15 --> currentyear-02-15
  14790. sep 12 9 --> 2009-09-12
  14791. 12:45 --> today 12:45
  14792. 22 sept 0:34 --> currentyear-09-22 0:34
  14793. 12 --> currentyear-currentmonth-12
  14794. Fri --> nearest Friday (today or later)
  14795. etc.
  14796. Furthermore you can specify a relative date by giving, as the *first* thing
  14797. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14798. change in days weeks, months, years.
  14799. With a single plus or minus, the date is relative to today. With a double
  14800. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14801. +4d --> four days from today
  14802. +4 --> same as above
  14803. +2w --> two weeks from today
  14804. ++5 --> five days from default date
  14805. The function understands only English month and weekday abbreviations.
  14806. While prompting, a calendar is popped up - you can also select the
  14807. date with the mouse (button 1). The calendar shows a period of three
  14808. months. To scroll it to other months, use the keys `>' and `<'.
  14809. If you don't like the calendar, turn it off with
  14810. \(setq org-read-date-popup-calendar nil)
  14811. With optional argument TO-TIME, the date will immediately be converted
  14812. to an internal time.
  14813. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14814. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14815. still enter a time, and this function will inform the calling routine
  14816. about this change. The calling routine may then choose to change the
  14817. format used to insert the time stamp into the buffer to include the time.
  14818. With optional argument FROM-STRING, read from this string instead from
  14819. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14820. the time/date that is used for everything that is not specified by the
  14821. user."
  14822. (require 'parse-time)
  14823. (let* ((org-time-stamp-rounding-minutes
  14824. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14825. (org-dcst org-display-custom-times)
  14826. (ct (org-current-time))
  14827. (org-def (or org-overriding-default-time default-time ct))
  14828. (org-defdecode (decode-time org-def))
  14829. (dummy (progn
  14830. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14831. (setcar (nthcdr 2 org-defdecode) -1)
  14832. (setcar (nthcdr 1 org-defdecode) 59)
  14833. (setq org-def (apply 'encode-time org-defdecode)
  14834. org-defdecode (decode-time org-def)))))
  14835. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14836. (calendar-frame-setup nil)
  14837. (calendar-setup nil)
  14838. (calendar-move-hook nil)
  14839. (calendar-view-diary-initially-flag nil)
  14840. (calendar-view-holidays-initially-flag nil)
  14841. (timestr (format-time-string
  14842. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14843. (prompt (concat (if prompt (concat prompt " ") "")
  14844. (format "Date+time [%s]: " timestr)))
  14845. ans (org-ans0 "") org-ans1 org-ans2 final)
  14846. (cond
  14847. (from-string (setq ans from-string))
  14848. (org-read-date-popup-calendar
  14849. (save-excursion
  14850. (save-window-excursion
  14851. (calendar)
  14852. (org-eval-in-calendar '(setq cursor-type nil) t)
  14853. (unwind-protect
  14854. (progn
  14855. (calendar-forward-day (- (time-to-days org-def)
  14856. (calendar-absolute-from-gregorian
  14857. (calendar-current-date))))
  14858. (org-eval-in-calendar nil t)
  14859. (let* ((old-map (current-local-map))
  14860. (map (copy-keymap calendar-mode-map))
  14861. (minibuffer-local-map
  14862. (copy-keymap org-read-date-minibuffer-local-map)))
  14863. (org-defkey map (kbd "RET") 'org-calendar-select)
  14864. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14865. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14866. (unwind-protect
  14867. (progn
  14868. (use-local-map map)
  14869. (setq org-read-date-inactive inactive)
  14870. (add-hook 'post-command-hook 'org-read-date-display)
  14871. (setq org-ans0 (read-string prompt default-input
  14872. 'org-read-date-history nil))
  14873. ;; org-ans0: from prompt
  14874. ;; org-ans1: from mouse click
  14875. ;; org-ans2: from calendar motion
  14876. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14877. (remove-hook 'post-command-hook 'org-read-date-display)
  14878. (use-local-map old-map)
  14879. (when org-read-date-overlay
  14880. (delete-overlay org-read-date-overlay)
  14881. (setq org-read-date-overlay nil)))))
  14882. (bury-buffer "*Calendar*")))))
  14883. (t ; Naked prompt only
  14884. (unwind-protect
  14885. (setq ans (read-string prompt default-input
  14886. 'org-read-date-history timestr))
  14887. (when org-read-date-overlay
  14888. (delete-overlay org-read-date-overlay)
  14889. (setq org-read-date-overlay nil)))))
  14890. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14891. (when org-read-date-analyze-forced-year
  14892. (message "Year was forced into %s"
  14893. (if org-read-date-force-compatible-dates
  14894. "compatible range (1970-2037)"
  14895. "range representable on this machine"))
  14896. (ding))
  14897. ;; One round trip to get rid of 34th of August and stuff like that....
  14898. (setq final (decode-time (apply 'encode-time final)))
  14899. (setq org-read-date-final-answer ans)
  14900. (if to-time
  14901. (apply 'encode-time final)
  14902. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14903. (format "%04d-%02d-%02d %02d:%02d"
  14904. (nth 5 final) (nth 4 final) (nth 3 final)
  14905. (nth 2 final) (nth 1 final))
  14906. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14907. (defvar org-def)
  14908. (defvar org-defdecode)
  14909. (defvar org-with-time)
  14910. (defun org-read-date-display ()
  14911. "Display the current date prompt interpretation in the minibuffer."
  14912. (when org-read-date-display-live
  14913. (when org-read-date-overlay
  14914. (delete-overlay org-read-date-overlay))
  14915. (when (minibufferp (current-buffer))
  14916. (save-excursion
  14917. (end-of-line 1)
  14918. (while (not (equal (buffer-substring
  14919. (max (point-min) (- (point) 4)) (point))
  14920. " "))
  14921. (insert " ")))
  14922. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14923. " " (or org-ans1 org-ans2)))
  14924. (org-end-time-was-given nil)
  14925. (f (org-read-date-analyze ans org-def org-defdecode))
  14926. (fmts (if org-dcst
  14927. org-time-stamp-custom-formats
  14928. org-time-stamp-formats))
  14929. (fmt (if (or org-with-time
  14930. (and (boundp 'org-time-was-given) org-time-was-given))
  14931. (cdr fmts)
  14932. (car fmts)))
  14933. (txt (format-time-string fmt (apply 'encode-time f)))
  14934. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14935. (txt (concat "=> " txt)))
  14936. (when (and org-end-time-was-given
  14937. (string-match org-plain-time-of-day-regexp txt))
  14938. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14939. org-end-time-was-given
  14940. (substring txt (match-end 0)))))
  14941. (when org-read-date-analyze-futurep
  14942. (setq txt (concat txt " (=>F)")))
  14943. (setq org-read-date-overlay
  14944. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14945. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14946. (defun org-read-date-analyze (ans org-def org-defdecode)
  14947. "Analyze the combined answer of the date prompt."
  14948. ;; FIXME: cleanup and comment
  14949. (let ((nowdecode (decode-time (current-time)))
  14950. delta deltan deltaw deltadef year month day
  14951. hour minute second wday pm h2 m2 tl wday1
  14952. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14953. (setq org-read-date-analyze-futurep nil
  14954. org-read-date-analyze-forced-year nil)
  14955. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14956. (setq ans "+0"))
  14957. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14958. (setq ans (replace-match "" t t ans)
  14959. deltan (car delta)
  14960. deltaw (nth 1 delta)
  14961. deltadef (nth 2 delta)))
  14962. ;; Check if there is an iso week date in there. If yes, store the
  14963. ;; info and postpone interpreting it until the rest of the parsing
  14964. ;; is done.
  14965. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14966. (setq iso-year (if (match-end 1)
  14967. (org-small-year-to-year
  14968. (string-to-number (match-string 1 ans))))
  14969. iso-weekday (if (match-end 3)
  14970. (string-to-number (match-string 3 ans)))
  14971. iso-week (string-to-number (match-string 2 ans)))
  14972. (setq ans (replace-match "" t t ans)))
  14973. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14974. (when (string-match
  14975. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14976. (setq year (if (match-end 2)
  14977. (string-to-number (match-string 2 ans))
  14978. (progn (setq kill-year t)
  14979. (string-to-number (format-time-string "%Y"))))
  14980. month (string-to-number (match-string 3 ans))
  14981. day (string-to-number (match-string 4 ans)))
  14982. (if (< year 100) (setq year (+ 2000 year)))
  14983. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14984. t nil ans)))
  14985. ;; Help matching dotted european dates
  14986. (when (string-match
  14987. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14988. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14989. (setq kill-year t)
  14990. (string-to-number (format-time-string "%Y")))
  14991. day (string-to-number (match-string 1 ans))
  14992. month (string-to-number (match-string 2 ans))
  14993. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14994. t nil ans)))
  14995. ;; Help matching american dates, like 5/30 or 5/30/7
  14996. (when (string-match
  14997. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14998. (setq year (if (match-end 4)
  14999. (string-to-number (match-string 4 ans))
  15000. (progn (setq kill-year t)
  15001. (string-to-number (format-time-string "%Y"))))
  15002. month (string-to-number (match-string 1 ans))
  15003. day (string-to-number (match-string 2 ans)))
  15004. (if (< year 100) (setq year (+ 2000 year)))
  15005. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15006. t nil ans)))
  15007. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15008. ;; If there is a time with am/pm, and *no* time without it, we convert
  15009. ;; so that matching will be successful.
  15010. (loop for i from 1 to 2 do ; twice, for end time as well
  15011. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15012. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15013. (setq hour (string-to-number (match-string 1 ans))
  15014. minute (if (match-end 3)
  15015. (string-to-number (match-string 3 ans))
  15016. 0)
  15017. pm (equal ?p
  15018. (string-to-char (downcase (match-string 4 ans)))))
  15019. (if (and (= hour 12) (not pm))
  15020. (setq hour 0)
  15021. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15022. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15023. t t ans))))
  15024. ;; Check if a time range is given as a duration
  15025. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15026. (setq hour (string-to-number (match-string 1 ans))
  15027. h2 (+ hour (string-to-number (match-string 3 ans)))
  15028. minute (string-to-number (match-string 2 ans))
  15029. m2 (+ minute (if (match-end 5) (string-to-number
  15030. (match-string 5 ans))0)))
  15031. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15032. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15033. t t ans)))
  15034. ;; Check if there is a time range
  15035. (when (boundp 'org-end-time-was-given)
  15036. (setq org-time-was-given nil)
  15037. (when (and (string-match org-plain-time-of-day-regexp ans)
  15038. (match-end 8))
  15039. (setq org-end-time-was-given (match-string 8 ans))
  15040. (setq ans (concat (substring ans 0 (match-beginning 7))
  15041. (substring ans (match-end 7))))))
  15042. (setq tl (parse-time-string ans)
  15043. day (or (nth 3 tl) (nth 3 org-defdecode))
  15044. month (or (nth 4 tl)
  15045. (if (and org-read-date-prefer-future
  15046. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15047. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15048. (nth 4 org-defdecode)))
  15049. year (or (and (not kill-year) (nth 5 tl))
  15050. (if (and org-read-date-prefer-future
  15051. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15052. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15053. (nth 5 org-defdecode)))
  15054. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15055. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15056. second (or (nth 0 tl) 0)
  15057. wday (nth 6 tl))
  15058. (when (and (eq org-read-date-prefer-future 'time)
  15059. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15060. (equal day (nth 3 nowdecode))
  15061. (equal month (nth 4 nowdecode))
  15062. (equal year (nth 5 nowdecode))
  15063. (nth 2 tl)
  15064. (or (< (nth 2 tl) (nth 2 nowdecode))
  15065. (and (= (nth 2 tl) (nth 2 nowdecode))
  15066. (nth 1 tl)
  15067. (< (nth 1 tl) (nth 1 nowdecode)))))
  15068. (setq day (1+ day)
  15069. futurep t))
  15070. ;; Special date definitions below
  15071. (cond
  15072. (iso-week
  15073. ;; There was an iso week
  15074. (require 'cal-iso)
  15075. (setq futurep nil)
  15076. (setq year (or iso-year year)
  15077. day (or iso-weekday wday 1)
  15078. wday nil ; to make sure that the trigger below does not match
  15079. iso-date (calendar-gregorian-from-absolute
  15080. (calendar-absolute-from-iso
  15081. (list iso-week day year))))
  15082. ; FIXME: Should we also push ISO weeks into the future?
  15083. ; (when (and org-read-date-prefer-future
  15084. ; (not iso-year)
  15085. ; (< (calendar-absolute-from-gregorian iso-date)
  15086. ; (time-to-days (current-time))))
  15087. ; (setq year (1+ year)
  15088. ; iso-date (calendar-gregorian-from-absolute
  15089. ; (calendar-absolute-from-iso
  15090. ; (list iso-week day year)))))
  15091. (setq month (car iso-date)
  15092. year (nth 2 iso-date)
  15093. day (nth 1 iso-date)))
  15094. (deltan
  15095. (setq futurep nil)
  15096. (unless deltadef
  15097. (let ((now (decode-time (current-time))))
  15098. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15099. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15100. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15101. ((equal deltaw "m") (setq month (+ month deltan)))
  15102. ((equal deltaw "y") (setq year (+ year deltan)))))
  15103. ((and wday (not (nth 3 tl)))
  15104. ;; Weekday was given, but no day, so pick that day in the week
  15105. ;; on or after the derived date.
  15106. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15107. (unless (equal wday wday1)
  15108. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15109. (if (and (boundp 'org-time-was-given)
  15110. (nth 2 tl))
  15111. (setq org-time-was-given t))
  15112. (if (< year 100) (setq year (+ 2000 year)))
  15113. ;; Check of the date is representable
  15114. (if org-read-date-force-compatible-dates
  15115. (progn
  15116. (if (< year 1970)
  15117. (setq year 1970 org-read-date-analyze-forced-year t))
  15118. (if (> year 2037)
  15119. (setq year 2037 org-read-date-analyze-forced-year t)))
  15120. (condition-case nil
  15121. (ignore (encode-time second minute hour day month year))
  15122. (error
  15123. (setq year (nth 5 org-defdecode))
  15124. (setq org-read-date-analyze-forced-year t))))
  15125. (setq org-read-date-analyze-futurep futurep)
  15126. (list second minute hour day month year)))
  15127. (defvar parse-time-weekdays)
  15128. (defun org-read-date-get-relative (s today default)
  15129. "Check string S for special relative date string.
  15130. TODAY and DEFAULT are internal times, for today and for a default.
  15131. Return shift list (N what def-flag)
  15132. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15133. N is the number of WHATs to shift.
  15134. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15135. the DEFAULT date rather than TODAY."
  15136. (require 'parse-time)
  15137. (when (and
  15138. (string-match
  15139. (concat
  15140. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15141. "\\([0-9]+\\)?"
  15142. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15143. "\\([ \t]\\|$\\)") s)
  15144. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15145. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15146. (string-to-char (substring (match-string 1 s) -1))
  15147. ?+))
  15148. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15149. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15150. (what (if (match-end 3) (match-string 3 s) "d"))
  15151. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15152. (date (if rel default today))
  15153. (wday (nth 6 (decode-time date)))
  15154. delta)
  15155. (if wday1
  15156. (progn
  15157. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15158. (if (= dir ?-) (setq delta (- delta 7)))
  15159. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15160. (list delta "d" rel))
  15161. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15162. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15163. "Turn a user-specified date into the internal representation.
  15164. The internal representation needed by the calendar is (month day year).
  15165. This is a wrapper to handle the brain-dead convention in calendar that
  15166. user function argument order change dependent on argument order."
  15167. (if (boundp 'calendar-date-style)
  15168. (cond
  15169. ((eq calendar-date-style 'american)
  15170. (list arg1 arg2 arg3))
  15171. ((eq calendar-date-style 'european)
  15172. (list arg2 arg1 arg3))
  15173. ((eq calendar-date-style 'iso)
  15174. (list arg2 arg3 arg1)))
  15175. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15176. (if (org-bound-and-true-p european-calendar-style)
  15177. (list arg2 arg1 arg3)
  15178. (list arg1 arg2 arg3)))))
  15179. (defun org-eval-in-calendar (form &optional keepdate)
  15180. "Eval FORM in the calendar window and return to current window.
  15181. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15182. otherwise stick to the current value of `org-ans2'."
  15183. (let ((sf (selected-frame))
  15184. (sw (selected-window)))
  15185. (select-window (get-buffer-window "*Calendar*" t))
  15186. (eval form)
  15187. (when (and (not keepdate) (calendar-cursor-to-date))
  15188. (let* ((date (calendar-cursor-to-date))
  15189. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15190. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15191. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15192. (select-window sw)
  15193. (org-select-frame-set-input-focus sf)))
  15194. (defun org-calendar-select ()
  15195. "Return to `org-read-date' with the date currently selected.
  15196. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15197. (interactive)
  15198. (when (calendar-cursor-to-date)
  15199. (let* ((date (calendar-cursor-to-date))
  15200. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15201. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15202. (if (active-minibuffer-window) (exit-minibuffer))))
  15203. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15204. "Insert a date stamp for the date given by the internal TIME.
  15205. WITH-HM means use the stamp format that includes the time of the day.
  15206. INACTIVE means use square brackets instead of angular ones, so that the
  15207. stamp will not contribute to the agenda.
  15208. PRE and POST are optional strings to be inserted before and after the
  15209. stamp.
  15210. The command returns the inserted time stamp."
  15211. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15212. stamp)
  15213. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15214. (insert-before-markers (or pre ""))
  15215. (when (listp extra)
  15216. (setq extra (car extra))
  15217. (if (and (stringp extra)
  15218. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15219. (setq extra (format "-%02d:%02d"
  15220. (string-to-number (match-string 1 extra))
  15221. (string-to-number (match-string 2 extra))))
  15222. (setq extra nil)))
  15223. (when extra
  15224. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15225. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15226. (insert-before-markers (or post ""))
  15227. (setq org-last-inserted-timestamp stamp)))
  15228. (defun org-toggle-time-stamp-overlays ()
  15229. "Toggle the use of custom time stamp formats."
  15230. (interactive)
  15231. (setq org-display-custom-times (not org-display-custom-times))
  15232. (unless org-display-custom-times
  15233. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15234. (while (setq p (next-single-property-change p 'display))
  15235. (if (and (get-text-property p 'display)
  15236. (eq (get-text-property p 'face) 'org-date))
  15237. (remove-text-properties
  15238. p (setq p (next-single-property-change p 'display))
  15239. '(display t))))
  15240. (set-buffer-modified-p bmp)))
  15241. (if (featurep 'xemacs)
  15242. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15243. (org-restart-font-lock)
  15244. (setq org-table-may-need-update t)
  15245. (if org-display-custom-times
  15246. (message "Time stamps are overlaid with custom format")
  15247. (message "Time stamp overlays removed")))
  15248. (defun org-display-custom-time (beg end)
  15249. "Overlay modified time stamp format over timestamp between BEG and END."
  15250. (let* ((ts (buffer-substring beg end))
  15251. t1 w1 with-hm tf time str w2 (off 0))
  15252. (save-match-data
  15253. (setq t1 (org-parse-time-string ts t))
  15254. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15255. (setq off (- (match-end 0) (match-beginning 0)))))
  15256. (setq end (- end off))
  15257. (setq w1 (- end beg)
  15258. with-hm (and (nth 1 t1) (nth 2 t1))
  15259. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15260. time (org-fix-decoded-time t1)
  15261. str (org-add-props
  15262. (format-time-string
  15263. (substring tf 1 -1) (apply 'encode-time time))
  15264. nil 'mouse-face 'highlight)
  15265. w2 (length str))
  15266. (if (not (= w2 w1))
  15267. (add-text-properties (1+ beg) (+ 2 beg)
  15268. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15269. (if (featurep 'xemacs)
  15270. (progn
  15271. (put-text-property beg end 'invisible t)
  15272. (put-text-property beg end 'end-glyph (make-glyph str)))
  15273. (put-text-property beg end 'display str))))
  15274. (defun org-translate-time (string)
  15275. "Translate all timestamps in STRING to custom format.
  15276. But do this only if the variable `org-display-custom-times' is set."
  15277. (when org-display-custom-times
  15278. (save-match-data
  15279. (let* ((start 0)
  15280. (re org-ts-regexp-both)
  15281. t1 with-hm inactive tf time str beg end)
  15282. (while (setq start (string-match re string start))
  15283. (setq beg (match-beginning 0)
  15284. end (match-end 0)
  15285. t1 (save-match-data
  15286. (org-parse-time-string (substring string beg end) t))
  15287. with-hm (and (nth 1 t1) (nth 2 t1))
  15288. inactive (equal (substring string beg (1+ beg)) "[")
  15289. tf (funcall (if with-hm 'cdr 'car)
  15290. org-time-stamp-custom-formats)
  15291. time (org-fix-decoded-time t1)
  15292. str (format-time-string
  15293. (concat
  15294. (if inactive "[" "<") (substring tf 1 -1)
  15295. (if inactive "]" ">"))
  15296. (apply 'encode-time time))
  15297. string (replace-match str t t string)
  15298. start (+ start (length str)))))))
  15299. string)
  15300. (defun org-fix-decoded-time (time)
  15301. "Set 0 instead of nil for the first 6 elements of time.
  15302. Don't touch the rest."
  15303. (let ((n 0))
  15304. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15305. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15306. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15307. "Difference between TIMESTAMP-STRING and now in days.
  15308. If SECONDS is non-nil, return the difference in seconds."
  15309. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15310. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15311. (funcall fdiff (current-time)))))
  15312. (defun org-deadline-close (timestamp-string &optional ndays)
  15313. "Is the time in TIMESTAMP-STRING close to the current date?"
  15314. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15315. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15316. (not (org-entry-is-done-p))))
  15317. (defun org-get-wdays (ts &optional delay zero-delay)
  15318. "Get the deadline lead time appropriate for timestring TS.
  15319. When DELAY is non-nil, get the delay time for scheduled items
  15320. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15321. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15322. don't try to find the delay cookie in the scheduled timestamp."
  15323. (let ((tv (if delay org-scheduled-delay-days
  15324. org-deadline-warning-days)))
  15325. (cond
  15326. ((or (and delay (< tv 0))
  15327. (and delay zero-delay (<= tv 0))
  15328. (and (not delay) (<= tv 0)))
  15329. ;; Enforce this value no matter what
  15330. (- tv))
  15331. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15332. ;; lead time is specified.
  15333. (floor (* (string-to-number (match-string 1 ts))
  15334. (cdr (assoc (match-string 2 ts)
  15335. '(("d" . 1) ("w" . 7)
  15336. ("m" . 30.4) ("y" . 365.25)
  15337. ("h" . 0.041667)))))))
  15338. ;; go for the default.
  15339. (t tv))))
  15340. (defun org-calendar-select-mouse (ev)
  15341. "Return to `org-read-date' with the date currently selected.
  15342. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15343. (interactive "e")
  15344. (mouse-set-point ev)
  15345. (when (calendar-cursor-to-date)
  15346. (let* ((date (calendar-cursor-to-date))
  15347. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15348. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15349. (if (active-minibuffer-window) (exit-minibuffer))))
  15350. (defun org-check-deadlines (ndays)
  15351. "Check if there are any deadlines due or past due.
  15352. A deadline is considered due if it happens within `org-deadline-warning-days'
  15353. days from today's date. If the deadline appears in an entry marked DONE,
  15354. it is not shown. The prefix arg NDAYS can be used to test that many
  15355. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15356. (interactive "P")
  15357. (let* ((org-warn-days
  15358. (cond
  15359. ((equal ndays '(4)) 100000)
  15360. (ndays (prefix-numeric-value ndays))
  15361. (t (abs org-deadline-warning-days))))
  15362. (case-fold-search nil)
  15363. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15364. (callback
  15365. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15366. (message "%d deadlines past-due or due within %d days"
  15367. (org-occur regexp nil callback)
  15368. org-warn-days)))
  15369. (defsubst org-re-timestamp (type)
  15370. "Return a regexp for timestamp TYPE.
  15371. Allowed values for TYPE are:
  15372. all: all timestamps
  15373. active: only active timestamps (<...>)
  15374. inactive: only inactive timestamps ([...])
  15375. scheduled: only scheduled timestamps
  15376. deadline: only deadline timestamps
  15377. closed: only closed time-stamps
  15378. When TYPE is nil, fall back on returning a regexp that matches
  15379. both scheduled and deadline timestamps."
  15380. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15381. ((eq type 'active) org-ts-regexp)
  15382. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15383. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15384. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15385. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15386. ((eq type 'scheduled-or-deadline)
  15387. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15388. (defun org-check-before-date (date)
  15389. "Check if there are deadlines or scheduled entries before DATE."
  15390. (interactive (list (org-read-date)))
  15391. (let ((case-fold-search nil)
  15392. (regexp (org-re-timestamp org-ts-type))
  15393. (callback
  15394. (lambda () (time-less-p
  15395. (org-time-string-to-time (match-string 1))
  15396. (org-time-string-to-time date)))))
  15397. (message "%d entries before %s"
  15398. (org-occur regexp nil callback) date)))
  15399. (defun org-check-after-date (date)
  15400. "Check if there are deadlines or scheduled entries after DATE."
  15401. (interactive (list (org-read-date)))
  15402. (let ((case-fold-search nil)
  15403. (regexp (org-re-timestamp org-ts-type))
  15404. (callback
  15405. (lambda () (not
  15406. (time-less-p
  15407. (org-time-string-to-time (match-string 1))
  15408. (org-time-string-to-time date))))))
  15409. (message "%d entries after %s"
  15410. (org-occur regexp nil callback) date)))
  15411. (defun org-check-dates-range (start-date end-date)
  15412. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15413. (interactive (list (org-read-date nil nil nil "Range starts")
  15414. (org-read-date nil nil nil "Range end")))
  15415. (let ((case-fold-search nil)
  15416. (regexp (org-re-timestamp org-ts-type))
  15417. (callback
  15418. (lambda ()
  15419. (let ((match (match-string 1)))
  15420. (and
  15421. (not (time-less-p
  15422. (org-time-string-to-time match)
  15423. (org-time-string-to-time start-date)))
  15424. (time-less-p
  15425. (org-time-string-to-time match)
  15426. (org-time-string-to-time end-date)))))))
  15427. (message "%d entries between %s and %s"
  15428. (org-occur regexp nil callback) start-date end-date)))
  15429. (defun org-evaluate-time-range (&optional to-buffer)
  15430. "Evaluate a time range by computing the difference between start and end.
  15431. Normally the result is just printed in the echo area, but with prefix arg
  15432. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15433. If the time range is actually in a table, the result is inserted into the
  15434. next column.
  15435. For time difference computation, a year is assumed to be exactly 365
  15436. days in order to avoid rounding problems."
  15437. (interactive "P")
  15438. (or
  15439. (org-clock-update-time-maybe)
  15440. (save-excursion
  15441. (unless (org-at-date-range-p t)
  15442. (goto-char (point-at-bol))
  15443. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15444. (if (not (org-at-date-range-p t))
  15445. (error "Not at a time-stamp range, and none found in current line")))
  15446. (let* ((ts1 (match-string 1))
  15447. (ts2 (match-string 2))
  15448. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15449. (match-end (match-end 0))
  15450. (time1 (org-time-string-to-time ts1))
  15451. (time2 (org-time-string-to-time ts2))
  15452. (t1 (org-float-time time1))
  15453. (t2 (org-float-time time2))
  15454. (diff (abs (- t2 t1)))
  15455. (negative (< (- t2 t1) 0))
  15456. ;; (ys (floor (* 365 24 60 60)))
  15457. (ds (* 24 60 60))
  15458. (hs (* 60 60))
  15459. (fy "%dy %dd %02d:%02d")
  15460. (fy1 "%dy %dd")
  15461. (fd "%dd %02d:%02d")
  15462. (fd1 "%dd")
  15463. (fh "%02d:%02d")
  15464. y d h m align)
  15465. (if havetime
  15466. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15467. y 0
  15468. d (floor (/ diff ds)) diff (mod diff ds)
  15469. h (floor (/ diff hs)) diff (mod diff hs)
  15470. m (floor (/ diff 60)))
  15471. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15472. y 0
  15473. d (floor (+ (/ diff ds) 0.5))
  15474. h 0 m 0))
  15475. (if (not to-buffer)
  15476. (message "%s" (org-make-tdiff-string y d h m))
  15477. (if (org-at-table-p)
  15478. (progn
  15479. (goto-char match-end)
  15480. (setq align t)
  15481. (and (looking-at " *|") (goto-char (match-end 0))))
  15482. (goto-char match-end))
  15483. (if (looking-at
  15484. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15485. (replace-match ""))
  15486. (if negative (insert " -"))
  15487. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15488. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15489. (insert " " (format fh h m))))
  15490. (if align (org-table-align))
  15491. (message "Time difference inserted")))))
  15492. (defun org-make-tdiff-string (y d h m)
  15493. (let ((fmt "")
  15494. (l nil))
  15495. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15496. l (push y l)))
  15497. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15498. l (push d l)))
  15499. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15500. l (push h l)))
  15501. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15502. l (push m l)))
  15503. (apply 'format fmt (nreverse l))))
  15504. (defun org-time-string-to-time (s &optional buffer pos)
  15505. "Convert a timestamp string into internal time."
  15506. (condition-case errdata
  15507. (apply 'encode-time (org-parse-time-string s))
  15508. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15509. s (if (not (and buffer pos))
  15510. ""
  15511. (format " at %d in buffer `%s'" pos buffer))
  15512. (cdr errdata)))))
  15513. (defun org-time-string-to-seconds (s)
  15514. "Convert a timestamp string to a number of seconds."
  15515. (org-float-time (org-time-string-to-time s)))
  15516. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15517. "Convert a time stamp to an absolute day number.
  15518. If there is a specifier for a cyclic time stamp, get the closest
  15519. date to DAYNR.
  15520. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15521. The variable `date' is bound by the calendar when this is called."
  15522. (cond
  15523. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15524. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15525. daynr
  15526. (+ daynr 1000)))
  15527. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15528. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15529. (time-to-days (current-time))) (match-string 0 s)
  15530. prefer show-all))
  15531. (t (time-to-days
  15532. (condition-case errdata
  15533. (apply 'encode-time (org-parse-time-string s))
  15534. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15535. s (if (not (and buffer pos))
  15536. ""
  15537. (format " at %d in buffer `%s'" pos buffer))
  15538. (cdr errdata))))))))
  15539. (defun org-days-to-iso-week (days)
  15540. "Return the iso week number."
  15541. (require 'cal-iso)
  15542. (car (calendar-iso-from-absolute days)))
  15543. (defun org-small-year-to-year (year)
  15544. "Convert 2-digit years into 4-digit years.
  15545. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15546. The year 2000 cannot be abbreviated. Any year larger than 99
  15547. is returned unchanged."
  15548. (if (< year 38)
  15549. (setq year (+ 2000 year))
  15550. (if (< year 100)
  15551. (setq year (+ 1900 year))))
  15552. year)
  15553. (defun org-time-from-absolute (d)
  15554. "Return the time corresponding to date D.
  15555. D may be an absolute day number, or a calendar-type list (month day year)."
  15556. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15557. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15558. (defun org-calendar-holiday ()
  15559. "List of holidays, for Diary display in Org-mode."
  15560. (require 'holidays)
  15561. (let ((hl (funcall
  15562. (if (fboundp 'calendar-check-holidays)
  15563. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15564. (if hl (mapconcat 'identity hl "; "))))
  15565. (defun org-diary-sexp-entry (sexp entry date)
  15566. "Process a SEXP diary ENTRY for DATE."
  15567. (require 'diary-lib)
  15568. (let ((result (if calendar-debug-sexp
  15569. (let ((stack-trace-on-error t))
  15570. (eval (car (read-from-string sexp))))
  15571. (condition-case nil
  15572. (eval (car (read-from-string sexp)))
  15573. (error
  15574. (beep)
  15575. (message "Bad sexp at line %d in %s: %s"
  15576. (org-current-line)
  15577. (buffer-file-name) sexp)
  15578. (sleep-for 2))))))
  15579. (cond ((stringp result) (split-string result "; "))
  15580. ((and (consp result)
  15581. (not (consp (cdr result)))
  15582. (stringp (cdr result))) (cdr result))
  15583. ((and (consp result)
  15584. (stringp (car result))) result)
  15585. (result entry))))
  15586. (defun org-diary-to-ical-string (frombuf)
  15587. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15588. This uses the icalendar.el library."
  15589. (let* ((tmpdir (if (featurep 'xemacs)
  15590. (temp-directory)
  15591. temporary-file-directory))
  15592. (tmpfile (make-temp-name
  15593. (expand-file-name "orgics" tmpdir)))
  15594. buf rtn b e)
  15595. (with-current-buffer frombuf
  15596. (icalendar-export-region (point-min) (point-max) tmpfile)
  15597. (setq buf (find-buffer-visiting tmpfile))
  15598. (set-buffer buf)
  15599. (goto-char (point-min))
  15600. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15601. (setq b (match-beginning 0)))
  15602. (goto-char (point-max))
  15603. (if (re-search-backward "^END:VEVENT" nil t)
  15604. (setq e (match-end 0)))
  15605. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15606. (kill-buffer buf)
  15607. (delete-file tmpfile)
  15608. rtn))
  15609. (defun org-closest-date (start current change prefer show-all)
  15610. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15611. When PREFER is `past', return a date that is either CURRENT or past.
  15612. When PREFER is `future', return a date that is either CURRENT or future.
  15613. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15614. ;; Make the proper lists from the dates
  15615. (catch 'exit
  15616. (let ((a1 '(("h" . hour)
  15617. ("d" . day)
  15618. ("w" . week)
  15619. ("m" . month)
  15620. ("y" . year)))
  15621. (shour (nth 2 (org-parse-time-string start)))
  15622. dn dw sday cday n1 n2 n0
  15623. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15624. (setq start (org-date-to-gregorian start)
  15625. current (org-date-to-gregorian
  15626. (if show-all
  15627. current
  15628. (time-to-days (current-time))))
  15629. sday (calendar-absolute-from-gregorian start)
  15630. cday (calendar-absolute-from-gregorian current))
  15631. (if (<= cday sday) (throw 'exit sday))
  15632. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15633. (setq dn (string-to-number (match-string 1 change))
  15634. dw (cdr (assoc (match-string 2 change) a1)))
  15635. (error "Invalid change specifier: %s" change))
  15636. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15637. (cond
  15638. ((eq dw 'hour)
  15639. (let ((missing-hours
  15640. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15641. dn)))
  15642. (setq n1 (if (zerop missing-hours) cday
  15643. (- cday (1+ (floor (/ missing-hours 24)))))
  15644. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15645. ((eq dw 'day)
  15646. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15647. n2 (+ n1 dn)))
  15648. ((eq dw 'year)
  15649. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15650. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15651. (setq date1 (list m d y1)
  15652. n1 (calendar-absolute-from-gregorian date1)
  15653. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15654. n2 (calendar-absolute-from-gregorian date2)))
  15655. ((eq dw 'month)
  15656. ;; approx number of month between the two dates
  15657. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15658. ;; How often does dn fit in there?
  15659. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15660. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15661. m (+ m nm)
  15662. ny (floor (/ m 12))
  15663. y (+ y ny)
  15664. m (- m (* ny 12)))
  15665. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15666. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15667. (setq m2 (+ m dn) y2 y)
  15668. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15669. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15670. (while (<= n2 cday)
  15671. (setq n1 n2 m m2 y y2)
  15672. (setq m2 (+ m dn) y2 y)
  15673. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15674. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15675. ;; Make sure n1 is the earlier date
  15676. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15677. (if show-all
  15678. (cond
  15679. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15680. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15681. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15682. (cond
  15683. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15684. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15685. (t (if (= cday n1) n1 n2)))))))
  15686. (defun org-date-to-gregorian (date)
  15687. "Turn any specification of DATE into a Gregorian date for the calendar."
  15688. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15689. ((and (listp date) (= (length date) 3)) date)
  15690. ((stringp date)
  15691. (setq date (org-parse-time-string date))
  15692. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15693. ((listp date)
  15694. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15695. (defun org-parse-time-string (s &optional nodefault)
  15696. "Parse the standard Org-mode time string.
  15697. This should be a lot faster than the normal `parse-time-string'.
  15698. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15699. hour and minute fields will be nil if not given."
  15700. (cond ((string-match org-ts-regexp0 s)
  15701. (list 0
  15702. (if (or (match-beginning 8) (not nodefault))
  15703. (string-to-number (or (match-string 8 s) "0")))
  15704. (if (or (match-beginning 7) (not nodefault))
  15705. (string-to-number (or (match-string 7 s) "0")))
  15706. (string-to-number (match-string 4 s))
  15707. (string-to-number (match-string 3 s))
  15708. (string-to-number (match-string 2 s))
  15709. nil nil nil))
  15710. ((string-match "^<[^>]+>$" s)
  15711. (decode-time (seconds-to-time (org-matcher-time s))))
  15712. (t (error "Not a standard Org-mode time string: %s" s))))
  15713. (defun org-timestamp-up (&optional arg)
  15714. "Increase the date item at the cursor by one.
  15715. If the cursor is on the year, change the year. If it is on the month,
  15716. the day or the time, change that.
  15717. With prefix ARG, change by that many units."
  15718. (interactive "p")
  15719. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15720. (defun org-timestamp-down (&optional arg)
  15721. "Decrease the date item at the cursor by one.
  15722. If the cursor is on the year, change the year. If it is on the month,
  15723. the day or the time, change that.
  15724. With prefix ARG, change by that many units."
  15725. (interactive "p")
  15726. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15727. (defun org-timestamp-up-day (&optional arg)
  15728. "Increase the date in the time stamp by one day.
  15729. With prefix ARG, change that many days."
  15730. (interactive "p")
  15731. (if (and (not (org-at-timestamp-p t))
  15732. (org-at-heading-p))
  15733. (org-todo 'up)
  15734. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15735. (defun org-timestamp-down-day (&optional arg)
  15736. "Decrease the date in the time stamp by one day.
  15737. With prefix ARG, change that many days."
  15738. (interactive "p")
  15739. (if (and (not (org-at-timestamp-p t))
  15740. (org-at-heading-p))
  15741. (org-todo 'down)
  15742. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15743. (defun org-at-timestamp-p (&optional inactive-ok)
  15744. "Determine if the cursor is in or at a timestamp."
  15745. (interactive)
  15746. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15747. (pos (point))
  15748. (ans (or (looking-at tsr)
  15749. (save-excursion
  15750. (skip-chars-backward "^[<\n\r\t")
  15751. (if (> (point) (point-min)) (backward-char 1))
  15752. (and (looking-at tsr)
  15753. (> (- (match-end 0) pos) -1))))))
  15754. (and ans
  15755. (boundp 'org-ts-what)
  15756. (setq org-ts-what
  15757. (cond
  15758. ((= pos (match-beginning 0)) 'bracket)
  15759. ;; Point is considered to be "on the bracket" whether
  15760. ;; it's really on it or right after it.
  15761. ((= pos (1- (match-end 0))) 'bracket)
  15762. ((= pos (match-end 0)) 'after)
  15763. ((org-pos-in-match-range pos 2) 'year)
  15764. ((org-pos-in-match-range pos 3) 'month)
  15765. ((org-pos-in-match-range pos 7) 'hour)
  15766. ((org-pos-in-match-range pos 8) 'minute)
  15767. ((or (org-pos-in-match-range pos 4)
  15768. (org-pos-in-match-range pos 5)) 'day)
  15769. ((and (> pos (or (match-end 8) (match-end 5)))
  15770. (< pos (match-end 0)))
  15771. (- pos (or (match-end 8) (match-end 5))))
  15772. (t 'day))))
  15773. ans))
  15774. (defun org-toggle-timestamp-type ()
  15775. "Toggle the type (<active> or [inactive]) of a time stamp."
  15776. (interactive)
  15777. (when (org-at-timestamp-p t)
  15778. (let ((beg (match-beginning 0)) (end (match-end 0))
  15779. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15780. (save-excursion
  15781. (goto-char beg)
  15782. (while (re-search-forward "[][<>]" end t)
  15783. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15784. t t)))
  15785. (message "Timestamp is now %sactive"
  15786. (if (equal (char-after beg) ?<) "" "in")))))
  15787. (defun org-at-clock-log-p nil
  15788. "Is the cursor on the clock log line?"
  15789. (save-excursion
  15790. (move-beginning-of-line 1)
  15791. (looking-at "^[ \t]*CLOCK:")))
  15792. (defvar org-clock-history) ; defined in org-clock.el
  15793. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15794. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15795. "Change the date in the time stamp at point.
  15796. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15797. `year', `minute', `second'. If WHAT is not given, the cursor position
  15798. in the timestamp determines what will be changed.
  15799. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15800. (let ((origin (point)) origin-cat
  15801. with-hm inactive
  15802. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15803. org-ts-what
  15804. extra rem
  15805. ts time time0 fixnext clrgx)
  15806. (if (not (org-at-timestamp-p t))
  15807. (error "Not at a timestamp"))
  15808. (if (and (not what) (eq org-ts-what 'bracket))
  15809. (org-toggle-timestamp-type)
  15810. ;; Point isn't on brackets. Remember the part of the time-stamp
  15811. ;; the point was in. Indeed, size of time-stamps may change,
  15812. ;; but point must be kept in the same category nonetheless.
  15813. (setq origin-cat org-ts-what)
  15814. (if (and (not what) (not (eq org-ts-what 'day))
  15815. org-display-custom-times
  15816. (get-text-property (point) 'display)
  15817. (not (get-text-property (1- (point)) 'display)))
  15818. (setq org-ts-what 'day))
  15819. (setq org-ts-what (or what org-ts-what)
  15820. inactive (= (char-after (match-beginning 0)) ?\[)
  15821. ts (match-string 0))
  15822. (replace-match "")
  15823. (when (string-match
  15824. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15825. ts)
  15826. (setq extra (match-string 1 ts))
  15827. (if suppress-tmp-delay
  15828. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15829. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15830. (setq with-hm t))
  15831. (setq time0 (org-parse-time-string ts))
  15832. (when (and updown
  15833. (eq org-ts-what 'minute)
  15834. (not current-prefix-arg))
  15835. ;; This looks like s-up and s-down. Change by one rounding step.
  15836. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15837. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15838. (setcar (cdr time0) (+ (nth 1 time0)
  15839. (if (> n 0) (- rem) (- dm rem))))))
  15840. (setq time
  15841. (encode-time (or (car time0) 0)
  15842. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15843. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15844. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15845. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15846. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15847. (nthcdr 6 time0)))
  15848. (when (and (member org-ts-what '(hour minute))
  15849. extra
  15850. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15851. (setq extra (org-modify-ts-extra
  15852. extra
  15853. (if (eq org-ts-what 'hour) 2 5)
  15854. n dm)))
  15855. (when (integerp org-ts-what)
  15856. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15857. (if (eq what 'calendar)
  15858. (let ((cal-date (org-get-date-from-calendar)))
  15859. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15860. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15861. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15862. (setcar time0 (or (car time0) 0))
  15863. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15864. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15865. (setq time (apply 'encode-time time0))))
  15866. ;; Insert the new time-stamp, and ensure point stays in the same
  15867. ;; category as before (i.e. not after the last position in that
  15868. ;; category).
  15869. (let ((pos (point)))
  15870. ;; Stay before inserted string. `save-excursion' is of no use.
  15871. (setq org-last-changed-timestamp
  15872. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15873. (goto-char pos))
  15874. (save-match-data
  15875. (looking-at org-ts-regexp3)
  15876. (goto-char (cond
  15877. ;; `day' category ends before `hour' if any, or at
  15878. ;; the end of the day name.
  15879. ((eq origin-cat 'day)
  15880. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15881. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15882. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15883. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15884. ;; `year' and `month' have both fixed size: point
  15885. ;; couldn't have moved into another part.
  15886. (t origin))))
  15887. ;; Update clock if on a CLOCK line.
  15888. (org-clock-update-time-maybe)
  15889. ;; Maybe adjust the closest clock in `org-clock-history'
  15890. (when org-clock-adjust-closest
  15891. (if (not (and (org-at-clock-log-p)
  15892. (< 1 (length (delq nil (mapcar 'marker-position
  15893. org-clock-history))))))
  15894. (message "No clock to adjust")
  15895. (cond ((save-excursion ; fix previous clock?
  15896. (re-search-backward org-ts-regexp0 nil t)
  15897. (org-looking-back (concat org-clock-string " \\[")))
  15898. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15899. ((save-excursion ; fix next clock?
  15900. (re-search-backward org-ts-regexp0 nil t)
  15901. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15902. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15903. (save-window-excursion
  15904. ;; Find closest clock to point, adjust the previous/next one in history
  15905. (let* ((p (save-excursion (org-back-to-heading t)))
  15906. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15907. (clfixnth
  15908. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15909. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15910. (if (not clfixpos)
  15911. (message "No clock to adjust")
  15912. (save-excursion
  15913. (org-goto-marker-or-bmk clfixpos)
  15914. (org-show-subtree)
  15915. (when (re-search-forward clrgx nil t)
  15916. (goto-char (match-beginning 1))
  15917. (let (org-clock-adjust-closest)
  15918. (org-timestamp-change n org-ts-what updown))
  15919. (message "Clock adjusted in %s for heading: %s"
  15920. (file-name-nondirectory (buffer-file-name))
  15921. (org-get-heading t t)))))))))
  15922. ;; Try to recenter the calendar window, if any.
  15923. (if (and org-calendar-follow-timestamp-change
  15924. (get-buffer-window "*Calendar*" t)
  15925. (memq org-ts-what '(day month year)))
  15926. (org-recenter-calendar (time-to-days time))))))
  15927. (defun org-modify-ts-extra (s pos n dm)
  15928. "Change the different parts of the lead-time and repeat fields in timestamp."
  15929. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15930. ng h m new rem)
  15931. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15932. (cond
  15933. ((or (org-pos-in-match-range pos 2)
  15934. (org-pos-in-match-range pos 3))
  15935. (setq m (string-to-number (match-string 3 s))
  15936. h (string-to-number (match-string 2 s)))
  15937. (if (org-pos-in-match-range pos 2)
  15938. (setq h (+ h n))
  15939. (setq n (* dm (org-no-warnings (signum n))))
  15940. (when (not (= 0 (setq rem (% m dm))))
  15941. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15942. (setq m (+ m n)))
  15943. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15944. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15945. (setq h (min 24 (max 0 h)))
  15946. (setq ng 1 new (format "-%02d:%02d" h m)))
  15947. ((org-pos-in-match-range pos 6)
  15948. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15949. ((org-pos-in-match-range pos 5)
  15950. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15951. ((org-pos-in-match-range pos 9)
  15952. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15953. ((org-pos-in-match-range pos 8)
  15954. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15955. (when ng
  15956. (setq s (concat
  15957. (substring s 0 (match-beginning ng))
  15958. new
  15959. (substring s (match-end ng))))))
  15960. s))
  15961. (defun org-recenter-calendar (date)
  15962. "If the calendar is visible, recenter it to DATE."
  15963. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15964. (when cwin
  15965. (let ((calendar-move-hook nil))
  15966. (with-selected-window cwin
  15967. (calendar-goto-date (if (listp date) date
  15968. (calendar-gregorian-from-absolute date))))))))
  15969. (defun org-goto-calendar (&optional arg)
  15970. "Go to the Emacs calendar at the current date.
  15971. If there is a time stamp in the current line, go to that date.
  15972. A prefix ARG can be used to force the current date."
  15973. (interactive "P")
  15974. (let ((tsr org-ts-regexp) diff
  15975. (calendar-move-hook nil)
  15976. (calendar-view-holidays-initially-flag nil)
  15977. (calendar-view-diary-initially-flag nil))
  15978. (if (or (org-at-timestamp-p)
  15979. (save-excursion
  15980. (beginning-of-line 1)
  15981. (looking-at (concat ".*" tsr))))
  15982. (let ((d1 (time-to-days (current-time)))
  15983. (d2 (time-to-days
  15984. (org-time-string-to-time (match-string 1)))))
  15985. (setq diff (- d2 d1))))
  15986. (calendar)
  15987. (calendar-goto-today)
  15988. (if (and diff (not arg)) (calendar-forward-day diff))))
  15989. (defun org-get-date-from-calendar ()
  15990. "Return a list (month day year) of date at point in calendar."
  15991. (with-current-buffer "*Calendar*"
  15992. (save-match-data
  15993. (calendar-cursor-to-date))))
  15994. (defun org-date-from-calendar ()
  15995. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15996. If there is already a time stamp at the cursor position, update it."
  15997. (interactive)
  15998. (if (org-at-timestamp-p t)
  15999. (org-timestamp-change 0 'calendar)
  16000. (let ((cal-date (org-get-date-from-calendar)))
  16001. (org-insert-time-stamp
  16002. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16003. (defcustom org-effort-durations
  16004. `(("h" . 60)
  16005. ("d" . ,(* 60 8))
  16006. ("w" . ,(* 60 8 5))
  16007. ("m" . ,(* 60 8 5 4))
  16008. ("y" . ,(* 60 8 5 40)))
  16009. "Conversion factor to minutes for an effort modifier.
  16010. Each entry has the form (MODIFIER . MINUTES).
  16011. In an effort string, a number followed by MODIFIER is multiplied
  16012. by the specified number of MINUTES to obtain an effort in
  16013. minutes.
  16014. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16015. effort string \"2hours\" is equivalent to 120 minutes."
  16016. :group 'org-agenda
  16017. :version "24.1"
  16018. :type '(alist :key-type (string :tag "Modifier")
  16019. :value-type (number :tag "Minutes")))
  16020. (defun org-minutes-to-clocksum-string (m)
  16021. "Format number of minutes as a clocksum string.
  16022. The format is determined by `org-time-clocksum-format',
  16023. `org-time-clocksum-use-fractional' and
  16024. `org-time-clocksum-fractional-format' and
  16025. `org-time-clocksum-use-effort-durations'."
  16026. (let ((clocksum "") h d w mo y fmt n)
  16027. (setq h (if org-time-clocksum-use-effort-durations
  16028. (cdr (assoc "h" org-effort-durations)) 60)
  16029. d (if org-time-clocksum-use-effort-durations
  16030. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16031. w (if org-time-clocksum-use-effort-durations
  16032. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16033. mo (if org-time-clocksum-use-effort-durations
  16034. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16035. y (if org-time-clocksum-use-effort-durations
  16036. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16037. ;; fractional format
  16038. (if org-time-clocksum-use-fractional
  16039. (cond
  16040. ;; single format string
  16041. ((stringp org-time-clocksum-fractional-format)
  16042. (format org-time-clocksum-fractional-format (/ m (float h))))
  16043. ;; choice of fractional formats for different time units
  16044. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16045. (> (/ (truncate m) (* y d h)) 0))
  16046. (format fmt (/ m (* y d (float h)))))
  16047. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16048. (> (/ (truncate m) (* mo d h)) 0))
  16049. (format fmt (/ m (* mo d (float h)))))
  16050. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16051. (> (/ (truncate m) (* w d h)) 0))
  16052. (format fmt (/ m (* w d (float h)))))
  16053. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16054. (> (/ (truncate m) (* d h)) 0))
  16055. (format fmt (/ m (* d (float h)))))
  16056. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16057. (> (/ (truncate m) h) 0))
  16058. (format fmt (/ m (float h))))
  16059. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16060. (format fmt m))
  16061. ;; fall back to smallest time unit with a format
  16062. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16063. (format fmt (/ m (float h))))
  16064. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16065. (format fmt (/ m (* d (float h)))))
  16066. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16067. (format fmt (/ m (* w d (float h)))))
  16068. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16069. (format fmt (/ m (* mo d (float h)))))
  16070. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16071. (format fmt (/ m (* y d (float h))))))
  16072. ;; standard (non-fractional) format, with single format string
  16073. (if (stringp org-time-clocksum-format)
  16074. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16075. ;; separate formats components
  16076. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16077. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16078. (plist-get org-time-clocksum-format :require-years))
  16079. (setq clocksum (concat clocksum (format fmt n))
  16080. m (- m (* n y d h))))
  16081. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16082. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16083. (plist-get org-time-clocksum-format :require-months))
  16084. (setq clocksum (concat clocksum (format fmt n))
  16085. m (- m (* n mo d h))))
  16086. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16087. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16088. (plist-get org-time-clocksum-format :require-weeks))
  16089. (setq clocksum (concat clocksum (format fmt n))
  16090. m (- m (* n w d h))))
  16091. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16092. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16093. (plist-get org-time-clocksum-format :require-days))
  16094. (setq clocksum (concat clocksum (format fmt n))
  16095. m (- m (* n d h))))
  16096. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16097. (or (> (setq n (/ (truncate m) h)) 0)
  16098. (plist-get org-time-clocksum-format :require-hours))
  16099. (setq clocksum (concat clocksum (format fmt n))
  16100. m (- m (* n h))))
  16101. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16102. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16103. (setq clocksum (concat clocksum (format fmt m))))
  16104. ;; return formatted time duration
  16105. clocksum))))
  16106. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16107. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16108. "Org mode version 8.0")
  16109. (defun org-hours-to-clocksum-string (n)
  16110. (org-minutes-to-clocksum-string (* n 60)))
  16111. (defun org-hh:mm-string-to-minutes (s)
  16112. "Convert a string H:MM to a number of minutes.
  16113. If the string is just a number, interpret it as minutes.
  16114. In fact, the first hh:mm or number in the string will be taken,
  16115. there can be extra stuff in the string.
  16116. If no number is found, the return value is 0."
  16117. (cond
  16118. ((integerp s) s)
  16119. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16120. (+ (* (string-to-number (match-string 1 s)) 60)
  16121. (string-to-number (match-string 2 s))))
  16122. ((string-match "\\([0-9]+\\)" s)
  16123. (string-to-number (match-string 1 s)))
  16124. (t 0)))
  16125. (defcustom org-image-actual-width t
  16126. "Should we use the actual width of images when inlining them?
  16127. When set to `t', always use the image width.
  16128. When set to a number, use imagemagick (when available) to set
  16129. the image's width to this value.
  16130. When set to a number in a list, try to get the width from any
  16131. #+ATTR.* keyword if it matches a width specification like
  16132. #+ATTR_HTML: :width 300px
  16133. and fall back on that number if none is found.
  16134. When set to nil, try to get the width from an #+ATTR.* keyword
  16135. and fall back on the original width if none is found.
  16136. This requires Emacs >= 24.1, build with imagemagick support."
  16137. :group 'org-appearance
  16138. :version "24.4"
  16139. :package-version '(Org . "8.0")
  16140. :type '(choice
  16141. (const :tag "Use the image width" t)
  16142. (integer :tag "Use a number of pixels")
  16143. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16144. (const :tag "Use #+ATTR* or don't resize" nil)))
  16145. (defcustom org-agenda-inhibit-startup nil
  16146. "Inhibit startup when preparing agenda buffers.
  16147. When this variable is `t' (the default), the initialization of
  16148. the Org agenda buffers is inhibited: e.g. the visibility state
  16149. is not set, the tables are not re-aligned, etc."
  16150. :type 'boolean
  16151. :version "24.3"
  16152. :group 'org-agenda)
  16153. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16154. "Convert a duration string S to minutes.
  16155. A bare number is interpreted as minutes, modifiers can be set by
  16156. customizing `org-effort-durations' (which see).
  16157. Entries containing a colon are interpreted as H:MM by
  16158. `org-hh:mm-string-to-minutes'."
  16159. (let ((result 0)
  16160. (re (concat "\\([0-9.]+\\) *\\("
  16161. (regexp-opt (mapcar 'car org-effort-durations))
  16162. "\\)")))
  16163. (while (string-match re s)
  16164. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16165. (string-to-number (match-string 1 s))))
  16166. (setq s (replace-match "" nil t s)))
  16167. (setq result (floor result))
  16168. (incf result (org-hh:mm-string-to-minutes s))
  16169. (if output-to-string (number-to-string result) result)))
  16170. ;;;; Files
  16171. (defun org-save-all-org-buffers ()
  16172. "Save all Org-mode buffers without user confirmation."
  16173. (interactive)
  16174. (message "Saving all Org-mode buffers...")
  16175. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16176. (when (featurep 'org-id) (org-id-locations-save))
  16177. (message "Saving all Org-mode buffers... done"))
  16178. (defun org-revert-all-org-buffers ()
  16179. "Revert all Org-mode buffers.
  16180. Prompt for confirmation when there are unsaved changes.
  16181. Be sure you know what you are doing before letting this function
  16182. overwrite your changes.
  16183. This function is useful in a setup where one tracks org files
  16184. with a version control system, to revert on one machine after pulling
  16185. changes from another. I believe the procedure must be like this:
  16186. 1. M-x org-save-all-org-buffers
  16187. 2. Pull changes from the other machine, resolve conflicts
  16188. 3. M-x org-revert-all-org-buffers"
  16189. (interactive)
  16190. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16191. (error "Abort"))
  16192. (save-excursion
  16193. (save-window-excursion
  16194. (mapc
  16195. (lambda (b)
  16196. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16197. (with-current-buffer b buffer-file-name))
  16198. (org-pop-to-buffer-same-window b)
  16199. (revert-buffer t 'no-confirm)))
  16200. (buffer-list))
  16201. (when (and (featurep 'org-id) org-id-track-globally)
  16202. (org-id-locations-load)))))
  16203. ;;;; Agenda files
  16204. ;;;###autoload
  16205. (defun org-switchb (&optional arg)
  16206. "Switch between Org buffers.
  16207. With one prefix argument, restrict available buffers to files.
  16208. With two prefix arguments, restrict available buffers to agenda files.
  16209. Defaults to `iswitchb' for buffer name completion.
  16210. Set `org-completion-use-ido' to make it use ido instead."
  16211. (interactive "P")
  16212. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16213. ((equal arg '(16)) (org-buffer-list 'agenda))
  16214. (t (org-buffer-list))))
  16215. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16216. (org-completion-use-ido org-completion-use-ido))
  16217. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16218. (setq org-completion-use-iswitchb t))
  16219. (org-pop-to-buffer-same-window
  16220. (org-icompleting-read "Org buffer: "
  16221. (mapcar 'list (mapcar 'buffer-name blist))
  16222. nil t))))
  16223. ;;; Define some older names previously used for this functionality
  16224. ;;;###autoload
  16225. (defalias 'org-ido-switchb 'org-switchb)
  16226. ;;;###autoload
  16227. (defalias 'org-iswitchb 'org-switchb)
  16228. (defun org-buffer-list (&optional predicate exclude-tmp)
  16229. "Return a list of Org buffers.
  16230. PREDICATE can be `export', `files' or `agenda'.
  16231. export restrict the list to Export buffers.
  16232. files restrict the list to buffers visiting Org files.
  16233. agenda restrict the list to buffers visiting agenda files.
  16234. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16235. (let* ((bfn nil)
  16236. (agenda-files (and (eq predicate 'agenda)
  16237. (mapcar 'file-truename (org-agenda-files t))))
  16238. (filter
  16239. (cond
  16240. ((eq predicate 'files)
  16241. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16242. ((eq predicate 'export)
  16243. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16244. ((eq predicate 'agenda)
  16245. (lambda (b)
  16246. (with-current-buffer b
  16247. (and (derived-mode-p 'org-mode)
  16248. (setq bfn (buffer-file-name b))
  16249. (member (file-truename bfn) agenda-files)))))
  16250. (t (lambda (b) (with-current-buffer b
  16251. (or (derived-mode-p 'org-mode)
  16252. (string-match "\*Org .*Export"
  16253. (buffer-name b)))))))))
  16254. (delq nil
  16255. (mapcar
  16256. (lambda(b)
  16257. (if (and (funcall filter b)
  16258. (or (not exclude-tmp)
  16259. (not (string-match "tmp" (buffer-name b)))))
  16260. b
  16261. nil))
  16262. (buffer-list)))))
  16263. (defun org-agenda-files (&optional unrestricted archives)
  16264. "Get the list of agenda files.
  16265. Optional UNRESTRICTED means return the full list even if a restriction
  16266. is currently in place.
  16267. When ARCHIVES is t, include all archive files that are really being
  16268. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16269. `org-agenda-archives-mode' is t."
  16270. (let ((files
  16271. (cond
  16272. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16273. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16274. ((listp org-agenda-files) org-agenda-files)
  16275. (t (error "Invalid value of `org-agenda-files'")))))
  16276. (setq files (apply 'append
  16277. (mapcar (lambda (f)
  16278. (if (file-directory-p f)
  16279. (directory-files
  16280. f t org-agenda-file-regexp)
  16281. (list f)))
  16282. files)))
  16283. (when org-agenda-skip-unavailable-files
  16284. (setq files (delq nil
  16285. (mapcar (function
  16286. (lambda (file)
  16287. (and (file-readable-p file) file)))
  16288. files))))
  16289. (when (or (eq archives t)
  16290. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16291. (setq files (org-add-archive-files files)))
  16292. files))
  16293. (defun org-agenda-file-p (&optional file)
  16294. "Return non-nil, if FILE is an agenda file.
  16295. If FILE is omitted, use the file associated with the current
  16296. buffer."
  16297. (member (or file (buffer-file-name))
  16298. (org-agenda-files t)))
  16299. (defun org-edit-agenda-file-list ()
  16300. "Edit the list of agenda files.
  16301. Depending on setup, this either uses customize to edit the variable
  16302. `org-agenda-files', or it visits the file that is holding the list. In the
  16303. latter case, the buffer is set up in a way that saving it automatically kills
  16304. the buffer and restores the previous window configuration."
  16305. (interactive)
  16306. (if (stringp org-agenda-files)
  16307. (let ((cw (current-window-configuration)))
  16308. (find-file org-agenda-files)
  16309. (org-set-local 'org-window-configuration cw)
  16310. (org-add-hook 'after-save-hook
  16311. (lambda ()
  16312. (set-window-configuration
  16313. (prog1 org-window-configuration
  16314. (kill-buffer (current-buffer))))
  16315. (org-install-agenda-files-menu)
  16316. (message "New agenda file list installed"))
  16317. nil 'local)
  16318. (message "%s" (substitute-command-keys
  16319. "Edit list and finish with \\[save-buffer]")))
  16320. (customize-variable 'org-agenda-files)))
  16321. (defun org-store-new-agenda-file-list (list)
  16322. "Set new value for the agenda file list and save it correctly."
  16323. (if (stringp org-agenda-files)
  16324. (let ((fe (org-read-agenda-file-list t)) b u)
  16325. (while (setq b (find-buffer-visiting org-agenda-files))
  16326. (kill-buffer b))
  16327. (with-temp-file org-agenda-files
  16328. (insert
  16329. (mapconcat
  16330. (lambda (f) ;; Keep un-expanded entries.
  16331. (if (setq u (assoc f fe))
  16332. (cdr u)
  16333. f))
  16334. list "\n")
  16335. "\n")))
  16336. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16337. (org-insert-mode-line-in-empty-file nil))
  16338. (setq org-agenda-files list)
  16339. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16340. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16341. "Read the list of agenda files from a file.
  16342. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16343. filenames, used by `org-store-new-agenda-file-list' to write back
  16344. un-expanded file names."
  16345. (when (file-directory-p org-agenda-files)
  16346. (error "`org-agenda-files' cannot be a single directory"))
  16347. (when (stringp org-agenda-files)
  16348. (with-temp-buffer
  16349. (insert-file-contents org-agenda-files)
  16350. (mapcar
  16351. (lambda (f)
  16352. (let ((e (expand-file-name (substitute-in-file-name f)
  16353. org-directory)))
  16354. (if pair-with-expansion
  16355. (cons e f)
  16356. e)))
  16357. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16358. ;;;###autoload
  16359. (defun org-cycle-agenda-files ()
  16360. "Cycle through the files in `org-agenda-files'.
  16361. If the current buffer visits an agenda file, find the next one in the list.
  16362. If the current buffer does not, find the first agenda file."
  16363. (interactive)
  16364. (let* ((fs (org-agenda-files t))
  16365. (files (append fs (list (car fs))))
  16366. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16367. file)
  16368. (unless files (error "No agenda files"))
  16369. (catch 'exit
  16370. (while (setq file (pop files))
  16371. (if (equal (file-truename file) tcf)
  16372. (when (car files)
  16373. (find-file (car files))
  16374. (throw 'exit t))))
  16375. (find-file (car fs)))
  16376. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16377. (defun org-agenda-file-to-front (&optional to-end)
  16378. "Move/add the current file to the top of the agenda file list.
  16379. If the file is not present in the list, it is added to the front. If it is
  16380. present, it is moved there. With optional argument TO-END, add/move to the
  16381. end of the list."
  16382. (interactive "P")
  16383. (let ((org-agenda-skip-unavailable-files nil)
  16384. (file-alist (mapcar (lambda (x)
  16385. (cons (file-truename x) x))
  16386. (org-agenda-files t)))
  16387. (ctf (file-truename
  16388. (or buffer-file-name
  16389. (error "Please save the current buffer to a file"))))
  16390. x had)
  16391. (setq x (assoc ctf file-alist) had x)
  16392. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16393. (if to-end
  16394. (setq file-alist (append (delq x file-alist) (list x)))
  16395. (setq file-alist (cons x (delq x file-alist))))
  16396. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16397. (org-install-agenda-files-menu)
  16398. (message "File %s to %s of agenda file list"
  16399. (if had "moved" "added") (if to-end "end" "front"))))
  16400. (defun org-remove-file (&optional file)
  16401. "Remove current file from the list of files in variable `org-agenda-files'.
  16402. These are the files which are being checked for agenda entries.
  16403. Optional argument FILE means use this file instead of the current."
  16404. (interactive)
  16405. (let* ((org-agenda-skip-unavailable-files nil)
  16406. (file (or file buffer-file-name
  16407. (error "Current buffer does not visit a file")))
  16408. (true-file (file-truename file))
  16409. (afile (abbreviate-file-name file))
  16410. (files (delq nil (mapcar
  16411. (lambda (x)
  16412. (if (equal true-file
  16413. (file-truename x))
  16414. nil x))
  16415. (org-agenda-files t)))))
  16416. (if (not (= (length files) (length (org-agenda-files t))))
  16417. (progn
  16418. (org-store-new-agenda-file-list files)
  16419. (org-install-agenda-files-menu)
  16420. (message "Removed file: %s" afile))
  16421. (message "File was not in list: %s (not removed)" afile))))
  16422. (defun org-file-menu-entry (file)
  16423. (vector file (list 'find-file file) t))
  16424. (defun org-check-agenda-file (file)
  16425. "Make sure FILE exists. If not, ask user what to do."
  16426. (when (not (file-exists-p file))
  16427. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16428. (abbreviate-file-name file))
  16429. (let ((r (downcase (read-char-exclusive))))
  16430. (cond
  16431. ((equal r ?r)
  16432. (org-remove-file file)
  16433. (throw 'nextfile t))
  16434. (t (error "Abort"))))))
  16435. (defun org-get-agenda-file-buffer (file)
  16436. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16437. it to the list of buffers which might be released later."
  16438. (let ((buf (org-find-base-buffer-visiting file)))
  16439. (if buf
  16440. buf ; just return it
  16441. ;; Make a new buffer and remember it
  16442. (setq buf (find-file-noselect file))
  16443. (if buf (push buf org-agenda-new-buffers))
  16444. buf)))
  16445. (defun org-release-buffers (blist)
  16446. "Release all buffers in list, asking the user for confirmation when needed.
  16447. When a buffer is unmodified, it is just killed. When modified, it is saved
  16448. \(if the user agrees) and then killed."
  16449. (let (buf file)
  16450. (while (setq buf (pop blist))
  16451. (setq file (buffer-file-name buf))
  16452. (when (and (buffer-modified-p buf)
  16453. file
  16454. (y-or-n-p (format "Save file %s? " file)))
  16455. (with-current-buffer buf (save-buffer)))
  16456. (kill-buffer buf))))
  16457. (defun org-agenda-prepare-buffers (files)
  16458. "Create buffers for all agenda files, protect archived trees and comments."
  16459. (interactive)
  16460. (let ((pa '(:org-archived t))
  16461. (pc '(:org-comment t))
  16462. (pall '(:org-archived t :org-comment t))
  16463. (inhibit-read-only t)
  16464. (org-inhibit-startup org-agenda-inhibit-startup)
  16465. (rea (concat ":" org-archive-tag ":"))
  16466. file re)
  16467. (setq org-tag-alist-for-agenda nil
  16468. org-tag-groups-alist-for-agenda nil)
  16469. (save-excursion
  16470. (save-restriction
  16471. (while (setq file (pop files))
  16472. (catch 'nextfile
  16473. (if (bufferp file)
  16474. (set-buffer file)
  16475. (org-check-agenda-file file)
  16476. (set-buffer (org-get-agenda-file-buffer file)))
  16477. (widen)
  16478. (org-set-regexps-and-options-for-tags)
  16479. (org-refresh-category-properties)
  16480. (org-refresh-properties org-effort-property 'org-effort)
  16481. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16482. (setq org-todo-keywords-for-agenda
  16483. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16484. (setq org-done-keywords-for-agenda
  16485. (append org-done-keywords-for-agenda org-done-keywords))
  16486. (setq org-todo-keyword-alist-for-agenda
  16487. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16488. (setq org-drawers-for-agenda
  16489. (append org-drawers-for-agenda org-drawers))
  16490. (unless (equal org-tag-alist-for-agenda org-tag-alist)
  16491. (setq org-tag-alist-for-agenda
  16492. (append org-tag-alist-for-agenda org-tag-alist)))
  16493. (if org-group-tags
  16494. (setq org-tag-groups-alist-for-agenda
  16495. (org-uniquify-alist
  16496. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16497. (org-with-silent-modifications
  16498. (save-excursion
  16499. (remove-text-properties (point-min) (point-max) pall)
  16500. (when org-agenda-skip-archived-trees
  16501. (goto-char (point-min))
  16502. (while (re-search-forward rea nil t)
  16503. (if (org-at-heading-p t)
  16504. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16505. (goto-char (point-min))
  16506. (setq re (format org-heading-keyword-regexp-format
  16507. org-comment-string))
  16508. (while (re-search-forward re nil t)
  16509. (add-text-properties
  16510. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16511. (setq org-todo-keywords-for-agenda
  16512. (org-uniquify org-todo-keywords-for-agenda))
  16513. (setq org-todo-keyword-alist-for-agenda
  16514. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16515. ;;;; CDLaTeX minor mode
  16516. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16517. "Keymap for the minor `org-cdlatex-mode'.")
  16518. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16519. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16520. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16521. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16522. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16523. (defvar org-cdlatex-texmathp-advice-is-done nil
  16524. "Flag remembering if we have applied the advice to texmathp already.")
  16525. (define-minor-mode org-cdlatex-mode
  16526. "Toggle the minor `org-cdlatex-mode'.
  16527. This mode supports entering LaTeX environment and math in LaTeX fragments
  16528. in Org-mode.
  16529. \\{org-cdlatex-mode-map}"
  16530. nil " OCDL" nil
  16531. (when org-cdlatex-mode
  16532. (require 'cdlatex)
  16533. (run-hooks 'cdlatex-mode-hook)
  16534. (cdlatex-compute-tables))
  16535. (unless org-cdlatex-texmathp-advice-is-done
  16536. (setq org-cdlatex-texmathp-advice-is-done t)
  16537. (defadvice texmathp (around org-math-always-on activate)
  16538. "Always return t in org-mode buffers.
  16539. This is because we want to insert math symbols without dollars even outside
  16540. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16541. an embedded LaTeX fragment, let texmathp do its job.
  16542. \\[org-cdlatex-mode-map]"
  16543. (interactive)
  16544. (let (p)
  16545. (cond
  16546. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16547. ((eq this-command 'cdlatex-math-symbol)
  16548. (setq ad-return-value t
  16549. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16550. (t
  16551. (let ((p (org-inside-LaTeX-fragment-p)))
  16552. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16553. (setq ad-return-value t
  16554. texmathp-why '("Org-mode embedded math" . 0))
  16555. (if p ad-do-it)))))))))
  16556. (defun turn-on-org-cdlatex ()
  16557. "Unconditionally turn on `org-cdlatex-mode'."
  16558. (org-cdlatex-mode 1))
  16559. (defun org-try-cdlatex-tab ()
  16560. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16561. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16562. - inside a LaTeX fragment, or
  16563. - after the first word in a line, where an abbreviation expansion could
  16564. insert a LaTeX environment."
  16565. (when org-cdlatex-mode
  16566. (cond
  16567. ;; Before any word on the line: No expansion possible.
  16568. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16569. ;; Just after first word on the line: Expand it. Make sure it
  16570. ;; cannot happen on headlines, though.
  16571. ((save-excursion
  16572. (skip-chars-backward "a-zA-Z0-9*")
  16573. (skip-chars-backward " \t")
  16574. (and (bolp) (not (org-at-heading-p))))
  16575. (cdlatex-tab) t)
  16576. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16577. (defun org-cdlatex-underscore-caret (&optional arg)
  16578. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16579. Revert to the normal definition outside of these fragments."
  16580. (interactive "P")
  16581. (if (org-inside-LaTeX-fragment-p)
  16582. (call-interactively 'cdlatex-sub-superscript)
  16583. (let (org-cdlatex-mode)
  16584. (call-interactively (key-binding (vector last-input-event))))))
  16585. (defun org-cdlatex-math-modify (&optional arg)
  16586. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16587. Revert to the normal definition outside of these fragments."
  16588. (interactive "P")
  16589. (if (org-inside-LaTeX-fragment-p)
  16590. (call-interactively 'cdlatex-math-modify)
  16591. (let (org-cdlatex-mode)
  16592. (call-interactively (key-binding (vector last-input-event))))))
  16593. ;;;; LaTeX fragments
  16594. (defvar org-latex-regexps
  16595. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16596. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16597. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16598. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16599. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16600. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16601. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16602. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16603. "Regular expressions for matching embedded LaTeX.")
  16604. (defun org-inside-LaTeX-fragment-p ()
  16605. "Test if point is inside a LaTeX fragment.
  16606. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16607. sequence appearing also before point.
  16608. Even though the matchers for math are configurable, this function assumes
  16609. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16610. delimiters are skipped when they have been removed by customization.
  16611. The return value is nil, or a cons cell with the delimiter and the
  16612. position of this delimiter.
  16613. This function does a reasonably good job, but can locally be fooled by
  16614. for example currency specifications. For example it will assume being in
  16615. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16616. fragments that are properly closed, but during editing, we have to live
  16617. with the uncertainty caused by missing closing delimiters. This function
  16618. looks only before point, not after."
  16619. (catch 'exit
  16620. (let ((pos (point))
  16621. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16622. (lim (progn
  16623. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16624. (point)))
  16625. dd-on str (start 0) m re)
  16626. (goto-char pos)
  16627. (when dodollar
  16628. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16629. re (nth 1 (assoc "$" org-latex-regexps)))
  16630. (while (string-match re str start)
  16631. (cond
  16632. ((= (match-end 0) (length str))
  16633. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16634. ((= (match-end 0) (- (length str) 5))
  16635. (throw 'exit nil))
  16636. (t (setq start (match-end 0))))))
  16637. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16638. (goto-char pos)
  16639. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16640. (and (match-beginning 2) (throw 'exit nil))
  16641. ;; count $$
  16642. (while (re-search-backward "\\$\\$" lim t)
  16643. (setq dd-on (not dd-on)))
  16644. (goto-char pos)
  16645. (if dd-on (cons "$$" m))))))
  16646. (defun org-inside-latex-macro-p ()
  16647. "Is point inside a LaTeX macro or its arguments?"
  16648. (save-match-data
  16649. (org-in-regexp
  16650. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16651. (defvar org-latex-fragment-image-overlays nil
  16652. "List of overlays carrying the images of latex fragments.")
  16653. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16654. (defun org-remove-latex-fragment-image-overlays ()
  16655. "Remove all overlays with LaTeX fragment images in current buffer."
  16656. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16657. (setq org-latex-fragment-image-overlays nil))
  16658. (defun org-preview-latex-fragment (&optional subtree)
  16659. "Preview the LaTeX fragment at point, or all locally or globally.
  16660. If the cursor is in a LaTeX fragment, create the image and overlay
  16661. it over the source code. If there is no fragment at point, display
  16662. all fragments in the current text, from one headline to the next. With
  16663. prefix SUBTREE, display all fragments in the current subtree. With a
  16664. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16665. the cursor is before the first headline,
  16666. display all fragments in the buffer.
  16667. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16668. (interactive "P")
  16669. (unless buffer-file-name
  16670. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16671. (org-remove-latex-fragment-image-overlays)
  16672. (save-excursion
  16673. (save-restriction
  16674. (let (beg end at msg)
  16675. (cond
  16676. ((or (equal subtree '(16))
  16677. (not (save-excursion
  16678. (re-search-backward org-outline-regexp-bol nil t))))
  16679. (setq beg (point-min) end (point-max)
  16680. msg "Creating images for buffer...%s"))
  16681. ((equal subtree '(4))
  16682. (org-back-to-heading)
  16683. (setq beg (point) end (org-end-of-subtree t)
  16684. msg "Creating images for subtree...%s"))
  16685. (t
  16686. (if (setq at (org-inside-LaTeX-fragment-p))
  16687. (goto-char (max (point-min) (- (cdr at) 2)))
  16688. (org-back-to-heading))
  16689. (setq beg (point) end (progn (outline-next-heading) (point))
  16690. msg (if at "Creating image...%s"
  16691. "Creating images for entry...%s"))))
  16692. (message msg "")
  16693. (narrow-to-region beg end)
  16694. (goto-char beg)
  16695. (org-format-latex
  16696. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16697. (file-name-nondirectory
  16698. buffer-file-name)))
  16699. default-directory 'overlays msg at 'forbuffer
  16700. org-latex-create-formula-image-program)
  16701. (message msg "done. Use `C-c C-c' to remove images.")))))
  16702. (defun org-format-latex (prefix &optional dir overlays msg at
  16703. forbuffer processing-type)
  16704. "Replace LaTeX fragments with links to an image, and produce images.
  16705. Some of the options can be changed using the variable
  16706. `org-format-latex-options'."
  16707. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16708. (let* ((prefixnodir (file-name-nondirectory prefix))
  16709. (absprefix (expand-file-name prefix dir))
  16710. (todir (file-name-directory absprefix))
  16711. (opt org-format-latex-options)
  16712. (optnew org-format-latex-options)
  16713. (matchers (plist-get opt :matchers))
  16714. (re-list org-latex-regexps)
  16715. (cnt 0) txt hash link beg end re e checkdir
  16716. string
  16717. m n block-type block linkfile movefile ov)
  16718. ;; Check the different regular expressions
  16719. (while (setq e (pop re-list))
  16720. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16721. block (if block-type "\n\n" ""))
  16722. (when (member m matchers)
  16723. (goto-char (point-min))
  16724. (while (re-search-forward re nil t)
  16725. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16726. (or (not overlays)
  16727. (not (eq (get-char-property (match-beginning n)
  16728. 'org-overlay-type)
  16729. 'org-latex-overlay))))
  16730. (cond
  16731. ((eq processing-type 'verbatim))
  16732. ((eq processing-type 'mathjax)
  16733. ;; Prepare for MathJax processing.
  16734. (setq string (match-string n))
  16735. (when (member m '("$" "$1"))
  16736. (save-excursion
  16737. (delete-region (match-beginning n) (match-end n))
  16738. (goto-char (match-beginning n))
  16739. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16740. ((or (eq processing-type 'dvipng)
  16741. (eq processing-type 'imagemagick))
  16742. ;; Process to an image.
  16743. (setq txt (match-string n)
  16744. beg (match-beginning n) end (match-end n)
  16745. cnt (1+ cnt))
  16746. (let ((face (face-at-point))
  16747. (fg (plist-get opt :foreground))
  16748. (bg (plist-get opt :background))
  16749. ;; Ensure full list is printed.
  16750. print-length print-level)
  16751. (when forbuffer
  16752. ;; Get the colors from the face at point.
  16753. (goto-char beg)
  16754. (when (eq fg 'auto)
  16755. (setq fg (face-attribute face :foreground nil 'default)))
  16756. (when (eq bg 'auto)
  16757. (setq bg (face-attribute face :background nil 'default)))
  16758. (setq optnew (copy-sequence opt))
  16759. (plist-put optnew :foreground fg)
  16760. (plist-put optnew :background bg))
  16761. (setq hash (sha1 (prin1-to-string
  16762. (list org-format-latex-header
  16763. org-latex-default-packages-alist
  16764. org-latex-packages-alist
  16765. org-format-latex-options
  16766. forbuffer txt fg bg)))
  16767. linkfile (format "%s_%s.png" prefix hash)
  16768. movefile (format "%s_%s.png" absprefix hash)))
  16769. (setq link (concat block "[[file:" linkfile "]]" block))
  16770. (if msg (message msg cnt))
  16771. (goto-char beg)
  16772. (unless checkdir ; Ensure the directory exists.
  16773. (setq checkdir t)
  16774. (or (file-directory-p todir) (make-directory todir t)))
  16775. (unless (file-exists-p movefile)
  16776. (org-create-formula-image
  16777. txt movefile optnew forbuffer processing-type))
  16778. (if overlays
  16779. (progn
  16780. (mapc (lambda (o)
  16781. (if (eq (overlay-get o 'org-overlay-type)
  16782. 'org-latex-overlay)
  16783. (delete-overlay o)))
  16784. (overlays-in beg end))
  16785. (setq ov (make-overlay beg end))
  16786. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16787. (if (featurep 'xemacs)
  16788. (progn
  16789. (overlay-put ov 'invisible t)
  16790. (overlay-put
  16791. ov 'end-glyph
  16792. (make-glyph (vector 'png :file movefile))))
  16793. (overlay-put
  16794. ov 'display
  16795. (list 'image :type 'png :file movefile :ascent 'center)))
  16796. (push ov org-latex-fragment-image-overlays)
  16797. (goto-char end))
  16798. (delete-region beg end)
  16799. (insert (org-add-props link
  16800. (list 'org-latex-src
  16801. (replace-regexp-in-string
  16802. "\"" "" txt)
  16803. 'org-latex-src-embed-type
  16804. (if block-type 'paragraph 'character))))))
  16805. ((eq processing-type 'mathml)
  16806. ;; Process to MathML
  16807. (unless (save-match-data (org-format-latex-mathml-available-p))
  16808. (error "LaTeX to MathML converter not configured"))
  16809. (setq txt (match-string n)
  16810. beg (match-beginning n) end (match-end n)
  16811. cnt (1+ cnt))
  16812. (if msg (message msg cnt))
  16813. (goto-char beg)
  16814. (delete-region beg end)
  16815. (insert (org-format-latex-as-mathml
  16816. txt block-type prefix dir)))
  16817. (t
  16818. (error "Unknown conversion type %s for latex fragments"
  16819. processing-type)))))))))
  16820. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16821. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16822. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16823. sucessful, return the portion between \"<math...> </math>\"
  16824. elements otherwise return nil. When MATHML-FILE is specified,
  16825. write the results in to that file. When invoked as an
  16826. interactive command, prompt for LATEX-FRAG, with initial value
  16827. set to the current active region and echo the results for user
  16828. inspection."
  16829. (interactive (list (let ((frag (when (org-region-active-p)
  16830. (buffer-substring-no-properties
  16831. (region-beginning) (region-end)))))
  16832. (read-string "LaTeX Fragment: " frag nil frag))))
  16833. (unless latex-frag (error "Invalid latex-frag"))
  16834. (let* ((tmp-in-file (file-relative-name
  16835. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16836. (ignore (write-region latex-frag nil tmp-in-file))
  16837. (tmp-out-file (file-relative-name
  16838. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16839. (cmd (format-spec
  16840. org-latex-to-mathml-convert-command
  16841. `((?j . ,(shell-quote-argument
  16842. (expand-file-name org-latex-to-mathml-jar-file)))
  16843. (?I . ,(shell-quote-argument tmp-in-file))
  16844. (?o . ,(shell-quote-argument tmp-out-file)))))
  16845. mathml shell-command-output)
  16846. (when (org-called-interactively-p 'any)
  16847. (unless (org-format-latex-mathml-available-p)
  16848. (error "LaTeX to MathML converter not configured")))
  16849. (message "Running %s" cmd)
  16850. (setq shell-command-output (shell-command-to-string cmd))
  16851. (setq mathml
  16852. (when (file-readable-p tmp-out-file)
  16853. (with-current-buffer (find-file-noselect tmp-out-file t)
  16854. (goto-char (point-min))
  16855. (when (re-search-forward
  16856. (concat
  16857. (regexp-quote
  16858. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16859. "\\(.\\|\n\\)*"
  16860. (regexp-quote "</math>")) nil t)
  16861. (prog1 (match-string 0) (kill-buffer))))))
  16862. (cond
  16863. (mathml
  16864. (setq mathml
  16865. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16866. (when mathml-file
  16867. (write-region mathml nil mathml-file))
  16868. (when (org-called-interactively-p 'any)
  16869. (message mathml)))
  16870. ((message "LaTeX to MathML conversion failed")
  16871. (message shell-command-output)))
  16872. (delete-file tmp-in-file)
  16873. (when (file-exists-p tmp-out-file)
  16874. (delete-file tmp-out-file))
  16875. mathml))
  16876. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16877. prefix &optional dir)
  16878. "Use `org-create-math-formula' but check local cache first."
  16879. (let* ((absprefix (expand-file-name prefix dir))
  16880. (print-length nil) (print-level nil)
  16881. (formula-id (concat
  16882. "formula-"
  16883. (sha1
  16884. (prin1-to-string
  16885. (list latex-frag
  16886. org-latex-to-mathml-convert-command)))))
  16887. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16888. (formula-cache-dir (file-name-directory formula-cache)))
  16889. (unless (file-directory-p formula-cache-dir)
  16890. (make-directory formula-cache-dir t))
  16891. (unless (file-exists-p formula-cache)
  16892. (org-create-math-formula latex-frag formula-cache))
  16893. (if (file-exists-p formula-cache)
  16894. ;; Successful conversion. Return the link to MathML file.
  16895. (org-add-props
  16896. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16897. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16898. 'org-latex-src-embed-type (if latex-frag-type
  16899. 'paragraph 'character)))
  16900. ;; Failed conversion. Return the LaTeX fragment verbatim
  16901. latex-frag)))
  16902. (defun org-create-formula-image (string tofile options buffer &optional type)
  16903. "Create an image from LaTeX source using dvipng or convert.
  16904. This function calls either `org-create-formula-image-with-dvipng'
  16905. or `org-create-formula-image-with-imagemagick' depending on the
  16906. value of `org-latex-create-formula-image-program' or on the value
  16907. of the optional TYPE variable.
  16908. Note: ultimately these two function should be combined as they
  16909. share a good deal of logic."
  16910. (org-check-external-command
  16911. "latex" "needed to convert LaTeX fragments to images")
  16912. (funcall
  16913. (case (or type org-latex-create-formula-image-program)
  16914. ('dvipng
  16915. (org-check-external-command
  16916. "dvipng" "needed to convert LaTeX fragments to images")
  16917. #'org-create-formula-image-with-dvipng)
  16918. ('imagemagick
  16919. (org-check-external-command
  16920. "convert" "you need to install imagemagick")
  16921. #'org-create-formula-image-with-imagemagick)
  16922. (t (error
  16923. "invalid value of `org-latex-create-formula-image-program'")))
  16924. string tofile options buffer))
  16925. (declare-function org-export--get-global-options "ox" (&optional backend))
  16926. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16927. (defun org-create-formula--latex-header ()
  16928. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16929. (org-latex-guess-inputenc
  16930. (org-splice-latex-header
  16931. org-format-latex-header
  16932. org-latex-default-packages-alist
  16933. org-latex-packages-alist t
  16934. (plist-get
  16935. (org-combine-plists
  16936. (org-export--get-global-options 'latex)
  16937. (org-export--get-inbuffer-options 'latex))
  16938. :latex-header))))
  16939. ;; This function borrows from Ganesh Swami's latex2png.el
  16940. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16941. "This calls dvipng."
  16942. (require 'ox-latex)
  16943. (let* ((tmpdir (if (featurep 'xemacs)
  16944. (temp-directory)
  16945. temporary-file-directory))
  16946. (texfilebase (make-temp-name
  16947. (expand-file-name "orgtex" tmpdir)))
  16948. (texfile (concat texfilebase ".tex"))
  16949. (dvifile (concat texfilebase ".dvi"))
  16950. (pngfile (concat texfilebase ".png"))
  16951. (fnh (if (featurep 'xemacs)
  16952. (font-height (face-font 'default))
  16953. (face-attribute 'default :height nil)))
  16954. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16955. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16956. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16957. "Black"))
  16958. (bg (or (plist-get options (if buffer :background :html-background))
  16959. "Transparent")))
  16960. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16961. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16962. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16963. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16964. (let ((latex-header (org-create-formula--latex-header)))
  16965. (with-temp-file texfile
  16966. (insert latex-header)
  16967. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16968. (let ((dir default-directory))
  16969. (condition-case nil
  16970. (progn
  16971. (cd tmpdir)
  16972. (call-process "latex" nil nil nil texfile))
  16973. (error nil))
  16974. (cd dir))
  16975. (if (not (file-exists-p dvifile))
  16976. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16977. (condition-case nil
  16978. (if (featurep 'xemacs)
  16979. (call-process "dvipng" nil nil nil
  16980. "-fg" fg "-bg" bg
  16981. "-T" "tight"
  16982. "-o" pngfile
  16983. dvifile)
  16984. (call-process "dvipng" nil nil nil
  16985. "-fg" fg "-bg" bg
  16986. "-D" dpi
  16987. ;;"-x" scale "-y" scale
  16988. "-T" "tight"
  16989. "-o" pngfile
  16990. dvifile))
  16991. (error nil))
  16992. (if (not (file-exists-p pngfile))
  16993. (if org-format-latex-signal-error
  16994. (error "Failed to create png file from %s" texfile)
  16995. (message "Failed to create png file from %s" texfile)
  16996. nil)
  16997. ;; Use the requested file name and clean up
  16998. (copy-file pngfile tofile 'replace)
  16999. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17000. (if (file-exists-p (concat texfilebase e))
  17001. (delete-file (concat texfilebase e))))
  17002. pngfile))))
  17003. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17004. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17005. "This calls convert, which is included into imagemagick."
  17006. (require 'ox-latex)
  17007. (let* ((tmpdir (if (featurep 'xemacs)
  17008. (temp-directory)
  17009. temporary-file-directory))
  17010. (texfilebase (make-temp-name
  17011. (expand-file-name "orgtex" tmpdir)))
  17012. (texfile (concat texfilebase ".tex"))
  17013. (pdffile (concat texfilebase ".pdf"))
  17014. (pngfile (concat texfilebase ".png"))
  17015. (fnh (if (featurep 'xemacs)
  17016. (font-height (face-font 'default))
  17017. (face-attribute 'default :height nil)))
  17018. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17019. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17020. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17021. "black"))
  17022. (bg (or (plist-get options (if buffer :background :html-background))
  17023. "white")))
  17024. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17025. (setq fg (org-latex-color-format fg)))
  17026. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17027. (setq bg (org-latex-color-format
  17028. (if (string= bg "Transparent") "white" bg))))
  17029. (let ((latex-header (org-create-formula--latex-header)))
  17030. (with-temp-file texfile
  17031. (insert latex-header)
  17032. (insert "\n\\begin{document}\n"
  17033. "\\definecolor{fg}{rgb}{" fg "}\n"
  17034. "\\definecolor{bg}{rgb}{" bg "}\n"
  17035. "\n\\pagecolor{bg}\n"
  17036. "\n{\\color{fg}\n"
  17037. string
  17038. "\n}\n"
  17039. "\n\\end{document}\n")))
  17040. (org-latex-compile texfile t)
  17041. (if (not (file-exists-p pdffile))
  17042. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17043. (condition-case nil
  17044. (if (featurep 'xemacs)
  17045. (call-process "convert" nil nil nil
  17046. "-density" "96"
  17047. "-trim"
  17048. "-antialias"
  17049. pdffile
  17050. "-quality" "100"
  17051. ;; "-sharpen" "0x1.0"
  17052. pngfile)
  17053. (call-process "convert" nil nil nil
  17054. "-density" dpi
  17055. "-trim"
  17056. "-antialias"
  17057. pdffile
  17058. "-quality" "100"
  17059. ;; "-sharpen" "0x1.0"
  17060. pngfile))
  17061. (error nil))
  17062. (if (not (file-exists-p pngfile))
  17063. (if org-format-latex-signal-error
  17064. (error "Failed to create png file from %s" texfile)
  17065. (message "Failed to create png file from %s" texfile)
  17066. nil)
  17067. ;; Use the requested file name and clean up
  17068. (copy-file pngfile tofile 'replace)
  17069. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17070. (if (file-exists-p (concat texfilebase e))
  17071. (delete-file (concat texfilebase e))))
  17072. pngfile))))
  17073. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17074. "Fill a LaTeX header template TPL.
  17075. In the template, the following place holders will be recognized:
  17076. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17077. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17078. [PACKAGES] \\usepackage statements for PKG
  17079. [NO-PACKAGES] do not include PKG
  17080. [EXTRA] the string EXTRA
  17081. [NO-EXTRA] do not include EXTRA
  17082. For backward compatibility, if both the positive and the negative place
  17083. holder is missing, the positive one (without the \"NO-\") will be
  17084. assumed to be present at the end of the template.
  17085. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17086. EXTRA is a string.
  17087. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17088. (let (rpl (end ""))
  17089. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17090. (setq rpl (if (or (match-end 1) (not def-pkg))
  17091. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17092. tpl (replace-match rpl t t tpl))
  17093. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17094. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17095. (setq rpl (if (or (match-end 1) (not pkg))
  17096. "" (org-latex-packages-to-string pkg snippets-p t))
  17097. tpl (replace-match rpl t t tpl))
  17098. (if pkg (setq end
  17099. (concat end "\n"
  17100. (org-latex-packages-to-string pkg snippets-p)))))
  17101. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17102. (setq rpl (if (or (match-end 1) (not extra))
  17103. "" (concat extra "\n"))
  17104. tpl (replace-match rpl t t tpl))
  17105. (if (and extra (string-match "\\S-" extra))
  17106. (setq end (concat end "\n" extra))))
  17107. (if (string-match "\\S-" end)
  17108. (concat tpl "\n" end)
  17109. tpl)))
  17110. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17111. "Turn an alist of packages into a string with the \\usepackage macros."
  17112. (setq pkg (mapconcat (lambda(p)
  17113. (cond
  17114. ((stringp p) p)
  17115. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17116. (format "%% Package %s omitted" (cadr p)))
  17117. ((equal "" (car p))
  17118. (format "\\usepackage{%s}" (cadr p)))
  17119. (t
  17120. (format "\\usepackage[%s]{%s}"
  17121. (car p) (cadr p)))))
  17122. pkg
  17123. "\n"))
  17124. (if newline (concat pkg "\n") pkg))
  17125. (defun org-dvipng-color (attr)
  17126. "Return a RGB color specification for dvipng."
  17127. (apply 'format "rgb %s %s %s"
  17128. (mapcar 'org-normalize-color
  17129. (if (featurep 'xemacs)
  17130. (color-rgb-components
  17131. (face-property 'default
  17132. (cond ((eq attr :foreground) 'foreground)
  17133. ((eq attr :background) 'background))))
  17134. (color-values (face-attribute 'default attr nil))))))
  17135. (defun org-dvipng-color-format (color-name)
  17136. "Convert COLOR-NAME to a RGB color value for dvipng."
  17137. (apply 'format "rgb %s %s %s"
  17138. (mapcar 'org-normalize-color
  17139. (color-values color-name))))
  17140. (defun org-latex-color (attr)
  17141. "Return a RGB color for the LaTeX color package."
  17142. (apply 'format "%s,%s,%s"
  17143. (mapcar 'org-normalize-color
  17144. (if (featurep 'xemacs)
  17145. (color-rgb-components
  17146. (face-property 'default
  17147. (cond ((eq attr :foreground) 'foreground)
  17148. ((eq attr :background) 'background))))
  17149. (color-values (face-attribute 'default attr nil))))))
  17150. (defun org-latex-color-format (color-name)
  17151. "Convert COLOR-NAME to a RGB color value."
  17152. (apply 'format "%s,%s,%s"
  17153. (mapcar 'org-normalize-color
  17154. (color-values color-name))))
  17155. (defun org-normalize-color (value)
  17156. "Return string to be used as color value for an RGB component."
  17157. (format "%g" (/ value 65535.0)))
  17158. ;; Image display
  17159. (defvar org-inline-image-overlays nil)
  17160. (make-variable-buffer-local 'org-inline-image-overlays)
  17161. (defun org-toggle-inline-images (&optional include-linked)
  17162. "Toggle the display of inline images.
  17163. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17164. (interactive "P")
  17165. (if org-inline-image-overlays
  17166. (progn
  17167. (org-remove-inline-images)
  17168. (message "Inline image display turned off"))
  17169. (org-display-inline-images include-linked)
  17170. (if (and (org-called-interactively-p)
  17171. org-inline-image-overlays)
  17172. (message "%d images displayed inline"
  17173. (length org-inline-image-overlays))
  17174. (message "No images to display inline"))))
  17175. (defun org-redisplay-inline-images ()
  17176. "Refresh the display of inline images."
  17177. (interactive)
  17178. (if (not org-inline-image-overlays)
  17179. (org-toggle-inline-images)
  17180. (org-toggle-inline-images)
  17181. (org-toggle-inline-images)))
  17182. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17183. "Display inline images.
  17184. Normally only links without a description part are inlined, because this
  17185. is how it will work for export. When INCLUDE-LINKED is set, also links
  17186. with a description part will be inlined. This can be nice for a quick
  17187. look at those images, but it does not reflect what exported files will look
  17188. like.
  17189. When REFRESH is set, refresh existing images between BEG and END.
  17190. This will create new image displays only if necessary.
  17191. BEG and END default to the buffer boundaries."
  17192. (interactive "P")
  17193. (unless refresh
  17194. (org-remove-inline-images)
  17195. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17196. (save-excursion
  17197. (save-restriction
  17198. (widen)
  17199. (setq beg (or beg (point-min)) end (or end (point-max)))
  17200. (goto-char beg)
  17201. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17202. (substring (org-image-file-name-regexp) 0 -2)
  17203. "\\)\\]" (if include-linked "" "\\]")))
  17204. (case-fold-search t)
  17205. old file ov img type attrwidth width)
  17206. (while (re-search-forward re end t)
  17207. (setq old (get-char-property-and-overlay (match-beginning 1)
  17208. 'org-image-overlay)
  17209. file (expand-file-name
  17210. (concat (or (match-string 3) "") (match-string 4))))
  17211. (when (image-type-available-p 'imagemagick)
  17212. (setq attrwidth (if (or (listp org-image-actual-width)
  17213. (null org-image-actual-width))
  17214. (save-excursion
  17215. (save-match-data
  17216. (when (re-search-backward
  17217. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17218. (save-excursion
  17219. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17220. (string-to-number (match-string 1))))))
  17221. width (cond ((eq org-image-actual-width t) nil)
  17222. ((null org-image-actual-width) attrwidth)
  17223. ((numberp org-image-actual-width)
  17224. org-image-actual-width)
  17225. ((listp org-image-actual-width)
  17226. (or attrwidth (car org-image-actual-width))))
  17227. type (if width 'imagemagick)))
  17228. (when (file-exists-p file)
  17229. (if (and (car-safe old) refresh)
  17230. (image-refresh (overlay-get (cdr old) 'display))
  17231. (setq img (save-match-data (create-image file type nil :width width)))
  17232. (when img
  17233. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17234. (overlay-put ov 'display img)
  17235. (overlay-put ov 'face 'default)
  17236. (overlay-put ov 'org-image-overlay t)
  17237. (overlay-put ov 'modification-hooks
  17238. (list 'org-display-inline-remove-overlay))
  17239. (push ov org-inline-image-overlays)))))))))
  17240. (define-obsolete-function-alias
  17241. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17242. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17243. "Remove inline-display overlay if a corresponding region is modified."
  17244. (let ((inhibit-modification-hooks t))
  17245. (when (and ov after)
  17246. (delete ov org-inline-image-overlays)
  17247. (delete-overlay ov))))
  17248. (defun org-remove-inline-images ()
  17249. "Remove inline display of images."
  17250. (interactive)
  17251. (mapc 'delete-overlay org-inline-image-overlays)
  17252. (setq org-inline-image-overlays nil))
  17253. ;;;; Key bindings
  17254. ;; Outline functions from `outline-mode-prefix-map'
  17255. ;; that can be remapped in Org:
  17256. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17257. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17258. (define-key org-mode-map [remap outline-forward-same-level]
  17259. 'org-forward-heading-same-level)
  17260. (define-key org-mode-map [remap outline-backward-same-level]
  17261. 'org-backward-heading-same-level)
  17262. (define-key org-mode-map [remap show-branches]
  17263. 'org-kill-note-or-show-branches)
  17264. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17265. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17266. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17267. ;; Outline functions from `outline-mode-prefix-map' that can not
  17268. ;; be remapped in Org:
  17269. ;;
  17270. ;; - the column "key binding" shows whether the Outline function is still
  17271. ;; available in Org mode on the same key that it has been bound to in
  17272. ;; Outline mode:
  17273. ;; - "overridden": key used for a different functionality in Org mode
  17274. ;; - else: key still bound to the same Outline function in Org mode
  17275. ;;
  17276. ;; | Outline function | key binding | Org replacement |
  17277. ;; |------------------------------------+-------------+-----------------------|
  17278. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17279. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17280. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17281. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17282. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17283. ;; | `show-entry' | overridden | no replacement |
  17284. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17285. ;; | `show-branches' | `C-c C-k' | still same function |
  17286. ;; | `show-subtree' | overridden | visibility cycling |
  17287. ;; | `show-all' | overridden | no replacement |
  17288. ;; | `hide-subtree' | overridden | visibility cycling |
  17289. ;; | `hide-body' | overridden | no replacement |
  17290. ;; | `hide-entry' | overridden | visibility cycling |
  17291. ;; | `hide-leaves' | overridden | no replacement |
  17292. ;; | `hide-sublevels' | overridden | no replacement |
  17293. ;; | `hide-other' | overridden | no replacement |
  17294. ;; Make `C-c C-x' a prefix key
  17295. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17296. ;; TAB key with modifiers
  17297. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17298. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17299. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17300. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17301. ;; The following line is necessary under Suse GNU/Linux
  17302. (unless (featurep 'xemacs)
  17303. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17304. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17305. (define-key org-mode-map [backtab] 'org-shifttab)
  17306. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17307. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17308. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17309. ;; Cursor keys with modifiers
  17310. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17311. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17312. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17313. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17314. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17315. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17316. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17317. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17318. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17319. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17320. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17321. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17322. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17323. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17324. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17325. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17326. ;; Babel keys
  17327. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17328. (mapc (lambda (pair)
  17329. (define-key org-babel-map (car pair) (cdr pair)))
  17330. org-babel-key-bindings)
  17331. ;;; Extra keys for tty access.
  17332. ;; We only set them when really needed because otherwise the
  17333. ;; menus don't show the simple keys
  17334. (when (or org-use-extra-keys
  17335. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17336. (not window-system))
  17337. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17338. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17339. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17340. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17341. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17342. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17343. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17344. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17345. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17346. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17347. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17348. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17349. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17350. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17351. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17352. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17353. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17354. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17355. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17356. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17357. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17358. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17359. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17360. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17361. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17362. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17363. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17364. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17365. ;; All the other keys
  17366. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17367. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17368. (if (boundp 'narrow-map)
  17369. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17370. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17371. (if (boundp 'narrow-map)
  17372. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17373. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17374. (if (boundp 'narrow-map)
  17375. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17376. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17377. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17378. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17379. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17380. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17381. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17382. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17383. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17384. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17385. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17386. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17387. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17388. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17389. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17390. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17391. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17392. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17393. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17394. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17395. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17396. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17397. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17398. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17399. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17400. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17401. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17402. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17403. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17404. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17405. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17406. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17407. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17408. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17409. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17410. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17411. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17412. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17413. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17414. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17415. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17416. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17417. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17418. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17419. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17420. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17421. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17422. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17423. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17424. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17425. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17426. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17427. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17428. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17429. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17430. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17431. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17432. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17433. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17434. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17435. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17436. (org-defkey org-mode-map "\C-o" 'org-ctrl-o)
  17437. (org-defkey org-mode-map "\C-m" 'org-return)
  17438. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17439. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17440. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17441. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17442. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17443. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17444. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17445. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17446. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17447. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17448. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17449. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17450. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17451. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17452. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17453. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17454. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17455. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17456. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17457. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17458. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17459. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17460. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17461. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17462. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17463. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17464. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17465. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17466. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17467. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17468. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17469. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17470. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17471. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17472. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17473. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17474. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17475. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17476. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17477. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17478. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17479. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17480. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17481. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17482. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17483. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17484. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17485. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17486. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17487. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17488. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17489. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17490. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17491. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17492. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17493. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17494. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17495. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17496. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17497. (when (featurep 'xemacs)
  17498. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17499. (defconst org-speed-commands-default
  17500. '(
  17501. ("Outline Navigation")
  17502. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17503. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17504. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17505. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17506. ("F" . org-next-block)
  17507. ("B" . org-previous-block)
  17508. ("u" . (org-speed-move-safe 'outline-up-heading))
  17509. ("j" . org-goto)
  17510. ("g" . (org-refile t))
  17511. ("Outline Visibility")
  17512. ("c" . org-cycle)
  17513. ("C" . org-shifttab)
  17514. (" " . org-display-outline-path)
  17515. ("s" . org-narrow-to-subtree)
  17516. ("=" . org-columns)
  17517. ("Outline Structure Editing")
  17518. ("U" . org-shiftmetaup)
  17519. ("D" . org-shiftmetadown)
  17520. ("r" . org-metaright)
  17521. ("l" . org-metaleft)
  17522. ("R" . org-shiftmetaright)
  17523. ("L" . org-shiftmetaleft)
  17524. ("i" . (progn (forward-char 1) (call-interactively
  17525. 'org-insert-heading-respect-content)))
  17526. ("^" . org-sort)
  17527. ("w" . org-refile)
  17528. ("a" . org-archive-subtree-default-with-confirmation)
  17529. ("@" . org-mark-subtree)
  17530. ("#" . org-toggle-comment)
  17531. ("Clock Commands")
  17532. ("I" . org-clock-in)
  17533. ("O" . org-clock-out)
  17534. ("Meta Data Editing")
  17535. ("t" . org-todo)
  17536. ("," . (org-priority))
  17537. ("0" . (org-priority ?\ ))
  17538. ("1" . (org-priority ?A))
  17539. ("2" . (org-priority ?B))
  17540. ("3" . (org-priority ?C))
  17541. (":" . org-set-tags-command)
  17542. ("e" . org-set-effort)
  17543. ("E" . org-inc-effort)
  17544. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17545. (org-entry-put (point) "APPT_WARNTIME" m)))
  17546. ("Agenda Views etc")
  17547. ("v" . org-agenda)
  17548. ("/" . org-sparse-tree)
  17549. ("Misc")
  17550. ("o" . org-open-at-point)
  17551. ("?" . org-speed-command-help)
  17552. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17553. (">" . (org-agenda-remove-restriction-lock))
  17554. )
  17555. "The default speed commands.")
  17556. (defun org-print-speed-command (e)
  17557. (if (> (length (car e)) 1)
  17558. (progn
  17559. (princ "\n")
  17560. (princ (car e))
  17561. (princ "\n")
  17562. (princ (make-string (length (car e)) ?-))
  17563. (princ "\n"))
  17564. (princ (car e))
  17565. (princ " ")
  17566. (if (symbolp (cdr e))
  17567. (princ (symbol-name (cdr e)))
  17568. (prin1 (cdr e)))
  17569. (princ "\n")))
  17570. (defun org-speed-command-help ()
  17571. "Show the available speed commands."
  17572. (interactive)
  17573. (if (not org-use-speed-commands)
  17574. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17575. (with-output-to-temp-buffer "*Help*"
  17576. (princ "User-defined Speed commands\n===========================\n")
  17577. (mapc 'org-print-speed-command org-speed-commands-user)
  17578. (princ "\n")
  17579. (princ "Built-in Speed commands\n=======================\n")
  17580. (mapc 'org-print-speed-command org-speed-commands-default))
  17581. (with-current-buffer "*Help*"
  17582. (setq truncate-lines t))))
  17583. (defun org-speed-move-safe (cmd)
  17584. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17585. If not, return to the original position and throw an error."
  17586. (interactive)
  17587. (let ((pos (point)))
  17588. (call-interactively cmd)
  17589. (unless (and (bolp) (org-at-heading-p))
  17590. (goto-char pos)
  17591. (error "Boundary reached while executing %s" cmd))))
  17592. (defvar org-self-insert-command-undo-counter 0)
  17593. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17594. (defvar org-speed-command nil)
  17595. (define-obsolete-function-alias
  17596. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17597. (defun org-speed-command-activate (keys)
  17598. "Hook for activating single-letter speed commands.
  17599. `org-speed-commands-default' specifies a minimal command set.
  17600. Use `org-speed-commands-user' for further customization."
  17601. (when (or (and (bolp) (looking-at org-outline-regexp))
  17602. (and (functionp org-use-speed-commands)
  17603. (funcall org-use-speed-commands)))
  17604. (cdr (assoc keys (append org-speed-commands-user
  17605. org-speed-commands-default)))))
  17606. (define-obsolete-function-alias
  17607. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17608. (defun org-babel-speed-command-activate (keys)
  17609. "Hook for activating single-letter code block commands."
  17610. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17611. (cdr (assoc keys org-babel-key-bindings))))
  17612. (defcustom org-speed-command-hook
  17613. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17614. "Hook for activating speed commands at strategic locations.
  17615. Hook functions are called in sequence until a valid handler is
  17616. found.
  17617. Each hook takes a single argument, a user-pressed command key
  17618. which is also a `self-insert-command' from the global map.
  17619. Within the hook, examine the cursor position and the command key
  17620. and return nil or a valid handler as appropriate. Handler could
  17621. be one of an interactive command, a function, or a form.
  17622. Set `org-use-speed-commands' to non-nil value to enable this
  17623. hook. The default setting is `org-speed-command-activate'."
  17624. :group 'org-structure
  17625. :version "24.1"
  17626. :type 'hook)
  17627. (defun org-self-insert-command (N)
  17628. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17629. If the cursor is in a table looking at whitespace, the whitespace is
  17630. overwritten, and the table is not marked as requiring realignment."
  17631. (interactive "p")
  17632. (org-check-before-invisible-edit 'insert)
  17633. (cond
  17634. ((and org-use-speed-commands
  17635. (setq org-speed-command
  17636. (run-hook-with-args-until-success
  17637. 'org-speed-command-hook (this-command-keys))))
  17638. (cond
  17639. ((commandp org-speed-command)
  17640. (setq this-command org-speed-command)
  17641. (call-interactively org-speed-command))
  17642. ((functionp org-speed-command)
  17643. (funcall org-speed-command))
  17644. ((and org-speed-command (listp org-speed-command))
  17645. (eval org-speed-command))
  17646. (t (let (org-use-speed-commands)
  17647. (call-interactively 'org-self-insert-command)))))
  17648. ((and
  17649. (org-table-p)
  17650. (progn
  17651. ;; check if we blank the field, and if that triggers align
  17652. (and (featurep 'org-table) org-table-auto-blank-field
  17653. (member last-command
  17654. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17655. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17656. ;; got extra space, this field does not determine column width
  17657. (let (org-table-may-need-update) (org-table-blank-field))
  17658. ;; no extra space, this field may determine column width
  17659. (org-table-blank-field)))
  17660. t)
  17661. (eq N 1)
  17662. (looking-at "[^|\n]* |"))
  17663. (let (org-table-may-need-update)
  17664. (goto-char (1- (match-end 0)))
  17665. (backward-delete-char 1)
  17666. (goto-char (match-beginning 0))
  17667. (self-insert-command N)))
  17668. (t
  17669. (setq org-table-may-need-update t)
  17670. (self-insert-command N)
  17671. (org-fix-tags-on-the-fly)
  17672. (if org-self-insert-cluster-for-undo
  17673. (if (not (eq last-command 'org-self-insert-command))
  17674. (setq org-self-insert-command-undo-counter 1)
  17675. (if (>= org-self-insert-command-undo-counter 20)
  17676. (setq org-self-insert-command-undo-counter 1)
  17677. (and (> org-self-insert-command-undo-counter 0)
  17678. buffer-undo-list (listp buffer-undo-list)
  17679. (not (cadr buffer-undo-list)) ; remove nil entry
  17680. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17681. (setq org-self-insert-command-undo-counter
  17682. (1+ org-self-insert-command-undo-counter))))))))
  17683. (defun org-check-before-invisible-edit (kind)
  17684. "Check is editing if kind KIND would be dangerous with invisible text around.
  17685. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17686. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17687. (if (and org-catch-invisible-edits
  17688. (or (not (boundp 'visible-mode)) (not visible-mode))
  17689. (or (get-char-property (point) 'invisible)
  17690. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17691. ;; OK, we need to take a closer look
  17692. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17693. (invisible-before-point (if (bobp) nil (get-char-property
  17694. (1- (point)) 'invisible)))
  17695. (border-and-ok-direction
  17696. (or
  17697. ;; Check if we are acting predictably before invisible text
  17698. (and invisible-at-point (not invisible-before-point)
  17699. (memq kind '(insert delete-backward)))
  17700. ;; Check if we are acting predictably after invisible text
  17701. ;; This works not well, and I have turned it off. It seems
  17702. ;; better to always show and stop after invisible text.
  17703. ;; (and (not invisible-at-point) invisible-before-point
  17704. ;; (memq kind '(insert delete)))
  17705. )))
  17706. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17707. (memq invisible-before-point '(outline org-hide-block t)))
  17708. (if (eq org-catch-invisible-edits 'error)
  17709. (error "Editing in invisible areas is prohibited - make visible first"))
  17710. (if (and org-custom-properties-overlays
  17711. (y-or-n-p "Display invisible properties in this buffer? "))
  17712. (org-toggle-custom-properties-visibility)
  17713. ;; Make the area visible
  17714. (save-excursion
  17715. (if invisible-before-point
  17716. (goto-char (previous-single-char-property-change
  17717. (point) 'invisible)))
  17718. (org-cycle))
  17719. (cond
  17720. ((eq org-catch-invisible-edits 'show)
  17721. ;; That's it, we do the edit after showing
  17722. (message
  17723. "Unfolding invisible region around point before editing")
  17724. (sit-for 1))
  17725. ((and (eq org-catch-invisible-edits 'smart)
  17726. border-and-ok-direction)
  17727. (message "Unfolding invisible region around point before editing"))
  17728. (t
  17729. ;; Don't do the edit, make the user repeat it in full visibility
  17730. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17731. (defun org-fix-tags-on-the-fly ()
  17732. (when (and (equal (char-after (point-at-bol)) ?*)
  17733. (org-at-heading-p))
  17734. (org-align-tags-here org-tags-column)))
  17735. (defun org-delete-backward-char (N)
  17736. "Like `delete-backward-char', insert whitespace at field end in tables.
  17737. When deleting backwards, in tables this function will insert whitespace in
  17738. front of the next \"|\" separator, to keep the table aligned. The table will
  17739. still be marked for re-alignment if the field did fill the entire column,
  17740. because, in this case the deletion might narrow the column."
  17741. (interactive "p")
  17742. (save-match-data
  17743. (org-check-before-invisible-edit 'delete-backward)
  17744. (if (and (org-table-p)
  17745. (eq N 1)
  17746. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17747. (looking-at ".*?|"))
  17748. (let ((pos (point))
  17749. (noalign (looking-at "[^|\n\r]* |"))
  17750. (c org-table-may-need-update))
  17751. (backward-delete-char N)
  17752. (if (not overwrite-mode)
  17753. (progn
  17754. (skip-chars-forward "^|")
  17755. (insert " ")
  17756. (goto-char (1- pos))))
  17757. ;; noalign: if there were two spaces at the end, this field
  17758. ;; does not determine the width of the column.
  17759. (if noalign (setq org-table-may-need-update c)))
  17760. (backward-delete-char N)
  17761. (org-fix-tags-on-the-fly))))
  17762. (defun org-delete-char (N)
  17763. "Like `delete-char', but insert whitespace at field end in tables.
  17764. When deleting characters, in tables this function will insert whitespace in
  17765. front of the next \"|\" separator, to keep the table aligned. The table will
  17766. still be marked for re-alignment if the field did fill the entire column,
  17767. because, in this case the deletion might narrow the column."
  17768. (interactive "p")
  17769. (save-match-data
  17770. (org-check-before-invisible-edit 'delete)
  17771. (if (and (org-table-p)
  17772. (not (bolp))
  17773. (not (= (char-after) ?|))
  17774. (eq N 1))
  17775. (if (looking-at ".*?|")
  17776. (let ((pos (point))
  17777. (noalign (looking-at "[^|\n\r]* |"))
  17778. (c org-table-may-need-update))
  17779. (replace-match
  17780. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17781. (goto-char pos)
  17782. ;; noalign: if there were two spaces at the end, this field
  17783. ;; does not determine the width of the column.
  17784. (if noalign (setq org-table-may-need-update c)))
  17785. (delete-char N))
  17786. (delete-char N)
  17787. (org-fix-tags-on-the-fly))))
  17788. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17789. (put 'org-self-insert-command 'delete-selection t)
  17790. (put 'orgtbl-self-insert-command 'delete-selection t)
  17791. (put 'org-delete-char 'delete-selection 'supersede)
  17792. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17793. (put 'org-yank 'delete-selection 'yank)
  17794. ;; Make `flyspell-mode' delay after some commands
  17795. (put 'org-self-insert-command 'flyspell-delayed t)
  17796. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17797. (put 'org-delete-char 'flyspell-delayed t)
  17798. (put 'org-delete-backward-char 'flyspell-delayed t)
  17799. ;; Make pabbrev-mode expand after org-mode commands
  17800. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17801. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17802. ;; How to do this: Measure non-white length of current string
  17803. ;; If equal to column width, we should realign.
  17804. (defun org-remap (map &rest commands)
  17805. "In MAP, remap the functions given in COMMANDS.
  17806. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17807. (let (new old)
  17808. (while commands
  17809. (setq old (pop commands) new (pop commands))
  17810. (if (fboundp 'command-remapping)
  17811. (org-defkey map (vector 'remap old) new)
  17812. (substitute-key-definition old new map global-map)))))
  17813. (defun org-transpose-words ()
  17814. "Transpose words for Org.
  17815. This uses the `org-mode-transpose-word-syntax-table' syntax
  17816. table, which interprets characters in `org-emphasis-alist' as
  17817. word constituants."
  17818. (interactive)
  17819. (with-syntax-table org-mode-transpose-word-syntax-table
  17820. (call-interactively 'transpose-words)))
  17821. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17822. (when (eq org-enable-table-editor 'optimized)
  17823. ;; If the user wants maximum table support, we need to hijack
  17824. ;; some standard editing functions
  17825. (org-remap org-mode-map
  17826. 'self-insert-command 'org-self-insert-command
  17827. 'delete-char 'org-delete-char
  17828. 'delete-backward-char 'org-delete-backward-char)
  17829. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17830. (defvar org-ctrl-c-ctrl-c-hook nil
  17831. "Hook for functions attaching themselves to `C-c C-c'.
  17832. This can be used to add additional functionality to the C-c C-c
  17833. key which executes context-dependent commands. This hook is run
  17834. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17835. run after the last test.
  17836. Each function will be called with no arguments. The function
  17837. must check if the context is appropriate for it to act. If yes,
  17838. it should do its thing and then return a non-nil value. If the
  17839. context is wrong, just do nothing and return nil.")
  17840. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17841. "Hook for functions attaching themselves to `C-c C-c'.
  17842. This can be used to add additional functionality to the C-c C-c
  17843. key which executes context-dependent commands. This hook is run
  17844. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17845. before the first test.
  17846. Each function will be called with no arguments. The function
  17847. must check if the context is appropriate for it to act. If yes,
  17848. it should do its thing and then return a non-nil value. If the
  17849. context is wrong, just do nothing and return nil.")
  17850. (defvar org-tab-first-hook nil
  17851. "Hook for functions to attach themselves to TAB.
  17852. See `org-ctrl-c-ctrl-c-hook' for more information.
  17853. This hook runs as the first action when TAB is pressed, even before
  17854. `org-cycle' messes around with the `outline-regexp' to cater for
  17855. inline tasks and plain list item folding.
  17856. If any function in this hook returns t, any other actions that
  17857. would have been caused by TAB (such as table field motion or visibility
  17858. cycling) will not occur.")
  17859. (defvar org-tab-after-check-for-table-hook nil
  17860. "Hook for functions to attach themselves to TAB.
  17861. See `org-ctrl-c-ctrl-c-hook' for more information.
  17862. This hook runs after it has been established that the cursor is not in a
  17863. table, but before checking if the cursor is in a headline or if global cycling
  17864. should be done.
  17865. If any function in this hook returns t, not other actions like visibility
  17866. cycling will be done.")
  17867. (defvar org-tab-after-check-for-cycling-hook nil
  17868. "Hook for functions to attach themselves to TAB.
  17869. See `org-ctrl-c-ctrl-c-hook' for more information.
  17870. This hook runs after it has been established that not table field motion and
  17871. not visibility should be done because of current context. This is probably
  17872. the place where a package like yasnippets can hook in.")
  17873. (defvar org-tab-before-tab-emulation-hook nil
  17874. "Hook for functions to attach themselves to TAB.
  17875. See `org-ctrl-c-ctrl-c-hook' for more information.
  17876. This hook runs after every other options for TAB have been exhausted, but
  17877. before indentation and \t insertion takes place.")
  17878. (defvar org-metaleft-hook nil
  17879. "Hook for functions attaching themselves to `M-left'.
  17880. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17881. (defvar org-metaright-hook nil
  17882. "Hook for functions attaching themselves to `M-right'.
  17883. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17884. (defvar org-metaup-hook nil
  17885. "Hook for functions attaching themselves to `M-up'.
  17886. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17887. (defvar org-metadown-hook nil
  17888. "Hook for functions attaching themselves to `M-down'.
  17889. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17890. (defvar org-shiftmetaleft-hook nil
  17891. "Hook for functions attaching themselves to `M-S-left'.
  17892. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17893. (defvar org-shiftmetaright-hook nil
  17894. "Hook for functions attaching themselves to `M-S-right'.
  17895. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17896. (defvar org-shiftmetaup-hook nil
  17897. "Hook for functions attaching themselves to `M-S-up'.
  17898. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17899. (defvar org-shiftmetadown-hook nil
  17900. "Hook for functions attaching themselves to `M-S-down'.
  17901. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17902. (defvar org-metareturn-hook nil
  17903. "Hook for functions attaching themselves to `M-RET'.
  17904. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17905. (defvar org-shiftup-hook nil
  17906. "Hook for functions attaching themselves to `S-up'.
  17907. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17908. (defvar org-shiftup-final-hook nil
  17909. "Hook for functions attaching themselves to `S-up'.
  17910. This one runs after all other options except shift-select have been excluded.
  17911. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17912. (defvar org-shiftdown-hook nil
  17913. "Hook for functions attaching themselves to `S-down'.
  17914. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17915. (defvar org-shiftdown-final-hook nil
  17916. "Hook for functions attaching themselves to `S-down'.
  17917. This one runs after all other options except shift-select have been excluded.
  17918. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17919. (defvar org-shiftleft-hook nil
  17920. "Hook for functions attaching themselves to `S-left'.
  17921. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17922. (defvar org-shiftleft-final-hook nil
  17923. "Hook for functions attaching themselves to `S-left'.
  17924. This one runs after all other options except shift-select have been excluded.
  17925. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17926. (defvar org-shiftright-hook nil
  17927. "Hook for functions attaching themselves to `S-right'.
  17928. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17929. (defvar org-shiftright-final-hook nil
  17930. "Hook for functions attaching themselves to `S-right'.
  17931. This one runs after all other options except shift-select have been excluded.
  17932. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17933. (defun org-modifier-cursor-error ()
  17934. "Throw an error, a modified cursor command was applied in wrong context."
  17935. (error "This command is active in special context like tables, headlines or items"))
  17936. (defun org-shiftselect-error ()
  17937. "Throw an error because Shift-Cursor command was applied in wrong context."
  17938. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17939. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17940. (error "This command works only in special context like headlines or timestamps")))
  17941. (defun org-call-for-shift-select (cmd)
  17942. (let ((this-command-keys-shift-translated t))
  17943. (call-interactively cmd)))
  17944. (defun org-shifttab (&optional arg)
  17945. "Global visibility cycling or move to previous table field.
  17946. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17947. on context.
  17948. See the individual commands for more information."
  17949. (interactive "P")
  17950. (cond
  17951. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17952. ((integerp arg)
  17953. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17954. (message "Content view to level: %d" arg)
  17955. (org-content (prefix-numeric-value arg2))
  17956. (setq org-cycle-global-status 'overview)))
  17957. (t (call-interactively 'org-global-cycle))))
  17958. (defun org-shiftmetaleft ()
  17959. "Promote subtree or delete table column.
  17960. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17961. `org-table-delete-column', depending on context. See the
  17962. individual commands for more information."
  17963. (interactive)
  17964. (cond
  17965. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17966. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17967. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17968. ((if (not (org-region-active-p)) (org-at-item-p)
  17969. (save-excursion (goto-char (region-beginning))
  17970. (org-at-item-p)))
  17971. (call-interactively 'org-outdent-item-tree))
  17972. (t (org-modifier-cursor-error))))
  17973. (defun org-shiftmetaright ()
  17974. "Demote subtree or insert table column.
  17975. Calls `org-demote-subtree', `org-indent-item-tree', or
  17976. `org-table-insert-column', depending on context. See the
  17977. individual commands for more information."
  17978. (interactive)
  17979. (cond
  17980. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17981. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17982. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17983. ((if (not (org-region-active-p)) (org-at-item-p)
  17984. (save-excursion (goto-char (region-beginning))
  17985. (org-at-item-p)))
  17986. (call-interactively 'org-indent-item-tree))
  17987. (t (org-modifier-cursor-error))))
  17988. (defun org-shiftmetaup (&optional arg)
  17989. "Move subtree up or kill table row.
  17990. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17991. `org-move-item-up' or `org-timestamp-up', depending on context.
  17992. See the individual commands for more information."
  17993. (interactive "P")
  17994. (cond
  17995. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17996. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17997. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17998. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17999. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18000. (call-interactively 'org-timestamp-up)))
  18001. (t (call-interactively 'org-drag-line-backward))))
  18002. (defun org-shiftmetadown (&optional arg)
  18003. "Move subtree down or insert table row.
  18004. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18005. `org-move-item-down' or `org-timestamp-up', depending on context.
  18006. See the individual commands for more information."
  18007. (interactive "P")
  18008. (cond
  18009. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18010. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18011. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18012. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18013. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18014. (call-interactively 'org-timestamp-down)))
  18015. (t (call-interactively 'org-drag-line-forward))))
  18016. (defsubst org-hidden-tree-error ()
  18017. (error
  18018. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18019. (defun org-metaleft (&optional arg)
  18020. "Promote heading or move table column to left.
  18021. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18022. With no specific context, calls the Emacs default `backward-word'.
  18023. See the individual commands for more information."
  18024. (interactive "P")
  18025. (cond
  18026. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18027. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18028. ((org-with-limited-levels
  18029. (or (org-at-heading-p)
  18030. (and (org-region-active-p)
  18031. (save-excursion
  18032. (goto-char (region-beginning))
  18033. (org-at-heading-p)))))
  18034. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18035. (call-interactively 'org-do-promote))
  18036. ;; At an inline task.
  18037. ((org-at-heading-p)
  18038. (call-interactively 'org-inlinetask-promote))
  18039. ((or (org-at-item-p)
  18040. (and (org-region-active-p)
  18041. (save-excursion
  18042. (goto-char (region-beginning))
  18043. (org-at-item-p))))
  18044. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18045. (call-interactively 'org-outdent-item))
  18046. (t (call-interactively 'backward-word))))
  18047. (defun org-metaright (&optional arg)
  18048. "Demote a subtree, a list item or move table column to right.
  18049. In front of a drawer or a block keyword, indent it correctly.
  18050. With no specific context, calls the Emacs default `forward-word'.
  18051. See the individual commands for more information."
  18052. (interactive "P")
  18053. (cond
  18054. ((run-hook-with-args-until-success 'org-metaright-hook))
  18055. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18056. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18057. ((org-at-block-p) (call-interactively 'org-indent-block))
  18058. ((org-with-limited-levels
  18059. (or (org-at-heading-p)
  18060. (and (org-region-active-p)
  18061. (save-excursion
  18062. (goto-char (region-beginning))
  18063. (org-at-heading-p)))))
  18064. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18065. (call-interactively 'org-do-demote))
  18066. ;; At an inline task.
  18067. ((org-at-heading-p)
  18068. (call-interactively 'org-inlinetask-demote))
  18069. ((or (org-at-item-p)
  18070. (and (org-region-active-p)
  18071. (save-excursion
  18072. (goto-char (region-beginning))
  18073. (org-at-item-p))))
  18074. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18075. (call-interactively 'org-indent-item))
  18076. (t (call-interactively 'forward-word))))
  18077. (defun org-check-for-hidden (what)
  18078. "Check if there are hidden headlines/items in the current visual line.
  18079. WHAT can be either `headlines' or `items'. If the current line is
  18080. an outline or item heading and it has a folded subtree below it,
  18081. this function returns t, nil otherwise."
  18082. (let ((re (cond
  18083. ((eq what 'headlines) org-outline-regexp-bol)
  18084. ((eq what 'items) (org-item-beginning-re))
  18085. (t (error "This should not happen"))))
  18086. beg end)
  18087. (save-excursion
  18088. (catch 'exit
  18089. (unless (org-region-active-p)
  18090. (setq beg (point-at-bol))
  18091. (beginning-of-line 2)
  18092. (while (and (not (eobp)) ;; this is like `next-line'
  18093. (get-char-property (1- (point)) 'invisible))
  18094. (beginning-of-line 2))
  18095. (setq end (point))
  18096. (goto-char beg)
  18097. (goto-char (point-at-eol))
  18098. (setq end (max end (point)))
  18099. (while (re-search-forward re end t)
  18100. (if (get-char-property (match-beginning 0) 'invisible)
  18101. (throw 'exit t))))
  18102. nil))))
  18103. (defun org-metaup (&optional arg)
  18104. "Move subtree up or move table row up.
  18105. Calls `org-move-subtree-up' or `org-table-move-row' or
  18106. `org-move-item-up', depending on context. See the individual commands
  18107. for more information."
  18108. (interactive "P")
  18109. (cond
  18110. ((run-hook-with-args-until-success 'org-metaup-hook))
  18111. ((org-region-active-p)
  18112. (let* ((a (min (region-beginning) (region-end)))
  18113. (b (1- (max (region-beginning) (region-end))))
  18114. (c (save-excursion (goto-char a)
  18115. (move-beginning-of-line 0)))
  18116. (d (save-excursion (goto-char a)
  18117. (move-end-of-line 0) (point))))
  18118. (transpose-regions a b c d)
  18119. (goto-char c)))
  18120. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18121. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18122. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18123. (t (org-drag-element-backward))))
  18124. (defun org-metadown (&optional arg)
  18125. "Move subtree down or move table row down.
  18126. Calls `org-move-subtree-down' or `org-table-move-row' or
  18127. `org-move-item-down', depending on context. See the individual
  18128. commands for more information."
  18129. (interactive "P")
  18130. (cond
  18131. ((run-hook-with-args-until-success 'org-metadown-hook))
  18132. ((org-region-active-p)
  18133. (let* ((a (min (region-beginning) (region-end)))
  18134. (b (max (region-beginning) (region-end)))
  18135. (c (save-excursion (goto-char b)
  18136. (move-beginning-of-line 1)))
  18137. (d (save-excursion (goto-char b)
  18138. (move-end-of-line 1) (1+ (point)))))
  18139. (transpose-regions a b c d)
  18140. (goto-char d)))
  18141. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18142. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18143. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18144. (t (org-drag-element-forward))))
  18145. (defun org-shiftup (&optional arg)
  18146. "Increase item in timestamp or increase priority of current headline.
  18147. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18148. depending on context. See the individual commands for more information."
  18149. (interactive "P")
  18150. (cond
  18151. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18152. ((and org-support-shift-select (org-region-active-p))
  18153. (org-call-for-shift-select 'previous-line))
  18154. ((org-at-timestamp-p t)
  18155. (call-interactively (if org-edit-timestamp-down-means-later
  18156. 'org-timestamp-down 'org-timestamp-up)))
  18157. ((and (not (eq org-support-shift-select 'always))
  18158. org-enable-priority-commands
  18159. (org-at-heading-p))
  18160. (call-interactively 'org-priority-up))
  18161. ((and (not org-support-shift-select) (org-at-item-p))
  18162. (call-interactively 'org-previous-item))
  18163. ((org-clocktable-try-shift 'up arg))
  18164. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18165. (org-support-shift-select
  18166. (org-call-for-shift-select 'previous-line))
  18167. (t (org-shiftselect-error))))
  18168. (defun org-shiftdown (&optional arg)
  18169. "Decrease item in timestamp or decrease priority of current headline.
  18170. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18171. depending on context. See the individual commands for more information."
  18172. (interactive "P")
  18173. (cond
  18174. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18175. ((and org-support-shift-select (org-region-active-p))
  18176. (org-call-for-shift-select 'next-line))
  18177. ((org-at-timestamp-p t)
  18178. (call-interactively (if org-edit-timestamp-down-means-later
  18179. 'org-timestamp-up 'org-timestamp-down)))
  18180. ((and (not (eq org-support-shift-select 'always))
  18181. org-enable-priority-commands
  18182. (org-at-heading-p))
  18183. (call-interactively 'org-priority-down))
  18184. ((and (not org-support-shift-select) (org-at-item-p))
  18185. (call-interactively 'org-next-item))
  18186. ((org-clocktable-try-shift 'down arg))
  18187. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18188. (org-support-shift-select
  18189. (org-call-for-shift-select 'next-line))
  18190. (t (org-shiftselect-error))))
  18191. (defun org-shiftright (&optional arg)
  18192. "Cycle the thing at point or in the current line, depending on context.
  18193. Depending on context, this does one of the following:
  18194. - switch a timestamp at point one day into the future
  18195. - on a headline, switch to the next TODO keyword.
  18196. - on an item, switch entire list to the next bullet type
  18197. - on a property line, switch to the next allowed value
  18198. - on a clocktable definition line, move time block into the future"
  18199. (interactive "P")
  18200. (cond
  18201. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18202. ((and org-support-shift-select (org-region-active-p))
  18203. (org-call-for-shift-select 'forward-char))
  18204. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18205. ((and (not (eq org-support-shift-select 'always))
  18206. (org-at-heading-p))
  18207. (let ((org-inhibit-logging
  18208. (not org-treat-S-cursor-todo-selection-as-state-change))
  18209. (org-inhibit-blocking
  18210. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18211. (org-call-with-arg 'org-todo 'right)))
  18212. ((or (and org-support-shift-select
  18213. (not (eq org-support-shift-select 'always))
  18214. (org-at-item-bullet-p))
  18215. (and (not org-support-shift-select) (org-at-item-p)))
  18216. (org-call-with-arg 'org-cycle-list-bullet nil))
  18217. ((and (not (eq org-support-shift-select 'always))
  18218. (org-at-property-p))
  18219. (call-interactively 'org-property-next-allowed-value))
  18220. ((org-clocktable-try-shift 'right arg))
  18221. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18222. (org-support-shift-select
  18223. (org-call-for-shift-select 'forward-char))
  18224. (t (org-shiftselect-error))))
  18225. (defun org-shiftleft (&optional arg)
  18226. "Cycle the thing at point or in the current line, depending on context.
  18227. Depending on context, this does one of the following:
  18228. - switch a timestamp at point one day into the past
  18229. - on a headline, switch to the previous TODO keyword.
  18230. - on an item, switch entire list to the previous bullet type
  18231. - on a property line, switch to the previous allowed value
  18232. - on a clocktable definition line, move time block into the past"
  18233. (interactive "P")
  18234. (cond
  18235. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18236. ((and org-support-shift-select (org-region-active-p))
  18237. (org-call-for-shift-select 'backward-char))
  18238. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18239. ((and (not (eq org-support-shift-select 'always))
  18240. (org-at-heading-p))
  18241. (let ((org-inhibit-logging
  18242. (not org-treat-S-cursor-todo-selection-as-state-change))
  18243. (org-inhibit-blocking
  18244. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18245. (org-call-with-arg 'org-todo 'left)))
  18246. ((or (and org-support-shift-select
  18247. (not (eq org-support-shift-select 'always))
  18248. (org-at-item-bullet-p))
  18249. (and (not org-support-shift-select) (org-at-item-p)))
  18250. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18251. ((and (not (eq org-support-shift-select 'always))
  18252. (org-at-property-p))
  18253. (call-interactively 'org-property-previous-allowed-value))
  18254. ((org-clocktable-try-shift 'left arg))
  18255. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18256. (org-support-shift-select
  18257. (org-call-for-shift-select 'backward-char))
  18258. (t (org-shiftselect-error))))
  18259. (defun org-shiftcontrolright ()
  18260. "Switch to next TODO set."
  18261. (interactive)
  18262. (cond
  18263. ((and org-support-shift-select (org-region-active-p))
  18264. (org-call-for-shift-select 'forward-word))
  18265. ((and (not (eq org-support-shift-select 'always))
  18266. (org-at-heading-p))
  18267. (org-call-with-arg 'org-todo 'nextset))
  18268. (org-support-shift-select
  18269. (org-call-for-shift-select 'forward-word))
  18270. (t (org-shiftselect-error))))
  18271. (defun org-shiftcontrolleft ()
  18272. "Switch to previous TODO set."
  18273. (interactive)
  18274. (cond
  18275. ((and org-support-shift-select (org-region-active-p))
  18276. (org-call-for-shift-select 'backward-word))
  18277. ((and (not (eq org-support-shift-select 'always))
  18278. (org-at-heading-p))
  18279. (org-call-with-arg 'org-todo 'previousset))
  18280. (org-support-shift-select
  18281. (org-call-for-shift-select 'backward-word))
  18282. (t (org-shiftselect-error))))
  18283. (defun org-shiftcontrolup (&optional n)
  18284. "Change timestamps synchronously up in CLOCK log lines.
  18285. Optional argument N tells to change by that many units."
  18286. (interactive "P")
  18287. (cond ((and (not org-support-shift-select)
  18288. (org-at-clock-log-p)
  18289. (org-at-timestamp-p t))
  18290. (org-clock-timestamps-up n))
  18291. (t (org-shiftselect-error))))
  18292. (defun org-shiftcontroldown (&optional n)
  18293. "Change timestamps synchronously down in CLOCK log lines.
  18294. Optional argument N tells to change by that many units."
  18295. (interactive "P")
  18296. (cond ((and (not org-support-shift-select)
  18297. (org-at-clock-log-p)
  18298. (org-at-timestamp-p t))
  18299. (org-clock-timestamps-down n))
  18300. (t (org-shiftselect-error))))
  18301. (defun org-ctrl-c-ret ()
  18302. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18303. (interactive)
  18304. (cond
  18305. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18306. (t (call-interactively 'org-insert-heading))))
  18307. (defun org-find-visible ()
  18308. (let ((s (point)))
  18309. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18310. (get-char-property s 'invisible)))
  18311. s))
  18312. (defun org-find-invisible ()
  18313. (let ((s (point)))
  18314. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18315. (not (get-char-property s 'invisible))))
  18316. s))
  18317. (defun org-copy-visible (beg end)
  18318. "Copy the visible parts of the region."
  18319. (interactive "r")
  18320. (let (snippets s)
  18321. (save-excursion
  18322. (save-restriction
  18323. (narrow-to-region beg end)
  18324. (setq s (goto-char (point-min)))
  18325. (while (not (= (point) (point-max)))
  18326. (goto-char (org-find-invisible))
  18327. (push (buffer-substring s (point)) snippets)
  18328. (setq s (goto-char (org-find-visible))))))
  18329. (kill-new (apply 'concat (nreverse snippets)))))
  18330. (defun org-copy-special ()
  18331. "Copy region in table or copy current subtree.
  18332. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18333. See the individual commands for more information."
  18334. (interactive)
  18335. (call-interactively
  18336. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18337. (defun org-cut-special ()
  18338. "Cut region in table or cut current subtree.
  18339. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18340. See the individual commands for more information."
  18341. (interactive)
  18342. (call-interactively
  18343. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18344. (defun org-paste-special (arg)
  18345. "Paste rectangular region into table, or past subtree relative to level.
  18346. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18347. See the individual commands for more information."
  18348. (interactive "P")
  18349. (if (org-at-table-p)
  18350. (org-table-paste-rectangle)
  18351. (org-paste-subtree arg)))
  18352. (defsubst org-in-fixed-width-region-p ()
  18353. "Is point in a fixed-width region?"
  18354. (save-match-data
  18355. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18356. (defun org-edit-special (&optional arg)
  18357. "Call a special editor for the element at point.
  18358. When at a table, call the formula editor with `org-table-edit-formulas'.
  18359. When in a source code block, call `org-edit-src-code'.
  18360. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18361. When at an #+INCLUDE keyword, visit the included file.
  18362. On a link, call `ffap' to visit the link at point.
  18363. Otherwise, return a user error."
  18364. (interactive "P")
  18365. (let ((element (org-element-at-point)))
  18366. (assert (not buffer-read-only) nil
  18367. "Buffer is read-only: %s" (buffer-name))
  18368. (case (org-element-type element)
  18369. (src-block
  18370. (if (not arg) (org-edit-src-code)
  18371. (let* ((info (org-babel-get-src-block-info))
  18372. (lang (nth 0 info))
  18373. (params (nth 2 info))
  18374. (session (cdr (assq :session params))))
  18375. (if (not session) (org-edit-src-code)
  18376. ;; At a src-block with a session and function called with
  18377. ;; an ARG: switch to the buffer related to the inferior
  18378. ;; process.
  18379. (switch-to-buffer
  18380. (funcall (intern (concat "org-babel-prep-session:" lang))
  18381. session params))))))
  18382. (keyword
  18383. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18384. (find-file
  18385. (org-remove-double-quotes
  18386. (car (org-split-string (org-element-property :value element)))))
  18387. (user-error "No special environment to edit here")))
  18388. (table
  18389. (if (eq (org-element-property :type element) 'table.el)
  18390. (org-edit-src-code)
  18391. (call-interactively 'org-table-edit-formulas)))
  18392. ;; Only Org tables contain `table-row' type elements.
  18393. (table-row (call-interactively 'org-table-edit-formulas))
  18394. ((example-block export-block) (org-edit-src-code))
  18395. (fixed-width (org-edit-fixed-width-region))
  18396. (otherwise
  18397. ;; No notable element at point. Though, we may be at a link,
  18398. ;; which is an object. Thus, scan deeper.
  18399. (if (eq (org-element-type (org-element-context element)) 'link)
  18400. (call-interactively 'ffap)
  18401. (user-error "No special environment to edit here"))))))
  18402. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18403. (defun org-ctrl-c-ctrl-c (&optional arg)
  18404. "Set tags in headline, or update according to changed information at point.
  18405. This command does many different things, depending on context:
  18406. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18407. this is what we do.
  18408. - If the cursor is on a statistics cookie, update it.
  18409. - If the cursor is in a headline, prompt for tags and insert them
  18410. into the current line, aligned to `org-tags-column'. When called
  18411. with prefix arg, realign all tags in the current buffer.
  18412. - If the cursor is in one of the special #+KEYWORD lines, this
  18413. triggers scanning the buffer for these lines and updating the
  18414. information.
  18415. - If the cursor is inside a table, realign the table. This command
  18416. works even if the automatic table editor has been turned off.
  18417. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18418. the entire table.
  18419. - If the cursor is at a footnote reference or definition, jump to
  18420. the corresponding definition or references, respectively.
  18421. - If the cursor is a the beginning of a dynamic block, update it.
  18422. - If the current buffer is a capture buffer, close note and file it.
  18423. - If the cursor is on a <<<target>>>, update radio targets and
  18424. corresponding links in this buffer.
  18425. - If the cursor is on a numbered item in a plain list, renumber the
  18426. ordered list.
  18427. - If the cursor is on a checkbox, toggle it.
  18428. - If the cursor is on a code block, evaluate it. The variable
  18429. `org-confirm-babel-evaluate' can be used to control prompting
  18430. before code block evaluation, by default every code block
  18431. evaluation requires confirmation. Code block evaluation can be
  18432. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18433. (interactive "P")
  18434. (cond
  18435. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18436. org-occur-highlights
  18437. org-latex-fragment-image-overlays)
  18438. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18439. (org-remove-occur-highlights)
  18440. (org-remove-latex-fragment-image-overlays)
  18441. (message "Temporary highlights/overlays removed from current buffer"))
  18442. ((and (local-variable-p 'org-finish-function (current-buffer))
  18443. (fboundp org-finish-function))
  18444. (funcall org-finish-function))
  18445. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18446. (t
  18447. (let* ((context (org-element-context)) (type (org-element-type context)))
  18448. ;; Test if point is within a blank line.
  18449. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18450. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18451. (user-error "C-c C-c can do nothing useful at this location"))
  18452. ;; For convenience: at the first line of a paragraph on the
  18453. ;; same line as an item, apply function on that item instead.
  18454. (when (eq type 'paragraph)
  18455. (let ((parent (org-element-property :parent context)))
  18456. (when (and (eq (org-element-type parent) 'item)
  18457. (= (point-at-bol) (org-element-property :begin parent)))
  18458. (setq context parent type 'item))))
  18459. ;; Act according to type of element or object at point.
  18460. (case type
  18461. (clock (org-clock-update-time-maybe))
  18462. (dynamic-block
  18463. (save-excursion
  18464. (goto-char (org-element-property :post-affiliated context))
  18465. (org-update-dblock)))
  18466. (footnote-definition
  18467. (goto-char (org-element-property :post-affiliated context))
  18468. (call-interactively 'org-footnote-action))
  18469. (footnote-reference (call-interactively 'org-footnote-action))
  18470. ((headline inlinetask)
  18471. (save-excursion (goto-char (org-element-property :begin context))
  18472. (call-interactively 'org-set-tags)))
  18473. (item
  18474. ;; At an item: a double C-u set checkbox to "[-]"
  18475. ;; unconditionally, whereas a single one will toggle its
  18476. ;; presence. Without an universal argument, if the item
  18477. ;; has a checkbox, toggle it. Otherwise repair the list.
  18478. (let* ((box (org-element-property :checkbox context))
  18479. (struct (org-element-property :structure context))
  18480. (old-struct (copy-tree struct))
  18481. (parents (org-list-parents-alist struct))
  18482. (prevs (org-list-prevs-alist struct))
  18483. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18484. (org-list-set-checkbox
  18485. (org-element-property :begin context) struct
  18486. (cond ((equal arg '(16)) "[-]")
  18487. ((and (not box) (equal arg '(4))) "[ ]")
  18488. ((or (not box) (equal arg '(4))) nil)
  18489. ((eq box 'on) "[ ]")
  18490. (t "[X]")))
  18491. ;; Mimic `org-list-write-struct' but with grabbing
  18492. ;; a return value from `org-list-struct-fix-box'.
  18493. (org-list-struct-fix-ind struct parents 2)
  18494. (org-list-struct-fix-item-end struct)
  18495. (org-list-struct-fix-bul struct prevs)
  18496. (org-list-struct-fix-ind struct parents)
  18497. (let ((block-item
  18498. (org-list-struct-fix-box struct parents prevs orderedp)))
  18499. (if (and box (equal struct old-struct))
  18500. (if (equal arg '(16))
  18501. (message "Checkboxes already reset")
  18502. (user-error "Cannot toggle this checkbox: %s"
  18503. (if (eq box 'on)
  18504. "all subitems checked"
  18505. "unchecked subitems")))
  18506. (org-list-struct-apply-struct struct old-struct)
  18507. (org-update-checkbox-count-maybe))
  18508. (when block-item
  18509. (message "Checkboxes were removed due to empty box at line %d"
  18510. (org-current-line block-item))))))
  18511. (keyword
  18512. (let ((org-inhibit-startup-visibility-stuff t)
  18513. (org-startup-align-all-tables nil))
  18514. (when (boundp 'org-table-coordinate-overlays)
  18515. (mapc 'delete-overlay org-table-coordinate-overlays)
  18516. (setq org-table-coordinate-overlays nil))
  18517. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18518. (message "Local setup has been refreshed"))
  18519. (plain-list
  18520. ;; At a plain list, with a double C-u argument, set
  18521. ;; checkboxes of each item to "[-]", whereas a single one
  18522. ;; will toggle their presence according to the state of the
  18523. ;; first item in the list. Without an argument, repair the
  18524. ;; list.
  18525. (let* ((begin (org-element-property :contents-begin context))
  18526. (beginm (move-marker (make-marker) begin))
  18527. (struct (org-element-property :structure context))
  18528. (old-struct (copy-tree struct))
  18529. (first-box (save-excursion
  18530. (goto-char begin)
  18531. (looking-at org-list-full-item-re)
  18532. (match-string-no-properties 3)))
  18533. (new-box (cond ((equal arg '(16)) "[-]")
  18534. ((equal arg '(4)) (unless first-box "[ ]"))
  18535. ((equal first-box "[X]") "[ ]")
  18536. (t "[X]"))))
  18537. (cond
  18538. (arg
  18539. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18540. (org-list-get-all-items
  18541. begin struct (org-list-prevs-alist struct))))
  18542. ((and first-box (eq (point) begin))
  18543. ;; For convenience, when point is at bol on the first
  18544. ;; item of the list and no argument is provided, simply
  18545. ;; toggle checkbox of that item, if any.
  18546. (org-list-set-checkbox begin struct new-box)))
  18547. (org-list-write-struct
  18548. struct (org-list-parents-alist struct) old-struct)
  18549. (org-update-checkbox-count-maybe)
  18550. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18551. ((property-drawer node-property)
  18552. (call-interactively 'org-property-action))
  18553. ((radio-target target)
  18554. (call-interactively 'org-update-radio-target-regexp))
  18555. (statistics-cookie
  18556. (call-interactively 'org-update-statistics-cookies))
  18557. ((table table-cell table-row)
  18558. ;; At a table, recalculate every field and align it. Also
  18559. ;; send the table if necessary. If the table has
  18560. ;; a `table.el' type, just give up. At a table row or
  18561. ;; cell, maybe recalculate line but always align table.
  18562. (if (eq (org-element-property :type context) 'table.el)
  18563. (message "Use C-c ' to edit table.el tables")
  18564. (let ((org-enable-table-editor t))
  18565. (if (or (eq type 'table)
  18566. ;; Check if point is at a TBLFM line.
  18567. (and (eq type 'table-row)
  18568. (= (point) (org-element-property :end context))))
  18569. (save-excursion
  18570. (if (org-at-TBLFM-p) (org-calc-current-TBLFM)
  18571. (goto-char (org-element-property :contents-begin context))
  18572. (org-call-with-arg 'org-table-recalculate (or arg t))
  18573. (orgtbl-send-table 'maybe)))
  18574. (org-table-maybe-eval-formula)
  18575. (cond (arg (call-interactively 'org-table-recalculate))
  18576. ((org-table-maybe-recalculate-line))
  18577. (t (org-table-align)))))))
  18578. (timestamp (org-timestamp-change 0 'day))
  18579. (otherwise
  18580. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18581. (user-error
  18582. "C-c C-c can do nothing useful at this location")))))))))
  18583. (defun org-mode-restart ()
  18584. "Restart Org-mode, to scan again for special lines.
  18585. Also updates the keyword regular expressions."
  18586. (interactive)
  18587. (org-mode)
  18588. (message "Org-mode restarted"))
  18589. (defun org-kill-note-or-show-branches ()
  18590. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18591. (interactive)
  18592. (if (not org-finish-function)
  18593. (progn
  18594. (hide-subtree)
  18595. (call-interactively 'show-branches))
  18596. (let ((org-note-abort t))
  18597. (funcall org-finish-function))))
  18598. (defun org-ctrl-o (n)
  18599. "Insert a new row in tables, call `open-line' elsewhere."
  18600. (interactive "*p")
  18601. (if (org-at-table-p)
  18602. (org-table-insert-row)
  18603. (open-line n)))
  18604. (defun org-return (&optional indent)
  18605. "Goto next table row or insert a newline.
  18606. Calls `org-table-next-row' or `newline', depending on context.
  18607. See the individual commands for more information."
  18608. (interactive)
  18609. (let (org-ts-what)
  18610. (cond
  18611. ((or (bobp) (org-in-src-block-p))
  18612. (if indent (newline-and-indent) (newline)))
  18613. ((org-at-table-p)
  18614. (org-table-justify-field-maybe)
  18615. (call-interactively 'org-table-next-row))
  18616. ;; when `newline-and-indent' is called within a list, make sure
  18617. ;; text moved stays inside the item.
  18618. ((and (org-in-item-p) indent)
  18619. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18620. (progn
  18621. (save-match-data (newline))
  18622. (org-indent-line-to (length (match-string 0))))
  18623. (let ((ind (org-get-indentation)))
  18624. (newline)
  18625. (if (org-looking-back org-list-end-re)
  18626. (org-indent-line)
  18627. (org-indent-line-to ind)))))
  18628. ((and org-return-follows-link
  18629. (org-at-timestamp-p t)
  18630. (not (eq org-ts-what 'after)))
  18631. (org-follow-timestamp-link))
  18632. ((and org-return-follows-link
  18633. (let ((tprop (get-text-property (point) 'face)))
  18634. (or (eq tprop 'org-link)
  18635. (and (listp tprop) (memq 'org-link tprop)))))
  18636. (call-interactively 'org-open-at-point))
  18637. ((and (org-at-heading-p)
  18638. (looking-at
  18639. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18640. (org-show-entry)
  18641. (end-of-line 1)
  18642. (newline))
  18643. (t (if indent (newline-and-indent) (newline))))))
  18644. (defun org-return-indent ()
  18645. "Goto next table row or insert a newline and indent.
  18646. Calls `org-table-next-row' or `newline-and-indent', depending on
  18647. context. See the individual commands for more information."
  18648. (interactive)
  18649. (org-return t))
  18650. (defun org-ctrl-c-star ()
  18651. "Compute table, or change heading status of lines.
  18652. Calls `org-table-recalculate' or `org-toggle-heading',
  18653. depending on context."
  18654. (interactive)
  18655. (cond
  18656. ((org-at-table-p)
  18657. (call-interactively 'org-table-recalculate))
  18658. (t
  18659. ;; Convert all lines in region to list items
  18660. (call-interactively 'org-toggle-heading))))
  18661. (defun org-ctrl-c-minus ()
  18662. "Insert separator line in table or modify bullet status of line.
  18663. Also turns a plain line or a region of lines into list items.
  18664. Calls `org-table-insert-hline', `org-toggle-item', or
  18665. `org-cycle-list-bullet', depending on context."
  18666. (interactive)
  18667. (cond
  18668. ((org-at-table-p)
  18669. (call-interactively 'org-table-insert-hline))
  18670. ((org-region-active-p)
  18671. (call-interactively 'org-toggle-item))
  18672. ((org-in-item-p)
  18673. (call-interactively 'org-cycle-list-bullet))
  18674. (t
  18675. (call-interactively 'org-toggle-item))))
  18676. (defun org-toggle-item (arg)
  18677. "Convert headings or normal lines to items, items to normal lines.
  18678. If there is no active region, only the current line is considered.
  18679. If the first non blank line in the region is a headline, convert
  18680. all headlines to items, shifting text accordingly.
  18681. If it is an item, convert all items to normal lines.
  18682. If it is normal text, change region into a list of items.
  18683. With a prefix argument ARG, change the region in a single item."
  18684. (interactive "P")
  18685. (let ((shift-text
  18686. (function
  18687. ;; Shift text in current section to IND, from point to END.
  18688. ;; The function leaves point to END line.
  18689. (lambda (ind end)
  18690. (let ((min-i 1000) (end (copy-marker end)))
  18691. ;; First determine the minimum indentation (MIN-I) of
  18692. ;; the text.
  18693. (save-excursion
  18694. (catch 'exit
  18695. (while (< (point) end)
  18696. (let ((i (org-get-indentation)))
  18697. (cond
  18698. ;; Skip blank lines and inline tasks.
  18699. ((looking-at "^[ \t]*$"))
  18700. ((looking-at org-outline-regexp-bol))
  18701. ;; We can't find less than 0 indentation.
  18702. ((zerop i) (throw 'exit (setq min-i 0)))
  18703. ((< i min-i) (setq min-i i))))
  18704. (forward-line))))
  18705. ;; Then indent each line so that a line indented to
  18706. ;; MIN-I becomes indented to IND. Ignore blank lines
  18707. ;; and inline tasks in the process.
  18708. (let ((delta (- ind min-i)))
  18709. (while (< (point) end)
  18710. (unless (or (looking-at "^[ \t]*$")
  18711. (looking-at org-outline-regexp-bol))
  18712. (org-indent-line-to (+ (org-get-indentation) delta)))
  18713. (forward-line)))))))
  18714. (skip-blanks
  18715. (function
  18716. ;; Return beginning of first non-blank line, starting from
  18717. ;; line at POS.
  18718. (lambda (pos)
  18719. (save-excursion
  18720. (goto-char pos)
  18721. (skip-chars-forward " \r\t\n")
  18722. (point-at-bol)))))
  18723. beg end)
  18724. ;; Determine boundaries of changes.
  18725. (if (org-region-active-p)
  18726. (setq beg (funcall skip-blanks (region-beginning))
  18727. end (copy-marker (region-end)))
  18728. (setq beg (funcall skip-blanks (point-at-bol))
  18729. end (copy-marker (point-at-eol))))
  18730. ;; Depending on the starting line, choose an action on the text
  18731. ;; between BEG and END.
  18732. (org-with-limited-levels
  18733. (save-excursion
  18734. (goto-char beg)
  18735. (cond
  18736. ;; Case 1. Start at an item: de-itemize. Note that it only
  18737. ;; happens when a region is active: `org-ctrl-c-minus'
  18738. ;; would call `org-cycle-list-bullet' otherwise.
  18739. ((org-at-item-p)
  18740. (while (< (point) end)
  18741. (when (org-at-item-p)
  18742. (skip-chars-forward " \t")
  18743. (delete-region (point) (match-end 0)))
  18744. (forward-line)))
  18745. ;; Case 2. Start at an heading: convert to items.
  18746. ((org-at-heading-p)
  18747. (let* ((bul (org-list-bullet-string "-"))
  18748. (bul-len (length bul))
  18749. ;; Indentation of the first heading. It should be
  18750. ;; relative to the indentation of its parent, if any.
  18751. (start-ind (save-excursion
  18752. (cond
  18753. ((not org-adapt-indentation) 0)
  18754. ((not (outline-previous-heading)) 0)
  18755. (t (length (match-string 0))))))
  18756. ;; Level of first heading. Further headings will be
  18757. ;; compared to it to determine hierarchy in the list.
  18758. (ref-level (org-reduced-level (org-outline-level))))
  18759. (while (< (point) end)
  18760. (let* ((level (org-reduced-level (org-outline-level)))
  18761. (delta (max 0 (- level ref-level))))
  18762. ;; If current headline is less indented than the first
  18763. ;; one, set it as reference, in order to preserve
  18764. ;; subtrees.
  18765. (when (< level ref-level) (setq ref-level level))
  18766. (replace-match bul t t)
  18767. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18768. ;; Ensure all text down to END (or SECTION-END) belongs
  18769. ;; to the newly created item.
  18770. (let ((section-end (save-excursion
  18771. (or (outline-next-heading) (point)))))
  18772. (forward-line)
  18773. (funcall shift-text
  18774. (+ start-ind (* (1+ delta) bul-len))
  18775. (min end section-end)))))))
  18776. ;; Case 3. Normal line with ARG: make the first line of region
  18777. ;; an item, and shift indentation of others lines to
  18778. ;; set them as item's body.
  18779. (arg (let* ((bul (org-list-bullet-string "-"))
  18780. (bul-len (length bul))
  18781. (ref-ind (org-get-indentation)))
  18782. (skip-chars-forward " \t")
  18783. (insert bul)
  18784. (forward-line)
  18785. (while (< (point) end)
  18786. ;; Ensure that lines less indented than first one
  18787. ;; still get included in item body.
  18788. (funcall shift-text
  18789. (+ ref-ind bul-len)
  18790. (min end (save-excursion (or (outline-next-heading)
  18791. (point)))))
  18792. (forward-line))))
  18793. ;; Case 4. Normal line without ARG: turn each non-item line
  18794. ;; into an item.
  18795. (t
  18796. (while (< (point) end)
  18797. (unless (or (org-at-heading-p) (org-at-item-p))
  18798. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18799. (replace-match
  18800. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18801. (forward-line))))))))
  18802. (defun org-toggle-heading (&optional nstars)
  18803. "Convert headings to normal text, or items or text to headings.
  18804. If there is no active region, only convert the current line.
  18805. With a \\[universal-argument] prefix, convert the whole list at
  18806. point into heading.
  18807. In a region:
  18808. - If the first non blank line is a headline, remove the stars
  18809. from all headlines in the region.
  18810. - If it is a normal line, turn each and every normal line (i.e.,
  18811. not an heading or an item) in the region into headings. If you
  18812. want to convert only the first line of this region, use one
  18813. universal prefix argument.
  18814. - If it is a plain list item, turn all plain list items into headings.
  18815. When converting a line into a heading, the number of stars is chosen
  18816. such that the lines become children of the current entry. However,
  18817. when a numeric prefix argument is given, its value determines the
  18818. number of stars to add."
  18819. (interactive "P")
  18820. (let ((skip-blanks
  18821. (function
  18822. ;; Return beginning of first non-blank line, starting from
  18823. ;; line at POS.
  18824. (lambda (pos)
  18825. (save-excursion
  18826. (goto-char pos)
  18827. (while (org-at-comment-p) (forward-line))
  18828. (skip-chars-forward " \r\t\n")
  18829. (point-at-bol)))))
  18830. beg end toggled)
  18831. ;; Determine boundaries of changes. If a universal prefix has
  18832. ;; been given, put the list in a region. If region ends at a bol,
  18833. ;; do not consider the last line to be in the region.
  18834. (when (and current-prefix-arg (org-at-item-p))
  18835. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18836. (org-mark-element))
  18837. (if (org-region-active-p)
  18838. (setq beg (funcall skip-blanks (region-beginning))
  18839. end (copy-marker (save-excursion
  18840. (goto-char (region-end))
  18841. (if (bolp) (point) (point-at-eol)))))
  18842. (setq beg (funcall skip-blanks (point-at-bol))
  18843. end (copy-marker (point-at-eol))))
  18844. ;; Ensure inline tasks don't count as headings.
  18845. (org-with-limited-levels
  18846. (save-excursion
  18847. (goto-char beg)
  18848. (cond
  18849. ;; Case 1. Started at an heading: de-star headings.
  18850. ((org-at-heading-p)
  18851. (while (< (point) end)
  18852. (when (org-at-heading-p t)
  18853. (looking-at org-outline-regexp) (replace-match "")
  18854. (setq toggled t))
  18855. (forward-line)))
  18856. ;; Case 2. Started at an item: change items into headlines.
  18857. ;; One star will be added by `org-list-to-subtree'.
  18858. ((org-at-item-p)
  18859. (let* ((stars (make-string
  18860. ;; subtract the star that will be added again by
  18861. ;; `org-list-to-subtree'
  18862. (if (numberp nstars) (1- nstars)
  18863. (or (org-current-level) 0))
  18864. ?*))
  18865. (add-stars
  18866. (cond (nstars "") ; stars from prefix only
  18867. ((equal stars "") "") ; before first heading
  18868. (org-odd-levels-only "*") ; inside heading, odd
  18869. (t "")))) ; inside heading, oddeven
  18870. (while (< (point) end)
  18871. (when (org-at-item-p)
  18872. ;; Pay attention to cases when region ends before list.
  18873. (let* ((struct (org-list-struct))
  18874. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18875. (save-restriction
  18876. (narrow-to-region (point) list-end)
  18877. (insert
  18878. (org-list-to-subtree
  18879. (org-list-parse-list t)
  18880. '(:istart (concat stars add-stars (funcall get-stars depth))
  18881. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18882. (setq toggled t))
  18883. (forward-line))))
  18884. ;; Case 3. Started at normal text: make every line an heading,
  18885. ;; skipping headlines and items.
  18886. (t (let* ((stars
  18887. (make-string
  18888. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18889. (add-stars
  18890. (cond (nstars "") ; stars from prefix only
  18891. ((equal stars "") "*") ; before first heading
  18892. (org-odd-levels-only "**") ; inside heading, odd
  18893. (t "*"))) ; inside heading, oddeven
  18894. (rpl (concat stars add-stars " "))
  18895. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18896. (while (< (point) (if (equal nstars '(4)) lend end))
  18897. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18898. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18899. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18900. (forward-line)))))))
  18901. (unless toggled (message "Cannot toggle heading from here"))))
  18902. (defun org-meta-return (&optional arg)
  18903. "Insert a new heading or wrap a region in a table.
  18904. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18905. See the individual commands for more information."
  18906. (interactive "P")
  18907. (cond
  18908. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18909. ((or (org-at-drawer-p) (org-at-property-p))
  18910. (newline-and-indent))
  18911. ((org-at-table-p)
  18912. (call-interactively 'org-table-wrap-region))
  18913. (t (call-interactively 'org-insert-heading))))
  18914. ;;; Menu entries
  18915. (defsubst org-in-subtree-not-table-p ()
  18916. "Are we in a subtree and not in a table?"
  18917. (and (not (org-before-first-heading-p))
  18918. (not (org-at-table-p))))
  18919. ;; Define the Org-mode menus
  18920. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18921. '("Tbl"
  18922. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18923. ["Next Field" org-cycle (org-at-table-p)]
  18924. ["Previous Field" org-shifttab (org-at-table-p)]
  18925. ["Next Row" org-return (org-at-table-p)]
  18926. "--"
  18927. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18928. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18929. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18930. "--"
  18931. ("Column"
  18932. ["Move Column Left" org-metaleft (org-at-table-p)]
  18933. ["Move Column Right" org-metaright (org-at-table-p)]
  18934. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18935. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18936. ("Row"
  18937. ["Move Row Up" org-metaup (org-at-table-p)]
  18938. ["Move Row Down" org-metadown (org-at-table-p)]
  18939. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18940. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18941. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18942. "--"
  18943. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18944. ("Rectangle"
  18945. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18946. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18947. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18948. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18949. "--"
  18950. ("Calculate"
  18951. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18952. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18953. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18954. "--"
  18955. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18956. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18957. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18958. "--"
  18959. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18960. "--"
  18961. ["Sum Column/Rectangle" org-table-sum
  18962. (or (org-at-table-p) (org-region-active-p))]
  18963. ["Which Column?" org-table-current-column (org-at-table-p)])
  18964. ["Debug Formulas"
  18965. org-table-toggle-formula-debugger
  18966. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18967. ["Show Col/Row Numbers"
  18968. org-table-toggle-coordinate-overlays
  18969. :style toggle
  18970. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18971. "--"
  18972. ["Create" org-table-create (and (not (org-at-table-p))
  18973. org-enable-table-editor)]
  18974. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18975. ["Import from File" org-table-import (not (org-at-table-p))]
  18976. ["Export to File" org-table-export (org-at-table-p)]
  18977. "--"
  18978. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18979. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18980. '("Org"
  18981. ("Show/Hide"
  18982. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18983. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18984. ["Sparse Tree..." org-sparse-tree t]
  18985. ["Reveal Context" org-reveal t]
  18986. ["Show All" show-all t]
  18987. "--"
  18988. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18989. "--"
  18990. ["New Heading" org-insert-heading t]
  18991. ("Navigate Headings"
  18992. ["Up" outline-up-heading t]
  18993. ["Next" outline-next-visible-heading t]
  18994. ["Previous" outline-previous-visible-heading t]
  18995. ["Next Same Level" outline-forward-same-level t]
  18996. ["Previous Same Level" outline-backward-same-level t]
  18997. "--"
  18998. ["Jump" org-goto t])
  18999. ("Edit Structure"
  19000. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19001. "--"
  19002. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19003. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19004. "--"
  19005. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19006. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19007. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19008. "--"
  19009. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19010. "--"
  19011. ["Copy visible text" org-copy-visible t]
  19012. "--"
  19013. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19014. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19015. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19016. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19017. "--"
  19018. ["Sort Region/Children" org-sort t]
  19019. "--"
  19020. ["Convert to odd levels" org-convert-to-odd-levels t]
  19021. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19022. ("Editing"
  19023. ["Emphasis..." org-emphasize t]
  19024. ["Edit Source Example" org-edit-special t]
  19025. "--"
  19026. ["Footnote new/jump" org-footnote-action t]
  19027. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19028. ("Archive"
  19029. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19030. "--"
  19031. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19032. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19033. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19034. )
  19035. "--"
  19036. ("Hyperlinks"
  19037. ["Store Link (Global)" org-store-link t]
  19038. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19039. ["Insert Link" org-insert-link t]
  19040. ["Follow Link" org-open-at-point t]
  19041. "--"
  19042. ["Next link" org-next-link t]
  19043. ["Previous link" org-previous-link t]
  19044. "--"
  19045. ["Descriptive Links"
  19046. org-toggle-link-display
  19047. :style radio
  19048. :selected org-descriptive-links
  19049. ]
  19050. ["Literal Links"
  19051. org-toggle-link-display
  19052. :style radio
  19053. :selected (not org-descriptive-links)])
  19054. "--"
  19055. ("TODO Lists"
  19056. ["TODO/DONE/-" org-todo t]
  19057. ("Select keyword"
  19058. ["Next keyword" org-shiftright (org-at-heading-p)]
  19059. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19060. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19061. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19062. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19063. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19064. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19065. "--"
  19066. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19067. :selected org-enforce-todo-dependencies :style toggle :active t]
  19068. "Settings for tree at point"
  19069. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19070. :selected (org-entry-get nil "ORDERED")
  19071. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19072. ["Do Children parallel" org-toggle-ordered-property :style radio
  19073. :selected (not (org-entry-get nil "ORDERED"))
  19074. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19075. "--"
  19076. ["Set Priority" org-priority t]
  19077. ["Priority Up" org-shiftup t]
  19078. ["Priority Down" org-shiftdown t]
  19079. "--"
  19080. ["Get news from all feeds" org-feed-update-all t]
  19081. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19082. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19083. ("TAGS and Properties"
  19084. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19085. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19086. "--"
  19087. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19088. ["Column view of properties" org-columns t]
  19089. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19090. ("Dates and Scheduling"
  19091. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19092. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19093. ("Change Date"
  19094. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19095. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19096. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19097. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19098. ["Compute Time Range" org-evaluate-time-range t]
  19099. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19100. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19101. "--"
  19102. ["Custom time format" org-toggle-time-stamp-overlays
  19103. :style radio :selected org-display-custom-times]
  19104. "--"
  19105. ["Goto Calendar" org-goto-calendar t]
  19106. ["Date from Calendar" org-date-from-calendar t]
  19107. "--"
  19108. ["Start/Restart Timer" org-timer-start t]
  19109. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19110. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19111. ["Insert Timer String" org-timer t]
  19112. ["Insert Timer Item" org-timer-item t])
  19113. ("Logging work"
  19114. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19115. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19116. ["Clock out" org-clock-out t]
  19117. ["Clock cancel" org-clock-cancel t]
  19118. "--"
  19119. ["Mark as default task" org-clock-mark-default-task t]
  19120. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19121. ["Goto running clock" org-clock-goto t]
  19122. "--"
  19123. ["Display times" org-clock-display t]
  19124. ["Create clock table" org-clock-report t]
  19125. "--"
  19126. ["Record DONE time"
  19127. (progn (setq org-log-done (not org-log-done))
  19128. (message "Switching to %s will %s record a timestamp"
  19129. (car org-done-keywords)
  19130. (if org-log-done "automatically" "not")))
  19131. :style toggle :selected org-log-done])
  19132. "--"
  19133. ["Agenda Command..." org-agenda t]
  19134. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19135. ("File List for Agenda")
  19136. ("Special views current file"
  19137. ["TODO Tree" org-show-todo-tree t]
  19138. ["Check Deadlines" org-check-deadlines t]
  19139. ["Timeline" org-timeline t]
  19140. ["Tags/Property tree" org-match-sparse-tree t])
  19141. "--"
  19142. ["Export/Publish..." org-export-dispatch t]
  19143. ("LaTeX"
  19144. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19145. :selected org-cdlatex-mode]
  19146. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19147. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19148. ["Modify math symbol" org-cdlatex-math-modify
  19149. (org-inside-LaTeX-fragment-p)]
  19150. ["Insert citation" org-reftex-citation t]
  19151. "--"
  19152. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19153. "--"
  19154. ("MobileOrg"
  19155. ["Push Files and Views" org-mobile-push t]
  19156. ["Get Captured and Flagged" org-mobile-pull t]
  19157. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19158. "--"
  19159. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19160. "--"
  19161. ("Documentation"
  19162. ["Show Version" org-version t]
  19163. ["Info Documentation" org-info t])
  19164. ("Customize"
  19165. ["Browse Org Group" org-customize t]
  19166. "--"
  19167. ["Expand This Menu" org-create-customize-menu
  19168. (fboundp 'customize-menu-create)])
  19169. ["Send bug report" org-submit-bug-report t]
  19170. "--"
  19171. ("Refresh/Reload"
  19172. ["Refresh setup current buffer" org-mode-restart t]
  19173. ["Reload Org (after update)" org-reload t]
  19174. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19175. ))
  19176. (defun org-info (&optional node)
  19177. "Read documentation for Org-mode in the info system.
  19178. With optional NODE, go directly to that node."
  19179. (interactive)
  19180. (info (format "(org)%s" (or node ""))))
  19181. ;;;###autoload
  19182. (defun org-submit-bug-report ()
  19183. "Submit a bug report on Org-mode via mail.
  19184. Don't hesitate to report any problems or inaccurate documentation.
  19185. If you don't have setup sending mail from (X)Emacs, please copy the
  19186. output buffer into your mail program, as it gives us important
  19187. information about your Org-mode version and configuration."
  19188. (interactive)
  19189. (require 'reporter)
  19190. (org-load-modules-maybe)
  19191. (org-require-autoloaded-modules)
  19192. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19193. (reporter-submit-bug-report
  19194. "emacs-orgmode@gnu.org"
  19195. (org-version nil 'full)
  19196. (let (list)
  19197. (save-window-excursion
  19198. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19199. (delete-other-windows)
  19200. (erase-buffer)
  19201. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19202. We would like to add your full Org-mode and Outline configuration to the
  19203. bug report. This greatly simplifies the work of the maintainer and
  19204. other experts on the mailing list.
  19205. HOWEVER, some variables you have customized may contain private
  19206. information. The names of customers, colleagues, or friends, might
  19207. appear in the form of file names, tags, todo states, or search strings.
  19208. If you answer yes to the prompt, you might want to check and remove
  19209. such private information before sending the email.")
  19210. (add-text-properties (point-min) (point-max) '(face org-warning))
  19211. (when (yes-or-no-p "Include your Org-mode configuration ")
  19212. (mapatoms
  19213. (lambda (v)
  19214. (and (boundp v)
  19215. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19216. (or (and (symbol-value v)
  19217. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19218. (and
  19219. (get v 'custom-type) (get v 'standard-value)
  19220. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19221. (push v list)))))
  19222. (kill-buffer (get-buffer "*Warn about privacy*"))
  19223. list))
  19224. nil nil
  19225. "Remember to cover the basics, that is, what you expected to happen and
  19226. what in fact did happen. You don't know how to make a good report? See
  19227. http://orgmode.org/manual/Feedback.html#Feedback
  19228. Your bug report will be posted to the Org-mode mailing list.
  19229. ------------------------------------------------------------------------")
  19230. (save-excursion
  19231. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19232. (replace-match "\\1Bug: \\3 [\\2]")))))
  19233. (defun org-install-agenda-files-menu ()
  19234. (let ((bl (buffer-list)))
  19235. (save-excursion
  19236. (while bl
  19237. (set-buffer (pop bl))
  19238. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19239. (when (derived-mode-p 'org-mode)
  19240. (easy-menu-change
  19241. '("Org") "File List for Agenda"
  19242. (append
  19243. (list
  19244. ["Edit File List" (org-edit-agenda-file-list) t]
  19245. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19246. ["Remove Current File from List" org-remove-file t]
  19247. ["Cycle through agenda files" org-cycle-agenda-files t]
  19248. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19249. "--")
  19250. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19251. ;;;; Documentation
  19252. (defun org-require-autoloaded-modules ()
  19253. (interactive)
  19254. (mapc 'require
  19255. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19256. org-remember org-table org-timer)))
  19257. ;;;###autoload
  19258. (defun org-reload (&optional uncompiled)
  19259. "Reload all org lisp files.
  19260. With prefix arg UNCOMPILED, load the uncompiled versions."
  19261. (interactive "P")
  19262. (require 'loadhist)
  19263. (let* ((org-dir (org-find-library-dir "org"))
  19264. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19265. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19266. (remove-re (mapconcat 'identity
  19267. (mapcar (lambda (f) (concat "^" f "$"))
  19268. (list (if (featurep 'xemacs)
  19269. "org-colview"
  19270. "org-colview-xemacs")
  19271. "org" "org-loaddefs" "org-version"))
  19272. "\\|"))
  19273. (feats (delete-dups
  19274. (mapcar 'file-name-sans-extension
  19275. (mapcar 'file-name-nondirectory
  19276. (delq nil
  19277. (mapcar 'feature-file
  19278. features))))))
  19279. (lfeat (append
  19280. (sort
  19281. (setq feats
  19282. (delq nil (mapcar
  19283. (lambda (f)
  19284. (if (and (string-match feature-re f)
  19285. (not (string-match remove-re f)))
  19286. f nil))
  19287. feats)))
  19288. 'string-lessp)
  19289. (list "org-version" "org")))
  19290. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19291. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19292. load-uncore load-misses)
  19293. (setq load-misses
  19294. (delq 't
  19295. (mapcar (lambda (f)
  19296. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19297. (and (string= org-dir contrib-dir)
  19298. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19299. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19300. (add-to-list 'load-uncore f 'append)
  19301. 't)
  19302. f))
  19303. lfeat)))
  19304. (if load-uncore
  19305. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19306. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19307. (if load-misses
  19308. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19309. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19310. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19311. ;;;###autoload
  19312. (defun org-customize ()
  19313. "Call the customize function with org as argument."
  19314. (interactive)
  19315. (org-load-modules-maybe)
  19316. (org-require-autoloaded-modules)
  19317. (customize-browse 'org))
  19318. (defun org-create-customize-menu ()
  19319. "Create a full customization menu for Org-mode, insert it into the menu."
  19320. (interactive)
  19321. (org-load-modules-maybe)
  19322. (org-require-autoloaded-modules)
  19323. (if (fboundp 'customize-menu-create)
  19324. (progn
  19325. (easy-menu-change
  19326. '("Org") "Customize"
  19327. `(["Browse Org group" org-customize t]
  19328. "--"
  19329. ,(customize-menu-create 'org)
  19330. ["Set" Custom-set t]
  19331. ["Save" Custom-save t]
  19332. ["Reset to Current" Custom-reset-current t]
  19333. ["Reset to Saved" Custom-reset-saved t]
  19334. ["Reset to Standard Settings" Custom-reset-standard t]))
  19335. (message "\"Org\"-menu now contains full customization menu"))
  19336. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19337. ;;;; Miscellaneous stuff
  19338. ;;; Generally useful functions
  19339. (defun org-get-at-bol (property)
  19340. "Get text property PROPERTY at beginning of line."
  19341. (get-text-property (point-at-bol) property))
  19342. (defun org-find-text-property-in-string (prop s)
  19343. "Return the first non-nil value of property PROP in string S."
  19344. (or (get-text-property 0 prop s)
  19345. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19346. prop s)))
  19347. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19348. "Display the given MESSAGE as a warning."
  19349. (if (fboundp 'display-warning)
  19350. (display-warning 'org message
  19351. (if (featurep 'xemacs) 'warning :warning))
  19352. (let ((buf (get-buffer-create "*Org warnings*")))
  19353. (with-current-buffer buf
  19354. (goto-char (point-max))
  19355. (insert "Warning (Org): " message)
  19356. (unless (bolp)
  19357. (newline)))
  19358. (display-buffer buf)
  19359. (sit-for 0))))
  19360. (defun org-eval (form)
  19361. "Eval FORM and return result."
  19362. (condition-case error
  19363. (eval form)
  19364. (error (format "%%![Error: %s]" error))))
  19365. (defun org-in-clocktable-p ()
  19366. "Check if the cursor is in a clocktable."
  19367. (let ((pos (point)) start)
  19368. (save-excursion
  19369. (end-of-line 1)
  19370. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19371. (setq start (match-beginning 0))
  19372. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19373. (>= (match-end 0) pos)
  19374. start))))
  19375. (defun org-in-commented-line ()
  19376. "Is point in a line starting with `#'?"
  19377. (equal (char-after (point-at-bol)) ?#))
  19378. (defun org-in-indented-comment-line ()
  19379. "Is point in a line starting with `#' after some white space?"
  19380. (save-excursion
  19381. (save-match-data
  19382. (goto-char (point-at-bol))
  19383. (looking-at "[ \t]*#"))))
  19384. (defun org-in-verbatim-emphasis ()
  19385. (save-match-data
  19386. (and (org-in-regexp org-emph-re 2)
  19387. (>= (point) (match-beginning 3))
  19388. (<= (point) (match-end 4))
  19389. (member (match-string 3) '("=" "~")))))
  19390. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19391. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19392. (if (and marker (marker-buffer marker)
  19393. (buffer-live-p (marker-buffer marker)))
  19394. (progn
  19395. (org-pop-to-buffer-same-window (marker-buffer marker))
  19396. (if (or (> marker (point-max)) (< marker (point-min)))
  19397. (widen))
  19398. (goto-char marker)
  19399. (org-show-context 'org-goto))
  19400. (if bookmark
  19401. (bookmark-jump bookmark)
  19402. (error "Cannot find location"))))
  19403. (defun org-quote-csv-field (s)
  19404. "Quote field for inclusion in CSV material."
  19405. (if (string-match "[\",]" s)
  19406. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19407. s))
  19408. (defun org-force-self-insert (N)
  19409. "Needed to enforce self-insert under remapping."
  19410. (interactive "p")
  19411. (self-insert-command N))
  19412. (defun org-string-width (s)
  19413. "Compute width of string, ignoring invisible characters.
  19414. This ignores character with invisibility property `org-link', and also
  19415. characters with property `org-cwidth', because these will become invisible
  19416. upon the next fontification round."
  19417. (let (b l)
  19418. (when (or (eq t buffer-invisibility-spec)
  19419. (assq 'org-link buffer-invisibility-spec))
  19420. (while (setq b (text-property-any 0 (length s)
  19421. 'invisible 'org-link s))
  19422. (setq s (concat (substring s 0 b)
  19423. (substring s (or (next-single-property-change
  19424. b 'invisible s) (length s)))))))
  19425. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19426. (setq s (concat (substring s 0 b)
  19427. (substring s (or (next-single-property-change
  19428. b 'org-cwidth s) (length s))))))
  19429. (setq l (string-width s) b -1)
  19430. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19431. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19432. l))
  19433. (defun org-shorten-string (s maxlength)
  19434. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19435. If the string is shorter or has length MAXLENGTH, just return the
  19436. original string. If it is longer, the functions finds a space in the
  19437. string, breaks this string off at that locations and adds three dots
  19438. as ellipsis. Including the ellipsis, the string will not be longer
  19439. than MAXLENGTH. If finding a good breaking point in the string does
  19440. not work, the string is just chopped off in the middle of a word
  19441. if necessary."
  19442. (if (<= (length s) maxlength)
  19443. s
  19444. (let* ((n (max (- maxlength 4) 1))
  19445. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19446. (if (string-match re s)
  19447. (concat (match-string 1 s) "...")
  19448. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19449. (defun org-get-indentation (&optional line)
  19450. "Get the indentation of the current line, interpreting tabs.
  19451. When LINE is given, assume it represents a line and compute its indentation."
  19452. (if line
  19453. (if (string-match "^ *" (org-remove-tabs line))
  19454. (match-end 0))
  19455. (save-excursion
  19456. (beginning-of-line 1)
  19457. (skip-chars-forward " \t")
  19458. (current-column))))
  19459. (defun org-get-string-indentation (s)
  19460. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19461. (let ((n -1) (i 0) (w tab-width) c)
  19462. (catch 'exit
  19463. (while (< (setq n (1+ n)) (length s))
  19464. (setq c (aref s n))
  19465. (cond ((= c ?\ ) (setq i (1+ i)))
  19466. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19467. (t (throw 'exit t)))))
  19468. i))
  19469. (defun org-remove-tabs (s &optional width)
  19470. "Replace tabulators in S with spaces.
  19471. Assumes that s is a single line, starting in column 0."
  19472. (setq width (or width tab-width))
  19473. (while (string-match "\t" s)
  19474. (setq s (replace-match
  19475. (make-string
  19476. (- (* width (/ (+ (match-beginning 0) width) width))
  19477. (match-beginning 0)) ?\ )
  19478. t t s)))
  19479. s)
  19480. (defun org-fix-indentation (line ind)
  19481. "Fix indentation in LINE.
  19482. IND is a cons cell with target and minimum indentation.
  19483. If the current indentation in LINE is smaller than the minimum,
  19484. leave it alone. If it is larger than ind, set it to the target."
  19485. (let* ((l (org-remove-tabs line))
  19486. (i (org-get-indentation l))
  19487. (i1 (car ind)) (i2 (cdr ind)))
  19488. (if (>= i i2) (setq l (substring line i2)))
  19489. (if (> i1 0)
  19490. (concat (make-string i1 ?\ ) l)
  19491. l)))
  19492. (defun org-remove-indentation (code &optional n)
  19493. "Remove the maximum common indentation from the lines in CODE.
  19494. N may optionally be the number of spaces to remove."
  19495. (with-temp-buffer
  19496. (insert code)
  19497. (org-do-remove-indentation n)
  19498. (buffer-string)))
  19499. (defun org-do-remove-indentation (&optional n)
  19500. "Remove the maximum common indentation from the buffer."
  19501. (untabify (point-min) (point-max))
  19502. (let ((min 10000) re)
  19503. (if n
  19504. (setq min n)
  19505. (goto-char (point-min))
  19506. (while (re-search-forward "^ *[^ \n]" nil t)
  19507. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19508. (unless (or (= min 0) (= min 10000))
  19509. (setq re (format "^ \\{%d\\}" min))
  19510. (goto-char (point-min))
  19511. (while (re-search-forward re nil t)
  19512. (replace-match "")
  19513. (end-of-line 1))
  19514. min)))
  19515. (defun org-fill-template (template alist)
  19516. "Find each %key of ALIST in TEMPLATE and replace it."
  19517. (let ((case-fold-search nil)
  19518. entry key value)
  19519. (setq alist (sort (copy-sequence alist)
  19520. (lambda (a b) (< (length (car a)) (length (car b))))))
  19521. (while (setq entry (pop alist))
  19522. (setq template
  19523. (replace-regexp-in-string
  19524. (concat "%" (regexp-quote (car entry)))
  19525. (or (cdr entry) "") template t t)))
  19526. template))
  19527. (defun org-base-buffer (buffer)
  19528. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19529. (if (not buffer)
  19530. buffer
  19531. (or (buffer-base-buffer buffer)
  19532. buffer)))
  19533. (defun org-trim (s)
  19534. "Remove whitespace at beginning and end of string."
  19535. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19536. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19537. s)
  19538. (defun org-wrap (string &optional width lines)
  19539. "Wrap string to either a number of lines, or a width in characters.
  19540. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19541. that costs. If there is a word longer than WIDTH, the text is actually
  19542. wrapped to the length of that word.
  19543. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19544. many lines, whatever width that takes.
  19545. The return value is a list of lines, without newlines at the end."
  19546. (let* ((words (org-split-string string "[ \t\n]+"))
  19547. (maxword (apply 'max (mapcar 'org-string-width words)))
  19548. w ll)
  19549. (cond (width
  19550. (org-do-wrap words (max maxword width)))
  19551. (lines
  19552. (setq w maxword)
  19553. (setq ll (org-do-wrap words maxword))
  19554. (if (<= (length ll) lines)
  19555. ll
  19556. (setq ll words)
  19557. (while (> (length ll) lines)
  19558. (setq w (1+ w))
  19559. (setq ll (org-do-wrap words w)))
  19560. ll))
  19561. (t (error "Cannot wrap this")))))
  19562. (defun org-do-wrap (words width)
  19563. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19564. (let (lines line)
  19565. (while words
  19566. (setq line (pop words))
  19567. (while (and words (< (+ (length line) (length (car words))) width))
  19568. (setq line (concat line " " (pop words))))
  19569. (setq lines (push line lines)))
  19570. (nreverse lines)))
  19571. (defun org-split-string (string &optional separators)
  19572. "Splits STRING into substrings at SEPARATORS.
  19573. No empty strings are returned if there are matches at the beginning
  19574. and end of string."
  19575. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19576. (start 0)
  19577. notfirst
  19578. (list nil))
  19579. (while (and (string-match rexp string
  19580. (if (and notfirst
  19581. (= start (match-beginning 0))
  19582. (< start (length string)))
  19583. (1+ start) start))
  19584. (< (match-beginning 0) (length string)))
  19585. (setq notfirst t)
  19586. (or (eq (match-beginning 0) 0)
  19587. (and (eq (match-beginning 0) (match-end 0))
  19588. (eq (match-beginning 0) start))
  19589. (setq list
  19590. (cons (substring string start (match-beginning 0))
  19591. list)))
  19592. (setq start (match-end 0)))
  19593. (or (eq start (length string))
  19594. (setq list
  19595. (cons (substring string start)
  19596. list)))
  19597. (nreverse list)))
  19598. (defun org-quote-vert (s)
  19599. "Replace \"|\" with \"\\vert\"."
  19600. (while (string-match "|" s)
  19601. (setq s (replace-match "\\vert" t t s)))
  19602. s)
  19603. (defun org-uuidgen-p (s)
  19604. "Is S an ID created by UUIDGEN?"
  19605. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19606. (defun org-in-src-block-p (&optional inside)
  19607. "Whether point is in a code source block.
  19608. When INSIDE is non-nil, don't consider we are within a src block
  19609. when point is at #+BEGIN_SRC or #+END_SRC."
  19610. (let ((case-fold-search t) ov)
  19611. (or (and (setq ov (overlays-at (point)))
  19612. (memq 'org-block-background
  19613. (overlay-properties (car ov))))
  19614. (and (not inside)
  19615. (save-match-data
  19616. (save-excursion
  19617. (beginning-of-line)
  19618. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19619. (defun org-context ()
  19620. "Return a list of contexts of the current cursor position.
  19621. If several contexts apply, all are returned.
  19622. Each context entry is a list with a symbol naming the context, and
  19623. two positions indicating start and end of the context. Possible
  19624. contexts are:
  19625. :headline anywhere in a headline
  19626. :headline-stars on the leading stars in a headline
  19627. :todo-keyword on a TODO keyword (including DONE) in a headline
  19628. :tags on the TAGS in a headline
  19629. :priority on the priority cookie in a headline
  19630. :item on the first line of a plain list item
  19631. :item-bullet on the bullet/number of a plain list item
  19632. :checkbox on the checkbox in a plain list item
  19633. :table in an org-mode table
  19634. :table-special on a special filed in a table
  19635. :table-table in a table.el table
  19636. :clocktable in a clocktable
  19637. :src-block in a source block
  19638. :link on a hyperlink
  19639. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19640. :target on a <<target>>
  19641. :radio-target on a <<<radio-target>>>
  19642. :latex-fragment on a LaTeX fragment
  19643. :latex-preview on a LaTeX fragment with overlaid preview image
  19644. This function expects the position to be visible because it uses font-lock
  19645. faces as a help to recognize the following contexts: :table-special, :link,
  19646. and :keyword."
  19647. (let* ((f (get-text-property (point) 'face))
  19648. (faces (if (listp f) f (list f)))
  19649. (case-fold-search t)
  19650. (p (point)) clist o)
  19651. ;; First the large context
  19652. (cond
  19653. ((org-at-heading-p t)
  19654. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19655. (when (progn
  19656. (beginning-of-line 1)
  19657. (looking-at org-todo-line-tags-regexp))
  19658. (push (org-point-in-group p 1 :headline-stars) clist)
  19659. (push (org-point-in-group p 2 :todo-keyword) clist)
  19660. (push (org-point-in-group p 4 :tags) clist))
  19661. (goto-char p)
  19662. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19663. (if (looking-at "\\[#[A-Z0-9]\\]")
  19664. (push (org-point-in-group p 0 :priority) clist)))
  19665. ((org-at-item-p)
  19666. (push (org-point-in-group p 2 :item-bullet) clist)
  19667. (push (list :item (point-at-bol)
  19668. (save-excursion (org-end-of-item) (point)))
  19669. clist)
  19670. (and (org-at-item-checkbox-p)
  19671. (push (org-point-in-group p 0 :checkbox) clist)))
  19672. ((org-at-table-p)
  19673. (push (list :table (org-table-begin) (org-table-end)) clist)
  19674. (if (memq 'org-formula faces)
  19675. (push (list :table-special
  19676. (previous-single-property-change p 'face)
  19677. (next-single-property-change p 'face)) clist)))
  19678. ((org-at-table-p 'any)
  19679. (push (list :table-table) clist)))
  19680. (goto-char p)
  19681. (let ((case-fold-search t))
  19682. ;; New the "medium" contexts: clocktables, source blocks
  19683. (cond ((org-in-clocktable-p)
  19684. (push (list :clocktable
  19685. (and (or (looking-at "#\\+BEGIN: clocktable")
  19686. (search-backward "#+BEGIN: clocktable" nil t))
  19687. (match-beginning 0))
  19688. (and (re-search-forward "#\\+END:?" nil t)
  19689. (match-end 0))) clist))
  19690. ((org-in-src-block-p)
  19691. (push (list :src-block
  19692. (and (or (looking-at "#\\+BEGIN_SRC")
  19693. (search-backward "#+BEGIN_SRC" nil t))
  19694. (match-beginning 0))
  19695. (and (search-forward "#+END_SRC" nil t)
  19696. (match-beginning 0))) clist))))
  19697. (goto-char p)
  19698. ;; Now the small context
  19699. (cond
  19700. ((org-at-timestamp-p)
  19701. (push (org-point-in-group p 0 :timestamp) clist))
  19702. ((memq 'org-link faces)
  19703. (push (list :link
  19704. (previous-single-property-change p 'face)
  19705. (next-single-property-change p 'face)) clist))
  19706. ((memq 'org-special-keyword faces)
  19707. (push (list :keyword
  19708. (previous-single-property-change p 'face)
  19709. (next-single-property-change p 'face)) clist))
  19710. ((org-at-target-p)
  19711. (push (org-point-in-group p 0 :target) clist)
  19712. (goto-char (1- (match-beginning 0)))
  19713. (if (looking-at org-radio-target-regexp)
  19714. (push (org-point-in-group p 0 :radio-target) clist))
  19715. (goto-char p))
  19716. ((setq o (car (delq nil
  19717. (mapcar
  19718. (lambda (x)
  19719. (if (memq x org-latex-fragment-image-overlays) x))
  19720. (overlays-at (point))))))
  19721. (push (list :latex-fragment
  19722. (overlay-start o) (overlay-end o)) clist)
  19723. (push (list :latex-preview
  19724. (overlay-start o) (overlay-end o)) clist))
  19725. ((org-inside-LaTeX-fragment-p)
  19726. ;; FIXME: positions wrong.
  19727. (push (list :latex-fragment (point) (point)) clist)))
  19728. (setq clist (nreverse (delq nil clist)))
  19729. clist))
  19730. ;; FIXME: Compare with at-regexp-p Do we need both?
  19731. (defun org-in-regexp (re &optional nlines visually)
  19732. "Check if point is inside a match of regexp.
  19733. Normally only the current line is checked, but you can include NLINES extra
  19734. lines both before and after point into the search.
  19735. If VISUALLY is set, require that the cursor is not after the match but
  19736. really on, so that the block visually is on the match."
  19737. (catch 'exit
  19738. (let ((pos (point))
  19739. (eol (point-at-eol (+ 1 (or nlines 0))))
  19740. (inc (if visually 1 0)))
  19741. (save-excursion
  19742. (beginning-of-line (- 1 (or nlines 0)))
  19743. (while (re-search-forward re eol t)
  19744. (if (and (<= (match-beginning 0) pos)
  19745. (>= (+ inc (match-end 0)) pos))
  19746. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19747. (defun org-at-regexp-p (regexp)
  19748. "Is point inside a match of REGEXP in the current line?"
  19749. (catch 'exit
  19750. (save-excursion
  19751. (let ((pos (point)) (end (point-at-eol)))
  19752. (beginning-of-line 1)
  19753. (while (re-search-forward regexp end t)
  19754. (if (and (<= (match-beginning 0) pos)
  19755. (>= (match-end 0) pos))
  19756. (throw 'exit t)))
  19757. nil))))
  19758. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19759. "Non-nil when point is between matches of START-RE and END-RE.
  19760. Also return a non-nil value when point is on one of the matches.
  19761. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19762. buffer positions. Default values are the positions of headlines
  19763. surrounding the point.
  19764. The functions returns a cons cell whose car (resp. cdr) is the
  19765. position before START-RE (resp. after END-RE)."
  19766. (save-match-data
  19767. (let ((pos (point))
  19768. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19769. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19770. beg end)
  19771. (save-excursion
  19772. ;; Point is on a block when on START-RE or if START-RE can be
  19773. ;; found before it...
  19774. (and (or (org-at-regexp-p start-re)
  19775. (re-search-backward start-re limit-up t))
  19776. (setq beg (match-beginning 0))
  19777. ;; ... and END-RE after it...
  19778. (goto-char (match-end 0))
  19779. (re-search-forward end-re limit-down t)
  19780. (> (setq end (match-end 0)) pos)
  19781. ;; ... without another START-RE in-between.
  19782. (goto-char (match-beginning 0))
  19783. (not (re-search-backward start-re (1+ beg) t))
  19784. ;; Return value.
  19785. (cons beg end))))))
  19786. (defun org-in-block-p (names)
  19787. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19788. NAMES is a list of strings containing names of blocks.
  19789. Return first block name matched, or nil. Beware that in case of
  19790. nested blocks, the returned name may not belong to the closest
  19791. block from point."
  19792. (save-match-data
  19793. (catch 'exit
  19794. (let ((case-fold-search t)
  19795. (lim-up (save-excursion (outline-previous-heading)))
  19796. (lim-down (save-excursion (outline-next-heading))))
  19797. (mapc (lambda (name)
  19798. (let ((n (regexp-quote name)))
  19799. (when (org-between-regexps-p
  19800. (concat "^[ \t]*#\\+begin_" n)
  19801. (concat "^[ \t]*#\\+end_" n)
  19802. lim-up lim-down)
  19803. (throw 'exit n))))
  19804. names))
  19805. nil)))
  19806. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19807. "Call `multi-occur' with buffers for all agenda files."
  19808. (interactive "sOrg-files matching: \np")
  19809. (let* ((files (org-agenda-files))
  19810. (tnames (mapcar 'file-truename files))
  19811. (extra org-agenda-text-search-extra-files)
  19812. f)
  19813. (when (eq (car extra) 'agenda-archives)
  19814. (setq extra (cdr extra))
  19815. (setq files (org-add-archive-files files)))
  19816. (while (setq f (pop extra))
  19817. (unless (member (file-truename f) tnames)
  19818. (add-to-list 'files f 'append)
  19819. (add-to-list 'tnames (file-truename f) 'append)))
  19820. (multi-occur
  19821. (mapcar (lambda (x)
  19822. (with-current-buffer
  19823. (or (get-file-buffer x) (find-file-noselect x))
  19824. (widen)
  19825. (current-buffer)))
  19826. files)
  19827. regexp)))
  19828. (if (boundp 'occur-mode-find-occurrence-hook)
  19829. ;; Emacs 23
  19830. (add-hook 'occur-mode-find-occurrence-hook
  19831. (lambda ()
  19832. (when (derived-mode-p 'org-mode)
  19833. (org-reveal))))
  19834. ;; Emacs 22
  19835. (defadvice occur-mode-goto-occurrence
  19836. (after org-occur-reveal activate)
  19837. (and (derived-mode-p 'org-mode) (org-reveal)))
  19838. (defadvice occur-mode-goto-occurrence-other-window
  19839. (after org-occur-reveal activate)
  19840. (and (derived-mode-p 'org-mode) (org-reveal)))
  19841. (defadvice occur-mode-display-occurrence
  19842. (after org-occur-reveal activate)
  19843. (when (derived-mode-p 'org-mode)
  19844. (let ((pos (occur-mode-find-occurrence)))
  19845. (with-current-buffer (marker-buffer pos)
  19846. (save-excursion
  19847. (goto-char pos)
  19848. (org-reveal)))))))
  19849. (defun org-occur-link-in-agenda-files ()
  19850. "Create a link and search for it in the agendas.
  19851. The link is not stored in `org-stored-links', it is just created
  19852. for the search purpose."
  19853. (interactive)
  19854. (let ((link (condition-case nil
  19855. (org-store-link nil)
  19856. (error "Unable to create a link to here"))))
  19857. (org-occur-in-agenda-files (regexp-quote link))))
  19858. (defun org-reverse-string (string)
  19859. "Return the reverse of STRING."
  19860. (apply 'string (reverse (string-to-list string))))
  19861. (defun org-uniquify (list)
  19862. "Remove duplicate elements from LIST."
  19863. (let (res)
  19864. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19865. res))
  19866. (defun org-uniquify-alist (alist)
  19867. "Merge duplicate elements of an alist.
  19868. For example, in this alist:
  19869. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19870. => '((a 1 3) (b 2))
  19871. merge (a 1) and (a 3) into (a 1 3) and return the new alist."
  19872. (let (rtn)
  19873. (mapc
  19874. (lambda (e)
  19875. (let (n)
  19876. (if (not (assoc (car e) rtn))
  19877. (push e rtn)
  19878. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19879. (setq rtn (assq-delete-all (car e) rtn))
  19880. (push n rtn))))
  19881. alist)
  19882. rtn))
  19883. (defun org-delete-all (elts list)
  19884. "Remove all elements in ELTS from LIST."
  19885. (while elts
  19886. (setq list (delete (pop elts) list)))
  19887. list)
  19888. (defun org-count (cl-item cl-seq)
  19889. "Count the number of occurrences of ITEM in SEQ.
  19890. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19891. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19892. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19893. (while (< cl-start cl-end)
  19894. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19895. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19896. (setq cl-start (1+ cl-start)))
  19897. cl-count))
  19898. (defun org-remove-if (predicate seq)
  19899. "Remove everything from SEQ that fulfills PREDICATE."
  19900. (let (res e)
  19901. (while seq
  19902. (setq e (pop seq))
  19903. (if (not (funcall predicate e)) (push e res)))
  19904. (nreverse res)))
  19905. (defun org-remove-if-not (predicate seq)
  19906. "Remove everything from SEQ that does not fulfill PREDICATE."
  19907. (let (res e)
  19908. (while seq
  19909. (setq e (pop seq))
  19910. (if (funcall predicate e) (push e res)))
  19911. (nreverse res)))
  19912. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19913. "Reduce two-argument FUNCTION across SEQ.
  19914. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19915. \":initial-value\" removed."
  19916. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19917. (cadr (memq :initial-value cl-keys)))
  19918. (cl-seq (pop cl-seq))
  19919. (t (funcall cl-func)))))
  19920. (while cl-seq
  19921. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19922. cl-accum))
  19923. (defun org-back-over-empty-lines ()
  19924. "Move backwards over whitespace, to the beginning of the first empty line.
  19925. Returns the number of empty lines passed."
  19926. (let ((pos (point)))
  19927. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19928. (skip-chars-backward " \t\n\r")
  19929. (unless (eobp)
  19930. (forward-line -1)))
  19931. (beginning-of-line 2)
  19932. (goto-char (min (point) pos))
  19933. (count-lines (point) pos)))
  19934. (defun org-skip-whitespace ()
  19935. (skip-chars-forward " \t\n\r"))
  19936. (defun org-point-in-group (point group &optional context)
  19937. "Check if POINT is in match-group GROUP.
  19938. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19939. match. If the match group does not exist or point is not inside it,
  19940. return nil."
  19941. (and (match-beginning group)
  19942. (>= point (match-beginning group))
  19943. (<= point (match-end group))
  19944. (if context
  19945. (list context (match-beginning group) (match-end group))
  19946. t)))
  19947. (defun org-switch-to-buffer-other-window (&rest args)
  19948. "Switch to buffer in a second window on the current frame.
  19949. In particular, do not allow pop-up frames.
  19950. Returns the newly created buffer."
  19951. (org-no-popups
  19952. (apply 'switch-to-buffer-other-window args)))
  19953. (defun org-combine-plists (&rest plists)
  19954. "Create a single property list from all plists in PLISTS.
  19955. The process starts by copying the first list, and then setting properties
  19956. from the other lists. Settings in the last list are the most significant
  19957. ones and overrule settings in the other lists."
  19958. (let ((rtn (copy-sequence (pop plists)))
  19959. p v ls)
  19960. (while plists
  19961. (setq ls (pop plists))
  19962. (while ls
  19963. (setq p (pop ls) v (pop ls))
  19964. (setq rtn (plist-put rtn p v))))
  19965. rtn))
  19966. (defun org-replace-escapes (string table)
  19967. "Replace %-escapes in STRING with values in TABLE.
  19968. TABLE is an association list with keys like \"%a\" and string values.
  19969. The sequences in STRING may contain normal field width and padding information,
  19970. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19971. so values can contain further %-escapes if they are define later in TABLE."
  19972. (let ((tbl (copy-alist table))
  19973. (case-fold-search nil)
  19974. (pchg 0)
  19975. e re rpl)
  19976. (while (setq e (pop tbl))
  19977. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19978. (when (and (cdr e) (string-match re (cdr e)))
  19979. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19980. (safe "SREF"))
  19981. (add-text-properties 0 3 (list 'sref sref) safe)
  19982. (setcdr e (replace-match safe t t (cdr e)))))
  19983. (while (string-match re string)
  19984. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19985. (cdr e)))
  19986. (setq string (replace-match rpl t t string))))
  19987. (while (setq pchg (next-property-change pchg string))
  19988. (let ((sref (get-text-property pchg 'sref string)))
  19989. (when (and sref (string-match "SREF" string pchg))
  19990. (setq string (replace-match sref t t string)))))
  19991. string))
  19992. (defun org-sublist (list start end)
  19993. "Return a section of LIST, from START to END.
  19994. Counting starts at 1."
  19995. (let (rtn (c start))
  19996. (setq list (nthcdr (1- start) list))
  19997. (while (and list (<= c end))
  19998. (push (pop list) rtn)
  19999. (setq c (1+ c)))
  20000. (nreverse rtn)))
  20001. (defun org-find-base-buffer-visiting (file)
  20002. "Like `find-buffer-visiting' but always return the base buffer and
  20003. not an indirect buffer."
  20004. (let ((buf (or (get-file-buffer file)
  20005. (find-buffer-visiting file))))
  20006. (if buf
  20007. (or (buffer-base-buffer buf) buf)
  20008. nil)))
  20009. (defun org-image-file-name-regexp (&optional extensions)
  20010. "Return regexp matching the file names of images.
  20011. If EXTENSIONS is given, only match these."
  20012. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20013. (image-file-name-regexp)
  20014. (let ((image-file-name-extensions
  20015. (or extensions
  20016. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20017. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20018. (concat "\\."
  20019. (regexp-opt (nconc (mapcar 'upcase
  20020. image-file-name-extensions)
  20021. image-file-name-extensions)
  20022. t)
  20023. "\\'"))))
  20024. (defun org-file-image-p (file &optional extensions)
  20025. "Return non-nil if FILE is an image."
  20026. (save-match-data
  20027. (string-match (org-image-file-name-regexp extensions) file)))
  20028. (defun org-get-cursor-date (&optional with-time)
  20029. "Return the date at cursor in as a time.
  20030. This works in the calendar and in the agenda, anywhere else it just
  20031. returns the current time.
  20032. If WITH-TIME is non-nil, returns the time of the event at point (in
  20033. the agenda) or the current time of the day."
  20034. (let (date day defd tp tm hod mod)
  20035. (when with-time
  20036. (setq tp (get-text-property (point) 'time))
  20037. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20038. (setq hod (string-to-number (match-string 1 tp))
  20039. mod (string-to-number (match-string 2 tp))))
  20040. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20041. mod (nth 1 (decode-time (current-time))))))
  20042. (cond
  20043. ((eq major-mode 'calendar-mode)
  20044. (setq date (calendar-cursor-to-date)
  20045. defd (encode-time 0 (or mod 0) (or hod 0)
  20046. (nth 1 date) (nth 0 date) (nth 2 date))))
  20047. ((eq major-mode 'org-agenda-mode)
  20048. (setq day (get-text-property (point) 'day))
  20049. (if day
  20050. (setq date (calendar-gregorian-from-absolute day)
  20051. defd (encode-time 0 (or mod 0) (or hod 0)
  20052. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20053. (or defd (current-time))))
  20054. (defun org-mark-subtree (&optional up)
  20055. "Mark the current subtree.
  20056. This puts point at the start of the current subtree, and mark at
  20057. the end. If a numeric prefix UP is given, move up into the
  20058. hierarchy of headlines by UP levels before marking the subtree."
  20059. (interactive "P")
  20060. (org-with-limited-levels
  20061. (cond ((org-at-heading-p) (beginning-of-line))
  20062. ((org-before-first-heading-p) (error "Not in a subtree"))
  20063. (t (outline-previous-visible-heading 1))))
  20064. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20065. (if (org-called-interactively-p 'any)
  20066. (call-interactively 'org-mark-element)
  20067. (org-mark-element)))
  20068. ;;; Indentation
  20069. (defun org-indent-line ()
  20070. "Indent line depending on context."
  20071. (interactive)
  20072. (let* ((pos (point))
  20073. (itemp (org-at-item-p))
  20074. (case-fold-search t)
  20075. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20076. (inline-task-p (and (featurep 'org-inlinetask)
  20077. (org-inlinetask-in-task-p)))
  20078. (inline-re (and inline-task-p
  20079. (org-inlinetask-outline-regexp)))
  20080. column)
  20081. (if (and orgstruct-is-++ (eq pos (point)))
  20082. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20083. (indent-according-to-mode))
  20084. (beginning-of-line 1)
  20085. (cond
  20086. ;; Headings
  20087. ((looking-at org-outline-regexp) (setq column 0))
  20088. ;; Footnote definition
  20089. ((looking-at org-footnote-definition-re) (setq column 0))
  20090. ;; Literal examples
  20091. ((looking-at "[ \t]*:\\( \\|$\\)")
  20092. (setq column (org-get-indentation))) ; do nothing
  20093. ;; Lists
  20094. ((ignore-errors (goto-char (org-in-item-p)))
  20095. (setq column (if itemp
  20096. (org-get-indentation)
  20097. (org-list-item-body-column (point))))
  20098. (goto-char pos))
  20099. ;; Drawers
  20100. ((and (looking-at "[ \t]*:END:")
  20101. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20102. (save-excursion
  20103. (goto-char (1- (match-beginning 1)))
  20104. (setq column (current-column))))
  20105. ;; Special blocks
  20106. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20107. (save-excursion
  20108. (re-search-backward
  20109. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20110. (setq column (org-get-indentation (match-string 0))))
  20111. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20112. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20113. (save-excursion
  20114. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20115. (setq column
  20116. (cond ((equal (downcase (match-string 1)) "src")
  20117. ;; src blocks: let `org-edit-src-exit' handle them
  20118. (org-get-indentation))
  20119. ((equal (downcase (match-string 1)) "example")
  20120. (max (org-get-indentation)
  20121. (org-get-indentation (match-string 0))))
  20122. (t
  20123. (org-get-indentation (match-string 0))))))
  20124. ;; This line has nothing special, look at the previous relevant
  20125. ;; line to compute indentation
  20126. (t
  20127. (beginning-of-line 0)
  20128. (while (and (not (bobp))
  20129. (not (looking-at org-table-line-regexp))
  20130. (not (looking-at org-drawer-regexp))
  20131. ;; When point started in an inline task, do not move
  20132. ;; above task starting line.
  20133. (not (and inline-task-p (looking-at inline-re)))
  20134. ;; Skip drawers, blocks, empty lines, verbatim,
  20135. ;; comments, tables, footnotes definitions, lists,
  20136. ;; inline tasks.
  20137. (or (and (looking-at "[ \t]*:END:")
  20138. (re-search-backward org-drawer-regexp nil t))
  20139. (and (looking-at "[ \t]*#\\+end_")
  20140. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20141. (looking-at "[ \t]*[\n:#|]")
  20142. (looking-at org-footnote-definition-re)
  20143. (and (not inline-task-p)
  20144. (featurep 'org-inlinetask)
  20145. (org-inlinetask-in-task-p)
  20146. (or (org-inlinetask-goto-beginning) t))))
  20147. (beginning-of-line 0))
  20148. (cond
  20149. ;; There was a list item above.
  20150. ((save-excursion
  20151. (and (ignore-errors (goto-char (org-in-item-p)))
  20152. (goto-char
  20153. (org-list-get-top-point (org-list-struct)))))
  20154. (looking-at org-list-full-item-re)
  20155. (setq column (length (match-string 0))))
  20156. ;; There was an heading above.
  20157. ((looking-at "\\*+[ \t]+")
  20158. (if (not org-adapt-indentation)
  20159. (setq column 0)
  20160. (goto-char (match-end 0))
  20161. (setq column (current-column))))
  20162. ;; A drawer had started and is unfinished
  20163. ((looking-at org-drawer-regexp)
  20164. (goto-char (1- (match-beginning 1)))
  20165. (setq column (current-column)))
  20166. ;; Else, nothing noticeable found: get indentation and go on.
  20167. (t (setq column (org-get-indentation))))))
  20168. ;; Now apply indentation and move cursor accordingly
  20169. (goto-char pos)
  20170. (if (<= (current-column) (current-indentation))
  20171. (org-indent-line-to column)
  20172. (save-excursion (org-indent-line-to column)))
  20173. ;; Special polishing for properties, see `org-property-format'
  20174. (setq column (current-column))
  20175. (beginning-of-line 1)
  20176. (if (looking-at
  20177. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20178. (replace-match (concat (match-string 1)
  20179. (format org-property-format
  20180. (match-string 2) (match-string 3)))
  20181. t t))
  20182. (org-move-to-column column))))
  20183. (defun org-indent-drawer ()
  20184. "Indent the drawer at point."
  20185. (interactive)
  20186. (let ((p (point))
  20187. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20188. (match-end 0)))
  20189. (folded
  20190. (save-excursion
  20191. (end-of-line)
  20192. (when (overlays-at (point))
  20193. (member 'invisible (overlay-properties
  20194. (car (overlays-at (point)))))))))
  20195. (when folded (org-cycle))
  20196. (indent-for-tab-command)
  20197. (while (and (move-beginning-of-line 2) (< (point) e))
  20198. (indent-for-tab-command))
  20199. (goto-char p)
  20200. (when folded (org-cycle)))
  20201. (message "Drawer at point indented"))
  20202. (defun org-indent-block ()
  20203. "Indent the block at point."
  20204. (interactive)
  20205. (let ((p (point))
  20206. (case-fold-search t)
  20207. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20208. (match-end 0)))
  20209. (folded
  20210. (save-excursion
  20211. (end-of-line)
  20212. (when (overlays-at (point))
  20213. (member 'invisible (overlay-properties
  20214. (car (overlays-at (point)))))))))
  20215. (when folded (org-cycle))
  20216. (indent-for-tab-command)
  20217. (while (and (move-beginning-of-line 2) (< (point) e))
  20218. (indent-for-tab-command))
  20219. (goto-char p)
  20220. (when folded (org-cycle)))
  20221. (message "Block at point indented"))
  20222. (defun org-indent-region (start end)
  20223. "Indent region."
  20224. (interactive "r")
  20225. (save-excursion
  20226. (let ((line-end (org-current-line end)))
  20227. (goto-char start)
  20228. (while (< (org-current-line) line-end)
  20229. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20230. (t (call-interactively 'org-indent-line)))
  20231. (move-beginning-of-line 2)))))
  20232. ;;; Filling
  20233. ;; We use our own fill-paragraph and auto-fill functions.
  20234. ;; `org-fill-paragraph' relies on adaptive filling and context
  20235. ;; checking. Appropriate `fill-prefix' is computed with
  20236. ;; `org-adaptive-fill-function'.
  20237. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20238. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20239. ;; `org-adaptive-fill-function' value. Internally,
  20240. ;; `org-comment-line-break-function' breaks the line.
  20241. ;; `org-setup-filling' installs filling and auto-filling related
  20242. ;; variables during `org-mode' initialization.
  20243. (defun org-setup-filling ()
  20244. (interactive)
  20245. ;; Prevent auto-fill from inserting unwanted new items.
  20246. (when (boundp 'fill-nobreak-predicate)
  20247. (org-set-local
  20248. 'fill-nobreak-predicate
  20249. (org-uniquify
  20250. (append fill-nobreak-predicate
  20251. '(org-fill-paragraph-separate-nobreak-p
  20252. org-fill-line-break-nobreak-p
  20253. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20254. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20255. (org-set-local 'auto-fill-inhibit-regexp nil)
  20256. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20257. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20258. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20259. (defvar org-element-paragraph-separate) ; org-element.el
  20260. (defun org-fill-paragraph-separate-nobreak-p ()
  20261. "Non-nil when a line break at point would insert a new item."
  20262. (looking-at (substring org-element-paragraph-separate 1)))
  20263. (defun org-fill-line-break-nobreak-p ()
  20264. "Non-nil when a line break at point would create an Org line break."
  20265. (save-excursion
  20266. (skip-chars-backward "[ \t]")
  20267. (skip-chars-backward "\\\\")
  20268. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20269. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20270. "Non-nil when a line break at point would insert a new item."
  20271. (and (org-at-timestamp-p t)
  20272. (not (looking-at org-ts-regexp-both))))
  20273. (declare-function message-in-body-p "message" ())
  20274. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20275. (defun org-adaptive-fill-function ()
  20276. "Compute a fill prefix for the current line.
  20277. Return fill prefix, as a string, or nil if current line isn't
  20278. meant to be filled."
  20279. (let (prefix)
  20280. (catch 'exit
  20281. (when (derived-mode-p 'message-mode)
  20282. (save-excursion
  20283. (beginning-of-line)
  20284. (cond ((or (not (message-in-body-p))
  20285. (looking-at orgtbl-line-start-regexp))
  20286. (throw 'exit nil))
  20287. ((looking-at message-cite-prefix-regexp)
  20288. (throw 'exit (match-string-no-properties 0)))
  20289. ((looking-at org-outline-regexp)
  20290. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20291. (org-with-wide-buffer
  20292. (let* ((p (line-beginning-position))
  20293. (element (save-excursion (beginning-of-line)
  20294. (org-element-at-point)))
  20295. (type (org-element-type element))
  20296. (post-affiliated (org-element-property :post-affiliated element)))
  20297. (unless (and post-affiliated (< p post-affiliated))
  20298. (case type
  20299. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20300. (footnote-definition "")
  20301. ((item plain-list)
  20302. (make-string (org-list-item-body-column
  20303. (or post-affiliated
  20304. (org-element-property :begin element)))
  20305. ? ))
  20306. (paragraph
  20307. ;; Fill prefix is usually the same as the current line,
  20308. ;; except if the paragraph is at the beginning of an item.
  20309. (let ((parent (org-element-property :parent element)))
  20310. (cond ((eq (org-element-type parent) 'item)
  20311. (make-string (org-list-item-body-column
  20312. (org-element-property :begin parent))
  20313. ? ))
  20314. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20315. (match-string 0))
  20316. (t ""))))
  20317. (comment-block
  20318. ;; Only fill contents if P is within block boundaries.
  20319. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20320. (forward-line)
  20321. (point)))
  20322. (cend (save-excursion
  20323. (goto-char (org-element-property :end element))
  20324. (skip-chars-backward " \r\t\n")
  20325. (line-beginning-position))))
  20326. (when (and (>= p cbeg) (< p cend))
  20327. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20328. (match-string 0)
  20329. "")))))))))))
  20330. (declare-function message-goto-body "message" ())
  20331. (defvar message-cite-prefix-regexp) ; From message.el
  20332. (defun org-fill-paragraph (&optional justify)
  20333. "Fill element at point, when applicable.
  20334. This function only applies to comment blocks, comments, example
  20335. blocks and paragraphs. Also, as a special case, re-align table
  20336. when point is at one.
  20337. If JUSTIFY is non-nil (interactively, with prefix argument),
  20338. justify as well. If `sentence-end-double-space' is non-nil, then
  20339. period followed by one space does not end a sentence, so don't
  20340. break a line there. The variable `fill-column' controls the
  20341. width for filling.
  20342. For convenience, when point is at a plain list, an item or
  20343. a footnote definition, try to fill the first paragraph within."
  20344. (interactive)
  20345. (if (and (derived-mode-p 'message-mode)
  20346. (or (not (message-in-body-p))
  20347. (save-excursion (move-beginning-of-line 1)
  20348. (looking-at message-cite-prefix-regexp))))
  20349. ;; First ensure filling is correct in message-mode.
  20350. (let ((fill-paragraph-function
  20351. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20352. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20353. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20354. (paragraph-separate
  20355. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20356. (fill-paragraph nil))
  20357. (with-syntax-table org-mode-transpose-word-syntax-table
  20358. ;; Move to end of line in order to get the first paragraph
  20359. ;; within a plain list or a footnote definition.
  20360. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20361. ;; First check if point is in a blank line at the beginning of
  20362. ;; the buffer. In that case, ignore filling.
  20363. (case (org-element-type element)
  20364. ;; Use major mode filling function is src blocks.
  20365. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20366. ;; Align Org tables, leave table.el tables as-is.
  20367. (table-row (org-table-align) t)
  20368. (table
  20369. (when (eq (org-element-property :type element) 'org)
  20370. (org-table-align))
  20371. t)
  20372. (paragraph
  20373. ;; Paragraphs may contain `line-break' type objects.
  20374. (let ((beg (max (point-min)
  20375. (org-element-property :contents-begin element)))
  20376. (end (min (point-max)
  20377. (org-element-property :contents-end element))))
  20378. ;; Do nothing if point is at an affiliated keyword.
  20379. (if (< (line-end-position) beg) t
  20380. (when (derived-mode-p 'message-mode)
  20381. ;; In `message-mode', do not fill following citation
  20382. ;; in current paragraph nor text before message body.
  20383. (let ((body-start (save-excursion (message-goto-body))))
  20384. (when body-start (setq beg (max body-start beg))))
  20385. (when (save-excursion
  20386. (re-search-forward
  20387. (concat "^" message-cite-prefix-regexp) end t))
  20388. (setq end (match-beginning 0))))
  20389. ;; Fill paragraph, taking line breaks into account.
  20390. ;; For that, slice the paragraph using line breaks as
  20391. ;; separators, and fill the parts in reverse order to
  20392. ;; avoid messing with markers.
  20393. (save-excursion
  20394. (goto-char end)
  20395. (mapc
  20396. (lambda (pos)
  20397. (fill-region-as-paragraph pos (point) justify)
  20398. (goto-char pos))
  20399. ;; Find the list of ending positions for line breaks
  20400. ;; in the current paragraph. Add paragraph
  20401. ;; beginning to include first slice.
  20402. (nreverse
  20403. (cons beg
  20404. (org-element-map
  20405. (org-element--parse-objects
  20406. beg end nil (org-element-restriction 'paragraph))
  20407. 'line-break
  20408. (lambda (lb) (org-element-property :end lb)))))))
  20409. t)))
  20410. ;; Contents of `comment-block' type elements should be
  20411. ;; filled as plain text, but only if point is within block
  20412. ;; markers.
  20413. (comment-block
  20414. (let* ((case-fold-search t)
  20415. (beg (save-excursion
  20416. (goto-char (org-element-property :begin element))
  20417. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20418. (forward-line)
  20419. (point)))
  20420. (end (save-excursion
  20421. (goto-char (org-element-property :end element))
  20422. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20423. (line-beginning-position))))
  20424. (when (and (>= (point) beg) (< (point) end))
  20425. (fill-region-as-paragraph
  20426. (save-excursion
  20427. (end-of-line)
  20428. (re-search-backward "^[ \t]*$" beg 'move)
  20429. (line-beginning-position))
  20430. (save-excursion
  20431. (beginning-of-line)
  20432. (re-search-forward "^[ \t]*$" end 'move)
  20433. (line-beginning-position))
  20434. justify)))
  20435. t)
  20436. ;; Fill comments.
  20437. (comment (fill-comment-paragraph justify))
  20438. ;; Ignore every other element.
  20439. (otherwise t))))))
  20440. (defun org-auto-fill-function ()
  20441. "Auto-fill function."
  20442. ;; Check if auto-filling is meaningful.
  20443. (let ((fc (current-fill-column)))
  20444. (when (and fc (> (current-column) fc))
  20445. (let* ((fill-prefix (org-adaptive-fill-function))
  20446. ;; Enforce empty fill prefix, if required. Otherwise, it
  20447. ;; will be computed again.
  20448. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20449. (when fill-prefix (do-auto-fill))))))
  20450. (defun org-comment-line-break-function (&optional soft)
  20451. "Break line at point and indent, continuing comment if within one.
  20452. The inserted newline is marked hard if variable
  20453. `use-hard-newlines' is true, unless optional argument SOFT is
  20454. non-nil."
  20455. (if soft (insert-and-inherit ?\n) (newline 1))
  20456. (save-excursion (forward-char -1) (delete-horizontal-space))
  20457. (delete-horizontal-space)
  20458. (indent-to-left-margin)
  20459. (insert-before-markers-and-inherit fill-prefix))
  20460. ;;; Comments
  20461. ;; Org comments syntax is quite complex. It requires the entire line
  20462. ;; to be just a comment. Also, even with the right syntax at the
  20463. ;; beginning of line, some some elements (i.e. verse-block or
  20464. ;; example-block) don't accept comments. Usual Emacs comment commands
  20465. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20466. ;; Org still relies on `comment-dwim', but cannot trust
  20467. ;; `comment-only-p'. So, `comment-region-function' and
  20468. ;; `uncomment-region-function' both point
  20469. ;; to`org-comment-or-uncomment-region'. Eventually,
  20470. ;; `org-insert-comment' takes care of insertion of comments at the
  20471. ;; beginning of line.
  20472. ;; `org-setup-comments-handling' install comments related variables
  20473. ;; during `org-mode' initialization.
  20474. (defun org-setup-comments-handling ()
  20475. (interactive)
  20476. (org-set-local 'comment-use-syntax nil)
  20477. (org-set-local 'comment-start "# ")
  20478. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20479. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20480. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20481. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20482. (defun org-insert-comment ()
  20483. "Insert an empty comment above current line.
  20484. If the line is empty, insert comment at its beginning."
  20485. (beginning-of-line)
  20486. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20487. (org-indent-line)
  20488. (insert "# "))
  20489. (defvar comment-empty-lines) ; From newcomment.el.
  20490. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20491. "Comment or uncomment each non-blank line in the region.
  20492. Uncomment each non-blank line between BEG and END if it only
  20493. contains commented lines. Otherwise, comment them."
  20494. (save-restriction
  20495. ;; Restrict region
  20496. (narrow-to-region (save-excursion (goto-char beg)
  20497. (skip-chars-forward " \r\t\n" end)
  20498. (line-beginning-position))
  20499. (save-excursion (goto-char end)
  20500. (skip-chars-backward " \r\t\n" beg)
  20501. (line-end-position)))
  20502. (let ((uncommentp
  20503. ;; UNCOMMENTP is non-nil when every non blank line between
  20504. ;; BEG and END is a comment.
  20505. (save-excursion
  20506. (goto-char (point-min))
  20507. (while (and (not (eobp))
  20508. (let ((element (org-element-at-point)))
  20509. (and (eq (org-element-type element) 'comment)
  20510. (goto-char (min (point-max)
  20511. (org-element-property
  20512. :end element)))))))
  20513. (eobp))))
  20514. (if uncommentp
  20515. ;; Only blank lines and comments in region: uncomment it.
  20516. (save-excursion
  20517. (goto-char (point-min))
  20518. (while (not (eobp))
  20519. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20520. (replace-match "" nil nil nil 1))
  20521. (forward-line)))
  20522. ;; Comment each line in region.
  20523. (let ((min-indent (point-max)))
  20524. ;; First find the minimum indentation across all lines.
  20525. (save-excursion
  20526. (goto-char (point-min))
  20527. (while (and (not (eobp)) (not (zerop min-indent)))
  20528. (unless (looking-at "[ \t]*$")
  20529. (setq min-indent (min min-indent (current-indentation))))
  20530. (forward-line)))
  20531. ;; Then loop over all lines.
  20532. (save-excursion
  20533. (goto-char (point-min))
  20534. (while (not (eobp))
  20535. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20536. ;; Don't get fooled by invisible text (e.g. link path)
  20537. ;; when moving to column MIN-INDENT.
  20538. (let ((buffer-invisibility-spec nil))
  20539. (org-move-to-column min-indent t))
  20540. (insert comment-start))
  20541. (forward-line))))))))
  20542. ;;; Planning
  20543. ;; This section contains tools to operate on timestamp objects, as
  20544. ;; returned by, e.g. `org-element-context'.
  20545. (defun org-timestamp-has-time-p (timestamp)
  20546. "Non-nil when TIMESTAMP has a time specified."
  20547. (org-element-property :hour-start timestamp))
  20548. (defun org-timestamp-format (timestamp format &optional end utc)
  20549. "Format a TIMESTAMP element into a string.
  20550. FORMAT is a format specifier to be passed to
  20551. `format-time-string'.
  20552. When optional argument END is non-nil, use end of date-range or
  20553. time-range, if possible.
  20554. When optional argument UTC is non-nil, time will be expressed as
  20555. Universal Time."
  20556. (format-time-string
  20557. format
  20558. (apply 'encode-time
  20559. (cons 0
  20560. (mapcar
  20561. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20562. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20563. '(:minute-start :hour-start :day-start :month-start
  20564. :year-start)))))
  20565. utc))
  20566. (defun org-timestamp-split-range (timestamp &optional end)
  20567. "Extract a timestamp object from a date or time range.
  20568. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20569. the end of the range. Otherwise, extract its start.
  20570. Return a new timestamp object sharing the same parent as
  20571. TIMESTAMP."
  20572. (let ((type (org-element-property :type timestamp)))
  20573. (if (memq type '(active inactive diary)) timestamp
  20574. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20575. ;; Set new type.
  20576. (org-element-put-property
  20577. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20578. ;; Copy start properties over end properties if END is
  20579. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20580. (let ((p-alist '((:minute-start . :minute-end)
  20581. (:hour-start . :hour-end)
  20582. (:day-start . :day-end)
  20583. (:month-start . :month-end)
  20584. (:year-start . :year-end))))
  20585. (dolist (p-cell p-alist)
  20586. (org-element-put-property
  20587. split-ts
  20588. (funcall (if end 'car 'cdr) p-cell)
  20589. (org-element-property
  20590. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20591. ;; Eventually refresh `:raw-value'.
  20592. (org-element-put-property split-ts :raw-value nil)
  20593. (org-element-put-property
  20594. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20595. (defun org-timestamp-translate (timestamp &optional boundary)
  20596. "Apply `org-translate-time' on a TIMESTAMP object.
  20597. When optional argument BOUNDARY is non-nil, it is either the
  20598. symbol `start' or `end'. In this case, only translate the
  20599. starting or ending part of TIMESTAMP if it is a date or time
  20600. range. Otherwise, translate both parts."
  20601. (if (and (not boundary)
  20602. (memq (org-element-property :type timestamp)
  20603. '(active-range inactive-range)))
  20604. (concat
  20605. (org-translate-time
  20606. (org-element-property :raw-value
  20607. (org-timestamp-split-range timestamp)))
  20608. "--"
  20609. (org-translate-time
  20610. (org-element-property :raw-value
  20611. (org-timestamp-split-range timestamp t))))
  20612. (org-translate-time
  20613. (org-element-property
  20614. :raw-value
  20615. (if (not boundary) timestamp
  20616. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20617. ;;; Other stuff.
  20618. (defun org-toggle-fixed-width-section (arg)
  20619. "Toggle the fixed-width export.
  20620. If there is no active region, the QUOTE keyword at the current headline is
  20621. inserted or removed. When present, it causes the text between this headline
  20622. and the next to be exported as fixed-width text, and unmodified.
  20623. If there is an active region, this command adds or removes a colon as the
  20624. first character of this line. If the first character of a line is a colon,
  20625. this line is also exported in fixed-width font."
  20626. (interactive "P")
  20627. (let* ((cc 0)
  20628. (regionp (org-region-active-p))
  20629. (beg (if regionp (region-beginning) (point)))
  20630. (end (if regionp (region-end)))
  20631. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20632. (case-fold-search nil)
  20633. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20634. off)
  20635. (if regionp
  20636. (save-excursion
  20637. (goto-char beg)
  20638. (setq cc (current-column))
  20639. (beginning-of-line 1)
  20640. (setq off (looking-at re))
  20641. (while (> nlines 0)
  20642. (setq nlines (1- nlines))
  20643. (beginning-of-line 1)
  20644. (cond
  20645. (arg
  20646. (org-move-to-column cc t)
  20647. (insert ": \n")
  20648. (forward-line -1))
  20649. ((and off (looking-at re))
  20650. (replace-match "" t t nil 1))
  20651. ((not off) (org-move-to-column cc t) (insert ": ")))
  20652. (forward-line 1)))
  20653. (save-excursion
  20654. (org-back-to-heading)
  20655. (cond
  20656. ((looking-at (format org-heading-keyword-regexp-format
  20657. org-quote-string))
  20658. (goto-char (match-end 1))
  20659. (looking-at (concat " +" org-quote-string))
  20660. (replace-match "" t t)
  20661. (when (eolp) (insert " ")))
  20662. ((looking-at org-outline-regexp)
  20663. (goto-char (match-end 0))
  20664. (insert org-quote-string " ")))))))
  20665. (defun org-reftex-citation ()
  20666. "Use reftex-citation to insert a citation into the buffer.
  20667. This looks for a line like
  20668. #+BIBLIOGRAPHY: foo plain option:-d
  20669. and derives from it that foo.bib is the bibliography file relevant
  20670. for this document. It then installs the necessary environment for RefTeX
  20671. to work in this buffer and calls `reftex-citation' to insert a citation
  20672. into the buffer.
  20673. Export of such citations to both LaTeX and HTML is handled by the contributed
  20674. package org-exp-bibtex by Taru Karttunen."
  20675. (interactive)
  20676. (let ((reftex-docstruct-symbol 'rds)
  20677. (reftex-cite-format "\\cite{%l}")
  20678. rds bib)
  20679. (save-excursion
  20680. (save-restriction
  20681. (widen)
  20682. (let ((case-fold-search t)
  20683. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20684. (if (not (save-excursion
  20685. (or (re-search-forward re nil t)
  20686. (re-search-backward re nil t))))
  20687. (error "No bibliography defined in file")
  20688. (setq bib (concat (match-string 1) ".bib")
  20689. rds (list (list 'bib bib)))))))
  20690. (call-interactively 'reftex-citation)))
  20691. ;;;; Functions extending outline functionality
  20692. (defun org-beginning-of-line (&optional arg)
  20693. "Go to the beginning of the current line. If that is invisible, continue
  20694. to a visible line beginning. This makes the function of C-a more intuitive.
  20695. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20696. first attempt, and only move to after the tags when the cursor is already
  20697. beyond the end of the headline."
  20698. (interactive "P")
  20699. (let ((pos (point))
  20700. (special (if (consp org-special-ctrl-a/e)
  20701. (car org-special-ctrl-a/e)
  20702. org-special-ctrl-a/e))
  20703. refpos)
  20704. (if (org-bound-and-true-p visual-line-mode)
  20705. (beginning-of-visual-line 1)
  20706. (beginning-of-line 1))
  20707. (if (and arg (fboundp 'move-beginning-of-line))
  20708. (call-interactively 'move-beginning-of-line)
  20709. (if (bobp)
  20710. nil
  20711. (backward-char 1)
  20712. (if (org-truely-invisible-p)
  20713. (while (and (not (bobp)) (org-truely-invisible-p))
  20714. (backward-char 1)
  20715. (beginning-of-line 1))
  20716. (forward-char 1))))
  20717. (when special
  20718. (cond
  20719. ((and (looking-at org-complex-heading-regexp)
  20720. (= (char-after (match-end 1)) ?\ ))
  20721. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20722. (point-at-eol)))
  20723. (goto-char
  20724. (if (eq special t)
  20725. (cond ((> pos refpos) refpos)
  20726. ((= pos (point)) refpos)
  20727. (t (point)))
  20728. (cond ((> pos (point)) (point))
  20729. ((not (eq last-command this-command)) (point))
  20730. (t refpos)))))
  20731. ((org-at-item-p)
  20732. ;; Being at an item and not looking at an the item means point
  20733. ;; was previously moved to beginning of a visual line, which
  20734. ;; doesn't contain the item. Therefore, do nothing special,
  20735. ;; just stay here.
  20736. (when (looking-at org-list-full-item-re)
  20737. ;; Set special position at first white space character after
  20738. ;; bullet, and check-box, if any.
  20739. (let ((after-bullet
  20740. (let ((box (match-end 3)))
  20741. (if (not box) (match-end 1)
  20742. (let ((after (char-after box)))
  20743. (if (and after (= after ? )) (1+ box) box))))))
  20744. ;; Special case: Move point to special position when
  20745. ;; currently after it or at beginning of line.
  20746. (if (eq special t)
  20747. (when (or (> pos after-bullet) (= (point) pos))
  20748. (goto-char after-bullet))
  20749. ;; Reversed case: Move point to special position when
  20750. ;; point was already at beginning of line and command is
  20751. ;; repeated.
  20752. (when (and (= (point) pos) (eq last-command this-command))
  20753. (goto-char after-bullet))))))))
  20754. (org-no-warnings
  20755. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20756. (defun org-end-of-line (&optional arg)
  20757. "Go to the end of the line.
  20758. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20759. tags on the first attempt, and only move to after the tags when
  20760. the cursor is already beyond the end of the headline."
  20761. (interactive "P")
  20762. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20763. org-special-ctrl-a/e))
  20764. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20765. 'end-of-visual-line)
  20766. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20767. (t 'end-of-line))))
  20768. (if (or (not special) arg) (call-interactively move-fun)
  20769. (let* ((element (save-excursion (beginning-of-line)
  20770. (org-element-at-point)))
  20771. (type (org-element-type element)))
  20772. (cond
  20773. ((memq type '(headline inlinetask))
  20774. (let ((pos (point)))
  20775. (beginning-of-line 1)
  20776. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20777. (if (eq special t)
  20778. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20779. (goto-char (match-beginning 1))
  20780. (goto-char (match-end 0)))
  20781. (if (or (< pos (match-end 0))
  20782. (not (eq this-command last-command)))
  20783. (goto-char (match-end 0))
  20784. (goto-char (match-beginning 1))))
  20785. (call-interactively move-fun))))
  20786. ((org-element-property :hiddenp element)
  20787. ;; If element is hidden, `move-end-of-line' would put point
  20788. ;; after it. Use `end-of-line' to stay on current line.
  20789. (call-interactively 'end-of-line))
  20790. (t (call-interactively move-fun)))))
  20791. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20792. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20793. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20794. (defun org-backward-sentence (&optional arg)
  20795. "Go to beginning of sentence, or beginning of table field.
  20796. This will call `backward-sentence' or `org-table-beginning-of-field',
  20797. depending on context."
  20798. (interactive "P")
  20799. (cond
  20800. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20801. (t (call-interactively 'backward-sentence))))
  20802. (defun org-forward-sentence (&optional arg)
  20803. "Go to end of sentence, or end of table field.
  20804. This will call `forward-sentence' or `org-table-end-of-field',
  20805. depending on context."
  20806. (interactive "P")
  20807. (cond
  20808. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20809. (t (call-interactively 'forward-sentence))))
  20810. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20811. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20812. (defun org-kill-line (&optional arg)
  20813. "Kill line, to tags or end of line."
  20814. (interactive "P")
  20815. (cond
  20816. ((or (not org-special-ctrl-k)
  20817. (bolp)
  20818. (not (org-at-heading-p)))
  20819. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20820. org-ctrl-k-protect-subtree)
  20821. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20822. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20823. (error "C-k aborted - would kill hidden subtree")))
  20824. (call-interactively
  20825. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20826. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20827. (kill-region (point) (match-beginning 1))
  20828. (org-set-tags nil t))
  20829. (t (kill-region (point) (point-at-eol)))))
  20830. (define-key org-mode-map "\C-k" 'org-kill-line)
  20831. (defun org-yank (&optional arg)
  20832. "Yank. If the kill is a subtree, treat it specially.
  20833. This command will look at the current kill and check if is a single
  20834. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20835. cursor is at the beginning of a line or after the stars of a currently
  20836. empty headline, then the yank is handled specially. How exactly depends
  20837. on the value of the following variables, both set by default.
  20838. org-yank-folded-subtrees
  20839. When set, the subtree(s) will be folded after insertion, but only
  20840. if doing so would now swallow text after the yanked text.
  20841. org-yank-adjusted-subtrees
  20842. When set, the subtree will be promoted or demoted in order to
  20843. fit into the local outline tree structure, which means that the level
  20844. will be adjusted so that it becomes the smaller one of the two
  20845. *visible* surrounding headings.
  20846. Any prefix to this command will cause `yank' to be called directly with
  20847. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20848. will just
  20849. plainly yank the text as it is.
  20850. \[1] The test checks if the first non-white line is a heading
  20851. and if there are no other headings with fewer stars."
  20852. (interactive "P")
  20853. (org-yank-generic 'yank arg))
  20854. (defun org-yank-generic (command arg)
  20855. "Perform some yank-like command.
  20856. This function implements the behavior described in the `org-yank'
  20857. documentation. However, it has been generalized to work for any
  20858. interactive command with similar behavior."
  20859. ;; pretend to be command COMMAND
  20860. (setq this-command command)
  20861. (if arg
  20862. (call-interactively command)
  20863. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20864. (and (org-kill-is-subtree-p)
  20865. (or (bolp)
  20866. (and (looking-at "[ \t]*$")
  20867. (string-match
  20868. "\\`\\*+\\'"
  20869. (buffer-substring (point-at-bol) (point)))))))
  20870. swallowp)
  20871. (cond
  20872. ((and subtreep org-yank-folded-subtrees)
  20873. (let ((beg (point))
  20874. end)
  20875. (if (and subtreep org-yank-adjusted-subtrees)
  20876. (org-paste-subtree nil nil 'for-yank)
  20877. (call-interactively command))
  20878. (setq end (point))
  20879. (goto-char beg)
  20880. (when (and (bolp) subtreep
  20881. (not (setq swallowp
  20882. (org-yank-folding-would-swallow-text beg end))))
  20883. (org-with-limited-levels
  20884. (or (looking-at org-outline-regexp)
  20885. (re-search-forward org-outline-regexp-bol end t))
  20886. (while (and (< (point) end) (looking-at org-outline-regexp))
  20887. (hide-subtree)
  20888. (org-cycle-show-empty-lines 'folded)
  20889. (condition-case nil
  20890. (outline-forward-same-level 1)
  20891. (error (goto-char end))))))
  20892. (when swallowp
  20893. (message
  20894. "Inserted text not folded because that would swallow text"))
  20895. (goto-char end)
  20896. (skip-chars-forward " \t\n\r")
  20897. (beginning-of-line 1)
  20898. (push-mark beg 'nomsg)))
  20899. ((and subtreep org-yank-adjusted-subtrees)
  20900. (let ((beg (point-at-bol)))
  20901. (org-paste-subtree nil nil 'for-yank)
  20902. (push-mark beg 'nomsg)))
  20903. (t
  20904. (call-interactively command))))))
  20905. (defun org-yank-folding-would-swallow-text (beg end)
  20906. "Would hide-subtree at BEG swallow any text after END?"
  20907. (let (level)
  20908. (org-with-limited-levels
  20909. (save-excursion
  20910. (goto-char beg)
  20911. (when (or (looking-at org-outline-regexp)
  20912. (re-search-forward org-outline-regexp-bol end t))
  20913. (setq level (org-outline-level)))
  20914. (goto-char end)
  20915. (skip-chars-forward " \t\r\n\v\f")
  20916. (if (or (eobp)
  20917. (and (bolp) (looking-at org-outline-regexp)
  20918. (<= (org-outline-level) level)))
  20919. nil ; Nothing would be swallowed
  20920. t))))) ; something would swallow
  20921. (define-key org-mode-map "\C-y" 'org-yank)
  20922. (defun org-truely-invisible-p ()
  20923. "Check if point is at a character currently not visible.
  20924. This version does not only check the character property, but also
  20925. `visible-mode'."
  20926. ;; Early versions of noutline don't have `outline-invisible-p'.
  20927. (if (org-bound-and-true-p visible-mode)
  20928. nil
  20929. (outline-invisible-p)))
  20930. (defun org-invisible-p2 ()
  20931. "Check if point is at a character currently not visible."
  20932. (save-excursion
  20933. (if (and (eolp) (not (bobp))) (backward-char 1))
  20934. ;; Early versions of noutline don't have `outline-invisible-p'.
  20935. (outline-invisible-p)))
  20936. (defun org-back-to-heading (&optional invisible-ok)
  20937. "Call `outline-back-to-heading', but provide a better error message."
  20938. (condition-case nil
  20939. (outline-back-to-heading invisible-ok)
  20940. (error (error "Before first headline at position %d in buffer %s"
  20941. (point) (current-buffer)))))
  20942. (defun org-before-first-heading-p ()
  20943. "Before first heading?"
  20944. (save-excursion
  20945. (end-of-line)
  20946. (null (re-search-backward org-outline-regexp-bol nil t))))
  20947. (defun org-at-heading-p (&optional ignored)
  20948. (outline-on-heading-p t))
  20949. ;; Compatibility alias with Org versions < 7.8.03
  20950. (defalias 'org-on-heading-p 'org-at-heading-p)
  20951. (defun org-at-comment-p nil
  20952. "Is cursor in a line starting with a # character?"
  20953. (save-excursion
  20954. (beginning-of-line)
  20955. (looking-at "^#")))
  20956. (defun org-at-drawer-p nil
  20957. "Is cursor at a drawer keyword?"
  20958. (save-excursion
  20959. (move-beginning-of-line 1)
  20960. (looking-at org-drawer-regexp)))
  20961. (defun org-at-block-p nil
  20962. "Is cursor at a block keyword?"
  20963. (save-excursion
  20964. (move-beginning-of-line 1)
  20965. (looking-at org-block-regexp)))
  20966. (defun org-point-at-end-of-empty-headline ()
  20967. "If point is at the end of an empty headline, return t, else nil.
  20968. If the heading only contains a TODO keyword, it is still still considered
  20969. empty."
  20970. (and (looking-at "[ \t]*$")
  20971. (when org-todo-line-regexp
  20972. (save-excursion
  20973. (beginning-of-line 1)
  20974. (let ((case-fold-search nil))
  20975. (looking-at org-todo-line-regexp)
  20976. (string= (match-string 3) ""))))))
  20977. (defun org-at-heading-or-item-p ()
  20978. (or (org-at-heading-p) (org-at-item-p)))
  20979. (defun org-at-target-p ()
  20980. (or (org-in-regexp org-radio-target-regexp)
  20981. (org-in-regexp org-target-regexp)))
  20982. ;; Compatibility alias with Org versions < 7.8.03
  20983. (defalias 'org-on-target-p 'org-at-target-p)
  20984. (defun org-up-heading-all (arg)
  20985. "Move to the heading line of which the present line is a subheading.
  20986. This function considers both visible and invisible heading lines.
  20987. With argument, move up ARG levels."
  20988. (if (fboundp 'outline-up-heading-all)
  20989. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20990. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20991. (defun org-up-heading-safe ()
  20992. "Move to the heading line of which the present line is a subheading.
  20993. This version will not throw an error. It will return the level of the
  20994. headline found, or nil if no higher level is found.
  20995. Also, this function will be a lot faster than `outline-up-heading',
  20996. because it relies on stars being the outline starters. This can really
  20997. make a significant difference in outlines with very many siblings."
  20998. (let (start-level re)
  20999. (org-back-to-heading t)
  21000. (setq start-level (funcall outline-level))
  21001. (if (equal start-level 1)
  21002. nil
  21003. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21004. (if (re-search-backward re nil t)
  21005. (funcall outline-level)))))
  21006. (defun org-first-sibling-p ()
  21007. "Is this heading the first child of its parents?"
  21008. (interactive)
  21009. (let ((re org-outline-regexp-bol)
  21010. level l)
  21011. (unless (org-at-heading-p t)
  21012. (error "Not at a heading"))
  21013. (setq level (funcall outline-level))
  21014. (save-excursion
  21015. (if (not (re-search-backward re nil t))
  21016. t
  21017. (setq l (funcall outline-level))
  21018. (< l level)))))
  21019. (defun org-goto-sibling (&optional previous)
  21020. "Goto the next sibling, even if it is invisible.
  21021. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21022. when a sibling was found. When none is found, return nil and don't
  21023. move point."
  21024. (let ((fun (if previous 're-search-backward 're-search-forward))
  21025. (pos (point))
  21026. (re org-outline-regexp-bol)
  21027. level l)
  21028. (when (condition-case nil (org-back-to-heading t) (error nil))
  21029. (setq level (funcall outline-level))
  21030. (catch 'exit
  21031. (or previous (forward-char 1))
  21032. (while (funcall fun re nil t)
  21033. (setq l (funcall outline-level))
  21034. (when (< l level) (goto-char pos) (throw 'exit nil))
  21035. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21036. (goto-char pos)
  21037. nil))))
  21038. (defun org-show-siblings ()
  21039. "Show all siblings of the current headline."
  21040. (save-excursion
  21041. (while (org-goto-sibling) (org-flag-heading nil)))
  21042. (save-excursion
  21043. (while (org-goto-sibling 'previous)
  21044. (org-flag-heading nil))))
  21045. (defun org-goto-first-child ()
  21046. "Goto the first child, even if it is invisible.
  21047. Return t when a child was found. Otherwise don't move point and
  21048. return nil."
  21049. (let (level (pos (point)) (re org-outline-regexp-bol))
  21050. (when (condition-case nil (org-back-to-heading t) (error nil))
  21051. (setq level (outline-level))
  21052. (forward-char 1)
  21053. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21054. (progn (goto-char (match-beginning 0)) t)
  21055. (goto-char pos) nil))))
  21056. (defun org-show-hidden-entry ()
  21057. "Show an entry where even the heading is hidden."
  21058. (save-excursion
  21059. (org-show-entry)))
  21060. (defun org-flag-heading (flag &optional entry)
  21061. "Flag the current heading. FLAG non-nil means make invisible.
  21062. When ENTRY is non-nil, show the entire entry."
  21063. (save-excursion
  21064. (org-back-to-heading t)
  21065. ;; Check if we should show the entire entry
  21066. (if entry
  21067. (progn
  21068. (org-show-entry)
  21069. (save-excursion
  21070. (and (outline-next-heading)
  21071. (org-flag-heading nil))))
  21072. (outline-flag-region (max (point-min) (1- (point)))
  21073. (save-excursion (outline-end-of-heading) (point))
  21074. flag))))
  21075. (defun org-get-next-sibling ()
  21076. "Move to next heading of the same level, and return point.
  21077. If there is no such heading, return nil.
  21078. This is like outline-next-sibling, but invisible headings are ok."
  21079. (let ((level (funcall outline-level)))
  21080. (outline-next-heading)
  21081. (while (and (not (eobp)) (> (funcall outline-level) level))
  21082. (outline-next-heading))
  21083. (if (or (eobp) (< (funcall outline-level) level))
  21084. nil
  21085. (point))))
  21086. (defun org-get-last-sibling ()
  21087. "Move to previous heading of the same level, and return point.
  21088. If there is no such heading, return nil."
  21089. (let ((opoint (point))
  21090. (level (funcall outline-level)))
  21091. (outline-previous-heading)
  21092. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21093. (while (and (> (funcall outline-level) level)
  21094. (not (bobp)))
  21095. (outline-previous-heading))
  21096. (if (< (funcall outline-level) level)
  21097. nil
  21098. (point)))))
  21099. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21100. "Goto to the end of a subtree."
  21101. ;; This contains an exact copy of the original function, but it uses
  21102. ;; `org-back-to-heading', to make it work also in invisible
  21103. ;; trees. And is uses an invisible-ok argument.
  21104. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21105. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21106. ;; with many children and grandchildren etc, this can be much faster
  21107. ;; than the outline version.
  21108. (org-back-to-heading invisible-ok)
  21109. (let ((first t)
  21110. (level (funcall outline-level)))
  21111. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21112. ;; A true heading (not a plain list item), in Org-mode
  21113. ;; This means we can easily find the end by looking
  21114. ;; only for the right number of stars. Using a regexp to do
  21115. ;; this is so much faster than using a Lisp loop.
  21116. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21117. (forward-char 1)
  21118. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21119. ;; something else, do it the slow way
  21120. (while (and (not (eobp))
  21121. (or first (> (funcall outline-level) level)))
  21122. (setq first nil)
  21123. (outline-next-heading)))
  21124. (unless to-heading
  21125. (if (memq (preceding-char) '(?\n ?\^M))
  21126. (progn
  21127. ;; Go to end of line before heading
  21128. (forward-char -1)
  21129. (if (memq (preceding-char) '(?\n ?\^M))
  21130. ;; leave blank line before heading
  21131. (forward-char -1))))))
  21132. (point))
  21133. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21134. "Use Org version in org-mode, for dramatic speed-up."
  21135. (if (derived-mode-p 'org-mode)
  21136. (progn
  21137. (org-end-of-subtree nil t)
  21138. (unless (eobp) (backward-char 1)))
  21139. ad-do-it))
  21140. (defun org-end-of-meta-data-and-drawers ()
  21141. "Jump to the first text after meta data and drawers in the current entry.
  21142. This will move over empty lines, lines with planning time stamps,
  21143. clocking lines, and drawers."
  21144. (org-back-to-heading t)
  21145. (let ((end (save-excursion (outline-next-heading) (point)))
  21146. (re (concat "\\(" org-drawer-regexp "\\)"
  21147. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21148. (forward-line 1)
  21149. (while (re-search-forward re end t)
  21150. (if (not (match-end 1))
  21151. ;; empty or planning line
  21152. (forward-line 1)
  21153. ;; a drawer, find the end
  21154. (re-search-forward "^[ \t]*:END:" end 'move)
  21155. (forward-line 1)))
  21156. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21157. (point)))
  21158. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21159. "Move forward to the ARG'th subheading at same level as this one.
  21160. Stop at the first and last subheadings of a superior heading.
  21161. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21162. non-nil it will also look at invisible ones."
  21163. (interactive "p")
  21164. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21165. (if (and arg (< arg 0))
  21166. (goto-char (point-min))
  21167. (outline-next-heading))
  21168. (org-at-heading-p)
  21169. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21170. (f (if (and arg (< arg 0))
  21171. 're-search-backward
  21172. 're-search-forward))
  21173. (count (if arg (abs arg) 1))
  21174. (result (point)))
  21175. (forward-char (if (and arg (< arg 0)) -1 1))
  21176. (while (and (> count 0)
  21177. (funcall f org-outline-regexp-bol nil 'move))
  21178. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21179. (cond ((< l level) (setq count 0))
  21180. ((and (= l level)
  21181. (or invisible-ok
  21182. (progn
  21183. (goto-char (line-beginning-position))
  21184. (not (outline-invisible-p)))))
  21185. (setq count (1- count))
  21186. (when (eq l level)
  21187. (setq result (point)))))))
  21188. (goto-char result))
  21189. (beginning-of-line 1)))
  21190. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21191. "Move backward to the ARG'th subheading at same level as this one.
  21192. Stop at the first and last subheadings of a superior heading."
  21193. (interactive "p")
  21194. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21195. (defun org-next-block (arg &optional backward block-regexp)
  21196. "Jump to the next block.
  21197. With a prefix argument ARG, jump forward ARG many source blocks.
  21198. When BACKWARD is non-nil, jump to the previous block.
  21199. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21200. (interactive "p")
  21201. (let ((re (or block-regexp org-block-regexp))
  21202. (re-search-fn (or (and backward 're-search-backward)
  21203. 're-search-forward)))
  21204. (if (looking-at re) (forward-char 1))
  21205. (condition-case nil
  21206. (funcall re-search-fn re nil nil arg)
  21207. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21208. (goto-char (match-beginning 0)) (org-show-context)))
  21209. (defun org-previous-block (arg &optional block-regexp)
  21210. "Jump to the previous block.
  21211. With a prefix argument ARG, jump backward ARG many source blocks.
  21212. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21213. (interactive "p")
  21214. (org-next-block arg t block-regexp))
  21215. (defun org-forward-element ()
  21216. "Move forward by one element.
  21217. Move to the next element at the same level, when possible."
  21218. (interactive)
  21219. (cond ((eobp) (user-error "Cannot move further down"))
  21220. ((org-with-limited-levels (org-at-heading-p))
  21221. (let ((origin (point)))
  21222. (goto-char (org-end-of-subtree nil t))
  21223. (unless (org-with-limited-levels (org-at-heading-p))
  21224. (goto-char origin)
  21225. (user-error "Cannot move further down"))))
  21226. (t
  21227. (let* ((elem (org-element-at-point))
  21228. (end (org-element-property :end elem))
  21229. (parent (org-element-property :parent elem)))
  21230. (if (and parent (= (org-element-property :contents-end parent) end))
  21231. (goto-char (org-element-property :end parent))
  21232. (goto-char end))))))
  21233. (defun org-backward-element ()
  21234. "Move backward by one element.
  21235. Move to the previous element at the same level, when possible."
  21236. (interactive)
  21237. (cond ((bobp) (user-error "Cannot move further up"))
  21238. ((org-with-limited-levels (org-at-heading-p))
  21239. ;; At a headline, move to the previous one, if any, or stay
  21240. ;; here.
  21241. (let ((origin (point)))
  21242. (org-with-limited-levels (org-backward-heading-same-level 1))
  21243. ;; When current headline has no sibling above, move to its
  21244. ;; parent.
  21245. (when (= (point) origin)
  21246. (or (org-with-limited-levels (org-up-heading-safe))
  21247. (progn (goto-char origin)
  21248. (user-error "Cannot move further up"))))))
  21249. (t
  21250. (let* ((trail (org-element-at-point 'keep-trail))
  21251. (elem (car trail))
  21252. (prev-elem (nth 1 trail))
  21253. (beg (org-element-property :begin elem)))
  21254. (cond
  21255. ;; Move to beginning of current element if point isn't
  21256. ;; there already.
  21257. ((/= (point) beg) (goto-char beg))
  21258. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21259. ((org-before-first-heading-p) (goto-char (point-min)))
  21260. (t (org-back-to-heading)))))))
  21261. (defun org-up-element ()
  21262. "Move to upper element."
  21263. (interactive)
  21264. (if (org-with-limited-levels (org-at-heading-p))
  21265. (unless (org-up-heading-safe) (error "No surrounding element"))
  21266. (let* ((elem (org-element-at-point))
  21267. (parent (org-element-property :parent elem)))
  21268. (if parent (goto-char (org-element-property :begin parent))
  21269. (if (org-with-limited-levels (org-before-first-heading-p))
  21270. (error "No surrounding element")
  21271. (org-with-limited-levels (org-back-to-heading)))))))
  21272. (defvar org-element-greater-elements)
  21273. (defun org-down-element ()
  21274. "Move to inner element."
  21275. (interactive)
  21276. (let ((element (org-element-at-point)))
  21277. (cond
  21278. ((memq (org-element-type element) '(plain-list table))
  21279. (goto-char (org-element-property :contents-begin element))
  21280. (forward-char))
  21281. ((memq (org-element-type element) org-element-greater-elements)
  21282. ;; If contents are hidden, first disclose them.
  21283. (when (org-element-property :hiddenp element) (org-cycle))
  21284. (goto-char (or (org-element-property :contents-begin element)
  21285. (error "No content for this element"))))
  21286. (t (error "No inner element")))))
  21287. (defun org-drag-element-backward ()
  21288. "Move backward element at point."
  21289. (interactive)
  21290. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21291. (let* ((trail (org-element-at-point 'keep-trail))
  21292. (elem (car trail))
  21293. (prev-elem (nth 1 trail)))
  21294. ;; Error out if no previous element or previous element is
  21295. ;; a parent of the current one.
  21296. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21297. (error "Cannot drag element backward")
  21298. (let ((pos (point)))
  21299. (org-element-swap-A-B prev-elem elem)
  21300. (goto-char (+ (org-element-property :begin prev-elem)
  21301. (- pos (org-element-property :begin elem)))))))))
  21302. (defun org-drag-element-forward ()
  21303. "Move forward element at point."
  21304. (interactive)
  21305. (let* ((pos (point))
  21306. (elem (org-element-at-point)))
  21307. (when (= (point-max) (org-element-property :end elem))
  21308. (error "Cannot drag element forward"))
  21309. (goto-char (org-element-property :end elem))
  21310. (let ((next-elem (org-element-at-point)))
  21311. (when (or (org-element-nested-p elem next-elem)
  21312. (and (eq (org-element-type next-elem) 'headline)
  21313. (not (eq (org-element-type elem) 'headline))))
  21314. (goto-char pos)
  21315. (error "Cannot drag element forward"))
  21316. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21317. ;; body's length (without final blanks) and by the length of
  21318. ;; blanks between ELEM and NEXT-ELEM.
  21319. (let ((size-next (- (save-excursion
  21320. (goto-char (org-element-property :end next-elem))
  21321. (skip-chars-backward " \r\t\n")
  21322. (forward-line)
  21323. ;; Small correction if buffer doesn't end
  21324. ;; with a newline character.
  21325. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21326. (org-element-property :begin next-elem)))
  21327. (size-blank (- (org-element-property :end elem)
  21328. (save-excursion
  21329. (goto-char (org-element-property :end elem))
  21330. (skip-chars-backward " \r\t\n")
  21331. (forward-line)
  21332. (point)))))
  21333. (org-element-swap-A-B elem next-elem)
  21334. (goto-char (+ pos size-next size-blank))))))
  21335. (defun org-drag-line-forward (arg)
  21336. "Drag the line at point ARG lines forward."
  21337. (interactive "p")
  21338. (dotimes (n (abs arg))
  21339. (let ((c (current-column)))
  21340. (if (< 0 arg)
  21341. (progn
  21342. (beginning-of-line 2)
  21343. (transpose-lines 1)
  21344. (beginning-of-line 0))
  21345. (transpose-lines 1)
  21346. (beginning-of-line -1))
  21347. (org-move-to-column c))))
  21348. (defun org-drag-line-backward (arg)
  21349. "Drag the line at point ARG lines backward."
  21350. (interactive "p")
  21351. (org-drag-line-forward (- arg)))
  21352. (defun org-mark-element ()
  21353. "Put point at beginning of this element, mark at end.
  21354. Interactively, if this command is repeated or (in Transient Mark
  21355. mode) if the mark is active, it marks the next element after the
  21356. ones already marked."
  21357. (interactive)
  21358. (let (deactivate-mark)
  21359. (if (and (org-called-interactively-p 'any)
  21360. (or (and (eq last-command this-command) (mark t))
  21361. (and transient-mark-mode mark-active)))
  21362. (set-mark
  21363. (save-excursion
  21364. (goto-char (mark))
  21365. (goto-char (org-element-property :end (org-element-at-point)))))
  21366. (let ((element (org-element-at-point)))
  21367. (end-of-line)
  21368. (push-mark (org-element-property :end element) t t)
  21369. (goto-char (org-element-property :begin element))))))
  21370. (defun org-narrow-to-element ()
  21371. "Narrow buffer to current element."
  21372. (interactive)
  21373. (let ((elem (org-element-at-point)))
  21374. (cond
  21375. ((eq (car elem) 'headline)
  21376. (narrow-to-region
  21377. (org-element-property :begin elem)
  21378. (org-element-property :end elem)))
  21379. ((memq (car elem) org-element-greater-elements)
  21380. (narrow-to-region
  21381. (org-element-property :contents-begin elem)
  21382. (org-element-property :contents-end elem)))
  21383. (t
  21384. (narrow-to-region
  21385. (org-element-property :begin elem)
  21386. (org-element-property :end elem))))))
  21387. (defun org-transpose-element ()
  21388. "Transpose current and previous elements, keeping blank lines between.
  21389. Point is moved after both elements."
  21390. (interactive)
  21391. (org-skip-whitespace)
  21392. (let ((end (org-element-property :end (org-element-at-point))))
  21393. (org-drag-element-backward)
  21394. (goto-char end)))
  21395. (defun org-unindent-buffer ()
  21396. "Un-indent the visible part of the buffer.
  21397. Relative indentation (between items, inside blocks, etc.) isn't
  21398. modified."
  21399. (interactive)
  21400. (unless (eq major-mode 'org-mode)
  21401. (error "Cannot un-indent a buffer not in Org mode"))
  21402. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21403. unindent-tree ; For byte-compiler.
  21404. (unindent-tree
  21405. (function
  21406. (lambda (contents)
  21407. (mapc
  21408. (lambda (element)
  21409. (if (memq (org-element-type element) '(headline section))
  21410. (funcall unindent-tree (org-element-contents element))
  21411. (save-excursion
  21412. (save-restriction
  21413. (narrow-to-region
  21414. (org-element-property :begin element)
  21415. (org-element-property :end element))
  21416. (org-do-remove-indentation)))))
  21417. (reverse contents))))))
  21418. (funcall unindent-tree (org-element-contents parse-tree))))
  21419. (defun org-show-subtree ()
  21420. "Show everything after this heading at deeper levels."
  21421. (interactive)
  21422. (outline-flag-region
  21423. (point)
  21424. (save-excursion
  21425. (org-end-of-subtree t t))
  21426. nil))
  21427. (defun org-show-entry ()
  21428. "Show the body directly following this heading.
  21429. Show the heading too, if it is currently invisible."
  21430. (interactive)
  21431. (save-excursion
  21432. (condition-case nil
  21433. (progn
  21434. (org-back-to-heading t)
  21435. (outline-flag-region
  21436. (max (point-min) (1- (point)))
  21437. (save-excursion
  21438. (if (re-search-forward
  21439. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21440. (match-beginning 1)
  21441. (point-max)))
  21442. nil)
  21443. (org-cycle-hide-drawers 'children))
  21444. (error nil))))
  21445. (defun org-make-options-regexp (kwds &optional extra)
  21446. "Make a regular expression for keyword lines."
  21447. (concat
  21448. "^#\\+\\("
  21449. (mapconcat 'regexp-quote kwds "\\|")
  21450. (if extra (concat "\\|" extra))
  21451. "\\):[ \t]*\\(.*\\)"))
  21452. ;; Make isearch reveal the necessary context
  21453. (defun org-isearch-end ()
  21454. "Reveal context after isearch exits."
  21455. (when isearch-success ; only if search was successful
  21456. (if (featurep 'xemacs)
  21457. ;; Under XEmacs, the hook is run in the correct place,
  21458. ;; we directly show the context.
  21459. (org-show-context 'isearch)
  21460. ;; In Emacs the hook runs *before* restoring the overlays.
  21461. ;; So we have to use a one-time post-command-hook to do this.
  21462. ;; (Emacs 22 has a special variable, see function `org-mode')
  21463. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21464. isearch-mode-end-hook-quit)
  21465. ;; Only when the isearch was not quitted.
  21466. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21467. 'append 'local)))
  21468. (org-fix-ellipsis-at-bol)))
  21469. (defun org-isearch-post-command ()
  21470. "Remove self from hook, and show context."
  21471. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21472. (org-show-context 'isearch))
  21473. ;;;; Integration with and fixes for other packages
  21474. ;;; Imenu support
  21475. (defvar org-imenu-markers nil
  21476. "All markers currently used by Imenu.")
  21477. (make-variable-buffer-local 'org-imenu-markers)
  21478. (defun org-imenu-new-marker (&optional pos)
  21479. "Return a new marker for use by Imenu, and remember the marker."
  21480. (let ((m (make-marker)))
  21481. (move-marker m (or pos (point)))
  21482. (push m org-imenu-markers)
  21483. m))
  21484. (defun org-imenu-get-tree ()
  21485. "Produce the index for Imenu."
  21486. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21487. (setq org-imenu-markers nil)
  21488. (let* ((n org-imenu-depth)
  21489. (re (concat "^" (org-get-limited-outline-regexp)))
  21490. (subs (make-vector (1+ n) nil))
  21491. (last-level 0)
  21492. m level head0 head)
  21493. (save-excursion
  21494. (save-restriction
  21495. (widen)
  21496. (goto-char (point-max))
  21497. (while (re-search-backward re nil t)
  21498. (setq level (org-reduced-level (funcall outline-level)))
  21499. (when (and (<= level n)
  21500. (looking-at org-complex-heading-regexp)
  21501. (setq head0 (org-match-string-no-properties 4)))
  21502. (setq head (org-link-display-format head0)
  21503. m (org-imenu-new-marker))
  21504. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21505. (if (>= level last-level)
  21506. (push (cons head m) (aref subs level))
  21507. (push (cons head (aref subs (1+ level))) (aref subs level))
  21508. (loop for i from (1+ level) to n do (aset subs i nil)))
  21509. (setq last-level level)))))
  21510. (aref subs 1)))
  21511. (eval-after-load "imenu"
  21512. '(progn
  21513. (add-hook 'imenu-after-jump-hook
  21514. (lambda ()
  21515. (if (derived-mode-p 'org-mode)
  21516. (org-show-context 'org-goto))))))
  21517. (defun org-link-display-format (link)
  21518. "Replace a link with its the description.
  21519. If there is no description, use the link target."
  21520. (save-match-data
  21521. (if (string-match org-bracket-link-analytic-regexp link)
  21522. (replace-match (if (match-end 5)
  21523. (match-string 5 link)
  21524. (concat (match-string 1 link)
  21525. (match-string 3 link)))
  21526. nil t link)
  21527. link)))
  21528. (defun org-toggle-link-display ()
  21529. "Toggle the literal or descriptive display of links."
  21530. (interactive)
  21531. (if org-descriptive-links
  21532. (progn (org-remove-from-invisibility-spec '(org-link))
  21533. (org-restart-font-lock)
  21534. (setq org-descriptive-links nil))
  21535. (progn (add-to-invisibility-spec '(org-link))
  21536. (org-restart-font-lock)
  21537. (setq org-descriptive-links t))))
  21538. ;; Speedbar support
  21539. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21540. "Overlay marking the agenda restriction line in speedbar.")
  21541. (overlay-put org-speedbar-restriction-lock-overlay
  21542. 'face 'org-agenda-restriction-lock)
  21543. (overlay-put org-speedbar-restriction-lock-overlay
  21544. 'help-echo "Agendas are currently limited to this item.")
  21545. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21546. (defun org-speedbar-set-agenda-restriction ()
  21547. "Restrict future agenda commands to the location at point in speedbar.
  21548. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21549. (interactive)
  21550. (require 'org-agenda)
  21551. (let (p m tp np dir txt)
  21552. (cond
  21553. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21554. 'org-imenu t))
  21555. (setq m (get-text-property p 'org-imenu-marker))
  21556. (with-current-buffer (marker-buffer m)
  21557. (goto-char m)
  21558. (org-agenda-set-restriction-lock 'subtree)))
  21559. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21560. 'speedbar-function 'speedbar-find-file))
  21561. (setq tp (previous-single-property-change
  21562. (1+ p) 'speedbar-function)
  21563. np (next-single-property-change
  21564. tp 'speedbar-function)
  21565. dir (speedbar-line-directory)
  21566. txt (buffer-substring-no-properties (or tp (point-min))
  21567. (or np (point-max))))
  21568. (with-current-buffer (find-file-noselect
  21569. (let ((default-directory dir))
  21570. (expand-file-name txt)))
  21571. (unless (derived-mode-p 'org-mode)
  21572. (error "Cannot restrict to non-Org-mode file"))
  21573. (org-agenda-set-restriction-lock 'file)))
  21574. (t (error "Don't know how to restrict Org-mode's agenda")))
  21575. (move-overlay org-speedbar-restriction-lock-overlay
  21576. (point-at-bol) (point-at-eol))
  21577. (setq current-prefix-arg nil)
  21578. (org-agenda-maybe-redo)))
  21579. (eval-after-load "speedbar"
  21580. '(progn
  21581. (speedbar-add-supported-extension ".org")
  21582. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21583. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21584. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21585. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21586. (add-hook 'speedbar-visiting-tag-hook
  21587. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21588. ;;; Fixes and Hacks for problems with other packages
  21589. ;; Make flyspell not check words in links, to not mess up our keymap
  21590. (defvar org-element-affiliated-keywords) ; From org-element.el
  21591. (defvar org-element-block-name-alist) ; From org-element.el
  21592. (defun org-mode-flyspell-verify ()
  21593. "Don't let flyspell put overlays at active buttons, or on
  21594. {todo,all-time,additional-option-like}-keywords."
  21595. (require 'org-element) ; For `org-element-affiliated-keywords'
  21596. (let ((pos (max (1- (point)) (point-min)))
  21597. (word (thing-at-point 'word)))
  21598. (and (not (get-text-property pos 'keymap))
  21599. (not (get-text-property pos 'org-no-flyspell))
  21600. (not (member word org-todo-keywords-1))
  21601. (not (member word org-all-time-keywords))
  21602. (not (member word org-options-keywords))
  21603. (not (member word (mapcar 'car org-startup-options)))
  21604. (not (member-ignore-case word org-element-affiliated-keywords))
  21605. (not (member-ignore-case word (org-get-export-keywords)))
  21606. (not (member-ignore-case
  21607. word (mapcar 'car org-element-block-name-alist)))
  21608. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21609. (defun org-remove-flyspell-overlays-in (beg end)
  21610. "Remove flyspell overlays in region."
  21611. (and (org-bound-and-true-p flyspell-mode)
  21612. (fboundp 'flyspell-delete-region-overlays)
  21613. (flyspell-delete-region-overlays beg end))
  21614. (add-text-properties beg end '(org-no-flyspell t)))
  21615. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21616. (eval-after-load "bookmark"
  21617. '(if (boundp 'bookmark-after-jump-hook)
  21618. ;; We can use the hook
  21619. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21620. ;; Hook not available, use advice
  21621. (defadvice bookmark-jump (after org-make-visible activate)
  21622. "Make the position visible."
  21623. (org-bookmark-jump-unhide))))
  21624. ;; Make sure saveplace shows the location if it was hidden
  21625. (eval-after-load "saveplace"
  21626. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21627. "Make the position visible."
  21628. (org-bookmark-jump-unhide)))
  21629. ;; Make sure ecb shows the location if it was hidden
  21630. (eval-after-load "ecb"
  21631. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21632. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21633. (if (derived-mode-p 'org-mode)
  21634. (org-show-context))))
  21635. (defun org-bookmark-jump-unhide ()
  21636. "Unhide the current position, to show the bookmark location."
  21637. (and (derived-mode-p 'org-mode)
  21638. (or (outline-invisible-p)
  21639. (save-excursion (goto-char (max (point-min) (1- (point))))
  21640. (outline-invisible-p)))
  21641. (org-show-context 'bookmark-jump)))
  21642. ;; Make session.el ignore our circular variable
  21643. (eval-after-load "session"
  21644. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21645. ;;;; Finish up
  21646. (provide 'org)
  21647. (run-hooks 'org-load-hook)
  21648. ;;; org.el ends here