org.el 853 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 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. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  74. ;; some places -- e.g. see the macro org-with-limited-levels.
  75. ;;
  76. ;; In Org buffers, the value of `outline-regexp' is that of
  77. ;; `org-outline-regexp'. The only function still directly relying on
  78. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  79. ;; job when `orgstruct-mode' is active.
  80. (defvar org-outline-regexp "\\*+ "
  81. "Regexp to match Org headlines.")
  82. (defvar org-outline-regexp-bol "^\\*+ "
  83. "Regexp to match Org headlines.
  84. This is similar to `org-outline-regexp' but additionally makes
  85. sure that we are at the beginning of the line.")
  86. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  87. "Matches an headline, putting stars and text into groups.
  88. Stars are put in group 1 and the trimmed body in group 2.")
  89. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  90. (when (fboundp 'defvaralias)
  91. (unless (boundp 'calendar-view-holidays-initially-flag)
  92. (defvaralias 'calendar-view-holidays-initially-flag
  93. 'view-calendar-holidays-initially))
  94. (unless (boundp 'calendar-view-diary-initially-flag)
  95. (defvaralias 'calendar-view-diary-initially-flag
  96. 'view-diary-entries-initially))
  97. (unless (boundp 'diary-fancy-buffer)
  98. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  99. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  100. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  101. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  102. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  103. (declare-function org-at-clock-log-p "org-clock" ())
  104. (declare-function org-clock-timestamps-up "org-clock" ())
  105. (declare-function org-clock-timestamps-down "org-clock" ())
  106. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  107. ;; load languages based on value of `org-babel-load-languages'
  108. (defvar org-babel-load-languages)
  109. ;;;###autoload
  110. (defun org-babel-do-load-languages (sym value)
  111. "Load the languages defined in `org-babel-load-languages'."
  112. (set-default sym value)
  113. (mapc (lambda (pair)
  114. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  115. (if active
  116. (progn
  117. (require (intern (concat "ob-" lang))))
  118. (progn
  119. (funcall 'fmakunbound
  120. (intern (concat "org-babel-execute:" lang)))
  121. (funcall 'fmakunbound
  122. (intern (concat "org-babel-expand-body:" lang)))))))
  123. org-babel-load-languages))
  124. (defcustom org-babel-load-languages '((emacs-lisp . t))
  125. "Languages which can be evaluated in Org-mode buffers.
  126. This list can be used to load support for any of the languages
  127. below, note that each language will depend on a different set of
  128. system executables and/or Emacs modes. When a language is
  129. \"loaded\", then code blocks in that language can be evaluated
  130. with `org-babel-execute-src-block' bound by default to C-c
  131. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  132. be set to remove code block evaluation from the C-c C-c
  133. keybinding. By default only Emacs Lisp (which has no
  134. requirements) is loaded."
  135. :group 'org-babel
  136. :set 'org-babel-do-load-languages
  137. :version "24.1"
  138. :type '(alist :tag "Babel Languages"
  139. :key-type
  140. (choice
  141. (const :tag "Awk" awk)
  142. (const :tag "C" C)
  143. (const :tag "R" R)
  144. (const :tag "Asymptote" asymptote)
  145. (const :tag "Calc" calc)
  146. (const :tag "Clojure" clojure)
  147. (const :tag "CSS" css)
  148. (const :tag "Ditaa" ditaa)
  149. (const :tag "Dot" dot)
  150. (const :tag "Emacs Lisp" emacs-lisp)
  151. (const :tag "Fortran" fortran)
  152. (const :tag "Gnuplot" gnuplot)
  153. (const :tag "Haskell" haskell)
  154. (const :tag "IO" io)
  155. (const :tag "Java" java)
  156. (const :tag "Javascript" js)
  157. (const :tag "LaTeX" latex)
  158. (const :tag "Ledger" ledger)
  159. (const :tag "Lilypond" lilypond)
  160. (const :tag "Lisp" lisp)
  161. (const :tag "Maxima" maxima)
  162. (const :tag "Matlab" matlab)
  163. (const :tag "Mscgen" mscgen)
  164. (const :tag "Ocaml" ocaml)
  165. (const :tag "Octave" octave)
  166. (const :tag "Org" org)
  167. (const :tag "Perl" perl)
  168. (const :tag "Pico Lisp" picolisp)
  169. (const :tag "PlantUML" plantuml)
  170. (const :tag "Python" python)
  171. (const :tag "Ruby" ruby)
  172. (const :tag "Sass" sass)
  173. (const :tag "Scala" scala)
  174. (const :tag "Scheme" scheme)
  175. (const :tag "Screen" screen)
  176. (const :tag "Shell Script" sh)
  177. (const :tag "Shen" shen)
  178. (const :tag "Sql" sql)
  179. (const :tag "Sqlite" sqlite))
  180. :value-type (boolean :tag "Activate" :value t)))
  181. ;;;; Customization variables
  182. (defcustom org-clone-delete-id nil
  183. "Remove ID property of clones of a subtree.
  184. When non-nil, clones of a subtree don't inherit the ID property.
  185. Otherwise they inherit the ID property with a new unique
  186. identifier."
  187. :type 'boolean
  188. :version "24.1"
  189. :group 'org-id)
  190. ;;; Version
  191. (require 'org-compat)
  192. (org-check-version)
  193. ;;;###autoload
  194. (defun org-version (&optional here full message)
  195. "Show the org-mode version in the echo area.
  196. With prefix argument HERE, insert it at point.
  197. When FULL is non-nil, use a verbose version string.
  198. When MESSAGE is non-nil, display a message with the version."
  199. (interactive "P")
  200. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  201. (org-install-dir (ignore-errors (org-find-library-dir "org-install.el")))
  202. (org-trash (or
  203. (and (fboundp 'org-release) (fboundp 'org-git-version))
  204. (load (concat org-dir "org-version.el")
  205. 'noerror 'nomessage 'nosuffix)))
  206. (org-version (org-release))
  207. (git-version (org-git-version))
  208. (version (format "Org-mode version %s (%s @ %s)"
  209. org-version
  210. git-version
  211. (if org-install-dir
  212. (if (string= org-dir org-install-dir)
  213. org-install-dir
  214. (concat "mixed installation! " org-install-dir " and " org-dir))
  215. "org-install.el can not be found!")))
  216. (_version (if full version org-version)))
  217. (if (org-called-interactively-p 'interactive)
  218. (if here
  219. (insert version)
  220. (message version))
  221. (if message (message _version))
  222. _version)))
  223. (defconst org-version (org-version))
  224. ;;; Compatibility constants
  225. ;;; The custom variables
  226. (defgroup org nil
  227. "Outline-based notes management and organizer."
  228. :tag "Org"
  229. :group 'outlines
  230. :group 'calendar)
  231. (defcustom org-mode-hook nil
  232. "Mode hook for Org-mode, run after the mode was turned on."
  233. :group 'org
  234. :type 'hook)
  235. (defcustom org-load-hook nil
  236. "Hook that is run after org.el has been loaded."
  237. :group 'org
  238. :type 'hook)
  239. (defcustom org-log-buffer-setup-hook nil
  240. "Hook that is run after an Org log buffer is created."
  241. :group 'org
  242. :version "24.1"
  243. :type 'hook)
  244. (defvar org-modules) ; defined below
  245. (defvar org-modules-loaded nil
  246. "Have the modules been loaded already?")
  247. (defun org-load-modules-maybe (&optional force)
  248. "Load all extensions listed in `org-modules'."
  249. (when (or force (not org-modules-loaded))
  250. (mapc (lambda (ext)
  251. (condition-case nil (require ext)
  252. (error (message "Problems while trying to load feature `%s'" ext))))
  253. org-modules)
  254. (setq org-modules-loaded t)))
  255. (defun org-set-modules (var value)
  256. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  257. (set var value)
  258. (when (featurep 'org)
  259. (org-load-modules-maybe 'force)))
  260. (when (org-bound-and-true-p org-modules)
  261. (let ((a (member 'org-infojs org-modules)))
  262. (and a (setcar a 'org-jsinfo))))
  263. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  264. "Modules that should always be loaded together with org.el.
  265. If a description starts with <C>, the file is not part of Emacs
  266. and loading it will require that you have downloaded and properly installed
  267. the org-mode distribution.
  268. You can also use this system to load external packages (i.e. neither Org
  269. core modules, nor modules from the CONTRIB directory). Just add symbols
  270. to the end of the list. If the package is called org-xyz.el, then you need
  271. to add the symbol `xyz', and the package must have a call to
  272. (provide 'org-xyz)"
  273. :group 'org
  274. :set 'org-set-modules
  275. :type
  276. '(set :greedy t
  277. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  278. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  279. (const :tag " crypt: Encryption of subtrees" org-crypt)
  280. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  281. (const :tag " docview: Links to doc-view buffers" org-docview)
  282. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  283. (const :tag " id: Global IDs for identifying entries" org-id)
  284. (const :tag " info: Links to Info nodes" org-info)
  285. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  286. (const :tag " habit: Track your consistency with habits" org-habit)
  287. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  288. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  289. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  290. (const :tag " mew Links to Mew folders/messages" org-mew)
  291. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  292. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  293. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  294. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  295. (const :tag " vm: Links to VM folders/messages" org-vm)
  296. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  297. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  298. (const :tag " mouse: Additional mouse support" org-mouse)
  299. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  300. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  301. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  302. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  303. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  304. (const :tag "C collector: Collect properties into tables" org-collector)
  305. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  306. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  307. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  308. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  309. (const :tag "C eval: Include command output as text" org-eval)
  310. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  311. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  312. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  313. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  314. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  315. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  316. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  317. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  318. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  319. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  320. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  321. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  322. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  323. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  324. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  325. (const :tag "C registry: A registry for Org-mode links" org-registry)
  326. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  327. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  328. (const :tag "C secretary: Team management with org-mode" org-secretary)
  329. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  330. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  331. (const :tag "C track: Keep up with Org-mode development" org-track)
  332. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  333. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  334. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  335. (defcustom org-support-shift-select nil
  336. "Non-nil means make shift-cursor commands select text when possible.
  337. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  338. start selecting a region, or enlarge regions started in this way.
  339. In Org-mode, in special contexts, these same keys are used for
  340. other purposes, important enough to compete with shift selection.
  341. Org tries to balance these needs by supporting `shift-select-mode'
  342. outside these special contexts, under control of this variable.
  343. The default of this variable is nil, to avoid confusing behavior. Shifted
  344. cursor keys will then execute Org commands in the following contexts:
  345. - on a headline, changing TODO state (left/right) and priority (up/down)
  346. - on a time stamp, changing the time
  347. - in a plain list item, changing the bullet type
  348. - in a property definition line, switching between allowed values
  349. - in the BEGIN line of a clock table (changing the time block).
  350. Outside these contexts, the commands will throw an error.
  351. When this variable is t and the cursor is not in a special
  352. context, Org-mode will support shift-selection for making and
  353. enlarging regions. To make this more effective, the bullet
  354. cycling will no longer happen anywhere in an item line, but only
  355. if the cursor is exactly on the bullet.
  356. If you set this variable to the symbol `always', then the keys
  357. will not be special in headlines, property lines, and item lines,
  358. to make shift selection work there as well. If this is what you
  359. want, you can use the following alternative commands: `C-c C-t'
  360. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  361. can be used to switch TODO sets, `C-c -' to cycle item bullet
  362. types, and properties can be edited by hand or in column view.
  363. However, when the cursor is on a timestamp, shift-cursor commands
  364. will still edit the time stamp - this is just too good to give up.
  365. XEmacs user should have this variable set to nil, because
  366. `shift-select-mode' is in Emacs 23 or later only."
  367. :group 'org
  368. :type '(choice
  369. (const :tag "Never" nil)
  370. (const :tag "When outside special context" t)
  371. (const :tag "Everywhere except timestamps" always)))
  372. (defcustom org-loop-over-headlines-in-active-region nil
  373. "Shall some commands act upon headlines in the active region?
  374. When set to `t', some commands will be performed in all headlines
  375. within the active region.
  376. When set to `start-level', some commands will be performed in all
  377. headlines within the active region, provided that these headlines
  378. are of the same level than the first one.
  379. When set to a string, those commands will be performed on the
  380. matching headlines within the active region. Such string must be
  381. a tags/property/todo match as it is used in the agenda tags view.
  382. The list of commands is: `org-schedule', `org-deadline',
  383. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  384. `org-archive-to-archive-sibling'. The archiving commands skip
  385. already archived entries."
  386. :type '(choice (const :tag "Don't loop" nil)
  387. (const :tag "All headlines in active region" t)
  388. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  389. (string :tag "Tags/Property/Todo matcher"))
  390. :version "24.1"
  391. :group 'org-todo
  392. :group 'org-archive)
  393. (defgroup org-startup nil
  394. "Options concerning startup of Org-mode."
  395. :tag "Org Startup"
  396. :group 'org)
  397. (defcustom org-startup-folded t
  398. "Non-nil means entering Org-mode will switch to OVERVIEW.
  399. This can also be configured on a per-file basis by adding one of
  400. the following lines anywhere in the buffer:
  401. #+STARTUP: fold (or `overview', this is equivalent)
  402. #+STARTUP: nofold (or `showall', this is equivalent)
  403. #+STARTUP: content
  404. #+STARTUP: showeverything"
  405. :group 'org-startup
  406. :type '(choice
  407. (const :tag "nofold: show all" nil)
  408. (const :tag "fold: overview" t)
  409. (const :tag "content: all headlines" content)
  410. (const :tag "show everything, even drawers" showeverything)))
  411. (defcustom org-startup-truncated t
  412. "Non-nil means entering Org-mode will set `truncate-lines'.
  413. This is useful since some lines containing links can be very long and
  414. uninteresting. Also tables look terrible when wrapped."
  415. :group 'org-startup
  416. :type 'boolean)
  417. (defcustom org-startup-indented nil
  418. "Non-nil means turn on `org-indent-mode' on startup.
  419. This can also be configured on a per-file basis by adding one of
  420. the following lines anywhere in the buffer:
  421. #+STARTUP: indent
  422. #+STARTUP: noindent"
  423. :group 'org-structure
  424. :type '(choice
  425. (const :tag "Not" nil)
  426. (const :tag "Globally (slow on startup in large files)" t)))
  427. (defcustom org-use-sub-superscripts t
  428. "Non-nil means interpret \"_\" and \"^\" for export.
  429. When this option is turned on, you can use TeX-like syntax for sub- and
  430. superscripts. Several characters after \"_\" or \"^\" will be
  431. considered as a single item - so grouping with {} is normally not
  432. needed. For example, the following things will be parsed as single
  433. sub- or superscripts.
  434. 10^24 or 10^tau several digits will be considered 1 item.
  435. 10^-12 or 10^-tau a leading sign with digits or a word
  436. x^2-y^3 will be read as x^2 - y^3, because items are
  437. terminated by almost any nonword/nondigit char.
  438. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  439. Still, ambiguity is possible - so when in doubt use {} to enclose the
  440. sub/superscript. If you set this variable to the symbol `{}',
  441. the braces are *required* in order to trigger interpretations as
  442. sub/superscript. This can be helpful in documents that need \"_\"
  443. frequently in plain text.
  444. Not all export backends support this, but HTML does.
  445. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  446. :group 'org-startup
  447. :group 'org-export-translation
  448. :version "24.1"
  449. :type '(choice
  450. (const :tag "Always interpret" t)
  451. (const :tag "Only with braces" {})
  452. (const :tag "Never interpret" nil)))
  453. (if (fboundp 'defvaralias)
  454. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  455. (defcustom org-startup-with-beamer-mode nil
  456. "Non-nil means turn on `org-beamer-mode' on startup.
  457. This can also be configured on a per-file basis by adding one of
  458. the following lines anywhere in the buffer:
  459. #+STARTUP: beamer"
  460. :group 'org-startup
  461. :version "24.1"
  462. :type 'boolean)
  463. (defcustom org-startup-align-all-tables nil
  464. "Non-nil means align all tables when visiting a file.
  465. This is useful when the column width in tables is forced with <N> cookies
  466. in table fields. Such tables will look correct only after the first re-align.
  467. This can also be configured on a per-file basis by adding one of
  468. the following lines anywhere in the buffer:
  469. #+STARTUP: align
  470. #+STARTUP: noalign"
  471. :group 'org-startup
  472. :type 'boolean)
  473. (defcustom org-startup-with-inline-images nil
  474. "Non-nil means show inline images when loading a new Org file.
  475. This can also be configured on a per-file basis by adding one of
  476. the following lines anywhere in the buffer:
  477. #+STARTUP: inlineimages
  478. #+STARTUP: noinlineimages"
  479. :group 'org-startup
  480. :version "24.1"
  481. :type 'boolean)
  482. (defcustom org-insert-mode-line-in-empty-file nil
  483. "Non-nil means insert the first line setting Org-mode in empty files.
  484. When the function `org-mode' is called interactively in an empty file, this
  485. normally means that the file name does not automatically trigger Org-mode.
  486. To ensure that the file will always be in Org-mode in the future, a
  487. line enforcing Org-mode will be inserted into the buffer, if this option
  488. has been set."
  489. :group 'org-startup
  490. :type 'boolean)
  491. (defcustom org-replace-disputed-keys nil
  492. "Non-nil means use alternative key bindings for some keys.
  493. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  494. These keys are also used by other packages like shift-selection-mode'
  495. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  496. If you want to use Org-mode together with one of these other modes,
  497. or more generally if you would like to move some Org-mode commands to
  498. other keys, set this variable and configure the keys with the variable
  499. `org-disputed-keys'.
  500. This option is only relevant at load-time of Org-mode, and must be set
  501. *before* org.el is loaded. Changing it requires a restart of Emacs to
  502. become effective."
  503. :group 'org-startup
  504. :type 'boolean)
  505. (defcustom org-use-extra-keys nil
  506. "Non-nil means use extra key sequence definitions for certain commands.
  507. This happens automatically if you run XEmacs or if `window-system'
  508. is nil. This variable lets you do the same manually. You must
  509. set it before loading org.
  510. Example: on Carbon Emacs 22 running graphically, with an external
  511. keyboard on a Powerbook, the default way of setting M-left might
  512. not work for either Alt or ESC. Setting this variable will make
  513. it work for ESC."
  514. :group 'org-startup
  515. :type 'boolean)
  516. (if (fboundp 'defvaralias)
  517. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  518. (defcustom org-disputed-keys
  519. '(([(shift up)] . [(meta p)])
  520. ([(shift down)] . [(meta n)])
  521. ([(shift left)] . [(meta -)])
  522. ([(shift right)] . [(meta +)])
  523. ([(control shift right)] . [(meta shift +)])
  524. ([(control shift left)] . [(meta shift -)]))
  525. "Keys for which Org-mode and other modes compete.
  526. This is an alist, cars are the default keys, second element specifies
  527. the alternative to use when `org-replace-disputed-keys' is t.
  528. Keys can be specified in any syntax supported by `define-key'.
  529. The value of this option takes effect only at Org-mode's startup,
  530. therefore you'll have to restart Emacs to apply it after changing."
  531. :group 'org-startup
  532. :type 'alist)
  533. (defun org-key (key)
  534. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  535. Or return the original if not disputed.
  536. Also apply the translations defined in `org-xemacs-key-equivalents'."
  537. (when org-replace-disputed-keys
  538. (let* ((nkey (key-description key))
  539. (x (org-find-if (lambda (x)
  540. (equal (key-description (car x)) nkey))
  541. org-disputed-keys)))
  542. (setq key (if x (cdr x) key))))
  543. (when (featurep 'xemacs)
  544. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  545. key)
  546. (defun org-find-if (predicate seq)
  547. (catch 'exit
  548. (while seq
  549. (if (funcall predicate (car seq))
  550. (throw 'exit (car seq))
  551. (pop seq)))))
  552. (defun org-defkey (keymap key def)
  553. "Define a key, possibly translated, as returned by `org-key'."
  554. (define-key keymap (org-key key) def))
  555. (defcustom org-ellipsis nil
  556. "The ellipsis to use in the Org-mode outline.
  557. When nil, just use the standard three dots. When a string, use that instead,
  558. When a face, use the standard 3 dots, but with the specified face.
  559. The change affects only Org-mode (which will then use its own display table).
  560. Changing this requires executing `M-x org-mode' in a buffer to become
  561. effective."
  562. :group 'org-startup
  563. :type '(choice (const :tag "Default" nil)
  564. (face :tag "Face" :value org-warning)
  565. (string :tag "String" :value "...#")))
  566. (defvar org-display-table nil
  567. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  568. (defgroup org-keywords nil
  569. "Keywords in Org-mode."
  570. :tag "Org Keywords"
  571. :group 'org)
  572. (defcustom org-deadline-string "DEADLINE:"
  573. "String to mark deadline entries.
  574. A deadline is this string, followed by a time stamp. Should be a word,
  575. terminated by a colon. You can insert a schedule keyword and
  576. a timestamp with \\[org-deadline].
  577. Changes become only effective after restarting Emacs."
  578. :group 'org-keywords
  579. :type 'string)
  580. (defcustom org-scheduled-string "SCHEDULED:"
  581. "String to mark scheduled TODO entries.
  582. A schedule is this string, followed by a time stamp. Should be a word,
  583. terminated by a colon. You can insert a schedule keyword and
  584. a timestamp with \\[org-schedule].
  585. Changes become only effective after restarting Emacs."
  586. :group 'org-keywords
  587. :type 'string)
  588. (defcustom org-closed-string "CLOSED:"
  589. "String used as the prefix for timestamps logging closing a TODO entry."
  590. :group 'org-keywords
  591. :type 'string)
  592. (defcustom org-clock-string "CLOCK:"
  593. "String used as prefix for timestamps clocking work hours on an item."
  594. :group 'org-keywords
  595. :type 'string)
  596. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  597. org-scheduled-string "\\|"
  598. org-deadline-string "\\|"
  599. org-closed-string "\\|"
  600. org-clock-string "\\)")
  601. "Matches a line with planning or clock info.")
  602. (defcustom org-comment-string "COMMENT"
  603. "Entries starting with this keyword will never be exported.
  604. An entry can be toggled between COMMENT and normal with
  605. \\[org-toggle-comment].
  606. Changes become only effective after restarting Emacs."
  607. :group 'org-keywords
  608. :type 'string)
  609. (defcustom org-quote-string "QUOTE"
  610. "Entries starting with this keyword will be exported in fixed-width font.
  611. Quoting applies only to the text in the entry following the headline, and does
  612. not extend beyond the next headline, even if that is lower level.
  613. An entry can be toggled between QUOTE and normal with
  614. \\[org-toggle-fixed-width-section]."
  615. :group 'org-keywords
  616. :type 'string)
  617. (defconst org-repeat-re
  618. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  619. "Regular expression for specifying repeated events.
  620. After a match, group 1 contains the repeat expression.")
  621. (defgroup org-structure nil
  622. "Options concerning the general structure of Org-mode files."
  623. :tag "Org Structure"
  624. :group 'org)
  625. (defgroup org-reveal-location nil
  626. "Options about how to make context of a location visible."
  627. :tag "Org Reveal Location"
  628. :group 'org-structure)
  629. (defconst org-context-choice
  630. '(choice
  631. (const :tag "Always" t)
  632. (const :tag "Never" nil)
  633. (repeat :greedy t :tag "Individual contexts"
  634. (cons
  635. (choice :tag "Context"
  636. (const agenda)
  637. (const org-goto)
  638. (const occur-tree)
  639. (const tags-tree)
  640. (const link-search)
  641. (const mark-goto)
  642. (const bookmark-jump)
  643. (const isearch)
  644. (const default))
  645. (boolean))))
  646. "Contexts for the reveal options.")
  647. (defcustom org-show-hierarchy-above '((default . t))
  648. "Non-nil means show full hierarchy when revealing a location.
  649. Org-mode often shows locations in an org-mode file which might have
  650. been invisible before. When this is set, the hierarchy of headings
  651. above the exposed location is shown.
  652. Turning this off for example for sparse trees makes them very compact.
  653. Instead of t, this can also be an alist specifying this option for different
  654. contexts. Valid contexts are
  655. agenda when exposing an entry from the agenda
  656. org-goto when using the command `org-goto' on key C-c C-j
  657. occur-tree when using the command `org-occur' on key C-c /
  658. tags-tree when constructing a sparse tree based on tags matches
  659. link-search when exposing search matches associated with a link
  660. mark-goto when exposing the jump goal of a mark
  661. bookmark-jump when exposing a bookmark location
  662. isearch when exiting from an incremental search
  663. default default for all contexts not set explicitly"
  664. :group 'org-reveal-location
  665. :type org-context-choice)
  666. (defcustom org-show-following-heading '((default . nil))
  667. "Non-nil means show following heading when revealing a location.
  668. Org-mode often shows locations in an org-mode file which might have
  669. been invisible before. When this is set, the heading following the
  670. match is shown.
  671. Turning this off for example for sparse trees makes them very compact,
  672. but makes it harder to edit the location of the match. In such a case,
  673. use the command \\[org-reveal] to show more context.
  674. Instead of t, this can also be an alist specifying this option for different
  675. contexts. See `org-show-hierarchy-above' for valid contexts."
  676. :group 'org-reveal-location
  677. :type org-context-choice)
  678. (defcustom org-show-siblings '((default . nil) (isearch t))
  679. "Non-nil means show all sibling heading when revealing a location.
  680. Org-mode often shows locations in an org-mode file which might have
  681. been invisible before. When this is set, the sibling of the current entry
  682. heading are all made visible. If `org-show-hierarchy-above' is t,
  683. the same happens on each level of the hierarchy above the current entry.
  684. By default this is on for the isearch context, off for all other contexts.
  685. Turning this off for example for sparse trees makes them very compact,
  686. but makes it harder to edit the location of the match. In such a case,
  687. use the command \\[org-reveal] to show more context.
  688. Instead of t, this can also be an alist specifying this option for different
  689. contexts. See `org-show-hierarchy-above' for valid contexts."
  690. :group 'org-reveal-location
  691. :type org-context-choice)
  692. (defcustom org-show-entry-below '((default . nil))
  693. "Non-nil means show the entry below a headline when revealing a location.
  694. Org-mode often shows locations in an org-mode file which might have
  695. been invisible before. When this is set, the text below the headline that is
  696. exposed is also shown.
  697. By default this is off for all contexts.
  698. Instead of t, this can also be an alist specifying this option for different
  699. contexts. See `org-show-hierarchy-above' for valid contexts."
  700. :group 'org-reveal-location
  701. :type org-context-choice)
  702. (defcustom org-indirect-buffer-display 'other-window
  703. "How should indirect tree buffers be displayed?
  704. This applies to indirect buffers created with the commands
  705. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  706. Valid values are:
  707. current-window Display in the current window
  708. other-window Just display in another window.
  709. dedicated-frame Create one new frame, and re-use it each time.
  710. new-frame Make a new frame each time. Note that in this case
  711. previously-made indirect buffers are kept, and you need to
  712. kill these buffers yourself."
  713. :group 'org-structure
  714. :group 'org-agenda-windows
  715. :type '(choice
  716. (const :tag "In current window" current-window)
  717. (const :tag "In current frame, other window" other-window)
  718. (const :tag "Each time a new frame" new-frame)
  719. (const :tag "One dedicated frame" dedicated-frame)))
  720. (defcustom org-use-speed-commands nil
  721. "Non-nil means activate single letter commands at beginning of a headline.
  722. This may also be a function to test for appropriate locations where speed
  723. commands should be active."
  724. :group 'org-structure
  725. :type '(choice
  726. (const :tag "Never" nil)
  727. (const :tag "At beginning of headline stars" t)
  728. (function)))
  729. (defcustom org-speed-commands-user nil
  730. "Alist of additional speed commands.
  731. This list will be checked before `org-speed-commands-default'
  732. when the variable `org-use-speed-commands' is non-nil
  733. and when the cursor is at the beginning of a headline.
  734. The car if each entry is a string with a single letter, which must
  735. be assigned to `self-insert-command' in the global map.
  736. The cdr is either a command to be called interactively, a function
  737. to be called, or a form to be evaluated.
  738. An entry that is just a list with a single string will be interpreted
  739. as a descriptive headline that will be added when listing the speed
  740. commands in the Help buffer using the `?' speed command."
  741. :group 'org-structure
  742. :type '(repeat :value ("k" . ignore)
  743. (choice :value ("k" . ignore)
  744. (list :tag "Descriptive Headline" (string :tag "Headline"))
  745. (cons :tag "Letter and Command"
  746. (string :tag "Command letter")
  747. (choice
  748. (function)
  749. (sexp))))))
  750. (defgroup org-cycle nil
  751. "Options concerning visibility cycling in Org-mode."
  752. :tag "Org Cycle"
  753. :group 'org-structure)
  754. (defcustom org-cycle-skip-children-state-if-no-children t
  755. "Non-nil means skip CHILDREN state in entries that don't have any."
  756. :group 'org-cycle
  757. :type 'boolean)
  758. (defcustom org-cycle-max-level nil
  759. "Maximum level which should still be subject to visibility cycling.
  760. Levels higher than this will, for cycling, be treated as text, not a headline.
  761. When `org-odd-levels-only' is set, a value of N in this variable actually
  762. means 2N-1 stars as the limiting headline.
  763. When nil, cycle all levels.
  764. Note that the limiting level of cycling is also influenced by
  765. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  766. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  767. than its value."
  768. :group 'org-cycle
  769. :type '(choice
  770. (const :tag "No limit" nil)
  771. (integer :tag "Maximum level")))
  772. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  773. "Names of drawers. Drawers are not opened by cycling on the headline above.
  774. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  775. this:
  776. :DRAWERNAME:
  777. .....
  778. :END:
  779. The drawer \"PROPERTIES\" is special for capturing properties through
  780. the property API.
  781. Drawers can be defined on the per-file basis with a line like:
  782. #+DRAWERS: HIDDEN STATE PROPERTIES"
  783. :group 'org-structure
  784. :group 'org-cycle
  785. :type '(repeat (string :tag "Drawer Name")))
  786. (defcustom org-hide-block-startup nil
  787. "Non-nil means entering Org-mode will fold all blocks.
  788. This can also be set in on a per-file basis with
  789. #+STARTUP: hideblocks
  790. #+STARTUP: showblocks"
  791. :group 'org-startup
  792. :group 'org-cycle
  793. :type 'boolean)
  794. (defcustom org-cycle-global-at-bob nil
  795. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  796. This makes it possible to do global cycling without having to use S-TAB or
  797. \\[universal-argument] TAB. For this special case to work, the first line \
  798. of the buffer
  799. must not be a headline - it may be empty or some other text. When used in
  800. this way, `org-cycle-hook' is disables temporarily, to make sure the
  801. cursor stays at the beginning of the buffer.
  802. When this option is nil, don't do anything special at the beginning
  803. of the buffer."
  804. :group 'org-cycle
  805. :type 'boolean)
  806. (defcustom org-cycle-level-after-item/entry-creation t
  807. "Non-nil means cycle entry level or item indentation in new empty entries.
  808. When the cursor is at the end of an empty headline, i.e with only stars
  809. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  810. and then all possible ancestor states, before returning to the original state.
  811. This makes data entry extremely fast: M-RET to create a new headline,
  812. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  813. When the cursor is at the end of an empty plain list item, one TAB will
  814. make it a subitem, two or more tabs will back up to make this an item
  815. higher up in the item hierarchy."
  816. :group 'org-cycle
  817. :type 'boolean)
  818. (defcustom org-cycle-emulate-tab t
  819. "Where should `org-cycle' emulate TAB.
  820. nil Never
  821. white Only in completely white lines
  822. whitestart Only at the beginning of lines, before the first non-white char
  823. t Everywhere except in headlines
  824. exc-hl-bol Everywhere except at the start of a headline
  825. If TAB is used in a place where it does not emulate TAB, the current subtree
  826. visibility is cycled."
  827. :group 'org-cycle
  828. :type '(choice (const :tag "Never" nil)
  829. (const :tag "Only in completely white lines" white)
  830. (const :tag "Before first char in a line" whitestart)
  831. (const :tag "Everywhere except in headlines" t)
  832. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  833. ))
  834. (defcustom org-cycle-separator-lines 2
  835. "Number of empty lines needed to keep an empty line between collapsed trees.
  836. If you leave an empty line between the end of a subtree and the following
  837. headline, this empty line is hidden when the subtree is folded.
  838. Org-mode will leave (exactly) one empty line visible if the number of
  839. empty lines is equal or larger to the number given in this variable.
  840. So the default 2 means at least 2 empty lines after the end of a subtree
  841. are needed to produce free space between a collapsed subtree and the
  842. following headline.
  843. If the number is negative, and the number of empty lines is at least -N,
  844. all empty lines are shown.
  845. Special case: when 0, never leave empty lines in collapsed view."
  846. :group 'org-cycle
  847. :type 'integer)
  848. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  849. (defcustom org-pre-cycle-hook nil
  850. "Hook that is run before visibility cycling is happening.
  851. The function(s) in this hook must accept a single argument which indicates
  852. the new state that will be set right after running this hook. The
  853. argument is a symbol. Before a global state change, it can have the values
  854. `overview', `content', or `all'. Before a local state change, it can have
  855. the values `folded', `children', or `subtree'."
  856. :group 'org-cycle
  857. :type 'hook)
  858. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  859. org-cycle-hide-drawers
  860. org-cycle-show-empty-lines
  861. org-optimize-window-after-visibility-change)
  862. "Hook that is run after `org-cycle' has changed the buffer visibility.
  863. The function(s) in this hook must accept a single argument which indicates
  864. the new state that was set by the most recent `org-cycle' command. The
  865. argument is a symbol. After a global state change, it can have the values
  866. `overview', `content', or `all'. After a local state change, it can have
  867. the values `folded', `children', or `subtree'."
  868. :group 'org-cycle
  869. :type 'hook)
  870. (defgroup org-edit-structure nil
  871. "Options concerning structure editing in Org-mode."
  872. :tag "Org Edit Structure"
  873. :group 'org-structure)
  874. (defcustom org-odd-levels-only nil
  875. "Non-nil means skip even levels and only use odd levels for the outline.
  876. This has the effect that two stars are being added/taken away in
  877. promotion/demotion commands. It also influences how levels are
  878. handled by the exporters.
  879. Changing it requires restart of `font-lock-mode' to become effective
  880. for fontification also in regions already fontified.
  881. You may also set this on a per-file basis by adding one of the following
  882. lines to the buffer:
  883. #+STARTUP: odd
  884. #+STARTUP: oddeven"
  885. :group 'org-edit-structure
  886. :group 'org-appearance
  887. :type 'boolean)
  888. (defcustom org-adapt-indentation t
  889. "Non-nil means adapt indentation to outline node level.
  890. When this variable is set, Org assumes that you write outlines by
  891. indenting text in each node to align with the headline (after the stars).
  892. The following issues are influenced by this variable:
  893. - When this is set and the *entire* text in an entry is indented, the
  894. indentation is increased by one space in a demotion command, and
  895. decreased by one in a promotion command. If any line in the entry
  896. body starts with text at column 0, indentation is not changed at all.
  897. - Property drawers and planning information is inserted indented when
  898. this variable s set. When nil, they will not be indented.
  899. - TAB indents a line relative to context. The lines below a headline
  900. will be indented when this variable is set.
  901. Note that this is all about true indentation, by adding and removing
  902. space characters. See also `org-indent.el' which does level-dependent
  903. indentation in a virtual way, i.e. at display time in Emacs."
  904. :group 'org-edit-structure
  905. :type 'boolean)
  906. (defcustom org-special-ctrl-a/e nil
  907. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  908. When t, `C-a' will bring back the cursor to the beginning of the
  909. headline text, i.e. after the stars and after a possible TODO
  910. keyword. In an item, this will be the position after bullet and
  911. check-box, if any. When the cursor is already at that position,
  912. another `C-a' will bring it to the beginning of the line.
  913. `C-e' will jump to the end of the headline, ignoring the presence
  914. of tags in the headline. A second `C-e' will then jump to the
  915. true end of the line, after any tags. This also means that, when
  916. this variable is non-nil, `C-e' also will never jump beyond the
  917. end of the heading of a folded section, i.e. not after the
  918. ellipses.
  919. When set to the symbol `reversed', the first `C-a' or `C-e' works
  920. normally, going to the true line boundary first. Only a directly
  921. following, identical keypress will bring the cursor to the
  922. special positions.
  923. This may also be a cons cell where the behavior for `C-a' and
  924. `C-e' is set separately."
  925. :group 'org-edit-structure
  926. :type '(choice
  927. (const :tag "off" nil)
  928. (const :tag "on: after stars/bullet and before tags first" t)
  929. (const :tag "reversed: true line boundary first" reversed)
  930. (cons :tag "Set C-a and C-e separately"
  931. (choice :tag "Special C-a"
  932. (const :tag "off" nil)
  933. (const :tag "on: after stars/bullet first" t)
  934. (const :tag "reversed: before stars/bullet first" reversed))
  935. (choice :tag "Special C-e"
  936. (const :tag "off" nil)
  937. (const :tag "on: before tags first" t)
  938. (const :tag "reversed: after tags first" reversed)))))
  939. (if (fboundp 'defvaralias)
  940. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  941. (defcustom org-special-ctrl-k nil
  942. "Non-nil means `C-k' will behave specially in headlines.
  943. When nil, `C-k' will call the default `kill-line' command.
  944. When t, the following will happen while the cursor is in the headline:
  945. - When the cursor is at the beginning of a headline, kill the entire
  946. line and possible the folded subtree below the line.
  947. - When in the middle of the headline text, kill the headline up to the tags.
  948. - When after the headline text, kill the tags."
  949. :group 'org-edit-structure
  950. :type 'boolean)
  951. (defcustom org-ctrl-k-protect-subtree nil
  952. "Non-nil means, do not delete a hidden subtree with C-k.
  953. When set to the symbol `error', simply throw an error when C-k is
  954. used to kill (part-of) a headline that has hidden text behind it.
  955. Any other non-nil value will result in a query to the user, if it is
  956. OK to kill that hidden subtree. When nil, kill without remorse."
  957. :group 'org-edit-structure
  958. :version "24.1"
  959. :type '(choice
  960. (const :tag "Do not protect hidden subtrees" nil)
  961. (const :tag "Protect hidden subtrees with a security query" t)
  962. (const :tag "Never kill a hidden subtree with C-k" error)))
  963. (defcustom org-catch-invisible-edits nil
  964. "Check if in invisible region before inserting or deleting a character.
  965. Valid values are:
  966. nil Do not check, so just do invisible edits.
  967. error Throw an error and do nothing.
  968. show Make point visible, and do the requested edit.
  969. show-and-error Make point visible, then throw an error and abort the edit.
  970. smart Make point visible, and do insertion/deletion if it is
  971. adjacent to visible text and the change feels predictable.
  972. Never delete a previously invisible character or add in the
  973. middle or right after an invisible region. Basically, this
  974. allows insertion and backward-delete right before ellipses.
  975. FIXME: maybe in this case we should not even show?"
  976. :group 'org-edit-structure
  977. :version "24.1"
  978. :type '(choice
  979. (const :tag "Do not check" nil)
  980. (const :tag "Throw error when trying to edit" error)
  981. (const :tag "Unhide, but do not do the edit" show-and-error)
  982. (const :tag "Show invisible part and do the edit" show)
  983. (const :tag "Be smart and do the right thing" smart)))
  984. (defcustom org-yank-folded-subtrees t
  985. "Non-nil means when yanking subtrees, fold them.
  986. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  987. it starts with a heading and all other headings in it are either children
  988. or siblings, then fold all the subtrees. However, do this only if no
  989. text after the yank would be swallowed into a folded tree by this action."
  990. :group 'org-edit-structure
  991. :type 'boolean)
  992. (defcustom org-yank-adjusted-subtrees nil
  993. "Non-nil means when yanking subtrees, adjust the level.
  994. With this setting, `org-paste-subtree' is used to insert the subtree, see
  995. this function for details."
  996. :group 'org-edit-structure
  997. :type 'boolean)
  998. (defcustom org-M-RET-may-split-line '((default . t))
  999. "Non-nil means M-RET will split the line at the cursor position.
  1000. When nil, it will go to the end of the line before making a
  1001. new line.
  1002. You may also set this option in a different way for different
  1003. contexts. Valid contexts are:
  1004. headline when creating a new headline
  1005. item when creating a new item
  1006. table in a table field
  1007. default the value to be used for all contexts not explicitly
  1008. customized"
  1009. :group 'org-structure
  1010. :group 'org-table
  1011. :type '(choice
  1012. (const :tag "Always" t)
  1013. (const :tag "Never" nil)
  1014. (repeat :greedy t :tag "Individual contexts"
  1015. (cons
  1016. (choice :tag "Context"
  1017. (const headline)
  1018. (const item)
  1019. (const table)
  1020. (const default))
  1021. (boolean)))))
  1022. (defcustom org-insert-heading-respect-content nil
  1023. "Non-nil means insert new headings after the current subtree.
  1024. When nil, the new heading is created directly after the current line.
  1025. The commands \\[org-insert-heading-respect-content] and
  1026. \\[org-insert-todo-heading-respect-content] turn this variable on
  1027. for the duration of the command."
  1028. :group 'org-structure
  1029. :type 'boolean)
  1030. (defcustom org-blank-before-new-entry '((heading . auto)
  1031. (plain-list-item . auto))
  1032. "Should `org-insert-heading' leave a blank line before new heading/item?
  1033. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1034. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1035. which case Org will look at the surrounding headings/items and try to
  1036. make an intelligent decision whether to insert a blank line or not.
  1037. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1038. set, the setting here is ignored and no empty line is inserted, to avoid
  1039. breaking the list structure."
  1040. :group 'org-edit-structure
  1041. :type '(list
  1042. (cons (const heading)
  1043. (choice (const :tag "Never" nil)
  1044. (const :tag "Always" t)
  1045. (const :tag "Auto" auto)))
  1046. (cons (const plain-list-item)
  1047. (choice (const :tag "Never" nil)
  1048. (const :tag "Always" t)
  1049. (const :tag "Auto" auto)))))
  1050. (defcustom org-insert-heading-hook nil
  1051. "Hook being run after inserting a new heading."
  1052. :group 'org-edit-structure
  1053. :type 'hook)
  1054. (defcustom org-enable-fixed-width-editor t
  1055. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1056. This currently only means they are never auto-wrapped.
  1057. When nil, such lines will be treated like ordinary lines.
  1058. See also the QUOTE keyword."
  1059. :group 'org-edit-structure
  1060. :type 'boolean)
  1061. (defcustom org-goto-auto-isearch t
  1062. "Non-nil means typing characters in `org-goto' starts incremental search."
  1063. :group 'org-edit-structure
  1064. :type 'boolean)
  1065. (defgroup org-sparse-trees nil
  1066. "Options concerning sparse trees in Org-mode."
  1067. :tag "Org Sparse Trees"
  1068. :group 'org-structure)
  1069. (defcustom org-highlight-sparse-tree-matches t
  1070. "Non-nil means highlight all matches that define a sparse tree.
  1071. The highlights will automatically disappear the next time the buffer is
  1072. changed by an edit command."
  1073. :group 'org-sparse-trees
  1074. :type 'boolean)
  1075. (defcustom org-remove-highlights-with-change t
  1076. "Non-nil means any change to the buffer will remove temporary highlights.
  1077. Such highlights are created by `org-occur' and `org-clock-display'.
  1078. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1079. The highlights created by `org-preview-latex-fragment' always need
  1080. `C-c C-c' to be removed."
  1081. :group 'org-sparse-trees
  1082. :group 'org-time
  1083. :type 'boolean)
  1084. (defcustom org-occur-hook '(org-first-headline-recenter)
  1085. "Hook that is run after `org-occur' has constructed a sparse tree.
  1086. This can be used to recenter the window to show as much of the structure
  1087. as possible."
  1088. :group 'org-sparse-trees
  1089. :type 'hook)
  1090. (defgroup org-imenu-and-speedbar nil
  1091. "Options concerning imenu and speedbar in Org-mode."
  1092. :tag "Org Imenu and Speedbar"
  1093. :group 'org-structure)
  1094. (defcustom org-imenu-depth 2
  1095. "The maximum level for Imenu access to Org-mode headlines.
  1096. This also applied for speedbar access."
  1097. :group 'org-imenu-and-speedbar
  1098. :type 'integer)
  1099. (defgroup org-table nil
  1100. "Options concerning tables in Org-mode."
  1101. :tag "Org Table"
  1102. :group 'org)
  1103. (defcustom org-enable-table-editor 'optimized
  1104. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1105. When nil, such lines will be treated like ordinary lines.
  1106. When equal to the symbol `optimized', the table editor will be optimized to
  1107. do the following:
  1108. - Automatic overwrite mode in front of whitespace in table fields.
  1109. This makes the structure of the table stay in tact as long as the edited
  1110. field does not exceed the column width.
  1111. - Minimize the number of realigns. Normally, the table is aligned each time
  1112. TAB or RET are pressed to move to another field. With optimization this
  1113. happens only if changes to a field might have changed the column width.
  1114. Optimization requires replacing the functions `self-insert-command',
  1115. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1116. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1117. very good at guessing when a re-align will be necessary, but you can always
  1118. force one with \\[org-ctrl-c-ctrl-c].
  1119. If you would like to use the optimized version in Org-mode, but the
  1120. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1121. This variable can be used to turn on and off the table editor during a session,
  1122. but in order to toggle optimization, a restart is required.
  1123. See also the variable `org-table-auto-blank-field'."
  1124. :group 'org-table
  1125. :type '(choice
  1126. (const :tag "off" nil)
  1127. (const :tag "on" t)
  1128. (const :tag "on, optimized" optimized)))
  1129. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1130. (version<= emacs-version "24.1"))
  1131. "Non-nil means cluster self-insert commands for undo when possible.
  1132. If this is set, then, like in the Emacs command loop, 20 consecutive
  1133. characters will be undone together.
  1134. This is configurable, because there is some impact on typing performance."
  1135. :group 'org-table
  1136. :type 'boolean)
  1137. (defcustom org-table-tab-recognizes-table.el t
  1138. "Non-nil means TAB will automatically notice a table.el table.
  1139. When it sees such a table, it moves point into it and - if necessary -
  1140. calls `table-recognize-table'."
  1141. :group 'org-table-editing
  1142. :type 'boolean)
  1143. (defgroup org-link nil
  1144. "Options concerning links in Org-mode."
  1145. :tag "Org Link"
  1146. :group 'org)
  1147. (defvar org-link-abbrev-alist-local nil
  1148. "Buffer-local version of `org-link-abbrev-alist', which see.
  1149. The value of this is taken from the #+LINK lines.")
  1150. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1151. (defcustom org-link-abbrev-alist nil
  1152. "Alist of link abbreviations.
  1153. The car of each element is a string, to be replaced at the start of a link.
  1154. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1155. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1156. [[linkkey:tag][description]]
  1157. The 'linkkey' must be a word word, starting with a letter, followed
  1158. by letters, numbers, '-' or '_'.
  1159. If REPLACE is a string, the tag will simply be appended to create the link.
  1160. If the string contains \"%s\", the tag will be inserted there. If the string
  1161. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1162. at that point (see the function `url-hexify-string'). If the string contains
  1163. the specifier \"%(my-function)\", then the custom function `my-function' will
  1164. be invoked: this function takes the tag as its only argument and must return
  1165. a string.
  1166. REPLACE may also be a function that will be called with the tag as the
  1167. only argument to create the link, which should be returned as a string.
  1168. See the manual for examples."
  1169. :group 'org-link
  1170. :type '(repeat
  1171. (cons
  1172. (string :tag "Protocol")
  1173. (choice
  1174. (string :tag "Format")
  1175. (function)))))
  1176. (defcustom org-descriptive-links t
  1177. "Non-nil means Org will display descriptive links.
  1178. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1179. \"Org Website\", hiding the link itself and just displaying its
  1180. description. When set to `nil', Org will display the full links
  1181. literally.
  1182. You can interactively set the value of this variable by calling
  1183. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1184. :group 'org-link
  1185. :type 'boolean)
  1186. (defcustom org-link-file-path-type 'adaptive
  1187. "How the path name in file links should be stored.
  1188. Valid values are:
  1189. relative Relative to the current directory, i.e. the directory of the file
  1190. into which the link is being inserted.
  1191. absolute Absolute path, if possible with ~ for home directory.
  1192. noabbrev Absolute path, no abbreviation of home directory.
  1193. adaptive Use relative path for files in the current directory and sub-
  1194. directories of it. For other files, use an absolute path."
  1195. :group 'org-link
  1196. :type '(choice
  1197. (const relative)
  1198. (const absolute)
  1199. (const noabbrev)
  1200. (const adaptive)))
  1201. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1202. "Types of links that should be activated in Org-mode files.
  1203. This is a list of symbols, each leading to the activation of a certain link
  1204. type. In principle, it does not hurt to turn on most link types - there may
  1205. be a small gain when turning off unused link types. The types are:
  1206. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1207. angle Links in angular brackets that may contain whitespace like
  1208. <bbdb:Carsten Dominik>.
  1209. plain Plain links in normal text, no whitespace, like http://google.com.
  1210. radio Text that is matched by a radio target, see manual for details.
  1211. tag Tag settings in a headline (link to tag search).
  1212. date Time stamps (link to calendar).
  1213. footnote Footnote labels.
  1214. Changing this variable requires a restart of Emacs to become effective."
  1215. :group 'org-link
  1216. :type '(set :greedy t
  1217. (const :tag "Double bracket links" bracket)
  1218. (const :tag "Angular bracket links" angle)
  1219. (const :tag "Plain text links" plain)
  1220. (const :tag "Radio target matches" radio)
  1221. (const :tag "Tags" tag)
  1222. (const :tag "Timestamps" date)
  1223. (const :tag "Footnotes" footnote)))
  1224. (defcustom org-make-link-description-function nil
  1225. "Function to use to generate link descriptions from links.
  1226. If nil the link location will be used. This function must take
  1227. two parameters; the first is the link and the second the
  1228. description `org-insert-link' has generated, and should return the
  1229. description to use."
  1230. :group 'org-link
  1231. :type 'function)
  1232. (defgroup org-link-store nil
  1233. "Options concerning storing links in Org-mode."
  1234. :tag "Org Store Link"
  1235. :group 'org-link)
  1236. (defcustom org-url-hexify-p t
  1237. "When non-nil, hexify URL when creating a link."
  1238. :type 'boolean
  1239. ;; :version "24.3"
  1240. :group 'org-link-store)
  1241. (defcustom org-email-link-description-format "Email %c: %.30s"
  1242. "Format of the description part of a link to an email or usenet message.
  1243. The following %-escapes will be replaced by corresponding information:
  1244. %F full \"From\" field
  1245. %f name, taken from \"From\" field, address if no name
  1246. %T full \"To\" field
  1247. %t first name in \"To\" field, address if no name
  1248. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1249. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1250. %s subject
  1251. %d date
  1252. %m message-id.
  1253. You may use normal field width specification between the % and the letter.
  1254. This is for example useful to limit the length of the subject.
  1255. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1256. :group 'org-link-store
  1257. :type 'string)
  1258. (defcustom org-from-is-user-regexp
  1259. (let (r1 r2)
  1260. (when (and user-mail-address (not (string= user-mail-address "")))
  1261. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1262. (when (and user-full-name (not (string= user-full-name "")))
  1263. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1264. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1265. "Regexp matched against the \"From:\" header of an email or usenet message.
  1266. It should match if the message is from the user him/herself."
  1267. :group 'org-link-store
  1268. :type 'regexp)
  1269. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1270. "Non-nil means storing a link to an Org file will use entry IDs.
  1271. Note that before this variable is even considered, org-id must be loaded,
  1272. so please customize `org-modules' and turn it on.
  1273. The variable can have the following values:
  1274. t Create an ID if needed to make a link to the current entry.
  1275. create-if-interactive
  1276. If `org-store-link' is called directly (interactively, as a user
  1277. command), do create an ID to support the link. But when doing the
  1278. job for capture, only use the ID if it already exists. The
  1279. purpose of this setting is to avoid proliferation of unwanted
  1280. IDs, just because you happen to be in an Org file when you
  1281. call `org-capture' that automatically and preemptively creates a
  1282. link. If you do want to get an ID link in a capture template to
  1283. an entry not having an ID, create it first by explicitly creating
  1284. a link to it, using `C-c C-l' first.
  1285. create-if-interactive-and-no-custom-id
  1286. Like create-if-interactive, but do not create an ID if there is
  1287. a CUSTOM_ID property defined in the entry. This is the default.
  1288. use-existing
  1289. Use existing ID, do not create one.
  1290. nil Never use an ID to make a link, instead link using a text search for
  1291. the headline text."
  1292. :group 'org-link-store
  1293. :type '(choice
  1294. (const :tag "Create ID to make link" t)
  1295. (const :tag "Create if storing link interactively"
  1296. create-if-interactive)
  1297. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1298. create-if-interactive-and-no-custom-id)
  1299. (const :tag "Only use existing" use-existing)
  1300. (const :tag "Do not use ID to create link" nil)))
  1301. (defcustom org-context-in-file-links t
  1302. "Non-nil means file links from `org-store-link' contain context.
  1303. A search string will be added to the file name with :: as separator and
  1304. used to find the context when the link is activated by the command
  1305. `org-open-at-point'. When this option is t, the entire active region
  1306. will be placed in the search string of the file link. If set to a
  1307. positive integer, only the first n lines of context will be stored.
  1308. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1309. negates this setting for the duration of the command."
  1310. :group 'org-link-store
  1311. :type '(choice boolean integer))
  1312. (defcustom org-keep-stored-link-after-insertion nil
  1313. "Non-nil means keep link in list for entire session.
  1314. The command `org-store-link' adds a link pointing to the current
  1315. location to an internal list. These links accumulate during a session.
  1316. The command `org-insert-link' can be used to insert links into any
  1317. Org-mode file (offering completion for all stored links). When this
  1318. option is nil, every link which has been inserted once using \\[org-insert-link]
  1319. will be removed from the list, to make completing the unused links
  1320. more efficient."
  1321. :group 'org-link-store
  1322. :type 'boolean)
  1323. (defgroup org-link-follow nil
  1324. "Options concerning following links in Org-mode."
  1325. :tag "Org Follow Link"
  1326. :group 'org-link)
  1327. (defcustom org-link-translation-function nil
  1328. "Function to translate links with different syntax to Org syntax.
  1329. This can be used to translate links created for example by the Planner
  1330. or emacs-wiki packages to Org syntax.
  1331. The function must accept two parameters, a TYPE containing the link
  1332. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1333. which is everything after the link protocol. It should return a cons
  1334. with possibly modified values of type and path.
  1335. Org contains a function for this, so if you set this variable to
  1336. `org-translate-link-from-planner', you should be able follow many
  1337. links created by planner."
  1338. :group 'org-link-follow
  1339. :type 'function)
  1340. (defcustom org-follow-link-hook nil
  1341. "Hook that is run after a link has been followed."
  1342. :group 'org-link-follow
  1343. :type 'hook)
  1344. (defcustom org-tab-follows-link nil
  1345. "Non-nil means on links TAB will follow the link.
  1346. Needs to be set before org.el is loaded.
  1347. This really should not be used, it does not make sense, and the
  1348. implementation is bad."
  1349. :group 'org-link-follow
  1350. :type 'boolean)
  1351. (defcustom org-return-follows-link nil
  1352. "Non-nil means on links RET will follow the link."
  1353. :group 'org-link-follow
  1354. :type 'boolean)
  1355. (defcustom org-mouse-1-follows-link
  1356. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1357. "Non-nil means mouse-1 on a link will follow the link.
  1358. A longer mouse click will still set point. Does not work on XEmacs.
  1359. Needs to be set before org.el is loaded."
  1360. :group 'org-link-follow
  1361. :type 'boolean)
  1362. (defcustom org-mark-ring-length 4
  1363. "Number of different positions to be recorded in the ring.
  1364. Changing this requires a restart of Emacs to work correctly."
  1365. :group 'org-link-follow
  1366. :type 'integer)
  1367. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1368. "Non-nil means internal links in Org files must exactly match a headline.
  1369. When nil, the link search tries to match a phrase with all words
  1370. in the search text."
  1371. :group 'org-link-follow
  1372. :version "24.1"
  1373. :type '(choice
  1374. (const :tag "Use fuzzy text search" nil)
  1375. (const :tag "Match only exact headline" t)
  1376. (const :tag "Match exact headline or query to create it"
  1377. query-to-create)))
  1378. (defcustom org-link-frame-setup
  1379. '((vm . vm-visit-folder-other-frame)
  1380. (vm-imap . vm-visit-imap-folder-other-frame)
  1381. (gnus . org-gnus-no-new-news)
  1382. (file . find-file-other-window)
  1383. (wl . wl-other-frame))
  1384. "Setup the frame configuration for following links.
  1385. When following a link with Emacs, it may often be useful to display
  1386. this link in another window or frame. This variable can be used to
  1387. set this up for the different types of links.
  1388. For VM, use any of
  1389. `vm-visit-folder'
  1390. `vm-visit-folder-other-window'
  1391. `vm-visit-folder-other-frame'
  1392. For Gnus, use any of
  1393. `gnus'
  1394. `gnus-other-frame'
  1395. `org-gnus-no-new-news'
  1396. For FILE, use any of
  1397. `find-file'
  1398. `find-file-other-window'
  1399. `find-file-other-frame'
  1400. For Wanderlust use any of
  1401. `wl'
  1402. `wl-other-frame'
  1403. For the calendar, use the variable `calendar-setup'.
  1404. For BBDB, it is currently only possible to display the matches in
  1405. another window."
  1406. :group 'org-link-follow
  1407. :type '(list
  1408. (cons (const vm)
  1409. (choice
  1410. (const vm-visit-folder)
  1411. (const vm-visit-folder-other-window)
  1412. (const vm-visit-folder-other-frame)))
  1413. (cons (const gnus)
  1414. (choice
  1415. (const gnus)
  1416. (const gnus-other-frame)
  1417. (const org-gnus-no-new-news)))
  1418. (cons (const file)
  1419. (choice
  1420. (const find-file)
  1421. (const find-file-other-window)
  1422. (const find-file-other-frame)))
  1423. (cons (const wl)
  1424. (choice
  1425. (const wl)
  1426. (const wl-other-frame)))))
  1427. (defcustom org-display-internal-link-with-indirect-buffer nil
  1428. "Non-nil means use indirect buffer to display infile links.
  1429. Activating internal links (from one location in a file to another location
  1430. in the same file) normally just jumps to the location. When the link is
  1431. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1432. is displayed in
  1433. another window. When this option is set, the other window actually displays
  1434. an indirect buffer clone of the current buffer, to avoid any visibility
  1435. changes to the current buffer."
  1436. :group 'org-link-follow
  1437. :type 'boolean)
  1438. (defcustom org-open-non-existing-files nil
  1439. "Non-nil means `org-open-file' will open non-existing files.
  1440. When nil, an error will be generated.
  1441. This variable applies only to external applications because they
  1442. might choke on non-existing files. If the link is to a file that
  1443. will be opened in Emacs, the variable is ignored."
  1444. :group 'org-link-follow
  1445. :type 'boolean)
  1446. (defcustom org-open-directory-means-index-dot-org nil
  1447. "Non-nil means a link to a directory really means to index.org.
  1448. When nil, following a directory link will run dired or open a finder/explorer
  1449. window on that directory."
  1450. :group 'org-link-follow
  1451. :type 'boolean)
  1452. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1453. "Function and arguments to call for following mailto links.
  1454. This is a list with the first element being a Lisp function, and the
  1455. remaining elements being arguments to the function. In string arguments,
  1456. %a will be replaced by the address, and %s will be replaced by the subject
  1457. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1458. :group 'org-link-follow
  1459. :type '(choice
  1460. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1461. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1462. (const :tag "message-mail" (message-mail "%a" "%s"))
  1463. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1464. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1465. "Non-nil means ask for confirmation before executing shell links.
  1466. Shell links can be dangerous: just think about a link
  1467. [[shell:rm -rf ~/*][Google Search]]
  1468. This link would show up in your Org-mode document as \"Google Search\",
  1469. but really it would remove your entire home directory.
  1470. Therefore we advise against setting this variable to nil.
  1471. Just change it to `y-or-n-p' if you want to confirm with a
  1472. single keystroke rather than having to type \"yes\"."
  1473. :group 'org-link-follow
  1474. :type '(choice
  1475. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1476. (const :tag "with y-or-n (faster)" y-or-n-p)
  1477. (const :tag "no confirmation (dangerous)" nil)))
  1478. (put 'org-confirm-shell-link-function
  1479. 'safe-local-variable
  1480. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1481. (defcustom org-confirm-shell-link-not-regexp ""
  1482. "A regexp to skip confirmation for shell links."
  1483. :group 'org-link-follow
  1484. :version "24.1"
  1485. :type 'regexp)
  1486. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1487. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1488. Elisp links can be dangerous: just think about a link
  1489. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1490. This link would show up in your Org-mode document as \"Google Search\",
  1491. but really it would remove your entire home directory.
  1492. Therefore we advise against setting this variable to nil.
  1493. Just change it to `y-or-n-p' if you want to confirm with a
  1494. single keystroke rather than having to type \"yes\"."
  1495. :group 'org-link-follow
  1496. :type '(choice
  1497. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1498. (const :tag "with y-or-n (faster)" y-or-n-p)
  1499. (const :tag "no confirmation (dangerous)" nil)))
  1500. (put 'org-confirm-shell-link-function
  1501. 'safe-local-variable
  1502. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1503. (defcustom org-confirm-elisp-link-not-regexp ""
  1504. "A regexp to skip confirmation for Elisp links."
  1505. :group 'org-link-follow
  1506. :version "24.1"
  1507. :type 'regexp)
  1508. (defconst org-file-apps-defaults-gnu
  1509. '((remote . emacs)
  1510. (system . mailcap)
  1511. (t . mailcap))
  1512. "Default file applications on a UNIX or GNU/Linux system.
  1513. See `org-file-apps'.")
  1514. (defconst org-file-apps-defaults-macosx
  1515. '((remote . emacs)
  1516. (t . "open %s")
  1517. (system . "open %s")
  1518. ("ps.gz" . "gv %s")
  1519. ("eps.gz" . "gv %s")
  1520. ("dvi" . "xdvi %s")
  1521. ("fig" . "xfig %s"))
  1522. "Default file applications on a MacOS X system.
  1523. The system \"open\" is known as a default, but we use X11 applications
  1524. for some files for which the OS does not have a good default.
  1525. See `org-file-apps'.")
  1526. (defconst org-file-apps-defaults-windowsnt
  1527. (list
  1528. '(remote . emacs)
  1529. (cons t
  1530. (list (if (featurep 'xemacs)
  1531. 'mswindows-shell-execute
  1532. 'w32-shell-execute)
  1533. "open" 'file))
  1534. (cons 'system
  1535. (list (if (featurep 'xemacs)
  1536. 'mswindows-shell-execute
  1537. 'w32-shell-execute)
  1538. "open" 'file)))
  1539. "Default file applications on a Windows NT system.
  1540. The system \"open\" is used for most files.
  1541. See `org-file-apps'.")
  1542. (defcustom org-file-apps
  1543. '(
  1544. (auto-mode . emacs)
  1545. ("\\.mm\\'" . default)
  1546. ("\\.x?html?\\'" . default)
  1547. ("\\.pdf\\'" . default)
  1548. )
  1549. "External applications for opening `file:path' items in a document.
  1550. Org-mode uses system defaults for different file types, but
  1551. you can use this variable to set the application for a given file
  1552. extension. The entries in this list are cons cells where the car identifies
  1553. files and the cdr the corresponding command. Possible values for the
  1554. file identifier are
  1555. \"string\" A string as a file identifier can be interpreted in different
  1556. ways, depending on its contents:
  1557. - Alphanumeric characters only:
  1558. Match links with this file extension.
  1559. Example: (\"pdf\" . \"evince %s\")
  1560. to open PDFs with evince.
  1561. - Regular expression: Match links where the
  1562. filename matches the regexp. If you want to
  1563. use groups here, use shy groups.
  1564. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1565. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1566. to open *.html and *.xhtml with firefox.
  1567. - Regular expression which contains (non-shy) groups:
  1568. Match links where the whole link, including \"::\", and
  1569. anything after that, matches the regexp.
  1570. In a custom command string, %1, %2, etc. are replaced with
  1571. the parts of the link that were matched by the groups.
  1572. For backwards compatibility, if a command string is given
  1573. that does not use any of the group matches, this case is
  1574. handled identically to the second one (i.e. match against
  1575. file name only).
  1576. In a custom lisp form, you can access the group matches with
  1577. (match-string n link).
  1578. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1579. to open [[file:document.pdf::5]] with evince at page 5.
  1580. `directory' Matches a directory
  1581. `remote' Matches a remote file, accessible through tramp or efs.
  1582. Remote files most likely should be visited through Emacs
  1583. because external applications cannot handle such paths.
  1584. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1585. so all files Emacs knows how to handle. Using this with
  1586. command `emacs' will open most files in Emacs. Beware that this
  1587. will also open html files inside Emacs, unless you add
  1588. (\"html\" . default) to the list as well.
  1589. t Default for files not matched by any of the other options.
  1590. `system' The system command to open files, like `open' on Windows
  1591. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1592. that will be selected if you call `C-c C-o' with a double
  1593. \\[universal-argument] \\[universal-argument] prefix.
  1594. Possible values for the command are:
  1595. `emacs' The file will be visited by the current Emacs process.
  1596. `default' Use the default application for this file type, which is the
  1597. association for t in the list, most likely in the system-specific
  1598. part.
  1599. This can be used to overrule an unwanted setting in the
  1600. system-specific variable.
  1601. `system' Use the system command for opening files, like \"open\".
  1602. This command is specified by the entry whose car is `system'.
  1603. Most likely, the system-specific version of this variable
  1604. does define this command, but you can overrule/replace it
  1605. here.
  1606. string A command to be executed by a shell; %s will be replaced
  1607. by the path to the file.
  1608. sexp A Lisp form which will be evaluated. The file path will
  1609. be available in the Lisp variable `file'.
  1610. For more examples, see the system specific constants
  1611. `org-file-apps-defaults-macosx'
  1612. `org-file-apps-defaults-windowsnt'
  1613. `org-file-apps-defaults-gnu'."
  1614. :group 'org-link-follow
  1615. :type '(repeat
  1616. (cons (choice :value ""
  1617. (string :tag "Extension")
  1618. (const :tag "System command to open files" system)
  1619. (const :tag "Default for unrecognized files" t)
  1620. (const :tag "Remote file" remote)
  1621. (const :tag "Links to a directory" directory)
  1622. (const :tag "Any files that have Emacs modes"
  1623. auto-mode))
  1624. (choice :value ""
  1625. (const :tag "Visit with Emacs" emacs)
  1626. (const :tag "Use default" default)
  1627. (const :tag "Use the system command" system)
  1628. (string :tag "Command")
  1629. (sexp :tag "Lisp form")))))
  1630. (defcustom org-doi-server-url "http://dx.doi.org/"
  1631. "The URL of the DOI server."
  1632. :type 'string
  1633. ;; :version "24.3"
  1634. :group 'org-link-follow)
  1635. (defgroup org-refile nil
  1636. "Options concerning refiling entries in Org-mode."
  1637. :tag "Org Refile"
  1638. :group 'org)
  1639. (defcustom org-directory "~/org"
  1640. "Directory with org files.
  1641. This is just a default location to look for Org files. There is no need
  1642. at all to put your files into this directory. It is only used in the
  1643. following situations:
  1644. 1. When a capture template specifies a target file that is not an
  1645. absolute path. The path will then be interpreted relative to
  1646. `org-directory'
  1647. 2. When a capture note is filed away in an interactive way (when exiting the
  1648. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1649. with `org-directory' as the default path."
  1650. :group 'org-refile
  1651. :group 'org-remember
  1652. :group 'org-capture
  1653. :type 'directory)
  1654. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1655. "Default target for storing notes.
  1656. Used as a fall back file for org-remember.el and org-capture.el, for
  1657. templates that do not specify a target file."
  1658. :group 'org-refile
  1659. :group 'org-remember
  1660. :group 'org-capture
  1661. :type '(choice
  1662. (const :tag "Default from remember-data-file" nil)
  1663. file))
  1664. (defcustom org-goto-interface 'outline
  1665. "The default interface to be used for `org-goto'.
  1666. Allowed values are:
  1667. outline The interface shows an outline of the relevant file
  1668. and the correct heading is found by moving through
  1669. the outline or by searching with incremental search.
  1670. outline-path-completion Headlines in the current buffer are offered via
  1671. completion. This is the interface also used by
  1672. the refile command."
  1673. :group 'org-refile
  1674. :type '(choice
  1675. (const :tag "Outline" outline)
  1676. (const :tag "Outline-path-completion" outline-path-completion)))
  1677. (defcustom org-goto-max-level 5
  1678. "Maximum target level when running `org-goto' with refile interface."
  1679. :group 'org-refile
  1680. :type 'integer)
  1681. (defcustom org-reverse-note-order nil
  1682. "Non-nil means store new notes at the beginning of a file or entry.
  1683. When nil, new notes will be filed to the end of a file or entry.
  1684. This can also be a list with cons cells of regular expressions that
  1685. are matched against file names, and values."
  1686. :group 'org-remember
  1687. :group 'org-capture
  1688. :group 'org-refile
  1689. :type '(choice
  1690. (const :tag "Reverse always" t)
  1691. (const :tag "Reverse never" nil)
  1692. (repeat :tag "By file name regexp"
  1693. (cons regexp boolean))))
  1694. (defcustom org-log-refile nil
  1695. "Information to record when a task is refiled.
  1696. Possible values are:
  1697. nil Don't add anything
  1698. time Add a time stamp to the task
  1699. note Prompt for a note and add it with template `org-log-note-headings'
  1700. This option can also be set with on a per-file-basis with
  1701. #+STARTUP: nologrefile
  1702. #+STARTUP: logrefile
  1703. #+STARTUP: lognoterefile
  1704. You can have local logging settings for a subtree by setting the LOGGING
  1705. property to one or more of these keywords.
  1706. When bulk-refiling from the agenda, the value `note' is forbidden and
  1707. will temporarily be changed to `time'."
  1708. :group 'org-refile
  1709. :group 'org-progress
  1710. :version "24.1"
  1711. :type '(choice
  1712. (const :tag "No logging" nil)
  1713. (const :tag "Record timestamp" time)
  1714. (const :tag "Record timestamp with note." note)))
  1715. (defcustom org-refile-targets nil
  1716. "Targets for refiling entries with \\[org-refile].
  1717. This is a list of cons cells. Each cell contains:
  1718. - a specification of the files to be considered, either a list of files,
  1719. or a symbol whose function or variable value will be used to retrieve
  1720. a file name or a list of file names. If you use `org-agenda-files' for
  1721. that, all agenda files will be scanned for targets. Nil means consider
  1722. headings in the current buffer.
  1723. - A specification of how to find candidate refile targets. This may be
  1724. any of:
  1725. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1726. This tag has to be present in all target headlines, inheritance will
  1727. not be considered.
  1728. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1729. todo keyword.
  1730. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1731. headlines that are refiling targets.
  1732. - a cons cell (:level . N). Any headline of level N is considered a target.
  1733. Note that, when `org-odd-levels-only' is set, level corresponds to
  1734. order in hierarchy, not to the number of stars.
  1735. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1736. Note that, when `org-odd-levels-only' is set, level corresponds to
  1737. order in hierarchy, not to the number of stars.
  1738. Each element of this list generates a set of possible targets.
  1739. The union of these sets is presented (with completion) to
  1740. the user by `org-refile'.
  1741. You can set the variable `org-refile-target-verify-function' to a function
  1742. to verify each headline found by the simple criteria above.
  1743. When this variable is nil, all top-level headlines in the current buffer
  1744. are used, equivalent to the value `((nil . (:level . 1))'."
  1745. :group 'org-refile
  1746. :type '(repeat
  1747. (cons
  1748. (choice :value org-agenda-files
  1749. (const :tag "All agenda files" org-agenda-files)
  1750. (const :tag "Current buffer" nil)
  1751. (function) (variable) (file))
  1752. (choice :tag "Identify target headline by"
  1753. (cons :tag "Specific tag" (const :value :tag) (string))
  1754. (cons :tag "TODO keyword" (const :value :todo) (string))
  1755. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1756. (cons :tag "Level number" (const :value :level) (integer))
  1757. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1758. (defcustom org-refile-target-verify-function nil
  1759. "Function to verify if the headline at point should be a refile target.
  1760. The function will be called without arguments, with point at the
  1761. beginning of the headline. It should return t and leave point
  1762. where it is if the headline is a valid target for refiling.
  1763. If the target should not be selected, the function must return nil.
  1764. In addition to this, it may move point to a place from where the search
  1765. should be continued. For example, the function may decide that the entire
  1766. subtree of the current entry should be excluded and move point to the end
  1767. of the subtree."
  1768. :group 'org-refile
  1769. :type 'function)
  1770. (defcustom org-refile-use-cache nil
  1771. "Non-nil means cache refile targets to speed up the process.
  1772. The cache for a particular file will be updated automatically when
  1773. the buffer has been killed, or when any of the marker used for flagging
  1774. refile targets no longer points at a live buffer.
  1775. If you have added new entries to a buffer that might themselves be targets,
  1776. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1777. find that easier, `C-u C-u C-u C-c C-w'."
  1778. :group 'org-refile
  1779. :version "24.1"
  1780. :type 'boolean)
  1781. (defcustom org-refile-use-outline-path nil
  1782. "Non-nil means provide refile targets as paths.
  1783. So a level 3 headline will be available as level1/level2/level3.
  1784. When the value is `file', also include the file name (without directory)
  1785. into the path. In this case, you can also stop the completion after
  1786. the file name, to get entries inserted as top level in the file.
  1787. When `full-file-path', include the full file path."
  1788. :group 'org-refile
  1789. :type '(choice
  1790. (const :tag "Not" nil)
  1791. (const :tag "Yes" t)
  1792. (const :tag "Start with file name" file)
  1793. (const :tag "Start with full file path" full-file-path)))
  1794. (defcustom org-outline-path-complete-in-steps t
  1795. "Non-nil means complete the outline path in hierarchical steps.
  1796. When Org-mode uses the refile interface to select an outline path
  1797. \(see variable `org-refile-use-outline-path'), the completion of
  1798. the path can be done is a single go, or if can be done in steps down
  1799. the headline hierarchy. Going in steps is probably the best if you
  1800. do not use a special completion package like `ido' or `icicles'.
  1801. However, when using these packages, going in one step can be very
  1802. fast, while still showing the whole path to the entry."
  1803. :group 'org-refile
  1804. :type 'boolean)
  1805. (defcustom org-refile-allow-creating-parent-nodes nil
  1806. "Non-nil means allow to create new nodes as refile targets.
  1807. New nodes are then created by adding \"/new node name\" to the completion
  1808. of an existing node. When the value of this variable is `confirm',
  1809. new node creation must be confirmed by the user (recommended)
  1810. When nil, the completion must match an existing entry.
  1811. Note that, if the new heading is not seen by the criteria
  1812. listed in `org-refile-targets', multiple instances of the same
  1813. heading would be created by trying again to file under the new
  1814. heading."
  1815. :group 'org-refile
  1816. :type '(choice
  1817. (const :tag "Never" nil)
  1818. (const :tag "Always" t)
  1819. (const :tag "Prompt for confirmation" confirm)))
  1820. (defcustom org-refile-active-region-within-subtree nil
  1821. "Non-nil means also refile active region within a subtree.
  1822. By default `org-refile' doesn't allow refiling regions if they
  1823. don't contain a set of subtrees, but it might be convenient to
  1824. do so sometimes: in that case, the first line of the region is
  1825. converted to a headline before refiling."
  1826. :group 'org-refile
  1827. :version "24.1"
  1828. :type 'boolean)
  1829. (defgroup org-todo nil
  1830. "Options concerning TODO items in Org-mode."
  1831. :tag "Org TODO"
  1832. :group 'org)
  1833. (defgroup org-progress nil
  1834. "Options concerning Progress logging in Org-mode."
  1835. :tag "Org Progress"
  1836. :group 'org-time)
  1837. (defvar org-todo-interpretation-widgets
  1838. '((:tag "Sequence (cycling hits every state)" sequence)
  1839. (:tag "Type (cycling directly to DONE)" type))
  1840. "The available interpretation symbols for customizing `org-todo-keywords'.
  1841. Interested libraries should add to this list.")
  1842. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1843. "List of TODO entry keyword sequences and their interpretation.
  1844. \\<org-mode-map>This is a list of sequences.
  1845. Each sequence starts with a symbol, either `sequence' or `type',
  1846. indicating if the keywords should be interpreted as a sequence of
  1847. action steps, or as different types of TODO items. The first
  1848. keywords are states requiring action - these states will select a headline
  1849. for inclusion into the global TODO list Org-mode produces. If one of
  1850. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1851. signify that no further action is necessary. If \"|\" is not found,
  1852. the last keyword is treated as the only DONE state of the sequence.
  1853. The command \\[org-todo] cycles an entry through these states, and one
  1854. additional state where no keyword is present. For details about this
  1855. cycling, see the manual.
  1856. TODO keywords and interpretation can also be set on a per-file basis with
  1857. the special #+SEQ_TODO and #+TYP_TODO lines.
  1858. Each keyword can optionally specify a character for fast state selection
  1859. \(in combination with the variable `org-use-fast-todo-selection')
  1860. and specifiers for state change logging, using the same syntax that
  1861. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1862. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1863. indicates to record a time stamp each time this state is selected.
  1864. Each keyword may also specify if a timestamp or a note should be
  1865. recorded when entering or leaving the state, by adding additional
  1866. characters in the parenthesis after the keyword. This looks like this:
  1867. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1868. record only the time of the state change. With X and Y being either
  1869. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1870. Y when leaving the state if and only if the *target* state does not
  1871. define X. You may omit any of the fast-selection key or X or /Y,
  1872. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1873. For backward compatibility, this variable may also be just a list
  1874. of keywords. In this case the interpretation (sequence or type) will be
  1875. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1876. :group 'org-todo
  1877. :group 'org-keywords
  1878. :type '(choice
  1879. (repeat :tag "Old syntax, just keywords"
  1880. (string :tag "Keyword"))
  1881. (repeat :tag "New syntax"
  1882. (cons
  1883. (choice
  1884. :tag "Interpretation"
  1885. ;;Quick and dirty way to see
  1886. ;;`org-todo-interpretations'. This takes the
  1887. ;;place of item arguments
  1888. :convert-widget
  1889. (lambda (widget)
  1890. (widget-put widget
  1891. :args (mapcar
  1892. #'(lambda (x)
  1893. (widget-convert
  1894. (cons 'const x)))
  1895. org-todo-interpretation-widgets))
  1896. widget))
  1897. (repeat
  1898. (string :tag "Keyword"))))))
  1899. (defvar org-todo-keywords-1 nil
  1900. "All TODO and DONE keywords active in a buffer.")
  1901. (make-variable-buffer-local 'org-todo-keywords-1)
  1902. (defvar org-todo-keywords-for-agenda nil)
  1903. (defvar org-done-keywords-for-agenda nil)
  1904. (defvar org-drawers-for-agenda nil)
  1905. (defvar org-todo-keyword-alist-for-agenda nil)
  1906. (defvar org-tag-alist-for-agenda nil)
  1907. (defvar org-agenda-contributing-files nil)
  1908. (defvar org-not-done-keywords nil)
  1909. (make-variable-buffer-local 'org-not-done-keywords)
  1910. (defvar org-done-keywords nil)
  1911. (make-variable-buffer-local 'org-done-keywords)
  1912. (defvar org-todo-heads nil)
  1913. (make-variable-buffer-local 'org-todo-heads)
  1914. (defvar org-todo-sets nil)
  1915. (make-variable-buffer-local 'org-todo-sets)
  1916. (defvar org-todo-log-states nil)
  1917. (make-variable-buffer-local 'org-todo-log-states)
  1918. (defvar org-todo-kwd-alist nil)
  1919. (make-variable-buffer-local 'org-todo-kwd-alist)
  1920. (defvar org-todo-key-alist nil)
  1921. (make-variable-buffer-local 'org-todo-key-alist)
  1922. (defvar org-todo-key-trigger nil)
  1923. (make-variable-buffer-local 'org-todo-key-trigger)
  1924. (defcustom org-todo-interpretation 'sequence
  1925. "Controls how TODO keywords are interpreted.
  1926. This variable is in principle obsolete and is only used for
  1927. backward compatibility, if the interpretation of todo keywords is
  1928. not given already in `org-todo-keywords'. See that variable for
  1929. more information."
  1930. :group 'org-todo
  1931. :group 'org-keywords
  1932. :type '(choice (const sequence)
  1933. (const type)))
  1934. (defcustom org-use-fast-todo-selection t
  1935. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1936. This variable describes if and under what circumstances the cycling
  1937. mechanism for TODO keywords will be replaced by a single-key, direct
  1938. selection scheme.
  1939. When nil, fast selection is never used.
  1940. When the symbol `prefix', it will be used when `org-todo' is called
  1941. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1942. `C-u t' in an agenda buffer.
  1943. When t, fast selection is used by default. In this case, the prefix
  1944. argument forces cycling instead.
  1945. In all cases, the special interface is only used if access keys have
  1946. actually been assigned by the user, i.e. if keywords in the configuration
  1947. are followed by a letter in parenthesis, like TODO(t)."
  1948. :group 'org-todo
  1949. :type '(choice
  1950. (const :tag "Never" nil)
  1951. (const :tag "By default" t)
  1952. (const :tag "Only with C-u C-c C-t" prefix)))
  1953. (defcustom org-provide-todo-statistics t
  1954. "Non-nil means update todo statistics after insert and toggle.
  1955. ALL-HEADLINES means update todo statistics by including headlines
  1956. with no TODO keyword as well, counting them as not done.
  1957. A list of TODO keywords means the same, but skip keywords that are
  1958. not in this list.
  1959. When this is set, todo statistics is updated in the parent of the
  1960. current entry each time a todo state is changed."
  1961. :group 'org-todo
  1962. :type '(choice
  1963. (const :tag "Yes, only for TODO entries" t)
  1964. (const :tag "Yes, including all entries" 'all-headlines)
  1965. (repeat :tag "Yes, for TODOs in this list"
  1966. (string :tag "TODO keyword"))
  1967. (other :tag "No TODO statistics" nil)))
  1968. (defcustom org-hierarchical-todo-statistics t
  1969. "Non-nil means TODO statistics covers just direct children.
  1970. When nil, all entries in the subtree are considered.
  1971. This has only an effect if `org-provide-todo-statistics' is set.
  1972. To set this to nil for only a single subtree, use a COOKIE_DATA
  1973. property and include the word \"recursive\" into the value."
  1974. :group 'org-todo
  1975. :type 'boolean)
  1976. (defcustom org-after-todo-state-change-hook nil
  1977. "Hook which is run after the state of a TODO item was changed.
  1978. The new state (a string with a TODO keyword, or nil) is available in the
  1979. Lisp variable `org-state'."
  1980. :group 'org-todo
  1981. :type 'hook)
  1982. (defvar org-blocker-hook nil
  1983. "Hook for functions that are allowed to block a state change.
  1984. Each function gets as its single argument a property list, see
  1985. `org-trigger-hook' for more information about this list.
  1986. If any of the functions in this hook returns nil, the state change
  1987. is blocked.")
  1988. (defvar org-trigger-hook nil
  1989. "Hook for functions that are triggered by a state change.
  1990. Each function gets as its single argument a property list with at least
  1991. the following elements:
  1992. (:type type-of-change :position pos-at-entry-start
  1993. :from old-state :to new-state)
  1994. Depending on the type, more properties may be present.
  1995. This mechanism is currently implemented for:
  1996. TODO state changes
  1997. ------------------
  1998. :type todo-state-change
  1999. :from previous state (keyword as a string), or nil, or a symbol
  2000. 'todo' or 'done', to indicate the general type of state.
  2001. :to new state, like in :from")
  2002. (defcustom org-enforce-todo-dependencies nil
  2003. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2004. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2005. be blocked if any prior sibling is not yet done.
  2006. Finally, if the parent is blocked because of ordered siblings of its own,
  2007. the child will also be blocked."
  2008. :set (lambda (var val)
  2009. (set var val)
  2010. (if val
  2011. (add-hook 'org-blocker-hook
  2012. 'org-block-todo-from-children-or-siblings-or-parent)
  2013. (remove-hook 'org-blocker-hook
  2014. 'org-block-todo-from-children-or-siblings-or-parent)))
  2015. :group 'org-todo
  2016. :type 'boolean)
  2017. (defcustom org-enforce-todo-checkbox-dependencies nil
  2018. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2019. When this is nil, checkboxes have no influence on switching TODO states.
  2020. When non-nil, you first need to check off all check boxes before the TODO
  2021. entry can be switched to DONE.
  2022. This variable needs to be set before org.el is loaded, and you need to
  2023. restart Emacs after a change to make the change effective. The only way
  2024. to change is while Emacs is running is through the customize interface."
  2025. :set (lambda (var val)
  2026. (set var val)
  2027. (if val
  2028. (add-hook 'org-blocker-hook
  2029. 'org-block-todo-from-checkboxes)
  2030. (remove-hook 'org-blocker-hook
  2031. 'org-block-todo-from-checkboxes)))
  2032. :group 'org-todo
  2033. :type 'boolean)
  2034. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2035. "Non-nil means inserting a TODO heading is treated as state change.
  2036. So when the command \\[org-insert-todo-heading] is used, state change
  2037. logging will apply if appropriate. When nil, the new TODO item will
  2038. be inserted directly, and no logging will take place."
  2039. :group 'org-todo
  2040. :type 'boolean)
  2041. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2042. "Non-nil means switching TODO states with S-cursor counts as state change.
  2043. This is the default behavior. However, setting this to nil allows a
  2044. convenient way to select a TODO state and bypass any logging associated
  2045. with that."
  2046. :group 'org-todo
  2047. :type 'boolean)
  2048. (defcustom org-todo-state-tags-triggers nil
  2049. "Tag changes that should be triggered by TODO state changes.
  2050. This is a list. Each entry is
  2051. (state-change (tag . flag) .......)
  2052. State-change can be a string with a state, and empty string to indicate the
  2053. state that has no TODO keyword, or it can be one of the symbols `todo'
  2054. or `done', meaning any not-done or done state, respectively."
  2055. :group 'org-todo
  2056. :group 'org-tags
  2057. :type '(repeat
  2058. (cons (choice :tag "When changing to"
  2059. (const :tag "Not-done state" todo)
  2060. (const :tag "Done state" done)
  2061. (string :tag "State"))
  2062. (repeat
  2063. (cons :tag "Tag action"
  2064. (string :tag "Tag")
  2065. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2066. (defcustom org-log-done nil
  2067. "Information to record when a task moves to the DONE state.
  2068. Possible values are:
  2069. nil Don't add anything, just change the keyword
  2070. time Add a time stamp to the task
  2071. note Prompt for a note and add it with template `org-log-note-headings'
  2072. This option can also be set with on a per-file-basis with
  2073. #+STARTUP: nologdone
  2074. #+STARTUP: logdone
  2075. #+STARTUP: lognotedone
  2076. You can have local logging settings for a subtree by setting the LOGGING
  2077. property to one or more of these keywords."
  2078. :group 'org-todo
  2079. :group 'org-progress
  2080. :type '(choice
  2081. (const :tag "No logging" nil)
  2082. (const :tag "Record CLOSED timestamp" time)
  2083. (const :tag "Record CLOSED timestamp with note." note)))
  2084. ;; Normalize old uses of org-log-done.
  2085. (cond
  2086. ((eq org-log-done t) (setq org-log-done 'time))
  2087. ((and (listp org-log-done) (memq 'done org-log-done))
  2088. (setq org-log-done 'note)))
  2089. (defcustom org-log-reschedule nil
  2090. "Information to record when the scheduling date of a tasks is modified.
  2091. Possible values are:
  2092. nil Don't add anything, just change the date
  2093. time Add a time stamp to the task
  2094. note Prompt for a note and add it with template `org-log-note-headings'
  2095. This option can also be set with on a per-file-basis with
  2096. #+STARTUP: nologreschedule
  2097. #+STARTUP: logreschedule
  2098. #+STARTUP: lognotereschedule"
  2099. :group 'org-todo
  2100. :group 'org-progress
  2101. :type '(choice
  2102. (const :tag "No logging" nil)
  2103. (const :tag "Record timestamp" time)
  2104. (const :tag "Record timestamp with note." note)))
  2105. (defcustom org-log-redeadline nil
  2106. "Information to record when the deadline date of a tasks is modified.
  2107. Possible values are:
  2108. nil Don't add anything, just change the date
  2109. time Add a time stamp to the task
  2110. note Prompt for a note and add it with template `org-log-note-headings'
  2111. This option can also be set with on a per-file-basis with
  2112. #+STARTUP: nologredeadline
  2113. #+STARTUP: logredeadline
  2114. #+STARTUP: lognoteredeadline
  2115. You can have local logging settings for a subtree by setting the LOGGING
  2116. property to one or more of these keywords."
  2117. :group 'org-todo
  2118. :group 'org-progress
  2119. :type '(choice
  2120. (const :tag "No logging" nil)
  2121. (const :tag "Record timestamp" time)
  2122. (const :tag "Record timestamp with note." note)))
  2123. (defcustom org-log-note-clock-out nil
  2124. "Non-nil means record a note when clocking out of an item.
  2125. This can also be configured on a per-file basis by adding one of
  2126. the following lines anywhere in the buffer:
  2127. #+STARTUP: lognoteclock-out
  2128. #+STARTUP: nolognoteclock-out"
  2129. :group 'org-todo
  2130. :group 'org-progress
  2131. :type 'boolean)
  2132. (defcustom org-log-done-with-time t
  2133. "Non-nil means the CLOSED time stamp will contain date and time.
  2134. When nil, only the date will be recorded."
  2135. :group 'org-progress
  2136. :type 'boolean)
  2137. (defcustom org-log-note-headings
  2138. '((done . "CLOSING NOTE %t")
  2139. (state . "State %-12s from %-12S %t")
  2140. (note . "Note taken on %t")
  2141. (reschedule . "Rescheduled from %S on %t")
  2142. (delschedule . "Not scheduled, was %S on %t")
  2143. (redeadline . "New deadline from %S on %t")
  2144. (deldeadline . "Removed deadline, was %S on %t")
  2145. (refile . "Refiled on %t")
  2146. (clock-out . ""))
  2147. "Headings for notes added to entries.
  2148. The value is an alist, with the car being a symbol indicating the note
  2149. context, and the cdr is the heading to be used. The heading may also be the
  2150. empty string.
  2151. %t in the heading will be replaced by a time stamp.
  2152. %T will be an active time stamp instead the default inactive one
  2153. %d will be replaced by a short-format time stamp.
  2154. %D will be replaced by an active short-format time stamp.
  2155. %s will be replaced by the new TODO state, in double quotes.
  2156. %S will be replaced by the old TODO state, in double quotes.
  2157. %u will be replaced by the user name.
  2158. %U will be replaced by the full user name.
  2159. In fact, it is not a good idea to change the `state' entry, because
  2160. agenda log mode depends on the format of these entries."
  2161. :group 'org-todo
  2162. :group 'org-progress
  2163. :type '(list :greedy t
  2164. (cons (const :tag "Heading when closing an item" done) string)
  2165. (cons (const :tag
  2166. "Heading when changing todo state (todo sequence only)"
  2167. state) string)
  2168. (cons (const :tag "Heading when just taking a note" note) string)
  2169. (cons (const :tag "Heading when clocking out" clock-out) string)
  2170. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2171. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2172. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2173. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2174. (cons (const :tag "Heading when refiling" refile) string)))
  2175. (unless (assq 'note org-log-note-headings)
  2176. (push '(note . "%t") org-log-note-headings))
  2177. (defcustom org-log-into-drawer nil
  2178. "Non-nil means insert state change notes and time stamps into a drawer.
  2179. When nil, state changes notes will be inserted after the headline and
  2180. any scheduling and clock lines, but not inside a drawer.
  2181. The value of this variable should be the name of the drawer to use.
  2182. LOGBOOK is proposed as the default drawer for this purpose, you can
  2183. also set this to a string to define the drawer of your choice.
  2184. A value of t is also allowed, representing \"LOGBOOK\".
  2185. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2186. will be ignored.
  2187. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2188. a subtree."
  2189. :group 'org-todo
  2190. :group 'org-progress
  2191. :type '(choice
  2192. (const :tag "Not into a drawer" nil)
  2193. (const :tag "LOGBOOK" t)
  2194. (string :tag "Other")))
  2195. (if (fboundp 'defvaralias)
  2196. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2197. (defun org-log-into-drawer ()
  2198. "Return the value of `org-log-into-drawer', but let properties overrule.
  2199. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2200. used instead of the default value."
  2201. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2202. (cond
  2203. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2204. ((equal p "t") "LOGBOOK")
  2205. (t p))))
  2206. (defcustom org-log-state-notes-insert-after-drawers nil
  2207. "Non-nil means insert state change notes after any drawers in entry.
  2208. Only the drawers that *immediately* follow the headline and the
  2209. deadline/scheduled line are skipped.
  2210. When nil, insert notes right after the heading and perhaps the line
  2211. with deadline/scheduling if present.
  2212. This variable will have no effect if `org-log-into-drawer' is
  2213. set."
  2214. :group 'org-todo
  2215. :group 'org-progress
  2216. :type 'boolean)
  2217. (defcustom org-log-states-order-reversed t
  2218. "Non-nil means the latest state note will be directly after heading.
  2219. When nil, the state change notes will be ordered according to time."
  2220. :group 'org-todo
  2221. :group 'org-progress
  2222. :type 'boolean)
  2223. (defcustom org-todo-repeat-to-state nil
  2224. "The TODO state to which a repeater should return the repeating task.
  2225. By default this is the first task in a TODO sequence, or the previous state
  2226. in a TODO_TYP set. But you can specify another task here.
  2227. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2228. :group 'org-todo
  2229. :version "24.1"
  2230. :type '(choice (const :tag "Head of sequence" nil)
  2231. (string :tag "Specific state")))
  2232. (defcustom org-log-repeat 'time
  2233. "Non-nil means record moving through the DONE state when triggering repeat.
  2234. An auto-repeating task is immediately switched back to TODO when
  2235. marked DONE. If you are not logging state changes (by adding \"@\"
  2236. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2237. record a closing note, there will be no record of the task moving
  2238. through DONE. This variable forces taking a note anyway.
  2239. nil Don't force a record
  2240. time Record a time stamp
  2241. note Prompt for a note and add it with template `org-log-note-headings'
  2242. This option can also be set with on a per-file-basis with
  2243. #+STARTUP: nologrepeat
  2244. #+STARTUP: logrepeat
  2245. #+STARTUP: lognoterepeat
  2246. You can have local logging settings for a subtree by setting the LOGGING
  2247. property to one or more of these keywords."
  2248. :group 'org-todo
  2249. :group 'org-progress
  2250. :type '(choice
  2251. (const :tag "Don't force a record" nil)
  2252. (const :tag "Force recording the DONE state" time)
  2253. (const :tag "Force recording a note with the DONE state" note)))
  2254. (defgroup org-priorities nil
  2255. "Priorities in Org-mode."
  2256. :tag "Org Priorities"
  2257. :group 'org-todo)
  2258. (defcustom org-enable-priority-commands t
  2259. "Non-nil means priority commands are active.
  2260. When nil, these commands will be disabled, so that you never accidentally
  2261. set a priority."
  2262. :group 'org-priorities
  2263. :type 'boolean)
  2264. (defcustom org-highest-priority ?A
  2265. "The highest priority of TODO items. A character like ?A, ?B etc.
  2266. Must have a smaller ASCII number than `org-lowest-priority'."
  2267. :group 'org-priorities
  2268. :type 'character)
  2269. (defcustom org-lowest-priority ?C
  2270. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2271. Must have a larger ASCII number than `org-highest-priority'."
  2272. :group 'org-priorities
  2273. :type 'character)
  2274. (defcustom org-default-priority ?B
  2275. "The default priority of TODO items.
  2276. This is the priority an item gets if no explicit priority is given.
  2277. When starting to cycle on an empty priority the first step in the cycle
  2278. depends on `org-priority-start-cycle-with-default'. The resulting first
  2279. step priority must not exceed the range from `org-highest-priority' to
  2280. `org-lowest-priority' which means that `org-default-priority' has to be
  2281. in this range exclusive or inclusive the range boundaries. Else the
  2282. first step refuses to set the default and the second will fall back
  2283. to (depending on the command used) the highest or lowest priority."
  2284. :group 'org-priorities
  2285. :type 'character)
  2286. (defcustom org-priority-start-cycle-with-default t
  2287. "Non-nil means start with default priority when starting to cycle.
  2288. When this is nil, the first step in the cycle will be (depending on the
  2289. command used) one higher or lower than the default priority.
  2290. See also `org-default-priority'."
  2291. :group 'org-priorities
  2292. :type 'boolean)
  2293. (defcustom org-get-priority-function nil
  2294. "Function to extract the priority from a string.
  2295. The string is normally the headline. If this is nil Org computes the
  2296. priority from the priority cookie like [#A] in the headline. It returns
  2297. an integer, increasing by 1000 for each priority level.
  2298. The user can set a different function here, which should take a string
  2299. as an argument and return the numeric priority."
  2300. :group 'org-priorities
  2301. :version "24.1"
  2302. :type 'function)
  2303. (defgroup org-time nil
  2304. "Options concerning time stamps and deadlines in Org-mode."
  2305. :tag "Org Time"
  2306. :group 'org)
  2307. (defcustom org-insert-labeled-timestamps-at-point nil
  2308. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2309. When nil, these labeled time stamps are forces into the second line of an
  2310. entry, just after the headline. When scheduling from the global TODO list,
  2311. the time stamp will always be forced into the second line."
  2312. :group 'org-time
  2313. :type 'boolean)
  2314. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2315. "Formats for `format-time-string' which are used for time stamps.
  2316. It is not recommended to change this constant.")
  2317. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2318. "Number of minutes to round time stamps to.
  2319. These are two values, the first applies when first creating a time stamp.
  2320. The second applies when changing it with the commands `S-up' and `S-down'.
  2321. When changing the time stamp, this means that it will change in steps
  2322. of N minutes, as given by the second value.
  2323. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2324. numbers should be factors of 60, so for example 5, 10, 15.
  2325. When this is larger than 1, you can still force an exact time stamp by using
  2326. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2327. and by using a prefix arg to `S-up/down' to specify the exact number
  2328. of minutes to shift."
  2329. :group 'org-time
  2330. :get #'(lambda (var) ; Make sure both elements are there
  2331. (if (integerp (default-value var))
  2332. (list (default-value var) 5)
  2333. (default-value var)))
  2334. :type '(list
  2335. (integer :tag "when inserting times")
  2336. (integer :tag "when modifying times")))
  2337. ;; Normalize old customizations of this variable.
  2338. (when (integerp org-time-stamp-rounding-minutes)
  2339. (setq org-time-stamp-rounding-minutes
  2340. (list org-time-stamp-rounding-minutes
  2341. org-time-stamp-rounding-minutes)))
  2342. (defcustom org-display-custom-times nil
  2343. "Non-nil means overlay custom formats over all time stamps.
  2344. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2345. To turn this on on a per-file basis, insert anywhere in the file:
  2346. #+STARTUP: customtime"
  2347. :group 'org-time
  2348. :set 'set-default
  2349. :type 'sexp)
  2350. (make-variable-buffer-local 'org-display-custom-times)
  2351. (defcustom org-time-stamp-custom-formats
  2352. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2353. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2354. These are overlaid over the default ISO format if the variable
  2355. `org-display-custom-times' is set. Time like %H:%M should be at the
  2356. end of the second format. The custom formats are also honored by export
  2357. commands, if custom time display is turned on at the time of export."
  2358. :group 'org-time
  2359. :type 'sexp)
  2360. (defun org-time-stamp-format (&optional long inactive)
  2361. "Get the right format for a time string."
  2362. (let ((f (if long (cdr org-time-stamp-formats)
  2363. (car org-time-stamp-formats))))
  2364. (if inactive
  2365. (concat "[" (substring f 1 -1) "]")
  2366. f)))
  2367. (defcustom org-time-clocksum-format "%d:%02d"
  2368. "The format string used when creating CLOCKSUM lines.
  2369. This is also used when org-mode generates a time duration."
  2370. :group 'org-time
  2371. :type 'string)
  2372. (defcustom org-time-clocksum-use-fractional nil
  2373. "If non-nil, \\[org-clock-display] uses fractional times.
  2374. org-mode generates a time duration."
  2375. :group 'org-time
  2376. :type 'boolean)
  2377. (defcustom org-time-clocksum-fractional-format "%.2f"
  2378. "The format string used when creating CLOCKSUM lines, or when
  2379. org-mode generates a time duration."
  2380. :group 'org-time
  2381. :type 'string)
  2382. (defcustom org-deadline-warning-days 14
  2383. "No. of days before expiration during which a deadline becomes active.
  2384. This variable governs the display in sparse trees and in the agenda.
  2385. When 0 or negative, it means use this number (the absolute value of it)
  2386. even if a deadline has a different individual lead time specified.
  2387. Custom commands can set this variable in the options section."
  2388. :group 'org-time
  2389. :group 'org-agenda-daily/weekly
  2390. :type 'integer)
  2391. (defcustom org-read-date-prefer-future t
  2392. "Non-nil means assume future for incomplete date input from user.
  2393. This affects the following situations:
  2394. 1. The user gives a month but not a year.
  2395. For example, if it is April and you enter \"feb 2\", this will be read
  2396. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2397. 2. The user gives a day, but no month.
  2398. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2399. read this as the third of *next* month. However, if you enter \"17\",
  2400. it will be considered as *this* month.
  2401. If you set this variable to the symbol `time', then also the following
  2402. will work:
  2403. 3. If the user gives a time.
  2404. If the time is before now, it will be interpreted as tomorrow.
  2405. Currently none of this works for ISO week specifications.
  2406. When this option is nil, the current day, month and year will always be
  2407. used as defaults.
  2408. See also `org-agenda-jump-prefer-future'."
  2409. :group 'org-time
  2410. :type '(choice
  2411. (const :tag "Never" nil)
  2412. (const :tag "Check month and day" t)
  2413. (const :tag "Check month, day, and time" time)))
  2414. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2415. "Should the agenda jump command prefer the future for incomplete dates?
  2416. The default is to do the same as configured in `org-read-date-prefer-future'.
  2417. But you can also set a deviating value here.
  2418. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2419. :group 'org-agenda
  2420. :group 'org-time
  2421. :version "24.1"
  2422. :type '(choice
  2423. (const :tag "Use org-read-date-prefer-future"
  2424. org-read-date-prefer-future)
  2425. (const :tag "Never" nil)
  2426. (const :tag "Always" t)))
  2427. (defcustom org-read-date-force-compatible-dates t
  2428. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2429. Depending on the system Emacs is running on, certain dates cannot
  2430. be represented with the type used internally to represent time.
  2431. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2432. correctly. Some systems allow for earlier dates, some for later,
  2433. some for both. One way to find out it to insert any date into an
  2434. Org buffer, putting the cursor on the year and hitting S-up and
  2435. S-down to test the range.
  2436. When this variable is set to t, the date/time prompt will not let
  2437. you specify dates outside the 1970-2037 range, so it is certain that
  2438. these dates will work in whatever version of Emacs you are
  2439. running, and also that you can move a file from one Emacs implementation
  2440. to another. WHenever Org is forcing the year for you, it will display
  2441. a message and beep.
  2442. When this variable is nil, Org will check if the date is
  2443. representable in the specific Emacs implementation you are using.
  2444. If not, it will force a year, usually the current year, and beep
  2445. to remind you. Currently this setting is not recommended because
  2446. the likelihood that you will open your Org files in an Emacs that
  2447. has limited date range is not negligible.
  2448. A workaround for this problem is to use diary sexp dates for time
  2449. stamps outside of this range."
  2450. :group 'org-time
  2451. :version "24.1"
  2452. :type 'boolean)
  2453. (defcustom org-read-date-display-live t
  2454. "Non-nil means display current interpretation of date prompt live.
  2455. This display will be in an overlay, in the minibuffer."
  2456. :group 'org-time
  2457. :type 'boolean)
  2458. (defcustom org-read-date-popup-calendar t
  2459. "Non-nil means pop up a calendar when prompting for a date.
  2460. In the calendar, the date can be selected with mouse-1. However, the
  2461. minibuffer will also be active, and you can simply enter the date as well.
  2462. When nil, only the minibuffer will be available."
  2463. :group 'org-time
  2464. :type 'boolean)
  2465. (if (fboundp 'defvaralias)
  2466. (defvaralias 'org-popup-calendar-for-date-prompt
  2467. 'org-read-date-popup-calendar))
  2468. (defcustom org-read-date-minibuffer-setup-hook nil
  2469. "Hook to be used to set up keys for the date/time interface.
  2470. Add key definitions to `minibuffer-local-map', which will be a temporary
  2471. copy."
  2472. :group 'org-time
  2473. :type 'hook)
  2474. (defcustom org-extend-today-until 0
  2475. "The hour when your day really ends. Must be an integer.
  2476. This has influence for the following applications:
  2477. - When switching the agenda to \"today\". It it is still earlier than
  2478. the time given here, the day recognized as TODAY is actually yesterday.
  2479. - When a date is read from the user and it is still before the time given
  2480. here, the current date and time will be assumed to be yesterday, 23:59.
  2481. Also, timestamps inserted in capture templates follow this rule.
  2482. IMPORTANT: This is a feature whose implementation is and likely will
  2483. remain incomplete. Really, it is only here because past midnight seems to
  2484. be the favorite working time of John Wiegley :-)"
  2485. :group 'org-time
  2486. :type 'integer)
  2487. (defcustom org-use-effective-time nil
  2488. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2489. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2490. \"effective time\" of any timestamps between midnight and 8am will be
  2491. 23:59 of the previous day."
  2492. :group 'org-time
  2493. :version "24.1"
  2494. :type 'boolean)
  2495. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2496. "When non-nil, use the last clock out time for `org-todo'.
  2497. Note that this option has precedence over the combined use of
  2498. `org-use-effective-time' and `org-extend-today-until'."
  2499. :group 'org-time
  2500. ;; :version "24.3"
  2501. :type 'boolean)
  2502. (defcustom org-edit-timestamp-down-means-later nil
  2503. "Non-nil means S-down will increase the time in a time stamp.
  2504. When nil, S-up will increase."
  2505. :group 'org-time
  2506. :type 'boolean)
  2507. (defcustom org-calendar-follow-timestamp-change t
  2508. "Non-nil means make the calendar window follow timestamp changes.
  2509. When a timestamp is modified and the calendar window is visible, it will be
  2510. moved to the new date."
  2511. :group 'org-time
  2512. :type 'boolean)
  2513. (defgroup org-tags nil
  2514. "Options concerning tags in Org-mode."
  2515. :tag "Org Tags"
  2516. :group 'org)
  2517. (defcustom org-tag-alist nil
  2518. "List of tags allowed in Org-mode files.
  2519. When this list is nil, Org-mode will base TAG input on what is already in the
  2520. buffer.
  2521. The value of this variable is an alist, the car of each entry must be a
  2522. keyword as a string, the cdr may be a character that is used to select
  2523. that tag through the fast-tag-selection interface.
  2524. See the manual for details."
  2525. :group 'org-tags
  2526. :type '(repeat
  2527. (choice
  2528. (cons (string :tag "Tag name")
  2529. (character :tag "Access char"))
  2530. (list :tag "Start radio group"
  2531. (const :startgroup)
  2532. (option (string :tag "Group description")))
  2533. (list :tag "End radio group"
  2534. (const :endgroup)
  2535. (option (string :tag "Group description")))
  2536. (const :tag "New line" (:newline)))))
  2537. (defcustom org-tag-persistent-alist nil
  2538. "List of tags that will always appear in all Org-mode files.
  2539. This is in addition to any in buffer settings or customizations
  2540. of `org-tag-alist'.
  2541. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2542. The value of this variable is an alist, the car of each entry must be a
  2543. keyword as a string, the cdr may be a character that is used to select
  2544. that tag through the fast-tag-selection interface.
  2545. See the manual for details.
  2546. To disable these tags on a per-file basis, insert anywhere in the file:
  2547. #+STARTUP: noptag"
  2548. :group 'org-tags
  2549. :type '(repeat
  2550. (choice
  2551. (cons (string :tag "Tag name")
  2552. (character :tag "Access char"))
  2553. (const :tag "Start radio group" (:startgroup))
  2554. (const :tag "End radio group" (:endgroup))
  2555. (const :tag "New line" (:newline)))))
  2556. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2557. "If non-nil, always offer completion for all tags of all agenda files.
  2558. Instead of customizing this variable directly, you might want to
  2559. set it locally for capture buffers, because there no list of
  2560. tags in that file can be created dynamically (there are none).
  2561. (add-hook 'org-capture-mode-hook
  2562. (lambda ()
  2563. (set (make-local-variable
  2564. 'org-complete-tags-always-offer-all-agenda-tags)
  2565. t)))"
  2566. :group 'org-tags
  2567. :version "24.1"
  2568. :type 'boolean)
  2569. (defvar org-file-tags nil
  2570. "List of tags that can be inherited by all entries in the file.
  2571. The tags will be inherited if the variable `org-use-tag-inheritance'
  2572. says they should be.
  2573. This variable is populated from #+FILETAGS lines.")
  2574. (defcustom org-use-fast-tag-selection 'auto
  2575. "Non-nil means use fast tag selection scheme.
  2576. This is a special interface to select and deselect tags with single keys.
  2577. When nil, fast selection is never used.
  2578. When the symbol `auto', fast selection is used if and only if selection
  2579. characters for tags have been configured, either through the variable
  2580. `org-tag-alist' or through a #+TAGS line in the buffer.
  2581. When t, fast selection is always used and selection keys are assigned
  2582. automatically if necessary."
  2583. :group 'org-tags
  2584. :type '(choice
  2585. (const :tag "Always" t)
  2586. (const :tag "Never" nil)
  2587. (const :tag "When selection characters are configured" 'auto)))
  2588. (defcustom org-fast-tag-selection-single-key nil
  2589. "Non-nil means fast tag selection exits after first change.
  2590. When nil, you have to press RET to exit it.
  2591. During fast tag selection, you can toggle this flag with `C-c'.
  2592. This variable can also have the value `expert'. In this case, the window
  2593. displaying the tags menu is not even shown, until you press C-c again."
  2594. :group 'org-tags
  2595. :type '(choice
  2596. (const :tag "No" nil)
  2597. (const :tag "Yes" t)
  2598. (const :tag "Expert" expert)))
  2599. (defvar org-fast-tag-selection-include-todo nil
  2600. "Non-nil means fast tags selection interface will also offer TODO states.
  2601. This is an undocumented feature, you should not rely on it.")
  2602. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2603. "The column to which tags should be indented in a headline.
  2604. If this number is positive, it specifies the column. If it is negative,
  2605. it means that the tags should be flushright to that column. For example,
  2606. -80 works well for a normal 80 character screen.
  2607. When 0, place tags directly after headline text, with only one space in
  2608. between."
  2609. :group 'org-tags
  2610. :type 'integer)
  2611. (defcustom org-auto-align-tags t
  2612. "Non-nil keeps tags aligned when modifying headlines.
  2613. Some operations (i.e. demoting) change the length of a headline and
  2614. therefore shift the tags around. With this option turned on, after
  2615. each such operation the tags are again aligned to `org-tags-column'."
  2616. :group 'org-tags
  2617. :type 'boolean)
  2618. (defcustom org-use-tag-inheritance t
  2619. "Non-nil means tags in levels apply also for sublevels.
  2620. When nil, only the tags directly given in a specific line apply there.
  2621. This may also be a list of tags that should be inherited, or a regexp that
  2622. matches tags that should be inherited. Additional control is possible
  2623. with the variable `org-tags-exclude-from-inheritance' which gives an
  2624. explicit list of tags to be excluded from inheritance., even if the value of
  2625. `org-use-tag-inheritance' would select it for inheritance.
  2626. If this option is t, a match early-on in a tree can lead to a large
  2627. number of matches in the subtree when constructing the agenda or creating
  2628. a sparse tree. If you only want to see the first match in a tree during
  2629. a search, check out the variable `org-tags-match-list-sublevels'."
  2630. :group 'org-tags
  2631. :type '(choice
  2632. (const :tag "Not" nil)
  2633. (const :tag "Always" t)
  2634. (repeat :tag "Specific tags" (string :tag "Tag"))
  2635. (regexp :tag "Tags matched by regexp")))
  2636. (defcustom org-tags-exclude-from-inheritance nil
  2637. "List of tags that should never be inherited.
  2638. This is a way to exclude a few tags from inheritance. For way to do
  2639. the opposite, to actively allow inheritance for selected tags,
  2640. see the variable `org-use-tag-inheritance'."
  2641. :group 'org-tags
  2642. :type '(repeat (string :tag "Tag")))
  2643. (defun org-tag-inherit-p (tag)
  2644. "Check if TAG is one that should be inherited."
  2645. (cond
  2646. ((member tag org-tags-exclude-from-inheritance) nil)
  2647. ((eq org-use-tag-inheritance t) t)
  2648. ((not org-use-tag-inheritance) nil)
  2649. ((stringp org-use-tag-inheritance)
  2650. (string-match org-use-tag-inheritance tag))
  2651. ((listp org-use-tag-inheritance)
  2652. (member tag org-use-tag-inheritance))
  2653. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2654. (defcustom org-tags-match-list-sublevels t
  2655. "Non-nil means list also sublevels of headlines matching a search.
  2656. This variable applies to tags/property searches, and also to stuck
  2657. projects because this search is based on a tags match as well.
  2658. When set to the symbol `indented', sublevels are indented with
  2659. leading dots.
  2660. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2661. the sublevels of a headline matching a tag search often also match
  2662. the same search. Listing all of them can create very long lists.
  2663. Setting this variable to nil causes subtrees of a match to be skipped.
  2664. This variable is semi-obsolete and probably should always be true. It
  2665. is better to limit inheritance to certain tags using the variables
  2666. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2667. :group 'org-tags
  2668. :type '(choice
  2669. (const :tag "No, don't list them" nil)
  2670. (const :tag "Yes, do list them" t)
  2671. (const :tag "List them, indented with leading dots" indented)))
  2672. (defcustom org-tags-sort-function nil
  2673. "When set, tags are sorted using this function as a comparator."
  2674. :group 'org-tags
  2675. :type '(choice
  2676. (const :tag "No sorting" nil)
  2677. (const :tag "Alphabetical" string<)
  2678. (const :tag "Reverse alphabetical" string>)
  2679. (function :tag "Custom function" nil)))
  2680. (defvar org-tags-history nil
  2681. "History of minibuffer reads for tags.")
  2682. (defvar org-last-tags-completion-table nil
  2683. "The last used completion table for tags.")
  2684. (defvar org-after-tags-change-hook nil
  2685. "Hook that is run after the tags in a line have changed.")
  2686. (defgroup org-properties nil
  2687. "Options concerning properties in Org-mode."
  2688. :tag "Org Properties"
  2689. :group 'org)
  2690. (defcustom org-property-format "%-10s %s"
  2691. "How property key/value pairs should be formatted by `indent-line'.
  2692. When `indent-line' hits a property definition, it will format the line
  2693. according to this format, mainly to make sure that the values are
  2694. lined-up with respect to each other."
  2695. :group 'org-properties
  2696. :type 'string)
  2697. (defcustom org-properties-postprocess-alist nil
  2698. "Alist of properties and functions to adjust inserted values.
  2699. Elements of this alist must be of the form
  2700. ([string] [function])
  2701. where [string] must be a property name and [function] must be a
  2702. lambda expression: this lambda expression must take one argument,
  2703. the value to adjust, and return the new value as a string.
  2704. For example, this element will allow the property \"Remaining\"
  2705. to be updated wrt the relation between the \"Effort\" property
  2706. and the clock summary:
  2707. ((\"Remaining\" (lambda(value)
  2708. (let ((clocksum (org-clock-sum-current-item))
  2709. (effort (org-duration-string-to-minutes
  2710. (org-entry-get (point) \"Effort\"))))
  2711. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2712. :group 'org-properties
  2713. :version "24.1"
  2714. :type '(alist :key-type (string :tag "Property")
  2715. :value-type (function :tag "Function")))
  2716. (defcustom org-use-property-inheritance nil
  2717. "Non-nil means properties apply also for sublevels.
  2718. This setting is chiefly used during property searches. Turning it on can
  2719. cause significant overhead when doing a search, which is why it is not
  2720. on by default.
  2721. When nil, only the properties directly given in the current entry count.
  2722. When t, every property is inherited. The value may also be a list of
  2723. properties that should have inheritance, or a regular expression matching
  2724. properties that should be inherited.
  2725. However, note that some special properties use inheritance under special
  2726. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2727. and the properties ending in \"_ALL\" when they are used as descriptor
  2728. for valid values of a property.
  2729. Note for programmers:
  2730. When querying an entry with `org-entry-get', you can control if inheritance
  2731. should be used. By default, `org-entry-get' looks only at the local
  2732. properties. You can request inheritance by setting the inherit argument
  2733. to t (to force inheritance) or to `selective' (to respect the setting
  2734. in this variable)."
  2735. :group 'org-properties
  2736. :type '(choice
  2737. (const :tag "Not" nil)
  2738. (const :tag "Always" t)
  2739. (repeat :tag "Specific properties" (string :tag "Property"))
  2740. (regexp :tag "Properties matched by regexp")))
  2741. (defun org-property-inherit-p (property)
  2742. "Check if PROPERTY is one that should be inherited."
  2743. (cond
  2744. ((eq org-use-property-inheritance t) t)
  2745. ((not org-use-property-inheritance) nil)
  2746. ((stringp org-use-property-inheritance)
  2747. (string-match org-use-property-inheritance property))
  2748. ((listp org-use-property-inheritance)
  2749. (member property org-use-property-inheritance))
  2750. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2751. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2752. "The default column format, if no other format has been defined.
  2753. This variable can be set on the per-file basis by inserting a line
  2754. #+COLUMNS: %25ITEM ....."
  2755. :group 'org-properties
  2756. :type 'string)
  2757. (defcustom org-columns-ellipses ".."
  2758. "The ellipses to be used when a field in column view is truncated.
  2759. When this is the empty string, as many characters as possible are shown,
  2760. but then there will be no visual indication that the field has been truncated.
  2761. When this is a string of length N, the last N characters of a truncated
  2762. field are replaced by this string. If the column is narrower than the
  2763. ellipses string, only part of the ellipses string will be shown."
  2764. :group 'org-properties
  2765. :type 'string)
  2766. (defcustom org-columns-modify-value-for-display-function nil
  2767. "Function that modifies values for display in column view.
  2768. For example, it can be used to cut out a certain part from a time stamp.
  2769. The function must take 2 arguments:
  2770. column-title The title of the column (*not* the property name)
  2771. value The value that should be modified.
  2772. The function should return the value that should be displayed,
  2773. or nil if the normal value should be used."
  2774. :group 'org-properties
  2775. :type 'function)
  2776. (defcustom org-effort-property "Effort"
  2777. "The property that is being used to keep track of effort estimates.
  2778. Effort estimates given in this property need to have the format H:MM."
  2779. :group 'org-properties
  2780. :group 'org-progress
  2781. :type '(string :tag "Property"))
  2782. (defconst org-global-properties-fixed
  2783. '(("VISIBILITY_ALL" . "folded children content all")
  2784. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2785. "List of property/value pairs that can be inherited by any entry.
  2786. These are fixed values, for the preset properties. The user variable
  2787. that can be used to add to this list is `org-global-properties'.
  2788. The entries in this list are cons cells where the car is a property
  2789. name and cdr is a string with the value. If the value represents
  2790. multiple items like an \"_ALL\" property, separate the items by
  2791. spaces.")
  2792. (defcustom org-global-properties nil
  2793. "List of property/value pairs that can be inherited by any entry.
  2794. This list will be combined with the constant `org-global-properties-fixed'.
  2795. The entries in this list are cons cells where the car is a property
  2796. name and cdr is a string with the value.
  2797. You can set buffer-local values for the same purpose in the variable
  2798. `org-file-properties' this by adding lines like
  2799. #+PROPERTY: NAME VALUE"
  2800. :group 'org-properties
  2801. :type '(repeat
  2802. (cons (string :tag "Property")
  2803. (string :tag "Value"))))
  2804. (defvar org-file-properties nil
  2805. "List of property/value pairs that can be inherited by any entry.
  2806. Valid for the current buffer.
  2807. This variable is populated from #+PROPERTY lines.")
  2808. (make-variable-buffer-local 'org-file-properties)
  2809. (defgroup org-agenda nil
  2810. "Options concerning agenda views in Org-mode."
  2811. :tag "Org Agenda"
  2812. :group 'org)
  2813. (defvar org-category nil
  2814. "Variable used by org files to set a category for agenda display.
  2815. Such files should use a file variable to set it, for example
  2816. # -*- mode: org; org-category: \"ELisp\"
  2817. or contain a special line
  2818. #+CATEGORY: ELisp
  2819. If the file does not specify a category, then file's base name
  2820. is used instead.")
  2821. (make-variable-buffer-local 'org-category)
  2822. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2823. (defcustom org-agenda-files nil
  2824. "The files to be used for agenda display.
  2825. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2826. \\[org-remove-file]. You can also use customize to edit the list.
  2827. If an entry is a directory, all files in that directory that are matched by
  2828. `org-agenda-file-regexp' will be part of the file list.
  2829. If the value of the variable is not a list but a single file name, then
  2830. the list of agenda files is actually stored and maintained in that file, one
  2831. agenda file per line. In this file paths can be given relative to
  2832. `org-directory'. Tilde expansion and environment variable substitution
  2833. are also made."
  2834. :group 'org-agenda
  2835. :type '(choice
  2836. (repeat :tag "List of files and directories" file)
  2837. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2838. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2839. "Regular expression to match files for `org-agenda-files'.
  2840. If any element in the list in that variable contains a directory instead
  2841. of a normal file, all files in that directory that are matched by this
  2842. regular expression will be included."
  2843. :group 'org-agenda
  2844. :type 'regexp)
  2845. (defcustom org-agenda-text-search-extra-files nil
  2846. "List of extra files to be searched by text search commands.
  2847. These files will be search in addition to the agenda files by the
  2848. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2849. Note that these files will only be searched for text search commands,
  2850. not for the other agenda views like todo lists, tag searches or the weekly
  2851. agenda. This variable is intended to list notes and possibly archive files
  2852. that should also be searched by these two commands.
  2853. In fact, if the first element in the list is the symbol `agenda-archives',
  2854. than all archive files of all agenda files will be added to the search
  2855. scope."
  2856. :group 'org-agenda
  2857. :type '(set :greedy t
  2858. (const :tag "Agenda Archives" agenda-archives)
  2859. (repeat :inline t (file))))
  2860. (if (fboundp 'defvaralias)
  2861. (defvaralias 'org-agenda-multi-occur-extra-files
  2862. 'org-agenda-text-search-extra-files))
  2863. (defcustom org-agenda-skip-unavailable-files nil
  2864. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2865. A nil value means to remove them, after a query, from the list."
  2866. :group 'org-agenda
  2867. :type 'boolean)
  2868. (defcustom org-calendar-to-agenda-key [?c]
  2869. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2870. The command `org-calendar-goto-agenda' will be bound to this key. The
  2871. default is the character `c' because then `c' can be used to switch back and
  2872. forth between agenda and calendar."
  2873. :group 'org-agenda
  2874. :type 'sexp)
  2875. (defcustom org-calendar-agenda-action-key [?k]
  2876. "The key to be installed in `calendar-mode-map' for agenda-action.
  2877. The command `org-agenda-action' will be bound to this key. The
  2878. default is the character `k' because we use the same key in the agenda."
  2879. :group 'org-agenda
  2880. :type 'sexp)
  2881. (defcustom org-calendar-insert-diary-entry-key [?i]
  2882. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2883. This option is irrelevant until `org-agenda-diary-file' has been configured
  2884. to point to an Org-mode file. When that is the case, the command
  2885. `org-agenda-diary-entry' will be bound to the key given here, by default
  2886. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2887. if you want to continue doing this, you need to change this to a different
  2888. key."
  2889. :group 'org-agenda
  2890. :type 'sexp)
  2891. (defcustom org-agenda-diary-file 'diary-file
  2892. "File to which to add new entries with the `i' key in agenda and calendar.
  2893. When this is the symbol `diary-file', the functionality in the Emacs
  2894. calendar will be used to add entries to the `diary-file'. But when this
  2895. points to a file, `org-agenda-diary-entry' will be used instead."
  2896. :group 'org-agenda
  2897. :type '(choice
  2898. (const :tag "The standard Emacs diary file" diary-file)
  2899. (file :tag "Special Org file diary entries")))
  2900. (eval-after-load "calendar"
  2901. '(progn
  2902. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2903. 'org-calendar-goto-agenda)
  2904. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2905. 'org-agenda-action)
  2906. (add-hook 'calendar-mode-hook
  2907. (lambda ()
  2908. (unless (eq org-agenda-diary-file 'diary-file)
  2909. (define-key calendar-mode-map
  2910. org-calendar-insert-diary-entry-key
  2911. 'org-agenda-diary-entry))))))
  2912. (defgroup org-latex nil
  2913. "Options for embedding LaTeX code into Org-mode."
  2914. :tag "Org LaTeX"
  2915. :group 'org)
  2916. (defcustom org-format-latex-options
  2917. '(:foreground default :background default :scale 1.0
  2918. :html-foreground "Black" :html-background "Transparent"
  2919. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2920. "Options for creating images from LaTeX fragments.
  2921. This is a property list with the following properties:
  2922. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2923. `default' means use the foreground of the default face.
  2924. `auto' means use the foreground from the text face.
  2925. :background the background color, or \"Transparent\".
  2926. `default' means use the background of the default face.
  2927. `auto' means use the background from the text face.
  2928. :scale a scaling factor for the size of the images, to get more pixels
  2929. :html-foreground, :html-background, :html-scale
  2930. the same numbers for HTML export.
  2931. :matchers a list indicating which matchers should be used to
  2932. find LaTeX fragments. Valid members of this list are:
  2933. \"begin\" find environments
  2934. \"$1\" find single characters surrounded by $.$
  2935. \"$\" find math expressions surrounded by $...$
  2936. \"$$\" find math expressions surrounded by $$....$$
  2937. \"\\(\" find math expressions surrounded by \\(...\\)
  2938. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2939. :group 'org-latex
  2940. :type 'plist)
  2941. (defcustom org-format-latex-signal-error t
  2942. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2943. When nil, just push out a message."
  2944. :group 'org-latex
  2945. :version "24.1"
  2946. :type 'boolean)
  2947. (defcustom org-latex-to-mathml-jar-file nil
  2948. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2949. Use this to specify additional executable file say a jar file.
  2950. When using MathToWeb as the converter, specify the full-path to
  2951. your mathtoweb.jar file."
  2952. :group 'org-latex
  2953. :version "24.1"
  2954. :type '(choice
  2955. (const :tag "None" nil)
  2956. (file :tag "JAR file" :must-match t)))
  2957. (defcustom org-latex-to-mathml-convert-command nil
  2958. "Command to convert LaTeX fragments to MathML.
  2959. Replace format-specifiers in the command as noted below and use
  2960. `shell-command' to convert LaTeX to MathML.
  2961. %j: Executable file in fully expanded form as specified by
  2962. `org-latex-to-mathml-jar-file'.
  2963. %I: Input LaTeX file in fully expanded form
  2964. %o: Output MathML file
  2965. This command is used by `org-create-math-formula'.
  2966. When using MathToWeb as the converter, set this to
  2967. \"java -jar %j -unicode -force -df %o %I\"."
  2968. :group 'org-latex
  2969. :version "24.1"
  2970. :type '(choice
  2971. (const :tag "None" nil)
  2972. (string :tag "\nShell command")))
  2973. (defcustom org-latex-create-formula-image-program 'dvipng
  2974. "Program to convert LaTeX fragments with.
  2975. dvipng Process the LaTeX fragments to dvi file, then convert
  2976. dvi files to png files using dvipng.
  2977. This will also include processing of non-math environments.
  2978. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2979. to convert pdf files to png files"
  2980. :group 'org-latex
  2981. :version "24.1"
  2982. :type '(choice
  2983. (const :tag "dvipng" dvipng)
  2984. (const :tag "imagemagick" imagemagick)))
  2985. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  2986. "Path to store latex preview images. A relative path here creates many
  2987. directories relative to the processed org files paths. An absolute path
  2988. puts all preview images at the same place."
  2989. :group 'org-latex
  2990. ;; :version "24.3"
  2991. :type 'string)
  2992. (defun org-format-latex-mathml-available-p ()
  2993. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2994. (save-match-data
  2995. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2996. org-latex-to-mathml-convert-command)
  2997. (let ((executable (car (split-string
  2998. org-latex-to-mathml-convert-command))))
  2999. (when (executable-find executable)
  3000. (if (string-match
  3001. "%j" org-latex-to-mathml-convert-command)
  3002. (file-readable-p org-latex-to-mathml-jar-file)
  3003. t))))))
  3004. (defcustom org-format-latex-header "\\documentclass{article}
  3005. \\usepackage[usenames]{color}
  3006. \\usepackage{amsmath}
  3007. \\usepackage[mathscr]{eucal}
  3008. \\pagestyle{empty} % do not remove
  3009. \[PACKAGES]
  3010. \[DEFAULT-PACKAGES]
  3011. % The settings below are copied from fullpage.sty
  3012. \\setlength{\\textwidth}{\\paperwidth}
  3013. \\addtolength{\\textwidth}{-3cm}
  3014. \\setlength{\\oddsidemargin}{1.5cm}
  3015. \\addtolength{\\oddsidemargin}{-2.54cm}
  3016. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3017. \\setlength{\\textheight}{\\paperheight}
  3018. \\addtolength{\\textheight}{-\\headheight}
  3019. \\addtolength{\\textheight}{-\\headsep}
  3020. \\addtolength{\\textheight}{-\\footskip}
  3021. \\addtolength{\\textheight}{-3cm}
  3022. \\setlength{\\topmargin}{1.5cm}
  3023. \\addtolength{\\topmargin}{-2.54cm}"
  3024. "The document header used for processing LaTeX fragments.
  3025. It is imperative that this header make sure that no page number
  3026. appears on the page. The package defined in the variables
  3027. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3028. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3029. will be appended."
  3030. :group 'org-latex
  3031. :type 'string)
  3032. (defvar org-format-latex-header-extra nil)
  3033. (defun org-set-packages-alist (var val)
  3034. "Set the packages alist and make sure it has 3 elements per entry."
  3035. (set var (mapcar (lambda (x)
  3036. (if (and (consp x) (= (length x) 2))
  3037. (list (car x) (nth 1 x) t)
  3038. x))
  3039. val)))
  3040. (defun org-get-packages-alist (var)
  3041. "Get the packages alist and make sure it has 3 elements per entry."
  3042. (mapcar (lambda (x)
  3043. (if (and (consp x) (= (length x) 2))
  3044. (list (car x) (nth 1 x) t)
  3045. x))
  3046. (default-value var)))
  3047. ;; The following variables are defined here because is it also used
  3048. ;; when formatting latex fragments. Originally it was part of the
  3049. ;; LaTeX exporter, which is why the name includes "export".
  3050. (defcustom org-export-latex-default-packages-alist
  3051. '(("AUTO" "inputenc" t)
  3052. ("T1" "fontenc" t)
  3053. ("" "fixltx2e" nil)
  3054. ("" "graphicx" t)
  3055. ("" "longtable" nil)
  3056. ("" "float" nil)
  3057. ("" "wrapfig" nil)
  3058. ("" "soul" t)
  3059. ("" "textcomp" t)
  3060. ("" "marvosym" t)
  3061. ("" "wasysym" t)
  3062. ("" "latexsym" t)
  3063. ("" "amssymb" t)
  3064. ("" "hyperref" nil)
  3065. "\\tolerance=1000"
  3066. )
  3067. "Alist of default packages to be inserted in the header.
  3068. Change this only if one of the packages here causes an incompatibility
  3069. with another package you are using.
  3070. The packages in this list are needed by one part or another of Org-mode
  3071. to function properly.
  3072. - inputenc, fontenc: for basic font and character selection
  3073. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3074. for interpreting the entities in `org-entities'. You can skip some of these
  3075. packages if you don't use any of the symbols in it.
  3076. - graphicx: for including images
  3077. - float, wrapfig: for figure placement
  3078. - longtable: for long tables
  3079. - hyperref: for cross references
  3080. Therefore you should not modify this variable unless you know what you
  3081. are doing. The one reason to change it anyway is that you might be loading
  3082. some other package that conflicts with one of the default packages.
  3083. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3084. If SNIPPET-FLAG is t, the package also needs to be included when
  3085. compiling LaTeX snippets into images for inclusion into HTML."
  3086. :group 'org-export-latex
  3087. :set 'org-set-packages-alist
  3088. :get 'org-get-packages-alist
  3089. :version "24.1"
  3090. :type '(repeat
  3091. (choice
  3092. (list :tag "options/package pair"
  3093. (string :tag "options")
  3094. (string :tag "package")
  3095. (boolean :tag "Snippet"))
  3096. (string :tag "A line of LaTeX"))))
  3097. (defcustom org-export-latex-packages-alist nil
  3098. "Alist of packages to be inserted in every LaTeX header.
  3099. These will be inserted after `org-export-latex-default-packages-alist'.
  3100. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3101. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3102. turning LaTeX snippets into images for inclusion into HTML.
  3103. Make sure that you only list packages here which:
  3104. - you want in every file
  3105. - do not conflict with the default packages in
  3106. `org-export-latex-default-packages-alist'
  3107. - do not conflict with the setup in `org-format-latex-header'."
  3108. :group 'org-export-latex
  3109. :set 'org-set-packages-alist
  3110. :get 'org-get-packages-alist
  3111. :type '(repeat
  3112. (choice
  3113. (list :tag "options/package pair"
  3114. (string :tag "options")
  3115. (string :tag "package")
  3116. (boolean :tag "Snippet"))
  3117. (string :tag "A line of LaTeX"))))
  3118. (defgroup org-appearance nil
  3119. "Settings for Org-mode appearance."
  3120. :tag "Org Appearance"
  3121. :group 'org)
  3122. (defcustom org-level-color-stars-only nil
  3123. "Non-nil means fontify only the stars in each headline.
  3124. When nil, the entire headline is fontified.
  3125. Changing it requires restart of `font-lock-mode' to become effective
  3126. also in regions already fontified."
  3127. :group 'org-appearance
  3128. :type 'boolean)
  3129. (defcustom org-hide-leading-stars nil
  3130. "Non-nil means hide the first N-1 stars in a headline.
  3131. This works by using the face `org-hide' for these stars. This
  3132. face is white for a light background, and black for a dark
  3133. background. You may have to customize the face `org-hide' to
  3134. make this work.
  3135. Changing it requires restart of `font-lock-mode' to become effective
  3136. also in regions already fontified.
  3137. You may also set this on a per-file basis by adding one of the following
  3138. lines to the buffer:
  3139. #+STARTUP: hidestars
  3140. #+STARTUP: showstars"
  3141. :group 'org-appearance
  3142. :type 'boolean)
  3143. (defcustom org-hidden-keywords nil
  3144. "List of symbols corresponding to keywords to be hidden the org buffer.
  3145. For example, a value '(title) for this list will make the document's title
  3146. appear in the buffer without the initial #+TITLE: keyword."
  3147. :group 'org-appearance
  3148. :version "24.1"
  3149. :type '(set (const :tag "#+AUTHOR" author)
  3150. (const :tag "#+DATE" date)
  3151. (const :tag "#+EMAIL" email)
  3152. (const :tag "#+TITLE" title)))
  3153. (defcustom org-custom-properties nil
  3154. "List of properties (as strings) with a special meaning.
  3155. The default use of these custom properties is to let the user
  3156. hide them with `org-toggle-custom-properties-visibility'."
  3157. :group 'org-properties
  3158. :group 'org-appearance
  3159. ;; :version "24.3"
  3160. :type '(repeat (string :tag "Property Name")))
  3161. (defcustom org-fontify-done-headline nil
  3162. "Non-nil means change the face of a headline if it is marked DONE.
  3163. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3164. When this is non-nil, the headline after the keyword is set to the
  3165. `org-headline-done' as an additional indication."
  3166. :group 'org-appearance
  3167. :type 'boolean)
  3168. (defcustom org-fontify-emphasized-text t
  3169. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3170. Changing this variable requires a restart of Emacs to take effect."
  3171. :group 'org-appearance
  3172. :type 'boolean)
  3173. (defcustom org-fontify-whole-heading-line nil
  3174. "Non-nil means fontify the whole line for headings.
  3175. This is useful when setting a background color for the
  3176. org-level-* faces."
  3177. :group 'org-appearance
  3178. :type 'boolean)
  3179. (defcustom org-highlight-latex-fragments-and-specials nil
  3180. "Non-nil means fontify what is treated specially by the exporters."
  3181. :group 'org-appearance
  3182. :type 'boolean)
  3183. (defcustom org-hide-emphasis-markers nil
  3184. "Non-nil mean font-lock should hide the emphasis marker characters."
  3185. :group 'org-appearance
  3186. :type 'boolean)
  3187. (defcustom org-pretty-entities nil
  3188. "Non-nil means show entities as UTF8 characters.
  3189. When nil, the \\name form remains in the buffer."
  3190. :group 'org-appearance
  3191. :version "24.1"
  3192. :type 'boolean)
  3193. (defcustom org-pretty-entities-include-sub-superscripts t
  3194. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3195. :group 'org-appearance
  3196. :version "24.1"
  3197. :type 'boolean)
  3198. (defvar org-emph-re nil
  3199. "Regular expression for matching emphasis.
  3200. After a match, the match groups contain these elements:
  3201. 0 The match of the full regular expression, including the characters
  3202. before and after the proper match
  3203. 1 The character before the proper match, or empty at beginning of line
  3204. 2 The proper match, including the leading and trailing markers
  3205. 3 The leading marker like * or /, indicating the type of highlighting
  3206. 4 The text between the emphasis markers, not including the markers
  3207. 5 The character after the match, empty at the end of a line")
  3208. (defvar org-verbatim-re nil
  3209. "Regular expression for matching verbatim text.")
  3210. (defvar org-emphasis-regexp-components) ; defined just below
  3211. (defvar org-emphasis-alist) ; defined just below
  3212. (defun org-set-emph-re (var val)
  3213. "Set variable and compute the emphasis regular expression."
  3214. (set var val)
  3215. (when (and (boundp 'org-emphasis-alist)
  3216. (boundp 'org-emphasis-regexp-components)
  3217. org-emphasis-alist org-emphasis-regexp-components)
  3218. (let* ((e org-emphasis-regexp-components)
  3219. (pre (car e))
  3220. (post (nth 1 e))
  3221. (border (nth 2 e))
  3222. (body (nth 3 e))
  3223. (nl (nth 4 e))
  3224. (body1 (concat body "*?"))
  3225. (markers (mapconcat 'car org-emphasis-alist ""))
  3226. (vmarkers (mapconcat
  3227. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3228. org-emphasis-alist "")))
  3229. ;; make sure special characters appear at the right position in the class
  3230. (if (string-match "\\^" markers)
  3231. (setq markers (concat (replace-match "" t t markers) "^")))
  3232. (if (string-match "-" markers)
  3233. (setq markers (concat (replace-match "" t t markers) "-")))
  3234. (if (string-match "\\^" vmarkers)
  3235. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3236. (if (string-match "-" vmarkers)
  3237. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3238. (if (> nl 0)
  3239. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3240. (int-to-string nl) "\\}")))
  3241. ;; Make the regexp
  3242. (setq org-emph-re
  3243. (concat "\\([" pre "]\\|^\\)"
  3244. "\\("
  3245. "\\([" markers "]\\)"
  3246. "\\("
  3247. "[^" border "]\\|"
  3248. "[^" border "]"
  3249. body1
  3250. "[^" border "]"
  3251. "\\)"
  3252. "\\3\\)"
  3253. "\\([" post "]\\|$\\)"))
  3254. (setq org-verbatim-re
  3255. (concat "\\([" pre "]\\|^\\)"
  3256. "\\("
  3257. "\\([" vmarkers "]\\)"
  3258. "\\("
  3259. "[^" border "]\\|"
  3260. "[^" border "]"
  3261. body1
  3262. "[^" border "]"
  3263. "\\)"
  3264. "\\3\\)"
  3265. "\\([" post "]\\|$\\)")))))
  3266. (defcustom org-emphasis-regexp-components
  3267. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3268. "Components used to build the regular expression for emphasis.
  3269. This is a list with five entries. Terminology: In an emphasis string
  3270. like \" *strong word* \", we call the initial space PREMATCH, the final
  3271. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3272. and \"trong wor\" is the body. The different components in this variable
  3273. specify what is allowed/forbidden in each part:
  3274. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3275. post Chars allowed as postmatch. End of line will be allowed too.
  3276. border The chars *forbidden* as border characters.
  3277. body-regexp A regexp like \".\" to match a body character. Don't use
  3278. non-shy groups here, and don't allow newline here.
  3279. newline The maximum number of newlines allowed in an emphasis exp.
  3280. Use customize to modify this, or restart Emacs after changing it."
  3281. :group 'org-appearance
  3282. :set 'org-set-emph-re
  3283. :type '(list
  3284. (sexp :tag "Allowed chars in pre ")
  3285. (sexp :tag "Allowed chars in post ")
  3286. (sexp :tag "Forbidden chars in border ")
  3287. (sexp :tag "Regexp for body ")
  3288. (integer :tag "number of newlines allowed")
  3289. (option (boolean :tag "Please ignore this button"))))
  3290. (defcustom org-emphasis-alist
  3291. `(("*" bold "<b>" "</b>")
  3292. ("/" italic "<i>" "</i>")
  3293. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3294. ("=" org-code "<code>" "</code>" verbatim)
  3295. ("~" org-verbatim "<code>" "</code>" verbatim)
  3296. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3297. "<del>" "</del>")
  3298. )
  3299. "Special syntax for emphasized text.
  3300. Text starting and ending with a special character will be emphasized, for
  3301. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3302. characters, the face to be used by font-lock for highlighting in Org-mode
  3303. Emacs buffers, and the HTML tags to be used for this.
  3304. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3305. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3306. Use customize to modify this, or restart Emacs after changing it."
  3307. :group 'org-appearance
  3308. :set 'org-set-emph-re
  3309. :type '(repeat
  3310. (list
  3311. (string :tag "Marker character")
  3312. (choice
  3313. (face :tag "Font-lock-face")
  3314. (plist :tag "Face property list"))
  3315. (string :tag "HTML start tag")
  3316. (string :tag "HTML end tag")
  3317. (option (const verbatim)))))
  3318. (defvar org-protecting-blocks
  3319. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3320. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3321. This is needed for font-lock setup.")
  3322. ;;; Miscellaneous options
  3323. (defgroup org-completion nil
  3324. "Completion in Org-mode."
  3325. :tag "Org Completion"
  3326. :group 'org)
  3327. (defcustom org-completion-use-ido nil
  3328. "Non-nil means use ido completion wherever possible.
  3329. Note that `ido-mode' must be active for this variable to be relevant.
  3330. If you decide to turn this variable on, you might well want to turn off
  3331. `org-outline-path-complete-in-steps'.
  3332. See also `org-completion-use-iswitchb'."
  3333. :group 'org-completion
  3334. :type 'boolean)
  3335. (defcustom org-completion-use-iswitchb nil
  3336. "Non-nil means use iswitchb completion wherever possible.
  3337. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3338. If you decide to turn this variable on, you might well want to turn off
  3339. `org-outline-path-complete-in-steps'.
  3340. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3341. :group 'org-completion
  3342. :type 'boolean)
  3343. (defcustom org-completion-fallback-command 'hippie-expand
  3344. "The expansion command called by \\[pcomplete] in normal context.
  3345. Normal means, no org-mode-specific context."
  3346. :group 'org-completion
  3347. :type 'function)
  3348. ;;; Functions and variables from their packages
  3349. ;; Declared here to avoid compiler warnings
  3350. ;; XEmacs only
  3351. (defvar outline-mode-menu-heading)
  3352. (defvar outline-mode-menu-show)
  3353. (defvar outline-mode-menu-hide)
  3354. (defvar zmacs-regions) ; XEmacs regions
  3355. ;; Emacs only
  3356. (defvar mark-active)
  3357. ;; Various packages
  3358. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3359. (declare-function calendar-forward-day "cal-move" (arg))
  3360. (declare-function calendar-goto-date "cal-move" (date))
  3361. (declare-function calendar-goto-today "cal-move" ())
  3362. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3363. (defvar calc-embedded-close-formula)
  3364. (defvar calc-embedded-open-formula)
  3365. (declare-function cdlatex-tab "ext:cdlatex" ())
  3366. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3367. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3368. (defvar font-lock-unfontify-region-function)
  3369. (declare-function iswitchb-read-buffer "iswitchb"
  3370. (prompt &optional default require-match start matches-set))
  3371. (defvar iswitchb-temp-buflist)
  3372. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3373. (defvar org-agenda-tags-todo-honor-ignore-options)
  3374. (declare-function org-agenda-skip "org-agenda" ())
  3375. (declare-function
  3376. org-agenda-format-item "org-agenda"
  3377. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3378. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3379. (declare-function org-agenda-change-all-lines "org-agenda"
  3380. (newhead hdmarker &optional fixface just-this))
  3381. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3382. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3383. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3384. (beg end))
  3385. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3386. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3387. "org-agenda" (&optional end))
  3388. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3389. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3390. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3391. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3392. (declare-function org-indent-mode "org-indent" (&optional arg))
  3393. (declare-function parse-time-string "parse-time" (string))
  3394. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3395. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3396. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3397. (defvar remember-data-file)
  3398. (defvar texmathp-why)
  3399. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3400. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3401. (defvar w3m-current-url)
  3402. (defvar w3m-current-title)
  3403. (defvar org-latex-regexps)
  3404. ;;; Autoload and prepare some org modules
  3405. ;; Some table stuff that needs to be defined here, because it is used
  3406. ;; by the functions setting up org-mode or checking for table context.
  3407. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3408. "Detect an org-type or table-type table.")
  3409. (defconst org-table-line-regexp "^[ \t]*|"
  3410. "Detect an org-type table line.")
  3411. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3412. "Detect an org-type table line.")
  3413. (defconst org-table-hline-regexp "^[ \t]*|-"
  3414. "Detect an org-type table hline.")
  3415. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3416. "Detect a table-type table hline.")
  3417. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3418. "Detect the first line outside a table when searching from within it.
  3419. This works for both table types.")
  3420. ;; Autoload the functions in org-table.el that are needed by functions here.
  3421. (eval-and-compile
  3422. (org-autoload "org-table"
  3423. '(org-table-align org-table-begin org-table-blank-field
  3424. org-table-convert org-table-convert-region org-table-copy-down
  3425. org-table-copy-region org-table-create
  3426. org-table-create-or-convert-from-region
  3427. org-table-create-with-table.el org-table-current-dline
  3428. org-table-cut-region org-table-delete-column org-table-edit-field
  3429. org-table-edit-formulas org-table-end org-table-eval-formula
  3430. org-table-export org-table-field-info
  3431. org-table-get-stored-formulas org-table-goto-column
  3432. org-table-hline-and-move org-table-import org-table-insert-column
  3433. org-table-insert-hline org-table-insert-row org-table-iterate
  3434. org-table-justify-field-maybe org-table-kill-row
  3435. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3436. org-table-move-column org-table-move-column-left
  3437. org-table-move-column-right org-table-move-row
  3438. org-table-move-row-down org-table-move-row-up
  3439. org-table-next-field org-table-next-row org-table-paste-rectangle
  3440. org-table-previous-field org-table-recalculate
  3441. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3442. org-table-toggle-coordinate-overlays
  3443. org-table-toggle-formula-debugger org-table-wrap-region
  3444. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3445. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3446. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3447. (defun org-at-table-p (&optional table-type)
  3448. "Return t if the cursor is inside an org-type table.
  3449. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3450. (if org-enable-table-editor
  3451. (save-excursion
  3452. (beginning-of-line 1)
  3453. (looking-at (if table-type org-table-any-line-regexp
  3454. org-table-line-regexp)))
  3455. nil))
  3456. (defsubst org-table-p () (org-at-table-p))
  3457. (defun org-at-table.el-p ()
  3458. "Return t if and only if we are at a table.el table."
  3459. (and (org-at-table-p 'any)
  3460. (save-excursion
  3461. (goto-char (org-table-begin 'any))
  3462. (looking-at org-table1-hline-regexp))))
  3463. (defun org-table-recognize-table.el ()
  3464. "If there is a table.el table nearby, recognize it and move into it."
  3465. (if org-table-tab-recognizes-table.el
  3466. (if (org-at-table.el-p)
  3467. (progn
  3468. (beginning-of-line 1)
  3469. (if (looking-at org-table-dataline-regexp)
  3470. nil
  3471. (if (looking-at org-table1-hline-regexp)
  3472. (progn
  3473. (beginning-of-line 2)
  3474. (if (looking-at org-table-any-border-regexp)
  3475. (beginning-of-line -1)))))
  3476. (if (re-search-forward "|" (org-table-end t) t)
  3477. (progn
  3478. (require 'table)
  3479. (if (table--at-cell-p (point))
  3480. t
  3481. (message "recognizing table.el table...")
  3482. (table-recognize-table)
  3483. (message "recognizing table.el table...done")))
  3484. (error "This should not happen"))
  3485. t)
  3486. nil)
  3487. nil))
  3488. (defun org-at-table-hline-p ()
  3489. "Return t if the cursor is inside a hline in a table."
  3490. (if org-enable-table-editor
  3491. (save-excursion
  3492. (beginning-of-line 1)
  3493. (looking-at org-table-hline-regexp))
  3494. nil))
  3495. (defvar org-table-clean-did-remove-column nil)
  3496. (defun org-table-map-tables (function &optional quietly)
  3497. "Apply FUNCTION to the start of all tables in the buffer."
  3498. (save-excursion
  3499. (save-restriction
  3500. (widen)
  3501. (goto-char (point-min))
  3502. (while (re-search-forward org-table-any-line-regexp nil t)
  3503. (unless quietly
  3504. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3505. (beginning-of-line 1)
  3506. (when (and (looking-at org-table-line-regexp)
  3507. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3508. (not (org-in-block-p '("src" "example"))))
  3509. (save-excursion (funcall function))
  3510. (or (looking-at org-table-line-regexp)
  3511. (forward-char 1)))
  3512. (re-search-forward org-table-any-border-regexp nil 1))))
  3513. (unless quietly (message "Mapping tables: done")))
  3514. ;; Declare and autoload functions from org-exp.el & Co
  3515. (declare-function org-default-export-plist "org-exp")
  3516. (declare-function org-infile-export-plist "org-exp")
  3517. (declare-function org-get-current-options "org-exp")
  3518. (eval-and-compile
  3519. (org-autoload "org-exp"
  3520. '(org-export org-export-visible
  3521. org-insert-export-options-template
  3522. org-table-clean-before-export))
  3523. (org-autoload "org-ascii"
  3524. '(org-export-as-ascii org-export-ascii-preprocess
  3525. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3526. org-export-region-as-ascii))
  3527. (org-autoload "org-latex"
  3528. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3529. org-replace-region-by-latex org-export-region-as-latex
  3530. org-export-as-latex org-export-as-pdf
  3531. org-export-as-pdf-and-open))
  3532. (org-autoload "org-html"
  3533. '(org-export-as-html-and-open
  3534. org-export-as-html-batch org-export-as-html-to-buffer
  3535. org-replace-region-by-html org-export-region-as-html
  3536. org-export-as-html))
  3537. (org-autoload "org-docbook"
  3538. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3539. org-replace-region-by-docbook org-export-region-as-docbook
  3540. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3541. org-export-as-docbook))
  3542. (org-autoload "org-icalendar"
  3543. '(org-export-icalendar-this-file
  3544. org-export-icalendar-all-agenda-files
  3545. org-export-icalendar-combine-agenda-files))
  3546. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3547. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3548. ;; Declare and autoload functions from org-agenda.el
  3549. (eval-and-compile
  3550. (org-autoload "org-agenda"
  3551. '(org-agenda org-agenda-list org-search-view
  3552. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3553. org-diary org-agenda-to-appt
  3554. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3555. ;; Autoload org-remember
  3556. (eval-and-compile
  3557. (org-autoload "org-remember"
  3558. '(org-remember-insinuate org-remember-annotation
  3559. org-remember-apply-template org-remember org-remember-handler)))
  3560. (eval-and-compile
  3561. (org-autoload "org-capture"
  3562. '(org-capture org-capture-insert-template-here
  3563. org-capture-import-remember-templates)))
  3564. ;; Autoload org-clock.el
  3565. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3566. (beg end))
  3567. (declare-function org-clock-update-mode-line "org-clock" ())
  3568. (declare-function org-resolve-clocks "org-clock"
  3569. (&optional also-non-dangling-p prompt last-valid))
  3570. (defvar org-clock-start-time)
  3571. (defvar org-clock-marker (make-marker)
  3572. "Marker recording the last clock-in.")
  3573. (defvar org-clock-hd-marker (make-marker)
  3574. "Marker recording the last clock-in, but the headline position.")
  3575. (defvar org-clock-heading ""
  3576. "The heading of the current clock entry.")
  3577. (defun org-clock-is-active ()
  3578. "Return non-nil if clock is currently running.
  3579. The return value is actually the clock marker."
  3580. (marker-buffer org-clock-marker))
  3581. (eval-and-compile
  3582. (org-autoload
  3583. "org-clock"
  3584. '(org-clock-in org-clock-out org-clock-cancel
  3585. org-clock-goto org-clock-sum org-clock-display
  3586. org-clock-remove-overlays org-clock-report
  3587. org-clocktable-shift org-dblock-write:clocktable
  3588. org-get-clocktable org-resolve-clocks)))
  3589. (defun org-clock-update-time-maybe ()
  3590. "If this is a CLOCK line, update it and return t.
  3591. Otherwise, return nil."
  3592. (interactive)
  3593. (save-excursion
  3594. (beginning-of-line 1)
  3595. (skip-chars-forward " \t")
  3596. (when (looking-at org-clock-string)
  3597. (let ((re (concat "[ \t]*" org-clock-string
  3598. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3599. "\\([ \t]*=>.*\\)?\\)?"))
  3600. ts te h m s neg)
  3601. (cond
  3602. ((not (looking-at re))
  3603. nil)
  3604. ((not (match-end 2))
  3605. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3606. (> org-clock-marker (point))
  3607. (<= org-clock-marker (point-at-eol)))
  3608. ;; The clock is running here
  3609. (setq org-clock-start-time
  3610. (apply 'encode-time
  3611. (org-parse-time-string (match-string 1))))
  3612. (org-clock-update-mode-line)))
  3613. (t
  3614. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3615. (end-of-line 1)
  3616. (setq ts (match-string 1)
  3617. te (match-string 3))
  3618. (setq s (- (org-float-time
  3619. (apply 'encode-time (org-parse-time-string te)))
  3620. (org-float-time
  3621. (apply 'encode-time (org-parse-time-string ts))))
  3622. neg (< s 0)
  3623. s (abs s)
  3624. h (floor (/ s 3600))
  3625. s (- s (* 3600 h))
  3626. m (floor (/ s 60))
  3627. s (- s (* 60 s)))
  3628. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3629. t))))))
  3630. (defun org-check-running-clock ()
  3631. "Check if the current buffer contains the running clock.
  3632. If yes, offer to stop it and to save the buffer with the changes."
  3633. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3634. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3635. (buffer-name))))
  3636. (org-clock-out)
  3637. (when (y-or-n-p "Save changed buffer?")
  3638. (save-buffer))))
  3639. (defun org-clocktable-try-shift (dir n)
  3640. "Check if this line starts a clock table, if yes, shift the time block."
  3641. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3642. (org-clocktable-shift dir n)))
  3643. ;; Autoload org-timer.el
  3644. (eval-and-compile
  3645. (org-autoload
  3646. "org-timer"
  3647. '(org-timer-start org-timer org-timer-item
  3648. org-timer-change-times-in-region
  3649. org-timer-set-timer
  3650. org-timer-reset-timers
  3651. org-timer-show-remaining-time)))
  3652. ;; Autoload org-feed.el
  3653. (eval-and-compile
  3654. (org-autoload
  3655. "org-feed"
  3656. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3657. ;; Autoload org-indent.el
  3658. ;; Define the variable already here, to make sure we have it.
  3659. (defvar org-indent-mode nil
  3660. "Non-nil if Org-Indent mode is enabled.
  3661. Use the command `org-indent-mode' to change this variable.")
  3662. (eval-and-compile
  3663. (org-autoload
  3664. "org-indent"
  3665. '(org-indent-mode)))
  3666. ;; Autoload org-mobile.el
  3667. (eval-and-compile
  3668. (org-autoload
  3669. "org-mobile"
  3670. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3671. ;; Autoload archiving code
  3672. ;; The stuff that is needed for cycling and tags has to be defined here.
  3673. (defgroup org-archive nil
  3674. "Options concerning archiving in Org-mode."
  3675. :tag "Org Archive"
  3676. :group 'org-structure)
  3677. (defcustom org-archive-location "%s_archive::"
  3678. "The location where subtrees should be archived.
  3679. The value of this variable is a string, consisting of two parts,
  3680. separated by a double-colon. The first part is a filename and
  3681. the second part is a headline.
  3682. When the filename is omitted, archiving happens in the same file.
  3683. %s in the filename will be replaced by the current file
  3684. name (without the directory part). Archiving to a different file
  3685. is useful to keep archived entries from contributing to the
  3686. Org-mode Agenda.
  3687. The archived entries will be filed as subtrees of the specified
  3688. headline. When the headline is omitted, the subtrees are simply
  3689. filed away at the end of the file, as top-level entries. Also in
  3690. the heading you can use %s to represent the file name, this can be
  3691. useful when using the same archive for a number of different files.
  3692. Here are a few examples:
  3693. \"%s_archive::\"
  3694. If the current file is Projects.org, archive in file
  3695. Projects.org_archive, as top-level trees. This is the default.
  3696. \"::* Archived Tasks\"
  3697. Archive in the current file, under the top-level headline
  3698. \"* Archived Tasks\".
  3699. \"~/org/archive.org::\"
  3700. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3701. \"~/org/archive.org::* From %s\"
  3702. Archive in file ~/org/archive.org (absolute path), under headlines
  3703. \"From FILENAME\" where file name is the current file name.
  3704. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3705. The \"datetree/\" string is special, signifying to archive
  3706. items to the datetree. Items are placed in either the CLOSED
  3707. date of the item, or the current date if there is no CLOSED date.
  3708. The heading will be a subentry to the current date. There doesn't
  3709. need to be a heading, but there always needs to be a slash after
  3710. datetree. For example, to store archived items directly in the
  3711. datetree, use \"~/org/datetree.org::datetree/\".
  3712. \"basement::** Finished Tasks\"
  3713. Archive in file ./basement (relative path), as level 3 trees
  3714. below the level 2 heading \"** Finished Tasks\".
  3715. You may set this option on a per-file basis by adding to the buffer a
  3716. line like
  3717. #+ARCHIVE: basement::** Finished Tasks
  3718. You may also define it locally for a subtree by setting an ARCHIVE property
  3719. in the entry. If such a property is found in an entry, or anywhere up
  3720. the hierarchy, it will be used."
  3721. :group 'org-archive
  3722. :type 'string)
  3723. (defcustom org-archive-tag "ARCHIVE"
  3724. "The tag that marks a subtree as archived.
  3725. An archived subtree does not open during visibility cycling, and does
  3726. not contribute to the agenda listings.
  3727. After changing this, font-lock must be restarted in the relevant buffers to
  3728. get the proper fontification."
  3729. :group 'org-archive
  3730. :group 'org-keywords
  3731. :type 'string)
  3732. (defcustom org-agenda-skip-archived-trees t
  3733. "Non-nil means the agenda will skip any items located in archived trees.
  3734. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3735. variable is no longer recommended, you should leave it at the value t.
  3736. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3737. :group 'org-archive
  3738. :group 'org-agenda-skip
  3739. :type 'boolean)
  3740. (defcustom org-columns-skip-archived-trees t
  3741. "Non-nil means ignore archived trees when creating column view."
  3742. :group 'org-archive
  3743. :group 'org-properties
  3744. :type 'boolean)
  3745. (defcustom org-cycle-open-archived-trees nil
  3746. "Non-nil means `org-cycle' will open archived trees.
  3747. An archived tree is a tree marked with the tag ARCHIVE.
  3748. When nil, archived trees will stay folded. You can still open them with
  3749. normal outline commands like `show-all', but not with the cycling commands."
  3750. :group 'org-archive
  3751. :group 'org-cycle
  3752. :type 'boolean)
  3753. (defcustom org-sparse-tree-open-archived-trees nil
  3754. "Non-nil means sparse tree construction shows matches in archived trees.
  3755. When nil, matches in these trees are highlighted, but the trees are kept in
  3756. collapsed state."
  3757. :group 'org-archive
  3758. :group 'org-sparse-trees
  3759. :type 'boolean)
  3760. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3761. "The default date type when building a sparse tree.
  3762. When this is nil, a date is a scheduled or a deadline timestamp.
  3763. Otherwise, these types are allowed:
  3764. all: all timestamps
  3765. active: only active timestamps (<...>)
  3766. inactive: only inactive timestamps (<...)
  3767. scheduled: only scheduled timestamps
  3768. deadline: only deadline timestamps"
  3769. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3770. (const :tag "All timestamps" all)
  3771. (const :tag "Only active timestamps" active)
  3772. (const :tag "Only inactive timestamps" inactive)
  3773. (const :tag "Only scheduled timestamps" scheduled)
  3774. (const :tag "Only deadline timestamps" deadline))
  3775. ;; :version "24.3"
  3776. :group 'org-sparse-trees)
  3777. (defun org-cycle-hide-archived-subtrees (state)
  3778. "Re-hide all archived subtrees after a visibility state change."
  3779. (when (and (not org-cycle-open-archived-trees)
  3780. (not (memq state '(overview folded))))
  3781. (save-excursion
  3782. (let* ((globalp (memq state '(contents all)))
  3783. (beg (if globalp (point-min) (point)))
  3784. (end (if globalp (point-max) (org-end-of-subtree t))))
  3785. (org-hide-archived-subtrees beg end)
  3786. (goto-char beg)
  3787. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3788. (message "%s" (substitute-command-keys
  3789. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3790. (defun org-force-cycle-archived ()
  3791. "Cycle subtree even if it is archived."
  3792. (interactive)
  3793. (setq this-command 'org-cycle)
  3794. (let ((org-cycle-open-archived-trees t))
  3795. (call-interactively 'org-cycle)))
  3796. (defun org-hide-archived-subtrees (beg end)
  3797. "Re-hide all archived subtrees after a visibility state change."
  3798. (save-excursion
  3799. (let* ((re (concat ":" org-archive-tag ":")))
  3800. (goto-char beg)
  3801. (while (re-search-forward re end t)
  3802. (when (org-at-heading-p)
  3803. (org-flag-subtree t)
  3804. (org-end-of-subtree t))))))
  3805. (declare-function outline-end-of-heading "outline" ())
  3806. (declare-function outline-flag-region "outline" (from to flag))
  3807. (defun org-flag-subtree (flag)
  3808. (save-excursion
  3809. (org-back-to-heading t)
  3810. (outline-end-of-heading)
  3811. (outline-flag-region (point)
  3812. (progn (org-end-of-subtree t) (point))
  3813. flag)))
  3814. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3815. (eval-and-compile
  3816. (org-autoload "org-archive"
  3817. '(org-add-archive-files org-archive-subtree
  3818. org-archive-to-archive-sibling org-toggle-archive-tag
  3819. org-archive-subtree-default
  3820. org-archive-subtree-default-with-confirmation)))
  3821. ;; Autoload Column View Code
  3822. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3823. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3824. (declare-function org-columns-compute "org-colview" (property))
  3825. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3826. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3827. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3828. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3829. ;; Autoload ID code
  3830. (declare-function org-id-store-link "org-id")
  3831. (declare-function org-id-locations-load "org-id")
  3832. (declare-function org-id-locations-save "org-id")
  3833. (defvar org-id-track-globally)
  3834. (org-autoload "org-id"
  3835. '(org-id-get-create org-id-new org-id-copy org-id-get
  3836. org-id-get-with-outline-path-completion
  3837. org-id-get-with-outline-drilling org-id-store-link
  3838. org-id-goto org-id-find org-id-store-link))
  3839. ;; Autoload Plotting Code
  3840. (org-autoload "org-plot"
  3841. '(org-plot/gnuplot))
  3842. ;;; Variables for pre-computed regular expressions, all buffer local
  3843. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3844. "Matches first line of a hidden block.")
  3845. (make-variable-buffer-local 'org-drawer-regexp)
  3846. (defvar org-todo-regexp nil
  3847. "Matches any of the TODO state keywords.")
  3848. (make-variable-buffer-local 'org-todo-regexp)
  3849. (defvar org-not-done-regexp nil
  3850. "Matches any of the TODO state keywords except the last one.")
  3851. (make-variable-buffer-local 'org-not-done-regexp)
  3852. (defvar org-not-done-heading-regexp nil
  3853. "Matches a TODO headline that is not done.")
  3854. (make-variable-buffer-local 'org-not-done-regexp)
  3855. (defvar org-todo-line-regexp nil
  3856. "Matches a headline and puts TODO state into group 2 if present.")
  3857. (make-variable-buffer-local 'org-todo-line-regexp)
  3858. (defvar org-complex-heading-regexp nil
  3859. "Matches a headline and puts everything into groups:
  3860. group 1: the stars
  3861. group 2: The todo keyword, maybe
  3862. group 3: Priority cookie
  3863. group 4: True headline
  3864. group 5: Tags")
  3865. (make-variable-buffer-local 'org-complex-heading-regexp)
  3866. (defvar org-complex-heading-regexp-format nil
  3867. "Printf format to make regexp to match an exact headline.
  3868. This regexp will match the headline of any node which has the
  3869. exact headline text that is put into the format, but may have any
  3870. TODO state, priority and tags.")
  3871. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3872. (defvar org-todo-line-tags-regexp nil
  3873. "Matches a headline and puts TODO state into group 2 if present.
  3874. Also put tags into group 4 if tags are present.")
  3875. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3876. (defvar org-ds-keyword-length 12
  3877. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3878. (make-variable-buffer-local 'org-ds-keyword-length)
  3879. (defvar org-deadline-regexp nil
  3880. "Matches the DEADLINE keyword.")
  3881. (make-variable-buffer-local 'org-deadline-regexp)
  3882. (defvar org-deadline-time-regexp nil
  3883. "Matches the DEADLINE keyword together with a time stamp.")
  3884. (make-variable-buffer-local 'org-deadline-time-regexp)
  3885. (defvar org-deadline-line-regexp nil
  3886. "Matches the DEADLINE keyword and the rest of the line.")
  3887. (make-variable-buffer-local 'org-deadline-line-regexp)
  3888. (defvar org-scheduled-regexp nil
  3889. "Matches the SCHEDULED keyword.")
  3890. (make-variable-buffer-local 'org-scheduled-regexp)
  3891. (defvar org-scheduled-time-regexp nil
  3892. "Matches the SCHEDULED keyword together with a time stamp.")
  3893. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3894. (defvar org-closed-time-regexp nil
  3895. "Matches the CLOSED keyword together with a time stamp.")
  3896. (make-variable-buffer-local 'org-closed-time-regexp)
  3897. (defvar org-keyword-time-regexp nil
  3898. "Matches any of the 4 keywords, together with the time stamp.")
  3899. (make-variable-buffer-local 'org-keyword-time-regexp)
  3900. (defvar org-keyword-time-not-clock-regexp nil
  3901. "Matches any of the 3 keywords, together with the time stamp.")
  3902. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3903. (defvar org-maybe-keyword-time-regexp nil
  3904. "Matches a timestamp, possibly preceded by a keyword.")
  3905. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3906. (defvar org-all-time-keywords nil
  3907. "List of time keywords.")
  3908. (make-variable-buffer-local 'org-all-time-keywords)
  3909. (defconst org-plain-time-of-day-regexp
  3910. (concat
  3911. "\\(\\<[012]?[0-9]"
  3912. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3913. "\\(--?"
  3914. "\\(\\<[012]?[0-9]"
  3915. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3916. "\\)?")
  3917. "Regular expression to match a plain time or time range.
  3918. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3919. groups carry important information:
  3920. 0 the full match
  3921. 1 the first time, range or not
  3922. 8 the second time, if it is a range.")
  3923. (defconst org-plain-time-extension-regexp
  3924. (concat
  3925. "\\(\\<[012]?[0-9]"
  3926. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3927. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3928. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3929. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3930. groups carry important information:
  3931. 0 the full match
  3932. 7 hours of duration
  3933. 9 minutes of duration")
  3934. (defconst org-stamp-time-of-day-regexp
  3935. (concat
  3936. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3937. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3938. "\\(--?"
  3939. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3940. "Regular expression to match a timestamp time or time range.
  3941. After a match, the following groups carry important information:
  3942. 0 the full match
  3943. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3944. 2 the first time, range or not
  3945. 4 the second time, if it is a range.")
  3946. (defconst org-startup-options
  3947. '(("fold" org-startup-folded t)
  3948. ("overview" org-startup-folded t)
  3949. ("nofold" org-startup-folded nil)
  3950. ("showall" org-startup-folded nil)
  3951. ("showeverything" org-startup-folded showeverything)
  3952. ("content" org-startup-folded content)
  3953. ("indent" org-startup-indented t)
  3954. ("noindent" org-startup-indented nil)
  3955. ("hidestars" org-hide-leading-stars t)
  3956. ("showstars" org-hide-leading-stars nil)
  3957. ("odd" org-odd-levels-only t)
  3958. ("oddeven" org-odd-levels-only nil)
  3959. ("align" org-startup-align-all-tables t)
  3960. ("noalign" org-startup-align-all-tables nil)
  3961. ("inlineimages" org-startup-with-inline-images t)
  3962. ("noinlineimages" org-startup-with-inline-images nil)
  3963. ("customtime" org-display-custom-times t)
  3964. ("logdone" org-log-done time)
  3965. ("lognotedone" org-log-done note)
  3966. ("nologdone" org-log-done nil)
  3967. ("lognoteclock-out" org-log-note-clock-out t)
  3968. ("nolognoteclock-out" org-log-note-clock-out nil)
  3969. ("logrepeat" org-log-repeat state)
  3970. ("lognoterepeat" org-log-repeat note)
  3971. ("nologrepeat" org-log-repeat nil)
  3972. ("logreschedule" org-log-reschedule time)
  3973. ("lognotereschedule" org-log-reschedule note)
  3974. ("nologreschedule" org-log-reschedule nil)
  3975. ("logredeadline" org-log-redeadline time)
  3976. ("lognoteredeadline" org-log-redeadline note)
  3977. ("nologredeadline" org-log-redeadline nil)
  3978. ("logrefile" org-log-refile time)
  3979. ("lognoterefile" org-log-refile note)
  3980. ("nologrefile" org-log-refile nil)
  3981. ("fninline" org-footnote-define-inline t)
  3982. ("nofninline" org-footnote-define-inline nil)
  3983. ("fnlocal" org-footnote-section nil)
  3984. ("fnauto" org-footnote-auto-label t)
  3985. ("fnprompt" org-footnote-auto-label nil)
  3986. ("fnconfirm" org-footnote-auto-label confirm)
  3987. ("fnplain" org-footnote-auto-label plain)
  3988. ("fnadjust" org-footnote-auto-adjust t)
  3989. ("nofnadjust" org-footnote-auto-adjust nil)
  3990. ("constcgs" constants-unit-system cgs)
  3991. ("constSI" constants-unit-system SI)
  3992. ("noptag" org-tag-persistent-alist nil)
  3993. ("hideblocks" org-hide-block-startup t)
  3994. ("nohideblocks" org-hide-block-startup nil)
  3995. ("beamer" org-startup-with-beamer-mode t)
  3996. ("entitiespretty" org-pretty-entities t)
  3997. ("entitiesplain" org-pretty-entities nil))
  3998. "Variable associated with STARTUP options for org-mode.
  3999. Each element is a list of three items: the startup options (as written
  4000. in the #+STARTUP line), the corresponding variable, and the value to set
  4001. this variable to if the option is found. An optional forth element PUSH
  4002. means to push this value onto the list in the variable.")
  4003. (defun org-update-property-plist (key val props)
  4004. "Update PROPS with KEY and VAL."
  4005. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4006. (key (if appending (substring key 0 (- (length key) 1)) key))
  4007. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4008. (previous (cdr (assoc key props))))
  4009. (if appending
  4010. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4011. (cons (cons key val) remainder))))
  4012. (defconst org-block-regexp
  4013. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4014. "Regular expression for hiding blocks.")
  4015. (defconst org-heading-keyword-regexp-format
  4016. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4017. "Printf format for a regexp matching an headline with some keyword.
  4018. This regexp will match the headline of any node which has the
  4019. exact keyword that is put into the format. The keyword isn't in
  4020. any group by default, but the stars and the body are.")
  4021. (defconst org-heading-keyword-maybe-regexp-format
  4022. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4023. "Printf format for a regexp matching an headline, possibly with some keyword.
  4024. This regexp can match any headline with the specified keyword, or
  4025. without a keyword. The keyword isn't in any group by default,
  4026. but the stars and the body are.")
  4027. (defun org-set-regexps-and-options ()
  4028. "Precompute regular expressions for current buffer."
  4029. (when (derived-mode-p 'org-mode)
  4030. (org-set-local 'org-todo-kwd-alist nil)
  4031. (org-set-local 'org-todo-key-alist nil)
  4032. (org-set-local 'org-todo-key-trigger nil)
  4033. (org-set-local 'org-todo-keywords-1 nil)
  4034. (org-set-local 'org-done-keywords nil)
  4035. (org-set-local 'org-todo-heads nil)
  4036. (org-set-local 'org-todo-sets nil)
  4037. (org-set-local 'org-todo-log-states nil)
  4038. (org-set-local 'org-file-properties nil)
  4039. (org-set-local 'org-file-tags nil)
  4040. (let ((re (org-make-options-regexp
  4041. '("CATEGORY" "TODO" "COLUMNS"
  4042. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  4043. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  4044. "OPTIONS")
  4045. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4046. (splitre "[ \t]+")
  4047. (scripts org-use-sub-superscripts)
  4048. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4049. tail sep kws1 prio props ftags drawers beamer-p
  4050. ext-setup-or-nil setup-contents (start 0))
  4051. (save-excursion
  4052. (save-restriction
  4053. (widen)
  4054. (goto-char (point-min))
  4055. (while (or (and ext-setup-or-nil
  4056. (string-match re ext-setup-or-nil start)
  4057. (setq start (match-end 0)))
  4058. (and (setq ext-setup-or-nil nil start 0)
  4059. (re-search-forward re nil t)))
  4060. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4061. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4062. (if (stringp value) (setq value (org-trim value)))
  4063. (cond
  4064. ((equal key "CATEGORY")
  4065. (setq cat value))
  4066. ((member key '("SEQ_TODO" "TODO"))
  4067. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4068. ((equal key "TYP_TODO")
  4069. (push (cons 'type (org-split-string value splitre)) kwds))
  4070. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4071. ;; general TODO-like setup
  4072. (push (cons (intern (downcase (match-string 1 key)))
  4073. (org-split-string value splitre)) kwds))
  4074. ((equal key "TAGS")
  4075. (setq tags (append tags (if tags '("\\n") nil)
  4076. (org-split-string value splitre))))
  4077. ((equal key "COLUMNS")
  4078. (org-set-local 'org-columns-default-format value))
  4079. ((equal key "LINK")
  4080. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4081. (push (cons (match-string 1 value)
  4082. (org-trim (match-string 2 value)))
  4083. links)))
  4084. ((equal key "PRIORITIES")
  4085. (setq prio (org-split-string value " +")))
  4086. ((equal key "PROPERTY")
  4087. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4088. (setq props (org-update-property-plist (match-string 1 value)
  4089. (match-string 2 value)
  4090. props))))
  4091. ((equal key "FILETAGS")
  4092. (when (string-match "\\S-" value)
  4093. (setq ftags
  4094. (append
  4095. ftags
  4096. (apply 'append
  4097. (mapcar (lambda (x) (org-split-string x ":"))
  4098. (org-split-string value)))))))
  4099. ((equal key "DRAWERS")
  4100. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4101. ((equal key "CONSTANTS")
  4102. (setq const (append const (org-split-string value splitre))))
  4103. ((equal key "STARTUP")
  4104. (let ((opts (org-split-string value splitre))
  4105. l var val)
  4106. (while (setq l (pop opts))
  4107. (when (setq l (assoc l org-startup-options))
  4108. (setq var (nth 1 l) val (nth 2 l))
  4109. (if (not (nth 3 l))
  4110. (set (make-local-variable var) val)
  4111. (if (not (listp (symbol-value var)))
  4112. (set (make-local-variable var) nil))
  4113. (set (make-local-variable var) (symbol-value var))
  4114. (add-to-list var val))))))
  4115. ((equal key "ARCHIVE")
  4116. (setq arch value)
  4117. (remove-text-properties 0 (length arch)
  4118. '(face t fontified t) arch))
  4119. ((equal key "LATEX_CLASS")
  4120. (setq beamer-p (equal value "beamer")))
  4121. ((equal key "OPTIONS")
  4122. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4123. (setq scripts (read (match-string 2 value)))))
  4124. ((equal key "SETUPFILE")
  4125. (setq setup-contents (org-file-contents
  4126. (expand-file-name
  4127. (org-remove-double-quotes value))
  4128. 'noerror))
  4129. (if (not ext-setup-or-nil)
  4130. (setq ext-setup-or-nil setup-contents start 0)
  4131. (setq ext-setup-or-nil
  4132. (concat (substring ext-setup-or-nil 0 start)
  4133. "\n" setup-contents "\n"
  4134. (substring ext-setup-or-nil start)))))))
  4135. ;; search for property blocks
  4136. (goto-char (point-min))
  4137. (while (re-search-forward org-block-regexp nil t)
  4138. (when (equal "PROPERTY" (upcase (match-string 1)))
  4139. (setq value (replace-regexp-in-string
  4140. "[\n\r]" " " (match-string 4)))
  4141. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4142. (setq props (org-update-property-plist (match-string 1 value)
  4143. (match-string 2 value)
  4144. props)))))))
  4145. (org-set-local 'org-use-sub-superscripts scripts)
  4146. (when cat
  4147. (org-set-local 'org-category (intern cat))
  4148. (push (cons "CATEGORY" cat) props))
  4149. (when prio
  4150. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4151. (setq prio (mapcar 'string-to-char prio))
  4152. (org-set-local 'org-highest-priority (nth 0 prio))
  4153. (org-set-local 'org-lowest-priority (nth 1 prio))
  4154. (org-set-local 'org-default-priority (nth 2 prio)))
  4155. (and props (org-set-local 'org-file-properties (nreverse props)))
  4156. (and ftags (org-set-local 'org-file-tags
  4157. (mapcar 'org-add-prop-inherited ftags)))
  4158. (and drawers (org-set-local 'org-drawers drawers))
  4159. (and arch (org-set-local 'org-archive-location arch))
  4160. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4161. ;; Process the TODO keywords
  4162. (unless kwds
  4163. ;; Use the global values as if they had been given locally.
  4164. (setq kwds (default-value 'org-todo-keywords))
  4165. (if (stringp (car kwds))
  4166. (setq kwds (list (cons org-todo-interpretation
  4167. (default-value 'org-todo-keywords)))))
  4168. (setq kwds (reverse kwds)))
  4169. (setq kwds (nreverse kwds))
  4170. (let (inter kws kw)
  4171. (while (setq kws (pop kwds))
  4172. (let ((kws (or
  4173. (run-hook-with-args-until-success
  4174. 'org-todo-setup-filter-hook kws)
  4175. kws)))
  4176. (setq inter (pop kws) sep (member "|" kws)
  4177. kws0 (delete "|" (copy-sequence kws))
  4178. kwsa nil
  4179. kws1 (mapcar
  4180. (lambda (x)
  4181. ;; 1 2
  4182. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4183. (progn
  4184. (setq kw (match-string 1 x)
  4185. key (and (match-end 2) (match-string 2 x))
  4186. log (org-extract-log-state-settings x))
  4187. (push (cons kw (and key (string-to-char key))) kwsa)
  4188. (and log (push log org-todo-log-states))
  4189. kw)
  4190. (error "Invalid TODO keyword %s" x)))
  4191. kws0)
  4192. kwsa (if kwsa (append '((:startgroup))
  4193. (nreverse kwsa)
  4194. '((:endgroup))))
  4195. hw (car kws1)
  4196. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4197. tail (list inter hw (car dws) (org-last dws))))
  4198. (add-to-list 'org-todo-heads hw 'append)
  4199. (push kws1 org-todo-sets)
  4200. (setq org-done-keywords (append org-done-keywords dws nil))
  4201. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4202. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4203. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4204. (setq org-todo-sets (nreverse org-todo-sets)
  4205. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4206. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4207. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4208. ;; Process the constants
  4209. (when const
  4210. (let (e cst)
  4211. (while (setq e (pop const))
  4212. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4213. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4214. (setq org-table-formula-constants-local cst)))
  4215. ;; Process the tags.
  4216. (when tags
  4217. (let (e tgs)
  4218. (while (setq e (pop tags))
  4219. (cond
  4220. ((equal e "{") (push '(:startgroup) tgs))
  4221. ((equal e "}") (push '(:endgroup) tgs))
  4222. ((equal e "\\n") (push '(:newline) tgs))
  4223. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4224. (push (cons (match-string 1 e)
  4225. (string-to-char (match-string 2 e)))
  4226. tgs))
  4227. (t (push (list e) tgs))))
  4228. (org-set-local 'org-tag-alist nil)
  4229. (while (setq e (pop tgs))
  4230. (or (and (stringp (car e))
  4231. (assoc (car e) org-tag-alist))
  4232. (push e org-tag-alist)))))
  4233. ;; Compute the regular expressions and other local variables.
  4234. ;; Using `org-outline-regexp-bol' would complicate them much,
  4235. ;; because of the fixed white space at the end of that string.
  4236. (if (not org-done-keywords)
  4237. (setq org-done-keywords (and org-todo-keywords-1
  4238. (list (org-last org-todo-keywords-1)))))
  4239. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4240. (length org-scheduled-string)
  4241. (length org-clock-string)
  4242. (length org-closed-string)))
  4243. org-drawer-regexp
  4244. (concat "^[ \t]*:\\("
  4245. (mapconcat 'regexp-quote org-drawers "\\|")
  4246. "\\):[ \t]*$")
  4247. org-not-done-keywords
  4248. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4249. org-todo-regexp
  4250. (concat "\\("
  4251. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4252. "\\)")
  4253. org-not-done-regexp
  4254. (concat "\\("
  4255. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4256. "\\)")
  4257. org-not-done-heading-regexp
  4258. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4259. org-todo-line-regexp
  4260. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4261. org-complex-heading-regexp
  4262. (concat "^\\(\\*+\\)"
  4263. "\\(?: +" org-todo-regexp "\\)?"
  4264. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4265. "\\(?: +\\(.*?\\)\\)??"
  4266. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4267. "[ \t]*$")
  4268. org-complex-heading-regexp-format
  4269. (concat "^\\(\\*+\\)"
  4270. "\\(?: +" org-todo-regexp "\\)?"
  4271. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4272. "\\(?: +"
  4273. ;; Stats cookies can be stuck to body.
  4274. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4275. "\\(%s\\)"
  4276. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4277. "\\)"
  4278. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4279. "[ \t]*$")
  4280. org-todo-line-tags-regexp
  4281. (concat "^\\(\\*+\\)"
  4282. "\\(?: +" org-todo-regexp "\\)?"
  4283. "\\(?: +\\(.*?\\)\\)??"
  4284. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4285. "[ \t]*$")
  4286. org-deadline-regexp (concat "\\<" org-deadline-string)
  4287. org-deadline-time-regexp
  4288. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4289. org-deadline-line-regexp
  4290. (concat "\\<\\(" org-deadline-string "\\).*")
  4291. org-scheduled-regexp
  4292. (concat "\\<" org-scheduled-string)
  4293. org-scheduled-time-regexp
  4294. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4295. org-closed-time-regexp
  4296. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4297. org-keyword-time-regexp
  4298. (concat "\\<\\(" org-scheduled-string
  4299. "\\|" org-deadline-string
  4300. "\\|" org-closed-string
  4301. "\\|" org-clock-string "\\)"
  4302. " *[[<]\\([^]>]+\\)[]>]")
  4303. org-keyword-time-not-clock-regexp
  4304. (concat "\\<\\(" org-scheduled-string
  4305. "\\|" org-deadline-string
  4306. "\\|" org-closed-string
  4307. "\\)"
  4308. " *[[<]\\([^]>]+\\)[]>]")
  4309. org-maybe-keyword-time-regexp
  4310. (concat "\\(\\<\\(" org-scheduled-string
  4311. "\\|" org-deadline-string
  4312. "\\|" org-closed-string
  4313. "\\|" org-clock-string "\\)\\)?"
  4314. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4315. org-all-time-keywords
  4316. (mapcar (lambda (w) (substring w 0 -1))
  4317. (list org-scheduled-string org-deadline-string
  4318. org-clock-string org-closed-string))
  4319. )
  4320. (org-compute-latex-and-specials-regexp)
  4321. (org-set-font-lock-defaults))))
  4322. (defun org-file-contents (file &optional noerror)
  4323. "Return the contents of FILE, as a string."
  4324. (if (or (not file)
  4325. (not (file-readable-p file)))
  4326. (if noerror
  4327. (progn
  4328. (message "Cannot read file \"%s\"" file)
  4329. (ding) (sit-for 2)
  4330. "")
  4331. (error "Cannot read file \"%s\"" file))
  4332. (with-temp-buffer
  4333. (insert-file-contents file)
  4334. (buffer-string))))
  4335. (defun org-extract-log-state-settings (x)
  4336. "Extract the log state setting from a TODO keyword string.
  4337. This will extract info from a string like \"WAIT(w@/!)\"."
  4338. (let (kw key log1 log2)
  4339. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4340. (setq kw (match-string 1 x)
  4341. key (and (match-end 2) (match-string 2 x))
  4342. log1 (and (match-end 3) (match-string 3 x))
  4343. log2 (and (match-end 4) (match-string 4 x)))
  4344. (and (or log1 log2)
  4345. (list kw
  4346. (and log1 (if (equal log1 "!") 'time 'note))
  4347. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4348. (defun org-remove-keyword-keys (list)
  4349. "Remove a pair of parenthesis at the end of each string in LIST."
  4350. (mapcar (lambda (x)
  4351. (if (string-match "(.*)$" x)
  4352. (substring x 0 (match-beginning 0))
  4353. x))
  4354. list))
  4355. (defun org-assign-fast-keys (alist)
  4356. "Assign fast keys to a keyword-key alist.
  4357. Respect keys that are already there."
  4358. (let (new e (alt ?0))
  4359. (while (setq e (pop alist))
  4360. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4361. (cdr e)) ;; Key already assigned.
  4362. (push e new)
  4363. (let ((clist (string-to-list (downcase (car e))))
  4364. (used (append new alist)))
  4365. (when (= (car clist) ?@)
  4366. (pop clist))
  4367. (while (and clist (rassoc (car clist) used))
  4368. (pop clist))
  4369. (unless clist
  4370. (while (rassoc alt used)
  4371. (incf alt)))
  4372. (push (cons (car e) (or (car clist) alt)) new))))
  4373. (nreverse new)))
  4374. ;;; Some variables used in various places
  4375. (defvar org-window-configuration nil
  4376. "Used in various places to store a window configuration.")
  4377. (defvar org-selected-window nil
  4378. "Used in various places to store a window configuration.")
  4379. (defvar org-finish-function nil
  4380. "Function to be called when `C-c C-c' is used.
  4381. This is for getting out of special buffers like capture.")
  4382. ;; FIXME: Occasionally check by commenting these, to make sure
  4383. ;; no other functions uses these, forgetting to let-bind them.
  4384. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4385. (defvar org-last-state)
  4386. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4387. ;; Defined somewhere in this file, but used before definition.
  4388. (defvar org-entities) ;; defined in org-entities.el
  4389. (defvar org-struct-menu)
  4390. (defvar org-org-menu)
  4391. (defvar org-tbl-menu)
  4392. ;;;; Define the Org-mode
  4393. ;; We use a before-change function to check if a table might need
  4394. ;; an update.
  4395. (defvar org-table-may-need-update t
  4396. "Indicates that a table might need an update.
  4397. This variable is set by `org-before-change-function'.
  4398. `org-table-align' sets it back to nil.")
  4399. (defun org-before-change-function (beg end)
  4400. "Every change indicates that a table might need an update."
  4401. (setq org-table-may-need-update t))
  4402. (defvar org-mode-map)
  4403. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4404. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4405. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4406. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4407. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4408. (defvar org-table-buffer-is-an nil)
  4409. (defvar bidi-paragraph-direction)
  4410. (defvar buffer-face-mode-face)
  4411. (require 'outline)
  4412. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4413. (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"))
  4414. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4415. ;; Other stuff we need.
  4416. (require 'time-date)
  4417. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4418. (require 'easymenu)
  4419. (require 'overlay)
  4420. (require 'org-macs)
  4421. (require 'org-entities)
  4422. ;; (require 'org-compat) moved higher up in the file before it is first used
  4423. (require 'org-faces)
  4424. (require 'org-list)
  4425. (require 'org-pcomplete)
  4426. (require 'org-src)
  4427. (require 'org-footnote)
  4428. ;; babel
  4429. (require 'ob)
  4430. (require 'ob-table)
  4431. (require 'ob-lob)
  4432. (require 'ob-ref)
  4433. (require 'ob-tangle)
  4434. (require 'ob-comint)
  4435. (require 'ob-keys)
  4436. ;;;###autoload
  4437. (define-derived-mode org-mode outline-mode "Org"
  4438. "Outline-based notes management and organizer, alias
  4439. \"Carsten's outline-mode for keeping track of everything.\"
  4440. Org-mode develops organizational tasks around a NOTES file which
  4441. contains information about projects as plain text. Org-mode is
  4442. implemented on top of outline-mode, which is ideal to keep the content
  4443. of large files well structured. It supports ToDo items, deadlines and
  4444. time stamps, which magically appear in the diary listing of the Emacs
  4445. calendar. Tables are easily created with a built-in table editor.
  4446. Plain text URL-like links connect to websites, emails (VM), Usenet
  4447. messages (Gnus), BBDB entries, and any files related to the project.
  4448. For printing and sharing of notes, an Org-mode file (or a part of it)
  4449. can be exported as a structured ASCII or HTML file.
  4450. The following commands are available:
  4451. \\{org-mode-map}"
  4452. ;; Get rid of Outline menus, they are not needed
  4453. ;; Need to do this here because define-derived-mode sets up
  4454. ;; the keymap so late. Still, it is a waste to call this each time
  4455. ;; we switch another buffer into org-mode.
  4456. (if (featurep 'xemacs)
  4457. (when (boundp 'outline-mode-menu-heading)
  4458. ;; Assume this is Greg's port, it uses easymenu
  4459. (easy-menu-remove outline-mode-menu-heading)
  4460. (easy-menu-remove outline-mode-menu-show)
  4461. (easy-menu-remove outline-mode-menu-hide))
  4462. (define-key org-mode-map [menu-bar headings] 'undefined)
  4463. (define-key org-mode-map [menu-bar hide] 'undefined)
  4464. (define-key org-mode-map [menu-bar show] 'undefined))
  4465. (org-load-modules-maybe)
  4466. (easy-menu-add org-org-menu)
  4467. (easy-menu-add org-tbl-menu)
  4468. (org-install-agenda-files-menu)
  4469. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4470. (add-to-invisibility-spec '(org-cwidth))
  4471. (add-to-invisibility-spec '(org-hide-block . t))
  4472. (when (featurep 'xemacs)
  4473. (org-set-local 'line-move-ignore-invisible t))
  4474. (org-set-local 'outline-regexp org-outline-regexp)
  4475. (org-set-local 'outline-level 'org-outline-level)
  4476. (setq bidi-paragraph-direction 'left-to-right)
  4477. (when (and org-ellipsis
  4478. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4479. (fboundp 'make-glyph-code))
  4480. (unless org-display-table
  4481. (setq org-display-table (make-display-table)))
  4482. (set-display-table-slot
  4483. org-display-table 4
  4484. (vconcat (mapcar
  4485. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4486. org-ellipsis)))
  4487. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4488. (setq buffer-display-table org-display-table))
  4489. (org-set-regexps-and-options)
  4490. (when (and org-tag-faces (not org-tags-special-faces-re))
  4491. ;; tag faces set outside customize.... force initialization.
  4492. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4493. ;; Calc embedded
  4494. (org-set-local 'calc-embedded-open-mode "# ")
  4495. (modify-syntax-entry ?@ "w")
  4496. (modify-syntax-entry ?\" "\"")
  4497. (if org-startup-truncated (setq truncate-lines t))
  4498. (org-set-local 'font-lock-unfontify-region-function
  4499. 'org-unfontify-region)
  4500. ;; Activate before-change-function
  4501. (org-set-local 'org-table-may-need-update t)
  4502. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4503. 'local)
  4504. ;; Check for running clock before killing a buffer
  4505. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4506. ;; Initialize macros templates.
  4507. (org-macro-initialize-templates)
  4508. ;; Initialize radio targets.
  4509. (org-update-radio-target-regexp)
  4510. ;; Indentation.
  4511. (org-set-local 'indent-line-function 'org-indent-line)
  4512. (org-set-local 'indent-region-function 'org-indent-region)
  4513. ;; Filling and auto-filling.
  4514. (org-setup-filling)
  4515. ;; Comments.
  4516. (org-setup-comments-handling)
  4517. ;; Beginning/end of defun
  4518. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4519. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4520. ;; Next error for sparse trees
  4521. (org-set-local 'next-error-function 'org-occur-next-match)
  4522. ;; Make sure dependence stuff works reliably, even for users who set it
  4523. ;; too late :-(
  4524. (if org-enforce-todo-dependencies
  4525. (add-hook 'org-blocker-hook
  4526. 'org-block-todo-from-children-or-siblings-or-parent)
  4527. (remove-hook 'org-blocker-hook
  4528. 'org-block-todo-from-children-or-siblings-or-parent))
  4529. (if org-enforce-todo-checkbox-dependencies
  4530. (add-hook 'org-blocker-hook
  4531. 'org-block-todo-from-checkboxes)
  4532. (remove-hook 'org-blocker-hook
  4533. 'org-block-todo-from-checkboxes))
  4534. ;; Align options lines
  4535. (org-set-local
  4536. 'align-mode-rules-list
  4537. '((org-in-buffer-settings
  4538. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4539. (modes . '(org-mode)))))
  4540. ;; Imenu
  4541. (org-set-local 'imenu-create-index-function
  4542. 'org-imenu-get-tree)
  4543. ;; Make isearch reveal context
  4544. (if (or (featurep 'xemacs)
  4545. (not (boundp 'outline-isearch-open-invisible-function)))
  4546. ;; Emacs 21 and XEmacs make use of the hook
  4547. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4548. ;; Emacs 22 deals with this through a special variable
  4549. (org-set-local 'outline-isearch-open-invisible-function
  4550. (lambda (&rest ignore) (org-show-context 'isearch))))
  4551. ;; Turn on org-beamer-mode?
  4552. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4553. ;; Setup the pcomplete hooks
  4554. (set (make-local-variable 'pcomplete-command-completion-function)
  4555. 'org-pcomplete-initial)
  4556. (set (make-local-variable 'pcomplete-command-name-function)
  4557. 'org-command-at-point)
  4558. (set (make-local-variable 'pcomplete-default-completion-function)
  4559. 'ignore)
  4560. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4561. 'org-parse-arguments)
  4562. (set (make-local-variable 'pcomplete-termination-string) "")
  4563. (when (>= emacs-major-version 23)
  4564. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4565. ;; If empty file that did not turn on org-mode automatically, make it to.
  4566. (if (and org-insert-mode-line-in-empty-file
  4567. (org-called-interactively-p 'any)
  4568. (= (point-min) (point-max)))
  4569. (insert "# -*- mode: org -*-\n\n"))
  4570. (unless org-inhibit-startup
  4571. (when org-startup-align-all-tables
  4572. (let ((bmp (buffer-modified-p)))
  4573. (org-table-map-tables 'org-table-align 'quietly)
  4574. (set-buffer-modified-p bmp)))
  4575. (when org-startup-with-inline-images
  4576. (org-display-inline-images))
  4577. (when org-startup-indented
  4578. (require 'org-indent)
  4579. (org-indent-mode 1))
  4580. (unless org-inhibit-startup-visibility-stuff
  4581. (org-set-startup-visibility)))
  4582. ;; Try to set org-hide correctly
  4583. (set-face-foreground
  4584. 'org-hide
  4585. (or (face-background 'default)
  4586. (face-background 'org-default)
  4587. (cdr (assoc 'background-color default-frame-alist))
  4588. (cdr (assoc 'background-color initial-frame-alist))
  4589. (cdr (assoc 'background-color window-system-default-frame-alist))
  4590. (face-foreground 'org-hide))))
  4591. (when (fboundp 'abbrev-table-put)
  4592. (abbrev-table-put org-mode-abbrev-table
  4593. :parents (list text-mode-abbrev-table)))
  4594. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4595. (defun org-current-time ()
  4596. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4597. (if (> (car org-time-stamp-rounding-minutes) 1)
  4598. (let ((r (car org-time-stamp-rounding-minutes))
  4599. (time (decode-time)))
  4600. (apply 'encode-time
  4601. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4602. (nthcdr 2 time))))
  4603. (current-time)))
  4604. (defun org-today ()
  4605. "Return today date, considering `org-extend-today-until'."
  4606. (time-to-days
  4607. (time-subtract (current-time)
  4608. (list 0 (* 3600 org-extend-today-until) 0))))
  4609. ;;;; Font-Lock stuff, including the activators
  4610. (defvar org-mouse-map (make-sparse-keymap))
  4611. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4612. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4613. (when org-mouse-1-follows-link
  4614. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4615. (when org-tab-follows-link
  4616. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4617. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4618. (require 'font-lock)
  4619. (defconst org-non-link-chars "]\t\n\r<>")
  4620. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4621. "shell" "elisp" "doi" "message"))
  4622. (defvar org-link-types-re nil
  4623. "Matches a link that has a url-like prefix like \"http:\"")
  4624. (defvar org-link-re-with-space nil
  4625. "Matches a link with spaces, optional angular brackets around it.")
  4626. (defvar org-link-re-with-space2 nil
  4627. "Matches a link with spaces, optional angular brackets around it.")
  4628. (defvar org-link-re-with-space3 nil
  4629. "Matches a link with spaces, only for internal part in bracket links.")
  4630. (defvar org-angle-link-re nil
  4631. "Matches link with angular brackets, spaces are allowed.")
  4632. (defvar org-plain-link-re nil
  4633. "Matches plain link, without spaces.")
  4634. (defvar org-bracket-link-regexp nil
  4635. "Matches a link in double brackets.")
  4636. (defvar org-bracket-link-analytic-regexp nil
  4637. "Regular expression used to analyze links.
  4638. Here is what the match groups contain after a match:
  4639. 1: http:
  4640. 2: http
  4641. 3: path
  4642. 4: [desc]
  4643. 5: desc")
  4644. (defvar org-bracket-link-analytic-regexp++ nil
  4645. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4646. (defvar org-any-link-re nil
  4647. "Regular expression matching any link.")
  4648. (defcustom org-match-sexp-depth 3
  4649. "Number of stacked braces for sub/superscript matching.
  4650. This has to be set before loading org.el to be effective."
  4651. :group 'org-export-translation ; ??????????????????????????/
  4652. :type 'integer)
  4653. (defun org-create-multibrace-regexp (left right n)
  4654. "Create a regular expression which will match a balanced sexp.
  4655. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4656. as single character strings.
  4657. The regexp returned will match the entire expression including the
  4658. delimiters. It will also define a single group which contains the
  4659. match except for the outermost delimiters. The maximum depth of
  4660. stacked delimiters is N. Escaping delimiters is not possible."
  4661. (let* ((nothing (concat "[^" left right "]*?"))
  4662. (or "\\|")
  4663. (re nothing)
  4664. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4665. (while (> n 1)
  4666. (setq n (1- n)
  4667. re (concat re or next)
  4668. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4669. (concat left "\\(" re "\\)" right)))
  4670. (defvar org-match-substring-regexp
  4671. (concat
  4672. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4673. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4674. "\\|"
  4675. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4676. "\\|"
  4677. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4678. "The regular expression matching a sub- or superscript.")
  4679. (defvar org-match-substring-with-braces-regexp
  4680. (concat
  4681. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4682. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4683. "\\)")
  4684. "The regular expression matching a sub- or superscript, forcing braces.")
  4685. (defun org-make-link-regexps ()
  4686. "Update the link regular expressions.
  4687. This should be called after the variable `org-link-types' has changed."
  4688. (setq org-link-types-re
  4689. (concat
  4690. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4691. org-link-re-with-space
  4692. (concat
  4693. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4694. "\\([^" org-non-link-chars " ]"
  4695. "[^" org-non-link-chars "]*"
  4696. "[^" org-non-link-chars " ]\\)>?")
  4697. org-link-re-with-space2
  4698. (concat
  4699. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4700. "\\([^" org-non-link-chars " ]"
  4701. "[^\t\n\r]*"
  4702. "[^" org-non-link-chars " ]\\)>?")
  4703. org-link-re-with-space3
  4704. (concat
  4705. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4706. "\\([^" org-non-link-chars " ]"
  4707. "[^\t\n\r]*\\)")
  4708. org-angle-link-re
  4709. (concat
  4710. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4711. "\\([^" org-non-link-chars " ]"
  4712. "[^" org-non-link-chars "]*"
  4713. "\\)>")
  4714. org-plain-link-re
  4715. (concat
  4716. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4717. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4718. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4719. org-bracket-link-regexp
  4720. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4721. org-bracket-link-analytic-regexp
  4722. (concat
  4723. "\\[\\["
  4724. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4725. "\\([^]]+\\)"
  4726. "\\]"
  4727. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4728. "\\]")
  4729. org-bracket-link-analytic-regexp++
  4730. (concat
  4731. "\\[\\["
  4732. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4733. "\\([^]]+\\)"
  4734. "\\]"
  4735. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4736. "\\]")
  4737. org-any-link-re
  4738. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4739. org-angle-link-re "\\)\\|\\("
  4740. org-plain-link-re "\\)")))
  4741. (org-make-link-regexps)
  4742. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4743. "Regular expression for fast time stamp matching.")
  4744. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4745. "Regular expression for fast time stamp matching.")
  4746. (defconst org-ts-regexp0
  4747. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4748. "Regular expression matching time strings for analysis.
  4749. This one does not require the space after the date, so it can be used
  4750. on a string that terminates immediately after the date.")
  4751. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4752. "Regular expression matching time strings for analysis.")
  4753. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4754. "Regular expression matching time stamps, with groups.")
  4755. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4756. "Regular expression matching time stamps (also [..]), with groups.")
  4757. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4758. "Regular expression matching a time stamp range.")
  4759. (defconst org-tr-regexp-both
  4760. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4761. "Regular expression matching a time stamp range.")
  4762. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4763. org-ts-regexp "\\)?")
  4764. "Regular expression matching a time stamp or time stamp range.")
  4765. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4766. org-ts-regexp-both "\\)?")
  4767. "Regular expression matching a time stamp or time stamp range.
  4768. The time stamps may be either active or inactive.")
  4769. (defvar org-emph-face nil)
  4770. (defun org-do-emphasis-faces (limit)
  4771. "Run through the buffer and add overlays to emphasized strings."
  4772. (let (rtn a)
  4773. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4774. (if (not (= (char-after (match-beginning 3))
  4775. (char-after (match-beginning 4))))
  4776. (progn
  4777. (setq rtn t)
  4778. (setq a (assoc (match-string 3) org-emphasis-alist))
  4779. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4780. 'face
  4781. (nth 1 a))
  4782. (and (nth 4 a)
  4783. (org-remove-flyspell-overlays-in
  4784. (match-beginning 0) (match-end 0)))
  4785. (add-text-properties (match-beginning 2) (match-end 2)
  4786. '(font-lock-multiline t org-emphasis t))
  4787. (when org-hide-emphasis-markers
  4788. (add-text-properties (match-end 4) (match-beginning 5)
  4789. '(invisible org-link))
  4790. (add-text-properties (match-beginning 3) (match-end 3)
  4791. '(invisible org-link)))))
  4792. (backward-char 1))
  4793. rtn))
  4794. (defun org-emphasize (&optional char)
  4795. "Insert or change an emphasis, i.e. a font like bold or italic.
  4796. If there is an active region, change that region to a new emphasis.
  4797. If there is no region, just insert the marker characters and position
  4798. the cursor between them.
  4799. CHAR should be either the marker character, or the first character of the
  4800. HTML tag associated with that emphasis. If CHAR is a space, the means
  4801. to remove the emphasis of the selected region.
  4802. If char is not given (for example in an interactive call) it
  4803. will be prompted for."
  4804. (interactive)
  4805. (let ((eal org-emphasis-alist) e det
  4806. (erc org-emphasis-regexp-components)
  4807. (prompt "")
  4808. (string "") beg end move tag c s)
  4809. (if (org-region-active-p)
  4810. (setq beg (region-beginning) end (region-end)
  4811. string (buffer-substring beg end))
  4812. (setq move t))
  4813. (while (setq e (pop eal))
  4814. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4815. c (aref tag 0))
  4816. (push (cons c (string-to-char (car e))) det)
  4817. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4818. (substring tag 1)))))
  4819. (setq det (nreverse det))
  4820. (unless char
  4821. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4822. (setq char (read-char-exclusive)))
  4823. (setq char (or (cdr (assoc char det)) char))
  4824. (if (equal char ?\ )
  4825. (setq s "" move nil)
  4826. (unless (assoc (char-to-string char) org-emphasis-alist)
  4827. (error "No such emphasis marker: \"%c\"" char))
  4828. (setq s (char-to-string char)))
  4829. (while (and (> (length string) 1)
  4830. (equal (substring string 0 1) (substring string -1))
  4831. (assoc (substring string 0 1) org-emphasis-alist))
  4832. (setq string (substring string 1 -1)))
  4833. (setq string (concat s string s))
  4834. (if beg (delete-region beg end))
  4835. (unless (or (bolp)
  4836. (string-match (concat "[" (nth 0 erc) "\n]")
  4837. (char-to-string (char-before (point)))))
  4838. (insert " "))
  4839. (unless (or (eobp)
  4840. (string-match (concat "[" (nth 1 erc) "\n]")
  4841. (char-to-string (char-after (point)))))
  4842. (insert " ") (backward-char 1))
  4843. (insert string)
  4844. (and move (backward-char 1))))
  4845. (defconst org-nonsticky-props
  4846. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4847. (defsubst org-rear-nonsticky-at (pos)
  4848. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4849. (defun org-activate-plain-links (limit)
  4850. "Run through the buffer and add overlays to links."
  4851. (catch 'exit
  4852. (let (f)
  4853. (when (re-search-forward (concat org-plain-link-re) limit t)
  4854. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4855. (setq f (get-text-property (match-beginning 0) 'face))
  4856. (if (or (eq f 'org-tag)
  4857. (and (listp f) (memq 'org-tag f)))
  4858. nil
  4859. (add-text-properties (match-beginning 0) (match-end 0)
  4860. (list 'mouse-face 'highlight
  4861. 'face 'org-link
  4862. 'keymap org-mouse-map))
  4863. (org-rear-nonsticky-at (match-end 0)))
  4864. t))))
  4865. (defun org-activate-code (limit)
  4866. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4867. (progn
  4868. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4869. (remove-text-properties (match-beginning 0) (match-end 0)
  4870. '(display t invisible t intangible t))
  4871. t)))
  4872. (defcustom org-src-fontify-natively nil
  4873. "When non-nil, fontify code in code blocks."
  4874. :type 'boolean
  4875. :version "24.1"
  4876. :group 'org-appearance
  4877. :group 'org-babel)
  4878. (defcustom org-allow-promoting-top-level-subtree nil
  4879. "When non-nil, allow promoting a top level subtree.
  4880. The leading star of the top level headline will be replaced
  4881. by a #."
  4882. :type 'boolean
  4883. :version "24.1"
  4884. :group 'org-appearance)
  4885. (defun org-fontify-meta-lines-and-blocks (limit)
  4886. (condition-case nil
  4887. (org-fontify-meta-lines-and-blocks-1 limit)
  4888. (error (message "org-mode fontification error"))))
  4889. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4890. "Fontify #+ lines and blocks, in the correct ways."
  4891. (let ((case-fold-search t))
  4892. (if (re-search-forward
  4893. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4894. limit t)
  4895. (let ((beg (match-beginning 0))
  4896. (block-start (match-end 0))
  4897. (block-end nil)
  4898. (lang (match-string 7))
  4899. (beg1 (line-beginning-position 2))
  4900. (dc1 (downcase (match-string 2)))
  4901. (dc3 (downcase (match-string 3)))
  4902. end end1 quoting block-type ovl)
  4903. (cond
  4904. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4905. ;; a single line of backend-specific content
  4906. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4907. (remove-text-properties (match-beginning 0) (match-end 0)
  4908. '(display t invisible t intangible t))
  4909. (add-text-properties (match-beginning 1) (match-end 3)
  4910. '(font-lock-fontified t face org-meta-line))
  4911. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4912. '(font-lock-fontified t face org-block))
  4913. ; for backend-specific code
  4914. t)
  4915. ((and (match-end 4) (equal dc3 "+begin"))
  4916. ;; Truly a block
  4917. (setq block-type (downcase (match-string 5))
  4918. quoting (member block-type org-protecting-blocks))
  4919. (when (re-search-forward
  4920. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4921. nil t) ;; on purpose, we look further than LIMIT
  4922. (setq end (min (point-max) (match-end 0))
  4923. end1 (min (point-max) (1- (match-beginning 0))))
  4924. (setq block-end (match-beginning 0))
  4925. (when quoting
  4926. (remove-text-properties beg end
  4927. '(display t invisible t intangible t)))
  4928. (add-text-properties
  4929. beg end
  4930. '(font-lock-fontified t font-lock-multiline t))
  4931. (add-text-properties beg beg1 '(face org-meta-line))
  4932. (add-text-properties end1 (min (point-max) (1+ end))
  4933. '(face org-meta-line)) ; for end_src
  4934. (cond
  4935. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4936. (org-src-font-lock-fontify-block lang block-start block-end)
  4937. ;; remove old background overlays
  4938. (mapc (lambda (ov)
  4939. (if (eq (overlay-get ov 'face) 'org-block-background)
  4940. (delete-overlay ov)))
  4941. (overlays-at (/ (+ beg1 block-end) 2)))
  4942. ;; add a background overlay
  4943. (setq ovl (make-overlay beg1 block-end))
  4944. (overlay-put ovl 'face 'org-block-background)
  4945. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4946. (quoting
  4947. (add-text-properties beg1 (min (point-max) (1+ end1))
  4948. '(face org-block))) ; end of source block
  4949. ((not org-fontify-quote-and-verse-blocks))
  4950. ((string= block-type "quote")
  4951. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4952. ((string= block-type "verse")
  4953. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4954. (add-text-properties beg beg1 '(face org-block-begin-line))
  4955. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4956. '(face org-block-end-line))
  4957. t))
  4958. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4959. (add-text-properties
  4960. beg (match-end 3)
  4961. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4962. '(font-lock-fontified t invisible t)
  4963. '(font-lock-fontified t face org-document-info-keyword)))
  4964. (add-text-properties
  4965. (match-beginning 6) (match-end 6)
  4966. (if (string-equal dc1 "+title:")
  4967. '(font-lock-fontified t face org-document-title)
  4968. '(font-lock-fontified t face org-document-info))))
  4969. ((or (equal dc1 "+results")
  4970. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4971. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4972. "+call:" "+header:" "+headers:" "+name:"))
  4973. (and (match-end 4) (equal dc3 "+attr")))
  4974. (add-text-properties
  4975. beg (match-end 0)
  4976. '(font-lock-fontified t face org-meta-line))
  4977. t)
  4978. ((member dc3 '(" " ""))
  4979. (add-text-properties
  4980. beg (match-end 0)
  4981. '(font-lock-fontified t face font-lock-comment-face)))
  4982. ((not (member (char-after beg) '(?\ ?\t)))
  4983. ;; just any other in-buffer setting, but not indented
  4984. (add-text-properties
  4985. beg (match-end 0)
  4986. '(font-lock-fontified t face org-meta-line))
  4987. t)
  4988. (t nil))))))
  4989. (defun org-strip-protective-commas (beg end)
  4990. "Strip protective commas between BEG and END in the current buffer."
  4991. (interactive "r")
  4992. (save-excursion
  4993. (save-match-data
  4994. (goto-char beg)
  4995. (let ((front-line (save-excursion
  4996. (re-search-forward
  4997. "[^[:space:]]" end t)
  4998. (goto-char (match-beginning 0))
  4999. (current-column))))
  5000. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\)" end t)
  5001. (goto-char (match-beginning 1))
  5002. (when (= (current-column) front-line)
  5003. (replace-match "" nil nil nil 1)))))))
  5004. (defun org-activate-angle-links (limit)
  5005. "Run through the buffer and add overlays to links."
  5006. (if (re-search-forward org-angle-link-re limit t)
  5007. (progn
  5008. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5009. (add-text-properties (match-beginning 0) (match-end 0)
  5010. (list 'mouse-face 'highlight
  5011. 'keymap org-mouse-map))
  5012. (org-rear-nonsticky-at (match-end 0))
  5013. t)))
  5014. (defun org-activate-footnote-links (limit)
  5015. "Run through the buffer and add overlays to footnotes."
  5016. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5017. (when fn
  5018. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5019. (org-remove-flyspell-overlays-in beg end)
  5020. (add-text-properties beg end
  5021. (list 'mouse-face 'highlight
  5022. 'keymap org-mouse-map
  5023. 'help-echo
  5024. (if (= (point-at-bol) beg)
  5025. "Footnote definition"
  5026. "Footnote reference")
  5027. 'font-lock-fontified t
  5028. 'font-lock-multiline t
  5029. 'face 'org-footnote))))))
  5030. (defun org-activate-bracket-links (limit)
  5031. "Run through the buffer and add overlays to bracketed links."
  5032. (if (re-search-forward org-bracket-link-regexp limit t)
  5033. (let* ((help (concat "LINK: "
  5034. (org-match-string-no-properties 1)))
  5035. ;; FIXME: above we should remove the escapes.
  5036. ;; but that requires another match, protecting match data,
  5037. ;; a lot of overhead for font-lock.
  5038. (ip (org-maybe-intangible
  5039. (list 'invisible 'org-link
  5040. 'keymap org-mouse-map 'mouse-face 'highlight
  5041. 'font-lock-multiline t 'help-echo help)))
  5042. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5043. 'font-lock-multiline t 'help-echo help)))
  5044. ;; We need to remove the invisible property here. Table narrowing
  5045. ;; may have made some of this invisible.
  5046. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5047. (remove-text-properties (match-beginning 0) (match-end 0)
  5048. '(invisible nil))
  5049. (if (match-end 3)
  5050. (progn
  5051. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5052. (org-rear-nonsticky-at (match-beginning 3))
  5053. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5054. (org-rear-nonsticky-at (match-end 3))
  5055. (add-text-properties (match-end 3) (match-end 0) ip)
  5056. (org-rear-nonsticky-at (match-end 0)))
  5057. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5058. (org-rear-nonsticky-at (match-beginning 1))
  5059. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5060. (org-rear-nonsticky-at (match-end 1))
  5061. (add-text-properties (match-end 1) (match-end 0) ip)
  5062. (org-rear-nonsticky-at (match-end 0)))
  5063. t)))
  5064. (defun org-activate-dates (limit)
  5065. "Run through the buffer and add overlays to dates."
  5066. (if (re-search-forward org-tsr-regexp-both limit t)
  5067. (progn
  5068. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5069. (add-text-properties (match-beginning 0) (match-end 0)
  5070. (list 'mouse-face 'highlight
  5071. 'keymap org-mouse-map))
  5072. (org-rear-nonsticky-at (match-end 0))
  5073. (when org-display-custom-times
  5074. (if (match-end 3)
  5075. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5076. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5077. t)))
  5078. (defvar org-target-link-regexp nil
  5079. "Regular expression matching radio targets in plain text.")
  5080. (make-variable-buffer-local 'org-target-link-regexp)
  5081. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5082. "Regular expression matching a link target.")
  5083. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5084. "Regular expression matching a radio target.")
  5085. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5086. "Regular expression matching any target.")
  5087. (defun org-activate-target-links (limit)
  5088. "Run through the buffer and add overlays to target matches."
  5089. (when org-target-link-regexp
  5090. (let ((case-fold-search t))
  5091. (if (re-search-forward org-target-link-regexp limit t)
  5092. (progn
  5093. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5094. (add-text-properties (match-beginning 0) (match-end 0)
  5095. (list 'mouse-face 'highlight
  5096. 'keymap org-mouse-map
  5097. 'help-echo "Radio target link"
  5098. 'org-linked-text t))
  5099. (org-rear-nonsticky-at (match-end 0))
  5100. t)))))
  5101. (defun org-update-radio-target-regexp ()
  5102. "Find all radio targets in this file and update the regular expression."
  5103. (interactive)
  5104. (when (memq 'radio org-activate-links)
  5105. (setq org-target-link-regexp
  5106. (org-make-target-link-regexp (org-all-targets 'radio)))
  5107. (org-restart-font-lock)))
  5108. (defun org-hide-wide-columns (limit)
  5109. (let (s e)
  5110. (setq s (text-property-any (point) (or limit (point-max))
  5111. 'org-cwidth t))
  5112. (when s
  5113. (setq e (next-single-property-change s 'org-cwidth))
  5114. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5115. (goto-char e)
  5116. t)))
  5117. (defvar org-latex-and-specials-regexp nil
  5118. "Regular expression for highlighting export special stuff.")
  5119. (defvar org-match-substring-regexp)
  5120. (defvar org-match-substring-with-braces-regexp)
  5121. ;; This should be with the exporter code, but we also use if for font-locking
  5122. (defconst org-export-html-special-string-regexps
  5123. '(("\\\\-" . "&shy;")
  5124. ("---\\([^-]\\)" . "&mdash;\\1")
  5125. ("--\\([^-]\\)" . "&ndash;\\1")
  5126. ("\\.\\.\\." . "&hellip;"))
  5127. "Regular expressions for special string conversion.")
  5128. (defun org-compute-latex-and-specials-regexp ()
  5129. "Compute regular expression for stuff treated specially by exporters."
  5130. (if (not org-highlight-latex-fragments-and-specials)
  5131. (org-set-local 'org-latex-and-specials-regexp nil)
  5132. (require 'org-exp)
  5133. (let*
  5134. ((matchers (plist-get org-format-latex-options :matchers))
  5135. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5136. org-latex-regexps)))
  5137. (org-export-allow-BIND nil)
  5138. (options (org-combine-plists (org-default-export-plist)
  5139. (org-infile-export-plist)))
  5140. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5141. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5142. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5143. (org-export-html-expand (plist-get options :expand-quoted-html))
  5144. (org-export-with-special-strings (plist-get options :special-strings))
  5145. (re-sub
  5146. (cond
  5147. ((equal org-export-with-sub-superscripts '{})
  5148. (list org-match-substring-with-braces-regexp))
  5149. (org-export-with-sub-superscripts
  5150. (list org-match-substring-regexp))))
  5151. (re-latex
  5152. (if org-export-with-LaTeX-fragments
  5153. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5154. (re-macros
  5155. (if org-export-with-TeX-macros
  5156. (list (concat "\\\\"
  5157. (regexp-opt
  5158. (append
  5159. (delq nil
  5160. (mapcar 'car-safe
  5161. (append org-entities-user
  5162. org-entities)))
  5163. (if (boundp 'org-latex-entities)
  5164. (mapcar (lambda (x)
  5165. (or (car-safe x) x))
  5166. org-latex-entities)
  5167. nil))
  5168. 'words))) ; FIXME
  5169. ))
  5170. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5171. (re-special (if org-export-with-special-strings
  5172. (mapcar (lambda (x) (car x))
  5173. org-export-html-special-string-regexps)))
  5174. (re-rest
  5175. (delq nil
  5176. (list
  5177. (if org-export-html-expand "@<[^>\n]+>")
  5178. ))))
  5179. (org-set-local
  5180. 'org-latex-and-specials-regexp
  5181. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5182. re-rest) "\\|")))))
  5183. (defun org-do-latex-and-special-faces (limit)
  5184. "Run through the buffer and add overlays to links."
  5185. (when org-latex-and-specials-regexp
  5186. (let (rtn d)
  5187. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5188. limit t))
  5189. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5190. 'face))
  5191. '(org-code org-verbatim underline)))
  5192. (progn
  5193. (setq rtn t
  5194. d (cond ((member (char-after (1+ (match-beginning 0)))
  5195. '(?_ ?^)) 1)
  5196. (t 0)))
  5197. (font-lock-prepend-text-property
  5198. (+ d (match-beginning 0)) (match-end 0)
  5199. 'face 'org-latex-and-export-specials)
  5200. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5201. '(font-lock-multiline t)))))
  5202. rtn)))
  5203. (defun org-restart-font-lock ()
  5204. "Restart `font-lock-mode', to force refontification."
  5205. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5206. (font-lock-mode -1)
  5207. (font-lock-mode 1)))
  5208. (defun org-all-targets (&optional radio)
  5209. "Return a list of all targets in this file.
  5210. With optional argument RADIO, only find radio targets."
  5211. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5212. rtn)
  5213. (save-excursion
  5214. (goto-char (point-min))
  5215. (while (re-search-forward re nil t)
  5216. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5217. rtn)))
  5218. (defun org-make-target-link-regexp (targets)
  5219. "Make regular expression matching all strings in TARGETS.
  5220. The regular expression finds the targets also if there is a line break
  5221. between words."
  5222. (and targets
  5223. (concat
  5224. "\\<\\("
  5225. (mapconcat
  5226. (lambda (x)
  5227. (setq x (regexp-quote x))
  5228. (while (string-match " +" x)
  5229. (setq x (replace-match "\\s-+" t t x)))
  5230. x)
  5231. targets
  5232. "\\|")
  5233. "\\)\\>")))
  5234. (defun org-activate-tags (limit)
  5235. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5236. (progn
  5237. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5238. (add-text-properties (match-beginning 1) (match-end 1)
  5239. (list 'mouse-face 'highlight
  5240. 'keymap org-mouse-map))
  5241. (org-rear-nonsticky-at (match-end 1))
  5242. t)))
  5243. (defun org-outline-level ()
  5244. "Compute the outline level of the heading at point.
  5245. If this is called at a normal headline, the level is the number of stars.
  5246. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5247. (save-excursion
  5248. (if (not (condition-case nil
  5249. (org-back-to-heading t)
  5250. (error nil)))
  5251. 0
  5252. (looking-at org-outline-regexp)
  5253. (1- (- (match-end 0) (match-beginning 0))))))
  5254. (defvar org-font-lock-keywords nil)
  5255. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5256. "Regular expression matching a property line.")
  5257. (defvar org-font-lock-hook nil
  5258. "Functions to be called for special font lock stuff.")
  5259. (defvar org-font-lock-set-keywords-hook nil
  5260. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5261. This is called after `org-font-lock-extra-keywords' is defined, but before
  5262. it is installed to be used by font lock. This can be useful if something
  5263. needs to be inserted at a specific position in the font-lock sequence.")
  5264. (defun org-font-lock-hook (limit)
  5265. (run-hook-with-args 'org-font-lock-hook limit))
  5266. (defun org-set-font-lock-defaults ()
  5267. (let* ((em org-fontify-emphasized-text)
  5268. (lk org-activate-links)
  5269. (org-font-lock-extra-keywords
  5270. (list
  5271. ;; Call the hook
  5272. '(org-font-lock-hook)
  5273. ;; Headlines
  5274. `(,(if org-fontify-whole-heading-line
  5275. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5276. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5277. (1 (org-get-level-face 1))
  5278. (2 (org-get-level-face 2))
  5279. (3 (org-get-level-face 3)))
  5280. ;; Table lines
  5281. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5282. (1 'org-table t))
  5283. ;; Table internals
  5284. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5285. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5286. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5287. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5288. ;; Drawers
  5289. (list org-drawer-regexp '(0 'org-special-keyword t))
  5290. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5291. ;; Properties
  5292. (list org-property-re
  5293. '(1 'org-special-keyword t)
  5294. '(3 'org-property-value t))
  5295. ;; Links
  5296. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5297. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5298. (if (memq 'plain lk) '(org-activate-plain-links))
  5299. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5300. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5301. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5302. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5303. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5304. '(org-hide-wide-columns (0 nil append))
  5305. ;; TODO keyword
  5306. (list (format org-heading-keyword-regexp-format
  5307. org-todo-regexp)
  5308. '(2 (org-get-todo-face 2) t))
  5309. ;; DONE
  5310. (if org-fontify-done-headline
  5311. (list (format org-heading-keyword-regexp-format
  5312. (concat
  5313. "\\(?:"
  5314. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5315. "\\)"))
  5316. '(2 'org-headline-done t))
  5317. nil)
  5318. ;; Priorities
  5319. '(org-font-lock-add-priority-faces)
  5320. ;; Tags
  5321. '(org-font-lock-add-tag-faces)
  5322. ;; Special keywords
  5323. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5324. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5325. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5326. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5327. ;; Emphasis
  5328. (if em
  5329. (if (featurep 'xemacs)
  5330. '(org-do-emphasis-faces (0 nil append))
  5331. '(org-do-emphasis-faces)))
  5332. ;; Checkboxes
  5333. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5334. 1 'org-checkbox prepend)
  5335. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5336. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5337. (0 (org-get-checkbox-statistics-face) t)))
  5338. ;; Description list items
  5339. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5340. 1 'org-list-dt prepend)
  5341. ;; ARCHIVEd headings
  5342. (list (concat
  5343. org-outline-regexp-bol
  5344. "\\(.*:" org-archive-tag ":.*\\)")
  5345. '(1 'org-archived prepend))
  5346. ;; Specials
  5347. '(org-do-latex-and-special-faces)
  5348. '(org-fontify-entities)
  5349. '(org-raise-scripts)
  5350. ;; Code
  5351. '(org-activate-code (1 'org-code t))
  5352. ;; COMMENT
  5353. (list (format org-heading-keyword-regexp-format
  5354. (concat "\\("
  5355. org-comment-string "\\|" org-quote-string
  5356. "\\)"))
  5357. '(2 'org-special-keyword t))
  5358. ;; Blocks and meta lines
  5359. '(org-fontify-meta-lines-and-blocks)
  5360. )))
  5361. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5362. (run-hooks 'org-font-lock-set-keywords-hook)
  5363. ;; Now set the full font-lock-keywords
  5364. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5365. (org-set-local 'font-lock-defaults
  5366. '(org-font-lock-keywords t nil nil backward-paragraph))
  5367. (kill-local-variable 'font-lock-keywords) nil))
  5368. (defun org-toggle-pretty-entities ()
  5369. "Toggle the composition display of entities as UTF8 characters."
  5370. (interactive)
  5371. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5372. (org-restart-font-lock)
  5373. (if org-pretty-entities
  5374. (message "Entities are displayed as UTF8 characters")
  5375. (save-restriction
  5376. (widen)
  5377. (org-decompose-region (point-min) (point-max))
  5378. (message "Entities are displayed plain"))))
  5379. (defvar org-custom-properties-overlays nil
  5380. "List of overlays used for custom properties.")
  5381. (make-variable-buffer-local 'org-custom-properties-overlays)
  5382. (defun org-toggle-custom-properties-visibility ()
  5383. "Display or hide properties in `org-custom-properties'."
  5384. (interactive)
  5385. (if org-custom-properties-overlays
  5386. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5387. (setq org-custom-properties-overlays nil))
  5388. (unless (not org-custom-properties)
  5389. (save-excursion
  5390. (save-restriction
  5391. (widen)
  5392. (goto-char (point-min))
  5393. (while (re-search-forward org-property-re nil t)
  5394. (mapc (lambda(p)
  5395. (when (equal p (substring (match-string 1) 1 -1))
  5396. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5397. (overlay-put o 'invisible t)
  5398. (overlay-put o 'org-custom-property t)
  5399. (push o org-custom-properties-overlays))))
  5400. org-custom-properties)))))))
  5401. (defun org-fontify-entities (limit)
  5402. "Find an entity to fontify."
  5403. (let (ee)
  5404. (when org-pretty-entities
  5405. (catch 'match
  5406. (while (re-search-forward
  5407. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5408. limit t)
  5409. (if (and (not (org-in-indented-comment-line))
  5410. (setq ee (org-entity-get (match-string 1)))
  5411. (= (length (nth 6 ee)) 1))
  5412. (let*
  5413. ((end (if (equal (match-string 2) "{}")
  5414. (match-end 2)
  5415. (match-end 1))))
  5416. (add-text-properties
  5417. (match-beginning 0) end
  5418. (list 'font-lock-fontified t))
  5419. (compose-region (match-beginning 0) end
  5420. (nth 6 ee) nil)
  5421. (backward-char 1)
  5422. (throw 'match t))))
  5423. nil))))
  5424. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5425. "Fontify string S like in Org-mode."
  5426. (with-temp-buffer
  5427. (insert s)
  5428. (let ((org-odd-levels-only odd-levels))
  5429. (org-mode)
  5430. (font-lock-fontify-buffer)
  5431. (buffer-string))))
  5432. (defvar org-m nil)
  5433. (defvar org-l nil)
  5434. (defvar org-f nil)
  5435. (defun org-get-level-face (n)
  5436. "Get the right face for match N in font-lock matching of headlines."
  5437. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5438. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5439. (if org-cycle-level-faces
  5440. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5441. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5442. (cond
  5443. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5444. ((eq n 2) org-f)
  5445. (t (if org-level-color-stars-only nil org-f))))
  5446. (defun org-get-todo-face (kwd)
  5447. "Get the right face for a TODO keyword KWD.
  5448. If KWD is a number, get the corresponding match group."
  5449. (if (numberp kwd) (setq kwd (match-string kwd)))
  5450. (or (org-face-from-face-or-color
  5451. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5452. (and (member kwd org-done-keywords) 'org-done)
  5453. 'org-todo))
  5454. (defun org-face-from-face-or-color (context inherit face-or-color)
  5455. "Create a face list that inherits INHERIT, but sets the foreground color.
  5456. When FACE-OR-COLOR is not a string, just return it."
  5457. (if (stringp face-or-color)
  5458. (list :inherit inherit
  5459. (cdr (assoc context org-faces-easy-properties))
  5460. face-or-color)
  5461. face-or-color))
  5462. (defun org-font-lock-add-tag-faces (limit)
  5463. "Add the special tag faces."
  5464. (when (and org-tag-faces org-tags-special-faces-re)
  5465. (while (re-search-forward org-tags-special-faces-re limit t)
  5466. (add-text-properties (match-beginning 1) (match-end 1)
  5467. (list 'face (org-get-tag-face 1)
  5468. 'font-lock-fontified t))
  5469. (backward-char 1))))
  5470. (defun org-font-lock-add-priority-faces (limit)
  5471. "Add the special priority faces."
  5472. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5473. (when (save-match-data (org-at-heading-p))
  5474. (add-text-properties
  5475. (match-beginning 0) (match-end 0)
  5476. (list 'face (or (org-face-from-face-or-color
  5477. 'priority 'org-special-keyword
  5478. (cdr (assoc (char-after (match-beginning 1))
  5479. org-priority-faces)))
  5480. 'org-special-keyword)
  5481. 'font-lock-fontified t)))))
  5482. (defun org-get-tag-face (kwd)
  5483. "Get the right face for a TODO keyword KWD.
  5484. If KWD is a number, get the corresponding match group."
  5485. (if (numberp kwd) (setq kwd (match-string kwd)))
  5486. (or (org-face-from-face-or-color
  5487. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5488. 'org-tag))
  5489. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5490. "Remove fontification and activation overlays from links."
  5491. (font-lock-default-unfontify-region beg end)
  5492. (let* ((buffer-undo-list t)
  5493. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5494. (inhibit-modification-hooks t)
  5495. deactivate-mark buffer-file-name buffer-file-truename)
  5496. (org-decompose-region beg end)
  5497. (remove-text-properties beg end
  5498. '(mouse-face t keymap t org-linked-text t
  5499. invisible t intangible t
  5500. org-no-flyspell t org-emphasis t))
  5501. (org-remove-font-lock-display-properties beg end)))
  5502. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5503. ((raise 0.3) (height 0.7))
  5504. ((raise -0.5))
  5505. ((raise 0.5)))
  5506. "Display properties for showing superscripts and subscripts.")
  5507. (defun org-remove-font-lock-display-properties (beg end)
  5508. "Remove specific display properties that have been added by font lock.
  5509. The will remove the raise properties that are used to show superscripts
  5510. and subscripts."
  5511. (let (next prop)
  5512. (while (< beg end)
  5513. (setq next (next-single-property-change beg 'display nil end)
  5514. prop (get-text-property beg 'display))
  5515. (if (member prop org-script-display)
  5516. (put-text-property beg next 'display nil))
  5517. (setq beg next))))
  5518. (defun org-raise-scripts (limit)
  5519. "Add raise properties to sub/superscripts."
  5520. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5521. (if (re-search-forward
  5522. (if (eq org-use-sub-superscripts t)
  5523. org-match-substring-regexp
  5524. org-match-substring-with-braces-regexp)
  5525. limit t)
  5526. (let* ((pos (point)) table-p comment-p
  5527. (mpos (match-beginning 3))
  5528. (emph-p (get-text-property mpos 'org-emphasis))
  5529. (link-p (get-text-property mpos 'mouse-face))
  5530. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5531. (goto-char (point-at-bol))
  5532. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5533. comment-p (org-looking-at-p "[ \t]*#"))
  5534. (goto-char pos)
  5535. ;; FIXME: Should we go back one character here, for a_b^c
  5536. ;; (goto-char (1- pos)) ;????????????????????
  5537. (if (or comment-p emph-p link-p keyw-p)
  5538. t
  5539. (put-text-property (match-beginning 3) (match-end 0)
  5540. 'display
  5541. (if (equal (char-after (match-beginning 2)) ?^)
  5542. (nth (if table-p 3 1) org-script-display)
  5543. (nth (if table-p 2 0) org-script-display)))
  5544. (add-text-properties (match-beginning 2) (match-end 2)
  5545. (list 'invisible t
  5546. 'org-dwidth t 'org-dwidth-n 1))
  5547. (if (and (eq (char-after (match-beginning 3)) ?{)
  5548. (eq (char-before (match-end 3)) ?}))
  5549. (progn
  5550. (add-text-properties
  5551. (match-beginning 3) (1+ (match-beginning 3))
  5552. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5553. (add-text-properties
  5554. (1- (match-end 3)) (match-end 3)
  5555. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5556. t)))))
  5557. ;;;; Visibility cycling, including org-goto and indirect buffer
  5558. ;;; Cycling
  5559. (defvar org-cycle-global-status nil)
  5560. (make-variable-buffer-local 'org-cycle-global-status)
  5561. (defvar org-cycle-subtree-status nil)
  5562. (make-variable-buffer-local 'org-cycle-subtree-status)
  5563. ;;;###autoload
  5564. (defvar org-inlinetask-min-level)
  5565. (defun org-cycle (&optional arg)
  5566. "TAB-action and visibility cycling for Org-mode.
  5567. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5568. is outline visibility cycling, but it also invokes other actions
  5569. in special contexts.
  5570. - When this function is called with a prefix argument, rotate the entire
  5571. buffer through 3 states (global cycling)
  5572. 1. OVERVIEW: Show only top-level headlines.
  5573. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5574. 3. SHOW ALL: Show everything.
  5575. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5576. determined by the variable `org-startup-folded', and by any VISIBILITY
  5577. properties in the buffer.
  5578. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5579. including any drawers.
  5580. - When inside a table, re-align the table and move to the next field.
  5581. - When point is at the beginning of a headline, rotate the subtree started
  5582. by this line through 3 different states (local cycling)
  5583. 1. FOLDED: Only the main headline is shown.
  5584. 2. CHILDREN: The main headline and the direct children are shown.
  5585. From this state, you can move to one of the children
  5586. and zoom in further.
  5587. 3. SUBTREE: Show the entire subtree, including body text.
  5588. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5589. - When point is at the beginning of an empty headline and the variable
  5590. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5591. of the headline by demoting and promoting it to likely levels. This
  5592. speeds up creation document structure by pressing TAB once or several
  5593. times right after creating a new headline.
  5594. - When there is a numeric prefix, go up to a heading with level ARG, do
  5595. a `show-subtree' and return to the previous cursor position. If ARG
  5596. is negative, go up that many levels.
  5597. - When point is not at the beginning of a headline, execute the global
  5598. binding for TAB, which is re-indenting the line. See the option
  5599. `org-cycle-emulate-tab' for details.
  5600. - Special case: if point is at the beginning of the buffer and there is
  5601. no headline in line 1, this function will act as if called with prefix arg
  5602. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5603. But only if also the variable `org-cycle-global-at-bob' is t."
  5604. (interactive "P")
  5605. (org-load-modules-maybe)
  5606. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5607. (and org-cycle-level-after-item/entry-creation
  5608. (or (org-cycle-level)
  5609. (org-cycle-item-indentation))))
  5610. (let* ((limit-level
  5611. (or org-cycle-max-level
  5612. (and (boundp 'org-inlinetask-min-level)
  5613. org-inlinetask-min-level
  5614. (1- org-inlinetask-min-level))))
  5615. (nstars (and limit-level
  5616. (if org-odd-levels-only
  5617. (and limit-level (1- (* limit-level 2)))
  5618. limit-level)))
  5619. (org-outline-regexp
  5620. (if (not (derived-mode-p 'org-mode))
  5621. outline-regexp
  5622. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5623. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5624. (not (looking-at org-outline-regexp))))
  5625. (org-cycle-hook
  5626. (if bob-special
  5627. (delq 'org-optimize-window-after-visibility-change
  5628. (copy-sequence org-cycle-hook))
  5629. org-cycle-hook))
  5630. (pos (point)))
  5631. (if (or bob-special (equal arg '(4)))
  5632. ;; special case: use global cycling
  5633. (setq arg t))
  5634. (cond
  5635. ((equal arg '(16))
  5636. (setq last-command 'dummy)
  5637. (org-set-startup-visibility)
  5638. (message "Startup visibility, plus VISIBILITY properties"))
  5639. ((equal arg '(64))
  5640. (show-all)
  5641. (message "Entire buffer visible, including drawers"))
  5642. ;; Table: enter it or move to the next field.
  5643. ((org-at-table-p 'any)
  5644. (if (org-at-table.el-p)
  5645. (message "Use C-c ' to edit table.el tables")
  5646. (if arg (org-table-edit-field t)
  5647. (org-table-justify-field-maybe)
  5648. (call-interactively 'org-table-next-field))))
  5649. ((run-hook-with-args-until-success
  5650. 'org-tab-after-check-for-table-hook))
  5651. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5652. ((eq arg t) (org-cycle-internal-global))
  5653. ;; Drawers: delegate to `org-flag-drawer'.
  5654. ((and org-drawers org-drawer-regexp
  5655. (save-excursion
  5656. (beginning-of-line 1)
  5657. (looking-at org-drawer-regexp)))
  5658. (org-flag-drawer ; toggle block visibility
  5659. (not (get-char-property (match-end 0) 'invisible))))
  5660. ;; Show-subtree, ARG levels up from here.
  5661. ((integerp arg)
  5662. (save-excursion
  5663. (org-back-to-heading)
  5664. (outline-up-heading (if (< arg 0) (- arg)
  5665. (- (funcall outline-level) arg)))
  5666. (org-show-subtree)))
  5667. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5668. ((and (featurep 'org-inlinetask)
  5669. (org-inlinetask-at-task-p)
  5670. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5671. (org-inlinetask-toggle-visibility))
  5672. ((org-try-cdlatex-tab))
  5673. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5674. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5675. (save-excursion (beginning-of-line 1)
  5676. (looking-at org-outline-regexp)))
  5677. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5678. (org-cycle-internal-local))
  5679. ;; From there: TAB emulation and template completion.
  5680. (buffer-read-only (org-back-to-heading))
  5681. ((run-hook-with-args-until-success
  5682. 'org-tab-after-check-for-cycling-hook))
  5683. ((org-try-structure-completion))
  5684. ((run-hook-with-args-until-success
  5685. 'org-tab-before-tab-emulation-hook))
  5686. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5687. (or (not (bolp))
  5688. (not (looking-at org-outline-regexp))))
  5689. (call-interactively (global-key-binding "\t")))
  5690. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5691. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5692. (or (and (eq org-cycle-emulate-tab 'white)
  5693. (= (match-end 0) (point-at-eol)))
  5694. (and (eq org-cycle-emulate-tab 'whitestart)
  5695. (>= (match-end 0) pos))))
  5696. t
  5697. (eq org-cycle-emulate-tab t))
  5698. (call-interactively (global-key-binding "\t")))
  5699. (t (save-excursion
  5700. (org-back-to-heading)
  5701. (org-cycle)))))))
  5702. (defun org-cycle-internal-global ()
  5703. "Do the global cycling action."
  5704. ;; Hack to avoid display of messages for .org attachments in Gnus
  5705. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5706. (cond
  5707. ((and (eq last-command this-command)
  5708. (eq org-cycle-global-status 'overview))
  5709. ;; We just created the overview - now do table of contents
  5710. ;; This can be slow in very large buffers, so indicate action
  5711. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5712. (unless ga (message "CONTENTS..."))
  5713. (org-content)
  5714. (unless ga (message "CONTENTS...done"))
  5715. (setq org-cycle-global-status 'contents)
  5716. (run-hook-with-args 'org-cycle-hook 'contents))
  5717. ((and (eq last-command this-command)
  5718. (eq org-cycle-global-status 'contents))
  5719. ;; We just showed the table of contents - now show everything
  5720. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5721. (show-all)
  5722. (unless ga (message "SHOW ALL"))
  5723. (setq org-cycle-global-status 'all)
  5724. (run-hook-with-args 'org-cycle-hook 'all))
  5725. (t
  5726. ;; Default action: go to overview
  5727. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5728. (org-overview)
  5729. (unless ga (message "OVERVIEW"))
  5730. (setq org-cycle-global-status 'overview)
  5731. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5732. (defun org-cycle-internal-local ()
  5733. "Do the local cycling action."
  5734. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5735. ;; First, determine end of headline (EOH), end of subtree or item
  5736. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5737. (save-excursion
  5738. (if (org-at-item-p)
  5739. (progn
  5740. (beginning-of-line)
  5741. (setq struct (org-list-struct))
  5742. (setq eoh (point-at-eol))
  5743. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5744. (setq has-children (org-list-has-child-p (point) struct)))
  5745. (org-back-to-heading)
  5746. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5747. (setq eos (save-excursion
  5748. (org-end-of-subtree t)
  5749. (unless (eobp)
  5750. (skip-chars-forward " \t\n"))
  5751. (if (eobp) (point) (1- (point)))))
  5752. (setq has-children
  5753. (or (save-excursion
  5754. (let ((level (funcall outline-level)))
  5755. (outline-next-heading)
  5756. (and (org-at-heading-p t)
  5757. (> (funcall outline-level) level))))
  5758. (save-excursion
  5759. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5760. ;; Determine end invisible part of buffer (EOL)
  5761. (beginning-of-line 2)
  5762. ;; XEmacs doesn't have `next-single-char-property-change'
  5763. (if (featurep 'xemacs)
  5764. (while (and (not (eobp)) ;; this is like `next-line'
  5765. (get-char-property (1- (point)) 'invisible))
  5766. (beginning-of-line 2))
  5767. (while (and (not (eobp)) ;; this is like `next-line'
  5768. (get-char-property (1- (point)) 'invisible))
  5769. (goto-char (next-single-char-property-change (point) 'invisible))
  5770. (and (eolp) (beginning-of-line 2))))
  5771. (setq eol (point)))
  5772. ;; Find out what to do next and set `this-command'
  5773. (cond
  5774. ((= eos eoh)
  5775. ;; Nothing is hidden behind this heading
  5776. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5777. (message "EMPTY ENTRY")
  5778. (setq org-cycle-subtree-status nil)
  5779. (save-excursion
  5780. (goto-char eos)
  5781. (outline-next-heading)
  5782. (if (outline-invisible-p) (org-flag-heading nil))))
  5783. ((and (or (>= eol eos)
  5784. (not (string-match "\\S-" (buffer-substring eol eos))))
  5785. (or has-children
  5786. (not (setq children-skipped
  5787. org-cycle-skip-children-state-if-no-children))))
  5788. ;; Entire subtree is hidden in one line: children view
  5789. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5790. (if (org-at-item-p)
  5791. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5792. (org-show-entry)
  5793. (org-with-limited-levels (show-children))
  5794. ;; FIXME: This slows down the func way too much.
  5795. ;; How keep drawers hidden in subtree anyway?
  5796. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5797. ;; (org-cycle-hide-drawers 'subtree))
  5798. ;; Fold every list in subtree to top-level items.
  5799. (when (eq org-cycle-include-plain-lists 'integrate)
  5800. (save-excursion
  5801. (org-back-to-heading)
  5802. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5803. (beginning-of-line 1)
  5804. (let* ((struct (org-list-struct))
  5805. (prevs (org-list-prevs-alist struct))
  5806. (end (org-list-get-bottom-point struct)))
  5807. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5808. (org-list-get-all-items (point) struct prevs))
  5809. (goto-char end))))))
  5810. (message "CHILDREN")
  5811. (save-excursion
  5812. (goto-char eos)
  5813. (outline-next-heading)
  5814. (if (outline-invisible-p) (org-flag-heading nil)))
  5815. (setq org-cycle-subtree-status 'children)
  5816. (run-hook-with-args 'org-cycle-hook 'children))
  5817. ((or children-skipped
  5818. (and (eq last-command this-command)
  5819. (eq org-cycle-subtree-status 'children)))
  5820. ;; We just showed the children, or no children are there,
  5821. ;; now show everything.
  5822. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5823. (outline-flag-region eoh eos nil)
  5824. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5825. (setq org-cycle-subtree-status 'subtree)
  5826. (run-hook-with-args 'org-cycle-hook 'subtree))
  5827. (t
  5828. ;; Default action: hide the subtree.
  5829. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5830. (outline-flag-region eoh eos t)
  5831. (message "FOLDED")
  5832. (setq org-cycle-subtree-status 'folded)
  5833. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5834. ;;;###autoload
  5835. (defun org-global-cycle (&optional arg)
  5836. "Cycle the global visibility. For details see `org-cycle'.
  5837. With \\[universal-argument] prefix arg, switch to startup visibility.
  5838. With a numeric prefix, show all headlines up to that level."
  5839. (interactive "P")
  5840. (let ((org-cycle-include-plain-lists
  5841. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5842. (cond
  5843. ((integerp arg)
  5844. (show-all)
  5845. (hide-sublevels arg)
  5846. (setq org-cycle-global-status 'contents))
  5847. ((equal arg '(4))
  5848. (org-set-startup-visibility)
  5849. (message "Startup visibility, plus VISIBILITY properties."))
  5850. (t
  5851. (org-cycle '(4))))))
  5852. (defun org-set-startup-visibility ()
  5853. "Set the visibility required by startup options and properties."
  5854. (cond
  5855. ((eq org-startup-folded t)
  5856. (org-cycle '(4)))
  5857. ((eq org-startup-folded 'content)
  5858. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5859. (org-cycle '(4)) (org-cycle '(4)))))
  5860. (unless (eq org-startup-folded 'showeverything)
  5861. (if org-hide-block-startup (org-hide-block-all))
  5862. (org-set-visibility-according-to-property 'no-cleanup)
  5863. (org-cycle-hide-archived-subtrees 'all)
  5864. (org-cycle-hide-drawers 'all)
  5865. (org-cycle-show-empty-lines t)))
  5866. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5867. "Switch subtree visibilities according to :VISIBILITY: property."
  5868. (interactive)
  5869. (let (org-show-entry-below state)
  5870. (save-excursion
  5871. (goto-char (point-min))
  5872. (while (re-search-forward
  5873. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5874. nil t)
  5875. (setq state (match-string 1))
  5876. (save-excursion
  5877. (org-back-to-heading t)
  5878. (hide-subtree)
  5879. (org-reveal)
  5880. (cond
  5881. ((equal state '("fold" "folded"))
  5882. (hide-subtree))
  5883. ((equal state "children")
  5884. (org-show-hidden-entry)
  5885. (show-children))
  5886. ((equal state "content")
  5887. (save-excursion
  5888. (save-restriction
  5889. (org-narrow-to-subtree)
  5890. (org-content))))
  5891. ((member state '("all" "showall"))
  5892. (show-subtree)))))
  5893. (unless no-cleanup
  5894. (org-cycle-hide-archived-subtrees 'all)
  5895. (org-cycle-hide-drawers 'all)
  5896. (org-cycle-show-empty-lines 'all)))))
  5897. ;; This function uses outline-regexp instead of the more fundamental
  5898. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5899. ;; buffers, where outline-regexp is needed.
  5900. (defun org-overview ()
  5901. "Switch to overview mode, showing only top-level headlines.
  5902. Really, this shows all headlines with level equal or greater than the level
  5903. of the first headline in the buffer. This is important, because if the
  5904. first headline is not level one, then (hide-sublevels 1) gives confusing
  5905. results."
  5906. (interactive)
  5907. (let ((level (save-excursion
  5908. (goto-char (point-min))
  5909. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5910. (progn
  5911. (goto-char (match-beginning 0))
  5912. (funcall outline-level))))))
  5913. (and level (hide-sublevels level))))
  5914. (defun org-content (&optional arg)
  5915. "Show all headlines in the buffer, like a table of contents.
  5916. With numerical argument N, show content up to level N."
  5917. (interactive "P")
  5918. (save-excursion
  5919. ;; Visit all headings and show their offspring
  5920. (and (integerp arg) (org-overview))
  5921. (goto-char (point-max))
  5922. (catch 'exit
  5923. (while (and (progn (condition-case nil
  5924. (outline-previous-visible-heading 1)
  5925. (error (goto-char (point-min))))
  5926. t)
  5927. (looking-at org-outline-regexp))
  5928. (if (integerp arg)
  5929. (show-children (1- arg))
  5930. (show-branches))
  5931. (if (bobp) (throw 'exit nil))))))
  5932. (defun org-optimize-window-after-visibility-change (state)
  5933. "Adjust the window after a change in outline visibility.
  5934. This function is the default value of the hook `org-cycle-hook'."
  5935. (when (get-buffer-window (current-buffer))
  5936. (cond
  5937. ((eq state 'content) nil)
  5938. ((eq state 'all) nil)
  5939. ((eq state 'folded) nil)
  5940. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5941. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5942. (defun org-remove-empty-overlays-at (pos)
  5943. "Remove outline overlays that do not contain non-white stuff."
  5944. (mapc
  5945. (lambda (o)
  5946. (and (eq 'outline (overlay-get o 'invisible))
  5947. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5948. (overlay-end o))))
  5949. (delete-overlay o)))
  5950. (overlays-at pos)))
  5951. (defun org-clean-visibility-after-subtree-move ()
  5952. "Fix visibility issues after moving a subtree."
  5953. ;; First, find a reasonable region to look at:
  5954. ;; Start two siblings above, end three below
  5955. (let* ((beg (save-excursion
  5956. (and (org-get-last-sibling)
  5957. (org-get-last-sibling))
  5958. (point)))
  5959. (end (save-excursion
  5960. (and (org-get-next-sibling)
  5961. (org-get-next-sibling)
  5962. (org-get-next-sibling))
  5963. (if (org-at-heading-p)
  5964. (point-at-eol)
  5965. (point))))
  5966. (level (looking-at "\\*+"))
  5967. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5968. (save-excursion
  5969. (save-restriction
  5970. (narrow-to-region beg end)
  5971. (when re
  5972. ;; Properly fold already folded siblings
  5973. (goto-char (point-min))
  5974. (while (re-search-forward re nil t)
  5975. (if (and (not (outline-invisible-p))
  5976. (save-excursion
  5977. (goto-char (point-at-eol)) (outline-invisible-p)))
  5978. (hide-entry))))
  5979. (org-cycle-show-empty-lines 'overview)
  5980. (org-cycle-hide-drawers 'overview)))))
  5981. (defun org-cycle-show-empty-lines (state)
  5982. "Show empty lines above all visible headlines.
  5983. The region to be covered depends on STATE when called through
  5984. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5985. entire buffer covered. Note that an empty line is only shown if there
  5986. are at least `org-cycle-separator-lines' empty lines before the headline."
  5987. (when (not (= org-cycle-separator-lines 0))
  5988. (save-excursion
  5989. (let* ((n (abs org-cycle-separator-lines))
  5990. (re (cond
  5991. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5992. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5993. (t (let ((ns (number-to-string (- n 2))))
  5994. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5995. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5996. beg end b e)
  5997. (cond
  5998. ((memq state '(overview contents t))
  5999. (setq beg (point-min) end (point-max)))
  6000. ((memq state '(children folded))
  6001. (setq beg (point) end (progn (org-end-of-subtree t t)
  6002. (beginning-of-line 2)
  6003. (point)))))
  6004. (when beg
  6005. (goto-char beg)
  6006. (while (re-search-forward re end t)
  6007. (unless (get-char-property (match-end 1) 'invisible)
  6008. (setq e (match-end 1))
  6009. (if (< org-cycle-separator-lines 0)
  6010. (setq b (save-excursion
  6011. (goto-char (match-beginning 0))
  6012. (org-back-over-empty-lines)
  6013. (if (save-excursion
  6014. (goto-char (max (point-min) (1- (point))))
  6015. (org-at-heading-p))
  6016. (1- (point))
  6017. (point))))
  6018. (setq b (match-beginning 1)))
  6019. (outline-flag-region b e nil)))))))
  6020. ;; Never hide empty lines at the end of the file.
  6021. (save-excursion
  6022. (goto-char (point-max))
  6023. (outline-previous-heading)
  6024. (outline-end-of-heading)
  6025. (if (and (looking-at "[ \t\n]+")
  6026. (= (match-end 0) (point-max)))
  6027. (outline-flag-region (point) (match-end 0) nil))))
  6028. (defun org-show-empty-lines-in-parent ()
  6029. "Move to the parent and re-show empty lines before visible headlines."
  6030. (save-excursion
  6031. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6032. (org-cycle-show-empty-lines context))))
  6033. (defun org-files-list ()
  6034. "Return `org-agenda-files' list, plus all open org-mode files.
  6035. This is useful for operations that need to scan all of a user's
  6036. open and agenda-wise Org files."
  6037. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6038. (dolist (buf (buffer-list))
  6039. (with-current-buffer buf
  6040. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6041. (let ((file (expand-file-name (buffer-file-name))))
  6042. (unless (member file files)
  6043. (push file files))))))
  6044. files))
  6045. (defsubst org-entry-beginning-position ()
  6046. "Return the beginning position of the current entry."
  6047. (save-excursion (outline-back-to-heading t) (point)))
  6048. (defsubst org-entry-end-position ()
  6049. "Return the end position of the current entry."
  6050. (save-excursion (outline-next-heading) (point)))
  6051. (defun org-cycle-hide-drawers (state)
  6052. "Re-hide all drawers after a visibility state change."
  6053. (when (and (derived-mode-p 'org-mode)
  6054. (not (memq state '(overview folded contents))))
  6055. (save-excursion
  6056. (let* ((globalp (memq state '(contents all)))
  6057. (beg (if globalp (point-min) (point)))
  6058. (end (if globalp (point-max)
  6059. (if (eq state 'children)
  6060. (save-excursion (outline-next-heading) (point))
  6061. (org-end-of-subtree t)))))
  6062. (goto-char beg)
  6063. (while (re-search-forward org-drawer-regexp end t)
  6064. (org-flag-drawer t))))))
  6065. (defun org-flag-drawer (flag)
  6066. "When FLAG is non-nil, hide the drawer we are within.
  6067. Otherwise make it visible."
  6068. (save-excursion
  6069. (beginning-of-line 1)
  6070. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6071. (let ((b (match-end 0)))
  6072. (if (re-search-forward
  6073. "^[ \t]*:END:"
  6074. (save-excursion (outline-next-heading) (point)) t)
  6075. (outline-flag-region b (point-at-eol) flag)
  6076. (error ":END: line missing at position %s" b))))))
  6077. (defun org-subtree-end-visible-p ()
  6078. "Is the end of the current subtree visible?"
  6079. (pos-visible-in-window-p
  6080. (save-excursion (org-end-of-subtree t) (point))))
  6081. (defun org-first-headline-recenter (&optional N)
  6082. "Move cursor to the first headline and recenter the headline.
  6083. Optional argument N means put the headline into the Nth line of the window."
  6084. (goto-char (point-min))
  6085. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6086. (beginning-of-line)
  6087. (recenter (prefix-numeric-value N))))
  6088. ;;; Saving and restoring visibility
  6089. (defun org-outline-overlay-data (&optional use-markers)
  6090. "Return a list of the locations of all outline overlays.
  6091. These are overlays with the `invisible' property value `outline'.
  6092. The return value is a list of cons cells, with start and stop
  6093. positions for each overlay.
  6094. If USE-MARKERS is set, return the positions as markers."
  6095. (let (beg end)
  6096. (save-excursion
  6097. (save-restriction
  6098. (widen)
  6099. (delq nil
  6100. (mapcar (lambda (o)
  6101. (when (eq (overlay-get o 'invisible) 'outline)
  6102. (setq beg (overlay-start o)
  6103. end (overlay-end o))
  6104. (and beg end (> end beg)
  6105. (if use-markers
  6106. (cons (move-marker (make-marker) beg)
  6107. (move-marker (make-marker) end))
  6108. (cons beg end)))))
  6109. (overlays-in (point-min) (point-max))))))))
  6110. (defun org-set-outline-overlay-data (data)
  6111. "Create visibility overlays for all positions in DATA.
  6112. DATA should have been made by `org-outline-overlay-data'."
  6113. (let (o)
  6114. (save-excursion
  6115. (save-restriction
  6116. (widen)
  6117. (show-all)
  6118. (mapc (lambda (c)
  6119. (outline-flag-region (car c) (cdr c) t))
  6120. data)))))
  6121. ;;; Folding of blocks
  6122. (defvar org-hide-block-overlays nil
  6123. "Overlays hiding blocks.")
  6124. (make-variable-buffer-local 'org-hide-block-overlays)
  6125. (defun org-block-map (function &optional start end)
  6126. "Call FUNCTION at the head of all source blocks in the current buffer.
  6127. Optional arguments START and END can be used to limit the range."
  6128. (let ((start (or start (point-min)))
  6129. (end (or end (point-max))))
  6130. (save-excursion
  6131. (goto-char start)
  6132. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6133. (save-excursion
  6134. (save-match-data
  6135. (goto-char (match-beginning 0))
  6136. (funcall function)))))))
  6137. (defun org-hide-block-toggle-all ()
  6138. "Toggle the visibility of all blocks in the current buffer."
  6139. (org-block-map #'org-hide-block-toggle))
  6140. (defun org-hide-block-all ()
  6141. "Fold all blocks in the current buffer."
  6142. (interactive)
  6143. (org-show-block-all)
  6144. (org-block-map #'org-hide-block-toggle-maybe))
  6145. (defun org-show-block-all ()
  6146. "Unfold all blocks in the current buffer."
  6147. (interactive)
  6148. (mapc 'delete-overlay org-hide-block-overlays)
  6149. (setq org-hide-block-overlays nil))
  6150. (defun org-hide-block-toggle-maybe ()
  6151. "Toggle visibility of block at point."
  6152. (interactive)
  6153. (let ((case-fold-search t))
  6154. (if (save-excursion
  6155. (beginning-of-line 1)
  6156. (looking-at org-block-regexp))
  6157. (progn (org-hide-block-toggle)
  6158. t) ;; to signal that we took action
  6159. nil))) ;; to signal that we did not
  6160. (defun org-hide-block-toggle (&optional force)
  6161. "Toggle the visibility of the current block."
  6162. (interactive)
  6163. (save-excursion
  6164. (beginning-of-line)
  6165. (if (re-search-forward org-block-regexp nil t)
  6166. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6167. (end (match-end 0)) ;; end of entire body
  6168. ov)
  6169. (if (memq t (mapcar (lambda (overlay)
  6170. (eq (overlay-get overlay 'invisible)
  6171. 'org-hide-block))
  6172. (overlays-at start)))
  6173. (if (or (not force) (eq force 'off))
  6174. (mapc (lambda (ov)
  6175. (when (member ov org-hide-block-overlays)
  6176. (setq org-hide-block-overlays
  6177. (delq ov org-hide-block-overlays)))
  6178. (when (eq (overlay-get ov 'invisible)
  6179. 'org-hide-block)
  6180. (delete-overlay ov)))
  6181. (overlays-at start)))
  6182. (setq ov (make-overlay start end))
  6183. (overlay-put ov 'invisible 'org-hide-block)
  6184. ;; make the block accessible to isearch
  6185. (overlay-put
  6186. ov 'isearch-open-invisible
  6187. (lambda (ov)
  6188. (when (member ov org-hide-block-overlays)
  6189. (setq org-hide-block-overlays
  6190. (delq ov org-hide-block-overlays)))
  6191. (when (eq (overlay-get ov 'invisible)
  6192. 'org-hide-block)
  6193. (delete-overlay ov))))
  6194. (push ov org-hide-block-overlays)))
  6195. (error "Not looking at a source block"))))
  6196. ;; org-tab-after-check-for-cycling-hook
  6197. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6198. ;; Remove overlays when changing major mode
  6199. (add-hook 'org-mode-hook
  6200. (lambda () (org-add-hook 'change-major-mode-hook
  6201. 'org-show-block-all 'append 'local)))
  6202. ;;; Org-goto
  6203. (defvar org-goto-window-configuration nil)
  6204. (defvar org-goto-marker nil)
  6205. (defvar org-goto-map
  6206. (let ((map (make-sparse-keymap)))
  6207. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6208. (while (setq cmd (pop cmds))
  6209. (substitute-key-definition cmd cmd map global-map)))
  6210. (suppress-keymap map)
  6211. (org-defkey map "\C-m" 'org-goto-ret)
  6212. (org-defkey map [(return)] 'org-goto-ret)
  6213. (org-defkey map [(left)] 'org-goto-left)
  6214. (org-defkey map [(right)] 'org-goto-right)
  6215. (org-defkey map [(control ?g)] 'org-goto-quit)
  6216. (org-defkey map "\C-i" 'org-cycle)
  6217. (org-defkey map [(tab)] 'org-cycle)
  6218. (org-defkey map [(down)] 'outline-next-visible-heading)
  6219. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6220. (if org-goto-auto-isearch
  6221. (if (fboundp 'define-key-after)
  6222. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6223. nil)
  6224. (org-defkey map "q" 'org-goto-quit)
  6225. (org-defkey map "n" 'outline-next-visible-heading)
  6226. (org-defkey map "p" 'outline-previous-visible-heading)
  6227. (org-defkey map "f" 'outline-forward-same-level)
  6228. (org-defkey map "b" 'outline-backward-same-level)
  6229. (org-defkey map "u" 'outline-up-heading))
  6230. (org-defkey map "/" 'org-occur)
  6231. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6232. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6233. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6234. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6235. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6236. map))
  6237. (defconst org-goto-help
  6238. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6239. RET=jump to location [Q]uit and return to previous location
  6240. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6241. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6242. ;; FIXME: Docstring does not mention both interfaces
  6243. (defun org-goto (&optional alternative-interface)
  6244. "Look up a different location in the current file, keeping current visibility.
  6245. When you want look-up or go to a different location in a
  6246. document, the fastest way is often to fold the entire buffer and
  6247. then dive into the tree. This method has the disadvantage, that
  6248. the previous location will be folded, which may not be what you
  6249. want.
  6250. This command works around this by showing a copy of the current
  6251. buffer in an indirect buffer, in overview mode. You can dive
  6252. into the tree in that copy, use org-occur and incremental search
  6253. to find a location. When pressing RET or `Q', the command
  6254. returns to the original buffer in which the visibility is still
  6255. unchanged. After RET it will also jump to the location selected
  6256. in the indirect buffer and expose the headline hierarchy above.
  6257. With a prefix argument, use the alternative interface: e.g. if
  6258. `org-goto-interface' is 'outline use 'outline-path-completion."
  6259. (interactive "P")
  6260. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6261. (org-refile-use-outline-path t)
  6262. (org-refile-target-verify-function nil)
  6263. (interface
  6264. (if (not alternative-interface)
  6265. org-goto-interface
  6266. (if (eq org-goto-interface 'outline)
  6267. 'outline-path-completion
  6268. 'outline)))
  6269. (org-goto-start-pos (point))
  6270. (selected-point
  6271. (if (eq interface 'outline)
  6272. (car (org-get-location (current-buffer) org-goto-help))
  6273. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6274. (org-refile-check-position pa)
  6275. (nth 3 pa)))))
  6276. (if selected-point
  6277. (progn
  6278. (org-mark-ring-push org-goto-start-pos)
  6279. (goto-char selected-point)
  6280. (if (or (outline-invisible-p) (org-invisible-p2))
  6281. (org-show-context 'org-goto)))
  6282. (message "Quit"))))
  6283. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6284. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6285. (defvar org-goto-local-auto-isearch-map) ; defined below
  6286. (defun org-get-location (buf help)
  6287. "Let the user select a location in the Org-mode buffer BUF.
  6288. This function uses a recursive edit. It returns the selected position
  6289. or nil."
  6290. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6291. (isearch-hide-immediately nil)
  6292. (isearch-search-fun-function
  6293. (lambda () 'org-goto-local-search-headings))
  6294. (org-goto-selected-point org-goto-exit-command)
  6295. (pop-up-frames nil)
  6296. (special-display-buffer-names nil)
  6297. (special-display-regexps nil)
  6298. (special-display-function nil))
  6299. (save-excursion
  6300. (save-window-excursion
  6301. (delete-other-windows)
  6302. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6303. (org-pop-to-buffer-same-window
  6304. (condition-case nil
  6305. (make-indirect-buffer (current-buffer) "*org-goto*")
  6306. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6307. (with-output-to-temp-buffer "*Help*"
  6308. (princ help))
  6309. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6310. (setq buffer-read-only nil)
  6311. (let ((org-startup-truncated t)
  6312. (org-startup-folded nil)
  6313. (org-startup-align-all-tables nil))
  6314. (org-mode)
  6315. (org-overview))
  6316. (setq buffer-read-only t)
  6317. (if (and (boundp 'org-goto-start-pos)
  6318. (integer-or-marker-p org-goto-start-pos))
  6319. (let ((org-show-hierarchy-above t)
  6320. (org-show-siblings t)
  6321. (org-show-following-heading t))
  6322. (goto-char org-goto-start-pos)
  6323. (and (outline-invisible-p) (org-show-context)))
  6324. (goto-char (point-min)))
  6325. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6326. (message "Select location and press RET")
  6327. (use-local-map org-goto-map)
  6328. (recursive-edit)
  6329. ))
  6330. (kill-buffer "*org-goto*")
  6331. (cons org-goto-selected-point org-goto-exit-command)))
  6332. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6333. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6334. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6335. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6336. (defun org-goto-local-search-headings (string bound noerror)
  6337. "Search and make sure that any matches are in headlines."
  6338. (catch 'return
  6339. (while (if isearch-forward
  6340. (search-forward string bound noerror)
  6341. (search-backward string bound noerror))
  6342. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6343. (and (member :headline context)
  6344. (not (member :tags context))))
  6345. (throw 'return (point))))))
  6346. (defun org-goto-local-auto-isearch ()
  6347. "Start isearch."
  6348. (interactive)
  6349. (goto-char (point-min))
  6350. (let ((keys (this-command-keys)))
  6351. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6352. (isearch-mode t)
  6353. (isearch-process-search-char (string-to-char keys)))))
  6354. (defun org-goto-ret (&optional arg)
  6355. "Finish `org-goto' by going to the new location."
  6356. (interactive "P")
  6357. (setq org-goto-selected-point (point)
  6358. org-goto-exit-command 'return)
  6359. (throw 'exit nil))
  6360. (defun org-goto-left ()
  6361. "Finish `org-goto' by going to the new location."
  6362. (interactive)
  6363. (if (org-at-heading-p)
  6364. (progn
  6365. (beginning-of-line 1)
  6366. (setq org-goto-selected-point (point)
  6367. org-goto-exit-command 'left)
  6368. (throw 'exit nil))
  6369. (error "Not on a heading")))
  6370. (defun org-goto-right ()
  6371. "Finish `org-goto' by going to the new location."
  6372. (interactive)
  6373. (if (org-at-heading-p)
  6374. (progn
  6375. (setq org-goto-selected-point (point)
  6376. org-goto-exit-command 'right)
  6377. (throw 'exit nil))
  6378. (error "Not on a heading")))
  6379. (defun org-goto-quit ()
  6380. "Finish `org-goto' without cursor motion."
  6381. (interactive)
  6382. (setq org-goto-selected-point nil)
  6383. (setq org-goto-exit-command 'quit)
  6384. (throw 'exit nil))
  6385. ;;; Indirect buffer display of subtrees
  6386. (defvar org-indirect-dedicated-frame nil
  6387. "This is the frame being used for indirect tree display.")
  6388. (defvar org-last-indirect-buffer nil)
  6389. (defun org-tree-to-indirect-buffer (&optional arg)
  6390. "Create indirect buffer and narrow it to current subtree.
  6391. With numerical prefix ARG, go up to this level and then take that tree.
  6392. If ARG is negative, go up that many levels.
  6393. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6394. indirect buffer previously made with this command, to avoid proliferation of
  6395. indirect buffers. However, when you call the command with a \
  6396. \\[universal-argument] prefix, or
  6397. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6398. is kept so that you can work with several indirect buffers at the same time.
  6399. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6400. \\[universal-argument] prefix also
  6401. requests that a new frame be made for the new buffer, so that the dedicated
  6402. frame is not changed."
  6403. (interactive "P")
  6404. (let ((cbuf (current-buffer))
  6405. (cwin (selected-window))
  6406. (pos (point))
  6407. beg end level heading ibuf)
  6408. (save-excursion
  6409. (org-back-to-heading t)
  6410. (when (numberp arg)
  6411. (setq level (org-outline-level))
  6412. (if (< arg 0) (setq arg (+ level arg)))
  6413. (while (> (setq level (org-outline-level)) arg)
  6414. (outline-up-heading 1 t)))
  6415. (setq beg (point)
  6416. heading (org-get-heading))
  6417. (org-end-of-subtree t t)
  6418. (if (org-at-heading-p) (backward-char 1))
  6419. (setq end (point)))
  6420. (if (and (buffer-live-p org-last-indirect-buffer)
  6421. (not (eq org-indirect-buffer-display 'new-frame))
  6422. (not arg))
  6423. (kill-buffer org-last-indirect-buffer))
  6424. (setq ibuf (org-get-indirect-buffer cbuf)
  6425. org-last-indirect-buffer ibuf)
  6426. (cond
  6427. ((or (eq org-indirect-buffer-display 'new-frame)
  6428. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6429. (select-frame (make-frame))
  6430. (delete-other-windows)
  6431. (org-pop-to-buffer-same-window ibuf)
  6432. (org-set-frame-title heading))
  6433. ((eq org-indirect-buffer-display 'dedicated-frame)
  6434. (raise-frame
  6435. (select-frame (or (and org-indirect-dedicated-frame
  6436. (frame-live-p org-indirect-dedicated-frame)
  6437. org-indirect-dedicated-frame)
  6438. (setq org-indirect-dedicated-frame (make-frame)))))
  6439. (delete-other-windows)
  6440. (org-pop-to-buffer-same-window ibuf)
  6441. (org-set-frame-title (concat "Indirect: " heading)))
  6442. ((eq org-indirect-buffer-display 'current-window)
  6443. (org-pop-to-buffer-same-window ibuf))
  6444. ((eq org-indirect-buffer-display 'other-window)
  6445. (pop-to-buffer ibuf))
  6446. (t (error "Invalid value")))
  6447. (if (featurep 'xemacs)
  6448. (save-excursion (org-mode) (turn-on-font-lock)))
  6449. (narrow-to-region beg end)
  6450. (show-all)
  6451. (goto-char pos)
  6452. (run-hook-with-args 'org-cycle-hook 'all)
  6453. (and (window-live-p cwin) (select-window cwin))))
  6454. (defun org-get-indirect-buffer (&optional buffer)
  6455. (setq buffer (or buffer (current-buffer)))
  6456. (let ((n 1) (base (buffer-name buffer)) bname)
  6457. (while (buffer-live-p
  6458. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6459. (setq n (1+ n)))
  6460. (condition-case nil
  6461. (make-indirect-buffer buffer bname 'clone)
  6462. (error (make-indirect-buffer buffer bname)))))
  6463. (defun org-set-frame-title (title)
  6464. "Set the title of the current frame to the string TITLE."
  6465. ;; FIXME: how to name a single frame in XEmacs???
  6466. (unless (featurep 'xemacs)
  6467. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6468. ;;;; Structure editing
  6469. ;;; Inserting headlines
  6470. (defun org-previous-line-empty-p ()
  6471. (save-excursion
  6472. (and (not (bobp))
  6473. (or (beginning-of-line 0) t)
  6474. (save-match-data
  6475. (looking-at "[ \t]*$")))))
  6476. (defun org-insert-heading (&optional force-heading invisible-ok)
  6477. "Insert a new heading or item with same depth at point.
  6478. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6479. If point is at the beginning of a headline, insert a sibling before the
  6480. current headline. If point is not at the beginning, split the line,
  6481. create the new headline with the text in the current line after point
  6482. \(but see also the variable `org-M-RET-may-split-line').
  6483. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6484. This is important for non-interactive uses of the command."
  6485. (interactive "P")
  6486. (if (or (= (buffer-size) 0)
  6487. (and (not (save-excursion
  6488. (and (ignore-errors (org-back-to-heading invisible-ok))
  6489. (org-at-heading-p))))
  6490. (or force-heading (not (org-in-item-p)))))
  6491. (progn
  6492. (insert "\n* ")
  6493. (run-hooks 'org-insert-heading-hook))
  6494. (when (or force-heading (not (org-insert-item)))
  6495. (let* ((empty-line-p nil)
  6496. (level nil)
  6497. (on-heading (org-at-heading-p))
  6498. (head (save-excursion
  6499. (condition-case nil
  6500. (progn
  6501. (org-back-to-heading invisible-ok)
  6502. (when (and (not on-heading)
  6503. (featurep 'org-inlinetask)
  6504. (integerp org-inlinetask-min-level)
  6505. (>= (length (match-string 0))
  6506. org-inlinetask-min-level))
  6507. ;; Find a heading level before the inline task
  6508. (while (and (setq level (org-up-heading-safe))
  6509. (>= level org-inlinetask-min-level)))
  6510. (if (org-at-heading-p)
  6511. (org-back-to-heading invisible-ok)
  6512. (error "This should not happen")))
  6513. (setq empty-line-p (org-previous-line-empty-p))
  6514. (match-string 0))
  6515. (error "*"))))
  6516. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6517. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6518. pos hide-previous previous-pos)
  6519. (cond
  6520. ((and (org-at-heading-p) (bolp)
  6521. (or (bobp)
  6522. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6523. ;; insert before the current line
  6524. (open-line (if blank 2 1)))
  6525. ((and (bolp)
  6526. (not org-insert-heading-respect-content)
  6527. (or (bobp)
  6528. (save-excursion
  6529. (backward-char 1) (not (outline-invisible-p)))))
  6530. ;; insert right here
  6531. nil)
  6532. (t
  6533. ;; somewhere in the line
  6534. (save-excursion
  6535. (setq previous-pos (point-at-bol))
  6536. (end-of-line)
  6537. (setq hide-previous (outline-invisible-p)))
  6538. (and org-insert-heading-respect-content (org-show-subtree))
  6539. (let ((split
  6540. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6541. (save-excursion
  6542. (let ((p (point)))
  6543. (goto-char (point-at-bol))
  6544. (and (looking-at org-complex-heading-regexp)
  6545. (match-beginning 4)
  6546. (> p (match-beginning 4)))))))
  6547. tags pos)
  6548. (cond
  6549. (org-insert-heading-respect-content
  6550. (org-end-of-subtree nil t)
  6551. (when (featurep 'org-inlinetask)
  6552. (while (and (not (eobp))
  6553. (looking-at "\\(\\*+\\)[ \t]+")
  6554. (>= (length (match-string 1))
  6555. org-inlinetask-min-level))
  6556. (org-end-of-subtree nil t)))
  6557. (or (bolp) (newline))
  6558. (or (org-previous-line-empty-p)
  6559. (and blank (newline)))
  6560. (open-line 1))
  6561. ((org-at-heading-p)
  6562. (when hide-previous
  6563. (show-children)
  6564. (org-show-entry))
  6565. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6566. (setq tags (and (match-end 2) (match-string 2)))
  6567. (and (match-end 1)
  6568. (delete-region (match-beginning 1) (match-end 1)))
  6569. (setq pos (point-at-bol))
  6570. (or split (end-of-line 1))
  6571. (delete-horizontal-space)
  6572. (if (string-match "\\`\\*+\\'"
  6573. (buffer-substring (point-at-bol) (point)))
  6574. (insert " "))
  6575. (newline (if blank 2 1))
  6576. (when tags
  6577. (save-excursion
  6578. (goto-char pos)
  6579. (end-of-line 1)
  6580. (insert " " tags)
  6581. (org-set-tags nil 'align))))
  6582. (t
  6583. (or split (end-of-line 1))
  6584. (newline (if blank 2 1)))))))
  6585. (insert head) (just-one-space)
  6586. (setq pos (point))
  6587. (end-of-line 1)
  6588. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6589. (when (and org-insert-heading-respect-content hide-previous)
  6590. (save-excursion
  6591. (goto-char previous-pos)
  6592. (hide-subtree)))
  6593. (run-hooks 'org-insert-heading-hook)))))
  6594. (defun org-get-heading (&optional no-tags no-todo)
  6595. "Return the heading of the current entry, without the stars.
  6596. When NO-TAGS is non-nil, don't include tags.
  6597. When NO-TODO is non-nil, don't include TODO keywords."
  6598. (save-excursion
  6599. (org-back-to-heading t)
  6600. (cond
  6601. ((and no-tags no-todo)
  6602. (looking-at org-complex-heading-regexp)
  6603. (match-string 4))
  6604. (no-tags
  6605. (looking-at (concat org-outline-regexp
  6606. "\\(.*?\\)"
  6607. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6608. (match-string 1))
  6609. (no-todo
  6610. (looking-at org-todo-line-regexp)
  6611. (match-string 3))
  6612. (t (looking-at org-heading-regexp)
  6613. (match-string 2)))))
  6614. (defun org-heading-components ()
  6615. "Return the components of the current heading.
  6616. This is a list with the following elements:
  6617. - the level as an integer
  6618. - the reduced level, different if `org-odd-levels-only' is set.
  6619. - the TODO keyword, or nil
  6620. - the priority character, like ?A, or nil if no priority is given
  6621. - the headline text itself, or the tags string if no headline text
  6622. - the tags string, or nil."
  6623. (save-excursion
  6624. (org-back-to-heading t)
  6625. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6626. (list (length (match-string 1))
  6627. (org-reduced-level (length (match-string 1)))
  6628. (org-match-string-no-properties 2)
  6629. (and (match-end 3) (aref (match-string 3) 2))
  6630. (org-match-string-no-properties 4)
  6631. (org-match-string-no-properties 5)))))
  6632. (defun org-get-entry ()
  6633. "Get the entry text, after heading, entire subtree."
  6634. (save-excursion
  6635. (org-back-to-heading t)
  6636. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6637. (defun org-insert-heading-after-current ()
  6638. "Insert a new heading with same level as current, after current subtree."
  6639. (interactive)
  6640. (org-back-to-heading)
  6641. (org-insert-heading)
  6642. (org-move-subtree-down)
  6643. (end-of-line 1))
  6644. (defun org-insert-heading-respect-content ()
  6645. (interactive)
  6646. (let ((org-insert-heading-respect-content t))
  6647. (org-insert-heading t)))
  6648. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6649. (interactive "P")
  6650. (let ((org-insert-heading-respect-content t))
  6651. (org-insert-todo-heading force-state t)))
  6652. (defun org-insert-todo-heading (arg &optional force-heading)
  6653. "Insert a new heading with the same level and TODO state as current heading.
  6654. If the heading has no TODO state, or if the state is DONE, use the first
  6655. state (TODO by default). Also with prefix arg, force first state."
  6656. (interactive "P")
  6657. (when (or force-heading (not (org-insert-item 'checkbox)))
  6658. (org-insert-heading force-heading)
  6659. (save-excursion
  6660. (org-back-to-heading)
  6661. (outline-previous-heading)
  6662. (looking-at org-todo-line-regexp))
  6663. (let*
  6664. ((new-mark-x
  6665. (if (or arg
  6666. (not (match-beginning 2))
  6667. (member (match-string 2) org-done-keywords))
  6668. (car org-todo-keywords-1)
  6669. (match-string 2)))
  6670. (new-mark
  6671. (or
  6672. (run-hook-with-args-until-success
  6673. 'org-todo-get-default-hook new-mark-x nil)
  6674. new-mark-x)))
  6675. (beginning-of-line 1)
  6676. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6677. (if org-treat-insert-todo-heading-as-state-change
  6678. (org-todo new-mark)
  6679. (insert new-mark " "))))
  6680. (when org-provide-todo-statistics
  6681. (org-update-parent-todo-statistics))))
  6682. (defun org-insert-subheading (arg)
  6683. "Insert a new subheading and demote it.
  6684. Works for outline headings and for plain lists alike."
  6685. (interactive "P")
  6686. (org-insert-heading arg)
  6687. (cond
  6688. ((org-at-heading-p) (org-do-demote))
  6689. ((org-at-item-p) (org-indent-item))))
  6690. (defun org-insert-todo-subheading (arg)
  6691. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6692. Works for outline headings and for plain lists alike."
  6693. (interactive "P")
  6694. (org-insert-todo-heading arg)
  6695. (cond
  6696. ((org-at-heading-p) (org-do-demote))
  6697. ((org-at-item-p) (org-indent-item))))
  6698. ;;; Promotion and Demotion
  6699. (defvar org-after-demote-entry-hook nil
  6700. "Hook run after an entry has been demoted.
  6701. The cursor will be at the beginning of the entry.
  6702. When a subtree is being demoted, the hook will be called for each node.")
  6703. (defvar org-after-promote-entry-hook nil
  6704. "Hook run after an entry has been promoted.
  6705. The cursor will be at the beginning of the entry.
  6706. When a subtree is being promoted, the hook will be called for each node.")
  6707. (defun org-promote-subtree ()
  6708. "Promote the entire subtree.
  6709. See also `org-promote'."
  6710. (interactive)
  6711. (save-excursion
  6712. (org-with-limited-levels (org-map-tree 'org-promote)))
  6713. (org-fix-position-after-promote))
  6714. (defun org-demote-subtree ()
  6715. "Demote the entire subtree. See `org-demote'.
  6716. See also `org-promote'."
  6717. (interactive)
  6718. (save-excursion
  6719. (org-with-limited-levels (org-map-tree 'org-demote)))
  6720. (org-fix-position-after-promote))
  6721. (defun org-do-promote ()
  6722. "Promote the current heading higher up the tree.
  6723. If the region is active in `transient-mark-mode', promote all headings
  6724. in the region."
  6725. (interactive)
  6726. (save-excursion
  6727. (if (org-region-active-p)
  6728. (org-map-region 'org-promote (region-beginning) (region-end))
  6729. (org-promote)))
  6730. (org-fix-position-after-promote))
  6731. (defun org-do-demote ()
  6732. "Demote the current heading lower down the tree.
  6733. If the region is active in `transient-mark-mode', demote all headings
  6734. in the region."
  6735. (interactive)
  6736. (save-excursion
  6737. (if (org-region-active-p)
  6738. (org-map-region 'org-demote (region-beginning) (region-end))
  6739. (org-demote)))
  6740. (org-fix-position-after-promote))
  6741. (defun org-fix-position-after-promote ()
  6742. "Make sure that after pro/demotion cursor position is right."
  6743. (let ((pos (point)))
  6744. (when (save-excursion
  6745. (beginning-of-line 1)
  6746. (looking-at org-todo-line-regexp)
  6747. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6748. (cond ((eobp) (insert " "))
  6749. ((eolp) (insert " "))
  6750. ((equal (char-after) ?\ ) (forward-char 1))))))
  6751. (defun org-current-level ()
  6752. "Return the level of the current entry, or nil if before the first headline.
  6753. The level is the number of stars at the beginning of the headline."
  6754. (save-excursion
  6755. (org-with-limited-levels
  6756. (if (ignore-errors (org-back-to-heading t))
  6757. (funcall outline-level)))))
  6758. (defun org-get-previous-line-level ()
  6759. "Return the outline depth of the last headline before the current line.
  6760. Returns 0 for the first headline in the buffer, and nil if before the
  6761. first headline."
  6762. (let ((current-level (org-current-level))
  6763. (prev-level (when (> (line-number-at-pos) 1)
  6764. (save-excursion
  6765. (beginning-of-line 0)
  6766. (org-current-level)))))
  6767. (cond ((null current-level) nil) ; Before first headline
  6768. ((null prev-level) 0) ; At first headline
  6769. (prev-level))))
  6770. (defun org-reduced-level (l)
  6771. "Compute the effective level of a heading.
  6772. This takes into account the setting of `org-odd-levels-only'."
  6773. (cond
  6774. ((zerop l) 0)
  6775. (org-odd-levels-only (1+ (floor (/ l 2))))
  6776. (t l)))
  6777. (defun org-level-increment ()
  6778. "Return the number of stars that will be added or removed at a
  6779. time to headlines when structure editing, based on the value of
  6780. `org-odd-levels-only'."
  6781. (if org-odd-levels-only 2 1))
  6782. (defun org-get-valid-level (level &optional change)
  6783. "Rectify a level change under the influence of `org-odd-levels-only'
  6784. LEVEL is a current level, CHANGE is by how much the level should be
  6785. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6786. even level numbers will become the next higher odd number."
  6787. (if org-odd-levels-only
  6788. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6789. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6790. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6791. (max 1 (+ level (or change 0)))))
  6792. (if (boundp 'define-obsolete-function-alias)
  6793. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6794. (define-obsolete-function-alias 'org-get-legal-level
  6795. 'org-get-valid-level)
  6796. (define-obsolete-function-alias 'org-get-legal-level
  6797. 'org-get-valid-level "23.1")))
  6798. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6799. ;; ̀org-with-limited-levels'
  6800. (defun org-promote ()
  6801. "Promote the current heading higher up the tree.
  6802. If the region is active in `transient-mark-mode', promote all headings
  6803. in the region."
  6804. (org-back-to-heading t)
  6805. (let* ((level (save-match-data (funcall outline-level)))
  6806. (after-change-functions (remove 'flyspell-after-change-function
  6807. after-change-functions))
  6808. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6809. (diff (abs (- level (length up-head) -1))))
  6810. (cond ((and (= level 1) org-called-with-limited-levels
  6811. org-allow-promoting-top-level-subtree)
  6812. (replace-match "# " nil t))
  6813. ((= level 1)
  6814. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6815. (t (replace-match up-head nil t)))
  6816. ;; Fixup tag positioning
  6817. (unless (= level 1)
  6818. (and org-auto-align-tags (org-set-tags nil t))
  6819. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6820. (run-hooks 'org-after-promote-entry-hook)))
  6821. (defun org-demote ()
  6822. "Demote the current heading lower down the tree.
  6823. If the region is active in `transient-mark-mode', demote all headings
  6824. in the region."
  6825. (org-back-to-heading t)
  6826. (let* ((level (save-match-data (funcall outline-level)))
  6827. (after-change-functions (remove 'flyspell-after-change-function
  6828. after-change-functions))
  6829. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6830. (diff (abs (- level (length down-head) -1))))
  6831. (replace-match down-head nil t)
  6832. ;; Fixup tag positioning
  6833. (and org-auto-align-tags (org-set-tags nil t))
  6834. (if org-adapt-indentation (org-fixup-indentation diff))
  6835. (run-hooks 'org-after-demote-entry-hook)))
  6836. (defun org-cycle-level ()
  6837. "Cycle the level of an empty headline through possible states.
  6838. This goes first to child, then to parent, level, then up the hierarchy.
  6839. After top level, it switches back to sibling level."
  6840. (interactive)
  6841. (let ((org-adapt-indentation nil))
  6842. (when (org-point-at-end-of-empty-headline)
  6843. (setq this-command 'org-cycle-level) ; Only needed for caching
  6844. (let ((cur-level (org-current-level))
  6845. (prev-level (org-get-previous-line-level)))
  6846. (cond
  6847. ;; If first headline in file, promote to top-level.
  6848. ((= prev-level 0)
  6849. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6850. do (org-do-promote)))
  6851. ;; If same level as prev, demote one.
  6852. ((= prev-level cur-level)
  6853. (org-do-demote))
  6854. ;; If parent is top-level, promote to top level if not already.
  6855. ((= prev-level 1)
  6856. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6857. do (org-do-promote)))
  6858. ;; If top-level, return to prev-level.
  6859. ((= cur-level 1)
  6860. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6861. do (org-do-demote)))
  6862. ;; If less than prev-level, promote one.
  6863. ((< cur-level prev-level)
  6864. (org-do-promote))
  6865. ;; If deeper than prev-level, promote until higher than
  6866. ;; prev-level.
  6867. ((> cur-level prev-level)
  6868. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6869. do (org-do-promote))))
  6870. t))))
  6871. (defun org-map-tree (fun)
  6872. "Call FUN for every heading underneath the current one."
  6873. (org-back-to-heading)
  6874. (let ((level (funcall outline-level)))
  6875. (save-excursion
  6876. (funcall fun)
  6877. (while (and (progn
  6878. (outline-next-heading)
  6879. (> (funcall outline-level) level))
  6880. (not (eobp)))
  6881. (funcall fun)))))
  6882. (defun org-map-region (fun beg end)
  6883. "Call FUN for every heading between BEG and END."
  6884. (let ((org-ignore-region t))
  6885. (save-excursion
  6886. (setq end (copy-marker end))
  6887. (goto-char beg)
  6888. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6889. (< (point) end))
  6890. (funcall fun))
  6891. (while (and (progn
  6892. (outline-next-heading)
  6893. (< (point) end))
  6894. (not (eobp)))
  6895. (funcall fun)))))
  6896. (defvar org-property-end-re) ; silence byte-compiler
  6897. (defun org-fixup-indentation (diff)
  6898. "Change the indentation in the current entry by DIFF.
  6899. However, if any line in the current entry has no indentation, or if it
  6900. would end up with no indentation after the change, nothing at all is done."
  6901. (save-excursion
  6902. (let ((end (save-excursion (outline-next-heading)
  6903. (point-marker)))
  6904. (prohibit (if (> diff 0)
  6905. "^\\S-"
  6906. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6907. col)
  6908. (unless (save-excursion (end-of-line 1)
  6909. (re-search-forward prohibit end t))
  6910. (while (and (< (point) end)
  6911. (re-search-forward "^[ \t]+" end t))
  6912. (goto-char (match-end 0))
  6913. (setq col (current-column))
  6914. (if (< diff 0) (replace-match ""))
  6915. (org-indent-to-column (+ diff col))))
  6916. (move-marker end nil))))
  6917. (defun org-convert-to-odd-levels ()
  6918. "Convert an org-mode file with all levels allowed to one with odd levels.
  6919. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6920. level 5 etc."
  6921. (interactive)
  6922. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6923. (let ((outline-level 'org-outline-level)
  6924. (org-odd-levels-only nil) n)
  6925. (save-excursion
  6926. (goto-char (point-min))
  6927. (while (re-search-forward "^\\*\\*+ " nil t)
  6928. (setq n (- (length (match-string 0)) 2))
  6929. (while (>= (setq n (1- n)) 0)
  6930. (org-demote))
  6931. (end-of-line 1))))))
  6932. (defun org-convert-to-oddeven-levels ()
  6933. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6934. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6935. file contains a section with an even level, conversion would
  6936. destroy the structure of the file. An error is signaled in this
  6937. case."
  6938. (interactive)
  6939. (goto-char (point-min))
  6940. ;; First check if there are no even levels
  6941. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6942. (org-show-context t)
  6943. (error "Not all levels are odd in this file. Conversion not possible"))
  6944. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6945. (let ((outline-regexp org-outline-regexp)
  6946. (outline-level 'org-outline-level)
  6947. (org-odd-levels-only nil) n)
  6948. (save-excursion
  6949. (goto-char (point-min))
  6950. (while (re-search-forward "^\\*\\*+ " nil t)
  6951. (setq n (/ (1- (length (match-string 0))) 2))
  6952. (while (>= (setq n (1- n)) 0)
  6953. (org-promote))
  6954. (end-of-line 1))))))
  6955. (defun org-tr-level (n)
  6956. "Make N odd if required."
  6957. (if org-odd-levels-only (1+ (/ n 2)) n))
  6958. ;;; Vertical tree motion, cutting and pasting of subtrees
  6959. (defun org-move-subtree-up (&optional arg)
  6960. "Move the current subtree up past ARG headlines of the same level."
  6961. (interactive "p")
  6962. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6963. (defun org-move-subtree-down (&optional arg)
  6964. "Move the current subtree down past ARG headlines of the same level."
  6965. (interactive "p")
  6966. (setq arg (prefix-numeric-value arg))
  6967. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6968. 'org-get-last-sibling))
  6969. (ins-point (make-marker))
  6970. (cnt (abs arg))
  6971. (col (current-column))
  6972. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6973. ;; Select the tree
  6974. (org-back-to-heading)
  6975. (setq beg0 (point))
  6976. (save-excursion
  6977. (setq ne-beg (org-back-over-empty-lines))
  6978. (setq beg (point)))
  6979. (save-match-data
  6980. (save-excursion (outline-end-of-heading)
  6981. (setq folded (outline-invisible-p)))
  6982. (outline-end-of-subtree))
  6983. (outline-next-heading)
  6984. (setq ne-end (org-back-over-empty-lines))
  6985. (setq end (point))
  6986. (goto-char beg0)
  6987. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6988. ;; include less whitespace
  6989. (save-excursion
  6990. (goto-char beg)
  6991. (forward-line (- ne-beg ne-end))
  6992. (setq beg (point))))
  6993. ;; Find insertion point, with error handling
  6994. (while (> cnt 0)
  6995. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6996. (progn (goto-char beg0)
  6997. (error "Cannot move past superior level or buffer limit")))
  6998. (setq cnt (1- cnt)))
  6999. (if (> arg 0)
  7000. ;; Moving forward - still need to move over subtree
  7001. (progn (org-end-of-subtree t t)
  7002. (save-excursion
  7003. (org-back-over-empty-lines)
  7004. (or (bolp) (newline)))))
  7005. (setq ne-ins (org-back-over-empty-lines))
  7006. (move-marker ins-point (point))
  7007. (setq txt (buffer-substring beg end))
  7008. (org-save-markers-in-region beg end)
  7009. (delete-region beg end)
  7010. (org-remove-empty-overlays-at beg)
  7011. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7012. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7013. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7014. (let ((bbb (point)))
  7015. (insert-before-markers txt)
  7016. (org-reinstall-markers-in-region bbb)
  7017. (move-marker ins-point bbb))
  7018. (or (bolp) (insert "\n"))
  7019. (setq ins-end (point))
  7020. (goto-char ins-point)
  7021. (org-skip-whitespace)
  7022. (when (and (< arg 0)
  7023. (org-first-sibling-p)
  7024. (> ne-ins ne-beg))
  7025. ;; Move whitespace back to beginning
  7026. (save-excursion
  7027. (goto-char ins-end)
  7028. (let ((kill-whole-line t))
  7029. (kill-line (- ne-ins ne-beg)) (point)))
  7030. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7031. (move-marker ins-point nil)
  7032. (if folded
  7033. (hide-subtree)
  7034. (org-show-entry)
  7035. (show-children)
  7036. (org-cycle-hide-drawers 'children))
  7037. (org-clean-visibility-after-subtree-move)
  7038. ;; move back to the initial column we were at
  7039. (move-to-column col)))
  7040. (defvar org-subtree-clip ""
  7041. "Clipboard for cut and paste of subtrees.
  7042. This is actually only a copy of the kill, because we use the normal kill
  7043. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7044. (defvar org-subtree-clip-folded nil
  7045. "Was the last copied subtree folded?
  7046. This is used to fold the tree back after pasting.")
  7047. (defun org-cut-subtree (&optional n)
  7048. "Cut the current subtree into the clipboard.
  7049. With prefix arg N, cut this many sequential subtrees.
  7050. This is a short-hand for marking the subtree and then cutting it."
  7051. (interactive "p")
  7052. (org-copy-subtree n 'cut))
  7053. (defun org-copy-subtree (&optional n cut force-store-markers)
  7054. "Cut the current subtree into the clipboard.
  7055. With prefix arg N, cut this many sequential subtrees.
  7056. This is a short-hand for marking the subtree and then copying it.
  7057. If CUT is non-nil, actually cut the subtree.
  7058. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7059. of some markers in the region, even if CUT is non-nil. This is
  7060. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7061. (interactive "p")
  7062. (let (beg end folded (beg0 (point)))
  7063. (if (org-called-interactively-p 'any)
  7064. (org-back-to-heading nil) ; take what looks like a subtree
  7065. (org-back-to-heading t)) ; take what is really there
  7066. (org-back-over-empty-lines)
  7067. (setq beg (point))
  7068. (skip-chars-forward " \t\r\n")
  7069. (save-match-data
  7070. (save-excursion (outline-end-of-heading)
  7071. (setq folded (outline-invisible-p)))
  7072. (condition-case nil
  7073. (org-forward-heading-same-level (1- n) t)
  7074. (error nil))
  7075. (org-end-of-subtree t t))
  7076. (org-back-over-empty-lines)
  7077. (setq end (point))
  7078. (goto-char beg0)
  7079. (when (> end beg)
  7080. (setq org-subtree-clip-folded folded)
  7081. (when (or cut force-store-markers)
  7082. (org-save-markers-in-region beg end))
  7083. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7084. (setq org-subtree-clip (current-kill 0))
  7085. (message "%s: Subtree(s) with %d characters"
  7086. (if cut "Cut" "Copied")
  7087. (length org-subtree-clip)))))
  7088. (defun org-paste-subtree (&optional level tree for-yank)
  7089. "Paste the clipboard as a subtree, with modification of headline level.
  7090. The entire subtree is promoted or demoted in order to match a new headline
  7091. level.
  7092. If the cursor is at the beginning of a headline, the same level as
  7093. that headline is used to paste the tree
  7094. If not, the new level is derived from the *visible* headings
  7095. before and after the insertion point, and taken to be the inferior headline
  7096. level of the two. So if the previous visible heading is level 3 and the
  7097. next is level 4 (or vice versa), level 4 will be used for insertion.
  7098. This makes sure that the subtree remains an independent subtree and does
  7099. not swallow low level entries.
  7100. You can also force a different level, either by using a numeric prefix
  7101. argument, or by inserting the heading marker by hand. For example, if the
  7102. cursor is after \"*****\", then the tree will be shifted to level 5.
  7103. If optional TREE is given, use this text instead of the kill ring.
  7104. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7105. move back over whitespace before inserting, and move point to the end of
  7106. the inserted text when done."
  7107. (interactive "P")
  7108. (setq tree (or tree (and kill-ring (current-kill 0))))
  7109. (unless (org-kill-is-subtree-p tree)
  7110. (error "%s"
  7111. (substitute-command-keys
  7112. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7113. (org-with-limited-levels
  7114. (let* ((visp (not (outline-invisible-p)))
  7115. (txt tree)
  7116. (^re_ "\\(\\*+\\)[ \t]*")
  7117. (old-level (if (string-match org-outline-regexp-bol txt)
  7118. (- (match-end 0) (match-beginning 0) 1)
  7119. -1))
  7120. (force-level (cond (level (prefix-numeric-value level))
  7121. ((and (looking-at "[ \t]*$")
  7122. (string-match
  7123. "^\\*+$" (buffer-substring
  7124. (point-at-bol) (point))))
  7125. (- (match-end 1) (match-beginning 1)))
  7126. ((and (bolp)
  7127. (looking-at org-outline-regexp))
  7128. (- (match-end 0) (point) 1))))
  7129. (previous-level (save-excursion
  7130. (condition-case nil
  7131. (progn
  7132. (outline-previous-visible-heading 1)
  7133. (if (looking-at ^re_)
  7134. (- (match-end 0) (match-beginning 0) 1)
  7135. 1))
  7136. (error 1))))
  7137. (next-level (save-excursion
  7138. (condition-case nil
  7139. (progn
  7140. (or (looking-at org-outline-regexp)
  7141. (outline-next-visible-heading 1))
  7142. (if (looking-at ^re_)
  7143. (- (match-end 0) (match-beginning 0) 1)
  7144. 1))
  7145. (error 1))))
  7146. (new-level (or force-level (max previous-level next-level)))
  7147. (shift (if (or (= old-level -1)
  7148. (= new-level -1)
  7149. (= old-level new-level))
  7150. 0
  7151. (- new-level old-level)))
  7152. (delta (if (> shift 0) -1 1))
  7153. (func (if (> shift 0) 'org-demote 'org-promote))
  7154. (org-odd-levels-only nil)
  7155. beg end newend)
  7156. ;; Remove the forced level indicator
  7157. (if force-level
  7158. (delete-region (point-at-bol) (point)))
  7159. ;; Paste
  7160. (beginning-of-line (if (bolp) 1 2))
  7161. (unless for-yank (org-back-over-empty-lines))
  7162. (setq beg (point))
  7163. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7164. (insert-before-markers txt)
  7165. (unless (string-match "\n\\'" txt) (insert "\n"))
  7166. (setq newend (point))
  7167. (org-reinstall-markers-in-region beg)
  7168. (setq end (point))
  7169. (goto-char beg)
  7170. (skip-chars-forward " \t\n\r")
  7171. (setq beg (point))
  7172. (if (and (outline-invisible-p) visp)
  7173. (save-excursion (outline-show-heading)))
  7174. ;; Shift if necessary
  7175. (unless (= shift 0)
  7176. (save-restriction
  7177. (narrow-to-region beg end)
  7178. (while (not (= shift 0))
  7179. (org-map-region func (point-min) (point-max))
  7180. (setq shift (+ delta shift)))
  7181. (goto-char (point-min))
  7182. (setq newend (point-max))))
  7183. (when (or (org-called-interactively-p 'interactive) for-yank)
  7184. (message "Clipboard pasted as level %d subtree" new-level))
  7185. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7186. kill-ring
  7187. (eq org-subtree-clip (current-kill 0))
  7188. org-subtree-clip-folded)
  7189. ;; The tree was folded before it was killed/copied
  7190. (hide-subtree))
  7191. (and for-yank (goto-char newend)))))
  7192. (defun org-kill-is-subtree-p (&optional txt)
  7193. "Check if the current kill is an outline subtree, or a set of trees.
  7194. Returns nil if kill does not start with a headline, or if the first
  7195. headline level is not the largest headline level in the tree.
  7196. So this will actually accept several entries of equal levels as well,
  7197. which is OK for `org-paste-subtree'.
  7198. If optional TXT is given, check this string instead of the current kill."
  7199. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7200. (re (org-get-limited-outline-regexp))
  7201. (^re (concat "^" re))
  7202. (start-level (and kill
  7203. (string-match
  7204. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7205. kill)
  7206. (- (match-end 2) (match-beginning 2) 1)))
  7207. (start (1+ (or (match-beginning 2) -1))))
  7208. (if (not start-level)
  7209. (progn
  7210. nil) ;; does not even start with a heading
  7211. (catch 'exit
  7212. (while (setq start (string-match ^re kill (1+ start)))
  7213. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7214. (throw 'exit nil)))
  7215. t))))
  7216. (defvar org-markers-to-move nil
  7217. "Markers that should be moved with a cut-and-paste operation.
  7218. Those markers are stored together with their positions relative to
  7219. the start of the region.")
  7220. (defun org-save-markers-in-region (beg end)
  7221. "Check markers in region.
  7222. If these markers are between BEG and END, record their position relative
  7223. to BEG, so that after moving the block of text, we can put the markers back
  7224. into place.
  7225. This function gets called just before an entry or tree gets cut from the
  7226. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7227. called immediately, to move the markers with the entries."
  7228. (setq org-markers-to-move nil)
  7229. (when (featurep 'org-clock)
  7230. (org-clock-save-markers-for-cut-and-paste beg end))
  7231. (when (featurep 'org-agenda)
  7232. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7233. (defun org-check-and-save-marker (marker beg end)
  7234. "Check if MARKER is between BEG and END.
  7235. If yes, remember the marker and the distance to BEG."
  7236. (when (and (marker-buffer marker)
  7237. (equal (marker-buffer marker) (current-buffer)))
  7238. (if (and (>= marker beg) (< marker end))
  7239. (push (cons marker (- marker beg)) org-markers-to-move))))
  7240. (defun org-reinstall-markers-in-region (beg)
  7241. "Move all remembered markers to their position relative to BEG."
  7242. (mapc (lambda (x)
  7243. (move-marker (car x) (+ beg (cdr x))))
  7244. org-markers-to-move)
  7245. (setq org-markers-to-move nil))
  7246. (defun org-narrow-to-subtree ()
  7247. "Narrow buffer to the current subtree."
  7248. (interactive)
  7249. (save-excursion
  7250. (save-match-data
  7251. (org-with-limited-levels
  7252. (narrow-to-region
  7253. (progn (org-back-to-heading t) (point))
  7254. (progn (org-end-of-subtree t t)
  7255. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7256. (point)))))))
  7257. (defun org-narrow-to-block ()
  7258. "Narrow buffer to the current block."
  7259. (interactive)
  7260. (let* ((case-fold-search t)
  7261. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7262. "^[ \t]*#\\+end_.*")))
  7263. (if blockp
  7264. (narrow-to-region (car blockp) (cdr blockp))
  7265. (error "Not in a block"))))
  7266. (eval-when-compile
  7267. (defvar org-property-drawer-re))
  7268. (defvar org-property-start-re) ;; defined below
  7269. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7270. "Clone the task (subtree) at point N times.
  7271. The clones will be inserted as siblings.
  7272. In interactive use, the user will be prompted for the number of
  7273. clones to be produced, and for a time SHIFT, which may be a
  7274. repeater as used in time stamps, for example `+3d'.
  7275. When a valid repeater is given and the entry contains any time
  7276. stamps, the clones will become a sequence in time, with time
  7277. stamps in the subtree shifted for each clone produced. If SHIFT
  7278. is nil or the empty string, time stamps will be left alone. The
  7279. ID property of the original subtree is removed.
  7280. If the original subtree did contain time stamps with a repeater,
  7281. the following will happen:
  7282. - the repeater will be removed in each clone
  7283. - an additional clone will be produced, with the current, unshifted
  7284. date(s) in the entry.
  7285. - the original entry will be placed *after* all the clones, with
  7286. repeater intact.
  7287. - the start days in the repeater in the original entry will be shifted
  7288. to past the last clone.
  7289. In this way you can spell out a number of instances of a repeating task,
  7290. and still retain the repeater to cover future instances of the task."
  7291. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7292. (let (beg end template task idprop
  7293. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7294. (drawer-re org-drawer-regexp))
  7295. (if (not (and (integerp n) (> n 0)))
  7296. (error "Invalid number of replications %s" n))
  7297. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7298. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7299. shift)))
  7300. (error "Invalid shift specification %s" shift))
  7301. (when doshift
  7302. (setq shift-n (string-to-number (match-string 1 shift))
  7303. shift-what (cdr (assoc (match-string 2 shift)
  7304. '(("d" . day) ("w" . week)
  7305. ("m" . month) ("y" . year))))))
  7306. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7307. (setq nmin 1 nmax n)
  7308. (org-back-to-heading t)
  7309. (setq beg (point))
  7310. (setq idprop (org-entry-get nil "ID"))
  7311. (org-end-of-subtree t t)
  7312. (or (bolp) (insert "\n"))
  7313. (setq end (point))
  7314. (setq template (buffer-substring beg end))
  7315. (when (and doshift
  7316. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7317. (delete-region beg end)
  7318. (setq end beg)
  7319. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7320. (goto-char end)
  7321. (loop for n from nmin to nmax do
  7322. ;; prepare clone
  7323. (with-temp-buffer
  7324. (insert template)
  7325. (org-mode)
  7326. (goto-char (point-min))
  7327. (org-show-subtree)
  7328. (and idprop (if org-clone-delete-id
  7329. (org-entry-delete nil "ID")
  7330. (org-id-get-create t)))
  7331. (unless (= n 0)
  7332. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7333. (kill-whole-line))
  7334. (goto-char (point-min))
  7335. (while (re-search-forward drawer-re nil t)
  7336. (mapc (lambda (d)
  7337. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7338. (goto-char (point-min))
  7339. (when doshift
  7340. (while (re-search-forward org-ts-regexp-both nil t)
  7341. (org-timestamp-change (* n shift-n) shift-what))
  7342. (unless (= n n-no-remove)
  7343. (goto-char (point-min))
  7344. (while (re-search-forward org-ts-regexp nil t)
  7345. (save-excursion
  7346. (goto-char (match-beginning 0))
  7347. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7348. (delete-region (match-beginning 1) (match-end 1)))))))
  7349. (setq task (buffer-string)))
  7350. (insert task))
  7351. (goto-char beg)))
  7352. ;;; Outline Sorting
  7353. (defun org-sort (with-case)
  7354. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7355. Optional argument WITH-CASE means sort case-sensitively."
  7356. (interactive "P")
  7357. (cond
  7358. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7359. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7360. (t
  7361. (org-call-with-arg 'org-sort-entries with-case))))
  7362. (defun org-sort-remove-invisible (s)
  7363. (remove-text-properties 0 (length s) org-rm-props s)
  7364. (while (string-match org-bracket-link-regexp s)
  7365. (setq s (replace-match (if (match-end 2)
  7366. (match-string 3 s)
  7367. (match-string 1 s)) t t s)))
  7368. s)
  7369. (defvar org-priority-regexp) ; defined later in the file
  7370. (defvar org-after-sorting-entries-or-items-hook nil
  7371. "Hook that is run after a bunch of entries or items have been sorted.
  7372. When children are sorted, the cursor is in the parent line when this
  7373. hook gets called. When a region or a plain list is sorted, the cursor
  7374. will be in the first entry of the sorted region/list.")
  7375. (defun org-sort-entries
  7376. (&optional with-case sorting-type getkey-func compare-func property)
  7377. "Sort entries on a certain level of an outline tree.
  7378. If there is an active region, the entries in the region are sorted.
  7379. Else, if the cursor is before the first entry, sort the top-level items.
  7380. Else, the children of the entry at point are sorted.
  7381. Sorting can be alphabetically, numerically, by date/time as given by
  7382. a time stamp, by a property or by priority.
  7383. The command prompts for the sorting type unless it has been given to the
  7384. function through the SORTING-TYPE argument, which needs to be a character,
  7385. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7386. precise meaning of each character:
  7387. n Numerically, by converting the beginning of the entry/item to a number.
  7388. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7389. t By date/time, either the first active time stamp in the entry, or, if
  7390. none exist, by the first inactive one.
  7391. s By the scheduled date/time.
  7392. d By deadline date/time.
  7393. c By creation time, which is assumed to be the first inactive time stamp
  7394. at the beginning of a line.
  7395. p By priority according to the cookie.
  7396. r By the value of a property.
  7397. Capital letters will reverse the sort order.
  7398. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7399. called with point at the beginning of the record. It must return either
  7400. a string or a number that should serve as the sorting key for that record.
  7401. Comparing entries ignores case by default. However, with an optional argument
  7402. WITH-CASE, the sorting considers case as well."
  7403. (interactive "P")
  7404. (let ((case-func (if with-case 'identity 'downcase))
  7405. start beg end stars re re2
  7406. txt what tmp)
  7407. ;; Find beginning and end of region to sort
  7408. (cond
  7409. ((org-region-active-p)
  7410. ;; we will sort the region
  7411. (setq end (region-end)
  7412. what "region")
  7413. (goto-char (region-beginning))
  7414. (if (not (org-at-heading-p)) (outline-next-heading))
  7415. (setq start (point)))
  7416. ((or (org-at-heading-p)
  7417. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7418. ;; we will sort the children of the current headline
  7419. (org-back-to-heading)
  7420. (setq start (point)
  7421. end (progn (org-end-of-subtree t t)
  7422. (or (bolp) (insert "\n"))
  7423. (org-back-over-empty-lines)
  7424. (point))
  7425. what "children")
  7426. (goto-char start)
  7427. (show-subtree)
  7428. (outline-next-heading))
  7429. (t
  7430. ;; we will sort the top-level entries in this file
  7431. (goto-char (point-min))
  7432. (or (org-at-heading-p) (outline-next-heading))
  7433. (setq start (point))
  7434. (goto-char (point-max))
  7435. (beginning-of-line 1)
  7436. (when (looking-at ".*?\\S-")
  7437. ;; File ends in a non-white line
  7438. (end-of-line 1)
  7439. (insert "\n"))
  7440. (setq end (point-max))
  7441. (setq what "top-level")
  7442. (goto-char start)
  7443. (show-all)))
  7444. (setq beg (point))
  7445. (if (>= beg end) (error "Nothing to sort"))
  7446. (looking-at "\\(\\*+\\)")
  7447. (setq stars (match-string 1)
  7448. re (concat "^" (regexp-quote stars) " +")
  7449. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7450. txt (buffer-substring beg end))
  7451. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7452. (if (and (not (equal stars "*")) (string-match re2 txt))
  7453. (error "Region to sort contains a level above the first entry"))
  7454. (unless sorting-type
  7455. (message
  7456. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7457. [t]ime [s]cheduled [d]eadline [c]reated
  7458. A/N/T/S/D/C/P/O/F means reversed:"
  7459. what)
  7460. (setq sorting-type (read-char-exclusive))
  7461. (and (= (downcase sorting-type) ?f)
  7462. (setq getkey-func
  7463. (org-icompleting-read "Sort using function: "
  7464. obarray 'fboundp t nil nil))
  7465. (setq getkey-func (intern getkey-func)))
  7466. (and (= (downcase sorting-type) ?r)
  7467. (setq property
  7468. (org-icompleting-read "Property: "
  7469. (mapcar 'list (org-buffer-property-keys t))
  7470. nil t))))
  7471. (message "Sorting entries...")
  7472. (save-restriction
  7473. (narrow-to-region start end)
  7474. (let ((dcst (downcase sorting-type))
  7475. (case-fold-search nil)
  7476. (now (current-time)))
  7477. (sort-subr
  7478. (/= dcst sorting-type)
  7479. ;; This function moves to the beginning character of the "record" to
  7480. ;; be sorted.
  7481. (lambda nil
  7482. (if (re-search-forward re nil t)
  7483. (goto-char (match-beginning 0))
  7484. (goto-char (point-max))))
  7485. ;; This function moves to the last character of the "record" being
  7486. ;; sorted.
  7487. (lambda nil
  7488. (save-match-data
  7489. (condition-case nil
  7490. (outline-forward-same-level 1)
  7491. (error
  7492. (goto-char (point-max))))))
  7493. ;; This function returns the value that gets sorted against.
  7494. (lambda nil
  7495. (cond
  7496. ((= dcst ?n)
  7497. (if (looking-at org-complex-heading-regexp)
  7498. (string-to-number (match-string 4))
  7499. nil))
  7500. ((= dcst ?a)
  7501. (if (looking-at org-complex-heading-regexp)
  7502. (funcall case-func (match-string 4))
  7503. nil))
  7504. ((= dcst ?t)
  7505. (let ((end (save-excursion (outline-next-heading) (point))))
  7506. (if (or (re-search-forward org-ts-regexp end t)
  7507. (re-search-forward org-ts-regexp-both end t))
  7508. (org-time-string-to-seconds (match-string 0))
  7509. (org-float-time now))))
  7510. ((= dcst ?c)
  7511. (let ((end (save-excursion (outline-next-heading) (point))))
  7512. (if (re-search-forward
  7513. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7514. end t)
  7515. (org-time-string-to-seconds (match-string 0))
  7516. (org-float-time now))))
  7517. ((= dcst ?s)
  7518. (let ((end (save-excursion (outline-next-heading) (point))))
  7519. (if (re-search-forward org-scheduled-time-regexp end t)
  7520. (org-time-string-to-seconds (match-string 1))
  7521. (org-float-time now))))
  7522. ((= dcst ?d)
  7523. (let ((end (save-excursion (outline-next-heading) (point))))
  7524. (if (re-search-forward org-deadline-time-regexp end t)
  7525. (org-time-string-to-seconds (match-string 1))
  7526. (org-float-time now))))
  7527. ((= dcst ?p)
  7528. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7529. (string-to-char (match-string 2))
  7530. org-default-priority))
  7531. ((= dcst ?r)
  7532. (or (org-entry-get nil property) ""))
  7533. ((= dcst ?o)
  7534. (if (looking-at org-complex-heading-regexp)
  7535. (- 9999 (length (member (match-string 2)
  7536. org-todo-keywords-1)))))
  7537. ((= dcst ?f)
  7538. (if getkey-func
  7539. (progn
  7540. (setq tmp (funcall getkey-func))
  7541. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7542. tmp)
  7543. (error "Invalid key function `%s'" getkey-func)))
  7544. (t (error "Invalid sorting type `%c'" sorting-type))))
  7545. nil
  7546. (cond
  7547. ((= dcst ?a) 'string<)
  7548. ((= dcst ?f) compare-func)
  7549. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7550. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7551. (message "Sorting entries...done")))
  7552. (defun org-do-sort (table what &optional with-case sorting-type)
  7553. "Sort TABLE of WHAT according to SORTING-TYPE.
  7554. The user will be prompted for the SORTING-TYPE if the call to this
  7555. function does not specify it. WHAT is only for the prompt, to indicate
  7556. what is being sorted. The sorting key will be extracted from
  7557. the car of the elements of the table.
  7558. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7559. (unless sorting-type
  7560. (message
  7561. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7562. what)
  7563. (setq sorting-type (read-char-exclusive)))
  7564. (let ((dcst (downcase sorting-type))
  7565. extractfun comparefun)
  7566. ;; Define the appropriate functions
  7567. (cond
  7568. ((= dcst ?n)
  7569. (setq extractfun 'string-to-number
  7570. comparefun (if (= dcst sorting-type) '< '>)))
  7571. ((= dcst ?a)
  7572. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7573. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7574. comparefun (if (= dcst sorting-type)
  7575. 'string<
  7576. (lambda (a b) (and (not (string< a b))
  7577. (not (string= a b)))))))
  7578. ((= dcst ?t)
  7579. (setq extractfun
  7580. (lambda (x)
  7581. (if (or (string-match org-ts-regexp x)
  7582. (string-match org-ts-regexp-both x))
  7583. (org-float-time
  7584. (org-time-string-to-time (match-string 0 x)))
  7585. 0))
  7586. comparefun (if (= dcst sorting-type) '< '>)))
  7587. (t (error "Invalid sorting type `%c'" sorting-type)))
  7588. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7589. table)
  7590. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7591. ;;; The orgstruct minor mode
  7592. ;; Define a minor mode which can be used in other modes in order to
  7593. ;; integrate the org-mode structure editing commands.
  7594. ;; This is really a hack, because the org-mode structure commands use
  7595. ;; keys which normally belong to the major mode. Here is how it
  7596. ;; works: The minor mode defines all the keys necessary to operate the
  7597. ;; structure commands, but wraps the commands into a function which
  7598. ;; tests if the cursor is currently at a headline or a plain list
  7599. ;; item. If that is the case, the structure command is used,
  7600. ;; temporarily setting many Org-mode variables like regular
  7601. ;; expressions for filling etc. However, when any of those keys is
  7602. ;; used at a different location, function uses `key-binding' to look
  7603. ;; up if the key has an associated command in another currently active
  7604. ;; keymap (minor modes, major mode, global), and executes that
  7605. ;; command. There might be problems if any of the keys is otherwise
  7606. ;; used as a prefix key.
  7607. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7608. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7609. ;; addresses this by checking explicitly for both bindings.
  7610. (defvar orgstruct-mode-map (make-sparse-keymap)
  7611. "Keymap for the minor `orgstruct-mode'.")
  7612. (defvar org-local-vars nil
  7613. "List of local variables, for use by `orgstruct-mode'.")
  7614. ;;;###autoload
  7615. (define-minor-mode orgstruct-mode
  7616. "Toggle the minor mode `orgstruct-mode'.
  7617. This mode is for using Org-mode structure commands in other
  7618. modes. The following keys behave as if Org-mode were active, if
  7619. the cursor is on a headline, or on a plain list item (both as
  7620. defined by Org-mode).
  7621. M-up Move entry/item up
  7622. M-down Move entry/item down
  7623. M-left Promote
  7624. M-right Demote
  7625. M-S-up Move entry/item up
  7626. M-S-down Move entry/item down
  7627. M-S-left Promote subtree
  7628. M-S-right Demote subtree
  7629. M-q Fill paragraph and items like in Org-mode
  7630. C-c ^ Sort entries
  7631. C-c - Cycle list bullet
  7632. TAB Cycle item visibility
  7633. M-RET Insert new heading/item
  7634. S-M-RET Insert new TODO heading / Checkbox item
  7635. C-c C-c Set tags / toggle checkbox"
  7636. nil " OrgStruct" nil
  7637. (org-load-modules-maybe)
  7638. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7639. ;;;###autoload
  7640. (defun turn-on-orgstruct ()
  7641. "Unconditionally turn on `orgstruct-mode'."
  7642. (orgstruct-mode 1))
  7643. (defvar org-fb-vars nil)
  7644. (make-variable-buffer-local 'org-fb-vars)
  7645. (defun orgstruct++-mode (&optional arg)
  7646. "Toggle `orgstruct-mode', the enhanced version of it.
  7647. In addition to setting orgstruct-mode, this also exports all
  7648. indentation and autofilling variables from org-mode into the
  7649. buffer. It will also recognize item context in multiline items."
  7650. (interactive "P")
  7651. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7652. (if (< arg 1)
  7653. (progn (orgstruct-mode -1)
  7654. (mapc (lambda(v)
  7655. (org-set-local (car v)
  7656. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7657. org-fb-vars))
  7658. (orgstruct-mode 1)
  7659. (setq org-fb-vars nil)
  7660. (let (var val)
  7661. (mapc
  7662. (lambda (x)
  7663. (when (string-match
  7664. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7665. (symbol-name (car x)))
  7666. (setq var (car x) val (nth 1 x))
  7667. (push (list var `(quote ,(eval var))) org-fb-vars)
  7668. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7669. org-local-vars)
  7670. (org-set-local 'orgstruct-is-++ t))))
  7671. (defvar orgstruct-is-++ nil
  7672. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7673. (make-variable-buffer-local 'orgstruct-is-++)
  7674. ;;;###autoload
  7675. (defun turn-on-orgstruct++ ()
  7676. "Unconditionally turn on `orgstruct++-mode'."
  7677. (orgstruct++-mode 1))
  7678. (defun orgstruct-error ()
  7679. "Error when there is no default binding for a structure key."
  7680. (interactive)
  7681. (error "This key has no function outside structure elements"))
  7682. (defun orgstruct-setup ()
  7683. "Setup orgstruct keymaps."
  7684. (let ((nfunc 0)
  7685. (bindings
  7686. (list
  7687. '([(meta up)] org-metaup)
  7688. '([(meta down)] org-metadown)
  7689. '([(meta left)] org-metaleft)
  7690. '([(meta right)] org-metaright)
  7691. '([(meta shift up)] org-shiftmetaup)
  7692. '([(meta shift down)] org-shiftmetadown)
  7693. '([(meta shift left)] org-shiftmetaleft)
  7694. '([(meta shift right)] org-shiftmetaright)
  7695. '([?\e (up)] org-metaup)
  7696. '([?\e (down)] org-metadown)
  7697. '([?\e (left)] org-metaleft)
  7698. '([?\e (right)] org-metaright)
  7699. '([?\e (shift up)] org-shiftmetaup)
  7700. '([?\e (shift down)] org-shiftmetadown)
  7701. '([?\e (shift left)] org-shiftmetaleft)
  7702. '([?\e (shift right)] org-shiftmetaright)
  7703. '([(shift up)] org-shiftup)
  7704. '([(shift down)] org-shiftdown)
  7705. '([(shift left)] org-shiftleft)
  7706. '([(shift right)] org-shiftright)
  7707. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7708. '("\M-q" fill-paragraph)
  7709. '("\C-c^" org-sort)
  7710. '("\C-c-" org-cycle-list-bullet)))
  7711. elt key fun cmd)
  7712. (while (setq elt (pop bindings))
  7713. (setq nfunc (1+ nfunc))
  7714. (setq key (org-key (car elt))
  7715. fun (nth 1 elt)
  7716. cmd (orgstruct-make-binding fun nfunc key))
  7717. (org-defkey orgstruct-mode-map key cmd))
  7718. ;; Prevent an error for users who forgot to make autoloads
  7719. (require 'org-element)
  7720. ;; Special treatment needed for TAB and RET
  7721. (org-defkey orgstruct-mode-map [(tab)]
  7722. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7723. (org-defkey orgstruct-mode-map "\C-i"
  7724. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7725. (org-defkey orgstruct-mode-map "\M-\C-m"
  7726. (orgstruct-make-binding 'org-insert-heading 105
  7727. "\M-\C-m" [(meta return)]))
  7728. (org-defkey orgstruct-mode-map [(meta return)]
  7729. (orgstruct-make-binding 'org-insert-heading 106
  7730. [(meta return)] "\M-\C-m"))
  7731. (org-defkey orgstruct-mode-map [(shift meta return)]
  7732. (orgstruct-make-binding 'org-insert-todo-heading 107
  7733. [(meta return)] "\M-\C-m"))
  7734. (org-defkey orgstruct-mode-map "\e\C-m"
  7735. (orgstruct-make-binding 'org-insert-heading 108
  7736. "\e\C-m" [?\e (return)]))
  7737. (org-defkey orgstruct-mode-map [?\e (return)]
  7738. (orgstruct-make-binding 'org-insert-heading 109
  7739. [?\e (return)] "\e\C-m"))
  7740. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7741. (orgstruct-make-binding 'org-insert-todo-heading 110
  7742. [?\e (return)] "\e\C-m"))
  7743. (unless org-local-vars
  7744. (setq org-local-vars (org-get-local-variables)))
  7745. t))
  7746. (defun orgstruct-make-binding (fun n &rest keys)
  7747. "Create a function for binding in the structure minor mode.
  7748. FUN is the command to call inside a table. N is used to create a unique
  7749. command name. KEYS are keys that should be checked in for a command
  7750. to execute outside of tables."
  7751. (eval
  7752. (list 'defun
  7753. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7754. '(arg)
  7755. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7756. "Outside of structure, run the binding of `"
  7757. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7758. "'.")
  7759. '(interactive "p")
  7760. (list 'if
  7761. `(org-context-p 'headline 'item
  7762. (and orgstruct-is-++
  7763. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7764. 'item-body))
  7765. (list 'org-run-like-in-org-mode (list 'quote fun))
  7766. (list 'let '(orgstruct-mode)
  7767. (list 'call-interactively
  7768. (append '(or)
  7769. (mapcar (lambda (k)
  7770. (list 'key-binding k))
  7771. keys)
  7772. '('orgstruct-error))))))))
  7773. (defun org-contextualize-keys (alist contexts)
  7774. "Return valid elements in ALIST depending on CONTEXTS.
  7775. `org-agenda-custom-commands' or `org-capture-templates' are the
  7776. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7777. or `org-capture-templates-contexts' are the associated contexts
  7778. definitions."
  7779. (let ((contexts
  7780. ;; normalize contexts
  7781. (mapcar
  7782. (lambda(c) (cond ((listp (cadr c))
  7783. (list (car c) (car c) (cadr c)))
  7784. ((string= "" (cadr c))
  7785. (list (car c) (car c) (caddr c)))
  7786. (t c))) contexts))
  7787. (a alist) c r s)
  7788. ;; loop over all commands or templates
  7789. (while (setq c (pop a))
  7790. (let (vrules repl)
  7791. (cond
  7792. ((not (assoc (car c) contexts))
  7793. (push c r))
  7794. ((and (assoc (car c) contexts)
  7795. (setq vrules (org-contextualize-validate-key
  7796. (car c) contexts)))
  7797. (mapc (lambda (vr)
  7798. (when (not (equal (car vr) (cadr vr)))
  7799. (setq repl vr))) vrules)
  7800. (if (not repl) (push c r)
  7801. (push (cadr repl) s)
  7802. (push
  7803. (cons (car c)
  7804. (cdr (or (assoc (cadr repl) alist)
  7805. (error "Undefined key `%s' as contextual replacement for `%s'"
  7806. (cadr repl) (car c)))))
  7807. r))))))
  7808. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7809. (delq
  7810. nil
  7811. (delete-dups
  7812. (mapcar (lambda (x)
  7813. (let ((tpl (car x)))
  7814. (when (not (delq
  7815. nil
  7816. (mapcar (lambda(y)
  7817. (equal y tpl)) s))) x)))
  7818. (reverse r))))))
  7819. (defun org-contextualize-validate-key (key contexts)
  7820. "Check CONTEXTS for agenda or capture KEY."
  7821. (let (r rr res)
  7822. (while (setq r (pop contexts))
  7823. (mapc
  7824. (lambda (rr)
  7825. (when
  7826. (and (equal key (car r))
  7827. (if (functionp rr) (funcall rr)
  7828. (or (and (eq (car rr) 'in-file)
  7829. (buffer-file-name)
  7830. (string-match (cdr rr) (buffer-file-name)))
  7831. (and (eq (car rr) 'in-mode)
  7832. (string-match (cdr rr) (symbol-name major-mode)))
  7833. (when (and (eq (car rr) 'not-in-file)
  7834. (buffer-file-name))
  7835. (not (string-match (cdr rr) (buffer-file-name))))
  7836. (when (eq (car rr) 'not-in-mode)
  7837. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7838. (push r res)))
  7839. (car (last r))))
  7840. (delete-dups (delq nil res))))
  7841. (defun org-context-p (&rest contexts)
  7842. "Check if local context is any of CONTEXTS.
  7843. Possible values in the list of contexts are `table', `headline', and `item'."
  7844. (let ((pos (point)))
  7845. (goto-char (point-at-bol))
  7846. (prog1 (or (and (memq 'table contexts)
  7847. (looking-at "[ \t]*|"))
  7848. (and (memq 'headline contexts)
  7849. (looking-at org-outline-regexp))
  7850. (and (memq 'item contexts)
  7851. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7852. (and (memq 'item-body contexts)
  7853. (org-in-item-p)))
  7854. (goto-char pos))))
  7855. (defun org-get-local-variables ()
  7856. "Return a list of all local variables in an Org mode buffer."
  7857. (let (varlist)
  7858. (with-current-buffer (get-buffer-create "*Org tmp*")
  7859. (erase-buffer)
  7860. (org-mode)
  7861. (setq varlist (buffer-local-variables)))
  7862. (kill-buffer "*Org tmp*")
  7863. (delq nil
  7864. (mapcar
  7865. (lambda (x)
  7866. (setq x
  7867. (if (symbolp x)
  7868. (list x)
  7869. (list (car x) (list 'quote (cdr x)))))
  7870. (if (string-match
  7871. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7872. (symbol-name (car x)))
  7873. x nil))
  7874. varlist))))
  7875. (defun org-clone-local-variables (from-buffer &optional regexp)
  7876. "Clone local variables from FROM-BUFFER.
  7877. Optional argument REGEXP selects variables to clone."
  7878. (mapc
  7879. (lambda (pair)
  7880. (and (symbolp (car pair))
  7881. (or (null regexp)
  7882. (string-match regexp (symbol-name (car pair))))
  7883. (set (make-local-variable (car pair))
  7884. (cdr pair))))
  7885. (buffer-local-variables from-buffer)))
  7886. ;;;###autoload
  7887. (defun org-run-like-in-org-mode (cmd)
  7888. "Run a command, pretending that the current buffer is in Org-mode.
  7889. This will temporarily bind local variables that are typically bound in
  7890. Org-mode to the values they have in Org-mode, and then interactively
  7891. call CMD."
  7892. (org-load-modules-maybe)
  7893. (unless org-local-vars
  7894. (setq org-local-vars (org-get-local-variables)))
  7895. (eval (list 'let org-local-vars
  7896. (list 'call-interactively (list 'quote cmd)))))
  7897. ;;;; Archiving
  7898. (defun org-get-category (&optional pos force-refresh)
  7899. "Get the category applying to position POS."
  7900. (save-match-data
  7901. (if force-refresh (org-refresh-category-properties))
  7902. (let ((pos (or pos (point))))
  7903. (or (get-text-property pos 'org-category)
  7904. (progn (org-refresh-category-properties)
  7905. (get-text-property pos 'org-category))))))
  7906. (defun org-refresh-category-properties ()
  7907. "Refresh category text properties in the buffer."
  7908. (let ((case-fold-search t)
  7909. (inhibit-read-only t)
  7910. (def-cat (cond
  7911. ((null org-category)
  7912. (if buffer-file-name
  7913. (file-name-sans-extension
  7914. (file-name-nondirectory buffer-file-name))
  7915. "???"))
  7916. ((symbolp org-category) (symbol-name org-category))
  7917. (t org-category)))
  7918. beg end cat pos optionp)
  7919. (org-unmodified
  7920. (save-excursion
  7921. (save-restriction
  7922. (widen)
  7923. (goto-char (point-min))
  7924. (put-text-property (point) (point-max) 'org-category def-cat)
  7925. (while (re-search-forward
  7926. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7927. (setq pos (match-end 0)
  7928. optionp (equal (char-after (match-beginning 0)) ?#)
  7929. cat (org-trim (match-string 2)))
  7930. (if optionp
  7931. (setq beg (point-at-bol) end (point-max))
  7932. (org-back-to-heading t)
  7933. (setq beg (point) end (org-end-of-subtree t t)))
  7934. (put-text-property beg end 'org-category cat)
  7935. (put-text-property beg end 'org-category-position beg)
  7936. (goto-char pos)))))))
  7937. ;;;; Link Stuff
  7938. ;;; Link abbreviations
  7939. (defun org-link-expand-abbrev (link)
  7940. "Apply replacements as defined in `org-link-abbrev-alist'."
  7941. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7942. (let* ((key (match-string 1 link))
  7943. (as (or (assoc key org-link-abbrev-alist-local)
  7944. (assoc key org-link-abbrev-alist)))
  7945. (tag (and (match-end 2) (match-string 3 link)))
  7946. rpl)
  7947. (if (not as)
  7948. link
  7949. (setq rpl (cdr as))
  7950. (cond
  7951. ((symbolp rpl) (funcall rpl tag))
  7952. ((string-match "%(\\([^)]+\\))" rpl)
  7953. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7954. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7955. ((string-match "%h" rpl)
  7956. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7957. (t (concat rpl tag)))))
  7958. link))
  7959. ;;; Storing and inserting links
  7960. (defvar org-insert-link-history nil
  7961. "Minibuffer history for links inserted with `org-insert-link'.")
  7962. (defvar org-stored-links nil
  7963. "Contains the links stored with `org-store-link'.")
  7964. (defvar org-store-link-plist nil
  7965. "Plist with info about the most recently link created with `org-store-link'.")
  7966. (defvar org-link-protocols nil
  7967. "Link protocols added to Org-mode using `org-add-link-type'.")
  7968. (defvar org-store-link-functions nil
  7969. "List of functions that are called to create and store a link.
  7970. Each function will be called in turn until one returns a non-nil
  7971. value. Each function should check if it is responsible for creating
  7972. this link (for example by looking at the major mode).
  7973. If not, it must exit and return nil.
  7974. If yes, it should return a non-nil value after a calling
  7975. `org-store-link-props' with a list of properties and values.
  7976. Special properties are:
  7977. :type The link prefix, like \"http\". This must be given.
  7978. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7979. This is obligatory as well.
  7980. :description Optional default description for the second pair
  7981. of brackets in an Org-mode link. The user can still change
  7982. this when inserting this link into an Org-mode buffer.
  7983. In addition to these, any additional properties can be specified
  7984. and then used in capture templates.")
  7985. (defun org-add-link-type (type &optional follow export)
  7986. "Add TYPE to the list of `org-link-types'.
  7987. Re-compute all regular expressions depending on `org-link-types'
  7988. FOLLOW and EXPORT are two functions.
  7989. FOLLOW should take the link path as the single argument and do whatever
  7990. is necessary to follow the link, for example find a file or display
  7991. a mail message.
  7992. EXPORT should format the link path for export to one of the export formats.
  7993. It should be a function accepting three arguments:
  7994. path the path of the link, the text after the prefix (like \"http:\")
  7995. desc the description of the link, if any, or a description added by
  7996. org-export-normalize-links if there is none
  7997. format the export format, a symbol like `html' or `latex' or `ascii'..
  7998. The function may use the FORMAT information to return different values
  7999. depending on the format. The return value will be put literally into
  8000. the exported file. If the return value is nil, this means Org should
  8001. do what it normally does with links which do not have EXPORT defined.
  8002. Org-mode has a built-in default for exporting links. If you are happy with
  8003. this default, there is no need to define an export function for the link
  8004. type. For a simple example of an export function, see `org-bbdb.el'."
  8005. (add-to-list 'org-link-types type t)
  8006. (org-make-link-regexps)
  8007. (if (assoc type org-link-protocols)
  8008. (setcdr (assoc type org-link-protocols) (list follow export))
  8009. (push (list type follow export) org-link-protocols)))
  8010. (defvar org-agenda-buffer-name)
  8011. ;;;###autoload
  8012. (defun org-store-link (arg)
  8013. "\\<org-mode-map>Store an org-link to the current location.
  8014. This link is added to `org-stored-links' and can later be inserted
  8015. into an org-buffer with \\[org-insert-link].
  8016. For some link types, a prefix arg is interpreted:
  8017. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  8018. For file links, arg negates `org-context-in-file-links'."
  8019. (interactive "P")
  8020. (org-load-modules-maybe)
  8021. (setq org-store-link-plist nil) ; reset
  8022. (org-with-limited-levels
  8023. (let (link cpltxt desc description search txt custom-id agenda-link)
  8024. (cond
  8025. ((run-hook-with-args-until-success 'org-store-link-functions)
  8026. (setq link (plist-get org-store-link-plist :link)
  8027. desc (or (plist-get org-store-link-plist :description) link)))
  8028. ((org-src-edit-buffer-p)
  8029. (let (label gc)
  8030. (while (or (not label)
  8031. (save-excursion
  8032. (save-restriction
  8033. (widen)
  8034. (goto-char (point-min))
  8035. (re-search-forward
  8036. (regexp-quote (format org-coderef-label-format label))
  8037. nil t))))
  8038. (when label (message "Label exists already") (sit-for 2))
  8039. (setq label (read-string "Code line label: " label)))
  8040. (end-of-line 1)
  8041. (setq link (format org-coderef-label-format label))
  8042. (setq gc (- 79 (length link)))
  8043. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8044. (insert link)
  8045. (setq link (concat "(" label ")") desc nil)))
  8046. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8047. ;; We are in the agenda, link to referenced location
  8048. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8049. (get-text-property (point) 'org-marker))))
  8050. (when m
  8051. (org-with-point-at m
  8052. (setq agenda-link
  8053. (if (org-called-interactively-p 'any)
  8054. (call-interactively 'org-store-link)
  8055. (org-store-link nil)))))))
  8056. ((eq major-mode 'calendar-mode)
  8057. (let ((cd (calendar-cursor-to-date)))
  8058. (setq link
  8059. (format-time-string
  8060. (car org-time-stamp-formats)
  8061. (apply 'encode-time
  8062. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8063. nil nil nil))))
  8064. (org-store-link-props :type "calendar" :date cd)))
  8065. ((eq major-mode 'help-mode)
  8066. (setq link (concat "help:" (save-excursion
  8067. (goto-char (point-min))
  8068. (looking-at "^[^ ]+")
  8069. (match-string 0))))
  8070. (org-store-link-props :type "help"))
  8071. ((eq major-mode 'w3-mode)
  8072. (setq cpltxt (if (and (buffer-name)
  8073. (not (string-match "Untitled" (buffer-name))))
  8074. (buffer-name)
  8075. (url-view-url t))
  8076. link (url-view-url t))
  8077. (org-store-link-props :type "w3" :url (url-view-url t)))
  8078. ((eq major-mode 'w3m-mode)
  8079. (setq cpltxt (or w3m-current-title w3m-current-url)
  8080. link w3m-current-url)
  8081. (org-store-link-props :type "w3m" :url (url-view-url t)))
  8082. ((setq search (run-hook-with-args-until-success
  8083. 'org-create-file-search-functions))
  8084. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8085. "::" search))
  8086. (setq cpltxt (or description link)))
  8087. ((eq major-mode 'image-mode)
  8088. (setq cpltxt (concat "file:"
  8089. (abbreviate-file-name buffer-file-name))
  8090. link cpltxt)
  8091. (org-store-link-props :type "image" :file buffer-file-name))
  8092. ((eq major-mode 'dired-mode)
  8093. ;; link to the file in the current line
  8094. (let ((file (dired-get-filename nil t)))
  8095. (setq file (if file
  8096. (abbreviate-file-name
  8097. (expand-file-name (dired-get-filename nil t)))
  8098. ;; otherwise, no file so use current directory.
  8099. default-directory))
  8100. (setq cpltxt (concat "file:" file)
  8101. link cpltxt)))
  8102. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8103. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8104. (cond
  8105. ((org-in-regexp "<<\\(.*?\\)>>")
  8106. (setq cpltxt
  8107. (concat "file:"
  8108. (abbreviate-file-name
  8109. (buffer-file-name (buffer-base-buffer)))
  8110. "::" (match-string 1))
  8111. link cpltxt))
  8112. ((and (featurep 'org-id)
  8113. (or (eq org-link-to-org-use-id t)
  8114. (and (org-called-interactively-p 'any)
  8115. (or (eq org-link-to-org-use-id 'create-if-interactive)
  8116. (and (eq org-link-to-org-use-id
  8117. 'create-if-interactive-and-no-custom-id)
  8118. (not custom-id))))
  8119. (and org-link-to-org-use-id (org-entry-get nil "ID"))))
  8120. ;; We can make a link using the ID.
  8121. (setq link (condition-case nil
  8122. (prog1 (org-id-store-link)
  8123. (setq desc (plist-get org-store-link-plist :description)))
  8124. (error
  8125. ;; probably before first headline, link to file only
  8126. (concat "file:"
  8127. (abbreviate-file-name
  8128. (buffer-file-name (buffer-base-buffer))))))))
  8129. (t
  8130. ;; Just link to current headline
  8131. (setq cpltxt (concat "file:"
  8132. (abbreviate-file-name
  8133. (buffer-file-name (buffer-base-buffer)))))
  8134. ;; Add a context search string
  8135. (when (org-xor org-context-in-file-links arg)
  8136. (setq txt (cond
  8137. ((org-at-heading-p) nil)
  8138. ((org-region-active-p)
  8139. (buffer-substring (region-beginning) (region-end)))))
  8140. (when (or (null txt) (string-match "\\S-" txt))
  8141. (setq cpltxt
  8142. (concat cpltxt "::"
  8143. (condition-case nil
  8144. (org-make-org-heading-search-string txt)
  8145. (error "")))
  8146. desc (or (nth 4 (ignore-errors
  8147. (org-heading-components))) "NONE"))))
  8148. (if (string-match "::\\'" cpltxt)
  8149. (setq cpltxt (substring cpltxt 0 -2)))
  8150. (setq link cpltxt))))
  8151. ((buffer-file-name (buffer-base-buffer))
  8152. ;; Just link to this file here.
  8153. (setq cpltxt (concat "file:"
  8154. (abbreviate-file-name
  8155. (buffer-file-name (buffer-base-buffer)))))
  8156. ;; Add a context string
  8157. (when (org-xor org-context-in-file-links arg)
  8158. (setq txt (if (org-region-active-p)
  8159. (buffer-substring (region-beginning) (region-end))
  8160. (buffer-substring (point-at-bol) (point-at-eol))))
  8161. ;; Only use search option if there is some text.
  8162. (when (string-match "\\S-" txt)
  8163. (setq cpltxt
  8164. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8165. desc "NONE")))
  8166. (setq link cpltxt))
  8167. ((org-called-interactively-p 'interactive)
  8168. (error "Cannot link to a buffer which is not visiting a file"))
  8169. (t (setq link nil)))
  8170. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8171. (setq link (or link cpltxt)
  8172. desc (or desc cpltxt))
  8173. (if (equal desc "NONE") (setq desc nil))
  8174. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8175. (progn
  8176. (setq org-stored-links
  8177. (cons (list link desc) org-stored-links))
  8178. (message "Stored: %s" (or desc link))
  8179. (when custom-id
  8180. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8181. "::#" custom-id))
  8182. (setq org-stored-links
  8183. (cons (list link desc) org-stored-links))))
  8184. (or agenda-link (and link (org-make-link-string link desc)))))))
  8185. (defun org-store-link-props (&rest plist)
  8186. "Store link properties, extract names and addresses."
  8187. (let (x adr)
  8188. (when (setq x (plist-get plist :from))
  8189. (setq adr (mail-extract-address-components x))
  8190. (setq plist (plist-put plist :fromname (car adr)))
  8191. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8192. (when (setq x (plist-get plist :to))
  8193. (setq adr (mail-extract-address-components x))
  8194. (setq plist (plist-put plist :toname (car adr)))
  8195. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8196. (let ((from (plist-get plist :from))
  8197. (to (plist-get plist :to)))
  8198. (when (and from to org-from-is-user-regexp)
  8199. (setq plist
  8200. (plist-put plist :fromto
  8201. (if (string-match org-from-is-user-regexp from)
  8202. (concat "to %t")
  8203. (concat "from %f"))))))
  8204. (setq org-store-link-plist plist))
  8205. (defun org-add-link-props (&rest plist)
  8206. "Add these properties to the link property list."
  8207. (let (key value)
  8208. (while plist
  8209. (setq key (pop plist) value (pop plist))
  8210. (setq org-store-link-plist
  8211. (plist-put org-store-link-plist key value)))))
  8212. (defun org-email-link-description (&optional fmt)
  8213. "Return the description part of an email link.
  8214. This takes information from `org-store-link-plist' and formats it
  8215. according to FMT (default from `org-email-link-description-format')."
  8216. (setq fmt (or fmt org-email-link-description-format))
  8217. (let* ((p org-store-link-plist)
  8218. (to (plist-get p :toaddress))
  8219. (from (plist-get p :fromaddress))
  8220. (table
  8221. (list
  8222. (cons "%c" (plist-get p :fromto))
  8223. (cons "%F" (plist-get p :from))
  8224. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8225. (cons "%T" (plist-get p :to))
  8226. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8227. (cons "%s" (plist-get p :subject))
  8228. (cons "%d" (plist-get p :date))
  8229. (cons "%m" (plist-get p :message-id)))))
  8230. (when (string-match "%c" fmt)
  8231. ;; Check if the user wrote this message
  8232. (if (and org-from-is-user-regexp from to
  8233. (save-match-data (string-match org-from-is-user-regexp from)))
  8234. (setq fmt (replace-match "to %t" t t fmt))
  8235. (setq fmt (replace-match "from %f" t t fmt))))
  8236. (org-replace-escapes fmt table)))
  8237. (defun org-make-org-heading-search-string (&optional string heading)
  8238. "Make search string for STRING or current headline."
  8239. (interactive)
  8240. (let ((s (or string (org-get-heading)))
  8241. (lines org-context-in-file-links))
  8242. (unless (and string (not heading))
  8243. ;; We are using a headline, clean up garbage in there.
  8244. (if (string-match org-todo-regexp s)
  8245. (setq s (replace-match "" t t s)))
  8246. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8247. (setq s (replace-match "" t t s)))
  8248. (setq s (org-trim s))
  8249. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8250. org-comment-string "\\)") s)
  8251. (setq s (replace-match "" t t s)))
  8252. (while (string-match org-ts-regexp s)
  8253. (setq s (replace-match "" t t s))))
  8254. (or string (setq s (concat "*" s))) ; Add * for headlines
  8255. (when (and string (integerp lines) (> lines 0))
  8256. (let ((slines (org-split-string s "\n")))
  8257. (when (< lines (length slines))
  8258. (setq s (mapconcat
  8259. 'identity
  8260. (reverse (nthcdr (- (length slines) lines)
  8261. (reverse slines))) "\n")))))
  8262. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8263. (defun org-make-link-string (link &optional description)
  8264. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8265. (unless (string-match "\\S-" link)
  8266. (error "Empty link"))
  8267. (when (and description
  8268. (stringp description)
  8269. (not (string-match "\\S-" description)))
  8270. (setq description nil))
  8271. (when (stringp description)
  8272. ;; Remove brackets from the description, they are fatal.
  8273. (while (string-match "\\[" description)
  8274. (setq description (replace-match "{" t t description)))
  8275. (while (string-match "\\]" description)
  8276. (setq description (replace-match "}" t t description))))
  8277. (when (equal link description)
  8278. ;; No description needed, it is identical
  8279. (setq description nil))
  8280. (when (and (not description)
  8281. (not (string-match (org-image-file-name-regexp) link))
  8282. (not (equal link (org-link-escape link))))
  8283. (setq description (org-extract-attributes link)))
  8284. (setq link
  8285. (cond ((string-match (org-image-file-name-regexp) link) link)
  8286. ((string-match org-link-types-re link)
  8287. (concat (match-string 1 link)
  8288. (org-link-escape (substring link (match-end 1)))))
  8289. (t (org-link-escape link))))
  8290. (concat "[[" link "]"
  8291. (if description (concat "[" description "]") "")
  8292. "]"))
  8293. (defconst org-link-escape-chars
  8294. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8295. "List of characters that should be escaped in link.
  8296. This is the list that is used for internal purposes.")
  8297. (defconst org-link-escape-chars-browser
  8298. '(?\ )
  8299. "List of escapes for characters that are problematic in links.
  8300. This is the list that is used before handing over to the browser.")
  8301. (defun org-link-escape (text &optional table merge)
  8302. "Return percent escaped representation of TEXT.
  8303. TEXT is a string with the text to escape.
  8304. Optional argument TABLE is a list with characters that should be
  8305. escaped. When nil, `org-link-escape-chars' is used.
  8306. If optional argument MERGE is set, merge TABLE into
  8307. `org-link-escape-chars'."
  8308. (cond
  8309. ((and table merge)
  8310. (mapc (lambda (defchr)
  8311. (unless (member defchr table)
  8312. (setq table (cons defchr table)))) org-link-escape-chars))
  8313. ((null table)
  8314. (setq table org-link-escape-chars)))
  8315. (mapconcat
  8316. (lambda (char)
  8317. (if (or (member char table)
  8318. (and (or (< char 32) (= char 37) (> char 126))
  8319. org-url-hexify-p))
  8320. (mapconcat (lambda (sequence-element)
  8321. (format "%%%.2X" sequence-element))
  8322. (or (encode-coding-char char 'utf-8)
  8323. (error "Unable to percent escape character: %s"
  8324. (char-to-string char))) "")
  8325. (char-to-string char))) text ""))
  8326. (defun org-link-unescape (str)
  8327. "Unhex hexified Unicode strings as returned from the JavaScript function
  8328. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8329. (unless (and (null str) (string= "" str))
  8330. (let ((pos 0) (case-fold-search t) unhexed)
  8331. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8332. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8333. (setq str (replace-match unhexed t t str))
  8334. (setq pos (+ pos (length unhexed))))))
  8335. str)
  8336. (defun org-link-unescape-compound (hex)
  8337. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8338. Note: this function also decodes single byte encodings like
  8339. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8340. (save-match-data
  8341. (let* ((bytes (cdr (split-string hex "%")))
  8342. (ret "")
  8343. (eat 0)
  8344. (sum 0))
  8345. (while bytes
  8346. (let* ((val (string-to-number (pop bytes) 16))
  8347. (shift-xor
  8348. (if (= 0 eat)
  8349. (cond
  8350. ((>= val 252) (cons 6 252))
  8351. ((>= val 248) (cons 5 248))
  8352. ((>= val 240) (cons 4 240))
  8353. ((>= val 224) (cons 3 224))
  8354. ((>= val 192) (cons 2 192))
  8355. (t (cons 0 0)))
  8356. (cons 6 128))))
  8357. (if (>= val 192) (setq eat (car shift-xor)))
  8358. (setq val (logxor val (cdr shift-xor)))
  8359. (setq sum (+ (lsh sum (car shift-xor)) val))
  8360. (if (> eat 0) (setq eat (- eat 1)))
  8361. (cond
  8362. ((= 0 eat) ;multi byte
  8363. (setq ret (concat ret (org-char-to-string sum)))
  8364. (setq sum 0))
  8365. ((not bytes) ; single byte(s)
  8366. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8367. )) ;; end (while bytes
  8368. ret )))
  8369. (defun org-link-unescape-single-byte-sequence (hex)
  8370. "Unhexify hex-encoded single byte character sequences."
  8371. (mapconcat (lambda (byte)
  8372. (char-to-string (string-to-number byte 16)))
  8373. (cdr (split-string hex "%")) ""))
  8374. (defun org-xor (a b)
  8375. "Exclusive or."
  8376. (if a (not b) b))
  8377. (defun org-fixup-message-id-for-http (s)
  8378. "Replace special characters in a message id, so it can be used in an http query."
  8379. (when (string-match "%" s)
  8380. (setq s (mapconcat (lambda (c)
  8381. (if (eq c ?%)
  8382. "%25"
  8383. (char-to-string c)))
  8384. s "")))
  8385. (while (string-match "<" s)
  8386. (setq s (replace-match "%3C" t t s)))
  8387. (while (string-match ">" s)
  8388. (setq s (replace-match "%3E" t t s)))
  8389. (while (string-match "@" s)
  8390. (setq s (replace-match "%40" t t s)))
  8391. s)
  8392. (defun org-link-prettify (link)
  8393. "Return a human-readable representation of LINK.
  8394. The car of LINK must be a raw link the cdr of LINK must be either
  8395. a link description or nil."
  8396. (let ((desc (or (cadr link) "<no description>")))
  8397. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8398. "<" (car link) ">")))
  8399. ;;;###autoload
  8400. (defun org-insert-link-global ()
  8401. "Insert a link like Org-mode does.
  8402. This command can be called in any mode to insert a link in Org-mode syntax."
  8403. (interactive)
  8404. (org-load-modules-maybe)
  8405. (org-run-like-in-org-mode 'org-insert-link))
  8406. (defun org-insert-all-links (&optional keep)
  8407. "Insert all links in `org-stored-links'."
  8408. (interactive "P")
  8409. (let ((links (copy-sequence org-stored-links)) l)
  8410. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8411. (insert "- ")
  8412. (org-insert-link nil (car l) (cadr l))
  8413. (insert "\n"))))
  8414. (defun org-link-fontify-links-to-this-file ()
  8415. "Fontify links to the current file in `org-stored-links'."
  8416. (let ((f (buffer-file-name)) a b)
  8417. (setq a (mapcar (lambda(l)
  8418. (let ((ll (car l)))
  8419. (when (and (string-match "^file:\\(.+\\)::" ll)
  8420. (equal f (expand-file-name (match-string 1 ll))))
  8421. ll)))
  8422. org-stored-links))
  8423. (when (featurep 'org-id)
  8424. (setq b (mapcar (lambda(l)
  8425. (let ((ll (car l)))
  8426. (when (and (string-match "^id:\\(.+\\)$" ll)
  8427. (equal f (expand-file-name
  8428. (or (org-id-find-id-file
  8429. (match-string 1 ll)) ""))))
  8430. ll)))
  8431. org-stored-links)))
  8432. (mapcar (lambda(l)
  8433. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8434. (delq nil (append a b)))))
  8435. (defvar org-link-links-in-this-file nil)
  8436. (defun org-insert-link (&optional complete-file link-location default-description)
  8437. "Insert a link. At the prompt, enter the link.
  8438. Completion can be used to insert any of the link protocol prefixes like
  8439. http or ftp in use.
  8440. The history can be used to select a link previously stored with
  8441. `org-store-link'. When the empty string is entered (i.e. if you just
  8442. press RET at the prompt), the link defaults to the most recently
  8443. stored link. As SPC triggers completion in the minibuffer, you need to
  8444. use M-SPC or C-q SPC to force the insertion of a space character.
  8445. You will also be prompted for a description, and if one is given, it will
  8446. be displayed in the buffer instead of the link.
  8447. If there is already a link at point, this command will allow you to edit link
  8448. and description parts.
  8449. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8450. be selected using completion. The path to the file will be relative to the
  8451. current directory if the file is in the current directory or a subdirectory.
  8452. Otherwise, the link will be the absolute path as completed in the minibuffer
  8453. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8454. option `org-link-file-path-type'.
  8455. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8456. the current directory or below.
  8457. With three \\[universal-argument] prefixes, negate the meaning of
  8458. `org-keep-stored-link-after-insertion'.
  8459. If `org-make-link-description-function' is non-nil, this function will be
  8460. called with the link target, and the result will be the default
  8461. link description.
  8462. If the LINK-LOCATION parameter is non-nil, this value will be
  8463. used as the link location instead of reading one interactively.
  8464. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8465. be used as the default description."
  8466. (interactive "P")
  8467. (let* ((wcf (current-window-configuration))
  8468. (region (if (org-region-active-p)
  8469. (buffer-substring (region-beginning) (region-end))))
  8470. (remove (and region (list (region-beginning) (region-end))))
  8471. (desc region)
  8472. tmphist ; byte-compile incorrectly complains about this
  8473. (link link-location)
  8474. (abbrevs org-link-abbrev-alist-local)
  8475. entry file all-prefixes auto-desc)
  8476. (cond
  8477. (link-location) ; specified by arg, just use it.
  8478. ((org-in-regexp org-bracket-link-regexp 1)
  8479. ;; We do have a link at point, and we are going to edit it.
  8480. (setq remove (list (match-beginning 0) (match-end 0)))
  8481. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8482. (setq link (read-string "Link: "
  8483. (org-link-unescape
  8484. (org-match-string-no-properties 1)))))
  8485. ((or (org-in-regexp org-angle-link-re)
  8486. (org-in-regexp org-plain-link-re))
  8487. ;; Convert to bracket link
  8488. (setq remove (list (match-beginning 0) (match-end 0))
  8489. link (read-string "Link: "
  8490. (org-remove-angle-brackets (match-string 0)))))
  8491. ((member complete-file '((4) (16)))
  8492. ;; Completing read for file names.
  8493. (setq link (org-file-complete-link complete-file)))
  8494. (t
  8495. ;; Read link, with completion for stored links.
  8496. (org-link-fontify-links-to-this-file)
  8497. (org-switch-to-buffer-other-window "*Org Links*")
  8498. (with-current-buffer "*Org Links*"
  8499. (erase-buffer)
  8500. (insert "Insert a link.
  8501. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8502. (when org-stored-links
  8503. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8504. (insert (mapconcat 'org-link-prettify
  8505. (reverse org-stored-links) "\n")))
  8506. (goto-char (point-min)))
  8507. (let ((cw (selected-window)))
  8508. (select-window (get-buffer-window "*Org Links*" 'visible))
  8509. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8510. (unless (pos-visible-in-window-p (point-max))
  8511. (org-fit-window-to-buffer))
  8512. (and (window-live-p cw) (select-window cw)))
  8513. ;; Fake a link history, containing the stored links.
  8514. (setq tmphist (append (mapcar 'car org-stored-links)
  8515. org-insert-link-history))
  8516. (setq all-prefixes (append (mapcar 'car abbrevs)
  8517. (mapcar 'car org-link-abbrev-alist)
  8518. org-link-types))
  8519. (unwind-protect
  8520. (progn
  8521. (setq link
  8522. (let ((org-completion-use-ido nil)
  8523. (org-completion-use-iswitchb nil))
  8524. (org-completing-read
  8525. "Link: "
  8526. (append
  8527. (mapcar (lambda (x) (list (concat x ":")))
  8528. all-prefixes)
  8529. (mapcar 'car org-stored-links)
  8530. (mapcar 'cadr org-stored-links))
  8531. nil nil nil
  8532. 'tmphist
  8533. (caar org-stored-links))))
  8534. (if (not (string-match "\\S-" link))
  8535. (error "No link selected"))
  8536. (mapc (lambda(l)
  8537. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8538. org-stored-links)
  8539. (if (or (member link all-prefixes)
  8540. (and (equal ":" (substring link -1))
  8541. (member (substring link 0 -1) all-prefixes)
  8542. (setq link (substring link 0 -1))))
  8543. (setq link (org-link-try-special-completion link))))
  8544. (set-window-configuration wcf)
  8545. (kill-buffer "*Org Links*"))
  8546. (setq entry (assoc link org-stored-links))
  8547. (or entry (push link org-insert-link-history))
  8548. (setq desc (or desc (nth 1 entry)))))
  8549. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8550. (not org-keep-stored-link-after-insertion))
  8551. (setq org-stored-links (delq (assoc link org-stored-links)
  8552. org-stored-links)))
  8553. (if (string-match org-plain-link-re link)
  8554. ;; URL-like link, normalize the use of angular brackets.
  8555. (setq link (org-remove-angle-brackets link)))
  8556. ;; Check if we are linking to the current file with a search option
  8557. ;; If yes, simplify the link by using only the search option.
  8558. (when (and buffer-file-name
  8559. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8560. (let* ((path (match-string 1 link))
  8561. (case-fold-search nil)
  8562. (search (match-string 2 link)))
  8563. (save-match-data
  8564. (if (equal (file-truename buffer-file-name) (file-truename path))
  8565. ;; We are linking to this same file, with a search option
  8566. (setq link search)))))
  8567. ;; Check if we can/should use a relative path. If yes, simplify the link
  8568. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8569. (let* ((type (match-string 1 link))
  8570. (path (match-string 2 link))
  8571. (origpath path)
  8572. (case-fold-search nil))
  8573. (cond
  8574. ((or (eq org-link-file-path-type 'absolute)
  8575. (equal complete-file '(16)))
  8576. (setq path (abbreviate-file-name (expand-file-name path))))
  8577. ((eq org-link-file-path-type 'noabbrev)
  8578. (setq path (expand-file-name path)))
  8579. ((eq org-link-file-path-type 'relative)
  8580. (setq path (file-relative-name path)))
  8581. (t
  8582. (save-match-data
  8583. (if (string-match (concat "^" (regexp-quote
  8584. (expand-file-name
  8585. (file-name-as-directory
  8586. default-directory))))
  8587. (expand-file-name path))
  8588. ;; We are linking a file with relative path name.
  8589. (setq path (substring (expand-file-name path)
  8590. (match-end 0)))
  8591. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8592. (setq link (concat type path))
  8593. (if (equal desc origpath)
  8594. (setq desc path))))
  8595. (if org-make-link-description-function
  8596. (setq desc (funcall org-make-link-description-function link desc))
  8597. (if default-description (setq desc default-description)
  8598. (setq desc (or (and auto-desc desc)
  8599. (read-string "Description: " desc)))))
  8600. (unless (string-match "\\S-" desc) (setq desc nil))
  8601. (if remove (apply 'delete-region remove))
  8602. (insert (org-make-link-string link desc))))
  8603. (defun org-link-try-special-completion (type)
  8604. "If there is completion support for link type TYPE, offer it."
  8605. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8606. (if (functionp fun)
  8607. (funcall fun)
  8608. (read-string "Link (no completion support): " (concat type ":")))))
  8609. (defun org-file-complete-link (&optional arg)
  8610. "Create a file link using completion."
  8611. (let (file link)
  8612. (setq file (read-file-name "File: "))
  8613. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8614. (pwd1 (file-name-as-directory (abbreviate-file-name
  8615. (expand-file-name ".")))))
  8616. (cond
  8617. ((equal arg '(16))
  8618. (setq link (concat
  8619. "file:"
  8620. (abbreviate-file-name (expand-file-name file)))))
  8621. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8622. (setq link (concat "file:" (match-string 1 file))))
  8623. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8624. (expand-file-name file))
  8625. (setq link (concat
  8626. "file:" (match-string 1 (expand-file-name file)))))
  8627. (t (setq link (concat "file:" file)))))
  8628. link))
  8629. (defun org-completing-read (&rest args)
  8630. "Completing-read with SPACE being a normal character."
  8631. (let ((enable-recursive-minibuffers t)
  8632. (minibuffer-local-completion-map
  8633. (copy-keymap minibuffer-local-completion-map)))
  8634. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8635. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8636. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8637. (apply 'org-icompleting-read args)))
  8638. (defun org-completing-read-no-i (&rest args)
  8639. (let (org-completion-use-ido org-completion-use-iswitchb)
  8640. (apply 'org-completing-read args)))
  8641. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8642. "Use iswitch as a completing-read replacement to choose from choices.
  8643. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8644. from."
  8645. (let* ((iswitchb-use-virtual-buffers nil)
  8646. (iswitchb-make-buflist-hook
  8647. (lambda ()
  8648. (setq iswitchb-temp-buflist choices))))
  8649. (iswitchb-read-buffer prompt)))
  8650. (defun org-icompleting-read (&rest args)
  8651. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8652. (org-without-partial-completion
  8653. (if (and org-completion-use-ido
  8654. (fboundp 'ido-completing-read)
  8655. (boundp 'ido-mode) ido-mode
  8656. (listp (second args)))
  8657. (let ((ido-enter-matching-directory nil))
  8658. (apply 'ido-completing-read (concat (car args))
  8659. (if (consp (car (nth 1 args)))
  8660. (mapcar 'car (nth 1 args))
  8661. (nth 1 args))
  8662. (cddr args)))
  8663. (if (and org-completion-use-iswitchb
  8664. (boundp 'iswitchb-mode) iswitchb-mode
  8665. (listp (second args)))
  8666. (apply 'org-iswitchb-completing-read (concat (car args))
  8667. (if (consp (car (nth 1 args)))
  8668. (mapcar 'car (nth 1 args))
  8669. (nth 1 args))
  8670. (cddr args))
  8671. (apply 'completing-read args)))))
  8672. (defun org-extract-attributes (s)
  8673. "Extract the attributes cookie from a string and set as text property."
  8674. (let (a attr (start 0) key value)
  8675. (save-match-data
  8676. (when (string-match "{{\\([^}]+\\)}}$" s)
  8677. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8678. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8679. (setq key (match-string 1 a) value (match-string 2 a)
  8680. start (match-end 0)
  8681. attr (plist-put attr (intern key) value))))
  8682. (org-add-props s nil 'org-attr attr))
  8683. s))
  8684. (defun org-extract-attributes-from-string (tag)
  8685. (let (key value attr)
  8686. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8687. (setq key (match-string 1 tag) value (match-string 2 tag)
  8688. tag (replace-match "" t t tag)
  8689. attr (plist-put attr (intern key) value)))
  8690. (cons tag attr)))
  8691. (defun org-attributes-to-string (plist)
  8692. "Format a property list into an HTML attribute list."
  8693. (let ((s "") key value)
  8694. (while plist
  8695. (setq key (pop plist) value (pop plist))
  8696. (and value
  8697. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8698. s))
  8699. ;;; Opening/following a link
  8700. (defvar org-link-search-failed nil)
  8701. (defvar org-open-link-functions nil
  8702. "Hook for functions finding a plain text link.
  8703. These functions must take a single argument, the link content.
  8704. They will be called for links that look like [[link text][description]]
  8705. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8706. like a filename (e.g. \"./blue.png\").
  8707. These functions will be called *before* Org attempts to resolve the
  8708. link by doing text searches in the current buffer - so if you want a
  8709. link \"[[target]]\" to still find \"<<target>>\", your function should
  8710. handle this as a special case.
  8711. When the function does handle the link, it must return a non-nil value.
  8712. If it decides that it is not responsible for this link, it must return
  8713. nil to indicate that that Org-mode can continue with other options
  8714. like exact and fuzzy text search.")
  8715. (defun org-next-link ()
  8716. "Move forward to the next link.
  8717. If the link is in hidden text, expose it."
  8718. (interactive)
  8719. (when (and org-link-search-failed (eq this-command last-command))
  8720. (goto-char (point-min))
  8721. (message "Link search wrapped back to beginning of buffer"))
  8722. (setq org-link-search-failed nil)
  8723. (let* ((pos (point))
  8724. (ct (org-context))
  8725. (a (assoc :link ct)))
  8726. (if a (goto-char (nth 2 a)))
  8727. (if (re-search-forward org-any-link-re nil t)
  8728. (progn
  8729. (goto-char (match-beginning 0))
  8730. (if (outline-invisible-p) (org-show-context)))
  8731. (goto-char pos)
  8732. (setq org-link-search-failed t)
  8733. (error "No further link found"))))
  8734. (defun org-previous-link ()
  8735. "Move backward to the previous link.
  8736. If the link is in hidden text, expose it."
  8737. (interactive)
  8738. (when (and org-link-search-failed (eq this-command last-command))
  8739. (goto-char (point-max))
  8740. (message "Link search wrapped back to end of buffer"))
  8741. (setq org-link-search-failed nil)
  8742. (let* ((pos (point))
  8743. (ct (org-context))
  8744. (a (assoc :link ct)))
  8745. (if a (goto-char (nth 1 a)))
  8746. (if (re-search-backward org-any-link-re nil t)
  8747. (progn
  8748. (goto-char (match-beginning 0))
  8749. (if (outline-invisible-p) (org-show-context)))
  8750. (goto-char pos)
  8751. (setq org-link-search-failed t)
  8752. (error "No further link found"))))
  8753. (defun org-translate-link (s)
  8754. "Translate a link string if a translation function has been defined."
  8755. (if (and org-link-translation-function
  8756. (fboundp org-link-translation-function)
  8757. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8758. (progn
  8759. (setq s (funcall org-link-translation-function
  8760. (match-string 1 s) (match-string 2 s)))
  8761. (concat (car s) ":" (cdr s)))
  8762. s))
  8763. (defun org-translate-link-from-planner (type path)
  8764. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8765. This is still an experimental function, your mileage may vary."
  8766. (cond
  8767. ((member type '("http" "https" "news" "ftp"))
  8768. ;; standard Internet links are the same.
  8769. nil)
  8770. ((and (equal type "irc") (string-match "^//" path))
  8771. ;; Planner has two / at the beginning of an irc link, we have 1.
  8772. ;; We should have zero, actually....
  8773. (setq path (substring path 1)))
  8774. ((and (equal type "lisp") (string-match "^/" path))
  8775. ;; Planner has a slash, we do not.
  8776. (setq type "elisp" path (substring path 1)))
  8777. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8778. ;; A typical message link. Planner has the id after the final slash,
  8779. ;; we separate it with a hash mark
  8780. (setq path (concat (match-string 1 path) "#"
  8781. (org-remove-angle-brackets (match-string 2 path)))))
  8782. )
  8783. (cons type path))
  8784. (defun org-find-file-at-mouse (ev)
  8785. "Open file link or URL at mouse."
  8786. (interactive "e")
  8787. (mouse-set-point ev)
  8788. (org-open-at-point 'in-emacs))
  8789. (defun org-open-at-mouse (ev)
  8790. "Open file link or URL at mouse.
  8791. See the docstring of `org-open-file' for details."
  8792. (interactive "e")
  8793. (mouse-set-point ev)
  8794. (if (eq major-mode 'org-agenda-mode)
  8795. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8796. (org-open-at-point))
  8797. (defvar org-window-config-before-follow-link nil
  8798. "The window configuration before following a link.
  8799. This is saved in case the need arises to restore it.")
  8800. (defvar org-open-link-marker (make-marker)
  8801. "Marker pointing to the location where `org-open-at-point; was called.")
  8802. ;;;###autoload
  8803. (defun org-open-at-point-global ()
  8804. "Follow a link like Org-mode does.
  8805. This command can be called in any mode to follow a link that has
  8806. Org-mode syntax."
  8807. (interactive)
  8808. (org-run-like-in-org-mode 'org-open-at-point))
  8809. ;;;###autoload
  8810. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8811. "Open a link in the string S, as if it was in Org-mode."
  8812. (interactive "sLink: \nP")
  8813. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8814. (with-temp-buffer
  8815. (let ((org-inhibit-startup (not reference-buffer)))
  8816. (org-mode)
  8817. (insert s)
  8818. (goto-char (point-min))
  8819. (when reference-buffer
  8820. (setq org-link-abbrev-alist-local
  8821. (with-current-buffer reference-buffer
  8822. org-link-abbrev-alist-local)))
  8823. (org-open-at-point arg reference-buffer)))))
  8824. (defvar org-open-at-point-functions nil
  8825. "Hook that is run when following a link at point.
  8826. Functions in this hook must return t if they identify and follow
  8827. a link at point. If they don't find anything interesting at point,
  8828. they must return nil.")
  8829. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8830. (defun org-open-at-point (&optional arg reference-buffer)
  8831. "Open link at or after point.
  8832. If there is no link at point, this function will search forward up to
  8833. the end of the current line.
  8834. Normally, files will be opened by an appropriate application. If the
  8835. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8836. With a double prefix argument, try to open outside of Emacs, in the
  8837. application the system uses for this file type."
  8838. (interactive "P")
  8839. ;; if in a code block, then open the block's results
  8840. (unless (call-interactively #'org-babel-open-src-block-result)
  8841. (org-load-modules-maybe)
  8842. (move-marker org-open-link-marker (point))
  8843. (setq org-window-config-before-follow-link (current-window-configuration))
  8844. (org-remove-occur-highlights nil nil t)
  8845. (cond
  8846. ((and (org-at-heading-p)
  8847. (not (org-at-timestamp-p t))
  8848. (not (org-in-regexp
  8849. (concat org-plain-link-re "\\|"
  8850. org-bracket-link-regexp "\\|"
  8851. org-angle-link-re "\\|"
  8852. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8853. (not (get-text-property (point) 'org-linked-text)))
  8854. (or (org-offer-links-in-entry arg)
  8855. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8856. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8857. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8858. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8859. (not (org-in-regexp org-bracket-link-regexp)))
  8860. (org-footnote-action))
  8861. (t
  8862. (let (type path link line search (pos (point)))
  8863. (catch 'match
  8864. (save-excursion
  8865. (skip-chars-forward "^]\n\r")
  8866. (when (org-in-regexp org-bracket-link-regexp 1)
  8867. (setq link (org-extract-attributes
  8868. (org-link-unescape (org-match-string-no-properties 1))))
  8869. (while (string-match " *\n *" link)
  8870. (setq link (replace-match " " t t link)))
  8871. (setq link (org-link-expand-abbrev link))
  8872. (cond
  8873. ((or (file-name-absolute-p link)
  8874. (string-match "^\\.\\.?/" link))
  8875. (setq type "file" path link))
  8876. ((string-match org-link-re-with-space3 link)
  8877. (setq type (match-string 1 link) path (match-string 2 link)))
  8878. ((string-match "^help:+\\(.+\\)" link)
  8879. (setq type "help" path (match-string 1 link)))
  8880. (t (setq type "thisfile" path link)))
  8881. (throw 'match t)))
  8882. (when (get-text-property (point) 'org-linked-text)
  8883. (setq type "thisfile"
  8884. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8885. (1+ (point)) (point))
  8886. path (buffer-substring
  8887. (or (previous-single-property-change pos 'org-linked-text)
  8888. (point-min))
  8889. (or (next-single-property-change pos 'org-linked-text)
  8890. (point-max))))
  8891. (throw 'match t))
  8892. (save-excursion
  8893. (when (or (org-in-regexp org-angle-link-re)
  8894. (and (goto-char (car (org-in-regexp org-plain-link-re)))
  8895. (save-match-data (not (looking-back "\\[\\[")))))
  8896. (setq type (match-string 1)
  8897. path (org-link-unescape (match-string 2)))
  8898. (throw 'match t)))
  8899. (save-excursion
  8900. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8901. (setq type "tags"
  8902. path (match-string 1))
  8903. (while (string-match ":" path)
  8904. (setq path (replace-match "+" t t path)))
  8905. (throw 'match t)))
  8906. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8907. (setq type "tree-match"
  8908. path (match-string 1))
  8909. (throw 'match t)))
  8910. (unless path
  8911. (error "No link found"))
  8912. ;; switch back to reference buffer
  8913. ;; needed when if called in a temporary buffer through
  8914. ;; org-open-link-from-string
  8915. (with-current-buffer (or reference-buffer (current-buffer))
  8916. ;; Remove any trailing spaces in path
  8917. (if (string-match " +\\'" path)
  8918. (setq path (replace-match "" t t path)))
  8919. (if (and org-link-translation-function
  8920. (fboundp org-link-translation-function))
  8921. ;; Check if we need to translate the link
  8922. (let ((tmp (funcall org-link-translation-function type path)))
  8923. (setq type (car tmp) path (cdr tmp))))
  8924. (cond
  8925. ((assoc type org-link-protocols)
  8926. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8927. ((equal type "help")
  8928. (let ((f-or-v (intern path)))
  8929. (cond ((fboundp f-or-v)
  8930. (describe-function f-or-v))
  8931. ((boundp f-or-v)
  8932. (describe-variable f-or-v))
  8933. (t (error "Not a known function or variable")))))
  8934. ((equal type "mailto")
  8935. (let ((cmd (car org-link-mailto-program))
  8936. (args (cdr org-link-mailto-program)) args1
  8937. (address path) (subject "") a)
  8938. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8939. (setq address (match-string 1 path)
  8940. subject (org-link-escape (match-string 2 path))))
  8941. (while args
  8942. (cond
  8943. ((not (stringp (car args))) (push (pop args) args1))
  8944. (t (setq a (pop args))
  8945. (if (string-match "%a" a)
  8946. (setq a (replace-match address t t a)))
  8947. (if (string-match "%s" a)
  8948. (setq a (replace-match subject t t a)))
  8949. (push a args1))))
  8950. (apply cmd (nreverse args1))))
  8951. ((member type '("http" "https" "ftp" "news"))
  8952. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8953. (org-link-escape
  8954. path org-link-escape-chars-browser)
  8955. path))))
  8956. ((string= type "doi")
  8957. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8958. (org-link-escape
  8959. path org-link-escape-chars-browser)
  8960. path))))
  8961. ((member type '("message"))
  8962. (browse-url (concat type ":" path)))
  8963. ((string= type "tags")
  8964. (org-tags-view arg path))
  8965. ((string= type "tree-match")
  8966. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8967. ((string= type "file")
  8968. (if (string-match "::\\([0-9]+\\)\\'" path)
  8969. (setq line (string-to-number (match-string 1 path))
  8970. path (substring path 0 (match-beginning 0)))
  8971. (if (string-match "::\\(.+\\)\\'" path)
  8972. (setq search (match-string 1 path)
  8973. path (substring path 0 (match-beginning 0)))))
  8974. (if (string-match "[*?{]" (file-name-nondirectory path))
  8975. (dired path)
  8976. (org-open-file path arg line search)))
  8977. ((string= type "shell")
  8978. (let ((buf (generate-new-buffer "*Org Shell Output"))
  8979. (cmd path))
  8980. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8981. (string-match org-confirm-shell-link-not-regexp cmd))
  8982. (not org-confirm-shell-link-function)
  8983. (funcall org-confirm-shell-link-function
  8984. (format "Execute \"%s\" in shell? "
  8985. (org-add-props cmd nil
  8986. 'face 'org-warning))))
  8987. (progn
  8988. (message "Executing %s" cmd)
  8989. (shell-command cmd buf)
  8990. (if (featurep 'midnight)
  8991. (setq clean-buffer-list-kill-buffer-names
  8992. (cons buf clean-buffer-list-kill-buffer-names))))
  8993. (error "Abort"))))
  8994. ((string= type "elisp")
  8995. (let ((cmd path))
  8996. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8997. (string-match org-confirm-elisp-link-not-regexp cmd))
  8998. (not org-confirm-elisp-link-function)
  8999. (funcall org-confirm-elisp-link-function
  9000. (format "Execute \"%s\" as elisp? "
  9001. (org-add-props cmd nil
  9002. 'face 'org-warning))))
  9003. (message "%s => %s" cmd
  9004. (if (equal (string-to-char cmd) ?\()
  9005. (eval (read cmd))
  9006. (call-interactively (read cmd))))
  9007. (error "Abort"))))
  9008. ((and (string= type "thisfile")
  9009. (run-hook-with-args-until-success
  9010. 'org-open-link-functions path)))
  9011. ((string= type "thisfile")
  9012. (if arg
  9013. (switch-to-buffer-other-window
  9014. (org-get-buffer-for-internal-link (current-buffer)))
  9015. (org-mark-ring-push))
  9016. (let ((cmd `(org-link-search
  9017. ,path
  9018. ,(cond ((equal arg '(4)) ''occur)
  9019. ((equal arg '(16)) ''org-occur))
  9020. ,pos)))
  9021. (condition-case nil (let ((org-link-search-inhibit-query t))
  9022. (eval cmd))
  9023. (error (progn (widen) (eval cmd))))))
  9024. (t (browse-url-at-point)))))))
  9025. (move-marker org-open-link-marker nil)
  9026. (run-hook-with-args 'org-follow-link-hook)))
  9027. (defun org-offer-links-in-entry (&optional nth zero)
  9028. "Offer links in the current entry and follow the selected link.
  9029. If there is only one link, follow it immediately as well.
  9030. If NTH is an integer, immediately pick the NTH link found.
  9031. If ZERO is a string, check also this string for a link, and if
  9032. there is one, offer it as link number zero."
  9033. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9034. "\\(" org-angle-link-re "\\)\\|"
  9035. "\\(" org-plain-link-re "\\)"))
  9036. (cnt ?0)
  9037. (in-emacs (if (integerp nth) nil nth))
  9038. have-zero end links link c)
  9039. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9040. (push (match-string 0 zero) links)
  9041. (setq cnt (1- cnt) have-zero t))
  9042. (save-excursion
  9043. (org-back-to-heading t)
  9044. (setq end (save-excursion (outline-next-heading) (point)))
  9045. (while (re-search-forward re end t)
  9046. (push (match-string 0) links))
  9047. (setq links (org-uniquify (reverse links))))
  9048. (cond
  9049. ((null links)
  9050. (message "No links"))
  9051. ((equal (length links) 1)
  9052. (setq link (list (car links))))
  9053. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9054. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  9055. (t ; we have to select a link
  9056. (save-excursion
  9057. (save-window-excursion
  9058. (delete-other-windows)
  9059. (with-output-to-temp-buffer "*Select Link*"
  9060. (mapc (lambda (l)
  9061. (if (not (string-match org-bracket-link-regexp l))
  9062. (princ (format "[%c] %s\n" (incf cnt)
  9063. (org-remove-angle-brackets l)))
  9064. (if (match-end 3)
  9065. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9066. (match-string 3 l) (match-string 1 l)))
  9067. (princ (format "[%c] %s\n" (incf cnt)
  9068. (match-string 1 l))))))
  9069. links))
  9070. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9071. (message "Select link to open, RET to open all:")
  9072. (setq c (read-char-exclusive))
  9073. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9074. (when (equal c ?q) (error "Abort"))
  9075. (if (equal c ?\C-m)
  9076. (setq link links)
  9077. (setq nth (- c ?0))
  9078. (if have-zero (setq nth (1+ nth)))
  9079. (unless (and (integerp nth) (>= (length links) nth))
  9080. (error "Invalid link selection"))
  9081. (setq link (list (nth (1- nth) links))))))
  9082. (if link
  9083. (let ((buf (current-buffer)))
  9084. (dolist (l link)
  9085. (org-open-link-from-string l in-emacs buf))
  9086. t)
  9087. nil)))
  9088. ;; Add special file links that specify the way of opening
  9089. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9090. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9091. (defun org-open-file-with-system (path)
  9092. "Open file at PATH using the system way of opening it."
  9093. (org-open-file path 'system))
  9094. (defun org-open-file-with-emacs (path)
  9095. "Open file at PATH in Emacs."
  9096. (org-open-file path 'emacs))
  9097. (defun org-remove-file-link-modifiers ()
  9098. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  9099. (goto-char (point-min))
  9100. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  9101. (org-if-unprotected
  9102. (replace-match "file:" t t))))
  9103. (eval-after-load "org-exp"
  9104. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  9105. 'org-remove-file-link-modifiers))
  9106. ;;;; Time estimates
  9107. (defun org-get-effort (&optional pom)
  9108. "Get the effort estimate for the current entry."
  9109. (org-entry-get pom org-effort-property))
  9110. ;;; File search
  9111. (defvar org-create-file-search-functions nil
  9112. "List of functions to construct the right search string for a file link.
  9113. These functions are called in turn with point at the location to
  9114. which the link should point.
  9115. A function in the hook should first test if it would like to
  9116. handle this file type, for example by checking the `major-mode'
  9117. or the file extension. If it decides not to handle this file, it
  9118. should just return nil to give other functions a chance. If it
  9119. does handle the file, it must return the search string to be used
  9120. when following the link. The search string will be part of the
  9121. file link, given after a double colon, and `org-open-at-point'
  9122. will automatically search for it. If special measures must be
  9123. taken to make the search successful, another function should be
  9124. added to the companion hook `org-execute-file-search-functions',
  9125. which see.
  9126. A function in this hook may also use `setq' to set the variable
  9127. `description' to provide a suggestion for the descriptive text to
  9128. be used for this link when it gets inserted into an Org-mode
  9129. buffer with \\[org-insert-link].")
  9130. (defvar org-execute-file-search-functions nil
  9131. "List of functions to execute a file search triggered by a link.
  9132. Functions added to this hook must accept a single argument, the
  9133. search string that was part of the file link, the part after the
  9134. double colon. The function must first check if it would like to
  9135. handle this search, for example by checking the `major-mode' or
  9136. the file extension. If it decides not to handle this search, it
  9137. should just return nil to give other functions a chance. If it
  9138. does handle the search, it must return a non-nil value to keep
  9139. other functions from trying.
  9140. Each function can access the current prefix argument through the
  9141. variable `current-prefix-argument'. Note that a single prefix is
  9142. used to force opening a link in Emacs, so it may be good to only
  9143. use a numeric or double prefix to guide the search function.
  9144. In case this is needed, a function in this hook can also restore
  9145. the window configuration before `org-open-at-point' was called using:
  9146. (set-window-configuration org-window-config-before-follow-link)")
  9147. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9148. (defun org-link-search (s &optional type avoid-pos stealth)
  9149. "Search for a link search option.
  9150. If S is surrounded by forward slashes, it is interpreted as a
  9151. regular expression. In org-mode files, this will create an `org-occur'
  9152. sparse tree. In ordinary files, `occur' will be used to list matches.
  9153. If the current buffer is in `dired-mode', grep will be used to search
  9154. in all files. If AVOID-POS is given, ignore matches near that position.
  9155. When optional argument STEALTH is non-nil, do not modify
  9156. visibility around point, thus ignoring
  9157. `org-show-hierarchy-above', `org-show-following-heading' and
  9158. `org-show-siblings' variables."
  9159. (let ((case-fold-search t)
  9160. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9161. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9162. (append '(("") (" ") ("\t") ("\n"))
  9163. org-emphasis-alist)
  9164. "\\|") "\\)"))
  9165. (pos (point))
  9166. (pre nil) (post nil)
  9167. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9168. (cond
  9169. ;; First check if there are any special search functions
  9170. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9171. ;; Now try the builtin stuff
  9172. ((and (equal (string-to-char s0) ?#)
  9173. (> (length s0) 1)
  9174. (save-excursion
  9175. (goto-char (point-min))
  9176. (and
  9177. (re-search-forward
  9178. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9179. (setq type 'dedicated
  9180. pos (match-beginning 0))))
  9181. ;; There is an exact target for this
  9182. (goto-char pos)
  9183. (org-back-to-heading t)))
  9184. ((save-excursion
  9185. (goto-char (point-min))
  9186. (and
  9187. (re-search-forward
  9188. (concat "<<" (regexp-quote s0) ">>") nil t)
  9189. (setq type 'dedicated
  9190. pos (match-beginning 0))))
  9191. ;; There is an exact target for this
  9192. (goto-char pos))
  9193. ((save-excursion
  9194. (goto-char (point-min))
  9195. (and
  9196. (re-search-forward
  9197. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9198. (setq type 'dedicated pos (match-beginning 0))))
  9199. ;; Found an invisible target.
  9200. (goto-char pos))
  9201. ((save-excursion
  9202. (goto-char (point-min))
  9203. (and
  9204. (re-search-forward
  9205. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9206. (setq type 'dedicated pos (match-beginning 0))))
  9207. ;; Found an element with a matching #+name affiliated keyword.
  9208. (goto-char pos))
  9209. ((and (string-match "^(\\(.*\\))$" s0)
  9210. (save-excursion
  9211. (goto-char (point-min))
  9212. (and
  9213. (re-search-forward
  9214. (concat "[^[]" (regexp-quote
  9215. (format org-coderef-label-format
  9216. (match-string 1 s0))))
  9217. nil t)
  9218. (setq type 'dedicated
  9219. pos (1+ (match-beginning 0))))))
  9220. ;; There is a coderef target for this
  9221. (goto-char pos))
  9222. ((string-match "^/\\(.*\\)/$" s)
  9223. ;; A regular expression
  9224. (cond
  9225. ((derived-mode-p 'org-mode)
  9226. (org-occur (match-string 1 s)))
  9227. ;;((eq major-mode 'dired-mode)
  9228. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9229. (t (org-do-occur (match-string 1 s)))))
  9230. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9231. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9232. (goto-char (point-min))
  9233. (cond
  9234. ((let (case-fold-search)
  9235. (re-search-forward (format org-complex-heading-regexp-format
  9236. (regexp-quote s))
  9237. nil t))
  9238. ;; OK, found a match
  9239. (setq type 'dedicated)
  9240. (goto-char (match-beginning 0)))
  9241. ((and (not org-link-search-inhibit-query)
  9242. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9243. (y-or-n-p "No match - create this as a new heading? "))
  9244. (goto-char (point-max))
  9245. (or (bolp) (newline))
  9246. (insert "* " s "\n")
  9247. (beginning-of-line 0))
  9248. (t
  9249. (goto-char pos)
  9250. (error "No match"))))
  9251. (t
  9252. ;; A normal search string
  9253. (when (equal (string-to-char s) ?*)
  9254. ;; Anchor on headlines, post may include tags.
  9255. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9256. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9257. s (substring s 1)))
  9258. (remove-text-properties
  9259. 0 (length s)
  9260. '(face nil mouse-face nil keymap nil fontified nil) s)
  9261. ;; Make a series of regular expressions to find a match
  9262. (setq words (org-split-string s "[ \n\r\t]+")
  9263. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9264. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9265. "\\)" markers)
  9266. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9267. re2a (concat "[ \t\r\n]" re2a_)
  9268. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9269. re4 (concat "[^a-zA-Z_]" re4_)
  9270. re1 (concat pre re2 post)
  9271. re3 (concat pre (if pre re4_ re4) post)
  9272. re5 (concat pre ".*" re4)
  9273. re2 (concat pre re2)
  9274. re2a (concat pre (if pre re2a_ re2a))
  9275. re4 (concat pre (if pre re4_ re4))
  9276. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9277. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9278. re5 "\\)"
  9279. ))
  9280. (cond
  9281. ((eq type 'org-occur) (org-occur reall))
  9282. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9283. (t (goto-char (point-min))
  9284. (setq type 'fuzzy)
  9285. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9286. (org-search-not-self 1 re1 nil t)
  9287. (org-search-not-self 1 re2 nil t)
  9288. (org-search-not-self 1 re2a nil t)
  9289. (org-search-not-self 1 re3 nil t)
  9290. (org-search-not-self 1 re4 nil t)
  9291. (org-search-not-self 1 re5 nil t)
  9292. )
  9293. (goto-char (match-beginning 1))
  9294. (goto-char pos)
  9295. (error "No match"))))))
  9296. (and (derived-mode-p 'org-mode)
  9297. (not stealth)
  9298. (org-show-context 'link-search))
  9299. type))
  9300. (defun org-search-not-self (group &rest args)
  9301. "Execute `re-search-forward', but only accept matches that do not
  9302. enclose the position of `org-open-link-marker'."
  9303. (let ((m org-open-link-marker))
  9304. (catch 'exit
  9305. (while (apply 're-search-forward args)
  9306. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9307. (goto-char (match-end group))
  9308. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9309. (> (match-beginning 0) (marker-position m))
  9310. (< (match-end 0) (marker-position m)))
  9311. (save-match-data
  9312. (or (not (org-in-regexp
  9313. org-bracket-link-analytic-regexp 1))
  9314. (not (match-end 4)) ; no description
  9315. (and (<= (match-beginning 4) (point))
  9316. (>= (match-end 4) (point))))))
  9317. (throw 'exit (point))))))))
  9318. (defun org-get-buffer-for-internal-link (buffer)
  9319. "Return a buffer to be used for displaying the link target of internal links."
  9320. (cond
  9321. ((not org-display-internal-link-with-indirect-buffer)
  9322. buffer)
  9323. ((string-match "(Clone)$" (buffer-name buffer))
  9324. (message "Buffer is already a clone, not making another one")
  9325. ;; we also do not modify visibility in this case
  9326. buffer)
  9327. (t ; make a new indirect buffer for displaying the link
  9328. (let* ((bn (buffer-name buffer))
  9329. (ibn (concat bn "(Clone)"))
  9330. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9331. (with-current-buffer ib (org-overview))
  9332. ib))))
  9333. (defun org-do-occur (regexp &optional cleanup)
  9334. "Call the Emacs command `occur'.
  9335. If CLEANUP is non-nil, remove the printout of the regular expression
  9336. in the *Occur* buffer. This is useful if the regex is long and not useful
  9337. to read."
  9338. (occur regexp)
  9339. (when cleanup
  9340. (let ((cwin (selected-window)) win beg end)
  9341. (when (setq win (get-buffer-window "*Occur*"))
  9342. (select-window win))
  9343. (goto-char (point-min))
  9344. (when (re-search-forward "match[a-z]+" nil t)
  9345. (setq beg (match-end 0))
  9346. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9347. (setq end (1- (match-beginning 0)))))
  9348. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9349. (goto-char (point-min))
  9350. (select-window cwin))))
  9351. ;;; The mark ring for links jumps
  9352. (defvar org-mark-ring nil
  9353. "Mark ring for positions before jumps in Org-mode.")
  9354. (defvar org-mark-ring-last-goto nil
  9355. "Last position in the mark ring used to go back.")
  9356. ;; Fill and close the ring
  9357. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9358. (loop for i from 1 to org-mark-ring-length do
  9359. (push (make-marker) org-mark-ring))
  9360. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9361. org-mark-ring)
  9362. (defun org-mark-ring-push (&optional pos buffer)
  9363. "Put the current position or POS into the mark ring and rotate it."
  9364. (interactive)
  9365. (setq pos (or pos (point)))
  9366. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9367. (move-marker (car org-mark-ring)
  9368. (or pos (point))
  9369. (or buffer (current-buffer)))
  9370. (message "%s"
  9371. (substitute-command-keys
  9372. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9373. (defun org-mark-ring-goto (&optional n)
  9374. "Jump to the previous position in the mark ring.
  9375. With prefix arg N, jump back that many stored positions. When
  9376. called several times in succession, walk through the entire ring.
  9377. Org-mode commands jumping to a different position in the current file,
  9378. or to another Org-mode file, automatically push the old position
  9379. onto the ring."
  9380. (interactive "p")
  9381. (let (p m)
  9382. (if (eq last-command this-command)
  9383. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9384. (setq p org-mark-ring))
  9385. (setq org-mark-ring-last-goto p)
  9386. (setq m (car p))
  9387. (org-pop-to-buffer-same-window (marker-buffer m))
  9388. (goto-char m)
  9389. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9390. (defun org-remove-angle-brackets (s)
  9391. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9392. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9393. s)
  9394. (defun org-add-angle-brackets (s)
  9395. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9396. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9397. s)
  9398. (defun org-remove-double-quotes (s)
  9399. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9400. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9401. s)
  9402. ;;; Following specific links
  9403. (defun org-follow-timestamp-link ()
  9404. (cond
  9405. ((org-at-date-range-p t)
  9406. (let ((org-agenda-start-on-weekday)
  9407. (t1 (match-string 1))
  9408. (t2 (match-string 2)))
  9409. (setq t1 (time-to-days (org-time-string-to-time t1))
  9410. t2 (time-to-days (org-time-string-to-time t2)))
  9411. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9412. ((org-at-timestamp-p t)
  9413. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9414. (substring (match-string 1) 0 10)))
  9415. 1))
  9416. (t (error "This should not happen"))))
  9417. ;;; Following file links
  9418. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9419. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9420. (declare-function mailcap-mime-info
  9421. "mailcap" (string &optional request no-decode))
  9422. (defvar org-wait nil)
  9423. (defun org-open-file (path &optional in-emacs line search)
  9424. "Open the file at PATH.
  9425. First, this expands any special file name abbreviations. Then the
  9426. configuration variable `org-file-apps' is checked if it contains an
  9427. entry for this file type, and if yes, the corresponding command is launched.
  9428. If no application is found, Emacs simply visits the file.
  9429. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9430. With a double \\[universal-argument] \\[universal-argument] \
  9431. prefix arg, Org tries to avoid opening in Emacs
  9432. and to use an external application to visit the file.
  9433. Optional LINE specifies a line to go to, optional SEARCH a string
  9434. to search for. If LINE or SEARCH is given, the file will be
  9435. opened in Emacs, unless an entry from org-file-apps that makes
  9436. use of groups in a regexp matches.
  9437. If you want to change the way frames are used when following a
  9438. link, please customize `org-link-frame-setup'.
  9439. If the file does not exist, an error is thrown."
  9440. (let* ((file (if (equal path "")
  9441. buffer-file-name
  9442. (substitute-in-file-name (expand-file-name path))))
  9443. (file-apps (append org-file-apps (org-default-apps)))
  9444. (apps (org-remove-if
  9445. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9446. (apps-dlink (org-remove-if-not
  9447. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9448. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9449. (dirp (if remp nil (file-directory-p file)))
  9450. (file (if (and dirp org-open-directory-means-index-dot-org)
  9451. (concat (file-name-as-directory file) "index.org")
  9452. file))
  9453. (a-m-a-p (assq 'auto-mode apps))
  9454. (dfile (downcase file))
  9455. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9456. (link (cond ((and (eq line nil)
  9457. (eq search nil))
  9458. file)
  9459. (line
  9460. (concat file "::" (number-to-string line)))
  9461. (search
  9462. (concat file "::" search))))
  9463. (dlink (downcase link))
  9464. (old-buffer (current-buffer))
  9465. (old-pos (point))
  9466. (old-mode major-mode)
  9467. ext cmd link-match-data)
  9468. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9469. (setq ext (match-string 1 dfile))
  9470. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9471. (setq ext (match-string 1 dfile))))
  9472. (cond
  9473. ((member in-emacs '((16) system))
  9474. (setq cmd (cdr (assoc 'system apps))))
  9475. (in-emacs (setq cmd 'emacs))
  9476. (t
  9477. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9478. (and dirp (cdr (assoc 'directory apps)))
  9479. ; first, try matching against apps-dlink
  9480. ; if we get a match here, store the match data for later
  9481. (let ((match (assoc-default dlink apps-dlink
  9482. 'string-match)))
  9483. (if match
  9484. (progn (setq link-match-data (match-data))
  9485. match)
  9486. (progn (setq in-emacs (or in-emacs line search))
  9487. nil))) ; if we have no match in apps-dlink,
  9488. ; always open the file in emacs if line or search
  9489. ; is given (for backwards compatibility)
  9490. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9491. 'string-match)
  9492. (cdr (assoc ext apps))
  9493. (cdr (assoc t apps))))))
  9494. (when (eq cmd 'system)
  9495. (setq cmd (cdr (assoc 'system apps))))
  9496. (when (eq cmd 'default)
  9497. (setq cmd (cdr (assoc t apps))))
  9498. (when (eq cmd 'mailcap)
  9499. (require 'mailcap)
  9500. (mailcap-parse-mailcaps)
  9501. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9502. (command (mailcap-mime-info mime-type)))
  9503. (if (stringp command)
  9504. (setq cmd command)
  9505. (setq cmd 'emacs))))
  9506. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9507. (not (file-exists-p file))
  9508. (not org-open-non-existing-files))
  9509. (error "No such file: %s" file))
  9510. (cond
  9511. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9512. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9513. (while (string-match "['\"]%s['\"]" cmd)
  9514. (setq cmd (replace-match "%s" t t cmd)))
  9515. (while (string-match "%s" cmd)
  9516. (setq cmd (replace-match
  9517. (save-match-data
  9518. (shell-quote-argument
  9519. (convert-standard-filename file)))
  9520. t t cmd)))
  9521. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9522. (save-match-data
  9523. (let ((match-index 1)
  9524. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9525. (set-match-data link-match-data)
  9526. (while (<= match-index number-of-groups)
  9527. (let ((regex (concat "%" (number-to-string match-index)))
  9528. (replace-with (match-string match-index dlink)))
  9529. (while (string-match regex cmd)
  9530. (setq cmd (replace-match replace-with t t cmd))))
  9531. (setq match-index (+ match-index 1)))))
  9532. (save-window-excursion
  9533. (message "Running %s...done" cmd)
  9534. (start-process-shell-command cmd nil cmd)
  9535. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9536. ((or (stringp cmd)
  9537. (eq cmd 'emacs))
  9538. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9539. (widen)
  9540. (if line (org-goto-line line)
  9541. (if search (org-link-search search))))
  9542. ((consp cmd)
  9543. (let ((file (convert-standard-filename file)))
  9544. (save-match-data
  9545. (set-match-data link-match-data)
  9546. (eval cmd))))
  9547. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9548. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9549. (or (not (equal old-buffer (current-buffer)))
  9550. (not (equal old-pos (point))))
  9551. (org-mark-ring-push old-pos old-buffer))))
  9552. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9553. "This function returns non-nil if `entry' uses a regular
  9554. expression which should be matched against the whole link by
  9555. org-open-file.
  9556. It assumes that is the case when the entry uses a regular
  9557. expression which has at least one grouping construct and the
  9558. action is either a lisp form or a command string containing
  9559. '%1', i.e. using at least one subexpression match as a
  9560. parameter."
  9561. (let ((selector (car entry))
  9562. (action (cdr entry)))
  9563. (if (stringp selector)
  9564. (and (> (regexp-opt-depth selector) 0)
  9565. (or (and (stringp action)
  9566. (string-match "%[0-9]" action))
  9567. (consp action)))
  9568. nil)))
  9569. (defun org-default-apps ()
  9570. "Return the default applications for this operating system."
  9571. (cond
  9572. ((eq system-type 'darwin)
  9573. org-file-apps-defaults-macosx)
  9574. ((eq system-type 'windows-nt)
  9575. org-file-apps-defaults-windowsnt)
  9576. (t org-file-apps-defaults-gnu)))
  9577. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9578. "Convert extensions to regular expressions in the cars of LIST.
  9579. Also, weed out any non-string entries, because the return value is used
  9580. only for regexp matching.
  9581. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9582. point to the symbol `emacs', indicating that the file should
  9583. be opened in Emacs."
  9584. (append
  9585. (delq nil
  9586. (mapcar (lambda (x)
  9587. (if (not (stringp (car x)))
  9588. nil
  9589. (if (string-match "\\W" (car x))
  9590. x
  9591. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9592. list))
  9593. (if add-auto-mode
  9594. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9595. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9596. (defun org-file-remote-p (file)
  9597. "Test whether FILE specifies a location on a remote system.
  9598. Return non-nil if the location is indeed remote.
  9599. For example, the filename \"/user@host:/foo\" specifies a location
  9600. on the system \"/user@host:\"."
  9601. (cond ((fboundp 'file-remote-p)
  9602. (file-remote-p file))
  9603. ((fboundp 'tramp-handle-file-remote-p)
  9604. (tramp-handle-file-remote-p file))
  9605. ((and (boundp 'ange-ftp-name-format)
  9606. (string-match (car ange-ftp-name-format) file))
  9607. t)))
  9608. ;;;; Refiling
  9609. (defun org-get-org-file ()
  9610. "Read a filename, with default directory `org-directory'."
  9611. (let ((default (or org-default-notes-file remember-data-file)))
  9612. (read-file-name (format "File name [%s]: " default)
  9613. (file-name-as-directory org-directory)
  9614. default)))
  9615. (defun org-notes-order-reversed-p ()
  9616. "Check if the current file should receive notes in reversed order."
  9617. (cond
  9618. ((not org-reverse-note-order) nil)
  9619. ((eq t org-reverse-note-order) t)
  9620. ((not (listp org-reverse-note-order)) nil)
  9621. (t (catch 'exit
  9622. (let ((all org-reverse-note-order)
  9623. entry)
  9624. (while (setq entry (pop all))
  9625. (if (string-match (car entry) buffer-file-name)
  9626. (throw 'exit (cdr entry))))
  9627. nil)))))
  9628. (defvar org-refile-target-table nil
  9629. "The list of refile targets, created by `org-refile'.")
  9630. (defvar org-agenda-new-buffers nil
  9631. "Buffers created to visit agenda files.")
  9632. (defvar org-refile-cache nil
  9633. "Cache for refile targets.")
  9634. (defvar org-refile-markers nil
  9635. "All the markers used for caching refile locations.")
  9636. (defun org-refile-marker (pos)
  9637. "Get a new refile marker, but only if caching is in use."
  9638. (if (not org-refile-use-cache)
  9639. pos
  9640. (let ((m (make-marker)))
  9641. (move-marker m pos)
  9642. (push m org-refile-markers)
  9643. m)))
  9644. (defun org-refile-cache-clear ()
  9645. "Clear the refile cache and disable all the markers."
  9646. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9647. (setq org-refile-markers nil)
  9648. (setq org-refile-cache nil)
  9649. (message "Refile cache has been cleared"))
  9650. (defun org-refile-cache-check-set (set)
  9651. "Check if all the markers in the cache still have live buffers."
  9652. (let (marker)
  9653. (catch 'exit
  9654. (while (and set (setq marker (nth 3 (pop set))))
  9655. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9656. (when (and marker (null (marker-buffer marker)))
  9657. (message "not found") (sit-for 3)
  9658. (throw 'exit nil)))
  9659. t)))
  9660. (defun org-refile-cache-put (set &rest identifiers)
  9661. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9662. (let* ((key (sha1 (prin1-to-string identifiers)))
  9663. (entry (assoc key org-refile-cache)))
  9664. (if entry
  9665. (setcdr entry set)
  9666. (push (cons key set) org-refile-cache))))
  9667. (defun org-refile-cache-get (&rest identifiers)
  9668. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9669. (cond
  9670. ((not org-refile-cache) nil)
  9671. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9672. (t
  9673. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9674. org-refile-cache))))
  9675. (and set (org-refile-cache-check-set set) set)))))
  9676. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9677. "Produce a table with refile targets."
  9678. (let ((case-fold-search nil)
  9679. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9680. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9681. targets tgs txt re files f desc descre fast-path-p level pos0)
  9682. (message "Getting targets...")
  9683. (with-current-buffer (or default-buffer (current-buffer))
  9684. (while (setq entry (pop entries))
  9685. (setq files (car entry) desc (cdr entry))
  9686. (setq fast-path-p nil)
  9687. (cond
  9688. ((null files) (setq files (list (current-buffer))))
  9689. ((eq files 'org-agenda-files)
  9690. (setq files (org-agenda-files 'unrestricted)))
  9691. ((and (symbolp files) (fboundp files))
  9692. (setq files (funcall files)))
  9693. ((and (symbolp files) (boundp files))
  9694. (setq files (symbol-value files))))
  9695. (if (stringp files) (setq files (list files)))
  9696. (cond
  9697. ((eq (car desc) :tag)
  9698. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9699. ((eq (car desc) :todo)
  9700. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9701. ((eq (car desc) :regexp)
  9702. (setq descre (cdr desc)))
  9703. ((eq (car desc) :level)
  9704. (setq descre (concat "^\\*\\{" (number-to-string
  9705. (if org-odd-levels-only
  9706. (1- (* 2 (cdr desc)))
  9707. (cdr desc)))
  9708. "\\}[ \t]")))
  9709. ((eq (car desc) :maxlevel)
  9710. (setq fast-path-p t)
  9711. (setq descre (concat "^\\*\\{1," (number-to-string
  9712. (if org-odd-levels-only
  9713. (1- (* 2 (cdr desc)))
  9714. (cdr desc)))
  9715. "\\}[ \t]")))
  9716. (t (error "Bad refiling target description %s" desc)))
  9717. (while (setq f (pop files))
  9718. (with-current-buffer
  9719. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9720. (or
  9721. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9722. (progn
  9723. (if (bufferp f) (setq f (buffer-file-name
  9724. (buffer-base-buffer f))))
  9725. (setq f (and f (expand-file-name f)))
  9726. (if (eq org-refile-use-outline-path 'file)
  9727. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9728. (save-excursion
  9729. (save-restriction
  9730. (widen)
  9731. (goto-char (point-min))
  9732. (while (re-search-forward descre nil t)
  9733. (goto-char (setq pos0 (point-at-bol)))
  9734. (catch 'next
  9735. (when org-refile-target-verify-function
  9736. (save-match-data
  9737. (or (funcall org-refile-target-verify-function)
  9738. (throw 'next t))))
  9739. (when (and (looking-at org-complex-heading-regexp)
  9740. (not (member (match-string 4) excluded-entries))
  9741. (match-string 4))
  9742. (setq level (org-reduced-level
  9743. (- (match-end 1) (match-beginning 1)))
  9744. txt (org-link-display-format (match-string 4))
  9745. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9746. re (format org-complex-heading-regexp-format
  9747. (regexp-quote (match-string 4))))
  9748. (when org-refile-use-outline-path
  9749. (setq txt (mapconcat
  9750. 'org-protect-slash
  9751. (append
  9752. (if (eq org-refile-use-outline-path
  9753. 'file)
  9754. (list (file-name-nondirectory
  9755. (buffer-file-name
  9756. (buffer-base-buffer))))
  9757. (if (eq org-refile-use-outline-path
  9758. 'full-file-path)
  9759. (list (buffer-file-name
  9760. (buffer-base-buffer)))))
  9761. (org-get-outline-path fast-path-p
  9762. level txt)
  9763. (list txt))
  9764. "/")))
  9765. (push (list txt f re (org-refile-marker (point)))
  9766. tgs)))
  9767. (when (= (point) pos0)
  9768. ;; verification function has not moved point
  9769. (goto-char (point-at-eol))))))))
  9770. (when org-refile-use-cache
  9771. (org-refile-cache-put tgs (buffer-file-name) descre))
  9772. (setq targets (append tgs targets))
  9773. ))))
  9774. (message "Getting targets...done")
  9775. (nreverse targets)))
  9776. (defun org-protect-slash (s)
  9777. (while (string-match "/" s)
  9778. (setq s (replace-match "\\" t t s)))
  9779. s)
  9780. (defvar org-olpa (make-vector 20 nil))
  9781. (defun org-get-outline-path (&optional fastp level heading)
  9782. "Return the outline path to the current entry, as a list.
  9783. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9784. routine which makes outline path derivations for an entire file,
  9785. avoiding backtracing. Refile target collection makes use of that."
  9786. (if fastp
  9787. (progn
  9788. (if (> level 19)
  9789. (error "Outline path failure, more than 19 levels"))
  9790. (loop for i from level upto 19 do
  9791. (aset org-olpa i nil))
  9792. (prog1
  9793. (delq nil (append org-olpa nil))
  9794. (aset org-olpa level heading)))
  9795. (let (rtn case-fold-search)
  9796. (save-excursion
  9797. (save-restriction
  9798. (widen)
  9799. (while (org-up-heading-safe)
  9800. (when (looking-at org-complex-heading-regexp)
  9801. (push (org-match-string-no-properties 4) rtn)))
  9802. rtn)))))
  9803. (defun org-format-outline-path (path &optional width prefix)
  9804. "Format the outline path PATH for display.
  9805. Width is the maximum number of characters that is available.
  9806. Prefix is a prefix to be included in the returned string,
  9807. such as the file name."
  9808. (setq width (or width 79))
  9809. (if prefix (setq width (- width (length prefix))))
  9810. (if (not path)
  9811. (or prefix "")
  9812. (let* ((nsteps (length path))
  9813. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9814. (maxwidth (if (<= total-width width)
  9815. 10000 ;; everything fits
  9816. ;; we need to shorten the level headings
  9817. (/ (- width nsteps) nsteps)))
  9818. (org-odd-levels-only nil)
  9819. (n 0)
  9820. (total (1+ (length prefix))))
  9821. (setq maxwidth (max maxwidth 10))
  9822. (concat prefix
  9823. (mapconcat
  9824. (lambda (h)
  9825. (setq n (1+ n))
  9826. (if (and (= n nsteps) (< maxwidth 10000))
  9827. (setq maxwidth (- total-width total)))
  9828. (if (< (length h) maxwidth)
  9829. (progn (setq total (+ total (length h) 1)) h)
  9830. (setq h (substring h 0 (- maxwidth 2))
  9831. total (+ total maxwidth 1))
  9832. (if (string-match "[ \t]+\\'" h)
  9833. (setq h (substring h 0 (match-beginning 0))))
  9834. (setq h (concat h "..")))
  9835. (org-add-props h nil 'face
  9836. (nth (% (1- n) org-n-level-faces)
  9837. org-level-faces))
  9838. h)
  9839. path "/")))))
  9840. (defun org-display-outline-path (&optional file current)
  9841. "Display the current outline path in the echo area."
  9842. (interactive "P")
  9843. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9844. (case-fold-search nil)
  9845. (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
  9846. (if current (setq path (append path
  9847. (save-excursion
  9848. (org-back-to-heading t)
  9849. (if (looking-at org-complex-heading-regexp)
  9850. (list (match-string 4)))))))
  9851. (message "%s"
  9852. (org-format-outline-path
  9853. path
  9854. (1- (frame-width))
  9855. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9856. (defvar org-refile-history nil
  9857. "History for refiling operations.")
  9858. (defvar org-after-refile-insert-hook nil
  9859. "Hook run after `org-refile' has inserted its stuff at the new location.
  9860. Note that this is still *before* the stuff will be removed from
  9861. the *old* location.")
  9862. (defvar org-capture-last-stored-marker)
  9863. (defun org-refile (&optional goto default-buffer rfloc)
  9864. "Move the entry or entries at point to another heading.
  9865. The list of target headings is compiled using the information in
  9866. `org-refile-targets', which see.
  9867. At the target location, the entry is filed as a subitem of the target
  9868. heading. Depending on `org-reverse-note-order', the new subitem will
  9869. either be the first or the last subitem.
  9870. If there is an active region, all entries in that region will be moved.
  9871. However, the region must fulfill the requirement that the first heading
  9872. is the first one sets the top-level of the moved text - at most siblings
  9873. below it are allowed.
  9874. With prefix arg GOTO, the command will only visit the target location
  9875. and not actually move anything.
  9876. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9877. go to the location where the last refiling operation has put the subtree.
  9878. With a prefix argument of `2', refile to the running clock.
  9879. RFLOC can be a refile location obtained in a different way.
  9880. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9881. If you are using target caching (see `org-refile-use-cache'),
  9882. you have to clear the target cache in order to find new targets.
  9883. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9884. prefix argument (`C-u C-u C-u C-c C-w')."
  9885. (interactive "P")
  9886. (if (member goto '(0 (64)))
  9887. (org-refile-cache-clear)
  9888. (let* ((cbuf (current-buffer))
  9889. (regionp (org-region-active-p))
  9890. (region-start (and regionp (region-beginning)))
  9891. (region-end (and regionp (region-end)))
  9892. (region-length (and regionp (- region-end region-start)))
  9893. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9894. pos it nbuf file re level reversed)
  9895. (setq last-command nil)
  9896. (when regionp
  9897. (goto-char region-start)
  9898. (or (bolp) (goto-char (point-at-bol)))
  9899. (setq region-start (point))
  9900. (unless (or (org-kill-is-subtree-p
  9901. (buffer-substring region-start region-end))
  9902. (prog1 org-refile-active-region-within-subtree
  9903. (org-toggle-heading)))
  9904. (error "The region is not a (sequence of) subtree(s)")))
  9905. (if (equal goto '(16))
  9906. (org-refile-goto-last-stored)
  9907. (when (or
  9908. (and (equal goto 2)
  9909. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9910. (prog1
  9911. (setq it (list (or org-clock-heading "running clock")
  9912. (buffer-file-name
  9913. (marker-buffer org-clock-hd-marker))
  9914. ""
  9915. (marker-position org-clock-hd-marker)))
  9916. (setq goto nil)))
  9917. (setq it (or rfloc
  9918. (let (heading-text)
  9919. (save-excursion
  9920. (unless goto
  9921. (org-back-to-heading t)
  9922. (setq heading-text
  9923. (nth 4 (org-heading-components))))
  9924. (org-refile-get-location
  9925. (cond (goto "Goto")
  9926. (regionp "Refile region to")
  9927. (t (concat "Refile subtree \""
  9928. heading-text "\" to")))
  9929. default-buffer
  9930. (and (not (equal '(4) goto))
  9931. org-refile-allow-creating-parent-nodes)
  9932. goto))))))
  9933. (setq file (nth 1 it)
  9934. re (nth 2 it)
  9935. pos (nth 3 it))
  9936. (if (and (not goto)
  9937. pos
  9938. (equal (buffer-file-name) file)
  9939. (if regionp
  9940. (and (>= pos region-start)
  9941. (<= pos region-end))
  9942. (and (>= pos (point))
  9943. (< pos (save-excursion
  9944. (org-end-of-subtree t t))))))
  9945. (error "Cannot refile to position inside the tree or region"))
  9946. (setq nbuf (or (find-buffer-visiting file)
  9947. (find-file-noselect file)))
  9948. (if goto
  9949. (progn
  9950. (org-pop-to-buffer-same-window nbuf)
  9951. (goto-char pos)
  9952. (org-show-context 'org-goto))
  9953. (if regionp
  9954. (progn
  9955. (org-kill-new (buffer-substring region-start region-end))
  9956. (org-save-markers-in-region region-start region-end))
  9957. (org-copy-subtree 1 nil t))
  9958. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9959. (find-file-noselect file)))
  9960. (setq reversed (org-notes-order-reversed-p))
  9961. (save-excursion
  9962. (save-restriction
  9963. (widen)
  9964. (if pos
  9965. (progn
  9966. (goto-char pos)
  9967. (looking-at org-outline-regexp)
  9968. (setq level (org-get-valid-level (funcall outline-level) 1))
  9969. (goto-char
  9970. (if reversed
  9971. (or (outline-next-heading) (point-max))
  9972. (or (save-excursion (org-get-next-sibling))
  9973. (org-end-of-subtree t t)
  9974. (point-max)))))
  9975. (setq level 1)
  9976. (if (not reversed)
  9977. (goto-char (point-max))
  9978. (goto-char (point-min))
  9979. (or (outline-next-heading) (goto-char (point-max)))))
  9980. (if (not (bolp)) (newline))
  9981. (org-paste-subtree level)
  9982. (when org-log-refile
  9983. (org-add-log-setup 'refile nil nil 'findpos
  9984. org-log-refile)
  9985. (unless (eq org-log-refile 'note)
  9986. (save-excursion (org-add-log-note))))
  9987. (and org-auto-align-tags
  9988. (let ((org-loop-over-headlines-in-active-region nil))
  9989. (org-set-tags nil t)))
  9990. (bookmark-set "org-refile-last-stored")
  9991. ;; If we are refiling for capture, make sure that the
  9992. ;; last-capture pointers point here
  9993. (when (org-bound-and-true-p org-refile-for-capture)
  9994. (bookmark-set "org-capture-last-stored-marker")
  9995. (move-marker org-capture-last-stored-marker (point)))
  9996. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9997. (run-hooks 'org-after-refile-insert-hook))))
  9998. (if regionp
  9999. (delete-region (point) (+ (point) region-length))
  10000. (org-cut-subtree))
  10001. (when (featurep 'org-inlinetask)
  10002. (org-inlinetask-remove-END-maybe))
  10003. (setq org-markers-to-move nil)
  10004. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  10005. (defun org-refile-goto-last-stored ()
  10006. "Go to the location where the last refile was stored."
  10007. (interactive)
  10008. (bookmark-jump "org-refile-last-stored")
  10009. (message "This is the location of the last refile"))
  10010. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10011. no-exclude)
  10012. "Prompt the user for a refile location, using PROMPT.
  10013. PROMPT should not be suffixed with a colon and a space, because
  10014. this function appends the default value from
  10015. `org-refile-history' automatically, if that is not empty.
  10016. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10017. this is used for the GOTO interface."
  10018. (let ((org-refile-targets org-refile-targets)
  10019. (org-refile-use-outline-path org-refile-use-outline-path)
  10020. excluded-entries)
  10021. (when (and (derived-mode-p 'org-mode)
  10022. (not org-refile-use-cache)
  10023. (not no-exclude))
  10024. (org-map-tree
  10025. (lambda()
  10026. (setq excluded-entries
  10027. (append excluded-entries (list (org-get-heading t t)))))))
  10028. (setq org-refile-target-table
  10029. (org-refile-get-targets default-buffer excluded-entries)))
  10030. (unless org-refile-target-table
  10031. (error "No refile targets"))
  10032. (let* ((prompt (concat prompt
  10033. (and (car org-refile-history)
  10034. (concat " (default " (car org-refile-history) ")"))
  10035. ": "))
  10036. (cbuf (current-buffer))
  10037. (partial-completion-mode nil)
  10038. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10039. (cfunc (if (and org-refile-use-outline-path
  10040. org-outline-path-complete-in-steps)
  10041. 'org-olpath-completing-read
  10042. 'org-icompleting-read))
  10043. (extra (if org-refile-use-outline-path "/" ""))
  10044. (filename (and cfn (expand-file-name cfn)))
  10045. (tbl (mapcar
  10046. (lambda (x)
  10047. (if (and (not (member org-refile-use-outline-path
  10048. '(file full-file-path)))
  10049. (not (equal filename (nth 1 x))))
  10050. (cons (concat (car x) extra " ("
  10051. (file-name-nondirectory (nth 1 x)) ")")
  10052. (cdr x))
  10053. (cons (concat (car x) extra) (cdr x))))
  10054. org-refile-target-table))
  10055. (completion-ignore-case t)
  10056. pa answ parent-target child parent old-hist)
  10057. (setq old-hist org-refile-history)
  10058. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10059. nil 'org-refile-history (car org-refile-history)))
  10060. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10061. (org-refile-check-position pa)
  10062. (if pa
  10063. (progn
  10064. (when (or (not org-refile-history)
  10065. (not (eq old-hist org-refile-history))
  10066. (not (equal (car pa) (car org-refile-history))))
  10067. (setq org-refile-history
  10068. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10069. org-refile-history
  10070. (cdr org-refile-history))))
  10071. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10072. (pop org-refile-history)))
  10073. pa)
  10074. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10075. (progn
  10076. (setq parent (match-string 1 answ)
  10077. child (match-string 2 answ))
  10078. (setq parent-target (or (assoc parent tbl)
  10079. (assoc (concat parent "/") tbl)))
  10080. (when (and parent-target
  10081. (or (eq new-nodes t)
  10082. (and (eq new-nodes 'confirm)
  10083. (y-or-n-p (format "Create new node \"%s\"? "
  10084. child)))))
  10085. (org-refile-new-child parent-target child)))
  10086. (error "Invalid target location")))))
  10087. (declare-function org-string-nw-p "org-macs.el" (s))
  10088. (defun org-refile-check-position (refile-pointer)
  10089. "Check if the refile pointer matches the readline to which it points."
  10090. (let* ((file (nth 1 refile-pointer))
  10091. (re (nth 2 refile-pointer))
  10092. (pos (nth 3 refile-pointer))
  10093. buffer)
  10094. (when (org-string-nw-p re)
  10095. (setq buffer (if (markerp pos)
  10096. (marker-buffer pos)
  10097. (or (find-buffer-visiting file)
  10098. (find-file-noselect file))))
  10099. (with-current-buffer buffer
  10100. (save-excursion
  10101. (save-restriction
  10102. (widen)
  10103. (goto-char pos)
  10104. (beginning-of-line 1)
  10105. (unless (org-looking-at-p re)
  10106. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  10107. (defun org-refile-new-child (parent-target child)
  10108. "Use refile target PARENT-TARGET to add new CHILD below it."
  10109. (unless parent-target
  10110. (error "Cannot find parent for new node"))
  10111. (let ((file (nth 1 parent-target))
  10112. (pos (nth 3 parent-target))
  10113. level)
  10114. (with-current-buffer (or (find-buffer-visiting file)
  10115. (find-file-noselect file))
  10116. (save-excursion
  10117. (save-restriction
  10118. (widen)
  10119. (if pos
  10120. (goto-char pos)
  10121. (goto-char (point-max))
  10122. (if (not (bolp)) (newline)))
  10123. (when (looking-at org-outline-regexp)
  10124. (setq level (funcall outline-level))
  10125. (org-end-of-subtree t t))
  10126. (org-back-over-empty-lines)
  10127. (insert "\n" (make-string
  10128. (if pos (org-get-valid-level level 1) 1) ?*)
  10129. " " child "\n")
  10130. (beginning-of-line 0)
  10131. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10132. (defun org-olpath-completing-read (prompt collection &rest args)
  10133. "Read an outline path like a file name."
  10134. (let ((thetable collection)
  10135. (org-completion-use-ido nil) ; does not work with ido.
  10136. (org-completion-use-iswitchb nil)) ; or iswitchb
  10137. (apply
  10138. 'org-icompleting-read prompt
  10139. (lambda (string predicate &optional flag)
  10140. (let (rtn r f (l (length string)))
  10141. (cond
  10142. ((eq flag nil)
  10143. ;; try completion
  10144. (try-completion string thetable))
  10145. ((eq flag t)
  10146. ;; all-completions
  10147. (setq rtn (all-completions string thetable predicate))
  10148. (mapcar
  10149. (lambda (x)
  10150. (setq r (substring x l))
  10151. (if (string-match " ([^)]*)$" x)
  10152. (setq f (match-string 0 x))
  10153. (setq f ""))
  10154. (if (string-match "/" r)
  10155. (concat string (substring r 0 (match-end 0)) f)
  10156. x))
  10157. rtn))
  10158. ((eq flag 'lambda)
  10159. ;; exact match?
  10160. (assoc string thetable)))))
  10161. args)))
  10162. ;;;; Dynamic blocks
  10163. (defun org-find-dblock (name)
  10164. "Find the first dynamic block with name NAME in the buffer.
  10165. If not found, stay at current position and return nil."
  10166. (let ((case-fold-search t) pos)
  10167. (save-excursion
  10168. (goto-char (point-min))
  10169. (setq pos (and (re-search-forward
  10170. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10171. (match-beginning 0))))
  10172. (if pos (goto-char pos))
  10173. pos))
  10174. (defconst org-dblock-start-re
  10175. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10176. "Matches the start line of a dynamic block, with parameters.")
  10177. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10178. "Matches the end of a dynamic block.")
  10179. (defun org-create-dblock (plist)
  10180. "Create a dynamic block section, with parameters taken from PLIST.
  10181. PLIST must contain a :name entry which is used as name of the block."
  10182. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10183. (end-of-line 1)
  10184. (newline))
  10185. (let ((col (current-column))
  10186. (name (plist-get plist :name)))
  10187. (insert "#+BEGIN: " name)
  10188. (while plist
  10189. (if (eq (car plist) :name)
  10190. (setq plist (cddr plist))
  10191. (insert " " (prin1-to-string (pop plist)))))
  10192. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10193. (beginning-of-line -2)))
  10194. (defun org-prepare-dblock ()
  10195. "Prepare dynamic block for refresh.
  10196. This empties the block, puts the cursor at the insert position and returns
  10197. the property list including an extra property :name with the block name."
  10198. (unless (looking-at org-dblock-start-re)
  10199. (error "Not at a dynamic block"))
  10200. (let* ((begdel (1+ (match-end 0)))
  10201. (name (org-no-properties (match-string 1)))
  10202. (params (append (list :name name)
  10203. (read (concat "(" (match-string 3) ")")))))
  10204. (save-excursion
  10205. (beginning-of-line 1)
  10206. (skip-chars-forward " \t")
  10207. (setq params (plist-put params :indentation-column (current-column))))
  10208. (unless (re-search-forward org-dblock-end-re nil t)
  10209. (error "Dynamic block not terminated"))
  10210. (setq params
  10211. (append params
  10212. (list :content (buffer-substring
  10213. begdel (match-beginning 0)))))
  10214. (delete-region begdel (match-beginning 0))
  10215. (goto-char begdel)
  10216. (open-line 1)
  10217. params))
  10218. (defun org-map-dblocks (&optional command)
  10219. "Apply COMMAND to all dynamic blocks in the current buffer.
  10220. If COMMAND is not given, use `org-update-dblock'."
  10221. (let ((cmd (or command 'org-update-dblock)))
  10222. (save-excursion
  10223. (goto-char (point-min))
  10224. (while (re-search-forward org-dblock-start-re nil t)
  10225. (goto-char (match-beginning 0))
  10226. (save-excursion
  10227. (condition-case nil
  10228. (funcall cmd)
  10229. (error (message "Error during update of dynamic block"))))
  10230. (unless (re-search-forward org-dblock-end-re nil t)
  10231. (error "Dynamic block not terminated"))))))
  10232. (defun org-dblock-update (&optional arg)
  10233. "User command for updating dynamic blocks.
  10234. Update the dynamic block at point. With prefix ARG, update all dynamic
  10235. blocks in the buffer."
  10236. (interactive "P")
  10237. (if arg
  10238. (org-update-all-dblocks)
  10239. (or (looking-at org-dblock-start-re)
  10240. (org-beginning-of-dblock))
  10241. (org-update-dblock)))
  10242. (defun org-update-dblock ()
  10243. "Update the dynamic block at point.
  10244. This means to empty the block, parse for parameters and then call
  10245. the correct writing function."
  10246. (interactive)
  10247. (save-window-excursion
  10248. (let* ((pos (point))
  10249. (line (org-current-line))
  10250. (params (org-prepare-dblock))
  10251. (name (plist-get params :name))
  10252. (indent (plist-get params :indentation-column))
  10253. (cmd (intern (concat "org-dblock-write:" name))))
  10254. (message "Updating dynamic block `%s' at line %d..." name line)
  10255. (funcall cmd params)
  10256. (message "Updating dynamic block `%s' at line %d...done" name line)
  10257. (goto-char pos)
  10258. (when (and indent (> indent 0))
  10259. (setq indent (make-string indent ?\ ))
  10260. (save-excursion
  10261. (org-beginning-of-dblock)
  10262. (forward-line 1)
  10263. (while (not (looking-at org-dblock-end-re))
  10264. (insert indent)
  10265. (beginning-of-line 2))
  10266. (when (looking-at org-dblock-end-re)
  10267. (and (looking-at "[ \t]+")
  10268. (replace-match ""))
  10269. (insert indent)))))))
  10270. (defun org-beginning-of-dblock ()
  10271. "Find the beginning of the dynamic block at point.
  10272. Error if there is no such block at point."
  10273. (let ((pos (point))
  10274. beg)
  10275. (end-of-line 1)
  10276. (if (and (re-search-backward org-dblock-start-re nil t)
  10277. (setq beg (match-beginning 0))
  10278. (re-search-forward org-dblock-end-re nil t)
  10279. (> (match-end 0) pos))
  10280. (goto-char beg)
  10281. (goto-char pos)
  10282. (error "Not in a dynamic block"))))
  10283. ;;;###autoload
  10284. (defun org-update-all-dblocks ()
  10285. "Update all dynamic blocks in the buffer.
  10286. This function can be used in a hook."
  10287. (interactive)
  10288. (when (derived-mode-p 'org-mode)
  10289. (org-map-dblocks 'org-update-dblock)))
  10290. ;;;; Completion
  10291. (defconst org-additional-option-like-keywords
  10292. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10293. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10294. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10295. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10296. "BEGIN:" "END:"
  10297. "ORGTBL" "TBLFM:" "TBLNAME:"
  10298. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10299. "BEGIN_VERBATIM" "END_VERBATIM"
  10300. "BEGIN_QUOTE" "END_QUOTE"
  10301. "BEGIN_VERSE" "END_VERSE"
  10302. "BEGIN_CENTER" "END_CENTER"
  10303. "BEGIN_SRC" "END_SRC"
  10304. "BEGIN_RESULT" "END_RESULT"
  10305. "BEGIN_lstlisting" "END_lstlisting"
  10306. "NAME:" "RESULTS:"
  10307. "HEADER:" "HEADERS:"
  10308. "COLUMNS:" "PROPERTY:"
  10309. "CAPTION:" "LABEL:"
  10310. "SETUPFILE:"
  10311. "INCLUDE:"
  10312. "BIND:"
  10313. "MACRO:"))
  10314. (defconst org-options-keywords
  10315. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10316. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10317. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10318. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10319. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10320. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:"
  10321. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10322. (defconst org-additional-option-like-keywords-for-flyspell
  10323. (delete-dups
  10324. (split-string
  10325. (mapconcat (lambda(k)
  10326. (replace-regexp-in-string
  10327. "_\\|:" " "
  10328. (concat k " " (downcase k) " " (upcase k))))
  10329. (append org-options-keywords org-additional-option-like-keywords)
  10330. " ")
  10331. " +" t)))
  10332. (defcustom org-structure-template-alist
  10333. '(
  10334. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10335. "<src lang=\"?\">\n\n</src>")
  10336. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10337. "<example>\n?\n</example>")
  10338. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10339. "<quote>\n?\n</quote>")
  10340. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10341. "<verse>\n?\n</verse>")
  10342. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10343. "<center>\n?\n</center>")
  10344. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10345. "<literal style=\"latex\">\n?\n</literal>")
  10346. ("L" "#+LaTeX: "
  10347. "<literal style=\"latex\">?</literal>")
  10348. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10349. "<literal style=\"html\">\n?\n</literal>")
  10350. ("H" "#+HTML: "
  10351. "<literal style=\"html\">?</literal>")
  10352. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10353. ("A" "#+ASCII: ")
  10354. ("i" "#+INDEX: ?"
  10355. "#+INDEX: ?")
  10356. ("I" "#+INCLUDE: %file ?"
  10357. "<include file=%file markup=\"?\">")
  10358. )
  10359. "Structure completion elements.
  10360. This is a list of abbreviation keys and values. The value gets inserted
  10361. if you type `<' followed by the key and then press the completion key,
  10362. usually `M-TAB'. %file will be replaced by a file name after prompting
  10363. for the file using completion. The cursor will be placed at the position
  10364. of the `?` in the template.
  10365. There are two templates for each key, the first uses the original Org syntax,
  10366. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10367. the default when the /org-mtags.el/ module has been loaded. See also the
  10368. variable `org-mtags-prefer-muse-templates'."
  10369. :group 'org-completion
  10370. :type '(repeat
  10371. (string :tag "Key")
  10372. (string :tag "Template")
  10373. (string :tag "Muse Template")))
  10374. (defun org-try-structure-completion ()
  10375. "Try to complete a structure template before point.
  10376. This looks for strings like \"<e\" on an otherwise empty line and
  10377. expands them."
  10378. (let ((l (buffer-substring (point-at-bol) (point)))
  10379. a)
  10380. (when (and (looking-at "[ \t]*$")
  10381. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10382. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10383. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10384. (match-beginning 1)) a)
  10385. t)))
  10386. (defun org-complete-expand-structure-template (start cell)
  10387. "Expand a structure template."
  10388. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10389. (rpl (nth (if musep 2 1) cell))
  10390. (ind ""))
  10391. (delete-region start (point))
  10392. (when (string-match "\\`#\\+" rpl)
  10393. (cond
  10394. ((bolp))
  10395. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10396. (setq ind (buffer-substring (point-at-bol) (point))))
  10397. (t (newline))))
  10398. (setq start (point))
  10399. (if (string-match "%file" rpl)
  10400. (setq rpl (replace-match
  10401. (concat
  10402. "\""
  10403. (save-match-data
  10404. (abbreviate-file-name (read-file-name "Include file: ")))
  10405. "\"")
  10406. t t rpl)))
  10407. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10408. (concat "\n" ind)))
  10409. (insert rpl)
  10410. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10411. ;;;; TODO, DEADLINE, Comments
  10412. (defun org-toggle-comment ()
  10413. "Change the COMMENT state of an entry."
  10414. (interactive)
  10415. (save-excursion
  10416. (org-back-to-heading)
  10417. (let (case-fold-search)
  10418. (cond
  10419. ((looking-at (format org-heading-keyword-regexp-format
  10420. org-comment-string))
  10421. (goto-char (match-end 1))
  10422. (looking-at (concat " +" org-comment-string))
  10423. (replace-match "" t t)
  10424. (when (eolp) (insert " ")))
  10425. ((looking-at org-outline-regexp)
  10426. (goto-char (match-end 0))
  10427. (insert org-comment-string " "))))))
  10428. (defvar org-last-todo-state-is-todo nil
  10429. "This is non-nil when the last TODO state change led to a TODO state.
  10430. If the last change removed the TODO tag or switched to DONE, then
  10431. this is nil.")
  10432. (defvar org-setting-tags nil) ; dynamically skipped
  10433. (defvar org-todo-setup-filter-hook nil
  10434. "Hook for functions that pre-filter todo specs.
  10435. Each function takes a todo spec and returns either nil or the spec
  10436. transformed into canonical form." )
  10437. (defvar org-todo-get-default-hook nil
  10438. "Hook for functions that get a default item for todo.
  10439. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10440. nil or a string to be used for the todo mark." )
  10441. (defvar org-agenda-headline-snapshot-before-repeat)
  10442. (defun org-current-effective-time ()
  10443. "Return current time adjusted for `org-extend-today-until' variable."
  10444. (let* ((ct (org-current-time))
  10445. (dct (decode-time ct))
  10446. (ct1
  10447. (cond
  10448. (org-use-last-clock-out-time-as-effective-time
  10449. (or (org-clock-get-last-clock-out-time) ct))
  10450. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10451. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10452. (t ct))))
  10453. ct1))
  10454. (defun org-todo-yesterday (&optional arg)
  10455. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10456. (interactive "P")
  10457. (if (eq major-mode 'org-agenda-mode)
  10458. (apply 'org-agenda-todo-yesterday arg)
  10459. (let* ((hour (third (decode-time
  10460. (org-current-time))))
  10461. (org-extend-today-until (1+ hour)))
  10462. (org-todo arg))))
  10463. (defun org-todo (&optional arg)
  10464. "Change the TODO state of an item.
  10465. The state of an item is given by a keyword at the start of the heading,
  10466. like
  10467. *** TODO Write paper
  10468. *** DONE Call mom
  10469. The different keywords are specified in the variable `org-todo-keywords'.
  10470. By default the available states are \"TODO\" and \"DONE\".
  10471. So for this example: when the item starts with TODO, it is changed to DONE.
  10472. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10473. DONE are present, add TODO at the beginning of the heading.
  10474. With \\[universal-argument] prefix arg, use completion to determine the new \
  10475. state.
  10476. With numeric prefix arg, switch to that state.
  10477. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10478. keywords (nextset).
  10479. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10480. With a numeric prefix arg of 0, inhibit note taking for the change.
  10481. For calling through lisp, arg is also interpreted in the following way:
  10482. 'none -> empty state
  10483. \"\"(empty string) -> switch to empty state
  10484. 'done -> switch to DONE
  10485. 'nextset -> switch to the next set of keywords
  10486. 'previousset -> switch to the previous set of keywords
  10487. \"WAITING\" -> switch to the specified keyword, but only if it
  10488. really is a member of `org-todo-keywords'."
  10489. (interactive "P")
  10490. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10491. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10492. 'region-start-level 'region))
  10493. org-loop-over-headlines-in-active-region)
  10494. (org-map-entries
  10495. `(org-todo ,arg)
  10496. org-loop-over-headlines-in-active-region
  10497. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10498. (if (equal arg '(16)) (setq arg 'nextset))
  10499. (let ((org-blocker-hook org-blocker-hook)
  10500. (case-fold-search nil))
  10501. (when (equal arg '(64))
  10502. (setq arg nil org-blocker-hook nil))
  10503. (when (and org-blocker-hook
  10504. (or org-inhibit-blocking
  10505. (org-entry-get nil "NOBLOCKING")))
  10506. (setq org-blocker-hook nil))
  10507. (save-excursion
  10508. (catch 'exit
  10509. (org-back-to-heading t)
  10510. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10511. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10512. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10513. (let* ((match-data (match-data))
  10514. (startpos (point-at-bol))
  10515. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10516. (org-log-done org-log-done)
  10517. (org-log-repeat org-log-repeat)
  10518. (org-todo-log-states org-todo-log-states)
  10519. (org-inhibit-logging
  10520. (if (equal arg 0)
  10521. (progn (setq arg nil) 'note) org-inhibit-logging))
  10522. (this (match-string 1))
  10523. (hl-pos (match-beginning 0))
  10524. (head (org-get-todo-sequence-head this))
  10525. (ass (assoc head org-todo-kwd-alist))
  10526. (interpret (nth 1 ass))
  10527. (done-word (nth 3 ass))
  10528. (final-done-word (nth 4 ass))
  10529. (org-last-state (or this ""))
  10530. (completion-ignore-case t)
  10531. (member (member this org-todo-keywords-1))
  10532. (tail (cdr member))
  10533. (org-state (cond
  10534. ((and org-todo-key-trigger
  10535. (or (and (equal arg '(4))
  10536. (eq org-use-fast-todo-selection 'prefix))
  10537. (and (not arg) org-use-fast-todo-selection
  10538. (not (eq org-use-fast-todo-selection
  10539. 'prefix)))))
  10540. ;; Use fast selection
  10541. (org-fast-todo-selection))
  10542. ((and (equal arg '(4))
  10543. (or (not org-use-fast-todo-selection)
  10544. (not org-todo-key-trigger)))
  10545. ;; Read a state with completion
  10546. (org-icompleting-read
  10547. "State: " (mapcar (lambda(x) (list x))
  10548. org-todo-keywords-1)
  10549. nil t))
  10550. ((eq arg 'right)
  10551. (if this
  10552. (if tail (car tail) nil)
  10553. (car org-todo-keywords-1)))
  10554. ((eq arg 'left)
  10555. (if (equal member org-todo-keywords-1)
  10556. nil
  10557. (if this
  10558. (nth (- (length org-todo-keywords-1)
  10559. (length tail) 2)
  10560. org-todo-keywords-1)
  10561. (org-last org-todo-keywords-1))))
  10562. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10563. (setq arg nil))) ; hack to fall back to cycling
  10564. (arg
  10565. ;; user or caller requests a specific state
  10566. (cond
  10567. ((equal arg "") nil)
  10568. ((eq arg 'none) nil)
  10569. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10570. ((eq arg 'nextset)
  10571. (or (car (cdr (member head org-todo-heads)))
  10572. (car org-todo-heads)))
  10573. ((eq arg 'previousset)
  10574. (let ((org-todo-heads (reverse org-todo-heads)))
  10575. (or (car (cdr (member head org-todo-heads)))
  10576. (car org-todo-heads))))
  10577. ((car (member arg org-todo-keywords-1)))
  10578. ((stringp arg)
  10579. (error "State `%s' not valid in this file" arg))
  10580. ((nth (1- (prefix-numeric-value arg))
  10581. org-todo-keywords-1))))
  10582. ((null member) (or head (car org-todo-keywords-1)))
  10583. ((equal this final-done-word) nil) ;; -> make empty
  10584. ((null tail) nil) ;; -> first entry
  10585. ((memq interpret '(type priority))
  10586. (if (eq this-command last-command)
  10587. (car tail)
  10588. (if (> (length tail) 0)
  10589. (or done-word (car org-done-keywords))
  10590. nil)))
  10591. (t
  10592. (car tail))))
  10593. (org-state (or
  10594. (run-hook-with-args-until-success
  10595. 'org-todo-get-default-hook org-state org-last-state)
  10596. org-state))
  10597. (next (if org-state (concat " " org-state " ") " "))
  10598. (change-plist (list :type 'todo-state-change :from this :to org-state
  10599. :position startpos))
  10600. dolog now-done-p)
  10601. (when org-blocker-hook
  10602. (setq org-last-todo-state-is-todo
  10603. (not (member this org-done-keywords)))
  10604. (unless (save-excursion
  10605. (save-match-data
  10606. (org-with-wide-buffer
  10607. (run-hook-with-args-until-failure
  10608. 'org-blocker-hook change-plist))))
  10609. (if (org-called-interactively-p 'interactive)
  10610. (error "TODO state change from %s to %s blocked" this org-state)
  10611. ;; fail silently
  10612. (message "TODO state change from %s to %s blocked" this org-state)
  10613. (throw 'exit nil))))
  10614. (store-match-data match-data)
  10615. (replace-match next t t)
  10616. (unless (pos-visible-in-window-p hl-pos)
  10617. (message "TODO state changed to %s" (org-trim next)))
  10618. (unless head
  10619. (setq head (org-get-todo-sequence-head org-state)
  10620. ass (assoc head org-todo-kwd-alist)
  10621. interpret (nth 1 ass)
  10622. done-word (nth 3 ass)
  10623. final-done-word (nth 4 ass)))
  10624. (when (memq arg '(nextset previousset))
  10625. (message "Keyword-Set %d/%d: %s"
  10626. (- (length org-todo-sets) -1
  10627. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10628. (length org-todo-sets)
  10629. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10630. (setq org-last-todo-state-is-todo
  10631. (not (member org-state org-done-keywords)))
  10632. (setq now-done-p (and (member org-state org-done-keywords)
  10633. (not (member this org-done-keywords))))
  10634. (and logging (org-local-logging logging))
  10635. (when (and (or org-todo-log-states org-log-done)
  10636. (not (eq org-inhibit-logging t))
  10637. (not (memq arg '(nextset previousset))))
  10638. ;; we need to look at recording a time and note
  10639. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10640. (nth 2 (assoc this org-todo-log-states))))
  10641. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10642. (setq dolog 'time))
  10643. (when (and org-state
  10644. (member org-state org-not-done-keywords)
  10645. (not (member this org-not-done-keywords)))
  10646. ;; This is now a todo state and was not one before
  10647. ;; If there was a CLOSED time stamp, get rid of it.
  10648. (org-add-planning-info nil nil 'closed))
  10649. (when (and now-done-p org-log-done)
  10650. ;; It is now done, and it was not done before
  10651. (org-add-planning-info 'closed (org-current-effective-time))
  10652. (if (and (not dolog) (eq 'note org-log-done))
  10653. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10654. (when (and org-state dolog)
  10655. ;; This is a non-nil state, and we need to log it
  10656. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10657. ;; Fixup tag positioning
  10658. (org-todo-trigger-tag-changes org-state)
  10659. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10660. (when org-provide-todo-statistics
  10661. (org-update-parent-todo-statistics))
  10662. (run-hooks 'org-after-todo-state-change-hook)
  10663. (if (and arg (not (member org-state org-done-keywords)))
  10664. (setq head (org-get-todo-sequence-head org-state)))
  10665. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10666. ;; Do we need to trigger a repeat?
  10667. (when now-done-p
  10668. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10669. ;; This is for the agenda, take a snapshot of the headline.
  10670. (save-match-data
  10671. (setq org-agenda-headline-snapshot-before-repeat
  10672. (org-get-heading))))
  10673. (org-auto-repeat-maybe org-state))
  10674. ;; Fixup cursor location if close to the keyword
  10675. (if (and (outline-on-heading-p)
  10676. (not (bolp))
  10677. (save-excursion (beginning-of-line 1)
  10678. (looking-at org-todo-line-regexp))
  10679. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10680. (progn
  10681. (goto-char (or (match-end 2) (match-end 1)))
  10682. (and (looking-at " ") (just-one-space))))
  10683. (when org-trigger-hook
  10684. (save-excursion
  10685. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10686. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10687. "Block turning an entry into a TODO, using the hierarchy.
  10688. This checks whether the current task should be blocked from state
  10689. changes. Such blocking occurs when:
  10690. 1. The task has children which are not all in a completed state.
  10691. 2. A task has a parent with the property :ORDERED:, and there
  10692. are siblings prior to the current task with incomplete
  10693. status.
  10694. 3. The parent of the task is blocked because it has siblings that should
  10695. be done first, or is child of a block grandparent TODO entry."
  10696. (if (not org-enforce-todo-dependencies)
  10697. t ; if locally turned off don't block
  10698. (catch 'dont-block
  10699. ;; If this is not a todo state change, or if this entry is already DONE,
  10700. ;; do not block
  10701. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10702. (member (plist-get change-plist :from)
  10703. (cons 'done org-done-keywords))
  10704. (member (plist-get change-plist :to)
  10705. (cons 'todo org-not-done-keywords))
  10706. (not (plist-get change-plist :to)))
  10707. (throw 'dont-block t))
  10708. ;; If this task has children, and any are undone, it's blocked
  10709. (save-excursion
  10710. (org-back-to-heading t)
  10711. (let ((this-level (funcall outline-level)))
  10712. (outline-next-heading)
  10713. (let ((child-level (funcall outline-level)))
  10714. (while (and (not (eobp))
  10715. (> child-level this-level))
  10716. ;; this todo has children, check whether they are all
  10717. ;; completed
  10718. (if (and (not (org-entry-is-done-p))
  10719. (org-entry-is-todo-p))
  10720. (throw 'dont-block nil))
  10721. (outline-next-heading)
  10722. (setq child-level (funcall outline-level))))))
  10723. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10724. ;; any previous siblings are undone, it's blocked
  10725. (save-excursion
  10726. (org-back-to-heading t)
  10727. (let* ((pos (point))
  10728. (parent-pos (and (org-up-heading-safe) (point))))
  10729. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10730. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10731. (forward-line 1)
  10732. (re-search-forward org-not-done-heading-regexp pos t))
  10733. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10734. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10735. (while t
  10736. (goto-char parent-pos)
  10737. (if (not (looking-at org-not-done-heading-regexp))
  10738. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10739. (setq pos (point))
  10740. (setq parent-pos (and (org-up-heading-safe) (point)))
  10741. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10742. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10743. (forward-line 1)
  10744. (re-search-forward org-not-done-heading-regexp pos t))
  10745. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10746. (defcustom org-track-ordered-property-with-tag nil
  10747. "Should the ORDERED property also be shown as a tag?
  10748. The ORDERED property decides if an entry should require subtasks to be
  10749. completed in sequence. Since a property is not very visible, setting
  10750. this option means that toggling the ORDERED property with the command
  10751. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10752. not relevant for the behavior, but it makes things more visible.
  10753. Note that toggling the tag with tags commands will not change the property
  10754. and therefore not influence behavior!
  10755. This can be t, meaning the tag ORDERED should be used, It can also be a
  10756. string to select a different tag for this task."
  10757. :group 'org-todo
  10758. :type '(choice
  10759. (const :tag "No tracking" nil)
  10760. (const :tag "Track with ORDERED tag" t)
  10761. (string :tag "Use other tag")))
  10762. (defun org-toggle-ordered-property ()
  10763. "Toggle the ORDERED property of the current entry.
  10764. For better visibility, you can track the value of this property with a tag.
  10765. See variable `org-track-ordered-property-with-tag'."
  10766. (interactive)
  10767. (let* ((t1 org-track-ordered-property-with-tag)
  10768. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10769. (save-excursion
  10770. (org-back-to-heading)
  10771. (if (org-entry-get nil "ORDERED")
  10772. (progn
  10773. (org-delete-property "ORDERED")
  10774. (and tag (org-toggle-tag tag 'off))
  10775. (message "Subtasks can be completed in arbitrary order"))
  10776. (org-entry-put nil "ORDERED" "t")
  10777. (and tag (org-toggle-tag tag 'on))
  10778. (message "Subtasks must be completed in sequence")))))
  10779. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10780. (defun org-block-todo-from-checkboxes (change-plist)
  10781. "Block turning an entry into a TODO, using checkboxes.
  10782. This checks whether the current task should be blocked from state
  10783. changes because there are unchecked boxes in this entry."
  10784. (if (not org-enforce-todo-checkbox-dependencies)
  10785. t ; if locally turned off don't block
  10786. (catch 'dont-block
  10787. ;; If this is not a todo state change, or if this entry is already DONE,
  10788. ;; do not block
  10789. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10790. (member (plist-get change-plist :from)
  10791. (cons 'done org-done-keywords))
  10792. (member (plist-get change-plist :to)
  10793. (cons 'todo org-not-done-keywords))
  10794. (not (plist-get change-plist :to)))
  10795. (throw 'dont-block t))
  10796. ;; If this task has checkboxes that are not checked, it's blocked
  10797. (save-excursion
  10798. (org-back-to-heading t)
  10799. (let ((beg (point)) end)
  10800. (outline-next-heading)
  10801. (setq end (point))
  10802. (goto-char beg)
  10803. (if (org-list-search-forward
  10804. (concat (org-item-beginning-re)
  10805. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10806. "\\[[- ]\\]")
  10807. end t)
  10808. (progn
  10809. (if (boundp 'org-blocked-by-checkboxes)
  10810. (setq org-blocked-by-checkboxes t))
  10811. (throw 'dont-block nil)))))
  10812. t))) ; do not block
  10813. (defun org-entry-blocked-p ()
  10814. "Is the current entry blocked?"
  10815. (if (org-entry-get nil "NOBLOCKING")
  10816. nil ;; Never block this entry
  10817. (not
  10818. (run-hook-with-args-until-failure
  10819. 'org-blocker-hook
  10820. (list :type 'todo-state-change
  10821. :position (point)
  10822. :from 'todo
  10823. :to 'done)))))
  10824. (defun org-update-statistics-cookies (all)
  10825. "Update the statistics cookie, either from TODO or from checkboxes.
  10826. This should be called with the cursor in a line with a statistics cookie."
  10827. (interactive "P")
  10828. (if all
  10829. (progn
  10830. (org-update-checkbox-count 'all)
  10831. (org-map-entries 'org-update-parent-todo-statistics))
  10832. (if (not (org-at-heading-p))
  10833. (org-update-checkbox-count)
  10834. (let ((pos (move-marker (make-marker) (point)))
  10835. end l1 l2)
  10836. (ignore-errors (org-back-to-heading t))
  10837. (if (not (org-at-heading-p))
  10838. (org-update-checkbox-count)
  10839. (setq l1 (org-outline-level))
  10840. (setq end (save-excursion
  10841. (outline-next-heading)
  10842. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10843. (point)))
  10844. (if (and (save-excursion
  10845. (re-search-forward
  10846. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10847. (not (save-excursion (re-search-forward
  10848. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10849. (org-update-checkbox-count)
  10850. (if (and l2 (> l2 l1))
  10851. (progn
  10852. (goto-char end)
  10853. (org-update-parent-todo-statistics))
  10854. (goto-char pos)
  10855. (beginning-of-line 1)
  10856. (while (re-search-forward
  10857. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10858. (point-at-eol) t)
  10859. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10860. (goto-char pos)
  10861. (move-marker pos nil)))))
  10862. (defvar org-entry-property-inherited-from) ;; defined below
  10863. (defun org-update-parent-todo-statistics ()
  10864. "Update any statistics cookie in the parent of the current headline.
  10865. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10866. the entire subtree and this will travel up the hierarchy and update
  10867. statistics everywhere."
  10868. (let* ((prop (save-excursion (org-up-heading-safe)
  10869. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10870. (recursive (or (not org-hierarchical-todo-statistics)
  10871. (and prop (string-match "\\<recursive\\>" prop))))
  10872. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10873. 0))
  10874. (first t)
  10875. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10876. level ltoggle l1 new ndel
  10877. (cnt-all 0) (cnt-done 0) is-percent kwd
  10878. checkbox-beg ov ovs ove cookie-present)
  10879. (catch 'exit
  10880. (save-excursion
  10881. (beginning-of-line 1)
  10882. (setq ltoggle (funcall outline-level))
  10883. ;; Three situations are to consider:
  10884. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10885. ;; to the top-level ancestor on the headline;
  10886. ;; 2. If parent has "recursive" property, repeat up to the
  10887. ;; headline setting that property, taking inheritance into
  10888. ;; account;
  10889. ;; 3. Else, move up to direct parent and proceed only once.
  10890. (while (and (setq level (org-up-heading-safe))
  10891. (or recursive first)
  10892. (>= (point) lim))
  10893. (setq first nil cookie-present nil)
  10894. (unless (and level
  10895. (not (string-match
  10896. "\\<checkbox\\>"
  10897. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10898. "")))))
  10899. (throw 'exit nil))
  10900. (while (re-search-forward box-re (point-at-eol) t)
  10901. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10902. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10903. (save-match-data
  10904. (unless (outline-next-heading) (throw 'exit nil))
  10905. (while (and (looking-at org-complex-heading-regexp)
  10906. (> (setq l1 (length (match-string 1))) level))
  10907. (setq kwd (and (or recursive (= l1 ltoggle))
  10908. (match-string 2)))
  10909. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10910. (and (listp org-provide-todo-statistics)
  10911. (or (member kwd org-provide-todo-statistics)
  10912. (member kwd org-done-keywords))))
  10913. (setq cnt-all (1+ cnt-all))
  10914. (if (eq org-provide-todo-statistics t)
  10915. (and kwd (setq cnt-all (1+ cnt-all)))))
  10916. (and (member kwd org-done-keywords)
  10917. (setq cnt-done (1+ cnt-done)))
  10918. (outline-next-heading)))
  10919. (setq new
  10920. (if is-percent
  10921. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10922. (format "[%d/%d]" cnt-done cnt-all))
  10923. ndel (- (match-end 0) checkbox-beg))
  10924. ;; handle overlays when updating cookie from column view
  10925. (when (setq ov (car (overlays-at checkbox-beg)))
  10926. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10927. (delete-overlay ov))
  10928. (goto-char checkbox-beg)
  10929. (insert new)
  10930. (delete-region (point) (+ (point) ndel))
  10931. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10932. (when ov (move-overlay ov ovs ove)))
  10933. (when cookie-present
  10934. (run-hook-with-args 'org-after-todo-statistics-hook
  10935. cnt-done (- cnt-all cnt-done))))))
  10936. (run-hooks 'org-todo-statistics-hook)))
  10937. (defvar org-after-todo-statistics-hook nil
  10938. "Hook that is called after a TODO statistics cookie has been updated.
  10939. Each function is called with two arguments: the number of not-done entries
  10940. and the number of done entries.
  10941. For example, the following function, when added to this hook, will switch
  10942. an entry to DONE when all children are done, and back to TODO when new
  10943. entries are set to a TODO status. Note that this hook is only called
  10944. when there is a statistics cookie in the headline!
  10945. (defun org-summary-todo (n-done n-not-done)
  10946. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10947. (let (org-log-done org-log-states) ; turn off logging
  10948. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10949. ")
  10950. (defvar org-todo-statistics-hook nil
  10951. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10952. This hook runs even if there is no statistics cookie present, in which case
  10953. `org-after-todo-statistics-hook' would not run.")
  10954. (defun org-todo-trigger-tag-changes (state)
  10955. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10956. (let ((l org-todo-state-tags-triggers)
  10957. changes)
  10958. (when (or (not state) (equal state ""))
  10959. (setq changes (append changes (cdr (assoc "" l)))))
  10960. (when (and (stringp state) (> (length state) 0))
  10961. (setq changes (append changes (cdr (assoc state l)))))
  10962. (when (member state org-not-done-keywords)
  10963. (setq changes (append changes (cdr (assoc 'todo l)))))
  10964. (when (member state org-done-keywords)
  10965. (setq changes (append changes (cdr (assoc 'done l)))))
  10966. (dolist (c changes)
  10967. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10968. (defun org-local-logging (value)
  10969. "Get logging settings from a property VALUE."
  10970. (let* (words w a)
  10971. ;; directly set the variables, they are already local.
  10972. (setq org-log-done nil
  10973. org-log-repeat nil
  10974. org-todo-log-states nil)
  10975. (setq words (org-split-string value))
  10976. (while (setq w (pop words))
  10977. (cond
  10978. ((setq a (assoc w org-startup-options))
  10979. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10980. (set (nth 1 a) (nth 2 a))))
  10981. ((setq a (org-extract-log-state-settings w))
  10982. (and (member (car a) org-todo-keywords-1)
  10983. (push a org-todo-log-states)))))))
  10984. (defun org-get-todo-sequence-head (kwd)
  10985. "Return the head of the TODO sequence to which KWD belongs.
  10986. If KWD is not set, check if there is a text property remembering the
  10987. right sequence."
  10988. (let (p)
  10989. (cond
  10990. ((not kwd)
  10991. (or (get-text-property (point-at-bol) 'org-todo-head)
  10992. (progn
  10993. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10994. nil (point-at-eol)))
  10995. (get-text-property p 'org-todo-head))))
  10996. ((not (member kwd org-todo-keywords-1))
  10997. (car org-todo-keywords-1))
  10998. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10999. (defun org-fast-todo-selection ()
  11000. "Fast TODO keyword selection with single keys.
  11001. Returns the new TODO keyword, or nil if no state change should occur."
  11002. (let* ((fulltable org-todo-key-alist)
  11003. (done-keywords org-done-keywords) ;; needed for the faces.
  11004. (maxlen (apply 'max (mapcar
  11005. (lambda (x)
  11006. (if (stringp (car x)) (string-width (car x)) 0))
  11007. fulltable)))
  11008. (expert nil)
  11009. (fwidth (+ maxlen 3 1 3))
  11010. (ncol (/ (- (window-width) 4) fwidth))
  11011. tg cnt e c tbl
  11012. groups ingroup)
  11013. (save-excursion
  11014. (save-window-excursion
  11015. (if expert
  11016. (set-buffer (get-buffer-create " *Org todo*"))
  11017. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11018. (erase-buffer)
  11019. (org-set-local 'org-done-keywords done-keywords)
  11020. (setq tbl fulltable cnt 0)
  11021. (while (setq e (pop tbl))
  11022. (cond
  11023. ((equal e '(:startgroup))
  11024. (push '() groups) (setq ingroup t)
  11025. (when (not (= cnt 0))
  11026. (setq cnt 0)
  11027. (insert "\n"))
  11028. (insert "{ "))
  11029. ((equal e '(:endgroup))
  11030. (setq ingroup nil cnt 0)
  11031. (insert "}\n"))
  11032. ((equal e '(:newline))
  11033. (when (not (= cnt 0))
  11034. (setq cnt 0)
  11035. (insert "\n")
  11036. (setq e (car tbl))
  11037. (while (equal (car tbl) '(:newline))
  11038. (insert "\n")
  11039. (setq tbl (cdr tbl)))))
  11040. (t
  11041. (setq tg (car e) c (cdr e))
  11042. (if ingroup (push tg (car groups)))
  11043. (setq tg (org-add-props tg nil 'face
  11044. (org-get-todo-face tg)))
  11045. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11046. (insert "[" c "] " tg (make-string
  11047. (- fwidth 4 (length tg)) ?\ ))
  11048. (when (= (setq cnt (1+ cnt)) ncol)
  11049. (insert "\n")
  11050. (if ingroup (insert " "))
  11051. (setq cnt 0)))))
  11052. (insert "\n")
  11053. (goto-char (point-min))
  11054. (if (not expert) (org-fit-window-to-buffer))
  11055. (message "[a-z..]:Set [SPC]:clear")
  11056. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11057. (cond
  11058. ((or (= c ?\C-g)
  11059. (and (= c ?q) (not (rassoc c fulltable))))
  11060. (setq quit-flag t))
  11061. ((= c ?\ ) nil)
  11062. ((setq e (rassoc c fulltable) tg (car e))
  11063. tg)
  11064. (t (setq quit-flag t)))))))
  11065. (defun org-entry-is-todo-p ()
  11066. (member (org-get-todo-state) org-not-done-keywords))
  11067. (defun org-entry-is-done-p ()
  11068. (member (org-get-todo-state) org-done-keywords))
  11069. (defun org-get-todo-state ()
  11070. (save-excursion
  11071. (org-back-to-heading t)
  11072. (and (looking-at org-todo-line-regexp)
  11073. (match-end 2)
  11074. (match-string 2))))
  11075. (defun org-at-date-range-p (&optional inactive-ok)
  11076. "Is the cursor inside a date range?"
  11077. (interactive)
  11078. (save-excursion
  11079. (catch 'exit
  11080. (let ((pos (point)))
  11081. (skip-chars-backward "^[<\r\n")
  11082. (skip-chars-backward "<[")
  11083. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11084. (>= (match-end 0) pos)
  11085. (throw 'exit t))
  11086. (skip-chars-backward "^<[\r\n")
  11087. (skip-chars-backward "<[")
  11088. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11089. (>= (match-end 0) pos)
  11090. (throw 'exit t)))
  11091. nil)))
  11092. (defun org-get-repeat (&optional tagline)
  11093. "Check if there is a deadline/schedule with repeater in this entry."
  11094. (save-match-data
  11095. (save-excursion
  11096. (org-back-to-heading t)
  11097. (and (re-search-forward (if tagline
  11098. (concat tagline "\\s-*" org-repeat-re)
  11099. org-repeat-re)
  11100. (org-entry-end-position) t)
  11101. (match-string-no-properties 1)))))
  11102. (defvar org-last-changed-timestamp)
  11103. (defvar org-last-inserted-timestamp)
  11104. (defvar org-log-post-message)
  11105. (defvar org-log-note-purpose)
  11106. (defvar org-log-note-how)
  11107. (defvar org-log-note-extra)
  11108. (defun org-auto-repeat-maybe (done-word)
  11109. "Check if the current headline contains a repeated deadline/schedule.
  11110. If yes, set TODO state back to what it was and change the base date
  11111. of repeating deadline/scheduled time stamps to new date.
  11112. This function is run automatically after each state change to a DONE state."
  11113. ;; last-state is dynamically scoped into this function
  11114. (let* ((repeat (org-get-repeat))
  11115. (aa (assoc org-last-state org-todo-kwd-alist))
  11116. (interpret (nth 1 aa))
  11117. (head (nth 2 aa))
  11118. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11119. (msg "Entry repeats: ")
  11120. (org-log-done nil)
  11121. (org-todo-log-states nil)
  11122. re type n what ts time to-state)
  11123. (when repeat
  11124. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11125. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11126. org-todo-repeat-to-state))
  11127. (unless (and to-state (member to-state org-todo-keywords-1))
  11128. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11129. (org-todo to-state)
  11130. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11131. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11132. (org-time-stamp-format t t))))
  11133. (when org-log-repeat
  11134. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11135. (memq 'org-add-log-note post-command-hook))
  11136. ;; OK, we are already setup for some record
  11137. (if (eq org-log-repeat 'note)
  11138. ;; make sure we take a note, not only a time stamp
  11139. (setq org-log-note-how 'note))
  11140. ;; Set up for taking a record
  11141. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11142. org-last-state
  11143. 'findpos org-log-repeat)))
  11144. (org-back-to-heading t)
  11145. (org-add-planning-info nil nil 'closed)
  11146. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11147. org-deadline-time-regexp "\\)\\|\\("
  11148. org-ts-regexp "\\)"))
  11149. (while (re-search-forward
  11150. re (save-excursion (outline-next-heading) (point)) t)
  11151. (setq type (if (match-end 1) org-scheduled-string
  11152. (if (match-end 3) org-deadline-string "Plain:"))
  11153. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11154. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11155. (setq n (string-to-number (match-string 2 ts))
  11156. what (match-string 3 ts))
  11157. (if (equal what "w") (setq n (* n 7) what "d"))
  11158. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11159. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11160. ;; Preparation, see if we need to modify the start date for the change
  11161. (when (match-end 1)
  11162. (setq time (save-match-data (org-time-string-to-time ts)))
  11163. (cond
  11164. ((equal (match-string 1 ts) ".")
  11165. ;; Shift starting date to today
  11166. (org-timestamp-change
  11167. (- (org-today) (time-to-days time))
  11168. 'day))
  11169. ((equal (match-string 1 ts) "+")
  11170. (let ((nshiftmax 10) (nshift 0))
  11171. (while (or (= nshift 0)
  11172. (<= (time-to-days time)
  11173. (time-to-days (current-time))))
  11174. (when (= (incf nshift) nshiftmax)
  11175. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11176. (error "Abort")))
  11177. (org-timestamp-change n (cdr (assoc what whata)))
  11178. (org-at-timestamp-p t)
  11179. (setq ts (match-string 1))
  11180. (setq time (save-match-data (org-time-string-to-time ts)))))
  11181. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11182. ;; rematch, so that we have everything in place for the real shift
  11183. (org-at-timestamp-p t)
  11184. (setq ts (match-string 1))
  11185. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11186. (org-timestamp-change n (cdr (assoc what whata)))
  11187. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11188. (setq org-log-post-message msg)
  11189. (message "%s" msg))))
  11190. (defun org-show-todo-tree (arg)
  11191. "Make a compact tree which shows all headlines marked with TODO.
  11192. The tree will show the lines where the regexp matches, and all higher
  11193. headlines above the match.
  11194. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11195. With a numeric prefix N, construct a sparse tree for the Nth element
  11196. of `org-todo-keywords-1'."
  11197. (interactive "P")
  11198. (let ((case-fold-search nil)
  11199. (kwd-re
  11200. (cond ((null arg) org-not-done-regexp)
  11201. ((equal arg '(4))
  11202. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11203. (mapcar 'list org-todo-keywords-1))))
  11204. (concat "\\("
  11205. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11206. "\\)\\>")))
  11207. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11208. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11209. org-todo-keywords-1)))
  11210. (t (error "Invalid prefix argument: %s" arg)))))
  11211. (message "%d TODO entries found"
  11212. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11213. (defun org-deadline (&optional remove time)
  11214. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11215. With argument REMOVE, remove any deadline from the item.
  11216. With argument TIME, set the deadline at the corresponding date. TIME
  11217. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11218. (interactive "P")
  11219. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11220. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11221. 'region-start-level 'region))
  11222. org-loop-over-headlines-in-active-region)
  11223. (org-map-entries
  11224. `(org-deadline ',remove ,time)
  11225. org-loop-over-headlines-in-active-region
  11226. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11227. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11228. (repeater (and old-date
  11229. (string-match
  11230. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11231. old-date)
  11232. (match-string 1 old-date))))
  11233. (if remove
  11234. (progn
  11235. (when (and old-date org-log-redeadline)
  11236. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11237. org-log-redeadline))
  11238. (org-remove-timestamp-with-keyword org-deadline-string)
  11239. (message "Item no longer has a deadline."))
  11240. (org-add-planning-info 'deadline time 'closed)
  11241. (when (and old-date org-log-redeadline
  11242. (not (equal old-date
  11243. (substring org-last-inserted-timestamp 1 -1))))
  11244. (org-add-log-setup 'redeadline nil old-date 'findpos
  11245. org-log-redeadline))
  11246. (when repeater
  11247. (save-excursion
  11248. (org-back-to-heading t)
  11249. (when (re-search-forward (concat org-deadline-string " "
  11250. org-last-inserted-timestamp)
  11251. (save-excursion
  11252. (outline-next-heading) (point)) t)
  11253. (goto-char (1- (match-end 0)))
  11254. (insert " " repeater)
  11255. (setq org-last-inserted-timestamp
  11256. (concat (substring org-last-inserted-timestamp 0 -1)
  11257. " " repeater
  11258. (substring org-last-inserted-timestamp -1))))))
  11259. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11260. (defun org-schedule (&optional remove time)
  11261. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11262. With argument REMOVE, remove any scheduling date from the item.
  11263. With argument TIME, scheduled at the corresponding date. TIME can
  11264. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11265. (interactive "P")
  11266. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11267. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11268. 'region-start-level 'region))
  11269. org-loop-over-headlines-in-active-region)
  11270. (org-map-entries
  11271. `(org-schedule ',remove ,time)
  11272. org-loop-over-headlines-in-active-region
  11273. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11274. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11275. (repeater (and old-date
  11276. (string-match
  11277. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11278. old-date)
  11279. (match-string 1 old-date))))
  11280. (if remove
  11281. (progn
  11282. (when (and old-date org-log-reschedule)
  11283. (org-add-log-setup 'delschedule nil old-date 'findpos
  11284. org-log-reschedule))
  11285. (org-remove-timestamp-with-keyword org-scheduled-string)
  11286. (message "Item is no longer scheduled."))
  11287. (org-add-planning-info 'scheduled time 'closed)
  11288. (when (and old-date org-log-reschedule
  11289. (not (equal old-date
  11290. (substring org-last-inserted-timestamp 1 -1))))
  11291. (org-add-log-setup 'reschedule nil old-date 'findpos
  11292. org-log-reschedule))
  11293. (when repeater
  11294. (save-excursion
  11295. (org-back-to-heading t)
  11296. (when (re-search-forward (concat org-scheduled-string " "
  11297. org-last-inserted-timestamp)
  11298. (save-excursion
  11299. (outline-next-heading) (point)) t)
  11300. (goto-char (1- (match-end 0)))
  11301. (insert " " repeater)
  11302. (setq org-last-inserted-timestamp
  11303. (concat (substring org-last-inserted-timestamp 0 -1)
  11304. " " repeater
  11305. (substring org-last-inserted-timestamp -1))))))
  11306. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11307. (defun org-get-scheduled-time (pom &optional inherit)
  11308. "Get the scheduled time as a time tuple, of a format suitable
  11309. for calling org-schedule with, or if there is no scheduling,
  11310. returns nil."
  11311. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11312. (when time
  11313. (apply 'encode-time (org-parse-time-string time)))))
  11314. (defun org-get-deadline-time (pom &optional inherit)
  11315. "Get the deadline as a time tuple, of a format suitable for
  11316. calling org-deadline with, or if there is no scheduling, returns
  11317. nil."
  11318. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11319. (when time
  11320. (apply 'encode-time (org-parse-time-string time)))))
  11321. (defun org-remove-timestamp-with-keyword (keyword)
  11322. "Remove all time stamps with KEYWORD in the current entry."
  11323. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11324. beg)
  11325. (save-excursion
  11326. (org-back-to-heading t)
  11327. (setq beg (point))
  11328. (outline-next-heading)
  11329. (while (re-search-backward re beg t)
  11330. (replace-match "")
  11331. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11332. (equal (char-before) ?\ ))
  11333. (backward-delete-char 1)
  11334. (if (string-match "^[ \t]*$" (buffer-substring
  11335. (point-at-bol) (point-at-eol)))
  11336. (delete-region (point-at-bol)
  11337. (min (point-max) (1+ (point-at-eol))))))))))
  11338. (defun org-add-planning-info (what &optional time &rest remove)
  11339. "Insert new timestamp with keyword in the line directly after the headline.
  11340. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11341. If non is given, the user is prompted for a date.
  11342. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11343. be removed."
  11344. (interactive)
  11345. (let (org-time-was-given org-end-time-was-given ts
  11346. end default-time default-input)
  11347. (catch 'exit
  11348. (when (and (memq what '(scheduled deadline))
  11349. (or (not time)
  11350. (and (stringp time)
  11351. (string-match "^[-+]+[0-9]" time))))
  11352. ;; Try to get a default date/time from existing timestamp
  11353. (save-excursion
  11354. (org-back-to-heading t)
  11355. (setq end (save-excursion (outline-next-heading) (point)))
  11356. (when (re-search-forward (if (eq what 'scheduled)
  11357. org-scheduled-time-regexp
  11358. org-deadline-time-regexp)
  11359. end t)
  11360. (setq ts (match-string 1)
  11361. default-time
  11362. (apply 'encode-time (org-parse-time-string ts))
  11363. default-input (and ts (org-get-compact-tod ts))))))
  11364. (when what
  11365. (setq time
  11366. (if (stringp time)
  11367. ;; This is a string (relative or absolute), set proper date
  11368. (apply 'encode-time
  11369. (org-read-date-analyze
  11370. time default-time (decode-time default-time)))
  11371. ;; If necessary, get the time from the user
  11372. (or time (org-read-date nil 'to-time nil nil
  11373. default-time default-input)))))
  11374. (when (and org-insert-labeled-timestamps-at-point
  11375. (member what '(scheduled deadline)))
  11376. (insert
  11377. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11378. (org-insert-time-stamp time org-time-was-given
  11379. nil nil nil (list org-end-time-was-given))
  11380. (setq what nil))
  11381. (save-excursion
  11382. (save-restriction
  11383. (let (col list elt ts buffer-invisibility-spec)
  11384. (org-back-to-heading t)
  11385. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11386. (goto-char (match-end 1))
  11387. (setq col (current-column))
  11388. (goto-char (match-end 0))
  11389. (if (eobp) (insert "\n") (forward-char 1))
  11390. (when (and (not what)
  11391. (not (looking-at
  11392. (concat "[ \t]*"
  11393. org-keyword-time-not-clock-regexp))))
  11394. ;; Nothing to add, nothing to remove...... :-)
  11395. (throw 'exit nil))
  11396. (if (and (not (looking-at org-outline-regexp))
  11397. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11398. "[^\r\n]*"))
  11399. (not (equal (match-string 1) org-clock-string)))
  11400. (narrow-to-region (match-beginning 0) (match-end 0))
  11401. (insert-before-markers "\n")
  11402. (backward-char 1)
  11403. (narrow-to-region (point) (point))
  11404. (and org-adapt-indentation (org-indent-to-column col)))
  11405. ;; Check if we have to remove something.
  11406. (setq list (cons what remove))
  11407. (while list
  11408. (setq elt (pop list))
  11409. (when (or (and (eq elt 'scheduled)
  11410. (re-search-forward org-scheduled-time-regexp nil t))
  11411. (and (eq elt 'deadline)
  11412. (re-search-forward org-deadline-time-regexp nil t))
  11413. (and (eq elt 'closed)
  11414. (re-search-forward org-closed-time-regexp nil t)))
  11415. (replace-match "")
  11416. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11417. (and (looking-at "[ \t]+") (replace-match ""))
  11418. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11419. (when what
  11420. (insert
  11421. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11422. (cond ((eq what 'scheduled) org-scheduled-string)
  11423. ((eq what 'deadline) org-deadline-string)
  11424. ((eq what 'closed) org-closed-string))
  11425. " ")
  11426. (setq ts (org-insert-time-stamp
  11427. time
  11428. (or org-time-was-given
  11429. (and (eq what 'closed) org-log-done-with-time))
  11430. (eq what 'closed)
  11431. nil nil (list org-end-time-was-given)))
  11432. (insert
  11433. (if (not (or (bolp) (eq (char-before) ?\ )
  11434. (memq (char-after) '(32 10))
  11435. (eobp))) " " ""))
  11436. (end-of-line 1))
  11437. (goto-char (point-min))
  11438. (widen)
  11439. (if (and (looking-at "[ \t]*\n")
  11440. (equal (char-before) ?\n))
  11441. (delete-region (1- (point)) (point-at-eol)))
  11442. ts))))))
  11443. (defvar org-log-note-marker (make-marker))
  11444. (defvar org-log-note-purpose nil)
  11445. (defvar org-log-note-state nil)
  11446. (defvar org-log-note-previous-state nil)
  11447. (defvar org-log-note-how nil)
  11448. (defvar org-log-note-extra nil)
  11449. (defvar org-log-note-window-configuration nil)
  11450. (defvar org-log-note-return-to (make-marker))
  11451. (defvar org-log-note-effective-time nil
  11452. "Remembered current time so that dynamically scoped
  11453. `org-extend-today-until' affects tha timestamps in state change
  11454. log")
  11455. (defvar org-log-post-message nil
  11456. "Message to be displayed after a log note has been stored.
  11457. The auto-repeater uses this.")
  11458. (defun org-add-note ()
  11459. "Add a note to the current entry.
  11460. This is done in the same way as adding a state change note."
  11461. (interactive)
  11462. (org-add-log-setup 'note nil nil 'findpos nil))
  11463. (defvar org-property-end-re)
  11464. (defun org-add-log-setup (&optional purpose state prev-state
  11465. findpos how extra)
  11466. "Set up the post command hook to take a note.
  11467. If this is about to TODO state change, the new state is expected in STATE.
  11468. When FINDPOS is non-nil, find the correct position for the note in
  11469. the current entry. If not, assume that it can be inserted at point.
  11470. HOW is an indicator what kind of note should be created.
  11471. EXTRA is additional text that will be inserted into the notes buffer."
  11472. (let* ((org-log-into-drawer (org-log-into-drawer))
  11473. (drawer (cond ((stringp org-log-into-drawer)
  11474. org-log-into-drawer)
  11475. (org-log-into-drawer "LOGBOOK"))))
  11476. (save-restriction
  11477. (save-excursion
  11478. (when findpos
  11479. (org-back-to-heading t)
  11480. (narrow-to-region (point) (save-excursion
  11481. (outline-next-heading) (point)))
  11482. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11483. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11484. "[^\r\n]*\\)?"))
  11485. (goto-char (match-end 0))
  11486. (cond
  11487. (drawer
  11488. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11489. nil t)
  11490. (progn
  11491. (goto-char (match-end 0))
  11492. (or org-log-states-order-reversed
  11493. (and (re-search-forward org-property-end-re nil t)
  11494. (goto-char (1- (match-beginning 0))))))
  11495. (insert "\n:" drawer ":\n:END:")
  11496. (beginning-of-line 0)
  11497. (org-indent-line)
  11498. (beginning-of-line 2)
  11499. (org-indent-line)
  11500. (end-of-line 0)))
  11501. ((and org-log-state-notes-insert-after-drawers
  11502. (save-excursion
  11503. (forward-line) (looking-at org-drawer-regexp)))
  11504. (forward-line)
  11505. (while (looking-at org-drawer-regexp)
  11506. (goto-char (match-end 0))
  11507. (re-search-forward org-property-end-re (point-max) t)
  11508. (forward-line))
  11509. (forward-line -1)))
  11510. (unless org-log-states-order-reversed
  11511. (and (= (char-after) ?\n) (forward-char 1))
  11512. (org-skip-over-state-notes)
  11513. (skip-chars-backward " \t\n\r")))
  11514. (move-marker org-log-note-marker (point))
  11515. (setq org-log-note-purpose purpose
  11516. org-log-note-state state
  11517. org-log-note-previous-state prev-state
  11518. org-log-note-how how
  11519. org-log-note-extra extra
  11520. org-log-note-effective-time (org-current-effective-time))
  11521. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11522. (defun org-skip-over-state-notes ()
  11523. "Skip past the list of State notes in an entry."
  11524. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11525. (when (ignore-errors (goto-char (org-in-item-p)))
  11526. (let* ((struct (org-list-struct))
  11527. (prevs (org-list-prevs-alist struct)))
  11528. (while (looking-at "[ \t]*- State")
  11529. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11530. (org-list-get-item-end (point) struct)))))))
  11531. (defun org-add-log-note (&optional purpose)
  11532. "Pop up a window for taking a note, and add this note later at point."
  11533. (remove-hook 'post-command-hook 'org-add-log-note)
  11534. (setq org-log-note-window-configuration (current-window-configuration))
  11535. (delete-other-windows)
  11536. (move-marker org-log-note-return-to (point))
  11537. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11538. (goto-char org-log-note-marker)
  11539. (org-switch-to-buffer-other-window "*Org Note*")
  11540. (erase-buffer)
  11541. (if (memq org-log-note-how '(time state))
  11542. (let (current-prefix-arg) (org-store-log-note))
  11543. (let ((org-inhibit-startup t)) (org-mode))
  11544. (insert (format "# Insert note for %s.
  11545. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11546. (cond
  11547. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11548. ((eq org-log-note-purpose 'done) "closed todo item")
  11549. ((eq org-log-note-purpose 'state)
  11550. (format "state change from \"%s\" to \"%s\""
  11551. (or org-log-note-previous-state "")
  11552. (or org-log-note-state "")))
  11553. ((eq org-log-note-purpose 'reschedule)
  11554. "rescheduling")
  11555. ((eq org-log-note-purpose 'delschedule)
  11556. "no longer scheduled")
  11557. ((eq org-log-note-purpose 'redeadline)
  11558. "changing deadline")
  11559. ((eq org-log-note-purpose 'deldeadline)
  11560. "removing deadline")
  11561. ((eq org-log-note-purpose 'refile)
  11562. "refiling")
  11563. ((eq org-log-note-purpose 'note)
  11564. "this entry")
  11565. (t (error "This should not happen")))))
  11566. (if org-log-note-extra (insert org-log-note-extra))
  11567. (org-set-local 'org-finish-function 'org-store-log-note)
  11568. (run-hooks 'org-log-buffer-setup-hook)))
  11569. (defvar org-note-abort nil) ; dynamically scoped
  11570. (defun org-store-log-note ()
  11571. "Finish taking a log note, and insert it to where it belongs."
  11572. (let ((txt (buffer-string))
  11573. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11574. lines ind bul)
  11575. (kill-buffer (current-buffer))
  11576. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11577. (setq txt (replace-match "" t t txt)))
  11578. (if (string-match "\\s-+\\'" txt)
  11579. (setq txt (replace-match "" t t txt)))
  11580. (setq lines (org-split-string txt "\n"))
  11581. (when (and note (string-match "\\S-" note))
  11582. (setq note
  11583. (org-replace-escapes
  11584. note
  11585. (list (cons "%u" (user-login-name))
  11586. (cons "%U" user-full-name)
  11587. (cons "%t" (format-time-string
  11588. (org-time-stamp-format 'long 'inactive)
  11589. org-log-note-effective-time))
  11590. (cons "%T" (format-time-string
  11591. (org-time-stamp-format 'long nil)
  11592. org-log-note-effective-time))
  11593. (cons "%d" (format-time-string
  11594. (org-time-stamp-format nil 'inactive)
  11595. org-log-note-effective-time))
  11596. (cons "%D" (format-time-string
  11597. (org-time-stamp-format nil nil)
  11598. org-log-note-effective-time))
  11599. (cons "%s" (if org-log-note-state
  11600. (concat "\"" org-log-note-state "\"")
  11601. ""))
  11602. (cons "%S" (if org-log-note-previous-state
  11603. (concat "\"" org-log-note-previous-state "\"")
  11604. "\"\"")))))
  11605. (if lines (setq note (concat note " \\\\")))
  11606. (push note lines))
  11607. (when (or current-prefix-arg org-note-abort)
  11608. (when org-log-into-drawer
  11609. (org-remove-empty-drawer-at
  11610. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11611. org-log-note-marker))
  11612. (setq lines nil))
  11613. (when lines
  11614. (with-current-buffer (marker-buffer org-log-note-marker)
  11615. (save-excursion
  11616. (goto-char org-log-note-marker)
  11617. (move-marker org-log-note-marker nil)
  11618. (end-of-line 1)
  11619. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11620. (setq ind (save-excursion
  11621. (if (ignore-errors (goto-char (org-in-item-p)))
  11622. (let ((struct (org-list-struct)))
  11623. (org-list-get-ind
  11624. (org-list-get-top-point struct) struct))
  11625. (skip-chars-backward " \r\t\n")
  11626. (cond
  11627. ((and (org-at-heading-p)
  11628. org-adapt-indentation)
  11629. (1+ (org-current-level)))
  11630. ((org-at-heading-p) 0)
  11631. (t (org-get-indentation))))))
  11632. (setq bul (org-list-bullet-string "-"))
  11633. (org-indent-line-to ind)
  11634. (insert bul (pop lines))
  11635. (let ((ind-body (+ (length bul) ind)))
  11636. (while lines
  11637. (insert "\n")
  11638. (org-indent-line-to ind-body)
  11639. (insert (pop lines))))
  11640. (message "Note stored")
  11641. (org-back-to-heading t)
  11642. (org-cycle-hide-drawers 'children)))))
  11643. (set-window-configuration org-log-note-window-configuration)
  11644. (with-current-buffer (marker-buffer org-log-note-return-to)
  11645. (goto-char org-log-note-return-to))
  11646. (move-marker org-log-note-return-to nil)
  11647. (and org-log-post-message (message "%s" org-log-post-message)))
  11648. (defun org-remove-empty-drawer-at (drawer pos)
  11649. "Remove an empty drawer DRAWER at position POS.
  11650. POS may also be a marker."
  11651. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11652. (save-excursion
  11653. (save-restriction
  11654. (widen)
  11655. (goto-char pos)
  11656. (if (org-in-regexp
  11657. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11658. (replace-match ""))))))
  11659. (defvar org-ts-type nil)
  11660. (defun org-sparse-tree (&optional arg type)
  11661. "Create a sparse tree, prompt for the details.
  11662. This command can create sparse trees. You first need to select the type
  11663. of match used to create the tree:
  11664. t Show all TODO entries.
  11665. T Show entries with a specific TODO keyword.
  11666. m Show entries selected by a tags/property match.
  11667. p Enter a property name and its value (both with completion on existing
  11668. names/values) and show entries with that property.
  11669. r Show entries matching a regular expression (`/' can be used as well).
  11670. b Show deadlines and scheduled items before a date.
  11671. a Show deadlines and scheduled items after a date.
  11672. d Show deadlines due within `org-deadline-warning-days'.
  11673. D Show deadlines and scheduled items between a date range."
  11674. (interactive "P")
  11675. (let (ans kwd value ts-type)
  11676. (setq type (or type org-sparse-tree-default-date-type))
  11677. (setq org-ts-type type)
  11678. (message "Sparse tree: [r]egexp [/]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"
  11679. (cond ((eq type 'all) "all timestamps")
  11680. ((eq type 'scheduled) "only scheduled")
  11681. ((eq type 'deadline) "only deadline")
  11682. ((eq type 'active) "only active timestamps")
  11683. ((eq type 'inactive) "only inactive timestamps")
  11684. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11685. (t "scheduled/deadline")))
  11686. (setq ans (read-char-exclusive))
  11687. (cond
  11688. ((equal ans ?c)
  11689. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11690. ((equal ans ?d)
  11691. (call-interactively 'org-check-deadlines))
  11692. ((equal ans ?b)
  11693. (call-interactively 'org-check-before-date))
  11694. ((equal ans ?a)
  11695. (call-interactively 'org-check-after-date))
  11696. ((equal ans ?D)
  11697. (call-interactively 'org-check-dates-range))
  11698. ((equal ans ?t)
  11699. (org-show-todo-tree nil))
  11700. ((equal ans ?T)
  11701. (org-show-todo-tree '(4)))
  11702. ((member ans '(?T ?m))
  11703. (call-interactively 'org-match-sparse-tree))
  11704. ((member ans '(?p ?P))
  11705. (setq kwd (org-icompleting-read "Property: "
  11706. (mapcar 'list (org-buffer-property-keys))))
  11707. (setq value (org-icompleting-read "Value: "
  11708. (mapcar 'list (org-property-values kwd))))
  11709. (unless (string-match "\\`{.*}\\'" value)
  11710. (setq value (concat "\"" value "\"")))
  11711. (org-match-sparse-tree arg (concat kwd "=" value)))
  11712. ((member ans '(?r ?R ?/))
  11713. (call-interactively 'org-occur))
  11714. (t (error "No such sparse tree command \"%c\"" ans)))))
  11715. (defvar org-occur-highlights nil
  11716. "List of overlays used for occur matches.")
  11717. (make-variable-buffer-local 'org-occur-highlights)
  11718. (defvar org-occur-parameters nil
  11719. "Parameters of the active org-occur calls.
  11720. This is a list, each call to org-occur pushes as cons cell,
  11721. containing the regular expression and the callback, onto the list.
  11722. The list can contain several entries if `org-occur' has been called
  11723. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11724. will only contain one set of parameters. When the highlights are
  11725. removed (for example with `C-c C-c', or with the next edit (depending
  11726. on `org-remove-highlights-with-change'), this variable is emptied
  11727. as well.")
  11728. (make-variable-buffer-local 'org-occur-parameters)
  11729. (defun org-occur (regexp &optional keep-previous callback)
  11730. "Make a compact tree which shows all matches of REGEXP.
  11731. The tree will show the lines where the regexp matches, and all higher
  11732. headlines above the match. It will also show the heading after the match,
  11733. to make sure editing the matching entry is easy.
  11734. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11735. call to `org-occur' will be kept, to allow stacking of calls to this
  11736. command.
  11737. If CALLBACK is non-nil, it is a function which is called to confirm
  11738. that the match should indeed be shown."
  11739. (interactive "sRegexp: \nP")
  11740. (when (equal regexp "")
  11741. (error "Regexp cannot be empty"))
  11742. (unless keep-previous
  11743. (org-remove-occur-highlights nil nil t))
  11744. (push (cons regexp callback) org-occur-parameters)
  11745. (let ((cnt 0))
  11746. (save-excursion
  11747. (goto-char (point-min))
  11748. (if (or (not keep-previous) ; do not want to keep
  11749. (not org-occur-highlights)) ; no previous matches
  11750. ;; hide everything
  11751. (org-overview))
  11752. (while (re-search-forward regexp nil t)
  11753. (when (or (not callback)
  11754. (save-match-data (funcall callback)))
  11755. (setq cnt (1+ cnt))
  11756. (when org-highlight-sparse-tree-matches
  11757. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11758. (org-show-context 'occur-tree))))
  11759. (when org-remove-highlights-with-change
  11760. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11761. nil 'local))
  11762. (unless org-sparse-tree-open-archived-trees
  11763. (org-hide-archived-subtrees (point-min) (point-max)))
  11764. (run-hooks 'org-occur-hook)
  11765. (if (org-called-interactively-p 'interactive)
  11766. (message "%d match(es) for regexp %s" cnt regexp))
  11767. cnt))
  11768. (defun org-occur-next-match (&optional n reset)
  11769. "Function for `next-error-function' to find sparse tree matches.
  11770. N is the number of matches to move, when negative move backwards.
  11771. RESET is entirely ignored - this function always goes back to the
  11772. starting point when no match is found."
  11773. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11774. (search-func (if (< n 0)
  11775. 'previous-single-char-property-change
  11776. 'next-single-char-property-change))
  11777. (n (abs n))
  11778. (pos (point))
  11779. p1)
  11780. (catch 'exit
  11781. (while (setq p1 (funcall search-func (point) 'org-type))
  11782. (when (equal p1 limit)
  11783. (goto-char pos)
  11784. (error "No more matches"))
  11785. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11786. (setq n (1- n))
  11787. (when (= n 0)
  11788. (goto-char p1)
  11789. (throw 'exit (point))))
  11790. (goto-char p1))
  11791. (goto-char p1)
  11792. (error "No more matches"))))
  11793. (defun org-show-context (&optional key)
  11794. "Make sure point and context are visible.
  11795. How much context is shown depends upon the variables
  11796. `org-show-hierarchy-above', `org-show-following-heading',
  11797. `org-show-entry-below' and `org-show-siblings'."
  11798. (let ((heading-p (org-at-heading-p t))
  11799. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11800. (following-p (org-get-alist-option org-show-following-heading key))
  11801. (entry-p (org-get-alist-option org-show-entry-below key))
  11802. (siblings-p (org-get-alist-option org-show-siblings key)))
  11803. (catch 'exit
  11804. ;; Show heading or entry text
  11805. (if (and heading-p (not entry-p))
  11806. (org-flag-heading nil) ; only show the heading
  11807. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11808. (org-show-hidden-entry))) ; show entire entry
  11809. (when following-p
  11810. ;; Show next sibling, or heading below text
  11811. (save-excursion
  11812. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11813. (org-flag-heading nil))))
  11814. (when siblings-p (org-show-siblings))
  11815. (when hierarchy-p
  11816. ;; show all higher headings, possibly with siblings
  11817. (save-excursion
  11818. (while (and (condition-case nil
  11819. (progn (org-up-heading-all 1) t)
  11820. (error nil))
  11821. (not (bobp)))
  11822. (org-flag-heading nil)
  11823. (when siblings-p (org-show-siblings))))))))
  11824. (defvar org-reveal-start-hook nil
  11825. "Hook run before revealing a location.")
  11826. (defun org-reveal (&optional siblings)
  11827. "Show current entry, hierarchy above it, and the following headline.
  11828. This can be used to show a consistent set of context around locations
  11829. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11830. not t for the search context.
  11831. With optional argument SIBLINGS, on each level of the hierarchy all
  11832. siblings are shown. This repairs the tree structure to what it would
  11833. look like when opened with hierarchical calls to `org-cycle'.
  11834. With double optional argument \\[universal-argument] \\[universal-argument], \
  11835. go to the parent and show the
  11836. entire tree."
  11837. (interactive "P")
  11838. (run-hooks 'org-reveal-start-hook)
  11839. (let ((org-show-hierarchy-above t)
  11840. (org-show-following-heading t)
  11841. (org-show-siblings (if siblings t org-show-siblings)))
  11842. (org-show-context nil))
  11843. (when (equal siblings '(16))
  11844. (save-excursion
  11845. (when (org-up-heading-safe)
  11846. (org-show-subtree)
  11847. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11848. (defun org-highlight-new-match (beg end)
  11849. "Highlight from BEG to END and mark the highlight is an occur headline."
  11850. (let ((ov (make-overlay beg end)))
  11851. (overlay-put ov 'face 'secondary-selection)
  11852. (overlay-put ov 'org-type 'org-occur)
  11853. (push ov org-occur-highlights)))
  11854. (defun org-remove-occur-highlights (&optional beg end noremove)
  11855. "Remove the occur highlights from the buffer.
  11856. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11857. from the `before-change-functions' in the current buffer."
  11858. (interactive)
  11859. (unless org-inhibit-highlight-removal
  11860. (mapc 'delete-overlay org-occur-highlights)
  11861. (setq org-occur-highlights nil)
  11862. (setq org-occur-parameters nil)
  11863. (unless noremove
  11864. (remove-hook 'before-change-functions
  11865. 'org-remove-occur-highlights 'local))))
  11866. ;;;; Priorities
  11867. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11868. "Regular expression matching the priority indicator.")
  11869. (defvar org-remove-priority-next-time nil)
  11870. (defun org-priority-up ()
  11871. "Increase the priority of the current item."
  11872. (interactive)
  11873. (org-priority 'up))
  11874. (defun org-priority-down ()
  11875. "Decrease the priority of the current item."
  11876. (interactive)
  11877. (org-priority 'down))
  11878. (defun org-priority (&optional action)
  11879. "Change the priority of an item.
  11880. ACTION can be `set', `up', `down', or a character."
  11881. (interactive)
  11882. (unless org-enable-priority-commands
  11883. (error "Priority commands are disabled"))
  11884. (setq action (or action 'set))
  11885. (let (current new news have remove)
  11886. (save-excursion
  11887. (org-back-to-heading t)
  11888. (if (looking-at org-priority-regexp)
  11889. (setq current (string-to-char (match-string 2))
  11890. have t))
  11891. (cond
  11892. ((eq action 'remove)
  11893. (setq remove t new ?\ ))
  11894. ((or (eq action 'set)
  11895. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11896. (if (not (eq action 'set))
  11897. (setq new action)
  11898. (message "Priority %c-%c, SPC to remove: "
  11899. org-highest-priority org-lowest-priority)
  11900. (save-match-data
  11901. (setq new (read-char-exclusive))))
  11902. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11903. (= (upcase org-lowest-priority) org-lowest-priority))
  11904. (setq new (upcase new)))
  11905. (cond ((equal new ?\ ) (setq remove t))
  11906. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11907. (error "Priority must be between `%c' and `%c'"
  11908. org-highest-priority org-lowest-priority))))
  11909. ((eq action 'up)
  11910. (setq new (if have
  11911. (1- current) ; normal cycling
  11912. ;; last priority was empty
  11913. (if (eq last-command this-command)
  11914. org-lowest-priority ; wrap around empty to lowest
  11915. ;; default
  11916. (if org-priority-start-cycle-with-default
  11917. org-default-priority
  11918. (1- org-default-priority))))))
  11919. ((eq action 'down)
  11920. (setq new (if have
  11921. (1+ current) ; normal cycling
  11922. ;; last priority was empty
  11923. (if (eq last-command this-command)
  11924. org-highest-priority ; wrap around empty to highest
  11925. ;; default
  11926. (if org-priority-start-cycle-with-default
  11927. org-default-priority
  11928. (1+ org-default-priority))))))
  11929. (t (error "Invalid action")))
  11930. (if (or (< (upcase new) org-highest-priority)
  11931. (> (upcase new) org-lowest-priority))
  11932. (if (and (memq action '(up down))
  11933. (not have) (not (eq last-command this-command)))
  11934. ;; `new' is from default priority
  11935. (error
  11936. "The default can not be set, see `org-default-priority' why")
  11937. ;; normal cycling: `new' is beyond highest/lowest priority
  11938. ;; and is wrapped around to the empty priority
  11939. (setq remove t)))
  11940. (setq news (format "%c" new))
  11941. (if have
  11942. (if remove
  11943. (replace-match "" t t nil 1)
  11944. (replace-match news t t nil 2))
  11945. (if remove
  11946. (error "No priority cookie found in line")
  11947. (let ((case-fold-search nil))
  11948. (looking-at org-todo-line-regexp))
  11949. (if (match-end 2)
  11950. (progn
  11951. (goto-char (match-end 2))
  11952. (insert " [#" news "]"))
  11953. (goto-char (match-beginning 3))
  11954. (insert "[#" news "] "))))
  11955. (org-preserve-lc (org-set-tags nil 'align)))
  11956. (if remove
  11957. (message "Priority removed")
  11958. (message "Priority of current item set to %s" news))))
  11959. (defun org-get-priority (s)
  11960. "Find priority cookie and return priority."
  11961. (if (functionp org-get-priority-function)
  11962. (funcall org-get-priority-function)
  11963. (save-match-data
  11964. (if (not (string-match org-priority-regexp s))
  11965. (* 1000 (- org-lowest-priority org-default-priority))
  11966. (* 1000 (- org-lowest-priority
  11967. (string-to-char (match-string 2 s))))))))
  11968. ;;;; Tags
  11969. (defvar org-agenda-archives-mode)
  11970. (defvar org-map-continue-from nil
  11971. "Position from where mapping should continue.
  11972. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11973. (defvar org-scanner-tags nil
  11974. "The current tag list while the tags scanner is running.")
  11975. (defvar org-trust-scanner-tags nil
  11976. "Should `org-get-tags-at' use the tags for the scanner.
  11977. This is for internal dynamical scoping only.
  11978. When this is non-nil, the function `org-get-tags-at' will return the value
  11979. of `org-scanner-tags' instead of building the list by itself. This
  11980. can lead to large speed-ups when the tags scanner is used in a file with
  11981. many entries, and when the list of tags is retrieved, for example to
  11982. obtain a list of properties. Building the tags list for each entry in such
  11983. a file becomes an N^2 operation - but with this variable set, it scales
  11984. as N.")
  11985. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11986. "Scan headline tags with inheritance and produce output ACTION.
  11987. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11988. or `agenda' to produce an entry list for an agenda view. It can also be
  11989. a Lisp form or a function that should be called at each matched headline, in
  11990. this case the return value is a list of all return values from these calls.
  11991. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11992. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11993. only lines with a not-done TODO keyword are included in the output.
  11994. This should be the same variable that was scoped into
  11995. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11996. START-LEVEL can be a string with asterisks, reducing the scope to
  11997. headlines matching this string."
  11998. (require 'org-agenda)
  11999. (let* ((re (concat "^"
  12000. (if start-level
  12001. ;; Get the correct level to match
  12002. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12003. org-outline-regexp)
  12004. " *\\(\\<\\("
  12005. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12006. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12007. (props (list 'face 'default
  12008. 'done-face 'org-agenda-done
  12009. 'undone-face 'default
  12010. 'mouse-face 'highlight
  12011. 'org-not-done-regexp org-not-done-regexp
  12012. 'org-todo-regexp org-todo-regexp
  12013. 'org-complex-heading-regexp org-complex-heading-regexp
  12014. 'help-echo
  12015. (format "mouse-2 or RET jump to org file %s"
  12016. (abbreviate-file-name
  12017. (or (buffer-file-name (buffer-base-buffer))
  12018. (buffer-name (buffer-base-buffer)))))))
  12019. (case-fold-search nil)
  12020. (org-map-continue-from nil)
  12021. lspos tags tags-list
  12022. (tags-alist (list (cons 0 org-file-tags)))
  12023. (llast 0) rtn rtn1 level category i txt
  12024. todo marker entry priority)
  12025. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12026. (setq action (list 'lambda nil action)))
  12027. (save-excursion
  12028. (goto-char (point-min))
  12029. (when (eq action 'sparse-tree)
  12030. (org-overview)
  12031. (org-remove-occur-highlights))
  12032. (while (re-search-forward re nil t)
  12033. (setq org-map-continue-from nil)
  12034. (catch :skip
  12035. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12036. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12037. (goto-char (setq lspos (match-beginning 0)))
  12038. (setq level (org-reduced-level (org-outline-level))
  12039. category (org-get-category))
  12040. (setq i llast llast level)
  12041. ;; remove tag lists from same and sublevels
  12042. (while (>= i level)
  12043. (when (setq entry (assoc i tags-alist))
  12044. (setq tags-alist (delete entry tags-alist)))
  12045. (setq i (1- i)))
  12046. ;; add the next tags
  12047. (when tags
  12048. (setq tags (org-split-string tags ":")
  12049. tags-alist
  12050. (cons (cons level tags) tags-alist)))
  12051. ;; compile tags for current headline
  12052. (setq tags-list
  12053. (if org-use-tag-inheritance
  12054. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12055. tags)
  12056. org-scanner-tags tags-list)
  12057. (when org-use-tag-inheritance
  12058. (setcdr (car tags-alist)
  12059. (mapcar (lambda (x)
  12060. (setq x (copy-sequence x))
  12061. (org-add-prop-inherited x))
  12062. (cdar tags-alist))))
  12063. (when (and tags org-use-tag-inheritance
  12064. (or (not (eq t org-use-tag-inheritance))
  12065. org-tags-exclude-from-inheritance))
  12066. ;; selective inheritance, remove uninherited ones
  12067. (setcdr (car tags-alist)
  12068. (org-remove-uninherited-tags (cdar tags-alist))))
  12069. (when (and
  12070. ;; eval matcher only when the todo condition is OK
  12071. (and (or (not todo-only) (member todo org-not-done-keywords))
  12072. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12073. (eval matcher)))
  12074. ;; Call the skipper, but return t if it does not skip,
  12075. ;; so that the `and' form continues evaluating
  12076. (progn
  12077. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12078. t)
  12079. ;; Check if timestamps are deselecting this entry
  12080. (or (not todo-only)
  12081. (and (member todo org-not-done-keywords)
  12082. (or (not org-agenda-tags-todo-honor-ignore-options)
  12083. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  12084. ;; Extra check for the archive tag
  12085. ;; FIXME: Does the skipper already do this????
  12086. (or
  12087. (not (member org-archive-tag tags-list))
  12088. ;; we have an archive tag, should we use this anyway?
  12089. (or (not org-agenda-skip-archived-trees)
  12090. (and (eq action 'agenda) org-agenda-archives-mode))))
  12091. ;; select this headline
  12092. (cond
  12093. ((eq action 'sparse-tree)
  12094. (and org-highlight-sparse-tree-matches
  12095. (org-get-heading) (match-end 0)
  12096. (org-highlight-new-match
  12097. (match-beginning 1) (match-end 1)))
  12098. (org-show-context 'tags-tree))
  12099. ((eq action 'agenda)
  12100. (setq txt (org-agenda-format-item
  12101. ""
  12102. (concat
  12103. (if (eq org-tags-match-list-sublevels 'indented)
  12104. (make-string (1- level) ?.) "")
  12105. (org-get-heading))
  12106. level category
  12107. tags-list)
  12108. priority (org-get-priority txt))
  12109. (goto-char lspos)
  12110. (setq marker (org-agenda-new-marker))
  12111. (org-add-props txt props
  12112. 'org-marker marker 'org-hd-marker marker 'org-category category
  12113. 'todo-state todo
  12114. 'priority priority 'type "tagsmatch")
  12115. (push txt rtn))
  12116. ((functionp action)
  12117. (setq org-map-continue-from nil)
  12118. (save-excursion
  12119. (setq rtn1 (funcall action))
  12120. (push rtn1 rtn)))
  12121. (t (error "Invalid action")))
  12122. ;; if we are to skip sublevels, jump to end of subtree
  12123. (unless org-tags-match-list-sublevels
  12124. (org-end-of-subtree t)
  12125. (backward-char 1))))
  12126. ;; Get the correct position from where to continue
  12127. (if org-map-continue-from
  12128. (goto-char org-map-continue-from)
  12129. (and (= (point) lspos) (end-of-line 1)))))
  12130. (when (and (eq action 'sparse-tree)
  12131. (not org-sparse-tree-open-archived-trees))
  12132. (org-hide-archived-subtrees (point-min) (point-max)))
  12133. (nreverse rtn)))
  12134. (defun org-remove-uninherited-tags (tags)
  12135. "Remove all tags that are not inherited from the list TAGS."
  12136. (cond
  12137. ((eq org-use-tag-inheritance t)
  12138. (if org-tags-exclude-from-inheritance
  12139. (org-delete-all org-tags-exclude-from-inheritance tags)
  12140. tags))
  12141. ((not org-use-tag-inheritance) nil)
  12142. ((stringp org-use-tag-inheritance)
  12143. (delq nil (mapcar
  12144. (lambda (x)
  12145. (if (and (string-match org-use-tag-inheritance x)
  12146. (not (member x org-tags-exclude-from-inheritance)))
  12147. x nil))
  12148. tags)))
  12149. ((listp org-use-tag-inheritance)
  12150. (delq nil (mapcar
  12151. (lambda (x)
  12152. (if (member x org-use-tag-inheritance) x nil))
  12153. tags)))))
  12154. (defun org-match-sparse-tree (&optional todo-only match)
  12155. "Create a sparse tree according to tags string MATCH.
  12156. MATCH can contain positive and negative selection of tags, like
  12157. \"+WORK+URGENT-WITHBOSS\".
  12158. If optional argument TODO-ONLY is non-nil, only select lines that are
  12159. also TODO lines."
  12160. (interactive "P")
  12161. (org-agenda-prepare-buffers (list (current-buffer)))
  12162. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12163. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12164. (defvar org-cached-props nil)
  12165. (defun org-cached-entry-get (pom property)
  12166. (if (or (eq t org-use-property-inheritance)
  12167. (and (stringp org-use-property-inheritance)
  12168. (string-match org-use-property-inheritance property))
  12169. (and (listp org-use-property-inheritance)
  12170. (member property org-use-property-inheritance)))
  12171. ;; Caching is not possible, check it directly
  12172. (org-entry-get pom property 'inherit)
  12173. ;; Get all properties, so that we can do complicated checks easily
  12174. (cdr (assoc property (or org-cached-props
  12175. (setq org-cached-props
  12176. (org-entry-properties pom)))))))
  12177. (defun org-global-tags-completion-table (&optional files)
  12178. "Return the list of all tags in all agenda buffer/files.
  12179. Optional FILES argument is a list of files which can be used
  12180. instead of the agenda files."
  12181. (save-excursion
  12182. (org-uniquify
  12183. (delq nil
  12184. (apply 'append
  12185. (mapcar
  12186. (lambda (file)
  12187. (set-buffer (find-file-noselect file))
  12188. (append (org-get-buffer-tags)
  12189. (mapcar (lambda (x) (if (stringp (car-safe x))
  12190. (list (car-safe x)) nil))
  12191. org-tag-alist)))
  12192. (if (and files (car files))
  12193. files
  12194. (org-agenda-files))))))))
  12195. (defun org-make-tags-matcher (match)
  12196. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12197. The variable `todo-only' is scoped dynamically into this function.
  12198. It will be set to t if the matcher restricts matching to TODO entries,
  12199. otherwise will not be touched.
  12200. Returns a cons of the selection string MATCH and the constructed
  12201. lisp form implementing the matcher. The matcher is to be evaluated
  12202. at an Org entry, with point on the headline, and returns t if the
  12203. entry matches the selection string MATCH. The returned lisp form
  12204. references two variables with information about the entry, which
  12205. must be bound around the form's evaluation: todo, the TODO keyword
  12206. at the entry (or nil of none); and tags-list, the list of all tags
  12207. at the entry including inherited ones. Additionally, the category
  12208. of the entry (if any) must be specified as the text property
  12209. 'org-category on the headline.
  12210. See also `org-scan-tags'.
  12211. "
  12212. (declare (special todo-only))
  12213. (unless (boundp 'todo-only)
  12214. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12215. (unless match
  12216. ;; Get a new match request, with completion
  12217. (let ((org-last-tags-completion-table
  12218. (org-global-tags-completion-table)))
  12219. (setq match (org-completing-read-no-i
  12220. "Match: " 'org-tags-completion-function nil nil nil
  12221. 'org-tags-history))))
  12222. ;; Parse the string and create a lisp form
  12223. (let ((match0 match)
  12224. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12225. minus tag mm
  12226. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12227. orterms term orlist re-p str-p level-p level-op time-p
  12228. prop-p pn pv po gv rest)
  12229. (if (string-match "/+" match)
  12230. ;; match contains also a todo-matching request
  12231. (progn
  12232. (setq tagsmatch (substring match 0 (match-beginning 0))
  12233. todomatch (substring match (match-end 0)))
  12234. (if (string-match "^!" todomatch)
  12235. (setq todo-only t todomatch (substring todomatch 1)))
  12236. (if (string-match "^\\s-*$" todomatch)
  12237. (setq todomatch nil)))
  12238. ;; only matching tags
  12239. (setq tagsmatch match todomatch nil))
  12240. ;; Make the tags matcher
  12241. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12242. (setq tagsmatcher t)
  12243. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12244. (while (setq term (pop orterms))
  12245. (while (and (equal (substring term -1) "\\") orterms)
  12246. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12247. (while (string-match re term)
  12248. (setq rest (substring term (match-end 0))
  12249. minus (and (match-end 1)
  12250. (equal (match-string 1 term) "-"))
  12251. tag (save-match-data (replace-regexp-in-string
  12252. "\\\\-" "-"
  12253. (match-string 2 term)))
  12254. re-p (equal (string-to-char tag) ?{)
  12255. level-p (match-end 4)
  12256. prop-p (match-end 5)
  12257. mm (cond
  12258. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12259. (level-p
  12260. (setq level-op (org-op-to-function (match-string 3 term)))
  12261. `(,level-op level ,(string-to-number
  12262. (match-string 4 term))))
  12263. (prop-p
  12264. (setq pn (match-string 5 term)
  12265. po (match-string 6 term)
  12266. pv (match-string 7 term)
  12267. re-p (equal (string-to-char pv) ?{)
  12268. str-p (equal (string-to-char pv) ?\")
  12269. time-p (save-match-data
  12270. (string-match "^\"[[<].*[]>]\"$" pv))
  12271. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12272. (if time-p (setq pv (org-matcher-time pv)))
  12273. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12274. (cond
  12275. ((equal pn "CATEGORY")
  12276. (setq gv '(get-text-property (point) 'org-category)))
  12277. ((equal pn "TODO")
  12278. (setq gv 'todo))
  12279. (t
  12280. (setq gv `(org-cached-entry-get nil ,pn))))
  12281. (if re-p
  12282. (if (eq po 'org<>)
  12283. `(not (string-match ,pv (or ,gv "")))
  12284. `(string-match ,pv (or ,gv "")))
  12285. (if str-p
  12286. `(,po (or ,gv "") ,pv)
  12287. `(,po (string-to-number (or ,gv ""))
  12288. ,(string-to-number pv) ))))
  12289. (t `(member ,tag tags-list)))
  12290. mm (if minus (list 'not mm) mm)
  12291. term rest)
  12292. (push mm tagsmatcher))
  12293. (push (if (> (length tagsmatcher) 1)
  12294. (cons 'and tagsmatcher)
  12295. (car tagsmatcher))
  12296. orlist)
  12297. (setq tagsmatcher nil))
  12298. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12299. (setq tagsmatcher
  12300. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12301. ;; Make the todo matcher
  12302. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12303. (setq todomatcher t)
  12304. (setq orterms (org-split-string todomatch "|") orlist nil)
  12305. (while (setq term (pop orterms))
  12306. (while (string-match re term)
  12307. (setq minus (and (match-end 1)
  12308. (equal (match-string 1 term) "-"))
  12309. kwd (match-string 2 term)
  12310. re-p (equal (string-to-char kwd) ?{)
  12311. term (substring term (match-end 0))
  12312. mm (if re-p
  12313. `(string-match ,(substring kwd 1 -1) todo)
  12314. (list 'equal 'todo kwd))
  12315. mm (if minus (list 'not mm) mm))
  12316. (push mm todomatcher))
  12317. (push (if (> (length todomatcher) 1)
  12318. (cons 'and todomatcher)
  12319. (car todomatcher))
  12320. orlist)
  12321. (setq todomatcher nil))
  12322. (setq todomatcher (if (> (length orlist) 1)
  12323. (cons 'or orlist) (car orlist))))
  12324. ;; Return the string and lisp forms of the matcher
  12325. (setq matcher (if todomatcher
  12326. (list 'and tagsmatcher todomatcher)
  12327. tagsmatcher))
  12328. (when todo-only
  12329. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12330. matcher)))
  12331. (cons match0 matcher)))
  12332. (defun org-op-to-function (op &optional stringp)
  12333. "Turn an operator into the appropriate function."
  12334. (setq op
  12335. (cond
  12336. ((equal op "<" ) '(< string< org-time<))
  12337. ((equal op ">" ) '(> org-string> org-time>))
  12338. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12339. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12340. ((member op '("=" "==")) '(= string= org-time=))
  12341. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12342. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12343. (defun org<> (a b) (not (= a b)))
  12344. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12345. (defun org-string>= (a b) (not (string< a b)))
  12346. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12347. (defun org-string<> (a b) (not (string= a b)))
  12348. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12349. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12350. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12351. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12352. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12353. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12354. (defun org-2ft (s)
  12355. "Convert S to a floating point time.
  12356. If S is already a number, just return it. If it is a string, parse
  12357. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12358. (cond
  12359. ((numberp s) s)
  12360. ((stringp s)
  12361. (condition-case nil
  12362. (float-time (apply 'encode-time (org-parse-time-string s)))
  12363. (error 0.)))
  12364. (t 0.)))
  12365. (defun org-time-today ()
  12366. "Time in seconds today at 0:00.
  12367. Returns the float number of seconds since the beginning of the
  12368. epoch to the beginning of today (00:00)."
  12369. (float-time (apply 'encode-time
  12370. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12371. (defun org-matcher-time (s)
  12372. "Interpret a time comparison value."
  12373. (save-match-data
  12374. (cond
  12375. ((string= s "<now>") (float-time))
  12376. ((string= s "<today>") (org-time-today))
  12377. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12378. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12379. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12380. (+ (org-time-today)
  12381. (* (string-to-number (match-string 1 s))
  12382. (cdr (assoc (match-string 2 s)
  12383. '(("d" . 86400.0) ("w" . 604800.0)
  12384. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12385. (t (org-2ft s)))))
  12386. (defun org-match-any-p (re list)
  12387. "Does re match any element of list?"
  12388. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12389. (delq nil list))
  12390. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12391. (defvar org-tags-overlay (make-overlay 1 1))
  12392. (org-detach-overlay org-tags-overlay)
  12393. (defun org-get-local-tags-at (&optional pos)
  12394. "Get a list of tags defined in the current headline."
  12395. (org-get-tags-at pos 'local))
  12396. (defun org-get-local-tags ()
  12397. "Get a list of tags defined in the current headline."
  12398. (org-get-tags-at nil 'local))
  12399. (defun org-get-tags-at (&optional pos local)
  12400. "Get a list of all headline tags applicable at POS.
  12401. POS defaults to point. If tags are inherited, the list contains
  12402. the targets in the same sequence as the headlines appear, i.e.
  12403. the tags of the current headline come last.
  12404. When LOCAL is non-nil, only return tags from the current headline,
  12405. ignore inherited ones."
  12406. (interactive)
  12407. (if (and org-trust-scanner-tags
  12408. (or (not pos) (equal pos (point)))
  12409. (not local))
  12410. org-scanner-tags
  12411. (let (tags ltags lastpos parent)
  12412. (save-excursion
  12413. (save-restriction
  12414. (widen)
  12415. (goto-char (or pos (point)))
  12416. (save-match-data
  12417. (catch 'done
  12418. (condition-case nil
  12419. (progn
  12420. (org-back-to-heading t)
  12421. (while (not (equal lastpos (point)))
  12422. (setq lastpos (point))
  12423. (when (looking-at
  12424. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12425. (setq ltags (org-split-string
  12426. (org-match-string-no-properties 1) ":"))
  12427. (when parent
  12428. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12429. (setq tags (append
  12430. (if parent
  12431. (org-remove-uninherited-tags ltags)
  12432. ltags)
  12433. tags)))
  12434. (or org-use-tag-inheritance (throw 'done t))
  12435. (if local (throw 'done t))
  12436. (or (org-up-heading-safe) (error nil))
  12437. (setq parent t)))
  12438. (error nil)))))
  12439. (if local
  12440. tags
  12441. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12442. (defun org-add-prop-inherited (s)
  12443. (add-text-properties 0 (length s) '(inherited t) s)
  12444. s)
  12445. (defun org-toggle-tag (tag &optional onoff)
  12446. "Toggle the tag TAG for the current line.
  12447. If ONOFF is `on' or `off', don't toggle but set to this state."
  12448. (let (res current)
  12449. (save-excursion
  12450. (org-back-to-heading t)
  12451. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12452. (point-at-eol) t)
  12453. (progn
  12454. (setq current (match-string 1))
  12455. (replace-match ""))
  12456. (setq current ""))
  12457. (setq current (nreverse (org-split-string current ":")))
  12458. (cond
  12459. ((eq onoff 'on)
  12460. (setq res t)
  12461. (or (member tag current) (push tag current)))
  12462. ((eq onoff 'off)
  12463. (or (not (member tag current)) (setq current (delete tag current))))
  12464. (t (if (member tag current)
  12465. (setq current (delete tag current))
  12466. (setq res t)
  12467. (push tag current))))
  12468. (end-of-line 1)
  12469. (if current
  12470. (progn
  12471. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12472. (org-set-tags nil t))
  12473. (delete-horizontal-space))
  12474. (run-hooks 'org-after-tags-change-hook))
  12475. res))
  12476. (defun org-align-tags-here (to-col)
  12477. ;; Assumes that this is a headline
  12478. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12479. (beginning-of-line 1)
  12480. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12481. (< pos (match-beginning 2)))
  12482. (progn
  12483. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12484. (goto-char (match-beginning 1))
  12485. (insert " ")
  12486. (delete-region (point) (1+ (match-beginning 2)))
  12487. (setq ncol (max (current-column)
  12488. (1+ col)
  12489. (if (> to-col 0)
  12490. to-col
  12491. (- (abs to-col) tags-l))))
  12492. (setq p (point))
  12493. (insert (make-string (- ncol (current-column)) ?\ ))
  12494. (setq ncol (current-column))
  12495. (when indent-tabs-mode (tabify p (point-at-eol)))
  12496. (org-move-to-column (min ncol col) t))
  12497. (goto-char pos))))
  12498. (defun org-set-tags-command (&optional arg just-align)
  12499. "Call the set-tags command for the current entry."
  12500. (interactive "P")
  12501. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12502. (org-set-tags arg just-align)
  12503. (save-excursion
  12504. (org-back-to-heading t)
  12505. (org-set-tags arg just-align))))
  12506. (defun org-set-tags-to (data)
  12507. "Set the tags of the current entry to DATA, replacing the current tags.
  12508. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12509. If DATA is nil or the empty string, any tags will be removed."
  12510. (interactive "sTags: ")
  12511. (setq data
  12512. (cond
  12513. ((eq data nil) "")
  12514. ((equal data "") "")
  12515. ((stringp data)
  12516. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12517. ":"))
  12518. ((listp data)
  12519. (concat ":" (mapconcat 'identity data ":") ":"))))
  12520. (when data
  12521. (save-excursion
  12522. (org-back-to-heading t)
  12523. (when (looking-at org-complex-heading-regexp)
  12524. (if (match-end 5)
  12525. (progn
  12526. (goto-char (match-beginning 5))
  12527. (insert data)
  12528. (delete-region (point) (point-at-eol))
  12529. (org-set-tags nil 'align))
  12530. (goto-char (point-at-eol))
  12531. (insert " " data)
  12532. (org-set-tags nil 'align)))
  12533. (beginning-of-line 1)
  12534. (if (looking-at ".*?\\([ \t]+\\)$")
  12535. (delete-region (match-beginning 1) (match-end 1))))))
  12536. (defun org-align-all-tags ()
  12537. "Align the tags i all headings."
  12538. (interactive)
  12539. (save-excursion
  12540. (or (ignore-errors (org-back-to-heading t))
  12541. (outline-next-heading))
  12542. (if (org-at-heading-p)
  12543. (org-set-tags t)
  12544. (message "No headings"))))
  12545. (defvar org-indent-indentation-per-level)
  12546. (defun org-set-tags (&optional arg just-align)
  12547. "Set the tags for the current headline.
  12548. With prefix ARG, realign all tags in headings in the current buffer."
  12549. (interactive "P")
  12550. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12551. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12552. 'region-start-level 'region))
  12553. org-loop-over-headlines-in-active-region)
  12554. (org-map-entries
  12555. ;; We don't use ARG and JUST-ALIGN here these args are not
  12556. ;; useful when looping over headlines
  12557. `(org-set-tags)
  12558. org-loop-over-headlines-in-active-region
  12559. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12560. (let* ((re org-outline-regexp-bol)
  12561. (current (unless arg (org-get-tags-string)))
  12562. (col (current-column))
  12563. (org-setting-tags t)
  12564. table current-tags inherited-tags ; computed below when needed
  12565. tags p0 c0 c1 rpl di tc level)
  12566. (if arg
  12567. (save-excursion
  12568. (goto-char (point-min))
  12569. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12570. (while (re-search-forward re nil t)
  12571. (org-set-tags nil t)
  12572. (end-of-line 1)))
  12573. (message "All tags realigned to column %d" org-tags-column))
  12574. (if just-align
  12575. (setq tags current)
  12576. ;; Get a new set of tags from the user
  12577. (save-excursion
  12578. (setq table (append org-tag-persistent-alist
  12579. (or org-tag-alist (org-get-buffer-tags))
  12580. (and
  12581. org-complete-tags-always-offer-all-agenda-tags
  12582. (org-global-tags-completion-table
  12583. (org-agenda-files))))
  12584. org-last-tags-completion-table table
  12585. current-tags (org-split-string current ":")
  12586. inherited-tags (nreverse
  12587. (nthcdr (length current-tags)
  12588. (nreverse (org-get-tags-at))))
  12589. tags
  12590. (if (or (eq t org-use-fast-tag-selection)
  12591. (and org-use-fast-tag-selection
  12592. (delq nil (mapcar 'cdr table))))
  12593. (org-fast-tag-selection
  12594. current-tags inherited-tags table
  12595. (if org-fast-tag-selection-include-todo
  12596. org-todo-key-alist))
  12597. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12598. (org-trim
  12599. (org-icompleting-read "Tags: "
  12600. 'org-tags-completion-function
  12601. nil nil current 'org-tags-history))))))
  12602. (while (string-match "[-+&]+" tags)
  12603. ;; No boolean logic, just a list
  12604. (setq tags (replace-match ":" t t tags))))
  12605. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12606. (if org-tags-sort-function
  12607. (setq tags (mapconcat 'identity
  12608. (sort (org-split-string
  12609. tags (org-re "[^[:alnum:]_@#%]+"))
  12610. org-tags-sort-function) ":")))
  12611. (if (string-match "\\`[\t ]*\\'" tags)
  12612. (setq tags "")
  12613. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12614. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12615. ;; Insert new tags at the correct column
  12616. (beginning-of-line 1)
  12617. (setq level (or (and (looking-at org-outline-regexp)
  12618. (- (match-end 0) (point) 1))
  12619. 1))
  12620. (cond
  12621. ((and (equal current "") (equal tags "")))
  12622. ((re-search-forward
  12623. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12624. (point-at-eol) t)
  12625. (if (equal tags "")
  12626. (setq rpl "")
  12627. (goto-char (match-beginning 0))
  12628. (setq c0 (current-column)
  12629. ;; compute offset for the case of org-indent-mode active
  12630. di (if org-indent-mode
  12631. (* (1- org-indent-indentation-per-level) (1- level))
  12632. 0)
  12633. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12634. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12635. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12636. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12637. (replace-match rpl t t)
  12638. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12639. tags)
  12640. (t (error "Tags alignment failed")))
  12641. (org-move-to-column col)
  12642. (unless just-align
  12643. (run-hooks 'org-after-tags-change-hook))))))
  12644. (defun org-change-tag-in-region (beg end tag off)
  12645. "Add or remove TAG for each entry in the region.
  12646. This works in the agenda, and also in an org-mode buffer."
  12647. (interactive
  12648. (list (region-beginning) (region-end)
  12649. (let ((org-last-tags-completion-table
  12650. (if (derived-mode-p 'org-mode)
  12651. (org-get-buffer-tags)
  12652. (org-global-tags-completion-table))))
  12653. (org-icompleting-read
  12654. "Tag: " 'org-tags-completion-function nil nil nil
  12655. 'org-tags-history))
  12656. (progn
  12657. (message "[s]et or [r]emove? ")
  12658. (equal (read-char-exclusive) ?r))))
  12659. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12660. (let ((agendap (equal major-mode 'org-agenda-mode))
  12661. l1 l2 m buf pos newhead (cnt 0))
  12662. (goto-char end)
  12663. (setq l2 (1- (org-current-line)))
  12664. (goto-char beg)
  12665. (setq l1 (org-current-line))
  12666. (loop for l from l1 to l2 do
  12667. (org-goto-line l)
  12668. (setq m (get-text-property (point) 'org-hd-marker))
  12669. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12670. (and agendap m))
  12671. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12672. pos (if agendap m (point)))
  12673. (with-current-buffer buf
  12674. (save-excursion
  12675. (save-restriction
  12676. (goto-char pos)
  12677. (setq cnt (1+ cnt))
  12678. (org-toggle-tag tag (if off 'off 'on))
  12679. (setq newhead (org-get-heading)))))
  12680. (and agendap (org-agenda-change-all-lines newhead m))))
  12681. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12682. (defun org-tags-completion-function (string predicate &optional flag)
  12683. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12684. (confirm (lambda (x) (stringp (car x)))))
  12685. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12686. (setq s1 (match-string 1 string)
  12687. s2 (match-string 2 string))
  12688. (setq s1 "" s2 string))
  12689. (cond
  12690. ((eq flag nil)
  12691. ;; try completion
  12692. (setq rtn (try-completion s2 ctable confirm))
  12693. (if (stringp rtn)
  12694. (setq rtn
  12695. (concat s1 s2 (substring rtn (length s2))
  12696. (if (and org-add-colon-after-tag-completion
  12697. (assoc rtn ctable))
  12698. ":" ""))))
  12699. rtn)
  12700. ((eq flag t)
  12701. ;; all-completions
  12702. (all-completions s2 ctable confirm)
  12703. )
  12704. ((eq flag 'lambda)
  12705. ;; exact match?
  12706. (assoc s2 ctable)))
  12707. ))
  12708. (defun org-fast-tag-insert (kwd tags face &optional end)
  12709. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12710. (insert (format "%-12s" (concat kwd ":"))
  12711. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12712. (or end "")))
  12713. (defun org-fast-tag-show-exit (flag)
  12714. (save-excursion
  12715. (org-goto-line 3)
  12716. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12717. (replace-match ""))
  12718. (when flag
  12719. (end-of-line 1)
  12720. (org-move-to-column (- (window-width) 19) t)
  12721. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12722. (defun org-set-current-tags-overlay (current prefix)
  12723. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12724. (if (featurep 'xemacs)
  12725. (org-overlay-display org-tags-overlay (concat prefix s)
  12726. 'secondary-selection)
  12727. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12728. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12729. (defvar org-last-tag-selection-key nil)
  12730. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12731. "Fast tag selection with single keys.
  12732. CURRENT is the current list of tags in the headline, INHERITED is the
  12733. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12734. possibly with grouping information. TODO-TABLE is a similar table with
  12735. TODO keywords, should these have keys assigned to them.
  12736. If the keys are nil, a-z are automatically assigned.
  12737. Returns the new tags string, or nil to not change the current settings."
  12738. (let* ((fulltable (append table todo-table))
  12739. (maxlen (apply 'max (mapcar
  12740. (lambda (x)
  12741. (if (stringp (car x)) (string-width (car x)) 0))
  12742. fulltable)))
  12743. (buf (current-buffer))
  12744. (expert (eq org-fast-tag-selection-single-key 'expert))
  12745. (buffer-tags nil)
  12746. (fwidth (+ maxlen 3 1 3))
  12747. (ncol (/ (- (window-width) 4) fwidth))
  12748. (i-face 'org-done)
  12749. (c-face 'org-todo)
  12750. tg cnt e c char c1 c2 ntable tbl rtn
  12751. ov-start ov-end ov-prefix
  12752. (exit-after-next org-fast-tag-selection-single-key)
  12753. (done-keywords org-done-keywords)
  12754. groups ingroup)
  12755. (save-excursion
  12756. (beginning-of-line 1)
  12757. (if (looking-at
  12758. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12759. (setq ov-start (match-beginning 1)
  12760. ov-end (match-end 1)
  12761. ov-prefix "")
  12762. (setq ov-start (1- (point-at-eol))
  12763. ov-end (1+ ov-start))
  12764. (skip-chars-forward "^\n\r")
  12765. (setq ov-prefix
  12766. (concat
  12767. (buffer-substring (1- (point)) (point))
  12768. (if (> (current-column) org-tags-column)
  12769. " "
  12770. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12771. (move-overlay org-tags-overlay ov-start ov-end)
  12772. (save-window-excursion
  12773. (if expert
  12774. (set-buffer (get-buffer-create " *Org tags*"))
  12775. (delete-other-windows)
  12776. (split-window-vertically)
  12777. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12778. (erase-buffer)
  12779. (org-set-local 'org-done-keywords done-keywords)
  12780. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12781. (org-fast-tag-insert "Current" current c-face "\n\n")
  12782. (org-fast-tag-show-exit exit-after-next)
  12783. (org-set-current-tags-overlay current ov-prefix)
  12784. (setq tbl fulltable char ?a cnt 0)
  12785. (while (setq e (pop tbl))
  12786. (cond
  12787. ((equal (car e) :startgroup)
  12788. (push '() groups) (setq ingroup t)
  12789. (when (not (= cnt 0))
  12790. (setq cnt 0)
  12791. (insert "\n"))
  12792. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12793. ((equal (car e) :endgroup)
  12794. (setq ingroup nil cnt 0)
  12795. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12796. ((equal e '(:newline))
  12797. (when (not (= cnt 0))
  12798. (setq cnt 0)
  12799. (insert "\n")
  12800. (setq e (car tbl))
  12801. (while (equal (car tbl) '(:newline))
  12802. (insert "\n")
  12803. (setq tbl (cdr tbl)))))
  12804. (t
  12805. (setq tg (copy-sequence (car e)) c2 nil)
  12806. (if (cdr e)
  12807. (setq c (cdr e))
  12808. ;; automatically assign a character.
  12809. (setq c1 (string-to-char
  12810. (downcase (substring
  12811. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12812. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12813. (while (or (rassoc char ntable) (rassoc char table))
  12814. (setq char (1+ char)))
  12815. (setq c2 c1))
  12816. (setq c (or c2 char)))
  12817. (if ingroup (push tg (car groups)))
  12818. (setq tg (org-add-props tg nil 'face
  12819. (cond
  12820. ((not (assoc tg table))
  12821. (org-get-todo-face tg))
  12822. ((member tg current) c-face)
  12823. ((member tg inherited) i-face))))
  12824. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12825. (insert "[" c "] " tg (make-string
  12826. (- fwidth 4 (length tg)) ?\ ))
  12827. (push (cons tg c) ntable)
  12828. (when (= (setq cnt (1+ cnt)) ncol)
  12829. (insert "\n")
  12830. (if ingroup (insert " "))
  12831. (setq cnt 0)))))
  12832. (setq ntable (nreverse ntable))
  12833. (insert "\n")
  12834. (goto-char (point-min))
  12835. (if (not expert) (org-fit-window-to-buffer))
  12836. (setq rtn
  12837. (catch 'exit
  12838. (while t
  12839. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12840. (if (not groups) "no " "")
  12841. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12842. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12843. (setq org-last-tag-selection-key c)
  12844. (cond
  12845. ((= c ?\r) (throw 'exit t))
  12846. ((= c ?!)
  12847. (setq groups (not groups))
  12848. (goto-char (point-min))
  12849. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12850. ((= c ?\C-c)
  12851. (if (not expert)
  12852. (org-fast-tag-show-exit
  12853. (setq exit-after-next (not exit-after-next)))
  12854. (setq expert nil)
  12855. (delete-other-windows)
  12856. (set-window-buffer (split-window-vertically) " *Org tags*")
  12857. (org-switch-to-buffer-other-window " *Org tags*")
  12858. (org-fit-window-to-buffer)))
  12859. ((or (= c ?\C-g)
  12860. (and (= c ?q) (not (rassoc c ntable))))
  12861. (org-detach-overlay org-tags-overlay)
  12862. (setq quit-flag t))
  12863. ((= c ?\ )
  12864. (setq current nil)
  12865. (if exit-after-next (setq exit-after-next 'now)))
  12866. ((= c ?\t)
  12867. (condition-case nil
  12868. (setq tg (org-icompleting-read
  12869. "Tag: "
  12870. (or buffer-tags
  12871. (with-current-buffer buf
  12872. (org-get-buffer-tags)))))
  12873. (quit (setq tg "")))
  12874. (when (string-match "\\S-" tg)
  12875. (add-to-list 'buffer-tags (list tg))
  12876. (if (member tg current)
  12877. (setq current (delete tg current))
  12878. (push tg current)))
  12879. (if exit-after-next (setq exit-after-next 'now)))
  12880. ((setq e (rassoc c todo-table) tg (car e))
  12881. (with-current-buffer buf
  12882. (save-excursion (org-todo tg)))
  12883. (if exit-after-next (setq exit-after-next 'now)))
  12884. ((setq e (rassoc c ntable) tg (car e))
  12885. (if (member tg current)
  12886. (setq current (delete tg current))
  12887. (loop for g in groups do
  12888. (if (member tg g)
  12889. (mapc (lambda (x)
  12890. (setq current (delete x current)))
  12891. g)))
  12892. (push tg current))
  12893. (if exit-after-next (setq exit-after-next 'now))))
  12894. ;; Create a sorted list
  12895. (setq current
  12896. (sort current
  12897. (lambda (a b)
  12898. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12899. (if (eq exit-after-next 'now) (throw 'exit t))
  12900. (goto-char (point-min))
  12901. (beginning-of-line 2)
  12902. (delete-region (point) (point-at-eol))
  12903. (org-fast-tag-insert "Current" current c-face)
  12904. (org-set-current-tags-overlay current ov-prefix)
  12905. (while (re-search-forward
  12906. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12907. (setq tg (match-string 1))
  12908. (add-text-properties
  12909. (match-beginning 1) (match-end 1)
  12910. (list 'face
  12911. (cond
  12912. ((member tg current) c-face)
  12913. ((member tg inherited) i-face)
  12914. (t (get-text-property (match-beginning 1) 'face))))))
  12915. (goto-char (point-min)))))
  12916. (org-detach-overlay org-tags-overlay)
  12917. (if rtn
  12918. (mapconcat 'identity current ":")
  12919. nil))))
  12920. (defun org-get-tags-string ()
  12921. "Get the TAGS string in the current headline."
  12922. (unless (org-at-heading-p t)
  12923. (error "Not on a heading"))
  12924. (save-excursion
  12925. (beginning-of-line 1)
  12926. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12927. (org-match-string-no-properties 1)
  12928. "")))
  12929. (defun org-get-tags ()
  12930. "Get the list of tags specified in the current headline."
  12931. (org-split-string (org-get-tags-string) ":"))
  12932. (defun org-get-buffer-tags ()
  12933. "Get a table of all tags used in the buffer, for completion."
  12934. (let (tags)
  12935. (save-excursion
  12936. (goto-char (point-min))
  12937. (while (re-search-forward
  12938. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12939. (when (equal (char-after (point-at-bol 0)) ?*)
  12940. (mapc (lambda (x) (add-to-list 'tags x))
  12941. (org-split-string (org-match-string-no-properties 1) ":")))))
  12942. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12943. (mapcar 'list tags)))
  12944. ;;;; The mapping API
  12945. ;;;###autoload
  12946. (defun org-map-entries (func &optional match scope &rest skip)
  12947. "Call FUNC at each headline selected by MATCH in SCOPE.
  12948. FUNC is a function or a lisp form. The function will be called without
  12949. arguments, with the cursor positioned at the beginning of the headline.
  12950. The return values of all calls to the function will be collected and
  12951. returned as a list.
  12952. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12953. does not need to preserve point. After evaluation, the cursor will be
  12954. moved to the end of the line (presumably of the headline of the
  12955. processed entry) and search continues from there. Under some
  12956. circumstances, this may not produce the wanted results. For example,
  12957. if you have removed (e.g. archived) the current (sub)tree it could
  12958. mean that the next entry will be skipped entirely. In such cases, you
  12959. can specify the position from where search should continue by making
  12960. FUNC set the variable `org-map-continue-from' to the desired buffer
  12961. position.
  12962. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12963. Only headlines that are matched by this query will be considered during
  12964. the iteration. When MATCH is nil or t, all headlines will be
  12965. visited by the iteration.
  12966. SCOPE determines the scope of this command. It can be any of:
  12967. nil The current buffer, respecting the restriction if any
  12968. tree The subtree started with the entry at point
  12969. region The entries within the active region, if any
  12970. region-start-level
  12971. The entries within the active region, but only those at
  12972. the same level than the first one.
  12973. file The current buffer, without restriction
  12974. file-with-archives
  12975. The current buffer, and any archives associated with it
  12976. agenda All agenda files
  12977. agenda-with-archives
  12978. All agenda files with any archive files associated with them
  12979. \(file1 file2 ...)
  12980. If this is a list, all files in the list will be scanned
  12981. The remaining args are treated as settings for the skipping facilities of
  12982. the scanner. The following items can be given here:
  12983. archive skip trees with the archive tag.
  12984. comment skip trees with the COMMENT keyword
  12985. function or Emacs Lisp form:
  12986. will be used as value for `org-agenda-skip-function', so whenever
  12987. the function returns t, FUNC will not be called for that
  12988. entry and search will continue from the point where the
  12989. function leaves it.
  12990. If your function needs to retrieve the tags including inherited tags
  12991. at the *current* entry, you can use the value of the variable
  12992. `org-scanner-tags' which will be much faster than getting the value
  12993. with `org-get-tags-at'. If your function gets properties with
  12994. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12995. to t around the call to `org-entry-properties' to get the same speedup.
  12996. Note that if your function moves around to retrieve tags and properties at
  12997. a *different* entry, you cannot use these techniques."
  12998. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12999. (not (org-region-active-p)))
  13000. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13001. (org-agenda-skip-archived-trees (memq 'archive skip))
  13002. (org-agenda-skip-comment-trees (memq 'comment skip))
  13003. (org-agenda-skip-function
  13004. (car (org-delete-all '(comment archive) skip)))
  13005. (org-tags-match-list-sublevels t)
  13006. (start-level (eq scope 'region-start-level))
  13007. matcher file res
  13008. org-todo-keywords-for-agenda
  13009. org-done-keywords-for-agenda
  13010. org-todo-keyword-alist-for-agenda
  13011. org-drawers-for-agenda
  13012. org-tag-alist-for-agenda
  13013. todo-only)
  13014. (cond
  13015. ((eq match t) (setq matcher t))
  13016. ((eq match nil) (setq matcher t))
  13017. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13018. (save-excursion
  13019. (save-restriction
  13020. (cond ((eq scope 'tree)
  13021. (org-back-to-heading t)
  13022. (org-narrow-to-subtree)
  13023. (setq scope nil))
  13024. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13025. (org-region-active-p))
  13026. ;; If needed, set start-level to a string like "2"
  13027. (when start-level
  13028. (save-excursion
  13029. (goto-char (region-beginning))
  13030. (unless (org-at-heading-p) (outline-next-heading))
  13031. (setq start-level (org-current-level))))
  13032. (narrow-to-region (region-beginning)
  13033. (save-excursion
  13034. (goto-char (region-end))
  13035. (unless (and (bolp) (org-at-heading-p))
  13036. (outline-next-heading))
  13037. (point)))
  13038. (setq scope nil)))
  13039. (if (not scope)
  13040. (progn
  13041. (org-agenda-prepare-buffers
  13042. (list (buffer-file-name (current-buffer))))
  13043. (setq res (org-scan-tags func matcher todo-only start-level)))
  13044. ;; Get the right scope
  13045. (cond
  13046. ((and scope (listp scope) (symbolp (car scope)))
  13047. (setq scope (eval scope)))
  13048. ((eq scope 'agenda)
  13049. (setq scope (org-agenda-files t)))
  13050. ((eq scope 'agenda-with-archives)
  13051. (setq scope (org-agenda-files t))
  13052. (setq scope (org-add-archive-files scope)))
  13053. ((eq scope 'file)
  13054. (setq scope (list (buffer-file-name))))
  13055. ((eq scope 'file-with-archives)
  13056. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13057. (org-agenda-prepare-buffers scope)
  13058. (while (setq file (pop scope))
  13059. (with-current-buffer (org-find-base-buffer-visiting file)
  13060. (save-excursion
  13061. (save-restriction
  13062. (widen)
  13063. (goto-char (point-min))
  13064. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13065. res)))
  13066. ;;;; Properties
  13067. ;;; Setting and retrieving properties
  13068. (defconst org-special-properties
  13069. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13070. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13071. "The special properties valid in Org-mode.
  13072. These are properties that are not defined in the property drawer,
  13073. but in some other way.")
  13074. (defconst org-default-properties
  13075. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13076. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13077. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13078. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13079. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13080. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13081. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13082. "Some properties that are used by Org-mode for various purposes.
  13083. Being in this list makes sure that they are offered for completion.")
  13084. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13085. "Regular expression matching the first line of a property drawer.")
  13086. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13087. "Regular expression matching the last line of a property drawer.")
  13088. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13089. "Regular expression matching the first line of a property drawer.")
  13090. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13091. "Regular expression matching the first line of a property drawer.")
  13092. (defconst org-property-drawer-re
  13093. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13094. org-property-end-re "\\)\n?")
  13095. "Matches an entire property drawer.")
  13096. (defconst org-clock-drawer-re
  13097. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13098. org-property-end-re "\\)\n?")
  13099. "Matches an entire clock drawer.")
  13100. (defsubst org-re-property (property)
  13101. "Return a regexp matching a PROPERTY line.
  13102. Match group 1 will be set to the value."
  13103. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13104. (defsubst org-re-property-keyword (property)
  13105. "Return a regexp matching a PROPERTY line, possibly with no
  13106. value for the property."
  13107. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13108. (defun org-property-action ()
  13109. "Do an action on properties."
  13110. (interactive)
  13111. (let (c)
  13112. (org-at-property-p)
  13113. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13114. (setq c (read-char-exclusive))
  13115. (cond
  13116. ((equal c ?s)
  13117. (call-interactively 'org-set-property))
  13118. ((equal c ?d)
  13119. (call-interactively 'org-delete-property))
  13120. ((equal c ?D)
  13121. (call-interactively 'org-delete-property-globally))
  13122. ((equal c ?c)
  13123. (call-interactively 'org-compute-property-at-point))
  13124. (t (error "No such property action %c" c)))))
  13125. (defun org-inc-effort ()
  13126. "Increment the value of the effort property in the current entry."
  13127. (interactive)
  13128. (org-set-effort nil t))
  13129. (defun org-set-effort (&optional value increment)
  13130. "Set the effort property of the current entry.
  13131. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13132. 10th allowed value.
  13133. When INCREMENT is non-nil, set the property to the next allowed value."
  13134. (interactive "P")
  13135. (if (equal value 0) (setq value 10))
  13136. (let* ((completion-ignore-case t)
  13137. (prop org-effort-property)
  13138. (cur (org-entry-get nil prop))
  13139. (allowed (org-property-get-allowed-values nil prop 'table))
  13140. (existing (mapcar 'list (org-property-values prop)))
  13141. rpl
  13142. (val (cond
  13143. ((stringp value) value)
  13144. ((and allowed (integerp value))
  13145. (or (car (nth (1- value) allowed))
  13146. (car (org-last allowed))))
  13147. ((and allowed increment)
  13148. (or (caadr (member (list cur) allowed))
  13149. (error "Allowed effort values are not set")))
  13150. (allowed
  13151. (message "Select 1-9,0, [RET%s]: %s"
  13152. (if cur (concat "=" cur) "")
  13153. (mapconcat 'car allowed " "))
  13154. (setq rpl (read-char-exclusive))
  13155. (if (equal rpl ?\r)
  13156. cur
  13157. (setq rpl (- rpl ?0))
  13158. (if (equal rpl 0) (setq rpl 10))
  13159. (if (and (> rpl 0) (<= rpl (length allowed)))
  13160. (car (nth (1- rpl) allowed))
  13161. (org-completing-read "Effort: " allowed nil))))
  13162. (t
  13163. (let (org-completion-use-ido org-completion-use-iswitchb)
  13164. (org-completing-read
  13165. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13166. (concat "[" cur "]") "")
  13167. ": ")
  13168. existing nil nil "" nil cur))))))
  13169. (unless (equal (org-entry-get nil prop) val)
  13170. (org-entry-put nil prop val))
  13171. (message "%s is now %s" prop val)))
  13172. (defun org-at-property-p ()
  13173. "Is cursor inside a property drawer?"
  13174. (save-excursion
  13175. (beginning-of-line 1)
  13176. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13177. (save-match-data ;; Used by calling procedures
  13178. (let ((p (point))
  13179. (range (unless (org-before-first-heading-p)
  13180. (org-get-property-block))))
  13181. (and range (<= (car range) p) (< p (cdr range))))))))
  13182. (defun org-get-property-block (&optional beg end force)
  13183. "Return the (beg . end) range of the body of the property drawer.
  13184. BEG and END are the beginning and end of the current subtree, or of
  13185. the part before the first headline. If they are not given, they will
  13186. be found. If the drawer does not exist and FORCE is non-nil, create
  13187. the drawer."
  13188. (catch 'exit
  13189. (save-excursion
  13190. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13191. (progn (org-back-to-heading t) (point))))
  13192. (end (or end (and (not (outline-next-heading)) (point-max))
  13193. (point))))
  13194. (goto-char beg)
  13195. (if (re-search-forward org-property-start-re end t)
  13196. (setq beg (1+ (match-end 0)))
  13197. (if force
  13198. (save-excursion
  13199. (org-insert-property-drawer)
  13200. (setq end (progn (outline-next-heading) (point))))
  13201. (throw 'exit nil))
  13202. (goto-char beg)
  13203. (if (re-search-forward org-property-start-re end t)
  13204. (setq beg (1+ (match-end 0)))))
  13205. (if (re-search-forward org-property-end-re end t)
  13206. (setq end (match-beginning 0))
  13207. (or force (throw 'exit nil))
  13208. (goto-char beg)
  13209. (setq end beg)
  13210. (org-indent-line)
  13211. (insert ":END:\n"))
  13212. (cons beg end)))))
  13213. (defun org-entry-properties (&optional pom which specific)
  13214. "Get all properties of the entry at point-or-marker POM.
  13215. This includes the TODO keyword, the tags, time strings for deadline,
  13216. scheduled, and clocking, and any additional properties defined in the
  13217. entry. The return value is an alist, keys may occur multiple times
  13218. if the property key was used several times.
  13219. POM may also be nil, in which case the current entry is used.
  13220. If WHICH is nil or `all', get all properties. If WHICH is
  13221. `special' or `standard', only get that subclass. If WHICH
  13222. is a string only get exactly this property. SPECIFIC can be a string, the
  13223. specific property we are interested in. Specifying it can speed
  13224. things up because then unnecessary parsing is avoided."
  13225. (setq which (or which 'all))
  13226. (org-with-point-at pom
  13227. (let ((clockstr (substring org-clock-string 0 -1))
  13228. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13229. (case-fold-search nil)
  13230. beg end range props sum-props key key1 value string clocksum clocksumt)
  13231. (save-excursion
  13232. (when (condition-case nil
  13233. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13234. (error nil))
  13235. (setq beg (point))
  13236. (setq sum-props (get-text-property (point) 'org-summaries))
  13237. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13238. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13239. (outline-next-heading)
  13240. (setq end (point))
  13241. (when (memq which '(all special))
  13242. ;; Get the special properties, like TODO and tags
  13243. (goto-char beg)
  13244. (when (and (or (not specific) (string= specific "TODO"))
  13245. (looking-at org-todo-line-regexp) (match-end 2))
  13246. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13247. (when (and (or (not specific) (string= specific "PRIORITY"))
  13248. (looking-at org-priority-regexp))
  13249. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13250. (when (or (not specific) (string= specific "FILE"))
  13251. (push (cons "FILE" buffer-file-name) props))
  13252. (when (and (or (not specific) (string= specific "TAGS"))
  13253. (setq value (org-get-tags-string))
  13254. (string-match "\\S-" value))
  13255. (push (cons "TAGS" value) props))
  13256. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13257. (setq value (org-get-tags-at)))
  13258. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13259. ":"))
  13260. props))
  13261. (when (or (not specific) (string= specific "BLOCKED"))
  13262. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13263. (when (or (not specific)
  13264. (member specific
  13265. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13266. "TIMESTAMP" "TIMESTAMP_IA")))
  13267. (catch 'match
  13268. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13269. (setq key (if (match-end 1)
  13270. (substring (org-match-string-no-properties 1)
  13271. 0 -1))
  13272. string (if (equal key clockstr)
  13273. (org-trim
  13274. (buffer-substring-no-properties
  13275. (match-beginning 3) (goto-char
  13276. (point-at-eol))))
  13277. (substring (org-match-string-no-properties 3)
  13278. 1 -1)))
  13279. ;; Get the correct property name from the key. This is
  13280. ;; necessary if the user has configured time keywords.
  13281. (setq key1 (concat key ":"))
  13282. (cond
  13283. ((not key)
  13284. (setq key
  13285. (if (= (char-after (match-beginning 3)) ?\[)
  13286. "TIMESTAMP_IA" "TIMESTAMP")))
  13287. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13288. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13289. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13290. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13291. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13292. (progn
  13293. (push (cons key string) props)
  13294. ;; no need to search further if match is found
  13295. (throw 'match t))
  13296. (when (or (equal key "CLOCK") (not (assoc key props)))
  13297. (push (cons key string) props)))))))
  13298. (when (memq which '(all standard))
  13299. ;; Get the standard properties, like :PROP: ...
  13300. (setq range (org-get-property-block beg end))
  13301. (when range
  13302. (goto-char (car range))
  13303. (while (re-search-forward
  13304. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13305. (cdr range) t)
  13306. (setq key (org-match-string-no-properties 1)
  13307. value (org-trim (or (org-match-string-no-properties 2) "")))
  13308. (unless (member key excluded)
  13309. (push (cons key (or value "")) props)))))
  13310. (if clocksum
  13311. (push (cons "CLOCKSUM"
  13312. (org-columns-number-to-string (/ (float clocksum) 60.)
  13313. 'add_times))
  13314. props))
  13315. (if clocksumt
  13316. (push (cons "CLOCKSUM_T"
  13317. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13318. 'add_times))
  13319. props))
  13320. (unless (assoc "CATEGORY" props)
  13321. (push (cons "CATEGORY" (org-get-category)) props))
  13322. (append sum-props (nreverse props)))))))
  13323. (defun org-entry-get (pom property &optional inherit literal-nil)
  13324. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13325. If INHERIT is non-nil and the entry does not have the property,
  13326. then also check higher levels of the hierarchy.
  13327. If INHERIT is the symbol `selective', use inheritance only if the setting
  13328. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13329. If the property is present but empty, the return value is the empty string.
  13330. If the property is not present at all, nil is returned.
  13331. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13332. do not interpret it as the list atom nil. This is used for inheritance
  13333. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13334. (org-with-point-at pom
  13335. (if (and inherit (if (eq inherit 'selective)
  13336. (org-property-inherit-p property)
  13337. t))
  13338. (org-entry-get-with-inheritance property literal-nil)
  13339. (if (member property org-special-properties)
  13340. ;; We need a special property. Use `org-entry-properties' to
  13341. ;; retrieve it, but specify the wanted property
  13342. (cdr (assoc property (org-entry-properties nil 'special property)))
  13343. (let* ((range (org-get-property-block))
  13344. (props (list (or (assoc property org-file-properties)
  13345. (assoc property org-global-properties)
  13346. (assoc property org-global-properties-fixed))))
  13347. (ap (lambda (key)
  13348. (when (re-search-forward
  13349. (org-re-property key) (cdr range) t)
  13350. (setq props
  13351. (org-update-property-plist
  13352. key
  13353. (if (match-end 1)
  13354. (org-match-string-no-properties 1) "")
  13355. props)))))
  13356. val)
  13357. (when (and range (goto-char (car range)))
  13358. (funcall ap property)
  13359. (goto-char (car range))
  13360. (while (funcall ap (concat property "+")))
  13361. (setq val (cdr (assoc property props)))
  13362. (when val (if literal-nil val (org-not-nil val)))))))))
  13363. (defun org-property-or-variable-value (var &optional inherit)
  13364. "Check if there is a property fixing the value of VAR.
  13365. If yes, return this value. If not, return the current value of the variable."
  13366. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13367. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13368. (read prop)
  13369. (symbol-value var))))
  13370. (defun org-entry-delete (pom property)
  13371. "Delete the property PROPERTY from entry at point-or-marker POM."
  13372. (org-with-point-at pom
  13373. (if (member property org-special-properties)
  13374. nil ; cannot delete these properties.
  13375. (let ((range (org-get-property-block)))
  13376. (if (and range
  13377. (goto-char (car range))
  13378. (re-search-forward
  13379. (org-re-property property)
  13380. (cdr range) t))
  13381. (progn
  13382. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13383. t)
  13384. nil)))))
  13385. ;; Multi-values properties are properties that contain multiple values
  13386. ;; These values are assumed to be single words, separated by whitespace.
  13387. (defun org-entry-add-to-multivalued-property (pom property value)
  13388. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13389. (let* ((old (org-entry-get pom property))
  13390. (values (and old (org-split-string old "[ \t]"))))
  13391. (setq value (org-entry-protect-space value))
  13392. (unless (member value values)
  13393. (setq values (cons value values))
  13394. (org-entry-put pom property
  13395. (mapconcat 'identity values " ")))))
  13396. (defun org-entry-remove-from-multivalued-property (pom property value)
  13397. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13398. (let* ((old (org-entry-get pom property))
  13399. (values (and old (org-split-string old "[ \t]"))))
  13400. (setq value (org-entry-protect-space value))
  13401. (when (member value values)
  13402. (setq values (delete value values))
  13403. (org-entry-put pom property
  13404. (mapconcat 'identity values " ")))))
  13405. (defun org-entry-member-in-multivalued-property (pom property value)
  13406. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13407. (let* ((old (org-entry-get pom property))
  13408. (values (and old (org-split-string old "[ \t]"))))
  13409. (setq value (org-entry-protect-space value))
  13410. (member value values)))
  13411. (defun org-entry-get-multivalued-property (pom property)
  13412. "Return a list of values in a multivalued property."
  13413. (let* ((value (org-entry-get pom property))
  13414. (values (and value (org-split-string value "[ \t]"))))
  13415. (mapcar 'org-entry-restore-space values)))
  13416. (defun org-entry-put-multivalued-property (pom property &rest values)
  13417. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13418. VALUES should be a list of strings. Spaces will be protected."
  13419. (org-entry-put pom property
  13420. (mapconcat 'org-entry-protect-space values " "))
  13421. (let* ((value (org-entry-get pom property))
  13422. (values (and value (org-split-string value "[ \t]"))))
  13423. (mapcar 'org-entry-restore-space values)))
  13424. (defun org-entry-protect-space (s)
  13425. "Protect spaces and newline in string S."
  13426. (while (string-match " " s)
  13427. (setq s (replace-match "%20" t t s)))
  13428. (while (string-match "\n" s)
  13429. (setq s (replace-match "%0A" t t s)))
  13430. s)
  13431. (defun org-entry-restore-space (s)
  13432. "Restore spaces and newline in string S."
  13433. (while (string-match "%20" s)
  13434. (setq s (replace-match " " t t s)))
  13435. (while (string-match "%0A" s)
  13436. (setq s (replace-match "\n" t t s)))
  13437. s)
  13438. (defvar org-entry-property-inherited-from (make-marker)
  13439. "Marker pointing to the entry from where a property was inherited.
  13440. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13441. location of the entry where the inheritance search matched. If there was
  13442. no match, the marker will point nowhere.
  13443. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13444. is set.")
  13445. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13446. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13447. The search will stop at the first ancestor which has the property defined.
  13448. If the value found is \"nil\", return nil to show that the property
  13449. should be considered as undefined (this is the meaning of nil here).
  13450. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13451. (move-marker org-entry-property-inherited-from nil)
  13452. (let (tmp)
  13453. (save-excursion
  13454. (save-restriction
  13455. (widen)
  13456. (catch 'ex
  13457. (while t
  13458. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13459. (or (ignore-errors (org-back-to-heading t))
  13460. (goto-char (point-min)))
  13461. (move-marker org-entry-property-inherited-from (point))
  13462. (throw 'ex tmp))
  13463. (or (ignore-errors (org-up-heading-safe))
  13464. (throw 'ex nil))))))
  13465. (setq tmp (or tmp
  13466. (cdr (assoc property org-file-properties))
  13467. (cdr (assoc property org-global-properties))
  13468. (cdr (assoc property org-global-properties-fixed))))
  13469. (if literal-nil tmp (org-not-nil tmp))))
  13470. (defvar org-property-changed-functions nil
  13471. "Hook called when the value of a property has changed.
  13472. Each hook function should accept two arguments, the name of the property
  13473. and the new value.")
  13474. (defun org-entry-put (pom property value)
  13475. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13476. (org-with-point-at pom
  13477. (org-back-to-heading t)
  13478. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13479. range)
  13480. (cond
  13481. ((equal property "TODO")
  13482. (when (and (stringp value) (string-match "\\S-" value)
  13483. (not (member value org-todo-keywords-1)))
  13484. (error "\"%s\" is not a valid TODO state" value))
  13485. (if (or (not value)
  13486. (not (string-match "\\S-" value)))
  13487. (setq value 'none))
  13488. (org-todo value)
  13489. (org-set-tags nil 'align))
  13490. ((equal property "PRIORITY")
  13491. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13492. (string-to-char value) ?\ ))
  13493. (org-set-tags nil 'align))
  13494. ((equal property "CLOCKSUM")
  13495. (if (not (re-search-forward
  13496. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13497. (error "Cannot find a clock log")
  13498. (goto-char (- (match-end 1) 2))
  13499. (cond
  13500. ((eq value 'earlier) (org-timestamp-down))
  13501. ((eq value 'later) (org-timestamp-up)))
  13502. (org-clock-sum-current-item)))
  13503. ((equal property "SCHEDULED")
  13504. (if (re-search-forward org-scheduled-time-regexp end t)
  13505. (cond
  13506. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13507. ((eq value 'later) (org-timestamp-change 1 'day))
  13508. (t (call-interactively 'org-schedule)))
  13509. (call-interactively 'org-schedule)))
  13510. ((equal property "DEADLINE")
  13511. (if (re-search-forward org-deadline-time-regexp end t)
  13512. (cond
  13513. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13514. ((eq value 'later) (org-timestamp-change 1 'day))
  13515. (t (call-interactively 'org-deadline)))
  13516. (call-interactively 'org-deadline)))
  13517. ((member property org-special-properties)
  13518. (error "The %s property can not yet be set with `org-entry-put'"
  13519. property))
  13520. (t ; a non-special property
  13521. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13522. (setq range (org-get-property-block beg end 'force))
  13523. (goto-char (car range))
  13524. (if (re-search-forward
  13525. (org-re-property-keyword property) (cdr range) t)
  13526. (progn
  13527. (delete-region (match-beginning 0) (match-end 0))
  13528. (goto-char (match-beginning 0)))
  13529. (goto-char (cdr range))
  13530. (insert "\n")
  13531. (backward-char 1)
  13532. (org-indent-line))
  13533. (insert ":" property ":")
  13534. (and value (insert " " value))
  13535. (org-indent-line)))))
  13536. (run-hook-with-args 'org-property-changed-functions property value)))
  13537. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13538. "Get all property keys in the current buffer.
  13539. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13540. like tags and TODO state.
  13541. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13542. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13543. and others.
  13544. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13545. formats in the current buffer."
  13546. (let (rtn range cfmt s p)
  13547. (save-excursion
  13548. (save-restriction
  13549. (widen)
  13550. (goto-char (point-min))
  13551. (while (re-search-forward org-property-start-re nil t)
  13552. (setq range (org-get-property-block))
  13553. (goto-char (car range))
  13554. (while (re-search-forward
  13555. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13556. (cdr range) t)
  13557. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13558. (outline-next-heading))))
  13559. (when include-specials
  13560. (setq rtn (append org-special-properties rtn)))
  13561. (when include-defaults
  13562. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13563. (add-to-list 'rtn org-effort-property))
  13564. (when include-columns
  13565. (save-excursion
  13566. (save-restriction
  13567. (widen)
  13568. (goto-char (point-min))
  13569. (while (re-search-forward
  13570. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13571. nil t)
  13572. (setq cfmt (match-string 2) s 0)
  13573. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13574. cfmt s)
  13575. (setq s (match-end 0)
  13576. p (match-string 1 cfmt))
  13577. (unless (or (equal p "ITEM")
  13578. (member p org-special-properties))
  13579. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13580. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13581. (defun org-property-values (key)
  13582. "Return a list of all values of property KEY in the current buffer."
  13583. (save-excursion
  13584. (save-restriction
  13585. (widen)
  13586. (goto-char (point-min))
  13587. (let ((re (org-re-property key))
  13588. values)
  13589. (while (re-search-forward re nil t)
  13590. (add-to-list 'values (org-trim (match-string 1))))
  13591. (delete "" values)))))
  13592. (defun org-insert-property-drawer ()
  13593. "Insert a property drawer into the current entry."
  13594. (org-back-to-heading t)
  13595. (looking-at org-outline-regexp)
  13596. (let ((indent (if org-adapt-indentation
  13597. (- (match-end 0) (match-beginning 0))
  13598. 0))
  13599. (beg (point))
  13600. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13601. end hiddenp)
  13602. (outline-next-heading)
  13603. (setq end (point))
  13604. (goto-char beg)
  13605. (while (re-search-forward re end t))
  13606. (setq hiddenp (outline-invisible-p))
  13607. (end-of-line 1)
  13608. (and (equal (char-after) ?\n) (forward-char 1))
  13609. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13610. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13611. ;; just skip this line
  13612. (beginning-of-line 2)
  13613. ;; Drawer start, find the end
  13614. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13615. (beginning-of-line 1)))
  13616. (org-skip-over-state-notes)
  13617. (skip-chars-backward " \t\n\r")
  13618. (if (eq (char-before) ?*) (forward-char 1))
  13619. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13620. (beginning-of-line 0)
  13621. (org-indent-to-column indent)
  13622. (beginning-of-line 2)
  13623. (org-indent-to-column indent)
  13624. (beginning-of-line 0)
  13625. (if hiddenp
  13626. (save-excursion
  13627. (org-back-to-heading t)
  13628. (hide-entry))
  13629. (org-flag-drawer t))))
  13630. (defun org-insert-drawer (&optional arg drawer)
  13631. "Insert a drawer at point.
  13632. Optional argument DRAWER, when non-nil, is a string representing
  13633. drawer's name. Otherwise, the user is prompted for a name.
  13634. If a region is active, insert the drawer around that region
  13635. instead.
  13636. Point is left between drawer's boundaries."
  13637. (interactive "P")
  13638. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13639. "LOGBOOK"))
  13640. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13641. ;; internally by Org. They are meant to be inserted
  13642. ;; automatically.
  13643. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13644. ;; Remove system drawers from list. Note: For some reason,
  13645. ;; `org-completing-read' ignores the predicate while
  13646. ;; `completing-read' handles it fine.
  13647. (drawer (if arg "PROPERTIES"
  13648. (or drawer
  13649. (completing-read
  13650. "Drawer: " org-drawers
  13651. (lambda (d) (not (member d system-drawers))))))))
  13652. (cond
  13653. ;; With C-u, fall back on `org-insert-property-drawer'
  13654. (arg (org-insert-property-drawer))
  13655. ;; With an active region, insert a drawer at point.
  13656. ((not (org-region-active-p))
  13657. (progn
  13658. (unless (bolp) (insert "\n"))
  13659. (insert (format ":%s:\n\n:END:\n" drawer))
  13660. (forward-line -2)))
  13661. ;; Otherwise, insert the drawer at point
  13662. (t
  13663. (let ((rbeg (region-beginning))
  13664. (rend (copy-marker (region-end))))
  13665. (unwind-protect
  13666. (progn
  13667. (goto-char rbeg)
  13668. (beginning-of-line)
  13669. (when (save-excursion
  13670. (re-search-forward org-outline-regexp-bol rend t))
  13671. (error "Drawers cannot contain headlines"))
  13672. ;; Position point at the beginning of the first
  13673. ;; non-blank line in region. Insert drawer's opening
  13674. ;; there, then indent it.
  13675. (org-skip-whitespace)
  13676. (beginning-of-line)
  13677. (insert ":" drawer ":\n")
  13678. (forward-line -1)
  13679. (indent-for-tab-command)
  13680. ;; Move point to the beginning of the first blank line
  13681. ;; after the last non-blank line in region. Insert
  13682. ;; drawer's closing, then indent it.
  13683. (goto-char rend)
  13684. (skip-chars-backward " \r\t\n")
  13685. (insert "\n:END:")
  13686. (indent-for-tab-command)
  13687. (unless (eolp) (insert "\n")))
  13688. ;; Clear marker, whatever the outcome of insertion is.
  13689. (set-marker rend nil)))))))
  13690. (defvar org-property-set-functions-alist nil
  13691. "Property set function alist.
  13692. Each entry should have the following format:
  13693. (PROPERTY . READ-FUNCTION)
  13694. The read function will be called with the same argument as
  13695. `org-completing-read'.")
  13696. (defun org-set-property-function (property)
  13697. "Get the function that should be used to set PROPERTY.
  13698. This is computed according to `org-property-set-functions-alist'."
  13699. (or (cdr (assoc property org-property-set-functions-alist))
  13700. 'org-completing-read))
  13701. (defun org-read-property-value (property)
  13702. "Read PROPERTY value from user."
  13703. (let* ((completion-ignore-case t)
  13704. (allowed (org-property-get-allowed-values nil property 'table))
  13705. (cur (org-entry-get nil property))
  13706. (prompt (concat property " value"
  13707. (if (and cur (string-match "\\S-" cur))
  13708. (concat " [" cur "]") "") ": "))
  13709. (set-function (org-set-property-function property))
  13710. (val (if allowed
  13711. (funcall set-function prompt allowed nil
  13712. (not (get-text-property 0 'org-unrestricted
  13713. (caar allowed))))
  13714. (let (org-completion-use-ido org-completion-use-iswitchb)
  13715. (funcall set-function prompt
  13716. (mapcar 'list (org-property-values property))
  13717. nil nil "" nil cur)))))
  13718. (if (equal val "")
  13719. cur
  13720. val)))
  13721. (defvar org-last-set-property nil)
  13722. (defun org-read-property-name ()
  13723. "Read a property name."
  13724. (let* ((completion-ignore-case t)
  13725. (keys (org-buffer-property-keys nil t t))
  13726. (default-prop (or (save-excursion
  13727. (save-match-data
  13728. (beginning-of-line)
  13729. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13730. (null (string= (match-string 1) "END"))
  13731. (match-string 1))))
  13732. org-last-set-property))
  13733. (property (org-icompleting-read
  13734. (concat "Property"
  13735. (if default-prop (concat " [" default-prop "]") "")
  13736. ": ")
  13737. (mapcar 'list keys)
  13738. nil nil nil nil
  13739. default-prop
  13740. )))
  13741. (if (member property keys)
  13742. property
  13743. (or (cdr (assoc (downcase property)
  13744. (mapcar (lambda (x) (cons (downcase x) x))
  13745. keys)))
  13746. property))))
  13747. (defun org-set-property (property value)
  13748. "In the current entry, set PROPERTY to VALUE.
  13749. When called interactively, this will prompt for a property name, offering
  13750. completion on existing and default properties. And then it will prompt
  13751. for a value, offering completion either on allowed values (via an inherited
  13752. xxx_ALL property) or on existing values in other instances of this property
  13753. in the current file."
  13754. (interactive (list nil nil))
  13755. (let* ((property (or property (org-read-property-name)))
  13756. (value (or value (org-read-property-value property)))
  13757. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13758. (setq org-last-set-property property)
  13759. ;; Possibly postprocess the inserted value:
  13760. (when fn (setq value (funcall fn value)))
  13761. (unless (equal (org-entry-get nil property) value)
  13762. (org-entry-put nil property value))))
  13763. (defun org-delete-property (property)
  13764. "In the current entry, delete PROPERTY."
  13765. (interactive
  13766. (let* ((completion-ignore-case t)
  13767. (prop (org-icompleting-read "Property: "
  13768. (org-entry-properties nil 'standard))))
  13769. (list prop)))
  13770. (message "Property %s %s" property
  13771. (if (org-entry-delete nil property)
  13772. "deleted"
  13773. "was not present in the entry")))
  13774. (defun org-delete-property-globally (property)
  13775. "Remove PROPERTY globally, from all entries."
  13776. (interactive
  13777. (let* ((completion-ignore-case t)
  13778. (prop (org-icompleting-read
  13779. "Globally remove property: "
  13780. (mapcar 'list (org-buffer-property-keys)))))
  13781. (list prop)))
  13782. (save-excursion
  13783. (save-restriction
  13784. (widen)
  13785. (goto-char (point-min))
  13786. (let ((cnt 0))
  13787. (while (re-search-forward
  13788. (org-re-property property)
  13789. nil t)
  13790. (setq cnt (1+ cnt))
  13791. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13792. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13793. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13794. (defun org-compute-property-at-point ()
  13795. "Compute the property at point.
  13796. This looks for an enclosing column format, extracts the operator and
  13797. then applies it to the property in the column format's scope."
  13798. (interactive)
  13799. (unless (org-at-property-p)
  13800. (error "Not at a property"))
  13801. (let ((prop (org-match-string-no-properties 2)))
  13802. (org-columns-get-format-and-top-level)
  13803. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13804. (error "No operator defined for property %s" prop))
  13805. (org-columns-compute prop)))
  13806. (defvar org-property-allowed-value-functions nil
  13807. "Hook for functions supplying allowed values for a specific property.
  13808. The functions must take a single argument, the name of the property, and
  13809. return a flat list of allowed values. If \":ETC\" is one of
  13810. the values, this means that these values are intended as defaults for
  13811. completion, but that other values should be allowed too.
  13812. The functions must return nil if they are not responsible for this
  13813. property.")
  13814. (defun org-property-get-allowed-values (pom property &optional table)
  13815. "Get allowed values for the property PROPERTY.
  13816. When TABLE is non-nil, return an alist that can directly be used for
  13817. completion."
  13818. (let (vals)
  13819. (cond
  13820. ((equal property "TODO")
  13821. (setq vals (org-with-point-at pom
  13822. (append org-todo-keywords-1 '("")))))
  13823. ((equal property "PRIORITY")
  13824. (let ((n org-lowest-priority))
  13825. (while (>= n org-highest-priority)
  13826. (push (char-to-string n) vals)
  13827. (setq n (1- n)))))
  13828. ((member property org-special-properties))
  13829. ((setq vals (run-hook-with-args-until-success
  13830. 'org-property-allowed-value-functions property)))
  13831. (t
  13832. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13833. (when (and vals (string-match "\\S-" vals))
  13834. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13835. (setq vals (mapcar (lambda (x)
  13836. (cond ((stringp x) x)
  13837. ((numberp x) (number-to-string x))
  13838. ((symbolp x) (symbol-name x))
  13839. (t "???")))
  13840. vals)))))
  13841. (when (member ":ETC" vals)
  13842. (setq vals (remove ":ETC" vals))
  13843. (org-add-props (car vals) '(org-unrestricted t)))
  13844. (if table (mapcar 'list vals) vals)))
  13845. (defun org-property-previous-allowed-value (&optional previous)
  13846. "Switch to the next allowed value for this property."
  13847. (interactive)
  13848. (org-property-next-allowed-value t))
  13849. (defun org-property-next-allowed-value (&optional previous)
  13850. "Switch to the next allowed value for this property."
  13851. (interactive)
  13852. (unless (org-at-property-p)
  13853. (error "Not at a property"))
  13854. (let* ((key (match-string 2))
  13855. (value (match-string 3))
  13856. (allowed (or (org-property-get-allowed-values (point) key)
  13857. (and (member value '("[ ]" "[-]" "[X]"))
  13858. '("[ ]" "[X]"))))
  13859. nval)
  13860. (unless allowed
  13861. (error "Allowed values for this property have not been defined"))
  13862. (if previous (setq allowed (reverse allowed)))
  13863. (if (member value allowed)
  13864. (setq nval (car (cdr (member value allowed)))))
  13865. (setq nval (or nval (car allowed)))
  13866. (if (equal nval value)
  13867. (error "Only one allowed value for this property"))
  13868. (org-at-property-p)
  13869. (replace-match (concat " :" key ": " nval) t t)
  13870. (org-indent-line)
  13871. (beginning-of-line 1)
  13872. (skip-chars-forward " \t")
  13873. (run-hook-with-args 'org-property-changed-functions key nval)))
  13874. (defun org-find-olp (path &optional this-buffer)
  13875. "Return a marker pointing to the entry at outline path OLP.
  13876. If anything goes wrong, throw an error.
  13877. You can wrap this call to catch the error like this:
  13878. (condition-case msg
  13879. (org-mobile-locate-entry (match-string 4))
  13880. (error (nth 1 msg)))
  13881. The return value will then be either a string with the error message,
  13882. or a marker if everything is OK.
  13883. If THIS-BUFFER is set, the outline path does not contain a file,
  13884. only headings."
  13885. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13886. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13887. (level 1)
  13888. (lmin 1)
  13889. (lmax 1)
  13890. limit re end found pos heading cnt flevel)
  13891. (unless buffer (error "File not found :%s" file))
  13892. (with-current-buffer buffer
  13893. (save-excursion
  13894. (save-restriction
  13895. (widen)
  13896. (setq limit (point-max))
  13897. (goto-char (point-min))
  13898. (while (setq heading (pop path))
  13899. (setq re (format org-complex-heading-regexp-format
  13900. (regexp-quote heading)))
  13901. (setq cnt 0 pos (point))
  13902. (while (re-search-forward re end t)
  13903. (setq level (- (match-end 1) (match-beginning 1)))
  13904. (if (and (>= level lmin) (<= level lmax))
  13905. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13906. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13907. lmax heading))
  13908. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13909. lmax heading))
  13910. (goto-char found)
  13911. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13912. (setq end (save-excursion (org-end-of-subtree t t))))
  13913. (when (org-at-heading-p)
  13914. (move-marker (make-marker) (point))))))))
  13915. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13916. "Find node HEADING in BUFFER.
  13917. Return a marker to the heading if it was found, or nil if not.
  13918. If POS-ONLY is set, return just the position instead of a marker.
  13919. The heading text must match exact, but it may have a TODO keyword,
  13920. a priority cookie and tags in the standard locations."
  13921. (with-current-buffer (or buffer (current-buffer))
  13922. (save-excursion
  13923. (save-restriction
  13924. (widen)
  13925. (goto-char (point-min))
  13926. (let (case-fold-search)
  13927. (if (re-search-forward
  13928. (format org-complex-heading-regexp-format
  13929. (regexp-quote heading)) nil t)
  13930. (if pos-only
  13931. (match-beginning 0)
  13932. (move-marker (make-marker) (match-beginning 0)))))))))
  13933. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13934. "Find Org node headline HEADING in all .org files in directory DIR.
  13935. When the target headline is found, return a marker to this location."
  13936. (let ((files (directory-files (or dir default-directory)
  13937. nil "\\`[^.#].*\\.org\\'"))
  13938. file visiting m buffer)
  13939. (catch 'found
  13940. (while (setq file (pop files))
  13941. (message "trying %s" file)
  13942. (setq visiting (org-find-base-buffer-visiting file))
  13943. (setq buffer (or visiting (find-file-noselect file)))
  13944. (setq m (org-find-exact-headline-in-buffer
  13945. heading buffer))
  13946. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13947. (and m (throw 'found m))))))
  13948. (defun org-find-entry-with-id (ident)
  13949. "Locate the entry that contains the ID property with exact value IDENT.
  13950. IDENT can be a string, a symbol or a number, this function will search for
  13951. the string representation of it.
  13952. Return the position where this entry starts, or nil if there is no such entry."
  13953. (interactive "sID: ")
  13954. (let ((id (cond
  13955. ((stringp ident) ident)
  13956. ((symbol-name ident) (symbol-name ident))
  13957. ((numberp ident) (number-to-string ident))
  13958. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13959. (case-fold-search nil))
  13960. (save-excursion
  13961. (save-restriction
  13962. (widen)
  13963. (goto-char (point-min))
  13964. (when (re-search-forward
  13965. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13966. nil t)
  13967. (org-back-to-heading t)
  13968. (point))))))
  13969. ;;;; Timestamps
  13970. (defvar org-last-changed-timestamp nil)
  13971. (defvar org-last-inserted-timestamp nil
  13972. "The last time stamp inserted with `org-insert-time-stamp'.")
  13973. (defvar org-time-was-given) ; dynamically scoped parameter
  13974. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13975. (defvar org-ts-what) ; dynamically scoped parameter
  13976. (defun org-time-stamp (arg &optional inactive)
  13977. "Prompt for a date/time and insert a time stamp.
  13978. If the user specifies a time like HH:MM or if this command is
  13979. called with at least one prefix argument, the time stamp contains
  13980. the date and the time. Otherwise, only the date is be included.
  13981. All parts of a date not specified by the user is filled in from
  13982. the current date/time. So if you just press return without
  13983. typing anything, the time stamp will represent the current
  13984. date/time.
  13985. If there is already a timestamp at the cursor, it will be
  13986. modified.
  13987. With two universal prefix arguments, insert an active timestamp
  13988. with the current time without prompting the user."
  13989. (interactive "P")
  13990. (let* ((ts nil)
  13991. (default-time
  13992. ;; Default time is either today, or, when entering a range,
  13993. ;; the range start.
  13994. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13995. (save-excursion
  13996. (re-search-backward
  13997. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13998. (- (point) 20) t)))
  13999. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14000. (current-time)))
  14001. (default-input (and ts (org-get-compact-tod ts)))
  14002. (repeater (save-excursion
  14003. (save-match-data
  14004. (beginning-of-line)
  14005. (when (re-search-forward
  14006. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14007. (save-excursion (progn (end-of-line) (point))) t)
  14008. (match-string 0)))))
  14009. org-time-was-given org-end-time-was-given time)
  14010. (cond
  14011. ((and (org-at-timestamp-p t)
  14012. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14013. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14014. (insert "--")
  14015. (setq time (let ((this-command this-command))
  14016. (org-read-date arg 'totime nil nil
  14017. default-time default-input inactive)))
  14018. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14019. ((org-at-timestamp-p t)
  14020. (setq time (let ((this-command this-command))
  14021. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14022. (when (org-at-timestamp-p t) ; just to get the match data
  14023. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14024. (replace-match "")
  14025. (setq org-last-changed-timestamp
  14026. (org-insert-time-stamp
  14027. time (or org-time-was-given arg)
  14028. inactive nil nil (list org-end-time-was-given)))
  14029. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14030. (setq org-last-changed-timestamp
  14031. (concat (substring org-last-inserted-timestamp 0 -1)
  14032. " " repeater ">"))))
  14033. (message "Timestamp updated"))
  14034. ((equal arg '(16))
  14035. (org-insert-time-stamp (current-time) t))
  14036. (t
  14037. (setq time (let ((this-command this-command))
  14038. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14039. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14040. nil nil (list org-end-time-was-given))))))
  14041. ;; FIXME: can we use this for something else, like computing time differences?
  14042. (defun org-get-compact-tod (s)
  14043. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14044. (let* ((t1 (match-string 1 s))
  14045. (h1 (string-to-number (match-string 2 s)))
  14046. (m1 (string-to-number (match-string 3 s)))
  14047. (t2 (and (match-end 4) (match-string 5 s)))
  14048. (h2 (and t2 (string-to-number (match-string 6 s))))
  14049. (m2 (and t2 (string-to-number (match-string 7 s))))
  14050. dh dm)
  14051. (if (not t2)
  14052. t1
  14053. (setq dh (- h2 h1) dm (- m2 m1))
  14054. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14055. (concat t1 "+" (number-to-string dh)
  14056. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14057. (defun org-time-stamp-inactive (&optional arg)
  14058. "Insert an inactive time stamp.
  14059. An inactive time stamp is enclosed in square brackets instead of angle
  14060. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14061. does not link to the calendar and cannot be changed with the S-cursor keys.
  14062. So these are more for recording a certain time/date."
  14063. (interactive "P")
  14064. (org-time-stamp arg 'inactive))
  14065. (defvar org-date-ovl (make-overlay 1 1))
  14066. (overlay-put org-date-ovl 'face 'org-date-selected)
  14067. (org-detach-overlay org-date-ovl)
  14068. (defvar org-ans1) ; dynamically scoped parameter
  14069. (defvar org-ans2) ; dynamically scoped parameter
  14070. (defvar org-plain-time-of-day-regexp) ; defined below
  14071. (defvar org-overriding-default-time nil) ; dynamically scoped
  14072. (defvar org-read-date-overlay nil)
  14073. (defvar org-dcst nil) ; dynamically scoped
  14074. (defvar org-read-date-history nil)
  14075. (defvar org-read-date-final-answer nil)
  14076. (defvar org-read-date-analyze-futurep nil)
  14077. (defvar org-read-date-analyze-forced-year nil)
  14078. (defvar org-read-date-inactive)
  14079. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14080. default-time default-input inactive)
  14081. "Read a date, possibly a time, and make things smooth for the user.
  14082. The prompt will suggest to enter an ISO date, but you can also enter anything
  14083. which will at least partially be understood by `parse-time-string'.
  14084. Unrecognized parts of the date will default to the current day, month, year,
  14085. hour and minute. If this command is called to replace a timestamp at point,
  14086. or to enter the second timestamp of a range, the default time is taken
  14087. from the existing stamp. Furthermore, the command prefers the future,
  14088. so if you are giving a date where the year is not given, and the day-month
  14089. combination is already past in the current year, it will assume you
  14090. mean next year. For details, see the manual. A few examples:
  14091. 3-2-5 --> 2003-02-05
  14092. feb 15 --> currentyear-02-15
  14093. 2/15 --> currentyear-02-15
  14094. sep 12 9 --> 2009-09-12
  14095. 12:45 --> today 12:45
  14096. 22 sept 0:34 --> currentyear-09-22 0:34
  14097. 12 --> currentyear-currentmonth-12
  14098. Fri --> nearest Friday (today or later)
  14099. etc.
  14100. Furthermore you can specify a relative date by giving, as the *first* thing
  14101. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14102. change in days weeks, months, years.
  14103. With a single plus or minus, the date is relative to today. With a double
  14104. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14105. +4d --> four days from today
  14106. +4 --> same as above
  14107. +2w --> two weeks from today
  14108. ++5 --> five days from default date
  14109. The function understands only English month and weekday abbreviations.
  14110. While prompting, a calendar is popped up - you can also select the
  14111. date with the mouse (button 1). The calendar shows a period of three
  14112. months. To scroll it to other months, use the keys `>' and `<'.
  14113. If you don't like the calendar, turn it off with
  14114. \(setq org-read-date-popup-calendar nil)
  14115. With optional argument TO-TIME, the date will immediately be converted
  14116. to an internal time.
  14117. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14118. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14119. still enter a time, and this function will inform the calling routine
  14120. about this change. The calling routine may then choose to change the
  14121. format used to insert the time stamp into the buffer to include the time.
  14122. With optional argument FROM-STRING, read from this string instead from
  14123. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14124. the time/date that is used for everything that is not specified by the
  14125. user."
  14126. (require 'parse-time)
  14127. (let* ((org-time-stamp-rounding-minutes
  14128. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14129. (org-dcst org-display-custom-times)
  14130. (ct (org-current-time))
  14131. (org-def (or org-overriding-default-time default-time ct))
  14132. (org-defdecode (decode-time org-def))
  14133. (dummy (progn
  14134. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14135. (setcar (nthcdr 2 org-defdecode) -1)
  14136. (setcar (nthcdr 1 org-defdecode) 59)
  14137. (setq org-def (apply 'encode-time org-defdecode)
  14138. org-defdecode (decode-time org-def)))))
  14139. (calendar-frame-setup nil)
  14140. (calendar-setup nil)
  14141. (calendar-move-hook nil)
  14142. (calendar-view-diary-initially-flag nil)
  14143. (calendar-view-holidays-initially-flag nil)
  14144. (timestr (format-time-string
  14145. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14146. (prompt (concat (if prompt (concat prompt " ") "")
  14147. (format "Date+time [%s]: " timestr)))
  14148. ans (org-ans0 "") org-ans1 org-ans2 final)
  14149. (cond
  14150. (from-string (setq ans from-string))
  14151. (org-read-date-popup-calendar
  14152. (save-excursion
  14153. (save-window-excursion
  14154. (calendar)
  14155. (org-eval-in-calendar '(setq cursor-type nil) t)
  14156. (unwind-protect
  14157. (progn
  14158. (calendar-forward-day (- (time-to-days org-def)
  14159. (calendar-absolute-from-gregorian
  14160. (calendar-current-date))))
  14161. (org-eval-in-calendar nil t)
  14162. (let* ((old-map (current-local-map))
  14163. (map (copy-keymap calendar-mode-map))
  14164. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14165. (org-defkey map (kbd "RET") 'org-calendar-select)
  14166. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14167. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14168. (org-defkey minibuffer-local-map [(meta shift left)]
  14169. (lambda () (interactive)
  14170. (org-eval-in-calendar '(calendar-backward-month 1))))
  14171. (org-defkey minibuffer-local-map [(meta shift right)]
  14172. (lambda () (interactive)
  14173. (org-eval-in-calendar '(calendar-forward-month 1))))
  14174. (org-defkey minibuffer-local-map [(meta shift up)]
  14175. (lambda () (interactive)
  14176. (org-eval-in-calendar '(calendar-backward-year 1))))
  14177. (org-defkey minibuffer-local-map [(meta shift down)]
  14178. (lambda () (interactive)
  14179. (org-eval-in-calendar '(calendar-forward-year 1))))
  14180. (org-defkey minibuffer-local-map [?\e (shift left)]
  14181. (lambda () (interactive)
  14182. (org-eval-in-calendar '(calendar-backward-month 1))))
  14183. (org-defkey minibuffer-local-map [?\e (shift right)]
  14184. (lambda () (interactive)
  14185. (org-eval-in-calendar '(calendar-forward-month 1))))
  14186. (org-defkey minibuffer-local-map [?\e (shift up)]
  14187. (lambda () (interactive)
  14188. (org-eval-in-calendar '(calendar-backward-year 1))))
  14189. (org-defkey minibuffer-local-map [?\e (shift down)]
  14190. (lambda () (interactive)
  14191. (org-eval-in-calendar '(calendar-forward-year 1))))
  14192. (org-defkey minibuffer-local-map [(shift up)]
  14193. (lambda () (interactive)
  14194. (org-eval-in-calendar '(calendar-backward-week 1))))
  14195. (org-defkey minibuffer-local-map [(shift down)]
  14196. (lambda () (interactive)
  14197. (org-eval-in-calendar '(calendar-forward-week 1))))
  14198. (org-defkey minibuffer-local-map [(shift left)]
  14199. (lambda () (interactive)
  14200. (org-eval-in-calendar '(calendar-backward-day 1))))
  14201. (org-defkey minibuffer-local-map [(shift right)]
  14202. (lambda () (interactive)
  14203. (org-eval-in-calendar '(calendar-forward-day 1))))
  14204. (org-defkey minibuffer-local-map ">"
  14205. (lambda () (interactive)
  14206. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14207. (org-defkey minibuffer-local-map "<"
  14208. (lambda () (interactive)
  14209. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14210. (org-defkey minibuffer-local-map "\C-v"
  14211. (lambda () (interactive)
  14212. (org-eval-in-calendar
  14213. '(calendar-scroll-left-three-months 1))))
  14214. (org-defkey minibuffer-local-map "\M-v"
  14215. (lambda () (interactive)
  14216. (org-eval-in-calendar
  14217. '(calendar-scroll-right-three-months 1))))
  14218. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14219. (unwind-protect
  14220. (progn
  14221. (use-local-map map)
  14222. (setq org-read-date-inactive inactive)
  14223. (add-hook 'post-command-hook 'org-read-date-display)
  14224. (setq org-ans0 (read-string prompt default-input
  14225. 'org-read-date-history nil))
  14226. ;; org-ans0: from prompt
  14227. ;; org-ans1: from mouse click
  14228. ;; org-ans2: from calendar motion
  14229. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14230. (remove-hook 'post-command-hook 'org-read-date-display)
  14231. (use-local-map old-map)
  14232. (when org-read-date-overlay
  14233. (delete-overlay org-read-date-overlay)
  14234. (setq org-read-date-overlay nil)))))
  14235. (bury-buffer "*Calendar*")))))
  14236. (t ; Naked prompt only
  14237. (unwind-protect
  14238. (setq ans (read-string prompt default-input
  14239. 'org-read-date-history timestr))
  14240. (when org-read-date-overlay
  14241. (delete-overlay org-read-date-overlay)
  14242. (setq org-read-date-overlay nil)))))
  14243. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14244. (when org-read-date-analyze-forced-year
  14245. (message "Year was forced into %s"
  14246. (if org-read-date-force-compatible-dates
  14247. "compatible range (1970-2037)"
  14248. "range representable on this machine"))
  14249. (ding))
  14250. ;; One round trip to get rid of 34th of August and stuff like that....
  14251. (setq final (decode-time (apply 'encode-time final)))
  14252. (setq org-read-date-final-answer ans)
  14253. (if to-time
  14254. (apply 'encode-time final)
  14255. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14256. (format "%04d-%02d-%02d %02d:%02d"
  14257. (nth 5 final) (nth 4 final) (nth 3 final)
  14258. (nth 2 final) (nth 1 final))
  14259. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14260. (defvar org-def)
  14261. (defvar org-defdecode)
  14262. (defvar org-with-time)
  14263. (defun org-read-date-display ()
  14264. "Display the current date prompt interpretation in the minibuffer."
  14265. (when org-read-date-display-live
  14266. (when org-read-date-overlay
  14267. (delete-overlay org-read-date-overlay))
  14268. (when (minibufferp (current-buffer))
  14269. (save-excursion
  14270. (end-of-line 1)
  14271. (while (not (equal (buffer-substring
  14272. (max (point-min) (- (point) 4)) (point))
  14273. " "))
  14274. (insert " ")))
  14275. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14276. " " (or org-ans1 org-ans2)))
  14277. (org-end-time-was-given nil)
  14278. (f (org-read-date-analyze ans org-def org-defdecode))
  14279. (fmts (if org-dcst
  14280. org-time-stamp-custom-formats
  14281. org-time-stamp-formats))
  14282. (fmt (if (or org-with-time
  14283. (and (boundp 'org-time-was-given) org-time-was-given))
  14284. (cdr fmts)
  14285. (car fmts)))
  14286. (txt (format-time-string fmt (apply 'encode-time f)))
  14287. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14288. (txt (concat "=> " txt)))
  14289. (when (and org-end-time-was-given
  14290. (string-match org-plain-time-of-day-regexp txt))
  14291. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14292. org-end-time-was-given
  14293. (substring txt (match-end 0)))))
  14294. (when org-read-date-analyze-futurep
  14295. (setq txt (concat txt " (=>F)")))
  14296. (setq org-read-date-overlay
  14297. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14298. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14299. (defun org-read-date-analyze (ans org-def org-defdecode)
  14300. "Analyze the combined answer of the date prompt."
  14301. ;; FIXME: cleanup and comment
  14302. (let ((nowdecode (decode-time (current-time)))
  14303. delta deltan deltaw deltadef year month day
  14304. hour minute second wday pm h2 m2 tl wday1
  14305. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14306. (setq org-read-date-analyze-futurep nil
  14307. org-read-date-analyze-forced-year nil)
  14308. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14309. (setq ans "+0"))
  14310. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14311. (setq ans (replace-match "" t t ans)
  14312. deltan (car delta)
  14313. deltaw (nth 1 delta)
  14314. deltadef (nth 2 delta)))
  14315. ;; Check if there is an iso week date in there. If yes, store the
  14316. ;; info and postpone interpreting it until the rest of the parsing
  14317. ;; is done.
  14318. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14319. (setq iso-year (if (match-end 1)
  14320. (org-small-year-to-year
  14321. (string-to-number (match-string 1 ans))))
  14322. iso-weekday (if (match-end 3)
  14323. (string-to-number (match-string 3 ans)))
  14324. iso-week (string-to-number (match-string 2 ans)))
  14325. (setq ans (replace-match "" t t ans)))
  14326. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14327. (when (string-match
  14328. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14329. (setq year (if (match-end 2)
  14330. (string-to-number (match-string 2 ans))
  14331. (progn (setq kill-year t)
  14332. (string-to-number (format-time-string "%Y"))))
  14333. month (string-to-number (match-string 3 ans))
  14334. day (string-to-number (match-string 4 ans)))
  14335. (if (< year 100) (setq year (+ 2000 year)))
  14336. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14337. t nil ans)))
  14338. ;; Help matching dotted european dates
  14339. (when (string-match
  14340. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  14341. (setq year (if (match-end 3)
  14342. (string-to-number (match-string 3 ans))
  14343. (progn (setq kill-year t)
  14344. (string-to-number (format-time-string "%Y"))))
  14345. day (string-to-number (match-string 1 ans))
  14346. month (string-to-number (match-string 2 ans))
  14347. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14348. t nil ans)))
  14349. ;; Help matching american dates, like 5/30 or 5/30/7
  14350. (when (string-match
  14351. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14352. (setq year (if (match-end 4)
  14353. (string-to-number (match-string 4 ans))
  14354. (progn (setq kill-year t)
  14355. (string-to-number (format-time-string "%Y"))))
  14356. month (string-to-number (match-string 1 ans))
  14357. day (string-to-number (match-string 2 ans)))
  14358. (if (< year 100) (setq year (+ 2000 year)))
  14359. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14360. t nil ans)))
  14361. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14362. ;; If there is a time with am/pm, and *no* time without it, we convert
  14363. ;; so that matching will be successful.
  14364. (loop for i from 1 to 2 do ; twice, for end time as well
  14365. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14366. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14367. (setq hour (string-to-number (match-string 1 ans))
  14368. minute (if (match-end 3)
  14369. (string-to-number (match-string 3 ans))
  14370. 0)
  14371. pm (equal ?p
  14372. (string-to-char (downcase (match-string 4 ans)))))
  14373. (if (and (= hour 12) (not pm))
  14374. (setq hour 0)
  14375. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14376. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14377. t t ans))))
  14378. ;; Check if a time range is given as a duration
  14379. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14380. (setq hour (string-to-number (match-string 1 ans))
  14381. h2 (+ hour (string-to-number (match-string 3 ans)))
  14382. minute (string-to-number (match-string 2 ans))
  14383. m2 (+ minute (if (match-end 5) (string-to-number
  14384. (match-string 5 ans))0)))
  14385. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14386. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14387. t t ans)))
  14388. ;; Check if there is a time range
  14389. (when (boundp 'org-end-time-was-given)
  14390. (setq org-time-was-given nil)
  14391. (when (and (string-match org-plain-time-of-day-regexp ans)
  14392. (match-end 8))
  14393. (setq org-end-time-was-given (match-string 8 ans))
  14394. (setq ans (concat (substring ans 0 (match-beginning 7))
  14395. (substring ans (match-end 7))))))
  14396. (setq tl (parse-time-string ans)
  14397. day (or (nth 3 tl) (nth 3 org-defdecode))
  14398. month (or (nth 4 tl)
  14399. (if (and org-read-date-prefer-future
  14400. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14401. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14402. (nth 4 org-defdecode)))
  14403. year (or (and (not kill-year) (nth 5 tl))
  14404. (if (and org-read-date-prefer-future
  14405. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14406. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14407. (nth 5 org-defdecode)))
  14408. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14409. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14410. second (or (nth 0 tl) 0)
  14411. wday (nth 6 tl))
  14412. (when (and (eq org-read-date-prefer-future 'time)
  14413. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14414. (equal day (nth 3 nowdecode))
  14415. (equal month (nth 4 nowdecode))
  14416. (equal year (nth 5 nowdecode))
  14417. (nth 2 tl)
  14418. (or (< (nth 2 tl) (nth 2 nowdecode))
  14419. (and (= (nth 2 tl) (nth 2 nowdecode))
  14420. (nth 1 tl)
  14421. (< (nth 1 tl) (nth 1 nowdecode)))))
  14422. (setq day (1+ day)
  14423. futurep t))
  14424. ;; Special date definitions below
  14425. (cond
  14426. (iso-week
  14427. ;; There was an iso week
  14428. (require 'cal-iso)
  14429. (setq futurep nil)
  14430. (setq year (or iso-year year)
  14431. day (or iso-weekday wday 1)
  14432. wday nil ; to make sure that the trigger below does not match
  14433. iso-date (calendar-gregorian-from-absolute
  14434. (calendar-absolute-from-iso
  14435. (list iso-week day year))))
  14436. ; FIXME: Should we also push ISO weeks into the future?
  14437. ; (when (and org-read-date-prefer-future
  14438. ; (not iso-year)
  14439. ; (< (calendar-absolute-from-gregorian iso-date)
  14440. ; (time-to-days (current-time))))
  14441. ; (setq year (1+ year)
  14442. ; iso-date (calendar-gregorian-from-absolute
  14443. ; (calendar-absolute-from-iso
  14444. ; (list iso-week day year)))))
  14445. (setq month (car iso-date)
  14446. year (nth 2 iso-date)
  14447. day (nth 1 iso-date)))
  14448. (deltan
  14449. (setq futurep nil)
  14450. (unless deltadef
  14451. (let ((now (decode-time (current-time))))
  14452. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14453. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14454. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14455. ((equal deltaw "m") (setq month (+ month deltan)))
  14456. ((equal deltaw "y") (setq year (+ year deltan)))))
  14457. ((and wday (not (nth 3 tl)))
  14458. ;; Weekday was given, but no day, so pick that day in the week
  14459. ;; on or after the derived date.
  14460. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14461. (unless (equal wday wday1)
  14462. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14463. (if (and (boundp 'org-time-was-given)
  14464. (nth 2 tl))
  14465. (setq org-time-was-given t))
  14466. (if (< year 100) (setq year (+ 2000 year)))
  14467. ;; Check of the date is representable
  14468. (if org-read-date-force-compatible-dates
  14469. (progn
  14470. (if (< year 1970)
  14471. (setq year 1970 org-read-date-analyze-forced-year t))
  14472. (if (> year 2037)
  14473. (setq year 2037 org-read-date-analyze-forced-year t)))
  14474. (condition-case nil
  14475. (ignore (encode-time second minute hour day month year))
  14476. (error
  14477. (setq year (nth 5 org-defdecode))
  14478. (setq org-read-date-analyze-forced-year t))))
  14479. (setq org-read-date-analyze-futurep futurep)
  14480. (list second minute hour day month year)))
  14481. (defvar parse-time-weekdays)
  14482. (defun org-read-date-get-relative (s today default)
  14483. "Check string S for special relative date string.
  14484. TODAY and DEFAULT are internal times, for today and for a default.
  14485. Return shift list (N what def-flag)
  14486. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14487. N is the number of WHATs to shift.
  14488. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14489. the DEFAULT date rather than TODAY."
  14490. (require 'parse-time)
  14491. (when (and
  14492. (string-match
  14493. (concat
  14494. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14495. "\\([0-9]+\\)?"
  14496. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14497. "\\([ \t]\\|$\\)") s)
  14498. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14499. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14500. (string-to-char (substring (match-string 1 s) -1))
  14501. ?+))
  14502. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14503. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14504. (what (if (match-end 3) (match-string 3 s) "d"))
  14505. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14506. (date (if rel default today))
  14507. (wday (nth 6 (decode-time date)))
  14508. delta)
  14509. (if wday1
  14510. (progn
  14511. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14512. (if (= dir ?-) (setq delta (- delta 7)))
  14513. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14514. (list delta "d" rel))
  14515. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14516. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14517. "Turn a user-specified date into the internal representation.
  14518. The internal representation needed by the calendar is (month day year).
  14519. This is a wrapper to handle the brain-dead convention in calendar that
  14520. user function argument order change dependent on argument order."
  14521. (if (boundp 'calendar-date-style)
  14522. (cond
  14523. ((eq calendar-date-style 'american)
  14524. (list arg1 arg2 arg3))
  14525. ((eq calendar-date-style 'european)
  14526. (list arg2 arg1 arg3))
  14527. ((eq calendar-date-style 'iso)
  14528. (list arg2 arg3 arg1)))
  14529. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14530. (if (org-bound-and-true-p european-calendar-style)
  14531. (list arg2 arg1 arg3)
  14532. (list arg1 arg2 arg3)))))
  14533. (defun org-eval-in-calendar (form &optional keepdate)
  14534. "Eval FORM in the calendar window and return to current window.
  14535. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14536. otherwise stick to the current value of `org-ans2'."
  14537. (let ((sf (selected-frame))
  14538. (sw (selected-window)))
  14539. (select-window (get-buffer-window "*Calendar*" t))
  14540. (eval form)
  14541. (when (and (not keepdate) (calendar-cursor-to-date))
  14542. (let* ((date (calendar-cursor-to-date))
  14543. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14544. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14545. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14546. (select-window sw)
  14547. (org-select-frame-set-input-focus sf)))
  14548. (defun org-calendar-select ()
  14549. "Return to `org-read-date' with the date currently selected.
  14550. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14551. (interactive)
  14552. (when (calendar-cursor-to-date)
  14553. (let* ((date (calendar-cursor-to-date))
  14554. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14555. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14556. (if (active-minibuffer-window) (exit-minibuffer))))
  14557. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14558. "Insert a date stamp for the date given by the internal TIME.
  14559. WITH-HM means use the stamp format that includes the time of the day.
  14560. INACTIVE means use square brackets instead of angular ones, so that the
  14561. stamp will not contribute to the agenda.
  14562. PRE and POST are optional strings to be inserted before and after the
  14563. stamp.
  14564. The command returns the inserted time stamp."
  14565. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14566. stamp)
  14567. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14568. (insert-before-markers (or pre ""))
  14569. (when (listp extra)
  14570. (setq extra (car extra))
  14571. (if (and (stringp extra)
  14572. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14573. (setq extra (format "-%02d:%02d"
  14574. (string-to-number (match-string 1 extra))
  14575. (string-to-number (match-string 2 extra))))
  14576. (setq extra nil)))
  14577. (when extra
  14578. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14579. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14580. (insert-before-markers (or post ""))
  14581. (setq org-last-inserted-timestamp stamp)))
  14582. (defun org-toggle-time-stamp-overlays ()
  14583. "Toggle the use of custom time stamp formats."
  14584. (interactive)
  14585. (setq org-display-custom-times (not org-display-custom-times))
  14586. (unless org-display-custom-times
  14587. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14588. (while (setq p (next-single-property-change p 'display))
  14589. (if (and (get-text-property p 'display)
  14590. (eq (get-text-property p 'face) 'org-date))
  14591. (remove-text-properties
  14592. p (setq p (next-single-property-change p 'display))
  14593. '(display t))))
  14594. (set-buffer-modified-p bmp)))
  14595. (if (featurep 'xemacs)
  14596. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14597. (org-restart-font-lock)
  14598. (setq org-table-may-need-update t)
  14599. (if org-display-custom-times
  14600. (message "Time stamps are overlaid with custom format")
  14601. (message "Time stamp overlays removed")))
  14602. (defun org-display-custom-time (beg end)
  14603. "Overlay modified time stamp format over timestamp between BEG and END."
  14604. (let* ((ts (buffer-substring beg end))
  14605. t1 w1 with-hm tf time str w2 (off 0))
  14606. (save-match-data
  14607. (setq t1 (org-parse-time-string ts t))
  14608. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14609. (setq off (- (match-end 0) (match-beginning 0)))))
  14610. (setq end (- end off))
  14611. (setq w1 (- end beg)
  14612. with-hm (and (nth 1 t1) (nth 2 t1))
  14613. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14614. time (org-fix-decoded-time t1)
  14615. str (org-add-props
  14616. (format-time-string
  14617. (substring tf 1 -1) (apply 'encode-time time))
  14618. nil 'mouse-face 'highlight)
  14619. w2 (length str))
  14620. (if (not (= w2 w1))
  14621. (add-text-properties (1+ beg) (+ 2 beg)
  14622. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14623. (if (featurep 'xemacs)
  14624. (progn
  14625. (put-text-property beg end 'invisible t)
  14626. (put-text-property beg end 'end-glyph (make-glyph str)))
  14627. (put-text-property beg end 'display str))))
  14628. (defun org-translate-time (string)
  14629. "Translate all timestamps in STRING to custom format.
  14630. But do this only if the variable `org-display-custom-times' is set."
  14631. (when org-display-custom-times
  14632. (save-match-data
  14633. (let* ((start 0)
  14634. (re org-ts-regexp-both)
  14635. t1 with-hm inactive tf time str beg end)
  14636. (while (setq start (string-match re string start))
  14637. (setq beg (match-beginning 0)
  14638. end (match-end 0)
  14639. t1 (save-match-data
  14640. (org-parse-time-string (substring string beg end) t))
  14641. with-hm (and (nth 1 t1) (nth 2 t1))
  14642. inactive (equal (substring string beg (1+ beg)) "[")
  14643. tf (funcall (if with-hm 'cdr 'car)
  14644. org-time-stamp-custom-formats)
  14645. time (org-fix-decoded-time t1)
  14646. str (format-time-string
  14647. (concat
  14648. (if inactive "[" "<") (substring tf 1 -1)
  14649. (if inactive "]" ">"))
  14650. (apply 'encode-time time))
  14651. string (replace-match str t t string)
  14652. start (+ start (length str)))))))
  14653. string)
  14654. (defun org-fix-decoded-time (time)
  14655. "Set 0 instead of nil for the first 6 elements of time.
  14656. Don't touch the rest."
  14657. (let ((n 0))
  14658. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14659. (defun org-days-to-time (timestamp-string)
  14660. "Difference between TIMESTAMP-STRING and now in days."
  14661. (- (time-to-days (org-time-string-to-time timestamp-string))
  14662. (time-to-days (current-time))))
  14663. (defun org-deadline-close (timestamp-string &optional ndays)
  14664. "Is the time in TIMESTAMP-STRING close to the current date?"
  14665. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14666. (and (< (org-days-to-time timestamp-string) ndays)
  14667. (not (org-entry-is-done-p))))
  14668. (defun org-get-wdays (ts)
  14669. "Get the deadline lead time appropriate for timestring TS."
  14670. (cond
  14671. ((<= org-deadline-warning-days 0)
  14672. ;; 0 or negative, enforce this value no matter what
  14673. (- org-deadline-warning-days))
  14674. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14675. ;; lead time is specified.
  14676. (floor (* (string-to-number (match-string 1 ts))
  14677. (cdr (assoc (match-string 2 ts)
  14678. '(("d" . 1) ("w" . 7)
  14679. ("m" . 30.4) ("y" . 365.25)))))))
  14680. ;; go for the default.
  14681. (t org-deadline-warning-days)))
  14682. (defun org-calendar-select-mouse (ev)
  14683. "Return to `org-read-date' with the date currently selected.
  14684. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14685. (interactive "e")
  14686. (mouse-set-point ev)
  14687. (when (calendar-cursor-to-date)
  14688. (let* ((date (calendar-cursor-to-date))
  14689. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14690. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14691. (if (active-minibuffer-window) (exit-minibuffer))))
  14692. (defun org-check-deadlines (ndays)
  14693. "Check if there are any deadlines due or past due.
  14694. A deadline is considered due if it happens within `org-deadline-warning-days'
  14695. days from today's date. If the deadline appears in an entry marked DONE,
  14696. it is not shown. The prefix arg NDAYS can be used to test that many
  14697. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14698. (interactive "P")
  14699. (let* ((org-warn-days
  14700. (cond
  14701. ((equal ndays '(4)) 100000)
  14702. (ndays (prefix-numeric-value ndays))
  14703. (t (abs org-deadline-warning-days))))
  14704. (case-fold-search nil)
  14705. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14706. (callback
  14707. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14708. (message "%d deadlines past-due or due within %d days"
  14709. (org-occur regexp nil callback)
  14710. org-warn-days)))
  14711. (defsubst org-re-timestamp (type)
  14712. "Return a regexp for timestamp TYPE.
  14713. Allowed values for TYPE are:
  14714. all: all timestamps
  14715. active: only active timestamps (<...>)
  14716. inactive: only inactive timestamps ([...])
  14717. scheduled: only scheduled timestamps
  14718. deadline: only deadline timestamps
  14719. When TYPE is nil, fall back on returning a regexp that matches
  14720. both scheduled and deadline timestamps."
  14721. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14722. ((eq type 'active) org-ts-regexp)
  14723. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14724. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14725. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14726. ((eq type 'scheduled-or-deadline)
  14727. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14728. (defun org-check-before-date (date)
  14729. "Check if there are deadlines or scheduled entries before DATE."
  14730. (interactive (list (org-read-date)))
  14731. (let ((case-fold-search nil)
  14732. (regexp (org-re-timestamp org-ts-type))
  14733. (callback
  14734. (lambda () (time-less-p
  14735. (org-time-string-to-time (match-string 1))
  14736. (org-time-string-to-time date)))))
  14737. (message "%d entries before %s"
  14738. (org-occur regexp nil callback) date)))
  14739. (defun org-check-after-date (date)
  14740. "Check if there are deadlines or scheduled entries after DATE."
  14741. (interactive (list (org-read-date)))
  14742. (let ((case-fold-search nil)
  14743. (regexp (org-re-timestamp org-ts-type))
  14744. (callback
  14745. (lambda () (not
  14746. (time-less-p
  14747. (org-time-string-to-time (match-string 1))
  14748. (org-time-string-to-time date))))))
  14749. (message "%d entries after %s"
  14750. (org-occur regexp nil callback) date)))
  14751. (defun org-check-dates-range (start-date end-date)
  14752. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14753. (interactive (list (org-read-date nil nil nil "Range starts")
  14754. (org-read-date nil nil nil "Range end")))
  14755. (let ((case-fold-search nil)
  14756. (regexp (org-re-timestamp org-ts-type))
  14757. (callback
  14758. (lambda ()
  14759. (let ((match (match-string 1)))
  14760. (and
  14761. (not (time-less-p
  14762. (org-time-string-to-time match)
  14763. (org-time-string-to-time start-date)))
  14764. (time-less-p
  14765. (org-time-string-to-time match)
  14766. (org-time-string-to-time end-date)))))))
  14767. (message "%d entries between %s and %s"
  14768. (org-occur regexp nil callback) start-date end-date)))
  14769. (defun org-evaluate-time-range (&optional to-buffer)
  14770. "Evaluate a time range by computing the difference between start and end.
  14771. Normally the result is just printed in the echo area, but with prefix arg
  14772. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14773. If the time range is actually in a table, the result is inserted into the
  14774. next column.
  14775. For time difference computation, a year is assumed to be exactly 365
  14776. days in order to avoid rounding problems."
  14777. (interactive "P")
  14778. (or
  14779. (org-clock-update-time-maybe)
  14780. (save-excursion
  14781. (unless (org-at-date-range-p t)
  14782. (goto-char (point-at-bol))
  14783. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14784. (if (not (org-at-date-range-p t))
  14785. (error "Not at a time-stamp range, and none found in current line")))
  14786. (let* ((ts1 (match-string 1))
  14787. (ts2 (match-string 2))
  14788. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14789. (match-end (match-end 0))
  14790. (time1 (org-time-string-to-time ts1))
  14791. (time2 (org-time-string-to-time ts2))
  14792. (t1 (org-float-time time1))
  14793. (t2 (org-float-time time2))
  14794. (diff (abs (- t2 t1)))
  14795. (negative (< (- t2 t1) 0))
  14796. ;; (ys (floor (* 365 24 60 60)))
  14797. (ds (* 24 60 60))
  14798. (hs (* 60 60))
  14799. (fy "%dy %dd %02d:%02d")
  14800. (fy1 "%dy %dd")
  14801. (fd "%dd %02d:%02d")
  14802. (fd1 "%dd")
  14803. (fh "%02d:%02d")
  14804. y d h m align)
  14805. (if havetime
  14806. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14807. y 0
  14808. d (floor (/ diff ds)) diff (mod diff ds)
  14809. h (floor (/ diff hs)) diff (mod diff hs)
  14810. m (floor (/ diff 60)))
  14811. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14812. y 0
  14813. d (floor (+ (/ diff ds) 0.5))
  14814. h 0 m 0))
  14815. (if (not to-buffer)
  14816. (message "%s" (org-make-tdiff-string y d h m))
  14817. (if (org-at-table-p)
  14818. (progn
  14819. (goto-char match-end)
  14820. (setq align t)
  14821. (and (looking-at " *|") (goto-char (match-end 0))))
  14822. (goto-char match-end))
  14823. (if (looking-at
  14824. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14825. (replace-match ""))
  14826. (if negative (insert " -"))
  14827. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14828. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14829. (insert " " (format fh h m))))
  14830. (if align (org-table-align))
  14831. (message "Time difference inserted")))))
  14832. (defun org-make-tdiff-string (y d h m)
  14833. (let ((fmt "")
  14834. (l nil))
  14835. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14836. l (push y l)))
  14837. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14838. l (push d l)))
  14839. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14840. l (push h l)))
  14841. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14842. l (push m l)))
  14843. (apply 'format fmt (nreverse l))))
  14844. (defun org-time-string-to-time (s &optional buffer pos)
  14845. "Convert a timestamp string into internal time."
  14846. (condition-case errdata
  14847. (apply 'encode-time (org-parse-time-string s))
  14848. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14849. s (if (not (and buffer pos))
  14850. ""
  14851. (format " at %d in buffer `%s'" pos buffer))
  14852. (cdr errdata)))))
  14853. (defun org-time-string-to-seconds (s)
  14854. "Convert a timestamp string to a number of seconds."
  14855. (org-float-time (org-time-string-to-time s)))
  14856. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14857. "Convert a time stamp to an absolute day number.
  14858. If there is a specifier for a cyclic time stamp, get the closest date to
  14859. DAYNR.
  14860. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14861. The variable date is bound by the calendar when this is called."
  14862. (cond
  14863. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14864. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14865. daynr
  14866. (+ daynr 1000)))
  14867. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14868. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14869. (time-to-days (current-time))) (match-string 0 s)
  14870. prefer show-all))
  14871. (t (time-to-days
  14872. (condition-case errdata
  14873. (apply 'encode-time (org-parse-time-string s))
  14874. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14875. s (if (not (and buffer pos))
  14876. ""
  14877. (format " at %d in buffer `%s'" pos buffer))
  14878. (cdr errdata))))))))
  14879. (defun org-days-to-iso-week (days)
  14880. "Return the iso week number."
  14881. (require 'cal-iso)
  14882. (car (calendar-iso-from-absolute days)))
  14883. (defun org-small-year-to-year (year)
  14884. "Convert 2-digit years into 4-digit years.
  14885. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14886. The year 2000 cannot be abbreviated. Any year larger than 99
  14887. is returned unchanged."
  14888. (if (< year 38)
  14889. (setq year (+ 2000 year))
  14890. (if (< year 100)
  14891. (setq year (+ 1900 year))))
  14892. year)
  14893. (defun org-time-from-absolute (d)
  14894. "Return the time corresponding to date D.
  14895. D may be an absolute day number, or a calendar-type list (month day year)."
  14896. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14897. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14898. (defun org-calendar-holiday ()
  14899. "List of holidays, for Diary display in Org-mode."
  14900. (require 'holidays)
  14901. (let ((hl (funcall
  14902. (if (fboundp 'calendar-check-holidays)
  14903. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14904. (if hl (mapconcat 'identity hl "; "))))
  14905. (defun org-diary-sexp-entry (sexp entry date)
  14906. "Process a SEXP diary ENTRY for DATE."
  14907. (require 'diary-lib)
  14908. (let ((result (if calendar-debug-sexp
  14909. (let ((stack-trace-on-error t))
  14910. (eval (car (read-from-string sexp))))
  14911. (condition-case nil
  14912. (eval (car (read-from-string sexp)))
  14913. (error
  14914. (beep)
  14915. (message "Bad sexp at line %d in %s: %s"
  14916. (org-current-line)
  14917. (buffer-file-name) sexp)
  14918. (sleep-for 2))))))
  14919. (cond ((stringp result) (split-string result "; "))
  14920. ((and (consp result)
  14921. (not (consp (cdr result)))
  14922. (stringp (cdr result))) (cdr result))
  14923. ((and (consp result)
  14924. (stringp (car result))) result)
  14925. (result entry))))
  14926. (defun org-diary-to-ical-string (frombuf)
  14927. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14928. This uses the icalendar.el library."
  14929. (let* ((tmpdir (if (featurep 'xemacs)
  14930. (temp-directory)
  14931. temporary-file-directory))
  14932. (tmpfile (make-temp-name
  14933. (expand-file-name "orgics" tmpdir)))
  14934. buf rtn b e)
  14935. (with-current-buffer frombuf
  14936. (icalendar-export-region (point-min) (point-max) tmpfile)
  14937. (setq buf (find-buffer-visiting tmpfile))
  14938. (set-buffer buf)
  14939. (goto-char (point-min))
  14940. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14941. (setq b (match-beginning 0)))
  14942. (goto-char (point-max))
  14943. (if (re-search-backward "^END:VEVENT" nil t)
  14944. (setq e (match-end 0)))
  14945. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14946. (kill-buffer buf)
  14947. (delete-file tmpfile)
  14948. rtn))
  14949. (defun org-closest-date (start current change prefer show-all)
  14950. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14951. When PREFER is `past', return a date that is either CURRENT or past.
  14952. When PREFER is `future', return a date that is either CURRENT or future.
  14953. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14954. ;; Make the proper lists from the dates
  14955. (catch 'exit
  14956. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14957. dn dw sday cday n1 n2 n0
  14958. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14959. (setq start (org-date-to-gregorian start)
  14960. current (org-date-to-gregorian
  14961. (if show-all
  14962. current
  14963. (time-to-days (current-time))))
  14964. sday (calendar-absolute-from-gregorian start)
  14965. cday (calendar-absolute-from-gregorian current))
  14966. (if (<= cday sday) (throw 'exit sday))
  14967. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14968. (setq dn (string-to-number (match-string 1 change))
  14969. dw (cdr (assoc (match-string 2 change) a1)))
  14970. (error "Invalid change specifier: %s" change))
  14971. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14972. (cond
  14973. ((eq dw 'day)
  14974. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14975. n2 (+ n1 dn)))
  14976. ((eq dw 'year)
  14977. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14978. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14979. (setq date1 (list m d y1)
  14980. n1 (calendar-absolute-from-gregorian date1)
  14981. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14982. n2 (calendar-absolute-from-gregorian date2)))
  14983. ((eq dw 'month)
  14984. ;; approx number of month between the two dates
  14985. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14986. ;; How often does dn fit in there?
  14987. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14988. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14989. m (+ m nm)
  14990. ny (floor (/ m 12))
  14991. y (+ y ny)
  14992. m (- m (* ny 12)))
  14993. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14994. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14995. (setq m2 (+ m dn) y2 y)
  14996. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14997. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14998. (while (<= n2 cday)
  14999. (setq n1 n2 m m2 y y2)
  15000. (setq m2 (+ m dn) y2 y)
  15001. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15002. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15003. ;; Make sure n1 is the earlier date
  15004. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15005. (if show-all
  15006. (cond
  15007. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15008. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15009. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15010. (cond
  15011. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15012. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15013. (t (if (= cday n1) n1 n2)))))))
  15014. (defun org-date-to-gregorian (date)
  15015. "Turn any specification of DATE into a Gregorian date for the calendar."
  15016. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15017. ((and (listp date) (= (length date) 3)) date)
  15018. ((stringp date)
  15019. (setq date (org-parse-time-string date))
  15020. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15021. ((listp date)
  15022. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15023. (defun org-parse-time-string (s &optional nodefault)
  15024. "Parse the standard Org-mode time string.
  15025. This should be a lot faster than the normal `parse-time-string'.
  15026. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15027. hour and minute fields will be nil if not given."
  15028. (if (string-match org-ts-regexp0 s)
  15029. (list 0
  15030. (if (or (match-beginning 8) (not nodefault))
  15031. (string-to-number (or (match-string 8 s) "0")))
  15032. (if (or (match-beginning 7) (not nodefault))
  15033. (string-to-number (or (match-string 7 s) "0")))
  15034. (string-to-number (match-string 4 s))
  15035. (string-to-number (match-string 3 s))
  15036. (string-to-number (match-string 2 s))
  15037. nil nil nil)
  15038. (error "Not a standard Org-mode time string: %s" s)))
  15039. (defun org-timestamp-up (&optional arg)
  15040. "Increase the date item at the cursor by one.
  15041. If the cursor is on the year, change the year. If it is on the month,
  15042. the day or the time, change that.
  15043. With prefix ARG, change by that many units."
  15044. (interactive "p")
  15045. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15046. (defun org-timestamp-down (&optional arg)
  15047. "Decrease the date item at the cursor by one.
  15048. If the cursor is on the year, change the year. If it is on the month,
  15049. the day or the time, change that.
  15050. With prefix ARG, change by that many units."
  15051. (interactive "p")
  15052. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15053. (defun org-timestamp-up-day (&optional arg)
  15054. "Increase the date in the time stamp by one day.
  15055. With prefix ARG, change that many days."
  15056. (interactive "p")
  15057. (if (and (not (org-at-timestamp-p t))
  15058. (org-at-heading-p))
  15059. (org-todo 'up)
  15060. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15061. (defun org-timestamp-down-day (&optional arg)
  15062. "Decrease the date in the time stamp by one day.
  15063. With prefix ARG, change that many days."
  15064. (interactive "p")
  15065. (if (and (not (org-at-timestamp-p t))
  15066. (org-at-heading-p))
  15067. (org-todo 'down)
  15068. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15069. (defun org-at-timestamp-p (&optional inactive-ok)
  15070. "Determine if the cursor is in or at a timestamp."
  15071. (interactive)
  15072. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15073. (pos (point))
  15074. (ans (or (looking-at tsr)
  15075. (save-excursion
  15076. (skip-chars-backward "^[<\n\r\t")
  15077. (if (> (point) (point-min)) (backward-char 1))
  15078. (and (looking-at tsr)
  15079. (> (- (match-end 0) pos) -1))))))
  15080. (and ans
  15081. (boundp 'org-ts-what)
  15082. (setq org-ts-what
  15083. (cond
  15084. ((= pos (match-beginning 0)) 'bracket)
  15085. ;; Point is considered to be "on the bracket" whether
  15086. ;; it's really on it or right after it.
  15087. ((= pos (1- (match-end 0))) 'bracket)
  15088. ((= pos (match-end 0)) 'after)
  15089. ((org-pos-in-match-range pos 2) 'year)
  15090. ((org-pos-in-match-range pos 3) 'month)
  15091. ((org-pos-in-match-range pos 7) 'hour)
  15092. ((org-pos-in-match-range pos 8) 'minute)
  15093. ((or (org-pos-in-match-range pos 4)
  15094. (org-pos-in-match-range pos 5)) 'day)
  15095. ((and (> pos (or (match-end 8) (match-end 5)))
  15096. (< pos (match-end 0)))
  15097. (- pos (or (match-end 8) (match-end 5))))
  15098. (t 'day))))
  15099. ans))
  15100. (defun org-toggle-timestamp-type ()
  15101. "Toggle the type (<active> or [inactive]) of a time stamp."
  15102. (interactive)
  15103. (when (org-at-timestamp-p t)
  15104. (let ((beg (match-beginning 0)) (end (match-end 0))
  15105. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15106. (save-excursion
  15107. (goto-char beg)
  15108. (while (re-search-forward "[][<>]" end t)
  15109. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15110. t t)))
  15111. (message "Timestamp is now %sactive"
  15112. (if (equal (char-after beg) ?<) "" "in")))))
  15113. (defvar org-clock-history) ; defined in org-clock.el
  15114. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15115. (defun org-timestamp-change (n &optional what updown)
  15116. "Change the date in the time stamp at point.
  15117. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15118. `year', `minute', `second'. If WHAT is not given, the cursor position
  15119. in the timestamp determines what will be changed."
  15120. (let ((origin (point)) origin-cat
  15121. with-hm inactive
  15122. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15123. org-ts-what
  15124. extra rem
  15125. ts time time0 fixnext clrgx)
  15126. (if (not (org-at-timestamp-p t))
  15127. (error "Not at a timestamp"))
  15128. (if (and (not what) (eq org-ts-what 'bracket))
  15129. (org-toggle-timestamp-type)
  15130. ;; Point isn't on brackets. Remember the part of the time-stamp
  15131. ;; the point was in. Indeed, size of time-stamps may change,
  15132. ;; but point must be kept in the same category nonetheless.
  15133. (setq origin-cat org-ts-what)
  15134. (if (and (not what) (not (eq org-ts-what 'day))
  15135. org-display-custom-times
  15136. (get-text-property (point) 'display)
  15137. (not (get-text-property (1- (point)) 'display)))
  15138. (setq org-ts-what 'day))
  15139. (setq org-ts-what (or what org-ts-what)
  15140. inactive (= (char-after (match-beginning 0)) ?\[)
  15141. ts (match-string 0))
  15142. (replace-match "")
  15143. (if (string-match
  15144. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15145. ts)
  15146. (setq extra (match-string 1 ts)))
  15147. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15148. (setq with-hm t))
  15149. (setq time0 (org-parse-time-string ts))
  15150. (when (and updown
  15151. (eq org-ts-what 'minute)
  15152. (not current-prefix-arg))
  15153. ;; This looks like s-up and s-down. Change by one rounding step.
  15154. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15155. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15156. (setcar (cdr time0) (+ (nth 1 time0)
  15157. (if (> n 0) (- rem) (- dm rem))))))
  15158. (setq time
  15159. (encode-time (or (car time0) 0)
  15160. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15161. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15162. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15163. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15164. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15165. (nthcdr 6 time0)))
  15166. (when (and (member org-ts-what '(hour minute))
  15167. extra
  15168. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15169. (setq extra (org-modify-ts-extra
  15170. extra
  15171. (if (eq org-ts-what 'hour) 2 5)
  15172. n dm)))
  15173. (when (integerp org-ts-what)
  15174. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15175. (if (eq what 'calendar)
  15176. (let ((cal-date (org-get-date-from-calendar)))
  15177. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15178. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15179. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15180. (setcar time0 (or (car time0) 0))
  15181. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15182. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15183. (setq time (apply 'encode-time time0))))
  15184. ;; Insert the new time-stamp, and ensure point stays in the same
  15185. ;; category as before (i.e. not after the last position in that
  15186. ;; category).
  15187. (let ((pos (point)))
  15188. ;; Stay before inserted string. `save-excursion' is of no use.
  15189. (setq org-last-changed-timestamp
  15190. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15191. (goto-char pos))
  15192. (save-match-data
  15193. (looking-at org-ts-regexp3)
  15194. (goto-char (cond
  15195. ;; `day' category ends before `hour' if any, or at
  15196. ;; the end of the day name.
  15197. ((eq origin-cat 'day)
  15198. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15199. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15200. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15201. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15202. ;; `year' and `month' have both fixed size: point
  15203. ;; couldn't have moved into another part.
  15204. (t origin))))
  15205. ;; Update clock if on a CLOCK line.
  15206. (org-clock-update-time-maybe)
  15207. ;; Maybe adjust the closest clock in `org-clock-history'
  15208. (when org-clock-adjust-closest
  15209. (if (not (and (org-at-clock-log-p)
  15210. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15211. org-clock-history))))))
  15212. (message "No clock to adjust")
  15213. (cond ((save-excursion ; fix previous clock?
  15214. (re-search-backward org-ts-regexp0 nil t)
  15215. (org-looking-back (concat org-clock-string " \\[")))
  15216. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15217. ((save-excursion ; fix next clock?
  15218. (re-search-backward org-ts-regexp0 nil t)
  15219. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15220. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15221. (save-window-excursion
  15222. ;; Find closest clock to point, adjust the previous/next one in history
  15223. (let* ((p (save-excursion (org-back-to-heading t)))
  15224. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15225. (clfixnth
  15226. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15227. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15228. (if (not clfixpos)
  15229. (message "No clock to adjust")
  15230. (save-excursion
  15231. (org-goto-marker-or-bmk clfixpos)
  15232. (org-show-subtree)
  15233. (when (re-search-forward clrgx nil t)
  15234. (goto-char (match-beginning 1))
  15235. (let (org-clock-adjust-closest)
  15236. (org-timestamp-change n org-ts-what updown))
  15237. (message "Clock adjusted in %s for heading: %s"
  15238. (file-name-nondirectory (buffer-file-name))
  15239. (org-get-heading t t)))))))))
  15240. ;; Try to recenter the calendar window, if any.
  15241. (if (and org-calendar-follow-timestamp-change
  15242. (get-buffer-window "*Calendar*" t)
  15243. (memq org-ts-what '(day month year)))
  15244. (org-recenter-calendar (time-to-days time))))))
  15245. (defun org-modify-ts-extra (s pos n dm)
  15246. "Change the different parts of the lead-time and repeat fields in timestamp."
  15247. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15248. ng h m new rem)
  15249. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15250. (cond
  15251. ((or (org-pos-in-match-range pos 2)
  15252. (org-pos-in-match-range pos 3))
  15253. (setq m (string-to-number (match-string 3 s))
  15254. h (string-to-number (match-string 2 s)))
  15255. (if (org-pos-in-match-range pos 2)
  15256. (setq h (+ h n))
  15257. (setq n (* dm (org-no-warnings (signum n))))
  15258. (when (not (= 0 (setq rem (% m dm))))
  15259. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15260. (setq m (+ m n)))
  15261. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15262. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15263. (setq h (min 24 (max 0 h)))
  15264. (setq ng 1 new (format "-%02d:%02d" h m)))
  15265. ((org-pos-in-match-range pos 6)
  15266. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15267. ((org-pos-in-match-range pos 5)
  15268. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15269. ((org-pos-in-match-range pos 9)
  15270. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15271. ((org-pos-in-match-range pos 8)
  15272. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15273. (when ng
  15274. (setq s (concat
  15275. (substring s 0 (match-beginning ng))
  15276. new
  15277. (substring s (match-end ng))))))
  15278. s))
  15279. (defun org-recenter-calendar (date)
  15280. "If the calendar is visible, recenter it to DATE."
  15281. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15282. (when cwin
  15283. (let ((calendar-move-hook nil))
  15284. (with-selected-window cwin
  15285. (calendar-goto-date (if (listp date) date
  15286. (calendar-gregorian-from-absolute date))))))))
  15287. (defun org-goto-calendar (&optional arg)
  15288. "Go to the Emacs calendar at the current date.
  15289. If there is a time stamp in the current line, go to that date.
  15290. A prefix ARG can be used to force the current date."
  15291. (interactive "P")
  15292. (let ((tsr org-ts-regexp) diff
  15293. (calendar-move-hook nil)
  15294. (calendar-view-holidays-initially-flag nil)
  15295. (calendar-view-diary-initially-flag nil))
  15296. (if (or (org-at-timestamp-p)
  15297. (save-excursion
  15298. (beginning-of-line 1)
  15299. (looking-at (concat ".*" tsr))))
  15300. (let ((d1 (time-to-days (current-time)))
  15301. (d2 (time-to-days
  15302. (org-time-string-to-time (match-string 1)))))
  15303. (setq diff (- d2 d1))))
  15304. (calendar)
  15305. (calendar-goto-today)
  15306. (if (and diff (not arg)) (calendar-forward-day diff))))
  15307. (defun org-get-date-from-calendar ()
  15308. "Return a list (month day year) of date at point in calendar."
  15309. (with-current-buffer "*Calendar*"
  15310. (save-match-data
  15311. (calendar-cursor-to-date))))
  15312. (defun org-date-from-calendar ()
  15313. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15314. If there is already a time stamp at the cursor position, update it."
  15315. (interactive)
  15316. (if (org-at-timestamp-p t)
  15317. (org-timestamp-change 0 'calendar)
  15318. (let ((cal-date (org-get-date-from-calendar)))
  15319. (org-insert-time-stamp
  15320. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15321. (defun org-minutes-to-hh:mm-string (m)
  15322. "Compute H:MM from a number of minutes."
  15323. (let ((h (/ m 60)))
  15324. (setq m (- m (* 60 h)))
  15325. (format org-time-clocksum-format h m)))
  15326. (defun org-hh:mm-string-to-minutes (s)
  15327. "Convert a string H:MM to a number of minutes.
  15328. If the string is just a number, interpret it as minutes.
  15329. In fact, the first hh:mm or number in the string will be taken,
  15330. there can be extra stuff in the string.
  15331. If no number is found, the return value is 0."
  15332. (cond
  15333. ((integerp s) s)
  15334. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15335. (+ (* (string-to-number (match-string 1 s)) 60)
  15336. (string-to-number (match-string 2 s))))
  15337. ((string-match "\\([0-9]+\\)" s)
  15338. (string-to-number (match-string 1 s)))
  15339. (t 0)))
  15340. (defcustom org-effort-durations
  15341. `(("h" . 60)
  15342. ("d" . ,(* 60 8))
  15343. ("w" . ,(* 60 8 5))
  15344. ("m" . ,(* 60 8 5 4))
  15345. ("y" . ,(* 60 8 5 40)))
  15346. "Conversion factor to minutes for an effort modifier.
  15347. Each entry has the form (MODIFIER . MINUTES).
  15348. In an effort string, a number followed by MODIFIER is multiplied
  15349. by the specified number of MINUTES to obtain an effort in
  15350. minutes.
  15351. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15352. effort string \"2hours\" is equivalent to 120 minutes."
  15353. :group 'org-agenda
  15354. :version "24.1"
  15355. :type '(alist :key-type (string :tag "Modifier")
  15356. :value-type (number :tag "Minutes")))
  15357. (defcustom org-image-actual-width t
  15358. "Should we use the actual width of images when inlining them?
  15359. When set to `t', always use the image width.
  15360. When set to a number, use imagemagick (when available) to set
  15361. the image's width to this value.
  15362. When set to a number in a list, try to get the width from the
  15363. #+ATTR.* keyword if it matches a width specification like
  15364. width=\"[0-9]+\", and fall back on that number if none is found.
  15365. When set to nil, try to get the width from an #+ATTR.* keyword
  15366. and fall back on the original width if none is found.
  15367. This requires Emacs >= 24.1, build with imagemagick support."
  15368. :group 'org-appearance
  15369. :version "24.3"
  15370. :type '(choice
  15371. (const :tag "Use the image width" t)
  15372. (integer :tag "Use a number of pixels")
  15373. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15374. (const :tag "Use #+ATTR* or don't resize" nil)))
  15375. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15376. "Convert a duration string S to minutes.
  15377. A bare number is interpreted as minutes, modifiers can be set by
  15378. customizing `org-effort-durations' (which see).
  15379. Entries containing a colon are interpreted as H:MM by
  15380. `org-hh:mm-string-to-minutes'."
  15381. (let ((result 0)
  15382. (re (concat "\\([0-9.]+\\) *\\("
  15383. (regexp-opt (mapcar 'car org-effort-durations))
  15384. "\\)")))
  15385. (while (string-match re s)
  15386. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15387. (string-to-number (match-string 1 s))))
  15388. (setq s (replace-match "" nil t s)))
  15389. (setq result (floor result))
  15390. (incf result (org-hh:mm-string-to-minutes s))
  15391. (if output-to-string (number-to-string result) result)))
  15392. ;;;; Files
  15393. (defun org-save-all-org-buffers ()
  15394. "Save all Org-mode buffers without user confirmation."
  15395. (interactive)
  15396. (message "Saving all Org-mode buffers...")
  15397. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15398. (when (featurep 'org-id) (org-id-locations-save))
  15399. (message "Saving all Org-mode buffers... done"))
  15400. (defun org-revert-all-org-buffers ()
  15401. "Revert all Org-mode buffers.
  15402. Prompt for confirmation when there are unsaved changes.
  15403. Be sure you know what you are doing before letting this function
  15404. overwrite your changes.
  15405. This function is useful in a setup where one tracks org files
  15406. with a version control system, to revert on one machine after pulling
  15407. changes from another. I believe the procedure must be like this:
  15408. 1. M-x org-save-all-org-buffers
  15409. 2. Pull changes from the other machine, resolve conflicts
  15410. 3. M-x org-revert-all-org-buffers"
  15411. (interactive)
  15412. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15413. (error "Abort"))
  15414. (save-excursion
  15415. (save-window-excursion
  15416. (mapc
  15417. (lambda (b)
  15418. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15419. (with-current-buffer b buffer-file-name))
  15420. (org-pop-to-buffer-same-window b)
  15421. (revert-buffer t 'no-confirm)))
  15422. (buffer-list))
  15423. (when (and (featurep 'org-id) org-id-track-globally)
  15424. (org-id-locations-load)))))
  15425. ;;;; Agenda files
  15426. ;;;###autoload
  15427. (defun org-switchb (&optional arg)
  15428. "Switch between Org buffers.
  15429. With one prefix argument, restrict available buffers to files.
  15430. With two prefix arguments, restrict available buffers to agenda files.
  15431. Defaults to `iswitchb' for buffer name completion.
  15432. Set `org-completion-use-ido' to make it use ido instead."
  15433. (interactive "P")
  15434. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15435. ((equal arg '(16)) (org-buffer-list 'agenda))
  15436. (t (org-buffer-list))))
  15437. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15438. (org-completion-use-ido org-completion-use-ido))
  15439. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15440. (setq org-completion-use-iswitchb t))
  15441. (org-pop-to-buffer-same-window
  15442. (org-icompleting-read "Org buffer: "
  15443. (mapcar 'list (mapcar 'buffer-name blist))
  15444. nil t))))
  15445. ;;; Define some older names previously used for this functionality
  15446. ;;;###autoload
  15447. (defalias 'org-ido-switchb 'org-switchb)
  15448. ;;;###autoload
  15449. (defalias 'org-iswitchb 'org-switchb)
  15450. (defun org-buffer-list (&optional predicate exclude-tmp)
  15451. "Return a list of Org buffers.
  15452. PREDICATE can be `export', `files' or `agenda'.
  15453. export restrict the list to Export buffers.
  15454. files restrict the list to buffers visiting Org files.
  15455. agenda restrict the list to buffers visiting agenda files.
  15456. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15457. (let* ((bfn nil)
  15458. (agenda-files (and (eq predicate 'agenda)
  15459. (mapcar 'file-truename (org-agenda-files t))))
  15460. (filter
  15461. (cond
  15462. ((eq predicate 'files)
  15463. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15464. ((eq predicate 'export)
  15465. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15466. ((eq predicate 'agenda)
  15467. (lambda (b)
  15468. (with-current-buffer b
  15469. (and (derived-mode-p 'org-mode)
  15470. (setq bfn (buffer-file-name b))
  15471. (member (file-truename bfn) agenda-files)))))
  15472. (t (lambda (b) (with-current-buffer b
  15473. (or (derived-mode-p 'org-mode)
  15474. (string-match "\*Org .*Export"
  15475. (buffer-name b)))))))))
  15476. (delq nil
  15477. (mapcar
  15478. (lambda(b)
  15479. (if (and (funcall filter b)
  15480. (or (not exclude-tmp)
  15481. (not (string-match "tmp" (buffer-name b)))))
  15482. b
  15483. nil))
  15484. (buffer-list)))))
  15485. (defun org-agenda-files (&optional unrestricted archives)
  15486. "Get the list of agenda files.
  15487. Optional UNRESTRICTED means return the full list even if a restriction
  15488. is currently in place.
  15489. When ARCHIVES is t, include all archive files that are really being
  15490. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15491. `org-agenda-archives-mode' is t."
  15492. (let ((files
  15493. (cond
  15494. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15495. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15496. ((listp org-agenda-files) org-agenda-files)
  15497. (t (error "Invalid value of `org-agenda-files'")))))
  15498. (setq files (apply 'append
  15499. (mapcar (lambda (f)
  15500. (if (file-directory-p f)
  15501. (directory-files
  15502. f t org-agenda-file-regexp)
  15503. (list f)))
  15504. files)))
  15505. (when org-agenda-skip-unavailable-files
  15506. (setq files (delq nil
  15507. (mapcar (function
  15508. (lambda (file)
  15509. (and (file-readable-p file) file)))
  15510. files))))
  15511. (when (or (eq archives t)
  15512. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15513. (setq files (org-add-archive-files files)))
  15514. files))
  15515. (defun org-agenda-file-p (&optional file)
  15516. "Return non-nil, if FILE is an agenda file.
  15517. If FILE is omitted, use the file associated with the current
  15518. buffer."
  15519. (member (or file (buffer-file-name))
  15520. (org-agenda-files t)))
  15521. (defun org-edit-agenda-file-list ()
  15522. "Edit the list of agenda files.
  15523. Depending on setup, this either uses customize to edit the variable
  15524. `org-agenda-files', or it visits the file that is holding the list. In the
  15525. latter case, the buffer is set up in a way that saving it automatically kills
  15526. the buffer and restores the previous window configuration."
  15527. (interactive)
  15528. (if (stringp org-agenda-files)
  15529. (let ((cw (current-window-configuration)))
  15530. (find-file org-agenda-files)
  15531. (org-set-local 'org-window-configuration cw)
  15532. (org-add-hook 'after-save-hook
  15533. (lambda ()
  15534. (set-window-configuration
  15535. (prog1 org-window-configuration
  15536. (kill-buffer (current-buffer))))
  15537. (org-install-agenda-files-menu)
  15538. (message "New agenda file list installed"))
  15539. nil 'local)
  15540. (message "%s" (substitute-command-keys
  15541. "Edit list and finish with \\[save-buffer]")))
  15542. (customize-variable 'org-agenda-files)))
  15543. (defun org-store-new-agenda-file-list (list)
  15544. "Set new value for the agenda file list and save it correctly."
  15545. (if (stringp org-agenda-files)
  15546. (let ((fe (org-read-agenda-file-list t)) b u)
  15547. (while (setq b (find-buffer-visiting org-agenda-files))
  15548. (kill-buffer b))
  15549. (with-temp-file org-agenda-files
  15550. (insert
  15551. (mapconcat
  15552. (lambda (f) ;; Keep un-expanded entries.
  15553. (if (setq u (assoc f fe))
  15554. (cdr u)
  15555. f))
  15556. list "\n")
  15557. "\n")))
  15558. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15559. (org-insert-mode-line-in-empty-file nil))
  15560. (setq org-agenda-files list)
  15561. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15562. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15563. "Read the list of agenda files from a file.
  15564. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15565. filenames, used by `org-store-new-agenda-file-list' to write back
  15566. un-expanded file names."
  15567. (when (file-directory-p org-agenda-files)
  15568. (error "`org-agenda-files' cannot be a single directory"))
  15569. (when (stringp org-agenda-files)
  15570. (with-temp-buffer
  15571. (insert-file-contents org-agenda-files)
  15572. (mapcar
  15573. (lambda (f)
  15574. (let ((e (expand-file-name (substitute-in-file-name f)
  15575. org-directory)))
  15576. (if pair-with-expansion
  15577. (cons e f)
  15578. e)))
  15579. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15580. ;;;###autoload
  15581. (defun org-cycle-agenda-files ()
  15582. "Cycle through the files in `org-agenda-files'.
  15583. If the current buffer visits an agenda file, find the next one in the list.
  15584. If the current buffer does not, find the first agenda file."
  15585. (interactive)
  15586. (let* ((fs (org-agenda-files t))
  15587. (files (append fs (list (car fs))))
  15588. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15589. file)
  15590. (unless files (error "No agenda files"))
  15591. (catch 'exit
  15592. (while (setq file (pop files))
  15593. (if (equal (file-truename file) tcf)
  15594. (when (car files)
  15595. (find-file (car files))
  15596. (throw 'exit t))))
  15597. (find-file (car fs)))
  15598. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15599. (defun org-agenda-file-to-front (&optional to-end)
  15600. "Move/add the current file to the top of the agenda file list.
  15601. If the file is not present in the list, it is added to the front. If it is
  15602. present, it is moved there. With optional argument TO-END, add/move to the
  15603. end of the list."
  15604. (interactive "P")
  15605. (let ((org-agenda-skip-unavailable-files nil)
  15606. (file-alist (mapcar (lambda (x)
  15607. (cons (file-truename x) x))
  15608. (org-agenda-files t)))
  15609. (ctf (file-truename buffer-file-name))
  15610. x had)
  15611. (setq x (assoc ctf file-alist) had x)
  15612. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15613. (if to-end
  15614. (setq file-alist (append (delq x file-alist) (list x)))
  15615. (setq file-alist (cons x (delq x file-alist))))
  15616. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15617. (org-install-agenda-files-menu)
  15618. (message "File %s to %s of agenda file list"
  15619. (if had "moved" "added") (if to-end "end" "front"))))
  15620. (defun org-remove-file (&optional file)
  15621. "Remove current file from the list of files in variable `org-agenda-files'.
  15622. These are the files which are being checked for agenda entries.
  15623. Optional argument FILE means use this file instead of the current."
  15624. (interactive)
  15625. (let* ((org-agenda-skip-unavailable-files nil)
  15626. (file (or file buffer-file-name))
  15627. (true-file (file-truename file))
  15628. (afile (abbreviate-file-name file))
  15629. (files (delq nil (mapcar
  15630. (lambda (x)
  15631. (if (equal true-file
  15632. (file-truename x))
  15633. nil x))
  15634. (org-agenda-files t)))))
  15635. (if (not (= (length files) (length (org-agenda-files t))))
  15636. (progn
  15637. (org-store-new-agenda-file-list files)
  15638. (org-install-agenda-files-menu)
  15639. (message "Removed file: %s" afile))
  15640. (message "File was not in list: %s (not removed)" afile))))
  15641. (defun org-file-menu-entry (file)
  15642. (vector file (list 'find-file file) t))
  15643. (defun org-check-agenda-file (file)
  15644. "Make sure FILE exists. If not, ask user what to do."
  15645. (when (not (file-exists-p file))
  15646. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15647. (abbreviate-file-name file))
  15648. (let ((r (downcase (read-char-exclusive))))
  15649. (cond
  15650. ((equal r ?r)
  15651. (org-remove-file file)
  15652. (throw 'nextfile t))
  15653. (t (error "Abort"))))))
  15654. (defun org-get-agenda-file-buffer (file)
  15655. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15656. it to the list of buffers which might be released later."
  15657. (let ((buf (org-find-base-buffer-visiting file)))
  15658. (if buf
  15659. buf ; just return it
  15660. ;; Make a new buffer and remember it
  15661. (setq buf (find-file-noselect file))
  15662. (if buf (push buf org-agenda-new-buffers))
  15663. buf)))
  15664. (defun org-release-buffers (blist)
  15665. "Release all buffers in list, asking the user for confirmation when needed.
  15666. When a buffer is unmodified, it is just killed. When modified, it is saved
  15667. \(if the user agrees) and then killed."
  15668. (let (buf file)
  15669. (while (setq buf (pop blist))
  15670. (setq file (buffer-file-name buf))
  15671. (when (and (buffer-modified-p buf)
  15672. file
  15673. (y-or-n-p (format "Save file %s? " file)))
  15674. (with-current-buffer buf (save-buffer)))
  15675. (kill-buffer buf))))
  15676. (defun org-agenda-prepare-buffers (files)
  15677. "Create buffers for all agenda files, protect archived trees and comments."
  15678. (interactive)
  15679. (let ((pa '(:org-archived t))
  15680. (pc '(:org-comment t))
  15681. (pall '(:org-archived t :org-comment t))
  15682. (inhibit-read-only t)
  15683. (rea (concat ":" org-archive-tag ":"))
  15684. bmp file re)
  15685. (save-excursion
  15686. (save-restriction
  15687. (while (setq file (pop files))
  15688. (catch 'nextfile
  15689. (if (bufferp file)
  15690. (set-buffer file)
  15691. (org-check-agenda-file file)
  15692. (set-buffer (org-get-agenda-file-buffer file)))
  15693. (widen)
  15694. (setq bmp (buffer-modified-p))
  15695. (org-refresh-category-properties)
  15696. (setq org-todo-keywords-for-agenda
  15697. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15698. (setq org-done-keywords-for-agenda
  15699. (append org-done-keywords-for-agenda org-done-keywords))
  15700. (setq org-todo-keyword-alist-for-agenda
  15701. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15702. (setq org-drawers-for-agenda
  15703. (append org-drawers-for-agenda org-drawers))
  15704. (setq org-tag-alist-for-agenda
  15705. (append org-tag-alist-for-agenda org-tag-alist))
  15706. (save-excursion
  15707. (remove-text-properties (point-min) (point-max) pall)
  15708. (when org-agenda-skip-archived-trees
  15709. (goto-char (point-min))
  15710. (while (re-search-forward rea nil t)
  15711. (if (org-at-heading-p t)
  15712. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15713. (goto-char (point-min))
  15714. (setq re (format org-heading-keyword-regexp-format
  15715. org-comment-string))
  15716. (while (re-search-forward re nil t)
  15717. (add-text-properties
  15718. (match-beginning 0) (org-end-of-subtree t) pc)))
  15719. (set-buffer-modified-p bmp)))))
  15720. (setq org-todo-keywords-for-agenda
  15721. (org-uniquify org-todo-keywords-for-agenda))
  15722. (setq org-todo-keyword-alist-for-agenda
  15723. (org-uniquify org-todo-keyword-alist-for-agenda)
  15724. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15725. ;;;; Embedded LaTeX
  15726. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15727. "Keymap for the minor `org-cdlatex-mode'.")
  15728. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15729. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15730. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15731. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15732. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15733. (defvar org-cdlatex-texmathp-advice-is-done nil
  15734. "Flag remembering if we have applied the advice to texmathp already.")
  15735. (define-minor-mode org-cdlatex-mode
  15736. "Toggle the minor `org-cdlatex-mode'.
  15737. This mode supports entering LaTeX environment and math in LaTeX fragments
  15738. in Org-mode.
  15739. \\{org-cdlatex-mode-map}"
  15740. nil " OCDL" nil
  15741. (when org-cdlatex-mode
  15742. (require 'cdlatex)
  15743. (run-hooks 'cdlatex-mode-hook)
  15744. (cdlatex-compute-tables))
  15745. (unless org-cdlatex-texmathp-advice-is-done
  15746. (setq org-cdlatex-texmathp-advice-is-done t)
  15747. (defadvice texmathp (around org-math-always-on activate)
  15748. "Always return t in org-mode buffers.
  15749. This is because we want to insert math symbols without dollars even outside
  15750. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15751. an embedded LaTeX fragment, let texmathp do its job.
  15752. \\[org-cdlatex-mode-map]"
  15753. (interactive)
  15754. (let (p)
  15755. (cond
  15756. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15757. ((eq this-command 'cdlatex-math-symbol)
  15758. (setq ad-return-value t
  15759. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15760. (t
  15761. (let ((p (org-inside-LaTeX-fragment-p)))
  15762. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15763. (setq ad-return-value t
  15764. texmathp-why '("Org-mode embedded math" . 0))
  15765. (if p ad-do-it)))))))))
  15766. (defun turn-on-org-cdlatex ()
  15767. "Unconditionally turn on `org-cdlatex-mode'."
  15768. (org-cdlatex-mode 1))
  15769. (defun org-inside-LaTeX-fragment-p ()
  15770. "Test if point is inside a LaTeX fragment.
  15771. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15772. sequence appearing also before point.
  15773. Even though the matchers for math are configurable, this function assumes
  15774. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15775. delimiters are skipped when they have been removed by customization.
  15776. The return value is nil, or a cons cell with the delimiter and the
  15777. position of this delimiter.
  15778. This function does a reasonably good job, but can locally be fooled by
  15779. for example currency specifications. For example it will assume being in
  15780. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15781. fragments that are properly closed, but during editing, we have to live
  15782. with the uncertainty caused by missing closing delimiters. This function
  15783. looks only before point, not after."
  15784. (catch 'exit
  15785. (let ((pos (point))
  15786. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15787. (lim (progn
  15788. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15789. (point)))
  15790. dd-on str (start 0) m re)
  15791. (goto-char pos)
  15792. (when dodollar
  15793. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15794. re (nth 1 (assoc "$" org-latex-regexps)))
  15795. (while (string-match re str start)
  15796. (cond
  15797. ((= (match-end 0) (length str))
  15798. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15799. ((= (match-end 0) (- (length str) 5))
  15800. (throw 'exit nil))
  15801. (t (setq start (match-end 0))))))
  15802. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15803. (goto-char pos)
  15804. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15805. (and (match-beginning 2) (throw 'exit nil))
  15806. ;; count $$
  15807. (while (re-search-backward "\\$\\$" lim t)
  15808. (setq dd-on (not dd-on)))
  15809. (goto-char pos)
  15810. (if dd-on (cons "$$" m))))))
  15811. (defun org-inside-latex-macro-p ()
  15812. "Is point inside a LaTeX macro or its arguments?"
  15813. (save-match-data
  15814. (org-in-regexp
  15815. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15816. (defun org-try-cdlatex-tab ()
  15817. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15818. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15819. - inside a LaTeX fragment, or
  15820. - after the first word in a line, where an abbreviation expansion could
  15821. insert a LaTeX environment."
  15822. (when org-cdlatex-mode
  15823. (cond
  15824. ;; Before any word on the line: No expansion possible.
  15825. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15826. ;; Just after first word on the line: Expand it. Make sure it
  15827. ;; cannot happen on headlines, though.
  15828. ((save-excursion
  15829. (skip-chars-backward "a-zA-Z0-9*")
  15830. (skip-chars-backward " \t")
  15831. (and (bolp) (not (org-at-heading-p))))
  15832. (cdlatex-tab) t)
  15833. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15834. (defun org-cdlatex-underscore-caret (&optional arg)
  15835. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15836. Revert to the normal definition outside of these fragments."
  15837. (interactive "P")
  15838. (if (org-inside-LaTeX-fragment-p)
  15839. (call-interactively 'cdlatex-sub-superscript)
  15840. (let (org-cdlatex-mode)
  15841. (call-interactively (key-binding (vector last-input-event))))))
  15842. (defun org-cdlatex-math-modify (&optional arg)
  15843. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15844. Revert to the normal definition outside of these fragments."
  15845. (interactive "P")
  15846. (if (org-inside-LaTeX-fragment-p)
  15847. (call-interactively 'cdlatex-math-modify)
  15848. (let (org-cdlatex-mode)
  15849. (call-interactively (key-binding (vector last-input-event))))))
  15850. (defvar org-latex-fragment-image-overlays nil
  15851. "List of overlays carrying the images of latex fragments.")
  15852. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15853. (defun org-remove-latex-fragment-image-overlays ()
  15854. "Remove all overlays with LaTeX fragment images in current buffer."
  15855. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15856. (setq org-latex-fragment-image-overlays nil))
  15857. (defun org-preview-latex-fragment (&optional subtree)
  15858. "Preview the LaTeX fragment at point, or all locally or globally.
  15859. If the cursor is in a LaTeX fragment, create the image and overlay
  15860. it over the source code. If there is no fragment at point, display
  15861. all fragments in the current text, from one headline to the next. With
  15862. prefix SUBTREE, display all fragments in the current subtree. With a
  15863. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15864. the cursor is before the first headline,
  15865. display all fragments in the buffer.
  15866. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15867. (interactive "P")
  15868. (unless buffer-file-name
  15869. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15870. (org-remove-latex-fragment-image-overlays)
  15871. (save-excursion
  15872. (save-restriction
  15873. (let (beg end at msg)
  15874. (cond
  15875. ((or (equal subtree '(16))
  15876. (not (save-excursion
  15877. (re-search-backward org-outline-regexp-bol nil t))))
  15878. (setq beg (point-min) end (point-max)
  15879. msg "Creating images for buffer...%s"))
  15880. ((equal subtree '(4))
  15881. (org-back-to-heading)
  15882. (setq beg (point) end (org-end-of-subtree t)
  15883. msg "Creating images for subtree...%s"))
  15884. (t
  15885. (if (setq at (org-inside-LaTeX-fragment-p))
  15886. (goto-char (max (point-min) (- (cdr at) 2)))
  15887. (org-back-to-heading))
  15888. (setq beg (point) end (progn (outline-next-heading) (point))
  15889. msg (if at "Creating image...%s"
  15890. "Creating images for entry...%s"))))
  15891. (message msg "")
  15892. (narrow-to-region beg end)
  15893. (goto-char beg)
  15894. (org-format-latex
  15895. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  15896. (file-name-nondirectory
  15897. buffer-file-name)))
  15898. default-directory 'overlays msg at 'forbuffer
  15899. org-latex-create-formula-image-program)
  15900. (message msg "done. Use `C-c C-c' to remove images.")))))
  15901. (defvar org-latex-regexps
  15902. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15903. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15904. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15905. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15906. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15907. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15908. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15909. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15910. "Regular expressions for matching embedded LaTeX.")
  15911. (defvar org-export-have-math nil) ;; dynamic scoping
  15912. (defun org-format-latex (prefix &optional dir overlays msg at
  15913. forbuffer processing-type)
  15914. "Replace LaTeX fragments with links to an image, and produce images.
  15915. Some of the options can be changed using the variable
  15916. `org-format-latex-options'."
  15917. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15918. (let* ((prefixnodir (file-name-nondirectory prefix))
  15919. (absprefix (expand-file-name prefix dir))
  15920. (todir (file-name-directory absprefix))
  15921. (opt org-format-latex-options)
  15922. (optnew org-format-latex-options)
  15923. (matchers (plist-get opt :matchers))
  15924. (re-list org-latex-regexps)
  15925. (org-format-latex-header-extra
  15926. (plist-get (org-infile-export-plist) :latex-header-extra))
  15927. (cnt 0) txt hash link beg end re e checkdir
  15928. string
  15929. m n block-type block linkfile movefile ov)
  15930. ;; Check the different regular expressions
  15931. (while (setq e (pop re-list))
  15932. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15933. block (if block-type "\n\n" ""))
  15934. (when (member m matchers)
  15935. (goto-char (point-min))
  15936. (while (re-search-forward re nil t)
  15937. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15938. (not (get-text-property (match-beginning n)
  15939. 'org-protected))
  15940. (or (not overlays)
  15941. (not (eq (get-char-property (match-beginning n)
  15942. 'org-overlay-type)
  15943. 'org-latex-overlay))))
  15944. (setq org-export-have-math t)
  15945. (cond
  15946. ((eq processing-type 'verbatim)
  15947. ;; Leave the text verbatim, just protect it
  15948. (add-text-properties (match-beginning n) (match-end n)
  15949. '(org-protected t)))
  15950. ((eq processing-type 'mathjax)
  15951. ;; Prepare for MathJax processing
  15952. (setq string (match-string n))
  15953. (if (member m '("$" "$1"))
  15954. (save-excursion
  15955. (delete-region (match-beginning n) (match-end n))
  15956. (goto-char (match-beginning n))
  15957. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15958. "\\)")
  15959. '(org-protected t))))
  15960. (add-text-properties (match-beginning n) (match-end n)
  15961. '(org-protected t))))
  15962. ((or (eq processing-type 'dvipng)
  15963. (eq processing-type 'imagemagick))
  15964. ;; Process to an image
  15965. (setq txt (match-string n)
  15966. beg (match-beginning n) end (match-end n)
  15967. cnt (1+ cnt))
  15968. (let ((face (face-at-point))
  15969. (fg (plist-get opt :foreground))
  15970. (bg (plist-get opt :background))
  15971. print-length print-level) ; make sure full list is printed
  15972. (when forbuffer
  15973. ; Get the colors from the face at point
  15974. (goto-char beg)
  15975. (when (eq fg 'auto)
  15976. (setq fg (face-attribute face :foreground nil 'default)))
  15977. (when (eq bg 'auto)
  15978. (setq bg (face-attribute face :background nil 'default)))
  15979. (setq optnew (copy-sequence opt))
  15980. (plist-put optnew :foreground fg)
  15981. (plist-put optnew :background bg))
  15982. (setq hash (sha1 (prin1-to-string
  15983. (list org-format-latex-header
  15984. org-format-latex-header-extra
  15985. org-export-latex-default-packages-alist
  15986. org-export-latex-packages-alist
  15987. org-format-latex-options
  15988. forbuffer txt fg bg)))
  15989. linkfile (format "%s_%s.png" prefix hash)
  15990. movefile (format "%s_%s.png" absprefix hash)))
  15991. (setq link (concat block "[[file:" linkfile "]]" block))
  15992. (if msg (message msg cnt))
  15993. (goto-char beg)
  15994. (unless checkdir ; make sure the directory exists
  15995. (setq checkdir t)
  15996. (or (file-directory-p todir) (make-directory todir t)))
  15997. (org-create-formula-image
  15998. txt movefile optnew forbuffer processing-type)
  15999. (if overlays
  16000. (progn
  16001. (mapc (lambda (o)
  16002. (if (eq (overlay-get o 'org-overlay-type)
  16003. 'org-latex-overlay)
  16004. (delete-overlay o)))
  16005. (overlays-in beg end))
  16006. (setq ov (make-overlay beg end))
  16007. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16008. (if (featurep 'xemacs)
  16009. (progn
  16010. (overlay-put ov 'invisible t)
  16011. (overlay-put
  16012. ov 'end-glyph
  16013. (make-glyph (vector 'png :file movefile))))
  16014. (overlay-put
  16015. ov 'display
  16016. (list 'image :type 'png :file movefile :ascent 'center)))
  16017. (push ov org-latex-fragment-image-overlays)
  16018. (goto-char end))
  16019. (delete-region beg end)
  16020. (insert (org-add-props link
  16021. (list 'org-latex-src
  16022. (replace-regexp-in-string
  16023. "\"" "" txt)
  16024. 'org-latex-src-embed-type
  16025. (if block-type 'paragraph 'character))))))
  16026. ((eq processing-type 'mathml)
  16027. ;; Process to MathML
  16028. (unless (save-match-data (org-format-latex-mathml-available-p))
  16029. (error "LaTeX to MathML converter not configured"))
  16030. (setq txt (match-string n)
  16031. beg (match-beginning n) end (match-end n)
  16032. cnt (1+ cnt))
  16033. (if msg (message msg cnt))
  16034. (goto-char beg)
  16035. (delete-region beg end)
  16036. (insert (org-format-latex-as-mathml
  16037. txt block-type prefix dir)))
  16038. (t
  16039. (error "Unknown conversion type %s for latex fragments"
  16040. processing-type)))))))))
  16041. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16042. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16043. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16044. sucessful, return the portion between \"<math...> </math>\"
  16045. elements otherwise return nil. When MATHML-FILE is specified,
  16046. write the results in to that file. When invoked as an
  16047. interactive command, prompt for LATEX-FRAG, with initial value
  16048. set to the current active region and echo the results for user
  16049. inspection."
  16050. (interactive (list (let ((frag (when (org-region-active-p)
  16051. (buffer-substring-no-properties
  16052. (region-beginning) (region-end)))))
  16053. (read-string "LaTeX Fragment: " frag nil frag))))
  16054. (unless latex-frag (error "Invalid latex-frag"))
  16055. (let* ((tmp-in-file (file-relative-name
  16056. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16057. (ignore (write-region latex-frag nil tmp-in-file))
  16058. (tmp-out-file (file-relative-name
  16059. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16060. (cmd (format-spec
  16061. org-latex-to-mathml-convert-command
  16062. `((?j . ,(shell-quote-argument
  16063. (expand-file-name org-latex-to-mathml-jar-file)))
  16064. (?I . ,(shell-quote-argument tmp-in-file))
  16065. (?o . ,(shell-quote-argument tmp-out-file)))))
  16066. mathml shell-command-output)
  16067. (when (org-called-interactively-p 'any)
  16068. (unless (org-format-latex-mathml-available-p)
  16069. (error "LaTeX to MathML converter not configured")))
  16070. (message "Running %s" cmd)
  16071. (setq shell-command-output (shell-command-to-string cmd))
  16072. (setq mathml
  16073. (when (file-readable-p tmp-out-file)
  16074. (with-current-buffer (find-file-noselect tmp-out-file t)
  16075. (goto-char (point-min))
  16076. (when (re-search-forward
  16077. (concat
  16078. (regexp-quote
  16079. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16080. "\\(.\\|\n\\)*"
  16081. (regexp-quote "</math>")) nil t)
  16082. (prog1 (match-string 0) (kill-buffer))))))
  16083. (cond
  16084. (mathml
  16085. (setq mathml
  16086. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16087. (when mathml-file
  16088. (write-region mathml nil mathml-file))
  16089. (when (org-called-interactively-p 'any)
  16090. (message mathml)))
  16091. ((message "LaTeX to MathML conversion failed")
  16092. (message shell-command-output)))
  16093. (delete-file tmp-in-file)
  16094. (when (file-exists-p tmp-out-file)
  16095. (delete-file tmp-out-file))
  16096. mathml))
  16097. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16098. prefix &optional dir)
  16099. "Use `org-create-math-formula' but check local cache first."
  16100. (let* ((absprefix (expand-file-name prefix dir))
  16101. (print-length nil) (print-level nil)
  16102. (formula-id (concat
  16103. "formula-"
  16104. (sha1
  16105. (prin1-to-string
  16106. (list latex-frag
  16107. org-latex-to-mathml-convert-command)))))
  16108. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16109. (formula-cache-dir (file-name-directory formula-cache)))
  16110. (unless (file-directory-p formula-cache-dir)
  16111. (make-directory formula-cache-dir t))
  16112. (unless (file-exists-p formula-cache)
  16113. (org-create-math-formula latex-frag formula-cache))
  16114. (if (file-exists-p formula-cache)
  16115. ;; Successful conversion. Return the link to MathML file.
  16116. (org-add-props
  16117. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16118. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16119. 'org-latex-src-embed-type (if latex-frag-type
  16120. 'paragraph 'character)))
  16121. ;; Failed conversion. Return the LaTeX fragment verbatim
  16122. (add-text-properties
  16123. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16124. latex-frag)))
  16125. (defun org-create-formula-image (string tofile options buffer &optional type)
  16126. "Create an image from LaTeX source using dvipng or convert.
  16127. This function calls either `org-create-formula-image-with-dvipng'
  16128. or `org-create-formula-image-with-imagemagick' depending on the
  16129. value of `org-latex-create-formula-image-program' or on the value
  16130. of the optional TYPE variable.
  16131. Note: ultimately these two function should be combined as they
  16132. share a good deal of logic."
  16133. (org-check-external-command
  16134. "latex" "needed to convert LaTeX fragments to images")
  16135. (funcall
  16136. (case (or type org-latex-create-formula-image-program)
  16137. ('dvipng
  16138. (org-check-external-command
  16139. "dvipng" "needed to convert LaTeX fragments to images")
  16140. #'org-create-formula-image-with-dvipng)
  16141. ('imagemagick
  16142. (org-check-external-command
  16143. "convert" "you need to install imagemagick")
  16144. #'org-create-formula-image-with-imagemagick)
  16145. (t (error
  16146. "invalid value of `org-latex-create-formula-image-program'")))
  16147. string tofile options buffer))
  16148. ;; This function borrows from Ganesh Swami's latex2png.el
  16149. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16150. "This calls dvipng."
  16151. (require 'org-latex)
  16152. (let* ((tmpdir (if (featurep 'xemacs)
  16153. (temp-directory)
  16154. temporary-file-directory))
  16155. (texfilebase (make-temp-name
  16156. (expand-file-name "orgtex" tmpdir)))
  16157. (texfile (concat texfilebase ".tex"))
  16158. (dvifile (concat texfilebase ".dvi"))
  16159. (pngfile (concat texfilebase ".png"))
  16160. (fnh (if (featurep 'xemacs)
  16161. (font-height (face-font 'default))
  16162. (face-attribute 'default :height nil)))
  16163. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16164. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16165. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16166. "Black"))
  16167. (bg (or (plist-get options (if buffer :background :html-background))
  16168. "Transparent")))
  16169. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16170. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16171. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16172. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16173. (with-temp-file texfile
  16174. (insert (org-splice-latex-header
  16175. org-format-latex-header
  16176. org-export-latex-default-packages-alist
  16177. org-export-latex-packages-alist t
  16178. org-format-latex-header-extra))
  16179. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16180. (require 'org-latex)
  16181. (org-export-latex-fix-inputenc))
  16182. (let ((dir default-directory))
  16183. (condition-case nil
  16184. (progn
  16185. (cd tmpdir)
  16186. (call-process "latex" nil nil nil texfile))
  16187. (error nil))
  16188. (cd dir))
  16189. (if (not (file-exists-p dvifile))
  16190. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16191. (condition-case nil
  16192. (if (featurep 'xemacs)
  16193. (call-process "dvipng" nil nil nil
  16194. "-fg" fg "-bg" bg
  16195. "-T" "tight"
  16196. "-o" pngfile
  16197. dvifile)
  16198. (call-process "dvipng" nil nil nil
  16199. "-fg" fg "-bg" bg
  16200. "-D" dpi
  16201. ;;"-x" scale "-y" scale
  16202. "-T" "tight"
  16203. "-o" pngfile
  16204. dvifile))
  16205. (error nil))
  16206. (if (not (file-exists-p pngfile))
  16207. (if org-format-latex-signal-error
  16208. (error "Failed to create png file from %s" texfile)
  16209. (message "Failed to create png file from %s" texfile)
  16210. nil)
  16211. ;; Use the requested file name and clean up
  16212. (copy-file pngfile tofile 'replace)
  16213. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16214. (if (file-exists-p (concat texfilebase e))
  16215. (delete-file (concat texfilebase e))))
  16216. pngfile))))
  16217. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16218. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16219. "This calls convert, which is included into imagemagick."
  16220. (require 'org-latex)
  16221. (let* ((tmpdir (if (featurep 'xemacs)
  16222. (temp-directory)
  16223. temporary-file-directory))
  16224. (texfilebase (make-temp-name
  16225. (expand-file-name "orgtex" tmpdir)))
  16226. (texfile (concat texfilebase ".tex"))
  16227. (pdffile (concat texfilebase ".pdf"))
  16228. (pngfile (concat texfilebase ".png"))
  16229. (fnh (if (featurep 'xemacs)
  16230. (font-height (face-font 'default))
  16231. (face-attribute 'default :height nil)))
  16232. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16233. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16234. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16235. "black"))
  16236. (bg (or (plist-get options (if buffer :background :html-background))
  16237. "white")))
  16238. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16239. (setq fg (org-latex-color-format fg)))
  16240. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16241. (setq bg (org-latex-color-format
  16242. (if (string= bg "Transparent") "white" bg))))
  16243. (with-temp-file texfile
  16244. (insert (org-splice-latex-header
  16245. org-format-latex-header
  16246. org-export-latex-default-packages-alist
  16247. org-export-latex-packages-alist t
  16248. org-format-latex-header-extra))
  16249. (insert "\n\\begin{document}\n"
  16250. "\\definecolor{fg}{rgb}{" fg "}\n"
  16251. "\\definecolor{bg}{rgb}{" bg "}\n"
  16252. "\n\\pagecolor{bg}\n"
  16253. "\n{\\color{fg}\n"
  16254. string
  16255. "\n}\n"
  16256. "\n\\end{document}\n" )
  16257. (require 'org-latex)
  16258. (org-export-latex-fix-inputenc))
  16259. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16260. (condition-case nil
  16261. (progn
  16262. (cd tmpdir)
  16263. (setq cmds org-latex-to-pdf-process)
  16264. (while cmds
  16265. (setq latex-frags-cmds (pop cmds))
  16266. (if (listp latex-frags-cmds)
  16267. (setq cmds nil)
  16268. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16269. (while latex-frags-cmds
  16270. (setq cmd (pop latex-frags-cmds))
  16271. (while (string-match "%b" cmd)
  16272. (setq cmd (replace-match
  16273. (save-match-data
  16274. (shell-quote-argument texfile))
  16275. t t cmd)))
  16276. (while (string-match "%f" cmd)
  16277. (setq cmd (replace-match
  16278. (save-match-data
  16279. (shell-quote-argument (file-name-nondirectory texfile)))
  16280. t t cmd)))
  16281. (while (string-match "%o" cmd)
  16282. (setq cmd (replace-match
  16283. (save-match-data
  16284. (shell-quote-argument (file-name-directory texfile)))
  16285. t t cmd)))
  16286. (setq cmd (split-string cmd))
  16287. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16288. (error nil))
  16289. (cd dir))
  16290. (if (not (file-exists-p pdffile))
  16291. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16292. (condition-case nil
  16293. (if (featurep 'xemacs)
  16294. (call-process "convert" nil nil nil
  16295. "-density" "96"
  16296. "-trim"
  16297. "-antialias"
  16298. pdffile
  16299. "-quality" "100"
  16300. ;; "-sharpen" "0x1.0"
  16301. pngfile)
  16302. (call-process "convert" nil nil nil
  16303. "-density" dpi
  16304. "-trim"
  16305. "-antialias"
  16306. pdffile
  16307. "-quality" "100"
  16308. ; "-sharpen" "0x1.0"
  16309. pngfile))
  16310. (error nil))
  16311. (if (not (file-exists-p pngfile))
  16312. (if org-format-latex-signal-error
  16313. (error "Failed to create png file from %s" texfile)
  16314. (message "Failed to create png file from %s" texfile)
  16315. nil)
  16316. ;; Use the requested file name and clean up
  16317. (copy-file pngfile tofile 'replace)
  16318. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16319. (if (file-exists-p (concat texfilebase e))
  16320. (delete-file (concat texfilebase e))))
  16321. pngfile))))
  16322. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16323. "Fill a LaTeX header template TPL.
  16324. In the template, the following place holders will be recognized:
  16325. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16326. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16327. [PACKAGES] \\usepackage statements for PKG
  16328. [NO-PACKAGES] do not include PKG
  16329. [EXTRA] the string EXTRA
  16330. [NO-EXTRA] do not include EXTRA
  16331. For backward compatibility, if both the positive and the negative place
  16332. holder is missing, the positive one (without the \"NO-\") will be
  16333. assumed to be present at the end of the template.
  16334. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16335. EXTRA is a string.
  16336. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16337. (let (rpl (end ""))
  16338. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16339. (setq rpl (if (or (match-end 1) (not def-pkg))
  16340. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16341. tpl (replace-match rpl t t tpl))
  16342. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16343. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16344. (setq rpl (if (or (match-end 1) (not pkg))
  16345. "" (org-latex-packages-to-string pkg snippets-p t))
  16346. tpl (replace-match rpl t t tpl))
  16347. (if pkg (setq end
  16348. (concat end "\n"
  16349. (org-latex-packages-to-string pkg snippets-p)))))
  16350. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16351. (setq rpl (if (or (match-end 1) (not extra))
  16352. "" (concat extra "\n"))
  16353. tpl (replace-match rpl t t tpl))
  16354. (if (and extra (string-match "\\S-" extra))
  16355. (setq end (concat end "\n" extra))))
  16356. (if (string-match "\\S-" end)
  16357. (concat tpl "\n" end)
  16358. tpl)))
  16359. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16360. "Turn an alist of packages into a string with the \\usepackage macros."
  16361. (setq pkg (mapconcat (lambda(p)
  16362. (cond
  16363. ((stringp p) p)
  16364. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16365. (format "%% Package %s omitted" (cadr p)))
  16366. ((equal "" (car p))
  16367. (format "\\usepackage{%s}" (cadr p)))
  16368. (t
  16369. (format "\\usepackage[%s]{%s}"
  16370. (car p) (cadr p)))))
  16371. pkg
  16372. "\n"))
  16373. (if newline (concat pkg "\n") pkg))
  16374. (defun org-dvipng-color (attr)
  16375. "Return a RGB color specification for dvipng."
  16376. (apply 'format "rgb %s %s %s"
  16377. (mapcar 'org-normalize-color
  16378. (if (featurep 'xemacs)
  16379. (color-rgb-components
  16380. (face-property 'default
  16381. (cond ((eq attr :foreground) 'foreground)
  16382. ((eq attr :background) 'background))))
  16383. (color-values (face-attribute 'default attr nil))))))
  16384. (defun org-dvipng-color-format (color-name)
  16385. "Convert COLOR-NAME to a RGB color value for dvipng."
  16386. (apply 'format "rgb %s %s %s"
  16387. (mapcar 'org-normalize-color
  16388. (color-values color-name))))
  16389. (defun org-latex-color (attr)
  16390. "Return a RGB color for the LaTeX color package."
  16391. (apply 'format "%s,%s,%s"
  16392. (mapcar 'org-normalize-color
  16393. (if (featurep 'xemacs)
  16394. (color-rgb-components
  16395. (face-property 'default
  16396. (cond ((eq attr :foreground) 'foreground)
  16397. ((eq attr :background) 'background))))
  16398. (color-values (face-attribute 'default attr nil))))))
  16399. (defun org-latex-color-format (color-name)
  16400. "Convert COLOR-NAME to a RGB color value."
  16401. (apply 'format "%s,%s,%s"
  16402. (mapcar 'org-normalize-color
  16403. (color-values color-name))))
  16404. (defun org-normalize-color (value)
  16405. "Return string to be used as color value for an RGB component."
  16406. (format "%g" (/ value 65535.0)))
  16407. ;; Image display
  16408. (defvar org-inline-image-overlays nil)
  16409. (make-variable-buffer-local 'org-inline-image-overlays)
  16410. (defun org-toggle-inline-images (&optional include-linked)
  16411. "Toggle the display of inline images.
  16412. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16413. (interactive "P")
  16414. (if org-inline-image-overlays
  16415. (progn
  16416. (org-remove-inline-images)
  16417. (message "Inline image display turned off"))
  16418. (org-display-inline-images include-linked)
  16419. (if (and (org-called-interactively-p)
  16420. org-inline-image-overlays)
  16421. (message "%d images displayed inline"
  16422. (length org-inline-image-overlays))
  16423. (message "No images to display inline"))))
  16424. (defun org-redisplay-inline-images ()
  16425. "Refresh the display of inline images."
  16426. (interactive)
  16427. (if (not org-inline-image-overlays)
  16428. (org-toggle-inline-images)
  16429. (org-toggle-inline-images)
  16430. (org-toggle-inline-images)))
  16431. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16432. "Display inline images.
  16433. Normally only links without a description part are inlined, because this
  16434. is how it will work for export. When INCLUDE-LINKED is set, also links
  16435. with a description part will be inlined. This can be nice for a quick
  16436. look at those images, but it does not reflect what exported files will look
  16437. like.
  16438. When REFRESH is set, refresh existing images between BEG and END.
  16439. This will create new image displays only if necessary.
  16440. BEG and END default to the buffer boundaries."
  16441. (interactive "P")
  16442. (unless refresh
  16443. (org-remove-inline-images)
  16444. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16445. (save-excursion
  16446. (save-restriction
  16447. (widen)
  16448. (setq beg (or beg (point-min)) end (or end (point-max)))
  16449. (goto-char beg)
  16450. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16451. (substring (org-image-file-name-regexp) 0 -2)
  16452. "\\)\\]" (if include-linked "" "\\]")))
  16453. old file ov img type attrwidth width)
  16454. (while (re-search-forward re end t)
  16455. (setq old (get-char-property-and-overlay (match-beginning 1)
  16456. 'org-image-overlay)
  16457. file (expand-file-name
  16458. (concat (or (match-string 3) "") (match-string 4))))
  16459. (when (image-type-available-p 'imagemagick)
  16460. (setq attrwidth (if (or (listp org-image-actual-width)
  16461. (null org-image-actual-width))
  16462. (save-excursion
  16463. (save-match-data
  16464. (move-beginning-of-line 0)
  16465. (if (looking-at "#\\+ATTR.*width=\"\\([^\"]+\\)\"")
  16466. (string-to-number (match-string 1))))))
  16467. width (cond ((eq org-image-actual-width t) nil)
  16468. ((null org-image-actual-width) attrwidth)
  16469. ((numberp org-image-actual-width)
  16470. org-image-actual-width)
  16471. ((listp org-image-actual-width)
  16472. (or attrwidth (car org-image-actual-width))))
  16473. type (if width 'imagemagick)))
  16474. (when (file-exists-p file)
  16475. (if (and (car-safe old) refresh)
  16476. (image-refresh (overlay-get (cdr old) 'display))
  16477. (setq img (save-match-data (create-image file type nil :width width)))
  16478. (when img
  16479. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16480. (overlay-put ov 'display img)
  16481. (overlay-put ov 'face 'default)
  16482. (overlay-put ov 'org-image-overlay t)
  16483. (overlay-put ov 'modification-hooks
  16484. (list 'org-display-inline-modification-hook))
  16485. (push ov org-inline-image-overlays)))))))))
  16486. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  16487. "Remove inline-display overlay if a corresponding region is modified."
  16488. (let ((inhibit-modification-hooks t))
  16489. (when (and ov after)
  16490. (delete ov org-inline-image-overlays)
  16491. (delete-overlay ov))))
  16492. (defun org-remove-inline-images ()
  16493. "Remove inline display of images."
  16494. (interactive)
  16495. (mapc 'delete-overlay org-inline-image-overlays)
  16496. (setq org-inline-image-overlays nil))
  16497. ;;;; Key bindings
  16498. ;; Outline functions from `outline-mode-prefix-map'
  16499. ;; that can be remapped in Org:
  16500. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16501. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16502. (define-key org-mode-map [remap outline-forward-same-level]
  16503. 'org-forward-heading-same-level)
  16504. (define-key org-mode-map [remap outline-backward-same-level]
  16505. 'org-backward-heading-same-level)
  16506. (define-key org-mode-map [remap show-branches]
  16507. 'org-kill-note-or-show-branches)
  16508. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16509. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16510. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16511. ;; Outline functions from `outline-mode-prefix-map' that can not
  16512. ;; be remapped in Org:
  16513. ;;
  16514. ;; - the column "key binding" shows whether the Outline function is still
  16515. ;; available in Org mode on the same key that it has been bound to in
  16516. ;; Outline mode:
  16517. ;; - "overridden": key used for a different functionality in Org mode
  16518. ;; - else: key still bound to the same Outline function in Org mode
  16519. ;;
  16520. ;; | Outline function | key binding | Org replacement |
  16521. ;; |------------------------------------+-------------+-----------------------|
  16522. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16523. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16524. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16525. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16526. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16527. ;; | `show-entry' | overridden | no replacement |
  16528. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16529. ;; | `show-branches' | `C-c C-k' | still same function |
  16530. ;; | `show-subtree' | overridden | visibility cycling |
  16531. ;; | `show-all' | overridden | no replacement |
  16532. ;; | `hide-subtree' | overridden | visibility cycling |
  16533. ;; | `hide-body' | overridden | no replacement |
  16534. ;; | `hide-entry' | overridden | visibility cycling |
  16535. ;; | `hide-leaves' | overridden | no replacement |
  16536. ;; | `hide-sublevels' | overridden | no replacement |
  16537. ;; | `hide-other' | overridden | no replacement |
  16538. ;; Make `C-c C-x' a prefix key
  16539. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16540. ;; TAB key with modifiers
  16541. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16542. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16543. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16544. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16545. ;; The following line is necessary under Suse GNU/Linux
  16546. (unless (featurep 'xemacs)
  16547. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16548. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16549. (define-key org-mode-map [backtab] 'org-shifttab)
  16550. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16551. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16552. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16553. ;; Cursor keys with modifiers
  16554. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16555. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16556. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16557. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16558. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16559. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16560. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16561. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16562. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16563. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16564. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16565. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16566. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16567. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16568. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16569. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16570. ;; Babel keys
  16571. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16572. (mapc (lambda (pair)
  16573. (define-key org-babel-map (car pair) (cdr pair)))
  16574. org-babel-key-bindings)
  16575. ;;; Extra keys for tty access.
  16576. ;; We only set them when really needed because otherwise the
  16577. ;; menus don't show the simple keys
  16578. (when (or org-use-extra-keys
  16579. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16580. (not window-system))
  16581. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16582. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16583. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16584. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16585. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16586. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16587. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16588. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16589. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16590. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16591. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16592. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16593. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16594. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16595. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16596. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16597. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16598. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16599. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16600. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16601. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16602. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16603. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16604. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16605. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16606. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16607. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16608. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16609. ;; All the other keys
  16610. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16611. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16612. (if (boundp 'narrow-map)
  16613. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16614. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16615. (if (boundp 'narrow-map)
  16616. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16617. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16618. (if (boundp 'narrow-map)
  16619. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16620. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16621. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16622. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16623. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16624. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16625. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16626. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16627. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16628. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16629. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16630. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16631. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16632. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16633. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16634. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16635. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16636. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16637. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16638. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16639. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16640. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16641. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16642. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16643. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16644. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16645. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16646. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16647. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16648. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16649. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16650. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16651. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16652. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16653. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16654. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16655. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16656. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16657. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16658. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16659. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16660. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16661. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16662. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16663. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16664. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16665. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16666. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16667. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16668. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16669. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16670. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16671. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16672. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16673. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16674. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16675. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16676. (org-defkey org-mode-map "\C-m" 'org-return)
  16677. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16678. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16679. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16680. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16681. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16682. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16683. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16684. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16685. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16686. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16687. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16688. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16689. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16690. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16691. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16692. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16693. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16694. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16695. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16696. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16697. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16698. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16699. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16700. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16701. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16702. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16703. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16704. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16705. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16706. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16707. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16708. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16709. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16710. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16711. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16712. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16713. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16714. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16715. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16716. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16717. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16718. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16719. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16720. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16721. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16722. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16723. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16724. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16725. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16726. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16727. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16728. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16729. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16730. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16731. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16732. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16733. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16734. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16735. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16736. (when (featurep 'xemacs)
  16737. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16738. (defconst org-speed-commands-default
  16739. '(
  16740. ("Outline Navigation")
  16741. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16742. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16743. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16744. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16745. ("u" . (org-speed-move-safe 'outline-up-heading))
  16746. ("j" . org-goto)
  16747. ("g" . (org-refile t))
  16748. ("Outline Visibility")
  16749. ("c" . org-cycle)
  16750. ("C" . org-shifttab)
  16751. (" " . org-display-outline-path)
  16752. (":" . org-columns)
  16753. ("Outline Structure Editing")
  16754. ("U" . org-shiftmetaup)
  16755. ("D" . org-shiftmetadown)
  16756. ("r" . org-metaright)
  16757. ("l" . org-metaleft)
  16758. ("R" . org-shiftmetaright)
  16759. ("L" . org-shiftmetaleft)
  16760. ("i" . (progn (forward-char 1) (call-interactively
  16761. 'org-insert-heading-respect-content)))
  16762. ("^" . org-sort)
  16763. ("w" . org-refile)
  16764. ("a" . org-archive-subtree-default-with-confirmation)
  16765. ("." . org-mark-subtree)
  16766. ("#" . org-toggle-comment)
  16767. ("Clock Commands")
  16768. ("I" . org-clock-in)
  16769. ("O" . org-clock-out)
  16770. ("Meta Data Editing")
  16771. ("t" . org-todo)
  16772. ("0" . (org-priority ?\ ))
  16773. ("1" . (org-priority ?A))
  16774. ("2" . (org-priority ?B))
  16775. ("3" . (org-priority ?C))
  16776. (";" . org-set-tags-command)
  16777. ("e" . org-set-effort)
  16778. ("E" . org-inc-effort)
  16779. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  16780. (org-entry-put (point) "APPT_WARNTIME" m)))
  16781. ("Agenda Views etc")
  16782. ("v" . org-agenda)
  16783. ("/" . org-sparse-tree)
  16784. ("Misc")
  16785. ("o" . org-open-at-point)
  16786. ("?" . org-speed-command-help)
  16787. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16788. (">" . (org-agenda-remove-restriction-lock))
  16789. )
  16790. "The default speed commands.")
  16791. (defun org-print-speed-command (e)
  16792. (if (> (length (car e)) 1)
  16793. (progn
  16794. (princ "\n")
  16795. (princ (car e))
  16796. (princ "\n")
  16797. (princ (make-string (length (car e)) ?-))
  16798. (princ "\n"))
  16799. (princ (car e))
  16800. (princ " ")
  16801. (if (symbolp (cdr e))
  16802. (princ (symbol-name (cdr e)))
  16803. (prin1 (cdr e)))
  16804. (princ "\n")))
  16805. (defun org-speed-command-help ()
  16806. "Show the available speed commands."
  16807. (interactive)
  16808. (if (not org-use-speed-commands)
  16809. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16810. (with-output-to-temp-buffer "*Help*"
  16811. (princ "User-defined Speed commands\n===========================\n")
  16812. (mapc 'org-print-speed-command org-speed-commands-user)
  16813. (princ "\n")
  16814. (princ "Built-in Speed commands\n=======================\n")
  16815. (mapc 'org-print-speed-command org-speed-commands-default))
  16816. (with-current-buffer "*Help*"
  16817. (setq truncate-lines t))))
  16818. (defun org-speed-move-safe (cmd)
  16819. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16820. If not, return to the original position and throw an error."
  16821. (interactive)
  16822. (let ((pos (point)))
  16823. (call-interactively cmd)
  16824. (unless (and (bolp) (org-at-heading-p))
  16825. (goto-char pos)
  16826. (error "Boundary reached while executing %s" cmd))))
  16827. (defvar org-self-insert-command-undo-counter 0)
  16828. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16829. (defvar org-speed-command nil)
  16830. (defun org-speed-command-default-hook (keys)
  16831. "Hook for activating single-letter speed commands.
  16832. `org-speed-commands-default' specifies a minimal command set.
  16833. Use `org-speed-commands-user' for further customization."
  16834. (when (or (and (bolp) (looking-at org-outline-regexp))
  16835. (and (functionp org-use-speed-commands)
  16836. (funcall org-use-speed-commands)))
  16837. (cdr (assoc keys (append org-speed-commands-user
  16838. org-speed-commands-default)))))
  16839. (defun org-babel-speed-command-hook (keys)
  16840. "Hook for activating single-letter code block commands."
  16841. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16842. (cdr (assoc keys org-babel-key-bindings))))
  16843. (defcustom org-speed-command-hook
  16844. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16845. "Hook for activating speed commands at strategic locations.
  16846. Hook functions are called in sequence until a valid handler is
  16847. found.
  16848. Each hook takes a single argument, a user-pressed command key
  16849. which is also a `self-insert-command' from the global map.
  16850. Within the hook, examine the cursor position and the command key
  16851. and return nil or a valid handler as appropriate. Handler could
  16852. be one of an interactive command, a function, or a form.
  16853. Set `org-use-speed-commands' to non-nil value to enable this
  16854. hook. The default setting is `org-speed-command-default-hook'."
  16855. :group 'org-structure
  16856. :version "24.1"
  16857. :type 'hook)
  16858. (defun org-self-insert-command (N)
  16859. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16860. If the cursor is in a table looking at whitespace, the whitespace is
  16861. overwritten, and the table is not marked as requiring realignment."
  16862. (interactive "p")
  16863. (org-check-before-invisible-edit 'insert)
  16864. (cond
  16865. ((and org-use-speed-commands
  16866. (setq org-speed-command
  16867. (run-hook-with-args-until-success
  16868. 'org-speed-command-hook (this-command-keys))))
  16869. (cond
  16870. ((commandp org-speed-command)
  16871. (setq this-command org-speed-command)
  16872. (call-interactively org-speed-command))
  16873. ((functionp org-speed-command)
  16874. (funcall org-speed-command))
  16875. ((and org-speed-command (listp org-speed-command))
  16876. (eval org-speed-command))
  16877. (t (let (org-use-speed-commands)
  16878. (call-interactively 'org-self-insert-command)))))
  16879. ((and
  16880. (org-table-p)
  16881. (progn
  16882. ;; check if we blank the field, and if that triggers align
  16883. (and (featurep 'org-table) org-table-auto-blank-field
  16884. (member last-command
  16885. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16886. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16887. ;; got extra space, this field does not determine column width
  16888. (let (org-table-may-need-update) (org-table-blank-field))
  16889. ;; no extra space, this field may determine column width
  16890. (org-table-blank-field)))
  16891. t)
  16892. (eq N 1)
  16893. (looking-at "[^|\n]* |"))
  16894. (let (org-table-may-need-update)
  16895. (goto-char (1- (match-end 0)))
  16896. (backward-delete-char 1)
  16897. (goto-char (match-beginning 0))
  16898. (self-insert-command N)))
  16899. (t
  16900. (setq org-table-may-need-update t)
  16901. (self-insert-command N)
  16902. (org-fix-tags-on-the-fly)
  16903. (if org-self-insert-cluster-for-undo
  16904. (if (not (eq last-command 'org-self-insert-command))
  16905. (setq org-self-insert-command-undo-counter 1)
  16906. (if (>= org-self-insert-command-undo-counter 20)
  16907. (setq org-self-insert-command-undo-counter 1)
  16908. (and (> org-self-insert-command-undo-counter 0)
  16909. buffer-undo-list (listp buffer-undo-list)
  16910. (not (cadr buffer-undo-list)) ; remove nil entry
  16911. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16912. (setq org-self-insert-command-undo-counter
  16913. (1+ org-self-insert-command-undo-counter))))))))
  16914. (defun org-check-before-invisible-edit (kind)
  16915. "Check is editing if kind KIND would be dangerous with invisible text around.
  16916. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16917. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16918. (if (and org-catch-invisible-edits
  16919. (or (not (boundp 'visible-mode)) (not visible-mode))
  16920. (or (get-char-property (point) 'invisible)
  16921. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16922. ;; OK, we need to take a closer look
  16923. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16924. (invisible-before-point (if (bobp) nil (get-char-property
  16925. (1- (point)) 'invisible)))
  16926. (border-and-ok-direction
  16927. (or
  16928. ;; Check if we are acting predictably before invisible text
  16929. (and invisible-at-point (not invisible-before-point)
  16930. (memq kind '(insert delete-backward)))
  16931. ;; Check if we are acting predictably after invisible text
  16932. ;; This works not well, and I have turned it off. It seems
  16933. ;; better to always show and stop after invisible text.
  16934. ;; (and (not invisible-at-point) invisible-before-point
  16935. ;; (memq kind '(insert delete)))
  16936. )))
  16937. (when (or (memq invisible-at-point '(outline org-hide-block t))
  16938. (memq invisible-before-point '(outline org-hide-block t)))
  16939. (if (eq org-catch-invisible-edits 'error)
  16940. (error "Editing in invisible areas is prohibited - make visible first"))
  16941. (if (and org-custom-properties-overlays
  16942. (y-or-n-p "Display invisible properties in this buffer? "))
  16943. (org-toggle-custom-properties-visibility)
  16944. ;; Make the area visible
  16945. (save-excursion
  16946. (if invisible-before-point
  16947. (goto-char (previous-single-char-property-change
  16948. (point) 'invisible)))
  16949. (org-cycle))
  16950. (cond
  16951. ((eq org-catch-invisible-edits 'show)
  16952. ;; That's it, we do the edit after showing
  16953. (message
  16954. "Unfolding invisible region around point before editing")
  16955. (sit-for 1))
  16956. ((and (eq org-catch-invisible-edits 'smart)
  16957. border-and-ok-direction)
  16958. (message "Unfolding invisible region around point before editing"))
  16959. (t
  16960. ;; Don't do the edit, make the user repeat it in full visibility
  16961. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  16962. (defun org-fix-tags-on-the-fly ()
  16963. (when (and (equal (char-after (point-at-bol)) ?*)
  16964. (org-at-heading-p))
  16965. (org-align-tags-here org-tags-column)))
  16966. (defun org-delete-backward-char (N)
  16967. "Like `delete-backward-char', insert whitespace at field end in tables.
  16968. When deleting backwards, in tables this function will insert whitespace in
  16969. front of the next \"|\" separator, to keep the table aligned. The table will
  16970. still be marked for re-alignment if the field did fill the entire column,
  16971. because, in this case the deletion might narrow the column."
  16972. (interactive "p")
  16973. (org-check-before-invisible-edit 'delete-backward)
  16974. (if (and (org-table-p)
  16975. (eq N 1)
  16976. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16977. (looking-at ".*?|"))
  16978. (let ((pos (point))
  16979. (noalign (looking-at "[^|\n\r]* |"))
  16980. (c org-table-may-need-update))
  16981. (backward-delete-char N)
  16982. (if (not overwrite-mode)
  16983. (progn
  16984. (skip-chars-forward "^|")
  16985. (insert " ")
  16986. (goto-char (1- pos))))
  16987. ;; noalign: if there were two spaces at the end, this field
  16988. ;; does not determine the width of the column.
  16989. (if noalign (setq org-table-may-need-update c)))
  16990. (backward-delete-char N)
  16991. (org-fix-tags-on-the-fly)))
  16992. (defun org-delete-char (N)
  16993. "Like `delete-char', but insert whitespace at field end in tables.
  16994. When deleting characters, in tables this function will insert whitespace in
  16995. front of the next \"|\" separator, to keep the table aligned. The table will
  16996. still be marked for re-alignment if the field did fill the entire column,
  16997. because, in this case the deletion might narrow the column."
  16998. (interactive "p")
  16999. (org-check-before-invisible-edit 'delete)
  17000. (if (and (org-table-p)
  17001. (not (bolp))
  17002. (not (= (char-after) ?|))
  17003. (eq N 1))
  17004. (if (looking-at ".*?|")
  17005. (let ((pos (point))
  17006. (noalign (looking-at "[^|\n\r]* |"))
  17007. (c org-table-may-need-update))
  17008. (replace-match (concat
  17009. (substring (match-string 0) 1 -1)
  17010. " |"))
  17011. (goto-char pos)
  17012. ;; noalign: if there were two spaces at the end, this field
  17013. ;; does not determine the width of the column.
  17014. (if noalign (setq org-table-may-need-update c)))
  17015. (delete-char N))
  17016. (delete-char N)
  17017. (org-fix-tags-on-the-fly)))
  17018. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17019. (put 'org-self-insert-command 'delete-selection t)
  17020. (put 'orgtbl-self-insert-command 'delete-selection t)
  17021. (put 'org-delete-char 'delete-selection 'supersede)
  17022. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17023. (put 'org-yank 'delete-selection 'yank)
  17024. ;; Make `flyspell-mode' delay after some commands
  17025. (put 'org-self-insert-command 'flyspell-delayed t)
  17026. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17027. (put 'org-delete-char 'flyspell-delayed t)
  17028. (put 'org-delete-backward-char 'flyspell-delayed t)
  17029. ;; Make pabbrev-mode expand after org-mode commands
  17030. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17031. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17032. ;; How to do this: Measure non-white length of current string
  17033. ;; If equal to column width, we should realign.
  17034. (defun org-remap (map &rest commands)
  17035. "In MAP, remap the functions given in COMMANDS.
  17036. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17037. (let (new old)
  17038. (while commands
  17039. (setq old (pop commands) new (pop commands))
  17040. (if (fboundp 'command-remapping)
  17041. (org-defkey map (vector 'remap old) new)
  17042. (substitute-key-definition old new map global-map)))))
  17043. (when (eq org-enable-table-editor 'optimized)
  17044. ;; If the user wants maximum table support, we need to hijack
  17045. ;; some standard editing functions
  17046. (org-remap org-mode-map
  17047. 'self-insert-command 'org-self-insert-command
  17048. 'delete-char 'org-delete-char
  17049. 'delete-backward-char 'org-delete-backward-char)
  17050. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17051. (defvar org-ctrl-c-ctrl-c-hook nil
  17052. "Hook for functions attaching themselves to `C-c C-c'.
  17053. This can be used to add additional functionality to the C-c C-c
  17054. key which executes context-dependent commands. This hook is run
  17055. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17056. run after the last test.
  17057. Each function will be called with no arguments. The function
  17058. must check if the context is appropriate for it to act. If yes,
  17059. it should do its thing and then return a non-nil value. If the
  17060. context is wrong, just do nothing and return nil.")
  17061. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17062. "Hook for functions attaching themselves to `C-c C-c'.
  17063. This can be used to add additional functionality to the C-c C-c
  17064. key which executes context-dependent commands. This hook is run
  17065. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17066. before the first test.
  17067. Each function will be called with no arguments. The function
  17068. must check if the context is appropriate for it to act. If yes,
  17069. it should do its thing and then return a non-nil value. If the
  17070. context is wrong, just do nothing and return nil.")
  17071. (defvar org-tab-first-hook nil
  17072. "Hook for functions to attach themselves to TAB.
  17073. See `org-ctrl-c-ctrl-c-hook' for more information.
  17074. This hook runs as the first action when TAB is pressed, even before
  17075. `org-cycle' messes around with the `outline-regexp' to cater for
  17076. inline tasks and plain list item folding.
  17077. If any function in this hook returns t, any other actions that
  17078. would have been caused by TAB (such as table field motion or visibility
  17079. cycling) will not occur.")
  17080. (defvar org-tab-after-check-for-table-hook nil
  17081. "Hook for functions to attach themselves to TAB.
  17082. See `org-ctrl-c-ctrl-c-hook' for more information.
  17083. This hook runs after it has been established that the cursor is not in a
  17084. table, but before checking if the cursor is in a headline or if global cycling
  17085. should be done.
  17086. If any function in this hook returns t, not other actions like visibility
  17087. cycling will be done.")
  17088. (defvar org-tab-after-check-for-cycling-hook nil
  17089. "Hook for functions to attach themselves to TAB.
  17090. See `org-ctrl-c-ctrl-c-hook' for more information.
  17091. This hook runs after it has been established that not table field motion and
  17092. not visibility should be done because of current context. This is probably
  17093. the place where a package like yasnippets can hook in.")
  17094. (defvar org-tab-before-tab-emulation-hook nil
  17095. "Hook for functions to attach themselves to TAB.
  17096. See `org-ctrl-c-ctrl-c-hook' for more information.
  17097. This hook runs after every other options for TAB have been exhausted, but
  17098. before indentation and \t insertion takes place.")
  17099. (defvar org-metaleft-hook nil
  17100. "Hook for functions attaching themselves to `M-left'.
  17101. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17102. (defvar org-metaright-hook nil
  17103. "Hook for functions attaching themselves to `M-right'.
  17104. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17105. (defvar org-metaup-hook nil
  17106. "Hook for functions attaching themselves to `M-up'.
  17107. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17108. (defvar org-metadown-hook nil
  17109. "Hook for functions attaching themselves to `M-down'.
  17110. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17111. (defvar org-shiftmetaleft-hook nil
  17112. "Hook for functions attaching themselves to `M-S-left'.
  17113. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17114. (defvar org-shiftmetaright-hook nil
  17115. "Hook for functions attaching themselves to `M-S-right'.
  17116. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17117. (defvar org-shiftmetaup-hook nil
  17118. "Hook for functions attaching themselves to `M-S-up'.
  17119. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17120. (defvar org-shiftmetadown-hook nil
  17121. "Hook for functions attaching themselves to `M-S-down'.
  17122. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17123. (defvar org-metareturn-hook nil
  17124. "Hook for functions attaching themselves to `M-RET'.
  17125. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17126. (defvar org-shiftup-hook nil
  17127. "Hook for functions attaching themselves to `S-up'.
  17128. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17129. (defvar org-shiftup-final-hook nil
  17130. "Hook for functions attaching themselves to `S-up'.
  17131. This one runs after all other options except shift-select have been excluded.
  17132. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17133. (defvar org-shiftdown-hook nil
  17134. "Hook for functions attaching themselves to `S-down'.
  17135. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17136. (defvar org-shiftdown-final-hook nil
  17137. "Hook for functions attaching themselves to `S-down'.
  17138. This one runs after all other options except shift-select have been excluded.
  17139. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17140. (defvar org-shiftleft-hook nil
  17141. "Hook for functions attaching themselves to `S-left'.
  17142. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17143. (defvar org-shiftleft-final-hook nil
  17144. "Hook for functions attaching themselves to `S-left'.
  17145. This one runs after all other options except shift-select have been excluded.
  17146. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17147. (defvar org-shiftright-hook nil
  17148. "Hook for functions attaching themselves to `S-right'.
  17149. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17150. (defvar org-shiftright-final-hook nil
  17151. "Hook for functions attaching themselves to `S-right'.
  17152. This one runs after all other options except shift-select have been excluded.
  17153. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17154. (defun org-modifier-cursor-error ()
  17155. "Throw an error, a modified cursor command was applied in wrong context."
  17156. (error "This command is active in special context like tables, headlines or items"))
  17157. (defun org-shiftselect-error ()
  17158. "Throw an error because Shift-Cursor command was applied in wrong context."
  17159. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17160. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17161. (error "This command works only in special context like headlines or timestamps")))
  17162. (defun org-call-for-shift-select (cmd)
  17163. (let ((this-command-keys-shift-translated t))
  17164. (call-interactively cmd)))
  17165. (defun org-shifttab (&optional arg)
  17166. "Global visibility cycling or move to previous table field.
  17167. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17168. on context.
  17169. See the individual commands for more information."
  17170. (interactive "P")
  17171. (cond
  17172. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17173. ((integerp arg)
  17174. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17175. (message "Content view to level: %d" arg)
  17176. (org-content (prefix-numeric-value arg2))
  17177. (setq org-cycle-global-status 'overview)))
  17178. (t (call-interactively 'org-global-cycle))))
  17179. (defun org-shiftmetaleft ()
  17180. "Promote subtree or delete table column.
  17181. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17182. `org-table-delete-column', depending on context. See the
  17183. individual commands for more information."
  17184. (interactive)
  17185. (cond
  17186. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17187. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17188. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17189. ((if (not (org-region-active-p)) (org-at-item-p)
  17190. (save-excursion (goto-char (region-beginning))
  17191. (org-at-item-p)))
  17192. (call-interactively 'org-outdent-item-tree))
  17193. (t (org-modifier-cursor-error))))
  17194. (defun org-shiftmetaright ()
  17195. "Demote subtree or insert table column.
  17196. Calls `org-demote-subtree', `org-indent-item-tree', or
  17197. `org-table-insert-column', depending on context. See the
  17198. individual commands for more information."
  17199. (interactive)
  17200. (cond
  17201. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17202. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17203. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17204. ((if (not (org-region-active-p)) (org-at-item-p)
  17205. (save-excursion (goto-char (region-beginning))
  17206. (org-at-item-p)))
  17207. (call-interactively 'org-indent-item-tree))
  17208. (t (org-modifier-cursor-error))))
  17209. (defun org-shiftmetaup (&optional arg)
  17210. "Move subtree up or kill table row.
  17211. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17212. `org-move-item-up' or `org-timestamp-up', depending on context.
  17213. See the individual commands for more information."
  17214. (interactive "P")
  17215. (cond
  17216. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17217. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17218. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17219. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17220. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17221. (call-interactively 'org-timestamp-up)))
  17222. (t (org-modifier-cursor-error))))
  17223. (defun org-shiftmetadown (&optional arg)
  17224. "Move subtree down or insert table row.
  17225. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17226. `org-move-item-down' or `org-timestamp-up', depending on context.
  17227. See the individual commands for more information."
  17228. (interactive "P")
  17229. (cond
  17230. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17231. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17232. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17233. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17234. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17235. (call-interactively 'org-timestamp-down)))
  17236. (t (org-modifier-cursor-error))))
  17237. (defsubst org-hidden-tree-error ()
  17238. (error
  17239. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17240. (defun org-metaleft (&optional arg)
  17241. "Promote heading or move table column to left.
  17242. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17243. With no specific context, calls the Emacs default `backward-word'.
  17244. See the individual commands for more information."
  17245. (interactive "P")
  17246. (cond
  17247. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17248. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17249. ((org-with-limited-levels
  17250. (or (org-at-heading-p)
  17251. (and (org-region-active-p)
  17252. (save-excursion
  17253. (goto-char (region-beginning))
  17254. (org-at-heading-p)))))
  17255. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17256. (call-interactively 'org-do-promote))
  17257. ;; At an inline task.
  17258. ((org-at-heading-p)
  17259. (call-interactively 'org-inlinetask-promote))
  17260. ((or (org-at-item-p)
  17261. (and (org-region-active-p)
  17262. (save-excursion
  17263. (goto-char (region-beginning))
  17264. (org-at-item-p))))
  17265. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17266. (call-interactively 'org-outdent-item))
  17267. (t (call-interactively 'backward-word))))
  17268. (defun org-metaright (&optional arg)
  17269. "Demote a subtree, a list item or move table column to right.
  17270. In front of a drawer or a block keyword, indent it correctly.
  17271. With no specific context, calls the Emacs default `forward-word'.
  17272. See the individual commands for more information."
  17273. (interactive "P")
  17274. (cond
  17275. ((run-hook-with-args-until-success 'org-metaright-hook))
  17276. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17277. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17278. ((org-at-block-p) (call-interactively 'org-indent-block))
  17279. ((org-with-limited-levels
  17280. (or (org-at-heading-p)
  17281. (and (org-region-active-p)
  17282. (save-excursion
  17283. (goto-char (region-beginning))
  17284. (org-at-heading-p)))))
  17285. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17286. (call-interactively 'org-do-demote))
  17287. ;; At an inline task.
  17288. ((org-at-heading-p)
  17289. (call-interactively 'org-inlinetask-demote))
  17290. ((or (org-at-item-p)
  17291. (and (org-region-active-p)
  17292. (save-excursion
  17293. (goto-char (region-beginning))
  17294. (org-at-item-p))))
  17295. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17296. (call-interactively 'org-indent-item))
  17297. (t (call-interactively 'forward-word))))
  17298. (defun org-check-for-hidden (what)
  17299. "Check if there are hidden headlines/items in the current visual line.
  17300. WHAT can be either `headlines' or `items'. If the current line is
  17301. an outline or item heading and it has a folded subtree below it,
  17302. this function returns t, nil otherwise."
  17303. (let ((re (cond
  17304. ((eq what 'headlines) org-outline-regexp-bol)
  17305. ((eq what 'items) (org-item-beginning-re))
  17306. (t (error "This should not happen"))))
  17307. beg end)
  17308. (save-excursion
  17309. (catch 'exit
  17310. (unless (org-region-active-p)
  17311. (setq beg (point-at-bol))
  17312. (beginning-of-line 2)
  17313. (while (and (not (eobp)) ;; this is like `next-line'
  17314. (get-char-property (1- (point)) 'invisible))
  17315. (beginning-of-line 2))
  17316. (setq end (point))
  17317. (goto-char beg)
  17318. (goto-char (point-at-eol))
  17319. (setq end (max end (point)))
  17320. (while (re-search-forward re end t)
  17321. (if (get-char-property (match-beginning 0) 'invisible)
  17322. (throw 'exit t))))
  17323. nil))))
  17324. (autoload 'org-element-at-point "org-element")
  17325. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  17326. (declare-function org-element-type "org-element" (element))
  17327. (declare-function org-element-context "org-element" ())
  17328. (declare-function org-element-contents "org-element" (element))
  17329. (declare-function org-element-property "org-element" (property element))
  17330. (declare-function org-element-paragraph-parser "org-element" (limit))
  17331. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  17332. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  17333. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  17334. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  17335. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  17336. (defun org-metaup (&optional arg)
  17337. "Move subtree up or move table row up.
  17338. Calls `org-move-subtree-up' or `org-table-move-row' or
  17339. `org-move-item-up', depending on context. See the individual commands
  17340. for more information."
  17341. (interactive "P")
  17342. (cond
  17343. ((run-hook-with-args-until-success 'org-metaup-hook))
  17344. ((org-region-active-p)
  17345. (let* ((a (min (region-beginning) (region-end)))
  17346. (b (1- (max (region-beginning) (region-end))))
  17347. (c (save-excursion (goto-char a)
  17348. (move-beginning-of-line 0)))
  17349. (d (save-excursion (goto-char a)
  17350. (move-end-of-line 0) (point))))
  17351. (transpose-regions a b c d)
  17352. (goto-char c)))
  17353. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17354. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17355. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17356. (t (org-drag-element-backward))))
  17357. (defun org-metadown (&optional arg)
  17358. "Move subtree down or move table row down.
  17359. Calls `org-move-subtree-down' or `org-table-move-row' or
  17360. `org-move-item-down', depending on context. See the individual
  17361. commands for more information."
  17362. (interactive "P")
  17363. (cond
  17364. ((run-hook-with-args-until-success 'org-metadown-hook))
  17365. ((org-region-active-p)
  17366. (let* ((a (min (region-beginning) (region-end)))
  17367. (b (max (region-beginning) (region-end)))
  17368. (c (save-excursion (goto-char b)
  17369. (move-beginning-of-line 1)))
  17370. (d (save-excursion (goto-char b)
  17371. (move-end-of-line 1) (1+ (point)))))
  17372. (transpose-regions a b c d)
  17373. (goto-char d)))
  17374. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17375. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17376. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17377. (t (org-drag-element-forward))))
  17378. (defun org-shiftup (&optional arg)
  17379. "Increase item in timestamp or increase priority of current headline.
  17380. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17381. depending on context. See the individual commands for more information."
  17382. (interactive "P")
  17383. (cond
  17384. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17385. ((and org-support-shift-select (org-region-active-p))
  17386. (org-call-for-shift-select 'previous-line))
  17387. ((org-at-timestamp-p t)
  17388. (call-interactively (if org-edit-timestamp-down-means-later
  17389. 'org-timestamp-down 'org-timestamp-up)))
  17390. ((and (not (eq org-support-shift-select 'always))
  17391. org-enable-priority-commands
  17392. (org-at-heading-p))
  17393. (call-interactively 'org-priority-up))
  17394. ((and (not org-support-shift-select) (org-at-item-p))
  17395. (call-interactively 'org-previous-item))
  17396. ((org-clocktable-try-shift 'up arg))
  17397. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17398. (org-support-shift-select
  17399. (org-call-for-shift-select 'previous-line))
  17400. (t (org-shiftselect-error))))
  17401. (defun org-shiftdown (&optional arg)
  17402. "Decrease item in timestamp or decrease priority of current headline.
  17403. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17404. depending on context. See the individual commands for more information."
  17405. (interactive "P")
  17406. (cond
  17407. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17408. ((and org-support-shift-select (org-region-active-p))
  17409. (org-call-for-shift-select 'next-line))
  17410. ((org-at-timestamp-p t)
  17411. (call-interactively (if org-edit-timestamp-down-means-later
  17412. 'org-timestamp-up 'org-timestamp-down)))
  17413. ((and (not (eq org-support-shift-select 'always))
  17414. org-enable-priority-commands
  17415. (org-at-heading-p))
  17416. (call-interactively 'org-priority-down))
  17417. ((and (not org-support-shift-select) (org-at-item-p))
  17418. (call-interactively 'org-next-item))
  17419. ((org-clocktable-try-shift 'down arg))
  17420. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17421. (org-support-shift-select
  17422. (org-call-for-shift-select 'next-line))
  17423. (t (org-shiftselect-error))))
  17424. (defun org-shiftright (&optional arg)
  17425. "Cycle the thing at point or in the current line, depending on context.
  17426. Depending on context, this does one of the following:
  17427. - switch a timestamp at point one day into the future
  17428. - on a headline, switch to the next TODO keyword.
  17429. - on an item, switch entire list to the next bullet type
  17430. - on a property line, switch to the next allowed value
  17431. - on a clocktable definition line, move time block into the future"
  17432. (interactive "P")
  17433. (cond
  17434. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17435. ((and org-support-shift-select (org-region-active-p))
  17436. (org-call-for-shift-select 'forward-char))
  17437. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17438. ((and (not (eq org-support-shift-select 'always))
  17439. (org-at-heading-p))
  17440. (let ((org-inhibit-logging
  17441. (not org-treat-S-cursor-todo-selection-as-state-change))
  17442. (org-inhibit-blocking
  17443. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17444. (org-call-with-arg 'org-todo 'right)))
  17445. ((or (and org-support-shift-select
  17446. (not (eq org-support-shift-select 'always))
  17447. (org-at-item-bullet-p))
  17448. (and (not org-support-shift-select) (org-at-item-p)))
  17449. (org-call-with-arg 'org-cycle-list-bullet nil))
  17450. ((and (not (eq org-support-shift-select 'always))
  17451. (org-at-property-p))
  17452. (call-interactively 'org-property-next-allowed-value))
  17453. ((org-clocktable-try-shift 'right arg))
  17454. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17455. (org-support-shift-select
  17456. (org-call-for-shift-select 'forward-char))
  17457. (t (org-shiftselect-error))))
  17458. (defun org-shiftleft (&optional arg)
  17459. "Cycle the thing at point or in the current line, depending on context.
  17460. Depending on context, this does one of the following:
  17461. - switch a timestamp at point one day into the past
  17462. - on a headline, switch to the previous TODO keyword.
  17463. - on an item, switch entire list to the previous bullet type
  17464. - on a property line, switch to the previous allowed value
  17465. - on a clocktable definition line, move time block into the past"
  17466. (interactive "P")
  17467. (cond
  17468. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17469. ((and org-support-shift-select (org-region-active-p))
  17470. (org-call-for-shift-select 'backward-char))
  17471. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17472. ((and (not (eq org-support-shift-select 'always))
  17473. (org-at-heading-p))
  17474. (let ((org-inhibit-logging
  17475. (not org-treat-S-cursor-todo-selection-as-state-change))
  17476. (org-inhibit-blocking
  17477. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17478. (org-call-with-arg 'org-todo 'left)))
  17479. ((or (and org-support-shift-select
  17480. (not (eq org-support-shift-select 'always))
  17481. (org-at-item-bullet-p))
  17482. (and (not org-support-shift-select) (org-at-item-p)))
  17483. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17484. ((and (not (eq org-support-shift-select 'always))
  17485. (org-at-property-p))
  17486. (call-interactively 'org-property-previous-allowed-value))
  17487. ((org-clocktable-try-shift 'left arg))
  17488. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17489. (org-support-shift-select
  17490. (org-call-for-shift-select 'backward-char))
  17491. (t (org-shiftselect-error))))
  17492. (defun org-shiftcontrolright ()
  17493. "Switch to next TODO set."
  17494. (interactive)
  17495. (cond
  17496. ((and org-support-shift-select (org-region-active-p))
  17497. (org-call-for-shift-select 'forward-word))
  17498. ((and (not (eq org-support-shift-select 'always))
  17499. (org-at-heading-p))
  17500. (org-call-with-arg 'org-todo 'nextset))
  17501. (org-support-shift-select
  17502. (org-call-for-shift-select 'forward-word))
  17503. (t (org-shiftselect-error))))
  17504. (defun org-shiftcontrolleft ()
  17505. "Switch to previous TODO set."
  17506. (interactive)
  17507. (cond
  17508. ((and org-support-shift-select (org-region-active-p))
  17509. (org-call-for-shift-select 'backward-word))
  17510. ((and (not (eq org-support-shift-select 'always))
  17511. (org-at-heading-p))
  17512. (org-call-with-arg 'org-todo 'previousset))
  17513. (org-support-shift-select
  17514. (org-call-for-shift-select 'backward-word))
  17515. (t (org-shiftselect-error))))
  17516. (defun org-shiftcontrolup ()
  17517. "Change timestamps synchronously up in CLOCK log lines."
  17518. (interactive)
  17519. (cond ((and (not org-support-shift-select)
  17520. (org-at-clock-log-p)
  17521. (org-at-timestamp-p t))
  17522. (org-clock-timestamps-up))
  17523. (t (org-shiftselect-error))))
  17524. (defun org-shiftcontroldown ()
  17525. "Change timestamps synchronously down in CLOCK log lines."
  17526. (interactive)
  17527. (cond ((and (not org-support-shift-select)
  17528. (org-at-clock-log-p)
  17529. (org-at-timestamp-p t))
  17530. (org-clock-timestamps-down))
  17531. (t (org-shiftselect-error))))
  17532. (defun org-ctrl-c-ret ()
  17533. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17534. (interactive)
  17535. (cond
  17536. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17537. (t (call-interactively 'org-insert-heading))))
  17538. (defun org-find-visible ()
  17539. (let ((s (point)))
  17540. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17541. (get-char-property s 'invisible)))
  17542. s))
  17543. (defun org-find-invisible ()
  17544. (let ((s (point)))
  17545. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17546. (not (get-char-property s 'invisible))))
  17547. s))
  17548. (defun org-copy-visible (beg end)
  17549. "Copy the visible parts of the region."
  17550. (interactive "r")
  17551. (let (snippets s)
  17552. (save-excursion
  17553. (save-restriction
  17554. (narrow-to-region beg end)
  17555. (setq s (goto-char (point-min)))
  17556. (while (not (= (point) (point-max)))
  17557. (goto-char (org-find-invisible))
  17558. (push (buffer-substring s (point)) snippets)
  17559. (setq s (goto-char (org-find-visible))))))
  17560. (kill-new (apply 'concat (nreverse snippets)))))
  17561. (defun org-copy-special ()
  17562. "Copy region in table or copy current subtree.
  17563. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17564. See the individual commands for more information."
  17565. (interactive)
  17566. (call-interactively
  17567. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17568. (defun org-cut-special ()
  17569. "Cut region in table or cut current subtree.
  17570. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17571. See the individual commands for more information."
  17572. (interactive)
  17573. (call-interactively
  17574. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17575. (defun org-paste-special (arg)
  17576. "Paste rectangular region into table, or past subtree relative to level.
  17577. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17578. See the individual commands for more information."
  17579. (interactive "P")
  17580. (if (org-at-table-p)
  17581. (org-table-paste-rectangle)
  17582. (org-paste-subtree arg)))
  17583. (defun org-edit-special (&optional arg)
  17584. "Call a special editor for the stuff at point.
  17585. When at a table, call the formula editor with `org-table-edit-formulas'.
  17586. When at the first line of an src example, call `org-edit-src-code'.
  17587. When in an #+include line, visit the include file. Otherwise call
  17588. `ffap' to visit the file at point."
  17589. (interactive)
  17590. ;; possibly prep session before editing source
  17591. (when arg
  17592. (let* ((info (org-babel-get-src-block-info))
  17593. (lang (nth 0 info))
  17594. (params (nth 2 info))
  17595. (session (cdr (assoc :session params))))
  17596. (when (and info session) ;; we are in a source-code block with a session
  17597. (funcall
  17598. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17599. (cond ;; proceed with `org-edit-special'
  17600. ((save-excursion
  17601. (beginning-of-line 1)
  17602. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17603. (find-file (org-trim (match-string 1))))
  17604. ((org-edit-src-code))
  17605. ((org-edit-fixed-width-region))
  17606. ((org-at-table.el-p)
  17607. (org-edit-src-code))
  17608. ((or (org-at-table-p)
  17609. (save-excursion
  17610. (beginning-of-line 1)
  17611. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17612. (call-interactively 'org-table-edit-formulas))
  17613. (t (call-interactively 'ffap))))
  17614. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17615. (defun org-ctrl-c-ctrl-c (&optional arg)
  17616. "Set tags in headline, or update according to changed information at point.
  17617. This command does many different things, depending on context:
  17618. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17619. this is what we do.
  17620. - If the cursor is on a statistics cookie, update it.
  17621. - If the cursor is in a headline, prompt for tags and insert them
  17622. into the current line, aligned to `org-tags-column'. When called
  17623. with prefix arg, realign all tags in the current buffer.
  17624. - If the cursor is in one of the special #+KEYWORD lines, this
  17625. triggers scanning the buffer for these lines and updating the
  17626. information.
  17627. - If the cursor is inside a table, realign the table. This command
  17628. works even if the automatic table editor has been turned off.
  17629. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17630. the entire table.
  17631. - If the cursor is at a footnote reference or definition, jump to
  17632. the corresponding definition or references, respectively.
  17633. - If the cursor is a the beginning of a dynamic block, update it.
  17634. - If the current buffer is a capture buffer, close note and file it.
  17635. - If the cursor is on a <<<target>>>, update radio targets and
  17636. corresponding links in this buffer.
  17637. - If the cursor is on a numbered item in a plain list, renumber the
  17638. ordered list.
  17639. - If the cursor is on a checkbox, toggle it.
  17640. - If the cursor is on a code block, evaluate it. The variable
  17641. `org-confirm-babel-evaluate' can be used to control prompting
  17642. before code block evaluation, by default every code block
  17643. evaluation requires confirmation. Code block evaluation can be
  17644. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17645. (interactive "P")
  17646. (let ((org-enable-table-editor t))
  17647. (cond
  17648. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17649. org-occur-highlights
  17650. org-latex-fragment-image-overlays)
  17651. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17652. (org-remove-occur-highlights)
  17653. (org-remove-latex-fragment-image-overlays)
  17654. (message "Temporary highlights/overlays removed from current buffer"))
  17655. ((and (local-variable-p 'org-finish-function (current-buffer))
  17656. (fboundp org-finish-function))
  17657. (funcall org-finish-function))
  17658. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17659. ((org-in-regexp org-ts-regexp-both)
  17660. (org-timestamp-change 0 'day))
  17661. ((or (looking-at org-property-start-re)
  17662. (org-at-property-p))
  17663. (call-interactively 'org-property-action))
  17664. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17665. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17666. (or (org-at-heading-p) (org-at-item-p)))
  17667. (call-interactively 'org-update-statistics-cookies))
  17668. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17669. ((org-at-table.el-p)
  17670. (message "Use C-c ' to edit table.el tables"))
  17671. ((org-at-table-p)
  17672. (org-table-maybe-eval-formula)
  17673. (if arg
  17674. (call-interactively 'org-table-recalculate)
  17675. (org-table-maybe-recalculate-line))
  17676. (call-interactively 'org-table-align)
  17677. (orgtbl-send-table 'maybe))
  17678. ((or (org-footnote-at-reference-p)
  17679. (org-footnote-at-definition-p))
  17680. (call-interactively 'org-footnote-action))
  17681. ((org-at-item-checkbox-p)
  17682. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17683. ;; list only if at top item.
  17684. (let* ((cbox (match-string 1))
  17685. (struct (org-list-struct))
  17686. (old-struct (copy-tree struct))
  17687. (parents (org-list-parents-alist struct))
  17688. (orderedp (org-entry-get nil "ORDERED"))
  17689. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17690. block-item)
  17691. ;; Use a light version of `org-toggle-checkbox' to avoid
  17692. ;; computing list structure twice.
  17693. (let ((new-box (cond
  17694. ((equal arg '(16)) "[-]")
  17695. ((equal arg '(4)) nil)
  17696. ((equal "[X]" cbox) "[ ]")
  17697. (t "[X]"))))
  17698. (if (and firstp arg)
  17699. ;; If at first item of sub-list, remove check-box from
  17700. ;; every item at the same level.
  17701. (mapc
  17702. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17703. (org-list-get-all-items
  17704. (point-at-bol) struct (org-list-prevs-alist struct)))
  17705. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17706. ;; Replicate `org-list-write-struct', while grabbing a return
  17707. ;; value from `org-list-struct-fix-box'.
  17708. (org-list-struct-fix-ind struct parents 2)
  17709. (org-list-struct-fix-item-end struct)
  17710. (let ((prevs (org-list-prevs-alist struct)))
  17711. (org-list-struct-fix-bul struct prevs)
  17712. (org-list-struct-fix-ind struct parents)
  17713. (setq block-item
  17714. (org-list-struct-fix-box struct parents prevs orderedp)))
  17715. (org-list-struct-apply-struct struct old-struct)
  17716. (org-update-checkbox-count-maybe)
  17717. (when block-item
  17718. (message
  17719. "Checkboxes were removed due to unchecked box at line %d"
  17720. (org-current-line block-item)))
  17721. (when firstp (org-list-send-list 'maybe))))
  17722. ((org-at-item-p)
  17723. ;; Cursor at an item: repair list. Do checkbox related actions
  17724. ;; only if function was called with an argument. Send list only
  17725. ;; if at top item.
  17726. (let* ((struct (org-list-struct))
  17727. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17728. old-struct)
  17729. (when arg
  17730. (setq old-struct (copy-tree struct))
  17731. (if firstp
  17732. ;; If at first item of sub-list, add check-box to every
  17733. ;; item at the same level.
  17734. (mapc
  17735. (lambda (pos)
  17736. (unless (org-list-get-checkbox pos struct)
  17737. (org-list-set-checkbox pos struct "[ ]")))
  17738. (org-list-get-all-items
  17739. (point-at-bol) struct (org-list-prevs-alist struct)))
  17740. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17741. (org-list-write-struct
  17742. struct (org-list-parents-alist struct) old-struct)
  17743. (when arg (org-update-checkbox-count-maybe))
  17744. (when firstp (org-list-send-list 'maybe))))
  17745. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17746. ;; Dynamic block
  17747. (beginning-of-line 1)
  17748. (save-excursion (org-update-dblock)))
  17749. ((save-excursion
  17750. (let ((case-fold-search t))
  17751. (beginning-of-line 1)
  17752. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17753. (cond
  17754. ((or (equal (match-string 1) "TBLFM")
  17755. (equal (match-string 1) "tblfm"))
  17756. ;; Recalculate the table before this line
  17757. (save-excursion
  17758. (beginning-of-line 1)
  17759. (skip-chars-backward " \r\n\t")
  17760. (if (org-at-table-p)
  17761. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17762. (t
  17763. (let ((org-inhibit-startup-visibility-stuff t)
  17764. (org-startup-align-all-tables nil))
  17765. (when (boundp 'org-table-coordinate-overlays)
  17766. (mapc 'delete-overlay org-table-coordinate-overlays)
  17767. (setq org-table-coordinate-overlays nil))
  17768. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17769. (message "Local setup has been refreshed"))))
  17770. ((org-clock-update-time-maybe))
  17771. (t
  17772. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17773. (error "C-c C-c can do nothing useful at this location"))))))
  17774. (defun org-mode-restart ()
  17775. "Restart Org-mode, to scan again for special lines.
  17776. Also updates the keyword regular expressions."
  17777. (interactive)
  17778. (org-mode)
  17779. (message "Org-mode restarted"))
  17780. (defun org-kill-note-or-show-branches ()
  17781. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17782. (interactive)
  17783. (if (not org-finish-function)
  17784. (progn
  17785. (hide-subtree)
  17786. (call-interactively 'show-branches))
  17787. (let ((org-note-abort t))
  17788. (funcall org-finish-function))))
  17789. (defun org-return (&optional indent)
  17790. "Goto next table row or insert a newline.
  17791. Calls `org-table-next-row' or `newline', depending on context.
  17792. See the individual commands for more information."
  17793. (interactive)
  17794. (let (org-ts-what)
  17795. (cond
  17796. ((or (bobp) (org-in-src-block-p))
  17797. (if indent (newline-and-indent) (newline)))
  17798. ((org-at-table-p)
  17799. (org-table-justify-field-maybe)
  17800. (call-interactively 'org-table-next-row))
  17801. ;; when `newline-and-indent' is called within a list, make sure
  17802. ;; text moved stays inside the item.
  17803. ((and (org-in-item-p) indent)
  17804. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17805. (progn
  17806. (save-match-data (newline))
  17807. (org-indent-line-to (length (match-string 0))))
  17808. (let ((ind (org-get-indentation)))
  17809. (newline)
  17810. (if (org-looking-back org-list-end-re)
  17811. (org-indent-line)
  17812. (org-indent-line-to ind)))))
  17813. ((and org-return-follows-link
  17814. (org-at-timestamp-p t)
  17815. (not (eq org-ts-what 'after)))
  17816. (org-follow-timestamp-link))
  17817. ((and org-return-follows-link
  17818. (let ((tprop (get-text-property (point) 'face)))
  17819. (or (eq tprop 'org-link)
  17820. (and (listp tprop) (memq 'org-link tprop)))))
  17821. (call-interactively 'org-open-at-point))
  17822. ((and (org-at-heading-p)
  17823. (looking-at
  17824. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17825. (org-show-entry)
  17826. (end-of-line 1)
  17827. (newline))
  17828. (t (if indent (newline-and-indent) (newline))))))
  17829. (defun org-return-indent ()
  17830. "Goto next table row or insert a newline and indent.
  17831. Calls `org-table-next-row' or `newline-and-indent', depending on
  17832. context. See the individual commands for more information."
  17833. (interactive)
  17834. (org-return t))
  17835. (defun org-ctrl-c-star ()
  17836. "Compute table, or change heading status of lines.
  17837. Calls `org-table-recalculate' or `org-toggle-heading',
  17838. depending on context."
  17839. (interactive)
  17840. (cond
  17841. ((org-at-table-p)
  17842. (call-interactively 'org-table-recalculate))
  17843. (t
  17844. ;; Convert all lines in region to list items
  17845. (call-interactively 'org-toggle-heading))))
  17846. (defun org-ctrl-c-minus ()
  17847. "Insert separator line in table or modify bullet status of line.
  17848. Also turns a plain line or a region of lines into list items.
  17849. Calls `org-table-insert-hline', `org-toggle-item', or
  17850. `org-cycle-list-bullet', depending on context."
  17851. (interactive)
  17852. (cond
  17853. ((org-at-table-p)
  17854. (call-interactively 'org-table-insert-hline))
  17855. ((org-region-active-p)
  17856. (call-interactively 'org-toggle-item))
  17857. ((org-in-item-p)
  17858. (call-interactively 'org-cycle-list-bullet))
  17859. (t
  17860. (call-interactively 'org-toggle-item))))
  17861. (defun org-toggle-item (arg)
  17862. "Convert headings or normal lines to items, items to normal lines.
  17863. If there is no active region, only the current line is considered.
  17864. If the first non blank line in the region is an headline, convert
  17865. all headlines to items, shifting text accordingly.
  17866. If it is an item, convert all items to normal lines.
  17867. If it is normal text, change region into an item. With a prefix
  17868. argument ARG, change each line in region into an item."
  17869. (interactive "P")
  17870. (let ((shift-text
  17871. (function
  17872. ;; Shift text in current section to IND, from point to END.
  17873. ;; The function leaves point to END line.
  17874. (lambda (ind end)
  17875. (let ((min-i 1000) (end (copy-marker end)))
  17876. ;; First determine the minimum indentation (MIN-I) of
  17877. ;; the text.
  17878. (save-excursion
  17879. (catch 'exit
  17880. (while (< (point) end)
  17881. (let ((i (org-get-indentation)))
  17882. (cond
  17883. ;; Skip blank lines and inline tasks.
  17884. ((looking-at "^[ \t]*$"))
  17885. ((looking-at org-outline-regexp-bol))
  17886. ;; We can't find less than 0 indentation.
  17887. ((zerop i) (throw 'exit (setq min-i 0)))
  17888. ((< i min-i) (setq min-i i))))
  17889. (forward-line))))
  17890. ;; Then indent each line so that a line indented to
  17891. ;; MIN-I becomes indented to IND. Ignore blank lines
  17892. ;; and inline tasks in the process.
  17893. (let ((delta (- ind min-i)))
  17894. (while (< (point) end)
  17895. (unless (or (looking-at "^[ \t]*$")
  17896. (looking-at org-outline-regexp-bol))
  17897. (org-indent-line-to (+ (org-get-indentation) delta)))
  17898. (forward-line)))))))
  17899. (skip-blanks
  17900. (function
  17901. ;; Return beginning of first non-blank line, starting from
  17902. ;; line at POS.
  17903. (lambda (pos)
  17904. (save-excursion
  17905. (goto-char pos)
  17906. (skip-chars-forward " \r\t\n")
  17907. (point-at-bol)))))
  17908. beg end)
  17909. ;; Determine boundaries of changes.
  17910. (if (org-region-active-p)
  17911. (setq beg (funcall skip-blanks (region-beginning))
  17912. end (copy-marker (region-end)))
  17913. (setq beg (funcall skip-blanks (point-at-bol))
  17914. end (copy-marker (point-at-eol))))
  17915. ;; Depending on the starting line, choose an action on the text
  17916. ;; between BEG and END.
  17917. (org-with-limited-levels
  17918. (save-excursion
  17919. (goto-char beg)
  17920. (cond
  17921. ;; Case 1. Start at an item: de-itemize. Note that it only
  17922. ;; happens when a region is active: `org-ctrl-c-minus'
  17923. ;; would call `org-cycle-list-bullet' otherwise.
  17924. ((org-at-item-p)
  17925. (while (< (point) end)
  17926. (when (org-at-item-p)
  17927. (skip-chars-forward " \t")
  17928. (delete-region (point) (match-end 0)))
  17929. (forward-line)))
  17930. ;; Case 2. Start at an heading: convert to items.
  17931. ((org-at-heading-p)
  17932. (let* ((bul (org-list-bullet-string "-"))
  17933. (bul-len (length bul))
  17934. ;; Indentation of the first heading. It should be
  17935. ;; relative to the indentation of its parent, if any.
  17936. (start-ind (save-excursion
  17937. (cond
  17938. ((not org-adapt-indentation) 0)
  17939. ((not (outline-previous-heading)) 0)
  17940. (t (length (match-string 0))))))
  17941. ;; Level of first heading. Further headings will be
  17942. ;; compared to it to determine hierarchy in the list.
  17943. (ref-level (org-reduced-level (org-outline-level))))
  17944. (while (< (point) end)
  17945. (let* ((level (org-reduced-level (org-outline-level)))
  17946. (delta (max 0 (- level ref-level))))
  17947. ;; If current headline is less indented than the first
  17948. ;; one, set it as reference, in order to preserve
  17949. ;; subtrees.
  17950. (when (< level ref-level) (setq ref-level level))
  17951. (replace-match bul t t)
  17952. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17953. ;; Ensure all text down to END (or SECTION-END) belongs
  17954. ;; to the newly created item.
  17955. (let ((section-end (save-excursion
  17956. (or (outline-next-heading) (point)))))
  17957. (forward-line)
  17958. (funcall shift-text
  17959. (+ start-ind (* (1+ delta) bul-len))
  17960. (min end section-end)))))))
  17961. ;; Case 3. Normal line with ARG: turn each non-item line into
  17962. ;; an item.
  17963. (arg
  17964. (while (< (point) end)
  17965. (unless (or (org-at-heading-p) (org-at-item-p))
  17966. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17967. (replace-match
  17968. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17969. (forward-line)))
  17970. ;; Case 4. Normal line without ARG: make the first line of
  17971. ;; region an item, and shift indentation of others
  17972. ;; lines to set them as item's body.
  17973. (t (let* ((bul (org-list-bullet-string "-"))
  17974. (bul-len (length bul))
  17975. (ref-ind (org-get-indentation)))
  17976. (skip-chars-forward " \t")
  17977. (insert bul)
  17978. (forward-line)
  17979. (while (< (point) end)
  17980. ;; Ensure that lines less indented than first one
  17981. ;; still get included in item body.
  17982. (funcall shift-text
  17983. (+ ref-ind bul-len)
  17984. (min end (save-excursion (or (outline-next-heading)
  17985. (point)))))
  17986. (forward-line)))))))))
  17987. (defun org-toggle-heading (&optional nstars)
  17988. "Convert headings to normal text, or items or text to headings.
  17989. If there is no active region, only the current line is considered.
  17990. With a \\[universal-argument] prefix, convert the whole list at
  17991. point into heading.
  17992. In a region:
  17993. - If the first non blank line is an headline, remove the stars
  17994. from all headlines in the region.
  17995. - If it is a normal line turn each and every normal line (i.e. not an
  17996. heading or an item) in the region into a heading.
  17997. - If it is a plain list item, turn all plain list items into headings.
  17998. When converting a line into a heading, the number of stars is chosen
  17999. such that the lines become children of the current entry. However,
  18000. when a prefix argument is given, its value determines the number of
  18001. stars to add."
  18002. (interactive "P")
  18003. (let ((skip-blanks
  18004. (function
  18005. ;; Return beginning of first non-blank line, starting from
  18006. ;; line at POS.
  18007. (lambda (pos)
  18008. (save-excursion
  18009. (goto-char pos)
  18010. (while (org-at-comment-p) (forward-line))
  18011. (skip-chars-forward " \r\t\n")
  18012. (point-at-bol)))))
  18013. beg end toggled)
  18014. ;; Determine boundaries of changes. If a universal prefix has
  18015. ;; been given, put the list in a region. If region ends at a bol,
  18016. ;; do not consider the last line to be in the region.
  18017. (when (and current-prefix-arg (org-at-item-p))
  18018. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18019. (org-mark-element))
  18020. (if (org-region-active-p)
  18021. (setq beg (funcall skip-blanks (region-beginning))
  18022. end (copy-marker (save-excursion
  18023. (goto-char (region-end))
  18024. (if (bolp) (point) (point-at-eol)))))
  18025. (setq beg (funcall skip-blanks (point-at-bol))
  18026. end (copy-marker (point-at-eol))))
  18027. ;; Ensure inline tasks don't count as headings.
  18028. (org-with-limited-levels
  18029. (save-excursion
  18030. (goto-char beg)
  18031. (cond
  18032. ;; Case 1. Started at an heading: de-star headings.
  18033. ((org-at-heading-p)
  18034. (while (< (point) end)
  18035. (when (org-at-heading-p t)
  18036. (looking-at org-outline-regexp) (replace-match "")
  18037. (setq toggled t))
  18038. (forward-line)))
  18039. ;; Case 2. Started at an item: change items into headlines.
  18040. ;; One star will be added by `org-list-to-subtree'.
  18041. ((org-at-item-p)
  18042. (let* ((stars (make-string
  18043. (if nstars
  18044. ;; subtract the star that will be added again by
  18045. ;; `org-list-to-subtree'
  18046. (1- (prefix-numeric-value current-prefix-arg))
  18047. (or (org-current-level) 0))
  18048. ?*))
  18049. (add-stars
  18050. (cond (nstars "") ; stars from prefix only
  18051. ((equal stars "") "") ; before first heading
  18052. (org-odd-levels-only "*") ; inside heading, odd
  18053. (t "")))) ; inside heading, oddeven
  18054. (while (< (point) end)
  18055. (when (org-at-item-p)
  18056. ;; Pay attention to cases when region ends before list.
  18057. (let* ((struct (org-list-struct))
  18058. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18059. (save-restriction
  18060. (narrow-to-region (point) list-end)
  18061. (insert
  18062. (org-list-to-subtree
  18063. (org-list-parse-list t)
  18064. '(:istart (concat stars add-stars (funcall get-stars depth))
  18065. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18066. (setq toggled t))
  18067. (forward-line))))
  18068. ;; Case 3. Started at normal text: make every line an heading,
  18069. ;; skipping headlines and items.
  18070. (t (let* ((stars (make-string
  18071. (if nstars
  18072. (prefix-numeric-value current-prefix-arg)
  18073. (or (org-current-level) 0))
  18074. ?*))
  18075. (add-stars
  18076. (cond (nstars "") ; stars from prefix only
  18077. ((equal stars "") "*") ; before first heading
  18078. (org-odd-levels-only "**") ; inside heading, odd
  18079. (t "*"))) ; inside heading, oddeven
  18080. (rpl (concat stars add-stars " ")))
  18081. (while (< (point) end)
  18082. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18083. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18084. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18085. (forward-line)))))))
  18086. (unless toggled (message "Cannot toggle heading from here"))))
  18087. (defun org-meta-return (&optional arg)
  18088. "Insert a new heading or wrap a region in a table.
  18089. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18090. See the individual commands for more information."
  18091. (interactive "P")
  18092. (cond
  18093. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18094. ((or (org-at-drawer-p) (org-at-property-p))
  18095. (newline-and-indent))
  18096. ((org-at-table-p)
  18097. (call-interactively 'org-table-wrap-region))
  18098. (t (call-interactively 'org-insert-heading))))
  18099. ;;; Menu entries
  18100. ;; Define the Org-mode menus
  18101. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18102. '("Tbl"
  18103. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18104. ["Next Field" org-cycle (org-at-table-p)]
  18105. ["Previous Field" org-shifttab (org-at-table-p)]
  18106. ["Next Row" org-return (org-at-table-p)]
  18107. "--"
  18108. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18109. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18110. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18111. "--"
  18112. ("Column"
  18113. ["Move Column Left" org-metaleft (org-at-table-p)]
  18114. ["Move Column Right" org-metaright (org-at-table-p)]
  18115. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18116. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18117. ("Row"
  18118. ["Move Row Up" org-metaup (org-at-table-p)]
  18119. ["Move Row Down" org-metadown (org-at-table-p)]
  18120. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18121. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18122. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18123. "--"
  18124. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18125. ("Rectangle"
  18126. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18127. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18128. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18129. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18130. "--"
  18131. ("Calculate"
  18132. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18133. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18134. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18135. "--"
  18136. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18137. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18138. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18139. "--"
  18140. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18141. "--"
  18142. ["Sum Column/Rectangle" org-table-sum
  18143. (or (org-at-table-p) (org-region-active-p))]
  18144. ["Which Column?" org-table-current-column (org-at-table-p)])
  18145. ["Debug Formulas"
  18146. org-table-toggle-formula-debugger
  18147. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18148. ["Show Col/Row Numbers"
  18149. org-table-toggle-coordinate-overlays
  18150. :style toggle
  18151. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18152. "--"
  18153. ["Create" org-table-create (and (not (org-at-table-p))
  18154. org-enable-table-editor)]
  18155. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18156. ["Import from File" org-table-import (not (org-at-table-p))]
  18157. ["Export to File" org-table-export (org-at-table-p)]
  18158. "--"
  18159. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18160. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18161. '("Org"
  18162. ("Show/Hide"
  18163. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18164. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18165. ["Sparse Tree..." org-sparse-tree t]
  18166. ["Reveal Context" org-reveal t]
  18167. ["Show All" show-all t]
  18168. "--"
  18169. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18170. "--"
  18171. ["New Heading" org-insert-heading t]
  18172. ("Navigate Headings"
  18173. ["Up" outline-up-heading t]
  18174. ["Next" outline-next-visible-heading t]
  18175. ["Previous" outline-previous-visible-heading t]
  18176. ["Next Same Level" outline-forward-same-level t]
  18177. ["Previous Same Level" outline-backward-same-level t]
  18178. "--"
  18179. ["Jump" org-goto t])
  18180. ("Edit Structure"
  18181. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  18182. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  18183. "--"
  18184. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  18185. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  18186. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18187. "--"
  18188. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18189. "--"
  18190. ["Copy visible text" org-copy-visible t]
  18191. "--"
  18192. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  18193. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  18194. ["Demote Heading" org-metaright (not (org-at-table-p))]
  18195. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  18196. "--"
  18197. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  18198. "--"
  18199. ["Convert to odd levels" org-convert-to-odd-levels t]
  18200. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18201. ("Editing"
  18202. ["Emphasis..." org-emphasize t]
  18203. ["Edit Source Example" org-edit-special t]
  18204. "--"
  18205. ["Footnote new/jump" org-footnote-action t]
  18206. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18207. ("Archive"
  18208. ["Archive (default method)" org-archive-subtree-default t]
  18209. "--"
  18210. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  18211. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  18212. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  18213. )
  18214. "--"
  18215. ("Hyperlinks"
  18216. ["Store Link (Global)" org-store-link t]
  18217. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18218. ["Insert Link" org-insert-link t]
  18219. ["Follow Link" org-open-at-point t]
  18220. "--"
  18221. ["Next link" org-next-link t]
  18222. ["Previous link" org-previous-link t]
  18223. "--"
  18224. ["Descriptive Links"
  18225. org-toggle-link-display
  18226. :style radio
  18227. :selected org-descriptive-links
  18228. ]
  18229. ["Literal Links"
  18230. org-toggle-link-display
  18231. :style radio
  18232. :selected (not org-descriptive-links)])
  18233. "--"
  18234. ("TODO Lists"
  18235. ["TODO/DONE/-" org-todo t]
  18236. ("Select keyword"
  18237. ["Next keyword" org-shiftright (org-at-heading-p)]
  18238. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18239. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18240. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18241. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18242. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18243. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18244. "--"
  18245. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18246. :selected org-enforce-todo-dependencies :style toggle :active t]
  18247. "Settings for tree at point"
  18248. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18249. :selected (org-entry-get nil "ORDERED")
  18250. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18251. ["Do Children parallel" org-toggle-ordered-property :style radio
  18252. :selected (not (org-entry-get nil "ORDERED"))
  18253. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18254. "--"
  18255. ["Set Priority" org-priority t]
  18256. ["Priority Up" org-shiftup t]
  18257. ["Priority Down" org-shiftdown t]
  18258. "--"
  18259. ["Get news from all feeds" org-feed-update-all t]
  18260. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18261. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18262. ("TAGS and Properties"
  18263. ["Set Tags" org-set-tags-command t]
  18264. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18265. "--"
  18266. ["Set property" org-set-property t]
  18267. ["Column view of properties" org-columns t]
  18268. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18269. ("Dates and Scheduling"
  18270. ["Timestamp" org-time-stamp t]
  18271. ["Timestamp (inactive)" org-time-stamp-inactive t]
  18272. ("Change Date"
  18273. ["1 Day Later" org-shiftright t]
  18274. ["1 Day Earlier" org-shiftleft t]
  18275. ["1 ... Later" org-shiftup t]
  18276. ["1 ... Earlier" org-shiftdown t])
  18277. ["Compute Time Range" org-evaluate-time-range t]
  18278. ["Schedule Item" org-schedule t]
  18279. ["Deadline" org-deadline t]
  18280. "--"
  18281. ["Custom time format" org-toggle-time-stamp-overlays
  18282. :style radio :selected org-display-custom-times]
  18283. "--"
  18284. ["Goto Calendar" org-goto-calendar t]
  18285. ["Date from Calendar" org-date-from-calendar t]
  18286. "--"
  18287. ["Start/Restart Timer" org-timer-start t]
  18288. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18289. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18290. ["Insert Timer String" org-timer t]
  18291. ["Insert Timer Item" org-timer-item t])
  18292. ("Logging work"
  18293. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18294. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18295. ["Clock out" org-clock-out t]
  18296. ["Clock cancel" org-clock-cancel t]
  18297. "--"
  18298. ["Mark as default task" org-clock-mark-default-task t]
  18299. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18300. ["Goto running clock" org-clock-goto t]
  18301. "--"
  18302. ["Display times" org-clock-display t]
  18303. ["Create clock table" org-clock-report t]
  18304. "--"
  18305. ["Record DONE time"
  18306. (progn (setq org-log-done (not org-log-done))
  18307. (message "Switching to %s will %s record a timestamp"
  18308. (car org-done-keywords)
  18309. (if org-log-done "automatically" "not")))
  18310. :style toggle :selected org-log-done])
  18311. "--"
  18312. ["Agenda Command..." org-agenda t]
  18313. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18314. ("File List for Agenda")
  18315. ("Special views current file"
  18316. ["TODO Tree" org-show-todo-tree t]
  18317. ["Check Deadlines" org-check-deadlines t]
  18318. ["Timeline" org-timeline t]
  18319. ["Tags/Property tree" org-match-sparse-tree t])
  18320. "--"
  18321. ["Export/Publish..." org-export t]
  18322. ("LaTeX"
  18323. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18324. :selected org-cdlatex-mode]
  18325. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18326. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18327. ["Modify math symbol" org-cdlatex-math-modify
  18328. (org-inside-LaTeX-fragment-p)]
  18329. ["Insert citation" org-reftex-citation t]
  18330. "--"
  18331. ["Template for BEAMER" (progn (require 'org-beamer)
  18332. (org-insert-beamer-options-template)) t])
  18333. "--"
  18334. ("MobileOrg"
  18335. ["Push Files and Views" org-mobile-push t]
  18336. ["Get Captured and Flagged" org-mobile-pull t]
  18337. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18338. "--"
  18339. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18340. "--"
  18341. ("Documentation"
  18342. ["Show Version" org-version t]
  18343. ["Info Documentation" org-info t])
  18344. ("Customize"
  18345. ["Browse Org Group" org-customize t]
  18346. "--"
  18347. ["Expand This Menu" org-create-customize-menu
  18348. (fboundp 'customize-menu-create)])
  18349. ["Send bug report" org-submit-bug-report t]
  18350. "--"
  18351. ("Refresh/Reload"
  18352. ["Refresh setup current buffer" org-mode-restart t]
  18353. ["Reload Org (after update)" org-reload t]
  18354. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18355. ))
  18356. (defun org-info (&optional node)
  18357. "Read documentation for Org-mode in the info system.
  18358. With optional NODE, go directly to that node."
  18359. (interactive)
  18360. (info (format "(org)%s" (or node ""))))
  18361. ;;;###autoload
  18362. (defun org-submit-bug-report ()
  18363. "Submit a bug report on Org-mode via mail.
  18364. Don't hesitate to report any problems or inaccurate documentation.
  18365. If you don't have setup sending mail from (X)Emacs, please copy the
  18366. output buffer into your mail program, as it gives us important
  18367. information about your Org-mode version and configuration."
  18368. (interactive)
  18369. (require 'reporter)
  18370. (org-load-modules-maybe)
  18371. (org-require-autoloaded-modules)
  18372. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18373. (reporter-submit-bug-report
  18374. "emacs-orgmode@gnu.org"
  18375. (org-version nil 'full)
  18376. (let (list)
  18377. (save-window-excursion
  18378. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18379. (delete-other-windows)
  18380. (erase-buffer)
  18381. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18382. We would like to add your full Org-mode and Outline configuration to the
  18383. bug report. This greatly simplifies the work of the maintainer and
  18384. other experts on the mailing list.
  18385. HOWEVER, some variables you have customized may contain private
  18386. information. The names of customers, colleagues, or friends, might
  18387. appear in the form of file names, tags, todo states, or search strings.
  18388. If you answer yes to the prompt, you might want to check and remove
  18389. such private information before sending the email.")
  18390. (add-text-properties (point-min) (point-max) '(face org-warning))
  18391. (when (yes-or-no-p "Include your Org-mode configuration ")
  18392. (mapatoms
  18393. (lambda (v)
  18394. (and (boundp v)
  18395. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18396. (or (and (symbol-value v)
  18397. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18398. (and
  18399. (get v 'custom-type) (get v 'standard-value)
  18400. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18401. (push v list)))))
  18402. (kill-buffer (get-buffer "*Warn about privacy*"))
  18403. list))
  18404. nil nil
  18405. "Remember to cover the basics, that is, what you expected to happen and
  18406. what in fact did happen. You don't know how to make a good report? See
  18407. http://orgmode.org/manual/Feedback.html#Feedback
  18408. Your bug report will be posted to the Org-mode mailing list.
  18409. ------------------------------------------------------------------------")
  18410. (save-excursion
  18411. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18412. (replace-match "\\1Bug: \\3 [\\2]")))))
  18413. (defun org-install-agenda-files-menu ()
  18414. (let ((bl (buffer-list)))
  18415. (save-excursion
  18416. (while bl
  18417. (set-buffer (pop bl))
  18418. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18419. (when (derived-mode-p 'org-mode)
  18420. (easy-menu-change
  18421. '("Org") "File List for Agenda"
  18422. (append
  18423. (list
  18424. ["Edit File List" (org-edit-agenda-file-list) t]
  18425. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18426. ["Remove Current File from List" org-remove-file t]
  18427. ["Cycle through agenda files" org-cycle-agenda-files t]
  18428. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18429. "--")
  18430. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18431. ;;;; Documentation
  18432. ;;;###autoload
  18433. (defun org-require-autoloaded-modules ()
  18434. (interactive)
  18435. (mapc 'require
  18436. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18437. org-docbook org-exp org-html org-icalendar
  18438. org-id org-latex
  18439. org-publish org-remember org-table
  18440. org-timer org-xoxo)))
  18441. ;;;###autoload
  18442. (defun org-reload (&optional uncompiled)
  18443. "Reload all org lisp files.
  18444. With prefix arg UNCOMPILED, load the uncompiled versions."
  18445. (interactive "P")
  18446. (require 'find-func)
  18447. (let* ((file-re "^org\\(-.*\\)?\\.el")
  18448. (dir-org (file-name-directory (org-find-library-dir "org")))
  18449. (dir-org-contrib (ignore-errors
  18450. (file-name-directory
  18451. (org-find-library-dir "org-contribdir"))))
  18452. (babel-files
  18453. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  18454. (append (list nil "comint" "eval" "exp" "keys"
  18455. "lob" "ref" "table" "tangle")
  18456. (delq nil
  18457. (mapcar
  18458. (lambda (lang)
  18459. (when (cdr lang) (symbol-name (car lang))))
  18460. org-babel-load-languages)))))
  18461. (files
  18462. (append babel-files
  18463. (and dir-org-contrib
  18464. (directory-files dir-org-contrib t file-re))
  18465. (directory-files dir-org t file-re)))
  18466. (remove-re (concat (if (featurep 'xemacs)
  18467. "org-colview" "org-colview-xemacs")
  18468. "\\'")))
  18469. (setq files (mapcar 'file-name-sans-extension files))
  18470. (setq files (mapcar
  18471. (lambda (x) (if (string-match remove-re x) nil x))
  18472. files))
  18473. (setq files (delq nil files))
  18474. (mapc
  18475. (lambda (f)
  18476. (when (featurep (intern (file-name-nondirectory f)))
  18477. (if (and (not uncompiled)
  18478. (file-exists-p (concat f ".elc")))
  18479. (load (concat f ".elc") nil nil 'nosuffix)
  18480. (load (concat f ".el") nil nil 'nosuffix))))
  18481. files)
  18482. (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
  18483. (org-version nil 'full 'message))
  18484. ;;;###autoload
  18485. (defun org-customize ()
  18486. "Call the customize function with org as argument."
  18487. (interactive)
  18488. (org-load-modules-maybe)
  18489. (org-require-autoloaded-modules)
  18490. (customize-browse 'org))
  18491. (defun org-create-customize-menu ()
  18492. "Create a full customization menu for Org-mode, insert it into the menu."
  18493. (interactive)
  18494. (org-load-modules-maybe)
  18495. (org-require-autoloaded-modules)
  18496. (if (fboundp 'customize-menu-create)
  18497. (progn
  18498. (easy-menu-change
  18499. '("Org") "Customize"
  18500. `(["Browse Org group" org-customize t]
  18501. "--"
  18502. ,(customize-menu-create 'org)
  18503. ["Set" Custom-set t]
  18504. ["Save" Custom-save t]
  18505. ["Reset to Current" Custom-reset-current t]
  18506. ["Reset to Saved" Custom-reset-saved t]
  18507. ["Reset to Standard Settings" Custom-reset-standard t]))
  18508. (message "\"Org\"-menu now contains full customization menu"))
  18509. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18510. ;;;; Miscellaneous stuff
  18511. ;;; Generally useful functions
  18512. (defun org-get-at-bol (property)
  18513. "Get text property PROPERTY at beginning of line."
  18514. (get-text-property (point-at-bol) property))
  18515. (defun org-find-text-property-in-string (prop s)
  18516. "Return the first non-nil value of property PROP in string S."
  18517. (or (get-text-property 0 prop s)
  18518. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18519. prop s)))
  18520. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18521. "Display the given MESSAGE as a warning."
  18522. (if (fboundp 'display-warning)
  18523. (display-warning 'org message
  18524. (if (featurep 'xemacs) 'warning :warning))
  18525. (let ((buf (get-buffer-create "*Org warnings*")))
  18526. (with-current-buffer buf
  18527. (goto-char (point-max))
  18528. (insert "Warning (Org): " message)
  18529. (unless (bolp)
  18530. (newline)))
  18531. (display-buffer buf)
  18532. (sit-for 0))))
  18533. (defun org-eval (form)
  18534. "Eval FORM and return result."
  18535. (condition-case error
  18536. (eval form)
  18537. (error (format "%%![Error: %s]" error))))
  18538. (defun org-in-clocktable-p ()
  18539. "Check if the cursor is in a clocktable."
  18540. (let ((pos (point)) start)
  18541. (save-excursion
  18542. (end-of-line 1)
  18543. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18544. (setq start (match-beginning 0))
  18545. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18546. (>= (match-end 0) pos)
  18547. start))))
  18548. (defun org-in-commented-line ()
  18549. "Is point in a line starting with `#'?"
  18550. (equal (char-after (point-at-bol)) ?#))
  18551. (defun org-in-indented-comment-line ()
  18552. "Is point in a line starting with `#' after some white space?"
  18553. (save-excursion
  18554. (save-match-data
  18555. (goto-char (point-at-bol))
  18556. (looking-at "[ \t]*#"))))
  18557. (defun org-in-verbatim-emphasis ()
  18558. (save-match-data
  18559. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18560. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18561. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18562. (if (and marker (marker-buffer marker)
  18563. (buffer-live-p (marker-buffer marker)))
  18564. (progn
  18565. (org-pop-to-buffer-same-window (marker-buffer marker))
  18566. (if (or (> marker (point-max)) (< marker (point-min)))
  18567. (widen))
  18568. (goto-char marker)
  18569. (org-show-context 'org-goto))
  18570. (if bookmark
  18571. (bookmark-jump bookmark)
  18572. (error "Cannot find location"))))
  18573. (defun org-quote-csv-field (s)
  18574. "Quote field for inclusion in CSV material."
  18575. (if (string-match "[\",]" s)
  18576. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18577. s))
  18578. (defun org-force-self-insert (N)
  18579. "Needed to enforce self-insert under remapping."
  18580. (interactive "p")
  18581. (self-insert-command N))
  18582. (defun org-string-width (s)
  18583. "Compute width of string, ignoring invisible characters.
  18584. This ignores character with invisibility property `org-link', and also
  18585. characters with property `org-cwidth', because these will become invisible
  18586. upon the next fontification round."
  18587. (let (b l)
  18588. (when (or (eq t buffer-invisibility-spec)
  18589. (assq 'org-link buffer-invisibility-spec))
  18590. (while (setq b (text-property-any 0 (length s)
  18591. 'invisible 'org-link s))
  18592. (setq s (concat (substring s 0 b)
  18593. (substring s (or (next-single-property-change
  18594. b 'invisible s) (length s)))))))
  18595. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18596. (setq s (concat (substring s 0 b)
  18597. (substring s (or (next-single-property-change
  18598. b 'org-cwidth s) (length s))))))
  18599. (setq l (string-width s) b -1)
  18600. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18601. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18602. l))
  18603. (defun org-shorten-string (s maxlength)
  18604. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18605. If the string is shorter or has length MAXLENGTH, just return the
  18606. original string. If it is longer, the functions finds a space in the
  18607. string, breaks this string off at that locations and adds three dots
  18608. as ellipsis. Including the ellipsis, the string will not be longer
  18609. than MAXLENGTH. If finding a good breaking point in the string does
  18610. not work, the string is just chopped off in the middle of a word
  18611. if necessary."
  18612. (if (<= (length s) maxlength)
  18613. s
  18614. (let* ((n (max (- maxlength 4) 1))
  18615. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18616. (if (string-match re s)
  18617. (concat (match-string 1 s) "...")
  18618. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18619. (defun org-get-indentation (&optional line)
  18620. "Get the indentation of the current line, interpreting tabs.
  18621. When LINE is given, assume it represents a line and compute its indentation."
  18622. (if line
  18623. (if (string-match "^ *" (org-remove-tabs line))
  18624. (match-end 0))
  18625. (save-excursion
  18626. (beginning-of-line 1)
  18627. (skip-chars-forward " \t")
  18628. (current-column))))
  18629. (defun org-get-string-indentation (s)
  18630. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18631. (let ((n -1) (i 0) (w tab-width) c)
  18632. (catch 'exit
  18633. (while (< (setq n (1+ n)) (length s))
  18634. (setq c (aref s n))
  18635. (cond ((= c ?\ ) (setq i (1+ i)))
  18636. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18637. (t (throw 'exit t)))))
  18638. i))
  18639. (defun org-remove-tabs (s &optional width)
  18640. "Replace tabulators in S with spaces.
  18641. Assumes that s is a single line, starting in column 0."
  18642. (setq width (or width tab-width))
  18643. (while (string-match "\t" s)
  18644. (setq s (replace-match
  18645. (make-string
  18646. (- (* width (/ (+ (match-beginning 0) width) width))
  18647. (match-beginning 0)) ?\ )
  18648. t t s)))
  18649. s)
  18650. (defun org-fix-indentation (line ind)
  18651. "Fix indentation in LINE.
  18652. IND is a cons cell with target and minimum indentation.
  18653. If the current indentation in LINE is smaller than the minimum,
  18654. leave it alone. If it is larger than ind, set it to the target."
  18655. (let* ((l (org-remove-tabs line))
  18656. (i (org-get-indentation l))
  18657. (i1 (car ind)) (i2 (cdr ind)))
  18658. (if (>= i i2) (setq l (substring line i2)))
  18659. (if (> i1 0)
  18660. (concat (make-string i1 ?\ ) l)
  18661. l)))
  18662. (defun org-remove-indentation (code &optional n)
  18663. "Remove the maximum common indentation from the lines in CODE.
  18664. N may optionally be the number of spaces to remove."
  18665. (with-temp-buffer
  18666. (insert code)
  18667. (org-do-remove-indentation n)
  18668. (buffer-string)))
  18669. (defun org-do-remove-indentation (&optional n)
  18670. "Remove the maximum common indentation from the buffer."
  18671. (untabify (point-min) (point-max))
  18672. (let ((min 10000) re)
  18673. (if n
  18674. (setq min n)
  18675. (goto-char (point-min))
  18676. (while (re-search-forward "^ *[^ \n]" nil t)
  18677. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18678. (unless (or (= min 0) (= min 10000))
  18679. (setq re (format "^ \\{%d\\}" min))
  18680. (goto-char (point-min))
  18681. (while (re-search-forward re nil t)
  18682. (replace-match "")
  18683. (end-of-line 1))
  18684. min)))
  18685. (defun org-fill-template (template alist)
  18686. "Find each %key of ALIST in TEMPLATE and replace it."
  18687. (let ((case-fold-search nil)
  18688. entry key value)
  18689. (setq alist (sort (copy-sequence alist)
  18690. (lambda (a b) (< (length (car a)) (length (car b))))))
  18691. (while (setq entry (pop alist))
  18692. (setq template
  18693. (replace-regexp-in-string
  18694. (concat "%" (regexp-quote (car entry)))
  18695. (or (cdr entry) "") template t t)))
  18696. template))
  18697. (defun org-base-buffer (buffer)
  18698. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18699. (if (not buffer)
  18700. buffer
  18701. (or (buffer-base-buffer buffer)
  18702. buffer)))
  18703. (defun org-trim (s)
  18704. "Remove whitespace at beginning and end of string."
  18705. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18706. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18707. s)
  18708. (defun org-wrap (string &optional width lines)
  18709. "Wrap string to either a number of lines, or a width in characters.
  18710. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18711. that costs. If there is a word longer than WIDTH, the text is actually
  18712. wrapped to the length of that word.
  18713. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18714. many lines, whatever width that takes.
  18715. The return value is a list of lines, without newlines at the end."
  18716. (let* ((words (org-split-string string "[ \t\n]+"))
  18717. (maxword (apply 'max (mapcar 'org-string-width words)))
  18718. w ll)
  18719. (cond (width
  18720. (org-do-wrap words (max maxword width)))
  18721. (lines
  18722. (setq w maxword)
  18723. (setq ll (org-do-wrap words maxword))
  18724. (if (<= (length ll) lines)
  18725. ll
  18726. (setq ll words)
  18727. (while (> (length ll) lines)
  18728. (setq w (1+ w))
  18729. (setq ll (org-do-wrap words w)))
  18730. ll))
  18731. (t (error "Cannot wrap this")))))
  18732. (defun org-do-wrap (words width)
  18733. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18734. (let (lines line)
  18735. (while words
  18736. (setq line (pop words))
  18737. (while (and words (< (+ (length line) (length (car words))) width))
  18738. (setq line (concat line " " (pop words))))
  18739. (setq lines (push line lines)))
  18740. (nreverse lines)))
  18741. (defun org-split-string (string &optional separators)
  18742. "Splits STRING into substrings at SEPARATORS.
  18743. No empty strings are returned if there are matches at the beginning
  18744. and end of string."
  18745. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18746. (start 0)
  18747. notfirst
  18748. (list nil))
  18749. (while (and (string-match rexp string
  18750. (if (and notfirst
  18751. (= start (match-beginning 0))
  18752. (< start (length string)))
  18753. (1+ start) start))
  18754. (< (match-beginning 0) (length string)))
  18755. (setq notfirst t)
  18756. (or (eq (match-beginning 0) 0)
  18757. (and (eq (match-beginning 0) (match-end 0))
  18758. (eq (match-beginning 0) start))
  18759. (setq list
  18760. (cons (substring string start (match-beginning 0))
  18761. list)))
  18762. (setq start (match-end 0)))
  18763. (or (eq start (length string))
  18764. (setq list
  18765. (cons (substring string start)
  18766. list)))
  18767. (nreverse list)))
  18768. (defun org-quote-vert (s)
  18769. "Replace \"|\" with \"\\vert\"."
  18770. (while (string-match "|" s)
  18771. (setq s (replace-match "\\vert" t t s)))
  18772. s)
  18773. (defun org-uuidgen-p (s)
  18774. "Is S an ID created by UUIDGEN?"
  18775. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18776. (defun org-in-src-block-p nil
  18777. "Whether point is in a code source block."
  18778. (let (ov)
  18779. (when (setq ov (overlays-at (point)))
  18780. (memq 'org-block-background
  18781. (overlay-properties
  18782. (car ov))))))
  18783. (defun org-context ()
  18784. "Return a list of contexts of the current cursor position.
  18785. If several contexts apply, all are returned.
  18786. Each context entry is a list with a symbol naming the context, and
  18787. two positions indicating start and end of the context. Possible
  18788. contexts are:
  18789. :headline anywhere in a headline
  18790. :headline-stars on the leading stars in a headline
  18791. :todo-keyword on a TODO keyword (including DONE) in a headline
  18792. :tags on the TAGS in a headline
  18793. :priority on the priority cookie in a headline
  18794. :item on the first line of a plain list item
  18795. :item-bullet on the bullet/number of a plain list item
  18796. :checkbox on the checkbox in a plain list item
  18797. :table in an org-mode table
  18798. :table-special on a special filed in a table
  18799. :table-table in a table.el table
  18800. :clocktable in a clocktable
  18801. :src-block in a source block
  18802. :link on a hyperlink
  18803. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18804. :target on a <<target>>
  18805. :radio-target on a <<<radio-target>>>
  18806. :latex-fragment on a LaTeX fragment
  18807. :latex-preview on a LaTeX fragment with overlaid preview image
  18808. This function expects the position to be visible because it uses font-lock
  18809. faces as a help to recognize the following contexts: :table-special, :link,
  18810. and :keyword."
  18811. (let* ((f (get-text-property (point) 'face))
  18812. (faces (if (listp f) f (list f)))
  18813. (case-fold-search t)
  18814. (p (point)) clist o)
  18815. ;; First the large context
  18816. (cond
  18817. ((org-at-heading-p t)
  18818. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18819. (when (progn
  18820. (beginning-of-line 1)
  18821. (looking-at org-todo-line-tags-regexp))
  18822. (push (org-point-in-group p 1 :headline-stars) clist)
  18823. (push (org-point-in-group p 2 :todo-keyword) clist)
  18824. (push (org-point-in-group p 4 :tags) clist))
  18825. (goto-char p)
  18826. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18827. (if (looking-at "\\[#[A-Z0-9]\\]")
  18828. (push (org-point-in-group p 0 :priority) clist)))
  18829. ((org-at-item-p)
  18830. (push (org-point-in-group p 2 :item-bullet) clist)
  18831. (push (list :item (point-at-bol)
  18832. (save-excursion (org-end-of-item) (point)))
  18833. clist)
  18834. (and (org-at-item-checkbox-p)
  18835. (push (org-point-in-group p 0 :checkbox) clist)))
  18836. ((org-at-table-p)
  18837. (push (list :table (org-table-begin) (org-table-end)) clist)
  18838. (if (memq 'org-formula faces)
  18839. (push (list :table-special
  18840. (previous-single-property-change p 'face)
  18841. (next-single-property-change p 'face)) clist)))
  18842. ((org-at-table-p 'any)
  18843. (push (list :table-table) clist)))
  18844. (goto-char p)
  18845. (let ((case-fold-search t))
  18846. ;; New the "medium" contexts: clocktables, source blocks
  18847. (cond ((org-in-clocktable-p)
  18848. (push (list :clocktable
  18849. (and (or (looking-at "#\\+BEGIN: clocktable")
  18850. (search-backward "#+BEGIN: clocktable" nil t))
  18851. (match-beginning 0))
  18852. (and (re-search-forward "#\\+END:?" nil t)
  18853. (match-end 0))) clist))
  18854. ((org-in-src-block-p)
  18855. (push (list :src-block
  18856. (and (or (looking-at "#\\+BEGIN_SRC")
  18857. (search-backward "#+BEGIN_SRC" nil t))
  18858. (match-beginning 0))
  18859. (and (search-forward "#+END_SRC" nil t)
  18860. (match-beginning 0))) clist))))
  18861. (goto-char p)
  18862. ;; Now the small context
  18863. (cond
  18864. ((org-at-timestamp-p)
  18865. (push (org-point-in-group p 0 :timestamp) clist))
  18866. ((memq 'org-link faces)
  18867. (push (list :link
  18868. (previous-single-property-change p 'face)
  18869. (next-single-property-change p 'face)) clist))
  18870. ((memq 'org-special-keyword faces)
  18871. (push (list :keyword
  18872. (previous-single-property-change p 'face)
  18873. (next-single-property-change p 'face)) clist))
  18874. ((org-at-target-p)
  18875. (push (org-point-in-group p 0 :target) clist)
  18876. (goto-char (1- (match-beginning 0)))
  18877. (if (looking-at org-radio-target-regexp)
  18878. (push (org-point-in-group p 0 :radio-target) clist))
  18879. (goto-char p))
  18880. ((setq o (car (delq nil
  18881. (mapcar
  18882. (lambda (x)
  18883. (if (memq x org-latex-fragment-image-overlays) x))
  18884. (overlays-at (point))))))
  18885. (push (list :latex-fragment
  18886. (overlay-start o) (overlay-end o)) clist)
  18887. (push (list :latex-preview
  18888. (overlay-start o) (overlay-end o)) clist))
  18889. ((org-inside-LaTeX-fragment-p)
  18890. ;; FIXME: positions wrong.
  18891. (push (list :latex-fragment (point) (point)) clist)))
  18892. (setq clist (nreverse (delq nil clist)))
  18893. clist))
  18894. ;; FIXME: Compare with at-regexp-p Do we need both?
  18895. (defun org-in-regexp (re &optional nlines visually)
  18896. "Check if point is inside a match of regexp.
  18897. Normally only the current line is checked, but you can include NLINES extra
  18898. lines both before and after point into the search.
  18899. If VISUALLY is set, require that the cursor is not after the match but
  18900. really on, so that the block visually is on the match."
  18901. (catch 'exit
  18902. (let ((pos (point))
  18903. (eol (point-at-eol (+ 1 (or nlines 0))))
  18904. (inc (if visually 1 0)))
  18905. (save-excursion
  18906. (beginning-of-line (- 1 (or nlines 0)))
  18907. (while (re-search-forward re eol t)
  18908. (if (and (<= (match-beginning 0) pos)
  18909. (>= (+ inc (match-end 0)) pos))
  18910. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18911. (defun org-at-regexp-p (regexp)
  18912. "Is point inside a match of REGEXP in the current line?"
  18913. (catch 'exit
  18914. (save-excursion
  18915. (let ((pos (point)) (end (point-at-eol)))
  18916. (beginning-of-line 1)
  18917. (while (re-search-forward regexp end t)
  18918. (if (and (<= (match-beginning 0) pos)
  18919. (>= (match-end 0) pos))
  18920. (throw 'exit t)))
  18921. nil))))
  18922. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18923. "Non-nil when point is between matches of START-RE and END-RE.
  18924. Also return a non-nil value when point is on one of the matches.
  18925. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18926. buffer positions. Default values are the positions of headlines
  18927. surrounding the point.
  18928. The functions returns a cons cell whose car (resp. cdr) is the
  18929. position before START-RE (resp. after END-RE)."
  18930. (save-match-data
  18931. (let ((pos (point))
  18932. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18933. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18934. beg end)
  18935. (save-excursion
  18936. ;; Point is on a block when on START-RE or if START-RE can be
  18937. ;; found before it...
  18938. (and (or (org-at-regexp-p start-re)
  18939. (re-search-backward start-re limit-up t))
  18940. (setq beg (match-beginning 0))
  18941. ;; ... and END-RE after it...
  18942. (goto-char (match-end 0))
  18943. (re-search-forward end-re limit-down t)
  18944. (> (setq end (match-end 0)) pos)
  18945. ;; ... without another START-RE in-between.
  18946. (goto-char (match-beginning 0))
  18947. (not (re-search-backward start-re (1+ beg) t))
  18948. ;; Return value.
  18949. (cons beg end))))))
  18950. (defun org-in-block-p (names)
  18951. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18952. NAMES is a list of strings containing names of blocks.
  18953. Return first block name matched, or nil. Beware that in case of
  18954. nested blocks, the returned name may not belong to the closest
  18955. block from point."
  18956. (save-match-data
  18957. (catch 'exit
  18958. (let ((case-fold-search t)
  18959. (lim-up (save-excursion (outline-previous-heading)))
  18960. (lim-down (save-excursion (outline-next-heading))))
  18961. (mapc (lambda (name)
  18962. (let ((n (regexp-quote name)))
  18963. (when (org-between-regexps-p
  18964. (concat "^[ \t]*#\\+begin_" n)
  18965. (concat "^[ \t]*#\\+end_" n)
  18966. lim-up lim-down)
  18967. (throw 'exit n))))
  18968. names))
  18969. nil)))
  18970. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18971. "Call `multi-occur' with buffers for all agenda files."
  18972. (interactive "sOrg-files matching: \np")
  18973. (let* ((files (org-agenda-files))
  18974. (tnames (mapcar 'file-truename files))
  18975. (extra org-agenda-text-search-extra-files)
  18976. f)
  18977. (when (eq (car extra) 'agenda-archives)
  18978. (setq extra (cdr extra))
  18979. (setq files (org-add-archive-files files)))
  18980. (while (setq f (pop extra))
  18981. (unless (member (file-truename f) tnames)
  18982. (add-to-list 'files f 'append)
  18983. (add-to-list 'tnames (file-truename f) 'append)))
  18984. (multi-occur
  18985. (mapcar (lambda (x)
  18986. (with-current-buffer
  18987. (or (get-file-buffer x) (find-file-noselect x))
  18988. (widen)
  18989. (current-buffer)))
  18990. files)
  18991. regexp)))
  18992. (if (boundp 'occur-mode-find-occurrence-hook)
  18993. ;; Emacs 23
  18994. (add-hook 'occur-mode-find-occurrence-hook
  18995. (lambda ()
  18996. (when (derived-mode-p 'org-mode)
  18997. (org-reveal))))
  18998. ;; Emacs 22
  18999. (defadvice occur-mode-goto-occurrence
  19000. (after org-occur-reveal activate)
  19001. (and (derived-mode-p 'org-mode) (org-reveal)))
  19002. (defadvice occur-mode-goto-occurrence-other-window
  19003. (after org-occur-reveal activate)
  19004. (and (derived-mode-p 'org-mode) (org-reveal)))
  19005. (defadvice occur-mode-display-occurrence
  19006. (after org-occur-reveal activate)
  19007. (when (derived-mode-p 'org-mode)
  19008. (let ((pos (occur-mode-find-occurrence)))
  19009. (with-current-buffer (marker-buffer pos)
  19010. (save-excursion
  19011. (goto-char pos)
  19012. (org-reveal)))))))
  19013. (defun org-occur-link-in-agenda-files ()
  19014. "Create a link and search for it in the agendas.
  19015. The link is not stored in `org-stored-links', it is just created
  19016. for the search purpose."
  19017. (interactive)
  19018. (let ((link (condition-case nil
  19019. (org-store-link nil)
  19020. (error "Unable to create a link to here"))))
  19021. (org-occur-in-agenda-files (regexp-quote link))))
  19022. (defun org-uniquify (list)
  19023. "Remove duplicate elements from LIST."
  19024. (let (res)
  19025. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19026. res))
  19027. (defun org-delete-all (elts list)
  19028. "Remove all elements in ELTS from LIST."
  19029. (while elts
  19030. (setq list (delete (pop elts) list)))
  19031. list)
  19032. (defun org-count (cl-item cl-seq)
  19033. "Count the number of occurrences of ITEM in SEQ.
  19034. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19035. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19036. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19037. (while (< cl-start cl-end)
  19038. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19039. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19040. (setq cl-start (1+ cl-start)))
  19041. cl-count))
  19042. (defun org-remove-if (predicate seq)
  19043. "Remove everything from SEQ that fulfills PREDICATE."
  19044. (let (res e)
  19045. (while seq
  19046. (setq e (pop seq))
  19047. (if (not (funcall predicate e)) (push e res)))
  19048. (nreverse res)))
  19049. (defun org-remove-if-not (predicate seq)
  19050. "Remove everything from SEQ that does not fulfill PREDICATE."
  19051. (let (res e)
  19052. (while seq
  19053. (setq e (pop seq))
  19054. (if (funcall predicate e) (push e res)))
  19055. (nreverse res)))
  19056. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19057. "Reduce two-argument FUNCTION across SEQ.
  19058. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19059. \":initial-value\" removed."
  19060. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19061. (cadr (memq :initial-value cl-keys)))
  19062. (cl-seq (pop cl-seq))
  19063. (t (funcall cl-func)))))
  19064. (while cl-seq
  19065. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19066. cl-accum))
  19067. (defun org-back-over-empty-lines ()
  19068. "Move backwards over whitespace, to the beginning of the first empty line.
  19069. Returns the number of empty lines passed."
  19070. (let ((pos (point)))
  19071. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19072. (skip-chars-backward " \t\n\r")
  19073. (unless (eobp)
  19074. (forward-line -1)))
  19075. (beginning-of-line 2)
  19076. (goto-char (min (point) pos))
  19077. (count-lines (point) pos)))
  19078. (defun org-skip-whitespace ()
  19079. (skip-chars-forward " \t\n\r"))
  19080. (defun org-point-in-group (point group &optional context)
  19081. "Check if POINT is in match-group GROUP.
  19082. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19083. match. If the match group does not exist or point is not inside it,
  19084. return nil."
  19085. (and (match-beginning group)
  19086. (>= point (match-beginning group))
  19087. (<= point (match-end group))
  19088. (if context
  19089. (list context (match-beginning group) (match-end group))
  19090. t)))
  19091. (defun org-switch-to-buffer-other-window (&rest args)
  19092. "Switch to buffer in a second window on the current frame.
  19093. In particular, do not allow pop-up frames.
  19094. Returns the newly created buffer."
  19095. (let (pop-up-frames special-display-buffer-names special-display-regexps
  19096. special-display-function)
  19097. (apply 'switch-to-buffer-other-window args)))
  19098. (defun org-combine-plists (&rest plists)
  19099. "Create a single property list from all plists in PLISTS.
  19100. The process starts by copying the first list, and then setting properties
  19101. from the other lists. Settings in the last list are the most significant
  19102. ones and overrule settings in the other lists."
  19103. (let ((rtn (copy-sequence (pop plists)))
  19104. p v ls)
  19105. (while plists
  19106. (setq ls (pop plists))
  19107. (while ls
  19108. (setq p (pop ls) v (pop ls))
  19109. (setq rtn (plist-put rtn p v))))
  19110. rtn))
  19111. (defun org-replace-escapes (string table)
  19112. "Replace %-escapes in STRING with values in TABLE.
  19113. TABLE is an association list with keys like \"%a\" and string values.
  19114. The sequences in STRING may contain normal field width and padding information,
  19115. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19116. so values can contain further %-escapes if they are define later in TABLE."
  19117. (let ((tbl (copy-alist table))
  19118. (case-fold-search nil)
  19119. (pchg 0)
  19120. e re rpl)
  19121. (while (setq e (pop tbl))
  19122. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19123. (when (and (cdr e) (string-match re (cdr e)))
  19124. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19125. (safe "SREF"))
  19126. (add-text-properties 0 3 (list 'sref sref) safe)
  19127. (setcdr e (replace-match safe t t (cdr e)))))
  19128. (while (string-match re string)
  19129. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19130. (cdr e)))
  19131. (setq string (replace-match rpl t t string))))
  19132. (while (setq pchg (next-property-change pchg string))
  19133. (let ((sref (get-text-property pchg 'sref string)))
  19134. (when (and sref (string-match "SREF" string pchg))
  19135. (setq string (replace-match sref t t string)))))
  19136. string))
  19137. (defun org-sublist (list start end)
  19138. "Return a section of LIST, from START to END.
  19139. Counting starts at 1."
  19140. (let (rtn (c start))
  19141. (setq list (nthcdr (1- start) list))
  19142. (while (and list (<= c end))
  19143. (push (pop list) rtn)
  19144. (setq c (1+ c)))
  19145. (nreverse rtn)))
  19146. (defun org-find-base-buffer-visiting (file)
  19147. "Like `find-buffer-visiting' but always return the base buffer and
  19148. not an indirect buffer."
  19149. (let ((buf (or (get-file-buffer file)
  19150. (find-buffer-visiting file))))
  19151. (if buf
  19152. (or (buffer-base-buffer buf) buf)
  19153. nil)))
  19154. (defun org-image-file-name-regexp (&optional extensions)
  19155. "Return regexp matching the file names of images.
  19156. If EXTENSIONS is given, only match these."
  19157. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19158. (image-file-name-regexp)
  19159. (let ((image-file-name-extensions
  19160. (or extensions
  19161. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19162. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19163. (concat "\\."
  19164. (regexp-opt (nconc (mapcar 'upcase
  19165. image-file-name-extensions)
  19166. image-file-name-extensions)
  19167. t)
  19168. "\\'"))))
  19169. (defun org-file-image-p (file &optional extensions)
  19170. "Return non-nil if FILE is an image."
  19171. (save-match-data
  19172. (string-match (org-image-file-name-regexp extensions) file)))
  19173. (defun org-get-cursor-date ()
  19174. "Return the date at cursor in as a time.
  19175. This works in the calendar and in the agenda, anywhere else it just
  19176. returns the current time."
  19177. (let (date day defd)
  19178. (cond
  19179. ((eq major-mode 'calendar-mode)
  19180. (setq date (calendar-cursor-to-date)
  19181. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19182. ((eq major-mode 'org-agenda-mode)
  19183. (setq day (get-text-property (point) 'day))
  19184. (if day
  19185. (setq date (calendar-gregorian-from-absolute day)
  19186. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  19187. (nth 2 date))))))
  19188. (or defd (current-time))))
  19189. (defvar org-agenda-action-marker (make-marker)
  19190. "Marker pointing to the entry for the next agenda action.")
  19191. (defun org-mark-entry-for-agenda-action ()
  19192. "Mark the current entry as target of an agenda action.
  19193. Agenda actions are actions executed from the agenda with the key `k',
  19194. which make use of the date at the cursor."
  19195. (interactive)
  19196. (move-marker org-agenda-action-marker
  19197. (save-excursion (org-back-to-heading t) (point))
  19198. (current-buffer))
  19199. (message
  19200. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  19201. (defun org-mark-subtree (&optional up)
  19202. "Mark the current subtree.
  19203. This puts point at the start of the current subtree, and mark at
  19204. the end. If a numeric prefix UP is given, move up into the
  19205. hierarchy of headlines by UP levels before marking the subtree."
  19206. (interactive "P")
  19207. (org-with-limited-levels
  19208. (cond ((org-at-heading-p) (beginning-of-line))
  19209. ((org-before-first-heading-p) (error "Not in a subtree"))
  19210. (t (outline-previous-visible-heading 1))))
  19211. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19212. (if (org-called-interactively-p 'any)
  19213. (call-interactively 'org-mark-element)
  19214. (org-mark-element)))
  19215. ;;; Macros
  19216. ;; Macros are expanded with `org-macro-replace-all', which relies
  19217. ;; internally on `org-macro-expand'.
  19218. ;; Templates for expansion are stored in the buffer-local variable
  19219. ;; `org-macro-templates'. This variable is updated by
  19220. ;; `org-macro-initialize-templates'.
  19221. (defvar org-macro-templates nil
  19222. "Alist containing all macro templates in current buffer.
  19223. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19224. stands for macro's name and template for its replacement value,
  19225. both as strings. This is an internal variable. Do not set it
  19226. directly, use instead:
  19227. #+MACRO: name template")
  19228. (make-variable-buffer-local 'org-macro-templates)
  19229. (defun org-macro-expand (macro)
  19230. "Return expanded MACRO, as a string.
  19231. MACRO is an object, obtained, for example, with
  19232. `org-element-context'. Return nil if no template was found."
  19233. (let ((template
  19234. (cdr (assoc-string (org-element-property :key macro)
  19235. org-macro-templates
  19236. ;; Macro names are case-insensitive.
  19237. t))))
  19238. (when template
  19239. (let ((value (replace-regexp-in-string
  19240. "\\$[0-9]+"
  19241. (lambda (arg)
  19242. (or (nth (1- (string-to-number (substring arg 1)))
  19243. (org-element-property :args macro))
  19244. ;; No argument provided: remove
  19245. ;; place-holder.
  19246. ""))
  19247. template)))
  19248. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19249. (when (string-match "\\`(eval\\>" value)
  19250. (setq value (eval (read value))))
  19251. ;; Return string.
  19252. (format "%s" (or value ""))))))
  19253. (defun org-macro-replace-all ()
  19254. "Replace all macros in current buffer by their expansion."
  19255. (save-excursion
  19256. (goto-char (point-min))
  19257. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19258. (let ((object (org-element-context)))
  19259. (when (eq (org-element-type object) 'macro)
  19260. (let ((value (org-macro-expand object)))
  19261. (when value
  19262. (delete-region
  19263. (org-element-property :begin object)
  19264. ;; Preserve white spaces after the macro.
  19265. (progn (goto-char (org-element-property :end object))
  19266. (skip-chars-backward " \t")
  19267. (point)))
  19268. ;; Leave point before replacement in case of recursive
  19269. ;; expansions.
  19270. (save-excursion (insert value)))))))))
  19271. (defun org-macro-initialize-templates ()
  19272. "Collect macro templates defined in current buffer.
  19273. Templates are stored in buffer-local variable
  19274. `org-macro-templates'. In addition to buffer-defined macros, the
  19275. function installs the following ones: \"property\", \"date\",
  19276. \"time\". and, if appropriate, \"input-file\" and
  19277. \"modification-time\"."
  19278. (let ((case-fold-search t)
  19279. (set-template
  19280. (lambda (cell)
  19281. ;; Add CELL to `org-macro-templates' if there's no
  19282. ;; association matching its name already. Otherwise,
  19283. ;; replace old association with the new one in that
  19284. ;; variable.
  19285. (let ((old-template (assoc (car cell) org-macro-templates)))
  19286. (if old-template (setcdr old-template (cdr cell))
  19287. (push cell org-macro-templates))))))
  19288. ;; Install buffer-local macros.
  19289. (org-with-wide-buffer
  19290. (goto-char (point-min))
  19291. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19292. (let ((element (org-element-at-point)))
  19293. (when (eq (org-element-type element) 'keyword)
  19294. (let ((value (org-element-property :value element)))
  19295. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19296. (funcall set-template
  19297. (cons (match-string 1 value)
  19298. (or (match-string 2 value) "")))))))))
  19299. ;; Install hard-coded macros.
  19300. (mapc (lambda (cell) (funcall set-template cell))
  19301. (list
  19302. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19303. (cons "date" "(eval (format-time-string \"$1\"))")
  19304. (cons "time" "(eval (format-time-string \"$1\"))")))
  19305. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19306. (when (and visited-file (file-exists-p visited-file))
  19307. (mapc (lambda (cell) (funcall set-template cell))
  19308. (list
  19309. (cons "input-file" (file-name-nondirectory visited-file))
  19310. (cons "modification-time"
  19311. (format "(eval (format-time-string \"$1\" '%s))"
  19312. (prin1-to-string
  19313. (nth 5 (file-attributes visited-file)))))))))))
  19314. ;;; Indentation
  19315. (defun org-indent-line ()
  19316. "Indent line depending on context."
  19317. (interactive)
  19318. (let* ((pos (point))
  19319. (itemp (org-at-item-p))
  19320. (case-fold-search t)
  19321. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19322. (inline-task-p (and (featurep 'org-inlinetask)
  19323. (org-inlinetask-in-task-p)))
  19324. (inline-re (and inline-task-p
  19325. (org-inlinetask-outline-regexp)))
  19326. column)
  19327. (if (and orgstruct-is-++ (eq pos (point)))
  19328. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19329. (indent-according-to-mode))
  19330. (beginning-of-line 1)
  19331. (cond
  19332. ;; Headings
  19333. ((looking-at org-outline-regexp) (setq column 0))
  19334. ;; Included files
  19335. ((looking-at "#\\+include:") (setq column 0))
  19336. ;; Footnote definition
  19337. ((looking-at org-footnote-definition-re) (setq column 0))
  19338. ;; Literal examples
  19339. ((looking-at "[ \t]*:\\( \\|$\\)")
  19340. (setq column (org-get-indentation))) ; do nothing
  19341. ;; Lists
  19342. ((ignore-errors (goto-char (org-in-item-p)))
  19343. (setq column (if itemp
  19344. (org-get-indentation)
  19345. (org-list-item-body-column (point))))
  19346. (goto-char pos))
  19347. ;; Drawers
  19348. ((and (looking-at "[ \t]*:END:")
  19349. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19350. (save-excursion
  19351. (goto-char (1- (match-beginning 1)))
  19352. (setq column (current-column))))
  19353. ;; Special blocks
  19354. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19355. (save-excursion
  19356. (re-search-backward
  19357. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19358. (setq column (org-get-indentation (match-string 0))))
  19359. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19360. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19361. (save-excursion
  19362. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19363. (setq column
  19364. (cond ((equal (downcase (match-string 1)) "src")
  19365. ;; src blocks: let `org-edit-src-exit' handle them
  19366. (org-get-indentation))
  19367. ((equal (downcase (match-string 1)) "example")
  19368. (max (org-get-indentation)
  19369. (org-get-indentation (match-string 0))))
  19370. (t
  19371. (org-get-indentation (match-string 0))))))
  19372. ;; This line has nothing special, look at the previous relevant
  19373. ;; line to compute indentation
  19374. (t
  19375. (beginning-of-line 0)
  19376. (while (and (not (bobp))
  19377. (not (looking-at org-drawer-regexp))
  19378. ;; When point started in an inline task, do not move
  19379. ;; above task starting line.
  19380. (not (and inline-task-p (looking-at inline-re)))
  19381. ;; Skip drawers, blocks, empty lines, verbatim,
  19382. ;; comments, tables, footnotes definitions, lists,
  19383. ;; inline tasks.
  19384. (or (and (looking-at "[ \t]*:END:")
  19385. (re-search-backward org-drawer-regexp nil t))
  19386. (and (looking-at "[ \t]*#\\+end_")
  19387. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19388. (looking-at "[ \t]*[\n:#|]")
  19389. (looking-at org-footnote-definition-re)
  19390. (and (ignore-errors (goto-char (org-in-item-p)))
  19391. (goto-char
  19392. (org-list-get-top-point (org-list-struct))))
  19393. (and (not inline-task-p)
  19394. (featurep 'org-inlinetask)
  19395. (org-inlinetask-in-task-p)
  19396. (or (org-inlinetask-goto-beginning) t))))
  19397. (beginning-of-line 0))
  19398. (cond
  19399. ;; There was an heading above.
  19400. ((looking-at "\\*+[ \t]+")
  19401. (if (not org-adapt-indentation)
  19402. (setq column 0)
  19403. (goto-char (match-end 0))
  19404. (setq column (current-column))))
  19405. ;; A drawer had started and is unfinished
  19406. ((looking-at org-drawer-regexp)
  19407. (goto-char (1- (match-beginning 1)))
  19408. (setq column (current-column)))
  19409. ;; Else, nothing noticeable found: get indentation and go on.
  19410. (t (setq column (org-get-indentation))))))
  19411. ;; Now apply indentation and move cursor accordingly
  19412. (goto-char pos)
  19413. (if (<= (current-column) (current-indentation))
  19414. (org-indent-line-to column)
  19415. (save-excursion (org-indent-line-to column)))
  19416. ;; Special polishing for properties, see `org-property-format'
  19417. (setq column (current-column))
  19418. (beginning-of-line 1)
  19419. (if (looking-at
  19420. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19421. (replace-match (concat (match-string 1)
  19422. (format org-property-format
  19423. (match-string 2) (match-string 3)))
  19424. t t))
  19425. (org-move-to-column column))))
  19426. (defun org-indent-drawer ()
  19427. "Indent the drawer at point."
  19428. (interactive)
  19429. (let ((p (point))
  19430. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19431. (match-end 0)))
  19432. (folded
  19433. (save-excursion
  19434. (end-of-line)
  19435. (when (overlays-at (point))
  19436. (member 'invisible (overlay-properties
  19437. (car (overlays-at (point)))))))))
  19438. (when folded (org-cycle))
  19439. (indent-for-tab-command)
  19440. (while (and (move-beginning-of-line 2) (< (point) e))
  19441. (indent-for-tab-command))
  19442. (goto-char p)
  19443. (when folded (org-cycle)))
  19444. (message "Drawer at point indented"))
  19445. (defun org-indent-block ()
  19446. "Indent the block at point."
  19447. (interactive)
  19448. (let ((p (point))
  19449. (case-fold-search t)
  19450. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19451. (match-end 0)))
  19452. (folded
  19453. (save-excursion
  19454. (end-of-line)
  19455. (when (overlays-at (point))
  19456. (member 'invisible (overlay-properties
  19457. (car (overlays-at (point)))))))))
  19458. (when folded (org-cycle))
  19459. (indent-for-tab-command)
  19460. (while (and (move-beginning-of-line 2) (< (point) e))
  19461. (indent-for-tab-command))
  19462. (goto-char p)
  19463. (when folded (org-cycle)))
  19464. (message "Block at point indented"))
  19465. (defun org-indent-region (start end)
  19466. "Indent region."
  19467. (interactive "r")
  19468. (save-excursion
  19469. (let ((line-end (org-current-line end)))
  19470. (goto-char start)
  19471. (while (< (org-current-line) line-end)
  19472. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19473. (t (call-interactively 'org-indent-line)))
  19474. (move-beginning-of-line 2)))))
  19475. ;;; Filling
  19476. ;; We use our own fill-paragraph and auto-fill functions.
  19477. ;; `org-fill-paragraph' relies on adaptive filling and context
  19478. ;; checking. Appropriate `fill-prefix' is computed with
  19479. ;; `org-adaptive-fill-function'.
  19480. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19481. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19482. ;; `org-adaptive-fill-function' value. Internally,
  19483. ;; `org-comment-line-break-function' breaks the line.
  19484. ;; `org-setup-filling' installs filling and auto-filling related
  19485. ;; variables during `org-mode' initialization.
  19486. (defun org-setup-filling ()
  19487. (interactive)
  19488. ;; Prevent auto-fill from inserting unwanted new items.
  19489. (when (boundp 'fill-nobreak-predicate)
  19490. (org-set-local
  19491. 'fill-nobreak-predicate
  19492. (org-uniquify
  19493. (append fill-nobreak-predicate
  19494. '(org-fill-paragraph-separate-nobreak-p
  19495. org-fill-line-break-nobreak-p)))))
  19496. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19497. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19498. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19499. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19500. (defvar org-element-paragraph-separate) ; org-element.el
  19501. (defun org-fill-paragraph-separate-nobreak-p ()
  19502. "Non-nil when a line break at point would insert a new item."
  19503. (looking-at (substring org-element-paragraph-separate 1)))
  19504. (defun org-fill-line-break-nobreak-p ()
  19505. "Non-nil when a line break at point would create an Org line break."
  19506. (save-excursion
  19507. (skip-chars-backward "[ \t]")
  19508. (skip-chars-backward "\\\\")
  19509. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19510. (declare-function message-in-body-p "message" ())
  19511. (defvar org-element--affiliated-re) ; From org-element.el
  19512. (defun org-adaptive-fill-function ()
  19513. "Compute a fill prefix for the current line.
  19514. Return fill prefix, as a string, or nil if current line isn't
  19515. meant to be filled."
  19516. (org-with-wide-buffer
  19517. (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
  19518. ;; FIXME: This is really the job of orgstruct++-mode
  19519. (let* ((p (line-beginning-position))
  19520. (element (save-excursion (beginning-of-line)
  19521. (org-element-at-point)))
  19522. (type (org-element-type element))
  19523. (post-affiliated
  19524. (save-excursion
  19525. (goto-char (org-element-property :begin element))
  19526. (while (looking-at org-element--affiliated-re) (forward-line))
  19527. (point))))
  19528. (unless (< p post-affiliated)
  19529. (case type
  19530. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19531. (footnote-definition "")
  19532. ((item plain-list)
  19533. (make-string (org-list-item-body-column post-affiliated) ? ))
  19534. (paragraph
  19535. ;; Fill prefix is usually the same as the current line,
  19536. ;; except if the paragraph is at the beginning of an item.
  19537. (let ((parent (org-element-property :parent element)))
  19538. (cond ((eq (org-element-type parent) 'item)
  19539. (make-string (org-list-item-body-column
  19540. (org-element-property :begin parent))
  19541. ? ))
  19542. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19543. (match-string 0))
  19544. (t ""))))
  19545. (comment-block
  19546. ;; Only fill contents if P is within block boundaries.
  19547. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19548. (forward-line)
  19549. (point)))
  19550. (cend (save-excursion
  19551. (goto-char (org-element-property :end element))
  19552. (skip-chars-backward " \r\t\n")
  19553. (line-beginning-position))))
  19554. (when (and (>= p cbeg) (< p cend))
  19555. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19556. (match-string 0)
  19557. ""))))))))))
  19558. (declare-function message-goto-body "message" ())
  19559. (defvar message-cite-prefix-regexp) ; From message.el
  19560. (defvar org-element-all-objects) ; From org-element.el
  19561. (defun org-fill-paragraph (&optional justify)
  19562. "Fill element at point, when applicable.
  19563. This function only applies to comment blocks, comments, example
  19564. blocks and paragraphs. Also, as a special case, re-align table
  19565. when point is at one.
  19566. If JUSTIFY is non-nil (interactively, with prefix argument),
  19567. justify as well. If `sentence-end-double-space' is non-nil, then
  19568. period followed by one space does not end a sentence, so don't
  19569. break a line there. The variable `fill-column' controls the
  19570. width for filling.
  19571. For convenience, when point is at a plain list, an item or
  19572. a footnote definition, try to fill the first paragraph within."
  19573. ;; Falls back on message-fill-paragraph when necessary
  19574. (interactive)
  19575. (if (and (derived-mode-p 'message-mode)
  19576. (or (not (message-in-body-p))
  19577. (save-excursion (move-beginning-of-line 1)
  19578. (looking-at message-cite-prefix-regexp))))
  19579. (let ((fill-paragraph-function
  19580. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19581. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19582. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19583. (paragraph-separate
  19584. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19585. (fill-paragraph nil))
  19586. (save-excursion
  19587. ;; Move to end of line in order to get the first paragraph
  19588. ;; within a plain list or a footnote definition.
  19589. (end-of-line)
  19590. (let ((element (org-element-at-point)))
  19591. ;; First check if point is in a blank line at the beginning of
  19592. ;; the buffer. In that case, ignore filling.
  19593. (if (< (point) (org-element-property :begin element)) t
  19594. (case (org-element-type element)
  19595. ;; Align Org tables, leave table.el tables as-is.
  19596. (table-row (org-table-align) t)
  19597. (table
  19598. (when (eq (org-element-property :type element) 'org)
  19599. (org-table-align))
  19600. t)
  19601. (paragraph
  19602. ;; Paragraphs may contain `line-break' type objects.
  19603. (let ((beg (max (point-min)
  19604. (org-element-property :contents-begin element)))
  19605. (end (min (point-max)
  19606. (org-element-property :contents-end element))))
  19607. ;; Do nothing if point is at an affiliated keyword.
  19608. (if (< (point) beg) t
  19609. (when (derived-mode-p 'message-mode)
  19610. ;; In `message-mode', do not fill following
  19611. ;; citation in current paragraph nor text before
  19612. ;; message body.
  19613. (let ((body-start (save-excursion (message-goto-body))))
  19614. (when body-start (setq beg (max body-start beg))))
  19615. (when (save-excursion
  19616. (re-search-forward
  19617. (concat "^" message-cite-prefix-regexp) end t))
  19618. (setq end (match-beginning 0))))
  19619. ;; Fill paragraph, taking line breaks into
  19620. ;; consideration. For that, slice the paragraph
  19621. ;; using line breaks as separators, and fill the
  19622. ;; parts in reverse order to avoid messing with
  19623. ;; markers.
  19624. (save-excursion
  19625. (goto-char end)
  19626. (mapc
  19627. (lambda (pos)
  19628. (fill-region-as-paragraph pos (point) justify)
  19629. (goto-char pos))
  19630. ;; Find the list of ending positions for line
  19631. ;; breaks in the current paragraph. Add paragraph
  19632. ;; beginning to include first slice.
  19633. (nreverse
  19634. (cons
  19635. beg
  19636. (org-element-map
  19637. (org-element--parse-objects
  19638. beg end nil org-element-all-objects)
  19639. 'line-break
  19640. (lambda (lb) (org-element-property :end lb)))))))
  19641. t)))
  19642. ;; Contents of `comment-block' type elements should be
  19643. ;; filled as plain text, but only if point is within block
  19644. ;; markers.
  19645. (comment-block
  19646. (let* ((case-fold-search t)
  19647. (beg (save-excursion
  19648. (goto-char (org-element-property :begin element))
  19649. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19650. (forward-line)
  19651. (point)))
  19652. (end (save-excursion
  19653. (goto-char (org-element-property :end element))
  19654. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19655. (line-beginning-position))))
  19656. (when (and (>= (point) beg) (< (point) end))
  19657. (fill-region-as-paragraph
  19658. (save-excursion
  19659. (end-of-line)
  19660. (re-search-backward "^[ \t]*$" beg 'move)
  19661. (line-beginning-position))
  19662. (save-excursion
  19663. (beginning-of-line)
  19664. (re-search-forward "^[ \t]*$" end 'move)
  19665. (line-beginning-position))
  19666. justify)))
  19667. t)
  19668. ;; Fill comments.
  19669. (comment (fill-comment-paragraph justify))
  19670. ;; Ignore every other element.
  19671. (otherwise t)))))))
  19672. (defun org-auto-fill-function ()
  19673. "Auto-fill function."
  19674. ;; Check if auto-filling is meaningful.
  19675. (let ((fc (current-fill-column)))
  19676. (when (and fc (> (current-column) fc))
  19677. (let ((fill-prefix (org-adaptive-fill-function)))
  19678. (when fill-prefix (do-auto-fill))))))
  19679. (defun org-comment-line-break-function (&optional soft)
  19680. "Break line at point and indent, continuing comment if within one.
  19681. The inserted newline is marked hard if variable
  19682. `use-hard-newlines' is true, unless optional argument SOFT is
  19683. non-nil."
  19684. (if soft (insert-and-inherit ?\n) (newline 1))
  19685. (save-excursion (forward-char -1) (delete-horizontal-space))
  19686. (delete-horizontal-space)
  19687. (indent-to-left-margin)
  19688. (insert-before-markers-and-inherit fill-prefix))
  19689. ;;; Comments
  19690. ;; Org comments syntax is quite complex. It requires the entire line
  19691. ;; to be just a comment. Also, even with the right syntax at the
  19692. ;; beginning of line, some some elements (i.e. verse-block or
  19693. ;; example-block) don't accept comments. Usual Emacs comment commands
  19694. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19695. ;; Org still relies on `comment-dwim', but cannot trust
  19696. ;; `comment-only-p'. So, `comment-region-function' and
  19697. ;; `uncomment-region-function' both point
  19698. ;; to`org-comment-or-uncomment-region'. Eventually,
  19699. ;; `org-insert-comment' takes care of insertion of comments at the
  19700. ;; beginning of line.
  19701. ;; `org-setup-comments-handling' install comments related variables
  19702. ;; during `org-mode' initialization.
  19703. (defun org-setup-comments-handling ()
  19704. (interactive)
  19705. (org-set-local 'comment-use-syntax nil)
  19706. (org-set-local 'comment-start "# ")
  19707. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19708. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19709. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19710. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19711. (defun org-insert-comment ()
  19712. "Insert an empty comment above current line.
  19713. If the line is empty, insert comment at its beginning."
  19714. (beginning-of-line)
  19715. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19716. (org-indent-line)
  19717. (insert "# "))
  19718. (defvar comment-empty-lines) ; From newcomment.el.
  19719. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19720. "Comment or uncomment each non-blank line in the region.
  19721. Uncomment each non-blank line between BEG and END if it only
  19722. contains commented lines. Otherwise, comment them."
  19723. (save-restriction
  19724. ;; Restrict region
  19725. (narrow-to-region (save-excursion (goto-char beg)
  19726. (skip-chars-forward " \r\t\n" end)
  19727. (line-beginning-position))
  19728. (save-excursion (goto-char end)
  19729. (skip-chars-backward " \r\t\n" beg)
  19730. (line-end-position)))
  19731. (let ((uncommentp
  19732. ;; UNCOMMENTP is non-nil when every non blank line between
  19733. ;; BEG and END is a comment.
  19734. (save-excursion
  19735. (goto-char (point-min))
  19736. (while (and (not (eobp))
  19737. (let ((element (org-element-at-point)))
  19738. (and (eq (org-element-type element) 'comment)
  19739. (goto-char (min (point-max)
  19740. (org-element-property
  19741. :end element)))))))
  19742. (eobp))))
  19743. (if uncommentp
  19744. ;; Only blank lines and comments in region: uncomment it.
  19745. (save-excursion
  19746. (goto-char (point-min))
  19747. (while (not (eobp))
  19748. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  19749. (replace-match "" nil nil nil 1))
  19750. (forward-line)))
  19751. ;; Comment each line in region.
  19752. (let ((min-indent (point-max)))
  19753. ;; First find the minimum indentation across all lines.
  19754. (save-excursion
  19755. (goto-char (point-min))
  19756. (while (and (not (eobp)) (not (zerop min-indent)))
  19757. (unless (looking-at "[ \t]*$")
  19758. (setq min-indent (min min-indent (current-indentation))))
  19759. (forward-line)))
  19760. ;; Then loop over all lines.
  19761. (save-excursion
  19762. (goto-char (point-min))
  19763. (while (not (eobp))
  19764. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  19765. (org-move-to-column min-indent t)
  19766. (insert comment-start))
  19767. (forward-line))))))))
  19768. ;;; Other stuff.
  19769. (defun org-toggle-fixed-width-section (arg)
  19770. "Toggle the fixed-width export.
  19771. If there is no active region, the QUOTE keyword at the current headline is
  19772. inserted or removed. When present, it causes the text between this headline
  19773. and the next to be exported as fixed-width text, and unmodified.
  19774. If there is an active region, this command adds or removes a colon as the
  19775. first character of this line. If the first character of a line is a colon,
  19776. this line is also exported in fixed-width font."
  19777. (interactive "P")
  19778. (let* ((cc 0)
  19779. (regionp (org-region-active-p))
  19780. (beg (if regionp (region-beginning) (point)))
  19781. (end (if regionp (region-end)))
  19782. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19783. (case-fold-search nil)
  19784. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19785. off)
  19786. (if regionp
  19787. (save-excursion
  19788. (goto-char beg)
  19789. (setq cc (current-column))
  19790. (beginning-of-line 1)
  19791. (setq off (looking-at re))
  19792. (while (> nlines 0)
  19793. (setq nlines (1- nlines))
  19794. (beginning-of-line 1)
  19795. (cond
  19796. (arg
  19797. (org-move-to-column cc t)
  19798. (insert ": \n")
  19799. (forward-line -1))
  19800. ((and off (looking-at re))
  19801. (replace-match "" t t nil 1))
  19802. ((not off) (org-move-to-column cc t) (insert ": ")))
  19803. (forward-line 1)))
  19804. (save-excursion
  19805. (org-back-to-heading)
  19806. (cond
  19807. ((looking-at (format org-heading-keyword-regexp-format
  19808. org-quote-string))
  19809. (goto-char (match-end 1))
  19810. (looking-at (concat " +" org-quote-string))
  19811. (replace-match "" t t)
  19812. (when (eolp) (insert " ")))
  19813. ((looking-at org-outline-regexp)
  19814. (goto-char (match-end 0))
  19815. (insert org-quote-string " ")))))))
  19816. (defun org-reftex-citation ()
  19817. "Use reftex-citation to insert a citation into the buffer.
  19818. This looks for a line like
  19819. #+BIBLIOGRAPHY: foo plain option:-d
  19820. and derives from it that foo.bib is the bibliography file relevant
  19821. for this document. It then installs the necessary environment for RefTeX
  19822. to work in this buffer and calls `reftex-citation' to insert a citation
  19823. into the buffer.
  19824. Export of such citations to both LaTeX and HTML is handled by the contributed
  19825. package org-exp-bibtex by Taru Karttunen."
  19826. (interactive)
  19827. (let ((reftex-docstruct-symbol 'rds)
  19828. (reftex-cite-format "\\cite{%l}")
  19829. rds bib)
  19830. (save-excursion
  19831. (save-restriction
  19832. (widen)
  19833. (let ((case-fold-search t)
  19834. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19835. (if (not (save-excursion
  19836. (or (re-search-forward re nil t)
  19837. (re-search-backward re nil t))))
  19838. (error "No bibliography defined in file")
  19839. (setq bib (concat (match-string 1) ".bib")
  19840. rds (list (list 'bib bib)))))))
  19841. (call-interactively 'reftex-citation)))
  19842. ;;;; Functions extending outline functionality
  19843. (defun org-beginning-of-line (&optional arg)
  19844. "Go to the beginning of the current line. If that is invisible, continue
  19845. to a visible line beginning. This makes the function of C-a more intuitive.
  19846. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19847. first attempt, and only move to after the tags when the cursor is already
  19848. beyond the end of the headline."
  19849. (interactive "P")
  19850. (let ((pos (point))
  19851. (special (if (consp org-special-ctrl-a/e)
  19852. (car org-special-ctrl-a/e)
  19853. org-special-ctrl-a/e))
  19854. refpos)
  19855. (if (org-bound-and-true-p line-move-visual)
  19856. (beginning-of-visual-line 1)
  19857. (beginning-of-line 1))
  19858. (if (and arg (fboundp 'move-beginning-of-line))
  19859. (call-interactively 'move-beginning-of-line)
  19860. (if (bobp)
  19861. nil
  19862. (backward-char 1)
  19863. (if (org-truely-invisible-p)
  19864. (while (and (not (bobp)) (org-truely-invisible-p))
  19865. (backward-char 1)
  19866. (beginning-of-line 1))
  19867. (forward-char 1))))
  19868. (when special
  19869. (cond
  19870. ((and (looking-at org-complex-heading-regexp)
  19871. (= (char-after (match-end 1)) ?\ ))
  19872. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19873. (point-at-eol)))
  19874. (goto-char
  19875. (if (eq special t)
  19876. (cond ((> pos refpos) refpos)
  19877. ((= pos (point)) refpos)
  19878. (t (point)))
  19879. (cond ((> pos (point)) (point))
  19880. ((not (eq last-command this-command)) (point))
  19881. (t refpos)))))
  19882. ((org-at-item-p)
  19883. ;; Being at an item and not looking at an the item means point
  19884. ;; was previously moved to beginning of a visual line, which
  19885. ;; doesn't contain the item. Therefore, do nothing special,
  19886. ;; just stay here.
  19887. (when (looking-at org-list-full-item-re)
  19888. ;; Set special position at first white space character after
  19889. ;; bullet, and check-box, if any.
  19890. (let ((after-bullet
  19891. (let ((box (match-end 3)))
  19892. (if (not box) (match-end 1)
  19893. (let ((after (char-after box)))
  19894. (if (and after (= after ? )) (1+ box) box))))))
  19895. ;; Special case: Move point to special position when
  19896. ;; currently after it or at beginning of line.
  19897. (if (eq special t)
  19898. (when (or (> pos after-bullet) (= (point) pos))
  19899. (goto-char after-bullet))
  19900. ;; Reversed case: Move point to special position when
  19901. ;; point was already at beginning of line and command is
  19902. ;; repeated.
  19903. (when (and (= (point) pos) (eq last-command this-command))
  19904. (goto-char after-bullet))))))))
  19905. (org-no-warnings
  19906. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19907. (defun org-end-of-line (&optional arg)
  19908. "Go to the end of the line.
  19909. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19910. first attempt, and only move to after the tags when the cursor is already
  19911. beyond the end of the headline."
  19912. (interactive "P")
  19913. (let ((special (if (consp org-special-ctrl-a/e)
  19914. (cdr org-special-ctrl-a/e)
  19915. org-special-ctrl-a/e)))
  19916. (cond
  19917. ((or (not special) arg
  19918. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19919. (call-interactively
  19920. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19921. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19922. (t 'end-of-line))))
  19923. ((org-at-heading-p)
  19924. (let ((pos (point)))
  19925. (beginning-of-line 1)
  19926. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19927. (if (eq special t)
  19928. (if (or (< pos (match-beginning 1))
  19929. (= pos (match-end 0)))
  19930. (goto-char (match-beginning 1))
  19931. (goto-char (match-end 0)))
  19932. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19933. (goto-char (match-end 0))
  19934. (goto-char (match-beginning 1))))
  19935. (call-interactively (if (fboundp 'move-end-of-line)
  19936. 'move-end-of-line
  19937. 'end-of-line)))))
  19938. ((org-at-drawer-p)
  19939. (move-end-of-line 1)
  19940. (when (overlays-at (1- (point))) (backward-char 1)))
  19941. ;; At an item: Move before any hidden text.
  19942. (t (call-interactively
  19943. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19944. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19945. (t 'end-of-line)))))
  19946. (org-no-warnings
  19947. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19948. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19949. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19950. (defun org-backward-sentence (&optional arg)
  19951. "Go to beginning of sentence, or beginning of table field.
  19952. This will call `backward-sentence' or `org-table-beginning-of-field',
  19953. depending on context."
  19954. (interactive "P")
  19955. (cond
  19956. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19957. (t (call-interactively 'backward-sentence))))
  19958. (defun org-forward-sentence (&optional arg)
  19959. "Go to end of sentence, or end of table field.
  19960. This will call `forward-sentence' or `org-table-end-of-field',
  19961. depending on context."
  19962. (interactive "P")
  19963. (cond
  19964. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19965. (t (call-interactively 'forward-sentence))))
  19966. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19967. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19968. (defun org-kill-line (&optional arg)
  19969. "Kill line, to tags or end of line."
  19970. (interactive "P")
  19971. (cond
  19972. ((or (not org-special-ctrl-k)
  19973. (bolp)
  19974. (not (org-at-heading-p)))
  19975. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19976. org-ctrl-k-protect-subtree)
  19977. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19978. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19979. (error "C-k aborted - would kill hidden subtree")))
  19980. (call-interactively
  19981. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  19982. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19983. (kill-region (point) (match-beginning 1))
  19984. (org-set-tags nil t))
  19985. (t (kill-region (point) (point-at-eol)))))
  19986. (define-key org-mode-map "\C-k" 'org-kill-line)
  19987. (defun org-yank (&optional arg)
  19988. "Yank. If the kill is a subtree, treat it specially.
  19989. This command will look at the current kill and check if is a single
  19990. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19991. cursor is at the beginning of a line or after the stars of a currently
  19992. empty headline, then the yank is handled specially. How exactly depends
  19993. on the value of the following variables, both set by default.
  19994. org-yank-folded-subtrees
  19995. When set, the subtree(s) will be folded after insertion, but only
  19996. if doing so would now swallow text after the yanked text.
  19997. org-yank-adjusted-subtrees
  19998. When set, the subtree will be promoted or demoted in order to
  19999. fit into the local outline tree structure, which means that the level
  20000. will be adjusted so that it becomes the smaller one of the two
  20001. *visible* surrounding headings.
  20002. Any prefix to this command will cause `yank' to be called directly with
  20003. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20004. will just
  20005. plainly yank the text as it is.
  20006. \[1] The test checks if the first non-white line is a heading
  20007. and if there are no other headings with fewer stars."
  20008. (interactive "P")
  20009. (org-yank-generic 'yank arg))
  20010. (defun org-yank-generic (command arg)
  20011. "Perform some yank-like command.
  20012. This function implements the behavior described in the `org-yank'
  20013. documentation. However, it has been generalized to work for any
  20014. interactive command with similar behavior."
  20015. ;; pretend to be command COMMAND
  20016. (setq this-command command)
  20017. (if arg
  20018. (call-interactively command)
  20019. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20020. (and (org-kill-is-subtree-p)
  20021. (or (bolp)
  20022. (and (looking-at "[ \t]*$")
  20023. (string-match
  20024. "\\`\\*+\\'"
  20025. (buffer-substring (point-at-bol) (point)))))))
  20026. swallowp)
  20027. (cond
  20028. ((and subtreep org-yank-folded-subtrees)
  20029. (let ((beg (point))
  20030. end)
  20031. (if (and subtreep org-yank-adjusted-subtrees)
  20032. (org-paste-subtree nil nil 'for-yank)
  20033. (call-interactively command))
  20034. (setq end (point))
  20035. (goto-char beg)
  20036. (when (and (bolp) subtreep
  20037. (not (setq swallowp
  20038. (org-yank-folding-would-swallow-text beg end))))
  20039. (org-with-limited-levels
  20040. (or (looking-at org-outline-regexp)
  20041. (re-search-forward org-outline-regexp-bol end t))
  20042. (while (and (< (point) end) (looking-at org-outline-regexp))
  20043. (hide-subtree)
  20044. (org-cycle-show-empty-lines 'folded)
  20045. (condition-case nil
  20046. (outline-forward-same-level 1)
  20047. (error (goto-char end))))))
  20048. (when swallowp
  20049. (message
  20050. "Inserted text not folded because that would swallow text"))
  20051. (goto-char end)
  20052. (skip-chars-forward " \t\n\r")
  20053. (beginning-of-line 1)
  20054. (push-mark beg 'nomsg)))
  20055. ((and subtreep org-yank-adjusted-subtrees)
  20056. (let ((beg (point-at-bol)))
  20057. (org-paste-subtree nil nil 'for-yank)
  20058. (push-mark beg 'nomsg)))
  20059. (t
  20060. (call-interactively command))))))
  20061. (defun org-yank-folding-would-swallow-text (beg end)
  20062. "Would hide-subtree at BEG swallow any text after END?"
  20063. (let (level)
  20064. (org-with-limited-levels
  20065. (save-excursion
  20066. (goto-char beg)
  20067. (when (or (looking-at org-outline-regexp)
  20068. (re-search-forward org-outline-regexp-bol end t))
  20069. (setq level (org-outline-level)))
  20070. (goto-char end)
  20071. (skip-chars-forward " \t\r\n\v\f")
  20072. (if (or (eobp)
  20073. (and (bolp) (looking-at org-outline-regexp)
  20074. (<= (org-outline-level) level)))
  20075. nil ; Nothing would be swallowed
  20076. t))))) ; something would swallow
  20077. (define-key org-mode-map "\C-y" 'org-yank)
  20078. (defun org-truely-invisible-p ()
  20079. "Check if point is at a character currently not visible.
  20080. This version does not only check the character property, but also
  20081. `visible-mode'."
  20082. ;; Early versions of noutline don't have `outline-invisible-p'.
  20083. (if (org-bound-and-true-p visible-mode)
  20084. nil
  20085. (outline-invisible-p)))
  20086. (defun org-invisible-p2 ()
  20087. "Check if point is at a character currently not visible."
  20088. (save-excursion
  20089. (if (and (eolp) (not (bobp))) (backward-char 1))
  20090. ;; Early versions of noutline don't have `outline-invisible-p'.
  20091. (outline-invisible-p)))
  20092. (defun org-back-to-heading (&optional invisible-ok)
  20093. "Call `outline-back-to-heading', but provide a better error message."
  20094. (condition-case nil
  20095. (outline-back-to-heading invisible-ok)
  20096. (error (error "Before first headline at position %d in buffer %s"
  20097. (point) (current-buffer)))))
  20098. (defun org-before-first-heading-p ()
  20099. "Before first heading?"
  20100. (save-excursion
  20101. (end-of-line)
  20102. (null (re-search-backward org-outline-regexp-bol nil t))))
  20103. (defun org-at-heading-p (&optional ignored)
  20104. (outline-on-heading-p t))
  20105. ;; Compatibility alias with Org versions < 7.8.03
  20106. (defalias 'org-on-heading-p 'org-at-heading-p)
  20107. (defun org-at-comment-p nil
  20108. "Is cursor in a line starting with a # character?"
  20109. (save-excursion
  20110. (beginning-of-line)
  20111. (looking-at "^#")))
  20112. (defun org-at-drawer-p nil
  20113. "Is cursor at a drawer keyword?"
  20114. (save-excursion
  20115. (move-beginning-of-line 1)
  20116. (looking-at org-drawer-regexp)))
  20117. (defun org-at-block-p nil
  20118. "Is cursor at a block keyword?"
  20119. (save-excursion
  20120. (move-beginning-of-line 1)
  20121. (looking-at org-block-regexp)))
  20122. (defun org-point-at-end-of-empty-headline ()
  20123. "If point is at the end of an empty headline, return t, else nil.
  20124. If the heading only contains a TODO keyword, it is still still considered
  20125. empty."
  20126. (and (looking-at "[ \t]*$")
  20127. (when org-todo-line-regexp
  20128. (save-excursion
  20129. (beginning-of-line 1)
  20130. (let ((case-fold-search nil))
  20131. (looking-at org-todo-line-regexp)
  20132. (string= (match-string 3) ""))))))
  20133. (defun org-at-heading-or-item-p ()
  20134. (or (org-at-heading-p) (org-at-item-p)))
  20135. (defun org-at-target-p ()
  20136. (or (org-in-regexp org-radio-target-regexp)
  20137. (org-in-regexp org-target-regexp)))
  20138. ;; Compatibility alias with Org versions < 7.8.03
  20139. (defalias 'org-on-target-p 'org-at-target-p)
  20140. (defun org-up-heading-all (arg)
  20141. "Move to the heading line of which the present line is a subheading.
  20142. This function considers both visible and invisible heading lines.
  20143. With argument, move up ARG levels."
  20144. (if (fboundp 'outline-up-heading-all)
  20145. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20146. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20147. (defun org-up-heading-safe ()
  20148. "Move to the heading line of which the present line is a subheading.
  20149. This version will not throw an error. It will return the level of the
  20150. headline found, or nil if no higher level is found.
  20151. Also, this function will be a lot faster than `outline-up-heading',
  20152. because it relies on stars being the outline starters. This can really
  20153. make a significant difference in outlines with very many siblings."
  20154. (let (start-level re)
  20155. (org-back-to-heading t)
  20156. (setq start-level (funcall outline-level))
  20157. (if (equal start-level 1)
  20158. nil
  20159. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20160. (if (re-search-backward re nil t)
  20161. (funcall outline-level)))))
  20162. (defun org-first-sibling-p ()
  20163. "Is this heading the first child of its parents?"
  20164. (interactive)
  20165. (let ((re org-outline-regexp-bol)
  20166. level l)
  20167. (unless (org-at-heading-p t)
  20168. (error "Not at a heading"))
  20169. (setq level (funcall outline-level))
  20170. (save-excursion
  20171. (if (not (re-search-backward re nil t))
  20172. t
  20173. (setq l (funcall outline-level))
  20174. (< l level)))))
  20175. (defun org-goto-sibling (&optional previous)
  20176. "Goto the next sibling, even if it is invisible.
  20177. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20178. when a sibling was found. When none is found, return nil and don't
  20179. move point."
  20180. (let ((fun (if previous 're-search-backward 're-search-forward))
  20181. (pos (point))
  20182. (re org-outline-regexp-bol)
  20183. level l)
  20184. (when (condition-case nil (org-back-to-heading t) (error nil))
  20185. (setq level (funcall outline-level))
  20186. (catch 'exit
  20187. (or previous (forward-char 1))
  20188. (while (funcall fun re nil t)
  20189. (setq l (funcall outline-level))
  20190. (when (< l level) (goto-char pos) (throw 'exit nil))
  20191. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20192. (goto-char pos)
  20193. nil))))
  20194. (defun org-show-siblings ()
  20195. "Show all siblings of the current headline."
  20196. (save-excursion
  20197. (while (org-goto-sibling) (org-flag-heading nil)))
  20198. (save-excursion
  20199. (while (org-goto-sibling 'previous)
  20200. (org-flag-heading nil))))
  20201. (defun org-goto-first-child ()
  20202. "Goto the first child, even if it is invisible.
  20203. Return t when a child was found. Otherwise don't move point and
  20204. return nil."
  20205. (let (level (pos (point)) (re org-outline-regexp-bol))
  20206. (when (condition-case nil (org-back-to-heading t) (error nil))
  20207. (setq level (outline-level))
  20208. (forward-char 1)
  20209. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20210. (progn (goto-char (match-beginning 0)) t)
  20211. (goto-char pos) nil))))
  20212. (defun org-show-hidden-entry ()
  20213. "Show an entry where even the heading is hidden."
  20214. (save-excursion
  20215. (org-show-entry)))
  20216. (defun org-flag-heading (flag &optional entry)
  20217. "Flag the current heading. FLAG non-nil means make invisible.
  20218. When ENTRY is non-nil, show the entire entry."
  20219. (save-excursion
  20220. (org-back-to-heading t)
  20221. ;; Check if we should show the entire entry
  20222. (if entry
  20223. (progn
  20224. (org-show-entry)
  20225. (save-excursion
  20226. (and (outline-next-heading)
  20227. (org-flag-heading nil))))
  20228. (outline-flag-region (max (point-min) (1- (point)))
  20229. (save-excursion (outline-end-of-heading) (point))
  20230. flag))))
  20231. (defun org-get-next-sibling ()
  20232. "Move to next heading of the same level, and return point.
  20233. If there is no such heading, return nil.
  20234. This is like outline-next-sibling, but invisible headings are ok."
  20235. (let ((level (funcall outline-level)))
  20236. (outline-next-heading)
  20237. (while (and (not (eobp)) (> (funcall outline-level) level))
  20238. (outline-next-heading))
  20239. (if (or (eobp) (< (funcall outline-level) level))
  20240. nil
  20241. (point))))
  20242. (defun org-get-last-sibling ()
  20243. "Move to previous heading of the same level, and return point.
  20244. If there is no such heading, return nil."
  20245. (let ((opoint (point))
  20246. (level (funcall outline-level)))
  20247. (outline-previous-heading)
  20248. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20249. (while (and (> (funcall outline-level) level)
  20250. (not (bobp)))
  20251. (outline-previous-heading))
  20252. (if (< (funcall outline-level) level)
  20253. nil
  20254. (point)))))
  20255. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20256. "Goto to the end of a subtree."
  20257. ;; This contains an exact copy of the original function, but it uses
  20258. ;; `org-back-to-heading', to make it work also in invisible
  20259. ;; trees. And is uses an invisible-ok argument.
  20260. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20261. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20262. ;; with many children and grandchildren etc, this can be much faster
  20263. ;; than the outline version.
  20264. (org-back-to-heading invisible-ok)
  20265. (let ((first t)
  20266. (level (funcall outline-level)))
  20267. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20268. ;; A true heading (not a plain list item), in Org-mode
  20269. ;; This means we can easily find the end by looking
  20270. ;; only for the right number of stars. Using a regexp to do
  20271. ;; this is so much faster than using a Lisp loop.
  20272. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20273. (forward-char 1)
  20274. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20275. ;; something else, do it the slow way
  20276. (while (and (not (eobp))
  20277. (or first (> (funcall outline-level) level)))
  20278. (setq first nil)
  20279. (outline-next-heading)))
  20280. (unless to-heading
  20281. (if (memq (preceding-char) '(?\n ?\^M))
  20282. (progn
  20283. ;; Go to end of line before heading
  20284. (forward-char -1)
  20285. (if (memq (preceding-char) '(?\n ?\^M))
  20286. ;; leave blank line before heading
  20287. (forward-char -1))))))
  20288. (point))
  20289. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20290. "Use Org version in org-mode, for dramatic speed-up."
  20291. (if (derived-mode-p 'org-mode)
  20292. (progn
  20293. (org-end-of-subtree nil t)
  20294. (unless (eobp) (backward-char 1)))
  20295. ad-do-it))
  20296. (defun org-end-of-meta-data-and-drawers ()
  20297. "Jump to the first text after meta data and drawers in the current entry.
  20298. This will move over empty lines, lines with planning time stamps,
  20299. clocking lines, and drawers."
  20300. (org-back-to-heading t)
  20301. (let ((end (save-excursion (outline-next-heading) (point)))
  20302. (re (concat "\\(" org-drawer-regexp "\\)"
  20303. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20304. (forward-line 1)
  20305. (while (re-search-forward re end t)
  20306. (if (not (match-end 1))
  20307. ;; empty or planning line
  20308. (forward-line 1)
  20309. ;; a drawer, find the end
  20310. (re-search-forward "^[ \t]*:END:" end 'move)
  20311. (forward-line 1)))
  20312. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20313. (point)))
  20314. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20315. "Move forward to the arg'th subheading at same level as this one.
  20316. Stop at the first and last subheadings of a superior heading.
  20317. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20318. non-nil it will also look at invisible ones."
  20319. (interactive "p")
  20320. (org-back-to-heading invisible-ok)
  20321. (org-at-heading-p)
  20322. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20323. (re (format "^\\*\\{1,%d\\} " level))
  20324. l)
  20325. (forward-char 1)
  20326. (while (> arg 0)
  20327. (while (and (re-search-forward re nil 'move)
  20328. (setq l (- (match-end 0) (match-beginning 0) 1))
  20329. (= l level)
  20330. (not invisible-ok)
  20331. (progn (backward-char 1) (outline-invisible-p)))
  20332. (if (< l level) (setq arg 1)))
  20333. (setq arg (1- arg)))
  20334. (beginning-of-line 1)))
  20335. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20336. "Move backward to the arg'th subheading at same level as this one.
  20337. Stop at the first and last subheadings of a superior heading."
  20338. (interactive "p")
  20339. (org-back-to-heading)
  20340. (org-at-heading-p)
  20341. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20342. (re (format "^\\*\\{1,%d\\} " level))
  20343. l)
  20344. (while (> arg 0)
  20345. (while (and (re-search-backward re nil 'move)
  20346. (setq l (- (match-end 0) (match-beginning 0) 1))
  20347. (= l level)
  20348. (not invisible-ok)
  20349. (outline-invisible-p))
  20350. (if (< l level) (setq arg 1)))
  20351. (setq arg (1- arg)))))
  20352. ;;;###autoload
  20353. (defun org-forward-element ()
  20354. "Move forward by one element.
  20355. Move to the next element at the same level, when possible."
  20356. (interactive)
  20357. (cond ((eobp) (error "Cannot move further down"))
  20358. ((org-with-limited-levels (org-at-heading-p))
  20359. (let ((origin (point)))
  20360. (org-forward-heading-same-level 1)
  20361. (unless (org-with-limited-levels (org-at-heading-p))
  20362. (goto-char origin)
  20363. (error "Cannot move further down"))))
  20364. (t
  20365. (let* ((elem (org-element-at-point))
  20366. (end (org-element-property :end elem))
  20367. (parent (org-element-property :parent elem)))
  20368. (if (and parent (= (org-element-property :contents-end parent) end))
  20369. (goto-char (org-element-property :end parent))
  20370. (goto-char end))))))
  20371. ;;;###autoload
  20372. (defun org-backward-element ()
  20373. "Move backward by one element.
  20374. Move to the previous element at the same level, when possible."
  20375. (interactive)
  20376. (cond ((bobp) (error "Cannot move further up"))
  20377. ((org-with-limited-levels (org-at-heading-p))
  20378. ;; At an headline, move to the previous one, if any, or stay
  20379. ;; here.
  20380. (let ((origin (point)))
  20381. (org-backward-heading-same-level 1)
  20382. (unless (org-with-limited-levels (org-at-heading-p))
  20383. (goto-char origin)
  20384. (error "Cannot move further up"))))
  20385. (t
  20386. (let* ((trail (org-element-at-point 'keep-trail))
  20387. (elem (car trail))
  20388. (prev-elem (nth 1 trail))
  20389. (beg (org-element-property :begin elem)))
  20390. (cond
  20391. ;; Move to beginning of current element if point isn't
  20392. ;; there already.
  20393. ((/= (point) beg) (goto-char beg))
  20394. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20395. ((org-before-first-heading-p) (goto-char (point-min)))
  20396. (t (org-back-to-heading)))))))
  20397. ;;;###autoload
  20398. (defun org-up-element ()
  20399. "Move to upper element."
  20400. (interactive)
  20401. (if (org-with-limited-levels (org-at-heading-p))
  20402. (unless (org-up-heading-safe) (error "No surrounding element"))
  20403. (let* ((elem (org-element-at-point))
  20404. (parent (org-element-property :parent elem)))
  20405. (if parent (goto-char (org-element-property :begin parent))
  20406. (if (org-with-limited-levels (org-before-first-heading-p))
  20407. (error "No surrounding element")
  20408. (org-with-limited-levels (org-back-to-heading)))))))
  20409. ;;;###autoload
  20410. (defvar org-element-greater-elements)
  20411. (defun org-down-element ()
  20412. "Move to inner element."
  20413. (interactive)
  20414. (let ((element (org-element-at-point)))
  20415. (cond
  20416. ((memq (org-element-type element) '(plain-list table))
  20417. (goto-char (org-element-property :contents-begin element))
  20418. (forward-char))
  20419. ((memq (org-element-type element) org-element-greater-elements)
  20420. ;; If contents are hidden, first disclose them.
  20421. (when (org-element-property :hiddenp element) (org-cycle))
  20422. (goto-char (or (org-element-property :contents-begin element)
  20423. (error "No content for this element"))))
  20424. (t (error "No inner element")))))
  20425. ;;;###autoload
  20426. (defun org-drag-element-backward ()
  20427. "Move backward element at point."
  20428. (interactive)
  20429. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20430. (let* ((trail (org-element-at-point 'keep-trail))
  20431. (elem (car trail))
  20432. (prev-elem (nth 1 trail)))
  20433. ;; Error out if no previous element or previous element is
  20434. ;; a parent of the current one.
  20435. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20436. (error "Cannot drag element backward")
  20437. (let ((pos (point)))
  20438. (org-element-swap-A-B prev-elem elem)
  20439. (goto-char (+ (org-element-property :begin prev-elem)
  20440. (- pos (org-element-property :begin elem)))))))))
  20441. ;;;###autoload
  20442. (defun org-drag-element-forward ()
  20443. "Move forward element at point."
  20444. (interactive)
  20445. (let* ((pos (point))
  20446. (elem (org-element-at-point)))
  20447. (when (= (point-max) (org-element-property :end elem))
  20448. (error "Cannot drag element forward"))
  20449. (goto-char (org-element-property :end elem))
  20450. (let ((next-elem (org-element-at-point)))
  20451. (when (or (org-element-nested-p elem next-elem)
  20452. (and (eq (org-element-type next-elem) 'headline)
  20453. (not (eq (org-element-type elem) 'headline))))
  20454. (goto-char pos)
  20455. (error "Cannot drag element forward"))
  20456. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20457. ;; body's length (without final blanks) and by the length of
  20458. ;; blanks between ELEM and NEXT-ELEM.
  20459. (let ((size-next (- (save-excursion
  20460. (goto-char (org-element-property :end next-elem))
  20461. (skip-chars-backward " \r\t\n")
  20462. (forward-line)
  20463. ;; Small correction if buffer doesn't end
  20464. ;; with a newline character.
  20465. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20466. (org-element-property :begin next-elem)))
  20467. (size-blank (- (org-element-property :end elem)
  20468. (save-excursion
  20469. (goto-char (org-element-property :end elem))
  20470. (skip-chars-backward " \r\t\n")
  20471. (forward-line)
  20472. (point)))))
  20473. (org-element-swap-A-B elem next-elem)
  20474. (goto-char (+ pos size-next size-blank))))))
  20475. ;;;###autoload
  20476. (defun org-mark-element ()
  20477. "Put point at beginning of this element, mark at end.
  20478. Interactively, if this command is repeated or (in Transient Mark
  20479. mode) if the mark is active, it marks the next element after the
  20480. ones already marked."
  20481. (interactive)
  20482. (let (deactivate-mark)
  20483. (if (and (org-called-interactively-p 'any)
  20484. (or (and (eq last-command this-command) (mark t))
  20485. (and transient-mark-mode mark-active)))
  20486. (set-mark
  20487. (save-excursion
  20488. (goto-char (mark))
  20489. (goto-char (org-element-property :end (org-element-at-point)))))
  20490. (let ((element (org-element-at-point)))
  20491. (end-of-line)
  20492. (push-mark (org-element-property :end element) t t)
  20493. (goto-char (org-element-property :begin element))))))
  20494. ;;;###autoload
  20495. (defun org-narrow-to-element ()
  20496. "Narrow buffer to current element."
  20497. (interactive)
  20498. (let ((elem (org-element-at-point)))
  20499. (cond
  20500. ((eq (car elem) 'headline)
  20501. (narrow-to-region
  20502. (org-element-property :begin elem)
  20503. (org-element-property :end elem)))
  20504. ((memq (car elem) org-element-greater-elements)
  20505. (narrow-to-region
  20506. (org-element-property :contents-begin elem)
  20507. (org-element-property :contents-end elem)))
  20508. (t
  20509. (narrow-to-region
  20510. (org-element-property :begin elem)
  20511. (org-element-property :end elem))))))
  20512. ;;;###autoload
  20513. (defun org-transpose-element ()
  20514. "Transpose current and previous elements, keeping blank lines between.
  20515. Point is moved after both elements."
  20516. (interactive)
  20517. (org-skip-whitespace)
  20518. (let ((end (org-element-property :end (org-element-at-point))))
  20519. (org-drag-element-backward)
  20520. (goto-char end)))
  20521. ;;;###autoload
  20522. (defun org-unindent-buffer ()
  20523. "Un-indent the visible part of the buffer.
  20524. Relative indentation (between items, inside blocks, etc.) isn't
  20525. modified."
  20526. (interactive)
  20527. (unless (eq major-mode 'org-mode)
  20528. (error "Cannot un-indent a buffer not in Org mode"))
  20529. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20530. unindent-tree ; For byte-compiler.
  20531. (unindent-tree
  20532. (function
  20533. (lambda (contents)
  20534. (mapc
  20535. (lambda (element)
  20536. (if (memq (org-element-type element) '(headline section))
  20537. (funcall unindent-tree (org-element-contents element))
  20538. (save-excursion
  20539. (save-restriction
  20540. (narrow-to-region
  20541. (org-element-property :begin element)
  20542. (org-element-property :end element))
  20543. (org-do-remove-indentation)))))
  20544. (reverse contents))))))
  20545. (funcall unindent-tree (org-element-contents parse-tree))))
  20546. (defun org-show-subtree ()
  20547. "Show everything after this heading at deeper levels."
  20548. (interactive)
  20549. (outline-flag-region
  20550. (point)
  20551. (save-excursion
  20552. (org-end-of-subtree t t))
  20553. nil))
  20554. (defun org-show-entry ()
  20555. "Show the body directly following this heading.
  20556. Show the heading too, if it is currently invisible."
  20557. (interactive)
  20558. (save-excursion
  20559. (condition-case nil
  20560. (progn
  20561. (org-back-to-heading t)
  20562. (outline-flag-region
  20563. (max (point-min) (1- (point)))
  20564. (save-excursion
  20565. (if (re-search-forward
  20566. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20567. (match-beginning 1)
  20568. (point-max)))
  20569. nil)
  20570. (org-cycle-hide-drawers 'children))
  20571. (error nil))))
  20572. (defun org-make-options-regexp (kwds &optional extra)
  20573. "Make a regular expression for keyword lines."
  20574. (concat
  20575. "^#\\+\\("
  20576. (mapconcat 'regexp-quote kwds "\\|")
  20577. (if extra (concat "\\|" extra))
  20578. "\\):[ \t]*\\(.*\\)"))
  20579. ;; Make isearch reveal the necessary context
  20580. (defun org-isearch-end ()
  20581. "Reveal context after isearch exits."
  20582. (when isearch-success ; only if search was successful
  20583. (if (featurep 'xemacs)
  20584. ;; Under XEmacs, the hook is run in the correct place,
  20585. ;; we directly show the context.
  20586. (org-show-context 'isearch)
  20587. ;; In Emacs the hook runs *before* restoring the overlays.
  20588. ;; So we have to use a one-time post-command-hook to do this.
  20589. ;; (Emacs 22 has a special variable, see function `org-mode')
  20590. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20591. isearch-mode-end-hook-quit)
  20592. ;; Only when the isearch was not quitted.
  20593. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20594. 'append 'local)))))
  20595. (defun org-isearch-post-command ()
  20596. "Remove self from hook, and show context."
  20597. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20598. (org-show-context 'isearch))
  20599. ;;;; Integration with and fixes for other packages
  20600. ;;; Imenu support
  20601. (defvar org-imenu-markers nil
  20602. "All markers currently used by Imenu.")
  20603. (make-variable-buffer-local 'org-imenu-markers)
  20604. (defun org-imenu-new-marker (&optional pos)
  20605. "Return a new marker for use by Imenu, and remember the marker."
  20606. (let ((m (make-marker)))
  20607. (move-marker m (or pos (point)))
  20608. (push m org-imenu-markers)
  20609. m))
  20610. (defun org-imenu-get-tree ()
  20611. "Produce the index for Imenu."
  20612. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20613. (setq org-imenu-markers nil)
  20614. (let* ((n org-imenu-depth)
  20615. (re (concat "^" (org-get-limited-outline-regexp)))
  20616. (subs (make-vector (1+ n) nil))
  20617. (last-level 0)
  20618. m level head)
  20619. (save-excursion
  20620. (save-restriction
  20621. (widen)
  20622. (goto-char (point-max))
  20623. (while (re-search-backward re nil t)
  20624. (setq level (org-reduced-level (funcall outline-level)))
  20625. (when (and (<= level n)
  20626. (looking-at org-complex-heading-regexp))
  20627. (setq head (org-link-display-format
  20628. (org-match-string-no-properties 4))
  20629. m (org-imenu-new-marker))
  20630. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20631. (if (>= level last-level)
  20632. (push (cons head m) (aref subs level))
  20633. (push (cons head (aref subs (1+ level))) (aref subs level))
  20634. (loop for i from (1+ level) to n do (aset subs i nil)))
  20635. (setq last-level level)))))
  20636. (aref subs 1)))
  20637. (eval-after-load "imenu"
  20638. '(progn
  20639. (add-hook 'imenu-after-jump-hook
  20640. (lambda ()
  20641. (if (derived-mode-p 'org-mode)
  20642. (org-show-context 'org-goto))))))
  20643. (defun org-link-display-format (link)
  20644. "Replace a link with either the description, or the link target
  20645. if no description is present"
  20646. (save-match-data
  20647. (if (string-match org-bracket-link-analytic-regexp link)
  20648. (replace-match (if (match-end 5)
  20649. (match-string 5 link)
  20650. (concat (match-string 1 link)
  20651. (match-string 3 link)))
  20652. nil t link)
  20653. link)))
  20654. (defun org-toggle-link-display ()
  20655. "Toggle the literal or descriptive display of links."
  20656. (interactive)
  20657. (if org-descriptive-links
  20658. (progn (org-remove-from-invisibility-spec '(org-link))
  20659. (org-restart-font-lock)
  20660. (setq org-descriptive-links nil))
  20661. (progn (add-to-invisibility-spec '(org-link))
  20662. (org-restart-font-lock)
  20663. (setq org-descriptive-links t))))
  20664. ;; Speedbar support
  20665. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20666. "Overlay marking the agenda restriction line in speedbar.")
  20667. (overlay-put org-speedbar-restriction-lock-overlay
  20668. 'face 'org-agenda-restriction-lock)
  20669. (overlay-put org-speedbar-restriction-lock-overlay
  20670. 'help-echo "Agendas are currently limited to this item.")
  20671. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20672. (defun org-speedbar-set-agenda-restriction ()
  20673. "Restrict future agenda commands to the location at point in speedbar.
  20674. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20675. (interactive)
  20676. (require 'org-agenda)
  20677. (let (p m tp np dir txt)
  20678. (cond
  20679. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20680. 'org-imenu t))
  20681. (setq m (get-text-property p 'org-imenu-marker))
  20682. (with-current-buffer (marker-buffer m)
  20683. (goto-char m)
  20684. (org-agenda-set-restriction-lock 'subtree)))
  20685. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20686. 'speedbar-function 'speedbar-find-file))
  20687. (setq tp (previous-single-property-change
  20688. (1+ p) 'speedbar-function)
  20689. np (next-single-property-change
  20690. tp 'speedbar-function)
  20691. dir (speedbar-line-directory)
  20692. txt (buffer-substring-no-properties (or tp (point-min))
  20693. (or np (point-max))))
  20694. (with-current-buffer (find-file-noselect
  20695. (let ((default-directory dir))
  20696. (expand-file-name txt)))
  20697. (unless (derived-mode-p 'org-mode)
  20698. (error "Cannot restrict to non-Org-mode file"))
  20699. (org-agenda-set-restriction-lock 'file)))
  20700. (t (error "Don't know how to restrict Org-mode's agenda")))
  20701. (move-overlay org-speedbar-restriction-lock-overlay
  20702. (point-at-bol) (point-at-eol))
  20703. (setq current-prefix-arg nil)
  20704. (org-agenda-maybe-redo)))
  20705. (eval-after-load "speedbar"
  20706. '(progn
  20707. (speedbar-add-supported-extension ".org")
  20708. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20709. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20710. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20711. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20712. (add-hook 'speedbar-visiting-tag-hook
  20713. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20714. ;;; Fixes and Hacks for problems with other packages
  20715. ;; Make flyspell not check words in links, to not mess up our keymap
  20716. (defun org-mode-flyspell-verify ()
  20717. "Don't let flyspell put overlays at active buttons, or on
  20718. {todo,all-time,additional-option-like}-keywords."
  20719. (let ((pos (max (1- (point)) (point-min)))
  20720. (word (thing-at-point 'word)))
  20721. (and (not (get-text-property pos 'keymap))
  20722. (not (get-text-property pos 'org-no-flyspell))
  20723. (not (member word org-todo-keywords-1))
  20724. (not (member word org-all-time-keywords))
  20725. (not (member word org-options-keywords))
  20726. (not (member word (mapcar 'car org-startup-options)))
  20727. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20728. (defun org-remove-flyspell-overlays-in (beg end)
  20729. "Remove flyspell overlays in region."
  20730. (and (org-bound-and-true-p flyspell-mode)
  20731. (fboundp 'flyspell-delete-region-overlays)
  20732. (flyspell-delete-region-overlays beg end))
  20733. (add-text-properties beg end '(org-no-flyspell t)))
  20734. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20735. (eval-after-load "bookmark"
  20736. '(if (boundp 'bookmark-after-jump-hook)
  20737. ;; We can use the hook
  20738. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20739. ;; Hook not available, use advice
  20740. (defadvice bookmark-jump (after org-make-visible activate)
  20741. "Make the position visible."
  20742. (org-bookmark-jump-unhide))))
  20743. ;; Make sure saveplace shows the location if it was hidden
  20744. (eval-after-load "saveplace"
  20745. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20746. "Make the position visible."
  20747. (org-bookmark-jump-unhide)))
  20748. ;; Make sure ecb shows the location if it was hidden
  20749. (eval-after-load "ecb"
  20750. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20751. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20752. (if (derived-mode-p 'org-mode)
  20753. (org-show-context))))
  20754. (defun org-bookmark-jump-unhide ()
  20755. "Unhide the current position, to show the bookmark location."
  20756. (and (derived-mode-p 'org-mode)
  20757. (or (outline-invisible-p)
  20758. (save-excursion (goto-char (max (point-min) (1- (point))))
  20759. (outline-invisible-p)))
  20760. (org-show-context 'bookmark-jump)))
  20761. ;; Make session.el ignore our circular variable
  20762. (eval-after-load "session"
  20763. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20764. ;;;; Experimental code
  20765. (defun org-closed-in-range ()
  20766. "Sparse tree of items closed in a certain time range.
  20767. Still experimental, may disappear in the future."
  20768. (interactive)
  20769. ;; Get the time interval from the user.
  20770. (let* ((time1 (org-float-time
  20771. (org-read-date nil 'to-time nil "Starting date: ")))
  20772. (time2 (org-float-time
  20773. (org-read-date nil 'to-time nil "End date:")))
  20774. ;; callback function
  20775. (callback (lambda ()
  20776. (let ((time
  20777. (org-float-time
  20778. (apply 'encode-time
  20779. (org-parse-time-string
  20780. (match-string 1))))))
  20781. ;; check if time in interval
  20782. (and (>= time time1) (<= time time2))))))
  20783. ;; make tree, check each match with the callback
  20784. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20785. ;;;; Finish up
  20786. (provide 'org)
  20787. (run-hooks 'org-load-hook)
  20788. ;;; org.el ends here