org.el 890 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551
  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 (not noerror)
  4554. (error "Cannot read file \"%s\"" file)
  4555. (message "Cannot read file \"%s\"" file)
  4556. (sit-for 3))
  4557. (with-temp-buffer
  4558. (insert-file-contents file)
  4559. (buffer-string))))
  4560. (defun org-extract-log-state-settings (x)
  4561. "Extract the log state setting from a TODO keyword string.
  4562. This will extract info from a string like \"WAIT(w@/!)\"."
  4563. (let (kw key log1 log2)
  4564. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4565. (setq kw (match-string 1 x)
  4566. key (and (match-end 2) (match-string 2 x))
  4567. log1 (and (match-end 3) (match-string 3 x))
  4568. log2 (and (match-end 4) (match-string 4 x)))
  4569. (and (or log1 log2)
  4570. (list kw
  4571. (and log1 (if (equal log1 "!") 'time 'note))
  4572. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4573. (defun org-remove-keyword-keys (list)
  4574. "Remove a pair of parenthesis at the end of each string in LIST."
  4575. (mapcar (lambda (x)
  4576. (if (string-match "(.*)$" x)
  4577. (substring x 0 (match-beginning 0))
  4578. x))
  4579. list))
  4580. (defun org-assign-fast-keys (alist)
  4581. "Assign fast keys to a keyword-key alist.
  4582. Respect keys that are already there."
  4583. (let (new e (alt ?0))
  4584. (while (setq e (pop alist))
  4585. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4586. (cdr e)) ;; Key already assigned.
  4587. (push e new)
  4588. (let ((clist (string-to-list (downcase (car e))))
  4589. (used (append new alist)))
  4590. (when (= (car clist) ?@)
  4591. (pop clist))
  4592. (while (and clist (rassoc (car clist) used))
  4593. (pop clist))
  4594. (unless clist
  4595. (while (rassoc alt used)
  4596. (incf alt)))
  4597. (push (cons (car e) (or (car clist) alt)) new))))
  4598. (nreverse new)))
  4599. ;;; Some variables used in various places
  4600. (defvar org-window-configuration nil
  4601. "Used in various places to store a window configuration.")
  4602. (defvar org-selected-window nil
  4603. "Used in various places to store a window configuration.")
  4604. (defvar org-finish-function nil
  4605. "Function to be called when `C-c C-c' is used.
  4606. This is for getting out of special buffers like capture.")
  4607. ;; FIXME: Occasionally check by commenting these, to make sure
  4608. ;; no other functions uses these, forgetting to let-bind them.
  4609. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4610. (defvar org-last-state)
  4611. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4612. ;; Defined somewhere in this file, but used before definition.
  4613. (defvar org-entities) ;; defined in org-entities.el
  4614. (defvar org-struct-menu)
  4615. (defvar org-org-menu)
  4616. (defvar org-tbl-menu)
  4617. ;;;; Define the Org-mode
  4618. ;; We use a before-change function to check if a table might need
  4619. ;; an update.
  4620. (defvar org-table-may-need-update t
  4621. "Indicates that a table might need an update.
  4622. This variable is set by `org-before-change-function'.
  4623. `org-table-align' sets it back to nil.")
  4624. (defun org-before-change-function (beg end)
  4625. "Every change indicates that a table might need an update."
  4626. (setq org-table-may-need-update t))
  4627. (defvar org-mode-map)
  4628. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4629. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4630. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4631. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4632. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4633. (defvar org-table-buffer-is-an nil)
  4634. (defvar bidi-paragraph-direction)
  4635. (defvar buffer-face-mode-face)
  4636. (require 'outline)
  4637. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4638. (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"))
  4639. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4640. ;; Other stuff we need.
  4641. (require 'time-date)
  4642. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4643. (require 'easymenu)
  4644. (require 'overlay)
  4645. ;; (require 'org-macs) moved higher up in the file before it is first used
  4646. (require 'org-entities)
  4647. ;; (require 'org-compat) moved higher up in the file before it is first used
  4648. (require 'org-faces)
  4649. (require 'org-list)
  4650. (require 'org-pcomplete)
  4651. (require 'org-src)
  4652. (require 'org-footnote)
  4653. (require 'org-macro)
  4654. ;; babel
  4655. (require 'ob)
  4656. ;;;###autoload
  4657. (define-derived-mode org-mode outline-mode "Org"
  4658. "Outline-based notes management and organizer, alias
  4659. \"Carsten's outline-mode for keeping track of everything.\"
  4660. Org-mode develops organizational tasks around a NOTES file which
  4661. contains information about projects as plain text. Org-mode is
  4662. implemented on top of outline-mode, which is ideal to keep the content
  4663. of large files well structured. It supports ToDo items, deadlines and
  4664. time stamps, which magically appear in the diary listing of the Emacs
  4665. calendar. Tables are easily created with a built-in table editor.
  4666. Plain text URL-like links connect to websites, emails (VM), Usenet
  4667. messages (Gnus), BBDB entries, and any files related to the project.
  4668. For printing and sharing of notes, an Org-mode file (or a part of it)
  4669. can be exported as a structured ASCII or HTML file.
  4670. The following commands are available:
  4671. \\{org-mode-map}"
  4672. ;; Get rid of Outline menus, they are not needed
  4673. ;; Need to do this here because define-derived-mode sets up
  4674. ;; the keymap so late. Still, it is a waste to call this each time
  4675. ;; we switch another buffer into org-mode.
  4676. (if (featurep 'xemacs)
  4677. (when (boundp 'outline-mode-menu-heading)
  4678. ;; Assume this is Greg's port, it uses easymenu
  4679. (easy-menu-remove outline-mode-menu-heading)
  4680. (easy-menu-remove outline-mode-menu-show)
  4681. (easy-menu-remove outline-mode-menu-hide))
  4682. (define-key org-mode-map [menu-bar headings] 'undefined)
  4683. (define-key org-mode-map [menu-bar hide] 'undefined)
  4684. (define-key org-mode-map [menu-bar show] 'undefined))
  4685. (org-load-modules-maybe)
  4686. (easy-menu-add org-org-menu)
  4687. (easy-menu-add org-tbl-menu)
  4688. (org-install-agenda-files-menu)
  4689. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4690. (add-to-invisibility-spec '(org-cwidth))
  4691. (add-to-invisibility-spec '(org-hide-block . t))
  4692. (when (featurep 'xemacs)
  4693. (org-set-local 'line-move-ignore-invisible t))
  4694. (org-set-local 'outline-regexp org-outline-regexp)
  4695. (org-set-local 'outline-level 'org-outline-level)
  4696. (setq bidi-paragraph-direction 'left-to-right)
  4697. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4698. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4699. (when (and org-ellipsis
  4700. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4701. (fboundp 'make-glyph-code))
  4702. (unless org-display-table
  4703. (setq org-display-table (make-display-table)))
  4704. (set-display-table-slot
  4705. org-display-table 4
  4706. (vconcat (mapcar
  4707. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4708. org-ellipsis)))
  4709. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4710. (setq buffer-display-table org-display-table))
  4711. (org-set-regexps-and-options-for-tags)
  4712. (org-set-regexps-and-options)
  4713. (when (and org-tag-faces (not org-tags-special-faces-re))
  4714. ;; tag faces set outside customize.... force initialization.
  4715. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4716. ;; Calc embedded
  4717. (org-set-local 'calc-embedded-open-mode "# ")
  4718. ;; Modify a few syntax entries
  4719. (modify-syntax-entry ?@ "w")
  4720. (modify-syntax-entry ?\" "\"")
  4721. (if org-startup-truncated (setq truncate-lines t))
  4722. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4723. (org-set-local 'font-lock-unfontify-region-function
  4724. 'org-unfontify-region)
  4725. ;; Activate before-change-function
  4726. (org-set-local 'org-table-may-need-update t)
  4727. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4728. 'local)
  4729. ;; Check for running clock before killing a buffer
  4730. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4731. ;; Initialize macros templates.
  4732. (org-macro-initialize-templates)
  4733. ;; Initialize radio targets.
  4734. (org-update-radio-target-regexp)
  4735. ;; Indentation.
  4736. (org-set-local 'indent-line-function 'org-indent-line)
  4737. (org-set-local 'indent-region-function 'org-indent-region)
  4738. ;; Filling and auto-filling.
  4739. (org-setup-filling)
  4740. ;; Comments.
  4741. (org-setup-comments-handling)
  4742. ;; Beginning/end of defun
  4743. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4744. (org-set-local 'end-of-defun-function 'org-forward-element)
  4745. ;; Next error for sparse trees
  4746. (org-set-local 'next-error-function 'org-occur-next-match)
  4747. ;; Make sure dependence stuff works reliably, even for users who set it
  4748. ;; too late :-(
  4749. (if org-enforce-todo-dependencies
  4750. (add-hook 'org-blocker-hook
  4751. 'org-block-todo-from-children-or-siblings-or-parent)
  4752. (remove-hook 'org-blocker-hook
  4753. 'org-block-todo-from-children-or-siblings-or-parent))
  4754. (if org-enforce-todo-checkbox-dependencies
  4755. (add-hook 'org-blocker-hook
  4756. 'org-block-todo-from-checkboxes)
  4757. (remove-hook 'org-blocker-hook
  4758. 'org-block-todo-from-checkboxes))
  4759. ;; Align options lines
  4760. (org-set-local
  4761. 'align-mode-rules-list
  4762. '((org-in-buffer-settings
  4763. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4764. (modes . '(org-mode)))))
  4765. ;; Imenu
  4766. (org-set-local 'imenu-create-index-function
  4767. 'org-imenu-get-tree)
  4768. ;; Make isearch reveal context
  4769. (if (or (featurep 'xemacs)
  4770. (not (boundp 'outline-isearch-open-invisible-function)))
  4771. ;; Emacs 21 and XEmacs make use of the hook
  4772. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4773. ;; Emacs 22 deals with this through a special variable
  4774. (org-set-local 'outline-isearch-open-invisible-function
  4775. (lambda (&rest ignore) (org-show-context 'isearch)))
  4776. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4777. ;; Setup the pcomplete hooks
  4778. (set (make-local-variable 'pcomplete-command-completion-function)
  4779. 'org-pcomplete-initial)
  4780. (set (make-local-variable 'pcomplete-command-name-function)
  4781. 'org-command-at-point)
  4782. (set (make-local-variable 'pcomplete-default-completion-function)
  4783. 'ignore)
  4784. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4785. 'org-parse-arguments)
  4786. (set (make-local-variable 'pcomplete-termination-string) "")
  4787. (when (>= emacs-major-version 23)
  4788. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4789. ;; If empty file that did not turn on org-mode automatically, make it to.
  4790. (if (and org-insert-mode-line-in-empty-file
  4791. (org-called-interactively-p 'any)
  4792. (= (point-min) (point-max)))
  4793. (insert "# -*- mode: org -*-\n\n"))
  4794. (unless org-inhibit-startup
  4795. (org-unmodified
  4796. (and org-startup-with-beamer-mode (org-beamer-mode))
  4797. (when org-startup-align-all-tables
  4798. (org-table-map-tables 'org-table-align 'quietly))
  4799. (when org-startup-with-inline-images
  4800. (org-display-inline-images))
  4801. (when org-startup-with-latex-preview
  4802. (org-preview-latex-fragment))
  4803. (unless org-inhibit-startup-visibility-stuff
  4804. (org-set-startup-visibility))))
  4805. ;; Try to set org-hide correctly
  4806. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4807. ;; Update `customize-package-emacs-version-alist'
  4808. (add-to-list 'customize-package-emacs-version-alist
  4809. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4810. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4811. ("8.0" . "24.4")))
  4812. (defvar org-mode-transpose-word-syntax-table
  4813. (let ((st (make-syntax-table)))
  4814. (mapc (lambda(c) (modify-syntax-entry
  4815. (string-to-char (car c)) "w p" st))
  4816. org-emphasis-alist)
  4817. st))
  4818. (when (fboundp 'abbrev-table-put)
  4819. (abbrev-table-put org-mode-abbrev-table
  4820. :parents (list text-mode-abbrev-table)))
  4821. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4822. (defsubst org-fix-ellipsis-at-bol ()
  4823. (save-excursion (goto-char (window-start)) (recenter 0)))
  4824. (defun org-find-invisible-foreground ()
  4825. (let ((candidates (remove
  4826. "unspecified-bg"
  4827. (nconc
  4828. (list (face-background 'default)
  4829. (face-background 'org-default))
  4830. (mapcar
  4831. (lambda (alist)
  4832. (when (boundp alist)
  4833. (cdr (assoc 'background-color (symbol-value alist)))))
  4834. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4835. (list (face-foreground 'org-hide))))))
  4836. (car (remove nil candidates))))
  4837. (defun org-current-time (&optional rounding-minutes past)
  4838. "Current time, possibly rounded to ROUNDING-MINUTES.
  4839. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4840. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4841. the rounding returns a past time."
  4842. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4843. (car org-time-stamp-rounding-minutes)))
  4844. (time (decode-time)) res)
  4845. (if (< r 1)
  4846. (current-time)
  4847. (setq res
  4848. (apply 'encode-time
  4849. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4850. (nthcdr 2 time))))
  4851. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4852. (seconds-to-time (- (org-float-time res) (* r 60)))
  4853. res))))
  4854. (defun org-today ()
  4855. "Return today date, considering `org-extend-today-until'."
  4856. (time-to-days
  4857. (time-subtract (current-time)
  4858. (list 0 (* 3600 org-extend-today-until) 0))))
  4859. ;;;; Font-Lock stuff, including the activators
  4860. (defvar org-mouse-map (make-sparse-keymap))
  4861. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4862. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4863. (when org-mouse-1-follows-link
  4864. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4865. (when org-tab-follows-link
  4866. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4867. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4868. (require 'font-lock)
  4869. (defconst org-non-link-chars "]\t\n\r<>")
  4870. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4871. "shell" "elisp" "doi" "message"))
  4872. (defvar org-link-types-re nil
  4873. "Matches a link that has a url-like prefix like \"http:\"")
  4874. (defvar org-link-re-with-space nil
  4875. "Matches a link with spaces, optional angular brackets around it.")
  4876. (defvar org-link-re-with-space2 nil
  4877. "Matches a link with spaces, optional angular brackets around it.")
  4878. (defvar org-link-re-with-space3 nil
  4879. "Matches a link with spaces, only for internal part in bracket links.")
  4880. (defvar org-angle-link-re nil
  4881. "Matches link with angular brackets, spaces are allowed.")
  4882. (defvar org-plain-link-re nil
  4883. "Matches plain link, without spaces.")
  4884. (defvar org-bracket-link-regexp nil
  4885. "Matches a link in double brackets.")
  4886. (defvar org-bracket-link-analytic-regexp nil
  4887. "Regular expression used to analyze links.
  4888. Here is what the match groups contain after a match:
  4889. 1: http:
  4890. 2: http
  4891. 3: path
  4892. 4: [desc]
  4893. 5: desc")
  4894. (defvar org-bracket-link-analytic-regexp++ nil
  4895. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4896. (defvar org-any-link-re nil
  4897. "Regular expression matching any link.")
  4898. (defconst org-match-sexp-depth 3
  4899. "Number of stacked braces for sub/superscript matching.")
  4900. (defun org-create-multibrace-regexp (left right n)
  4901. "Create a regular expression which will match a balanced sexp.
  4902. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4903. as single character strings.
  4904. The regexp returned will match the entire expression including the
  4905. delimiters. It will also define a single group which contains the
  4906. match except for the outermost delimiters. The maximum depth of
  4907. stacked delimiters is N. Escaping delimiters is not possible."
  4908. (let* ((nothing (concat "[^" left right "]*?"))
  4909. (or "\\|")
  4910. (re nothing)
  4911. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4912. (while (> n 1)
  4913. (setq n (1- n)
  4914. re (concat re or next)
  4915. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4916. (concat left "\\(" re "\\)" right)))
  4917. (defvar org-match-substring-regexp
  4918. (concat
  4919. "\\(\\S-\\)\\([_^]\\)\\("
  4920. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4921. "\\|"
  4922. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4923. "\\|"
  4924. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4925. "The regular expression matching a sub- or superscript.")
  4926. (defvar org-match-substring-with-braces-regexp
  4927. (concat
  4928. "\\(\\S-\\)\\([_^]\\)\\("
  4929. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4930. "\\)")
  4931. "The regular expression matching a sub- or superscript, forcing braces.")
  4932. (defun org-make-link-regexps ()
  4933. "Update the link regular expressions.
  4934. This should be called after the variable `org-link-types' has changed."
  4935. (setq org-link-types-re
  4936. (concat
  4937. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4938. org-link-re-with-space
  4939. (concat
  4940. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4941. "\\([^" org-non-link-chars " ]"
  4942. "[^" org-non-link-chars "]*"
  4943. "[^" org-non-link-chars " ]\\)>?")
  4944. org-link-re-with-space2
  4945. (concat
  4946. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4947. "\\([^" org-non-link-chars " ]"
  4948. "[^\t\n\r]*"
  4949. "[^" org-non-link-chars " ]\\)>?")
  4950. org-link-re-with-space3
  4951. (concat
  4952. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4953. "\\([^" org-non-link-chars " ]"
  4954. "[^\t\n\r]*\\)")
  4955. org-angle-link-re
  4956. (concat
  4957. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4958. "\\([^" org-non-link-chars " ]"
  4959. "[^" org-non-link-chars "]*"
  4960. "\\)>")
  4961. org-plain-link-re
  4962. (concat
  4963. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4964. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4965. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4966. org-bracket-link-regexp
  4967. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4968. org-bracket-link-analytic-regexp
  4969. (concat
  4970. "\\[\\["
  4971. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4972. "\\([^]]+\\)"
  4973. "\\]"
  4974. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4975. "\\]")
  4976. org-bracket-link-analytic-regexp++
  4977. (concat
  4978. "\\[\\["
  4979. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4980. "\\([^]]+\\)"
  4981. "\\]"
  4982. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4983. "\\]")
  4984. org-any-link-re
  4985. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4986. org-angle-link-re "\\)\\|\\("
  4987. org-plain-link-re "\\)")))
  4988. (org-make-link-regexps)
  4989. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4990. "Regular expression for fast time stamp matching.")
  4991. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4992. "Regular expression for fast time stamp matching.")
  4993. (defconst org-ts-regexp0
  4994. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4995. "Regular expression matching time strings for analysis.
  4996. This one does not require the space after the date, so it can be used
  4997. on a string that terminates immediately after the date.")
  4998. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4999. "Regular expression matching time strings for analysis.")
  5000. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5001. "Regular expression matching time stamps, with groups.")
  5002. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5003. "Regular expression matching time stamps (also [..]), with groups.")
  5004. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5005. "Regular expression matching a time stamp range.")
  5006. (defconst org-tr-regexp-both
  5007. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5008. "Regular expression matching a time stamp range.")
  5009. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5010. org-ts-regexp "\\)?")
  5011. "Regular expression matching a time stamp or time stamp range.")
  5012. (defconst org-tsr-regexp-both
  5013. (concat org-ts-regexp-both "\\(--?-?"
  5014. org-ts-regexp-both "\\)?")
  5015. "Regular expression matching a time stamp or time stamp range.
  5016. The time stamps may be either active or inactive.")
  5017. (defvar org-emph-face nil)
  5018. (defun org-do-emphasis-faces (limit)
  5019. "Run through the buffer and add overlays to emphasized strings."
  5020. (let (rtn a)
  5021. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5022. (if (not (= (char-after (match-beginning 3))
  5023. (char-after (match-beginning 4))))
  5024. (progn
  5025. (setq rtn t)
  5026. (setq a (assoc (match-string 3) org-emphasis-alist))
  5027. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5028. 'face
  5029. (nth 1 a))
  5030. (and (nth 4 a)
  5031. (org-remove-flyspell-overlays-in
  5032. (match-beginning 0) (match-end 0)))
  5033. (add-text-properties (match-beginning 2) (match-end 2)
  5034. '(font-lock-multiline t org-emphasis t))
  5035. (when org-hide-emphasis-markers
  5036. (add-text-properties (match-end 4) (match-beginning 5)
  5037. '(invisible org-link))
  5038. (add-text-properties (match-beginning 3) (match-end 3)
  5039. '(invisible org-link)))))
  5040. (backward-char 1))
  5041. rtn))
  5042. (defun org-emphasize (&optional char)
  5043. "Insert or change an emphasis, i.e. a font like bold or italic.
  5044. If there is an active region, change that region to a new emphasis.
  5045. If there is no region, just insert the marker characters and position
  5046. the cursor between them.
  5047. CHAR should be either the marker character, or the first character of the
  5048. HTML tag associated with that emphasis. If CHAR is a space, the means
  5049. to remove the emphasis of the selected region.
  5050. If char is not given (for example in an interactive call) it
  5051. will be prompted for."
  5052. (interactive)
  5053. (let ((eal org-emphasis-alist) e det
  5054. (erc org-emphasis-regexp-components)
  5055. (prompt "")
  5056. (string "") beg end move tag c s)
  5057. (if (org-region-active-p)
  5058. (setq beg (region-beginning) end (region-end)
  5059. string (buffer-substring beg end))
  5060. (setq move t))
  5061. (while (setq e (pop eal))
  5062. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  5063. c (aref tag 0))
  5064. (push (cons c (string-to-char (car e))) det)
  5065. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  5066. (substring tag 1)))))
  5067. (setq det (nreverse det))
  5068. (unless char
  5069. (message "%s" (concat "Emphasis marker or tag:" prompt))
  5070. (setq char (read-char-exclusive)))
  5071. (setq char (or (cdr (assoc char det)) char))
  5072. (if (equal char ?\ )
  5073. (setq s "" move nil)
  5074. (unless (assoc (char-to-string char) org-emphasis-alist)
  5075. (error "No such emphasis marker: \"%c\"" char))
  5076. (setq s (char-to-string char)))
  5077. (while (and (> (length string) 1)
  5078. (equal (substring string 0 1) (substring string -1))
  5079. (assoc (substring string 0 1) org-emphasis-alist))
  5080. (setq string (substring string 1 -1)))
  5081. (setq string (concat s string s))
  5082. (if beg (delete-region beg end))
  5083. (unless (or (bolp)
  5084. (string-match (concat "[" (nth 0 erc) "\n]")
  5085. (char-to-string (char-before (point)))))
  5086. (insert " "))
  5087. (unless (or (eobp)
  5088. (string-match (concat "[" (nth 1 erc) "\n]")
  5089. (char-to-string (char-after (point)))))
  5090. (insert " ") (backward-char 1))
  5091. (insert string)
  5092. (and move (backward-char 1))))
  5093. (defconst org-nonsticky-props
  5094. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5095. (defsubst org-rear-nonsticky-at (pos)
  5096. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5097. (defun org-activate-plain-links (limit)
  5098. "Run through the buffer and add overlays to links."
  5099. (let (f hl)
  5100. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5101. (not (org-in-src-block-p)))
  5102. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5103. (setq f (get-text-property (match-beginning 0) 'face))
  5104. (setq hl (org-match-string-no-properties 0))
  5105. (if (or (eq f 'org-tag)
  5106. (and (listp f) (memq 'org-tag f)))
  5107. nil
  5108. (add-text-properties (match-beginning 0) (match-end 0)
  5109. (list 'mouse-face 'highlight
  5110. 'face 'org-link
  5111. 'htmlize-link `(:uri ,hl)
  5112. 'keymap org-mouse-map))
  5113. (org-rear-nonsticky-at (match-end 0)))
  5114. t)))
  5115. (defun org-activate-code (limit)
  5116. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5117. (progn
  5118. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5119. (remove-text-properties (match-beginning 0) (match-end 0)
  5120. '(display t invisible t intangible t))
  5121. t)))
  5122. (defcustom org-src-fontify-natively nil
  5123. "When non-nil, fontify code in code blocks."
  5124. :type 'boolean
  5125. :version "24.1"
  5126. :group 'org-appearance
  5127. :group 'org-babel)
  5128. (defcustom org-allow-promoting-top-level-subtree nil
  5129. "When non-nil, allow promoting a top level subtree.
  5130. The leading star of the top level headline will be replaced
  5131. by a #."
  5132. :type 'boolean
  5133. :version "24.1"
  5134. :group 'org-appearance)
  5135. (defun org-fontify-meta-lines-and-blocks (limit)
  5136. (condition-case nil
  5137. (org-fontify-meta-lines-and-blocks-1 limit)
  5138. (error (message "org-mode fontification error"))))
  5139. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5140. "Fontify #+ lines and blocks."
  5141. (let ((case-fold-search t))
  5142. (if (re-search-forward
  5143. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5144. limit t)
  5145. (let ((beg (match-beginning 0))
  5146. (block-start (match-end 0))
  5147. (block-end nil)
  5148. (lang (match-string 7))
  5149. (beg1 (line-beginning-position 2))
  5150. (dc1 (downcase (match-string 2)))
  5151. (dc3 (downcase (match-string 3)))
  5152. end end1 quoting block-type ovl)
  5153. (cond
  5154. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5155. ;; a single line of backend-specific content
  5156. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5157. (remove-text-properties (match-beginning 0) (match-end 0)
  5158. '(display t invisible t intangible t))
  5159. (add-text-properties (match-beginning 1) (match-end 3)
  5160. '(font-lock-fontified t face org-meta-line))
  5161. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5162. '(font-lock-fontified t face org-block))
  5163. ; for backend-specific code
  5164. t)
  5165. ((and (match-end 4) (equal dc3 "+begin"))
  5166. ;; Truly a block
  5167. (setq block-type (downcase (match-string 5))
  5168. quoting (member block-type org-protecting-blocks))
  5169. (when (re-search-forward
  5170. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5171. nil t) ;; on purpose, we look further than LIMIT
  5172. (setq end (min (point-max) (match-end 0))
  5173. end1 (min (point-max) (1- (match-beginning 0))))
  5174. (setq block-end (match-beginning 0))
  5175. (when quoting
  5176. (remove-text-properties beg end
  5177. '(display t invisible t intangible t)))
  5178. (add-text-properties
  5179. beg end
  5180. '(font-lock-fontified t font-lock-multiline t))
  5181. (add-text-properties beg beg1 '(face org-meta-line))
  5182. (add-text-properties end1 (min (point-max) (1+ end))
  5183. '(face org-meta-line)) ; for end_src
  5184. (cond
  5185. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5186. (org-src-font-lock-fontify-block lang block-start block-end)
  5187. ;; remove old background overlays
  5188. (mapc (lambda (ov)
  5189. (if (eq (overlay-get ov 'face) 'org-block-background)
  5190. (delete-overlay ov)))
  5191. (overlays-at (/ (+ beg1 block-end) 2)))
  5192. ;; add a background overlay
  5193. (setq ovl (make-overlay beg1 block-end))
  5194. (overlay-put ovl 'face 'org-block-background)
  5195. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5196. (quoting
  5197. (add-text-properties beg1 (min (point-max) (1+ end1))
  5198. '(face org-block))) ; end of source block
  5199. ((not org-fontify-quote-and-verse-blocks))
  5200. ((string= block-type "quote")
  5201. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5202. ((string= block-type "verse")
  5203. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5204. (add-text-properties beg beg1 '(face org-block-begin-line))
  5205. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5206. '(face org-block-end-line))
  5207. t))
  5208. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5209. (add-text-properties
  5210. beg (match-end 3)
  5211. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5212. '(font-lock-fontified t invisible t)
  5213. '(font-lock-fontified t face org-document-info-keyword)))
  5214. (add-text-properties
  5215. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5216. (if (string-equal dc1 "+title:")
  5217. '(font-lock-fontified t face org-document-title)
  5218. '(font-lock-fontified t face org-document-info))))
  5219. ((or (equal dc1 "+results")
  5220. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5221. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5222. "+call:" "+header:" "+headers:" "+name:"))
  5223. (and (match-end 4) (equal dc3 "+attr")))
  5224. (add-text-properties
  5225. beg (match-end 0)
  5226. '(font-lock-fontified t face org-meta-line))
  5227. t)
  5228. ((member dc3 '(" " ""))
  5229. (add-text-properties
  5230. beg (match-end 0)
  5231. '(font-lock-fontified t face font-lock-comment-face)))
  5232. ((not (member (char-after beg) '(?\ ?\t)))
  5233. ;; just any other in-buffer setting, but not indented
  5234. (add-text-properties
  5235. beg (match-end 0)
  5236. '(font-lock-fontified t face org-meta-line))
  5237. t)
  5238. (t nil))))))
  5239. (defun org-activate-angle-links (limit)
  5240. "Run through the buffer and add overlays to links."
  5241. (if (and (re-search-forward org-angle-link-re limit t)
  5242. (not (org-in-src-block-p)))
  5243. (progn
  5244. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5245. (add-text-properties (match-beginning 0) (match-end 0)
  5246. (list 'mouse-face 'highlight
  5247. 'keymap org-mouse-map))
  5248. (org-rear-nonsticky-at (match-end 0))
  5249. t)))
  5250. (defun org-activate-footnote-links (limit)
  5251. "Run through the buffer and add overlays to footnotes."
  5252. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5253. (when fn
  5254. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5255. (org-remove-flyspell-overlays-in beg end)
  5256. (add-text-properties beg end
  5257. (list 'mouse-face 'highlight
  5258. 'keymap org-mouse-map
  5259. 'help-echo
  5260. (if (= (point-at-bol) beg)
  5261. "Footnote definition"
  5262. "Footnote reference")
  5263. 'font-lock-fontified t
  5264. 'font-lock-multiline t
  5265. 'face 'org-footnote))))))
  5266. (defun org-activate-bracket-links (limit)
  5267. "Run through the buffer and add overlays to bracketed links."
  5268. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5269. (not (org-in-src-block-p)))
  5270. (let* ((hl (org-match-string-no-properties 1))
  5271. (help (concat "LINK: " hl))
  5272. ;; FIXME: Above we should remove the escapes. But that
  5273. ;; requires another match, protecting match data, a lot
  5274. ;; of overhead for font-lock.
  5275. (ip (org-maybe-intangible
  5276. (list 'invisible 'org-link
  5277. 'keymap org-mouse-map 'mouse-face 'highlight
  5278. 'font-lock-multiline t 'help-echo help
  5279. 'htmlize-link `(:uri ,hl))))
  5280. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5281. 'font-lock-multiline t 'help-echo help
  5282. 'htmlize-link `(:uri ,hl))))
  5283. ;; We need to remove the invisible property here. Table narrowing
  5284. ;; may have made some of this invisible.
  5285. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5286. (remove-text-properties (match-beginning 0) (match-end 0)
  5287. '(invisible nil))
  5288. (if (match-end 3)
  5289. (progn
  5290. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5291. (org-rear-nonsticky-at (match-beginning 3))
  5292. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5293. (org-rear-nonsticky-at (match-end 3))
  5294. (add-text-properties (match-end 3) (match-end 0) ip)
  5295. (org-rear-nonsticky-at (match-end 0)))
  5296. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5297. (org-rear-nonsticky-at (match-beginning 1))
  5298. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5299. (org-rear-nonsticky-at (match-end 1))
  5300. (add-text-properties (match-end 1) (match-end 0) ip)
  5301. (org-rear-nonsticky-at (match-end 0)))
  5302. t)))
  5303. (defun org-activate-dates (limit)
  5304. "Run through the buffer and add overlays to dates."
  5305. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5306. (not (equal (char-before (match-beginning 0)) 91)))
  5307. (progn
  5308. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5309. (add-text-properties (match-beginning 0) (match-end 0)
  5310. (list 'mouse-face 'highlight
  5311. 'keymap org-mouse-map))
  5312. (org-rear-nonsticky-at (match-end 0))
  5313. (when org-display-custom-times
  5314. (if (match-end 3)
  5315. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5316. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5317. t)))
  5318. (defvar org-target-link-regexp nil
  5319. "Regular expression matching radio targets in plain text.")
  5320. (make-variable-buffer-local 'org-target-link-regexp)
  5321. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5322. "Regular expression matching a link target.")
  5323. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5324. "Regular expression matching a radio target.")
  5325. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5326. "Regular expression matching any target.")
  5327. (defun org-activate-target-links (limit)
  5328. "Run through the buffer and add overlays to target matches."
  5329. (when org-target-link-regexp
  5330. (let ((case-fold-search t))
  5331. (if (re-search-forward org-target-link-regexp limit t)
  5332. (progn
  5333. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5334. (add-text-properties (match-beginning 0) (match-end 0)
  5335. (list 'mouse-face 'highlight
  5336. 'keymap org-mouse-map
  5337. 'help-echo "Radio target link"
  5338. 'org-linked-text t))
  5339. (org-rear-nonsticky-at (match-end 0))
  5340. t)))))
  5341. (defun org-update-radio-target-regexp ()
  5342. "Find all radio targets in this file and update the regular expression."
  5343. (interactive)
  5344. (when (memq 'radio org-activate-links)
  5345. (setq org-target-link-regexp
  5346. (org-make-target-link-regexp (org-all-targets 'radio)))
  5347. (org-restart-font-lock)))
  5348. (defun org-hide-wide-columns (limit)
  5349. (let (s e)
  5350. (setq s (text-property-any (point) (or limit (point-max))
  5351. 'org-cwidth t))
  5352. (when s
  5353. (setq e (next-single-property-change s 'org-cwidth))
  5354. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5355. (goto-char e)
  5356. t)))
  5357. (defvar org-latex-and-related-regexp nil
  5358. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5359. (defvar org-match-substring-regexp)
  5360. (defvar org-match-substring-with-braces-regexp)
  5361. (defun org-compute-latex-and-related-regexp ()
  5362. "Compute regular expression for LaTeX, entities and sub/superscript.
  5363. Result depends on variable `org-highlight-latex-and-related'."
  5364. (org-set-local
  5365. 'org-latex-and-related-regexp
  5366. (let* ((re-sub
  5367. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5368. ((eq org-use-sub-superscripts '{})
  5369. (list org-match-substring-with-braces-regexp))
  5370. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5371. (re-latex
  5372. (when (memq 'latex org-highlight-latex-and-related)
  5373. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5374. (delq nil
  5375. (mapcar (lambda (x)
  5376. (and (member (car x) matchers) (nth 1 x)))
  5377. org-latex-regexps)))))
  5378. (re-entities
  5379. (when (memq 'entities org-highlight-latex-and-related)
  5380. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5381. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5382. (defun org-do-latex-and-related (limit)
  5383. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5384. LIMIT bounds the search for syntax to highlight. Stop at first
  5385. highlighted object, if any. Return t if some highlighting was
  5386. done, nil otherwise."
  5387. (when (org-string-nw-p org-latex-and-related-regexp)
  5388. (catch 'found
  5389. (while (re-search-forward org-latex-and-related-regexp limit t)
  5390. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5391. 'face))
  5392. '(org-code org-verbatim underline))
  5393. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5394. '(?_ ?^))
  5395. 1
  5396. 0)))
  5397. (font-lock-prepend-text-property
  5398. (+ offset (match-beginning 0)) (match-end 0)
  5399. 'face 'org-latex-and-related)
  5400. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5401. '(font-lock-multiline t)))
  5402. (throw 'found t)))
  5403. nil)))
  5404. (defun org-restart-font-lock ()
  5405. "Restart `font-lock-mode', to force refontification."
  5406. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5407. (font-lock-mode -1)
  5408. (font-lock-mode 1)))
  5409. (defun org-all-targets (&optional radio)
  5410. "Return a list of all targets in this file.
  5411. When optional argument RADIO is non-nil, only find radio
  5412. targets."
  5413. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5414. (save-excursion
  5415. (goto-char (point-min))
  5416. (while (re-search-forward re nil t)
  5417. ;; Make sure point is really within the object.
  5418. (backward-char)
  5419. (let ((obj (org-element-context)))
  5420. (when (memq (org-element-type obj) '(radio-target target))
  5421. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5422. rtn)))
  5423. (defun org-make-target-link-regexp (targets)
  5424. "Make regular expression matching all strings in TARGETS.
  5425. The regular expression finds the targets also if there is a line break
  5426. between words."
  5427. (and targets
  5428. (concat
  5429. "\\<\\("
  5430. (mapconcat
  5431. (lambda (x)
  5432. (setq x (regexp-quote x))
  5433. (while (string-match " +" x)
  5434. (setq x (replace-match "\\s-+" t t x)))
  5435. x)
  5436. targets
  5437. "\\|")
  5438. "\\)\\>")))
  5439. (defun org-activate-tags (limit)
  5440. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5441. (progn
  5442. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5443. (add-text-properties (match-beginning 1) (match-end 1)
  5444. (list 'mouse-face 'highlight
  5445. 'keymap org-mouse-map))
  5446. (org-rear-nonsticky-at (match-end 1))
  5447. t)))
  5448. (defun org-outline-level ()
  5449. "Compute the outline level of the heading at point.
  5450. If this is called at a normal headline, the level is the number of stars.
  5451. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5452. (save-excursion
  5453. (if (not (condition-case nil
  5454. (org-back-to-heading t)
  5455. (error nil)))
  5456. 0
  5457. (looking-at org-outline-regexp)
  5458. (1- (- (match-end 0) (match-beginning 0))))))
  5459. (defvar org-font-lock-keywords nil)
  5460. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5461. "Regular expression matching a property line.")
  5462. (defvar org-font-lock-hook nil
  5463. "Functions to be called for special font lock stuff.")
  5464. (defvar org-font-lock-set-keywords-hook nil
  5465. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5466. This is called after `org-font-lock-extra-keywords' is defined, but before
  5467. it is installed to be used by font lock. This can be useful if something
  5468. needs to be inserted at a specific position in the font-lock sequence.")
  5469. (defun org-font-lock-hook (limit)
  5470. "Run `org-font-lock-hook' within LIMIT."
  5471. (run-hook-with-args 'org-font-lock-hook limit))
  5472. (defun org-set-font-lock-defaults ()
  5473. "Set font lock defaults for the current buffer."
  5474. (let* ((em org-fontify-emphasized-text)
  5475. (lk org-activate-links)
  5476. (org-font-lock-extra-keywords
  5477. (list
  5478. ;; Call the hook
  5479. '(org-font-lock-hook)
  5480. ;; Headlines
  5481. `(,(if org-fontify-whole-heading-line
  5482. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5483. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5484. (1 (org-get-level-face 1))
  5485. (2 (org-get-level-face 2))
  5486. (3 (org-get-level-face 3)))
  5487. ;; Table lines
  5488. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5489. (1 'org-table t))
  5490. ;; Table internals
  5491. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5492. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5493. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5494. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5495. ;; Drawers
  5496. (list org-drawer-regexp '(0 'org-special-keyword t))
  5497. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5498. ;; Properties
  5499. (list org-property-re
  5500. '(1 'org-special-keyword t)
  5501. '(3 'org-property-value t))
  5502. ;; Links
  5503. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5504. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5505. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5506. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5507. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5508. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5509. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5510. ;; Targets.
  5511. (list org-any-target-regexp '(0 'org-target t))
  5512. ;; Diary sexps.
  5513. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5514. ;; Macro
  5515. '("{{{.+}}}" (0 'org-macro t))
  5516. '(org-hide-wide-columns (0 nil append))
  5517. ;; TODO keyword
  5518. (list (format org-heading-keyword-regexp-format
  5519. org-todo-regexp)
  5520. '(2 (org-get-todo-face 2) t))
  5521. ;; DONE
  5522. (if org-fontify-done-headline
  5523. (list (format org-heading-keyword-regexp-format
  5524. (concat
  5525. "\\(?:"
  5526. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5527. "\\)"))
  5528. '(2 'org-headline-done t))
  5529. nil)
  5530. ;; Priorities
  5531. '(org-font-lock-add-priority-faces)
  5532. ;; Tags
  5533. '(org-font-lock-add-tag-faces)
  5534. ;; Tags groups
  5535. (if (and org-group-tags org-tag-groups-alist)
  5536. (list (concat org-outline-regexp-bol ".+\\(:"
  5537. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5538. ":\\).*$")
  5539. '(1 'org-tag-group prepend)))
  5540. ;; Special keywords
  5541. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5542. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5543. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5544. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5545. ;; Emphasis
  5546. (if em
  5547. (if (featurep 'xemacs)
  5548. '(org-do-emphasis-faces (0 nil append))
  5549. '(org-do-emphasis-faces)))
  5550. ;; Checkboxes
  5551. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5552. 1 'org-checkbox prepend)
  5553. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5554. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5555. (0 (org-get-checkbox-statistics-face) t)))
  5556. ;; Description list items
  5557. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5558. 1 'org-list-dt prepend)
  5559. ;; ARCHIVEd headings
  5560. (list (concat
  5561. org-outline-regexp-bol
  5562. "\\(.*:" org-archive-tag ":.*\\)")
  5563. '(1 'org-archived prepend))
  5564. ;; Specials
  5565. '(org-do-latex-and-related)
  5566. '(org-fontify-entities)
  5567. '(org-raise-scripts)
  5568. ;; Code
  5569. '(org-activate-code (1 'org-code t))
  5570. ;; COMMENT
  5571. (list (format org-heading-keyword-regexp-format
  5572. (concat "\\("
  5573. org-comment-string "\\|" org-quote-string
  5574. "\\)"))
  5575. '(2 'org-special-keyword t))
  5576. ;; Blocks and meta lines
  5577. '(org-fontify-meta-lines-and-blocks)
  5578. )))
  5579. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5580. (run-hooks 'org-font-lock-set-keywords-hook)
  5581. ;; Now set the full font-lock-keywords
  5582. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5583. (org-set-local 'font-lock-defaults
  5584. '(org-font-lock-keywords t nil nil backward-paragraph))
  5585. (kill-local-variable 'font-lock-keywords) nil))
  5586. (defun org-toggle-pretty-entities ()
  5587. "Toggle the composition display of entities as UTF8 characters."
  5588. (interactive)
  5589. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5590. (org-restart-font-lock)
  5591. (if org-pretty-entities
  5592. (message "Entities are now displayed as UTF8 characters")
  5593. (save-restriction
  5594. (widen)
  5595. (org-decompose-region (point-min) (point-max))
  5596. (message "Entities are now displayed as plain text"))))
  5597. (defvar org-custom-properties-overlays nil
  5598. "List of overlays used for custom properties.")
  5599. (make-variable-buffer-local 'org-custom-properties-overlays)
  5600. (defun org-toggle-custom-properties-visibility ()
  5601. "Display or hide properties in `org-custom-properties'."
  5602. (interactive)
  5603. (if org-custom-properties-overlays
  5604. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5605. (setq org-custom-properties-overlays nil))
  5606. (unless (not org-custom-properties)
  5607. (save-excursion
  5608. (save-restriction
  5609. (widen)
  5610. (goto-char (point-min))
  5611. (while (re-search-forward org-property-re nil t)
  5612. (mapc (lambda(p)
  5613. (when (equal p (substring (match-string 1) 1 -1))
  5614. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5615. (overlay-put o 'invisible t)
  5616. (overlay-put o 'org-custom-property t)
  5617. (push o org-custom-properties-overlays))))
  5618. org-custom-properties)))))))
  5619. (defun org-fontify-entities (limit)
  5620. "Find an entity to fontify."
  5621. (let (ee)
  5622. (when org-pretty-entities
  5623. (catch 'match
  5624. (while (re-search-forward
  5625. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5626. limit t)
  5627. (if (and (not (org-in-indented-comment-line))
  5628. (setq ee (org-entity-get (match-string 1)))
  5629. (= (length (nth 6 ee)) 1))
  5630. (let*
  5631. ((end (if (equal (match-string 2) "{}")
  5632. (match-end 2)
  5633. (match-end 1))))
  5634. (add-text-properties
  5635. (match-beginning 0) end
  5636. (list 'font-lock-fontified t))
  5637. (compose-region (match-beginning 0) end
  5638. (nth 6 ee) nil)
  5639. (backward-char 1)
  5640. (throw 'match t))))
  5641. nil))))
  5642. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5643. "Fontify string S like in Org-mode."
  5644. (with-temp-buffer
  5645. (insert s)
  5646. (let ((org-odd-levels-only odd-levels))
  5647. (org-mode)
  5648. (font-lock-fontify-buffer)
  5649. (buffer-string))))
  5650. (defvar org-m nil)
  5651. (defvar org-l nil)
  5652. (defvar org-f nil)
  5653. (defun org-get-level-face (n)
  5654. "Get the right face for match N in font-lock matching of headlines."
  5655. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5656. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5657. (if org-cycle-level-faces
  5658. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5659. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5660. (cond
  5661. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5662. ((eq n 2) org-f)
  5663. (t (if org-level-color-stars-only nil org-f))))
  5664. (defun org-get-todo-face (kwd)
  5665. "Get the right face for a TODO keyword KWD.
  5666. If KWD is a number, get the corresponding match group."
  5667. (if (numberp kwd) (setq kwd (match-string kwd)))
  5668. (or (org-face-from-face-or-color
  5669. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5670. (and (member kwd org-done-keywords) 'org-done)
  5671. 'org-todo))
  5672. (defun org-face-from-face-or-color (context inherit face-or-color)
  5673. "Create a face list that inherits INHERIT, but sets the foreground color.
  5674. When FACE-OR-COLOR is not a string, just return it."
  5675. (if (stringp face-or-color)
  5676. (list :inherit inherit
  5677. (cdr (assoc context org-faces-easy-properties))
  5678. face-or-color)
  5679. face-or-color))
  5680. (defun org-font-lock-add-tag-faces (limit)
  5681. "Add the special tag faces."
  5682. (when (and org-tag-faces org-tags-special-faces-re)
  5683. (while (re-search-forward org-tags-special-faces-re limit t)
  5684. (add-text-properties (match-beginning 1) (match-end 1)
  5685. (list 'face (org-get-tag-face 1)
  5686. 'font-lock-fontified t))
  5687. (backward-char 1))))
  5688. (defun org-font-lock-add-priority-faces (limit)
  5689. "Add the special priority faces."
  5690. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5691. (when (save-match-data (org-at-heading-p))
  5692. (add-text-properties
  5693. (match-beginning 0) (match-end 0)
  5694. (list 'face (or (org-face-from-face-or-color
  5695. 'priority 'org-priority
  5696. (cdr (assoc (char-after (match-beginning 1))
  5697. org-priority-faces)))
  5698. 'org-priority)
  5699. 'font-lock-fontified t)))))
  5700. (defun org-get-tag-face (kwd)
  5701. "Get the right face for a TODO keyword KWD.
  5702. If KWD is a number, get the corresponding match group."
  5703. (if (numberp kwd) (setq kwd (match-string kwd)))
  5704. (or (org-face-from-face-or-color
  5705. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5706. 'org-tag))
  5707. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5708. "Remove fontification and activation overlays from links."
  5709. (font-lock-default-unfontify-region beg end)
  5710. (let* ((buffer-undo-list t)
  5711. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5712. (inhibit-modification-hooks t)
  5713. deactivate-mark buffer-file-name buffer-file-truename)
  5714. (org-decompose-region beg end)
  5715. (remove-text-properties beg end
  5716. '(mouse-face t keymap t org-linked-text t
  5717. invisible t intangible t
  5718. org-no-flyspell t org-emphasis t))
  5719. (org-remove-font-lock-display-properties beg end)))
  5720. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5721. ((raise 0.3) (height 0.7))
  5722. ((raise -0.5))
  5723. ((raise 0.5)))
  5724. "Display properties for showing superscripts and subscripts.")
  5725. (defun org-remove-font-lock-display-properties (beg end)
  5726. "Remove specific display properties that have been added by font lock.
  5727. The will remove the raise properties that are used to show superscripts
  5728. and subscripts."
  5729. (let (next prop)
  5730. (while (< beg end)
  5731. (setq next (next-single-property-change beg 'display nil end)
  5732. prop (get-text-property beg 'display))
  5733. (if (member prop org-script-display)
  5734. (put-text-property beg next 'display nil))
  5735. (setq beg next))))
  5736. (defun org-raise-scripts (limit)
  5737. "Add raise properties to sub/superscripts."
  5738. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5739. (if (re-search-forward
  5740. (if (eq org-use-sub-superscripts t)
  5741. org-match-substring-regexp
  5742. org-match-substring-with-braces-regexp)
  5743. limit t)
  5744. (let* ((pos (point)) table-p comment-p
  5745. (mpos (match-beginning 3))
  5746. (emph-p (get-text-property mpos 'org-emphasis))
  5747. (link-p (get-text-property mpos 'mouse-face))
  5748. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5749. (goto-char (point-at-bol))
  5750. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5751. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5752. (goto-char pos)
  5753. ;; Handle a_b^c
  5754. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5755. (if (or comment-p emph-p link-p keyw-p)
  5756. t
  5757. (put-text-property (match-beginning 3) (match-end 0)
  5758. 'display
  5759. (if (equal (char-after (match-beginning 2)) ?^)
  5760. (nth (if table-p 3 1) org-script-display)
  5761. (nth (if table-p 2 0) org-script-display)))
  5762. (add-text-properties (match-beginning 2) (match-end 2)
  5763. (list 'invisible t
  5764. 'org-dwidth t 'org-dwidth-n 1))
  5765. (if (and (eq (char-after (match-beginning 3)) ?{)
  5766. (eq (char-before (match-end 3)) ?}))
  5767. (progn
  5768. (add-text-properties
  5769. (match-beginning 3) (1+ (match-beginning 3))
  5770. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5771. (add-text-properties
  5772. (1- (match-end 3)) (match-end 3)
  5773. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5774. t)))))
  5775. ;;;; Visibility cycling, including org-goto and indirect buffer
  5776. ;;; Cycling
  5777. (defvar org-cycle-global-status nil)
  5778. (make-variable-buffer-local 'org-cycle-global-status)
  5779. (put 'org-cycle-global-status 'org-state t)
  5780. (defvar org-cycle-subtree-status nil)
  5781. (make-variable-buffer-local 'org-cycle-subtree-status)
  5782. (put 'org-cycle-subtree-status 'org-state t)
  5783. (defvar org-inlinetask-min-level)
  5784. ;;;###autoload
  5785. (defun org-cycle (&optional arg)
  5786. "TAB-action and visibility cycling for Org-mode.
  5787. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5788. is outline visibility cycling, but it also invokes other actions
  5789. in special contexts.
  5790. - When this function is called with a prefix argument, rotate the entire
  5791. buffer through 3 states (global cycling)
  5792. 1. OVERVIEW: Show only top-level headlines.
  5793. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5794. 3. SHOW ALL: Show everything.
  5795. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5796. determined by the variable `org-startup-folded', and by any VISIBILITY
  5797. properties in the buffer.
  5798. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5799. including any drawers.
  5800. - When inside a table, re-align the table and move to the next field.
  5801. - When point is at the beginning of a headline, rotate the subtree started
  5802. by this line through 3 different states (local cycling)
  5803. 1. FOLDED: Only the main headline is shown.
  5804. 2. CHILDREN: The main headline and the direct children are shown.
  5805. From this state, you can move to one of the children
  5806. and zoom in further.
  5807. 3. SUBTREE: Show the entire subtree, including body text.
  5808. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5809. - When point is at the beginning of an empty headline and the variable
  5810. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5811. of the headline by demoting and promoting it to likely levels. This
  5812. speeds up creation document structure by pressing TAB once or several
  5813. times right after creating a new headline.
  5814. - When there is a numeric prefix, go up to a heading with level ARG, do
  5815. a `show-subtree' and return to the previous cursor position. If ARG
  5816. is negative, go up that many levels.
  5817. - When point is not at the beginning of a headline, execute the global
  5818. binding for TAB, which is re-indenting the line. See the option
  5819. `org-cycle-emulate-tab' for details.
  5820. - Special case: if point is at the beginning of the buffer and there is
  5821. no headline in line 1, this function will act as if called with prefix arg
  5822. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5823. But only if also the variable `org-cycle-global-at-bob' is t."
  5824. (interactive "P")
  5825. (org-load-modules-maybe)
  5826. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5827. (and org-cycle-level-after-item/entry-creation
  5828. (or (org-cycle-level)
  5829. (org-cycle-item-indentation))))
  5830. (let* (message-log-max ; Don't populate the *Messages* buffer
  5831. (limit-level
  5832. (or org-cycle-max-level
  5833. (and (boundp 'org-inlinetask-min-level)
  5834. org-inlinetask-min-level
  5835. (1- org-inlinetask-min-level))))
  5836. (nstars (and limit-level
  5837. (if org-odd-levels-only
  5838. (and limit-level (1- (* limit-level 2)))
  5839. limit-level)))
  5840. (org-outline-regexp
  5841. (if (not (derived-mode-p 'org-mode))
  5842. outline-regexp
  5843. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5844. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5845. (not (looking-at org-outline-regexp))))
  5846. (org-cycle-hook
  5847. (if bob-special
  5848. (delq 'org-optimize-window-after-visibility-change
  5849. (copy-sequence org-cycle-hook))
  5850. org-cycle-hook))
  5851. (pos (point)))
  5852. (if (or bob-special (equal arg '(4)))
  5853. ;; special case: use global cycling
  5854. (setq arg t))
  5855. (cond
  5856. ((equal arg '(16))
  5857. (setq last-command 'dummy)
  5858. (org-set-startup-visibility)
  5859. (message "Startup visibility, plus VISIBILITY properties"))
  5860. ((equal arg '(64))
  5861. (show-all)
  5862. (message "Entire buffer visible, including drawers"))
  5863. ;; Table: enter it or move to the next field.
  5864. ((org-at-table-p 'any)
  5865. (if (org-at-table.el-p)
  5866. (message "Use C-c ' to edit table.el tables")
  5867. (if arg (org-table-edit-field t)
  5868. (org-table-justify-field-maybe)
  5869. (call-interactively 'org-table-next-field))))
  5870. ((run-hook-with-args-until-success
  5871. 'org-tab-after-check-for-table-hook))
  5872. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5873. ((eq arg t) (org-cycle-internal-global))
  5874. ;; Drawers: delegate to `org-flag-drawer'.
  5875. ((and org-drawers org-drawer-regexp
  5876. (save-excursion
  5877. (beginning-of-line 1)
  5878. (looking-at org-drawer-regexp)))
  5879. (org-flag-drawer ; toggle block visibility
  5880. (not (get-char-property (match-end 0) 'invisible))))
  5881. ;; Show-subtree, ARG levels up from here.
  5882. ((integerp arg)
  5883. (save-excursion
  5884. (org-back-to-heading)
  5885. (outline-up-heading (if (< arg 0) (- arg)
  5886. (- (funcall outline-level) arg)))
  5887. (org-show-subtree)))
  5888. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5889. ((and (featurep 'org-inlinetask)
  5890. (org-inlinetask-at-task-p)
  5891. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5892. (org-inlinetask-toggle-visibility))
  5893. ((org-try-cdlatex-tab))
  5894. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5895. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5896. (save-excursion (beginning-of-line 1)
  5897. (looking-at org-outline-regexp)))
  5898. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5899. (org-cycle-internal-local))
  5900. ;; From there: TAB emulation and template completion.
  5901. (buffer-read-only (org-back-to-heading))
  5902. ((run-hook-with-args-until-success
  5903. 'org-tab-after-check-for-cycling-hook))
  5904. ((org-try-structure-completion))
  5905. ((run-hook-with-args-until-success
  5906. 'org-tab-before-tab-emulation-hook))
  5907. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5908. (or (not (bolp))
  5909. (not (looking-at org-outline-regexp))))
  5910. (call-interactively (global-key-binding "\t")))
  5911. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5912. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5913. (or (and (eq org-cycle-emulate-tab 'white)
  5914. (= (match-end 0) (point-at-eol)))
  5915. (and (eq org-cycle-emulate-tab 'whitestart)
  5916. (>= (match-end 0) pos))))
  5917. t
  5918. (eq org-cycle-emulate-tab t))
  5919. (call-interactively (global-key-binding "\t")))
  5920. (t (save-excursion
  5921. (org-back-to-heading)
  5922. (org-cycle)))))))
  5923. (defun org-cycle-internal-global ()
  5924. "Do the global cycling action."
  5925. ;; Hack to avoid display of messages for .org attachments in Gnus
  5926. (let (message-log-max ; Don't populate the *Messages* buffer
  5927. (ga (string-match "\\*fontification" (buffer-name))))
  5928. (cond
  5929. ((and (eq last-command this-command)
  5930. (eq org-cycle-global-status 'overview))
  5931. ;; We just created the overview - now do table of contents
  5932. ;; This can be slow in very large buffers, so indicate action
  5933. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5934. (unless ga (message "CONTENTS..."))
  5935. (org-content)
  5936. (unless ga (message "CONTENTS...done"))
  5937. (setq org-cycle-global-status 'contents)
  5938. (run-hook-with-args 'org-cycle-hook 'contents))
  5939. ((and (eq last-command this-command)
  5940. (eq org-cycle-global-status 'contents))
  5941. ;; We just showed the table of contents - now show everything
  5942. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5943. (show-all)
  5944. (unless ga (message "SHOW ALL"))
  5945. (setq org-cycle-global-status 'all)
  5946. (run-hook-with-args 'org-cycle-hook 'all))
  5947. (t
  5948. ;; Default action: go to overview
  5949. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5950. (org-overview)
  5951. (unless ga (message "OVERVIEW"))
  5952. (setq org-cycle-global-status 'overview)
  5953. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5954. (defun org-cycle-internal-local ()
  5955. "Do the local cycling action."
  5956. (let (message-log-max ; Don't populate the *Messages* buffer
  5957. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5958. ;; First, determine end of headline (EOH), end of subtree or item
  5959. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5960. (save-excursion
  5961. (if (org-at-item-p)
  5962. (progn
  5963. (beginning-of-line)
  5964. (setq struct (org-list-struct))
  5965. (setq eoh (point-at-eol))
  5966. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5967. (setq has-children (org-list-has-child-p (point) struct)))
  5968. (org-back-to-heading)
  5969. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5970. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5971. (setq has-children
  5972. (or (save-excursion
  5973. (let ((level (funcall outline-level)))
  5974. (outline-next-heading)
  5975. (and (org-at-heading-p t)
  5976. (> (funcall outline-level) level))))
  5977. (save-excursion
  5978. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5979. ;; Determine end invisible part of buffer (EOL)
  5980. (beginning-of-line 2)
  5981. ;; XEmacs doesn't have `next-single-char-property-change'
  5982. (if (featurep 'xemacs)
  5983. (while (and (not (eobp)) ;; this is like `next-line'
  5984. (get-char-property (1- (point)) 'invisible))
  5985. (beginning-of-line 2))
  5986. (while (and (not (eobp)) ;; this is like `next-line'
  5987. (get-char-property (1- (point)) 'invisible))
  5988. (goto-char (next-single-char-property-change (point) 'invisible))
  5989. (and (eolp) (beginning-of-line 2))))
  5990. (setq eol (point)))
  5991. ;; Find out what to do next and set `this-command'
  5992. (cond
  5993. ((= eos eoh)
  5994. ;; Nothing is hidden behind this heading
  5995. (unless (org-before-first-heading-p)
  5996. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5997. (message "EMPTY ENTRY")
  5998. (setq org-cycle-subtree-status nil)
  5999. (save-excursion
  6000. (goto-char eos)
  6001. (outline-next-heading)
  6002. (if (outline-invisible-p) (org-flag-heading nil))))
  6003. ((and (or (>= eol eos)
  6004. (not (string-match "\\S-" (buffer-substring eol eos))))
  6005. (or has-children
  6006. (not (setq children-skipped
  6007. org-cycle-skip-children-state-if-no-children))))
  6008. ;; Entire subtree is hidden in one line: children view
  6009. (unless (org-before-first-heading-p)
  6010. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6011. (if (org-at-item-p)
  6012. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6013. (org-show-entry)
  6014. (org-with-limited-levels (show-children))
  6015. ;; FIXME: This slows down the func way too much.
  6016. ;; How keep drawers hidden in subtree anyway?
  6017. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6018. ;; (org-cycle-hide-drawers 'subtree))
  6019. ;; Fold every list in subtree to top-level items.
  6020. (when (eq org-cycle-include-plain-lists 'integrate)
  6021. (save-excursion
  6022. (org-back-to-heading)
  6023. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6024. (beginning-of-line 1)
  6025. (let* ((struct (org-list-struct))
  6026. (prevs (org-list-prevs-alist struct))
  6027. (end (org-list-get-bottom-point struct)))
  6028. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6029. (org-list-get-all-items (point) struct prevs))
  6030. (goto-char (if (< end eos) end eos)))))))
  6031. (message "CHILDREN")
  6032. (save-excursion
  6033. (goto-char eos)
  6034. (outline-next-heading)
  6035. (if (outline-invisible-p) (org-flag-heading nil)))
  6036. (setq org-cycle-subtree-status 'children)
  6037. (unless (org-before-first-heading-p)
  6038. (run-hook-with-args 'org-cycle-hook 'children)))
  6039. ((or children-skipped
  6040. (and (eq last-command this-command)
  6041. (eq org-cycle-subtree-status 'children)))
  6042. ;; We just showed the children, or no children are there,
  6043. ;; now show everything.
  6044. (unless (org-before-first-heading-p)
  6045. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6046. (outline-flag-region eoh eos nil)
  6047. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6048. (setq org-cycle-subtree-status 'subtree)
  6049. (unless (org-before-first-heading-p)
  6050. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6051. (t
  6052. ;; Default action: hide the subtree.
  6053. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6054. (outline-flag-region eoh eos t)
  6055. (message "FOLDED")
  6056. (setq org-cycle-subtree-status 'folded)
  6057. (unless (org-before-first-heading-p)
  6058. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6059. ;;;###autoload
  6060. (defun org-global-cycle (&optional arg)
  6061. "Cycle the global visibility. For details see `org-cycle'.
  6062. With \\[universal-argument] prefix arg, switch to startup visibility.
  6063. With a numeric prefix, show all headlines up to that level."
  6064. (interactive "P")
  6065. (let ((org-cycle-include-plain-lists
  6066. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6067. (cond
  6068. ((integerp arg)
  6069. (show-all)
  6070. (hide-sublevels arg)
  6071. (setq org-cycle-global-status 'contents))
  6072. ((equal arg '(4))
  6073. (org-set-startup-visibility)
  6074. (message "Startup visibility, plus VISIBILITY properties."))
  6075. (t
  6076. (org-cycle '(4))))))
  6077. (defun org-set-startup-visibility ()
  6078. "Set the visibility required by startup options and properties."
  6079. (cond
  6080. ((eq org-startup-folded t)
  6081. (org-cycle '(4)))
  6082. ((eq org-startup-folded 'content)
  6083. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6084. (org-cycle '(4)) (org-cycle '(4)))))
  6085. (unless (eq org-startup-folded 'showeverything)
  6086. (if org-hide-block-startup (org-hide-block-all))
  6087. (org-set-visibility-according-to-property 'no-cleanup)
  6088. (org-cycle-hide-archived-subtrees 'all)
  6089. (org-cycle-hide-drawers 'all)
  6090. (org-cycle-show-empty-lines t)))
  6091. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6092. "Switch subtree visibilities according to :VISIBILITY: property."
  6093. (interactive)
  6094. (let (org-show-entry-below state)
  6095. (save-excursion
  6096. (goto-char (point-min))
  6097. (while (re-search-forward
  6098. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6099. nil t)
  6100. (setq state (match-string 1))
  6101. (save-excursion
  6102. (org-back-to-heading t)
  6103. (hide-subtree)
  6104. (org-reveal)
  6105. (cond
  6106. ((equal state '("fold" "folded"))
  6107. (hide-subtree))
  6108. ((equal state "children")
  6109. (org-show-hidden-entry)
  6110. (show-children))
  6111. ((equal state "content")
  6112. (save-excursion
  6113. (save-restriction
  6114. (org-narrow-to-subtree)
  6115. (org-content))))
  6116. ((member state '("all" "showall"))
  6117. (show-subtree)))))
  6118. (unless no-cleanup
  6119. (org-cycle-hide-archived-subtrees 'all)
  6120. (org-cycle-hide-drawers 'all)
  6121. (org-cycle-show-empty-lines 'all)))))
  6122. ;; This function uses outline-regexp instead of the more fundamental
  6123. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6124. ;; buffers, where outline-regexp is needed.
  6125. (defun org-overview ()
  6126. "Switch to overview mode, showing only top-level headlines.
  6127. Really, this shows all headlines with level equal or greater than the level
  6128. of the first headline in the buffer. This is important, because if the
  6129. first headline is not level one, then (hide-sublevels 1) gives confusing
  6130. results."
  6131. (interactive)
  6132. (let ((l (org-current-line))
  6133. (level (save-excursion
  6134. (goto-char (point-min))
  6135. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6136. (progn
  6137. (goto-char (match-beginning 0))
  6138. (funcall outline-level))))))
  6139. (and level (hide-sublevels level))
  6140. (recenter '(4))
  6141. (org-goto-line l)))
  6142. (defun org-content (&optional arg)
  6143. "Show all headlines in the buffer, like a table of contents.
  6144. With numerical argument N, show content up to level N."
  6145. (interactive "P")
  6146. (save-excursion
  6147. ;; Visit all headings and show their offspring
  6148. (and (integerp arg) (org-overview))
  6149. (goto-char (point-max))
  6150. (catch 'exit
  6151. (while (and (progn (condition-case nil
  6152. (outline-previous-visible-heading 1)
  6153. (error (goto-char (point-min))))
  6154. t)
  6155. (looking-at org-outline-regexp))
  6156. (if (integerp arg)
  6157. (show-children (1- arg))
  6158. (show-branches))
  6159. (if (bobp) (throw 'exit nil))))))
  6160. (defun org-optimize-window-after-visibility-change (state)
  6161. "Adjust the window after a change in outline visibility.
  6162. This function is the default value of the hook `org-cycle-hook'."
  6163. (when (get-buffer-window (current-buffer))
  6164. (cond
  6165. ((eq state 'content) nil)
  6166. ((eq state 'all) nil)
  6167. ((eq state 'folded) nil)
  6168. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6169. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6170. (defun org-remove-empty-overlays-at (pos)
  6171. "Remove outline overlays that do not contain non-white stuff."
  6172. (mapc
  6173. (lambda (o)
  6174. (and (eq 'outline (overlay-get o 'invisible))
  6175. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6176. (overlay-end o))))
  6177. (delete-overlay o)))
  6178. (overlays-at pos)))
  6179. (defun org-clean-visibility-after-subtree-move ()
  6180. "Fix visibility issues after moving a subtree."
  6181. ;; First, find a reasonable region to look at:
  6182. ;; Start two siblings above, end three below
  6183. (let* ((beg (save-excursion
  6184. (and (org-get-last-sibling)
  6185. (org-get-last-sibling))
  6186. (point)))
  6187. (end (save-excursion
  6188. (and (org-get-next-sibling)
  6189. (org-get-next-sibling)
  6190. (org-get-next-sibling))
  6191. (if (org-at-heading-p)
  6192. (point-at-eol)
  6193. (point))))
  6194. (level (looking-at "\\*+"))
  6195. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6196. (save-excursion
  6197. (save-restriction
  6198. (narrow-to-region beg end)
  6199. (when re
  6200. ;; Properly fold already folded siblings
  6201. (goto-char (point-min))
  6202. (while (re-search-forward re nil t)
  6203. (if (and (not (outline-invisible-p))
  6204. (save-excursion
  6205. (goto-char (point-at-eol)) (outline-invisible-p)))
  6206. (hide-entry))))
  6207. (org-cycle-show-empty-lines 'overview)
  6208. (org-cycle-hide-drawers 'overview)))))
  6209. (defun org-cycle-show-empty-lines (state)
  6210. "Show empty lines above all visible headlines.
  6211. The region to be covered depends on STATE when called through
  6212. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6213. entire buffer covered. Note that an empty line is only shown if there
  6214. are at least `org-cycle-separator-lines' empty lines before the headline."
  6215. (when (not (= org-cycle-separator-lines 0))
  6216. (save-excursion
  6217. (let* ((n (abs org-cycle-separator-lines))
  6218. (re (cond
  6219. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6220. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6221. (t (let ((ns (number-to-string (- n 2))))
  6222. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6223. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6224. beg end b e)
  6225. (cond
  6226. ((memq state '(overview contents t))
  6227. (setq beg (point-min) end (point-max)))
  6228. ((memq state '(children folded))
  6229. (setq beg (point) end (progn (org-end-of-subtree t t)
  6230. (beginning-of-line 2)
  6231. (point)))))
  6232. (when beg
  6233. (goto-char beg)
  6234. (while (re-search-forward re end t)
  6235. (unless (get-char-property (match-end 1) 'invisible)
  6236. (setq e (match-end 1))
  6237. (if (< org-cycle-separator-lines 0)
  6238. (setq b (save-excursion
  6239. (goto-char (match-beginning 0))
  6240. (org-back-over-empty-lines)
  6241. (if (save-excursion
  6242. (goto-char (max (point-min) (1- (point))))
  6243. (org-at-heading-p))
  6244. (1- (point))
  6245. (point))))
  6246. (setq b (match-beginning 1)))
  6247. (outline-flag-region b e nil)))))))
  6248. ;; Never hide empty lines at the end of the file.
  6249. (save-excursion
  6250. (goto-char (point-max))
  6251. (outline-previous-heading)
  6252. (outline-end-of-heading)
  6253. (if (and (looking-at "[ \t\n]+")
  6254. (= (match-end 0) (point-max)))
  6255. (outline-flag-region (point) (match-end 0) nil))))
  6256. (defun org-show-empty-lines-in-parent ()
  6257. "Move to the parent and re-show empty lines before visible headlines."
  6258. (save-excursion
  6259. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6260. (org-cycle-show-empty-lines context))))
  6261. (defun org-files-list ()
  6262. "Return `org-agenda-files' list, plus all open org-mode files.
  6263. This is useful for operations that need to scan all of a user's
  6264. open and agenda-wise Org files."
  6265. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6266. (dolist (buf (buffer-list))
  6267. (with-current-buffer buf
  6268. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6269. (let ((file (expand-file-name (buffer-file-name))))
  6270. (unless (member file files)
  6271. (push file files))))))
  6272. files))
  6273. (defsubst org-entry-beginning-position ()
  6274. "Return the beginning position of the current entry."
  6275. (save-excursion (outline-back-to-heading t) (point)))
  6276. (defsubst org-entry-end-position ()
  6277. "Return the end position of the current entry."
  6278. (save-excursion (outline-next-heading) (point)))
  6279. (defun org-cycle-hide-drawers (state)
  6280. "Re-hide all drawers after a visibility state change."
  6281. (when (and (derived-mode-p 'org-mode)
  6282. (not (memq state '(overview folded contents))))
  6283. (save-excursion
  6284. (let* ((globalp (memq state '(contents all)))
  6285. (beg (if globalp (point-min) (point)))
  6286. (end (if globalp (point-max)
  6287. (if (eq state 'children)
  6288. (save-excursion (outline-next-heading) (point))
  6289. (org-end-of-subtree t)))))
  6290. (goto-char beg)
  6291. (while (re-search-forward org-drawer-regexp end t)
  6292. (org-flag-drawer t))))))
  6293. (defun org-cycle-hide-inline-tasks (state)
  6294. "Re-hide inline task when switching to 'contents visibility state."
  6295. (when (and (eq state 'contents)
  6296. (boundp 'org-inlinetask-min-level)
  6297. org-inlinetask-min-level)
  6298. (hide-sublevels (1- org-inlinetask-min-level))))
  6299. (defun org-flag-drawer (flag)
  6300. "When FLAG is non-nil, hide the drawer we are within.
  6301. Otherwise make it visible."
  6302. (save-excursion
  6303. (beginning-of-line 1)
  6304. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6305. (let ((b (match-end 0)))
  6306. (if (re-search-forward
  6307. "^[ \t]*:END:"
  6308. (save-excursion (outline-next-heading) (point)) t)
  6309. (outline-flag-region b (point-at-eol) flag)
  6310. (error ":END: line missing at position %s" b))))))
  6311. (defun org-subtree-end-visible-p ()
  6312. "Is the end of the current subtree visible?"
  6313. (pos-visible-in-window-p
  6314. (save-excursion (org-end-of-subtree t) (point))))
  6315. (defun org-first-headline-recenter (&optional N)
  6316. "Move cursor to the first headline and recenter the headline.
  6317. Optional argument N means put the headline into the Nth line of the window."
  6318. (goto-char (point-min))
  6319. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6320. (beginning-of-line)
  6321. (recenter (prefix-numeric-value N))))
  6322. ;;; Saving and restoring visibility
  6323. (defun org-outline-overlay-data (&optional use-markers)
  6324. "Return a list of the locations of all outline overlays.
  6325. These are overlays with the `invisible' property value `outline'.
  6326. The return value is a list of cons cells, with start and stop
  6327. positions for each overlay.
  6328. If USE-MARKERS is set, return the positions as markers."
  6329. (let (beg end)
  6330. (save-excursion
  6331. (save-restriction
  6332. (widen)
  6333. (delq nil
  6334. (mapcar (lambda (o)
  6335. (when (eq (overlay-get o 'invisible) 'outline)
  6336. (setq beg (overlay-start o)
  6337. end (overlay-end o))
  6338. (and beg end (> end beg)
  6339. (if use-markers
  6340. (cons (move-marker (make-marker) beg)
  6341. (move-marker (make-marker) end))
  6342. (cons beg end)))))
  6343. (overlays-in (point-min) (point-max))))))))
  6344. (defun org-set-outline-overlay-data (data)
  6345. "Create visibility overlays for all positions in DATA.
  6346. DATA should have been made by `org-outline-overlay-data'."
  6347. (let (o)
  6348. (save-excursion
  6349. (save-restriction
  6350. (widen)
  6351. (show-all)
  6352. (mapc (lambda (c)
  6353. (outline-flag-region (car c) (cdr c) t))
  6354. data)))))
  6355. ;;; Folding of blocks
  6356. (defvar org-hide-block-overlays nil
  6357. "Overlays hiding blocks.")
  6358. (make-variable-buffer-local 'org-hide-block-overlays)
  6359. (defun org-block-map (function &optional start end)
  6360. "Call FUNCTION at the head of all source blocks in the current buffer.
  6361. Optional arguments START and END can be used to limit the range."
  6362. (let ((start (or start (point-min)))
  6363. (end (or end (point-max))))
  6364. (save-excursion
  6365. (goto-char start)
  6366. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6367. (save-excursion
  6368. (save-match-data
  6369. (goto-char (match-beginning 0))
  6370. (funcall function)))))))
  6371. (defun org-hide-block-toggle-all ()
  6372. "Toggle the visibility of all blocks in the current buffer."
  6373. (org-block-map #'org-hide-block-toggle))
  6374. (defun org-hide-block-all ()
  6375. "Fold all blocks in the current buffer."
  6376. (interactive)
  6377. (org-show-block-all)
  6378. (org-block-map #'org-hide-block-toggle-maybe))
  6379. (defun org-show-block-all ()
  6380. "Unfold all blocks in the current buffer."
  6381. (interactive)
  6382. (mapc 'delete-overlay org-hide-block-overlays)
  6383. (setq org-hide-block-overlays nil))
  6384. (defun org-hide-block-toggle-maybe ()
  6385. "Toggle visibility of block at point."
  6386. (interactive)
  6387. (let ((case-fold-search t))
  6388. (if (save-excursion
  6389. (beginning-of-line 1)
  6390. (looking-at org-block-regexp))
  6391. (progn (org-hide-block-toggle)
  6392. t) ;; to signal that we took action
  6393. nil))) ;; to signal that we did not
  6394. (defun org-hide-block-toggle (&optional force)
  6395. "Toggle the visibility of the current block."
  6396. (interactive)
  6397. (save-excursion
  6398. (beginning-of-line)
  6399. (if (re-search-forward org-block-regexp nil t)
  6400. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6401. (end (match-end 0)) ;; end of entire body
  6402. ov)
  6403. (if (memq t (mapcar (lambda (overlay)
  6404. (eq (overlay-get overlay 'invisible)
  6405. 'org-hide-block))
  6406. (overlays-at start)))
  6407. (if (or (not force) (eq force 'off))
  6408. (mapc (lambda (ov)
  6409. (when (member ov org-hide-block-overlays)
  6410. (setq org-hide-block-overlays
  6411. (delq ov org-hide-block-overlays)))
  6412. (when (eq (overlay-get ov 'invisible)
  6413. 'org-hide-block)
  6414. (delete-overlay ov)))
  6415. (overlays-at start)))
  6416. (setq ov (make-overlay start end))
  6417. (overlay-put ov 'invisible 'org-hide-block)
  6418. ;; make the block accessible to isearch
  6419. (overlay-put
  6420. ov 'isearch-open-invisible
  6421. (lambda (ov)
  6422. (when (member ov org-hide-block-overlays)
  6423. (setq org-hide-block-overlays
  6424. (delq ov org-hide-block-overlays)))
  6425. (when (eq (overlay-get ov 'invisible)
  6426. 'org-hide-block)
  6427. (delete-overlay ov))))
  6428. (push ov org-hide-block-overlays)))
  6429. (error "Not looking at a source block"))))
  6430. ;; org-tab-after-check-for-cycling-hook
  6431. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6432. ;; Remove overlays when changing major mode
  6433. (add-hook 'org-mode-hook
  6434. (lambda () (org-add-hook 'change-major-mode-hook
  6435. 'org-show-block-all 'append 'local)))
  6436. ;;; Org-goto
  6437. (defvar org-goto-window-configuration nil)
  6438. (defvar org-goto-marker nil)
  6439. (defvar org-goto-map)
  6440. (defun org-goto-map ()
  6441. "Set the keymap `org-goto'."
  6442. (setq org-goto-map
  6443. (let ((map (make-sparse-keymap)))
  6444. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6445. mouse-drag-region universal-argument org-occur))
  6446. cmd)
  6447. (while (setq cmd (pop cmds))
  6448. (substitute-key-definition cmd cmd map global-map)))
  6449. (suppress-keymap map)
  6450. (org-defkey map "\C-m" 'org-goto-ret)
  6451. (org-defkey map [(return)] 'org-goto-ret)
  6452. (org-defkey map [(left)] 'org-goto-left)
  6453. (org-defkey map [(right)] 'org-goto-right)
  6454. (org-defkey map [(control ?g)] 'org-goto-quit)
  6455. (org-defkey map "\C-i" 'org-cycle)
  6456. (org-defkey map [(tab)] 'org-cycle)
  6457. (org-defkey map [(down)] 'outline-next-visible-heading)
  6458. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6459. (if org-goto-auto-isearch
  6460. (if (fboundp 'define-key-after)
  6461. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6462. nil)
  6463. (org-defkey map "q" 'org-goto-quit)
  6464. (org-defkey map "n" 'outline-next-visible-heading)
  6465. (org-defkey map "p" 'outline-previous-visible-heading)
  6466. (org-defkey map "f" 'outline-forward-same-level)
  6467. (org-defkey map "b" 'outline-backward-same-level)
  6468. (org-defkey map "u" 'outline-up-heading))
  6469. (org-defkey map "/" 'org-occur)
  6470. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6471. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6472. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6473. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6474. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6475. map)))
  6476. (defconst org-goto-help
  6477. "Browse buffer copy, to find location or copy text.%s
  6478. RET=jump to location C-g=quit and return to previous location
  6479. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6480. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6481. (defun org-goto (&optional alternative-interface)
  6482. "Look up a different location in the current file, keeping current visibility.
  6483. When you want look-up or go to a different location in a
  6484. document, the fastest way is often to fold the entire buffer and
  6485. then dive into the tree. This method has the disadvantage, that
  6486. the previous location will be folded, which may not be what you
  6487. want.
  6488. This command works around this by showing a copy of the current
  6489. buffer in an indirect buffer, in overview mode. You can dive
  6490. into the tree in that copy, use org-occur and incremental search
  6491. to find a location. When pressing RET or `Q', the command
  6492. returns to the original buffer in which the visibility is still
  6493. unchanged. After RET it will also jump to the location selected
  6494. in the indirect buffer and expose the headline hierarchy above.
  6495. With a prefix argument, use the alternative interface: e.g. if
  6496. `org-goto-interface' is 'outline use 'outline-path-completion."
  6497. (interactive "P")
  6498. (org-goto-map)
  6499. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6500. (org-refile-use-outline-path t)
  6501. (org-refile-target-verify-function nil)
  6502. (interface
  6503. (if (not alternative-interface)
  6504. org-goto-interface
  6505. (if (eq org-goto-interface 'outline)
  6506. 'outline-path-completion
  6507. 'outline)))
  6508. (org-goto-start-pos (point))
  6509. (selected-point
  6510. (if (eq interface 'outline)
  6511. (car (org-get-location (current-buffer) org-goto-help))
  6512. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6513. (org-refile-check-position pa)
  6514. (nth 3 pa)))))
  6515. (if selected-point
  6516. (progn
  6517. (org-mark-ring-push org-goto-start-pos)
  6518. (goto-char selected-point)
  6519. (if (or (outline-invisible-p) (org-invisible-p2))
  6520. (org-show-context 'org-goto)))
  6521. (message "Quit"))))
  6522. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6523. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6524. (defvar org-goto-local-auto-isearch-map) ; defined below
  6525. (defun org-get-location (buf help)
  6526. "Let the user select a location in the Org-mode buffer BUF.
  6527. This function uses a recursive edit. It returns the selected position
  6528. or nil."
  6529. (org-no-popups
  6530. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6531. (isearch-hide-immediately nil)
  6532. (isearch-search-fun-function
  6533. (lambda () 'org-goto-local-search-headings))
  6534. (org-goto-selected-point org-goto-exit-command))
  6535. (save-excursion
  6536. (save-window-excursion
  6537. (delete-other-windows)
  6538. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6539. (org-pop-to-buffer-same-window
  6540. (condition-case nil
  6541. (make-indirect-buffer (current-buffer) "*org-goto*")
  6542. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6543. (with-output-to-temp-buffer "*Org Help*"
  6544. (princ (format help (if org-goto-auto-isearch
  6545. " Just type for auto-isearch."
  6546. " n/p/f/b/u to navigate, q to quit."))))
  6547. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6548. (setq buffer-read-only nil)
  6549. (let ((org-startup-truncated t)
  6550. (org-startup-folded nil)
  6551. (org-startup-align-all-tables nil))
  6552. (org-mode)
  6553. (org-overview))
  6554. (setq buffer-read-only t)
  6555. (if (and (boundp 'org-goto-start-pos)
  6556. (integer-or-marker-p org-goto-start-pos))
  6557. (let ((org-show-hierarchy-above t)
  6558. (org-show-siblings t)
  6559. (org-show-following-heading t))
  6560. (goto-char org-goto-start-pos)
  6561. (and (outline-invisible-p) (org-show-context)))
  6562. (goto-char (point-min)))
  6563. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6564. (message "Select location and press RET")
  6565. (use-local-map org-goto-map)
  6566. (recursive-edit)))
  6567. (kill-buffer "*org-goto*")
  6568. (cons org-goto-selected-point org-goto-exit-command))))
  6569. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6570. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6571. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6572. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6573. (defun org-goto-local-search-headings (string bound noerror)
  6574. "Search and make sure that any matches are in headlines."
  6575. (catch 'return
  6576. (while (if isearch-forward
  6577. (search-forward string bound noerror)
  6578. (search-backward string bound noerror))
  6579. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6580. (and (member :headline context)
  6581. (not (member :tags context))))
  6582. (throw 'return (point))))))
  6583. (defun org-goto-local-auto-isearch ()
  6584. "Start isearch."
  6585. (interactive)
  6586. (goto-char (point-min))
  6587. (let ((keys (this-command-keys)))
  6588. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6589. (isearch-mode t)
  6590. (isearch-process-search-char (string-to-char keys)))))
  6591. (defun org-goto-ret (&optional arg)
  6592. "Finish `org-goto' by going to the new location."
  6593. (interactive "P")
  6594. (setq org-goto-selected-point (point)
  6595. org-goto-exit-command 'return)
  6596. (throw 'exit nil))
  6597. (defun org-goto-left ()
  6598. "Finish `org-goto' by going to the new location."
  6599. (interactive)
  6600. (if (org-at-heading-p)
  6601. (progn
  6602. (beginning-of-line 1)
  6603. (setq org-goto-selected-point (point)
  6604. org-goto-exit-command 'left)
  6605. (throw 'exit nil))
  6606. (error "Not on a heading")))
  6607. (defun org-goto-right ()
  6608. "Finish `org-goto' by going to the new location."
  6609. (interactive)
  6610. (if (org-at-heading-p)
  6611. (progn
  6612. (setq org-goto-selected-point (point)
  6613. org-goto-exit-command 'right)
  6614. (throw 'exit nil))
  6615. (error "Not on a heading")))
  6616. (defun org-goto-quit ()
  6617. "Finish `org-goto' without cursor motion."
  6618. (interactive)
  6619. (setq org-goto-selected-point nil)
  6620. (setq org-goto-exit-command 'quit)
  6621. (throw 'exit nil))
  6622. ;;; Indirect buffer display of subtrees
  6623. (defvar org-indirect-dedicated-frame nil
  6624. "This is the frame being used for indirect tree display.")
  6625. (defvar org-last-indirect-buffer nil)
  6626. (defun org-tree-to-indirect-buffer (&optional arg)
  6627. "Create indirect buffer and narrow it to current subtree.
  6628. With a numerical prefix ARG, go up to this level and then take that tree.
  6629. If ARG is negative, go up that many levels.
  6630. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6631. indirect buffer previously made with this command, to avoid proliferation of
  6632. indirect buffers. However, when you call the command with a \
  6633. \\[universal-argument] prefix, or
  6634. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6635. is kept so that you can work with several indirect buffers at the same time.
  6636. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6637. \\[universal-argument] prefix also
  6638. requests that a new frame be made for the new buffer, so that the dedicated
  6639. frame is not changed."
  6640. (interactive "P")
  6641. (let ((cbuf (current-buffer))
  6642. (cwin (selected-window))
  6643. (pos (point))
  6644. beg end level heading ibuf)
  6645. (save-excursion
  6646. (org-back-to-heading t)
  6647. (when (numberp arg)
  6648. (setq level (org-outline-level))
  6649. (if (< arg 0) (setq arg (+ level arg)))
  6650. (while (> (setq level (org-outline-level)) arg)
  6651. (org-up-heading-safe)))
  6652. (setq beg (point)
  6653. heading (org-get-heading))
  6654. (org-end-of-subtree t t)
  6655. (if (org-at-heading-p) (backward-char 1))
  6656. (setq end (point)))
  6657. (if (and (buffer-live-p org-last-indirect-buffer)
  6658. (not (eq org-indirect-buffer-display 'new-frame))
  6659. (not arg))
  6660. (kill-buffer org-last-indirect-buffer))
  6661. (setq ibuf (org-get-indirect-buffer cbuf)
  6662. org-last-indirect-buffer ibuf)
  6663. (cond
  6664. ((or (eq org-indirect-buffer-display 'new-frame)
  6665. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6666. (select-frame (make-frame))
  6667. (delete-other-windows)
  6668. (org-pop-to-buffer-same-window ibuf)
  6669. (org-set-frame-title heading))
  6670. ((eq org-indirect-buffer-display 'dedicated-frame)
  6671. (raise-frame
  6672. (select-frame (or (and org-indirect-dedicated-frame
  6673. (frame-live-p org-indirect-dedicated-frame)
  6674. org-indirect-dedicated-frame)
  6675. (setq org-indirect-dedicated-frame (make-frame)))))
  6676. (delete-other-windows)
  6677. (org-pop-to-buffer-same-window ibuf)
  6678. (org-set-frame-title (concat "Indirect: " heading)))
  6679. ((eq org-indirect-buffer-display 'current-window)
  6680. (org-pop-to-buffer-same-window ibuf))
  6681. ((eq org-indirect-buffer-display 'other-window)
  6682. (pop-to-buffer ibuf))
  6683. (t (error "Invalid value")))
  6684. (if (featurep 'xemacs)
  6685. (save-excursion (org-mode) (turn-on-font-lock)))
  6686. (narrow-to-region beg end)
  6687. (show-all)
  6688. (goto-char pos)
  6689. (run-hook-with-args 'org-cycle-hook 'all)
  6690. (and (window-live-p cwin) (select-window cwin))))
  6691. (defun org-get-indirect-buffer (&optional buffer)
  6692. (setq buffer (or buffer (current-buffer)))
  6693. (let ((n 1) (base (buffer-name buffer)) bname)
  6694. (while (buffer-live-p
  6695. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6696. (setq n (1+ n)))
  6697. (condition-case nil
  6698. (make-indirect-buffer buffer bname 'clone)
  6699. (error (make-indirect-buffer buffer bname)))))
  6700. (defun org-set-frame-title (title)
  6701. "Set the title of the current frame to the string TITLE."
  6702. ;; FIXME: how to name a single frame in XEmacs???
  6703. (unless (featurep 'xemacs)
  6704. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6705. ;;;; Structure editing
  6706. ;;; Inserting headlines
  6707. (defun org-previous-line-empty-p (&optional next)
  6708. "Is the previous line a blank line?
  6709. When NEXT is non-nil, check the next line instead."
  6710. (save-excursion
  6711. (and (not (bobp))
  6712. (or (beginning-of-line (if next 2 0)) t)
  6713. (save-match-data
  6714. (looking-at "[ \t]*$")))))
  6715. (defun org-insert-heading (&optional arg invisible-ok)
  6716. "Insert a new heading or item with same depth at point.
  6717. If point is in a plain list and ARG is nil, create a new list item.
  6718. With one universal prefix argument, insert a heading even in lists.
  6719. With two universal prefix arguments, insert the heading at the end
  6720. of the parent subtree.
  6721. If point is at the beginning of a headline, insert a sibling before
  6722. the current headline. If point is not at the beginning, split the line
  6723. and create a new headline with the text in the current line after point
  6724. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6725. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6726. This is important for non-interactive uses of the command."
  6727. (interactive "P")
  6728. (cond
  6729. ((or (= (buffer-size) 0)
  6730. (and (not (save-excursion
  6731. (and (ignore-errors (org-back-to-heading invisible-ok))
  6732. (org-at-heading-p))))
  6733. (or arg (not (org-in-item-p)))))
  6734. (insert
  6735. (if (org-previous-line-empty-p) "" "\n")
  6736. (if (org-in-src-block-p) ",* " "* "))
  6737. (run-hooks 'org-insert-heading-hook))
  6738. ((or arg (not (org-insert-item
  6739. (save-excursion
  6740. (beginning-of-line)
  6741. (looking-at org-list-full-item-re)
  6742. (match-string 3)))))
  6743. (let (begn endn)
  6744. (when (org-buffer-narrowed-p)
  6745. (setq begn (point-min) endn (point-max))
  6746. (widen))
  6747. (let* ((empty-line-p nil)
  6748. (eops (equal arg '(16))) ; insert at end of parent subtree
  6749. (org-insert-heading-respect-content
  6750. (or (not (null arg)) org-insert-heading-respect-content))
  6751. (level nil)
  6752. (on-heading (org-at-heading-p))
  6753. (head (save-excursion
  6754. (condition-case nil
  6755. (progn
  6756. (org-back-to-heading invisible-ok)
  6757. (when (and (not on-heading)
  6758. (featurep 'org-inlinetask)
  6759. (integerp org-inlinetask-min-level)
  6760. (>= (length (match-string 0))
  6761. org-inlinetask-min-level))
  6762. ;; Find a heading level before the inline task
  6763. (while (and (setq level (org-up-heading-safe))
  6764. (>= level org-inlinetask-min-level)))
  6765. (if (org-at-heading-p)
  6766. (org-back-to-heading invisible-ok)
  6767. (error "This should not happen")))
  6768. (unless (and (save-excursion
  6769. (save-match-data
  6770. (org-backward-heading-same-level 1 invisible-ok))
  6771. (= (point) (match-beginning 0)))
  6772. (not (org-previous-line-empty-p t)))
  6773. (setq empty-line-p (org-previous-line-empty-p)))
  6774. (match-string 0))
  6775. (error "* "))))
  6776. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6777. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6778. pos hide-previous previous-pos)
  6779. (if ;; At the beginning of a heading, open a new line for insertiong
  6780. (and (bolp) (org-at-heading-p)
  6781. (not eops)
  6782. (or (bobp)
  6783. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6784. (open-line (if blank 2 1))
  6785. (save-excursion
  6786. (setq previous-pos (point-at-bol))
  6787. (end-of-line)
  6788. (setq hide-previous (outline-invisible-p)))
  6789. (and org-insert-heading-respect-content
  6790. (save-excursion
  6791. (while (outline-invisible-p)
  6792. (org-show-subtree)
  6793. (org-up-heading-safe))))
  6794. (let ((split
  6795. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6796. (save-excursion
  6797. (let ((p (point)))
  6798. (goto-char (point-at-bol))
  6799. (and (looking-at org-complex-heading-regexp)
  6800. (match-beginning 4)
  6801. (> p (match-beginning 4)))))))
  6802. tags pos)
  6803. (cond
  6804. ;; Insert a new line, possibly at end of parent subtree
  6805. (org-insert-heading-respect-content
  6806. (if (not eops)
  6807. (progn
  6808. (org-end-of-subtree nil t)
  6809. (and (looking-at "^\\*") (backward-char 1))
  6810. (while (and (not (bobp))
  6811. ;; Don't delete spaces in empty headlines
  6812. (not (looking-back org-outline-regexp))
  6813. (member (char-before) '(?\ ?\t ?\n)))
  6814. (backward-delete-char 1)))
  6815. (let ((p (point)))
  6816. (org-up-heading-safe)
  6817. (if (= p (point))
  6818. (goto-char (point-max))
  6819. (org-end-of-subtree nil t))))
  6820. (when (featurep 'org-inlinetask)
  6821. (while (and (not (eobp))
  6822. (looking-at "\\(\\*+\\)[ \t]+")
  6823. (>= (length (match-string 1))
  6824. org-inlinetask-min-level))
  6825. (org-end-of-subtree nil t)))
  6826. (or (bolp) (newline))
  6827. (or (org-previous-line-empty-p)
  6828. (and blank (newline)))
  6829. (if (or empty-line-p eops) (open-line 1)))
  6830. ;; Insert a headling containing text after point
  6831. ((org-at-heading-p)
  6832. (when hide-previous
  6833. (show-children)
  6834. (org-show-entry))
  6835. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6836. (setq tags (and (match-end 2) (match-string 2)))
  6837. (and (match-end 1)
  6838. (delete-region (match-beginning 1) (match-end 1)))
  6839. (setq pos (point-at-bol))
  6840. (or split (end-of-line 1))
  6841. (delete-horizontal-space)
  6842. (if (string-match "\\`\\*+\\'"
  6843. (buffer-substring (point-at-bol) (point)))
  6844. (insert " "))
  6845. (newline (if blank 2 1))
  6846. (when tags
  6847. (save-excursion
  6848. (goto-char pos)
  6849. (end-of-line 1)
  6850. (insert " " tags)
  6851. (org-set-tags nil 'align))))
  6852. (t
  6853. (or split (end-of-line 1))
  6854. (newline (if blank 2 1))))))
  6855. (insert head) (just-one-space)
  6856. (setq pos (point))
  6857. (end-of-line 1)
  6858. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6859. (when (and org-insert-heading-respect-content hide-previous)
  6860. (save-excursion
  6861. (goto-char previous-pos)
  6862. (hide-subtree)))
  6863. (when (and begn endn)
  6864. (narrow-to-region (min (point) begn) (max (point) endn)))
  6865. (run-hooks 'org-insert-heading-hook))))))
  6866. (defun org-get-heading (&optional no-tags no-todo)
  6867. "Return the heading of the current entry, without the stars.
  6868. When NO-TAGS is non-nil, don't include tags.
  6869. When NO-TODO is non-nil, don't include TODO keywords."
  6870. (save-excursion
  6871. (org-back-to-heading t)
  6872. (cond
  6873. ((and no-tags no-todo)
  6874. (looking-at org-complex-heading-regexp)
  6875. (match-string 4))
  6876. (no-tags
  6877. (looking-at (concat org-outline-regexp
  6878. "\\(.*?\\)"
  6879. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6880. (match-string 1))
  6881. (no-todo
  6882. (looking-at org-todo-line-regexp)
  6883. (match-string 3))
  6884. (t (looking-at org-heading-regexp)
  6885. (match-string 2)))))
  6886. (defvar orgstruct-mode) ; defined below
  6887. (defun org-heading-components ()
  6888. "Return the components of the current heading.
  6889. This is a list with the following elements:
  6890. - the level as an integer
  6891. - the reduced level, different if `org-odd-levels-only' is set.
  6892. - the TODO keyword, or nil
  6893. - the priority character, like ?A, or nil if no priority is given
  6894. - the headline text itself, or the tags string if no headline text
  6895. - the tags string, or nil."
  6896. (save-excursion
  6897. (org-back-to-heading t)
  6898. (if (let (case-fold-search)
  6899. (looking-at
  6900. (if orgstruct-mode
  6901. org-heading-regexp
  6902. org-complex-heading-regexp)))
  6903. (if orgstruct-mode
  6904. (list (length (match-string 1))
  6905. (org-reduced-level (length (match-string 1)))
  6906. nil
  6907. nil
  6908. (match-string 2)
  6909. nil)
  6910. (list (length (match-string 1))
  6911. (org-reduced-level (length (match-string 1)))
  6912. (org-match-string-no-properties 2)
  6913. (and (match-end 3) (aref (match-string 3) 2))
  6914. (org-match-string-no-properties 4)
  6915. (org-match-string-no-properties 5))))))
  6916. (defun org-get-entry ()
  6917. "Get the entry text, after heading, entire subtree."
  6918. (save-excursion
  6919. (org-back-to-heading t)
  6920. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6921. (defun org-insert-heading-after-current ()
  6922. "Insert a new heading with same level as current, after current subtree."
  6923. (interactive)
  6924. (org-back-to-heading)
  6925. (org-insert-heading)
  6926. (org-move-subtree-down)
  6927. (end-of-line 1))
  6928. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6929. "Insert heading with `org-insert-heading-respect-content' set to t."
  6930. (interactive "P")
  6931. (let ((org-insert-heading-respect-content t))
  6932. (org-insert-heading arg invisible-ok)))
  6933. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6934. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6935. (interactive "P")
  6936. (let ((org-insert-heading-respect-content t))
  6937. (org-insert-todo-heading force-state t)))
  6938. (defun org-insert-todo-heading (arg &optional force-heading)
  6939. "Insert a new heading with the same level and TODO state as current heading.
  6940. If the heading has no TODO state, or if the state is DONE, use the first
  6941. state (TODO by default). Also one prefix arg, force first state. With two
  6942. prefix args, force inserting at the end of the parent subtree."
  6943. (interactive "P")
  6944. (when (or force-heading (not (org-insert-item 'checkbox)))
  6945. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6946. force-heading))
  6947. (save-excursion
  6948. (org-back-to-heading)
  6949. (outline-previous-heading)
  6950. (looking-at org-todo-line-regexp))
  6951. (let*
  6952. ((new-mark-x
  6953. (if (or arg
  6954. (not (match-beginning 2))
  6955. (member (match-string 2) org-done-keywords))
  6956. (car org-todo-keywords-1)
  6957. (match-string 2)))
  6958. (new-mark
  6959. (or
  6960. (run-hook-with-args-until-success
  6961. 'org-todo-get-default-hook new-mark-x nil)
  6962. new-mark-x)))
  6963. (beginning-of-line 1)
  6964. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6965. (if org-treat-insert-todo-heading-as-state-change
  6966. (org-todo new-mark)
  6967. (insert new-mark " "))))
  6968. (when org-provide-todo-statistics
  6969. (org-update-parent-todo-statistics))))
  6970. (defun org-insert-subheading (arg)
  6971. "Insert a new subheading and demote it.
  6972. Works for outline headings and for plain lists alike."
  6973. (interactive "P")
  6974. (org-insert-heading arg)
  6975. (cond
  6976. ((org-at-heading-p) (org-do-demote))
  6977. ((org-at-item-p) (org-indent-item))))
  6978. (defun org-insert-todo-subheading (arg)
  6979. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6980. Works for outline headings and for plain lists alike."
  6981. (interactive "P")
  6982. (org-insert-todo-heading arg)
  6983. (cond
  6984. ((org-at-heading-p) (org-do-demote))
  6985. ((org-at-item-p) (org-indent-item))))
  6986. ;;; Promotion and Demotion
  6987. (defvar org-after-demote-entry-hook nil
  6988. "Hook run after an entry has been demoted.
  6989. The cursor will be at the beginning of the entry.
  6990. When a subtree is being demoted, the hook will be called for each node.")
  6991. (defvar org-after-promote-entry-hook nil
  6992. "Hook run after an entry has been promoted.
  6993. The cursor will be at the beginning of the entry.
  6994. When a subtree is being promoted, the hook will be called for each node.")
  6995. (defun org-promote-subtree ()
  6996. "Promote the entire subtree.
  6997. See also `org-promote'."
  6998. (interactive)
  6999. (save-excursion
  7000. (org-with-limited-levels (org-map-tree 'org-promote)))
  7001. (org-fix-position-after-promote))
  7002. (defun org-demote-subtree ()
  7003. "Demote the entire subtree. See `org-demote'.
  7004. See also `org-promote'."
  7005. (interactive)
  7006. (save-excursion
  7007. (org-with-limited-levels (org-map-tree 'org-demote)))
  7008. (org-fix-position-after-promote))
  7009. (defun org-do-promote ()
  7010. "Promote the current heading higher up the tree.
  7011. If the region is active in `transient-mark-mode', promote all headings
  7012. in the region."
  7013. (interactive)
  7014. (save-excursion
  7015. (if (org-region-active-p)
  7016. (org-map-region 'org-promote (region-beginning) (region-end))
  7017. (org-promote)))
  7018. (org-fix-position-after-promote))
  7019. (defun org-do-demote ()
  7020. "Demote the current heading lower down the tree.
  7021. If the region is active in `transient-mark-mode', demote all headings
  7022. in the region."
  7023. (interactive)
  7024. (save-excursion
  7025. (if (org-region-active-p)
  7026. (org-map-region 'org-demote (region-beginning) (region-end))
  7027. (org-demote)))
  7028. (org-fix-position-after-promote))
  7029. (defun org-fix-position-after-promote ()
  7030. "Make sure that after pro/demotion cursor position is right."
  7031. (let ((pos (point)))
  7032. (when (save-excursion
  7033. (beginning-of-line 1)
  7034. (looking-at org-todo-line-regexp)
  7035. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7036. (cond ((eobp) (insert " "))
  7037. ((eolp) (insert " "))
  7038. ((equal (char-after) ?\ ) (forward-char 1))))))
  7039. (defun org-current-level ()
  7040. "Return the level of the current entry, or nil if before the first headline.
  7041. The level is the number of stars at the beginning of the headline."
  7042. (save-excursion
  7043. (org-with-limited-levels
  7044. (if (ignore-errors (org-back-to-heading t))
  7045. (funcall outline-level)))))
  7046. (defun org-get-previous-line-level ()
  7047. "Return the outline depth of the last headline before the current line.
  7048. Returns 0 for the first headline in the buffer, and nil if before the
  7049. first headline."
  7050. (let ((current-level (org-current-level))
  7051. (prev-level (when (> (line-number-at-pos) 1)
  7052. (save-excursion
  7053. (beginning-of-line 0)
  7054. (org-current-level)))))
  7055. (cond ((null current-level) nil) ; Before first headline
  7056. ((null prev-level) 0) ; At first headline
  7057. (prev-level))))
  7058. (defun org-reduced-level (l)
  7059. "Compute the effective level of a heading.
  7060. This takes into account the setting of `org-odd-levels-only'."
  7061. (cond
  7062. ((zerop l) 0)
  7063. (org-odd-levels-only (1+ (floor (/ l 2))))
  7064. (t l)))
  7065. (defun org-level-increment ()
  7066. "Return the number of stars that will be added or removed at a
  7067. time to headlines when structure editing, based on the value of
  7068. `org-odd-levels-only'."
  7069. (if org-odd-levels-only 2 1))
  7070. (defun org-get-valid-level (level &optional change)
  7071. "Rectify a level change under the influence of `org-odd-levels-only'
  7072. LEVEL is a current level, CHANGE is by how much the level should be
  7073. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7074. even level numbers will become the next higher odd number."
  7075. (if org-odd-levels-only
  7076. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7077. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7078. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7079. (max 1 (+ level (or change 0)))))
  7080. (if (boundp 'define-obsolete-function-alias)
  7081. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7082. (define-obsolete-function-alias 'org-get-legal-level
  7083. 'org-get-valid-level)
  7084. (define-obsolete-function-alias 'org-get-legal-level
  7085. 'org-get-valid-level "23.1")))
  7086. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7087. ;; ̀org-with-limited-levels'
  7088. (defun org-promote ()
  7089. "Promote the current heading higher up the tree.
  7090. If the region is active in `transient-mark-mode', promote all headings
  7091. in the region."
  7092. (org-back-to-heading t)
  7093. (let* ((level (save-match-data (funcall outline-level)))
  7094. (after-change-functions (remove 'flyspell-after-change-function
  7095. after-change-functions))
  7096. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7097. (diff (abs (- level (length up-head) -1))))
  7098. (cond ((and (= level 1) org-called-with-limited-levels
  7099. org-allow-promoting-top-level-subtree)
  7100. (replace-match "# " nil t))
  7101. ((= level 1)
  7102. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  7103. (t (replace-match up-head nil t)))
  7104. ;; Fixup tag positioning
  7105. (unless (= level 1)
  7106. (and org-auto-align-tags (org-set-tags nil t))
  7107. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7108. (run-hooks 'org-after-promote-entry-hook)))
  7109. (defun org-demote ()
  7110. "Demote the current heading lower down the tree.
  7111. If the region is active in `transient-mark-mode', demote all headings
  7112. in the region."
  7113. (org-back-to-heading t)
  7114. (let* ((level (save-match-data (funcall outline-level)))
  7115. (after-change-functions (remove 'flyspell-after-change-function
  7116. after-change-functions))
  7117. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7118. (diff (abs (- level (length down-head) -1))))
  7119. (replace-match down-head nil t)
  7120. ;; Fixup tag positioning
  7121. (and org-auto-align-tags (org-set-tags nil t))
  7122. (if org-adapt-indentation (org-fixup-indentation diff))
  7123. (run-hooks 'org-after-demote-entry-hook)))
  7124. (defun org-cycle-level ()
  7125. "Cycle the level of an empty headline through possible states.
  7126. This goes first to child, then to parent, level, then up the hierarchy.
  7127. After top level, it switches back to sibling level."
  7128. (interactive)
  7129. (let ((org-adapt-indentation nil))
  7130. (when (org-point-at-end-of-empty-headline)
  7131. (setq this-command 'org-cycle-level) ; Only needed for caching
  7132. (let ((cur-level (org-current-level))
  7133. (prev-level (org-get-previous-line-level)))
  7134. (cond
  7135. ;; If first headline in file, promote to top-level.
  7136. ((= prev-level 0)
  7137. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7138. do (org-do-promote)))
  7139. ;; If same level as prev, demote one.
  7140. ((= prev-level cur-level)
  7141. (org-do-demote))
  7142. ;; If parent is top-level, promote to top level if not already.
  7143. ((= prev-level 1)
  7144. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7145. do (org-do-promote)))
  7146. ;; If top-level, return to prev-level.
  7147. ((= cur-level 1)
  7148. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7149. do (org-do-demote)))
  7150. ;; If less than prev-level, promote one.
  7151. ((< cur-level prev-level)
  7152. (org-do-promote))
  7153. ;; If deeper than prev-level, promote until higher than
  7154. ;; prev-level.
  7155. ((> cur-level prev-level)
  7156. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7157. do (org-do-promote))))
  7158. t))))
  7159. (defun org-map-tree (fun)
  7160. "Call FUN for every heading underneath the current one."
  7161. (org-back-to-heading)
  7162. (let ((level (funcall outline-level)))
  7163. (save-excursion
  7164. (funcall fun)
  7165. (while (and (progn
  7166. (outline-next-heading)
  7167. (> (funcall outline-level) level))
  7168. (not (eobp)))
  7169. (funcall fun)))))
  7170. (defun org-map-region (fun beg end)
  7171. "Call FUN for every heading between BEG and END."
  7172. (let ((org-ignore-region t))
  7173. (save-excursion
  7174. (setq end (copy-marker end))
  7175. (goto-char beg)
  7176. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7177. (< (point) end))
  7178. (funcall fun))
  7179. (while (and (progn
  7180. (outline-next-heading)
  7181. (< (point) end))
  7182. (not (eobp)))
  7183. (funcall fun)))))
  7184. (defvar org-property-end-re) ; silence byte-compiler
  7185. (defun org-fixup-indentation (diff)
  7186. "Change the indentation in the current entry by DIFF.
  7187. However, if any line in the current entry has no indentation, or if it
  7188. would end up with no indentation after the change, nothing at all is done."
  7189. (save-excursion
  7190. (let ((end (save-excursion (outline-next-heading)
  7191. (point-marker)))
  7192. (prohibit (if (> diff 0)
  7193. "^\\S-"
  7194. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7195. col)
  7196. (unless (save-excursion (end-of-line 1)
  7197. (re-search-forward prohibit end t))
  7198. (while (and (< (point) end)
  7199. (re-search-forward "^[ \t]+" end t))
  7200. (goto-char (match-end 0))
  7201. (setq col (current-column))
  7202. (if (< diff 0) (replace-match ""))
  7203. (org-indent-to-column (+ diff col))))
  7204. (move-marker end nil))))
  7205. (defun org-convert-to-odd-levels ()
  7206. "Convert an org-mode file with all levels allowed to one with odd levels.
  7207. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7208. level 5 etc."
  7209. (interactive)
  7210. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7211. (let ((outline-level 'org-outline-level)
  7212. (org-odd-levels-only nil) n)
  7213. (save-excursion
  7214. (goto-char (point-min))
  7215. (while (re-search-forward "^\\*\\*+ " nil t)
  7216. (setq n (- (length (match-string 0)) 2))
  7217. (while (>= (setq n (1- n)) 0)
  7218. (org-demote))
  7219. (end-of-line 1))))))
  7220. (defun org-convert-to-oddeven-levels ()
  7221. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7222. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7223. file contains a section with an even level, conversion would
  7224. destroy the structure of the file. An error is signaled in this
  7225. case."
  7226. (interactive)
  7227. (goto-char (point-min))
  7228. ;; First check if there are no even levels
  7229. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7230. (org-show-context t)
  7231. (error "Not all levels are odd in this file. Conversion not possible"))
  7232. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7233. (let ((outline-regexp org-outline-regexp)
  7234. (outline-level 'org-outline-level)
  7235. (org-odd-levels-only nil) n)
  7236. (save-excursion
  7237. (goto-char (point-min))
  7238. (while (re-search-forward "^\\*\\*+ " nil t)
  7239. (setq n (/ (1- (length (match-string 0))) 2))
  7240. (while (>= (setq n (1- n)) 0)
  7241. (org-promote))
  7242. (end-of-line 1))))))
  7243. (defun org-tr-level (n)
  7244. "Make N odd if required."
  7245. (if org-odd-levels-only (1+ (/ n 2)) n))
  7246. ;;; Vertical tree motion, cutting and pasting of subtrees
  7247. (defun org-move-subtree-up (&optional arg)
  7248. "Move the current subtree up past ARG headlines of the same level."
  7249. (interactive "p")
  7250. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7251. (defun org-move-subtree-down (&optional arg)
  7252. "Move the current subtree down past ARG headlines of the same level."
  7253. (interactive "p")
  7254. (setq arg (prefix-numeric-value arg))
  7255. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7256. 'org-get-last-sibling))
  7257. (ins-point (make-marker))
  7258. (cnt (abs arg))
  7259. (col (current-column))
  7260. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7261. ;; Select the tree
  7262. (org-back-to-heading)
  7263. (setq beg0 (point))
  7264. (save-excursion
  7265. (setq ne-beg (org-back-over-empty-lines))
  7266. (setq beg (point)))
  7267. (save-match-data
  7268. (save-excursion (outline-end-of-heading)
  7269. (setq folded (outline-invisible-p)))
  7270. (outline-end-of-subtree))
  7271. (outline-next-heading)
  7272. (setq ne-end (org-back-over-empty-lines))
  7273. (setq end (point))
  7274. (goto-char beg0)
  7275. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7276. ;; include less whitespace
  7277. (save-excursion
  7278. (goto-char beg)
  7279. (forward-line (- ne-beg ne-end))
  7280. (setq beg (point))))
  7281. ;; Find insertion point, with error handling
  7282. (while (> cnt 0)
  7283. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7284. (progn (goto-char beg0)
  7285. (user-error "Cannot move past superior level or buffer limit")))
  7286. (setq cnt (1- cnt)))
  7287. (if (> arg 0)
  7288. ;; Moving forward - still need to move over subtree
  7289. (progn (org-end-of-subtree t t)
  7290. (save-excursion
  7291. (org-back-over-empty-lines)
  7292. (or (bolp) (newline)))))
  7293. (setq ne-ins (org-back-over-empty-lines))
  7294. (move-marker ins-point (point))
  7295. (setq txt (buffer-substring beg end))
  7296. (org-save-markers-in-region beg end)
  7297. (delete-region beg end)
  7298. (org-remove-empty-overlays-at beg)
  7299. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7300. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7301. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7302. (let ((bbb (point)))
  7303. (insert-before-markers txt)
  7304. (org-reinstall-markers-in-region bbb)
  7305. (move-marker ins-point bbb))
  7306. (or (bolp) (insert "\n"))
  7307. (setq ins-end (point))
  7308. (goto-char ins-point)
  7309. (org-skip-whitespace)
  7310. (when (and (< arg 0)
  7311. (org-first-sibling-p)
  7312. (> ne-ins ne-beg))
  7313. ;; Move whitespace back to beginning
  7314. (save-excursion
  7315. (goto-char ins-end)
  7316. (let ((kill-whole-line t))
  7317. (kill-line (- ne-ins ne-beg)) (point)))
  7318. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7319. (move-marker ins-point nil)
  7320. (if folded
  7321. (hide-subtree)
  7322. (org-show-entry)
  7323. (show-children)
  7324. (org-cycle-hide-drawers 'children))
  7325. (org-clean-visibility-after-subtree-move)
  7326. ;; move back to the initial column we were at
  7327. (move-to-column col)))
  7328. (defvar org-subtree-clip ""
  7329. "Clipboard for cut and paste of subtrees.
  7330. This is actually only a copy of the kill, because we use the normal kill
  7331. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7332. (defvar org-subtree-clip-folded nil
  7333. "Was the last copied subtree folded?
  7334. This is used to fold the tree back after pasting.")
  7335. (defun org-cut-subtree (&optional n)
  7336. "Cut the current subtree into the clipboard.
  7337. With prefix arg N, cut this many sequential subtrees.
  7338. This is a short-hand for marking the subtree and then cutting it."
  7339. (interactive "p")
  7340. (org-copy-subtree n 'cut))
  7341. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7342. "Cut the current subtree into the clipboard.
  7343. With prefix arg N, cut this many sequential subtrees.
  7344. This is a short-hand for marking the subtree and then copying it.
  7345. If CUT is non-nil, actually cut the subtree.
  7346. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7347. of some markers in the region, even if CUT is non-nil. This is
  7348. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7349. (interactive "p")
  7350. (let (beg end folded (beg0 (point)))
  7351. (if (org-called-interactively-p 'any)
  7352. (org-back-to-heading nil) ; take what looks like a subtree
  7353. (org-back-to-heading t)) ; take what is really there
  7354. (setq beg (point))
  7355. (skip-chars-forward " \t\r\n")
  7356. (save-match-data
  7357. (if nosubtrees
  7358. (outline-next-heading)
  7359. (save-excursion (outline-end-of-heading)
  7360. (setq folded (outline-invisible-p)))
  7361. (condition-case nil
  7362. (org-forward-heading-same-level (1- n) t)
  7363. (error nil))
  7364. (org-end-of-subtree t t)))
  7365. (setq end (point))
  7366. (goto-char beg0)
  7367. (when (> end beg)
  7368. (setq org-subtree-clip-folded folded)
  7369. (when (or cut force-store-markers)
  7370. (org-save-markers-in-region beg end))
  7371. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7372. (setq org-subtree-clip (current-kill 0))
  7373. (message "%s: Subtree(s) with %d characters"
  7374. (if cut "Cut" "Copied")
  7375. (length org-subtree-clip)))))
  7376. (defun org-paste-subtree (&optional level tree for-yank)
  7377. "Paste the clipboard as a subtree, with modification of headline level.
  7378. The entire subtree is promoted or demoted in order to match a new headline
  7379. level.
  7380. If the cursor is at the beginning of a headline, the same level as
  7381. that headline is used to paste the tree.
  7382. If not, the new level is derived from the *visible* headings
  7383. before and after the insertion point, and taken to be the inferior headline
  7384. level of the two. So if the previous visible heading is level 3 and the
  7385. next is level 4 (or vice versa), level 4 will be used for insertion.
  7386. This makes sure that the subtree remains an independent subtree and does
  7387. not swallow low level entries.
  7388. You can also force a different level, either by using a numeric prefix
  7389. argument, or by inserting the heading marker by hand. For example, if the
  7390. cursor is after \"*****\", then the tree will be shifted to level 5.
  7391. If optional TREE is given, use this text instead of the kill ring.
  7392. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7393. move back over whitespace before inserting, and move point to the end of
  7394. the inserted text when done."
  7395. (interactive "P")
  7396. (setq tree (or tree (and kill-ring (current-kill 0))))
  7397. (unless (org-kill-is-subtree-p tree)
  7398. (error "%s"
  7399. (substitute-command-keys
  7400. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7401. (org-with-limited-levels
  7402. (let* ((visp (not (outline-invisible-p)))
  7403. (txt tree)
  7404. (^re_ "\\(\\*+\\)[ \t]*")
  7405. (old-level (if (string-match org-outline-regexp-bol txt)
  7406. (- (match-end 0) (match-beginning 0) 1)
  7407. -1))
  7408. (force-level (cond (level (prefix-numeric-value level))
  7409. ((and (looking-at "[ \t]*$")
  7410. (string-match
  7411. "^\\*+$" (buffer-substring
  7412. (point-at-bol) (point))))
  7413. (- (match-end 1) (match-beginning 1)))
  7414. ((and (bolp)
  7415. (looking-at org-outline-regexp))
  7416. (- (match-end 0) (point) 1))))
  7417. (previous-level (save-excursion
  7418. (condition-case nil
  7419. (progn
  7420. (outline-previous-visible-heading 1)
  7421. (if (looking-at ^re_)
  7422. (- (match-end 0) (match-beginning 0) 1)
  7423. 1))
  7424. (error 1))))
  7425. (next-level (save-excursion
  7426. (condition-case nil
  7427. (progn
  7428. (or (looking-at org-outline-regexp)
  7429. (outline-next-visible-heading 1))
  7430. (if (looking-at ^re_)
  7431. (- (match-end 0) (match-beginning 0) 1)
  7432. 1))
  7433. (error 1))))
  7434. (new-level (or force-level (max previous-level next-level)))
  7435. (shift (if (or (= old-level -1)
  7436. (= new-level -1)
  7437. (= old-level new-level))
  7438. 0
  7439. (- new-level old-level)))
  7440. (delta (if (> shift 0) -1 1))
  7441. (func (if (> shift 0) 'org-demote 'org-promote))
  7442. (org-odd-levels-only nil)
  7443. beg end newend)
  7444. ;; Remove the forced level indicator
  7445. (if force-level
  7446. (delete-region (point-at-bol) (point)))
  7447. ;; Paste
  7448. (beginning-of-line (if (bolp) 1 2))
  7449. (setq beg (point))
  7450. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7451. (insert-before-markers txt)
  7452. (unless (string-match "\n\\'" txt) (insert "\n"))
  7453. (setq newend (point))
  7454. (org-reinstall-markers-in-region beg)
  7455. (setq end (point))
  7456. (goto-char beg)
  7457. (skip-chars-forward " \t\n\r")
  7458. (setq beg (point))
  7459. (if (and (outline-invisible-p) visp)
  7460. (save-excursion (outline-show-heading)))
  7461. ;; Shift if necessary
  7462. (unless (= shift 0)
  7463. (save-restriction
  7464. (narrow-to-region beg end)
  7465. (while (not (= shift 0))
  7466. (org-map-region func (point-min) (point-max))
  7467. (setq shift (+ delta shift)))
  7468. (goto-char (point-min))
  7469. (setq newend (point-max))))
  7470. (when (or (org-called-interactively-p 'interactive) for-yank)
  7471. (message "Clipboard pasted as level %d subtree" new-level))
  7472. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7473. kill-ring
  7474. (eq org-subtree-clip (current-kill 0))
  7475. org-subtree-clip-folded)
  7476. ;; The tree was folded before it was killed/copied
  7477. (hide-subtree))
  7478. (and for-yank (goto-char newend)))))
  7479. (defun org-kill-is-subtree-p (&optional txt)
  7480. "Check if the current kill is an outline subtree, or a set of trees.
  7481. Returns nil if kill does not start with a headline, or if the first
  7482. headline level is not the largest headline level in the tree.
  7483. So this will actually accept several entries of equal levels as well,
  7484. which is OK for `org-paste-subtree'.
  7485. If optional TXT is given, check this string instead of the current kill."
  7486. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7487. (re (org-get-limited-outline-regexp))
  7488. (^re (concat "^" re))
  7489. (start-level (and kill
  7490. (string-match
  7491. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7492. kill)
  7493. (- (match-end 2) (match-beginning 2) 1)))
  7494. (start (1+ (or (match-beginning 2) -1))))
  7495. (if (not start-level)
  7496. (progn
  7497. nil) ;; does not even start with a heading
  7498. (catch 'exit
  7499. (while (setq start (string-match ^re kill (1+ start)))
  7500. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7501. (throw 'exit nil)))
  7502. t))))
  7503. (defvar org-markers-to-move nil
  7504. "Markers that should be moved with a cut-and-paste operation.
  7505. Those markers are stored together with their positions relative to
  7506. the start of the region.")
  7507. (defun org-save-markers-in-region (beg end)
  7508. "Check markers in region.
  7509. If these markers are between BEG and END, record their position relative
  7510. to BEG, so that after moving the block of text, we can put the markers back
  7511. into place.
  7512. This function gets called just before an entry or tree gets cut from the
  7513. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7514. called immediately, to move the markers with the entries."
  7515. (setq org-markers-to-move nil)
  7516. (when (featurep 'org-clock)
  7517. (org-clock-save-markers-for-cut-and-paste beg end))
  7518. (when (featurep 'org-agenda)
  7519. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7520. (defun org-check-and-save-marker (marker beg end)
  7521. "Check if MARKER is between BEG and END.
  7522. If yes, remember the marker and the distance to BEG."
  7523. (when (and (marker-buffer marker)
  7524. (equal (marker-buffer marker) (current-buffer)))
  7525. (if (and (>= marker beg) (< marker end))
  7526. (push (cons marker (- marker beg)) org-markers-to-move))))
  7527. (defun org-reinstall-markers-in-region (beg)
  7528. "Move all remembered markers to their position relative to BEG."
  7529. (mapc (lambda (x)
  7530. (move-marker (car x) (+ beg (cdr x))))
  7531. org-markers-to-move)
  7532. (setq org-markers-to-move nil))
  7533. (defun org-narrow-to-subtree ()
  7534. "Narrow buffer to the current subtree."
  7535. (interactive)
  7536. (save-excursion
  7537. (save-match-data
  7538. (org-with-limited-levels
  7539. (narrow-to-region
  7540. (progn (org-back-to-heading t) (point))
  7541. (progn (org-end-of-subtree t t)
  7542. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7543. (point)))))))
  7544. (defun org-narrow-to-block ()
  7545. "Narrow buffer to the current block."
  7546. (interactive)
  7547. (let* ((case-fold-search t)
  7548. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7549. "^[ \t]*#\\+end_.*")))
  7550. (if blockp
  7551. (narrow-to-region (car blockp) (cdr blockp))
  7552. (error "Not in a block"))))
  7553. (eval-when-compile
  7554. (defvar org-property-drawer-re))
  7555. (defvar org-property-start-re) ;; defined below
  7556. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7557. "Clone the task (subtree) at point N times.
  7558. The clones will be inserted as siblings.
  7559. In interactive use, the user will be prompted for the number of
  7560. clones to be produced. When called with a universal prefix argument
  7561. and if the entry has a timestamp, the user will also be prompted for
  7562. a time shift, which may be a repeater as used in time stamps, for
  7563. example `+3d'.
  7564. When a valid repeater is given and the entry contains any time
  7565. stamps, the clones will become a sequence in time, with time
  7566. stamps in the subtree shifted for each clone produced. If SHIFT
  7567. is nil or the empty string, time stamps will be left alone. The
  7568. ID property of the original subtree is removed.
  7569. If the original subtree did contain time stamps with a repeater,
  7570. the following will happen:
  7571. - the repeater will be removed in each clone
  7572. - an additional clone will be produced, with the current, unshifted
  7573. date(s) in the entry.
  7574. - the original entry will be placed *after* all the clones, with
  7575. repeater intact.
  7576. - the start days in the repeater in the original entry will be shifted
  7577. to past the last clone.
  7578. In this way you can spell out a number of instances of a repeating task,
  7579. and still retain the repeater to cover future instances of the task."
  7580. (interactive "nNumber of clones to produce: ")
  7581. (let ((shift
  7582. (or shift
  7583. (if (and (equal current-prefix-arg '(4))
  7584. (save-excursion
  7585. (re-search-forward org-ts-regexp-both
  7586. (save-excursion
  7587. (org-end-of-subtree t)
  7588. (point)) t)))
  7589. (read-from-minibuffer
  7590. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7591. ""))) ;; No time shift
  7592. (n-no-remove -1)
  7593. (drawer-re org-drawer-regexp)
  7594. beg end template task idprop
  7595. shift-n shift-what doshift nmin nmax)
  7596. (if (not (and (integerp n) (> n 0)))
  7597. (error "Invalid number of replications %s" n))
  7598. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7599. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7600. shift)))
  7601. (error "Invalid shift specification %s" shift))
  7602. (when doshift
  7603. (setq shift-n (string-to-number (match-string 1 shift))
  7604. shift-what (cdr (assoc (match-string 2 shift)
  7605. '(("d" . day) ("w" . week)
  7606. ("m" . month) ("y" . year))))))
  7607. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7608. (setq nmin 1 nmax n)
  7609. (org-back-to-heading t)
  7610. (setq beg (point))
  7611. (setq idprop (org-entry-get nil "ID"))
  7612. (org-end-of-subtree t t)
  7613. (or (bolp) (insert "\n"))
  7614. (setq end (point))
  7615. (setq template (buffer-substring beg end))
  7616. (when (and doshift
  7617. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7618. (delete-region beg end)
  7619. (setq end beg)
  7620. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7621. (goto-char end)
  7622. (loop for n from nmin to nmax do
  7623. ;; prepare clone
  7624. (with-temp-buffer
  7625. (insert template)
  7626. (org-mode)
  7627. (goto-char (point-min))
  7628. (org-show-subtree)
  7629. (and idprop (if org-clone-delete-id
  7630. (org-entry-delete nil "ID")
  7631. (org-id-get-create t)))
  7632. (unless (= n 0)
  7633. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7634. (kill-whole-line))
  7635. (goto-char (point-min))
  7636. (while (re-search-forward drawer-re nil t)
  7637. (mapc (lambda (d)
  7638. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7639. (goto-char (point-min))
  7640. (when doshift
  7641. (while (re-search-forward org-ts-regexp-both nil t)
  7642. (org-timestamp-change (* n shift-n) shift-what))
  7643. (unless (= n n-no-remove)
  7644. (goto-char (point-min))
  7645. (while (re-search-forward org-ts-regexp nil t)
  7646. (save-excursion
  7647. (goto-char (match-beginning 0))
  7648. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7649. (delete-region (match-beginning 1) (match-end 1)))))))
  7650. (setq task (buffer-string)))
  7651. (insert task))
  7652. (goto-char beg)))
  7653. ;;; Outline Sorting
  7654. (defun org-sort (with-case)
  7655. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7656. Optional argument WITH-CASE means sort case-sensitively."
  7657. (interactive "P")
  7658. (cond
  7659. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7660. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7661. (t
  7662. (org-call-with-arg 'org-sort-entries with-case))))
  7663. (defun org-sort-remove-invisible (s)
  7664. "Remove invisible links from string S."
  7665. (remove-text-properties 0 (length s) org-rm-props s)
  7666. (while (string-match org-bracket-link-regexp s)
  7667. (setq s (replace-match (if (match-end 2)
  7668. (match-string 3 s)
  7669. (match-string 1 s)) t t s)))
  7670. (let ((st (format " %s " s)))
  7671. (while (string-match org-emph-re st)
  7672. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7673. (setq s (substring st 1 -1)))
  7674. s)
  7675. (defvar org-priority-regexp) ; defined later in the file
  7676. (defvar org-after-sorting-entries-or-items-hook nil
  7677. "Hook that is run after a bunch of entries or items have been sorted.
  7678. When children are sorted, the cursor is in the parent line when this
  7679. hook gets called. When a region or a plain list is sorted, the cursor
  7680. will be in the first entry of the sorted region/list.")
  7681. (defun org-sort-entries
  7682. (&optional with-case sorting-type getkey-func compare-func property)
  7683. "Sort entries on a certain level of an outline tree.
  7684. If there is an active region, the entries in the region are sorted.
  7685. Else, if the cursor is before the first entry, sort the top-level items.
  7686. Else, the children of the entry at point are sorted.
  7687. Sorting can be alphabetically, numerically, by date/time as given by
  7688. a time stamp, by a property, by priority order, or by a custom function.
  7689. The command prompts for the sorting type unless it has been given to the
  7690. function through the SORTING-TYPE argument, which needs to be a character,
  7691. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7692. precise meaning of each character:
  7693. n Numerically, by converting the beginning of the entry/item to a number.
  7694. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7695. o By order of TODO keywords.
  7696. t By date/time, either the first active time stamp in the entry, or, if
  7697. none exist, by the first inactive one.
  7698. s By the scheduled date/time.
  7699. d By deadline date/time.
  7700. c By creation time, which is assumed to be the first inactive time stamp
  7701. at the beginning of a line.
  7702. p By priority according to the cookie.
  7703. r By the value of a property.
  7704. Capital letters will reverse the sort order.
  7705. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7706. called with point at the beginning of the record. It must return either
  7707. a string or a number that should serve as the sorting key for that record.
  7708. Comparing entries ignores case by default. However, with an optional argument
  7709. WITH-CASE, the sorting considers case as well.
  7710. Sorting is done against the visible part of the headlines, it ignores hidden
  7711. links."
  7712. (interactive "P")
  7713. (let ((case-func (if with-case 'identity 'downcase))
  7714. (cmstr
  7715. ;; The clock marker is lost when using `sort-subr', let's
  7716. ;; store the clocking string.
  7717. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7718. (save-excursion
  7719. (goto-char org-clock-marker)
  7720. (looking-back "^.*") (match-string-no-properties 0))))
  7721. start beg end stars re re2
  7722. txt what tmp)
  7723. ;; Find beginning and end of region to sort
  7724. (cond
  7725. ((org-region-active-p)
  7726. ;; we will sort the region
  7727. (setq end (region-end)
  7728. what "region")
  7729. (goto-char (region-beginning))
  7730. (if (not (org-at-heading-p)) (outline-next-heading))
  7731. (setq start (point)))
  7732. ((or (org-at-heading-p)
  7733. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7734. ;; we will sort the children of the current headline
  7735. (org-back-to-heading)
  7736. (setq start (point)
  7737. end (progn (org-end-of-subtree t t)
  7738. (or (bolp) (insert "\n"))
  7739. (org-back-over-empty-lines)
  7740. (point))
  7741. what "children")
  7742. (goto-char start)
  7743. (show-subtree)
  7744. (outline-next-heading))
  7745. (t
  7746. ;; we will sort the top-level entries in this file
  7747. (goto-char (point-min))
  7748. (or (org-at-heading-p) (outline-next-heading))
  7749. (setq start (point))
  7750. (goto-char (point-max))
  7751. (beginning-of-line 1)
  7752. (when (looking-at ".*?\\S-")
  7753. ;; File ends in a non-white line
  7754. (end-of-line 1)
  7755. (insert "\n"))
  7756. (setq end (point-max))
  7757. (setq what "top-level")
  7758. (goto-char start)
  7759. (show-all)))
  7760. (setq beg (point))
  7761. (if (>= beg end) (error "Nothing to sort"))
  7762. (looking-at "\\(\\*+\\)")
  7763. (setq stars (match-string 1)
  7764. re (concat "^" (regexp-quote stars) " +")
  7765. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7766. txt (buffer-substring beg end))
  7767. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7768. (if (and (not (equal stars "*")) (string-match re2 txt))
  7769. (error "Region to sort contains a level above the first entry"))
  7770. (unless sorting-type
  7771. (message
  7772. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7773. [t]ime [s]cheduled [d]eadline [c]reated
  7774. A/N/P/R/O/F/T/S/D/C means reversed:"
  7775. what)
  7776. (setq sorting-type (read-char-exclusive))
  7777. (unless getkey-func
  7778. (and (= (downcase sorting-type) ?f)
  7779. (setq getkey-func
  7780. (org-icompleting-read "Sort using function: "
  7781. obarray 'fboundp t nil nil))
  7782. (setq getkey-func (intern getkey-func))))
  7783. (and (= (downcase sorting-type) ?r)
  7784. (not property)
  7785. (setq property
  7786. (org-icompleting-read "Property: "
  7787. (mapcar 'list (org-buffer-property-keys t))
  7788. nil t))))
  7789. (message "Sorting entries...")
  7790. (save-restriction
  7791. (narrow-to-region start end)
  7792. (let ((dcst (downcase sorting-type))
  7793. (case-fold-search nil)
  7794. (now (current-time)))
  7795. (sort-subr
  7796. (/= dcst sorting-type)
  7797. ;; This function moves to the beginning character of the "record" to
  7798. ;; be sorted.
  7799. (lambda nil
  7800. (if (re-search-forward re nil t)
  7801. (goto-char (match-beginning 0))
  7802. (goto-char (point-max))))
  7803. ;; This function moves to the last character of the "record" being
  7804. ;; sorted.
  7805. (lambda nil
  7806. (save-match-data
  7807. (condition-case nil
  7808. (outline-forward-same-level 1)
  7809. (error
  7810. (goto-char (point-max))))))
  7811. ;; This function returns the value that gets sorted against.
  7812. (lambda nil
  7813. (cond
  7814. ((= dcst ?n)
  7815. (if (looking-at org-complex-heading-regexp)
  7816. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7817. nil))
  7818. ((= dcst ?a)
  7819. (if (looking-at org-complex-heading-regexp)
  7820. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7821. nil))
  7822. ((= dcst ?t)
  7823. (let ((end (save-excursion (outline-next-heading) (point))))
  7824. (if (or (re-search-forward org-ts-regexp end t)
  7825. (re-search-forward org-ts-regexp-both end t))
  7826. (org-time-string-to-seconds (match-string 0))
  7827. (org-float-time now))))
  7828. ((= dcst ?c)
  7829. (let ((end (save-excursion (outline-next-heading) (point))))
  7830. (if (re-search-forward
  7831. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7832. end t)
  7833. (org-time-string-to-seconds (match-string 0))
  7834. (org-float-time now))))
  7835. ((= dcst ?s)
  7836. (let ((end (save-excursion (outline-next-heading) (point))))
  7837. (if (re-search-forward org-scheduled-time-regexp end t)
  7838. (org-time-string-to-seconds (match-string 1))
  7839. (org-float-time now))))
  7840. ((= dcst ?d)
  7841. (let ((end (save-excursion (outline-next-heading) (point))))
  7842. (if (re-search-forward org-deadline-time-regexp end t)
  7843. (org-time-string-to-seconds (match-string 1))
  7844. (org-float-time now))))
  7845. ((= dcst ?p)
  7846. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7847. (string-to-char (match-string 2))
  7848. org-default-priority))
  7849. ((= dcst ?r)
  7850. (or (org-entry-get nil property) ""))
  7851. ((= dcst ?o)
  7852. (if (looking-at org-complex-heading-regexp)
  7853. (- 9999 (length (member (match-string 2)
  7854. org-todo-keywords-1)))))
  7855. ((= dcst ?f)
  7856. (if getkey-func
  7857. (progn
  7858. (setq tmp (funcall getkey-func))
  7859. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7860. tmp)
  7861. (error "Invalid key function `%s'" getkey-func)))
  7862. (t (error "Invalid sorting type `%c'" sorting-type))))
  7863. nil
  7864. (cond
  7865. ((= dcst ?a) 'string<)
  7866. ((= dcst ?f) compare-func)
  7867. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7868. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7869. ;; Reset the clock marker if needed
  7870. (when cmstr
  7871. (save-excursion
  7872. (goto-char start)
  7873. (search-forward cmstr nil t)
  7874. (move-marker org-clock-marker (point))))
  7875. (message "Sorting entries...done")))
  7876. (defun org-do-sort (table what &optional with-case sorting-type)
  7877. "Sort TABLE of WHAT according to SORTING-TYPE.
  7878. The user will be prompted for the SORTING-TYPE if the call to this
  7879. function does not specify it. WHAT is only for the prompt, to indicate
  7880. what is being sorted. The sorting key will be extracted from
  7881. the car of the elements of the table.
  7882. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7883. (unless sorting-type
  7884. (message
  7885. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7886. what)
  7887. (setq sorting-type (read-char-exclusive)))
  7888. (let ((dcst (downcase sorting-type))
  7889. extractfun comparefun)
  7890. ;; Define the appropriate functions
  7891. (cond
  7892. ((= dcst ?n)
  7893. (setq extractfun 'string-to-number
  7894. comparefun (if (= dcst sorting-type) '< '>)))
  7895. ((= dcst ?a)
  7896. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7897. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7898. comparefun (if (= dcst sorting-type)
  7899. 'string<
  7900. (lambda (a b) (and (not (string< a b))
  7901. (not (string= a b)))))))
  7902. ((= dcst ?t)
  7903. (setq extractfun
  7904. (lambda (x)
  7905. (if (or (string-match org-ts-regexp x)
  7906. (string-match org-ts-regexp-both x))
  7907. (org-float-time
  7908. (org-time-string-to-time (match-string 0 x)))
  7909. 0))
  7910. comparefun (if (= dcst sorting-type) '< '>)))
  7911. (t (error "Invalid sorting type `%c'" sorting-type)))
  7912. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7913. table)
  7914. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7915. ;;; The orgstruct minor mode
  7916. ;; Define a minor mode which can be used in other modes in order to
  7917. ;; integrate the org-mode structure editing commands.
  7918. ;; This is really a hack, because the org-mode structure commands use
  7919. ;; keys which normally belong to the major mode. Here is how it
  7920. ;; works: The minor mode defines all the keys necessary to operate the
  7921. ;; structure commands, but wraps the commands into a function which
  7922. ;; tests if the cursor is currently at a headline or a plain list
  7923. ;; item. If that is the case, the structure command is used,
  7924. ;; temporarily setting many Org-mode variables like regular
  7925. ;; expressions for filling etc. However, when any of those keys is
  7926. ;; used at a different location, function uses `key-binding' to look
  7927. ;; up if the key has an associated command in another currently active
  7928. ;; keymap (minor modes, major mode, global), and executes that
  7929. ;; command. There might be problems if any of the keys is otherwise
  7930. ;; used as a prefix key.
  7931. (defcustom orgstruct-heading-prefix-regexp nil
  7932. "Regexp that matches the custom prefix of Org headlines in
  7933. orgstruct(++)-mode."
  7934. :group 'org
  7935. :version "24.4"
  7936. :package-version '(Org . "8.0")
  7937. :type 'string)
  7938. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7939. (defcustom orgstruct-setup-hook nil
  7940. "Hook run after orgstruct-mode-map is filled."
  7941. :group 'org
  7942. :version "24.4"
  7943. :package-version '(Org . "8.0")
  7944. :type 'hook)
  7945. (defvar orgstruct-initialized nil)
  7946. (defvar org-local-vars nil
  7947. "List of local variables, for use by `orgstruct-mode'.")
  7948. ;;;###autoload
  7949. (define-minor-mode orgstruct-mode
  7950. "Toggle the minor mode `orgstruct-mode'.
  7951. This mode is for using Org-mode structure commands in other
  7952. modes. The following keys behave as if Org-mode were active, if
  7953. the cursor is on a headline, or on a plain list item (both as
  7954. defined by Org-mode)."
  7955. nil " OrgStruct" (make-sparse-keymap)
  7956. (funcall (if orgstruct-mode
  7957. 'add-to-invisibility-spec
  7958. 'remove-from-invisibility-spec)
  7959. '(outline . t))
  7960. (when orgstruct-mode
  7961. (org-load-modules-maybe)
  7962. (unless orgstruct-initialized
  7963. (orgstruct-setup)
  7964. (setq orgstruct-initialized t))))
  7965. ;;;###autoload
  7966. (defun turn-on-orgstruct ()
  7967. "Unconditionally turn on `orgstruct-mode'."
  7968. (orgstruct-mode 1))
  7969. (defvar org-fb-vars nil)
  7970. (make-variable-buffer-local 'org-fb-vars)
  7971. (defun orgstruct++-mode (&optional arg)
  7972. "Toggle `orgstruct-mode', the enhanced version of it.
  7973. In addition to setting orgstruct-mode, this also exports all
  7974. indentation and autofilling variables from org-mode into the
  7975. buffer. It will also recognize item context in multiline items."
  7976. (interactive "P")
  7977. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7978. (if (< arg 1)
  7979. (progn (orgstruct-mode -1)
  7980. (mapc (lambda(v)
  7981. (org-set-local (car v)
  7982. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7983. org-fb-vars))
  7984. (orgstruct-mode 1)
  7985. (setq org-fb-vars nil)
  7986. (let (var val)
  7987. (mapc
  7988. (lambda (x)
  7989. (when (string-match
  7990. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7991. (symbol-name (car x)))
  7992. (setq var (car x) val (nth 1 x))
  7993. (push (list var `(quote ,(eval var))) org-fb-vars)
  7994. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7995. org-local-vars)
  7996. (org-set-local 'orgstruct-is-++ t))))
  7997. (defvar orgstruct-is-++ nil
  7998. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7999. (make-variable-buffer-local 'orgstruct-is-++)
  8000. ;;;###autoload
  8001. (defun turn-on-orgstruct++ ()
  8002. "Unconditionally turn on `orgstruct++-mode'."
  8003. (orgstruct++-mode 1))
  8004. (defun orgstruct-error ()
  8005. "Error when there is no default binding for a structure key."
  8006. (interactive)
  8007. (funcall (if (fboundp 'user-error)
  8008. 'user-error
  8009. 'error)
  8010. "This key has no function outside structure elements"))
  8011. (defun orgstruct-setup ()
  8012. "Setup orgstruct keymap."
  8013. (dolist (cell '((org-demote . t)
  8014. (org-metaleft . t)
  8015. (org-metaright . t)
  8016. (org-promote . t)
  8017. (org-shiftmetaleft . t)
  8018. (org-shiftmetaright . t)
  8019. org-backward-element
  8020. org-backward-heading-same-level
  8021. org-ctrl-c-ret
  8022. org-ctrl-c-minus
  8023. org-ctrl-c-star
  8024. org-cycle
  8025. org-forward-heading-same-level
  8026. org-insert-heading
  8027. org-insert-heading-respect-content
  8028. org-kill-note-or-show-branches
  8029. org-mark-subtree
  8030. org-meta-return
  8031. org-metadown
  8032. org-metaup
  8033. org-narrow-to-subtree
  8034. org-promote-subtree
  8035. org-reveal
  8036. org-shiftdown
  8037. org-shiftleft
  8038. org-shiftmetadown
  8039. org-shiftmetaup
  8040. org-shiftright
  8041. org-shifttab
  8042. org-shifttab
  8043. org-shiftup
  8044. org-show-subtree
  8045. org-sort
  8046. org-up-element
  8047. outline-demote
  8048. outline-next-visible-heading
  8049. outline-previous-visible-heading
  8050. outline-promote
  8051. outline-up-heading
  8052. show-children))
  8053. (let ((f (or (car-safe cell) cell))
  8054. (disable-when-heading-prefix (cdr-safe cell)))
  8055. (when (fboundp f)
  8056. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8057. (where-is-internal f outline-mode-map)))
  8058. ;; TODO use local-function-key-map
  8059. (dolist (rep '(("<tab>" . "TAB")
  8060. ("<return>" . "RET")
  8061. ("<escape>" . "ESC")
  8062. ("<delete>" . "DEL")))
  8063. (setq binding (read-kbd-macro
  8064. (let ((case-fold-search))
  8065. (replace-regexp-in-string
  8066. (regexp-quote (cdr rep))
  8067. (car rep)
  8068. (key-description binding))))))
  8069. (let ((key (lookup-key orgstruct-mode-map binding)))
  8070. (when (or (not key) (numberp key))
  8071. (condition-case nil
  8072. (org-defkey orgstruct-mode-map
  8073. binding
  8074. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8075. (error nil))))))))
  8076. (run-hooks 'orgstruct-setup-hook))
  8077. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8078. "Create a function for binding in the structure minor mode.
  8079. FUN is the command to call inside a table. KEY is the key that
  8080. should be checked in for a command to execute outside of tables.
  8081. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8082. if `orgstruct-heading-prefix-regexp' is non-nil."
  8083. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8084. (let ((nname name)
  8085. (i 0))
  8086. (while (fboundp (intern nname))
  8087. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8088. (setq name (intern nname)))
  8089. (eval
  8090. (let ((bindings '((org-heading-regexp
  8091. (concat "^"
  8092. orgstruct-heading-prefix-regexp
  8093. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8094. (org-outline-regexp
  8095. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8096. (org-outline-regexp-bol
  8097. (concat "^" org-outline-regexp))
  8098. (outline-regexp org-outline-regexp)
  8099. (outline-heading-end-regexp "\n")
  8100. (outline-level 'org-outline-level)
  8101. (outline-heading-alist))))
  8102. `(defun ,name (arg)
  8103. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8104. "Outside of structure, run the binding of `"
  8105. (key-description key) "'."
  8106. (when disable-when-heading-prefix
  8107. (concat
  8108. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8109. "back to the default binding due to limitations of Org's implementation of\n"
  8110. "`" (symbol-name fun) "'.")))
  8111. (interactive "p")
  8112. (let* ((disable
  8113. ,(when disable-when-heading-prefix
  8114. '(and orgstruct-heading-prefix-regexp
  8115. (not (string= orgstruct-heading-prefix-regexp "")))))
  8116. (fallback
  8117. (or disable
  8118. (not
  8119. (let* ,bindings
  8120. (org-context-p 'headline 'item
  8121. ,(when (memq fun '(org-insert-heading))
  8122. '(when orgstruct-is-++
  8123. 'item-body))))))))
  8124. (if fallback
  8125. (let* ((orgstruct-mode)
  8126. (binding
  8127. (loop with key = ,key
  8128. for rep in
  8129. '(nil
  8130. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8131. ("<\\([^>]*\\)return>" . "\\1RET")
  8132. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8133. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8134. do
  8135. (when rep
  8136. (setq key (read-kbd-macro
  8137. (let ((case-fold-search))
  8138. (replace-regexp-in-string
  8139. (car rep)
  8140. (cdr rep)
  8141. (key-description key))))))
  8142. thereis (key-binding key))))
  8143. (if (keymapp binding)
  8144. (set-temporary-overlay-map binding)
  8145. (let ((func (or binding
  8146. (unless disable
  8147. 'orgstruct-error))))
  8148. (when func
  8149. (call-interactively func)))))
  8150. (org-run-like-in-org-mode
  8151. (lambda ()
  8152. (interactive)
  8153. (let* ,bindings
  8154. (call-interactively ',fun)))))))))
  8155. name))
  8156. (defun org-contextualize-keys (alist contexts)
  8157. "Return valid elements in ALIST depending on CONTEXTS.
  8158. `org-agenda-custom-commands' or `org-capture-templates' are the
  8159. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8160. or `org-capture-templates-contexts' are the associated contexts
  8161. definitions."
  8162. (let ((contexts
  8163. ;; normalize contexts
  8164. (mapcar
  8165. (lambda(c) (cond ((listp (cadr c))
  8166. (list (car c) (car c) (cadr c)))
  8167. ((string= "" (cadr c))
  8168. (list (car c) (car c) (caddr c)))
  8169. (t c))) contexts))
  8170. (a alist) c r s)
  8171. ;; loop over all commands or templates
  8172. (while (setq c (pop a))
  8173. (let (vrules repl)
  8174. (cond
  8175. ((not (assoc (car c) contexts))
  8176. (push c r))
  8177. ((and (assoc (car c) contexts)
  8178. (setq vrules (org-contextualize-validate-key
  8179. (car c) contexts)))
  8180. (mapc (lambda (vr)
  8181. (when (not (equal (car vr) (cadr vr)))
  8182. (setq repl vr))) vrules)
  8183. (if (not repl) (push c r)
  8184. (push (cadr repl) s)
  8185. (push
  8186. (cons (car c)
  8187. (cdr (or (assoc (cadr repl) alist)
  8188. (error "Undefined key `%s' as contextual replacement for `%s'"
  8189. (cadr repl) (car c)))))
  8190. r))))))
  8191. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8192. (delq
  8193. nil
  8194. (delete-dups
  8195. (mapcar (lambda (x)
  8196. (let ((tpl (car x)))
  8197. (when (not (delq
  8198. nil
  8199. (mapcar (lambda(y)
  8200. (equal y tpl)) s))) x)))
  8201. (reverse r))))))
  8202. (defun org-contextualize-validate-key (key contexts)
  8203. "Check CONTEXTS for agenda or capture KEY."
  8204. (let (r rr res)
  8205. (while (setq r (pop contexts))
  8206. (mapc
  8207. (lambda (rr)
  8208. (when
  8209. (and (equal key (car r))
  8210. (if (functionp rr) (funcall rr)
  8211. (or (and (eq (car rr) 'in-file)
  8212. (buffer-file-name)
  8213. (string-match (cdr rr) (buffer-file-name)))
  8214. (and (eq (car rr) 'in-mode)
  8215. (string-match (cdr rr) (symbol-name major-mode)))
  8216. (and (eq (car rr) 'in-buffer)
  8217. (string-match (cdr rr) (buffer-name)))
  8218. (when (and (eq (car rr) 'not-in-file)
  8219. (buffer-file-name))
  8220. (not (string-match (cdr rr) (buffer-file-name))))
  8221. (when (eq (car rr) 'not-in-mode)
  8222. (not (string-match (cdr rr) (symbol-name major-mode))))
  8223. (when (eq (car rr) 'not-in-buffer)
  8224. (not (string-match (cdr rr) (buffer-name)))))))
  8225. (push r res)))
  8226. (car (last r))))
  8227. (delete-dups (delq nil res))))
  8228. (defun org-context-p (&rest contexts)
  8229. "Check if local context is any of CONTEXTS.
  8230. Possible values in the list of contexts are `table', `headline', and `item'."
  8231. (let ((pos (point)))
  8232. (goto-char (point-at-bol))
  8233. (prog1 (or (and (memq 'table contexts)
  8234. (looking-at "[ \t]*|"))
  8235. (and (memq 'headline contexts)
  8236. (looking-at org-outline-regexp))
  8237. (and (memq 'item contexts)
  8238. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8239. (and (memq 'item-body contexts)
  8240. (org-in-item-p)))
  8241. (goto-char pos))))
  8242. (defun org-get-local-variables ()
  8243. "Return a list of all local variables in an Org mode buffer."
  8244. (let (varlist)
  8245. (with-current-buffer (get-buffer-create "*Org tmp*")
  8246. (erase-buffer)
  8247. (org-mode)
  8248. (setq varlist (buffer-local-variables)))
  8249. (kill-buffer "*Org tmp*")
  8250. (delq nil
  8251. (mapcar
  8252. (lambda (x)
  8253. (setq x
  8254. (if (symbolp x)
  8255. (list x)
  8256. (list (car x) (cdr x))))
  8257. (if (and (not (get (car x) 'org-state))
  8258. (string-match
  8259. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8260. (symbol-name (car x))))
  8261. x nil))
  8262. varlist))))
  8263. (defun org-clone-local-variables (from-buffer &optional regexp)
  8264. "Clone local variables from FROM-BUFFER.
  8265. Optional argument REGEXP selects variables to clone."
  8266. (mapc
  8267. (lambda (pair)
  8268. (and (symbolp (car pair))
  8269. (or (null regexp)
  8270. (string-match regexp (symbol-name (car pair))))
  8271. (set (make-local-variable (car pair))
  8272. (cdr pair))))
  8273. (buffer-local-variables from-buffer)))
  8274. ;;;###autoload
  8275. (defun org-run-like-in-org-mode (cmd)
  8276. "Run a command, pretending that the current buffer is in Org-mode.
  8277. This will temporarily bind local variables that are typically bound in
  8278. Org-mode to the values they have in Org-mode, and then interactively
  8279. call CMD."
  8280. (org-load-modules-maybe)
  8281. (unless org-local-vars
  8282. (setq org-local-vars (org-get-local-variables)))
  8283. (let (binds)
  8284. (dolist (var org-local-vars)
  8285. (when (or (not (boundp (car var)))
  8286. (eq (symbol-value (car var))
  8287. (default-value (car var))))
  8288. (push (list (car var) `(quote ,(cadr var))) binds)))
  8289. (eval `(let ,binds
  8290. (call-interactively (quote ,cmd))))))
  8291. ;;;; Archiving
  8292. (defun org-get-category (&optional pos force-refresh)
  8293. "Get the category applying to position POS."
  8294. (save-match-data
  8295. (if force-refresh (org-refresh-category-properties))
  8296. (let ((pos (or pos (point))))
  8297. (or (get-text-property pos 'org-category)
  8298. (progn (org-refresh-category-properties)
  8299. (get-text-property pos 'org-category))))))
  8300. (defun org-refresh-category-properties ()
  8301. "Refresh category text properties in the buffer."
  8302. (let ((case-fold-search t)
  8303. (inhibit-read-only t)
  8304. (def-cat (cond
  8305. ((null org-category)
  8306. (if buffer-file-name
  8307. (file-name-sans-extension
  8308. (file-name-nondirectory buffer-file-name))
  8309. "???"))
  8310. ((symbolp org-category) (symbol-name org-category))
  8311. (t org-category)))
  8312. beg end cat pos optionp)
  8313. (org-with-silent-modifications
  8314. (save-excursion
  8315. (save-restriction
  8316. (widen)
  8317. (goto-char (point-min))
  8318. (put-text-property (point) (point-max) 'org-category def-cat)
  8319. (while (re-search-forward
  8320. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8321. (setq pos (match-end 0)
  8322. optionp (equal (char-after (match-beginning 0)) ?#)
  8323. cat (org-trim (match-string 2)))
  8324. (if optionp
  8325. (setq beg (point-at-bol) end (point-max))
  8326. (org-back-to-heading t)
  8327. (setq beg (point) end (org-end-of-subtree t t)))
  8328. (put-text-property beg end 'org-category cat)
  8329. (put-text-property beg end 'org-category-position beg)
  8330. (goto-char pos)))))))
  8331. (defun org-refresh-properties (dprop tprop)
  8332. "Refresh buffer text properties.
  8333. DPROP is the drawer property and TPROP is the corresponding text
  8334. property to set."
  8335. (let ((case-fold-search t)
  8336. (inhibit-read-only t) p)
  8337. (org-with-silent-modifications
  8338. (save-excursion
  8339. (save-restriction
  8340. (widen)
  8341. (goto-char (point-min))
  8342. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8343. (setq p (org-match-string-no-properties 1))
  8344. (save-excursion
  8345. (org-back-to-heading t)
  8346. (put-text-property
  8347. (point-at-bol) (point-at-eol) tprop p))))))))
  8348. ;;;; Link Stuff
  8349. ;;; Link abbreviations
  8350. (defun org-link-expand-abbrev (link)
  8351. "Apply replacements as defined in `org-link-abbrev-alist'."
  8352. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8353. (let* ((key (match-string 1 link))
  8354. (as (or (assoc key org-link-abbrev-alist-local)
  8355. (assoc key org-link-abbrev-alist)))
  8356. (tag (and (match-end 2) (match-string 3 link)))
  8357. rpl)
  8358. (if (not as)
  8359. link
  8360. (setq rpl (cdr as))
  8361. (cond
  8362. ((symbolp rpl) (funcall rpl tag))
  8363. ((string-match "%(\\([^)]+\\))" rpl)
  8364. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8365. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8366. ((string-match "%h" rpl)
  8367. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8368. (t (concat rpl tag)))))
  8369. link))
  8370. ;;; Storing and inserting links
  8371. (defvar org-insert-link-history nil
  8372. "Minibuffer history for links inserted with `org-insert-link'.")
  8373. (defvar org-stored-links nil
  8374. "Contains the links stored with `org-store-link'.")
  8375. (defvar org-store-link-plist nil
  8376. "Plist with info about the most recently link created with `org-store-link'.")
  8377. (defvar org-link-protocols nil
  8378. "Link protocols added to Org-mode using `org-add-link-type'.")
  8379. (defvar org-store-link-functions nil
  8380. "List of functions that are called to create and store a link.
  8381. Each function will be called in turn until one returns a non-nil
  8382. value. Each function should check if it is responsible for creating
  8383. this link (for example by looking at the major mode).
  8384. If not, it must exit and return nil.
  8385. If yes, it should return a non-nil value after a calling
  8386. `org-store-link-props' with a list of properties and values.
  8387. Special properties are:
  8388. :type The link prefix, like \"http\". This must be given.
  8389. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8390. This is obligatory as well.
  8391. :description Optional default description for the second pair
  8392. of brackets in an Org-mode link. The user can still change
  8393. this when inserting this link into an Org-mode buffer.
  8394. In addition to these, any additional properties can be specified
  8395. and then used in capture templates.")
  8396. (defun org-add-link-type (type &optional follow export)
  8397. "Add TYPE to the list of `org-link-types'.
  8398. Re-compute all regular expressions depending on `org-link-types'
  8399. FOLLOW and EXPORT are two functions.
  8400. FOLLOW should take the link path as the single argument and do whatever
  8401. is necessary to follow the link, for example find a file or display
  8402. a mail message.
  8403. EXPORT should format the link path for export to one of the export formats.
  8404. It should be a function accepting three arguments:
  8405. path the path of the link, the text after the prefix (like \"http:\")
  8406. desc the description of the link, if any, or a description added by
  8407. org-export-normalize-links if there is none
  8408. format the export format, a symbol like `html' or `latex' or `ascii'..
  8409. The function may use the FORMAT information to return different values
  8410. depending on the format. The return value will be put literally into
  8411. the exported file. If the return value is nil, this means Org should
  8412. do what it normally does with links which do not have EXPORT defined.
  8413. Org-mode has a built-in default for exporting links. If you are happy with
  8414. this default, there is no need to define an export function for the link
  8415. type. For a simple example of an export function, see `org-bbdb.el'."
  8416. (add-to-list 'org-link-types type t)
  8417. (org-make-link-regexps)
  8418. (if (assoc type org-link-protocols)
  8419. (setcdr (assoc type org-link-protocols) (list follow export))
  8420. (push (list type follow export) org-link-protocols)))
  8421. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8422. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8423. ;;;###autoload
  8424. (defun org-store-link (arg)
  8425. "\\<org-mode-map>Store an org-link to the current location.
  8426. This link is added to `org-stored-links' and can later be inserted
  8427. into an org-buffer with \\[org-insert-link].
  8428. For some link types, a prefix arg is interpreted.
  8429. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8430. For file links, arg negates `org-context-in-file-links'.
  8431. A double prefix arg force skipping storing functions that are not
  8432. part of Org's core.
  8433. A triple prefix arg force storing a link for each line in the
  8434. active region."
  8435. (interactive "P")
  8436. (org-load-modules-maybe)
  8437. (if (and (equal arg '(64)) (org-region-active-p))
  8438. (save-excursion
  8439. (let ((end (region-end)))
  8440. (goto-char (region-beginning))
  8441. (set-mark (point))
  8442. (while (< (point-at-eol) end)
  8443. (move-end-of-line 1) (activate-mark)
  8444. (let (current-prefix-arg)
  8445. (call-interactively 'org-store-link))
  8446. (move-beginning-of-line 2)
  8447. (set-mark (point)))))
  8448. (org-with-limited-levels
  8449. (setq org-store-link-plist nil)
  8450. (let (link cpltxt desc description search
  8451. txt custom-id agenda-link sfuns sfunsn)
  8452. (cond
  8453. ;; Store a link using an external link type
  8454. ((and (not (equal arg '(16)))
  8455. (setq sfuns
  8456. (delq
  8457. nil (mapcar (lambda (f)
  8458. (let (fs) (if (funcall f) (push f fs))))
  8459. org-store-link-functions))
  8460. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8461. (or (and (cdr sfuns)
  8462. (funcall (intern
  8463. (completing-read
  8464. "Which function for creating the link? "
  8465. sfunsn t (car sfunsn)))))
  8466. (funcall (caar sfuns)))
  8467. (setq link (plist-get org-store-link-plist :link)
  8468. desc (or (plist-get org-store-link-plist
  8469. :description) link))))
  8470. ;; Store a link from a source code buffer
  8471. ((org-src-edit-buffer-p)
  8472. (let (label gc)
  8473. (while (or (not label)
  8474. (save-excursion
  8475. (save-restriction
  8476. (widen)
  8477. (goto-char (point-min))
  8478. (re-search-forward
  8479. (regexp-quote (format org-coderef-label-format label))
  8480. nil t))))
  8481. (when label (message "Label exists already") (sit-for 2))
  8482. (setq label (read-string "Code line label: " label)))
  8483. (end-of-line 1)
  8484. (setq link (format org-coderef-label-format label))
  8485. (setq gc (- 79 (length link)))
  8486. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8487. (insert link)
  8488. (setq link (concat "(" label ")") desc nil)))
  8489. ;; We are in the agenda, link to referenced location
  8490. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8491. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8492. (get-text-property (point) 'org-marker))))
  8493. (when m
  8494. (org-with-point-at m
  8495. (setq agenda-link
  8496. (if (org-called-interactively-p 'any)
  8497. (call-interactively 'org-store-link)
  8498. (org-store-link nil)))))))
  8499. ((eq major-mode 'calendar-mode)
  8500. (let ((cd (calendar-cursor-to-date)))
  8501. (setq link
  8502. (format-time-string
  8503. (car org-time-stamp-formats)
  8504. (apply 'encode-time
  8505. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8506. nil nil nil))))
  8507. (org-store-link-props :type "calendar" :date cd)))
  8508. ((eq major-mode 'help-mode)
  8509. (setq link (concat "help:" (save-excursion
  8510. (goto-char (point-min))
  8511. (looking-at "^[^ ]+")
  8512. (match-string 0))))
  8513. (org-store-link-props :type "help"))
  8514. ((eq major-mode 'w3-mode)
  8515. (setq cpltxt (if (and (buffer-name)
  8516. (not (string-match "Untitled" (buffer-name))))
  8517. (buffer-name)
  8518. (url-view-url t))
  8519. link (url-view-url t))
  8520. (org-store-link-props :type "w3" :url (url-view-url t)))
  8521. ((eq major-mode 'image-mode)
  8522. (setq cpltxt (concat "file:"
  8523. (abbreviate-file-name buffer-file-name))
  8524. link cpltxt)
  8525. (org-store-link-props :type "image" :file buffer-file-name))
  8526. ;; In dired, store a link to the file of the current line
  8527. ((eq major-mode 'dired-mode)
  8528. (let ((file (dired-get-filename nil t)))
  8529. (setq file (if file
  8530. (abbreviate-file-name
  8531. (expand-file-name (dired-get-filename nil t)))
  8532. ;; otherwise, no file so use current directory.
  8533. default-directory))
  8534. (setq cpltxt (concat "file:" file)
  8535. link cpltxt)))
  8536. ((setq search (run-hook-with-args-until-success
  8537. 'org-create-file-search-functions))
  8538. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8539. "::" search))
  8540. (setq cpltxt (or description link)))
  8541. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8542. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8543. (cond
  8544. ;; Store a link using the radio target at point
  8545. ((org-in-regexp "<<\\(.*?\\)>>")
  8546. (setq cpltxt
  8547. (concat "file:"
  8548. (abbreviate-file-name
  8549. (buffer-file-name (buffer-base-buffer)))
  8550. "::" (match-string 1))
  8551. link cpltxt))
  8552. ((and (featurep 'org-id)
  8553. (or (eq org-id-link-to-org-use-id t)
  8554. (and (org-called-interactively-p 'any)
  8555. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8556. (and (eq org-id-link-to-org-use-id
  8557. 'create-if-interactive-and-no-custom-id)
  8558. (not custom-id))))
  8559. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8560. ;; Store a link using the ID at point
  8561. (setq link (condition-case nil
  8562. (prog1 (org-id-store-link)
  8563. (setq desc (plist-get org-store-link-plist
  8564. :description)))
  8565. (error
  8566. ;; Probably before first headline, link only to file
  8567. (concat "file:"
  8568. (abbreviate-file-name
  8569. (buffer-file-name (buffer-base-buffer))))))))
  8570. (t
  8571. ;; Just link to current headline
  8572. (setq cpltxt (concat "file:"
  8573. (abbreviate-file-name
  8574. (buffer-file-name (buffer-base-buffer)))))
  8575. ;; Add a context search string
  8576. (when (org-xor org-context-in-file-links arg)
  8577. (let* ((ee (org-element-at-point))
  8578. (et (org-element-type ee))
  8579. (ev (plist-get (cadr ee) :value))
  8580. (ek (plist-get (cadr ee) :key))
  8581. (eok (and (stringp ek) (string-match "name" ek))))
  8582. (setq txt (cond
  8583. ((org-at-heading-p) nil)
  8584. ((and (eq et 'keyword) eok) ev)
  8585. ((org-region-active-p)
  8586. (buffer-substring (region-beginning) (region-end)))))
  8587. (when (or (null txt) (string-match "\\S-" txt))
  8588. (setq cpltxt
  8589. (concat cpltxt "::"
  8590. (condition-case nil
  8591. (org-make-org-heading-search-string txt)
  8592. (error "")))
  8593. desc (or (and (eq et 'keyword) eok ev)
  8594. (nth 4 (ignore-errors (org-heading-components)))
  8595. "NONE")))))
  8596. (if (string-match "::\\'" cpltxt)
  8597. (setq cpltxt (substring cpltxt 0 -2)))
  8598. (setq link cpltxt))))
  8599. ((buffer-file-name (buffer-base-buffer))
  8600. ;; Just link to this file here.
  8601. (setq cpltxt (concat "file:"
  8602. (abbreviate-file-name
  8603. (buffer-file-name (buffer-base-buffer)))))
  8604. ;; Add a context string.
  8605. (when (org-xor org-context-in-file-links arg)
  8606. (setq txt (if (org-region-active-p)
  8607. (buffer-substring (region-beginning) (region-end))
  8608. (buffer-substring (point-at-bol) (point-at-eol))))
  8609. ;; Only use search option if there is some text.
  8610. (when (string-match "\\S-" txt)
  8611. (setq cpltxt
  8612. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8613. desc "NONE")))
  8614. (setq link cpltxt))
  8615. ((org-called-interactively-p 'interactive)
  8616. (user-error "No method for storing a link from this buffer"))
  8617. (t (setq link nil)))
  8618. ;; We're done setting link and desc, clean up
  8619. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8620. (setq link (or link cpltxt)
  8621. desc (or desc cpltxt))
  8622. (cond ((equal desc "NONE") (setq desc nil))
  8623. ((string-match org-bracket-link-regexp desc)
  8624. (setq desc
  8625. (replace-regexp-in-string
  8626. org-bracket-link-regexp
  8627. (concat "\\3" (if (equal (length (match-string 0 desc))
  8628. (length desc)) "*" "")) desc))))
  8629. ;; Return the link
  8630. (if (not (and (or (org-called-interactively-p 'any)
  8631. executing-kbd-macro) link))
  8632. (or agenda-link (and link (org-make-link-string link desc)))
  8633. (push (list link desc) org-stored-links)
  8634. (message "Stored: %s" (or desc link))
  8635. (when custom-id
  8636. (setq link (concat "file:" (abbreviate-file-name
  8637. (buffer-file-name)) "::#" custom-id))
  8638. (push (list link desc) org-stored-links)))))))
  8639. (defun org-store-link-props (&rest plist)
  8640. "Store link properties, extract names and addresses."
  8641. (let (x adr)
  8642. (when (setq x (plist-get plist :from))
  8643. (setq adr (mail-extract-address-components x))
  8644. (setq plist (plist-put plist :fromname (car adr)))
  8645. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8646. (when (setq x (plist-get plist :to))
  8647. (setq adr (mail-extract-address-components x))
  8648. (setq plist (plist-put plist :toname (car adr)))
  8649. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8650. (let ((from (plist-get plist :from))
  8651. (to (plist-get plist :to)))
  8652. (when (and from to org-from-is-user-regexp)
  8653. (setq plist
  8654. (plist-put plist :fromto
  8655. (if (string-match org-from-is-user-regexp from)
  8656. (concat "to %t")
  8657. (concat "from %f"))))))
  8658. (setq org-store-link-plist plist))
  8659. (defun org-add-link-props (&rest plist)
  8660. "Add these properties to the link property list."
  8661. (let (key value)
  8662. (while plist
  8663. (setq key (pop plist) value (pop plist))
  8664. (setq org-store-link-plist
  8665. (plist-put org-store-link-plist key value)))))
  8666. (defun org-email-link-description (&optional fmt)
  8667. "Return the description part of an email link.
  8668. This takes information from `org-store-link-plist' and formats it
  8669. according to FMT (default from `org-email-link-description-format')."
  8670. (setq fmt (or fmt org-email-link-description-format))
  8671. (let* ((p org-store-link-plist)
  8672. (to (plist-get p :toaddress))
  8673. (from (plist-get p :fromaddress))
  8674. (table
  8675. (list
  8676. (cons "%c" (plist-get p :fromto))
  8677. (cons "%F" (plist-get p :from))
  8678. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8679. (cons "%T" (plist-get p :to))
  8680. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8681. (cons "%s" (plist-get p :subject))
  8682. (cons "%d" (plist-get p :date))
  8683. (cons "%m" (plist-get p :message-id)))))
  8684. (when (string-match "%c" fmt)
  8685. ;; Check if the user wrote this message
  8686. (if (and org-from-is-user-regexp from to
  8687. (save-match-data (string-match org-from-is-user-regexp from)))
  8688. (setq fmt (replace-match "to %t" t t fmt))
  8689. (setq fmt (replace-match "from %f" t t fmt))))
  8690. (org-replace-escapes fmt table)))
  8691. (defun org-make-org-heading-search-string (&optional string)
  8692. "Make search string for the current headline or STRING."
  8693. (let ((s (or string
  8694. (and (derived-mode-p 'org-mode)
  8695. (save-excursion
  8696. (org-back-to-heading t)
  8697. (org-element-property :raw-value (org-element-at-point))))))
  8698. (lines org-context-in-file-links))
  8699. (or string (setq s (concat "*" s))) ; Add * for headlines
  8700. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8701. (when (and string (integerp lines) (> lines 0))
  8702. (let ((slines (org-split-string s "\n")))
  8703. (when (< lines (length slines))
  8704. (setq s (mapconcat
  8705. 'identity
  8706. (reverse (nthcdr (- (length slines) lines)
  8707. (reverse slines))) "\n")))))
  8708. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8709. (defun org-make-link-string (link &optional description)
  8710. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8711. (unless (string-match "\\S-" link)
  8712. (error "Empty link"))
  8713. (when (and description
  8714. (stringp description)
  8715. (not (string-match "\\S-" description)))
  8716. (setq description nil))
  8717. (when (stringp description)
  8718. ;; Remove brackets from the description, they are fatal.
  8719. (while (string-match "\\[" description)
  8720. (setq description (replace-match "{" t t description)))
  8721. (while (string-match "\\]" description)
  8722. (setq description (replace-match "}" t t description))))
  8723. (when (equal link description)
  8724. ;; No description needed, it is identical
  8725. (setq description nil))
  8726. (when (and (not description)
  8727. (not (string-match (org-image-file-name-regexp) link))
  8728. (not (equal link (org-link-escape link))))
  8729. (setq description (org-extract-attributes link)))
  8730. (setq link
  8731. (cond ((string-match (org-image-file-name-regexp) link) link)
  8732. ((string-match org-link-types-re link)
  8733. (concat (match-string 1 link)
  8734. (org-link-escape (substring link (match-end 1)))))
  8735. (t (org-link-escape link))))
  8736. (concat "[[" link "]"
  8737. (if description (concat "[" description "]") "")
  8738. "]"))
  8739. (defconst org-link-escape-chars
  8740. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8741. "List of characters that should be escaped in link.
  8742. This is the list that is used for internal purposes.")
  8743. (defconst org-link-escape-chars-browser
  8744. '(?\ )
  8745. "List of escapes for characters that are problematic in links.
  8746. This is the list that is used before handing over to the browser.")
  8747. (defun org-link-escape (text &optional table merge)
  8748. "Return percent escaped representation of TEXT.
  8749. TEXT is a string with the text to escape.
  8750. Optional argument TABLE is a list with characters that should be
  8751. escaped. When nil, `org-link-escape-chars' is used.
  8752. If optional argument MERGE is set, merge TABLE into
  8753. `org-link-escape-chars'."
  8754. (cond
  8755. ((and table merge)
  8756. (mapc (lambda (defchr)
  8757. (unless (member defchr table)
  8758. (setq table (cons defchr table)))) org-link-escape-chars))
  8759. ((null table)
  8760. (setq table org-link-escape-chars)))
  8761. (mapconcat
  8762. (lambda (char)
  8763. (if (or (member char table)
  8764. (and (or (< char 32) (= char 37) (> char 126))
  8765. org-url-hexify-p))
  8766. (mapconcat (lambda (sequence-element)
  8767. (format "%%%.2X" sequence-element))
  8768. (or (encode-coding-char char 'utf-8)
  8769. (error "Unable to percent escape character: %s"
  8770. (char-to-string char))) "")
  8771. (char-to-string char))) text ""))
  8772. (defun org-link-unescape (str)
  8773. "Unhex hexified Unicode strings as returned from the JavaScript function
  8774. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8775. (unless (and (null str) (string= "" str))
  8776. (let ((pos 0) (case-fold-search t) unhexed)
  8777. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8778. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8779. (setq str (replace-match unhexed t t str))
  8780. (setq pos (+ pos (length unhexed))))))
  8781. str)
  8782. (defun org-link-unescape-compound (hex)
  8783. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8784. Note: this function also decodes single byte encodings like
  8785. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8786. (save-match-data
  8787. (let* ((bytes (cdr (split-string hex "%")))
  8788. (ret "")
  8789. (eat 0)
  8790. (sum 0))
  8791. (while bytes
  8792. (let* ((val (string-to-number (pop bytes) 16))
  8793. (shift-xor
  8794. (if (= 0 eat)
  8795. (cond
  8796. ((>= val 252) (cons 6 252))
  8797. ((>= val 248) (cons 5 248))
  8798. ((>= val 240) (cons 4 240))
  8799. ((>= val 224) (cons 3 224))
  8800. ((>= val 192) (cons 2 192))
  8801. (t (cons 0 0)))
  8802. (cons 6 128))))
  8803. (if (>= val 192) (setq eat (car shift-xor)))
  8804. (setq val (logxor val (cdr shift-xor)))
  8805. (setq sum (+ (lsh sum (car shift-xor)) val))
  8806. (if (> eat 0) (setq eat (- eat 1)))
  8807. (cond
  8808. ((= 0 eat) ;multi byte
  8809. (setq ret (concat ret (org-char-to-string sum)))
  8810. (setq sum 0))
  8811. ((not bytes) ; single byte(s)
  8812. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8813. )) ;; end (while bytes
  8814. ret )))
  8815. (defun org-link-unescape-single-byte-sequence (hex)
  8816. "Unhexify hex-encoded single byte character sequences."
  8817. (mapconcat (lambda (byte)
  8818. (char-to-string (string-to-number byte 16)))
  8819. (cdr (split-string hex "%")) ""))
  8820. (defun org-xor (a b)
  8821. "Exclusive or."
  8822. (if a (not b) b))
  8823. (defun org-fixup-message-id-for-http (s)
  8824. "Replace special characters in a message id, so it can be used in an http query."
  8825. (when (string-match "%" s)
  8826. (setq s (mapconcat (lambda (c)
  8827. (if (eq c ?%)
  8828. "%25"
  8829. (char-to-string c)))
  8830. s "")))
  8831. (while (string-match "<" s)
  8832. (setq s (replace-match "%3C" t t s)))
  8833. (while (string-match ">" s)
  8834. (setq s (replace-match "%3E" t t s)))
  8835. (while (string-match "@" s)
  8836. (setq s (replace-match "%40" t t s)))
  8837. s)
  8838. (defun org-link-prettify (link)
  8839. "Return a human-readable representation of LINK.
  8840. The car of LINK must be a raw link the cdr of LINK must be either
  8841. a link description or nil."
  8842. (let ((desc (or (cadr link) "<no description>")))
  8843. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8844. "<" (car link) ">")))
  8845. ;;;###autoload
  8846. (defun org-insert-link-global ()
  8847. "Insert a link like Org-mode does.
  8848. This command can be called in any mode to insert a link in Org-mode syntax."
  8849. (interactive)
  8850. (org-load-modules-maybe)
  8851. (org-run-like-in-org-mode 'org-insert-link))
  8852. (defun org-insert-all-links (&optional keep)
  8853. "Insert all links in `org-stored-links'."
  8854. (interactive "P")
  8855. (let ((links (copy-sequence org-stored-links)) l)
  8856. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8857. (insert "- ")
  8858. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8859. (insert "\n"))))
  8860. (defun org-link-fontify-links-to-this-file ()
  8861. "Fontify links to the current file in `org-stored-links'."
  8862. (let ((f (buffer-file-name)) a b)
  8863. (setq a (mapcar (lambda(l)
  8864. (let ((ll (car l)))
  8865. (when (and (string-match "^file:\\(.+\\)::" ll)
  8866. (equal f (expand-file-name (match-string 1 ll))))
  8867. ll)))
  8868. org-stored-links))
  8869. (when (featurep 'org-id)
  8870. (setq b (mapcar (lambda(l)
  8871. (let ((ll (car l)))
  8872. (when (and (string-match "^id:\\(.+\\)$" ll)
  8873. (equal f (expand-file-name
  8874. (or (org-id-find-id-file
  8875. (match-string 1 ll)) ""))))
  8876. ll)))
  8877. org-stored-links)))
  8878. (mapcar (lambda(l)
  8879. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8880. (delq nil (append a b)))))
  8881. (defvar org-link-links-in-this-file nil)
  8882. (defun org-insert-link (&optional complete-file link-location default-description)
  8883. "Insert a link. At the prompt, enter the link.
  8884. Completion can be used to insert any of the link protocol prefixes like
  8885. http or ftp in use.
  8886. The history can be used to select a link previously stored with
  8887. `org-store-link'. When the empty string is entered (i.e. if you just
  8888. press RET at the prompt), the link defaults to the most recently
  8889. stored link. As SPC triggers completion in the minibuffer, you need to
  8890. use M-SPC or C-q SPC to force the insertion of a space character.
  8891. You will also be prompted for a description, and if one is given, it will
  8892. be displayed in the buffer instead of the link.
  8893. If there is already a link at point, this command will allow you to edit link
  8894. and description parts.
  8895. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8896. be selected using completion. The path to the file will be relative to the
  8897. current directory if the file is in the current directory or a subdirectory.
  8898. Otherwise, the link will be the absolute path as completed in the minibuffer
  8899. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8900. option `org-link-file-path-type'.
  8901. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8902. the current directory or below.
  8903. With three \\[universal-argument] prefixes, negate the meaning of
  8904. `org-keep-stored-link-after-insertion'.
  8905. If `org-make-link-description-function' is non-nil, this function will be
  8906. called with the link target, and the result will be the default
  8907. link description.
  8908. If the LINK-LOCATION parameter is non-nil, this value will be
  8909. used as the link location instead of reading one interactively.
  8910. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8911. be used as the default description."
  8912. (interactive "P")
  8913. (let* ((wcf (current-window-configuration))
  8914. (origbuf (current-buffer))
  8915. (region (if (org-region-active-p)
  8916. (buffer-substring (region-beginning) (region-end))))
  8917. (remove (and region (list (region-beginning) (region-end))))
  8918. (desc region)
  8919. tmphist ; byte-compile incorrectly complains about this
  8920. (link link-location)
  8921. (abbrevs org-link-abbrev-alist-local)
  8922. entry file all-prefixes auto-desc)
  8923. (cond
  8924. (link-location) ; specified by arg, just use it.
  8925. ((org-in-regexp org-bracket-link-regexp 1)
  8926. ;; We do have a link at point, and we are going to edit it.
  8927. (setq remove (list (match-beginning 0) (match-end 0)))
  8928. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8929. (setq link (read-string "Link: "
  8930. (org-link-unescape
  8931. (org-match-string-no-properties 1)))))
  8932. ((or (org-in-regexp org-angle-link-re)
  8933. (org-in-regexp org-plain-link-re))
  8934. ;; Convert to bracket link
  8935. (setq remove (list (match-beginning 0) (match-end 0))
  8936. link (read-string "Link: "
  8937. (org-remove-angle-brackets (match-string 0)))))
  8938. ((member complete-file '((4) (16)))
  8939. ;; Completing read for file names.
  8940. (setq link (org-file-complete-link complete-file)))
  8941. (t
  8942. ;; Read link, with completion for stored links.
  8943. (org-link-fontify-links-to-this-file)
  8944. (org-switch-to-buffer-other-window "*Org Links*")
  8945. (with-current-buffer "*Org Links*"
  8946. (erase-buffer)
  8947. (insert "Insert a link.
  8948. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8949. (when org-stored-links
  8950. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8951. (insert (mapconcat 'org-link-prettify
  8952. (reverse org-stored-links) "\n")))
  8953. (goto-char (point-min)))
  8954. (let ((cw (selected-window)))
  8955. (select-window (get-buffer-window "*Org Links*" 'visible))
  8956. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8957. (unless (pos-visible-in-window-p (point-max))
  8958. (org-fit-window-to-buffer))
  8959. (and (window-live-p cw) (select-window cw)))
  8960. ;; Fake a link history, containing the stored links.
  8961. (setq tmphist (append (mapcar 'car org-stored-links)
  8962. org-insert-link-history))
  8963. (setq all-prefixes (append (mapcar 'car abbrevs)
  8964. (mapcar 'car org-link-abbrev-alist)
  8965. org-link-types))
  8966. (unwind-protect
  8967. (progn
  8968. (setq link
  8969. (org-completing-read
  8970. "Link: "
  8971. (append
  8972. (mapcar (lambda (x) (concat x ":"))
  8973. all-prefixes)
  8974. (mapcar 'car org-stored-links))
  8975. nil nil nil
  8976. 'tmphist
  8977. (caar org-stored-links)))
  8978. (if (not (string-match "\\S-" link))
  8979. (error "No link selected"))
  8980. (mapc (lambda(l)
  8981. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8982. org-stored-links)
  8983. (if (or (member link all-prefixes)
  8984. (and (equal ":" (substring link -1))
  8985. (member (substring link 0 -1) all-prefixes)
  8986. (setq link (substring link 0 -1))))
  8987. (setq link (with-current-buffer origbuf
  8988. (org-link-try-special-completion link)))))
  8989. (set-window-configuration wcf)
  8990. (kill-buffer "*Org Links*"))
  8991. (setq entry (assoc link org-stored-links))
  8992. (or entry (push link org-insert-link-history))
  8993. (setq desc (or desc (nth 1 entry)))))
  8994. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8995. (not org-keep-stored-link-after-insertion))
  8996. (setq org-stored-links (delq (assoc link org-stored-links)
  8997. org-stored-links)))
  8998. (if (string-match org-plain-link-re link)
  8999. ;; URL-like link, normalize the use of angular brackets.
  9000. (setq link (org-remove-angle-brackets link)))
  9001. ;; Check if we are linking to the current file with a search
  9002. ;; option If yes, simplify the link by using only the search
  9003. ;; option.
  9004. (when (and buffer-file-name
  9005. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9006. (let* ((path (match-string 1 link))
  9007. (case-fold-search nil)
  9008. (search (match-string 2 link)))
  9009. (save-match-data
  9010. (if (equal (file-truename buffer-file-name) (file-truename path))
  9011. ;; We are linking to this same file, with a search option
  9012. (setq link search)))))
  9013. ;; Check if we can/should use a relative path. If yes, simplify the link
  9014. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9015. (let* ((type (match-string 1 link))
  9016. (path (match-string 2 link))
  9017. (origpath path)
  9018. (case-fold-search nil))
  9019. (cond
  9020. ((or (eq org-link-file-path-type 'absolute)
  9021. (equal complete-file '(16)))
  9022. (setq path (abbreviate-file-name (expand-file-name path))))
  9023. ((eq org-link-file-path-type 'noabbrev)
  9024. (setq path (expand-file-name path)))
  9025. ((eq org-link-file-path-type 'relative)
  9026. (setq path (file-relative-name path)))
  9027. (t
  9028. (save-match-data
  9029. (if (string-match (concat "^" (regexp-quote
  9030. (expand-file-name
  9031. (file-name-as-directory
  9032. default-directory))))
  9033. (expand-file-name path))
  9034. ;; We are linking a file with relative path name.
  9035. (setq path (substring (expand-file-name path)
  9036. (match-end 0)))
  9037. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9038. (setq link (concat type path))
  9039. (if (equal desc origpath)
  9040. (setq desc path))))
  9041. (if org-make-link-description-function
  9042. (setq desc
  9043. (or (condition-case nil
  9044. (funcall org-make-link-description-function link desc)
  9045. (error (progn (message "Can't get link description from `%s'"
  9046. (symbol-name org-make-link-description-function))
  9047. (sit-for 2) nil)))
  9048. (read-string "Description: " default-description)))
  9049. (if default-description (setq desc default-description)
  9050. (setq desc (or (and auto-desc desc)
  9051. (read-string "Description: " desc)))))
  9052. (unless (string-match "\\S-" desc) (setq desc nil))
  9053. (if remove (apply 'delete-region remove))
  9054. (insert (org-make-link-string link desc))))
  9055. (defun org-link-try-special-completion (type)
  9056. "If there is completion support for link type TYPE, offer it."
  9057. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9058. (if (functionp fun)
  9059. (funcall fun)
  9060. (read-string "Link (no completion support): " (concat type ":")))))
  9061. (defun org-file-complete-link (&optional arg)
  9062. "Create a file link using completion."
  9063. (let (file link)
  9064. (setq file (org-iread-file-name "File: "))
  9065. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9066. (pwd1 (file-name-as-directory (abbreviate-file-name
  9067. (expand-file-name ".")))))
  9068. (cond
  9069. ((equal arg '(16))
  9070. (setq link (concat
  9071. "file:"
  9072. (abbreviate-file-name (expand-file-name file)))))
  9073. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9074. (setq link (concat "file:" (match-string 1 file))))
  9075. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9076. (expand-file-name file))
  9077. (setq link (concat
  9078. "file:" (match-string 1 (expand-file-name file)))))
  9079. (t (setq link (concat "file:" file)))))
  9080. link))
  9081. (defun org-iread-file-name (&rest args)
  9082. "Read-file-name using `ido-mode' speedup if available.
  9083. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9084. See `read-file-name' for a description of parameters."
  9085. (org-without-partial-completion
  9086. (if (and org-completion-use-ido
  9087. (fboundp 'ido-read-file-name)
  9088. (boundp 'ido-mode) ido-mode
  9089. (listp (second args)))
  9090. (let ((ido-enter-matching-directory nil))
  9091. (apply 'ido-read-file-name args))
  9092. (apply 'read-file-name args))))
  9093. (defun org-completing-read (&rest args)
  9094. "Completing-read with SPACE being a normal character."
  9095. (let ((enable-recursive-minibuffers t)
  9096. (minibuffer-local-completion-map
  9097. (copy-keymap minibuffer-local-completion-map)))
  9098. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9099. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9100. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9101. (apply 'org-icompleting-read args)))
  9102. (defun org-completing-read-no-i (&rest args)
  9103. (let (org-completion-use-ido org-completion-use-iswitchb)
  9104. (apply 'org-completing-read args)))
  9105. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9106. "Use iswitch as a completing-read replacement to choose from choices.
  9107. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9108. from."
  9109. (let* ((iswitchb-use-virtual-buffers nil)
  9110. (iswitchb-make-buflist-hook
  9111. (lambda ()
  9112. (setq iswitchb-temp-buflist choices))))
  9113. (iswitchb-read-buffer prompt)))
  9114. (defun org-icompleting-read (&rest args)
  9115. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9116. (org-without-partial-completion
  9117. (if (and org-completion-use-ido
  9118. (fboundp 'ido-completing-read)
  9119. (boundp 'ido-mode) ido-mode
  9120. (listp (second args)))
  9121. (let ((ido-enter-matching-directory nil))
  9122. (apply 'ido-completing-read (concat (car args))
  9123. (if (consp (car (nth 1 args)))
  9124. (mapcar 'car (nth 1 args))
  9125. (nth 1 args))
  9126. (cddr args)))
  9127. (if (and org-completion-use-iswitchb
  9128. (boundp 'iswitchb-mode) iswitchb-mode
  9129. (listp (second args)))
  9130. (apply 'org-iswitchb-completing-read (concat (car args))
  9131. (if (consp (car (nth 1 args)))
  9132. (mapcar 'car (nth 1 args))
  9133. (nth 1 args))
  9134. (cddr args))
  9135. (apply 'completing-read args)))))
  9136. (defun org-extract-attributes (s)
  9137. "Extract the attributes cookie from a string and set as text property."
  9138. (let (a attr (start 0) key value)
  9139. (save-match-data
  9140. (when (string-match "{{\\([^}]+\\)}}$" s)
  9141. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9142. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9143. (setq key (match-string 1 a) value (match-string 2 a)
  9144. start (match-end 0)
  9145. attr (plist-put attr (intern key) value))))
  9146. (org-add-props s nil 'org-attr attr))
  9147. s))
  9148. (defun org-extract-attributes-from-string (tag)
  9149. (let (key value attr)
  9150. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  9151. (setq key (match-string 1 tag) value (match-string 2 tag)
  9152. tag (replace-match "" t t tag)
  9153. attr (plist-put attr (intern key) value)))
  9154. (cons tag attr)))
  9155. (defun org-attributes-to-string (plist)
  9156. "Format a property list into an HTML attribute list."
  9157. (let ((s "") key value)
  9158. (while plist
  9159. (setq key (pop plist) value (pop plist))
  9160. (and value
  9161. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  9162. s))
  9163. ;;; Opening/following a link
  9164. (defvar org-link-search-failed nil)
  9165. (defvar org-open-link-functions nil
  9166. "Hook for functions finding a plain text link.
  9167. These functions must take a single argument, the link content.
  9168. They will be called for links that look like [[link text][description]]
  9169. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9170. like a filename (e.g. \"./blue.png\").
  9171. These functions will be called *before* Org attempts to resolve the
  9172. link by doing text searches in the current buffer - so if you want a
  9173. link \"[[target]]\" to still find \"<<target>>\", your function should
  9174. handle this as a special case.
  9175. When the function does handle the link, it must return a non-nil value.
  9176. If it decides that it is not responsible for this link, it must return
  9177. nil to indicate that that Org-mode can continue with other options
  9178. like exact and fuzzy text search.")
  9179. (defun org-next-link (&optional search-backward)
  9180. "Move forward to the next link.
  9181. If the link is in hidden text, expose it."
  9182. (interactive "P")
  9183. (when (and org-link-search-failed (eq this-command last-command))
  9184. (goto-char (point-min))
  9185. (message "Link search wrapped back to beginning of buffer"))
  9186. (setq org-link-search-failed nil)
  9187. (let* ((pos (point))
  9188. (ct (org-context))
  9189. (a (assoc :link ct))
  9190. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9191. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9192. ((looking-at org-any-link-re)
  9193. ;; Don't stay stuck at link without an org-link face
  9194. (forward-char (if search-backward -1 1))))
  9195. (if (funcall srch-fun org-any-link-re nil t)
  9196. (progn
  9197. (goto-char (match-beginning 0))
  9198. (if (outline-invisible-p) (org-show-context)))
  9199. (goto-char pos)
  9200. (setq org-link-search-failed t)
  9201. (message "No further link found"))))
  9202. (defun org-previous-link ()
  9203. "Move backward to the previous link.
  9204. If the link is in hidden text, expose it."
  9205. (interactive)
  9206. (funcall 'org-next-link t))
  9207. (defun org-translate-link (s)
  9208. "Translate a link string if a translation function has been defined."
  9209. (if (and org-link-translation-function
  9210. (fboundp org-link-translation-function)
  9211. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9212. (progn
  9213. (setq s (funcall org-link-translation-function
  9214. (match-string 1 s) (match-string 2 s)))
  9215. (concat (car s) ":" (cdr s)))
  9216. s))
  9217. (defun org-translate-link-from-planner (type path)
  9218. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9219. This is still an experimental function, your mileage may vary."
  9220. (cond
  9221. ((member type '("http" "https" "news" "ftp"))
  9222. ;; standard Internet links are the same.
  9223. nil)
  9224. ((and (equal type "irc") (string-match "^//" path))
  9225. ;; Planner has two / at the beginning of an irc link, we have 1.
  9226. ;; We should have zero, actually....
  9227. (setq path (substring path 1)))
  9228. ((and (equal type "lisp") (string-match "^/" path))
  9229. ;; Planner has a slash, we do not.
  9230. (setq type "elisp" path (substring path 1)))
  9231. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9232. ;; A typical message link. Planner has the id after the final slash,
  9233. ;; we separate it with a hash mark
  9234. (setq path (concat (match-string 1 path) "#"
  9235. (org-remove-angle-brackets (match-string 2 path)))))
  9236. )
  9237. (cons type path))
  9238. (defun org-find-file-at-mouse (ev)
  9239. "Open file link or URL at mouse."
  9240. (interactive "e")
  9241. (mouse-set-point ev)
  9242. (org-open-at-point 'in-emacs))
  9243. (defun org-open-at-mouse (ev)
  9244. "Open file link or URL at mouse.
  9245. See the docstring of `org-open-file' for details."
  9246. (interactive "e")
  9247. (mouse-set-point ev)
  9248. (if (eq major-mode 'org-agenda-mode)
  9249. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9250. (org-open-at-point))
  9251. (defvar org-window-config-before-follow-link nil
  9252. "The window configuration before following a link.
  9253. This is saved in case the need arises to restore it.")
  9254. (defvar org-open-link-marker (make-marker)
  9255. "Marker pointing to the location where `org-open-at-point; was called.")
  9256. ;;;###autoload
  9257. (defun org-open-at-point-global ()
  9258. "Follow a link like Org-mode does.
  9259. This command can be called in any mode to follow a link that has
  9260. Org-mode syntax."
  9261. (interactive)
  9262. (org-run-like-in-org-mode 'org-open-at-point))
  9263. ;;;###autoload
  9264. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9265. "Open a link in the string S, as if it was in Org-mode."
  9266. (interactive "sLink: \nP")
  9267. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9268. (with-temp-buffer
  9269. (let ((org-inhibit-startup (not reference-buffer)))
  9270. (org-mode)
  9271. (insert s)
  9272. (goto-char (point-min))
  9273. (when reference-buffer
  9274. (setq org-link-abbrev-alist-local
  9275. (with-current-buffer reference-buffer
  9276. org-link-abbrev-alist-local)))
  9277. (org-open-at-point arg reference-buffer)))))
  9278. (defvar org-open-at-point-functions nil
  9279. "Hook that is run when following a link at point.
  9280. Functions in this hook must return t if they identify and follow
  9281. a link at point. If they don't find anything interesting at point,
  9282. they must return nil.")
  9283. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9284. (defun org-open-at-point (&optional arg reference-buffer)
  9285. "Open link at or after point.
  9286. If there is no link at point, this function will search forward up to
  9287. the end of the current line.
  9288. Normally, files will be opened by an appropriate application. If the
  9289. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9290. With a double prefix argument, try to open outside of Emacs, in the
  9291. application the system uses for this file type."
  9292. (interactive "P")
  9293. ;; if in a code block, then open the block's results
  9294. (unless (call-interactively #'org-babel-open-src-block-result)
  9295. (org-load-modules-maybe)
  9296. (move-marker org-open-link-marker (point))
  9297. (setq org-window-config-before-follow-link (current-window-configuration))
  9298. (org-remove-occur-highlights nil nil t)
  9299. (cond
  9300. ((and (org-at-heading-p)
  9301. (not (org-at-timestamp-p t))
  9302. (not (org-in-regexp
  9303. (concat org-plain-link-re "\\|"
  9304. org-bracket-link-regexp "\\|"
  9305. org-angle-link-re "\\|"
  9306. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9307. (not (get-text-property (point) 'org-linked-text)))
  9308. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9309. (lk0 (car lkall))
  9310. (lk (if (stringp lk0) (list lk0) lk0))
  9311. (lkend (cdr lkall)))
  9312. (mapcar (lambda(l)
  9313. (search-forward l nil lkend)
  9314. (goto-char (match-beginning 0))
  9315. (org-open-at-point))
  9316. lk))
  9317. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9318. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9319. ((and (org-at-timestamp-p t)
  9320. (not (org-in-regexp org-bracket-link-regexp)))
  9321. (org-follow-timestamp-link))
  9322. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9323. (not (org-in-regexp org-any-link-re)))
  9324. (org-footnote-action))
  9325. (t
  9326. (let (type path link line search (pos (point)))
  9327. (catch 'match
  9328. (save-excursion
  9329. (skip-chars-forward "^]\n\r")
  9330. (when (org-in-regexp org-bracket-link-regexp 1)
  9331. (setq link (org-extract-attributes
  9332. (org-link-unescape (org-match-string-no-properties 1))))
  9333. (while (string-match " *\n *" link)
  9334. (setq link (replace-match " " t t link)))
  9335. (setq link (org-link-expand-abbrev link))
  9336. (cond
  9337. ((or (file-name-absolute-p link)
  9338. (string-match "^\\.\\.?/" link))
  9339. (setq type "file" path link))
  9340. ((string-match org-link-re-with-space3 link)
  9341. (setq type (match-string 1 link) path (match-string 2 link)))
  9342. ((string-match "^help:+\\(.+\\)" link)
  9343. (setq type "help" path (match-string 1 link)))
  9344. (t (setq type "thisfile" path link)))
  9345. (throw 'match t)))
  9346. (when (get-text-property (point) 'org-linked-text)
  9347. (setq type "thisfile"
  9348. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9349. (1+ (point)) (point))
  9350. path (buffer-substring
  9351. (or (previous-single-property-change pos 'org-linked-text)
  9352. (point-min))
  9353. (or (next-single-property-change pos 'org-linked-text)
  9354. (point-max))))
  9355. (throw 'match t))
  9356. (save-excursion
  9357. (when (or (org-in-regexp org-angle-link-re)
  9358. (let ((match (org-in-regexp org-plain-link-re)))
  9359. ;; Check a plain link is not within a bracket link
  9360. (and match
  9361. (save-excursion
  9362. (progn
  9363. (goto-char (car match))
  9364. (not (org-in-regexp org-bracket-link-regexp))))))
  9365. (let ((line_ending (save-excursion (end-of-line) (point))))
  9366. ;; We are in a line before a plain or bracket link
  9367. (or (re-search-forward org-plain-link-re line_ending t)
  9368. (re-search-forward org-bracket-link-regexp line_ending t))))
  9369. (setq type (match-string 1)
  9370. path (org-link-unescape (match-string 2)))
  9371. (throw 'match t)))
  9372. (save-excursion
  9373. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9374. (setq type "tags"
  9375. path (match-string 1))
  9376. (while (string-match ":" path)
  9377. (setq path (replace-match "+" t t path)))
  9378. (throw 'match t)))
  9379. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9380. (setq type "tree-match"
  9381. path (match-string 1))
  9382. (throw 'match t)))
  9383. (unless path
  9384. (user-error "No link found"))
  9385. ;; switch back to reference buffer
  9386. ;; needed when if called in a temporary buffer through
  9387. ;; org-open-link-from-string
  9388. (with-current-buffer (or reference-buffer (current-buffer))
  9389. ;; Remove any trailing spaces in path
  9390. (if (string-match " +\\'" path)
  9391. (setq path (replace-match "" t t path)))
  9392. (if (and org-link-translation-function
  9393. (fboundp org-link-translation-function))
  9394. ;; Check if we need to translate the link
  9395. (let ((tmp (funcall org-link-translation-function type path)))
  9396. (setq type (car tmp) path (cdr tmp))))
  9397. (cond
  9398. ((assoc type org-link-protocols)
  9399. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9400. ((equal type "help")
  9401. (let ((f-or-v (intern path)))
  9402. (cond ((fboundp f-or-v)
  9403. (describe-function f-or-v))
  9404. ((boundp f-or-v)
  9405. (describe-variable f-or-v))
  9406. (t (error "Not a known function or variable")))))
  9407. ((equal type "mailto")
  9408. (let ((cmd (car org-link-mailto-program))
  9409. (args (cdr org-link-mailto-program)) args1
  9410. (address path) (subject "") a)
  9411. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9412. (setq address (match-string 1 path)
  9413. subject (org-link-escape (match-string 2 path))))
  9414. (while args
  9415. (cond
  9416. ((not (stringp (car args))) (push (pop args) args1))
  9417. (t (setq a (pop args))
  9418. (if (string-match "%a" a)
  9419. (setq a (replace-match address t t a)))
  9420. (if (string-match "%s" a)
  9421. (setq a (replace-match subject t t a)))
  9422. (push a args1))))
  9423. (apply cmd (nreverse args1))))
  9424. ((member type '("http" "https" "ftp" "news"))
  9425. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9426. (org-link-escape
  9427. path org-link-escape-chars-browser)
  9428. path))))
  9429. ((string= type "doi")
  9430. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9431. (org-link-escape
  9432. path org-link-escape-chars-browser)
  9433. path))))
  9434. ((member type '("message"))
  9435. (browse-url (concat type ":" path)))
  9436. ((string= type "tags")
  9437. (org-tags-view arg path))
  9438. ((string= type "tree-match")
  9439. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9440. ((string= type "file")
  9441. (if (string-match "::\\([0-9]+\\)\\'" path)
  9442. (setq line (string-to-number (match-string 1 path))
  9443. path (substring path 0 (match-beginning 0)))
  9444. (if (string-match "::\\(.+\\)\\'" path)
  9445. (setq search (match-string 1 path)
  9446. path (substring path 0 (match-beginning 0)))))
  9447. (if (string-match "[*?{]" (file-name-nondirectory path))
  9448. (dired path)
  9449. (org-open-file path arg line search)))
  9450. ((string= type "shell")
  9451. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9452. (cmd path))
  9453. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9454. (string-match org-confirm-shell-link-not-regexp cmd))
  9455. (not org-confirm-shell-link-function)
  9456. (funcall org-confirm-shell-link-function
  9457. (format "Execute \"%s\" in shell? "
  9458. (org-add-props cmd nil
  9459. 'face 'org-warning))))
  9460. (progn
  9461. (message "Executing %s" cmd)
  9462. (shell-command cmd buf)
  9463. (if (featurep 'midnight)
  9464. (setq clean-buffer-list-kill-buffer-names
  9465. (cons buf clean-buffer-list-kill-buffer-names))))
  9466. (error "Abort"))))
  9467. ((string= type "elisp")
  9468. (let ((cmd path))
  9469. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9470. (string-match org-confirm-elisp-link-not-regexp cmd))
  9471. (not org-confirm-elisp-link-function)
  9472. (funcall org-confirm-elisp-link-function
  9473. (format "Execute \"%s\" as elisp? "
  9474. (org-add-props cmd nil
  9475. 'face 'org-warning))))
  9476. (message "%s => %s" cmd
  9477. (if (equal (string-to-char cmd) ?\()
  9478. (eval (read cmd))
  9479. (call-interactively (read cmd))))
  9480. (error "Abort"))))
  9481. ((and (string= type "thisfile")
  9482. (run-hook-with-args-until-success
  9483. 'org-open-link-functions path)))
  9484. ((string= type "thisfile")
  9485. (if arg
  9486. (switch-to-buffer-other-window
  9487. (org-get-buffer-for-internal-link (current-buffer)))
  9488. (org-mark-ring-push))
  9489. (let ((cmd `(org-link-search
  9490. ,path
  9491. ,(cond ((equal arg '(4)) ''occur)
  9492. ((equal arg '(16)) ''org-occur))
  9493. ,pos)))
  9494. (condition-case nil (let ((org-link-search-inhibit-query t))
  9495. (eval cmd))
  9496. (error (progn (widen) (eval cmd))))))
  9497. (t (browse-url-at-point)))))))
  9498. (move-marker org-open-link-marker nil)
  9499. (run-hook-with-args 'org-follow-link-hook)))
  9500. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9501. "Offer links in the current entry and return the selected link.
  9502. If there is only one link, return it.
  9503. If NTH is an integer, return the NTH link found.
  9504. If ZERO is a string, check also this string for a link, and if
  9505. there is one, return it."
  9506. (with-current-buffer buffer
  9507. (save-excursion
  9508. (save-restriction
  9509. (widen)
  9510. (goto-char marker)
  9511. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9512. "\\(" org-angle-link-re "\\)\\|"
  9513. "\\(" org-plain-link-re "\\)"))
  9514. (cnt ?0)
  9515. (in-emacs (if (integerp nth) nil nth))
  9516. have-zero end links link c)
  9517. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9518. (push (match-string 0 zero) links)
  9519. (setq cnt (1- cnt) have-zero t))
  9520. (save-excursion
  9521. (org-back-to-heading t)
  9522. (setq end (save-excursion (outline-next-heading) (point)))
  9523. (while (re-search-forward re end t)
  9524. (push (match-string 0) links))
  9525. (setq links (org-uniquify (reverse links))))
  9526. (cond
  9527. ((null links)
  9528. (message "No links"))
  9529. ((equal (length links) 1)
  9530. (setq link (car links)))
  9531. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9532. (setq link (nth (if have-zero nth (1- nth)) links)))
  9533. (t ; we have to select a link
  9534. (save-excursion
  9535. (save-window-excursion
  9536. (delete-other-windows)
  9537. (with-output-to-temp-buffer "*Select Link*"
  9538. (mapc (lambda (l)
  9539. (if (not (string-match org-bracket-link-regexp l))
  9540. (princ (format "[%c] %s\n" (incf cnt)
  9541. (org-remove-angle-brackets l)))
  9542. (if (match-end 3)
  9543. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9544. (match-string 3 l) (match-string 1 l)))
  9545. (princ (format "[%c] %s\n" (incf cnt)
  9546. (match-string 1 l))))))
  9547. links))
  9548. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9549. (message "Select link to open, RET to open all:")
  9550. (setq c (read-char-exclusive))
  9551. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9552. (when (equal c ?q) (error "Abort"))
  9553. (if (equal c ?\C-m)
  9554. (setq link links)
  9555. (setq nth (- c ?0))
  9556. (if have-zero (setq nth (1+ nth)))
  9557. (unless (and (integerp nth) (>= (length links) nth))
  9558. (error "Invalid link selection"))
  9559. (setq link (nth (1- nth) links)))))
  9560. (cons link end))))))
  9561. ;; Add special file links that specify the way of opening
  9562. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9563. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9564. (defun org-open-file-with-system (path)
  9565. "Open file at PATH using the system way of opening it."
  9566. (org-open-file path 'system))
  9567. (defun org-open-file-with-emacs (path)
  9568. "Open file at PATH in Emacs."
  9569. (org-open-file path 'emacs))
  9570. ;;; File search
  9571. (defvar org-create-file-search-functions nil
  9572. "List of functions to construct the right search string for a file link.
  9573. These functions are called in turn with point at the location to
  9574. which the link should point.
  9575. A function in the hook should first test if it would like to
  9576. handle this file type, for example by checking the `major-mode'
  9577. or the file extension. If it decides not to handle this file, it
  9578. should just return nil to give other functions a chance. If it
  9579. does handle the file, it must return the search string to be used
  9580. when following the link. The search string will be part of the
  9581. file link, given after a double colon, and `org-open-at-point'
  9582. will automatically search for it. If special measures must be
  9583. taken to make the search successful, another function should be
  9584. added to the companion hook `org-execute-file-search-functions',
  9585. which see.
  9586. A function in this hook may also use `setq' to set the variable
  9587. `description' to provide a suggestion for the descriptive text to
  9588. be used for this link when it gets inserted into an Org-mode
  9589. buffer with \\[org-insert-link].")
  9590. (defvar org-execute-file-search-functions nil
  9591. "List of functions to execute a file search triggered by a link.
  9592. Functions added to this hook must accept a single argument, the
  9593. search string that was part of the file link, the part after the
  9594. double colon. The function must first check if it would like to
  9595. handle this search, for example by checking the `major-mode' or
  9596. the file extension. If it decides not to handle this search, it
  9597. should just return nil to give other functions a chance. If it
  9598. does handle the search, it must return a non-nil value to keep
  9599. other functions from trying.
  9600. Each function can access the current prefix argument through the
  9601. variable `current-prefix-arg'. Note that a single prefix is used
  9602. to force opening a link in Emacs, so it may be good to only use a
  9603. numeric or double prefix to guide the search function.
  9604. In case this is needed, a function in this hook can also restore
  9605. the window configuration before `org-open-at-point' was called using:
  9606. (set-window-configuration org-window-config-before-follow-link)")
  9607. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9608. (defun org-link-search (s &optional type avoid-pos stealth)
  9609. "Search for a link search option.
  9610. If S is surrounded by forward slashes, it is interpreted as a
  9611. regular expression. In org-mode files, this will create an `org-occur'
  9612. sparse tree. In ordinary files, `occur' will be used to list matches.
  9613. If the current buffer is in `dired-mode', grep will be used to search
  9614. in all files. If AVOID-POS is given, ignore matches near that position.
  9615. When optional argument STEALTH is non-nil, do not modify
  9616. visibility around point, thus ignoring
  9617. `org-show-hierarchy-above', `org-show-following-heading' and
  9618. `org-show-siblings' variables."
  9619. (let ((case-fold-search t)
  9620. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9621. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9622. (append '(("") (" ") ("\t") ("\n"))
  9623. org-emphasis-alist)
  9624. "\\|") "\\)"))
  9625. (pos (point))
  9626. (pre nil) (post nil)
  9627. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9628. (cond
  9629. ;; First check if there are any special search functions
  9630. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9631. ;; Now try the builtin stuff
  9632. ((and (equal (string-to-char s0) ?#)
  9633. (> (length s0) 1)
  9634. (save-excursion
  9635. (goto-char (point-min))
  9636. (and
  9637. (re-search-forward
  9638. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9639. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9640. (setq type 'dedicated
  9641. pos (match-beginning 0))))
  9642. ;; There is an exact target for this
  9643. (goto-char pos)
  9644. (org-back-to-heading t)))
  9645. ((save-excursion
  9646. (goto-char (point-min))
  9647. (and
  9648. (re-search-forward
  9649. (concat "<<" (regexp-quote s0) ">>") nil t)
  9650. (setq type 'dedicated
  9651. pos (match-beginning 0))))
  9652. ;; There is an exact target for this
  9653. (goto-char pos))
  9654. ((save-excursion
  9655. (goto-char (point-min))
  9656. (and
  9657. (re-search-forward
  9658. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9659. (setq type 'dedicated pos (match-beginning 0))))
  9660. ;; Found an element with a matching #+name affiliated keyword.
  9661. (goto-char pos))
  9662. ((and (string-match "^(\\(.*\\))$" s0)
  9663. (save-excursion
  9664. (goto-char (point-min))
  9665. (and
  9666. (re-search-forward
  9667. (concat "[^[]" (regexp-quote
  9668. (format org-coderef-label-format
  9669. (match-string 1 s0))))
  9670. nil t)
  9671. (setq type 'dedicated
  9672. pos (1+ (match-beginning 0))))))
  9673. ;; There is a coderef target for this
  9674. (goto-char pos))
  9675. ((string-match "^/\\(.*\\)/$" s)
  9676. ;; A regular expression
  9677. (cond
  9678. ((derived-mode-p 'org-mode)
  9679. (org-occur (match-string 1 s)))
  9680. (t (org-do-occur (match-string 1 s)))))
  9681. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9682. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9683. (goto-char (point-min))
  9684. (cond
  9685. ((let (case-fold-search)
  9686. (re-search-forward (format org-complex-heading-regexp-format
  9687. (regexp-quote s))
  9688. nil t))
  9689. ;; OK, found a match
  9690. (setq type 'dedicated)
  9691. (goto-char (match-beginning 0)))
  9692. ((and (not org-link-search-inhibit-query)
  9693. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9694. (y-or-n-p "No match - create this as a new heading? "))
  9695. (goto-char (point-max))
  9696. (or (bolp) (newline))
  9697. (insert "* " s "\n")
  9698. (beginning-of-line 0))
  9699. (t
  9700. (goto-char pos)
  9701. (error "No match"))))
  9702. (t
  9703. ;; A normal search string
  9704. (when (equal (string-to-char s) ?*)
  9705. ;; Anchor on headlines, post may include tags.
  9706. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9707. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9708. s (substring s 1)))
  9709. (remove-text-properties
  9710. 0 (length s)
  9711. '(face nil mouse-face nil keymap nil fontified nil) s)
  9712. ;; Make a series of regular expressions to find a match
  9713. (setq words (org-split-string s "[ \n\r\t]+")
  9714. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9715. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9716. "\\)" markers)
  9717. re2a_ (concat "\\(" (mapconcat 'downcase words
  9718. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9719. re2a (concat "[ \t\r\n]" re2a_)
  9720. re4_ (concat "\\(" (mapconcat 'downcase words
  9721. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9722. re4 (concat "[^a-zA-Z_]" re4_)
  9723. re1 (concat pre re2 post)
  9724. re3 (concat pre (if pre re4_ re4) post)
  9725. re5 (concat pre ".*" re4)
  9726. re2 (concat pre re2)
  9727. re2a (concat pre (if pre re2a_ re2a))
  9728. re4 (concat pre (if pre re4_ re4))
  9729. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9730. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9731. re5 "\\)"
  9732. ))
  9733. (cond
  9734. ((eq type 'org-occur) (org-occur reall))
  9735. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9736. (t (goto-char (point-min))
  9737. (setq type 'fuzzy)
  9738. (if (or (and (org-search-not-self 1 re0 nil t)
  9739. (setq type 'dedicated))
  9740. (org-search-not-self 1 re1 nil t)
  9741. (org-search-not-self 1 re2 nil t)
  9742. (org-search-not-self 1 re2a nil t)
  9743. (org-search-not-self 1 re3 nil t)
  9744. (org-search-not-self 1 re4 nil t)
  9745. (org-search-not-self 1 re5 nil t))
  9746. (goto-char (match-beginning 1))
  9747. (goto-char pos)
  9748. (error "No match"))))))
  9749. (and (derived-mode-p 'org-mode)
  9750. (not stealth)
  9751. (org-show-context 'link-search))
  9752. type))
  9753. (defun org-search-not-self (group &rest args)
  9754. "Execute `re-search-forward', but only accept matches that do not
  9755. enclose the position of `org-open-link-marker'."
  9756. (let ((m org-open-link-marker))
  9757. (catch 'exit
  9758. (while (apply 're-search-forward args)
  9759. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9760. (goto-char (match-end group))
  9761. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9762. (> (match-beginning 0) (marker-position m))
  9763. (< (match-end 0) (marker-position m)))
  9764. (save-match-data
  9765. (or (not (org-in-regexp
  9766. org-bracket-link-analytic-regexp 1))
  9767. (not (match-end 4)) ; no description
  9768. (and (<= (match-beginning 4) (point))
  9769. (>= (match-end 4) (point))))))
  9770. (throw 'exit (point))))))))
  9771. (defun org-get-buffer-for-internal-link (buffer)
  9772. "Return a buffer to be used for displaying the link target of internal links."
  9773. (cond
  9774. ((not org-display-internal-link-with-indirect-buffer)
  9775. buffer)
  9776. ((string-match "(Clone)$" (buffer-name buffer))
  9777. (message "Buffer is already a clone, not making another one")
  9778. ;; we also do not modify visibility in this case
  9779. buffer)
  9780. (t ; make a new indirect buffer for displaying the link
  9781. (let* ((bn (buffer-name buffer))
  9782. (ibn (concat bn "(Clone)"))
  9783. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9784. (with-current-buffer ib (org-overview))
  9785. ib))))
  9786. (defun org-do-occur (regexp &optional cleanup)
  9787. "Call the Emacs command `occur'.
  9788. If CLEANUP is non-nil, remove the printout of the regular expression
  9789. in the *Occur* buffer. This is useful if the regex is long and not useful
  9790. to read."
  9791. (occur regexp)
  9792. (when cleanup
  9793. (let ((cwin (selected-window)) win beg end)
  9794. (when (setq win (get-buffer-window "*Occur*"))
  9795. (select-window win))
  9796. (goto-char (point-min))
  9797. (when (re-search-forward "match[a-z]+" nil t)
  9798. (setq beg (match-end 0))
  9799. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9800. (setq end (1- (match-beginning 0)))))
  9801. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9802. (goto-char (point-min))
  9803. (select-window cwin))))
  9804. ;;; The mark ring for links jumps
  9805. (defvar org-mark-ring nil
  9806. "Mark ring for positions before jumps in Org-mode.")
  9807. (defvar org-mark-ring-last-goto nil
  9808. "Last position in the mark ring used to go back.")
  9809. ;; Fill and close the ring
  9810. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9811. (loop for i from 1 to org-mark-ring-length do
  9812. (push (make-marker) org-mark-ring))
  9813. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9814. org-mark-ring)
  9815. (defun org-mark-ring-push (&optional pos buffer)
  9816. "Put the current position or POS into the mark ring and rotate it."
  9817. (interactive)
  9818. (setq pos (or pos (point)))
  9819. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9820. (move-marker (car org-mark-ring)
  9821. (or pos (point))
  9822. (or buffer (current-buffer)))
  9823. (message "%s"
  9824. (substitute-command-keys
  9825. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9826. (defun org-mark-ring-goto (&optional n)
  9827. "Jump to the previous position in the mark ring.
  9828. With prefix arg N, jump back that many stored positions. When
  9829. called several times in succession, walk through the entire ring.
  9830. Org-mode commands jumping to a different position in the current file,
  9831. or to another Org-mode file, automatically push the old position
  9832. onto the ring."
  9833. (interactive "p")
  9834. (let (p m)
  9835. (if (eq last-command this-command)
  9836. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9837. (setq p org-mark-ring))
  9838. (setq org-mark-ring-last-goto p)
  9839. (setq m (car p))
  9840. (org-pop-to-buffer-same-window (marker-buffer m))
  9841. (goto-char m)
  9842. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9843. (defun org-remove-angle-brackets (s)
  9844. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9845. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9846. s)
  9847. (defun org-add-angle-brackets (s)
  9848. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9849. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9850. s)
  9851. (defun org-remove-double-quotes (s)
  9852. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9853. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9854. s)
  9855. ;;; Following specific links
  9856. (defun org-follow-timestamp-link ()
  9857. "Open an agenda view for the time-stamp date/range at point."
  9858. (cond
  9859. ((org-at-date-range-p t)
  9860. (let ((org-agenda-start-on-weekday)
  9861. (t1 (match-string 1))
  9862. (t2 (match-string 2)) tt1 tt2)
  9863. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9864. tt2 (time-to-days (org-time-string-to-time t2)))
  9865. (let ((org-agenda-buffer-tmp-name
  9866. (format "*Org Agenda(a:%s)"
  9867. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9868. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9869. ((org-at-timestamp-p t)
  9870. (let ((org-agenda-buffer-tmp-name
  9871. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9872. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9873. (substring (match-string 1) 0 10)))
  9874. 1)))
  9875. (t (error "This should not happen"))))
  9876. ;;; Following file links
  9877. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9878. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9879. (declare-function mailcap-mime-info
  9880. "mailcap" (string &optional request no-decode))
  9881. (defvar org-wait nil)
  9882. (defun org-open-file (path &optional in-emacs line search)
  9883. "Open the file at PATH.
  9884. First, this expands any special file name abbreviations. Then the
  9885. configuration variable `org-file-apps' is checked if it contains an
  9886. entry for this file type, and if yes, the corresponding command is launched.
  9887. If no application is found, Emacs simply visits the file.
  9888. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9889. With a double \\[universal-argument] \\[universal-argument] \
  9890. prefix arg, Org tries to avoid opening in Emacs
  9891. and to use an external application to visit the file.
  9892. Optional LINE specifies a line to go to, optional SEARCH a string
  9893. to search for. If LINE or SEARCH is given, the file will be
  9894. opened in Emacs, unless an entry from org-file-apps that makes
  9895. use of groups in a regexp matches.
  9896. If you want to change the way frames are used when following a
  9897. link, please customize `org-link-frame-setup'.
  9898. If the file does not exist, an error is thrown."
  9899. (let* ((file (if (equal path "")
  9900. buffer-file-name
  9901. (substitute-in-file-name (expand-file-name path))))
  9902. (file-apps (append org-file-apps (org-default-apps)))
  9903. (apps (org-remove-if
  9904. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9905. (apps-dlink (org-remove-if-not
  9906. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9907. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9908. (dirp (if remp nil (file-directory-p file)))
  9909. (file (if (and dirp org-open-directory-means-index-dot-org)
  9910. (concat (file-name-as-directory file) "index.org")
  9911. file))
  9912. (a-m-a-p (assq 'auto-mode apps))
  9913. (dfile (downcase file))
  9914. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9915. (link (cond ((and (eq line nil)
  9916. (eq search nil))
  9917. file)
  9918. (line
  9919. (concat file "::" (number-to-string line)))
  9920. (search
  9921. (concat file "::" search))))
  9922. (dlink (downcase link))
  9923. (old-buffer (current-buffer))
  9924. (old-pos (point))
  9925. (old-mode major-mode)
  9926. ext cmd link-match-data)
  9927. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9928. (setq ext (match-string 1 dfile))
  9929. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9930. (setq ext (match-string 1 dfile))))
  9931. (cond
  9932. ((member in-emacs '((16) system))
  9933. (setq cmd (cdr (assoc 'system apps))))
  9934. (in-emacs (setq cmd 'emacs))
  9935. (t
  9936. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9937. (and dirp (cdr (assoc 'directory apps)))
  9938. ; first, try matching against apps-dlink
  9939. ; if we get a match here, store the match data for later
  9940. (let ((match (assoc-default dlink apps-dlink
  9941. 'string-match)))
  9942. (if match
  9943. (progn (setq link-match-data (match-data))
  9944. match)
  9945. (progn (setq in-emacs (or in-emacs line search))
  9946. nil))) ; if we have no match in apps-dlink,
  9947. ; always open the file in emacs if line or search
  9948. ; is given (for backwards compatibility)
  9949. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9950. 'string-match)
  9951. (cdr (assoc ext apps))
  9952. (cdr (assoc t apps))))))
  9953. (when (eq cmd 'system)
  9954. (setq cmd (cdr (assoc 'system apps))))
  9955. (when (eq cmd 'default)
  9956. (setq cmd (cdr (assoc t apps))))
  9957. (when (eq cmd 'mailcap)
  9958. (require 'mailcap)
  9959. (mailcap-parse-mailcaps)
  9960. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9961. (command (mailcap-mime-info mime-type)))
  9962. (if (stringp command)
  9963. (setq cmd command)
  9964. (setq cmd 'emacs))))
  9965. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9966. (not (file-exists-p file))
  9967. (not org-open-non-existing-files))
  9968. (error "No such file: %s" file))
  9969. (cond
  9970. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9971. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9972. (while (string-match "['\"]%s['\"]" cmd)
  9973. (setq cmd (replace-match "%s" t t cmd)))
  9974. (while (string-match "%s" cmd)
  9975. (setq cmd (replace-match
  9976. (save-match-data
  9977. (shell-quote-argument
  9978. (convert-standard-filename file)))
  9979. t t cmd)))
  9980. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9981. (save-match-data
  9982. (let ((match-index 1)
  9983. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9984. (set-match-data link-match-data)
  9985. (while (<= match-index number-of-groups)
  9986. (let ((regex (concat "%" (number-to-string match-index)))
  9987. (replace-with (match-string match-index dlink)))
  9988. (while (string-match regex cmd)
  9989. (setq cmd (replace-match replace-with t t cmd))))
  9990. (setq match-index (+ match-index 1)))))
  9991. (save-window-excursion
  9992. (message "Running %s...done" cmd)
  9993. (start-process-shell-command cmd nil cmd)
  9994. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9995. ((or (stringp cmd)
  9996. (eq cmd 'emacs))
  9997. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9998. (widen)
  9999. (if line (org-goto-line line)
  10000. (if search (org-link-search search))))
  10001. ((consp cmd)
  10002. (let ((file (convert-standard-filename file)))
  10003. (save-match-data
  10004. (set-match-data link-match-data)
  10005. (eval cmd))))
  10006. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10007. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10008. (or (not (equal old-buffer (current-buffer)))
  10009. (not (equal old-pos (point))))
  10010. (org-mark-ring-push old-pos old-buffer))))
  10011. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10012. "This function returns non-nil if `entry' uses a regular
  10013. expression which should be matched against the whole link by
  10014. org-open-file.
  10015. It assumes that is the case when the entry uses a regular
  10016. expression which has at least one grouping construct and the
  10017. action is either a lisp form or a command string containing
  10018. '%1', i.e. using at least one subexpression match as a
  10019. parameter."
  10020. (let ((selector (car entry))
  10021. (action (cdr entry)))
  10022. (if (stringp selector)
  10023. (and (> (regexp-opt-depth selector) 0)
  10024. (or (and (stringp action)
  10025. (string-match "%[0-9]" action))
  10026. (consp action)))
  10027. nil)))
  10028. (defun org-default-apps ()
  10029. "Return the default applications for this operating system."
  10030. (cond
  10031. ((eq system-type 'darwin)
  10032. org-file-apps-defaults-macosx)
  10033. ((eq system-type 'windows-nt)
  10034. org-file-apps-defaults-windowsnt)
  10035. (t org-file-apps-defaults-gnu)))
  10036. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10037. "Convert extensions to regular expressions in the cars of LIST.
  10038. Also, weed out any non-string entries, because the return value is used
  10039. only for regexp matching.
  10040. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10041. point to the symbol `emacs', indicating that the file should
  10042. be opened in Emacs."
  10043. (append
  10044. (delq nil
  10045. (mapcar (lambda (x)
  10046. (if (not (stringp (car x)))
  10047. nil
  10048. (if (string-match "\\W" (car x))
  10049. x
  10050. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10051. list))
  10052. (if add-auto-mode
  10053. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10054. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10055. (defun org-file-remote-p (file)
  10056. "Test whether FILE specifies a location on a remote system.
  10057. Return non-nil if the location is indeed remote.
  10058. For example, the filename \"/user@host:/foo\" specifies a location
  10059. on the system \"/user@host:\"."
  10060. (cond ((fboundp 'file-remote-p)
  10061. (file-remote-p file))
  10062. ((fboundp 'tramp-handle-file-remote-p)
  10063. (tramp-handle-file-remote-p file))
  10064. ((and (boundp 'ange-ftp-name-format)
  10065. (string-match (car ange-ftp-name-format) file))
  10066. t)))
  10067. ;;;; Refiling
  10068. (defun org-get-org-file ()
  10069. "Read a filename, with default directory `org-directory'."
  10070. (let ((default (or org-default-notes-file remember-data-file)))
  10071. (read-file-name (format "File name [%s]: " default)
  10072. (file-name-as-directory org-directory)
  10073. default)))
  10074. (defun org-notes-order-reversed-p ()
  10075. "Check if the current file should receive notes in reversed order."
  10076. (cond
  10077. ((not org-reverse-note-order) nil)
  10078. ((eq t org-reverse-note-order) t)
  10079. ((not (listp org-reverse-note-order)) nil)
  10080. (t (catch 'exit
  10081. (let ((all org-reverse-note-order)
  10082. entry)
  10083. (while (setq entry (pop all))
  10084. (if (string-match (car entry) buffer-file-name)
  10085. (throw 'exit (cdr entry))))
  10086. nil)))))
  10087. (defvar org-refile-target-table nil
  10088. "The list of refile targets, created by `org-refile'.")
  10089. (defvar org-agenda-new-buffers nil
  10090. "Buffers created to visit agenda files.")
  10091. (defvar org-refile-cache nil
  10092. "Cache for refile targets.")
  10093. (defvar org-refile-markers nil
  10094. "All the markers used for caching refile locations.")
  10095. (defun org-refile-marker (pos)
  10096. "Get a new refile marker, but only if caching is in use."
  10097. (if (not org-refile-use-cache)
  10098. pos
  10099. (let ((m (make-marker)))
  10100. (move-marker m pos)
  10101. (push m org-refile-markers)
  10102. m)))
  10103. (defun org-refile-cache-clear ()
  10104. "Clear the refile cache and disable all the markers."
  10105. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10106. (setq org-refile-markers nil)
  10107. (setq org-refile-cache nil)
  10108. (message "Refile cache has been cleared"))
  10109. (defun org-refile-cache-check-set (set)
  10110. "Check if all the markers in the cache still have live buffers."
  10111. (let (marker)
  10112. (catch 'exit
  10113. (while (and set (setq marker (nth 3 (pop set))))
  10114. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10115. (when (and marker (null (marker-buffer marker)))
  10116. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10117. (sit-for 3)
  10118. (throw 'exit nil)))
  10119. t)))
  10120. (defun org-refile-cache-put (set &rest identifiers)
  10121. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10122. (let* ((key (sha1 (prin1-to-string identifiers)))
  10123. (entry (assoc key org-refile-cache)))
  10124. (if entry
  10125. (setcdr entry set)
  10126. (push (cons key set) org-refile-cache))))
  10127. (defun org-refile-cache-get (&rest identifiers)
  10128. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10129. (cond
  10130. ((not org-refile-cache) nil)
  10131. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10132. (t
  10133. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10134. org-refile-cache))))
  10135. (and set (org-refile-cache-check-set set) set)))))
  10136. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10137. "Produce a table with refile targets."
  10138. (let ((case-fold-search nil)
  10139. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10140. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10141. targets tgs txt re files f desc descre fast-path-p level pos0)
  10142. (message "Getting targets...")
  10143. (with-current-buffer (or default-buffer (current-buffer))
  10144. (while (setq entry (pop entries))
  10145. (setq files (car entry) desc (cdr entry))
  10146. (setq fast-path-p nil)
  10147. (cond
  10148. ((null files) (setq files (list (current-buffer))))
  10149. ((eq files 'org-agenda-files)
  10150. (setq files (org-agenda-files 'unrestricted)))
  10151. ((and (symbolp files) (fboundp files))
  10152. (setq files (funcall files)))
  10153. ((and (symbolp files) (boundp files))
  10154. (setq files (symbol-value files))))
  10155. (if (stringp files) (setq files (list files)))
  10156. (cond
  10157. ((eq (car desc) :tag)
  10158. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10159. ((eq (car desc) :todo)
  10160. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10161. ((eq (car desc) :regexp)
  10162. (setq descre (cdr desc)))
  10163. ((eq (car desc) :level)
  10164. (setq descre (concat "^\\*\\{" (number-to-string
  10165. (if org-odd-levels-only
  10166. (1- (* 2 (cdr desc)))
  10167. (cdr desc)))
  10168. "\\}[ \t]")))
  10169. ((eq (car desc) :maxlevel)
  10170. (setq fast-path-p t)
  10171. (setq descre (concat "^\\*\\{1," (number-to-string
  10172. (if org-odd-levels-only
  10173. (1- (* 2 (cdr desc)))
  10174. (cdr desc)))
  10175. "\\}[ \t]")))
  10176. (t (error "Bad refiling target description %s" desc)))
  10177. (while (setq f (pop files))
  10178. (with-current-buffer
  10179. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10180. (or
  10181. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10182. (progn
  10183. (if (bufferp f) (setq f (buffer-file-name
  10184. (buffer-base-buffer f))))
  10185. (setq f (and f (expand-file-name f)))
  10186. (if (eq org-refile-use-outline-path 'file)
  10187. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10188. (save-excursion
  10189. (save-restriction
  10190. (widen)
  10191. (goto-char (point-min))
  10192. (while (re-search-forward descre nil t)
  10193. (goto-char (setq pos0 (point-at-bol)))
  10194. (catch 'next
  10195. (when org-refile-target-verify-function
  10196. (save-match-data
  10197. (or (funcall org-refile-target-verify-function)
  10198. (throw 'next t))))
  10199. (when (and (looking-at org-complex-heading-regexp)
  10200. (not (member (match-string 4) excluded-entries))
  10201. (match-string 4))
  10202. (setq level (org-reduced-level
  10203. (- (match-end 1) (match-beginning 1)))
  10204. txt (org-link-display-format (match-string 4))
  10205. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10206. re (format org-complex-heading-regexp-format
  10207. (regexp-quote (match-string 4))))
  10208. (when org-refile-use-outline-path
  10209. (setq txt (mapconcat
  10210. 'org-protect-slash
  10211. (append
  10212. (if (eq org-refile-use-outline-path
  10213. 'file)
  10214. (list (file-name-nondirectory
  10215. (buffer-file-name
  10216. (buffer-base-buffer))))
  10217. (if (eq org-refile-use-outline-path
  10218. 'full-file-path)
  10219. (list (buffer-file-name
  10220. (buffer-base-buffer)))))
  10221. (org-get-outline-path fast-path-p
  10222. level txt)
  10223. (list txt))
  10224. "/")))
  10225. (push (list txt f re (org-refile-marker (point)))
  10226. tgs)))
  10227. (when (= (point) pos0)
  10228. ;; verification function has not moved point
  10229. (goto-char (point-at-eol))))))))
  10230. (when org-refile-use-cache
  10231. (org-refile-cache-put tgs (buffer-file-name) descre))
  10232. (setq targets (append tgs targets))
  10233. ))))
  10234. (message "Getting targets...done")
  10235. (nreverse targets)))
  10236. (defun org-protect-slash (s)
  10237. (while (string-match "/" s)
  10238. (setq s (replace-match "\\" t t s)))
  10239. s)
  10240. (defvar org-olpa (make-vector 20 nil))
  10241. (defun org-get-outline-path (&optional fastp level heading)
  10242. "Return the outline path to the current entry, as a list.
  10243. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10244. routine which makes outline path derivations for an entire file,
  10245. avoiding backtracing. Refile target collection makes use of that."
  10246. (if fastp
  10247. (progn
  10248. (if (> level 19)
  10249. (error "Outline path failure, more than 19 levels"))
  10250. (loop for i from level upto 19 do
  10251. (aset org-olpa i nil))
  10252. (prog1
  10253. (delq nil (append org-olpa nil))
  10254. (aset org-olpa level heading)))
  10255. (let (rtn case-fold-search)
  10256. (save-excursion
  10257. (save-restriction
  10258. (widen)
  10259. (while (org-up-heading-safe)
  10260. (when (looking-at org-complex-heading-regexp)
  10261. (push (org-match-string-no-properties 4) rtn)))
  10262. rtn)))))
  10263. (defun org-format-outline-path (path &optional width prefix separator)
  10264. "Format the outline path PATH for display.
  10265. WIDTH is the maximum number of characters that is available.
  10266. PREFIX is a prefix to be included in the returned string,
  10267. such as the file name.
  10268. SEPARATOR is inserted between the different parts of the path,
  10269. the default is \"/\"."
  10270. (setq width (or width 79))
  10271. (if prefix (setq width (- width (length prefix))))
  10272. (if (not path)
  10273. (or prefix "")
  10274. (let* ((nsteps (length path))
  10275. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10276. (maxwidth (if (<= total-width width)
  10277. 10000 ;; everything fits
  10278. ;; we need to shorten the level headings
  10279. (/ (- width nsteps) nsteps)))
  10280. (org-odd-levels-only nil)
  10281. (n 0)
  10282. (total (1+ (length prefix))))
  10283. (setq maxwidth (max maxwidth 10))
  10284. (concat prefix
  10285. (if prefix (or separator "/"))
  10286. (mapconcat
  10287. (lambda (h)
  10288. (setq n (1+ n))
  10289. (if (and (= n nsteps) (< maxwidth 10000))
  10290. (setq maxwidth (- total-width total)))
  10291. (if (< (length h) maxwidth)
  10292. (progn (setq total (+ total (length h) 1)) h)
  10293. (setq h (substring h 0 (- maxwidth 2))
  10294. total (+ total maxwidth 1))
  10295. (if (string-match "[ \t]+\\'" h)
  10296. (setq h (substring h 0 (match-beginning 0))))
  10297. (setq h (concat h "..")))
  10298. (org-add-props h nil 'face
  10299. (nth (% (1- n) org-n-level-faces)
  10300. org-level-faces))
  10301. h)
  10302. path (or separator "/"))))))
  10303. (defun org-display-outline-path (&optional file current separator just-return-string)
  10304. "Display the current outline path in the echo area.
  10305. If FILE is non-nil, prepend the output with the file name.
  10306. If CURRENT is non-nil, append the current heading to the output.
  10307. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10308. the different parts of the path and defaults to \"/\".
  10309. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10310. (interactive "P")
  10311. (let* (case-fold-search
  10312. message-log-max ; Don't populate the *Messages* buffer
  10313. (bfn (buffer-file-name (buffer-base-buffer)))
  10314. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10315. res)
  10316. (if current (setq path (append path
  10317. (save-excursion
  10318. (org-back-to-heading t)
  10319. (if (looking-at org-complex-heading-regexp)
  10320. (list (match-string 4)))))))
  10321. (setq res
  10322. (org-format-outline-path
  10323. path
  10324. (1- (frame-width))
  10325. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10326. separator))
  10327. (if just-return-string
  10328. (org-no-properties res)
  10329. (message "%s" res))))
  10330. (defvar org-refile-history nil
  10331. "History for refiling operations.")
  10332. (defvar org-after-refile-insert-hook nil
  10333. "Hook run after `org-refile' has inserted its stuff at the new location.
  10334. Note that this is still *before* the stuff will be removed from
  10335. the *old* location.")
  10336. (defvar org-capture-last-stored-marker)
  10337. (defvar org-refile-keep nil
  10338. "Non-nil means `org-refile' will copy instead of refile.")
  10339. (defun org-copy ()
  10340. "Like `org-refile', but copy."
  10341. (interactive)
  10342. (let ((org-refile-keep t))
  10343. (funcall 'org-refile nil nil nil "Copy")))
  10344. (defun org-refile (&optional goto default-buffer rfloc msg)
  10345. "Move the entry or entries at point to another heading.
  10346. The list of target headings is compiled using the information in
  10347. `org-refile-targets', which see.
  10348. At the target location, the entry is filed as a subitem of the target
  10349. heading. Depending on `org-reverse-note-order', the new subitem will
  10350. either be the first or the last subitem.
  10351. If there is an active region, all entries in that region will be moved.
  10352. However, the region must fulfill the requirement that the first heading
  10353. is the first one sets the top-level of the moved text - at most siblings
  10354. below it are allowed.
  10355. With prefix arg GOTO, the command will only visit the target location
  10356. and not actually move anything.
  10357. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10358. go to the location where the last refiling operation has put the subtree.
  10359. With a prefix argument of `2', refile to the running clock.
  10360. RFLOC can be a refile location obtained in a different way.
  10361. MSG is a string to replace \"Refile\" in the default prompt with
  10362. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10363. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10364. If you are using target caching (see `org-refile-use-cache'),
  10365. you have to clear the target cache in order to find new targets.
  10366. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10367. prefix argument (`C-u C-u C-u C-c C-w')."
  10368. (interactive "P")
  10369. (if (member goto '(0 (64)))
  10370. (org-refile-cache-clear)
  10371. (let* ((actionmsg (or msg "Refile"))
  10372. (cbuf (current-buffer))
  10373. (regionp (org-region-active-p))
  10374. (region-start (and regionp (region-beginning)))
  10375. (region-end (and regionp (region-end)))
  10376. (region-length (and regionp (- region-end region-start)))
  10377. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10378. pos it nbuf file re level reversed)
  10379. (setq last-command nil)
  10380. (when regionp
  10381. (goto-char region-start)
  10382. (or (bolp) (goto-char (point-at-bol)))
  10383. (setq region-start (point))
  10384. (unless (or (org-kill-is-subtree-p
  10385. (buffer-substring region-start region-end))
  10386. (prog1 org-refile-active-region-within-subtree
  10387. (org-toggle-heading)))
  10388. (error "The region is not a (sequence of) subtree(s)")))
  10389. (if (equal goto '(16))
  10390. (org-refile-goto-last-stored)
  10391. (when (or
  10392. (and (equal goto 2)
  10393. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10394. (prog1
  10395. (setq it (list (or org-clock-heading "running clock")
  10396. (buffer-file-name
  10397. (marker-buffer org-clock-hd-marker))
  10398. ""
  10399. (marker-position org-clock-hd-marker)))
  10400. (setq goto nil)))
  10401. (setq it (or rfloc
  10402. (let (heading-text)
  10403. (save-excursion
  10404. (unless goto
  10405. (org-back-to-heading t)
  10406. (setq heading-text
  10407. (nth 4 (org-heading-components))))
  10408. (org-refile-get-location
  10409. (cond (goto "Goto")
  10410. (regionp (concat actionmsg " region to"))
  10411. (t (concat actionmsg " subtree \""
  10412. heading-text "\" to")))
  10413. default-buffer
  10414. (and (not (equal '(4) goto))
  10415. org-refile-allow-creating-parent-nodes)
  10416. goto))))))
  10417. (setq file (nth 1 it)
  10418. re (nth 2 it)
  10419. pos (nth 3 it))
  10420. (if (and (not goto)
  10421. pos
  10422. (equal (buffer-file-name) file)
  10423. (if regionp
  10424. (and (>= pos region-start)
  10425. (<= pos region-end))
  10426. (and (>= pos (point))
  10427. (< pos (save-excursion
  10428. (org-end-of-subtree t t))))))
  10429. (error "Cannot refile to position inside the tree or region"))
  10430. (setq nbuf (or (find-buffer-visiting file)
  10431. (find-file-noselect file)))
  10432. (if goto
  10433. (progn
  10434. (org-pop-to-buffer-same-window nbuf)
  10435. (goto-char pos)
  10436. (org-show-context 'org-goto))
  10437. (if regionp
  10438. (progn
  10439. (org-kill-new (buffer-substring region-start region-end))
  10440. (org-save-markers-in-region region-start region-end))
  10441. (org-copy-subtree 1 nil t))
  10442. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10443. (find-file-noselect file)))
  10444. (setq reversed (org-notes-order-reversed-p))
  10445. (save-excursion
  10446. (save-restriction
  10447. (widen)
  10448. (if pos
  10449. (progn
  10450. (goto-char pos)
  10451. (looking-at org-outline-regexp)
  10452. (setq level (org-get-valid-level (funcall outline-level) 1))
  10453. (goto-char
  10454. (if reversed
  10455. (or (outline-next-heading) (point-max))
  10456. (or (save-excursion (org-get-next-sibling))
  10457. (org-end-of-subtree t t)
  10458. (point-max)))))
  10459. (setq level 1)
  10460. (if (not reversed)
  10461. (goto-char (point-max))
  10462. (goto-char (point-min))
  10463. (or (outline-next-heading) (goto-char (point-max)))))
  10464. (if (not (bolp)) (newline))
  10465. (org-paste-subtree level)
  10466. (when org-log-refile
  10467. (org-add-log-setup 'refile nil nil 'findpos
  10468. org-log-refile)
  10469. (unless (eq org-log-refile 'note)
  10470. (save-excursion (org-add-log-note))))
  10471. (and org-auto-align-tags
  10472. (let ((org-loop-over-headlines-in-active-region nil))
  10473. (org-set-tags nil t)))
  10474. (with-demoted-errors
  10475. (bookmark-set "org-refile-last-stored"))
  10476. ;; If we are refiling for capture, make sure that the
  10477. ;; last-capture pointers point here
  10478. (when (org-bound-and-true-p org-refile-for-capture)
  10479. (with-demoted-errors
  10480. (bookmark-set "org-capture-last-stored-marker"))
  10481. (move-marker org-capture-last-stored-marker (point)))
  10482. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10483. (run-hooks 'org-after-refile-insert-hook))))
  10484. (unless org-refile-keep
  10485. (if regionp
  10486. (delete-region (point) (+ (point) region-length))
  10487. (org-cut-subtree)))
  10488. (when (featurep 'org-inlinetask)
  10489. (org-inlinetask-remove-END-maybe))
  10490. (setq org-markers-to-move nil)
  10491. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10492. (defun org-refile-goto-last-stored ()
  10493. "Go to the location where the last refile was stored."
  10494. (interactive)
  10495. (bookmark-jump "org-refile-last-stored")
  10496. (message "This is the location of the last refile"))
  10497. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10498. no-exclude)
  10499. "Prompt the user for a refile location, using PROMPT.
  10500. PROMPT should not be suffixed with a colon and a space, because
  10501. this function appends the default value from
  10502. `org-refile-history' automatically, if that is not empty.
  10503. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10504. this is used for the GOTO interface."
  10505. (let ((org-refile-targets org-refile-targets)
  10506. (org-refile-use-outline-path org-refile-use-outline-path)
  10507. excluded-entries)
  10508. (when (and (derived-mode-p 'org-mode)
  10509. (not org-refile-use-cache)
  10510. (not no-exclude))
  10511. (org-map-tree
  10512. (lambda()
  10513. (setq excluded-entries
  10514. (append excluded-entries (list (org-get-heading t t)))))))
  10515. (setq org-refile-target-table
  10516. (org-refile-get-targets default-buffer excluded-entries)))
  10517. (unless org-refile-target-table
  10518. (error "No refile targets"))
  10519. (let* ((cbuf (current-buffer))
  10520. (partial-completion-mode nil)
  10521. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10522. (cfunc (if (and org-refile-use-outline-path
  10523. org-outline-path-complete-in-steps)
  10524. 'org-olpath-completing-read
  10525. 'org-icompleting-read))
  10526. (extra (if org-refile-use-outline-path "/" ""))
  10527. (cbnex (concat (buffer-name) extra))
  10528. (filename (and cfn (expand-file-name cfn)))
  10529. (tbl (mapcar
  10530. (lambda (x)
  10531. (if (and (not (member org-refile-use-outline-path
  10532. '(file full-file-path)))
  10533. (not (equal filename (nth 1 x))))
  10534. (cons (concat (car x) extra " ("
  10535. (file-name-nondirectory (nth 1 x)) ")")
  10536. (cdr x))
  10537. (cons (concat (car x) extra) (cdr x))))
  10538. org-refile-target-table))
  10539. (completion-ignore-case t)
  10540. cdef
  10541. (prompt (concat prompt
  10542. (or (and (car org-refile-history)
  10543. (concat " (default " (car org-refile-history) ")"))
  10544. (and (assoc cbnex tbl) (setq cdef cbnex)
  10545. (concat " (default " cbnex ")"))) ": "))
  10546. pa answ parent-target child parent old-hist)
  10547. (setq old-hist org-refile-history)
  10548. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10549. nil 'org-refile-history (or cdef (car org-refile-history))))
  10550. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10551. (org-refile-check-position pa)
  10552. (if pa
  10553. (progn
  10554. (when (or (not org-refile-history)
  10555. (not (eq old-hist org-refile-history))
  10556. (not (equal (car pa) (car org-refile-history))))
  10557. (setq org-refile-history
  10558. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10559. org-refile-history
  10560. (cdr org-refile-history))))
  10561. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10562. (pop org-refile-history)))
  10563. pa)
  10564. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10565. (progn
  10566. (setq parent (match-string 1 answ)
  10567. child (match-string 2 answ))
  10568. (setq parent-target (or (assoc parent tbl)
  10569. (assoc (concat parent "/") tbl)))
  10570. (when (and parent-target
  10571. (or (eq new-nodes t)
  10572. (and (eq new-nodes 'confirm)
  10573. (y-or-n-p (format "Create new node \"%s\"? "
  10574. child)))))
  10575. (org-refile-new-child parent-target child)))
  10576. (error "Invalid target location")))))
  10577. (declare-function org-string-nw-p "org-macs" (s))
  10578. (defun org-refile-check-position (refile-pointer)
  10579. "Check if the refile pointer matches the headline to which it points."
  10580. (let* ((file (nth 1 refile-pointer))
  10581. (re (nth 2 refile-pointer))
  10582. (pos (nth 3 refile-pointer))
  10583. buffer)
  10584. (if (and (not (markerp pos)) (not file))
  10585. (error "Please save the buffer to a file before refiling")
  10586. (when (org-string-nw-p re)
  10587. (setq buffer (if (markerp pos)
  10588. (marker-buffer pos)
  10589. (or (find-buffer-visiting file)
  10590. (find-file-noselect file))))
  10591. (with-current-buffer buffer
  10592. (save-excursion
  10593. (save-restriction
  10594. (widen)
  10595. (goto-char pos)
  10596. (beginning-of-line 1)
  10597. (unless (org-looking-at-p re)
  10598. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10599. (defun org-refile-new-child (parent-target child)
  10600. "Use refile target PARENT-TARGET to add new CHILD below it."
  10601. (unless parent-target
  10602. (error "Cannot find parent for new node"))
  10603. (let ((file (nth 1 parent-target))
  10604. (pos (nth 3 parent-target))
  10605. level)
  10606. (with-current-buffer (or (find-buffer-visiting file)
  10607. (find-file-noselect file))
  10608. (save-excursion
  10609. (save-restriction
  10610. (widen)
  10611. (if pos
  10612. (goto-char pos)
  10613. (goto-char (point-max))
  10614. (if (not (bolp)) (newline)))
  10615. (when (looking-at org-outline-regexp)
  10616. (setq level (funcall outline-level))
  10617. (org-end-of-subtree t t))
  10618. (org-back-over-empty-lines)
  10619. (insert "\n" (make-string
  10620. (if pos (org-get-valid-level level 1) 1) ?*)
  10621. " " child "\n")
  10622. (beginning-of-line 0)
  10623. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10624. (defun org-olpath-completing-read (prompt collection &rest args)
  10625. "Read an outline path like a file name."
  10626. (let ((thetable collection)
  10627. (org-completion-use-ido nil) ; does not work with ido.
  10628. (org-completion-use-iswitchb nil)) ; or iswitchb
  10629. (apply
  10630. 'org-icompleting-read prompt
  10631. (lambda (string predicate &optional flag)
  10632. (let (rtn r f (l (length string)))
  10633. (cond
  10634. ((eq flag nil)
  10635. ;; try completion
  10636. (try-completion string thetable))
  10637. ((eq flag t)
  10638. ;; all-completions
  10639. (setq rtn (all-completions string thetable predicate))
  10640. (mapcar
  10641. (lambda (x)
  10642. (setq r (substring x l))
  10643. (if (string-match " ([^)]*)$" x)
  10644. (setq f (match-string 0 x))
  10645. (setq f ""))
  10646. (if (string-match "/" r)
  10647. (concat string (substring r 0 (match-end 0)) f)
  10648. x))
  10649. rtn))
  10650. ((eq flag 'lambda)
  10651. ;; exact match?
  10652. (assoc string thetable)))))
  10653. args)))
  10654. ;;;; Dynamic blocks
  10655. (defun org-find-dblock (name)
  10656. "Find the first dynamic block with name NAME in the buffer.
  10657. If not found, stay at current position and return nil."
  10658. (let ((case-fold-search t) pos)
  10659. (save-excursion
  10660. (goto-char (point-min))
  10661. (setq pos (and (re-search-forward
  10662. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10663. (match-beginning 0))))
  10664. (if pos (goto-char pos))
  10665. pos))
  10666. (defconst org-dblock-start-re
  10667. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10668. "Matches the start line of a dynamic block, with parameters.")
  10669. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10670. "Matches the end of a dynamic block.")
  10671. (defun org-create-dblock (plist)
  10672. "Create a dynamic block section, with parameters taken from PLIST.
  10673. PLIST must contain a :name entry which is used as name of the block."
  10674. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10675. (end-of-line 1)
  10676. (newline))
  10677. (let ((col (current-column))
  10678. (name (plist-get plist :name)))
  10679. (insert "#+BEGIN: " name)
  10680. (while plist
  10681. (if (eq (car plist) :name)
  10682. (setq plist (cddr plist))
  10683. (insert " " (prin1-to-string (pop plist)))))
  10684. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10685. (beginning-of-line -2)))
  10686. (defun org-prepare-dblock ()
  10687. "Prepare dynamic block for refresh.
  10688. This empties the block, puts the cursor at the insert position and returns
  10689. the property list including an extra property :name with the block name."
  10690. (unless (looking-at org-dblock-start-re)
  10691. (error "Not at a dynamic block"))
  10692. (let* ((begdel (1+ (match-end 0)))
  10693. (name (org-no-properties (match-string 1)))
  10694. (params (append (list :name name)
  10695. (read (concat "(" (match-string 3) ")")))))
  10696. (save-excursion
  10697. (beginning-of-line 1)
  10698. (skip-chars-forward " \t")
  10699. (setq params (plist-put params :indentation-column (current-column))))
  10700. (unless (re-search-forward org-dblock-end-re nil t)
  10701. (error "Dynamic block not terminated"))
  10702. (setq params
  10703. (append params
  10704. (list :content (buffer-substring
  10705. begdel (match-beginning 0)))))
  10706. (delete-region begdel (match-beginning 0))
  10707. (goto-char begdel)
  10708. (open-line 1)
  10709. params))
  10710. (defun org-map-dblocks (&optional command)
  10711. "Apply COMMAND to all dynamic blocks in the current buffer.
  10712. If COMMAND is not given, use `org-update-dblock'."
  10713. (let ((cmd (or command 'org-update-dblock)))
  10714. (save-excursion
  10715. (goto-char (point-min))
  10716. (while (re-search-forward org-dblock-start-re nil t)
  10717. (goto-char (match-beginning 0))
  10718. (save-excursion
  10719. (condition-case nil
  10720. (funcall cmd)
  10721. (error (message "Error during update of dynamic block"))))
  10722. (unless (re-search-forward org-dblock-end-re nil t)
  10723. (error "Dynamic block not terminated"))))))
  10724. (defun org-dblock-update (&optional arg)
  10725. "User command for updating dynamic blocks.
  10726. Update the dynamic block at point. With prefix ARG, update all dynamic
  10727. blocks in the buffer."
  10728. (interactive "P")
  10729. (if arg
  10730. (org-update-all-dblocks)
  10731. (or (looking-at org-dblock-start-re)
  10732. (org-beginning-of-dblock))
  10733. (org-update-dblock)))
  10734. (defun org-update-dblock ()
  10735. "Update the dynamic block at point.
  10736. This means to empty the block, parse for parameters and then call
  10737. the correct writing function."
  10738. (interactive)
  10739. (save-window-excursion
  10740. (let* ((pos (point))
  10741. (line (org-current-line))
  10742. (params (org-prepare-dblock))
  10743. (name (plist-get params :name))
  10744. (indent (plist-get params :indentation-column))
  10745. (cmd (intern (concat "org-dblock-write:" name))))
  10746. (message "Updating dynamic block `%s' at line %d..." name line)
  10747. (funcall cmd params)
  10748. (message "Updating dynamic block `%s' at line %d...done" name line)
  10749. (goto-char pos)
  10750. (when (and indent (> indent 0))
  10751. (setq indent (make-string indent ?\ ))
  10752. (save-excursion
  10753. (org-beginning-of-dblock)
  10754. (forward-line 1)
  10755. (while (not (looking-at org-dblock-end-re))
  10756. (insert indent)
  10757. (beginning-of-line 2))
  10758. (when (looking-at org-dblock-end-re)
  10759. (and (looking-at "[ \t]+")
  10760. (replace-match ""))
  10761. (insert indent)))))))
  10762. (defun org-beginning-of-dblock ()
  10763. "Find the beginning of the dynamic block at point.
  10764. Error if there is no such block at point."
  10765. (let ((pos (point))
  10766. beg)
  10767. (end-of-line 1)
  10768. (if (and (re-search-backward org-dblock-start-re nil t)
  10769. (setq beg (match-beginning 0))
  10770. (re-search-forward org-dblock-end-re nil t)
  10771. (> (match-end 0) pos))
  10772. (goto-char beg)
  10773. (goto-char pos)
  10774. (error "Not in a dynamic block"))))
  10775. (defun org-update-all-dblocks ()
  10776. "Update all dynamic blocks in the buffer.
  10777. This function can be used in a hook."
  10778. (interactive)
  10779. (when (derived-mode-p 'org-mode)
  10780. (org-map-dblocks 'org-update-dblock)))
  10781. ;;;; Completion
  10782. (defun org-get-export-keywords ()
  10783. "Return a list of all currently understood export keywords.
  10784. Export keywords include options, block names, attributes and
  10785. keywords relative to each registered export back-end."
  10786. (delq nil
  10787. (let (keywords)
  10788. (mapc
  10789. (lambda (back-end)
  10790. (let ((props (cdr back-end)))
  10791. ;; Back-end name (for keywords, like #+LATEX:)
  10792. (push (upcase (symbol-name (car back-end))) keywords)
  10793. ;; Back-end options.
  10794. (mapc (lambda (option) (push (cadr option) keywords))
  10795. (plist-get (cdr back-end) :options-alist))))
  10796. (org-bound-and-true-p org-export-registered-backends))
  10797. keywords)))
  10798. (defconst org-options-keywords
  10799. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10800. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10801. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10802. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10803. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10804. (defcustom org-structure-template-alist
  10805. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10806. "<src lang=\"?\">\n\n</src>")
  10807. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10808. "<example>\n?\n</example>")
  10809. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10810. "<quote>\n?\n</quote>")
  10811. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10812. "<verse>\n?\n</verse>")
  10813. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10814. "<verbatim>\n?\n</verbatim>")
  10815. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10816. "<center>\n?\n</center>")
  10817. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10818. "<literal style=\"latex\">\n?\n</literal>")
  10819. ("L" "#+LaTeX: "
  10820. "<literal style=\"latex\">?</literal>")
  10821. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10822. "<literal style=\"html\">\n?\n</literal>")
  10823. ("H" "#+HTML: "
  10824. "<literal style=\"html\">?</literal>")
  10825. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10826. ("A" "#+ASCII: ")
  10827. ("i" "#+INDEX: ?"
  10828. "#+INDEX: ?")
  10829. ("I" "#+INCLUDE: %file ?"
  10830. "<include file=%file markup=\"?\">"))
  10831. "Structure completion elements.
  10832. This is a list of abbreviation keys and values. The value gets inserted
  10833. if you type `<' followed by the key and then press the completion key,
  10834. usually `M-TAB'. %file will be replaced by a file name after prompting
  10835. for the file using completion. The cursor will be placed at the position
  10836. of the `?` in the template.
  10837. There are two templates for each key, the first uses the original Org syntax,
  10838. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10839. the default when the /org-mtags.el/ module has been loaded. See also the
  10840. variable `org-mtags-prefer-muse-templates'."
  10841. :group 'org-completion
  10842. :type '(repeat
  10843. (string :tag "Key")
  10844. (string :tag "Template")
  10845. (string :tag "Muse Template")))
  10846. (defun org-try-structure-completion ()
  10847. "Try to complete a structure template before point.
  10848. This looks for strings like \"<e\" on an otherwise empty line and
  10849. expands them."
  10850. (let ((l (buffer-substring (point-at-bol) (point)))
  10851. a)
  10852. (when (and (looking-at "[ \t]*$")
  10853. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10854. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10855. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10856. (match-beginning 1)) a)
  10857. t)))
  10858. (defun org-complete-expand-structure-template (start cell)
  10859. "Expand a structure template."
  10860. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10861. (rpl (nth (if musep 2 1) cell))
  10862. (ind ""))
  10863. (delete-region start (point))
  10864. (when (string-match "\\`#\\+" rpl)
  10865. (cond
  10866. ((bolp))
  10867. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10868. (setq ind (buffer-substring (point-at-bol) (point))))
  10869. (t (newline))))
  10870. (setq start (point))
  10871. (if (string-match "%file" rpl)
  10872. (setq rpl (replace-match
  10873. (concat
  10874. "\""
  10875. (save-match-data
  10876. (abbreviate-file-name (read-file-name "Include file: ")))
  10877. "\"")
  10878. t t rpl)))
  10879. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10880. (concat "\n" ind)))
  10881. (insert rpl)
  10882. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10883. ;;;; TODO, DEADLINE, Comments
  10884. (defun org-toggle-comment ()
  10885. "Change the COMMENT state of an entry."
  10886. (interactive)
  10887. (save-excursion
  10888. (org-back-to-heading)
  10889. (let (case-fold-search)
  10890. (cond
  10891. ((looking-at (format org-heading-keyword-regexp-format
  10892. org-comment-string))
  10893. (goto-char (match-end 1))
  10894. (looking-at (concat " +" org-comment-string))
  10895. (replace-match "" t t)
  10896. (when (eolp) (insert " ")))
  10897. ((looking-at org-outline-regexp)
  10898. (goto-char (match-end 0))
  10899. (insert org-comment-string " "))))))
  10900. (defvar org-last-todo-state-is-todo nil
  10901. "This is non-nil when the last TODO state change led to a TODO state.
  10902. If the last change removed the TODO tag or switched to DONE, then
  10903. this is nil.")
  10904. (defvar org-setting-tags nil) ; dynamically skipped
  10905. (defvar org-todo-setup-filter-hook nil
  10906. "Hook for functions that pre-filter todo specs.
  10907. Each function takes a todo spec and returns either nil or the spec
  10908. transformed into canonical form." )
  10909. (defvar org-todo-get-default-hook nil
  10910. "Hook for functions that get a default item for todo.
  10911. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10912. nil or a string to be used for the todo mark." )
  10913. (defvar org-agenda-headline-snapshot-before-repeat)
  10914. (defun org-current-effective-time ()
  10915. "Return current time adjusted for `org-extend-today-until' variable."
  10916. (let* ((ct (org-current-time))
  10917. (dct (decode-time ct))
  10918. (ct1
  10919. (cond
  10920. (org-use-last-clock-out-time-as-effective-time
  10921. (or (org-clock-get-last-clock-out-time) ct))
  10922. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10923. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10924. (t ct))))
  10925. ct1))
  10926. (defun org-todo-yesterday (&optional arg)
  10927. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10928. (interactive "P")
  10929. (if (eq major-mode 'org-agenda-mode)
  10930. (apply 'org-agenda-todo-yesterday arg)
  10931. (let* ((hour (third (decode-time
  10932. (org-current-time))))
  10933. (org-extend-today-until (1+ hour)))
  10934. (org-todo arg))))
  10935. (defvar org-block-entry-blocking ""
  10936. "First entry preventing the TODO state change.")
  10937. (defun org-todo (&optional arg)
  10938. "Change the TODO state of an item.
  10939. The state of an item is given by a keyword at the start of the heading,
  10940. like
  10941. *** TODO Write paper
  10942. *** DONE Call mom
  10943. The different keywords are specified in the variable `org-todo-keywords'.
  10944. By default the available states are \"TODO\" and \"DONE\".
  10945. So for this example: when the item starts with TODO, it is changed to DONE.
  10946. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10947. DONE are present, add TODO at the beginning of the heading.
  10948. With \\[universal-argument] prefix arg, use completion to determine the new \
  10949. state.
  10950. With numeric prefix arg, switch to that state.
  10951. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10952. keywords (nextset).
  10953. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10954. With a numeric prefix arg of 0, inhibit note taking for the change.
  10955. For calling through lisp, arg is also interpreted in the following way:
  10956. 'none -> empty state
  10957. \"\"(empty string) -> switch to empty state
  10958. 'done -> switch to DONE
  10959. 'nextset -> switch to the next set of keywords
  10960. 'previousset -> switch to the previous set of keywords
  10961. \"WAITING\" -> switch to the specified keyword, but only if it
  10962. really is a member of `org-todo-keywords'."
  10963. (interactive "P")
  10964. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10965. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10966. 'region-start-level 'region))
  10967. org-loop-over-headlines-in-active-region)
  10968. (org-map-entries
  10969. `(org-todo ,arg)
  10970. org-loop-over-headlines-in-active-region
  10971. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10972. (if (equal arg '(16)) (setq arg 'nextset))
  10973. (let ((org-blocker-hook org-blocker-hook)
  10974. commentp
  10975. case-fold-search)
  10976. (when (equal arg '(64))
  10977. (setq arg nil org-blocker-hook nil))
  10978. (when (and org-blocker-hook
  10979. (or org-inhibit-blocking
  10980. (org-entry-get nil "NOBLOCKING")))
  10981. (setq org-blocker-hook nil))
  10982. (save-excursion
  10983. (catch 'exit
  10984. (org-back-to-heading t)
  10985. (when (looking-at (concat "^\\*+ " org-comment-string))
  10986. (org-toggle-comment)
  10987. (setq commentp t))
  10988. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10989. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10990. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10991. (let* ((match-data (match-data))
  10992. (startpos (point-at-bol))
  10993. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10994. (org-log-done org-log-done)
  10995. (org-log-repeat org-log-repeat)
  10996. (org-todo-log-states org-todo-log-states)
  10997. (org-inhibit-logging
  10998. (if (equal arg 0)
  10999. (progn (setq arg nil) 'note) org-inhibit-logging))
  11000. (this (match-string 1))
  11001. (hl-pos (match-beginning 0))
  11002. (head (org-get-todo-sequence-head this))
  11003. (ass (assoc head org-todo-kwd-alist))
  11004. (interpret (nth 1 ass))
  11005. (done-word (nth 3 ass))
  11006. (final-done-word (nth 4 ass))
  11007. (org-last-state (or this ""))
  11008. (completion-ignore-case t)
  11009. (member (member this org-todo-keywords-1))
  11010. (tail (cdr member))
  11011. (org-state (cond
  11012. ((and org-todo-key-trigger
  11013. (or (and (equal arg '(4))
  11014. (eq org-use-fast-todo-selection 'prefix))
  11015. (and (not arg) org-use-fast-todo-selection
  11016. (not (eq org-use-fast-todo-selection
  11017. 'prefix)))))
  11018. ;; Use fast selection
  11019. (org-fast-todo-selection))
  11020. ((and (equal arg '(4))
  11021. (or (not org-use-fast-todo-selection)
  11022. (not org-todo-key-trigger)))
  11023. ;; Read a state with completion
  11024. (org-icompleting-read
  11025. "State: " (mapcar 'list org-todo-keywords-1)
  11026. nil t))
  11027. ((eq arg 'right)
  11028. (if this
  11029. (if tail (car tail) nil)
  11030. (car org-todo-keywords-1)))
  11031. ((eq arg 'left)
  11032. (if (equal member org-todo-keywords-1)
  11033. nil
  11034. (if this
  11035. (nth (- (length org-todo-keywords-1)
  11036. (length tail) 2)
  11037. org-todo-keywords-1)
  11038. (org-last org-todo-keywords-1))))
  11039. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11040. (setq arg nil))) ; hack to fall back to cycling
  11041. (arg
  11042. ;; user or caller requests a specific state
  11043. (cond
  11044. ((equal arg "") nil)
  11045. ((eq arg 'none) nil)
  11046. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11047. ((eq arg 'nextset)
  11048. (or (car (cdr (member head org-todo-heads)))
  11049. (car org-todo-heads)))
  11050. ((eq arg 'previousset)
  11051. (let ((org-todo-heads (reverse org-todo-heads)))
  11052. (or (car (cdr (member head org-todo-heads)))
  11053. (car org-todo-heads))))
  11054. ((car (member arg org-todo-keywords-1)))
  11055. ((stringp arg)
  11056. (error "State `%s' not valid in this file" arg))
  11057. ((nth (1- (prefix-numeric-value arg))
  11058. org-todo-keywords-1))))
  11059. ((null member) (or head (car org-todo-keywords-1)))
  11060. ((equal this final-done-word) nil) ;; -> make empty
  11061. ((null tail) nil) ;; -> first entry
  11062. ((memq interpret '(type priority))
  11063. (if (eq this-command last-command)
  11064. (car tail)
  11065. (if (> (length tail) 0)
  11066. (or done-word (car org-done-keywords))
  11067. nil)))
  11068. (t
  11069. (car tail))))
  11070. (org-state (or
  11071. (run-hook-with-args-until-success
  11072. 'org-todo-get-default-hook org-state org-last-state)
  11073. org-state))
  11074. (next (if org-state (concat " " org-state " ") " "))
  11075. (change-plist (list :type 'todo-state-change :from this :to org-state
  11076. :position startpos))
  11077. dolog now-done-p)
  11078. (when org-blocker-hook
  11079. (setq org-last-todo-state-is-todo
  11080. (not (member this org-done-keywords)))
  11081. (unless (save-excursion
  11082. (save-match-data
  11083. (org-with-wide-buffer
  11084. (run-hook-with-args-until-failure
  11085. 'org-blocker-hook change-plist))))
  11086. (if (org-called-interactively-p 'interactive)
  11087. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11088. this org-state org-block-entry-blocking)
  11089. ;; fail silently
  11090. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11091. this org-state org-block-entry-blocking)
  11092. (throw 'exit nil))))
  11093. (store-match-data match-data)
  11094. (replace-match next t t)
  11095. (unless (pos-visible-in-window-p hl-pos)
  11096. (message "TODO state changed to %s" (org-trim next)))
  11097. (unless head
  11098. (setq head (org-get-todo-sequence-head org-state)
  11099. ass (assoc head org-todo-kwd-alist)
  11100. interpret (nth 1 ass)
  11101. done-word (nth 3 ass)
  11102. final-done-word (nth 4 ass)))
  11103. (when (memq arg '(nextset previousset))
  11104. (message "Keyword-Set %d/%d: %s"
  11105. (- (length org-todo-sets) -1
  11106. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11107. (length org-todo-sets)
  11108. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11109. (setq org-last-todo-state-is-todo
  11110. (not (member org-state org-done-keywords)))
  11111. (setq now-done-p (and (member org-state org-done-keywords)
  11112. (not (member this org-done-keywords))))
  11113. (and logging (org-local-logging logging))
  11114. (when (and (or org-todo-log-states org-log-done)
  11115. (not (eq org-inhibit-logging t))
  11116. (not (memq arg '(nextset previousset))))
  11117. ;; we need to look at recording a time and note
  11118. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11119. (nth 2 (assoc this org-todo-log-states))))
  11120. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11121. (setq dolog 'time))
  11122. (when (and org-state
  11123. (member org-state org-not-done-keywords)
  11124. (not (member this org-not-done-keywords)))
  11125. ;; This is now a todo state and was not one before
  11126. ;; If there was a CLOSED time stamp, get rid of it.
  11127. (org-add-planning-info nil nil 'closed))
  11128. (when (and now-done-p org-log-done)
  11129. ;; It is now done, and it was not done before
  11130. (org-add-planning-info 'closed (org-current-effective-time))
  11131. (if (and (not dolog) (eq 'note org-log-done))
  11132. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11133. (when (and org-state dolog)
  11134. ;; This is a non-nil state, and we need to log it
  11135. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11136. ;; Fixup tag positioning
  11137. (org-todo-trigger-tag-changes org-state)
  11138. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11139. (when org-provide-todo-statistics
  11140. (org-update-parent-todo-statistics))
  11141. (run-hooks 'org-after-todo-state-change-hook)
  11142. (if (and arg (not (member org-state org-done-keywords)))
  11143. (setq head (org-get-todo-sequence-head org-state)))
  11144. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11145. ;; Do we need to trigger a repeat?
  11146. (when now-done-p
  11147. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11148. ;; This is for the agenda, take a snapshot of the headline.
  11149. (save-match-data
  11150. (setq org-agenda-headline-snapshot-before-repeat
  11151. (org-get-heading))))
  11152. (org-auto-repeat-maybe org-state))
  11153. ;; Fixup cursor location if close to the keyword
  11154. (if (and (outline-on-heading-p)
  11155. (not (bolp))
  11156. (save-excursion (beginning-of-line 1)
  11157. (looking-at org-todo-line-regexp))
  11158. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11159. (progn
  11160. (goto-char (or (match-end 2) (match-end 1)))
  11161. (and (looking-at " ") (just-one-space))))
  11162. (when org-trigger-hook
  11163. (save-excursion
  11164. (run-hook-with-args 'org-trigger-hook change-plist)))
  11165. (when commentp (org-toggle-comment))))))))
  11166. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11167. "Block turning an entry into a TODO, using the hierarchy.
  11168. This checks whether the current task should be blocked from state
  11169. changes. Such blocking occurs when:
  11170. 1. The task has children which are not all in a completed state.
  11171. 2. A task has a parent with the property :ORDERED:, and there
  11172. are siblings prior to the current task with incomplete
  11173. status.
  11174. 3. The parent of the task is blocked because it has siblings that should
  11175. be done first, or is child of a block grandparent TODO entry."
  11176. (if (not org-enforce-todo-dependencies)
  11177. t ; if locally turned off don't block
  11178. (catch 'dont-block
  11179. ;; If this is not a todo state change, or if this entry is already DONE,
  11180. ;; do not block
  11181. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11182. (member (plist-get change-plist :from)
  11183. (cons 'done org-done-keywords))
  11184. (member (plist-get change-plist :to)
  11185. (cons 'todo org-not-done-keywords))
  11186. (not (plist-get change-plist :to)))
  11187. (throw 'dont-block t))
  11188. ;; If this task has children, and any are undone, it's blocked
  11189. (save-excursion
  11190. (org-back-to-heading t)
  11191. (let ((this-level (funcall outline-level)))
  11192. (outline-next-heading)
  11193. (let ((child-level (funcall outline-level)))
  11194. (while (and (not (eobp))
  11195. (> child-level this-level))
  11196. ;; this todo has children, check whether they are all
  11197. ;; completed
  11198. (if (and (not (org-entry-is-done-p))
  11199. (org-entry-is-todo-p))
  11200. (progn (setq org-block-entry-blocking (org-get-heading))
  11201. (throw 'dont-block nil)))
  11202. (outline-next-heading)
  11203. (setq child-level (funcall outline-level))))))
  11204. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11205. ;; any previous siblings are undone, it's blocked
  11206. (save-excursion
  11207. (org-back-to-heading t)
  11208. (let* ((pos (point))
  11209. (parent-pos (and (org-up-heading-safe) (point))))
  11210. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11211. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11212. (forward-line 1)
  11213. (re-search-forward org-not-done-heading-regexp pos t))
  11214. (setq org-block-entry-blocking (match-string 0))
  11215. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11216. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11217. (while t
  11218. (goto-char parent-pos)
  11219. (if (not (looking-at org-not-done-heading-regexp))
  11220. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11221. (setq pos (point))
  11222. (setq parent-pos (and (org-up-heading-safe) (point)))
  11223. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11224. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11225. (forward-line 1)
  11226. (re-search-forward org-not-done-heading-regexp pos t)
  11227. (setq org-block-entry-blocking (org-get-heading)))
  11228. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11229. (defcustom org-track-ordered-property-with-tag nil
  11230. "Should the ORDERED property also be shown as a tag?
  11231. The ORDERED property decides if an entry should require subtasks to be
  11232. completed in sequence. Since a property is not very visible, setting
  11233. this option means that toggling the ORDERED property with the command
  11234. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11235. not relevant for the behavior, but it makes things more visible.
  11236. Note that toggling the tag with tags commands will not change the property
  11237. and therefore not influence behavior!
  11238. This can be t, meaning the tag ORDERED should be used, It can also be a
  11239. string to select a different tag for this task."
  11240. :group 'org-todo
  11241. :type '(choice
  11242. (const :tag "No tracking" nil)
  11243. (const :tag "Track with ORDERED tag" t)
  11244. (string :tag "Use other tag")))
  11245. (defun org-toggle-ordered-property ()
  11246. "Toggle the ORDERED property of the current entry.
  11247. For better visibility, you can track the value of this property with a tag.
  11248. See variable `org-track-ordered-property-with-tag'."
  11249. (interactive)
  11250. (let* ((t1 org-track-ordered-property-with-tag)
  11251. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11252. (save-excursion
  11253. (org-back-to-heading)
  11254. (if (org-entry-get nil "ORDERED")
  11255. (progn
  11256. (org-delete-property "ORDERED" "PROPERTIES")
  11257. (and tag (org-toggle-tag tag 'off))
  11258. (message "Subtasks can be completed in arbitrary order"))
  11259. (org-entry-put nil "ORDERED" "t")
  11260. (and tag (org-toggle-tag tag 'on))
  11261. (message "Subtasks must be completed in sequence")))))
  11262. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11263. (defun org-block-todo-from-checkboxes (change-plist)
  11264. "Block turning an entry into a TODO, using checkboxes.
  11265. This checks whether the current task should be blocked from state
  11266. changes because there are unchecked boxes in this entry."
  11267. (if (not org-enforce-todo-checkbox-dependencies)
  11268. t ; if locally turned off don't block
  11269. (catch 'dont-block
  11270. ;; If this is not a todo state change, or if this entry is already DONE,
  11271. ;; do not block
  11272. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11273. (member (plist-get change-plist :from)
  11274. (cons 'done org-done-keywords))
  11275. (member (plist-get change-plist :to)
  11276. (cons 'todo org-not-done-keywords))
  11277. (not (plist-get change-plist :to)))
  11278. (throw 'dont-block t))
  11279. ;; If this task has checkboxes that are not checked, it's blocked
  11280. (save-excursion
  11281. (org-back-to-heading t)
  11282. (let ((beg (point)) end)
  11283. (outline-next-heading)
  11284. (setq end (point))
  11285. (goto-char beg)
  11286. (if (org-list-search-forward
  11287. (concat (org-item-beginning-re)
  11288. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11289. "\\[[- ]\\]")
  11290. end t)
  11291. (progn
  11292. (if (boundp 'org-blocked-by-checkboxes)
  11293. (setq org-blocked-by-checkboxes t))
  11294. (throw 'dont-block nil)))))
  11295. t))) ; do not block
  11296. (defun org-entry-blocked-p ()
  11297. "Is the current entry blocked?"
  11298. (org-with-silent-modifications
  11299. (if (org-entry-get nil "NOBLOCKING")
  11300. nil ;; Never block this entry
  11301. (not (run-hook-with-args-until-failure
  11302. 'org-blocker-hook
  11303. (list :type 'todo-state-change
  11304. :position (point)
  11305. :from 'todo
  11306. :to 'done))))))
  11307. (defun org-update-statistics-cookies (all)
  11308. "Update the statistics cookie, either from TODO or from checkboxes.
  11309. This should be called with the cursor in a line with a statistics cookie."
  11310. (interactive "P")
  11311. (if all
  11312. (progn
  11313. (org-update-checkbox-count 'all)
  11314. (org-map-entries 'org-update-parent-todo-statistics))
  11315. (if (not (org-at-heading-p))
  11316. (org-update-checkbox-count)
  11317. (let ((pos (point-marker))
  11318. end l1 l2)
  11319. (ignore-errors (org-back-to-heading t))
  11320. (if (not (org-at-heading-p))
  11321. (org-update-checkbox-count)
  11322. (setq l1 (org-outline-level))
  11323. (setq end (save-excursion
  11324. (outline-next-heading)
  11325. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11326. (point)))
  11327. (if (and (save-excursion
  11328. (re-search-forward
  11329. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11330. (not (save-excursion (re-search-forward
  11331. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11332. (org-update-checkbox-count)
  11333. (if (and l2 (> l2 l1))
  11334. (progn
  11335. (goto-char end)
  11336. (org-update-parent-todo-statistics))
  11337. (goto-char pos)
  11338. (beginning-of-line 1)
  11339. (while (re-search-forward
  11340. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11341. (point-at-eol) t)
  11342. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11343. (goto-char pos)
  11344. (move-marker pos nil)))))
  11345. (defvar org-entry-property-inherited-from) ;; defined below
  11346. (defun org-update-parent-todo-statistics ()
  11347. "Update any statistics cookie in the parent of the current headline.
  11348. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11349. the entire subtree and this will travel up the hierarchy and update
  11350. statistics everywhere."
  11351. (let* ((prop (save-excursion (org-up-heading-safe)
  11352. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11353. (recursive (or (not org-hierarchical-todo-statistics)
  11354. (and prop (string-match "\\<recursive\\>" prop))))
  11355. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11356. 0))
  11357. (first t)
  11358. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11359. level ltoggle l1 new ndel
  11360. (cnt-all 0) (cnt-done 0) is-percent kwd
  11361. checkbox-beg ov ovs ove cookie-present)
  11362. (catch 'exit
  11363. (save-excursion
  11364. (beginning-of-line 1)
  11365. (setq ltoggle (funcall outline-level))
  11366. ;; Three situations are to consider:
  11367. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11368. ;; to the top-level ancestor on the headline;
  11369. ;; 2. If parent has "recursive" property, repeat up to the
  11370. ;; headline setting that property, taking inheritance into
  11371. ;; account;
  11372. ;; 3. Else, move up to direct parent and proceed only once.
  11373. (while (and (setq level (org-up-heading-safe))
  11374. (or recursive first)
  11375. (>= (point) lim))
  11376. (setq first nil cookie-present nil)
  11377. (unless (and level
  11378. (not (string-match
  11379. "\\<checkbox\\>"
  11380. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11381. "")))))
  11382. (throw 'exit nil))
  11383. (while (re-search-forward box-re (point-at-eol) t)
  11384. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11385. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11386. (save-match-data
  11387. (unless (outline-next-heading) (throw 'exit nil))
  11388. (while (and (looking-at org-complex-heading-regexp)
  11389. (> (setq l1 (length (match-string 1))) level))
  11390. (setq kwd (and (or recursive (= l1 ltoggle))
  11391. (match-string 2)))
  11392. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11393. (and (listp org-provide-todo-statistics)
  11394. (or (member kwd org-provide-todo-statistics)
  11395. (member kwd org-done-keywords))))
  11396. (setq cnt-all (1+ cnt-all))
  11397. (if (eq org-provide-todo-statistics t)
  11398. (and kwd (setq cnt-all (1+ cnt-all)))))
  11399. (and (member kwd org-done-keywords)
  11400. (setq cnt-done (1+ cnt-done)))
  11401. (outline-next-heading)))
  11402. (setq new
  11403. (if is-percent
  11404. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11405. (format "[%d/%d]" cnt-done cnt-all))
  11406. ndel (- (match-end 0) checkbox-beg))
  11407. ;; handle overlays when updating cookie from column view
  11408. (when (setq ov (car (overlays-at checkbox-beg)))
  11409. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11410. (delete-overlay ov))
  11411. (goto-char checkbox-beg)
  11412. (insert new)
  11413. (delete-region (point) (+ (point) ndel))
  11414. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11415. (when ov (move-overlay ov ovs ove)))
  11416. (when cookie-present
  11417. (run-hook-with-args 'org-after-todo-statistics-hook
  11418. cnt-done (- cnt-all cnt-done))))))
  11419. (run-hooks 'org-todo-statistics-hook)))
  11420. (defvar org-after-todo-statistics-hook nil
  11421. "Hook that is called after a TODO statistics cookie has been updated.
  11422. Each function is called with two arguments: the number of not-done entries
  11423. and the number of done entries.
  11424. For example, the following function, when added to this hook, will switch
  11425. an entry to DONE when all children are done, and back to TODO when new
  11426. entries are set to a TODO status. Note that this hook is only called
  11427. when there is a statistics cookie in the headline!
  11428. (defun org-summary-todo (n-done n-not-done)
  11429. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11430. (let (org-log-done org-log-states) ; turn off logging
  11431. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11432. ")
  11433. (defvar org-todo-statistics-hook nil
  11434. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11435. This hook runs even if there is no statistics cookie present, in which case
  11436. `org-after-todo-statistics-hook' would not run.")
  11437. (defun org-todo-trigger-tag-changes (state)
  11438. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11439. (let ((l org-todo-state-tags-triggers)
  11440. changes)
  11441. (when (or (not state) (equal state ""))
  11442. (setq changes (append changes (cdr (assoc "" l)))))
  11443. (when (and (stringp state) (> (length state) 0))
  11444. (setq changes (append changes (cdr (assoc state l)))))
  11445. (when (member state org-not-done-keywords)
  11446. (setq changes (append changes (cdr (assoc 'todo l)))))
  11447. (when (member state org-done-keywords)
  11448. (setq changes (append changes (cdr (assoc 'done l)))))
  11449. (dolist (c changes)
  11450. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11451. (defun org-local-logging (value)
  11452. "Get logging settings from a property VALUE."
  11453. (let* (words w a)
  11454. ;; directly set the variables, they are already local.
  11455. (setq org-log-done nil
  11456. org-log-repeat nil
  11457. org-todo-log-states nil)
  11458. (setq words (org-split-string value))
  11459. (while (setq w (pop words))
  11460. (cond
  11461. ((setq a (assoc w org-startup-options))
  11462. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11463. (set (nth 1 a) (nth 2 a))))
  11464. ((setq a (org-extract-log-state-settings w))
  11465. (and (member (car a) org-todo-keywords-1)
  11466. (push a org-todo-log-states)))))))
  11467. (defun org-get-todo-sequence-head (kwd)
  11468. "Return the head of the TODO sequence to which KWD belongs.
  11469. If KWD is not set, check if there is a text property remembering the
  11470. right sequence."
  11471. (let (p)
  11472. (cond
  11473. ((not kwd)
  11474. (or (get-text-property (point-at-bol) 'org-todo-head)
  11475. (progn
  11476. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11477. nil (point-at-eol)))
  11478. (get-text-property p 'org-todo-head))))
  11479. ((not (member kwd org-todo-keywords-1))
  11480. (car org-todo-keywords-1))
  11481. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11482. (defun org-fast-todo-selection ()
  11483. "Fast TODO keyword selection with single keys.
  11484. Returns the new TODO keyword, or nil if no state change should occur."
  11485. (let* ((fulltable org-todo-key-alist)
  11486. (done-keywords org-done-keywords) ;; needed for the faces.
  11487. (maxlen (apply 'max (mapcar
  11488. (lambda (x)
  11489. (if (stringp (car x)) (string-width (car x)) 0))
  11490. fulltable)))
  11491. (expert nil)
  11492. (fwidth (+ maxlen 3 1 3))
  11493. (ncol (/ (- (window-width) 4) fwidth))
  11494. tg cnt e c tbl
  11495. groups ingroup)
  11496. (save-excursion
  11497. (save-window-excursion
  11498. (if expert
  11499. (set-buffer (get-buffer-create " *Org todo*"))
  11500. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11501. (erase-buffer)
  11502. (org-set-local 'org-done-keywords done-keywords)
  11503. (setq tbl fulltable cnt 0)
  11504. (while (setq e (pop tbl))
  11505. (cond
  11506. ((equal e '(:startgroup))
  11507. (push '() groups) (setq ingroup t)
  11508. (when (not (= cnt 0))
  11509. (setq cnt 0)
  11510. (insert "\n"))
  11511. (insert "{ "))
  11512. ((equal e '(:endgroup))
  11513. (setq ingroup nil cnt 0)
  11514. (insert "}\n"))
  11515. ((equal e '(:newline))
  11516. (when (not (= cnt 0))
  11517. (setq cnt 0)
  11518. (insert "\n")
  11519. (setq e (car tbl))
  11520. (while (equal (car tbl) '(:newline))
  11521. (insert "\n")
  11522. (setq tbl (cdr tbl)))))
  11523. (t
  11524. (setq tg (car e) c (cdr e))
  11525. (if ingroup (push tg (car groups)))
  11526. (setq tg (org-add-props tg nil 'face
  11527. (org-get-todo-face tg)))
  11528. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11529. (insert "[" c "] " tg (make-string
  11530. (- fwidth 4 (length tg)) ?\ ))
  11531. (when (= (setq cnt (1+ cnt)) ncol)
  11532. (insert "\n")
  11533. (if ingroup (insert " "))
  11534. (setq cnt 0)))))
  11535. (insert "\n")
  11536. (goto-char (point-min))
  11537. (if (not expert) (org-fit-window-to-buffer))
  11538. (message "[a-z..]:Set [SPC]:clear")
  11539. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11540. (cond
  11541. ((or (= c ?\C-g)
  11542. (and (= c ?q) (not (rassoc c fulltable))))
  11543. (setq quit-flag t))
  11544. ((= c ?\ ) nil)
  11545. ((setq e (rassoc c fulltable) tg (car e))
  11546. tg)
  11547. (t (setq quit-flag t)))))))
  11548. (defun org-entry-is-todo-p ()
  11549. (member (org-get-todo-state) org-not-done-keywords))
  11550. (defun org-entry-is-done-p ()
  11551. (member (org-get-todo-state) org-done-keywords))
  11552. (defun org-get-todo-state ()
  11553. "Return the TODO keyword of the current subtree."
  11554. (save-excursion
  11555. (org-back-to-heading t)
  11556. (and (looking-at org-todo-line-regexp)
  11557. (match-end 2)
  11558. (match-string 2))))
  11559. (defun org-at-date-range-p (&optional inactive-ok)
  11560. "Is the cursor inside a date range?"
  11561. (interactive)
  11562. (save-excursion
  11563. (catch 'exit
  11564. (let ((pos (point)))
  11565. (skip-chars-backward "^[<\r\n")
  11566. (skip-chars-backward "<[")
  11567. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11568. (>= (match-end 0) pos)
  11569. (throw 'exit t))
  11570. (skip-chars-backward "^<[\r\n")
  11571. (skip-chars-backward "<[")
  11572. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11573. (>= (match-end 0) pos)
  11574. (throw 'exit t)))
  11575. nil)))
  11576. (defun org-get-repeat (&optional tagline)
  11577. "Check if there is a deadline/schedule with repeater in this entry."
  11578. (save-match-data
  11579. (save-excursion
  11580. (org-back-to-heading t)
  11581. (and (re-search-forward (if tagline
  11582. (concat tagline "\\s-*" org-repeat-re)
  11583. org-repeat-re)
  11584. (org-entry-end-position) t)
  11585. (match-string-no-properties 1)))))
  11586. (defvar org-last-changed-timestamp)
  11587. (defvar org-last-inserted-timestamp)
  11588. (defvar org-log-post-message)
  11589. (defvar org-log-note-purpose)
  11590. (defvar org-log-note-how)
  11591. (defvar org-log-note-extra)
  11592. (defun org-auto-repeat-maybe (done-word)
  11593. "Check if the current headline contains a repeated deadline/schedule.
  11594. If yes, set TODO state back to what it was and change the base date
  11595. of repeating deadline/scheduled time stamps to new date.
  11596. This function is run automatically after each state change to a DONE state."
  11597. ;; last-state is dynamically scoped into this function
  11598. (let* ((repeat (org-get-repeat))
  11599. (aa (assoc org-last-state org-todo-kwd-alist))
  11600. (interpret (nth 1 aa))
  11601. (head (nth 2 aa))
  11602. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11603. (msg "Entry repeats: ")
  11604. (org-log-done nil)
  11605. (org-todo-log-states nil)
  11606. re type n what ts time to-state)
  11607. (when repeat
  11608. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11609. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11610. org-todo-repeat-to-state))
  11611. (unless (and to-state (member to-state org-todo-keywords-1))
  11612. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11613. (org-todo to-state)
  11614. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11615. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11616. (org-time-stamp-format t t))))
  11617. (when org-log-repeat
  11618. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11619. (memq 'org-add-log-note post-command-hook))
  11620. ;; OK, we are already setup for some record
  11621. (if (eq org-log-repeat 'note)
  11622. ;; make sure we take a note, not only a time stamp
  11623. (setq org-log-note-how 'note))
  11624. ;; Set up for taking a record
  11625. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11626. org-last-state
  11627. 'findpos org-log-repeat)))
  11628. (org-back-to-heading t)
  11629. (org-add-planning-info nil nil 'closed)
  11630. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11631. org-deadline-time-regexp "\\)\\|\\("
  11632. org-ts-regexp "\\)"))
  11633. (while (re-search-forward
  11634. re (save-excursion (outline-next-heading) (point)) t)
  11635. (setq type (if (match-end 1) org-scheduled-string
  11636. (if (match-end 3) org-deadline-string "Plain:"))
  11637. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11638. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11639. (setq n (string-to-number (match-string 2 ts))
  11640. what (match-string 3 ts))
  11641. (if (equal what "w") (setq n (* n 7) what "d"))
  11642. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11643. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11644. ;; Preparation, see if we need to modify the start date for the change
  11645. (when (match-end 1)
  11646. (setq time (save-match-data (org-time-string-to-time ts)))
  11647. (cond
  11648. ((equal (match-string 1 ts) ".")
  11649. ;; Shift starting date to today
  11650. (org-timestamp-change
  11651. (- (org-today) (time-to-days time))
  11652. 'day))
  11653. ((equal (match-string 1 ts) "+")
  11654. (let ((nshiftmax 10) (nshift 0))
  11655. (while (or (= nshift 0)
  11656. (<= (time-to-days time)
  11657. (time-to-days (current-time))))
  11658. (when (= (incf nshift) nshiftmax)
  11659. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11660. (error "Abort")))
  11661. (org-timestamp-change n (cdr (assoc what whata)))
  11662. (org-at-timestamp-p t)
  11663. (setq ts (match-string 1))
  11664. (setq time (save-match-data (org-time-string-to-time ts)))))
  11665. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11666. ;; rematch, so that we have everything in place for the real shift
  11667. (org-at-timestamp-p t)
  11668. (setq ts (match-string 1))
  11669. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11670. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11671. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11672. (setq org-log-post-message msg)
  11673. (message "%s" msg))))
  11674. (defun org-show-todo-tree (arg)
  11675. "Make a compact tree which shows all headlines marked with TODO.
  11676. The tree will show the lines where the regexp matches, and all higher
  11677. headlines above the match.
  11678. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11679. With a numeric prefix N, construct a sparse tree for the Nth element
  11680. of `org-todo-keywords-1'."
  11681. (interactive "P")
  11682. (let ((case-fold-search nil)
  11683. (kwd-re
  11684. (cond ((null arg) org-not-done-regexp)
  11685. ((equal arg '(4))
  11686. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11687. (mapcar 'list org-todo-keywords-1))))
  11688. (concat "\\("
  11689. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11690. "\\)\\>")))
  11691. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11692. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11693. org-todo-keywords-1)))
  11694. (t (error "Invalid prefix argument: %s" arg)))))
  11695. (message "%d TODO entries found"
  11696. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11697. (defun org-deadline (arg &optional time)
  11698. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11699. With one universal prefix argument, remove any deadline from the item.
  11700. With two universal prefix arguments, prompt for a warning delay.
  11701. With argument TIME, set the deadline at the corresponding date. TIME
  11702. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11703. (interactive "P")
  11704. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11705. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11706. 'region-start-level 'region))
  11707. org-loop-over-headlines-in-active-region)
  11708. (org-map-entries
  11709. `(org-deadline ',arg ,time)
  11710. org-loop-over-headlines-in-active-region
  11711. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11712. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11713. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11714. (repeater (and old-date
  11715. (string-match
  11716. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11717. old-date)
  11718. (match-string 1 old-date))))
  11719. (cond
  11720. ((equal arg '(4))
  11721. (when (and old-date org-log-redeadline)
  11722. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11723. org-log-redeadline))
  11724. (org-remove-timestamp-with-keyword org-deadline-string)
  11725. (message "Item no longer has a deadline."))
  11726. ((equal arg '(16))
  11727. (save-excursion
  11728. (if (re-search-forward
  11729. org-deadline-time-regexp
  11730. (save-excursion (outline-next-heading) (point)) t)
  11731. (let* ((rpl0 (match-string 1))
  11732. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11733. (replace-match
  11734. (concat org-deadline-string
  11735. " <" rpl
  11736. (format " -%dd"
  11737. (abs
  11738. (- (time-to-days
  11739. (save-match-data
  11740. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11741. (time-to-days old-date-time))))
  11742. ">") t t))
  11743. (user-error "No deadline information to update"))))
  11744. (t
  11745. (org-add-planning-info 'deadline time 'closed)
  11746. (when (and old-date org-log-redeadline
  11747. (not (equal old-date
  11748. (substring org-last-inserted-timestamp 1 -1))))
  11749. (org-add-log-setup 'redeadline nil old-date 'findpos
  11750. org-log-redeadline))
  11751. (when repeater
  11752. (save-excursion
  11753. (org-back-to-heading t)
  11754. (when (re-search-forward (concat org-deadline-string " "
  11755. org-last-inserted-timestamp)
  11756. (save-excursion
  11757. (outline-next-heading) (point)) t)
  11758. (goto-char (1- (match-end 0)))
  11759. (insert " " repeater)
  11760. (setq org-last-inserted-timestamp
  11761. (concat (substring org-last-inserted-timestamp 0 -1)
  11762. " " repeater
  11763. (substring org-last-inserted-timestamp -1))))))
  11764. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11765. (defun org-schedule (arg &optional time)
  11766. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11767. With one universal prefix argument, remove any scheduling date from the item.
  11768. With two universal prefix arguments, prompt for a delay cookie.
  11769. With argument TIME, scheduled at the corresponding date. TIME can
  11770. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11771. (interactive "P")
  11772. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11773. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11774. 'region-start-level 'region))
  11775. org-loop-over-headlines-in-active-region)
  11776. (org-map-entries
  11777. `(org-schedule ',arg ,time)
  11778. org-loop-over-headlines-in-active-region
  11779. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11780. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11781. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11782. (repeater (and old-date
  11783. (string-match
  11784. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11785. old-date)
  11786. (match-string 1 old-date))))
  11787. (cond
  11788. ((equal arg '(4))
  11789. (progn
  11790. (when (and old-date org-log-reschedule)
  11791. (org-add-log-setup 'delschedule nil old-date 'findpos
  11792. org-log-reschedule))
  11793. (org-remove-timestamp-with-keyword org-scheduled-string)
  11794. (message "Item is no longer scheduled.")))
  11795. ((equal arg '(16))
  11796. (save-excursion
  11797. (if (re-search-forward
  11798. org-scheduled-time-regexp
  11799. (save-excursion (outline-next-heading) (point)) t)
  11800. (let* ((rpl0 (match-string 1))
  11801. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11802. (replace-match
  11803. (concat org-scheduled-string
  11804. " <" rpl
  11805. (format " -%dd"
  11806. (abs
  11807. (- (time-to-days
  11808. (save-match-data
  11809. (org-read-date nil t nil "Delay until" old-date-time)))
  11810. (time-to-days old-date-time))))
  11811. ">") t t))
  11812. (user-error "No scheduled information to update"))))
  11813. (t
  11814. (org-add-planning-info 'scheduled time 'closed)
  11815. (when (and old-date org-log-reschedule
  11816. (not (equal old-date
  11817. (substring org-last-inserted-timestamp 1 -1))))
  11818. (org-add-log-setup 'reschedule nil old-date 'findpos
  11819. org-log-reschedule))
  11820. (when repeater
  11821. (save-excursion
  11822. (org-back-to-heading t)
  11823. (when (re-search-forward (concat org-scheduled-string " "
  11824. org-last-inserted-timestamp)
  11825. (save-excursion
  11826. (outline-next-heading) (point)) t)
  11827. (goto-char (1- (match-end 0)))
  11828. (insert " " repeater)
  11829. (setq org-last-inserted-timestamp
  11830. (concat (substring org-last-inserted-timestamp 0 -1)
  11831. " " repeater
  11832. (substring org-last-inserted-timestamp -1))))))
  11833. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11834. (defun org-get-scheduled-time (pom &optional inherit)
  11835. "Get the scheduled time as a time tuple, of a format suitable
  11836. for calling org-schedule with, or if there is no scheduling,
  11837. returns nil."
  11838. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11839. (when time
  11840. (apply 'encode-time (org-parse-time-string time)))))
  11841. (defun org-get-deadline-time (pom &optional inherit)
  11842. "Get the deadline as a time tuple, of a format suitable for
  11843. calling org-deadline with, or if there is no scheduling, returns
  11844. nil."
  11845. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11846. (when time
  11847. (apply 'encode-time (org-parse-time-string time)))))
  11848. (defun org-remove-timestamp-with-keyword (keyword)
  11849. "Remove all time stamps with KEYWORD in the current entry."
  11850. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11851. beg)
  11852. (save-excursion
  11853. (org-back-to-heading t)
  11854. (setq beg (point))
  11855. (outline-next-heading)
  11856. (while (re-search-backward re beg t)
  11857. (replace-match "")
  11858. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11859. (equal (char-before) ?\ ))
  11860. (backward-delete-char 1)
  11861. (if (string-match "^[ \t]*$" (buffer-substring
  11862. (point-at-bol) (point-at-eol)))
  11863. (delete-region (point-at-bol)
  11864. (min (point-max) (1+ (point-at-eol))))))))))
  11865. (defun org-add-planning-info (what &optional time &rest remove)
  11866. "Insert new timestamp with keyword in the line directly after the headline.
  11867. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11868. If non is given, the user is prompted for a date.
  11869. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11870. be removed."
  11871. (interactive)
  11872. (let (org-time-was-given org-end-time-was-given ts
  11873. end default-time default-input)
  11874. (catch 'exit
  11875. (when (and (memq what '(scheduled deadline))
  11876. (or (not time)
  11877. (and (stringp time)
  11878. (string-match "^[-+]+[0-9]" time))))
  11879. ;; Try to get a default date/time from existing timestamp
  11880. (save-excursion
  11881. (org-back-to-heading t)
  11882. (setq end (save-excursion (outline-next-heading) (point)))
  11883. (when (re-search-forward (if (eq what 'scheduled)
  11884. org-scheduled-time-regexp
  11885. org-deadline-time-regexp)
  11886. end t)
  11887. (setq ts (match-string 1)
  11888. default-time
  11889. (apply 'encode-time (org-parse-time-string ts))
  11890. default-input (and ts (org-get-compact-tod ts))))))
  11891. (when what
  11892. (setq time
  11893. (if (stringp time)
  11894. ;; This is a string (relative or absolute), set proper date
  11895. (apply 'encode-time
  11896. (org-read-date-analyze
  11897. time default-time (decode-time default-time)))
  11898. ;; If necessary, get the time from the user
  11899. (or time (org-read-date nil 'to-time nil nil
  11900. default-time default-input)))))
  11901. (when (and org-insert-labeled-timestamps-at-point
  11902. (member what '(scheduled deadline)))
  11903. (insert
  11904. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11905. (org-insert-time-stamp time org-time-was-given
  11906. nil nil nil (list org-end-time-was-given))
  11907. (setq what nil))
  11908. (save-excursion
  11909. (save-restriction
  11910. (let (col list elt ts buffer-invisibility-spec)
  11911. (org-back-to-heading t)
  11912. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11913. (goto-char (match-end 1))
  11914. (setq col (current-column))
  11915. (goto-char (match-end 0))
  11916. (if (eobp) (insert "\n") (forward-char 1))
  11917. (when (and (not what)
  11918. (not (looking-at
  11919. (concat "[ \t]*"
  11920. org-keyword-time-not-clock-regexp))))
  11921. ;; Nothing to add, nothing to remove...... :-)
  11922. (throw 'exit nil))
  11923. (if (and (not (looking-at org-outline-regexp))
  11924. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11925. "[^\r\n]*"))
  11926. (not (equal (match-string 1) org-clock-string)))
  11927. (narrow-to-region (match-beginning 0) (match-end 0))
  11928. (insert-before-markers "\n")
  11929. (backward-char 1)
  11930. (narrow-to-region (point) (point))
  11931. (and org-adapt-indentation (org-indent-to-column col)))
  11932. ;; Check if we have to remove something.
  11933. (setq list (cons what remove))
  11934. (while list
  11935. (setq elt (pop list))
  11936. (when (or (and (eq elt 'scheduled)
  11937. (re-search-forward org-scheduled-time-regexp nil t))
  11938. (and (eq elt 'deadline)
  11939. (re-search-forward org-deadline-time-regexp nil t))
  11940. (and (eq elt 'closed)
  11941. (re-search-forward org-closed-time-regexp nil t)))
  11942. (replace-match "")
  11943. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11944. (and (looking-at "[ \t]+") (replace-match ""))
  11945. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11946. (when what
  11947. (insert
  11948. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11949. (cond ((eq what 'scheduled) org-scheduled-string)
  11950. ((eq what 'deadline) org-deadline-string)
  11951. ((eq what 'closed) org-closed-string))
  11952. " ")
  11953. (setq ts (org-insert-time-stamp
  11954. time
  11955. (or org-time-was-given
  11956. (and (eq what 'closed) org-log-done-with-time))
  11957. (eq what 'closed)
  11958. nil nil (list org-end-time-was-given)))
  11959. (insert
  11960. (if (not (or (bolp) (eq (char-before) ?\ )
  11961. (memq (char-after) '(32 10))
  11962. (eobp))) " " ""))
  11963. (end-of-line 1))
  11964. (goto-char (point-min))
  11965. (widen)
  11966. (if (and (looking-at "[ \t]*\n")
  11967. (equal (char-before) ?\n))
  11968. (delete-region (1- (point)) (point-at-eol)))
  11969. ts))))))
  11970. (defvar org-log-note-marker (make-marker))
  11971. (defvar org-log-note-purpose nil)
  11972. (defvar org-log-note-state nil)
  11973. (defvar org-log-note-previous-state nil)
  11974. (defvar org-log-note-how nil)
  11975. (defvar org-log-note-extra nil)
  11976. (defvar org-log-note-window-configuration nil)
  11977. (defvar org-log-note-return-to (make-marker))
  11978. (defvar org-log-note-effective-time nil
  11979. "Remembered current time so that dynamically scoped
  11980. `org-extend-today-until' affects tha timestamps in state change
  11981. log")
  11982. (defvar org-log-post-message nil
  11983. "Message to be displayed after a log note has been stored.
  11984. The auto-repeater uses this.")
  11985. (defun org-add-note ()
  11986. "Add a note to the current entry.
  11987. This is done in the same way as adding a state change note."
  11988. (interactive)
  11989. (org-add-log-setup 'note nil nil 'findpos nil))
  11990. (defvar org-property-end-re)
  11991. (defun org-add-log-setup (&optional purpose state prev-state
  11992. findpos how extra)
  11993. "Set up the post command hook to take a note.
  11994. If this is about to TODO state change, the new state is expected in STATE.
  11995. When FINDPOS is non-nil, find the correct position for the note in
  11996. the current entry. If not, assume that it can be inserted at point.
  11997. HOW is an indicator what kind of note should be created.
  11998. EXTRA is additional text that will be inserted into the notes buffer."
  11999. (let* ((org-log-into-drawer (org-log-into-drawer))
  12000. (drawer (cond ((stringp org-log-into-drawer)
  12001. org-log-into-drawer)
  12002. (org-log-into-drawer "LOGBOOK"))))
  12003. (save-restriction
  12004. (save-excursion
  12005. (when findpos
  12006. (org-back-to-heading t)
  12007. (narrow-to-region (point) (save-excursion
  12008. (outline-next-heading) (point)))
  12009. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12010. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12011. "[^\r\n]*\\)?"))
  12012. (goto-char (match-end 0))
  12013. (cond
  12014. (drawer
  12015. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12016. nil t)
  12017. (progn
  12018. (goto-char (match-end 0))
  12019. (or org-log-states-order-reversed
  12020. (and (re-search-forward org-property-end-re nil t)
  12021. (goto-char (1- (match-beginning 0))))))
  12022. (insert "\n:" drawer ":\n:END:")
  12023. (beginning-of-line 0)
  12024. (org-indent-line)
  12025. (beginning-of-line 2)
  12026. (org-indent-line)
  12027. (end-of-line 0)))
  12028. ((and org-log-state-notes-insert-after-drawers
  12029. (save-excursion
  12030. (forward-line) (looking-at org-drawer-regexp)))
  12031. (forward-line)
  12032. (while (looking-at org-drawer-regexp)
  12033. (goto-char (match-end 0))
  12034. (re-search-forward org-property-end-re (point-max) t)
  12035. (forward-line))
  12036. (forward-line -1)))
  12037. (unless org-log-states-order-reversed
  12038. (and (= (char-after) ?\n) (forward-char 1))
  12039. (org-skip-over-state-notes)
  12040. (skip-chars-backward " \t\n\r")))
  12041. (move-marker org-log-note-marker (point))
  12042. (setq org-log-note-purpose purpose
  12043. org-log-note-state state
  12044. org-log-note-previous-state prev-state
  12045. org-log-note-how how
  12046. org-log-note-extra extra
  12047. org-log-note-effective-time (org-current-effective-time))
  12048. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12049. (defun org-skip-over-state-notes ()
  12050. "Skip past the list of State notes in an entry."
  12051. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12052. (when (ignore-errors (goto-char (org-in-item-p)))
  12053. (let* ((struct (org-list-struct))
  12054. (prevs (org-list-prevs-alist struct)))
  12055. (while (looking-at "[ \t]*- State")
  12056. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12057. (org-list-get-item-end (point) struct)))))))
  12058. (defun org-add-log-note (&optional purpose)
  12059. "Pop up a window for taking a note, and add this note later at point."
  12060. (remove-hook 'post-command-hook 'org-add-log-note)
  12061. (setq org-log-note-window-configuration (current-window-configuration))
  12062. (delete-other-windows)
  12063. (move-marker org-log-note-return-to (point))
  12064. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12065. (goto-char org-log-note-marker)
  12066. (org-switch-to-buffer-other-window "*Org Note*")
  12067. (erase-buffer)
  12068. (if (memq org-log-note-how '(time state))
  12069. (let (current-prefix-arg) (org-store-log-note))
  12070. (let ((org-inhibit-startup t)) (org-mode))
  12071. (insert (format "# Insert note for %s.
  12072. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12073. (cond
  12074. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12075. ((eq org-log-note-purpose 'done) "closed todo item")
  12076. ((eq org-log-note-purpose 'state)
  12077. (format "state change from \"%s\" to \"%s\""
  12078. (or org-log-note-previous-state "")
  12079. (or org-log-note-state "")))
  12080. ((eq org-log-note-purpose 'reschedule)
  12081. "rescheduling")
  12082. ((eq org-log-note-purpose 'delschedule)
  12083. "no longer scheduled")
  12084. ((eq org-log-note-purpose 'redeadline)
  12085. "changing deadline")
  12086. ((eq org-log-note-purpose 'deldeadline)
  12087. "removing deadline")
  12088. ((eq org-log-note-purpose 'refile)
  12089. "refiling")
  12090. ((eq org-log-note-purpose 'note)
  12091. "this entry")
  12092. (t (error "This should not happen")))))
  12093. (if org-log-note-extra (insert org-log-note-extra))
  12094. (org-set-local 'org-finish-function 'org-store-log-note)
  12095. (run-hooks 'org-log-buffer-setup-hook)))
  12096. (defvar org-note-abort nil) ; dynamically scoped
  12097. (defun org-store-log-note ()
  12098. "Finish taking a log note, and insert it to where it belongs."
  12099. (let ((txt (buffer-string)))
  12100. (kill-buffer (current-buffer))
  12101. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12102. lines ind bul)
  12103. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12104. (setq txt (replace-match "" t t txt)))
  12105. (if (string-match "\\s-+\\'" txt)
  12106. (setq txt (replace-match "" t t txt)))
  12107. (setq lines (org-split-string txt "\n"))
  12108. (when (and note (string-match "\\S-" note))
  12109. (setq note
  12110. (org-replace-escapes
  12111. note
  12112. (list (cons "%u" (user-login-name))
  12113. (cons "%U" user-full-name)
  12114. (cons "%t" (format-time-string
  12115. (org-time-stamp-format 'long 'inactive)
  12116. org-log-note-effective-time))
  12117. (cons "%T" (format-time-string
  12118. (org-time-stamp-format 'long nil)
  12119. org-log-note-effective-time))
  12120. (cons "%d" (format-time-string
  12121. (org-time-stamp-format nil 'inactive)
  12122. org-log-note-effective-time))
  12123. (cons "%D" (format-time-string
  12124. (org-time-stamp-format nil nil)
  12125. org-log-note-effective-time))
  12126. (cons "%s" (if org-log-note-state
  12127. (concat "\"" org-log-note-state "\"")
  12128. ""))
  12129. (cons "%S" (if org-log-note-previous-state
  12130. (concat "\"" org-log-note-previous-state "\"")
  12131. "\"\"")))))
  12132. (if lines (setq note (concat note " \\\\")))
  12133. (push note lines))
  12134. (when (or current-prefix-arg org-note-abort)
  12135. (when org-log-into-drawer
  12136. (org-remove-empty-drawer-at
  12137. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12138. org-log-note-marker))
  12139. (setq lines nil))
  12140. (when lines
  12141. (with-current-buffer (marker-buffer org-log-note-marker)
  12142. (save-excursion
  12143. (goto-char org-log-note-marker)
  12144. (move-marker org-log-note-marker nil)
  12145. (end-of-line 1)
  12146. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12147. (setq ind (save-excursion
  12148. (if (ignore-errors (goto-char (org-in-item-p)))
  12149. (let ((struct (org-list-struct)))
  12150. (org-list-get-ind
  12151. (org-list-get-top-point struct) struct))
  12152. (skip-chars-backward " \r\t\n")
  12153. (cond
  12154. ((and (org-at-heading-p)
  12155. org-adapt-indentation)
  12156. (1+ (org-current-level)))
  12157. ((org-at-heading-p) 0)
  12158. (t (org-get-indentation))))))
  12159. (setq bul (org-list-bullet-string "-"))
  12160. (org-indent-line-to ind)
  12161. (insert bul (pop lines))
  12162. (let ((ind-body (+ (length bul) ind)))
  12163. (while lines
  12164. (insert "\n")
  12165. (org-indent-line-to ind-body)
  12166. (insert (pop lines))))
  12167. (message "Note stored")
  12168. (org-back-to-heading t)
  12169. (org-cycle-hide-drawers 'children))))))
  12170. (set-window-configuration org-log-note-window-configuration)
  12171. (with-current-buffer (marker-buffer org-log-note-return-to)
  12172. (goto-char org-log-note-return-to))
  12173. (move-marker org-log-note-return-to nil)
  12174. (and org-log-post-message (message "%s" org-log-post-message)))
  12175. (defun org-remove-empty-drawer-at (drawer pos)
  12176. "Remove an empty drawer DRAWER at position POS.
  12177. POS may also be a marker."
  12178. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12179. (save-excursion
  12180. (save-restriction
  12181. (widen)
  12182. (goto-char pos)
  12183. (if (org-in-regexp
  12184. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12185. (replace-match ""))))))
  12186. (defvar org-ts-type nil)
  12187. (defun org-sparse-tree (&optional arg type)
  12188. "Create a sparse tree, prompt for the details.
  12189. This command can create sparse trees. You first need to select the type
  12190. of match used to create the tree:
  12191. t Show all TODO entries.
  12192. T Show entries with a specific TODO keyword.
  12193. m Show entries selected by a tags/property match.
  12194. p Enter a property name and its value (both with completion on existing
  12195. names/values) and show entries with that property.
  12196. r Show entries matching a regular expression (`/' can be used as well).
  12197. b Show deadlines and scheduled items before a date.
  12198. a Show deadlines and scheduled items after a date.
  12199. d Show deadlines due within `org-deadline-warning-days'.
  12200. D Show deadlines and scheduled items between a date range."
  12201. (interactive "P")
  12202. (let (ans kwd value ts-type)
  12203. (setq type (or type org-sparse-tree-default-date-type))
  12204. (setq org-ts-type type)
  12205. (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"
  12206. (cond ((eq type 'all) "all timestamps")
  12207. ((eq type 'scheduled) "only scheduled")
  12208. ((eq type 'deadline) "only deadline")
  12209. ((eq type 'active) "only active timestamps")
  12210. ((eq type 'inactive) "only inactive timestamps")
  12211. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12212. ((eq type 'closed) "with a closed time-stamp")
  12213. (t "scheduled/deadline")))
  12214. (setq ans (read-char-exclusive))
  12215. (cond
  12216. ((equal ans ?c)
  12217. (org-sparse-tree
  12218. arg (cadr (member type '(scheduled-or-deadline
  12219. all scheduled deadline active inactive closed)))))
  12220. ((equal ans ?d)
  12221. (call-interactively 'org-check-deadlines))
  12222. ((equal ans ?b)
  12223. (call-interactively 'org-check-before-date))
  12224. ((equal ans ?a)
  12225. (call-interactively 'org-check-after-date))
  12226. ((equal ans ?D)
  12227. (call-interactively 'org-check-dates-range))
  12228. ((equal ans ?t)
  12229. (call-interactively 'org-show-todo-tree))
  12230. ((equal ans ?T)
  12231. (org-show-todo-tree '(4)))
  12232. ((member ans '(?T ?m))
  12233. (call-interactively 'org-match-sparse-tree))
  12234. ((member ans '(?p ?P))
  12235. (setq kwd (org-icompleting-read "Property: "
  12236. (mapcar 'list (org-buffer-property-keys))))
  12237. (setq value (org-icompleting-read "Value: "
  12238. (mapcar 'list (org-property-values kwd))))
  12239. (unless (string-match "\\`{.*}\\'" value)
  12240. (setq value (concat "\"" value "\"")))
  12241. (org-match-sparse-tree arg (concat kwd "=" value)))
  12242. ((member ans '(?r ?R ?/))
  12243. (call-interactively 'org-occur))
  12244. (t (error "No such sparse tree command \"%c\"" ans)))))
  12245. (defvar org-occur-highlights nil
  12246. "List of overlays used for occur matches.")
  12247. (make-variable-buffer-local 'org-occur-highlights)
  12248. (defvar org-occur-parameters nil
  12249. "Parameters of the active org-occur calls.
  12250. This is a list, each call to org-occur pushes as cons cell,
  12251. containing the regular expression and the callback, onto the list.
  12252. The list can contain several entries if `org-occur' has been called
  12253. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12254. will only contain one set of parameters. When the highlights are
  12255. removed (for example with `C-c C-c', or with the next edit (depending
  12256. on `org-remove-highlights-with-change'), this variable is emptied
  12257. as well.")
  12258. (make-variable-buffer-local 'org-occur-parameters)
  12259. (defun org-occur (regexp &optional keep-previous callback)
  12260. "Make a compact tree which shows all matches of REGEXP.
  12261. The tree will show the lines where the regexp matches, and all higher
  12262. headlines above the match. It will also show the heading after the match,
  12263. to make sure editing the matching entry is easy.
  12264. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12265. call to `org-occur' will be kept, to allow stacking of calls to this
  12266. command.
  12267. If CALLBACK is non-nil, it is a function which is called to confirm
  12268. that the match should indeed be shown."
  12269. (interactive "sRegexp: \nP")
  12270. (when (equal regexp "")
  12271. (error "Regexp cannot be empty"))
  12272. (unless keep-previous
  12273. (org-remove-occur-highlights nil nil t))
  12274. (push (cons regexp callback) org-occur-parameters)
  12275. (let ((cnt 0))
  12276. (save-excursion
  12277. (goto-char (point-min))
  12278. (if (or (not keep-previous) ; do not want to keep
  12279. (not org-occur-highlights)) ; no previous matches
  12280. ;; hide everything
  12281. (org-overview))
  12282. (while (re-search-forward regexp nil t)
  12283. (when (or (not callback)
  12284. (save-match-data (funcall callback)))
  12285. (setq cnt (1+ cnt))
  12286. (when org-highlight-sparse-tree-matches
  12287. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12288. (org-show-context 'occur-tree))))
  12289. (when org-remove-highlights-with-change
  12290. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12291. nil 'local))
  12292. (unless org-sparse-tree-open-archived-trees
  12293. (org-hide-archived-subtrees (point-min) (point-max)))
  12294. (run-hooks 'org-occur-hook)
  12295. (if (org-called-interactively-p 'interactive)
  12296. (message "%d match(es) for regexp %s" cnt regexp))
  12297. cnt))
  12298. (defun org-occur-next-match (&optional n reset)
  12299. "Function for `next-error-function' to find sparse tree matches.
  12300. N is the number of matches to move, when negative move backwards.
  12301. RESET is entirely ignored - this function always goes back to the
  12302. starting point when no match is found."
  12303. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12304. (search-func (if (< n 0)
  12305. 'previous-single-char-property-change
  12306. 'next-single-char-property-change))
  12307. (n (abs n))
  12308. (pos (point))
  12309. p1)
  12310. (catch 'exit
  12311. (while (setq p1 (funcall search-func (point) 'org-type))
  12312. (when (equal p1 limit)
  12313. (goto-char pos)
  12314. (error "No more matches"))
  12315. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12316. (setq n (1- n))
  12317. (when (= n 0)
  12318. (goto-char p1)
  12319. (throw 'exit (point))))
  12320. (goto-char p1))
  12321. (goto-char p1)
  12322. (error "No more matches"))))
  12323. (defun org-show-context (&optional key)
  12324. "Make sure point and context are visible.
  12325. How much context is shown depends upon the variables
  12326. `org-show-hierarchy-above', `org-show-following-heading',
  12327. `org-show-entry-below' and `org-show-siblings'."
  12328. (let ((heading-p (org-at-heading-p t))
  12329. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12330. (following-p (org-get-alist-option org-show-following-heading key))
  12331. (entry-p (org-get-alist-option org-show-entry-below key))
  12332. (siblings-p (org-get-alist-option org-show-siblings key)))
  12333. ;; Show heading or entry text
  12334. (if (and heading-p (not entry-p))
  12335. (org-flag-heading nil) ; only show the heading
  12336. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12337. (org-show-hidden-entry))) ; show entire entry
  12338. (when following-p
  12339. ;; Show next sibling, or heading below text
  12340. (save-excursion
  12341. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12342. (org-flag-heading nil))))
  12343. (when siblings-p (org-show-siblings))
  12344. (when hierarchy-p
  12345. ;; show all higher headings, possibly with siblings
  12346. (save-excursion
  12347. (while (and (condition-case nil
  12348. (progn (org-up-heading-all 1) t)
  12349. (error nil))
  12350. (not (bobp)))
  12351. (org-flag-heading nil)
  12352. (when siblings-p (org-show-siblings)))))
  12353. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12354. (defvar org-reveal-start-hook nil
  12355. "Hook run before revealing a location.")
  12356. (defun org-reveal (&optional siblings)
  12357. "Show current entry, hierarchy above it, and the following headline.
  12358. This can be used to show a consistent set of context around locations
  12359. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12360. not t for the search context.
  12361. With optional argument SIBLINGS, on each level of the hierarchy all
  12362. siblings are shown. This repairs the tree structure to what it would
  12363. look like when opened with hierarchical calls to `org-cycle'.
  12364. With double optional argument \\[universal-argument] \\[universal-argument], \
  12365. go to the parent and show the
  12366. entire tree."
  12367. (interactive "P")
  12368. (run-hooks 'org-reveal-start-hook)
  12369. (let ((org-show-hierarchy-above t)
  12370. (org-show-following-heading t)
  12371. (org-show-siblings (if siblings t org-show-siblings)))
  12372. (org-show-context nil))
  12373. (when (equal siblings '(16))
  12374. (save-excursion
  12375. (when (org-up-heading-safe)
  12376. (org-show-subtree)
  12377. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12378. (defun org-highlight-new-match (beg end)
  12379. "Highlight from BEG to END and mark the highlight is an occur headline."
  12380. (let ((ov (make-overlay beg end)))
  12381. (overlay-put ov 'face 'secondary-selection)
  12382. (overlay-put ov 'org-type 'org-occur)
  12383. (push ov org-occur-highlights)))
  12384. (defun org-remove-occur-highlights (&optional beg end noremove)
  12385. "Remove the occur highlights from the buffer.
  12386. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12387. from the `before-change-functions' in the current buffer."
  12388. (interactive)
  12389. (unless org-inhibit-highlight-removal
  12390. (mapc 'delete-overlay org-occur-highlights)
  12391. (setq org-occur-highlights nil)
  12392. (setq org-occur-parameters nil)
  12393. (unless noremove
  12394. (remove-hook 'before-change-functions
  12395. 'org-remove-occur-highlights 'local))))
  12396. ;;;; Priorities
  12397. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12398. "Regular expression matching the priority indicator.")
  12399. (defvar org-remove-priority-next-time nil)
  12400. (defun org-priority-up ()
  12401. "Increase the priority of the current item."
  12402. (interactive)
  12403. (org-priority 'up))
  12404. (defun org-priority-down ()
  12405. "Decrease the priority of the current item."
  12406. (interactive)
  12407. (org-priority 'down))
  12408. (defun org-priority (&optional action show)
  12409. "Change the priority of an item.
  12410. ACTION can be `set', `up', `down', or a character."
  12411. (interactive "P")
  12412. (if (equal action '(4))
  12413. (org-show-priority)
  12414. (unless org-enable-priority-commands
  12415. (error "Priority commands are disabled"))
  12416. (setq action (or action 'set))
  12417. (let (current new news have remove)
  12418. (save-excursion
  12419. (org-back-to-heading t)
  12420. (if (looking-at org-priority-regexp)
  12421. (setq current (string-to-char (match-string 2))
  12422. have t))
  12423. (cond
  12424. ((eq action 'remove)
  12425. (setq remove t new ?\ ))
  12426. ((or (eq action 'set)
  12427. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12428. (if (not (eq action 'set))
  12429. (setq new action)
  12430. (message "Priority %c-%c, SPC to remove: "
  12431. org-highest-priority org-lowest-priority)
  12432. (save-match-data
  12433. (setq new (read-char-exclusive))))
  12434. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12435. (= (upcase org-lowest-priority) org-lowest-priority))
  12436. (setq new (upcase new)))
  12437. (cond ((equal new ?\ ) (setq remove t))
  12438. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12439. (error "Priority must be between `%c' and `%c'"
  12440. org-highest-priority org-lowest-priority))))
  12441. ((eq action 'up)
  12442. (setq new (if have
  12443. (1- current) ; normal cycling
  12444. ;; last priority was empty
  12445. (if (eq last-command this-command)
  12446. org-lowest-priority ; wrap around empty to lowest
  12447. ;; default
  12448. (if org-priority-start-cycle-with-default
  12449. org-default-priority
  12450. (1- org-default-priority))))))
  12451. ((eq action 'down)
  12452. (setq new (if have
  12453. (1+ current) ; normal cycling
  12454. ;; last priority was empty
  12455. (if (eq last-command this-command)
  12456. org-highest-priority ; wrap around empty to highest
  12457. ;; default
  12458. (if org-priority-start-cycle-with-default
  12459. org-default-priority
  12460. (1+ org-default-priority))))))
  12461. (t (error "Invalid action")))
  12462. (if (or (< (upcase new) org-highest-priority)
  12463. (> (upcase new) org-lowest-priority))
  12464. (if (and (memq action '(up down))
  12465. (not have) (not (eq last-command this-command)))
  12466. ;; `new' is from default priority
  12467. (error
  12468. "The default can not be set, see `org-default-priority' why")
  12469. ;; normal cycling: `new' is beyond highest/lowest priority
  12470. ;; and is wrapped around to the empty priority
  12471. (setq remove t)))
  12472. (setq news (format "%c" new))
  12473. (if have
  12474. (if remove
  12475. (replace-match "" t t nil 1)
  12476. (replace-match news t t nil 2))
  12477. (if remove
  12478. (error "No priority cookie found in line")
  12479. (let ((case-fold-search nil))
  12480. (looking-at org-todo-line-regexp))
  12481. (if (match-end 2)
  12482. (progn
  12483. (goto-char (match-end 2))
  12484. (insert " [#" news "]"))
  12485. (goto-char (match-beginning 3))
  12486. (insert "[#" news "] "))))
  12487. (org-preserve-lc (org-set-tags nil 'align)))
  12488. (if remove
  12489. (message "Priority removed")
  12490. (message "Priority of current item set to %s" news)))))
  12491. (defun org-show-priority ()
  12492. "Show the priority of the current item.
  12493. This priority is composed of the main priority given with the [#A] cookies,
  12494. and by additional input from the age of a schedules or deadline entry."
  12495. (interactive)
  12496. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12497. (org-get-at-bol 'priority)
  12498. (save-excursion
  12499. (save-match-data
  12500. (beginning-of-line)
  12501. (and (looking-at org-heading-regexp)
  12502. (org-get-priority (match-string 0))))))))
  12503. (message "Priority is %d" (if pri pri -1000))))
  12504. (defun org-get-priority (s)
  12505. "Find priority cookie and return priority."
  12506. (save-match-data
  12507. (if (functionp org-get-priority-function)
  12508. (funcall org-get-priority-function)
  12509. (if (not (string-match org-priority-regexp s))
  12510. (* 1000 (- org-lowest-priority org-default-priority))
  12511. (* 1000 (- org-lowest-priority
  12512. (string-to-char (match-string 2 s))))))))
  12513. ;;;; Tags
  12514. (defvar org-agenda-archives-mode)
  12515. (defvar org-map-continue-from nil
  12516. "Position from where mapping should continue.
  12517. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12518. (defvar org-scanner-tags nil
  12519. "The current tag list while the tags scanner is running.")
  12520. (defvar org-trust-scanner-tags nil
  12521. "Should `org-get-tags-at' use the tags for the scanner.
  12522. This is for internal dynamical scoping only.
  12523. When this is non-nil, the function `org-get-tags-at' will return the value
  12524. of `org-scanner-tags' instead of building the list by itself. This
  12525. can lead to large speed-ups when the tags scanner is used in a file with
  12526. many entries, and when the list of tags is retrieved, for example to
  12527. obtain a list of properties. Building the tags list for each entry in such
  12528. a file becomes an N^2 operation - but with this variable set, it scales
  12529. as N.")
  12530. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12531. "Sca headline tags with inheritance and produce output ACTION.
  12532. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12533. or `agenda' to produce an entry list for an agenda view. It can also be
  12534. a Lisp form or a function that should be called at each matched headline, in
  12535. this case the return value is a list of all return values from these calls.
  12536. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12537. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12538. only lines with a not-done TODO keyword are included in the output.
  12539. This should be the same variable that was scoped into
  12540. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12541. START-LEVEL can be a string with asterisks, reducing the scope to
  12542. headlines matching this string."
  12543. (require 'org-agenda)
  12544. (let* ((re (concat "^"
  12545. (if start-level
  12546. ;; Get the correct level to match
  12547. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12548. org-outline-regexp)
  12549. " *\\(\\<\\("
  12550. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12551. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12552. (props (list 'face 'default
  12553. 'done-face 'org-agenda-done
  12554. 'undone-face 'default
  12555. 'mouse-face 'highlight
  12556. 'org-not-done-regexp org-not-done-regexp
  12557. 'org-todo-regexp org-todo-regexp
  12558. 'org-complex-heading-regexp org-complex-heading-regexp
  12559. 'help-echo
  12560. (format "mouse-2 or RET jump to org file %s"
  12561. (abbreviate-file-name
  12562. (or (buffer-file-name (buffer-base-buffer))
  12563. (buffer-name (buffer-base-buffer)))))))
  12564. (case-fold-search nil)
  12565. (org-map-continue-from nil)
  12566. lspos tags tags-list
  12567. (tags-alist (list (cons 0 org-file-tags)))
  12568. (llast 0) rtn rtn1 level category i txt
  12569. todo marker entry priority)
  12570. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12571. (setq action (list 'lambda nil action)))
  12572. (save-excursion
  12573. (goto-char (point-min))
  12574. (when (eq action 'sparse-tree)
  12575. (org-overview)
  12576. (org-remove-occur-highlights))
  12577. (while (re-search-forward re nil t)
  12578. (setq org-map-continue-from nil)
  12579. (catch :skip
  12580. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12581. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12582. (goto-char (setq lspos (match-beginning 0)))
  12583. (setq level (org-reduced-level (org-outline-level))
  12584. category (org-get-category))
  12585. (setq i llast llast level)
  12586. ;; remove tag lists from same and sublevels
  12587. (while (>= i level)
  12588. (when (setq entry (assoc i tags-alist))
  12589. (setq tags-alist (delete entry tags-alist)))
  12590. (setq i (1- i)))
  12591. ;; add the next tags
  12592. (when tags
  12593. (setq tags (org-split-string tags ":")
  12594. tags-alist
  12595. (cons (cons level tags) tags-alist)))
  12596. ;; compile tags for current headline
  12597. (setq tags-list
  12598. (if org-use-tag-inheritance
  12599. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12600. tags)
  12601. org-scanner-tags tags-list)
  12602. (when org-use-tag-inheritance
  12603. (setcdr (car tags-alist)
  12604. (mapcar (lambda (x)
  12605. (setq x (copy-sequence x))
  12606. (org-add-prop-inherited x))
  12607. (cdar tags-alist))))
  12608. (when (and tags org-use-tag-inheritance
  12609. (or (not (eq t org-use-tag-inheritance))
  12610. org-tags-exclude-from-inheritance))
  12611. ;; selective inheritance, remove uninherited ones
  12612. (setcdr (car tags-alist)
  12613. (org-remove-uninherited-tags (cdar tags-alist))))
  12614. (when (and
  12615. ;; eval matcher only when the todo condition is OK
  12616. (and (or (not todo-only) (member todo org-not-done-keywords))
  12617. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12618. (eval matcher)))
  12619. ;; Call the skipper, but return t if it does not skip,
  12620. ;; so that the `and' form continues evaluating
  12621. (progn
  12622. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12623. t)
  12624. ;; Check if timestamps are deselecting this entry
  12625. (or (not todo-only)
  12626. (and (member todo org-not-done-keywords)
  12627. (or (not org-agenda-tags-todo-honor-ignore-options)
  12628. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12629. ;; select this headline
  12630. (cond
  12631. ((eq action 'sparse-tree)
  12632. (and org-highlight-sparse-tree-matches
  12633. (org-get-heading) (match-end 0)
  12634. (org-highlight-new-match
  12635. (match-beginning 1) (match-end 1)))
  12636. (org-show-context 'tags-tree))
  12637. ((eq action 'agenda)
  12638. (setq txt (org-agenda-format-item
  12639. ""
  12640. (concat
  12641. (if (eq org-tags-match-list-sublevels 'indented)
  12642. (make-string (1- level) ?.) "")
  12643. (org-get-heading))
  12644. level category
  12645. tags-list)
  12646. priority (org-get-priority txt))
  12647. (goto-char lspos)
  12648. (setq marker (org-agenda-new-marker))
  12649. (org-add-props txt props
  12650. 'org-marker marker 'org-hd-marker marker 'org-category category
  12651. 'todo-state todo
  12652. 'priority priority 'type "tagsmatch")
  12653. (push txt rtn))
  12654. ((functionp action)
  12655. (setq org-map-continue-from nil)
  12656. (save-excursion
  12657. (setq rtn1 (funcall action))
  12658. (push rtn1 rtn)))
  12659. (t (error "Invalid action")))
  12660. ;; if we are to skip sublevels, jump to end of subtree
  12661. (unless org-tags-match-list-sublevels
  12662. (org-end-of-subtree t)
  12663. (backward-char 1))))
  12664. ;; Get the correct position from where to continue
  12665. (if org-map-continue-from
  12666. (goto-char org-map-continue-from)
  12667. (and (= (point) lspos) (end-of-line 1)))))
  12668. (when (and (eq action 'sparse-tree)
  12669. (not org-sparse-tree-open-archived-trees))
  12670. (org-hide-archived-subtrees (point-min) (point-max)))
  12671. (nreverse rtn)))
  12672. (defun org-remove-uninherited-tags (tags)
  12673. "Remove all tags that are not inherited from the list TAGS."
  12674. (cond
  12675. ((eq org-use-tag-inheritance t)
  12676. (if org-tags-exclude-from-inheritance
  12677. (org-delete-all org-tags-exclude-from-inheritance tags)
  12678. tags))
  12679. ((not org-use-tag-inheritance) nil)
  12680. ((stringp org-use-tag-inheritance)
  12681. (delq nil (mapcar
  12682. (lambda (x)
  12683. (if (and (string-match org-use-tag-inheritance x)
  12684. (not (member x org-tags-exclude-from-inheritance)))
  12685. x nil))
  12686. tags)))
  12687. ((listp org-use-tag-inheritance)
  12688. (delq nil (mapcar
  12689. (lambda (x)
  12690. (if (member x org-use-tag-inheritance) x nil))
  12691. tags)))))
  12692. (defun org-match-sparse-tree (&optional todo-only match)
  12693. "Create a sparse tree according to tags string MATCH.
  12694. MATCH can contain positive and negative selection of tags, like
  12695. \"+WORK+URGENT-WITHBOSS\".
  12696. If optional argument TODO-ONLY is non-nil, only select lines that are
  12697. also TODO lines."
  12698. (interactive "P")
  12699. (org-agenda-prepare-buffers (list (current-buffer)))
  12700. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12701. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12702. (defvar org-cached-props nil)
  12703. (defun org-cached-entry-get (pom property)
  12704. (if (or (eq t org-use-property-inheritance)
  12705. (and (stringp org-use-property-inheritance)
  12706. (string-match org-use-property-inheritance property))
  12707. (and (listp org-use-property-inheritance)
  12708. (member property org-use-property-inheritance)))
  12709. ;; Caching is not possible, check it directly
  12710. (org-entry-get pom property 'inherit)
  12711. ;; Get all properties, so that we can do complicated checks easily
  12712. (cdr (assoc property (or org-cached-props
  12713. (setq org-cached-props
  12714. (org-entry-properties pom)))))))
  12715. (defun org-global-tags-completion-table (&optional files)
  12716. "Return the list of all tags in all agenda buffer/files.
  12717. Optional FILES argument is a list of files which can be used
  12718. instead of the agenda files."
  12719. (save-excursion
  12720. (org-uniquify
  12721. (delq nil
  12722. (apply 'append
  12723. (mapcar
  12724. (lambda (file)
  12725. (set-buffer (find-file-noselect file))
  12726. (append (org-get-buffer-tags)
  12727. (mapcar (lambda (x) (if (stringp (car-safe x))
  12728. (list (car-safe x)) nil))
  12729. org-tag-alist)))
  12730. (if (and files (car files))
  12731. files
  12732. (org-agenda-files))))))))
  12733. (defun org-make-tags-matcher (match)
  12734. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12735. The variable `todo-only' is scoped dynamically into this function.
  12736. It will be set to t if the matcher restricts matching to TODO entries,
  12737. otherwise will not be touched.
  12738. Returns a cons of the selection string MATCH and the constructed
  12739. lisp form implementing the matcher. The matcher is to be evaluated
  12740. at an Org entry, with point on the headline, and returns t if the
  12741. entry matches the selection string MATCH. The returned lisp form
  12742. references two variables with information about the entry, which
  12743. must be bound around the form's evaluation: todo, the TODO keyword
  12744. at the entry (or nil of none); and tags-list, the list of all tags
  12745. at the entry including inherited ones. Additionally, the category
  12746. of the entry (if any) must be specified as the text property
  12747. 'org-category on the headline.
  12748. See also `org-scan-tags'.
  12749. "
  12750. (declare (special todo-only))
  12751. (unless (boundp 'todo-only)
  12752. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12753. (unless match
  12754. ;; Get a new match request, with completion
  12755. (let ((org-last-tags-completion-table
  12756. (org-global-tags-completion-table)))
  12757. (setq match (org-completing-read-no-i
  12758. "Match: " 'org-tags-completion-function nil nil nil
  12759. 'org-tags-history))))
  12760. ;; Parse the string and create a lisp form
  12761. (let ((match0 match)
  12762. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12763. minus tag mm
  12764. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12765. orterms term orlist re-p str-p level-p level-op time-p
  12766. prop-p pn pv po gv rest)
  12767. ;; Expand group tags
  12768. (setq match (org-tags-expand match))
  12769. (if (string-match "/+" match)
  12770. ;; match contains also a todo-matching request
  12771. (progn
  12772. (setq tagsmatch (substring match 0 (match-beginning 0))
  12773. todomatch (substring match (match-end 0)))
  12774. (if (string-match "^!" todomatch)
  12775. (setq todo-only t todomatch (substring todomatch 1)))
  12776. (if (string-match "^\\s-*$" todomatch)
  12777. (setq todomatch nil)))
  12778. ;; only matching tags
  12779. (setq tagsmatch match todomatch nil))
  12780. ;; Make the tags matcher
  12781. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12782. (setq tagsmatcher t)
  12783. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12784. (while (setq term (pop orterms))
  12785. (while (and (equal (substring term -1) "\\") orterms)
  12786. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12787. (while (string-match re term)
  12788. (setq rest (substring term (match-end 0))
  12789. minus (and (match-end 1)
  12790. (equal (match-string 1 term) "-"))
  12791. tag (save-match-data (replace-regexp-in-string
  12792. "\\\\-" "-"
  12793. (match-string 2 term)))
  12794. re-p (equal (string-to-char tag) ?{)
  12795. level-p (match-end 4)
  12796. prop-p (match-end 5)
  12797. mm (cond
  12798. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12799. (level-p
  12800. (setq level-op (org-op-to-function (match-string 3 term)))
  12801. `(,level-op level ,(string-to-number
  12802. (match-string 4 term))))
  12803. (prop-p
  12804. (setq pn (match-string 5 term)
  12805. po (match-string 6 term)
  12806. pv (match-string 7 term)
  12807. re-p (equal (string-to-char pv) ?{)
  12808. str-p (equal (string-to-char pv) ?\")
  12809. time-p (save-match-data
  12810. (string-match "^\"[[<].*[]>]\"$" pv))
  12811. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12812. (if time-p (setq pv (org-matcher-time pv)))
  12813. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12814. (cond
  12815. ((equal pn "CATEGORY")
  12816. (setq gv '(get-text-property (point) 'org-category)))
  12817. ((equal pn "TODO")
  12818. (setq gv 'todo))
  12819. (t
  12820. (setq gv `(org-cached-entry-get nil ,pn))))
  12821. (if re-p
  12822. (if (eq po 'org<>)
  12823. `(not (string-match ,pv (or ,gv "")))
  12824. `(string-match ,pv (or ,gv "")))
  12825. (if str-p
  12826. `(,po (or ,gv "") ,pv)
  12827. `(,po (string-to-number (or ,gv ""))
  12828. ,(string-to-number pv) ))))
  12829. (t `(member ,tag tags-list)))
  12830. mm (if minus (list 'not mm) mm)
  12831. term rest)
  12832. (push mm tagsmatcher))
  12833. (push (if (> (length tagsmatcher) 1)
  12834. (cons 'and tagsmatcher)
  12835. (car tagsmatcher))
  12836. orlist)
  12837. (setq tagsmatcher nil))
  12838. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12839. (setq tagsmatcher
  12840. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12841. ;; Make the todo matcher
  12842. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12843. (setq todomatcher t)
  12844. (setq orterms (org-split-string todomatch "|") orlist nil)
  12845. (while (setq term (pop orterms))
  12846. (while (string-match re term)
  12847. (setq minus (and (match-end 1)
  12848. (equal (match-string 1 term) "-"))
  12849. kwd (match-string 2 term)
  12850. re-p (equal (string-to-char kwd) ?{)
  12851. term (substring term (match-end 0))
  12852. mm (if re-p
  12853. `(string-match ,(substring kwd 1 -1) todo)
  12854. (list 'equal 'todo kwd))
  12855. mm (if minus (list 'not mm) mm))
  12856. (push mm todomatcher))
  12857. (push (if (> (length todomatcher) 1)
  12858. (cons 'and todomatcher)
  12859. (car todomatcher))
  12860. orlist)
  12861. (setq todomatcher nil))
  12862. (setq todomatcher (if (> (length orlist) 1)
  12863. (cons 'or orlist) (car orlist))))
  12864. ;; Return the string and lisp forms of the matcher
  12865. (setq matcher (if todomatcher
  12866. (list 'and tagsmatcher todomatcher)
  12867. tagsmatcher))
  12868. (when todo-only
  12869. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12870. matcher)))
  12871. (cons match0 matcher)))
  12872. (defun org-tags-expand (match &optional single-as-list downcased)
  12873. "Expand group tags in MATCH.
  12874. This replaces every group tag in MATCH with a regexp tag search.
  12875. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12876. will be replaced like this:
  12877. Work => {\(?:Work\|Lab\|Conf\}
  12878. +Work => +{\(?:Work\|Lab\|Conf\}
  12879. -Work => -{\(?:Work\|Lab\|Conf\}
  12880. Replacing by a regexp preserves the structure of the match.
  12881. E.g., this expansion
  12882. Work|Home => {\(?:Work\|Lab\|Conf\}|Home
  12883. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12884. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12885. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12886. assumed to be a single group tag, and the function will return
  12887. the list of tags in this group.
  12888. When DOWNCASE is non-nil, expand downcased TAGS."
  12889. (if org-group-tags
  12890. (let* ((case-fold-search t)
  12891. (stable org-mode-syntax-table)
  12892. (tal (or org-tag-groups-alist-for-agenda
  12893. org-tag-groups-alist))
  12894. (tal (if downcased (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12895. (tml (mapcar 'car tal))
  12896. (rtnmatch match) rpl)
  12897. ;; @ and _ are allowed as word-components in tags
  12898. (modify-syntax-entry ?@ "w" stable)
  12899. (modify-syntax-entry ?_ "w" stable)
  12900. (while (and tml (string-match
  12901. (concat "\\(?1:[+-]?\\)\\(?2:\\<" (regexp-opt tml) "\\>\\)")
  12902. rtnmatch))
  12903. (let* ((dir (match-string 1 rtnmatch))
  12904. (tag (match-string 2 rtnmatch))
  12905. (tag (if downcased (downcase tag) tag)))
  12906. (setq tml (delete tag tml))
  12907. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12908. (setq rtnmatch
  12909. (replace-match
  12910. (concat dir "{" (regexp-opt rpl) "}") t t rtnmatch))))
  12911. (if single-as-list
  12912. (or (reverse rpl) (list rtnmatch))
  12913. rtnmatch))
  12914. (if single-as-list (list (if downcased (downcase match) match))
  12915. match)))
  12916. (defun org-op-to-function (op &optional stringp)
  12917. "Turn an operator into the appropriate function."
  12918. (setq op
  12919. (cond
  12920. ((equal op "<" ) '(< string< org-time<))
  12921. ((equal op ">" ) '(> org-string> org-time>))
  12922. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12923. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12924. ((member op '("=" "==")) '(= string= org-time=))
  12925. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12926. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12927. (defun org<> (a b) (not (= a b)))
  12928. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12929. (defun org-string>= (a b) (not (string< a b)))
  12930. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12931. (defun org-string<> (a b) (not (string= 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) (>= a b)))
  12937. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12938. (defun org-2ft (s)
  12939. "Convert S to a floating point time.
  12940. If S is already a number, just return it. If it is a string, parse
  12941. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12942. (cond
  12943. ((numberp s) s)
  12944. ((stringp s)
  12945. (condition-case nil
  12946. (float-time (apply 'encode-time (org-parse-time-string s)))
  12947. (error 0.)))
  12948. (t 0.)))
  12949. (defun org-time-today ()
  12950. "Time in seconds today at 0:00.
  12951. Returns the float number of seconds since the beginning of the
  12952. epoch to the beginning of today (00:00)."
  12953. (float-time (apply 'encode-time
  12954. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12955. (defun org-matcher-time (s)
  12956. "Interpret a time comparison value."
  12957. (save-match-data
  12958. (cond
  12959. ((string= s "<now>") (float-time))
  12960. ((string= s "<today>") (org-time-today))
  12961. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12962. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12963. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12964. (+ (org-time-today)
  12965. (* (string-to-number (match-string 1 s))
  12966. (cdr (assoc (match-string 2 s)
  12967. '(("d" . 86400.0) ("w" . 604800.0)
  12968. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12969. (t (org-2ft s)))))
  12970. (defun org-match-any-p (re list)
  12971. "Does re match any element of list?"
  12972. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12973. (delq nil list))
  12974. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12975. (defvar org-tags-overlay (make-overlay 1 1))
  12976. (org-detach-overlay org-tags-overlay)
  12977. (defun org-get-local-tags-at (&optional pos)
  12978. "Get a list of tags defined in the current headline."
  12979. (org-get-tags-at pos 'local))
  12980. (defun org-get-local-tags ()
  12981. "Get a list of tags defined in the current headline."
  12982. (org-get-tags-at nil 'local))
  12983. (defun org-get-tags-at (&optional pos local)
  12984. "Get a list of all headline tags applicable at POS.
  12985. POS defaults to point. If tags are inherited, the list contains
  12986. the targets in the same sequence as the headlines appear, i.e.
  12987. the tags of the current headline come last.
  12988. When LOCAL is non-nil, only return tags from the current headline,
  12989. ignore inherited ones."
  12990. (interactive)
  12991. (if (and org-trust-scanner-tags
  12992. (or (not pos) (equal pos (point)))
  12993. (not local))
  12994. org-scanner-tags
  12995. (let (tags ltags lastpos parent)
  12996. (save-excursion
  12997. (save-restriction
  12998. (widen)
  12999. (goto-char (or pos (point)))
  13000. (save-match-data
  13001. (catch 'done
  13002. (condition-case nil
  13003. (progn
  13004. (org-back-to-heading t)
  13005. (while (not (equal lastpos (point)))
  13006. (setq lastpos (point))
  13007. (when (looking-at
  13008. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13009. (setq ltags (org-split-string
  13010. (org-match-string-no-properties 1) ":"))
  13011. (when parent
  13012. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13013. (setq tags (append
  13014. (if parent
  13015. (org-remove-uninherited-tags ltags)
  13016. ltags)
  13017. tags)))
  13018. (or org-use-tag-inheritance (throw 'done t))
  13019. (if local (throw 'done t))
  13020. (or (org-up-heading-safe) (error nil))
  13021. (setq parent t)))
  13022. (error nil)))))
  13023. (if local
  13024. tags
  13025. (reverse (delete-dups
  13026. (reverse (append
  13027. (org-remove-uninherited-tags
  13028. org-file-tags) tags)))))))))
  13029. (defun org-add-prop-inherited (s)
  13030. (add-text-properties 0 (length s) '(inherited t) s)
  13031. s)
  13032. (defun org-toggle-tag (tag &optional onoff)
  13033. "Toggle the tag TAG for the current line.
  13034. If ONOFF is `on' or `off', don't toggle but set to this state."
  13035. (let (res current)
  13036. (save-excursion
  13037. (org-back-to-heading t)
  13038. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13039. (point-at-eol) t)
  13040. (progn
  13041. (setq current (match-string 1))
  13042. (replace-match ""))
  13043. (setq current ""))
  13044. (setq current (nreverse (org-split-string current ":")))
  13045. (cond
  13046. ((eq onoff 'on)
  13047. (setq res t)
  13048. (or (member tag current) (push tag current)))
  13049. ((eq onoff 'off)
  13050. (or (not (member tag current)) (setq current (delete tag current))))
  13051. (t (if (member tag current)
  13052. (setq current (delete tag current))
  13053. (setq res t)
  13054. (push tag current))))
  13055. (end-of-line 1)
  13056. (if current
  13057. (progn
  13058. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13059. (org-set-tags nil t))
  13060. (delete-horizontal-space))
  13061. (run-hooks 'org-after-tags-change-hook))
  13062. res))
  13063. (defun org-align-tags-here (to-col)
  13064. ;; Assumes that this is a headline
  13065. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13066. (beginning-of-line 1)
  13067. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13068. (< pos (match-beginning 2)))
  13069. (progn
  13070. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13071. (goto-char (match-beginning 1))
  13072. (insert " ")
  13073. (delete-region (point) (1+ (match-beginning 2)))
  13074. (setq ncol (max (current-column)
  13075. (1+ col)
  13076. (if (> to-col 0)
  13077. to-col
  13078. (- (abs to-col) tags-l))))
  13079. (setq p (point))
  13080. (insert (make-string (- ncol (current-column)) ?\ ))
  13081. (setq ncol (current-column))
  13082. (when indent-tabs-mode (tabify p (point-at-eol)))
  13083. (org-move-to-column (min ncol col) t))
  13084. (goto-char pos))))
  13085. (defun org-set-tags-command (&optional arg just-align)
  13086. "Call the set-tags command for the current entry."
  13087. (interactive "P")
  13088. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13089. (org-set-tags arg just-align)
  13090. (save-excursion
  13091. (unless (and (org-region-active-p)
  13092. org-loop-over-headlines-in-active-region)
  13093. (org-back-to-heading t))
  13094. (org-set-tags arg just-align))))
  13095. (defun org-set-tags-to (data)
  13096. "Set the tags of the current entry to DATA, replacing the current tags.
  13097. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13098. If DATA is nil or the empty string, any tags will be removed."
  13099. (interactive "sTags: ")
  13100. (setq data
  13101. (cond
  13102. ((eq data nil) "")
  13103. ((equal data "") "")
  13104. ((stringp data)
  13105. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13106. ":"))
  13107. ((listp data)
  13108. (concat ":" (mapconcat 'identity data ":") ":"))))
  13109. (when data
  13110. (save-excursion
  13111. (org-back-to-heading t)
  13112. (when (looking-at org-complex-heading-regexp)
  13113. (if (match-end 5)
  13114. (progn
  13115. (goto-char (match-beginning 5))
  13116. (insert data)
  13117. (delete-region (point) (point-at-eol))
  13118. (org-set-tags nil 'align))
  13119. (goto-char (point-at-eol))
  13120. (insert " " data)
  13121. (org-set-tags nil 'align)))
  13122. (beginning-of-line 1)
  13123. (if (looking-at ".*?\\([ \t]+\\)$")
  13124. (delete-region (match-beginning 1) (match-end 1))))))
  13125. (defun org-align-all-tags ()
  13126. "Align the tags i all headings."
  13127. (interactive)
  13128. (save-excursion
  13129. (or (ignore-errors (org-back-to-heading t))
  13130. (outline-next-heading))
  13131. (if (org-at-heading-p)
  13132. (org-set-tags t)
  13133. (message "No headings"))))
  13134. (defvar org-indent-indentation-per-level)
  13135. (defun org-set-tags (&optional arg just-align)
  13136. "Set the tags for the current headline.
  13137. With prefix ARG, realign all tags in headings in the current buffer."
  13138. (interactive "P")
  13139. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13140. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13141. 'region-start-level 'region))
  13142. org-loop-over-headlines-in-active-region)
  13143. (org-map-entries
  13144. ;; We don't use ARG and JUST-ALIGN here these args are not
  13145. ;; useful when looping over headlines
  13146. `(org-set-tags)
  13147. org-loop-over-headlines-in-active-region
  13148. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13149. (let* ((re org-outline-regexp-bol)
  13150. (current (unless arg (org-get-tags-string)))
  13151. (col (current-column))
  13152. (org-setting-tags t)
  13153. table current-tags inherited-tags ; computed below when needed
  13154. tags p0 c0 c1 rpl di tc level)
  13155. (if arg
  13156. (save-excursion
  13157. (goto-char (point-min))
  13158. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13159. (while (re-search-forward re nil t)
  13160. (org-set-tags nil t)
  13161. (end-of-line 1)))
  13162. (message "All tags realigned to column %d" org-tags-column))
  13163. (if just-align
  13164. (setq tags current)
  13165. ;; Get a new set of tags from the user
  13166. (save-excursion
  13167. (setq table (append org-tag-persistent-alist
  13168. (or org-tag-alist (org-get-buffer-tags))
  13169. (and
  13170. org-complete-tags-always-offer-all-agenda-tags
  13171. (org-global-tags-completion-table
  13172. (org-agenda-files))))
  13173. org-last-tags-completion-table table
  13174. current-tags (org-split-string current ":")
  13175. inherited-tags (nreverse
  13176. (nthcdr (length current-tags)
  13177. (nreverse (org-get-tags-at))))
  13178. tags
  13179. (if (or (eq t org-use-fast-tag-selection)
  13180. (and org-use-fast-tag-selection
  13181. (delq nil (mapcar 'cdr table))))
  13182. (org-fast-tag-selection
  13183. current-tags inherited-tags table
  13184. (if org-fast-tag-selection-include-todo
  13185. org-todo-key-alist))
  13186. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13187. (org-trim
  13188. (org-icompleting-read "Tags: "
  13189. 'org-tags-completion-function
  13190. nil nil current 'org-tags-history))))))
  13191. (while (string-match "[-+&]+" tags)
  13192. ;; No boolean logic, just a list
  13193. (setq tags (replace-match ":" t t tags))))
  13194. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13195. (if org-tags-sort-function
  13196. (setq tags (mapconcat 'identity
  13197. (sort (org-split-string
  13198. tags (org-re "[^[:alnum:]_@#%]+"))
  13199. org-tags-sort-function) ":")))
  13200. (if (string-match "\\`[\t ]*\\'" tags)
  13201. (setq tags "")
  13202. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13203. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13204. ;; Insert new tags at the correct column
  13205. (beginning-of-line 1)
  13206. (setq level (or (and (looking-at org-outline-regexp)
  13207. (- (match-end 0) (point) 1))
  13208. 1))
  13209. (cond
  13210. ((and (equal current "") (equal tags "")))
  13211. ((re-search-forward
  13212. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13213. (point-at-eol) t)
  13214. (if (equal tags "")
  13215. (setq rpl "")
  13216. (goto-char (match-beginning 0))
  13217. (setq c0 (current-column)
  13218. ;; compute offset for the case of org-indent-mode active
  13219. di (if org-indent-mode
  13220. (* (1- org-indent-indentation-per-level) (1- level))
  13221. 0)
  13222. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13223. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13224. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13225. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13226. (replace-match rpl t t)
  13227. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13228. tags)
  13229. (t (error "Tags alignment failed")))
  13230. (org-move-to-column col)
  13231. (unless just-align
  13232. (run-hooks 'org-after-tags-change-hook))))))
  13233. (defun org-change-tag-in-region (beg end tag off)
  13234. "Add or remove TAG for each entry in the region.
  13235. This works in the agenda, and also in an org-mode buffer."
  13236. (interactive
  13237. (list (region-beginning) (region-end)
  13238. (let ((org-last-tags-completion-table
  13239. (if (derived-mode-p 'org-mode)
  13240. (org-get-buffer-tags)
  13241. (org-global-tags-completion-table))))
  13242. (org-icompleting-read
  13243. "Tag: " 'org-tags-completion-function nil nil nil
  13244. 'org-tags-history))
  13245. (progn
  13246. (message "[s]et or [r]emove? ")
  13247. (equal (read-char-exclusive) ?r))))
  13248. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13249. (let ((agendap (equal major-mode 'org-agenda-mode))
  13250. l1 l2 m buf pos newhead (cnt 0))
  13251. (goto-char end)
  13252. (setq l2 (1- (org-current-line)))
  13253. (goto-char beg)
  13254. (setq l1 (org-current-line))
  13255. (loop for l from l1 to l2 do
  13256. (org-goto-line l)
  13257. (setq m (get-text-property (point) 'org-hd-marker))
  13258. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13259. (and agendap m))
  13260. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13261. pos (if agendap m (point)))
  13262. (with-current-buffer buf
  13263. (save-excursion
  13264. (save-restriction
  13265. (goto-char pos)
  13266. (setq cnt (1+ cnt))
  13267. (org-toggle-tag tag (if off 'off 'on))
  13268. (setq newhead (org-get-heading)))))
  13269. (and agendap (org-agenda-change-all-lines newhead m))))
  13270. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13271. (defun org-tags-completion-function (string predicate &optional flag)
  13272. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13273. (confirm (lambda (x) (stringp (car x)))))
  13274. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13275. (setq s1 (match-string 1 string)
  13276. s2 (match-string 2 string))
  13277. (setq s1 "" s2 string))
  13278. (cond
  13279. ((eq flag nil)
  13280. ;; try completion
  13281. (setq rtn (try-completion s2 ctable confirm))
  13282. (if (stringp rtn)
  13283. (setq rtn
  13284. (concat s1 s2 (substring rtn (length s2))
  13285. (if (and org-add-colon-after-tag-completion
  13286. (assoc rtn ctable))
  13287. ":" ""))))
  13288. rtn)
  13289. ((eq flag t)
  13290. ;; all-completions
  13291. (all-completions s2 ctable confirm))
  13292. ((eq flag 'lambda)
  13293. ;; exact match?
  13294. (assoc s2 ctable)))))
  13295. (defun org-fast-tag-insert (kwd tags face &optional end)
  13296. "Insert KDW, and the TAGS, the latter with face FACE.
  13297. Also insert END."
  13298. (insert (format "%-12s" (concat kwd ":"))
  13299. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13300. (or end "")))
  13301. (defun org-fast-tag-show-exit (flag)
  13302. (save-excursion
  13303. (org-goto-line 3)
  13304. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13305. (replace-match ""))
  13306. (when flag
  13307. (end-of-line 1)
  13308. (org-move-to-column (- (window-width) 19) t)
  13309. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13310. (defun org-set-current-tags-overlay (current prefix)
  13311. "Add an overlay to CURRENT tag with PREFIX."
  13312. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13313. (if (featurep 'xemacs)
  13314. (org-overlay-display org-tags-overlay (concat prefix s)
  13315. 'secondary-selection)
  13316. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13317. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13318. (defvar org-last-tag-selection-key nil)
  13319. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13320. "Fast tag selection with single keys.
  13321. CURRENT is the current list of tags in the headline, INHERITED is the
  13322. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13323. possibly with grouping information. TODO-TABLE is a similar table with
  13324. TODO keywords, should these have keys assigned to them.
  13325. If the keys are nil, a-z are automatically assigned.
  13326. Returns the new tags string, or nil to not change the current settings."
  13327. (let* ((fulltable (append table todo-table))
  13328. (maxlen (apply 'max (mapcar
  13329. (lambda (x)
  13330. (if (stringp (car x)) (string-width (car x)) 0))
  13331. fulltable)))
  13332. (buf (current-buffer))
  13333. (expert (eq org-fast-tag-selection-single-key 'expert))
  13334. (buffer-tags nil)
  13335. (fwidth (+ maxlen 3 1 3))
  13336. (ncol (/ (- (window-width) 4) fwidth))
  13337. (i-face 'org-done)
  13338. (c-face 'org-todo)
  13339. tg cnt e c char c1 c2 ntable tbl rtn
  13340. ov-start ov-end ov-prefix
  13341. (exit-after-next org-fast-tag-selection-single-key)
  13342. (done-keywords org-done-keywords)
  13343. groups ingroup)
  13344. (save-excursion
  13345. (beginning-of-line 1)
  13346. (if (looking-at
  13347. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13348. (setq ov-start (match-beginning 1)
  13349. ov-end (match-end 1)
  13350. ov-prefix "")
  13351. (setq ov-start (1- (point-at-eol))
  13352. ov-end (1+ ov-start))
  13353. (skip-chars-forward "^\n\r")
  13354. (setq ov-prefix
  13355. (concat
  13356. (buffer-substring (1- (point)) (point))
  13357. (if (> (current-column) org-tags-column)
  13358. " "
  13359. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13360. (move-overlay org-tags-overlay ov-start ov-end)
  13361. (save-window-excursion
  13362. (if expert
  13363. (set-buffer (get-buffer-create " *Org tags*"))
  13364. (delete-other-windows)
  13365. (split-window-vertically)
  13366. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13367. (erase-buffer)
  13368. (org-set-local 'org-done-keywords done-keywords)
  13369. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13370. (org-fast-tag-insert "Current" current c-face "\n\n")
  13371. (org-fast-tag-show-exit exit-after-next)
  13372. (org-set-current-tags-overlay current ov-prefix)
  13373. (setq tbl fulltable char ?a cnt 0)
  13374. (while (setq e (pop tbl))
  13375. (cond
  13376. ((equal (car e) :startgroup)
  13377. (push '() groups) (setq ingroup t)
  13378. (when (not (= cnt 0))
  13379. (setq cnt 0)
  13380. (insert "\n"))
  13381. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13382. ((equal (car e) :endgroup)
  13383. (setq ingroup nil cnt 0)
  13384. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13385. ((equal e '(:newline))
  13386. (when (not (= cnt 0))
  13387. (setq cnt 0)
  13388. (insert "\n")
  13389. (setq e (car tbl))
  13390. (while (equal (car tbl) '(:newline))
  13391. (insert "\n")
  13392. (setq tbl (cdr tbl)))))
  13393. ((equal e '(:grouptags)) nil)
  13394. (t
  13395. (setq tg (copy-sequence (car e)) c2 nil)
  13396. (if (cdr e)
  13397. (setq c (cdr e))
  13398. ;; automatically assign a character.
  13399. (setq c1 (string-to-char
  13400. (downcase (substring
  13401. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13402. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13403. (while (or (rassoc char ntable) (rassoc char table))
  13404. (setq char (1+ char)))
  13405. (setq c2 c1))
  13406. (setq c (or c2 char)))
  13407. (if ingroup (push tg (car groups)))
  13408. (setq tg (org-add-props tg nil 'face
  13409. (cond
  13410. ((not (assoc tg table))
  13411. (org-get-todo-face tg))
  13412. ((member tg current) c-face)
  13413. ((member tg inherited) i-face))))
  13414. (if (equal (caar tbl) :grouptags)
  13415. (org-add-props tg nil 'face 'org-tag-group))
  13416. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13417. (insert "[" c "] " tg (make-string
  13418. (- fwidth 4 (length tg)) ?\ ))
  13419. (push (cons tg c) ntable)
  13420. (when (= (setq cnt (1+ cnt)) ncol)
  13421. (insert "\n")
  13422. (if ingroup (insert " "))
  13423. (setq cnt 0)))))
  13424. (setq ntable (nreverse ntable))
  13425. (insert "\n")
  13426. (goto-char (point-min))
  13427. (if (not expert) (org-fit-window-to-buffer))
  13428. (setq rtn
  13429. (catch 'exit
  13430. (while t
  13431. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13432. (if (not groups) "no " "")
  13433. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13434. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13435. (setq org-last-tag-selection-key c)
  13436. (cond
  13437. ((= c ?\r) (throw 'exit t))
  13438. ((= c ?!)
  13439. (setq groups (not groups))
  13440. (goto-char (point-min))
  13441. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13442. ((= c ?\C-c)
  13443. (if (not expert)
  13444. (org-fast-tag-show-exit
  13445. (setq exit-after-next (not exit-after-next)))
  13446. (setq expert nil)
  13447. (delete-other-windows)
  13448. (set-window-buffer (split-window-vertically) " *Org tags*")
  13449. (org-switch-to-buffer-other-window " *Org tags*")
  13450. (org-fit-window-to-buffer)))
  13451. ((or (= c ?\C-g)
  13452. (and (= c ?q) (not (rassoc c ntable))))
  13453. (org-detach-overlay org-tags-overlay)
  13454. (setq quit-flag t))
  13455. ((= c ?\ )
  13456. (setq current nil)
  13457. (if exit-after-next (setq exit-after-next 'now)))
  13458. ((= c ?\t)
  13459. (condition-case nil
  13460. (setq tg (org-icompleting-read
  13461. "Tag: "
  13462. (or buffer-tags
  13463. (with-current-buffer buf
  13464. (org-get-buffer-tags)))))
  13465. (quit (setq tg "")))
  13466. (when (string-match "\\S-" tg)
  13467. (add-to-list 'buffer-tags (list tg))
  13468. (if (member tg current)
  13469. (setq current (delete tg current))
  13470. (push tg current)))
  13471. (if exit-after-next (setq exit-after-next 'now)))
  13472. ((setq e (rassoc c todo-table) tg (car e))
  13473. (with-current-buffer buf
  13474. (save-excursion (org-todo tg)))
  13475. (if exit-after-next (setq exit-after-next 'now)))
  13476. ((setq e (rassoc c ntable) tg (car e))
  13477. (if (member tg current)
  13478. (setq current (delete tg current))
  13479. (loop for g in groups do
  13480. (if (member tg g)
  13481. (mapc (lambda (x)
  13482. (setq current (delete x current)))
  13483. g)))
  13484. (push tg current))
  13485. (if exit-after-next (setq exit-after-next 'now))))
  13486. ;; Create a sorted list
  13487. (setq current
  13488. (sort current
  13489. (lambda (a b)
  13490. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13491. (if (eq exit-after-next 'now) (throw 'exit t))
  13492. (goto-char (point-min))
  13493. (beginning-of-line 2)
  13494. (delete-region (point) (point-at-eol))
  13495. (org-fast-tag-insert "Current" current c-face)
  13496. (org-set-current-tags-overlay current ov-prefix)
  13497. (while (re-search-forward
  13498. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13499. (setq tg (match-string 1))
  13500. (add-text-properties
  13501. (match-beginning 1) (match-end 1)
  13502. (list 'face
  13503. (cond
  13504. ((member tg current) c-face)
  13505. ((member tg inherited) i-face)
  13506. (t (get-text-property (match-beginning 1) 'face))))))
  13507. (goto-char (point-min)))))
  13508. (org-detach-overlay org-tags-overlay)
  13509. (if rtn
  13510. (mapconcat 'identity current ":")
  13511. nil))))
  13512. (defun org-get-tags-string ()
  13513. "Get the TAGS string in the current headline."
  13514. (unless (org-at-heading-p t)
  13515. (error "Not on a heading"))
  13516. (save-excursion
  13517. (beginning-of-line 1)
  13518. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13519. (org-match-string-no-properties 1)
  13520. "")))
  13521. (defun org-get-tags ()
  13522. "Get the list of tags specified in the current headline."
  13523. (org-split-string (org-get-tags-string) ":"))
  13524. (defun org-get-buffer-tags ()
  13525. "Get a table of all tags used in the buffer, for completion."
  13526. (let (tags)
  13527. (save-excursion
  13528. (goto-char (point-min))
  13529. (while (re-search-forward
  13530. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13531. (when (equal (char-after (point-at-bol 0)) ?*)
  13532. (mapc (lambda (x) (add-to-list 'tags x))
  13533. (org-split-string (org-match-string-no-properties 1) ":")))))
  13534. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13535. (mapcar 'list tags)))
  13536. ;;;; The mapping API
  13537. (defun org-map-entries (func &optional match scope &rest skip)
  13538. "Call FUNC at each headline selected by MATCH in SCOPE.
  13539. FUNC is a function or a lisp form. The function will be called without
  13540. arguments, with the cursor positioned at the beginning of the headline.
  13541. The return values of all calls to the function will be collected and
  13542. returned as a list.
  13543. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13544. does not need to preserve point. After evaluation, the cursor will be
  13545. moved to the end of the line (presumably of the headline of the
  13546. processed entry) and search continues from there. Under some
  13547. circumstances, this may not produce the wanted results. For example,
  13548. if you have removed (e.g. archived) the current (sub)tree it could
  13549. mean that the next entry will be skipped entirely. In such cases, you
  13550. can specify the position from where search should continue by making
  13551. FUNC set the variable `org-map-continue-from' to the desired buffer
  13552. position.
  13553. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13554. Only headlines that are matched by this query will be considered during
  13555. the iteration. When MATCH is nil or t, all headlines will be
  13556. visited by the iteration.
  13557. SCOPE determines the scope of this command. It can be any of:
  13558. nil The current buffer, respecting the restriction if any
  13559. tree The subtree started with the entry at point
  13560. region The entries within the active region, if any
  13561. region-start-level
  13562. The entries within the active region, but only those at
  13563. the same level than the first one.
  13564. file The current buffer, without restriction
  13565. file-with-archives
  13566. The current buffer, and any archives associated with it
  13567. agenda All agenda files
  13568. agenda-with-archives
  13569. All agenda files with any archive files associated with them
  13570. \(file1 file2 ...)
  13571. If this is a list, all files in the list will be scanned
  13572. The remaining args are treated as settings for the skipping facilities of
  13573. the scanner. The following items can be given here:
  13574. archive skip trees with the archive tag
  13575. comment skip trees with the COMMENT keyword
  13576. function or Emacs Lisp form:
  13577. will be used as value for `org-agenda-skip-function', so
  13578. whenever the function returns a position, FUNC will not be
  13579. called for that entry and search will continue from the
  13580. position returned
  13581. If your function needs to retrieve the tags including inherited tags
  13582. at the *current* entry, you can use the value of the variable
  13583. `org-scanner-tags' which will be much faster than getting the value
  13584. with `org-get-tags-at'. If your function gets properties with
  13585. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13586. to t around the call to `org-entry-properties' to get the same speedup.
  13587. Note that if your function moves around to retrieve tags and properties at
  13588. a *different* entry, you cannot use these techniques."
  13589. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13590. (not (org-region-active-p)))
  13591. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13592. (org-agenda-skip-archived-trees (memq 'archive skip))
  13593. (org-agenda-skip-comment-trees (memq 'comment skip))
  13594. (org-agenda-skip-function
  13595. (car (org-delete-all '(comment archive) skip)))
  13596. (org-tags-match-list-sublevels t)
  13597. (start-level (eq scope 'region-start-level))
  13598. matcher file res
  13599. org-todo-keywords-for-agenda
  13600. org-done-keywords-for-agenda
  13601. org-todo-keyword-alist-for-agenda
  13602. org-drawers-for-agenda
  13603. org-tag-alist-for-agenda
  13604. todo-only)
  13605. (cond
  13606. ((eq match t) (setq matcher t))
  13607. ((eq match nil) (setq matcher t))
  13608. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13609. (save-window-excursion
  13610. (save-restriction
  13611. (cond ((eq scope 'tree)
  13612. (org-back-to-heading t)
  13613. (org-narrow-to-subtree)
  13614. (setq scope nil))
  13615. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13616. (org-region-active-p))
  13617. ;; If needed, set start-level to a string like "2"
  13618. (when start-level
  13619. (save-excursion
  13620. (goto-char (region-beginning))
  13621. (unless (org-at-heading-p) (outline-next-heading))
  13622. (setq start-level (org-current-level))))
  13623. (narrow-to-region (region-beginning)
  13624. (save-excursion
  13625. (goto-char (region-end))
  13626. (unless (and (bolp) (org-at-heading-p))
  13627. (outline-next-heading))
  13628. (point)))
  13629. (setq scope nil)))
  13630. (if (not scope)
  13631. (progn
  13632. (org-agenda-prepare-buffers
  13633. (list (buffer-file-name (current-buffer))))
  13634. (setq res (org-scan-tags func matcher todo-only start-level)))
  13635. ;; Get the right scope
  13636. (cond
  13637. ((and scope (listp scope) (symbolp (car scope)))
  13638. (setq scope (eval scope)))
  13639. ((eq scope 'agenda)
  13640. (setq scope (org-agenda-files t)))
  13641. ((eq scope 'agenda-with-archives)
  13642. (setq scope (org-agenda-files t))
  13643. (setq scope (org-add-archive-files scope)))
  13644. ((eq scope 'file)
  13645. (setq scope (list (buffer-file-name))))
  13646. ((eq scope 'file-with-archives)
  13647. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13648. (org-agenda-prepare-buffers scope)
  13649. (while (setq file (pop scope))
  13650. (with-current-buffer (org-find-base-buffer-visiting file)
  13651. (save-excursion
  13652. (save-restriction
  13653. (widen)
  13654. (goto-char (point-min))
  13655. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13656. res)))
  13657. ;;;; Properties
  13658. ;;; Setting and retrieving properties
  13659. (defconst org-special-properties
  13660. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13661. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13662. "The special properties valid in Org-mode.
  13663. These are properties that are not defined in the property drawer,
  13664. but in some other way.")
  13665. (defconst org-default-properties
  13666. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13667. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13668. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13669. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13670. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13671. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13672. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13673. "Some properties that are used by Org-mode for various purposes.
  13674. Being in this list makes sure that they are offered for completion.")
  13675. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13676. "Regular expression matching the first line of a property drawer.")
  13677. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13678. "Regular expression matching the last line of a property drawer.")
  13679. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13680. "Regular expression matching the first line of a property drawer.")
  13681. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13682. "Regular expression matching the first line of a property drawer.")
  13683. (defconst org-property-drawer-re
  13684. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13685. org-property-end-re "\\)\n?")
  13686. "Matches an entire property drawer.")
  13687. (defconst org-clock-drawer-re
  13688. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13689. org-property-end-re "\\)\n?")
  13690. "Matches an entire clock drawer.")
  13691. (defsubst org-re-property (property)
  13692. "Return a regexp matching a PROPERTY line.
  13693. Match group 1 will be set to the value."
  13694. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13695. (defsubst org-re-property-keyword (property)
  13696. "Return a regexp matching a PROPERTY line, possibly with no
  13697. value for the property."
  13698. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13699. (defun org-property-action ()
  13700. "Do an action on properties."
  13701. (interactive)
  13702. (let (c)
  13703. (org-at-property-p)
  13704. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13705. (setq c (read-char-exclusive))
  13706. (cond
  13707. ((equal c ?s)
  13708. (call-interactively 'org-set-property))
  13709. ((equal c ?d)
  13710. (call-interactively 'org-delete-property))
  13711. ((equal c ?D)
  13712. (call-interactively 'org-delete-property-globally))
  13713. ((equal c ?c)
  13714. (call-interactively 'org-compute-property-at-point))
  13715. (t (error "No such property action %c" c)))))
  13716. (defun org-inc-effort ()
  13717. "Increment the value of the effort property in the current entry."
  13718. (interactive)
  13719. (org-set-effort nil t))
  13720. (defvar org-clock-effort) ;; Defined in org-clock.el
  13721. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13722. (defun org-set-effort (&optional value increment)
  13723. "Set the effort property of the current entry.
  13724. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13725. 10th allowed value.
  13726. When INCREMENT is non-nil, set the property to the next allowed value."
  13727. (interactive "P")
  13728. (if (equal value 0) (setq value 10))
  13729. (let* ((completion-ignore-case t)
  13730. (prop org-effort-property)
  13731. (cur (org-entry-get nil prop))
  13732. (allowed (org-property-get-allowed-values nil prop 'table))
  13733. (existing (mapcar 'list (org-property-values prop)))
  13734. (heading (nth 4 (org-heading-components)))
  13735. rpl
  13736. (val (cond
  13737. ((stringp value) value)
  13738. ((and allowed (integerp value))
  13739. (or (car (nth (1- value) allowed))
  13740. (car (org-last allowed))))
  13741. ((and allowed increment)
  13742. (or (caadr (member (list cur) allowed))
  13743. (error "Allowed effort values are not set")))
  13744. (allowed
  13745. (message "Select 1-9,0, [RET%s]: %s"
  13746. (if cur (concat "=" cur) "")
  13747. (mapconcat 'car allowed " "))
  13748. (setq rpl (read-char-exclusive))
  13749. (if (equal rpl ?\r)
  13750. cur
  13751. (setq rpl (- rpl ?0))
  13752. (if (equal rpl 0) (setq rpl 10))
  13753. (if (and (> rpl 0) (<= rpl (length allowed)))
  13754. (car (nth (1- rpl) allowed))
  13755. (org-completing-read "Effort: " allowed nil))))
  13756. (t
  13757. (let (org-completion-use-ido org-completion-use-iswitchb)
  13758. (org-completing-read
  13759. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13760. (concat "[" cur "]") "")
  13761. ": ")
  13762. existing nil nil "" nil cur))))))
  13763. (unless (equal (org-entry-get nil prop) val)
  13764. (org-entry-put nil prop val))
  13765. (save-excursion
  13766. (org-back-to-heading t)
  13767. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13768. (when (string= heading org-clock-current-task)
  13769. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13770. (org-clock-update-mode-line))
  13771. (message "%s is now %s" prop val)))
  13772. (defun org-at-property-p ()
  13773. "Is cursor inside a property drawer?"
  13774. (save-excursion
  13775. (beginning-of-line 1)
  13776. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13777. (save-match-data ;; Used by calling procedures
  13778. (let ((p (point))
  13779. (range (unless (org-before-first-heading-p)
  13780. (org-get-property-block))))
  13781. (and range (<= (car range) p) (< p (cdr range))))))))
  13782. (defun org-get-property-block (&optional beg end force)
  13783. "Return the (beg . end) range of the body of the property drawer.
  13784. BEG and END are the beginning and end of the current subtree, or of
  13785. the part before the first headline. If they are not given, they will
  13786. be found. If the drawer does not exist and FORCE is non-nil, create
  13787. the drawer."
  13788. (catch 'exit
  13789. (save-excursion
  13790. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13791. (progn (org-back-to-heading t) (point))))
  13792. (end (or end (and (not (outline-next-heading)) (point-max))
  13793. (point))))
  13794. (goto-char beg)
  13795. (if (re-search-forward org-property-start-re end t)
  13796. (setq beg (1+ (match-end 0)))
  13797. (if force
  13798. (save-excursion
  13799. (org-insert-property-drawer)
  13800. (setq end (progn (outline-next-heading) (point))))
  13801. (throw 'exit nil))
  13802. (goto-char beg)
  13803. (if (re-search-forward org-property-start-re end t)
  13804. (setq beg (1+ (match-end 0)))))
  13805. (if (re-search-forward org-property-end-re end t)
  13806. (setq end (match-beginning 0))
  13807. (or force (throw 'exit nil))
  13808. (goto-char beg)
  13809. (setq end beg)
  13810. (org-indent-line)
  13811. (insert ":END:\n"))
  13812. (cons beg end)))))
  13813. (defun org-entry-properties (&optional pom which specific)
  13814. "Get all properties of the entry at point-or-marker POM.
  13815. This includes the TODO keyword, the tags, time strings for deadline,
  13816. scheduled, and clocking, and any additional properties defined in the
  13817. entry. The return value is an alist, keys may occur multiple times
  13818. if the property key was used several times.
  13819. POM may also be nil, in which case the current entry is used.
  13820. If WHICH is nil or `all', get all properties. If WHICH is
  13821. `special' or `standard', only get that subclass. If WHICH
  13822. is a string only get exactly this property. SPECIFIC can be a string, the
  13823. specific property we are interested in. Specifying it can speed
  13824. things up because then unnecessary parsing is avoided."
  13825. (setq which (or which 'all))
  13826. (org-with-point-at pom
  13827. (let ((clockstr (substring org-clock-string 0 -1))
  13828. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13829. (case-fold-search nil)
  13830. beg end range props sum-props key key1 value string clocksum clocksumt)
  13831. (save-excursion
  13832. (when (condition-case nil
  13833. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13834. (error nil))
  13835. (setq beg (point))
  13836. (setq sum-props (get-text-property (point) 'org-summaries))
  13837. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13838. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13839. (outline-next-heading)
  13840. (setq end (point))
  13841. (when (memq which '(all special))
  13842. ;; Get the special properties, like TODO and tags
  13843. (goto-char beg)
  13844. (when (and (or (not specific) (string= specific "TODO"))
  13845. (looking-at org-todo-line-regexp) (match-end 2))
  13846. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13847. (when (and (or (not specific) (string= specific "PRIORITY"))
  13848. (looking-at org-priority-regexp))
  13849. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13850. (when (or (not specific) (string= specific "FILE"))
  13851. (push (cons "FILE" buffer-file-name) props))
  13852. (when (and (or (not specific) (string= specific "TAGS"))
  13853. (setq value (org-get-tags-string))
  13854. (string-match "\\S-" value))
  13855. (push (cons "TAGS" value) props))
  13856. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13857. (setq value (org-get-tags-at)))
  13858. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13859. ":"))
  13860. props))
  13861. (when (or (not specific) (string= specific "BLOCKED"))
  13862. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13863. (when (or (not specific)
  13864. (member specific
  13865. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13866. "TIMESTAMP" "TIMESTAMP_IA")))
  13867. (catch 'match
  13868. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13869. (setq key (if (match-end 1)
  13870. (substring (org-match-string-no-properties 1)
  13871. 0 -1))
  13872. string (if (equal key clockstr)
  13873. (org-trim
  13874. (buffer-substring-no-properties
  13875. (match-beginning 3) (goto-char
  13876. (point-at-eol))))
  13877. (substring (org-match-string-no-properties 3)
  13878. 1 -1)))
  13879. ;; Get the correct property name from the key. This is
  13880. ;; necessary if the user has configured time keywords.
  13881. (setq key1 (concat key ":"))
  13882. (cond
  13883. ((not key)
  13884. (setq key
  13885. (if (= (char-after (match-beginning 3)) ?\[)
  13886. "TIMESTAMP_IA" "TIMESTAMP")))
  13887. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13888. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13889. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13890. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13891. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13892. (progn
  13893. (push (cons key string) props)
  13894. ;; no need to search further if match is found
  13895. (throw 'match t))
  13896. (when (or (equal key "CLOCK") (not (assoc key props)))
  13897. (push (cons key string) props)))))))
  13898. (when (memq which '(all standard))
  13899. ;; Get the standard properties, like :PROP: ...
  13900. (setq range (org-get-property-block beg end))
  13901. (when range
  13902. (goto-char (car range))
  13903. (while (re-search-forward
  13904. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13905. (cdr range) t)
  13906. (setq key (org-match-string-no-properties 1)
  13907. value (org-trim (or (org-match-string-no-properties 2) "")))
  13908. (unless (member key excluded)
  13909. (push (cons key (or value "")) props)))))
  13910. (if clocksum
  13911. (push (cons "CLOCKSUM"
  13912. (org-columns-number-to-string (/ (float clocksum) 60.)
  13913. 'add_times))
  13914. props))
  13915. (if clocksumt
  13916. (push (cons "CLOCKSUM_T"
  13917. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13918. 'add_times))
  13919. props))
  13920. (unless (assoc "CATEGORY" props)
  13921. (push (cons "CATEGORY" (org-get-category)) props))
  13922. (append sum-props (nreverse props)))))))
  13923. (defun org-entry-get (pom property &optional inherit literal-nil)
  13924. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13925. If INHERIT is non-nil and the entry does not have the property,
  13926. then also check higher levels of the hierarchy.
  13927. If INHERIT is the symbol `selective', use inheritance only if the setting
  13928. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13929. If the property is present but empty, the return value is the empty string.
  13930. If the property is not present at all, nil is returned.
  13931. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13932. do not interpret it as the list atom nil. This is used for inheritance
  13933. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13934. (org-with-point-at pom
  13935. (if (and inherit (if (eq inherit 'selective)
  13936. (org-property-inherit-p property)
  13937. t))
  13938. (org-entry-get-with-inheritance property literal-nil)
  13939. (if (member property org-special-properties)
  13940. ;; We need a special property. Use `org-entry-properties' to
  13941. ;; retrieve it, but specify the wanted property
  13942. (cdr (assoc property (org-entry-properties nil 'special property)))
  13943. (let ((range (org-get-property-block)))
  13944. (when (and range (not (eq (car range) (cdr range))))
  13945. (let* ((props (list (or (assoc property org-file-properties)
  13946. (assoc property org-global-properties)
  13947. (assoc property org-global-properties-fixed))))
  13948. (ap (lambda (key)
  13949. (when (re-search-forward
  13950. (org-re-property key) (cdr range) t)
  13951. (setq props
  13952. (org-update-property-plist
  13953. key
  13954. (if (match-end 1)
  13955. (org-match-string-no-properties 1) "")
  13956. props)))))
  13957. val)
  13958. (goto-char (car range))
  13959. (funcall ap property)
  13960. (goto-char (car range))
  13961. (while (funcall ap (concat property "+")))
  13962. (setq val (cdr (assoc property props)))
  13963. (when val (if literal-nil val (org-not-nil val))))))))))
  13964. (defun org-property-or-variable-value (var &optional inherit)
  13965. "Check if there is a property fixing the value of VAR.
  13966. If yes, return this value. If not, return the current value of the variable."
  13967. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13968. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13969. (read prop)
  13970. (symbol-value var))))
  13971. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13972. "Delete the property PROPERTY from entry at point-or-marker POM.
  13973. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13974. an empty drawer to delete."
  13975. (org-with-point-at pom
  13976. (if (member property org-special-properties)
  13977. nil ; cannot delete these properties.
  13978. (let ((range (org-get-property-block)))
  13979. (if (and range
  13980. (goto-char (car range))
  13981. (re-search-forward
  13982. (org-re-property property)
  13983. (cdr range) t))
  13984. (progn
  13985. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13986. (and delete-empty-drawer
  13987. (org-remove-empty-drawer-at
  13988. delete-empty-drawer (car range)))
  13989. t)
  13990. nil)))))
  13991. ;; Multi-values properties are properties that contain multiple values
  13992. ;; These values are assumed to be single words, separated by whitespace.
  13993. (defun org-entry-add-to-multivalued-property (pom property value)
  13994. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13995. (let* ((old (org-entry-get pom property))
  13996. (values (and old (org-split-string old "[ \t]"))))
  13997. (setq value (org-entry-protect-space value))
  13998. (unless (member value values)
  13999. (setq values (append values (list value)))
  14000. (org-entry-put pom property
  14001. (mapconcat 'identity values " ")))))
  14002. (defun org-entry-remove-from-multivalued-property (pom property value)
  14003. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14004. (let* ((old (org-entry-get pom property))
  14005. (values (and old (org-split-string old "[ \t]"))))
  14006. (setq value (org-entry-protect-space value))
  14007. (when (member value values)
  14008. (setq values (delete value values))
  14009. (org-entry-put pom property
  14010. (mapconcat 'identity values " ")))))
  14011. (defun org-entry-member-in-multivalued-property (pom property value)
  14012. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14013. (let* ((old (org-entry-get pom property))
  14014. (values (and old (org-split-string old "[ \t]"))))
  14015. (setq value (org-entry-protect-space value))
  14016. (member value values)))
  14017. (defun org-entry-get-multivalued-property (pom property)
  14018. "Return a list of values in a multivalued property."
  14019. (let* ((value (org-entry-get pom property))
  14020. (values (and value (org-split-string value "[ \t]"))))
  14021. (mapcar 'org-entry-restore-space values)))
  14022. (defun org-entry-put-multivalued-property (pom property &rest values)
  14023. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14024. VALUES should be a list of strings. Spaces will be protected."
  14025. (org-entry-put pom property
  14026. (mapconcat 'org-entry-protect-space values " "))
  14027. (let* ((value (org-entry-get pom property))
  14028. (values (and value (org-split-string value "[ \t]"))))
  14029. (mapcar 'org-entry-restore-space values)))
  14030. (defun org-entry-protect-space (s)
  14031. "Protect spaces and newline in string S."
  14032. (while (string-match " " s)
  14033. (setq s (replace-match "%20" t t s)))
  14034. (while (string-match "\n" s)
  14035. (setq s (replace-match "%0A" t t s)))
  14036. s)
  14037. (defun org-entry-restore-space (s)
  14038. "Restore spaces and newline in string S."
  14039. (while (string-match "%20" s)
  14040. (setq s (replace-match " " t t s)))
  14041. (while (string-match "%0A" s)
  14042. (setq s (replace-match "\n" t t s)))
  14043. s)
  14044. (defvar org-entry-property-inherited-from (make-marker)
  14045. "Marker pointing to the entry from where a property was inherited.
  14046. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14047. location of the entry where the inheritance search matched. If there was
  14048. no match, the marker will point nowhere.
  14049. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14050. is set.")
  14051. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14052. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14053. The search will stop at the first ancestor which has the property defined.
  14054. If the value found is \"nil\", return nil to show that the property
  14055. should be considered as undefined (this is the meaning of nil here).
  14056. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14057. (move-marker org-entry-property-inherited-from nil)
  14058. (let (tmp)
  14059. (save-excursion
  14060. (save-restriction
  14061. (widen)
  14062. (catch 'ex
  14063. (while t
  14064. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14065. (or (ignore-errors (org-back-to-heading t))
  14066. (goto-char (point-min)))
  14067. (move-marker org-entry-property-inherited-from (point))
  14068. (throw 'ex tmp))
  14069. (or (ignore-errors (org-up-heading-safe))
  14070. (throw 'ex nil))))))
  14071. (setq tmp (or tmp
  14072. (cdr (assoc property org-file-properties))
  14073. (cdr (assoc property org-global-properties))
  14074. (cdr (assoc property org-global-properties-fixed))))
  14075. (if literal-nil tmp (org-not-nil tmp))))
  14076. (defvar org-property-changed-functions nil
  14077. "Hook called when the value of a property has changed.
  14078. Each hook function should accept two arguments, the name of the property
  14079. and the new value.")
  14080. (defun org-entry-put (pom property value)
  14081. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14082. (org-with-point-at pom
  14083. (org-back-to-heading t)
  14084. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14085. range)
  14086. (cond
  14087. ((equal property "TODO")
  14088. (when (and (stringp value) (string-match "\\S-" value)
  14089. (not (member value org-todo-keywords-1)))
  14090. (error "\"%s\" is not a valid TODO state" value))
  14091. (if (or (not value)
  14092. (not (string-match "\\S-" value)))
  14093. (setq value 'none))
  14094. (org-todo value)
  14095. (org-set-tags nil 'align))
  14096. ((equal property "PRIORITY")
  14097. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14098. (string-to-char value) ?\ ))
  14099. (org-set-tags nil 'align))
  14100. ((equal property "CLOCKSUM")
  14101. (if (not (re-search-forward
  14102. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14103. (error "Cannot find a clock log")
  14104. (goto-char (- (match-end 1) 2))
  14105. (cond
  14106. ((eq value 'earlier) (org-timestamp-down))
  14107. ((eq value 'later) (org-timestamp-up)))
  14108. (org-clock-sum-current-item)))
  14109. ((equal property "SCHEDULED")
  14110. (if (re-search-forward org-scheduled-time-regexp end t)
  14111. (cond
  14112. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14113. ((eq value 'later) (org-timestamp-change 1 'day))
  14114. (t (call-interactively 'org-schedule)))
  14115. (call-interactively 'org-schedule)))
  14116. ((equal property "DEADLINE")
  14117. (if (re-search-forward org-deadline-time-regexp end t)
  14118. (cond
  14119. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14120. ((eq value 'later) (org-timestamp-change 1 'day))
  14121. (t (call-interactively 'org-deadline)))
  14122. (call-interactively 'org-deadline)))
  14123. ((member property org-special-properties)
  14124. (error "The %s property can not yet be set with `org-entry-put'"
  14125. property))
  14126. (t ; a non-special property
  14127. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14128. (setq range (org-get-property-block beg end 'force))
  14129. (goto-char (car range))
  14130. (if (re-search-forward
  14131. (org-re-property-keyword property) (cdr range) t)
  14132. (progn
  14133. (delete-region (match-beginning 0) (match-end 0))
  14134. (goto-char (match-beginning 0)))
  14135. (goto-char (cdr range))
  14136. (insert "\n")
  14137. (backward-char 1)
  14138. (org-indent-line))
  14139. (insert ":" property ":")
  14140. (and value (insert " " value))
  14141. (org-indent-line)))))
  14142. (run-hook-with-args 'org-property-changed-functions property value)))
  14143. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14144. "Get all property keys in the current buffer.
  14145. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14146. like tags and TODO state.
  14147. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14148. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14149. and others.
  14150. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14151. formats in the current buffer."
  14152. (let (rtn range cfmt s p)
  14153. (save-excursion
  14154. (save-restriction
  14155. (widen)
  14156. (goto-char (point-min))
  14157. (while (re-search-forward org-property-start-re nil t)
  14158. (setq range (org-get-property-block))
  14159. (goto-char (car range))
  14160. (while (re-search-forward
  14161. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14162. (cdr range) t)
  14163. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14164. (outline-next-heading))))
  14165. (when include-specials
  14166. (setq rtn (append org-special-properties rtn)))
  14167. (when include-defaults
  14168. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14169. (add-to-list 'rtn org-effort-property))
  14170. (when include-columns
  14171. (save-excursion
  14172. (save-restriction
  14173. (widen)
  14174. (goto-char (point-min))
  14175. (while (re-search-forward
  14176. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14177. nil t)
  14178. (setq cfmt (match-string 2) s 0)
  14179. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14180. cfmt s)
  14181. (setq s (match-end 0)
  14182. p (match-string 1 cfmt))
  14183. (unless (or (equal p "ITEM")
  14184. (member p org-special-properties))
  14185. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14186. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14187. (defun org-property-values (key)
  14188. "Return a list of all values of property KEY in the current buffer."
  14189. (save-excursion
  14190. (save-restriction
  14191. (widen)
  14192. (goto-char (point-min))
  14193. (let ((re (org-re-property key))
  14194. values)
  14195. (while (re-search-forward re nil t)
  14196. (add-to-list 'values (org-trim (match-string 1))))
  14197. (delete "" values)))))
  14198. (defun org-insert-property-drawer ()
  14199. "Insert a property drawer into the current entry."
  14200. (org-back-to-heading t)
  14201. (looking-at org-outline-regexp)
  14202. (let ((indent (if org-adapt-indentation
  14203. (- (match-end 0) (match-beginning 0))
  14204. 0))
  14205. (beg (point))
  14206. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14207. end hiddenp)
  14208. (outline-next-heading)
  14209. (setq end (point))
  14210. (goto-char beg)
  14211. (while (re-search-forward re end t))
  14212. (setq hiddenp (outline-invisible-p))
  14213. (end-of-line 1)
  14214. (and (equal (char-after) ?\n) (forward-char 1))
  14215. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14216. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14217. ;; just skip this line
  14218. (beginning-of-line 2)
  14219. ;; Drawer start, find the end
  14220. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14221. (beginning-of-line 1)))
  14222. (org-skip-over-state-notes)
  14223. (skip-chars-backward " \t\n\r")
  14224. (if (eq (char-before) ?*) (forward-char 1))
  14225. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14226. (beginning-of-line 0)
  14227. (org-indent-to-column indent)
  14228. (beginning-of-line 2)
  14229. (org-indent-to-column indent)
  14230. (beginning-of-line 0)
  14231. (if hiddenp
  14232. (save-excursion
  14233. (org-back-to-heading t)
  14234. (hide-entry))
  14235. (org-flag-drawer t))))
  14236. (defun org-insert-drawer (&optional arg drawer)
  14237. "Insert a drawer at point.
  14238. Optional argument DRAWER, when non-nil, is a string representing
  14239. drawer's name. Otherwise, the user is prompted for a name.
  14240. If a region is active, insert the drawer around that region
  14241. instead.
  14242. Point is left between drawer's boundaries."
  14243. (interactive "P")
  14244. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14245. "LOGBOOK"))
  14246. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14247. ;; internally by Org. They are meant to be inserted
  14248. ;; automatically.
  14249. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14250. ;; Remove system drawers from list. Note: For some reason,
  14251. ;; `org-completing-read' ignores the predicate while
  14252. ;; `completing-read' handles it fine.
  14253. (drawer (if arg "PROPERTIES"
  14254. (or drawer
  14255. (completing-read
  14256. "Drawer: " org-drawers
  14257. (lambda (d) (not (member d system-drawers))))))))
  14258. (cond
  14259. ;; With C-u, fall back on `org-insert-property-drawer'
  14260. (arg (org-insert-property-drawer))
  14261. ;; With an active region, insert a drawer at point.
  14262. ((not (org-region-active-p))
  14263. (progn
  14264. (unless (bolp) (insert "\n"))
  14265. (insert (format ":%s:\n\n:END:\n" drawer))
  14266. (forward-line -2)))
  14267. ;; Otherwise, insert the drawer at point
  14268. (t
  14269. (let ((rbeg (region-beginning))
  14270. (rend (copy-marker (region-end))))
  14271. (unwind-protect
  14272. (progn
  14273. (goto-char rbeg)
  14274. (beginning-of-line)
  14275. (when (save-excursion
  14276. (re-search-forward org-outline-regexp-bol rend t))
  14277. (error "Drawers cannot contain headlines"))
  14278. ;; Position point at the beginning of the first
  14279. ;; non-blank line in region. Insert drawer's opening
  14280. ;; there, then indent it.
  14281. (org-skip-whitespace)
  14282. (beginning-of-line)
  14283. (insert ":" drawer ":\n")
  14284. (forward-line -1)
  14285. (indent-for-tab-command)
  14286. ;; Move point to the beginning of the first blank line
  14287. ;; after the last non-blank line in region. Insert
  14288. ;; drawer's closing, then indent it.
  14289. (goto-char rend)
  14290. (skip-chars-backward " \r\t\n")
  14291. (insert "\n:END:")
  14292. (deactivate-mark t)
  14293. (indent-for-tab-command)
  14294. (unless (eolp) (insert "\n")))
  14295. ;; Clear marker, whatever the outcome of insertion is.
  14296. (set-marker rend nil)))))))
  14297. (defvar org-property-set-functions-alist nil
  14298. "Property set function alist.
  14299. Each entry should have the following format:
  14300. (PROPERTY . READ-FUNCTION)
  14301. The read function will be called with the same argument as
  14302. `org-completing-read'.")
  14303. (defun org-set-property-function (property)
  14304. "Get the function that should be used to set PROPERTY.
  14305. This is computed according to `org-property-set-functions-alist'."
  14306. (or (cdr (assoc property org-property-set-functions-alist))
  14307. 'org-completing-read))
  14308. (defun org-read-property-value (property)
  14309. "Read PROPERTY value from user."
  14310. (let* ((completion-ignore-case t)
  14311. (allowed (org-property-get-allowed-values nil property 'table))
  14312. (cur (org-entry-get nil property))
  14313. (prompt (concat property " value"
  14314. (if (and cur (string-match "\\S-" cur))
  14315. (concat " [" cur "]") "") ": "))
  14316. (set-function (org-set-property-function property))
  14317. (val (if allowed
  14318. (funcall set-function prompt allowed nil
  14319. (not (get-text-property 0 'org-unrestricted
  14320. (caar allowed))))
  14321. (let (org-completion-use-ido org-completion-use-iswitchb)
  14322. (funcall set-function prompt
  14323. (mapcar 'list (org-property-values property))
  14324. nil nil "" nil cur)))))
  14325. (if (equal val "")
  14326. cur
  14327. val)))
  14328. (defvar org-last-set-property nil)
  14329. (defvar org-last-set-property-value nil)
  14330. (defun org-read-property-name ()
  14331. "Read a property name."
  14332. (let* ((completion-ignore-case t)
  14333. (keys (org-buffer-property-keys nil t t))
  14334. (default-prop (or (save-excursion
  14335. (save-match-data
  14336. (beginning-of-line)
  14337. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14338. (null (string= (match-string 1) "END"))
  14339. (match-string 1))))
  14340. org-last-set-property))
  14341. (property (org-icompleting-read
  14342. (concat "Property"
  14343. (if default-prop (concat " [" default-prop "]") "")
  14344. ": ")
  14345. (mapcar 'list keys)
  14346. nil nil nil nil
  14347. default-prop)))
  14348. (if (member property keys)
  14349. property
  14350. (or (cdr (assoc (downcase property)
  14351. (mapcar (lambda (x) (cons (downcase x) x))
  14352. keys)))
  14353. property))))
  14354. (defun org-set-property-and-value (use-last)
  14355. "Allow to set [PROPERTY]: [value] direction from prompt.
  14356. When use-default, don't even ask, just use the last
  14357. \"[PROPERTY]: [value]\" string from the history."
  14358. (interactive "P")
  14359. (let* ((completion-ignore-case t)
  14360. (pv (or (and use-last org-last-set-property-value)
  14361. (org-completing-read
  14362. "Enter a \"[Property]: [value]\" pair: "
  14363. nil nil nil nil nil
  14364. org-last-set-property-value)))
  14365. prop val)
  14366. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14367. (setq prop (match-string 1 pv)
  14368. val (match-string 2 pv))
  14369. (org-set-property prop val))))
  14370. (defun org-set-property (property value)
  14371. "In the current entry, set PROPERTY to VALUE.
  14372. When called interactively, this will prompt for a property name, offering
  14373. completion on existing and default properties. And then it will prompt
  14374. for a value, offering completion either on allowed values (via an inherited
  14375. xxx_ALL property) or on existing values in other instances of this property
  14376. in the current file."
  14377. (interactive (list nil nil))
  14378. (let* ((property (or property (org-read-property-name)))
  14379. (value (or value (org-read-property-value property)))
  14380. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14381. (setq org-last-set-property property)
  14382. (setq org-last-set-property-value (concat property ": " value))
  14383. ;; Possibly postprocess the inserted value:
  14384. (when fn (setq value (funcall fn value)))
  14385. (unless (equal (org-entry-get nil property) value)
  14386. (org-entry-put nil property value))))
  14387. (defun org-delete-property (property &optional delete-empty-drawer)
  14388. "In the current entry, delete PROPERTY.
  14389. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14390. an empty drawer to delete."
  14391. (interactive
  14392. (let* ((completion-ignore-case t)
  14393. (prop (org-icompleting-read "Property: "
  14394. (org-entry-properties nil 'standard))))
  14395. (list prop)))
  14396. (message "Property %s %s" property
  14397. (if (org-entry-delete nil property delete-empty-drawer)
  14398. "deleted"
  14399. "was not present in the entry")))
  14400. (defun org-delete-property-globally (property)
  14401. "Remove PROPERTY globally, from all entries."
  14402. (interactive
  14403. (let* ((completion-ignore-case t)
  14404. (prop (org-icompleting-read
  14405. "Globally remove property: "
  14406. (mapcar 'list (org-buffer-property-keys)))))
  14407. (list prop)))
  14408. (save-excursion
  14409. (save-restriction
  14410. (widen)
  14411. (goto-char (point-min))
  14412. (let ((cnt 0))
  14413. (while (re-search-forward
  14414. (org-re-property property)
  14415. nil t)
  14416. (setq cnt (1+ cnt))
  14417. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14418. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14419. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14420. (defun org-compute-property-at-point ()
  14421. "Compute the property at point.
  14422. This looks for an enclosing column format, extracts the operator and
  14423. then applies it to the property in the column format's scope."
  14424. (interactive)
  14425. (unless (org-at-property-p)
  14426. (error "Not at a property"))
  14427. (let ((prop (org-match-string-no-properties 2)))
  14428. (org-columns-get-format-and-top-level)
  14429. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14430. (error "No operator defined for property %s" prop))
  14431. (org-columns-compute prop)))
  14432. (defvar org-property-allowed-value-functions nil
  14433. "Hook for functions supplying allowed values for a specific property.
  14434. The functions must take a single argument, the name of the property, and
  14435. return a flat list of allowed values. If \":ETC\" is one of
  14436. the values, this means that these values are intended as defaults for
  14437. completion, but that other values should be allowed too.
  14438. The functions must return nil if they are not responsible for this
  14439. property.")
  14440. (defun org-property-get-allowed-values (pom property &optional table)
  14441. "Get allowed values for the property PROPERTY.
  14442. When TABLE is non-nil, return an alist that can directly be used for
  14443. completion."
  14444. (let (vals)
  14445. (cond
  14446. ((equal property "TODO")
  14447. (setq vals (org-with-point-at pom
  14448. (append org-todo-keywords-1 '("")))))
  14449. ((equal property "PRIORITY")
  14450. (let ((n org-lowest-priority))
  14451. (while (>= n org-highest-priority)
  14452. (push (char-to-string n) vals)
  14453. (setq n (1- n)))))
  14454. ((member property org-special-properties))
  14455. ((setq vals (run-hook-with-args-until-success
  14456. 'org-property-allowed-value-functions property)))
  14457. (t
  14458. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14459. (when (and vals (string-match "\\S-" vals))
  14460. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14461. (setq vals (mapcar (lambda (x)
  14462. (cond ((stringp x) x)
  14463. ((numberp x) (number-to-string x))
  14464. ((symbolp x) (symbol-name x))
  14465. (t "???")))
  14466. vals)))))
  14467. (when (member ":ETC" vals)
  14468. (setq vals (remove ":ETC" vals))
  14469. (org-add-props (car vals) '(org-unrestricted t)))
  14470. (if table (mapcar 'list vals) vals)))
  14471. (defun org-property-previous-allowed-value (&optional previous)
  14472. "Switch to the next allowed value for this property."
  14473. (interactive)
  14474. (org-property-next-allowed-value t))
  14475. (defun org-property-next-allowed-value (&optional previous)
  14476. "Switch to the next allowed value for this property."
  14477. (interactive)
  14478. (unless (org-at-property-p)
  14479. (error "Not at a property"))
  14480. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14481. (key (match-string 2))
  14482. (value (match-string 3))
  14483. (allowed (or (org-property-get-allowed-values (point) key)
  14484. (and (member value '("[ ]" "[-]" "[X]"))
  14485. '("[ ]" "[X]"))))
  14486. (heading (save-match-data (nth 4 (org-heading-components))))
  14487. nval)
  14488. (unless allowed
  14489. (error "Allowed values for this property have not been defined"))
  14490. (if previous (setq allowed (reverse allowed)))
  14491. (if (member value allowed)
  14492. (setq nval (car (cdr (member value allowed)))))
  14493. (setq nval (or nval (car allowed)))
  14494. (if (equal nval value)
  14495. (error "Only one allowed value for this property"))
  14496. (org-at-property-p)
  14497. (replace-match (concat " :" key ": " nval) t t)
  14498. (org-indent-line)
  14499. (beginning-of-line 1)
  14500. (skip-chars-forward " \t")
  14501. (when (equal prop org-effort-property)
  14502. (save-excursion
  14503. (org-back-to-heading t)
  14504. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14505. (when (string= org-clock-current-task heading)
  14506. (setq org-clock-effort nval)
  14507. (org-clock-update-mode-line)))
  14508. (run-hook-with-args 'org-property-changed-functions key nval)))
  14509. (defun org-find-olp (path &optional this-buffer)
  14510. "Return a marker pointing to the entry at outline path OLP.
  14511. If anything goes wrong, throw an error.
  14512. You can wrap this call to catch the error like this:
  14513. (condition-case msg
  14514. (org-mobile-locate-entry (match-string 4))
  14515. (error (nth 1 msg)))
  14516. The return value will then be either a string with the error message,
  14517. or a marker if everything is OK.
  14518. If THIS-BUFFER is set, the outline path does not contain a file,
  14519. only headings."
  14520. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14521. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14522. (level 1)
  14523. (lmin 1)
  14524. (lmax 1)
  14525. limit re end found pos heading cnt flevel)
  14526. (unless buffer (error "File not found :%s" file))
  14527. (with-current-buffer buffer
  14528. (save-excursion
  14529. (save-restriction
  14530. (widen)
  14531. (setq limit (point-max))
  14532. (goto-char (point-min))
  14533. (while (setq heading (pop path))
  14534. (setq re (format org-complex-heading-regexp-format
  14535. (regexp-quote heading)))
  14536. (setq cnt 0 pos (point))
  14537. (while (re-search-forward re end t)
  14538. (setq level (- (match-end 1) (match-beginning 1)))
  14539. (if (and (>= level lmin) (<= level lmax))
  14540. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14541. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14542. lmax heading))
  14543. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14544. lmax heading))
  14545. (goto-char found)
  14546. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14547. (setq end (save-excursion (org-end-of-subtree t t))))
  14548. (when (org-at-heading-p)
  14549. (point-marker)))))))
  14550. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14551. "Find node HEADING in BUFFER.
  14552. Return a marker to the heading if it was found, or nil if not.
  14553. If POS-ONLY is set, return just the position instead of a marker.
  14554. The heading text must match exact, but it may have a TODO keyword,
  14555. a priority cookie and tags in the standard locations."
  14556. (with-current-buffer (or buffer (current-buffer))
  14557. (save-excursion
  14558. (save-restriction
  14559. (widen)
  14560. (goto-char (point-min))
  14561. (let (case-fold-search)
  14562. (if (re-search-forward
  14563. (format org-complex-heading-regexp-format
  14564. (regexp-quote heading)) nil t)
  14565. (if pos-only
  14566. (match-beginning 0)
  14567. (move-marker (make-marker) (match-beginning 0)))))))))
  14568. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14569. "Find Org node headline HEADING in all .org files in directory DIR.
  14570. When the target headline is found, return a marker to this location."
  14571. (let ((files (directory-files (or dir default-directory)
  14572. nil "\\`[^.#].*\\.org\\'"))
  14573. file visiting m buffer)
  14574. (catch 'found
  14575. (while (setq file (pop files))
  14576. (message "trying %s" file)
  14577. (setq visiting (org-find-base-buffer-visiting file))
  14578. (setq buffer (or visiting (find-file-noselect file)))
  14579. (setq m (org-find-exact-headline-in-buffer
  14580. heading buffer))
  14581. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14582. (and m (throw 'found m))))))
  14583. (defun org-find-entry-with-id (ident)
  14584. "Locate the entry that contains the ID property with exact value IDENT.
  14585. IDENT can be a string, a symbol or a number, this function will search for
  14586. the string representation of it.
  14587. Return the position where this entry starts, or nil if there is no such entry."
  14588. (interactive "sID: ")
  14589. (let ((id (cond
  14590. ((stringp ident) ident)
  14591. ((symbol-name ident) (symbol-name ident))
  14592. ((numberp ident) (number-to-string ident))
  14593. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14594. (case-fold-search nil))
  14595. (save-excursion
  14596. (save-restriction
  14597. (widen)
  14598. (goto-char (point-min))
  14599. (when (re-search-forward
  14600. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14601. nil t)
  14602. (org-back-to-heading t)
  14603. (point))))))
  14604. ;;;; Timestamps
  14605. (defvar org-last-changed-timestamp nil)
  14606. (defvar org-last-inserted-timestamp nil
  14607. "The last time stamp inserted with `org-insert-time-stamp'.")
  14608. (defvar org-time-was-given) ; dynamically scoped parameter
  14609. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14610. (defvar org-ts-what) ; dynamically scoped parameter
  14611. (defun org-time-stamp (arg &optional inactive)
  14612. "Prompt for a date/time and insert a time stamp.
  14613. If the user specifies a time like HH:MM or if this command is
  14614. called with at least one prefix argument, the time stamp contains
  14615. the date and the time. Otherwise, only the date is be included.
  14616. All parts of a date not specified by the user is filled in from
  14617. the current date/time. So if you just press return without
  14618. typing anything, the time stamp will represent the current
  14619. date/time.
  14620. If there is already a timestamp at the cursor, it will be
  14621. modified.
  14622. With two universal prefix arguments, insert an active timestamp
  14623. with the current time without prompting the user."
  14624. (interactive "P")
  14625. (let* ((ts nil)
  14626. (default-time
  14627. ;; Default time is either today, or, when entering a range,
  14628. ;; the range start.
  14629. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14630. (save-excursion
  14631. (re-search-backward
  14632. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14633. (- (point) 20) t)))
  14634. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14635. (current-time)))
  14636. (default-input (and ts (org-get-compact-tod ts)))
  14637. (repeater (save-excursion
  14638. (save-match-data
  14639. (beginning-of-line)
  14640. (when (re-search-forward
  14641. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14642. (save-excursion (progn (end-of-line) (point))) t)
  14643. (match-string 0)))))
  14644. org-time-was-given org-end-time-was-given time)
  14645. (cond
  14646. ((and (org-at-timestamp-p t)
  14647. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14648. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14649. (insert "--")
  14650. (setq time (let ((this-command this-command))
  14651. (org-read-date arg 'totime nil nil
  14652. default-time default-input inactive)))
  14653. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14654. ((org-at-timestamp-p t)
  14655. (setq time (let ((this-command this-command))
  14656. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14657. (when (org-at-timestamp-p t) ; just to get the match data
  14658. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14659. (replace-match "")
  14660. (setq org-last-changed-timestamp
  14661. (org-insert-time-stamp
  14662. time (or org-time-was-given arg)
  14663. inactive nil nil (list org-end-time-was-given)))
  14664. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14665. (setq org-last-changed-timestamp
  14666. (concat (substring org-last-inserted-timestamp 0 -1)
  14667. " " repeater ">"))))
  14668. (message "Timestamp updated"))
  14669. ((equal arg '(16))
  14670. (org-insert-time-stamp (current-time) t))
  14671. (t
  14672. (setq time (let ((this-command this-command))
  14673. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14674. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14675. nil nil (list org-end-time-was-given))))))
  14676. ;; FIXME: can we use this for something else, like computing time differences?
  14677. (defun org-get-compact-tod (s)
  14678. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14679. (let* ((t1 (match-string 1 s))
  14680. (h1 (string-to-number (match-string 2 s)))
  14681. (m1 (string-to-number (match-string 3 s)))
  14682. (t2 (and (match-end 4) (match-string 5 s)))
  14683. (h2 (and t2 (string-to-number (match-string 6 s))))
  14684. (m2 (and t2 (string-to-number (match-string 7 s))))
  14685. dh dm)
  14686. (if (not t2)
  14687. t1
  14688. (setq dh (- h2 h1) dm (- m2 m1))
  14689. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14690. (concat t1 "+" (number-to-string dh)
  14691. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14692. (defun org-time-stamp-inactive (&optional arg)
  14693. "Insert an inactive time stamp.
  14694. An inactive time stamp is enclosed in square brackets instead of angle
  14695. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14696. does not link to the calendar and cannot be changed with the S-cursor keys.
  14697. So these are more for recording a certain time/date."
  14698. (interactive "P")
  14699. (org-time-stamp arg 'inactive))
  14700. (defvar org-date-ovl (make-overlay 1 1))
  14701. (overlay-put org-date-ovl 'face 'org-date-selected)
  14702. (org-detach-overlay org-date-ovl)
  14703. (defvar org-ans1) ; dynamically scoped parameter
  14704. (defvar org-ans2) ; dynamically scoped parameter
  14705. (defvar org-plain-time-of-day-regexp) ; defined below
  14706. (defvar org-overriding-default-time nil) ; dynamically scoped
  14707. (defvar org-read-date-overlay nil)
  14708. (defvar org-dcst nil) ; dynamically scoped
  14709. (defvar org-read-date-history nil)
  14710. (defvar org-read-date-final-answer nil)
  14711. (defvar org-read-date-analyze-futurep nil)
  14712. (defvar org-read-date-analyze-forced-year nil)
  14713. (defvar org-read-date-inactive)
  14714. (defvar org-read-date-minibuffer-local-map
  14715. (let ((map (make-sparse-keymap)))
  14716. (set-keymap-parent map minibuffer-local-map)
  14717. (org-defkey map (kbd ".")
  14718. (lambda () (interactive)
  14719. (org-eval-in-calendar '(calendar-goto-today))))
  14720. (org-defkey map [(meta shift left)]
  14721. (lambda () (interactive)
  14722. (org-eval-in-calendar '(calendar-backward-month 1))))
  14723. (org-defkey map [(meta shift right)]
  14724. (lambda () (interactive)
  14725. (org-eval-in-calendar '(calendar-forward-month 1))))
  14726. (org-defkey map [(meta shift up)]
  14727. (lambda () (interactive)
  14728. (org-eval-in-calendar '(calendar-backward-year 1))))
  14729. (org-defkey map [(meta shift down)]
  14730. (lambda () (interactive)
  14731. (org-eval-in-calendar '(calendar-forward-year 1))))
  14732. (org-defkey map [?\e (shift left)]
  14733. (lambda () (interactive)
  14734. (org-eval-in-calendar '(calendar-backward-month 1))))
  14735. (org-defkey map [?\e (shift right)]
  14736. (lambda () (interactive)
  14737. (org-eval-in-calendar '(calendar-forward-month 1))))
  14738. (org-defkey map [?\e (shift up)]
  14739. (lambda () (interactive)
  14740. (org-eval-in-calendar '(calendar-backward-year 1))))
  14741. (org-defkey map [?\e (shift down)]
  14742. (lambda () (interactive)
  14743. (org-eval-in-calendar '(calendar-forward-year 1))))
  14744. (org-defkey map [(shift up)]
  14745. (lambda () (interactive)
  14746. (org-eval-in-calendar '(calendar-backward-week 1))))
  14747. (org-defkey map [(shift down)]
  14748. (lambda () (interactive)
  14749. (org-eval-in-calendar '(calendar-forward-week 1))))
  14750. (org-defkey map [(shift left)]
  14751. (lambda () (interactive)
  14752. (org-eval-in-calendar '(calendar-backward-day 1))))
  14753. (org-defkey map [(shift right)]
  14754. (lambda () (interactive)
  14755. (org-eval-in-calendar '(calendar-forward-day 1))))
  14756. (org-defkey map "!"
  14757. (lambda () (interactive)
  14758. (org-eval-in-calendar '(diary-view-entries))
  14759. (message "")))
  14760. (org-defkey map ">"
  14761. (lambda () (interactive)
  14762. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14763. (org-defkey map "<"
  14764. (lambda () (interactive)
  14765. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14766. (org-defkey map "\C-v"
  14767. (lambda () (interactive)
  14768. (org-eval-in-calendar
  14769. '(calendar-scroll-left-three-months 1))))
  14770. (org-defkey map "\M-v"
  14771. (lambda () (interactive)
  14772. (org-eval-in-calendar
  14773. '(calendar-scroll-right-three-months 1))))
  14774. map)
  14775. "Keymap for minibuffer commands when using `org-read-date'.")
  14776. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14777. default-time default-input inactive)
  14778. "Read a date, possibly a time, and make things smooth for the user.
  14779. The prompt will suggest to enter an ISO date, but you can also enter anything
  14780. which will at least partially be understood by `parse-time-string'.
  14781. Unrecognized parts of the date will default to the current day, month, year,
  14782. hour and minute. If this command is called to replace a timestamp at point,
  14783. or to enter the second timestamp of a range, the default time is taken
  14784. from the existing stamp. Furthermore, the command prefers the future,
  14785. so if you are giving a date where the year is not given, and the day-month
  14786. combination is already past in the current year, it will assume you
  14787. mean next year. For details, see the manual. A few examples:
  14788. 3-2-5 --> 2003-02-05
  14789. feb 15 --> currentyear-02-15
  14790. 2/15 --> currentyear-02-15
  14791. sep 12 9 --> 2009-09-12
  14792. 12:45 --> today 12:45
  14793. 22 sept 0:34 --> currentyear-09-22 0:34
  14794. 12 --> currentyear-currentmonth-12
  14795. Fri --> nearest Friday (today or later)
  14796. etc.
  14797. Furthermore you can specify a relative date by giving, as the *first* thing
  14798. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14799. change in days weeks, months, years.
  14800. With a single plus or minus, the date is relative to today. With a double
  14801. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14802. +4d --> four days from today
  14803. +4 --> same as above
  14804. +2w --> two weeks from today
  14805. ++5 --> five days from default date
  14806. The function understands only English month and weekday abbreviations.
  14807. While prompting, a calendar is popped up - you can also select the
  14808. date with the mouse (button 1). The calendar shows a period of three
  14809. months. To scroll it to other months, use the keys `>' and `<'.
  14810. If you don't like the calendar, turn it off with
  14811. \(setq org-read-date-popup-calendar nil)
  14812. With optional argument TO-TIME, the date will immediately be converted
  14813. to an internal time.
  14814. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14815. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14816. still enter a time, and this function will inform the calling routine
  14817. about this change. The calling routine may then choose to change the
  14818. format used to insert the time stamp into the buffer to include the time.
  14819. With optional argument FROM-STRING, read from this string instead from
  14820. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14821. the time/date that is used for everything that is not specified by the
  14822. user."
  14823. (require 'parse-time)
  14824. (let* ((org-time-stamp-rounding-minutes
  14825. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14826. (org-dcst org-display-custom-times)
  14827. (ct (org-current-time))
  14828. (org-def (or org-overriding-default-time default-time ct))
  14829. (org-defdecode (decode-time org-def))
  14830. (dummy (progn
  14831. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14832. (setcar (nthcdr 2 org-defdecode) -1)
  14833. (setcar (nthcdr 1 org-defdecode) 59)
  14834. (setq org-def (apply 'encode-time org-defdecode)
  14835. org-defdecode (decode-time org-def)))))
  14836. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14837. (calendar-frame-setup nil)
  14838. (calendar-setup nil)
  14839. (calendar-move-hook nil)
  14840. (calendar-view-diary-initially-flag nil)
  14841. (calendar-view-holidays-initially-flag nil)
  14842. (timestr (format-time-string
  14843. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14844. (prompt (concat (if prompt (concat prompt " ") "")
  14845. (format "Date+time [%s]: " timestr)))
  14846. ans (org-ans0 "") org-ans1 org-ans2 final)
  14847. (cond
  14848. (from-string (setq ans from-string))
  14849. (org-read-date-popup-calendar
  14850. (save-excursion
  14851. (save-window-excursion
  14852. (calendar)
  14853. (org-eval-in-calendar '(setq cursor-type nil) t)
  14854. (unwind-protect
  14855. (progn
  14856. (calendar-forward-day (- (time-to-days org-def)
  14857. (calendar-absolute-from-gregorian
  14858. (calendar-current-date))))
  14859. (org-eval-in-calendar nil t)
  14860. (let* ((old-map (current-local-map))
  14861. (map (copy-keymap calendar-mode-map))
  14862. (minibuffer-local-map
  14863. (copy-keymap org-read-date-minibuffer-local-map)))
  14864. (org-defkey map (kbd "RET") 'org-calendar-select)
  14865. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14866. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14867. (unwind-protect
  14868. (progn
  14869. (use-local-map map)
  14870. (setq org-read-date-inactive inactive)
  14871. (add-hook 'post-command-hook 'org-read-date-display)
  14872. (setq org-ans0 (read-string prompt default-input
  14873. 'org-read-date-history nil))
  14874. ;; org-ans0: from prompt
  14875. ;; org-ans1: from mouse click
  14876. ;; org-ans2: from calendar motion
  14877. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14878. (remove-hook 'post-command-hook 'org-read-date-display)
  14879. (use-local-map old-map)
  14880. (when org-read-date-overlay
  14881. (delete-overlay org-read-date-overlay)
  14882. (setq org-read-date-overlay nil)))))
  14883. (bury-buffer "*Calendar*")))))
  14884. (t ; Naked prompt only
  14885. (unwind-protect
  14886. (setq ans (read-string prompt default-input
  14887. 'org-read-date-history timestr))
  14888. (when org-read-date-overlay
  14889. (delete-overlay org-read-date-overlay)
  14890. (setq org-read-date-overlay nil)))))
  14891. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14892. (when org-read-date-analyze-forced-year
  14893. (message "Year was forced into %s"
  14894. (if org-read-date-force-compatible-dates
  14895. "compatible range (1970-2037)"
  14896. "range representable on this machine"))
  14897. (ding))
  14898. ;; One round trip to get rid of 34th of August and stuff like that....
  14899. (setq final (decode-time (apply 'encode-time final)))
  14900. (setq org-read-date-final-answer ans)
  14901. (if to-time
  14902. (apply 'encode-time final)
  14903. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14904. (format "%04d-%02d-%02d %02d:%02d"
  14905. (nth 5 final) (nth 4 final) (nth 3 final)
  14906. (nth 2 final) (nth 1 final))
  14907. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14908. (defvar org-def)
  14909. (defvar org-defdecode)
  14910. (defvar org-with-time)
  14911. (defun org-read-date-display ()
  14912. "Display the current date prompt interpretation in the minibuffer."
  14913. (when org-read-date-display-live
  14914. (when org-read-date-overlay
  14915. (delete-overlay org-read-date-overlay))
  14916. (when (minibufferp (current-buffer))
  14917. (save-excursion
  14918. (end-of-line 1)
  14919. (while (not (equal (buffer-substring
  14920. (max (point-min) (- (point) 4)) (point))
  14921. " "))
  14922. (insert " ")))
  14923. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14924. " " (or org-ans1 org-ans2)))
  14925. (org-end-time-was-given nil)
  14926. (f (org-read-date-analyze ans org-def org-defdecode))
  14927. (fmts (if org-dcst
  14928. org-time-stamp-custom-formats
  14929. org-time-stamp-formats))
  14930. (fmt (if (or org-with-time
  14931. (and (boundp 'org-time-was-given) org-time-was-given))
  14932. (cdr fmts)
  14933. (car fmts)))
  14934. (txt (format-time-string fmt (apply 'encode-time f)))
  14935. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14936. (txt (concat "=> " txt)))
  14937. (when (and org-end-time-was-given
  14938. (string-match org-plain-time-of-day-regexp txt))
  14939. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14940. org-end-time-was-given
  14941. (substring txt (match-end 0)))))
  14942. (when org-read-date-analyze-futurep
  14943. (setq txt (concat txt " (=>F)")))
  14944. (setq org-read-date-overlay
  14945. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14946. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14947. (defun org-read-date-analyze (ans org-def org-defdecode)
  14948. "Analyze the combined answer of the date prompt."
  14949. ;; FIXME: cleanup and comment
  14950. (let ((nowdecode (decode-time (current-time)))
  14951. delta deltan deltaw deltadef year month day
  14952. hour minute second wday pm h2 m2 tl wday1
  14953. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14954. (setq org-read-date-analyze-futurep nil
  14955. org-read-date-analyze-forced-year nil)
  14956. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14957. (setq ans "+0"))
  14958. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14959. (setq ans (replace-match "" t t ans)
  14960. deltan (car delta)
  14961. deltaw (nth 1 delta)
  14962. deltadef (nth 2 delta)))
  14963. ;; Check if there is an iso week date in there. If yes, store the
  14964. ;; info and postpone interpreting it until the rest of the parsing
  14965. ;; is done.
  14966. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14967. (setq iso-year (if (match-end 1)
  14968. (org-small-year-to-year
  14969. (string-to-number (match-string 1 ans))))
  14970. iso-weekday (if (match-end 3)
  14971. (string-to-number (match-string 3 ans)))
  14972. iso-week (string-to-number (match-string 2 ans)))
  14973. (setq ans (replace-match "" t t ans)))
  14974. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14975. (when (string-match
  14976. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14977. (setq year (if (match-end 2)
  14978. (string-to-number (match-string 2 ans))
  14979. (progn (setq kill-year t)
  14980. (string-to-number (format-time-string "%Y"))))
  14981. month (string-to-number (match-string 3 ans))
  14982. day (string-to-number (match-string 4 ans)))
  14983. (if (< year 100) (setq year (+ 2000 year)))
  14984. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14985. t nil ans)))
  14986. ;; Help matching dotted european dates
  14987. (when (string-match
  14988. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14989. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14990. (setq kill-year t)
  14991. (string-to-number (format-time-string "%Y")))
  14992. day (string-to-number (match-string 1 ans))
  14993. month (string-to-number (match-string 2 ans))
  14994. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14995. t nil ans)))
  14996. ;; Help matching american dates, like 5/30 or 5/30/7
  14997. (when (string-match
  14998. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14999. (setq year (if (match-end 4)
  15000. (string-to-number (match-string 4 ans))
  15001. (progn (setq kill-year t)
  15002. (string-to-number (format-time-string "%Y"))))
  15003. month (string-to-number (match-string 1 ans))
  15004. day (string-to-number (match-string 2 ans)))
  15005. (if (< year 100) (setq year (+ 2000 year)))
  15006. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15007. t nil ans)))
  15008. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15009. ;; If there is a time with am/pm, and *no* time without it, we convert
  15010. ;; so that matching will be successful.
  15011. (loop for i from 1 to 2 do ; twice, for end time as well
  15012. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15013. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15014. (setq hour (string-to-number (match-string 1 ans))
  15015. minute (if (match-end 3)
  15016. (string-to-number (match-string 3 ans))
  15017. 0)
  15018. pm (equal ?p
  15019. (string-to-char (downcase (match-string 4 ans)))))
  15020. (if (and (= hour 12) (not pm))
  15021. (setq hour 0)
  15022. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15023. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15024. t t ans))))
  15025. ;; Check if a time range is given as a duration
  15026. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15027. (setq hour (string-to-number (match-string 1 ans))
  15028. h2 (+ hour (string-to-number (match-string 3 ans)))
  15029. minute (string-to-number (match-string 2 ans))
  15030. m2 (+ minute (if (match-end 5) (string-to-number
  15031. (match-string 5 ans))0)))
  15032. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15033. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15034. t t ans)))
  15035. ;; Check if there is a time range
  15036. (when (boundp 'org-end-time-was-given)
  15037. (setq org-time-was-given nil)
  15038. (when (and (string-match org-plain-time-of-day-regexp ans)
  15039. (match-end 8))
  15040. (setq org-end-time-was-given (match-string 8 ans))
  15041. (setq ans (concat (substring ans 0 (match-beginning 7))
  15042. (substring ans (match-end 7))))))
  15043. (setq tl (parse-time-string ans)
  15044. day (or (nth 3 tl) (nth 3 org-defdecode))
  15045. month (or (nth 4 tl)
  15046. (if (and org-read-date-prefer-future
  15047. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15048. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15049. (nth 4 org-defdecode)))
  15050. year (or (and (not kill-year) (nth 5 tl))
  15051. (if (and org-read-date-prefer-future
  15052. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15053. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15054. (nth 5 org-defdecode)))
  15055. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15056. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15057. second (or (nth 0 tl) 0)
  15058. wday (nth 6 tl))
  15059. (when (and (eq org-read-date-prefer-future 'time)
  15060. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15061. (equal day (nth 3 nowdecode))
  15062. (equal month (nth 4 nowdecode))
  15063. (equal year (nth 5 nowdecode))
  15064. (nth 2 tl)
  15065. (or (< (nth 2 tl) (nth 2 nowdecode))
  15066. (and (= (nth 2 tl) (nth 2 nowdecode))
  15067. (nth 1 tl)
  15068. (< (nth 1 tl) (nth 1 nowdecode)))))
  15069. (setq day (1+ day)
  15070. futurep t))
  15071. ;; Special date definitions below
  15072. (cond
  15073. (iso-week
  15074. ;; There was an iso week
  15075. (require 'cal-iso)
  15076. (setq futurep nil)
  15077. (setq year (or iso-year year)
  15078. day (or iso-weekday wday 1)
  15079. wday nil ; to make sure that the trigger below does not match
  15080. iso-date (calendar-gregorian-from-absolute
  15081. (calendar-absolute-from-iso
  15082. (list iso-week day year))))
  15083. ; FIXME: Should we also push ISO weeks into the future?
  15084. ; (when (and org-read-date-prefer-future
  15085. ; (not iso-year)
  15086. ; (< (calendar-absolute-from-gregorian iso-date)
  15087. ; (time-to-days (current-time))))
  15088. ; (setq year (1+ year)
  15089. ; iso-date (calendar-gregorian-from-absolute
  15090. ; (calendar-absolute-from-iso
  15091. ; (list iso-week day year)))))
  15092. (setq month (car iso-date)
  15093. year (nth 2 iso-date)
  15094. day (nth 1 iso-date)))
  15095. (deltan
  15096. (setq futurep nil)
  15097. (unless deltadef
  15098. (let ((now (decode-time (current-time))))
  15099. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15100. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15101. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15102. ((equal deltaw "m") (setq month (+ month deltan)))
  15103. ((equal deltaw "y") (setq year (+ year deltan)))))
  15104. ((and wday (not (nth 3 tl)))
  15105. ;; Weekday was given, but no day, so pick that day in the week
  15106. ;; on or after the derived date.
  15107. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15108. (unless (equal wday wday1)
  15109. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15110. (if (and (boundp 'org-time-was-given)
  15111. (nth 2 tl))
  15112. (setq org-time-was-given t))
  15113. (if (< year 100) (setq year (+ 2000 year)))
  15114. ;; Check of the date is representable
  15115. (if org-read-date-force-compatible-dates
  15116. (progn
  15117. (if (< year 1970)
  15118. (setq year 1970 org-read-date-analyze-forced-year t))
  15119. (if (> year 2037)
  15120. (setq year 2037 org-read-date-analyze-forced-year t)))
  15121. (condition-case nil
  15122. (ignore (encode-time second minute hour day month year))
  15123. (error
  15124. (setq year (nth 5 org-defdecode))
  15125. (setq org-read-date-analyze-forced-year t))))
  15126. (setq org-read-date-analyze-futurep futurep)
  15127. (list second minute hour day month year)))
  15128. (defvar parse-time-weekdays)
  15129. (defun org-read-date-get-relative (s today default)
  15130. "Check string S for special relative date string.
  15131. TODAY and DEFAULT are internal times, for today and for a default.
  15132. Return shift list (N what def-flag)
  15133. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15134. N is the number of WHATs to shift.
  15135. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15136. the DEFAULT date rather than TODAY."
  15137. (require 'parse-time)
  15138. (when (and
  15139. (string-match
  15140. (concat
  15141. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15142. "\\([0-9]+\\)?"
  15143. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15144. "\\([ \t]\\|$\\)") s)
  15145. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15146. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15147. (string-to-char (substring (match-string 1 s) -1))
  15148. ?+))
  15149. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15150. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15151. (what (if (match-end 3) (match-string 3 s) "d"))
  15152. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15153. (date (if rel default today))
  15154. (wday (nth 6 (decode-time date)))
  15155. delta)
  15156. (if wday1
  15157. (progn
  15158. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15159. (if (= dir ?-) (setq delta (- delta 7)))
  15160. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15161. (list delta "d" rel))
  15162. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15163. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15164. "Turn a user-specified date into the internal representation.
  15165. The internal representation needed by the calendar is (month day year).
  15166. This is a wrapper to handle the brain-dead convention in calendar that
  15167. user function argument order change dependent on argument order."
  15168. (if (boundp 'calendar-date-style)
  15169. (cond
  15170. ((eq calendar-date-style 'american)
  15171. (list arg1 arg2 arg3))
  15172. ((eq calendar-date-style 'european)
  15173. (list arg2 arg1 arg3))
  15174. ((eq calendar-date-style 'iso)
  15175. (list arg2 arg3 arg1)))
  15176. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15177. (if (org-bound-and-true-p european-calendar-style)
  15178. (list arg2 arg1 arg3)
  15179. (list arg1 arg2 arg3)))))
  15180. (defun org-eval-in-calendar (form &optional keepdate)
  15181. "Eval FORM in the calendar window and return to current window.
  15182. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15183. otherwise stick to the current value of `org-ans2'."
  15184. (let ((sf (selected-frame))
  15185. (sw (selected-window)))
  15186. (select-window (get-buffer-window "*Calendar*" t))
  15187. (eval form)
  15188. (when (and (not keepdate) (calendar-cursor-to-date))
  15189. (let* ((date (calendar-cursor-to-date))
  15190. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15191. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15192. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15193. (select-window sw)
  15194. (org-select-frame-set-input-focus sf)))
  15195. (defun org-calendar-select ()
  15196. "Return to `org-read-date' with the date currently selected.
  15197. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15198. (interactive)
  15199. (when (calendar-cursor-to-date)
  15200. (let* ((date (calendar-cursor-to-date))
  15201. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15202. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15203. (if (active-minibuffer-window) (exit-minibuffer))))
  15204. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15205. "Insert a date stamp for the date given by the internal TIME.
  15206. WITH-HM means use the stamp format that includes the time of the day.
  15207. INACTIVE means use square brackets instead of angular ones, so that the
  15208. stamp will not contribute to the agenda.
  15209. PRE and POST are optional strings to be inserted before and after the
  15210. stamp.
  15211. The command returns the inserted time stamp."
  15212. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15213. stamp)
  15214. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15215. (insert-before-markers (or pre ""))
  15216. (when (listp extra)
  15217. (setq extra (car extra))
  15218. (if (and (stringp extra)
  15219. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15220. (setq extra (format "-%02d:%02d"
  15221. (string-to-number (match-string 1 extra))
  15222. (string-to-number (match-string 2 extra))))
  15223. (setq extra nil)))
  15224. (when extra
  15225. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15226. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15227. (insert-before-markers (or post ""))
  15228. (setq org-last-inserted-timestamp stamp)))
  15229. (defun org-toggle-time-stamp-overlays ()
  15230. "Toggle the use of custom time stamp formats."
  15231. (interactive)
  15232. (setq org-display-custom-times (not org-display-custom-times))
  15233. (unless org-display-custom-times
  15234. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15235. (while (setq p (next-single-property-change p 'display))
  15236. (if (and (get-text-property p 'display)
  15237. (eq (get-text-property p 'face) 'org-date))
  15238. (remove-text-properties
  15239. p (setq p (next-single-property-change p 'display))
  15240. '(display t))))
  15241. (set-buffer-modified-p bmp)))
  15242. (if (featurep 'xemacs)
  15243. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15244. (org-restart-font-lock)
  15245. (setq org-table-may-need-update t)
  15246. (if org-display-custom-times
  15247. (message "Time stamps are overlaid with custom format")
  15248. (message "Time stamp overlays removed")))
  15249. (defun org-display-custom-time (beg end)
  15250. "Overlay modified time stamp format over timestamp between BEG and END."
  15251. (let* ((ts (buffer-substring beg end))
  15252. t1 w1 with-hm tf time str w2 (off 0))
  15253. (save-match-data
  15254. (setq t1 (org-parse-time-string ts t))
  15255. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15256. (setq off (- (match-end 0) (match-beginning 0)))))
  15257. (setq end (- end off))
  15258. (setq w1 (- end beg)
  15259. with-hm (and (nth 1 t1) (nth 2 t1))
  15260. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15261. time (org-fix-decoded-time t1)
  15262. str (org-add-props
  15263. (format-time-string
  15264. (substring tf 1 -1) (apply 'encode-time time))
  15265. nil 'mouse-face 'highlight)
  15266. w2 (length str))
  15267. (if (not (= w2 w1))
  15268. (add-text-properties (1+ beg) (+ 2 beg)
  15269. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15270. (if (featurep 'xemacs)
  15271. (progn
  15272. (put-text-property beg end 'invisible t)
  15273. (put-text-property beg end 'end-glyph (make-glyph str)))
  15274. (put-text-property beg end 'display str))))
  15275. (defun org-translate-time (string)
  15276. "Translate all timestamps in STRING to custom format.
  15277. But do this only if the variable `org-display-custom-times' is set."
  15278. (when org-display-custom-times
  15279. (save-match-data
  15280. (let* ((start 0)
  15281. (re org-ts-regexp-both)
  15282. t1 with-hm inactive tf time str beg end)
  15283. (while (setq start (string-match re string start))
  15284. (setq beg (match-beginning 0)
  15285. end (match-end 0)
  15286. t1 (save-match-data
  15287. (org-parse-time-string (substring string beg end) t))
  15288. with-hm (and (nth 1 t1) (nth 2 t1))
  15289. inactive (equal (substring string beg (1+ beg)) "[")
  15290. tf (funcall (if with-hm 'cdr 'car)
  15291. org-time-stamp-custom-formats)
  15292. time (org-fix-decoded-time t1)
  15293. str (format-time-string
  15294. (concat
  15295. (if inactive "[" "<") (substring tf 1 -1)
  15296. (if inactive "]" ">"))
  15297. (apply 'encode-time time))
  15298. string (replace-match str t t string)
  15299. start (+ start (length str)))))))
  15300. string)
  15301. (defun org-fix-decoded-time (time)
  15302. "Set 0 instead of nil for the first 6 elements of time.
  15303. Don't touch the rest."
  15304. (let ((n 0))
  15305. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15306. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15307. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15308. "Difference between TIMESTAMP-STRING and now in days.
  15309. If SECONDS is non-nil, return the difference in seconds."
  15310. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15311. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15312. (funcall fdiff (current-time)))))
  15313. (defun org-deadline-close (timestamp-string &optional ndays)
  15314. "Is the time in TIMESTAMP-STRING close to the current date?"
  15315. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15316. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15317. (not (org-entry-is-done-p))))
  15318. (defun org-get-wdays (ts &optional delay zero-delay)
  15319. "Get the deadline lead time appropriate for timestring TS.
  15320. When DELAY is non-nil, get the delay time for scheduled items
  15321. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15322. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15323. don't try to find the delay cookie in the scheduled timestamp."
  15324. (let ((tv (if delay org-scheduled-delay-days
  15325. org-deadline-warning-days)))
  15326. (cond
  15327. ((or (and delay (< tv 0))
  15328. (and delay zero-delay (<= tv 0))
  15329. (and (not delay) (<= tv 0)))
  15330. ;; Enforce this value no matter what
  15331. (- tv))
  15332. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15333. ;; lead time is specified.
  15334. (floor (* (string-to-number (match-string 1 ts))
  15335. (cdr (assoc (match-string 2 ts)
  15336. '(("d" . 1) ("w" . 7)
  15337. ("m" . 30.4) ("y" . 365.25)
  15338. ("h" . 0.041667)))))))
  15339. ;; go for the default.
  15340. (t tv))))
  15341. (defun org-calendar-select-mouse (ev)
  15342. "Return to `org-read-date' with the date currently selected.
  15343. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15344. (interactive "e")
  15345. (mouse-set-point ev)
  15346. (when (calendar-cursor-to-date)
  15347. (let* ((date (calendar-cursor-to-date))
  15348. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15349. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15350. (if (active-minibuffer-window) (exit-minibuffer))))
  15351. (defun org-check-deadlines (ndays)
  15352. "Check if there are any deadlines due or past due.
  15353. A deadline is considered due if it happens within `org-deadline-warning-days'
  15354. days from today's date. If the deadline appears in an entry marked DONE,
  15355. it is not shown. The prefix arg NDAYS can be used to test that many
  15356. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15357. (interactive "P")
  15358. (let* ((org-warn-days
  15359. (cond
  15360. ((equal ndays '(4)) 100000)
  15361. (ndays (prefix-numeric-value ndays))
  15362. (t (abs org-deadline-warning-days))))
  15363. (case-fold-search nil)
  15364. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15365. (callback
  15366. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15367. (message "%d deadlines past-due or due within %d days"
  15368. (org-occur regexp nil callback)
  15369. org-warn-days)))
  15370. (defsubst org-re-timestamp (type)
  15371. "Return a regexp for timestamp TYPE.
  15372. Allowed values for TYPE are:
  15373. all: all timestamps
  15374. active: only active timestamps (<...>)
  15375. inactive: only inactive timestamps ([...])
  15376. scheduled: only scheduled timestamps
  15377. deadline: only deadline timestamps
  15378. closed: only closed time-stamps
  15379. When TYPE is nil, fall back on returning a regexp that matches
  15380. both scheduled and deadline timestamps."
  15381. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15382. ((eq type 'active) org-ts-regexp)
  15383. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15384. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15385. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15386. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15387. ((eq type 'scheduled-or-deadline)
  15388. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15389. (defun org-check-before-date (date)
  15390. "Check if there are deadlines or scheduled entries before DATE."
  15391. (interactive (list (org-read-date)))
  15392. (let ((case-fold-search nil)
  15393. (regexp (org-re-timestamp org-ts-type))
  15394. (callback
  15395. (lambda () (time-less-p
  15396. (org-time-string-to-time (match-string 1))
  15397. (org-time-string-to-time date)))))
  15398. (message "%d entries before %s"
  15399. (org-occur regexp nil callback) date)))
  15400. (defun org-check-after-date (date)
  15401. "Check if there are deadlines or scheduled entries after DATE."
  15402. (interactive (list (org-read-date)))
  15403. (let ((case-fold-search nil)
  15404. (regexp (org-re-timestamp org-ts-type))
  15405. (callback
  15406. (lambda () (not
  15407. (time-less-p
  15408. (org-time-string-to-time (match-string 1))
  15409. (org-time-string-to-time date))))))
  15410. (message "%d entries after %s"
  15411. (org-occur regexp nil callback) date)))
  15412. (defun org-check-dates-range (start-date end-date)
  15413. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15414. (interactive (list (org-read-date nil nil nil "Range starts")
  15415. (org-read-date nil nil nil "Range end")))
  15416. (let ((case-fold-search nil)
  15417. (regexp (org-re-timestamp org-ts-type))
  15418. (callback
  15419. (lambda ()
  15420. (let ((match (match-string 1)))
  15421. (and
  15422. (not (time-less-p
  15423. (org-time-string-to-time match)
  15424. (org-time-string-to-time start-date)))
  15425. (time-less-p
  15426. (org-time-string-to-time match)
  15427. (org-time-string-to-time end-date)))))))
  15428. (message "%d entries between %s and %s"
  15429. (org-occur regexp nil callback) start-date end-date)))
  15430. (defun org-evaluate-time-range (&optional to-buffer)
  15431. "Evaluate a time range by computing the difference between start and end.
  15432. Normally the result is just printed in the echo area, but with prefix arg
  15433. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15434. If the time range is actually in a table, the result is inserted into the
  15435. next column.
  15436. For time difference computation, a year is assumed to be exactly 365
  15437. days in order to avoid rounding problems."
  15438. (interactive "P")
  15439. (or
  15440. (org-clock-update-time-maybe)
  15441. (save-excursion
  15442. (unless (org-at-date-range-p t)
  15443. (goto-char (point-at-bol))
  15444. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15445. (if (not (org-at-date-range-p t))
  15446. (error "Not at a time-stamp range, and none found in current line")))
  15447. (let* ((ts1 (match-string 1))
  15448. (ts2 (match-string 2))
  15449. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15450. (match-end (match-end 0))
  15451. (time1 (org-time-string-to-time ts1))
  15452. (time2 (org-time-string-to-time ts2))
  15453. (t1 (org-float-time time1))
  15454. (t2 (org-float-time time2))
  15455. (diff (abs (- t2 t1)))
  15456. (negative (< (- t2 t1) 0))
  15457. ;; (ys (floor (* 365 24 60 60)))
  15458. (ds (* 24 60 60))
  15459. (hs (* 60 60))
  15460. (fy "%dy %dd %02d:%02d")
  15461. (fy1 "%dy %dd")
  15462. (fd "%dd %02d:%02d")
  15463. (fd1 "%dd")
  15464. (fh "%02d:%02d")
  15465. y d h m align)
  15466. (if havetime
  15467. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15468. y 0
  15469. d (floor (/ diff ds)) diff (mod diff ds)
  15470. h (floor (/ diff hs)) diff (mod diff hs)
  15471. m (floor (/ diff 60)))
  15472. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15473. y 0
  15474. d (floor (+ (/ diff ds) 0.5))
  15475. h 0 m 0))
  15476. (if (not to-buffer)
  15477. (message "%s" (org-make-tdiff-string y d h m))
  15478. (if (org-at-table-p)
  15479. (progn
  15480. (goto-char match-end)
  15481. (setq align t)
  15482. (and (looking-at " *|") (goto-char (match-end 0))))
  15483. (goto-char match-end))
  15484. (if (looking-at
  15485. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15486. (replace-match ""))
  15487. (if negative (insert " -"))
  15488. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15489. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15490. (insert " " (format fh h m))))
  15491. (if align (org-table-align))
  15492. (message "Time difference inserted")))))
  15493. (defun org-make-tdiff-string (y d h m)
  15494. (let ((fmt "")
  15495. (l nil))
  15496. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15497. l (push y l)))
  15498. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15499. l (push d l)))
  15500. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15501. l (push h l)))
  15502. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15503. l (push m l)))
  15504. (apply 'format fmt (nreverse l))))
  15505. (defun org-time-string-to-time (s &optional buffer pos)
  15506. "Convert a timestamp string into internal time."
  15507. (condition-case errdata
  15508. (apply 'encode-time (org-parse-time-string s))
  15509. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15510. s (if (not (and buffer pos))
  15511. ""
  15512. (format " at %d in buffer `%s'" pos buffer))
  15513. (cdr errdata)))))
  15514. (defun org-time-string-to-seconds (s)
  15515. "Convert a timestamp string to a number of seconds."
  15516. (org-float-time (org-time-string-to-time s)))
  15517. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15518. "Convert a time stamp to an absolute day number.
  15519. If there is a specifier for a cyclic time stamp, get the closest
  15520. date to DAYNR.
  15521. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15522. The variable `date' is bound by the calendar when this is called."
  15523. (cond
  15524. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15525. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15526. daynr
  15527. (+ daynr 1000)))
  15528. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15529. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15530. (time-to-days (current-time))) (match-string 0 s)
  15531. prefer show-all))
  15532. (t (time-to-days
  15533. (condition-case errdata
  15534. (apply 'encode-time (org-parse-time-string s))
  15535. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15536. s (if (not (and buffer pos))
  15537. ""
  15538. (format " at %d in buffer `%s'" pos buffer))
  15539. (cdr errdata))))))))
  15540. (defun org-days-to-iso-week (days)
  15541. "Return the iso week number."
  15542. (require 'cal-iso)
  15543. (car (calendar-iso-from-absolute days)))
  15544. (defun org-small-year-to-year (year)
  15545. "Convert 2-digit years into 4-digit years.
  15546. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15547. The year 2000 cannot be abbreviated. Any year larger than 99
  15548. is returned unchanged."
  15549. (if (< year 38)
  15550. (setq year (+ 2000 year))
  15551. (if (< year 100)
  15552. (setq year (+ 1900 year))))
  15553. year)
  15554. (defun org-time-from-absolute (d)
  15555. "Return the time corresponding to date D.
  15556. D may be an absolute day number, or a calendar-type list (month day year)."
  15557. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15558. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15559. (defun org-calendar-holiday ()
  15560. "List of holidays, for Diary display in Org-mode."
  15561. (require 'holidays)
  15562. (let ((hl (funcall
  15563. (if (fboundp 'calendar-check-holidays)
  15564. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15565. (if hl (mapconcat 'identity hl "; "))))
  15566. (defun org-diary-sexp-entry (sexp entry date)
  15567. "Process a SEXP diary ENTRY for DATE."
  15568. (require 'diary-lib)
  15569. (let ((result (if calendar-debug-sexp
  15570. (let ((stack-trace-on-error t))
  15571. (eval (car (read-from-string sexp))))
  15572. (condition-case nil
  15573. (eval (car (read-from-string sexp)))
  15574. (error
  15575. (beep)
  15576. (message "Bad sexp at line %d in %s: %s"
  15577. (org-current-line)
  15578. (buffer-file-name) sexp)
  15579. (sleep-for 2))))))
  15580. (cond ((stringp result) (split-string result "; "))
  15581. ((and (consp result)
  15582. (not (consp (cdr result)))
  15583. (stringp (cdr result))) (cdr result))
  15584. ((and (consp result)
  15585. (stringp (car result))) result)
  15586. (result entry))))
  15587. (defun org-diary-to-ical-string (frombuf)
  15588. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15589. This uses the icalendar.el library."
  15590. (let* ((tmpdir (if (featurep 'xemacs)
  15591. (temp-directory)
  15592. temporary-file-directory))
  15593. (tmpfile (make-temp-name
  15594. (expand-file-name "orgics" tmpdir)))
  15595. buf rtn b e)
  15596. (with-current-buffer frombuf
  15597. (icalendar-export-region (point-min) (point-max) tmpfile)
  15598. (setq buf (find-buffer-visiting tmpfile))
  15599. (set-buffer buf)
  15600. (goto-char (point-min))
  15601. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15602. (setq b (match-beginning 0)))
  15603. (goto-char (point-max))
  15604. (if (re-search-backward "^END:VEVENT" nil t)
  15605. (setq e (match-end 0)))
  15606. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15607. (kill-buffer buf)
  15608. (delete-file tmpfile)
  15609. rtn))
  15610. (defun org-closest-date (start current change prefer show-all)
  15611. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15612. When PREFER is `past', return a date that is either CURRENT or past.
  15613. When PREFER is `future', return a date that is either CURRENT or future.
  15614. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15615. ;; Make the proper lists from the dates
  15616. (catch 'exit
  15617. (let ((a1 '(("h" . hour)
  15618. ("d" . day)
  15619. ("w" . week)
  15620. ("m" . month)
  15621. ("y" . year)))
  15622. (shour (nth 2 (org-parse-time-string start)))
  15623. dn dw sday cday n1 n2 n0
  15624. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15625. (setq start (org-date-to-gregorian start)
  15626. current (org-date-to-gregorian
  15627. (if show-all
  15628. current
  15629. (time-to-days (current-time))))
  15630. sday (calendar-absolute-from-gregorian start)
  15631. cday (calendar-absolute-from-gregorian current))
  15632. (if (<= cday sday) (throw 'exit sday))
  15633. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15634. (setq dn (string-to-number (match-string 1 change))
  15635. dw (cdr (assoc (match-string 2 change) a1)))
  15636. (error "Invalid change specifier: %s" change))
  15637. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15638. (cond
  15639. ((eq dw 'hour)
  15640. (let ((missing-hours
  15641. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15642. dn)))
  15643. (setq n1 (if (zerop missing-hours) cday
  15644. (- cday (1+ (floor (/ missing-hours 24)))))
  15645. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15646. ((eq dw 'day)
  15647. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15648. n2 (+ n1 dn)))
  15649. ((eq dw 'year)
  15650. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15651. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15652. (setq date1 (list m d y1)
  15653. n1 (calendar-absolute-from-gregorian date1)
  15654. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15655. n2 (calendar-absolute-from-gregorian date2)))
  15656. ((eq dw 'month)
  15657. ;; approx number of month between the two dates
  15658. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15659. ;; How often does dn fit in there?
  15660. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15661. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15662. m (+ m nm)
  15663. ny (floor (/ m 12))
  15664. y (+ y ny)
  15665. m (- m (* ny 12)))
  15666. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15667. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15668. (setq m2 (+ m dn) y2 y)
  15669. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15670. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15671. (while (<= n2 cday)
  15672. (setq n1 n2 m m2 y y2)
  15673. (setq m2 (+ m dn) y2 y)
  15674. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15675. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15676. ;; Make sure n1 is the earlier date
  15677. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15678. (if show-all
  15679. (cond
  15680. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15681. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15682. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15683. (cond
  15684. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15685. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15686. (t (if (= cday n1) n1 n2)))))))
  15687. (defun org-date-to-gregorian (date)
  15688. "Turn any specification of DATE into a Gregorian date for the calendar."
  15689. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15690. ((and (listp date) (= (length date) 3)) date)
  15691. ((stringp date)
  15692. (setq date (org-parse-time-string date))
  15693. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15694. ((listp date)
  15695. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15696. (defun org-parse-time-string (s &optional nodefault)
  15697. "Parse the standard Org-mode time string.
  15698. This should be a lot faster than the normal `parse-time-string'.
  15699. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15700. hour and minute fields will be nil if not given."
  15701. (cond ((string-match org-ts-regexp0 s)
  15702. (list 0
  15703. (if (or (match-beginning 8) (not nodefault))
  15704. (string-to-number (or (match-string 8 s) "0")))
  15705. (if (or (match-beginning 7) (not nodefault))
  15706. (string-to-number (or (match-string 7 s) "0")))
  15707. (string-to-number (match-string 4 s))
  15708. (string-to-number (match-string 3 s))
  15709. (string-to-number (match-string 2 s))
  15710. nil nil nil))
  15711. ((string-match "^<[^>]+>$" s)
  15712. (decode-time (seconds-to-time (org-matcher-time s))))
  15713. (t (error "Not a standard Org-mode time string: %s" s))))
  15714. (defun org-timestamp-up (&optional arg)
  15715. "Increase the date item at the cursor by one.
  15716. If the cursor is on the year, change the year. If it is on the month,
  15717. the day or the time, change that.
  15718. With prefix ARG, change by that many units."
  15719. (interactive "p")
  15720. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15721. (defun org-timestamp-down (&optional arg)
  15722. "Decrease the date item at the cursor by one.
  15723. If the cursor is on the year, change the year. If it is on the month,
  15724. the day or the time, change that.
  15725. With prefix ARG, change by that many units."
  15726. (interactive "p")
  15727. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15728. (defun org-timestamp-up-day (&optional arg)
  15729. "Increase the date in the time stamp by one day.
  15730. With prefix ARG, change that many days."
  15731. (interactive "p")
  15732. (if (and (not (org-at-timestamp-p t))
  15733. (org-at-heading-p))
  15734. (org-todo 'up)
  15735. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15736. (defun org-timestamp-down-day (&optional arg)
  15737. "Decrease the date in the time stamp by one day.
  15738. With prefix ARG, change that many days."
  15739. (interactive "p")
  15740. (if (and (not (org-at-timestamp-p t))
  15741. (org-at-heading-p))
  15742. (org-todo 'down)
  15743. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15744. (defun org-at-timestamp-p (&optional inactive-ok)
  15745. "Determine if the cursor is in or at a timestamp."
  15746. (interactive)
  15747. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15748. (pos (point))
  15749. (ans (or (looking-at tsr)
  15750. (save-excursion
  15751. (skip-chars-backward "^[<\n\r\t")
  15752. (if (> (point) (point-min)) (backward-char 1))
  15753. (and (looking-at tsr)
  15754. (> (- (match-end 0) pos) -1))))))
  15755. (and ans
  15756. (boundp 'org-ts-what)
  15757. (setq org-ts-what
  15758. (cond
  15759. ((= pos (match-beginning 0)) 'bracket)
  15760. ;; Point is considered to be "on the bracket" whether
  15761. ;; it's really on it or right after it.
  15762. ((= pos (1- (match-end 0))) 'bracket)
  15763. ((= pos (match-end 0)) 'after)
  15764. ((org-pos-in-match-range pos 2) 'year)
  15765. ((org-pos-in-match-range pos 3) 'month)
  15766. ((org-pos-in-match-range pos 7) 'hour)
  15767. ((org-pos-in-match-range pos 8) 'minute)
  15768. ((or (org-pos-in-match-range pos 4)
  15769. (org-pos-in-match-range pos 5)) 'day)
  15770. ((and (> pos (or (match-end 8) (match-end 5)))
  15771. (< pos (match-end 0)))
  15772. (- pos (or (match-end 8) (match-end 5))))
  15773. (t 'day))))
  15774. ans))
  15775. (defun org-toggle-timestamp-type ()
  15776. "Toggle the type (<active> or [inactive]) of a time stamp."
  15777. (interactive)
  15778. (when (org-at-timestamp-p t)
  15779. (let ((beg (match-beginning 0)) (end (match-end 0))
  15780. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15781. (save-excursion
  15782. (goto-char beg)
  15783. (while (re-search-forward "[][<>]" end t)
  15784. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15785. t t)))
  15786. (message "Timestamp is now %sactive"
  15787. (if (equal (char-after beg) ?<) "" "in")))))
  15788. (defun org-at-clock-log-p nil
  15789. "Is the cursor on the clock log line?"
  15790. (save-excursion
  15791. (move-beginning-of-line 1)
  15792. (looking-at "^[ \t]*CLOCK:")))
  15793. (defvar org-clock-history) ; defined in org-clock.el
  15794. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15795. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15796. "Change the date in the time stamp at point.
  15797. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15798. `year', `minute', `second'. If WHAT is not given, the cursor position
  15799. in the timestamp determines what will be changed.
  15800. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15801. (let ((origin (point)) origin-cat
  15802. with-hm inactive
  15803. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15804. org-ts-what
  15805. extra rem
  15806. ts time time0 fixnext clrgx)
  15807. (if (not (org-at-timestamp-p t))
  15808. (error "Not at a timestamp"))
  15809. (if (and (not what) (eq org-ts-what 'bracket))
  15810. (org-toggle-timestamp-type)
  15811. ;; Point isn't on brackets. Remember the part of the time-stamp
  15812. ;; the point was in. Indeed, size of time-stamps may change,
  15813. ;; but point must be kept in the same category nonetheless.
  15814. (setq origin-cat org-ts-what)
  15815. (if (and (not what) (not (eq org-ts-what 'day))
  15816. org-display-custom-times
  15817. (get-text-property (point) 'display)
  15818. (not (get-text-property (1- (point)) 'display)))
  15819. (setq org-ts-what 'day))
  15820. (setq org-ts-what (or what org-ts-what)
  15821. inactive (= (char-after (match-beginning 0)) ?\[)
  15822. ts (match-string 0))
  15823. (replace-match "")
  15824. (when (string-match
  15825. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15826. ts)
  15827. (setq extra (match-string 1 ts))
  15828. (if suppress-tmp-delay
  15829. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15830. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15831. (setq with-hm t))
  15832. (setq time0 (org-parse-time-string ts))
  15833. (when (and updown
  15834. (eq org-ts-what 'minute)
  15835. (not current-prefix-arg))
  15836. ;; This looks like s-up and s-down. Change by one rounding step.
  15837. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15838. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15839. (setcar (cdr time0) (+ (nth 1 time0)
  15840. (if (> n 0) (- rem) (- dm rem))))))
  15841. (setq time
  15842. (encode-time (or (car time0) 0)
  15843. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15844. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15845. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15846. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15847. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15848. (nthcdr 6 time0)))
  15849. (when (and (member org-ts-what '(hour minute))
  15850. extra
  15851. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15852. (setq extra (org-modify-ts-extra
  15853. extra
  15854. (if (eq org-ts-what 'hour) 2 5)
  15855. n dm)))
  15856. (when (integerp org-ts-what)
  15857. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15858. (if (eq what 'calendar)
  15859. (let ((cal-date (org-get-date-from-calendar)))
  15860. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15861. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15862. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15863. (setcar time0 (or (car time0) 0))
  15864. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15865. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15866. (setq time (apply 'encode-time time0))))
  15867. ;; Insert the new time-stamp, and ensure point stays in the same
  15868. ;; category as before (i.e. not after the last position in that
  15869. ;; category).
  15870. (let ((pos (point)))
  15871. ;; Stay before inserted string. `save-excursion' is of no use.
  15872. (setq org-last-changed-timestamp
  15873. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15874. (goto-char pos))
  15875. (save-match-data
  15876. (looking-at org-ts-regexp3)
  15877. (goto-char (cond
  15878. ;; `day' category ends before `hour' if any, or at
  15879. ;; the end of the day name.
  15880. ((eq origin-cat 'day)
  15881. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15882. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15883. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15884. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15885. ;; `year' and `month' have both fixed size: point
  15886. ;; couldn't have moved into another part.
  15887. (t origin))))
  15888. ;; Update clock if on a CLOCK line.
  15889. (org-clock-update-time-maybe)
  15890. ;; Maybe adjust the closest clock in `org-clock-history'
  15891. (when org-clock-adjust-closest
  15892. (if (not (and (org-at-clock-log-p)
  15893. (< 1 (length (delq nil (mapcar 'marker-position
  15894. org-clock-history))))))
  15895. (message "No clock to adjust")
  15896. (cond ((save-excursion ; fix previous clock?
  15897. (re-search-backward org-ts-regexp0 nil t)
  15898. (org-looking-back (concat org-clock-string " \\[")))
  15899. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15900. ((save-excursion ; fix next clock?
  15901. (re-search-backward org-ts-regexp0 nil t)
  15902. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15903. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15904. (save-window-excursion
  15905. ;; Find closest clock to point, adjust the previous/next one in history
  15906. (let* ((p (save-excursion (org-back-to-heading t)))
  15907. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15908. (clfixnth
  15909. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15910. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15911. (if (not clfixpos)
  15912. (message "No clock to adjust")
  15913. (save-excursion
  15914. (org-goto-marker-or-bmk clfixpos)
  15915. (org-show-subtree)
  15916. (when (re-search-forward clrgx nil t)
  15917. (goto-char (match-beginning 1))
  15918. (let (org-clock-adjust-closest)
  15919. (org-timestamp-change n org-ts-what updown))
  15920. (message "Clock adjusted in %s for heading: %s"
  15921. (file-name-nondirectory (buffer-file-name))
  15922. (org-get-heading t t)))))))))
  15923. ;; Try to recenter the calendar window, if any.
  15924. (if (and org-calendar-follow-timestamp-change
  15925. (get-buffer-window "*Calendar*" t)
  15926. (memq org-ts-what '(day month year)))
  15927. (org-recenter-calendar (time-to-days time))))))
  15928. (defun org-modify-ts-extra (s pos n dm)
  15929. "Change the different parts of the lead-time and repeat fields in timestamp."
  15930. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15931. ng h m new rem)
  15932. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15933. (cond
  15934. ((or (org-pos-in-match-range pos 2)
  15935. (org-pos-in-match-range pos 3))
  15936. (setq m (string-to-number (match-string 3 s))
  15937. h (string-to-number (match-string 2 s)))
  15938. (if (org-pos-in-match-range pos 2)
  15939. (setq h (+ h n))
  15940. (setq n (* dm (org-no-warnings (signum n))))
  15941. (when (not (= 0 (setq rem (% m dm))))
  15942. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15943. (setq m (+ m n)))
  15944. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15945. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15946. (setq h (min 24 (max 0 h)))
  15947. (setq ng 1 new (format "-%02d:%02d" h m)))
  15948. ((org-pos-in-match-range pos 6)
  15949. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15950. ((org-pos-in-match-range pos 5)
  15951. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15952. ((org-pos-in-match-range pos 9)
  15953. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15954. ((org-pos-in-match-range pos 8)
  15955. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15956. (when ng
  15957. (setq s (concat
  15958. (substring s 0 (match-beginning ng))
  15959. new
  15960. (substring s (match-end ng))))))
  15961. s))
  15962. (defun org-recenter-calendar (date)
  15963. "If the calendar is visible, recenter it to DATE."
  15964. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15965. (when cwin
  15966. (let ((calendar-move-hook nil))
  15967. (with-selected-window cwin
  15968. (calendar-goto-date (if (listp date) date
  15969. (calendar-gregorian-from-absolute date))))))))
  15970. (defun org-goto-calendar (&optional arg)
  15971. "Go to the Emacs calendar at the current date.
  15972. If there is a time stamp in the current line, go to that date.
  15973. A prefix ARG can be used to force the current date."
  15974. (interactive "P")
  15975. (let ((tsr org-ts-regexp) diff
  15976. (calendar-move-hook nil)
  15977. (calendar-view-holidays-initially-flag nil)
  15978. (calendar-view-diary-initially-flag nil))
  15979. (if (or (org-at-timestamp-p)
  15980. (save-excursion
  15981. (beginning-of-line 1)
  15982. (looking-at (concat ".*" tsr))))
  15983. (let ((d1 (time-to-days (current-time)))
  15984. (d2 (time-to-days
  15985. (org-time-string-to-time (match-string 1)))))
  15986. (setq diff (- d2 d1))))
  15987. (calendar)
  15988. (calendar-goto-today)
  15989. (if (and diff (not arg)) (calendar-forward-day diff))))
  15990. (defun org-get-date-from-calendar ()
  15991. "Return a list (month day year) of date at point in calendar."
  15992. (with-current-buffer "*Calendar*"
  15993. (save-match-data
  15994. (calendar-cursor-to-date))))
  15995. (defun org-date-from-calendar ()
  15996. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15997. If there is already a time stamp at the cursor position, update it."
  15998. (interactive)
  15999. (if (org-at-timestamp-p t)
  16000. (org-timestamp-change 0 'calendar)
  16001. (let ((cal-date (org-get-date-from-calendar)))
  16002. (org-insert-time-stamp
  16003. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16004. (defcustom org-effort-durations
  16005. `(("h" . 60)
  16006. ("d" . ,(* 60 8))
  16007. ("w" . ,(* 60 8 5))
  16008. ("m" . ,(* 60 8 5 4))
  16009. ("y" . ,(* 60 8 5 40)))
  16010. "Conversion factor to minutes for an effort modifier.
  16011. Each entry has the form (MODIFIER . MINUTES).
  16012. In an effort string, a number followed by MODIFIER is multiplied
  16013. by the specified number of MINUTES to obtain an effort in
  16014. minutes.
  16015. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16016. effort string \"2hours\" is equivalent to 120 minutes."
  16017. :group 'org-agenda
  16018. :version "24.1"
  16019. :type '(alist :key-type (string :tag "Modifier")
  16020. :value-type (number :tag "Minutes")))
  16021. (defun org-minutes-to-clocksum-string (m)
  16022. "Format number of minutes as a clocksum string.
  16023. The format is determined by `org-time-clocksum-format',
  16024. `org-time-clocksum-use-fractional' and
  16025. `org-time-clocksum-fractional-format' and
  16026. `org-time-clocksum-use-effort-durations'."
  16027. (let ((clocksum "") h d w mo y fmt n)
  16028. (setq h (if org-time-clocksum-use-effort-durations
  16029. (cdr (assoc "h" org-effort-durations)) 60)
  16030. d (if org-time-clocksum-use-effort-durations
  16031. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16032. w (if org-time-clocksum-use-effort-durations
  16033. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16034. mo (if org-time-clocksum-use-effort-durations
  16035. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16036. y (if org-time-clocksum-use-effort-durations
  16037. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16038. ;; fractional format
  16039. (if org-time-clocksum-use-fractional
  16040. (cond
  16041. ;; single format string
  16042. ((stringp org-time-clocksum-fractional-format)
  16043. (format org-time-clocksum-fractional-format (/ m (float h))))
  16044. ;; choice of fractional formats for different time units
  16045. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16046. (> (/ (truncate m) (* y d h)) 0))
  16047. (format fmt (/ m (* y d (float h)))))
  16048. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16049. (> (/ (truncate m) (* mo d h)) 0))
  16050. (format fmt (/ m (* mo d (float h)))))
  16051. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16052. (> (/ (truncate m) (* w d h)) 0))
  16053. (format fmt (/ m (* w d (float h)))))
  16054. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16055. (> (/ (truncate m) (* d h)) 0))
  16056. (format fmt (/ m (* d (float h)))))
  16057. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16058. (> (/ (truncate m) h) 0))
  16059. (format fmt (/ m (float h))))
  16060. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16061. (format fmt m))
  16062. ;; fall back to smallest time unit with a format
  16063. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16064. (format fmt (/ m (float h))))
  16065. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16066. (format fmt (/ m (* d (float h)))))
  16067. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16068. (format fmt (/ m (* w d (float h)))))
  16069. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16070. (format fmt (/ m (* mo d (float h)))))
  16071. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16072. (format fmt (/ m (* y d (float h))))))
  16073. ;; standard (non-fractional) format, with single format string
  16074. (if (stringp org-time-clocksum-format)
  16075. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16076. ;; separate formats components
  16077. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16078. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16079. (plist-get org-time-clocksum-format :require-years))
  16080. (setq clocksum (concat clocksum (format fmt n))
  16081. m (- m (* n y d h))))
  16082. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16083. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16084. (plist-get org-time-clocksum-format :require-months))
  16085. (setq clocksum (concat clocksum (format fmt n))
  16086. m (- m (* n mo d h))))
  16087. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16088. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16089. (plist-get org-time-clocksum-format :require-weeks))
  16090. (setq clocksum (concat clocksum (format fmt n))
  16091. m (- m (* n w d h))))
  16092. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16093. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16094. (plist-get org-time-clocksum-format :require-days))
  16095. (setq clocksum (concat clocksum (format fmt n))
  16096. m (- m (* n d h))))
  16097. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16098. (or (> (setq n (/ (truncate m) h)) 0)
  16099. (plist-get org-time-clocksum-format :require-hours))
  16100. (setq clocksum (concat clocksum (format fmt n))
  16101. m (- m (* n h))))
  16102. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16103. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16104. (setq clocksum (concat clocksum (format fmt m))))
  16105. ;; return formatted time duration
  16106. clocksum))))
  16107. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16108. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16109. "Org mode version 8.0")
  16110. (defun org-hours-to-clocksum-string (n)
  16111. (org-minutes-to-clocksum-string (* n 60)))
  16112. (defun org-hh:mm-string-to-minutes (s)
  16113. "Convert a string H:MM to a number of minutes.
  16114. If the string is just a number, interpret it as minutes.
  16115. In fact, the first hh:mm or number in the string will be taken,
  16116. there can be extra stuff in the string.
  16117. If no number is found, the return value is 0."
  16118. (cond
  16119. ((integerp s) s)
  16120. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16121. (+ (* (string-to-number (match-string 1 s)) 60)
  16122. (string-to-number (match-string 2 s))))
  16123. ((string-match "\\([0-9]+\\)" s)
  16124. (string-to-number (match-string 1 s)))
  16125. (t 0)))
  16126. (defcustom org-image-actual-width t
  16127. "Should we use the actual width of images when inlining them?
  16128. When set to `t', always use the image width.
  16129. When set to a number, use imagemagick (when available) to set
  16130. the image's width to this value.
  16131. When set to a number in a list, try to get the width from any
  16132. #+ATTR.* keyword if it matches a width specification like
  16133. #+ATTR_HTML: :width 300px
  16134. and fall back on that number if none is found.
  16135. When set to nil, try to get the width from an #+ATTR.* keyword
  16136. and fall back on the original width if none is found.
  16137. This requires Emacs >= 24.1, build with imagemagick support."
  16138. :group 'org-appearance
  16139. :version "24.4"
  16140. :package-version '(Org . "8.0")
  16141. :type '(choice
  16142. (const :tag "Use the image width" t)
  16143. (integer :tag "Use a number of pixels")
  16144. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16145. (const :tag "Use #+ATTR* or don't resize" nil)))
  16146. (defcustom org-agenda-inhibit-startup nil
  16147. "Inhibit startup when preparing agenda buffers.
  16148. When this variable is `t' (the default), the initialization of
  16149. the Org agenda buffers is inhibited: e.g. the visibility state
  16150. is not set, the tables are not re-aligned, etc."
  16151. :type 'boolean
  16152. :version "24.3"
  16153. :group 'org-agenda)
  16154. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16155. "Convert a duration string S to minutes.
  16156. A bare number is interpreted as minutes, modifiers can be set by
  16157. customizing `org-effort-durations' (which see).
  16158. Entries containing a colon are interpreted as H:MM by
  16159. `org-hh:mm-string-to-minutes'."
  16160. (let ((result 0)
  16161. (re (concat "\\([0-9.]+\\) *\\("
  16162. (regexp-opt (mapcar 'car org-effort-durations))
  16163. "\\)")))
  16164. (while (string-match re s)
  16165. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16166. (string-to-number (match-string 1 s))))
  16167. (setq s (replace-match "" nil t s)))
  16168. (setq result (floor result))
  16169. (incf result (org-hh:mm-string-to-minutes s))
  16170. (if output-to-string (number-to-string result) result)))
  16171. ;;;; Files
  16172. (defun org-save-all-org-buffers ()
  16173. "Save all Org-mode buffers without user confirmation."
  16174. (interactive)
  16175. (message "Saving all Org-mode buffers...")
  16176. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16177. (when (featurep 'org-id) (org-id-locations-save))
  16178. (message "Saving all Org-mode buffers... done"))
  16179. (defun org-revert-all-org-buffers ()
  16180. "Revert all Org-mode buffers.
  16181. Prompt for confirmation when there are unsaved changes.
  16182. Be sure you know what you are doing before letting this function
  16183. overwrite your changes.
  16184. This function is useful in a setup where one tracks org files
  16185. with a version control system, to revert on one machine after pulling
  16186. changes from another. I believe the procedure must be like this:
  16187. 1. M-x org-save-all-org-buffers
  16188. 2. Pull changes from the other machine, resolve conflicts
  16189. 3. M-x org-revert-all-org-buffers"
  16190. (interactive)
  16191. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16192. (error "Abort"))
  16193. (save-excursion
  16194. (save-window-excursion
  16195. (mapc
  16196. (lambda (b)
  16197. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16198. (with-current-buffer b buffer-file-name))
  16199. (org-pop-to-buffer-same-window b)
  16200. (revert-buffer t 'no-confirm)))
  16201. (buffer-list))
  16202. (when (and (featurep 'org-id) org-id-track-globally)
  16203. (org-id-locations-load)))))
  16204. ;;;; Agenda files
  16205. ;;;###autoload
  16206. (defun org-switchb (&optional arg)
  16207. "Switch between Org buffers.
  16208. With one prefix argument, restrict available buffers to files.
  16209. With two prefix arguments, restrict available buffers to agenda files.
  16210. Defaults to `iswitchb' for buffer name completion.
  16211. Set `org-completion-use-ido' to make it use ido instead."
  16212. (interactive "P")
  16213. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16214. ((equal arg '(16)) (org-buffer-list 'agenda))
  16215. (t (org-buffer-list))))
  16216. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16217. (org-completion-use-ido org-completion-use-ido))
  16218. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16219. (setq org-completion-use-iswitchb t))
  16220. (org-pop-to-buffer-same-window
  16221. (org-icompleting-read "Org buffer: "
  16222. (mapcar 'list (mapcar 'buffer-name blist))
  16223. nil t))))
  16224. ;;; Define some older names previously used for this functionality
  16225. ;;;###autoload
  16226. (defalias 'org-ido-switchb 'org-switchb)
  16227. ;;;###autoload
  16228. (defalias 'org-iswitchb 'org-switchb)
  16229. (defun org-buffer-list (&optional predicate exclude-tmp)
  16230. "Return a list of Org buffers.
  16231. PREDICATE can be `export', `files' or `agenda'.
  16232. export restrict the list to Export buffers.
  16233. files restrict the list to buffers visiting Org files.
  16234. agenda restrict the list to buffers visiting agenda files.
  16235. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16236. (let* ((bfn nil)
  16237. (agenda-files (and (eq predicate 'agenda)
  16238. (mapcar 'file-truename (org-agenda-files t))))
  16239. (filter
  16240. (cond
  16241. ((eq predicate 'files)
  16242. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16243. ((eq predicate 'export)
  16244. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16245. ((eq predicate 'agenda)
  16246. (lambda (b)
  16247. (with-current-buffer b
  16248. (and (derived-mode-p 'org-mode)
  16249. (setq bfn (buffer-file-name b))
  16250. (member (file-truename bfn) agenda-files)))))
  16251. (t (lambda (b) (with-current-buffer b
  16252. (or (derived-mode-p 'org-mode)
  16253. (string-match "\*Org .*Export"
  16254. (buffer-name b)))))))))
  16255. (delq nil
  16256. (mapcar
  16257. (lambda(b)
  16258. (if (and (funcall filter b)
  16259. (or (not exclude-tmp)
  16260. (not (string-match "tmp" (buffer-name b)))))
  16261. b
  16262. nil))
  16263. (buffer-list)))))
  16264. (defun org-agenda-files (&optional unrestricted archives)
  16265. "Get the list of agenda files.
  16266. Optional UNRESTRICTED means return the full list even if a restriction
  16267. is currently in place.
  16268. When ARCHIVES is t, include all archive files that are really being
  16269. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16270. `org-agenda-archives-mode' is t."
  16271. (let ((files
  16272. (cond
  16273. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16274. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16275. ((listp org-agenda-files) org-agenda-files)
  16276. (t (error "Invalid value of `org-agenda-files'")))))
  16277. (setq files (apply 'append
  16278. (mapcar (lambda (f)
  16279. (if (file-directory-p f)
  16280. (directory-files
  16281. f t org-agenda-file-regexp)
  16282. (list f)))
  16283. files)))
  16284. (when org-agenda-skip-unavailable-files
  16285. (setq files (delq nil
  16286. (mapcar (function
  16287. (lambda (file)
  16288. (and (file-readable-p file) file)))
  16289. files))))
  16290. (when (or (eq archives t)
  16291. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16292. (setq files (org-add-archive-files files)))
  16293. files))
  16294. (defun org-agenda-file-p (&optional file)
  16295. "Return non-nil, if FILE is an agenda file.
  16296. If FILE is omitted, use the file associated with the current
  16297. buffer."
  16298. (member (or file (buffer-file-name))
  16299. (org-agenda-files t)))
  16300. (defun org-edit-agenda-file-list ()
  16301. "Edit the list of agenda files.
  16302. Depending on setup, this either uses customize to edit the variable
  16303. `org-agenda-files', or it visits the file that is holding the list. In the
  16304. latter case, the buffer is set up in a way that saving it automatically kills
  16305. the buffer and restores the previous window configuration."
  16306. (interactive)
  16307. (if (stringp org-agenda-files)
  16308. (let ((cw (current-window-configuration)))
  16309. (find-file org-agenda-files)
  16310. (org-set-local 'org-window-configuration cw)
  16311. (org-add-hook 'after-save-hook
  16312. (lambda ()
  16313. (set-window-configuration
  16314. (prog1 org-window-configuration
  16315. (kill-buffer (current-buffer))))
  16316. (org-install-agenda-files-menu)
  16317. (message "New agenda file list installed"))
  16318. nil 'local)
  16319. (message "%s" (substitute-command-keys
  16320. "Edit list and finish with \\[save-buffer]")))
  16321. (customize-variable 'org-agenda-files)))
  16322. (defun org-store-new-agenda-file-list (list)
  16323. "Set new value for the agenda file list and save it correctly."
  16324. (if (stringp org-agenda-files)
  16325. (let ((fe (org-read-agenda-file-list t)) b u)
  16326. (while (setq b (find-buffer-visiting org-agenda-files))
  16327. (kill-buffer b))
  16328. (with-temp-file org-agenda-files
  16329. (insert
  16330. (mapconcat
  16331. (lambda (f) ;; Keep un-expanded entries.
  16332. (if (setq u (assoc f fe))
  16333. (cdr u)
  16334. f))
  16335. list "\n")
  16336. "\n")))
  16337. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16338. (org-insert-mode-line-in-empty-file nil))
  16339. (setq org-agenda-files list)
  16340. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16341. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16342. "Read the list of agenda files from a file.
  16343. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16344. filenames, used by `org-store-new-agenda-file-list' to write back
  16345. un-expanded file names."
  16346. (when (file-directory-p org-agenda-files)
  16347. (error "`org-agenda-files' cannot be a single directory"))
  16348. (when (stringp org-agenda-files)
  16349. (with-temp-buffer
  16350. (insert-file-contents org-agenda-files)
  16351. (mapcar
  16352. (lambda (f)
  16353. (let ((e (expand-file-name (substitute-in-file-name f)
  16354. org-directory)))
  16355. (if pair-with-expansion
  16356. (cons e f)
  16357. e)))
  16358. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16359. ;;;###autoload
  16360. (defun org-cycle-agenda-files ()
  16361. "Cycle through the files in `org-agenda-files'.
  16362. If the current buffer visits an agenda file, find the next one in the list.
  16363. If the current buffer does not, find the first agenda file."
  16364. (interactive)
  16365. (let* ((fs (org-agenda-files t))
  16366. (files (append fs (list (car fs))))
  16367. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16368. file)
  16369. (unless files (error "No agenda files"))
  16370. (catch 'exit
  16371. (while (setq file (pop files))
  16372. (if (equal (file-truename file) tcf)
  16373. (when (car files)
  16374. (find-file (car files))
  16375. (throw 'exit t))))
  16376. (find-file (car fs)))
  16377. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16378. (defun org-agenda-file-to-front (&optional to-end)
  16379. "Move/add the current file to the top of the agenda file list.
  16380. If the file is not present in the list, it is added to the front. If it is
  16381. present, it is moved there. With optional argument TO-END, add/move to the
  16382. end of the list."
  16383. (interactive "P")
  16384. (let ((org-agenda-skip-unavailable-files nil)
  16385. (file-alist (mapcar (lambda (x)
  16386. (cons (file-truename x) x))
  16387. (org-agenda-files t)))
  16388. (ctf (file-truename
  16389. (or buffer-file-name
  16390. (error "Please save the current buffer to a file"))))
  16391. x had)
  16392. (setq x (assoc ctf file-alist) had x)
  16393. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16394. (if to-end
  16395. (setq file-alist (append (delq x file-alist) (list x)))
  16396. (setq file-alist (cons x (delq x file-alist))))
  16397. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16398. (org-install-agenda-files-menu)
  16399. (message "File %s to %s of agenda file list"
  16400. (if had "moved" "added") (if to-end "end" "front"))))
  16401. (defun org-remove-file (&optional file)
  16402. "Remove current file from the list of files in variable `org-agenda-files'.
  16403. These are the files which are being checked for agenda entries.
  16404. Optional argument FILE means use this file instead of the current."
  16405. (interactive)
  16406. (let* ((org-agenda-skip-unavailable-files nil)
  16407. (file (or file buffer-file-name
  16408. (error "Current buffer does not visit a file")))
  16409. (true-file (file-truename file))
  16410. (afile (abbreviate-file-name file))
  16411. (files (delq nil (mapcar
  16412. (lambda (x)
  16413. (if (equal true-file
  16414. (file-truename x))
  16415. nil x))
  16416. (org-agenda-files t)))))
  16417. (if (not (= (length files) (length (org-agenda-files t))))
  16418. (progn
  16419. (org-store-new-agenda-file-list files)
  16420. (org-install-agenda-files-menu)
  16421. (message "Removed file: %s" afile))
  16422. (message "File was not in list: %s (not removed)" afile))))
  16423. (defun org-file-menu-entry (file)
  16424. (vector file (list 'find-file file) t))
  16425. (defun org-check-agenda-file (file)
  16426. "Make sure FILE exists. If not, ask user what to do."
  16427. (when (not (file-exists-p file))
  16428. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16429. (abbreviate-file-name file))
  16430. (let ((r (downcase (read-char-exclusive))))
  16431. (cond
  16432. ((equal r ?r)
  16433. (org-remove-file file)
  16434. (throw 'nextfile t))
  16435. (t (error "Abort"))))))
  16436. (defun org-get-agenda-file-buffer (file)
  16437. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16438. it to the list of buffers which might be released later."
  16439. (let ((buf (org-find-base-buffer-visiting file)))
  16440. (if buf
  16441. buf ; just return it
  16442. ;; Make a new buffer and remember it
  16443. (setq buf (find-file-noselect file))
  16444. (if buf (push buf org-agenda-new-buffers))
  16445. buf)))
  16446. (defun org-release-buffers (blist)
  16447. "Release all buffers in list, asking the user for confirmation when needed.
  16448. When a buffer is unmodified, it is just killed. When modified, it is saved
  16449. \(if the user agrees) and then killed."
  16450. (let (buf file)
  16451. (while (setq buf (pop blist))
  16452. (setq file (buffer-file-name buf))
  16453. (when (and (buffer-modified-p buf)
  16454. file
  16455. (y-or-n-p (format "Save file %s? " file)))
  16456. (with-current-buffer buf (save-buffer)))
  16457. (kill-buffer buf))))
  16458. (defun org-agenda-prepare-buffers (files)
  16459. "Create buffers for all agenda files, protect archived trees and comments."
  16460. (interactive)
  16461. (let ((pa '(:org-archived t))
  16462. (pc '(:org-comment t))
  16463. (pall '(:org-archived t :org-comment t))
  16464. (inhibit-read-only t)
  16465. (org-inhibit-startup org-agenda-inhibit-startup)
  16466. (rea (concat ":" org-archive-tag ":"))
  16467. file re)
  16468. (setq org-tag-alist-for-agenda nil
  16469. org-tag-groups-alist-for-agenda nil)
  16470. (save-excursion
  16471. (save-restriction
  16472. (while (setq file (pop files))
  16473. (catch 'nextfile
  16474. (if (bufferp file)
  16475. (set-buffer file)
  16476. (org-check-agenda-file file)
  16477. (set-buffer (org-get-agenda-file-buffer file)))
  16478. (widen)
  16479. (org-set-regexps-and-options-for-tags)
  16480. (org-refresh-category-properties)
  16481. (org-refresh-properties org-effort-property 'org-effort)
  16482. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16483. (setq org-todo-keywords-for-agenda
  16484. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16485. (setq org-done-keywords-for-agenda
  16486. (append org-done-keywords-for-agenda org-done-keywords))
  16487. (setq org-todo-keyword-alist-for-agenda
  16488. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16489. (setq org-drawers-for-agenda
  16490. (append org-drawers-for-agenda org-drawers))
  16491. (unless (equal org-tag-alist-for-agenda org-tag-alist)
  16492. (setq org-tag-alist-for-agenda
  16493. (append org-tag-alist-for-agenda org-tag-alist)))
  16494. (if org-group-tags
  16495. (setq org-tag-groups-alist-for-agenda
  16496. (org-uniquify-alist
  16497. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16498. (org-with-silent-modifications
  16499. (save-excursion
  16500. (remove-text-properties (point-min) (point-max) pall)
  16501. (when org-agenda-skip-archived-trees
  16502. (goto-char (point-min))
  16503. (while (re-search-forward rea nil t)
  16504. (if (org-at-heading-p t)
  16505. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16506. (goto-char (point-min))
  16507. (setq re (format org-heading-keyword-regexp-format
  16508. org-comment-string))
  16509. (while (re-search-forward re nil t)
  16510. (add-text-properties
  16511. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16512. (setq org-todo-keywords-for-agenda
  16513. (org-uniquify org-todo-keywords-for-agenda))
  16514. (setq org-todo-keyword-alist-for-agenda
  16515. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16516. ;;;; CDLaTeX minor mode
  16517. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16518. "Keymap for the minor `org-cdlatex-mode'.")
  16519. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16520. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16521. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16522. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16523. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16524. (defvar org-cdlatex-texmathp-advice-is-done nil
  16525. "Flag remembering if we have applied the advice to texmathp already.")
  16526. (define-minor-mode org-cdlatex-mode
  16527. "Toggle the minor `org-cdlatex-mode'.
  16528. This mode supports entering LaTeX environment and math in LaTeX fragments
  16529. in Org-mode.
  16530. \\{org-cdlatex-mode-map}"
  16531. nil " OCDL" nil
  16532. (when org-cdlatex-mode
  16533. (require 'cdlatex)
  16534. (run-hooks 'cdlatex-mode-hook)
  16535. (cdlatex-compute-tables))
  16536. (unless org-cdlatex-texmathp-advice-is-done
  16537. (setq org-cdlatex-texmathp-advice-is-done t)
  16538. (defadvice texmathp (around org-math-always-on activate)
  16539. "Always return t in org-mode buffers.
  16540. This is because we want to insert math symbols without dollars even outside
  16541. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16542. an embedded LaTeX fragment, let texmathp do its job.
  16543. \\[org-cdlatex-mode-map]"
  16544. (interactive)
  16545. (let (p)
  16546. (cond
  16547. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16548. ((eq this-command 'cdlatex-math-symbol)
  16549. (setq ad-return-value t
  16550. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16551. (t
  16552. (let ((p (org-inside-LaTeX-fragment-p)))
  16553. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16554. (setq ad-return-value t
  16555. texmathp-why '("Org-mode embedded math" . 0))
  16556. (if p ad-do-it)))))))))
  16557. (defun turn-on-org-cdlatex ()
  16558. "Unconditionally turn on `org-cdlatex-mode'."
  16559. (org-cdlatex-mode 1))
  16560. (defun org-try-cdlatex-tab ()
  16561. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16562. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16563. - inside a LaTeX fragment, or
  16564. - after the first word in a line, where an abbreviation expansion could
  16565. insert a LaTeX environment."
  16566. (when org-cdlatex-mode
  16567. (cond
  16568. ;; Before any word on the line: No expansion possible.
  16569. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16570. ;; Just after first word on the line: Expand it. Make sure it
  16571. ;; cannot happen on headlines, though.
  16572. ((save-excursion
  16573. (skip-chars-backward "a-zA-Z0-9*")
  16574. (skip-chars-backward " \t")
  16575. (and (bolp) (not (org-at-heading-p))))
  16576. (cdlatex-tab) t)
  16577. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16578. (defun org-cdlatex-underscore-caret (&optional arg)
  16579. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16580. Revert to the normal definition outside of these fragments."
  16581. (interactive "P")
  16582. (if (org-inside-LaTeX-fragment-p)
  16583. (call-interactively 'cdlatex-sub-superscript)
  16584. (let (org-cdlatex-mode)
  16585. (call-interactively (key-binding (vector last-input-event))))))
  16586. (defun org-cdlatex-math-modify (&optional arg)
  16587. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16588. Revert to the normal definition outside of these fragments."
  16589. (interactive "P")
  16590. (if (org-inside-LaTeX-fragment-p)
  16591. (call-interactively 'cdlatex-math-modify)
  16592. (let (org-cdlatex-mode)
  16593. (call-interactively (key-binding (vector last-input-event))))))
  16594. ;;;; LaTeX fragments
  16595. (defvar org-latex-regexps
  16596. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16597. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16598. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16599. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16600. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16601. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16602. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16603. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16604. "Regular expressions for matching embedded LaTeX.")
  16605. (defun org-inside-LaTeX-fragment-p ()
  16606. "Test if point is inside a LaTeX fragment.
  16607. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16608. sequence appearing also before point.
  16609. Even though the matchers for math are configurable, this function assumes
  16610. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16611. delimiters are skipped when they have been removed by customization.
  16612. The return value is nil, or a cons cell with the delimiter and the
  16613. position of this delimiter.
  16614. This function does a reasonably good job, but can locally be fooled by
  16615. for example currency specifications. For example it will assume being in
  16616. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16617. fragments that are properly closed, but during editing, we have to live
  16618. with the uncertainty caused by missing closing delimiters. This function
  16619. looks only before point, not after."
  16620. (catch 'exit
  16621. (let ((pos (point))
  16622. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16623. (lim (progn
  16624. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16625. (point)))
  16626. dd-on str (start 0) m re)
  16627. (goto-char pos)
  16628. (when dodollar
  16629. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16630. re (nth 1 (assoc "$" org-latex-regexps)))
  16631. (while (string-match re str start)
  16632. (cond
  16633. ((= (match-end 0) (length str))
  16634. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16635. ((= (match-end 0) (- (length str) 5))
  16636. (throw 'exit nil))
  16637. (t (setq start (match-end 0))))))
  16638. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16639. (goto-char pos)
  16640. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16641. (and (match-beginning 2) (throw 'exit nil))
  16642. ;; count $$
  16643. (while (re-search-backward "\\$\\$" lim t)
  16644. (setq dd-on (not dd-on)))
  16645. (goto-char pos)
  16646. (if dd-on (cons "$$" m))))))
  16647. (defun org-inside-latex-macro-p ()
  16648. "Is point inside a LaTeX macro or its arguments?"
  16649. (save-match-data
  16650. (org-in-regexp
  16651. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16652. (defvar org-latex-fragment-image-overlays nil
  16653. "List of overlays carrying the images of latex fragments.")
  16654. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16655. (defun org-remove-latex-fragment-image-overlays ()
  16656. "Remove all overlays with LaTeX fragment images in current buffer."
  16657. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16658. (setq org-latex-fragment-image-overlays nil))
  16659. (defun org-preview-latex-fragment (&optional subtree)
  16660. "Preview the LaTeX fragment at point, or all locally or globally.
  16661. If the cursor is in a LaTeX fragment, create the image and overlay
  16662. it over the source code. If there is no fragment at point, display
  16663. all fragments in the current text, from one headline to the next. With
  16664. prefix SUBTREE, display all fragments in the current subtree. With a
  16665. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16666. the cursor is before the first headline,
  16667. display all fragments in the buffer.
  16668. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16669. (interactive "P")
  16670. (unless buffer-file-name
  16671. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16672. (org-remove-latex-fragment-image-overlays)
  16673. (save-excursion
  16674. (save-restriction
  16675. (let (beg end at msg)
  16676. (cond
  16677. ((or (equal subtree '(16))
  16678. (not (save-excursion
  16679. (re-search-backward org-outline-regexp-bol nil t))))
  16680. (setq beg (point-min) end (point-max)
  16681. msg "Creating images for buffer...%s"))
  16682. ((equal subtree '(4))
  16683. (org-back-to-heading)
  16684. (setq beg (point) end (org-end-of-subtree t)
  16685. msg "Creating images for subtree...%s"))
  16686. (t
  16687. (if (setq at (org-inside-LaTeX-fragment-p))
  16688. (goto-char (max (point-min) (- (cdr at) 2)))
  16689. (org-back-to-heading))
  16690. (setq beg (point) end (progn (outline-next-heading) (point))
  16691. msg (if at "Creating image...%s"
  16692. "Creating images for entry...%s"))))
  16693. (message msg "")
  16694. (narrow-to-region beg end)
  16695. (goto-char beg)
  16696. (org-format-latex
  16697. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16698. (file-name-nondirectory
  16699. buffer-file-name)))
  16700. default-directory 'overlays msg at 'forbuffer
  16701. org-latex-create-formula-image-program)
  16702. (message msg "done. Use `C-c C-c' to remove images.")))))
  16703. (defun org-format-latex (prefix &optional dir overlays msg at
  16704. forbuffer processing-type)
  16705. "Replace LaTeX fragments with links to an image, and produce images.
  16706. Some of the options can be changed using the variable
  16707. `org-format-latex-options'."
  16708. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16709. (let* ((prefixnodir (file-name-nondirectory prefix))
  16710. (absprefix (expand-file-name prefix dir))
  16711. (todir (file-name-directory absprefix))
  16712. (opt org-format-latex-options)
  16713. (optnew org-format-latex-options)
  16714. (matchers (plist-get opt :matchers))
  16715. (re-list org-latex-regexps)
  16716. (cnt 0) txt hash link beg end re e checkdir
  16717. string
  16718. m n block-type block linkfile movefile ov)
  16719. ;; Check the different regular expressions
  16720. (while (setq e (pop re-list))
  16721. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16722. block (if block-type "\n\n" ""))
  16723. (when (member m matchers)
  16724. (goto-char (point-min))
  16725. (while (re-search-forward re nil t)
  16726. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16727. (or (not overlays)
  16728. (not (eq (get-char-property (match-beginning n)
  16729. 'org-overlay-type)
  16730. 'org-latex-overlay))))
  16731. (cond
  16732. ((eq processing-type 'verbatim))
  16733. ((eq processing-type 'mathjax)
  16734. ;; Prepare for MathJax processing.
  16735. (setq string (match-string n))
  16736. (when (member m '("$" "$1"))
  16737. (save-excursion
  16738. (delete-region (match-beginning n) (match-end n))
  16739. (goto-char (match-beginning n))
  16740. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16741. ((or (eq processing-type 'dvipng)
  16742. (eq processing-type 'imagemagick))
  16743. ;; Process to an image.
  16744. (setq txt (match-string n)
  16745. beg (match-beginning n) end (match-end n)
  16746. cnt (1+ cnt))
  16747. (let ((face (face-at-point))
  16748. (fg (plist-get opt :foreground))
  16749. (bg (plist-get opt :background))
  16750. ;; Ensure full list is printed.
  16751. print-length print-level)
  16752. (when forbuffer
  16753. ;; Get the colors from the face at point.
  16754. (goto-char beg)
  16755. (when (eq fg 'auto)
  16756. (setq fg (face-attribute face :foreground nil 'default)))
  16757. (when (eq bg 'auto)
  16758. (setq bg (face-attribute face :background nil 'default)))
  16759. (setq optnew (copy-sequence opt))
  16760. (plist-put optnew :foreground fg)
  16761. (plist-put optnew :background bg))
  16762. (setq hash (sha1 (prin1-to-string
  16763. (list org-format-latex-header
  16764. org-latex-default-packages-alist
  16765. org-latex-packages-alist
  16766. org-format-latex-options
  16767. forbuffer txt fg bg)))
  16768. linkfile (format "%s_%s.png" prefix hash)
  16769. movefile (format "%s_%s.png" absprefix hash)))
  16770. (setq link (concat block "[[file:" linkfile "]]" block))
  16771. (if msg (message msg cnt))
  16772. (goto-char beg)
  16773. (unless checkdir ; Ensure the directory exists.
  16774. (setq checkdir t)
  16775. (or (file-directory-p todir) (make-directory todir t)))
  16776. (unless (file-exists-p movefile)
  16777. (org-create-formula-image
  16778. txt movefile optnew forbuffer processing-type))
  16779. (if overlays
  16780. (progn
  16781. (mapc (lambda (o)
  16782. (if (eq (overlay-get o 'org-overlay-type)
  16783. 'org-latex-overlay)
  16784. (delete-overlay o)))
  16785. (overlays-in beg end))
  16786. (setq ov (make-overlay beg end))
  16787. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16788. (if (featurep 'xemacs)
  16789. (progn
  16790. (overlay-put ov 'invisible t)
  16791. (overlay-put
  16792. ov 'end-glyph
  16793. (make-glyph (vector 'png :file movefile))))
  16794. (overlay-put
  16795. ov 'display
  16796. (list 'image :type 'png :file movefile :ascent 'center)))
  16797. (push ov org-latex-fragment-image-overlays)
  16798. (goto-char end))
  16799. (delete-region beg end)
  16800. (insert (org-add-props link
  16801. (list 'org-latex-src
  16802. (replace-regexp-in-string
  16803. "\"" "" txt)
  16804. 'org-latex-src-embed-type
  16805. (if block-type 'paragraph 'character))))))
  16806. ((eq processing-type 'mathml)
  16807. ;; Process to MathML
  16808. (unless (save-match-data (org-format-latex-mathml-available-p))
  16809. (error "LaTeX to MathML converter not configured"))
  16810. (setq txt (match-string n)
  16811. beg (match-beginning n) end (match-end n)
  16812. cnt (1+ cnt))
  16813. (if msg (message msg cnt))
  16814. (goto-char beg)
  16815. (delete-region beg end)
  16816. (insert (org-format-latex-as-mathml
  16817. txt block-type prefix dir)))
  16818. (t
  16819. (error "Unknown conversion type %s for latex fragments"
  16820. processing-type)))))))))
  16821. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16822. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16823. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16824. sucessful, return the portion between \"<math...> </math>\"
  16825. elements otherwise return nil. When MATHML-FILE is specified,
  16826. write the results in to that file. When invoked as an
  16827. interactive command, prompt for LATEX-FRAG, with initial value
  16828. set to the current active region and echo the results for user
  16829. inspection."
  16830. (interactive (list (let ((frag (when (org-region-active-p)
  16831. (buffer-substring-no-properties
  16832. (region-beginning) (region-end)))))
  16833. (read-string "LaTeX Fragment: " frag nil frag))))
  16834. (unless latex-frag (error "Invalid latex-frag"))
  16835. (let* ((tmp-in-file (file-relative-name
  16836. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16837. (ignore (write-region latex-frag nil tmp-in-file))
  16838. (tmp-out-file (file-relative-name
  16839. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16840. (cmd (format-spec
  16841. org-latex-to-mathml-convert-command
  16842. `((?j . ,(shell-quote-argument
  16843. (expand-file-name org-latex-to-mathml-jar-file)))
  16844. (?I . ,(shell-quote-argument tmp-in-file))
  16845. (?o . ,(shell-quote-argument tmp-out-file)))))
  16846. mathml shell-command-output)
  16847. (when (org-called-interactively-p 'any)
  16848. (unless (org-format-latex-mathml-available-p)
  16849. (error "LaTeX to MathML converter not configured")))
  16850. (message "Running %s" cmd)
  16851. (setq shell-command-output (shell-command-to-string cmd))
  16852. (setq mathml
  16853. (when (file-readable-p tmp-out-file)
  16854. (with-current-buffer (find-file-noselect tmp-out-file t)
  16855. (goto-char (point-min))
  16856. (when (re-search-forward
  16857. (concat
  16858. (regexp-quote
  16859. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16860. "\\(.\\|\n\\)*"
  16861. (regexp-quote "</math>")) nil t)
  16862. (prog1 (match-string 0) (kill-buffer))))))
  16863. (cond
  16864. (mathml
  16865. (setq mathml
  16866. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16867. (when mathml-file
  16868. (write-region mathml nil mathml-file))
  16869. (when (org-called-interactively-p 'any)
  16870. (message mathml)))
  16871. ((message "LaTeX to MathML conversion failed")
  16872. (message shell-command-output)))
  16873. (delete-file tmp-in-file)
  16874. (when (file-exists-p tmp-out-file)
  16875. (delete-file tmp-out-file))
  16876. mathml))
  16877. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16878. prefix &optional dir)
  16879. "Use `org-create-math-formula' but check local cache first."
  16880. (let* ((absprefix (expand-file-name prefix dir))
  16881. (print-length nil) (print-level nil)
  16882. (formula-id (concat
  16883. "formula-"
  16884. (sha1
  16885. (prin1-to-string
  16886. (list latex-frag
  16887. org-latex-to-mathml-convert-command)))))
  16888. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16889. (formula-cache-dir (file-name-directory formula-cache)))
  16890. (unless (file-directory-p formula-cache-dir)
  16891. (make-directory formula-cache-dir t))
  16892. (unless (file-exists-p formula-cache)
  16893. (org-create-math-formula latex-frag formula-cache))
  16894. (if (file-exists-p formula-cache)
  16895. ;; Successful conversion. Return the link to MathML file.
  16896. (org-add-props
  16897. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16898. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16899. 'org-latex-src-embed-type (if latex-frag-type
  16900. 'paragraph 'character)))
  16901. ;; Failed conversion. Return the LaTeX fragment verbatim
  16902. latex-frag)))
  16903. (defun org-create-formula-image (string tofile options buffer &optional type)
  16904. "Create an image from LaTeX source using dvipng or convert.
  16905. This function calls either `org-create-formula-image-with-dvipng'
  16906. or `org-create-formula-image-with-imagemagick' depending on the
  16907. value of `org-latex-create-formula-image-program' or on the value
  16908. of the optional TYPE variable.
  16909. Note: ultimately these two function should be combined as they
  16910. share a good deal of logic."
  16911. (org-check-external-command
  16912. "latex" "needed to convert LaTeX fragments to images")
  16913. (funcall
  16914. (case (or type org-latex-create-formula-image-program)
  16915. ('dvipng
  16916. (org-check-external-command
  16917. "dvipng" "needed to convert LaTeX fragments to images")
  16918. #'org-create-formula-image-with-dvipng)
  16919. ('imagemagick
  16920. (org-check-external-command
  16921. "convert" "you need to install imagemagick")
  16922. #'org-create-formula-image-with-imagemagick)
  16923. (t (error
  16924. "invalid value of `org-latex-create-formula-image-program'")))
  16925. string tofile options buffer))
  16926. (declare-function org-export--get-global-options "ox" (&optional backend))
  16927. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16928. (defun org-create-formula--latex-header ()
  16929. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16930. (org-latex-guess-inputenc
  16931. (org-splice-latex-header
  16932. org-format-latex-header
  16933. org-latex-default-packages-alist
  16934. org-latex-packages-alist t
  16935. (plist-get
  16936. (org-combine-plists
  16937. (org-export--get-global-options 'latex)
  16938. (org-export--get-inbuffer-options 'latex))
  16939. :latex-header))))
  16940. ;; This function borrows from Ganesh Swami's latex2png.el
  16941. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16942. "This calls dvipng."
  16943. (require 'ox-latex)
  16944. (let* ((tmpdir (if (featurep 'xemacs)
  16945. (temp-directory)
  16946. temporary-file-directory))
  16947. (texfilebase (make-temp-name
  16948. (expand-file-name "orgtex" tmpdir)))
  16949. (texfile (concat texfilebase ".tex"))
  16950. (dvifile (concat texfilebase ".dvi"))
  16951. (pngfile (concat texfilebase ".png"))
  16952. (fnh (if (featurep 'xemacs)
  16953. (font-height (face-font 'default))
  16954. (face-attribute 'default :height nil)))
  16955. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16956. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16957. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16958. "Black"))
  16959. (bg (or (plist-get options (if buffer :background :html-background))
  16960. "Transparent")))
  16961. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16962. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16963. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16964. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16965. (let ((latex-header (org-create-formula--latex-header)))
  16966. (with-temp-file texfile
  16967. (insert latex-header)
  16968. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16969. (let ((dir default-directory))
  16970. (condition-case nil
  16971. (progn
  16972. (cd tmpdir)
  16973. (call-process "latex" nil nil nil texfile))
  16974. (error nil))
  16975. (cd dir))
  16976. (if (not (file-exists-p dvifile))
  16977. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16978. (condition-case nil
  16979. (if (featurep 'xemacs)
  16980. (call-process "dvipng" nil nil nil
  16981. "-fg" fg "-bg" bg
  16982. "-T" "tight"
  16983. "-o" pngfile
  16984. dvifile)
  16985. (call-process "dvipng" nil nil nil
  16986. "-fg" fg "-bg" bg
  16987. "-D" dpi
  16988. ;;"-x" scale "-y" scale
  16989. "-T" "tight"
  16990. "-o" pngfile
  16991. dvifile))
  16992. (error nil))
  16993. (if (not (file-exists-p pngfile))
  16994. (if org-format-latex-signal-error
  16995. (error "Failed to create png file from %s" texfile)
  16996. (message "Failed to create png file from %s" texfile)
  16997. nil)
  16998. ;; Use the requested file name and clean up
  16999. (copy-file pngfile tofile 'replace)
  17000. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17001. (if (file-exists-p (concat texfilebase e))
  17002. (delete-file (concat texfilebase e))))
  17003. pngfile))))
  17004. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17005. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17006. "This calls convert, which is included into imagemagick."
  17007. (require 'ox-latex)
  17008. (let* ((tmpdir (if (featurep 'xemacs)
  17009. (temp-directory)
  17010. temporary-file-directory))
  17011. (texfilebase (make-temp-name
  17012. (expand-file-name "orgtex" tmpdir)))
  17013. (texfile (concat texfilebase ".tex"))
  17014. (pdffile (concat texfilebase ".pdf"))
  17015. (pngfile (concat texfilebase ".png"))
  17016. (fnh (if (featurep 'xemacs)
  17017. (font-height (face-font 'default))
  17018. (face-attribute 'default :height nil)))
  17019. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17020. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17021. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17022. "black"))
  17023. (bg (or (plist-get options (if buffer :background :html-background))
  17024. "white")))
  17025. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17026. (setq fg (org-latex-color-format fg)))
  17027. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17028. (setq bg (org-latex-color-format
  17029. (if (string= bg "Transparent") "white" bg))))
  17030. (let ((latex-header (org-create-formula--latex-header)))
  17031. (with-temp-file texfile
  17032. (insert latex-header)
  17033. (insert "\n\\begin{document}\n"
  17034. "\\definecolor{fg}{rgb}{" fg "}\n"
  17035. "\\definecolor{bg}{rgb}{" bg "}\n"
  17036. "\n\\pagecolor{bg}\n"
  17037. "\n{\\color{fg}\n"
  17038. string
  17039. "\n}\n"
  17040. "\n\\end{document}\n")))
  17041. (org-latex-compile texfile t)
  17042. (if (not (file-exists-p pdffile))
  17043. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17044. (condition-case nil
  17045. (if (featurep 'xemacs)
  17046. (call-process "convert" nil nil nil
  17047. "-density" "96"
  17048. "-trim"
  17049. "-antialias"
  17050. pdffile
  17051. "-quality" "100"
  17052. ;; "-sharpen" "0x1.0"
  17053. pngfile)
  17054. (call-process "convert" nil nil nil
  17055. "-density" dpi
  17056. "-trim"
  17057. "-antialias"
  17058. pdffile
  17059. "-quality" "100"
  17060. ;; "-sharpen" "0x1.0"
  17061. pngfile))
  17062. (error nil))
  17063. (if (not (file-exists-p pngfile))
  17064. (if org-format-latex-signal-error
  17065. (error "Failed to create png file from %s" texfile)
  17066. (message "Failed to create png file from %s" texfile)
  17067. nil)
  17068. ;; Use the requested file name and clean up
  17069. (copy-file pngfile tofile 'replace)
  17070. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17071. (if (file-exists-p (concat texfilebase e))
  17072. (delete-file (concat texfilebase e))))
  17073. pngfile))))
  17074. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17075. "Fill a LaTeX header template TPL.
  17076. In the template, the following place holders will be recognized:
  17077. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17078. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17079. [PACKAGES] \\usepackage statements for PKG
  17080. [NO-PACKAGES] do not include PKG
  17081. [EXTRA] the string EXTRA
  17082. [NO-EXTRA] do not include EXTRA
  17083. For backward compatibility, if both the positive and the negative place
  17084. holder is missing, the positive one (without the \"NO-\") will be
  17085. assumed to be present at the end of the template.
  17086. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17087. EXTRA is a string.
  17088. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17089. (let (rpl (end ""))
  17090. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17091. (setq rpl (if (or (match-end 1) (not def-pkg))
  17092. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17093. tpl (replace-match rpl t t tpl))
  17094. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17095. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17096. (setq rpl (if (or (match-end 1) (not pkg))
  17097. "" (org-latex-packages-to-string pkg snippets-p t))
  17098. tpl (replace-match rpl t t tpl))
  17099. (if pkg (setq end
  17100. (concat end "\n"
  17101. (org-latex-packages-to-string pkg snippets-p)))))
  17102. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17103. (setq rpl (if (or (match-end 1) (not extra))
  17104. "" (concat extra "\n"))
  17105. tpl (replace-match rpl t t tpl))
  17106. (if (and extra (string-match "\\S-" extra))
  17107. (setq end (concat end "\n" extra))))
  17108. (if (string-match "\\S-" end)
  17109. (concat tpl "\n" end)
  17110. tpl)))
  17111. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17112. "Turn an alist of packages into a string with the \\usepackage macros."
  17113. (setq pkg (mapconcat (lambda(p)
  17114. (cond
  17115. ((stringp p) p)
  17116. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17117. (format "%% Package %s omitted" (cadr p)))
  17118. ((equal "" (car p))
  17119. (format "\\usepackage{%s}" (cadr p)))
  17120. (t
  17121. (format "\\usepackage[%s]{%s}"
  17122. (car p) (cadr p)))))
  17123. pkg
  17124. "\n"))
  17125. (if newline (concat pkg "\n") pkg))
  17126. (defun org-dvipng-color (attr)
  17127. "Return a RGB color specification for dvipng."
  17128. (apply 'format "rgb %s %s %s"
  17129. (mapcar 'org-normalize-color
  17130. (if (featurep 'xemacs)
  17131. (color-rgb-components
  17132. (face-property 'default
  17133. (cond ((eq attr :foreground) 'foreground)
  17134. ((eq attr :background) 'background))))
  17135. (color-values (face-attribute 'default attr nil))))))
  17136. (defun org-dvipng-color-format (color-name)
  17137. "Convert COLOR-NAME to a RGB color value for dvipng."
  17138. (apply 'format "rgb %s %s %s"
  17139. (mapcar 'org-normalize-color
  17140. (color-values color-name))))
  17141. (defun org-latex-color (attr)
  17142. "Return a RGB color for the LaTeX color package."
  17143. (apply 'format "%s,%s,%s"
  17144. (mapcar 'org-normalize-color
  17145. (if (featurep 'xemacs)
  17146. (color-rgb-components
  17147. (face-property 'default
  17148. (cond ((eq attr :foreground) 'foreground)
  17149. ((eq attr :background) 'background))))
  17150. (color-values (face-attribute 'default attr nil))))))
  17151. (defun org-latex-color-format (color-name)
  17152. "Convert COLOR-NAME to a RGB color value."
  17153. (apply 'format "%s,%s,%s"
  17154. (mapcar 'org-normalize-color
  17155. (color-values color-name))))
  17156. (defun org-normalize-color (value)
  17157. "Return string to be used as color value for an RGB component."
  17158. (format "%g" (/ value 65535.0)))
  17159. ;; Image display
  17160. (defvar org-inline-image-overlays nil)
  17161. (make-variable-buffer-local 'org-inline-image-overlays)
  17162. (defun org-toggle-inline-images (&optional include-linked)
  17163. "Toggle the display of inline images.
  17164. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17165. (interactive "P")
  17166. (if org-inline-image-overlays
  17167. (progn
  17168. (org-remove-inline-images)
  17169. (message "Inline image display turned off"))
  17170. (org-display-inline-images include-linked)
  17171. (if (and (org-called-interactively-p)
  17172. org-inline-image-overlays)
  17173. (message "%d images displayed inline"
  17174. (length org-inline-image-overlays))
  17175. (message "No images to display inline"))))
  17176. (defun org-redisplay-inline-images ()
  17177. "Refresh the display of inline images."
  17178. (interactive)
  17179. (if (not org-inline-image-overlays)
  17180. (org-toggle-inline-images)
  17181. (org-toggle-inline-images)
  17182. (org-toggle-inline-images)))
  17183. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17184. "Display inline images.
  17185. Normally only links without a description part are inlined, because this
  17186. is how it will work for export. When INCLUDE-LINKED is set, also links
  17187. with a description part will be inlined. This can be nice for a quick
  17188. look at those images, but it does not reflect what exported files will look
  17189. like.
  17190. When REFRESH is set, refresh existing images between BEG and END.
  17191. This will create new image displays only if necessary.
  17192. BEG and END default to the buffer boundaries."
  17193. (interactive "P")
  17194. (unless refresh
  17195. (org-remove-inline-images)
  17196. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17197. (save-excursion
  17198. (save-restriction
  17199. (widen)
  17200. (setq beg (or beg (point-min)) end (or end (point-max)))
  17201. (goto-char beg)
  17202. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17203. (substring (org-image-file-name-regexp) 0 -2)
  17204. "\\)\\]" (if include-linked "" "\\]")))
  17205. (case-fold-search t)
  17206. old file ov img type attrwidth width)
  17207. (while (re-search-forward re end t)
  17208. (setq old (get-char-property-and-overlay (match-beginning 1)
  17209. 'org-image-overlay)
  17210. file (expand-file-name
  17211. (concat (or (match-string 3) "") (match-string 4))))
  17212. (when (image-type-available-p 'imagemagick)
  17213. (setq attrwidth (if (or (listp org-image-actual-width)
  17214. (null org-image-actual-width))
  17215. (save-excursion
  17216. (save-match-data
  17217. (when (re-search-backward
  17218. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17219. (save-excursion
  17220. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17221. (string-to-number (match-string 1))))))
  17222. width (cond ((eq org-image-actual-width t) nil)
  17223. ((null org-image-actual-width) attrwidth)
  17224. ((numberp org-image-actual-width)
  17225. org-image-actual-width)
  17226. ((listp org-image-actual-width)
  17227. (or attrwidth (car org-image-actual-width))))
  17228. type (if width 'imagemagick)))
  17229. (when (file-exists-p file)
  17230. (if (and (car-safe old) refresh)
  17231. (image-refresh (overlay-get (cdr old) 'display))
  17232. (setq img (save-match-data (create-image file type nil :width width)))
  17233. (when img
  17234. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17235. (overlay-put ov 'display img)
  17236. (overlay-put ov 'face 'default)
  17237. (overlay-put ov 'org-image-overlay t)
  17238. (overlay-put ov 'modification-hooks
  17239. (list 'org-display-inline-remove-overlay))
  17240. (push ov org-inline-image-overlays)))))))))
  17241. (define-obsolete-function-alias
  17242. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17243. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17244. "Remove inline-display overlay if a corresponding region is modified."
  17245. (let ((inhibit-modification-hooks t))
  17246. (when (and ov after)
  17247. (delete ov org-inline-image-overlays)
  17248. (delete-overlay ov))))
  17249. (defun org-remove-inline-images ()
  17250. "Remove inline display of images."
  17251. (interactive)
  17252. (mapc 'delete-overlay org-inline-image-overlays)
  17253. (setq org-inline-image-overlays nil))
  17254. ;;;; Key bindings
  17255. ;; Outline functions from `outline-mode-prefix-map'
  17256. ;; that can be remapped in Org:
  17257. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17258. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17259. (define-key org-mode-map [remap outline-forward-same-level]
  17260. 'org-forward-heading-same-level)
  17261. (define-key org-mode-map [remap outline-backward-same-level]
  17262. 'org-backward-heading-same-level)
  17263. (define-key org-mode-map [remap show-branches]
  17264. 'org-kill-note-or-show-branches)
  17265. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17266. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17267. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17268. ;; Outline functions from `outline-mode-prefix-map' that can not
  17269. ;; be remapped in Org:
  17270. ;;
  17271. ;; - the column "key binding" shows whether the Outline function is still
  17272. ;; available in Org mode on the same key that it has been bound to in
  17273. ;; Outline mode:
  17274. ;; - "overridden": key used for a different functionality in Org mode
  17275. ;; - else: key still bound to the same Outline function in Org mode
  17276. ;;
  17277. ;; | Outline function | key binding | Org replacement |
  17278. ;; |------------------------------------+-------------+-----------------------|
  17279. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17280. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17281. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17282. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17283. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17284. ;; | `show-entry' | overridden | no replacement |
  17285. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17286. ;; | `show-branches' | `C-c C-k' | still same function |
  17287. ;; | `show-subtree' | overridden | visibility cycling |
  17288. ;; | `show-all' | overridden | no replacement |
  17289. ;; | `hide-subtree' | overridden | visibility cycling |
  17290. ;; | `hide-body' | overridden | no replacement |
  17291. ;; | `hide-entry' | overridden | visibility cycling |
  17292. ;; | `hide-leaves' | overridden | no replacement |
  17293. ;; | `hide-sublevels' | overridden | no replacement |
  17294. ;; | `hide-other' | overridden | no replacement |
  17295. ;; Make `C-c C-x' a prefix key
  17296. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17297. ;; TAB key with modifiers
  17298. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17299. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17300. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17301. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17302. ;; The following line is necessary under Suse GNU/Linux
  17303. (unless (featurep 'xemacs)
  17304. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17305. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17306. (define-key org-mode-map [backtab] 'org-shifttab)
  17307. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17308. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17309. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17310. ;; Cursor keys with modifiers
  17311. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17312. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17313. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17314. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17315. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17316. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17317. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17318. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17319. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17320. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17321. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17322. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17323. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17324. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17325. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17326. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17327. ;; Babel keys
  17328. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17329. (mapc (lambda (pair)
  17330. (define-key org-babel-map (car pair) (cdr pair)))
  17331. org-babel-key-bindings)
  17332. ;;; Extra keys for tty access.
  17333. ;; We only set them when really needed because otherwise the
  17334. ;; menus don't show the simple keys
  17335. (when (or org-use-extra-keys
  17336. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17337. (not window-system))
  17338. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17339. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17340. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17341. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17342. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17343. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17344. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17345. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17346. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17347. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17348. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17349. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17350. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17351. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17352. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17353. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17354. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17355. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17356. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17357. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17358. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17359. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17360. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17361. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17362. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17363. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17364. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17365. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17366. ;; All the other keys
  17367. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17368. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17369. (if (boundp 'narrow-map)
  17370. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17371. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17372. (if (boundp 'narrow-map)
  17373. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17374. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17375. (if (boundp 'narrow-map)
  17376. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17377. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17378. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17379. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17380. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17381. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17382. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17383. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17384. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17385. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17386. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17387. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17388. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17389. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17390. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17391. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17392. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17393. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17394. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17395. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17396. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17397. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17398. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17399. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17400. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17401. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17402. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17403. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17404. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17405. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17406. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17407. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17408. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17409. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17410. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17411. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17412. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17413. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17414. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17415. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17416. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17417. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17418. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17419. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17420. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17421. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17422. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17423. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17424. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17425. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17426. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17427. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17428. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17429. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17430. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17431. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17432. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17433. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17434. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17435. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17436. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17437. (org-defkey org-mode-map "\C-o" 'org-ctrl-o)
  17438. (org-defkey org-mode-map "\C-m" 'org-return)
  17439. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17440. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17441. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17442. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17443. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17444. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17445. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17446. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17447. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17448. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17449. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17450. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17451. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17452. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17453. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17454. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17455. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17456. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17457. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17458. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17459. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17460. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17461. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17462. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17463. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17464. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17465. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17466. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17467. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17468. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17469. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17470. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17471. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17472. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17473. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17474. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17475. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17476. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17477. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17478. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17479. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17480. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17481. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17482. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17483. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17484. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17485. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17486. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17487. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17488. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17489. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17490. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17491. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17492. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17493. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17494. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17495. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17496. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17497. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17498. (when (featurep 'xemacs)
  17499. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17500. (defconst org-speed-commands-default
  17501. '(
  17502. ("Outline Navigation")
  17503. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17504. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17505. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17506. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17507. ("F" . org-next-block)
  17508. ("B" . org-previous-block)
  17509. ("u" . (org-speed-move-safe 'outline-up-heading))
  17510. ("j" . org-goto)
  17511. ("g" . (org-refile t))
  17512. ("Outline Visibility")
  17513. ("c" . org-cycle)
  17514. ("C" . org-shifttab)
  17515. (" " . org-display-outline-path)
  17516. ("s" . org-narrow-to-subtree)
  17517. ("=" . org-columns)
  17518. ("Outline Structure Editing")
  17519. ("U" . org-shiftmetaup)
  17520. ("D" . org-shiftmetadown)
  17521. ("r" . org-metaright)
  17522. ("l" . org-metaleft)
  17523. ("R" . org-shiftmetaright)
  17524. ("L" . org-shiftmetaleft)
  17525. ("i" . (progn (forward-char 1) (call-interactively
  17526. 'org-insert-heading-respect-content)))
  17527. ("^" . org-sort)
  17528. ("w" . org-refile)
  17529. ("a" . org-archive-subtree-default-with-confirmation)
  17530. ("@" . org-mark-subtree)
  17531. ("#" . org-toggle-comment)
  17532. ("Clock Commands")
  17533. ("I" . org-clock-in)
  17534. ("O" . org-clock-out)
  17535. ("Meta Data Editing")
  17536. ("t" . org-todo)
  17537. ("," . (org-priority))
  17538. ("0" . (org-priority ?\ ))
  17539. ("1" . (org-priority ?A))
  17540. ("2" . (org-priority ?B))
  17541. ("3" . (org-priority ?C))
  17542. (":" . org-set-tags-command)
  17543. ("e" . org-set-effort)
  17544. ("E" . org-inc-effort)
  17545. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17546. (org-entry-put (point) "APPT_WARNTIME" m)))
  17547. ("Agenda Views etc")
  17548. ("v" . org-agenda)
  17549. ("/" . org-sparse-tree)
  17550. ("Misc")
  17551. ("o" . org-open-at-point)
  17552. ("?" . org-speed-command-help)
  17553. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17554. (">" . (org-agenda-remove-restriction-lock))
  17555. )
  17556. "The default speed commands.")
  17557. (defun org-print-speed-command (e)
  17558. (if (> (length (car e)) 1)
  17559. (progn
  17560. (princ "\n")
  17561. (princ (car e))
  17562. (princ "\n")
  17563. (princ (make-string (length (car e)) ?-))
  17564. (princ "\n"))
  17565. (princ (car e))
  17566. (princ " ")
  17567. (if (symbolp (cdr e))
  17568. (princ (symbol-name (cdr e)))
  17569. (prin1 (cdr e)))
  17570. (princ "\n")))
  17571. (defun org-speed-command-help ()
  17572. "Show the available speed commands."
  17573. (interactive)
  17574. (if (not org-use-speed-commands)
  17575. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17576. (with-output-to-temp-buffer "*Help*"
  17577. (princ "User-defined Speed commands\n===========================\n")
  17578. (mapc 'org-print-speed-command org-speed-commands-user)
  17579. (princ "\n")
  17580. (princ "Built-in Speed commands\n=======================\n")
  17581. (mapc 'org-print-speed-command org-speed-commands-default))
  17582. (with-current-buffer "*Help*"
  17583. (setq truncate-lines t))))
  17584. (defun org-speed-move-safe (cmd)
  17585. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17586. If not, return to the original position and throw an error."
  17587. (interactive)
  17588. (let ((pos (point)))
  17589. (call-interactively cmd)
  17590. (unless (and (bolp) (org-at-heading-p))
  17591. (goto-char pos)
  17592. (error "Boundary reached while executing %s" cmd))))
  17593. (defvar org-self-insert-command-undo-counter 0)
  17594. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17595. (defvar org-speed-command nil)
  17596. (define-obsolete-function-alias
  17597. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17598. (defun org-speed-command-activate (keys)
  17599. "Hook for activating single-letter speed commands.
  17600. `org-speed-commands-default' specifies a minimal command set.
  17601. Use `org-speed-commands-user' for further customization."
  17602. (when (or (and (bolp) (looking-at org-outline-regexp))
  17603. (and (functionp org-use-speed-commands)
  17604. (funcall org-use-speed-commands)))
  17605. (cdr (assoc keys (append org-speed-commands-user
  17606. org-speed-commands-default)))))
  17607. (define-obsolete-function-alias
  17608. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17609. (defun org-babel-speed-command-activate (keys)
  17610. "Hook for activating single-letter code block commands."
  17611. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17612. (cdr (assoc keys org-babel-key-bindings))))
  17613. (defcustom org-speed-command-hook
  17614. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17615. "Hook for activating speed commands at strategic locations.
  17616. Hook functions are called in sequence until a valid handler is
  17617. found.
  17618. Each hook takes a single argument, a user-pressed command key
  17619. which is also a `self-insert-command' from the global map.
  17620. Within the hook, examine the cursor position and the command key
  17621. and return nil or a valid handler as appropriate. Handler could
  17622. be one of an interactive command, a function, or a form.
  17623. Set `org-use-speed-commands' to non-nil value to enable this
  17624. hook. The default setting is `org-speed-command-activate'."
  17625. :group 'org-structure
  17626. :version "24.1"
  17627. :type 'hook)
  17628. (defun org-self-insert-command (N)
  17629. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17630. If the cursor is in a table looking at whitespace, the whitespace is
  17631. overwritten, and the table is not marked as requiring realignment."
  17632. (interactive "p")
  17633. (org-check-before-invisible-edit 'insert)
  17634. (cond
  17635. ((and org-use-speed-commands
  17636. (setq org-speed-command
  17637. (run-hook-with-args-until-success
  17638. 'org-speed-command-hook (this-command-keys))))
  17639. (cond
  17640. ((commandp org-speed-command)
  17641. (setq this-command org-speed-command)
  17642. (call-interactively org-speed-command))
  17643. ((functionp org-speed-command)
  17644. (funcall org-speed-command))
  17645. ((and org-speed-command (listp org-speed-command))
  17646. (eval org-speed-command))
  17647. (t (let (org-use-speed-commands)
  17648. (call-interactively 'org-self-insert-command)))))
  17649. ((and
  17650. (org-table-p)
  17651. (progn
  17652. ;; check if we blank the field, and if that triggers align
  17653. (and (featurep 'org-table) org-table-auto-blank-field
  17654. (member last-command
  17655. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17656. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17657. ;; got extra space, this field does not determine column width
  17658. (let (org-table-may-need-update) (org-table-blank-field))
  17659. ;; no extra space, this field may determine column width
  17660. (org-table-blank-field)))
  17661. t)
  17662. (eq N 1)
  17663. (looking-at "[^|\n]* |"))
  17664. (let (org-table-may-need-update)
  17665. (goto-char (1- (match-end 0)))
  17666. (backward-delete-char 1)
  17667. (goto-char (match-beginning 0))
  17668. (self-insert-command N)))
  17669. (t
  17670. (setq org-table-may-need-update t)
  17671. (self-insert-command N)
  17672. (org-fix-tags-on-the-fly)
  17673. (if org-self-insert-cluster-for-undo
  17674. (if (not (eq last-command 'org-self-insert-command))
  17675. (setq org-self-insert-command-undo-counter 1)
  17676. (if (>= org-self-insert-command-undo-counter 20)
  17677. (setq org-self-insert-command-undo-counter 1)
  17678. (and (> org-self-insert-command-undo-counter 0)
  17679. buffer-undo-list (listp buffer-undo-list)
  17680. (not (cadr buffer-undo-list)) ; remove nil entry
  17681. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17682. (setq org-self-insert-command-undo-counter
  17683. (1+ org-self-insert-command-undo-counter))))))))
  17684. (defun org-check-before-invisible-edit (kind)
  17685. "Check is editing if kind KIND would be dangerous with invisible text around.
  17686. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17687. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17688. (if (and org-catch-invisible-edits
  17689. (or (not (boundp 'visible-mode)) (not visible-mode))
  17690. (or (get-char-property (point) 'invisible)
  17691. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17692. ;; OK, we need to take a closer look
  17693. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17694. (invisible-before-point (if (bobp) nil (get-char-property
  17695. (1- (point)) 'invisible)))
  17696. (border-and-ok-direction
  17697. (or
  17698. ;; Check if we are acting predictably before invisible text
  17699. (and invisible-at-point (not invisible-before-point)
  17700. (memq kind '(insert delete-backward)))
  17701. ;; Check if we are acting predictably after invisible text
  17702. ;; This works not well, and I have turned it off. It seems
  17703. ;; better to always show and stop after invisible text.
  17704. ;; (and (not invisible-at-point) invisible-before-point
  17705. ;; (memq kind '(insert delete)))
  17706. )))
  17707. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17708. (memq invisible-before-point '(outline org-hide-block t)))
  17709. (if (eq org-catch-invisible-edits 'error)
  17710. (error "Editing in invisible areas is prohibited - make visible first"))
  17711. (if (and org-custom-properties-overlays
  17712. (y-or-n-p "Display invisible properties in this buffer? "))
  17713. (org-toggle-custom-properties-visibility)
  17714. ;; Make the area visible
  17715. (save-excursion
  17716. (if invisible-before-point
  17717. (goto-char (previous-single-char-property-change
  17718. (point) 'invisible)))
  17719. (org-cycle))
  17720. (cond
  17721. ((eq org-catch-invisible-edits 'show)
  17722. ;; That's it, we do the edit after showing
  17723. (message
  17724. "Unfolding invisible region around point before editing")
  17725. (sit-for 1))
  17726. ((and (eq org-catch-invisible-edits 'smart)
  17727. border-and-ok-direction)
  17728. (message "Unfolding invisible region around point before editing"))
  17729. (t
  17730. ;; Don't do the edit, make the user repeat it in full visibility
  17731. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17732. (defun org-fix-tags-on-the-fly ()
  17733. (when (and (equal (char-after (point-at-bol)) ?*)
  17734. (org-at-heading-p))
  17735. (org-align-tags-here org-tags-column)))
  17736. (defun org-delete-backward-char (N)
  17737. "Like `delete-backward-char', insert whitespace at field end in tables.
  17738. When deleting backwards, in tables this function will insert whitespace in
  17739. front of the next \"|\" separator, to keep the table aligned. The table will
  17740. still be marked for re-alignment if the field did fill the entire column,
  17741. because, in this case the deletion might narrow the column."
  17742. (interactive "p")
  17743. (save-match-data
  17744. (org-check-before-invisible-edit 'delete-backward)
  17745. (if (and (org-table-p)
  17746. (eq N 1)
  17747. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17748. (looking-at ".*?|"))
  17749. (let ((pos (point))
  17750. (noalign (looking-at "[^|\n\r]* |"))
  17751. (c org-table-may-need-update))
  17752. (backward-delete-char N)
  17753. (if (not overwrite-mode)
  17754. (progn
  17755. (skip-chars-forward "^|")
  17756. (insert " ")
  17757. (goto-char (1- pos))))
  17758. ;; noalign: if there were two spaces at the end, this field
  17759. ;; does not determine the width of the column.
  17760. (if noalign (setq org-table-may-need-update c)))
  17761. (backward-delete-char N)
  17762. (org-fix-tags-on-the-fly))))
  17763. (defun org-delete-char (N)
  17764. "Like `delete-char', but insert whitespace at field end in tables.
  17765. When deleting characters, in tables this function will insert whitespace in
  17766. front of the next \"|\" separator, to keep the table aligned. The table will
  17767. still be marked for re-alignment if the field did fill the entire column,
  17768. because, in this case the deletion might narrow the column."
  17769. (interactive "p")
  17770. (save-match-data
  17771. (org-check-before-invisible-edit 'delete)
  17772. (if (and (org-table-p)
  17773. (not (bolp))
  17774. (not (= (char-after) ?|))
  17775. (eq N 1))
  17776. (if (looking-at ".*?|")
  17777. (let ((pos (point))
  17778. (noalign (looking-at "[^|\n\r]* |"))
  17779. (c org-table-may-need-update))
  17780. (replace-match
  17781. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17782. (goto-char pos)
  17783. ;; noalign: if there were two spaces at the end, this field
  17784. ;; does not determine the width of the column.
  17785. (if noalign (setq org-table-may-need-update c)))
  17786. (delete-char N))
  17787. (delete-char N)
  17788. (org-fix-tags-on-the-fly))))
  17789. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17790. (put 'org-self-insert-command 'delete-selection t)
  17791. (put 'orgtbl-self-insert-command 'delete-selection t)
  17792. (put 'org-delete-char 'delete-selection 'supersede)
  17793. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17794. (put 'org-yank 'delete-selection 'yank)
  17795. ;; Make `flyspell-mode' delay after some commands
  17796. (put 'org-self-insert-command 'flyspell-delayed t)
  17797. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17798. (put 'org-delete-char 'flyspell-delayed t)
  17799. (put 'org-delete-backward-char 'flyspell-delayed t)
  17800. ;; Make pabbrev-mode expand after org-mode commands
  17801. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17802. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17803. ;; How to do this: Measure non-white length of current string
  17804. ;; If equal to column width, we should realign.
  17805. (defun org-remap (map &rest commands)
  17806. "In MAP, remap the functions given in COMMANDS.
  17807. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17808. (let (new old)
  17809. (while commands
  17810. (setq old (pop commands) new (pop commands))
  17811. (if (fboundp 'command-remapping)
  17812. (org-defkey map (vector 'remap old) new)
  17813. (substitute-key-definition old new map global-map)))))
  17814. (defun org-transpose-words ()
  17815. "Transpose words for Org.
  17816. This uses the `org-mode-transpose-word-syntax-table' syntax
  17817. table, which interprets characters in `org-emphasis-alist' as
  17818. word constituants."
  17819. (interactive)
  17820. (with-syntax-table org-mode-transpose-word-syntax-table
  17821. (call-interactively 'transpose-words)))
  17822. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17823. (when (eq org-enable-table-editor 'optimized)
  17824. ;; If the user wants maximum table support, we need to hijack
  17825. ;; some standard editing functions
  17826. (org-remap org-mode-map
  17827. 'self-insert-command 'org-self-insert-command
  17828. 'delete-char 'org-delete-char
  17829. 'delete-backward-char 'org-delete-backward-char)
  17830. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17831. (defvar org-ctrl-c-ctrl-c-hook nil
  17832. "Hook for functions attaching themselves to `C-c C-c'.
  17833. This can be used to add additional functionality to the C-c C-c
  17834. key which executes context-dependent commands. This hook is run
  17835. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17836. run after the last test.
  17837. Each function will be called with no arguments. The function
  17838. must check if the context is appropriate for it to act. If yes,
  17839. it should do its thing and then return a non-nil value. If the
  17840. context is wrong, just do nothing and return nil.")
  17841. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17842. "Hook for functions attaching themselves to `C-c C-c'.
  17843. This can be used to add additional functionality to the C-c C-c
  17844. key which executes context-dependent commands. This hook is run
  17845. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17846. before the first test.
  17847. Each function will be called with no arguments. The function
  17848. must check if the context is appropriate for it to act. If yes,
  17849. it should do its thing and then return a non-nil value. If the
  17850. context is wrong, just do nothing and return nil.")
  17851. (defvar org-tab-first-hook nil
  17852. "Hook for functions to attach themselves to TAB.
  17853. See `org-ctrl-c-ctrl-c-hook' for more information.
  17854. This hook runs as the first action when TAB is pressed, even before
  17855. `org-cycle' messes around with the `outline-regexp' to cater for
  17856. inline tasks and plain list item folding.
  17857. If any function in this hook returns t, any other actions that
  17858. would have been caused by TAB (such as table field motion or visibility
  17859. cycling) will not occur.")
  17860. (defvar org-tab-after-check-for-table-hook nil
  17861. "Hook for functions to attach themselves to TAB.
  17862. See `org-ctrl-c-ctrl-c-hook' for more information.
  17863. This hook runs after it has been established that the cursor is not in a
  17864. table, but before checking if the cursor is in a headline or if global cycling
  17865. should be done.
  17866. If any function in this hook returns t, not other actions like visibility
  17867. cycling will be done.")
  17868. (defvar org-tab-after-check-for-cycling-hook nil
  17869. "Hook for functions to attach themselves to TAB.
  17870. See `org-ctrl-c-ctrl-c-hook' for more information.
  17871. This hook runs after it has been established that not table field motion and
  17872. not visibility should be done because of current context. This is probably
  17873. the place where a package like yasnippets can hook in.")
  17874. (defvar org-tab-before-tab-emulation-hook nil
  17875. "Hook for functions to attach themselves to TAB.
  17876. See `org-ctrl-c-ctrl-c-hook' for more information.
  17877. This hook runs after every other options for TAB have been exhausted, but
  17878. before indentation and \t insertion takes place.")
  17879. (defvar org-metaleft-hook nil
  17880. "Hook for functions attaching themselves to `M-left'.
  17881. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17882. (defvar org-metaright-hook nil
  17883. "Hook for functions attaching themselves to `M-right'.
  17884. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17885. (defvar org-metaup-hook nil
  17886. "Hook for functions attaching themselves to `M-up'.
  17887. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17888. (defvar org-metadown-hook nil
  17889. "Hook for functions attaching themselves to `M-down'.
  17890. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17891. (defvar org-shiftmetaleft-hook nil
  17892. "Hook for functions attaching themselves to `M-S-left'.
  17893. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17894. (defvar org-shiftmetaright-hook nil
  17895. "Hook for functions attaching themselves to `M-S-right'.
  17896. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17897. (defvar org-shiftmetaup-hook nil
  17898. "Hook for functions attaching themselves to `M-S-up'.
  17899. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17900. (defvar org-shiftmetadown-hook nil
  17901. "Hook for functions attaching themselves to `M-S-down'.
  17902. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17903. (defvar org-metareturn-hook nil
  17904. "Hook for functions attaching themselves to `M-RET'.
  17905. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17906. (defvar org-shiftup-hook nil
  17907. "Hook for functions attaching themselves to `S-up'.
  17908. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17909. (defvar org-shiftup-final-hook nil
  17910. "Hook for functions attaching themselves to `S-up'.
  17911. This one runs after all other options except shift-select have been excluded.
  17912. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17913. (defvar org-shiftdown-hook nil
  17914. "Hook for functions attaching themselves to `S-down'.
  17915. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17916. (defvar org-shiftdown-final-hook nil
  17917. "Hook for functions attaching themselves to `S-down'.
  17918. This one runs after all other options except shift-select have been excluded.
  17919. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17920. (defvar org-shiftleft-hook nil
  17921. "Hook for functions attaching themselves to `S-left'.
  17922. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17923. (defvar org-shiftleft-final-hook nil
  17924. "Hook for functions attaching themselves to `S-left'.
  17925. This one runs after all other options except shift-select have been excluded.
  17926. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17927. (defvar org-shiftright-hook nil
  17928. "Hook for functions attaching themselves to `S-right'.
  17929. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17930. (defvar org-shiftright-final-hook nil
  17931. "Hook for functions attaching themselves to `S-right'.
  17932. This one runs after all other options except shift-select have been excluded.
  17933. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17934. (defun org-modifier-cursor-error ()
  17935. "Throw an error, a modified cursor command was applied in wrong context."
  17936. (error "This command is active in special context like tables, headlines or items"))
  17937. (defun org-shiftselect-error ()
  17938. "Throw an error because Shift-Cursor command was applied in wrong context."
  17939. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17940. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17941. (error "This command works only in special context like headlines or timestamps")))
  17942. (defun org-call-for-shift-select (cmd)
  17943. (let ((this-command-keys-shift-translated t))
  17944. (call-interactively cmd)))
  17945. (defun org-shifttab (&optional arg)
  17946. "Global visibility cycling or move to previous table field.
  17947. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17948. on context.
  17949. See the individual commands for more information."
  17950. (interactive "P")
  17951. (cond
  17952. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17953. ((integerp arg)
  17954. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17955. (message "Content view to level: %d" arg)
  17956. (org-content (prefix-numeric-value arg2))
  17957. (setq org-cycle-global-status 'overview)))
  17958. (t (call-interactively 'org-global-cycle))))
  17959. (defun org-shiftmetaleft ()
  17960. "Promote subtree or delete table column.
  17961. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17962. `org-table-delete-column', depending on context. See the
  17963. individual commands for more information."
  17964. (interactive)
  17965. (cond
  17966. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17967. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17968. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17969. ((if (not (org-region-active-p)) (org-at-item-p)
  17970. (save-excursion (goto-char (region-beginning))
  17971. (org-at-item-p)))
  17972. (call-interactively 'org-outdent-item-tree))
  17973. (t (org-modifier-cursor-error))))
  17974. (defun org-shiftmetaright ()
  17975. "Demote subtree or insert table column.
  17976. Calls `org-demote-subtree', `org-indent-item-tree', or
  17977. `org-table-insert-column', depending on context. See the
  17978. individual commands for more information."
  17979. (interactive)
  17980. (cond
  17981. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17982. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17983. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17984. ((if (not (org-region-active-p)) (org-at-item-p)
  17985. (save-excursion (goto-char (region-beginning))
  17986. (org-at-item-p)))
  17987. (call-interactively 'org-indent-item-tree))
  17988. (t (org-modifier-cursor-error))))
  17989. (defun org-shiftmetaup (&optional arg)
  17990. "Move subtree up or kill table row.
  17991. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17992. `org-move-item-up' or `org-timestamp-up', depending on context.
  17993. See the individual commands for more information."
  17994. (interactive "P")
  17995. (cond
  17996. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17997. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17998. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17999. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18000. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18001. (call-interactively 'org-timestamp-up)))
  18002. (t (call-interactively 'org-drag-line-backward))))
  18003. (defun org-shiftmetadown (&optional arg)
  18004. "Move subtree down or insert table row.
  18005. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18006. `org-move-item-down' or `org-timestamp-up', depending on context.
  18007. See the individual commands for more information."
  18008. (interactive "P")
  18009. (cond
  18010. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18011. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18012. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18013. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18014. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18015. (call-interactively 'org-timestamp-down)))
  18016. (t (call-interactively 'org-drag-line-forward))))
  18017. (defsubst org-hidden-tree-error ()
  18018. (error
  18019. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18020. (defun org-metaleft (&optional arg)
  18021. "Promote heading or move table column to left.
  18022. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18023. With no specific context, calls the Emacs default `backward-word'.
  18024. See the individual commands for more information."
  18025. (interactive "P")
  18026. (cond
  18027. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18028. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18029. ((org-with-limited-levels
  18030. (or (org-at-heading-p)
  18031. (and (org-region-active-p)
  18032. (save-excursion
  18033. (goto-char (region-beginning))
  18034. (org-at-heading-p)))))
  18035. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18036. (call-interactively 'org-do-promote))
  18037. ;; At an inline task.
  18038. ((org-at-heading-p)
  18039. (call-interactively 'org-inlinetask-promote))
  18040. ((or (org-at-item-p)
  18041. (and (org-region-active-p)
  18042. (save-excursion
  18043. (goto-char (region-beginning))
  18044. (org-at-item-p))))
  18045. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18046. (call-interactively 'org-outdent-item))
  18047. (t (call-interactively 'backward-word))))
  18048. (defun org-metaright (&optional arg)
  18049. "Demote a subtree, a list item or move table column to right.
  18050. In front of a drawer or a block keyword, indent it correctly.
  18051. With no specific context, calls the Emacs default `forward-word'.
  18052. See the individual commands for more information."
  18053. (interactive "P")
  18054. (cond
  18055. ((run-hook-with-args-until-success 'org-metaright-hook))
  18056. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18057. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18058. ((org-at-block-p) (call-interactively 'org-indent-block))
  18059. ((org-with-limited-levels
  18060. (or (org-at-heading-p)
  18061. (and (org-region-active-p)
  18062. (save-excursion
  18063. (goto-char (region-beginning))
  18064. (org-at-heading-p)))))
  18065. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18066. (call-interactively 'org-do-demote))
  18067. ;; At an inline task.
  18068. ((org-at-heading-p)
  18069. (call-interactively 'org-inlinetask-demote))
  18070. ((or (org-at-item-p)
  18071. (and (org-region-active-p)
  18072. (save-excursion
  18073. (goto-char (region-beginning))
  18074. (org-at-item-p))))
  18075. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18076. (call-interactively 'org-indent-item))
  18077. (t (call-interactively 'forward-word))))
  18078. (defun org-check-for-hidden (what)
  18079. "Check if there are hidden headlines/items in the current visual line.
  18080. WHAT can be either `headlines' or `items'. If the current line is
  18081. an outline or item heading and it has a folded subtree below it,
  18082. this function returns t, nil otherwise."
  18083. (let ((re (cond
  18084. ((eq what 'headlines) org-outline-regexp-bol)
  18085. ((eq what 'items) (org-item-beginning-re))
  18086. (t (error "This should not happen"))))
  18087. beg end)
  18088. (save-excursion
  18089. (catch 'exit
  18090. (unless (org-region-active-p)
  18091. (setq beg (point-at-bol))
  18092. (beginning-of-line 2)
  18093. (while (and (not (eobp)) ;; this is like `next-line'
  18094. (get-char-property (1- (point)) 'invisible))
  18095. (beginning-of-line 2))
  18096. (setq end (point))
  18097. (goto-char beg)
  18098. (goto-char (point-at-eol))
  18099. (setq end (max end (point)))
  18100. (while (re-search-forward re end t)
  18101. (if (get-char-property (match-beginning 0) 'invisible)
  18102. (throw 'exit t))))
  18103. nil))))
  18104. (defun org-metaup (&optional arg)
  18105. "Move subtree up or move table row up.
  18106. Calls `org-move-subtree-up' or `org-table-move-row' or
  18107. `org-move-item-up', depending on context. See the individual commands
  18108. for more information."
  18109. (interactive "P")
  18110. (cond
  18111. ((run-hook-with-args-until-success 'org-metaup-hook))
  18112. ((org-region-active-p)
  18113. (let* ((a (min (region-beginning) (region-end)))
  18114. (b (1- (max (region-beginning) (region-end))))
  18115. (c (save-excursion (goto-char a)
  18116. (move-beginning-of-line 0)))
  18117. (d (save-excursion (goto-char a)
  18118. (move-end-of-line 0) (point))))
  18119. (transpose-regions a b c d)
  18120. (goto-char c)))
  18121. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18122. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18123. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18124. (t (org-drag-element-backward))))
  18125. (defun org-metadown (&optional arg)
  18126. "Move subtree down or move table row down.
  18127. Calls `org-move-subtree-down' or `org-table-move-row' or
  18128. `org-move-item-down', depending on context. See the individual
  18129. commands for more information."
  18130. (interactive "P")
  18131. (cond
  18132. ((run-hook-with-args-until-success 'org-metadown-hook))
  18133. ((org-region-active-p)
  18134. (let* ((a (min (region-beginning) (region-end)))
  18135. (b (max (region-beginning) (region-end)))
  18136. (c (save-excursion (goto-char b)
  18137. (move-beginning-of-line 1)))
  18138. (d (save-excursion (goto-char b)
  18139. (move-end-of-line 1) (1+ (point)))))
  18140. (transpose-regions a b c d)
  18141. (goto-char d)))
  18142. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18143. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18144. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18145. (t (org-drag-element-forward))))
  18146. (defun org-shiftup (&optional arg)
  18147. "Increase item in timestamp or increase priority of current headline.
  18148. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18149. depending on context. See the individual commands for more information."
  18150. (interactive "P")
  18151. (cond
  18152. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18153. ((and org-support-shift-select (org-region-active-p))
  18154. (org-call-for-shift-select 'previous-line))
  18155. ((org-at-timestamp-p t)
  18156. (call-interactively (if org-edit-timestamp-down-means-later
  18157. 'org-timestamp-down 'org-timestamp-up)))
  18158. ((and (not (eq org-support-shift-select 'always))
  18159. org-enable-priority-commands
  18160. (org-at-heading-p))
  18161. (call-interactively 'org-priority-up))
  18162. ((and (not org-support-shift-select) (org-at-item-p))
  18163. (call-interactively 'org-previous-item))
  18164. ((org-clocktable-try-shift 'up arg))
  18165. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18166. (org-support-shift-select
  18167. (org-call-for-shift-select 'previous-line))
  18168. (t (org-shiftselect-error))))
  18169. (defun org-shiftdown (&optional arg)
  18170. "Decrease item in timestamp or decrease priority of current headline.
  18171. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18172. depending on context. See the individual commands for more information."
  18173. (interactive "P")
  18174. (cond
  18175. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18176. ((and org-support-shift-select (org-region-active-p))
  18177. (org-call-for-shift-select 'next-line))
  18178. ((org-at-timestamp-p t)
  18179. (call-interactively (if org-edit-timestamp-down-means-later
  18180. 'org-timestamp-up 'org-timestamp-down)))
  18181. ((and (not (eq org-support-shift-select 'always))
  18182. org-enable-priority-commands
  18183. (org-at-heading-p))
  18184. (call-interactively 'org-priority-down))
  18185. ((and (not org-support-shift-select) (org-at-item-p))
  18186. (call-interactively 'org-next-item))
  18187. ((org-clocktable-try-shift 'down arg))
  18188. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18189. (org-support-shift-select
  18190. (org-call-for-shift-select 'next-line))
  18191. (t (org-shiftselect-error))))
  18192. (defun org-shiftright (&optional arg)
  18193. "Cycle the thing at point or in the current line, depending on context.
  18194. Depending on context, this does one of the following:
  18195. - switch a timestamp at point one day into the future
  18196. - on a headline, switch to the next TODO keyword.
  18197. - on an item, switch entire list to the next bullet type
  18198. - on a property line, switch to the next allowed value
  18199. - on a clocktable definition line, move time block into the future"
  18200. (interactive "P")
  18201. (cond
  18202. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18203. ((and org-support-shift-select (org-region-active-p))
  18204. (org-call-for-shift-select 'forward-char))
  18205. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18206. ((and (not (eq org-support-shift-select 'always))
  18207. (org-at-heading-p))
  18208. (let ((org-inhibit-logging
  18209. (not org-treat-S-cursor-todo-selection-as-state-change))
  18210. (org-inhibit-blocking
  18211. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18212. (org-call-with-arg 'org-todo 'right)))
  18213. ((or (and org-support-shift-select
  18214. (not (eq org-support-shift-select 'always))
  18215. (org-at-item-bullet-p))
  18216. (and (not org-support-shift-select) (org-at-item-p)))
  18217. (org-call-with-arg 'org-cycle-list-bullet nil))
  18218. ((and (not (eq org-support-shift-select 'always))
  18219. (org-at-property-p))
  18220. (call-interactively 'org-property-next-allowed-value))
  18221. ((org-clocktable-try-shift 'right arg))
  18222. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18223. (org-support-shift-select
  18224. (org-call-for-shift-select 'forward-char))
  18225. (t (org-shiftselect-error))))
  18226. (defun org-shiftleft (&optional arg)
  18227. "Cycle the thing at point or in the current line, depending on context.
  18228. Depending on context, this does one of the following:
  18229. - switch a timestamp at point one day into the past
  18230. - on a headline, switch to the previous TODO keyword.
  18231. - on an item, switch entire list to the previous bullet type
  18232. - on a property line, switch to the previous allowed value
  18233. - on a clocktable definition line, move time block into the past"
  18234. (interactive "P")
  18235. (cond
  18236. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18237. ((and org-support-shift-select (org-region-active-p))
  18238. (org-call-for-shift-select 'backward-char))
  18239. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18240. ((and (not (eq org-support-shift-select 'always))
  18241. (org-at-heading-p))
  18242. (let ((org-inhibit-logging
  18243. (not org-treat-S-cursor-todo-selection-as-state-change))
  18244. (org-inhibit-blocking
  18245. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18246. (org-call-with-arg 'org-todo 'left)))
  18247. ((or (and org-support-shift-select
  18248. (not (eq org-support-shift-select 'always))
  18249. (org-at-item-bullet-p))
  18250. (and (not org-support-shift-select) (org-at-item-p)))
  18251. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18252. ((and (not (eq org-support-shift-select 'always))
  18253. (org-at-property-p))
  18254. (call-interactively 'org-property-previous-allowed-value))
  18255. ((org-clocktable-try-shift 'left arg))
  18256. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18257. (org-support-shift-select
  18258. (org-call-for-shift-select 'backward-char))
  18259. (t (org-shiftselect-error))))
  18260. (defun org-shiftcontrolright ()
  18261. "Switch to next TODO set."
  18262. (interactive)
  18263. (cond
  18264. ((and org-support-shift-select (org-region-active-p))
  18265. (org-call-for-shift-select 'forward-word))
  18266. ((and (not (eq org-support-shift-select 'always))
  18267. (org-at-heading-p))
  18268. (org-call-with-arg 'org-todo 'nextset))
  18269. (org-support-shift-select
  18270. (org-call-for-shift-select 'forward-word))
  18271. (t (org-shiftselect-error))))
  18272. (defun org-shiftcontrolleft ()
  18273. "Switch to previous TODO set."
  18274. (interactive)
  18275. (cond
  18276. ((and org-support-shift-select (org-region-active-p))
  18277. (org-call-for-shift-select 'backward-word))
  18278. ((and (not (eq org-support-shift-select 'always))
  18279. (org-at-heading-p))
  18280. (org-call-with-arg 'org-todo 'previousset))
  18281. (org-support-shift-select
  18282. (org-call-for-shift-select 'backward-word))
  18283. (t (org-shiftselect-error))))
  18284. (defun org-shiftcontrolup (&optional n)
  18285. "Change timestamps synchronously up in CLOCK log lines.
  18286. Optional argument N tells to change by that many units."
  18287. (interactive "P")
  18288. (cond ((and (not org-support-shift-select)
  18289. (org-at-clock-log-p)
  18290. (org-at-timestamp-p t))
  18291. (org-clock-timestamps-up n))
  18292. (t (org-shiftselect-error))))
  18293. (defun org-shiftcontroldown (&optional n)
  18294. "Change timestamps synchronously down in CLOCK log lines.
  18295. Optional argument N tells to change by that many units."
  18296. (interactive "P")
  18297. (cond ((and (not org-support-shift-select)
  18298. (org-at-clock-log-p)
  18299. (org-at-timestamp-p t))
  18300. (org-clock-timestamps-down n))
  18301. (t (org-shiftselect-error))))
  18302. (defun org-ctrl-c-ret ()
  18303. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18304. (interactive)
  18305. (cond
  18306. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18307. (t (call-interactively 'org-insert-heading))))
  18308. (defun org-find-visible ()
  18309. (let ((s (point)))
  18310. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18311. (get-char-property s 'invisible)))
  18312. s))
  18313. (defun org-find-invisible ()
  18314. (let ((s (point)))
  18315. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18316. (not (get-char-property s 'invisible))))
  18317. s))
  18318. (defun org-copy-visible (beg end)
  18319. "Copy the visible parts of the region."
  18320. (interactive "r")
  18321. (let (snippets s)
  18322. (save-excursion
  18323. (save-restriction
  18324. (narrow-to-region beg end)
  18325. (setq s (goto-char (point-min)))
  18326. (while (not (= (point) (point-max)))
  18327. (goto-char (org-find-invisible))
  18328. (push (buffer-substring s (point)) snippets)
  18329. (setq s (goto-char (org-find-visible))))))
  18330. (kill-new (apply 'concat (nreverse snippets)))))
  18331. (defun org-copy-special ()
  18332. "Copy region in table or copy current subtree.
  18333. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18334. See the individual commands for more information."
  18335. (interactive)
  18336. (call-interactively
  18337. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18338. (defun org-cut-special ()
  18339. "Cut region in table or cut current subtree.
  18340. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18341. See the individual commands for more information."
  18342. (interactive)
  18343. (call-interactively
  18344. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18345. (defun org-paste-special (arg)
  18346. "Paste rectangular region into table, or past subtree relative to level.
  18347. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18348. See the individual commands for more information."
  18349. (interactive "P")
  18350. (if (org-at-table-p)
  18351. (org-table-paste-rectangle)
  18352. (org-paste-subtree arg)))
  18353. (defsubst org-in-fixed-width-region-p ()
  18354. "Is point in a fixed-width region?"
  18355. (save-match-data
  18356. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18357. (defun org-edit-special (&optional arg)
  18358. "Call a special editor for the element at point.
  18359. When at a table, call the formula editor with `org-table-edit-formulas'.
  18360. When in a source code block, call `org-edit-src-code'.
  18361. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18362. When at an #+INCLUDE keyword, visit the included file.
  18363. On a link, call `ffap' to visit the link at point.
  18364. Otherwise, return a user error."
  18365. (interactive "P")
  18366. (let ((element (org-element-at-point)))
  18367. (assert (not buffer-read-only) nil
  18368. "Buffer is read-only: %s" (buffer-name))
  18369. (case (org-element-type element)
  18370. (src-block
  18371. (if (not arg) (org-edit-src-code)
  18372. (let* ((info (org-babel-get-src-block-info))
  18373. (lang (nth 0 info))
  18374. (params (nth 2 info))
  18375. (session (cdr (assq :session params))))
  18376. (if (not session) (org-edit-src-code)
  18377. ;; At a src-block with a session and function called with
  18378. ;; an ARG: switch to the buffer related to the inferior
  18379. ;; process.
  18380. (switch-to-buffer
  18381. (funcall (intern (concat "org-babel-prep-session:" lang))
  18382. session params))))))
  18383. (keyword
  18384. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18385. (find-file
  18386. (org-remove-double-quotes
  18387. (car (org-split-string (org-element-property :value element)))))
  18388. (user-error "No special environment to edit here")))
  18389. (table
  18390. (if (eq (org-element-property :type element) 'table.el)
  18391. (org-edit-src-code)
  18392. (call-interactively 'org-table-edit-formulas)))
  18393. ;; Only Org tables contain `table-row' type elements.
  18394. (table-row (call-interactively 'org-table-edit-formulas))
  18395. ((example-block export-block) (org-edit-src-code))
  18396. (fixed-width (org-edit-fixed-width-region))
  18397. (otherwise
  18398. ;; No notable element at point. Though, we may be at a link,
  18399. ;; which is an object. Thus, scan deeper.
  18400. (if (eq (org-element-type (org-element-context element)) 'link)
  18401. (call-interactively 'ffap)
  18402. (user-error "No special environment to edit here"))))))
  18403. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18404. (defun org-ctrl-c-ctrl-c (&optional arg)
  18405. "Set tags in headline, or update according to changed information at point.
  18406. This command does many different things, depending on context:
  18407. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18408. this is what we do.
  18409. - If the cursor is on a statistics cookie, update it.
  18410. - If the cursor is in a headline, prompt for tags and insert them
  18411. into the current line, aligned to `org-tags-column'. When called
  18412. with prefix arg, realign all tags in the current buffer.
  18413. - If the cursor is in one of the special #+KEYWORD lines, this
  18414. triggers scanning the buffer for these lines and updating the
  18415. information.
  18416. - If the cursor is inside a table, realign the table. This command
  18417. works even if the automatic table editor has been turned off.
  18418. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18419. the entire table.
  18420. - If the cursor is at a footnote reference or definition, jump to
  18421. the corresponding definition or references, respectively.
  18422. - If the cursor is a the beginning of a dynamic block, update it.
  18423. - If the current buffer is a capture buffer, close note and file it.
  18424. - If the cursor is on a <<<target>>>, update radio targets and
  18425. corresponding links in this buffer.
  18426. - If the cursor is on a numbered item in a plain list, renumber the
  18427. ordered list.
  18428. - If the cursor is on a checkbox, toggle it.
  18429. - If the cursor is on a code block, evaluate it. The variable
  18430. `org-confirm-babel-evaluate' can be used to control prompting
  18431. before code block evaluation, by default every code block
  18432. evaluation requires confirmation. Code block evaluation can be
  18433. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18434. (interactive "P")
  18435. (cond
  18436. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18437. org-occur-highlights
  18438. org-latex-fragment-image-overlays)
  18439. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18440. (org-remove-occur-highlights)
  18441. (org-remove-latex-fragment-image-overlays)
  18442. (message "Temporary highlights/overlays removed from current buffer"))
  18443. ((and (local-variable-p 'org-finish-function (current-buffer))
  18444. (fboundp org-finish-function))
  18445. (funcall org-finish-function))
  18446. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18447. (t
  18448. (let* ((context (org-element-context)) (type (org-element-type context)))
  18449. ;; Test if point is within a blank line.
  18450. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18451. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18452. (user-error "C-c C-c can do nothing useful at this location"))
  18453. ;; For convenience: at the first line of a paragraph on the
  18454. ;; same line as an item, apply function on that item instead.
  18455. (when (eq type 'paragraph)
  18456. (let ((parent (org-element-property :parent context)))
  18457. (when (and (eq (org-element-type parent) 'item)
  18458. (= (point-at-bol) (org-element-property :begin parent)))
  18459. (setq context parent type 'item))))
  18460. ;; Act according to type of element or object at point.
  18461. (case type
  18462. (clock (org-clock-update-time-maybe))
  18463. (dynamic-block
  18464. (save-excursion
  18465. (goto-char (org-element-property :post-affiliated context))
  18466. (org-update-dblock)))
  18467. (footnote-definition
  18468. (goto-char (org-element-property :post-affiliated context))
  18469. (call-interactively 'org-footnote-action))
  18470. (footnote-reference (call-interactively 'org-footnote-action))
  18471. ((headline inlinetask)
  18472. (save-excursion (goto-char (org-element-property :begin context))
  18473. (call-interactively 'org-set-tags)))
  18474. (item
  18475. ;; At an item: a double C-u set checkbox to "[-]"
  18476. ;; unconditionally, whereas a single one will toggle its
  18477. ;; presence. Without an universal argument, if the item
  18478. ;; has a checkbox, toggle it. Otherwise repair the list.
  18479. (let* ((box (org-element-property :checkbox context))
  18480. (struct (org-element-property :structure context))
  18481. (old-struct (copy-tree struct))
  18482. (parents (org-list-parents-alist struct))
  18483. (prevs (org-list-prevs-alist struct))
  18484. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18485. (org-list-set-checkbox
  18486. (org-element-property :begin context) struct
  18487. (cond ((equal arg '(16)) "[-]")
  18488. ((and (not box) (equal arg '(4))) "[ ]")
  18489. ((or (not box) (equal arg '(4))) nil)
  18490. ((eq box 'on) "[ ]")
  18491. (t "[X]")))
  18492. ;; Mimic `org-list-write-struct' but with grabbing
  18493. ;; a return value from `org-list-struct-fix-box'.
  18494. (org-list-struct-fix-ind struct parents 2)
  18495. (org-list-struct-fix-item-end struct)
  18496. (org-list-struct-fix-bul struct prevs)
  18497. (org-list-struct-fix-ind struct parents)
  18498. (let ((block-item
  18499. (org-list-struct-fix-box struct parents prevs orderedp)))
  18500. (if (and box (equal struct old-struct))
  18501. (if (equal arg '(16))
  18502. (message "Checkboxes already reset")
  18503. (user-error "Cannot toggle this checkbox: %s"
  18504. (if (eq box 'on)
  18505. "all subitems checked"
  18506. "unchecked subitems")))
  18507. (org-list-struct-apply-struct struct old-struct)
  18508. (org-update-checkbox-count-maybe))
  18509. (when block-item
  18510. (message "Checkboxes were removed due to empty box at line %d"
  18511. (org-current-line block-item))))))
  18512. (keyword
  18513. (let ((org-inhibit-startup-visibility-stuff t)
  18514. (org-startup-align-all-tables nil))
  18515. (when (boundp 'org-table-coordinate-overlays)
  18516. (mapc 'delete-overlay org-table-coordinate-overlays)
  18517. (setq org-table-coordinate-overlays nil))
  18518. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18519. (message "Local setup has been refreshed"))
  18520. (plain-list
  18521. ;; At a plain list, with a double C-u argument, set
  18522. ;; checkboxes of each item to "[-]", whereas a single one
  18523. ;; will toggle their presence according to the state of the
  18524. ;; first item in the list. Without an argument, repair the
  18525. ;; list.
  18526. (let* ((begin (org-element-property :contents-begin context))
  18527. (beginm (move-marker (make-marker) begin))
  18528. (struct (org-element-property :structure context))
  18529. (old-struct (copy-tree struct))
  18530. (first-box (save-excursion
  18531. (goto-char begin)
  18532. (looking-at org-list-full-item-re)
  18533. (match-string-no-properties 3)))
  18534. (new-box (cond ((equal arg '(16)) "[-]")
  18535. ((equal arg '(4)) (unless first-box "[ ]"))
  18536. ((equal first-box "[X]") "[ ]")
  18537. (t "[X]"))))
  18538. (cond
  18539. (arg
  18540. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18541. (org-list-get-all-items
  18542. begin struct (org-list-prevs-alist struct))))
  18543. ((and first-box (eq (point) begin))
  18544. ;; For convenience, when point is at bol on the first
  18545. ;; item of the list and no argument is provided, simply
  18546. ;; toggle checkbox of that item, if any.
  18547. (org-list-set-checkbox begin struct new-box)))
  18548. (org-list-write-struct
  18549. struct (org-list-parents-alist struct) old-struct)
  18550. (org-update-checkbox-count-maybe)
  18551. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18552. ((property-drawer node-property)
  18553. (call-interactively 'org-property-action))
  18554. ((radio-target target)
  18555. (call-interactively 'org-update-radio-target-regexp))
  18556. (statistics-cookie
  18557. (call-interactively 'org-update-statistics-cookies))
  18558. ((table table-cell table-row)
  18559. ;; At a table, recalculate every field and align it. Also
  18560. ;; send the table if necessary. If the table has
  18561. ;; a `table.el' type, just give up. At a table row or
  18562. ;; cell, maybe recalculate line but always align table.
  18563. (if (eq (org-element-property :type context) 'table.el)
  18564. (message "Use C-c ' to edit table.el tables")
  18565. (let ((org-enable-table-editor t))
  18566. (if (or (eq type 'table)
  18567. ;; Check if point is at a TBLFM line.
  18568. (and (eq type 'table-row)
  18569. (= (point) (org-element-property :end context))))
  18570. (save-excursion
  18571. (if (org-at-TBLFM-p) (org-calc-current-TBLFM)
  18572. (goto-char (org-element-property :contents-begin context))
  18573. (org-call-with-arg 'org-table-recalculate (or arg t))
  18574. (orgtbl-send-table 'maybe)))
  18575. (org-table-maybe-eval-formula)
  18576. (cond (arg (call-interactively 'org-table-recalculate))
  18577. ((org-table-maybe-recalculate-line))
  18578. (t (org-table-align)))))))
  18579. (timestamp (org-timestamp-change 0 'day))
  18580. (otherwise
  18581. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18582. (user-error
  18583. "C-c C-c can do nothing useful at this location")))))))))
  18584. (defun org-mode-restart ()
  18585. "Restart Org-mode, to scan again for special lines.
  18586. Also updates the keyword regular expressions."
  18587. (interactive)
  18588. (org-mode)
  18589. (message "Org-mode restarted"))
  18590. (defun org-kill-note-or-show-branches ()
  18591. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18592. (interactive)
  18593. (if (not org-finish-function)
  18594. (progn
  18595. (hide-subtree)
  18596. (call-interactively 'show-branches))
  18597. (let ((org-note-abort t))
  18598. (funcall org-finish-function))))
  18599. (defun org-ctrl-o (n)
  18600. "Insert a new row in tables, call `open-line' elsewhere."
  18601. (interactive "*p")
  18602. (if (org-at-table-p)
  18603. (org-table-insert-row)
  18604. (open-line n)))
  18605. (defun org-return (&optional indent)
  18606. "Goto next table row or insert a newline.
  18607. Calls `org-table-next-row' or `newline', depending on context.
  18608. See the individual commands for more information."
  18609. (interactive)
  18610. (let (org-ts-what)
  18611. (cond
  18612. ((or (bobp) (org-in-src-block-p))
  18613. (if indent (newline-and-indent) (newline)))
  18614. ((org-at-table-p)
  18615. (org-table-justify-field-maybe)
  18616. (call-interactively 'org-table-next-row))
  18617. ;; when `newline-and-indent' is called within a list, make sure
  18618. ;; text moved stays inside the item.
  18619. ((and (org-in-item-p) indent)
  18620. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18621. (progn
  18622. (save-match-data (newline))
  18623. (org-indent-line-to (length (match-string 0))))
  18624. (let ((ind (org-get-indentation)))
  18625. (newline)
  18626. (if (org-looking-back org-list-end-re)
  18627. (org-indent-line)
  18628. (org-indent-line-to ind)))))
  18629. ((and org-return-follows-link
  18630. (org-at-timestamp-p t)
  18631. (not (eq org-ts-what 'after)))
  18632. (org-follow-timestamp-link))
  18633. ((and org-return-follows-link
  18634. (let ((tprop (get-text-property (point) 'face)))
  18635. (or (eq tprop 'org-link)
  18636. (and (listp tprop) (memq 'org-link tprop)))))
  18637. (call-interactively 'org-open-at-point))
  18638. ((and (org-at-heading-p)
  18639. (looking-at
  18640. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18641. (org-show-entry)
  18642. (end-of-line 1)
  18643. (newline))
  18644. (t (if indent (newline-and-indent) (newline))))))
  18645. (defun org-return-indent ()
  18646. "Goto next table row or insert a newline and indent.
  18647. Calls `org-table-next-row' or `newline-and-indent', depending on
  18648. context. See the individual commands for more information."
  18649. (interactive)
  18650. (org-return t))
  18651. (defun org-ctrl-c-star ()
  18652. "Compute table, or change heading status of lines.
  18653. Calls `org-table-recalculate' or `org-toggle-heading',
  18654. depending on context."
  18655. (interactive)
  18656. (cond
  18657. ((org-at-table-p)
  18658. (call-interactively 'org-table-recalculate))
  18659. (t
  18660. ;; Convert all lines in region to list items
  18661. (call-interactively 'org-toggle-heading))))
  18662. (defun org-ctrl-c-minus ()
  18663. "Insert separator line in table or modify bullet status of line.
  18664. Also turns a plain line or a region of lines into list items.
  18665. Calls `org-table-insert-hline', `org-toggle-item', or
  18666. `org-cycle-list-bullet', depending on context."
  18667. (interactive)
  18668. (cond
  18669. ((org-at-table-p)
  18670. (call-interactively 'org-table-insert-hline))
  18671. ((org-region-active-p)
  18672. (call-interactively 'org-toggle-item))
  18673. ((org-in-item-p)
  18674. (call-interactively 'org-cycle-list-bullet))
  18675. (t
  18676. (call-interactively 'org-toggle-item))))
  18677. (defun org-toggle-item (arg)
  18678. "Convert headings or normal lines to items, items to normal lines.
  18679. If there is no active region, only the current line is considered.
  18680. If the first non blank line in the region is a headline, convert
  18681. all headlines to items, shifting text accordingly.
  18682. If it is an item, convert all items to normal lines.
  18683. If it is normal text, change region into a list of items.
  18684. With a prefix argument ARG, change the region in a single item."
  18685. (interactive "P")
  18686. (let ((shift-text
  18687. (function
  18688. ;; Shift text in current section to IND, from point to END.
  18689. ;; The function leaves point to END line.
  18690. (lambda (ind end)
  18691. (let ((min-i 1000) (end (copy-marker end)))
  18692. ;; First determine the minimum indentation (MIN-I) of
  18693. ;; the text.
  18694. (save-excursion
  18695. (catch 'exit
  18696. (while (< (point) end)
  18697. (let ((i (org-get-indentation)))
  18698. (cond
  18699. ;; Skip blank lines and inline tasks.
  18700. ((looking-at "^[ \t]*$"))
  18701. ((looking-at org-outline-regexp-bol))
  18702. ;; We can't find less than 0 indentation.
  18703. ((zerop i) (throw 'exit (setq min-i 0)))
  18704. ((< i min-i) (setq min-i i))))
  18705. (forward-line))))
  18706. ;; Then indent each line so that a line indented to
  18707. ;; MIN-I becomes indented to IND. Ignore blank lines
  18708. ;; and inline tasks in the process.
  18709. (let ((delta (- ind min-i)))
  18710. (while (< (point) end)
  18711. (unless (or (looking-at "^[ \t]*$")
  18712. (looking-at org-outline-regexp-bol))
  18713. (org-indent-line-to (+ (org-get-indentation) delta)))
  18714. (forward-line)))))))
  18715. (skip-blanks
  18716. (function
  18717. ;; Return beginning of first non-blank line, starting from
  18718. ;; line at POS.
  18719. (lambda (pos)
  18720. (save-excursion
  18721. (goto-char pos)
  18722. (skip-chars-forward " \r\t\n")
  18723. (point-at-bol)))))
  18724. beg end)
  18725. ;; Determine boundaries of changes.
  18726. (if (org-region-active-p)
  18727. (setq beg (funcall skip-blanks (region-beginning))
  18728. end (copy-marker (region-end)))
  18729. (setq beg (funcall skip-blanks (point-at-bol))
  18730. end (copy-marker (point-at-eol))))
  18731. ;; Depending on the starting line, choose an action on the text
  18732. ;; between BEG and END.
  18733. (org-with-limited-levels
  18734. (save-excursion
  18735. (goto-char beg)
  18736. (cond
  18737. ;; Case 1. Start at an item: de-itemize. Note that it only
  18738. ;; happens when a region is active: `org-ctrl-c-minus'
  18739. ;; would call `org-cycle-list-bullet' otherwise.
  18740. ((org-at-item-p)
  18741. (while (< (point) end)
  18742. (when (org-at-item-p)
  18743. (skip-chars-forward " \t")
  18744. (delete-region (point) (match-end 0)))
  18745. (forward-line)))
  18746. ;; Case 2. Start at an heading: convert to items.
  18747. ((org-at-heading-p)
  18748. (let* ((bul (org-list-bullet-string "-"))
  18749. (bul-len (length bul))
  18750. ;; Indentation of the first heading. It should be
  18751. ;; relative to the indentation of its parent, if any.
  18752. (start-ind (save-excursion
  18753. (cond
  18754. ((not org-adapt-indentation) 0)
  18755. ((not (outline-previous-heading)) 0)
  18756. (t (length (match-string 0))))))
  18757. ;; Level of first heading. Further headings will be
  18758. ;; compared to it to determine hierarchy in the list.
  18759. (ref-level (org-reduced-level (org-outline-level))))
  18760. (while (< (point) end)
  18761. (let* ((level (org-reduced-level (org-outline-level)))
  18762. (delta (max 0 (- level ref-level))))
  18763. ;; If current headline is less indented than the first
  18764. ;; one, set it as reference, in order to preserve
  18765. ;; subtrees.
  18766. (when (< level ref-level) (setq ref-level level))
  18767. (replace-match bul t t)
  18768. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18769. ;; Ensure all text down to END (or SECTION-END) belongs
  18770. ;; to the newly created item.
  18771. (let ((section-end (save-excursion
  18772. (or (outline-next-heading) (point)))))
  18773. (forward-line)
  18774. (funcall shift-text
  18775. (+ start-ind (* (1+ delta) bul-len))
  18776. (min end section-end)))))))
  18777. ;; Case 3. Normal line with ARG: make the first line of region
  18778. ;; an item, and shift indentation of others lines to
  18779. ;; set them as item's body.
  18780. (arg (let* ((bul (org-list-bullet-string "-"))
  18781. (bul-len (length bul))
  18782. (ref-ind (org-get-indentation)))
  18783. (skip-chars-forward " \t")
  18784. (insert bul)
  18785. (forward-line)
  18786. (while (< (point) end)
  18787. ;; Ensure that lines less indented than first one
  18788. ;; still get included in item body.
  18789. (funcall shift-text
  18790. (+ ref-ind bul-len)
  18791. (min end (save-excursion (or (outline-next-heading)
  18792. (point)))))
  18793. (forward-line))))
  18794. ;; Case 4. Normal line without ARG: turn each non-item line
  18795. ;; into an item.
  18796. (t
  18797. (while (< (point) end)
  18798. (unless (or (org-at-heading-p) (org-at-item-p))
  18799. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18800. (replace-match
  18801. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18802. (forward-line))))))))
  18803. (defun org-toggle-heading (&optional nstars)
  18804. "Convert headings to normal text, or items or text to headings.
  18805. If there is no active region, only convert the current line.
  18806. With a \\[universal-argument] prefix, convert the whole list at
  18807. point into heading.
  18808. In a region:
  18809. - If the first non blank line is a headline, remove the stars
  18810. from all headlines in the region.
  18811. - If it is a normal line, turn each and every normal line (i.e.,
  18812. not an heading or an item) in the region into headings. If you
  18813. want to convert only the first line of this region, use one
  18814. universal prefix argument.
  18815. - If it is a plain list item, turn all plain list items into headings.
  18816. When converting a line into a heading, the number of stars is chosen
  18817. such that the lines become children of the current entry. However,
  18818. when a numeric prefix argument is given, its value determines the
  18819. number of stars to add."
  18820. (interactive "P")
  18821. (let ((skip-blanks
  18822. (function
  18823. ;; Return beginning of first non-blank line, starting from
  18824. ;; line at POS.
  18825. (lambda (pos)
  18826. (save-excursion
  18827. (goto-char pos)
  18828. (while (org-at-comment-p) (forward-line))
  18829. (skip-chars-forward " \r\t\n")
  18830. (point-at-bol)))))
  18831. beg end toggled)
  18832. ;; Determine boundaries of changes. If a universal prefix has
  18833. ;; been given, put the list in a region. If region ends at a bol,
  18834. ;; do not consider the last line to be in the region.
  18835. (when (and current-prefix-arg (org-at-item-p))
  18836. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18837. (org-mark-element))
  18838. (if (org-region-active-p)
  18839. (setq beg (funcall skip-blanks (region-beginning))
  18840. end (copy-marker (save-excursion
  18841. (goto-char (region-end))
  18842. (if (bolp) (point) (point-at-eol)))))
  18843. (setq beg (funcall skip-blanks (point-at-bol))
  18844. end (copy-marker (point-at-eol))))
  18845. ;; Ensure inline tasks don't count as headings.
  18846. (org-with-limited-levels
  18847. (save-excursion
  18848. (goto-char beg)
  18849. (cond
  18850. ;; Case 1. Started at an heading: de-star headings.
  18851. ((org-at-heading-p)
  18852. (while (< (point) end)
  18853. (when (org-at-heading-p t)
  18854. (looking-at org-outline-regexp) (replace-match "")
  18855. (setq toggled t))
  18856. (forward-line)))
  18857. ;; Case 2. Started at an item: change items into headlines.
  18858. ;; One star will be added by `org-list-to-subtree'.
  18859. ((org-at-item-p)
  18860. (let* ((stars (make-string
  18861. ;; subtract the star that will be added again by
  18862. ;; `org-list-to-subtree'
  18863. (if (numberp nstars) (1- nstars)
  18864. (or (org-current-level) 0))
  18865. ?*))
  18866. (add-stars
  18867. (cond (nstars "") ; stars from prefix only
  18868. ((equal stars "") "") ; before first heading
  18869. (org-odd-levels-only "*") ; inside heading, odd
  18870. (t "")))) ; inside heading, oddeven
  18871. (while (< (point) end)
  18872. (when (org-at-item-p)
  18873. ;; Pay attention to cases when region ends before list.
  18874. (let* ((struct (org-list-struct))
  18875. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18876. (save-restriction
  18877. (narrow-to-region (point) list-end)
  18878. (insert
  18879. (org-list-to-subtree
  18880. (org-list-parse-list t)
  18881. '(:istart (concat stars add-stars (funcall get-stars depth))
  18882. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18883. (setq toggled t))
  18884. (forward-line))))
  18885. ;; Case 3. Started at normal text: make every line an heading,
  18886. ;; skipping headlines and items.
  18887. (t (let* ((stars
  18888. (make-string
  18889. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18890. (add-stars
  18891. (cond (nstars "") ; stars from prefix only
  18892. ((equal stars "") "*") ; before first heading
  18893. (org-odd-levels-only "**") ; inside heading, odd
  18894. (t "*"))) ; inside heading, oddeven
  18895. (rpl (concat stars add-stars " "))
  18896. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18897. (while (< (point) (if (equal nstars '(4)) lend end))
  18898. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18899. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18900. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18901. (forward-line)))))))
  18902. (unless toggled (message "Cannot toggle heading from here"))))
  18903. (defun org-meta-return (&optional arg)
  18904. "Insert a new heading or wrap a region in a table.
  18905. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18906. See the individual commands for more information."
  18907. (interactive "P")
  18908. (cond
  18909. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18910. ((or (org-at-drawer-p) (org-at-property-p))
  18911. (newline-and-indent))
  18912. ((org-at-table-p)
  18913. (call-interactively 'org-table-wrap-region))
  18914. (t (call-interactively 'org-insert-heading))))
  18915. ;;; Menu entries
  18916. (defsubst org-in-subtree-not-table-p ()
  18917. "Are we in a subtree and not in a table?"
  18918. (and (not (org-before-first-heading-p))
  18919. (not (org-at-table-p))))
  18920. ;; Define the Org-mode menus
  18921. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18922. '("Tbl"
  18923. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18924. ["Next Field" org-cycle (org-at-table-p)]
  18925. ["Previous Field" org-shifttab (org-at-table-p)]
  18926. ["Next Row" org-return (org-at-table-p)]
  18927. "--"
  18928. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18929. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18930. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18931. "--"
  18932. ("Column"
  18933. ["Move Column Left" org-metaleft (org-at-table-p)]
  18934. ["Move Column Right" org-metaright (org-at-table-p)]
  18935. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18936. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18937. ("Row"
  18938. ["Move Row Up" org-metaup (org-at-table-p)]
  18939. ["Move Row Down" org-metadown (org-at-table-p)]
  18940. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18941. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18942. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18943. "--"
  18944. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18945. ("Rectangle"
  18946. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18947. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18948. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18949. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18950. "--"
  18951. ("Calculate"
  18952. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18953. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18954. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18955. "--"
  18956. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18957. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18958. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18959. "--"
  18960. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18961. "--"
  18962. ["Sum Column/Rectangle" org-table-sum
  18963. (or (org-at-table-p) (org-region-active-p))]
  18964. ["Which Column?" org-table-current-column (org-at-table-p)])
  18965. ["Debug Formulas"
  18966. org-table-toggle-formula-debugger
  18967. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18968. ["Show Col/Row Numbers"
  18969. org-table-toggle-coordinate-overlays
  18970. :style toggle
  18971. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18972. "--"
  18973. ["Create" org-table-create (and (not (org-at-table-p))
  18974. org-enable-table-editor)]
  18975. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18976. ["Import from File" org-table-import (not (org-at-table-p))]
  18977. ["Export to File" org-table-export (org-at-table-p)]
  18978. "--"
  18979. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18980. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18981. '("Org"
  18982. ("Show/Hide"
  18983. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18984. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18985. ["Sparse Tree..." org-sparse-tree t]
  18986. ["Reveal Context" org-reveal t]
  18987. ["Show All" show-all t]
  18988. "--"
  18989. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18990. "--"
  18991. ["New Heading" org-insert-heading t]
  18992. ("Navigate Headings"
  18993. ["Up" outline-up-heading t]
  18994. ["Next" outline-next-visible-heading t]
  18995. ["Previous" outline-previous-visible-heading t]
  18996. ["Next Same Level" outline-forward-same-level t]
  18997. ["Previous Same Level" outline-backward-same-level t]
  18998. "--"
  18999. ["Jump" org-goto t])
  19000. ("Edit Structure"
  19001. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19002. "--"
  19003. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19004. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19005. "--"
  19006. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19007. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19008. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19009. "--"
  19010. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19011. "--"
  19012. ["Copy visible text" org-copy-visible t]
  19013. "--"
  19014. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19015. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19016. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19017. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19018. "--"
  19019. ["Sort Region/Children" org-sort t]
  19020. "--"
  19021. ["Convert to odd levels" org-convert-to-odd-levels t]
  19022. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19023. ("Editing"
  19024. ["Emphasis..." org-emphasize t]
  19025. ["Edit Source Example" org-edit-special t]
  19026. "--"
  19027. ["Footnote new/jump" org-footnote-action t]
  19028. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19029. ("Archive"
  19030. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19031. "--"
  19032. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19033. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19034. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19035. )
  19036. "--"
  19037. ("Hyperlinks"
  19038. ["Store Link (Global)" org-store-link t]
  19039. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19040. ["Insert Link" org-insert-link t]
  19041. ["Follow Link" org-open-at-point t]
  19042. "--"
  19043. ["Next link" org-next-link t]
  19044. ["Previous link" org-previous-link t]
  19045. "--"
  19046. ["Descriptive Links"
  19047. org-toggle-link-display
  19048. :style radio
  19049. :selected org-descriptive-links
  19050. ]
  19051. ["Literal Links"
  19052. org-toggle-link-display
  19053. :style radio
  19054. :selected (not org-descriptive-links)])
  19055. "--"
  19056. ("TODO Lists"
  19057. ["TODO/DONE/-" org-todo t]
  19058. ("Select keyword"
  19059. ["Next keyword" org-shiftright (org-at-heading-p)]
  19060. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19061. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19062. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19063. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19064. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19065. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19066. "--"
  19067. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19068. :selected org-enforce-todo-dependencies :style toggle :active t]
  19069. "Settings for tree at point"
  19070. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19071. :selected (org-entry-get nil "ORDERED")
  19072. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19073. ["Do Children parallel" org-toggle-ordered-property :style radio
  19074. :selected (not (org-entry-get nil "ORDERED"))
  19075. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19076. "--"
  19077. ["Set Priority" org-priority t]
  19078. ["Priority Up" org-shiftup t]
  19079. ["Priority Down" org-shiftdown t]
  19080. "--"
  19081. ["Get news from all feeds" org-feed-update-all t]
  19082. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19083. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19084. ("TAGS and Properties"
  19085. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19086. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19087. "--"
  19088. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19089. ["Column view of properties" org-columns t]
  19090. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19091. ("Dates and Scheduling"
  19092. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19093. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19094. ("Change Date"
  19095. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19096. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19097. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19098. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19099. ["Compute Time Range" org-evaluate-time-range t]
  19100. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19101. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19102. "--"
  19103. ["Custom time format" org-toggle-time-stamp-overlays
  19104. :style radio :selected org-display-custom-times]
  19105. "--"
  19106. ["Goto Calendar" org-goto-calendar t]
  19107. ["Date from Calendar" org-date-from-calendar t]
  19108. "--"
  19109. ["Start/Restart Timer" org-timer-start t]
  19110. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19111. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19112. ["Insert Timer String" org-timer t]
  19113. ["Insert Timer Item" org-timer-item t])
  19114. ("Logging work"
  19115. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19116. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19117. ["Clock out" org-clock-out t]
  19118. ["Clock cancel" org-clock-cancel t]
  19119. "--"
  19120. ["Mark as default task" org-clock-mark-default-task t]
  19121. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19122. ["Goto running clock" org-clock-goto t]
  19123. "--"
  19124. ["Display times" org-clock-display t]
  19125. ["Create clock table" org-clock-report t]
  19126. "--"
  19127. ["Record DONE time"
  19128. (progn (setq org-log-done (not org-log-done))
  19129. (message "Switching to %s will %s record a timestamp"
  19130. (car org-done-keywords)
  19131. (if org-log-done "automatically" "not")))
  19132. :style toggle :selected org-log-done])
  19133. "--"
  19134. ["Agenda Command..." org-agenda t]
  19135. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19136. ("File List for Agenda")
  19137. ("Special views current file"
  19138. ["TODO Tree" org-show-todo-tree t]
  19139. ["Check Deadlines" org-check-deadlines t]
  19140. ["Timeline" org-timeline t]
  19141. ["Tags/Property tree" org-match-sparse-tree t])
  19142. "--"
  19143. ["Export/Publish..." org-export-dispatch t]
  19144. ("LaTeX"
  19145. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19146. :selected org-cdlatex-mode]
  19147. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19148. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19149. ["Modify math symbol" org-cdlatex-math-modify
  19150. (org-inside-LaTeX-fragment-p)]
  19151. ["Insert citation" org-reftex-citation t]
  19152. "--"
  19153. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19154. "--"
  19155. ("MobileOrg"
  19156. ["Push Files and Views" org-mobile-push t]
  19157. ["Get Captured and Flagged" org-mobile-pull t]
  19158. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19159. "--"
  19160. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19161. "--"
  19162. ("Documentation"
  19163. ["Show Version" org-version t]
  19164. ["Info Documentation" org-info t])
  19165. ("Customize"
  19166. ["Browse Org Group" org-customize t]
  19167. "--"
  19168. ["Expand This Menu" org-create-customize-menu
  19169. (fboundp 'customize-menu-create)])
  19170. ["Send bug report" org-submit-bug-report t]
  19171. "--"
  19172. ("Refresh/Reload"
  19173. ["Refresh setup current buffer" org-mode-restart t]
  19174. ["Reload Org (after update)" org-reload t]
  19175. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19176. ))
  19177. (defun org-info (&optional node)
  19178. "Read documentation for Org-mode in the info system.
  19179. With optional NODE, go directly to that node."
  19180. (interactive)
  19181. (info (format "(org)%s" (or node ""))))
  19182. ;;;###autoload
  19183. (defun org-submit-bug-report ()
  19184. "Submit a bug report on Org-mode via mail.
  19185. Don't hesitate to report any problems or inaccurate documentation.
  19186. If you don't have setup sending mail from (X)Emacs, please copy the
  19187. output buffer into your mail program, as it gives us important
  19188. information about your Org-mode version and configuration."
  19189. (interactive)
  19190. (require 'reporter)
  19191. (org-load-modules-maybe)
  19192. (org-require-autoloaded-modules)
  19193. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19194. (reporter-submit-bug-report
  19195. "emacs-orgmode@gnu.org"
  19196. (org-version nil 'full)
  19197. (let (list)
  19198. (save-window-excursion
  19199. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19200. (delete-other-windows)
  19201. (erase-buffer)
  19202. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19203. We would like to add your full Org-mode and Outline configuration to the
  19204. bug report. This greatly simplifies the work of the maintainer and
  19205. other experts on the mailing list.
  19206. HOWEVER, some variables you have customized may contain private
  19207. information. The names of customers, colleagues, or friends, might
  19208. appear in the form of file names, tags, todo states, or search strings.
  19209. If you answer yes to the prompt, you might want to check and remove
  19210. such private information before sending the email.")
  19211. (add-text-properties (point-min) (point-max) '(face org-warning))
  19212. (when (yes-or-no-p "Include your Org-mode configuration ")
  19213. (mapatoms
  19214. (lambda (v)
  19215. (and (boundp v)
  19216. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19217. (or (and (symbol-value v)
  19218. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19219. (and
  19220. (get v 'custom-type) (get v 'standard-value)
  19221. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19222. (push v list)))))
  19223. (kill-buffer (get-buffer "*Warn about privacy*"))
  19224. list))
  19225. nil nil
  19226. "Remember to cover the basics, that is, what you expected to happen and
  19227. what in fact did happen. You don't know how to make a good report? See
  19228. http://orgmode.org/manual/Feedback.html#Feedback
  19229. Your bug report will be posted to the Org-mode mailing list.
  19230. ------------------------------------------------------------------------")
  19231. (save-excursion
  19232. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19233. (replace-match "\\1Bug: \\3 [\\2]")))))
  19234. (defun org-install-agenda-files-menu ()
  19235. (let ((bl (buffer-list)))
  19236. (save-excursion
  19237. (while bl
  19238. (set-buffer (pop bl))
  19239. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19240. (when (derived-mode-p 'org-mode)
  19241. (easy-menu-change
  19242. '("Org") "File List for Agenda"
  19243. (append
  19244. (list
  19245. ["Edit File List" (org-edit-agenda-file-list) t]
  19246. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19247. ["Remove Current File from List" org-remove-file t]
  19248. ["Cycle through agenda files" org-cycle-agenda-files t]
  19249. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19250. "--")
  19251. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19252. ;;;; Documentation
  19253. (defun org-require-autoloaded-modules ()
  19254. (interactive)
  19255. (mapc 'require
  19256. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19257. org-remember org-table org-timer)))
  19258. ;;;###autoload
  19259. (defun org-reload (&optional uncompiled)
  19260. "Reload all org lisp files.
  19261. With prefix arg UNCOMPILED, load the uncompiled versions."
  19262. (interactive "P")
  19263. (require 'loadhist)
  19264. (let* ((org-dir (org-find-library-dir "org"))
  19265. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19266. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19267. (remove-re (mapconcat 'identity
  19268. (mapcar (lambda (f) (concat "^" f "$"))
  19269. (list (if (featurep 'xemacs)
  19270. "org-colview"
  19271. "org-colview-xemacs")
  19272. "org" "org-loaddefs" "org-version"))
  19273. "\\|"))
  19274. (feats (delete-dups
  19275. (mapcar 'file-name-sans-extension
  19276. (mapcar 'file-name-nondirectory
  19277. (delq nil
  19278. (mapcar 'feature-file
  19279. features))))))
  19280. (lfeat (append
  19281. (sort
  19282. (setq feats
  19283. (delq nil (mapcar
  19284. (lambda (f)
  19285. (if (and (string-match feature-re f)
  19286. (not (string-match remove-re f)))
  19287. f nil))
  19288. feats)))
  19289. 'string-lessp)
  19290. (list "org-version" "org")))
  19291. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19292. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19293. load-uncore load-misses)
  19294. (setq load-misses
  19295. (delq 't
  19296. (mapcar (lambda (f)
  19297. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19298. (and (string= org-dir contrib-dir)
  19299. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19300. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19301. (add-to-list 'load-uncore f 'append)
  19302. 't)
  19303. f))
  19304. lfeat)))
  19305. (if load-uncore
  19306. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19307. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19308. (if load-misses
  19309. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19310. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19311. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19312. ;;;###autoload
  19313. (defun org-customize ()
  19314. "Call the customize function with org as argument."
  19315. (interactive)
  19316. (org-load-modules-maybe)
  19317. (org-require-autoloaded-modules)
  19318. (customize-browse 'org))
  19319. (defun org-create-customize-menu ()
  19320. "Create a full customization menu for Org-mode, insert it into the menu."
  19321. (interactive)
  19322. (org-load-modules-maybe)
  19323. (org-require-autoloaded-modules)
  19324. (if (fboundp 'customize-menu-create)
  19325. (progn
  19326. (easy-menu-change
  19327. '("Org") "Customize"
  19328. `(["Browse Org group" org-customize t]
  19329. "--"
  19330. ,(customize-menu-create 'org)
  19331. ["Set" Custom-set t]
  19332. ["Save" Custom-save t]
  19333. ["Reset to Current" Custom-reset-current t]
  19334. ["Reset to Saved" Custom-reset-saved t]
  19335. ["Reset to Standard Settings" Custom-reset-standard t]))
  19336. (message "\"Org\"-menu now contains full customization menu"))
  19337. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19338. ;;;; Miscellaneous stuff
  19339. ;;; Generally useful functions
  19340. (defun org-get-at-bol (property)
  19341. "Get text property PROPERTY at beginning of line."
  19342. (get-text-property (point-at-bol) property))
  19343. (defun org-find-text-property-in-string (prop s)
  19344. "Return the first non-nil value of property PROP in string S."
  19345. (or (get-text-property 0 prop s)
  19346. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19347. prop s)))
  19348. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19349. "Display the given MESSAGE as a warning."
  19350. (if (fboundp 'display-warning)
  19351. (display-warning 'org message
  19352. (if (featurep 'xemacs) 'warning :warning))
  19353. (let ((buf (get-buffer-create "*Org warnings*")))
  19354. (with-current-buffer buf
  19355. (goto-char (point-max))
  19356. (insert "Warning (Org): " message)
  19357. (unless (bolp)
  19358. (newline)))
  19359. (display-buffer buf)
  19360. (sit-for 0))))
  19361. (defun org-eval (form)
  19362. "Eval FORM and return result."
  19363. (condition-case error
  19364. (eval form)
  19365. (error (format "%%![Error: %s]" error))))
  19366. (defun org-in-clocktable-p ()
  19367. "Check if the cursor is in a clocktable."
  19368. (let ((pos (point)) start)
  19369. (save-excursion
  19370. (end-of-line 1)
  19371. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19372. (setq start (match-beginning 0))
  19373. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19374. (>= (match-end 0) pos)
  19375. start))))
  19376. (defun org-in-commented-line ()
  19377. "Is point in a line starting with `#'?"
  19378. (equal (char-after (point-at-bol)) ?#))
  19379. (defun org-in-indented-comment-line ()
  19380. "Is point in a line starting with `#' after some white space?"
  19381. (save-excursion
  19382. (save-match-data
  19383. (goto-char (point-at-bol))
  19384. (looking-at "[ \t]*#"))))
  19385. (defun org-in-verbatim-emphasis ()
  19386. (save-match-data
  19387. (and (org-in-regexp org-emph-re 2)
  19388. (>= (point) (match-beginning 3))
  19389. (<= (point) (match-end 4))
  19390. (member (match-string 3) '("=" "~")))))
  19391. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19392. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19393. (if (and marker (marker-buffer marker)
  19394. (buffer-live-p (marker-buffer marker)))
  19395. (progn
  19396. (org-pop-to-buffer-same-window (marker-buffer marker))
  19397. (if (or (> marker (point-max)) (< marker (point-min)))
  19398. (widen))
  19399. (goto-char marker)
  19400. (org-show-context 'org-goto))
  19401. (if bookmark
  19402. (bookmark-jump bookmark)
  19403. (error "Cannot find location"))))
  19404. (defun org-quote-csv-field (s)
  19405. "Quote field for inclusion in CSV material."
  19406. (if (string-match "[\",]" s)
  19407. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19408. s))
  19409. (defun org-force-self-insert (N)
  19410. "Needed to enforce self-insert under remapping."
  19411. (interactive "p")
  19412. (self-insert-command N))
  19413. (defun org-string-width (s)
  19414. "Compute width of string, ignoring invisible characters.
  19415. This ignores character with invisibility property `org-link', and also
  19416. characters with property `org-cwidth', because these will become invisible
  19417. upon the next fontification round."
  19418. (let (b l)
  19419. (when (or (eq t buffer-invisibility-spec)
  19420. (assq 'org-link buffer-invisibility-spec))
  19421. (while (setq b (text-property-any 0 (length s)
  19422. 'invisible 'org-link s))
  19423. (setq s (concat (substring s 0 b)
  19424. (substring s (or (next-single-property-change
  19425. b 'invisible s) (length s)))))))
  19426. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19427. (setq s (concat (substring s 0 b)
  19428. (substring s (or (next-single-property-change
  19429. b 'org-cwidth s) (length s))))))
  19430. (setq l (string-width s) b -1)
  19431. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19432. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19433. l))
  19434. (defun org-shorten-string (s maxlength)
  19435. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19436. If the string is shorter or has length MAXLENGTH, just return the
  19437. original string. If it is longer, the functions finds a space in the
  19438. string, breaks this string off at that locations and adds three dots
  19439. as ellipsis. Including the ellipsis, the string will not be longer
  19440. than MAXLENGTH. If finding a good breaking point in the string does
  19441. not work, the string is just chopped off in the middle of a word
  19442. if necessary."
  19443. (if (<= (length s) maxlength)
  19444. s
  19445. (let* ((n (max (- maxlength 4) 1))
  19446. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19447. (if (string-match re s)
  19448. (concat (match-string 1 s) "...")
  19449. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19450. (defun org-get-indentation (&optional line)
  19451. "Get the indentation of the current line, interpreting tabs.
  19452. When LINE is given, assume it represents a line and compute its indentation."
  19453. (if line
  19454. (if (string-match "^ *" (org-remove-tabs line))
  19455. (match-end 0))
  19456. (save-excursion
  19457. (beginning-of-line 1)
  19458. (skip-chars-forward " \t")
  19459. (current-column))))
  19460. (defun org-get-string-indentation (s)
  19461. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19462. (let ((n -1) (i 0) (w tab-width) c)
  19463. (catch 'exit
  19464. (while (< (setq n (1+ n)) (length s))
  19465. (setq c (aref s n))
  19466. (cond ((= c ?\ ) (setq i (1+ i)))
  19467. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19468. (t (throw 'exit t)))))
  19469. i))
  19470. (defun org-remove-tabs (s &optional width)
  19471. "Replace tabulators in S with spaces.
  19472. Assumes that s is a single line, starting in column 0."
  19473. (setq width (or width tab-width))
  19474. (while (string-match "\t" s)
  19475. (setq s (replace-match
  19476. (make-string
  19477. (- (* width (/ (+ (match-beginning 0) width) width))
  19478. (match-beginning 0)) ?\ )
  19479. t t s)))
  19480. s)
  19481. (defun org-fix-indentation (line ind)
  19482. "Fix indentation in LINE.
  19483. IND is a cons cell with target and minimum indentation.
  19484. If the current indentation in LINE is smaller than the minimum,
  19485. leave it alone. If it is larger than ind, set it to the target."
  19486. (let* ((l (org-remove-tabs line))
  19487. (i (org-get-indentation l))
  19488. (i1 (car ind)) (i2 (cdr ind)))
  19489. (if (>= i i2) (setq l (substring line i2)))
  19490. (if (> i1 0)
  19491. (concat (make-string i1 ?\ ) l)
  19492. l)))
  19493. (defun org-remove-indentation (code &optional n)
  19494. "Remove the maximum common indentation from the lines in CODE.
  19495. N may optionally be the number of spaces to remove."
  19496. (with-temp-buffer
  19497. (insert code)
  19498. (org-do-remove-indentation n)
  19499. (buffer-string)))
  19500. (defun org-do-remove-indentation (&optional n)
  19501. "Remove the maximum common indentation from the buffer."
  19502. (untabify (point-min) (point-max))
  19503. (let ((min 10000) re)
  19504. (if n
  19505. (setq min n)
  19506. (goto-char (point-min))
  19507. (while (re-search-forward "^ *[^ \n]" nil t)
  19508. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19509. (unless (or (= min 0) (= min 10000))
  19510. (setq re (format "^ \\{%d\\}" min))
  19511. (goto-char (point-min))
  19512. (while (re-search-forward re nil t)
  19513. (replace-match "")
  19514. (end-of-line 1))
  19515. min)))
  19516. (defun org-fill-template (template alist)
  19517. "Find each %key of ALIST in TEMPLATE and replace it."
  19518. (let ((case-fold-search nil)
  19519. entry key value)
  19520. (setq alist (sort (copy-sequence alist)
  19521. (lambda (a b) (< (length (car a)) (length (car b))))))
  19522. (while (setq entry (pop alist))
  19523. (setq template
  19524. (replace-regexp-in-string
  19525. (concat "%" (regexp-quote (car entry)))
  19526. (or (cdr entry) "") template t t)))
  19527. template))
  19528. (defun org-base-buffer (buffer)
  19529. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19530. (if (not buffer)
  19531. buffer
  19532. (or (buffer-base-buffer buffer)
  19533. buffer)))
  19534. (defun org-trim (s)
  19535. "Remove whitespace at beginning and end of string."
  19536. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19537. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19538. s)
  19539. (defun org-wrap (string &optional width lines)
  19540. "Wrap string to either a number of lines, or a width in characters.
  19541. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19542. that costs. If there is a word longer than WIDTH, the text is actually
  19543. wrapped to the length of that word.
  19544. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19545. many lines, whatever width that takes.
  19546. The return value is a list of lines, without newlines at the end."
  19547. (let* ((words (org-split-string string "[ \t\n]+"))
  19548. (maxword (apply 'max (mapcar 'org-string-width words)))
  19549. w ll)
  19550. (cond (width
  19551. (org-do-wrap words (max maxword width)))
  19552. (lines
  19553. (setq w maxword)
  19554. (setq ll (org-do-wrap words maxword))
  19555. (if (<= (length ll) lines)
  19556. ll
  19557. (setq ll words)
  19558. (while (> (length ll) lines)
  19559. (setq w (1+ w))
  19560. (setq ll (org-do-wrap words w)))
  19561. ll))
  19562. (t (error "Cannot wrap this")))))
  19563. (defun org-do-wrap (words width)
  19564. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19565. (let (lines line)
  19566. (while words
  19567. (setq line (pop words))
  19568. (while (and words (< (+ (length line) (length (car words))) width))
  19569. (setq line (concat line " " (pop words))))
  19570. (setq lines (push line lines)))
  19571. (nreverse lines)))
  19572. (defun org-split-string (string &optional separators)
  19573. "Splits STRING into substrings at SEPARATORS.
  19574. No empty strings are returned if there are matches at the beginning
  19575. and end of string."
  19576. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19577. (start 0)
  19578. notfirst
  19579. (list nil))
  19580. (while (and (string-match rexp string
  19581. (if (and notfirst
  19582. (= start (match-beginning 0))
  19583. (< start (length string)))
  19584. (1+ start) start))
  19585. (< (match-beginning 0) (length string)))
  19586. (setq notfirst t)
  19587. (or (eq (match-beginning 0) 0)
  19588. (and (eq (match-beginning 0) (match-end 0))
  19589. (eq (match-beginning 0) start))
  19590. (setq list
  19591. (cons (substring string start (match-beginning 0))
  19592. list)))
  19593. (setq start (match-end 0)))
  19594. (or (eq start (length string))
  19595. (setq list
  19596. (cons (substring string start)
  19597. list)))
  19598. (nreverse list)))
  19599. (defun org-quote-vert (s)
  19600. "Replace \"|\" with \"\\vert\"."
  19601. (while (string-match "|" s)
  19602. (setq s (replace-match "\\vert" t t s)))
  19603. s)
  19604. (defun org-uuidgen-p (s)
  19605. "Is S an ID created by UUIDGEN?"
  19606. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19607. (defun org-in-src-block-p (&optional inside)
  19608. "Whether point is in a code source block.
  19609. When INSIDE is non-nil, don't consider we are within a src block
  19610. when point is at #+BEGIN_SRC or #+END_SRC."
  19611. (let ((case-fold-search t) ov)
  19612. (or (and (setq ov (overlays-at (point)))
  19613. (memq 'org-block-background
  19614. (overlay-properties (car ov))))
  19615. (and (not inside)
  19616. (save-match-data
  19617. (save-excursion
  19618. (beginning-of-line)
  19619. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19620. (defun org-context ()
  19621. "Return a list of contexts of the current cursor position.
  19622. If several contexts apply, all are returned.
  19623. Each context entry is a list with a symbol naming the context, and
  19624. two positions indicating start and end of the context. Possible
  19625. contexts are:
  19626. :headline anywhere in a headline
  19627. :headline-stars on the leading stars in a headline
  19628. :todo-keyword on a TODO keyword (including DONE) in a headline
  19629. :tags on the TAGS in a headline
  19630. :priority on the priority cookie in a headline
  19631. :item on the first line of a plain list item
  19632. :item-bullet on the bullet/number of a plain list item
  19633. :checkbox on the checkbox in a plain list item
  19634. :table in an org-mode table
  19635. :table-special on a special filed in a table
  19636. :table-table in a table.el table
  19637. :clocktable in a clocktable
  19638. :src-block in a source block
  19639. :link on a hyperlink
  19640. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19641. :target on a <<target>>
  19642. :radio-target on a <<<radio-target>>>
  19643. :latex-fragment on a LaTeX fragment
  19644. :latex-preview on a LaTeX fragment with overlaid preview image
  19645. This function expects the position to be visible because it uses font-lock
  19646. faces as a help to recognize the following contexts: :table-special, :link,
  19647. and :keyword."
  19648. (let* ((f (get-text-property (point) 'face))
  19649. (faces (if (listp f) f (list f)))
  19650. (case-fold-search t)
  19651. (p (point)) clist o)
  19652. ;; First the large context
  19653. (cond
  19654. ((org-at-heading-p t)
  19655. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19656. (when (progn
  19657. (beginning-of-line 1)
  19658. (looking-at org-todo-line-tags-regexp))
  19659. (push (org-point-in-group p 1 :headline-stars) clist)
  19660. (push (org-point-in-group p 2 :todo-keyword) clist)
  19661. (push (org-point-in-group p 4 :tags) clist))
  19662. (goto-char p)
  19663. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19664. (if (looking-at "\\[#[A-Z0-9]\\]")
  19665. (push (org-point-in-group p 0 :priority) clist)))
  19666. ((org-at-item-p)
  19667. (push (org-point-in-group p 2 :item-bullet) clist)
  19668. (push (list :item (point-at-bol)
  19669. (save-excursion (org-end-of-item) (point)))
  19670. clist)
  19671. (and (org-at-item-checkbox-p)
  19672. (push (org-point-in-group p 0 :checkbox) clist)))
  19673. ((org-at-table-p)
  19674. (push (list :table (org-table-begin) (org-table-end)) clist)
  19675. (if (memq 'org-formula faces)
  19676. (push (list :table-special
  19677. (previous-single-property-change p 'face)
  19678. (next-single-property-change p 'face)) clist)))
  19679. ((org-at-table-p 'any)
  19680. (push (list :table-table) clist)))
  19681. (goto-char p)
  19682. (let ((case-fold-search t))
  19683. ;; New the "medium" contexts: clocktables, source blocks
  19684. (cond ((org-in-clocktable-p)
  19685. (push (list :clocktable
  19686. (and (or (looking-at "#\\+BEGIN: clocktable")
  19687. (search-backward "#+BEGIN: clocktable" nil t))
  19688. (match-beginning 0))
  19689. (and (re-search-forward "#\\+END:?" nil t)
  19690. (match-end 0))) clist))
  19691. ((org-in-src-block-p)
  19692. (push (list :src-block
  19693. (and (or (looking-at "#\\+BEGIN_SRC")
  19694. (search-backward "#+BEGIN_SRC" nil t))
  19695. (match-beginning 0))
  19696. (and (search-forward "#+END_SRC" nil t)
  19697. (match-beginning 0))) clist))))
  19698. (goto-char p)
  19699. ;; Now the small context
  19700. (cond
  19701. ((org-at-timestamp-p)
  19702. (push (org-point-in-group p 0 :timestamp) clist))
  19703. ((memq 'org-link faces)
  19704. (push (list :link
  19705. (previous-single-property-change p 'face)
  19706. (next-single-property-change p 'face)) clist))
  19707. ((memq 'org-special-keyword faces)
  19708. (push (list :keyword
  19709. (previous-single-property-change p 'face)
  19710. (next-single-property-change p 'face)) clist))
  19711. ((org-at-target-p)
  19712. (push (org-point-in-group p 0 :target) clist)
  19713. (goto-char (1- (match-beginning 0)))
  19714. (if (looking-at org-radio-target-regexp)
  19715. (push (org-point-in-group p 0 :radio-target) clist))
  19716. (goto-char p))
  19717. ((setq o (car (delq nil
  19718. (mapcar
  19719. (lambda (x)
  19720. (if (memq x org-latex-fragment-image-overlays) x))
  19721. (overlays-at (point))))))
  19722. (push (list :latex-fragment
  19723. (overlay-start o) (overlay-end o)) clist)
  19724. (push (list :latex-preview
  19725. (overlay-start o) (overlay-end o)) clist))
  19726. ((org-inside-LaTeX-fragment-p)
  19727. ;; FIXME: positions wrong.
  19728. (push (list :latex-fragment (point) (point)) clist)))
  19729. (setq clist (nreverse (delq nil clist)))
  19730. clist))
  19731. ;; FIXME: Compare with at-regexp-p Do we need both?
  19732. (defun org-in-regexp (re &optional nlines visually)
  19733. "Check if point is inside a match of regexp.
  19734. Normally only the current line is checked, but you can include NLINES extra
  19735. lines both before and after point into the search.
  19736. If VISUALLY is set, require that the cursor is not after the match but
  19737. really on, so that the block visually is on the match."
  19738. (catch 'exit
  19739. (let ((pos (point))
  19740. (eol (point-at-eol (+ 1 (or nlines 0))))
  19741. (inc (if visually 1 0)))
  19742. (save-excursion
  19743. (beginning-of-line (- 1 (or nlines 0)))
  19744. (while (re-search-forward re eol t)
  19745. (if (and (<= (match-beginning 0) pos)
  19746. (>= (+ inc (match-end 0)) pos))
  19747. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19748. (defun org-at-regexp-p (regexp)
  19749. "Is point inside a match of REGEXP in the current line?"
  19750. (catch 'exit
  19751. (save-excursion
  19752. (let ((pos (point)) (end (point-at-eol)))
  19753. (beginning-of-line 1)
  19754. (while (re-search-forward regexp end t)
  19755. (if (and (<= (match-beginning 0) pos)
  19756. (>= (match-end 0) pos))
  19757. (throw 'exit t)))
  19758. nil))))
  19759. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19760. "Non-nil when point is between matches of START-RE and END-RE.
  19761. Also return a non-nil value when point is on one of the matches.
  19762. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19763. buffer positions. Default values are the positions of headlines
  19764. surrounding the point.
  19765. The functions returns a cons cell whose car (resp. cdr) is the
  19766. position before START-RE (resp. after END-RE)."
  19767. (save-match-data
  19768. (let ((pos (point))
  19769. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19770. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19771. beg end)
  19772. (save-excursion
  19773. ;; Point is on a block when on START-RE or if START-RE can be
  19774. ;; found before it...
  19775. (and (or (org-at-regexp-p start-re)
  19776. (re-search-backward start-re limit-up t))
  19777. (setq beg (match-beginning 0))
  19778. ;; ... and END-RE after it...
  19779. (goto-char (match-end 0))
  19780. (re-search-forward end-re limit-down t)
  19781. (> (setq end (match-end 0)) pos)
  19782. ;; ... without another START-RE in-between.
  19783. (goto-char (match-beginning 0))
  19784. (not (re-search-backward start-re (1+ beg) t))
  19785. ;; Return value.
  19786. (cons beg end))))))
  19787. (defun org-in-block-p (names)
  19788. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19789. NAMES is a list of strings containing names of blocks.
  19790. Return first block name matched, or nil. Beware that in case of
  19791. nested blocks, the returned name may not belong to the closest
  19792. block from point."
  19793. (save-match-data
  19794. (catch 'exit
  19795. (let ((case-fold-search t)
  19796. (lim-up (save-excursion (outline-previous-heading)))
  19797. (lim-down (save-excursion (outline-next-heading))))
  19798. (mapc (lambda (name)
  19799. (let ((n (regexp-quote name)))
  19800. (when (org-between-regexps-p
  19801. (concat "^[ \t]*#\\+begin_" n)
  19802. (concat "^[ \t]*#\\+end_" n)
  19803. lim-up lim-down)
  19804. (throw 'exit n))))
  19805. names))
  19806. nil)))
  19807. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19808. "Call `multi-occur' with buffers for all agenda files."
  19809. (interactive "sOrg-files matching: \np")
  19810. (let* ((files (org-agenda-files))
  19811. (tnames (mapcar 'file-truename files))
  19812. (extra org-agenda-text-search-extra-files)
  19813. f)
  19814. (when (eq (car extra) 'agenda-archives)
  19815. (setq extra (cdr extra))
  19816. (setq files (org-add-archive-files files)))
  19817. (while (setq f (pop extra))
  19818. (unless (member (file-truename f) tnames)
  19819. (add-to-list 'files f 'append)
  19820. (add-to-list 'tnames (file-truename f) 'append)))
  19821. (multi-occur
  19822. (mapcar (lambda (x)
  19823. (with-current-buffer
  19824. (or (get-file-buffer x) (find-file-noselect x))
  19825. (widen)
  19826. (current-buffer)))
  19827. files)
  19828. regexp)))
  19829. (if (boundp 'occur-mode-find-occurrence-hook)
  19830. ;; Emacs 23
  19831. (add-hook 'occur-mode-find-occurrence-hook
  19832. (lambda ()
  19833. (when (derived-mode-p 'org-mode)
  19834. (org-reveal))))
  19835. ;; Emacs 22
  19836. (defadvice occur-mode-goto-occurrence
  19837. (after org-occur-reveal activate)
  19838. (and (derived-mode-p 'org-mode) (org-reveal)))
  19839. (defadvice occur-mode-goto-occurrence-other-window
  19840. (after org-occur-reveal activate)
  19841. (and (derived-mode-p 'org-mode) (org-reveal)))
  19842. (defadvice occur-mode-display-occurrence
  19843. (after org-occur-reveal activate)
  19844. (when (derived-mode-p 'org-mode)
  19845. (let ((pos (occur-mode-find-occurrence)))
  19846. (with-current-buffer (marker-buffer pos)
  19847. (save-excursion
  19848. (goto-char pos)
  19849. (org-reveal)))))))
  19850. (defun org-occur-link-in-agenda-files ()
  19851. "Create a link and search for it in the agendas.
  19852. The link is not stored in `org-stored-links', it is just created
  19853. for the search purpose."
  19854. (interactive)
  19855. (let ((link (condition-case nil
  19856. (org-store-link nil)
  19857. (error "Unable to create a link to here"))))
  19858. (org-occur-in-agenda-files (regexp-quote link))))
  19859. (defun org-reverse-string (string)
  19860. "Return the reverse of STRING."
  19861. (apply 'string (reverse (string-to-list string))))
  19862. (defun org-uniquify (list)
  19863. "Remove duplicate elements from LIST."
  19864. (let (res)
  19865. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19866. res))
  19867. (defun org-uniquify-alist (alist)
  19868. "Merge duplicate elements of an alist.
  19869. For example, in this alist:
  19870. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19871. => '((a 1 3) (b 2))
  19872. merge (a 1) and (a 3) into (a 1 3) and return the new alist."
  19873. (let (rtn)
  19874. (mapc
  19875. (lambda (e)
  19876. (let (n)
  19877. (if (not (assoc (car e) rtn))
  19878. (push e rtn)
  19879. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19880. (setq rtn (assq-delete-all (car e) rtn))
  19881. (push n rtn))))
  19882. alist)
  19883. rtn))
  19884. (defun org-delete-all (elts list)
  19885. "Remove all elements in ELTS from LIST."
  19886. (while elts
  19887. (setq list (delete (pop elts) list)))
  19888. list)
  19889. (defun org-count (cl-item cl-seq)
  19890. "Count the number of occurrences of ITEM in SEQ.
  19891. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19892. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19893. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19894. (while (< cl-start cl-end)
  19895. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19896. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19897. (setq cl-start (1+ cl-start)))
  19898. cl-count))
  19899. (defun org-remove-if (predicate seq)
  19900. "Remove everything from SEQ that fulfills PREDICATE."
  19901. (let (res e)
  19902. (while seq
  19903. (setq e (pop seq))
  19904. (if (not (funcall predicate e)) (push e res)))
  19905. (nreverse res)))
  19906. (defun org-remove-if-not (predicate seq)
  19907. "Remove everything from SEQ that does not fulfill PREDICATE."
  19908. (let (res e)
  19909. (while seq
  19910. (setq e (pop seq))
  19911. (if (funcall predicate e) (push e res)))
  19912. (nreverse res)))
  19913. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19914. "Reduce two-argument FUNCTION across SEQ.
  19915. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19916. \":initial-value\" removed."
  19917. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19918. (cadr (memq :initial-value cl-keys)))
  19919. (cl-seq (pop cl-seq))
  19920. (t (funcall cl-func)))))
  19921. (while cl-seq
  19922. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19923. cl-accum))
  19924. (defun org-back-over-empty-lines ()
  19925. "Move backwards over whitespace, to the beginning of the first empty line.
  19926. Returns the number of empty lines passed."
  19927. (let ((pos (point)))
  19928. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19929. (skip-chars-backward " \t\n\r")
  19930. (unless (eobp)
  19931. (forward-line -1)))
  19932. (beginning-of-line 2)
  19933. (goto-char (min (point) pos))
  19934. (count-lines (point) pos)))
  19935. (defun org-skip-whitespace ()
  19936. (skip-chars-forward " \t\n\r"))
  19937. (defun org-point-in-group (point group &optional context)
  19938. "Check if POINT is in match-group GROUP.
  19939. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19940. match. If the match group does not exist or point is not inside it,
  19941. return nil."
  19942. (and (match-beginning group)
  19943. (>= point (match-beginning group))
  19944. (<= point (match-end group))
  19945. (if context
  19946. (list context (match-beginning group) (match-end group))
  19947. t)))
  19948. (defun org-switch-to-buffer-other-window (&rest args)
  19949. "Switch to buffer in a second window on the current frame.
  19950. In particular, do not allow pop-up frames.
  19951. Returns the newly created buffer."
  19952. (org-no-popups
  19953. (apply 'switch-to-buffer-other-window args)))
  19954. (defun org-combine-plists (&rest plists)
  19955. "Create a single property list from all plists in PLISTS.
  19956. The process starts by copying the first list, and then setting properties
  19957. from the other lists. Settings in the last list are the most significant
  19958. ones and overrule settings in the other lists."
  19959. (let ((rtn (copy-sequence (pop plists)))
  19960. p v ls)
  19961. (while plists
  19962. (setq ls (pop plists))
  19963. (while ls
  19964. (setq p (pop ls) v (pop ls))
  19965. (setq rtn (plist-put rtn p v))))
  19966. rtn))
  19967. (defun org-replace-escapes (string table)
  19968. "Replace %-escapes in STRING with values in TABLE.
  19969. TABLE is an association list with keys like \"%a\" and string values.
  19970. The sequences in STRING may contain normal field width and padding information,
  19971. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19972. so values can contain further %-escapes if they are define later in TABLE."
  19973. (let ((tbl (copy-alist table))
  19974. (case-fold-search nil)
  19975. (pchg 0)
  19976. e re rpl)
  19977. (while (setq e (pop tbl))
  19978. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19979. (when (and (cdr e) (string-match re (cdr e)))
  19980. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19981. (safe "SREF"))
  19982. (add-text-properties 0 3 (list 'sref sref) safe)
  19983. (setcdr e (replace-match safe t t (cdr e)))))
  19984. (while (string-match re string)
  19985. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19986. (cdr e)))
  19987. (setq string (replace-match rpl t t string))))
  19988. (while (setq pchg (next-property-change pchg string))
  19989. (let ((sref (get-text-property pchg 'sref string)))
  19990. (when (and sref (string-match "SREF" string pchg))
  19991. (setq string (replace-match sref t t string)))))
  19992. string))
  19993. (defun org-sublist (list start end)
  19994. "Return a section of LIST, from START to END.
  19995. Counting starts at 1."
  19996. (let (rtn (c start))
  19997. (setq list (nthcdr (1- start) list))
  19998. (while (and list (<= c end))
  19999. (push (pop list) rtn)
  20000. (setq c (1+ c)))
  20001. (nreverse rtn)))
  20002. (defun org-find-base-buffer-visiting (file)
  20003. "Like `find-buffer-visiting' but always return the base buffer and
  20004. not an indirect buffer."
  20005. (let ((buf (or (get-file-buffer file)
  20006. (find-buffer-visiting file))))
  20007. (if buf
  20008. (or (buffer-base-buffer buf) buf)
  20009. nil)))
  20010. (defun org-image-file-name-regexp (&optional extensions)
  20011. "Return regexp matching the file names of images.
  20012. If EXTENSIONS is given, only match these."
  20013. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20014. (image-file-name-regexp)
  20015. (let ((image-file-name-extensions
  20016. (or extensions
  20017. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20018. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20019. (concat "\\."
  20020. (regexp-opt (nconc (mapcar 'upcase
  20021. image-file-name-extensions)
  20022. image-file-name-extensions)
  20023. t)
  20024. "\\'"))))
  20025. (defun org-file-image-p (file &optional extensions)
  20026. "Return non-nil if FILE is an image."
  20027. (save-match-data
  20028. (string-match (org-image-file-name-regexp extensions) file)))
  20029. (defun org-get-cursor-date (&optional with-time)
  20030. "Return the date at cursor in as a time.
  20031. This works in the calendar and in the agenda, anywhere else it just
  20032. returns the current time.
  20033. If WITH-TIME is non-nil, returns the time of the event at point (in
  20034. the agenda) or the current time of the day."
  20035. (let (date day defd tp tm hod mod)
  20036. (when with-time
  20037. (setq tp (get-text-property (point) 'time))
  20038. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20039. (setq hod (string-to-number (match-string 1 tp))
  20040. mod (string-to-number (match-string 2 tp))))
  20041. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20042. mod (nth 1 (decode-time (current-time))))))
  20043. (cond
  20044. ((eq major-mode 'calendar-mode)
  20045. (setq date (calendar-cursor-to-date)
  20046. defd (encode-time 0 (or mod 0) (or hod 0)
  20047. (nth 1 date) (nth 0 date) (nth 2 date))))
  20048. ((eq major-mode 'org-agenda-mode)
  20049. (setq day (get-text-property (point) 'day))
  20050. (if day
  20051. (setq date (calendar-gregorian-from-absolute day)
  20052. defd (encode-time 0 (or mod 0) (or hod 0)
  20053. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20054. (or defd (current-time))))
  20055. (defun org-mark-subtree (&optional up)
  20056. "Mark the current subtree.
  20057. This puts point at the start of the current subtree, and mark at
  20058. the end. If a numeric prefix UP is given, move up into the
  20059. hierarchy of headlines by UP levels before marking the subtree."
  20060. (interactive "P")
  20061. (org-with-limited-levels
  20062. (cond ((org-at-heading-p) (beginning-of-line))
  20063. ((org-before-first-heading-p) (error "Not in a subtree"))
  20064. (t (outline-previous-visible-heading 1))))
  20065. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20066. (if (org-called-interactively-p 'any)
  20067. (call-interactively 'org-mark-element)
  20068. (org-mark-element)))
  20069. ;;; Indentation
  20070. (defun org-indent-line ()
  20071. "Indent line depending on context."
  20072. (interactive)
  20073. (let* ((pos (point))
  20074. (itemp (org-at-item-p))
  20075. (case-fold-search t)
  20076. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20077. (inline-task-p (and (featurep 'org-inlinetask)
  20078. (org-inlinetask-in-task-p)))
  20079. (inline-re (and inline-task-p
  20080. (org-inlinetask-outline-regexp)))
  20081. column)
  20082. (if (and orgstruct-is-++ (eq pos (point)))
  20083. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20084. (indent-according-to-mode))
  20085. (beginning-of-line 1)
  20086. (cond
  20087. ;; Headings
  20088. ((looking-at org-outline-regexp) (setq column 0))
  20089. ;; Footnote definition
  20090. ((looking-at org-footnote-definition-re) (setq column 0))
  20091. ;; Literal examples
  20092. ((looking-at "[ \t]*:\\( \\|$\\)")
  20093. (setq column (org-get-indentation))) ; do nothing
  20094. ;; Lists
  20095. ((ignore-errors (goto-char (org-in-item-p)))
  20096. (setq column (if itemp
  20097. (org-get-indentation)
  20098. (org-list-item-body-column (point))))
  20099. (goto-char pos))
  20100. ;; Drawers
  20101. ((and (looking-at "[ \t]*:END:")
  20102. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20103. (save-excursion
  20104. (goto-char (1- (match-beginning 1)))
  20105. (setq column (current-column))))
  20106. ;; Special blocks
  20107. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20108. (save-excursion
  20109. (re-search-backward
  20110. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20111. (setq column (org-get-indentation (match-string 0))))
  20112. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20113. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20114. (save-excursion
  20115. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20116. (setq column
  20117. (cond ((equal (downcase (match-string 1)) "src")
  20118. ;; src blocks: let `org-edit-src-exit' handle them
  20119. (org-get-indentation))
  20120. ((equal (downcase (match-string 1)) "example")
  20121. (max (org-get-indentation)
  20122. (org-get-indentation (match-string 0))))
  20123. (t
  20124. (org-get-indentation (match-string 0))))))
  20125. ;; This line has nothing special, look at the previous relevant
  20126. ;; line to compute indentation
  20127. (t
  20128. (beginning-of-line 0)
  20129. (while (and (not (bobp))
  20130. (not (looking-at org-table-line-regexp))
  20131. (not (looking-at org-drawer-regexp))
  20132. ;; When point started in an inline task, do not move
  20133. ;; above task starting line.
  20134. (not (and inline-task-p (looking-at inline-re)))
  20135. ;; Skip drawers, blocks, empty lines, verbatim,
  20136. ;; comments, tables, footnotes definitions, lists,
  20137. ;; inline tasks.
  20138. (or (and (looking-at "[ \t]*:END:")
  20139. (re-search-backward org-drawer-regexp nil t))
  20140. (and (looking-at "[ \t]*#\\+end_")
  20141. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20142. (looking-at "[ \t]*[\n:#|]")
  20143. (looking-at org-footnote-definition-re)
  20144. (and (not inline-task-p)
  20145. (featurep 'org-inlinetask)
  20146. (org-inlinetask-in-task-p)
  20147. (or (org-inlinetask-goto-beginning) t))))
  20148. (beginning-of-line 0))
  20149. (cond
  20150. ;; There was a list item above.
  20151. ((save-excursion
  20152. (and (ignore-errors (goto-char (org-in-item-p)))
  20153. (goto-char
  20154. (org-list-get-top-point (org-list-struct)))))
  20155. (looking-at org-list-full-item-re)
  20156. (setq column (length (match-string 0))))
  20157. ;; There was an heading above.
  20158. ((looking-at "\\*+[ \t]+")
  20159. (if (not org-adapt-indentation)
  20160. (setq column 0)
  20161. (goto-char (match-end 0))
  20162. (setq column (current-column))))
  20163. ;; A drawer had started and is unfinished
  20164. ((looking-at org-drawer-regexp)
  20165. (goto-char (1- (match-beginning 1)))
  20166. (setq column (current-column)))
  20167. ;; Else, nothing noticeable found: get indentation and go on.
  20168. (t (setq column (org-get-indentation))))))
  20169. ;; Now apply indentation and move cursor accordingly
  20170. (goto-char pos)
  20171. (if (<= (current-column) (current-indentation))
  20172. (org-indent-line-to column)
  20173. (save-excursion (org-indent-line-to column)))
  20174. ;; Special polishing for properties, see `org-property-format'
  20175. (setq column (current-column))
  20176. (beginning-of-line 1)
  20177. (if (looking-at
  20178. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20179. (replace-match (concat (match-string 1)
  20180. (format org-property-format
  20181. (match-string 2) (match-string 3)))
  20182. t t))
  20183. (org-move-to-column column))))
  20184. (defun org-indent-drawer ()
  20185. "Indent the drawer at point."
  20186. (interactive)
  20187. (let ((p (point))
  20188. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20189. (match-end 0)))
  20190. (folded
  20191. (save-excursion
  20192. (end-of-line)
  20193. (when (overlays-at (point))
  20194. (member 'invisible (overlay-properties
  20195. (car (overlays-at (point)))))))))
  20196. (when folded (org-cycle))
  20197. (indent-for-tab-command)
  20198. (while (and (move-beginning-of-line 2) (< (point) e))
  20199. (indent-for-tab-command))
  20200. (goto-char p)
  20201. (when folded (org-cycle)))
  20202. (message "Drawer at point indented"))
  20203. (defun org-indent-block ()
  20204. "Indent the block at point."
  20205. (interactive)
  20206. (let ((p (point))
  20207. (case-fold-search t)
  20208. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20209. (match-end 0)))
  20210. (folded
  20211. (save-excursion
  20212. (end-of-line)
  20213. (when (overlays-at (point))
  20214. (member 'invisible (overlay-properties
  20215. (car (overlays-at (point)))))))))
  20216. (when folded (org-cycle))
  20217. (indent-for-tab-command)
  20218. (while (and (move-beginning-of-line 2) (< (point) e))
  20219. (indent-for-tab-command))
  20220. (goto-char p)
  20221. (when folded (org-cycle)))
  20222. (message "Block at point indented"))
  20223. (defun org-indent-region (start end)
  20224. "Indent region."
  20225. (interactive "r")
  20226. (save-excursion
  20227. (let ((line-end (org-current-line end)))
  20228. (goto-char start)
  20229. (while (< (org-current-line) line-end)
  20230. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20231. (t (call-interactively 'org-indent-line)))
  20232. (move-beginning-of-line 2)))))
  20233. ;;; Filling
  20234. ;; We use our own fill-paragraph and auto-fill functions.
  20235. ;; `org-fill-paragraph' relies on adaptive filling and context
  20236. ;; checking. Appropriate `fill-prefix' is computed with
  20237. ;; `org-adaptive-fill-function'.
  20238. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20239. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20240. ;; `org-adaptive-fill-function' value. Internally,
  20241. ;; `org-comment-line-break-function' breaks the line.
  20242. ;; `org-setup-filling' installs filling and auto-filling related
  20243. ;; variables during `org-mode' initialization.
  20244. (defun org-setup-filling ()
  20245. (interactive)
  20246. ;; Prevent auto-fill from inserting unwanted new items.
  20247. (when (boundp 'fill-nobreak-predicate)
  20248. (org-set-local
  20249. 'fill-nobreak-predicate
  20250. (org-uniquify
  20251. (append fill-nobreak-predicate
  20252. '(org-fill-paragraph-separate-nobreak-p
  20253. org-fill-line-break-nobreak-p
  20254. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20255. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20256. (org-set-local 'auto-fill-inhibit-regexp nil)
  20257. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20258. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20259. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20260. (defvar org-element-paragraph-separate) ; org-element.el
  20261. (defun org-fill-paragraph-separate-nobreak-p ()
  20262. "Non-nil when a line break at point would insert a new item."
  20263. (looking-at (substring org-element-paragraph-separate 1)))
  20264. (defun org-fill-line-break-nobreak-p ()
  20265. "Non-nil when a line break at point would create an Org line break."
  20266. (save-excursion
  20267. (skip-chars-backward "[ \t]")
  20268. (skip-chars-backward "\\\\")
  20269. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20270. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20271. "Non-nil when a line break at point would insert a new item."
  20272. (and (org-at-timestamp-p t)
  20273. (not (looking-at org-ts-regexp-both))))
  20274. (declare-function message-in-body-p "message" ())
  20275. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20276. (defun org-adaptive-fill-function ()
  20277. "Compute a fill prefix for the current line.
  20278. Return fill prefix, as a string, or nil if current line isn't
  20279. meant to be filled."
  20280. (let (prefix)
  20281. (catch 'exit
  20282. (when (derived-mode-p 'message-mode)
  20283. (save-excursion
  20284. (beginning-of-line)
  20285. (cond ((or (not (message-in-body-p))
  20286. (looking-at orgtbl-line-start-regexp))
  20287. (throw 'exit nil))
  20288. ((looking-at message-cite-prefix-regexp)
  20289. (throw 'exit (match-string-no-properties 0)))
  20290. ((looking-at org-outline-regexp)
  20291. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20292. (org-with-wide-buffer
  20293. (let* ((p (line-beginning-position))
  20294. (element (save-excursion (beginning-of-line)
  20295. (org-element-at-point)))
  20296. (type (org-element-type element))
  20297. (post-affiliated (org-element-property :post-affiliated element)))
  20298. (unless (and post-affiliated (< p post-affiliated))
  20299. (case type
  20300. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20301. (footnote-definition "")
  20302. ((item plain-list)
  20303. (make-string (org-list-item-body-column
  20304. (or post-affiliated
  20305. (org-element-property :begin element)))
  20306. ? ))
  20307. (paragraph
  20308. ;; Fill prefix is usually the same as the current line,
  20309. ;; except if the paragraph is at the beginning of an item.
  20310. (let ((parent (org-element-property :parent element)))
  20311. (cond ((eq (org-element-type parent) 'item)
  20312. (make-string (org-list-item-body-column
  20313. (org-element-property :begin parent))
  20314. ? ))
  20315. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20316. (match-string 0))
  20317. (t ""))))
  20318. (comment-block
  20319. ;; Only fill contents if P is within block boundaries.
  20320. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20321. (forward-line)
  20322. (point)))
  20323. (cend (save-excursion
  20324. (goto-char (org-element-property :end element))
  20325. (skip-chars-backward " \r\t\n")
  20326. (line-beginning-position))))
  20327. (when (and (>= p cbeg) (< p cend))
  20328. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20329. (match-string 0)
  20330. "")))))))))))
  20331. (declare-function message-goto-body "message" ())
  20332. (defvar message-cite-prefix-regexp) ; From message.el
  20333. (defun org-fill-paragraph (&optional justify)
  20334. "Fill element at point, when applicable.
  20335. This function only applies to comment blocks, comments, example
  20336. blocks and paragraphs. Also, as a special case, re-align table
  20337. when point is at one.
  20338. If JUSTIFY is non-nil (interactively, with prefix argument),
  20339. justify as well. If `sentence-end-double-space' is non-nil, then
  20340. period followed by one space does not end a sentence, so don't
  20341. break a line there. The variable `fill-column' controls the
  20342. width for filling.
  20343. For convenience, when point is at a plain list, an item or
  20344. a footnote definition, try to fill the first paragraph within."
  20345. (interactive)
  20346. (if (and (derived-mode-p 'message-mode)
  20347. (or (not (message-in-body-p))
  20348. (save-excursion (move-beginning-of-line 1)
  20349. (looking-at message-cite-prefix-regexp))))
  20350. ;; First ensure filling is correct in message-mode.
  20351. (let ((fill-paragraph-function
  20352. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20353. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20354. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20355. (paragraph-separate
  20356. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20357. (fill-paragraph nil))
  20358. (with-syntax-table org-mode-transpose-word-syntax-table
  20359. ;; Move to end of line in order to get the first paragraph
  20360. ;; within a plain list or a footnote definition.
  20361. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20362. ;; First check if point is in a blank line at the beginning of
  20363. ;; the buffer. In that case, ignore filling.
  20364. (case (org-element-type element)
  20365. ;; Use major mode filling function is src blocks.
  20366. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20367. ;; Align Org tables, leave table.el tables as-is.
  20368. (table-row (org-table-align) t)
  20369. (table
  20370. (when (eq (org-element-property :type element) 'org)
  20371. (org-table-align))
  20372. t)
  20373. (paragraph
  20374. ;; Paragraphs may contain `line-break' type objects.
  20375. (let ((beg (max (point-min)
  20376. (org-element-property :contents-begin element)))
  20377. (end (min (point-max)
  20378. (org-element-property :contents-end element))))
  20379. ;; Do nothing if point is at an affiliated keyword.
  20380. (if (< (line-end-position) beg) t
  20381. (when (derived-mode-p 'message-mode)
  20382. ;; In `message-mode', do not fill following citation
  20383. ;; in current paragraph nor text before message body.
  20384. (let ((body-start (save-excursion (message-goto-body))))
  20385. (when body-start (setq beg (max body-start beg))))
  20386. (when (save-excursion
  20387. (re-search-forward
  20388. (concat "^" message-cite-prefix-regexp) end t))
  20389. (setq end (match-beginning 0))))
  20390. ;; Fill paragraph, taking line breaks into account.
  20391. ;; For that, slice the paragraph using line breaks as
  20392. ;; separators, and fill the parts in reverse order to
  20393. ;; avoid messing with markers.
  20394. (save-excursion
  20395. (goto-char end)
  20396. (mapc
  20397. (lambda (pos)
  20398. (fill-region-as-paragraph pos (point) justify)
  20399. (goto-char pos))
  20400. ;; Find the list of ending positions for line breaks
  20401. ;; in the current paragraph. Add paragraph
  20402. ;; beginning to include first slice.
  20403. (nreverse
  20404. (cons beg
  20405. (org-element-map
  20406. (org-element--parse-objects
  20407. beg end nil (org-element-restriction 'paragraph))
  20408. 'line-break
  20409. (lambda (lb) (org-element-property :end lb)))))))
  20410. t)))
  20411. ;; Contents of `comment-block' type elements should be
  20412. ;; filled as plain text, but only if point is within block
  20413. ;; markers.
  20414. (comment-block
  20415. (let* ((case-fold-search t)
  20416. (beg (save-excursion
  20417. (goto-char (org-element-property :begin element))
  20418. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20419. (forward-line)
  20420. (point)))
  20421. (end (save-excursion
  20422. (goto-char (org-element-property :end element))
  20423. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20424. (line-beginning-position))))
  20425. (when (and (>= (point) beg) (< (point) end))
  20426. (fill-region-as-paragraph
  20427. (save-excursion
  20428. (end-of-line)
  20429. (re-search-backward "^[ \t]*$" beg 'move)
  20430. (line-beginning-position))
  20431. (save-excursion
  20432. (beginning-of-line)
  20433. (re-search-forward "^[ \t]*$" end 'move)
  20434. (line-beginning-position))
  20435. justify)))
  20436. t)
  20437. ;; Fill comments.
  20438. (comment (fill-comment-paragraph justify))
  20439. ;; Ignore every other element.
  20440. (otherwise t))))))
  20441. (defun org-auto-fill-function ()
  20442. "Auto-fill function."
  20443. ;; Check if auto-filling is meaningful.
  20444. (let ((fc (current-fill-column)))
  20445. (when (and fc (> (current-column) fc))
  20446. (let* ((fill-prefix (org-adaptive-fill-function))
  20447. ;; Enforce empty fill prefix, if required. Otherwise, it
  20448. ;; will be computed again.
  20449. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20450. (when fill-prefix (do-auto-fill))))))
  20451. (defun org-comment-line-break-function (&optional soft)
  20452. "Break line at point and indent, continuing comment if within one.
  20453. The inserted newline is marked hard if variable
  20454. `use-hard-newlines' is true, unless optional argument SOFT is
  20455. non-nil."
  20456. (if soft (insert-and-inherit ?\n) (newline 1))
  20457. (save-excursion (forward-char -1) (delete-horizontal-space))
  20458. (delete-horizontal-space)
  20459. (indent-to-left-margin)
  20460. (insert-before-markers-and-inherit fill-prefix))
  20461. ;;; Comments
  20462. ;; Org comments syntax is quite complex. It requires the entire line
  20463. ;; to be just a comment. Also, even with the right syntax at the
  20464. ;; beginning of line, some some elements (i.e. verse-block or
  20465. ;; example-block) don't accept comments. Usual Emacs comment commands
  20466. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20467. ;; Org still relies on `comment-dwim', but cannot trust
  20468. ;; `comment-only-p'. So, `comment-region-function' and
  20469. ;; `uncomment-region-function' both point
  20470. ;; to`org-comment-or-uncomment-region'. Eventually,
  20471. ;; `org-insert-comment' takes care of insertion of comments at the
  20472. ;; beginning of line.
  20473. ;; `org-setup-comments-handling' install comments related variables
  20474. ;; during `org-mode' initialization.
  20475. (defun org-setup-comments-handling ()
  20476. (interactive)
  20477. (org-set-local 'comment-use-syntax nil)
  20478. (org-set-local 'comment-start "# ")
  20479. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20480. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20481. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20482. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20483. (defun org-insert-comment ()
  20484. "Insert an empty comment above current line.
  20485. If the line is empty, insert comment at its beginning."
  20486. (beginning-of-line)
  20487. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20488. (org-indent-line)
  20489. (insert "# "))
  20490. (defvar comment-empty-lines) ; From newcomment.el.
  20491. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20492. "Comment or uncomment each non-blank line in the region.
  20493. Uncomment each non-blank line between BEG and END if it only
  20494. contains commented lines. Otherwise, comment them."
  20495. (save-restriction
  20496. ;; Restrict region
  20497. (narrow-to-region (save-excursion (goto-char beg)
  20498. (skip-chars-forward " \r\t\n" end)
  20499. (line-beginning-position))
  20500. (save-excursion (goto-char end)
  20501. (skip-chars-backward " \r\t\n" beg)
  20502. (line-end-position)))
  20503. (let ((uncommentp
  20504. ;; UNCOMMENTP is non-nil when every non blank line between
  20505. ;; BEG and END is a comment.
  20506. (save-excursion
  20507. (goto-char (point-min))
  20508. (while (and (not (eobp))
  20509. (let ((element (org-element-at-point)))
  20510. (and (eq (org-element-type element) 'comment)
  20511. (goto-char (min (point-max)
  20512. (org-element-property
  20513. :end element)))))))
  20514. (eobp))))
  20515. (if uncommentp
  20516. ;; Only blank lines and comments in region: uncomment it.
  20517. (save-excursion
  20518. (goto-char (point-min))
  20519. (while (not (eobp))
  20520. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20521. (replace-match "" nil nil nil 1))
  20522. (forward-line)))
  20523. ;; Comment each line in region.
  20524. (let ((min-indent (point-max)))
  20525. ;; First find the minimum indentation across all lines.
  20526. (save-excursion
  20527. (goto-char (point-min))
  20528. (while (and (not (eobp)) (not (zerop min-indent)))
  20529. (unless (looking-at "[ \t]*$")
  20530. (setq min-indent (min min-indent (current-indentation))))
  20531. (forward-line)))
  20532. ;; Then loop over all lines.
  20533. (save-excursion
  20534. (goto-char (point-min))
  20535. (while (not (eobp))
  20536. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20537. ;; Don't get fooled by invisible text (e.g. link path)
  20538. ;; when moving to column MIN-INDENT.
  20539. (let ((buffer-invisibility-spec nil))
  20540. (org-move-to-column min-indent t))
  20541. (insert comment-start))
  20542. (forward-line))))))))
  20543. ;;; Planning
  20544. ;; This section contains tools to operate on timestamp objects, as
  20545. ;; returned by, e.g. `org-element-context'.
  20546. (defun org-timestamp-has-time-p (timestamp)
  20547. "Non-nil when TIMESTAMP has a time specified."
  20548. (org-element-property :hour-start timestamp))
  20549. (defun org-timestamp-format (timestamp format &optional end utc)
  20550. "Format a TIMESTAMP element into a string.
  20551. FORMAT is a format specifier to be passed to
  20552. `format-time-string'.
  20553. When optional argument END is non-nil, use end of date-range or
  20554. time-range, if possible.
  20555. When optional argument UTC is non-nil, time will be expressed as
  20556. Universal Time."
  20557. (format-time-string
  20558. format
  20559. (apply 'encode-time
  20560. (cons 0
  20561. (mapcar
  20562. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20563. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20564. '(:minute-start :hour-start :day-start :month-start
  20565. :year-start)))))
  20566. utc))
  20567. (defun org-timestamp-split-range (timestamp &optional end)
  20568. "Extract a timestamp object from a date or time range.
  20569. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20570. the end of the range. Otherwise, extract its start.
  20571. Return a new timestamp object sharing the same parent as
  20572. TIMESTAMP."
  20573. (let ((type (org-element-property :type timestamp)))
  20574. (if (memq type '(active inactive diary)) timestamp
  20575. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20576. ;; Set new type.
  20577. (org-element-put-property
  20578. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20579. ;; Copy start properties over end properties if END is
  20580. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20581. (let ((p-alist '((:minute-start . :minute-end)
  20582. (:hour-start . :hour-end)
  20583. (:day-start . :day-end)
  20584. (:month-start . :month-end)
  20585. (:year-start . :year-end))))
  20586. (dolist (p-cell p-alist)
  20587. (org-element-put-property
  20588. split-ts
  20589. (funcall (if end 'car 'cdr) p-cell)
  20590. (org-element-property
  20591. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20592. ;; Eventually refresh `:raw-value'.
  20593. (org-element-put-property split-ts :raw-value nil)
  20594. (org-element-put-property
  20595. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20596. (defun org-timestamp-translate (timestamp &optional boundary)
  20597. "Apply `org-translate-time' on a TIMESTAMP object.
  20598. When optional argument BOUNDARY is non-nil, it is either the
  20599. symbol `start' or `end'. In this case, only translate the
  20600. starting or ending part of TIMESTAMP if it is a date or time
  20601. range. Otherwise, translate both parts."
  20602. (if (and (not boundary)
  20603. (memq (org-element-property :type timestamp)
  20604. '(active-range inactive-range)))
  20605. (concat
  20606. (org-translate-time
  20607. (org-element-property :raw-value
  20608. (org-timestamp-split-range timestamp)))
  20609. "--"
  20610. (org-translate-time
  20611. (org-element-property :raw-value
  20612. (org-timestamp-split-range timestamp t))))
  20613. (org-translate-time
  20614. (org-element-property
  20615. :raw-value
  20616. (if (not boundary) timestamp
  20617. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20618. ;;; Other stuff.
  20619. (defun org-toggle-fixed-width-section (arg)
  20620. "Toggle the fixed-width export.
  20621. If there is no active region, the QUOTE keyword at the current headline is
  20622. inserted or removed. When present, it causes the text between this headline
  20623. and the next to be exported as fixed-width text, and unmodified.
  20624. If there is an active region, this command adds or removes a colon as the
  20625. first character of this line. If the first character of a line is a colon,
  20626. this line is also exported in fixed-width font."
  20627. (interactive "P")
  20628. (let* ((cc 0)
  20629. (regionp (org-region-active-p))
  20630. (beg (if regionp (region-beginning) (point)))
  20631. (end (if regionp (region-end)))
  20632. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20633. (case-fold-search nil)
  20634. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20635. off)
  20636. (if regionp
  20637. (save-excursion
  20638. (goto-char beg)
  20639. (setq cc (current-column))
  20640. (beginning-of-line 1)
  20641. (setq off (looking-at re))
  20642. (while (> nlines 0)
  20643. (setq nlines (1- nlines))
  20644. (beginning-of-line 1)
  20645. (cond
  20646. (arg
  20647. (org-move-to-column cc t)
  20648. (insert ": \n")
  20649. (forward-line -1))
  20650. ((and off (looking-at re))
  20651. (replace-match "" t t nil 1))
  20652. ((not off) (org-move-to-column cc t) (insert ": ")))
  20653. (forward-line 1)))
  20654. (save-excursion
  20655. (org-back-to-heading)
  20656. (cond
  20657. ((looking-at (format org-heading-keyword-regexp-format
  20658. org-quote-string))
  20659. (goto-char (match-end 1))
  20660. (looking-at (concat " +" org-quote-string))
  20661. (replace-match "" t t)
  20662. (when (eolp) (insert " ")))
  20663. ((looking-at org-outline-regexp)
  20664. (goto-char (match-end 0))
  20665. (insert org-quote-string " ")))))))
  20666. (defun org-reftex-citation ()
  20667. "Use reftex-citation to insert a citation into the buffer.
  20668. This looks for a line like
  20669. #+BIBLIOGRAPHY: foo plain option:-d
  20670. and derives from it that foo.bib is the bibliography file relevant
  20671. for this document. It then installs the necessary environment for RefTeX
  20672. to work in this buffer and calls `reftex-citation' to insert a citation
  20673. into the buffer.
  20674. Export of such citations to both LaTeX and HTML is handled by the contributed
  20675. package org-exp-bibtex by Taru Karttunen."
  20676. (interactive)
  20677. (let ((reftex-docstruct-symbol 'rds)
  20678. (reftex-cite-format "\\cite{%l}")
  20679. rds bib)
  20680. (save-excursion
  20681. (save-restriction
  20682. (widen)
  20683. (let ((case-fold-search t)
  20684. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20685. (if (not (save-excursion
  20686. (or (re-search-forward re nil t)
  20687. (re-search-backward re nil t))))
  20688. (error "No bibliography defined in file")
  20689. (setq bib (concat (match-string 1) ".bib")
  20690. rds (list (list 'bib bib)))))))
  20691. (call-interactively 'reftex-citation)))
  20692. ;;;; Functions extending outline functionality
  20693. (defun org-beginning-of-line (&optional arg)
  20694. "Go to the beginning of the current line. If that is invisible, continue
  20695. to a visible line beginning. This makes the function of C-a more intuitive.
  20696. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20697. first attempt, and only move to after the tags when the cursor is already
  20698. beyond the end of the headline."
  20699. (interactive "P")
  20700. (let ((pos (point))
  20701. (special (if (consp org-special-ctrl-a/e)
  20702. (car org-special-ctrl-a/e)
  20703. org-special-ctrl-a/e))
  20704. refpos)
  20705. (if (org-bound-and-true-p visual-line-mode)
  20706. (beginning-of-visual-line 1)
  20707. (beginning-of-line 1))
  20708. (if (and arg (fboundp 'move-beginning-of-line))
  20709. (call-interactively 'move-beginning-of-line)
  20710. (if (bobp)
  20711. nil
  20712. (backward-char 1)
  20713. (if (org-truely-invisible-p)
  20714. (while (and (not (bobp)) (org-truely-invisible-p))
  20715. (backward-char 1)
  20716. (beginning-of-line 1))
  20717. (forward-char 1))))
  20718. (when special
  20719. (cond
  20720. ((and (looking-at org-complex-heading-regexp)
  20721. (= (char-after (match-end 1)) ?\ ))
  20722. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20723. (point-at-eol)))
  20724. (goto-char
  20725. (if (eq special t)
  20726. (cond ((> pos refpos) refpos)
  20727. ((= pos (point)) refpos)
  20728. (t (point)))
  20729. (cond ((> pos (point)) (point))
  20730. ((not (eq last-command this-command)) (point))
  20731. (t refpos)))))
  20732. ((org-at-item-p)
  20733. ;; Being at an item and not looking at an the item means point
  20734. ;; was previously moved to beginning of a visual line, which
  20735. ;; doesn't contain the item. Therefore, do nothing special,
  20736. ;; just stay here.
  20737. (when (looking-at org-list-full-item-re)
  20738. ;; Set special position at first white space character after
  20739. ;; bullet, and check-box, if any.
  20740. (let ((after-bullet
  20741. (let ((box (match-end 3)))
  20742. (if (not box) (match-end 1)
  20743. (let ((after (char-after box)))
  20744. (if (and after (= after ? )) (1+ box) box))))))
  20745. ;; Special case: Move point to special position when
  20746. ;; currently after it or at beginning of line.
  20747. (if (eq special t)
  20748. (when (or (> pos after-bullet) (= (point) pos))
  20749. (goto-char after-bullet))
  20750. ;; Reversed case: Move point to special position when
  20751. ;; point was already at beginning of line and command is
  20752. ;; repeated.
  20753. (when (and (= (point) pos) (eq last-command this-command))
  20754. (goto-char after-bullet))))))))
  20755. (org-no-warnings
  20756. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20757. (defun org-end-of-line (&optional arg)
  20758. "Go to the end of the line.
  20759. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20760. tags on the first attempt, and only move to after the tags when
  20761. the cursor is already beyond the end of the headline."
  20762. (interactive "P")
  20763. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20764. org-special-ctrl-a/e))
  20765. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20766. 'end-of-visual-line)
  20767. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20768. (t 'end-of-line))))
  20769. (if (or (not special) arg) (call-interactively move-fun)
  20770. (let* ((element (save-excursion (beginning-of-line)
  20771. (org-element-at-point)))
  20772. (type (org-element-type element)))
  20773. (cond
  20774. ((memq type '(headline inlinetask))
  20775. (let ((pos (point)))
  20776. (beginning-of-line 1)
  20777. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20778. (if (eq special t)
  20779. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20780. (goto-char (match-beginning 1))
  20781. (goto-char (match-end 0)))
  20782. (if (or (< pos (match-end 0))
  20783. (not (eq this-command last-command)))
  20784. (goto-char (match-end 0))
  20785. (goto-char (match-beginning 1))))
  20786. (call-interactively move-fun))))
  20787. ((org-element-property :hiddenp element)
  20788. ;; If element is hidden, `move-end-of-line' would put point
  20789. ;; after it. Use `end-of-line' to stay on current line.
  20790. (call-interactively 'end-of-line))
  20791. (t (call-interactively move-fun)))))
  20792. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20793. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20794. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20795. (defun org-backward-sentence (&optional arg)
  20796. "Go to beginning of sentence, or beginning of table field.
  20797. This will call `backward-sentence' or `org-table-beginning-of-field',
  20798. depending on context."
  20799. (interactive "P")
  20800. (cond
  20801. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20802. (t (call-interactively 'backward-sentence))))
  20803. (defun org-forward-sentence (&optional arg)
  20804. "Go to end of sentence, or end of table field.
  20805. This will call `forward-sentence' or `org-table-end-of-field',
  20806. depending on context."
  20807. (interactive "P")
  20808. (cond
  20809. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20810. (t (call-interactively 'forward-sentence))))
  20811. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20812. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20813. (defun org-kill-line (&optional arg)
  20814. "Kill line, to tags or end of line."
  20815. (interactive "P")
  20816. (cond
  20817. ((or (not org-special-ctrl-k)
  20818. (bolp)
  20819. (not (org-at-heading-p)))
  20820. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20821. org-ctrl-k-protect-subtree)
  20822. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20823. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20824. (error "C-k aborted - would kill hidden subtree")))
  20825. (call-interactively
  20826. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20827. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20828. (kill-region (point) (match-beginning 1))
  20829. (org-set-tags nil t))
  20830. (t (kill-region (point) (point-at-eol)))))
  20831. (define-key org-mode-map "\C-k" 'org-kill-line)
  20832. (defun org-yank (&optional arg)
  20833. "Yank. If the kill is a subtree, treat it specially.
  20834. This command will look at the current kill and check if is a single
  20835. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20836. cursor is at the beginning of a line or after the stars of a currently
  20837. empty headline, then the yank is handled specially. How exactly depends
  20838. on the value of the following variables, both set by default.
  20839. org-yank-folded-subtrees
  20840. When set, the subtree(s) will be folded after insertion, but only
  20841. if doing so would now swallow text after the yanked text.
  20842. org-yank-adjusted-subtrees
  20843. When set, the subtree will be promoted or demoted in order to
  20844. fit into the local outline tree structure, which means that the level
  20845. will be adjusted so that it becomes the smaller one of the two
  20846. *visible* surrounding headings.
  20847. Any prefix to this command will cause `yank' to be called directly with
  20848. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20849. will just
  20850. plainly yank the text as it is.
  20851. \[1] The test checks if the first non-white line is a heading
  20852. and if there are no other headings with fewer stars."
  20853. (interactive "P")
  20854. (org-yank-generic 'yank arg))
  20855. (defun org-yank-generic (command arg)
  20856. "Perform some yank-like command.
  20857. This function implements the behavior described in the `org-yank'
  20858. documentation. However, it has been generalized to work for any
  20859. interactive command with similar behavior."
  20860. ;; pretend to be command COMMAND
  20861. (setq this-command command)
  20862. (if arg
  20863. (call-interactively command)
  20864. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20865. (and (org-kill-is-subtree-p)
  20866. (or (bolp)
  20867. (and (looking-at "[ \t]*$")
  20868. (string-match
  20869. "\\`\\*+\\'"
  20870. (buffer-substring (point-at-bol) (point)))))))
  20871. swallowp)
  20872. (cond
  20873. ((and subtreep org-yank-folded-subtrees)
  20874. (let ((beg (point))
  20875. end)
  20876. (if (and subtreep org-yank-adjusted-subtrees)
  20877. (org-paste-subtree nil nil 'for-yank)
  20878. (call-interactively command))
  20879. (setq end (point))
  20880. (goto-char beg)
  20881. (when (and (bolp) subtreep
  20882. (not (setq swallowp
  20883. (org-yank-folding-would-swallow-text beg end))))
  20884. (org-with-limited-levels
  20885. (or (looking-at org-outline-regexp)
  20886. (re-search-forward org-outline-regexp-bol end t))
  20887. (while (and (< (point) end) (looking-at org-outline-regexp))
  20888. (hide-subtree)
  20889. (org-cycle-show-empty-lines 'folded)
  20890. (condition-case nil
  20891. (outline-forward-same-level 1)
  20892. (error (goto-char end))))))
  20893. (when swallowp
  20894. (message
  20895. "Inserted text not folded because that would swallow text"))
  20896. (goto-char end)
  20897. (skip-chars-forward " \t\n\r")
  20898. (beginning-of-line 1)
  20899. (push-mark beg 'nomsg)))
  20900. ((and subtreep org-yank-adjusted-subtrees)
  20901. (let ((beg (point-at-bol)))
  20902. (org-paste-subtree nil nil 'for-yank)
  20903. (push-mark beg 'nomsg)))
  20904. (t
  20905. (call-interactively command))))))
  20906. (defun org-yank-folding-would-swallow-text (beg end)
  20907. "Would hide-subtree at BEG swallow any text after END?"
  20908. (let (level)
  20909. (org-with-limited-levels
  20910. (save-excursion
  20911. (goto-char beg)
  20912. (when (or (looking-at org-outline-regexp)
  20913. (re-search-forward org-outline-regexp-bol end t))
  20914. (setq level (org-outline-level)))
  20915. (goto-char end)
  20916. (skip-chars-forward " \t\r\n\v\f")
  20917. (if (or (eobp)
  20918. (and (bolp) (looking-at org-outline-regexp)
  20919. (<= (org-outline-level) level)))
  20920. nil ; Nothing would be swallowed
  20921. t))))) ; something would swallow
  20922. (define-key org-mode-map "\C-y" 'org-yank)
  20923. (defun org-truely-invisible-p ()
  20924. "Check if point is at a character currently not visible.
  20925. This version does not only check the character property, but also
  20926. `visible-mode'."
  20927. ;; Early versions of noutline don't have `outline-invisible-p'.
  20928. (if (org-bound-and-true-p visible-mode)
  20929. nil
  20930. (outline-invisible-p)))
  20931. (defun org-invisible-p2 ()
  20932. "Check if point is at a character currently not visible."
  20933. (save-excursion
  20934. (if (and (eolp) (not (bobp))) (backward-char 1))
  20935. ;; Early versions of noutline don't have `outline-invisible-p'.
  20936. (outline-invisible-p)))
  20937. (defun org-back-to-heading (&optional invisible-ok)
  20938. "Call `outline-back-to-heading', but provide a better error message."
  20939. (condition-case nil
  20940. (outline-back-to-heading invisible-ok)
  20941. (error (error "Before first headline at position %d in buffer %s"
  20942. (point) (current-buffer)))))
  20943. (defun org-before-first-heading-p ()
  20944. "Before first heading?"
  20945. (save-excursion
  20946. (end-of-line)
  20947. (null (re-search-backward org-outline-regexp-bol nil t))))
  20948. (defun org-at-heading-p (&optional ignored)
  20949. (outline-on-heading-p t))
  20950. ;; Compatibility alias with Org versions < 7.8.03
  20951. (defalias 'org-on-heading-p 'org-at-heading-p)
  20952. (defun org-at-comment-p nil
  20953. "Is cursor in a line starting with a # character?"
  20954. (save-excursion
  20955. (beginning-of-line)
  20956. (looking-at "^#")))
  20957. (defun org-at-drawer-p nil
  20958. "Is cursor at a drawer keyword?"
  20959. (save-excursion
  20960. (move-beginning-of-line 1)
  20961. (looking-at org-drawer-regexp)))
  20962. (defun org-at-block-p nil
  20963. "Is cursor at a block keyword?"
  20964. (save-excursion
  20965. (move-beginning-of-line 1)
  20966. (looking-at org-block-regexp)))
  20967. (defun org-point-at-end-of-empty-headline ()
  20968. "If point is at the end of an empty headline, return t, else nil.
  20969. If the heading only contains a TODO keyword, it is still still considered
  20970. empty."
  20971. (and (looking-at "[ \t]*$")
  20972. (when org-todo-line-regexp
  20973. (save-excursion
  20974. (beginning-of-line 1)
  20975. (let ((case-fold-search nil))
  20976. (looking-at org-todo-line-regexp)
  20977. (string= (match-string 3) ""))))))
  20978. (defun org-at-heading-or-item-p ()
  20979. (or (org-at-heading-p) (org-at-item-p)))
  20980. (defun org-at-target-p ()
  20981. (or (org-in-regexp org-radio-target-regexp)
  20982. (org-in-regexp org-target-regexp)))
  20983. ;; Compatibility alias with Org versions < 7.8.03
  20984. (defalias 'org-on-target-p 'org-at-target-p)
  20985. (defun org-up-heading-all (arg)
  20986. "Move to the heading line of which the present line is a subheading.
  20987. This function considers both visible and invisible heading lines.
  20988. With argument, move up ARG levels."
  20989. (if (fboundp 'outline-up-heading-all)
  20990. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20991. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20992. (defun org-up-heading-safe ()
  20993. "Move to the heading line of which the present line is a subheading.
  20994. This version will not throw an error. It will return the level of the
  20995. headline found, or nil if no higher level is found.
  20996. Also, this function will be a lot faster than `outline-up-heading',
  20997. because it relies on stars being the outline starters. This can really
  20998. make a significant difference in outlines with very many siblings."
  20999. (let (start-level re)
  21000. (org-back-to-heading t)
  21001. (setq start-level (funcall outline-level))
  21002. (if (equal start-level 1)
  21003. nil
  21004. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21005. (if (re-search-backward re nil t)
  21006. (funcall outline-level)))))
  21007. (defun org-first-sibling-p ()
  21008. "Is this heading the first child of its parents?"
  21009. (interactive)
  21010. (let ((re org-outline-regexp-bol)
  21011. level l)
  21012. (unless (org-at-heading-p t)
  21013. (error "Not at a heading"))
  21014. (setq level (funcall outline-level))
  21015. (save-excursion
  21016. (if (not (re-search-backward re nil t))
  21017. t
  21018. (setq l (funcall outline-level))
  21019. (< l level)))))
  21020. (defun org-goto-sibling (&optional previous)
  21021. "Goto the next sibling, even if it is invisible.
  21022. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21023. when a sibling was found. When none is found, return nil and don't
  21024. move point."
  21025. (let ((fun (if previous 're-search-backward 're-search-forward))
  21026. (pos (point))
  21027. (re org-outline-regexp-bol)
  21028. level l)
  21029. (when (condition-case nil (org-back-to-heading t) (error nil))
  21030. (setq level (funcall outline-level))
  21031. (catch 'exit
  21032. (or previous (forward-char 1))
  21033. (while (funcall fun re nil t)
  21034. (setq l (funcall outline-level))
  21035. (when (< l level) (goto-char pos) (throw 'exit nil))
  21036. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21037. (goto-char pos)
  21038. nil))))
  21039. (defun org-show-siblings ()
  21040. "Show all siblings of the current headline."
  21041. (save-excursion
  21042. (while (org-goto-sibling) (org-flag-heading nil)))
  21043. (save-excursion
  21044. (while (org-goto-sibling 'previous)
  21045. (org-flag-heading nil))))
  21046. (defun org-goto-first-child ()
  21047. "Goto the first child, even if it is invisible.
  21048. Return t when a child was found. Otherwise don't move point and
  21049. return nil."
  21050. (let (level (pos (point)) (re org-outline-regexp-bol))
  21051. (when (condition-case nil (org-back-to-heading t) (error nil))
  21052. (setq level (outline-level))
  21053. (forward-char 1)
  21054. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21055. (progn (goto-char (match-beginning 0)) t)
  21056. (goto-char pos) nil))))
  21057. (defun org-show-hidden-entry ()
  21058. "Show an entry where even the heading is hidden."
  21059. (save-excursion
  21060. (org-show-entry)))
  21061. (defun org-flag-heading (flag &optional entry)
  21062. "Flag the current heading. FLAG non-nil means make invisible.
  21063. When ENTRY is non-nil, show the entire entry."
  21064. (save-excursion
  21065. (org-back-to-heading t)
  21066. ;; Check if we should show the entire entry
  21067. (if entry
  21068. (progn
  21069. (org-show-entry)
  21070. (save-excursion
  21071. (and (outline-next-heading)
  21072. (org-flag-heading nil))))
  21073. (outline-flag-region (max (point-min) (1- (point)))
  21074. (save-excursion (outline-end-of-heading) (point))
  21075. flag))))
  21076. (defun org-get-next-sibling ()
  21077. "Move to next heading of the same level, and return point.
  21078. If there is no such heading, return nil.
  21079. This is like outline-next-sibling, but invisible headings are ok."
  21080. (let ((level (funcall outline-level)))
  21081. (outline-next-heading)
  21082. (while (and (not (eobp)) (> (funcall outline-level) level))
  21083. (outline-next-heading))
  21084. (if (or (eobp) (< (funcall outline-level) level))
  21085. nil
  21086. (point))))
  21087. (defun org-get-last-sibling ()
  21088. "Move to previous heading of the same level, and return point.
  21089. If there is no such heading, return nil."
  21090. (let ((opoint (point))
  21091. (level (funcall outline-level)))
  21092. (outline-previous-heading)
  21093. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21094. (while (and (> (funcall outline-level) level)
  21095. (not (bobp)))
  21096. (outline-previous-heading))
  21097. (if (< (funcall outline-level) level)
  21098. nil
  21099. (point)))))
  21100. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21101. "Goto to the end of a subtree."
  21102. ;; This contains an exact copy of the original function, but it uses
  21103. ;; `org-back-to-heading', to make it work also in invisible
  21104. ;; trees. And is uses an invisible-ok argument.
  21105. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21106. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21107. ;; with many children and grandchildren etc, this can be much faster
  21108. ;; than the outline version.
  21109. (org-back-to-heading invisible-ok)
  21110. (let ((first t)
  21111. (level (funcall outline-level)))
  21112. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21113. ;; A true heading (not a plain list item), in Org-mode
  21114. ;; This means we can easily find the end by looking
  21115. ;; only for the right number of stars. Using a regexp to do
  21116. ;; this is so much faster than using a Lisp loop.
  21117. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21118. (forward-char 1)
  21119. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21120. ;; something else, do it the slow way
  21121. (while (and (not (eobp))
  21122. (or first (> (funcall outline-level) level)))
  21123. (setq first nil)
  21124. (outline-next-heading)))
  21125. (unless to-heading
  21126. (if (memq (preceding-char) '(?\n ?\^M))
  21127. (progn
  21128. ;; Go to end of line before heading
  21129. (forward-char -1)
  21130. (if (memq (preceding-char) '(?\n ?\^M))
  21131. ;; leave blank line before heading
  21132. (forward-char -1))))))
  21133. (point))
  21134. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21135. "Use Org version in org-mode, for dramatic speed-up."
  21136. (if (derived-mode-p 'org-mode)
  21137. (progn
  21138. (org-end-of-subtree nil t)
  21139. (unless (eobp) (backward-char 1)))
  21140. ad-do-it))
  21141. (defun org-end-of-meta-data-and-drawers ()
  21142. "Jump to the first text after meta data and drawers in the current entry.
  21143. This will move over empty lines, lines with planning time stamps,
  21144. clocking lines, and drawers."
  21145. (org-back-to-heading t)
  21146. (let ((end (save-excursion (outline-next-heading) (point)))
  21147. (re (concat "\\(" org-drawer-regexp "\\)"
  21148. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21149. (forward-line 1)
  21150. (while (re-search-forward re end t)
  21151. (if (not (match-end 1))
  21152. ;; empty or planning line
  21153. (forward-line 1)
  21154. ;; a drawer, find the end
  21155. (re-search-forward "^[ \t]*:END:" end 'move)
  21156. (forward-line 1)))
  21157. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21158. (point)))
  21159. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21160. "Move forward to the ARG'th subheading at same level as this one.
  21161. Stop at the first and last subheadings of a superior heading.
  21162. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21163. non-nil it will also look at invisible ones."
  21164. (interactive "p")
  21165. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21166. (if (and arg (< arg 0))
  21167. (goto-char (point-min))
  21168. (outline-next-heading))
  21169. (org-at-heading-p)
  21170. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21171. (f (if (and arg (< arg 0))
  21172. 're-search-backward
  21173. 're-search-forward))
  21174. (count (if arg (abs arg) 1))
  21175. (result (point)))
  21176. (forward-char (if (and arg (< arg 0)) -1 1))
  21177. (while (and (> count 0)
  21178. (funcall f org-outline-regexp-bol nil 'move))
  21179. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21180. (cond ((< l level) (setq count 0))
  21181. ((and (= l level)
  21182. (or invisible-ok
  21183. (progn
  21184. (goto-char (line-beginning-position))
  21185. (not (outline-invisible-p)))))
  21186. (setq count (1- count))
  21187. (when (eq l level)
  21188. (setq result (point)))))))
  21189. (goto-char result))
  21190. (beginning-of-line 1)))
  21191. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21192. "Move backward to the ARG'th subheading at same level as this one.
  21193. Stop at the first and last subheadings of a superior heading."
  21194. (interactive "p")
  21195. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21196. (defun org-next-block (arg &optional backward block-regexp)
  21197. "Jump to the next block.
  21198. With a prefix argument ARG, jump forward ARG many source blocks.
  21199. When BACKWARD is non-nil, jump to the previous block.
  21200. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21201. (interactive "p")
  21202. (let ((re (or block-regexp org-block-regexp))
  21203. (re-search-fn (or (and backward 're-search-backward)
  21204. 're-search-forward)))
  21205. (if (looking-at re) (forward-char 1))
  21206. (condition-case nil
  21207. (funcall re-search-fn re nil nil arg)
  21208. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21209. (goto-char (match-beginning 0)) (org-show-context)))
  21210. (defun org-previous-block (arg &optional block-regexp)
  21211. "Jump to the previous block.
  21212. With a prefix argument ARG, jump backward ARG many source blocks.
  21213. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21214. (interactive "p")
  21215. (org-next-block arg t block-regexp))
  21216. (defun org-forward-element ()
  21217. "Move forward by one element.
  21218. Move to the next element at the same level, when possible."
  21219. (interactive)
  21220. (cond ((eobp) (user-error "Cannot move further down"))
  21221. ((org-with-limited-levels (org-at-heading-p))
  21222. (let ((origin (point)))
  21223. (goto-char (org-end-of-subtree nil t))
  21224. (unless (org-with-limited-levels (org-at-heading-p))
  21225. (goto-char origin)
  21226. (user-error "Cannot move further down"))))
  21227. (t
  21228. (let* ((elem (org-element-at-point))
  21229. (end (org-element-property :end elem))
  21230. (parent (org-element-property :parent elem)))
  21231. (if (and parent (= (org-element-property :contents-end parent) end))
  21232. (goto-char (org-element-property :end parent))
  21233. (goto-char end))))))
  21234. (defun org-backward-element ()
  21235. "Move backward by one element.
  21236. Move to the previous element at the same level, when possible."
  21237. (interactive)
  21238. (cond ((bobp) (user-error "Cannot move further up"))
  21239. ((org-with-limited-levels (org-at-heading-p))
  21240. ;; At a headline, move to the previous one, if any, or stay
  21241. ;; here.
  21242. (let ((origin (point)))
  21243. (org-with-limited-levels (org-backward-heading-same-level 1))
  21244. ;; When current headline has no sibling above, move to its
  21245. ;; parent.
  21246. (when (= (point) origin)
  21247. (or (org-with-limited-levels (org-up-heading-safe))
  21248. (progn (goto-char origin)
  21249. (user-error "Cannot move further up"))))))
  21250. (t
  21251. (let* ((trail (org-element-at-point 'keep-trail))
  21252. (elem (car trail))
  21253. (prev-elem (nth 1 trail))
  21254. (beg (org-element-property :begin elem)))
  21255. (cond
  21256. ;; Move to beginning of current element if point isn't
  21257. ;; there already.
  21258. ((/= (point) beg) (goto-char beg))
  21259. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21260. ((org-before-first-heading-p) (goto-char (point-min)))
  21261. (t (org-back-to-heading)))))))
  21262. (defun org-up-element ()
  21263. "Move to upper element."
  21264. (interactive)
  21265. (if (org-with-limited-levels (org-at-heading-p))
  21266. (unless (org-up-heading-safe) (error "No surrounding element"))
  21267. (let* ((elem (org-element-at-point))
  21268. (parent (org-element-property :parent elem)))
  21269. (if parent (goto-char (org-element-property :begin parent))
  21270. (if (org-with-limited-levels (org-before-first-heading-p))
  21271. (error "No surrounding element")
  21272. (org-with-limited-levels (org-back-to-heading)))))))
  21273. (defvar org-element-greater-elements)
  21274. (defun org-down-element ()
  21275. "Move to inner element."
  21276. (interactive)
  21277. (let ((element (org-element-at-point)))
  21278. (cond
  21279. ((memq (org-element-type element) '(plain-list table))
  21280. (goto-char (org-element-property :contents-begin element))
  21281. (forward-char))
  21282. ((memq (org-element-type element) org-element-greater-elements)
  21283. ;; If contents are hidden, first disclose them.
  21284. (when (org-element-property :hiddenp element) (org-cycle))
  21285. (goto-char (or (org-element-property :contents-begin element)
  21286. (error "No content for this element"))))
  21287. (t (error "No inner element")))))
  21288. (defun org-drag-element-backward ()
  21289. "Move backward element at point."
  21290. (interactive)
  21291. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21292. (let* ((trail (org-element-at-point 'keep-trail))
  21293. (elem (car trail))
  21294. (prev-elem (nth 1 trail)))
  21295. ;; Error out if no previous element or previous element is
  21296. ;; a parent of the current one.
  21297. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21298. (error "Cannot drag element backward")
  21299. (let ((pos (point)))
  21300. (org-element-swap-A-B prev-elem elem)
  21301. (goto-char (+ (org-element-property :begin prev-elem)
  21302. (- pos (org-element-property :begin elem)))))))))
  21303. (defun org-drag-element-forward ()
  21304. "Move forward element at point."
  21305. (interactive)
  21306. (let* ((pos (point))
  21307. (elem (org-element-at-point)))
  21308. (when (= (point-max) (org-element-property :end elem))
  21309. (error "Cannot drag element forward"))
  21310. (goto-char (org-element-property :end elem))
  21311. (let ((next-elem (org-element-at-point)))
  21312. (when (or (org-element-nested-p elem next-elem)
  21313. (and (eq (org-element-type next-elem) 'headline)
  21314. (not (eq (org-element-type elem) 'headline))))
  21315. (goto-char pos)
  21316. (error "Cannot drag element forward"))
  21317. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21318. ;; body's length (without final blanks) and by the length of
  21319. ;; blanks between ELEM and NEXT-ELEM.
  21320. (let ((size-next (- (save-excursion
  21321. (goto-char (org-element-property :end next-elem))
  21322. (skip-chars-backward " \r\t\n")
  21323. (forward-line)
  21324. ;; Small correction if buffer doesn't end
  21325. ;; with a newline character.
  21326. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21327. (org-element-property :begin next-elem)))
  21328. (size-blank (- (org-element-property :end elem)
  21329. (save-excursion
  21330. (goto-char (org-element-property :end elem))
  21331. (skip-chars-backward " \r\t\n")
  21332. (forward-line)
  21333. (point)))))
  21334. (org-element-swap-A-B elem next-elem)
  21335. (goto-char (+ pos size-next size-blank))))))
  21336. (defun org-drag-line-forward (arg)
  21337. "Drag the line at point ARG lines forward."
  21338. (interactive "p")
  21339. (dotimes (n (abs arg))
  21340. (let ((c (current-column)))
  21341. (if (< 0 arg)
  21342. (progn
  21343. (beginning-of-line 2)
  21344. (transpose-lines 1)
  21345. (beginning-of-line 0))
  21346. (transpose-lines 1)
  21347. (beginning-of-line -1))
  21348. (org-move-to-column c))))
  21349. (defun org-drag-line-backward (arg)
  21350. "Drag the line at point ARG lines backward."
  21351. (interactive "p")
  21352. (org-drag-line-forward (- arg)))
  21353. (defun org-mark-element ()
  21354. "Put point at beginning of this element, mark at end.
  21355. Interactively, if this command is repeated or (in Transient Mark
  21356. mode) if the mark is active, it marks the next element after the
  21357. ones already marked."
  21358. (interactive)
  21359. (let (deactivate-mark)
  21360. (if (and (org-called-interactively-p 'any)
  21361. (or (and (eq last-command this-command) (mark t))
  21362. (and transient-mark-mode mark-active)))
  21363. (set-mark
  21364. (save-excursion
  21365. (goto-char (mark))
  21366. (goto-char (org-element-property :end (org-element-at-point)))))
  21367. (let ((element (org-element-at-point)))
  21368. (end-of-line)
  21369. (push-mark (org-element-property :end element) t t)
  21370. (goto-char (org-element-property :begin element))))))
  21371. (defun org-narrow-to-element ()
  21372. "Narrow buffer to current element."
  21373. (interactive)
  21374. (let ((elem (org-element-at-point)))
  21375. (cond
  21376. ((eq (car elem) 'headline)
  21377. (narrow-to-region
  21378. (org-element-property :begin elem)
  21379. (org-element-property :end elem)))
  21380. ((memq (car elem) org-element-greater-elements)
  21381. (narrow-to-region
  21382. (org-element-property :contents-begin elem)
  21383. (org-element-property :contents-end elem)))
  21384. (t
  21385. (narrow-to-region
  21386. (org-element-property :begin elem)
  21387. (org-element-property :end elem))))))
  21388. (defun org-transpose-element ()
  21389. "Transpose current and previous elements, keeping blank lines between.
  21390. Point is moved after both elements."
  21391. (interactive)
  21392. (org-skip-whitespace)
  21393. (let ((end (org-element-property :end (org-element-at-point))))
  21394. (org-drag-element-backward)
  21395. (goto-char end)))
  21396. (defun org-unindent-buffer ()
  21397. "Un-indent the visible part of the buffer.
  21398. Relative indentation (between items, inside blocks, etc.) isn't
  21399. modified."
  21400. (interactive)
  21401. (unless (eq major-mode 'org-mode)
  21402. (error "Cannot un-indent a buffer not in Org mode"))
  21403. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21404. unindent-tree ; For byte-compiler.
  21405. (unindent-tree
  21406. (function
  21407. (lambda (contents)
  21408. (mapc
  21409. (lambda (element)
  21410. (if (memq (org-element-type element) '(headline section))
  21411. (funcall unindent-tree (org-element-contents element))
  21412. (save-excursion
  21413. (save-restriction
  21414. (narrow-to-region
  21415. (org-element-property :begin element)
  21416. (org-element-property :end element))
  21417. (org-do-remove-indentation)))))
  21418. (reverse contents))))))
  21419. (funcall unindent-tree (org-element-contents parse-tree))))
  21420. (defun org-show-subtree ()
  21421. "Show everything after this heading at deeper levels."
  21422. (interactive)
  21423. (outline-flag-region
  21424. (point)
  21425. (save-excursion
  21426. (org-end-of-subtree t t))
  21427. nil))
  21428. (defun org-show-entry ()
  21429. "Show the body directly following this heading.
  21430. Show the heading too, if it is currently invisible."
  21431. (interactive)
  21432. (save-excursion
  21433. (condition-case nil
  21434. (progn
  21435. (org-back-to-heading t)
  21436. (outline-flag-region
  21437. (max (point-min) (1- (point)))
  21438. (save-excursion
  21439. (if (re-search-forward
  21440. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21441. (match-beginning 1)
  21442. (point-max)))
  21443. nil)
  21444. (org-cycle-hide-drawers 'children))
  21445. (error nil))))
  21446. (defun org-make-options-regexp (kwds &optional extra)
  21447. "Make a regular expression for keyword lines."
  21448. (concat
  21449. "^#\\+\\("
  21450. (mapconcat 'regexp-quote kwds "\\|")
  21451. (if extra (concat "\\|" extra))
  21452. "\\):[ \t]*\\(.*\\)"))
  21453. ;; Make isearch reveal the necessary context
  21454. (defun org-isearch-end ()
  21455. "Reveal context after isearch exits."
  21456. (when isearch-success ; only if search was successful
  21457. (if (featurep 'xemacs)
  21458. ;; Under XEmacs, the hook is run in the correct place,
  21459. ;; we directly show the context.
  21460. (org-show-context 'isearch)
  21461. ;; In Emacs the hook runs *before* restoring the overlays.
  21462. ;; So we have to use a one-time post-command-hook to do this.
  21463. ;; (Emacs 22 has a special variable, see function `org-mode')
  21464. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21465. isearch-mode-end-hook-quit)
  21466. ;; Only when the isearch was not quitted.
  21467. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21468. 'append 'local)))
  21469. (org-fix-ellipsis-at-bol)))
  21470. (defun org-isearch-post-command ()
  21471. "Remove self from hook, and show context."
  21472. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21473. (org-show-context 'isearch))
  21474. ;;;; Integration with and fixes for other packages
  21475. ;;; Imenu support
  21476. (defvar org-imenu-markers nil
  21477. "All markers currently used by Imenu.")
  21478. (make-variable-buffer-local 'org-imenu-markers)
  21479. (defun org-imenu-new-marker (&optional pos)
  21480. "Return a new marker for use by Imenu, and remember the marker."
  21481. (let ((m (make-marker)))
  21482. (move-marker m (or pos (point)))
  21483. (push m org-imenu-markers)
  21484. m))
  21485. (defun org-imenu-get-tree ()
  21486. "Produce the index for Imenu."
  21487. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21488. (setq org-imenu-markers nil)
  21489. (let* ((n org-imenu-depth)
  21490. (re (concat "^" (org-get-limited-outline-regexp)))
  21491. (subs (make-vector (1+ n) nil))
  21492. (last-level 0)
  21493. m level head0 head)
  21494. (save-excursion
  21495. (save-restriction
  21496. (widen)
  21497. (goto-char (point-max))
  21498. (while (re-search-backward re nil t)
  21499. (setq level (org-reduced-level (funcall outline-level)))
  21500. (when (and (<= level n)
  21501. (looking-at org-complex-heading-regexp)
  21502. (setq head0 (org-match-string-no-properties 4)))
  21503. (setq head (org-link-display-format head0)
  21504. m (org-imenu-new-marker))
  21505. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21506. (if (>= level last-level)
  21507. (push (cons head m) (aref subs level))
  21508. (push (cons head (aref subs (1+ level))) (aref subs level))
  21509. (loop for i from (1+ level) to n do (aset subs i nil)))
  21510. (setq last-level level)))))
  21511. (aref subs 1)))
  21512. (eval-after-load "imenu"
  21513. '(progn
  21514. (add-hook 'imenu-after-jump-hook
  21515. (lambda ()
  21516. (if (derived-mode-p 'org-mode)
  21517. (org-show-context 'org-goto))))))
  21518. (defun org-link-display-format (link)
  21519. "Replace a link with its the description.
  21520. If there is no description, use the link target."
  21521. (save-match-data
  21522. (if (string-match org-bracket-link-analytic-regexp link)
  21523. (replace-match (if (match-end 5)
  21524. (match-string 5 link)
  21525. (concat (match-string 1 link)
  21526. (match-string 3 link)))
  21527. nil t link)
  21528. link)))
  21529. (defun org-toggle-link-display ()
  21530. "Toggle the literal or descriptive display of links."
  21531. (interactive)
  21532. (if org-descriptive-links
  21533. (progn (org-remove-from-invisibility-spec '(org-link))
  21534. (org-restart-font-lock)
  21535. (setq org-descriptive-links nil))
  21536. (progn (add-to-invisibility-spec '(org-link))
  21537. (org-restart-font-lock)
  21538. (setq org-descriptive-links t))))
  21539. ;; Speedbar support
  21540. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21541. "Overlay marking the agenda restriction line in speedbar.")
  21542. (overlay-put org-speedbar-restriction-lock-overlay
  21543. 'face 'org-agenda-restriction-lock)
  21544. (overlay-put org-speedbar-restriction-lock-overlay
  21545. 'help-echo "Agendas are currently limited to this item.")
  21546. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21547. (defun org-speedbar-set-agenda-restriction ()
  21548. "Restrict future agenda commands to the location at point in speedbar.
  21549. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21550. (interactive)
  21551. (require 'org-agenda)
  21552. (let (p m tp np dir txt)
  21553. (cond
  21554. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21555. 'org-imenu t))
  21556. (setq m (get-text-property p 'org-imenu-marker))
  21557. (with-current-buffer (marker-buffer m)
  21558. (goto-char m)
  21559. (org-agenda-set-restriction-lock 'subtree)))
  21560. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21561. 'speedbar-function 'speedbar-find-file))
  21562. (setq tp (previous-single-property-change
  21563. (1+ p) 'speedbar-function)
  21564. np (next-single-property-change
  21565. tp 'speedbar-function)
  21566. dir (speedbar-line-directory)
  21567. txt (buffer-substring-no-properties (or tp (point-min))
  21568. (or np (point-max))))
  21569. (with-current-buffer (find-file-noselect
  21570. (let ((default-directory dir))
  21571. (expand-file-name txt)))
  21572. (unless (derived-mode-p 'org-mode)
  21573. (error "Cannot restrict to non-Org-mode file"))
  21574. (org-agenda-set-restriction-lock 'file)))
  21575. (t (error "Don't know how to restrict Org-mode's agenda")))
  21576. (move-overlay org-speedbar-restriction-lock-overlay
  21577. (point-at-bol) (point-at-eol))
  21578. (setq current-prefix-arg nil)
  21579. (org-agenda-maybe-redo)))
  21580. (eval-after-load "speedbar"
  21581. '(progn
  21582. (speedbar-add-supported-extension ".org")
  21583. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21584. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21585. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21586. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21587. (add-hook 'speedbar-visiting-tag-hook
  21588. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21589. ;;; Fixes and Hacks for problems with other packages
  21590. ;; Make flyspell not check words in links, to not mess up our keymap
  21591. (defvar org-element-affiliated-keywords) ; From org-element.el
  21592. (defvar org-element-block-name-alist) ; From org-element.el
  21593. (defun org-mode-flyspell-verify ()
  21594. "Don't let flyspell put overlays at active buttons, or on
  21595. {todo,all-time,additional-option-like}-keywords."
  21596. (require 'org-element) ; For `org-element-affiliated-keywords'
  21597. (let ((pos (max (1- (point)) (point-min)))
  21598. (word (thing-at-point 'word)))
  21599. (and (not (get-text-property pos 'keymap))
  21600. (not (get-text-property pos 'org-no-flyspell))
  21601. (not (member word org-todo-keywords-1))
  21602. (not (member word org-all-time-keywords))
  21603. (not (member word org-options-keywords))
  21604. (not (member word (mapcar 'car org-startup-options)))
  21605. (not (member-ignore-case word org-element-affiliated-keywords))
  21606. (not (member-ignore-case word (org-get-export-keywords)))
  21607. (not (member-ignore-case
  21608. word (mapcar 'car org-element-block-name-alist)))
  21609. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21610. (defun org-remove-flyspell-overlays-in (beg end)
  21611. "Remove flyspell overlays in region."
  21612. (and (org-bound-and-true-p flyspell-mode)
  21613. (fboundp 'flyspell-delete-region-overlays)
  21614. (flyspell-delete-region-overlays beg end))
  21615. (add-text-properties beg end '(org-no-flyspell t)))
  21616. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21617. (eval-after-load "bookmark"
  21618. '(if (boundp 'bookmark-after-jump-hook)
  21619. ;; We can use the hook
  21620. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21621. ;; Hook not available, use advice
  21622. (defadvice bookmark-jump (after org-make-visible activate)
  21623. "Make the position visible."
  21624. (org-bookmark-jump-unhide))))
  21625. ;; Make sure saveplace shows the location if it was hidden
  21626. (eval-after-load "saveplace"
  21627. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21628. "Make the position visible."
  21629. (org-bookmark-jump-unhide)))
  21630. ;; Make sure ecb shows the location if it was hidden
  21631. (eval-after-load "ecb"
  21632. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21633. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21634. (if (derived-mode-p 'org-mode)
  21635. (org-show-context))))
  21636. (defun org-bookmark-jump-unhide ()
  21637. "Unhide the current position, to show the bookmark location."
  21638. (and (derived-mode-p 'org-mode)
  21639. (or (outline-invisible-p)
  21640. (save-excursion (goto-char (max (point-min) (1- (point))))
  21641. (outline-invisible-p)))
  21642. (org-show-context 'bookmark-jump)))
  21643. ;; Make session.el ignore our circular variable
  21644. (eval-after-load "session"
  21645. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21646. ;;;; Finish up
  21647. (provide 'org)
  21648. (run-hooks 'org-load-hook)
  21649. ;;; org.el ends here