org.el 881 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t t)
  74. (require 'org-macs)
  75. (require 'org-compat)
  76. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  77. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  78. ;;
  79. ;; In Org buffers, the value of `outline-regexp' is that of
  80. ;; `org-outline-regexp'. The only function still directly relying on
  81. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  82. ;; job when `orgstruct-mode' is active.
  83. (defvar org-outline-regexp "\\*+ "
  84. "Regexp to match Org headlines.")
  85. (defvar org-outline-regexp-bol "^\\*+ "
  86. "Regexp to match Org headlines.
  87. This is similar to `org-outline-regexp' but additionally makes
  88. sure that we are at the beginning of the line.")
  89. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  90. "Matches a headline, putting stars and text into groups.
  91. Stars are put in group 1 and the trimmed body in group 2.")
  92. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  93. (unless (boundp 'calendar-view-holidays-initially-flag)
  94. (org-defvaralias 'calendar-view-holidays-initially-flag
  95. 'view-calendar-holidays-initially))
  96. (unless (boundp 'calendar-view-diary-initially-flag)
  97. (org-defvaralias 'calendar-view-diary-initially-flag
  98. 'view-diary-entries-initially))
  99. (unless (boundp 'diary-fancy-buffer)
  100. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  101. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  102. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  103. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  104. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  105. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  106. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  107. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  108. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  109. (declare-function orgtbl-mode "org-table" (&optional arg))
  110. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  111. (declare-function org-beamer-mode "ox-beamer" ())
  112. (declare-function org-table-edit-field "org-table" (arg))
  113. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  114. (declare-function org-id-get-create "org-id" (&optional force))
  115. (declare-function org-id-find-id-file "org-id" (id))
  116. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  117. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  118. (declare-function org-table-align "org-table" ())
  119. (declare-function org-table-paste-rectangle "org-table" ())
  120. (declare-function org-table-maybe-eval-formula "org-table" ())
  121. (declare-function org-table-maybe-recalculate-line "org-table" ())
  122. (declare-function org-element--parse-objects "org-element"
  123. (beg end acc restriction))
  124. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  125. (declare-function org-element-contents "org-element" (element))
  126. (declare-function org-element-context "org-element" (&optional element))
  127. (declare-function org-element-interpret-data "org-element"
  128. (data &optional parent))
  129. (declare-function org-element-map "org-element"
  130. (data types fun &optional info first-match no-recursion))
  131. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  132. (declare-function org-element-parse-buffer "org-element"
  133. (&optional granularity visible-only))
  134. (declare-function org-element-property "org-element" (property element))
  135. (declare-function org-element-put-property "org-element"
  136. (element property value))
  137. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  138. (declare-function org-element--parse-objects "org-element"
  139. (beg end acc restriction))
  140. (declare-function org-element-parse-buffer "org-element"
  141. (&optional granularity visible-only))
  142. (declare-function org-element-restriction "org-element" (element))
  143. (declare-function org-element-type "org-element" (element))
  144. ;; load languages based on value of `org-babel-load-languages'
  145. (defvar org-babel-load-languages)
  146. ;;;###autoload
  147. (defun org-babel-do-load-languages (sym value)
  148. "Load the languages defined in `org-babel-load-languages'."
  149. (set-default sym value)
  150. (mapc (lambda (pair)
  151. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  152. (if active
  153. (progn
  154. (require (intern (concat "ob-" lang))))
  155. (progn
  156. (funcall 'fmakunbound
  157. (intern (concat "org-babel-execute:" lang)))
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-expand-body:" lang)))))))
  160. org-babel-load-languages))
  161. (defcustom org-babel-load-languages '((emacs-lisp . t))
  162. "Languages which can be evaluated in Org-mode buffers.
  163. This list can be used to load support for any of the languages
  164. below, note that each language will depend on a different set of
  165. system executables and/or Emacs modes. When a language is
  166. \"loaded\", then code blocks in that language can be evaluated
  167. with `org-babel-execute-src-block' bound by default to C-c
  168. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  169. be set to remove code block evaluation from the C-c C-c
  170. keybinding. By default only Emacs Lisp (which has no
  171. requirements) is loaded."
  172. :group 'org-babel
  173. :set 'org-babel-do-load-languages
  174. :version "24.1"
  175. :type '(alist :tag "Babel Languages"
  176. :key-type
  177. (choice
  178. (const :tag "Awk" awk)
  179. (const :tag "C" C)
  180. (const :tag "R" R)
  181. (const :tag "Asymptote" asymptote)
  182. (const :tag "Calc" calc)
  183. (const :tag "Clojure" clojure)
  184. (const :tag "CSS" css)
  185. (const :tag "Ditaa" ditaa)
  186. (const :tag "Dot" dot)
  187. (const :tag "Emacs Lisp" emacs-lisp)
  188. (const :tag "Fortran" fortran)
  189. (const :tag "Gnuplot" gnuplot)
  190. (const :tag "Haskell" haskell)
  191. (const :tag "IO" io)
  192. (const :tag "Java" java)
  193. (const :tag "Javascript" js)
  194. (const :tag "LaTeX" latex)
  195. (const :tag "Ledger" ledger)
  196. (const :tag "Lilypond" lilypond)
  197. (const :tag "Lisp" lisp)
  198. (const :tag "Makefile" makefile)
  199. (const :tag "Maxima" maxima)
  200. (const :tag "Matlab" matlab)
  201. (const :tag "Mscgen" mscgen)
  202. (const :tag "Ocaml" ocaml)
  203. (const :tag "Octave" octave)
  204. (const :tag "Org" org)
  205. (const :tag "Perl" perl)
  206. (const :tag "Pico Lisp" picolisp)
  207. (const :tag "PlantUML" plantuml)
  208. (const :tag "Python" python)
  209. (const :tag "Ruby" ruby)
  210. (const :tag "Sass" sass)
  211. (const :tag "Scala" scala)
  212. (const :tag "Scheme" scheme)
  213. (const :tag "Screen" screen)
  214. (const :tag "Shell Script" sh)
  215. (const :tag "Shen" shen)
  216. (const :tag "Sql" sql)
  217. (const :tag "Sqlite" sqlite))
  218. :value-type (boolean :tag "Activate" :value t)))
  219. ;;;; Customization variables
  220. (defcustom org-clone-delete-id nil
  221. "Remove ID property of clones of a subtree.
  222. When non-nil, clones of a subtree don't inherit the ID property.
  223. Otherwise they inherit the ID property with a new unique
  224. identifier."
  225. :type 'boolean
  226. :version "24.1"
  227. :group 'org-id)
  228. ;;; Version
  229. (org-check-version)
  230. ;;;###autoload
  231. (defun org-version (&optional here full message)
  232. "Show the org-mode version in the echo area.
  233. With prefix argument HERE, insert it at point.
  234. When FULL is non-nil, use a verbose version string.
  235. When MESSAGE is non-nil, display a message with the version."
  236. (interactive "P")
  237. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  238. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  239. (load-suffixes (list ".el"))
  240. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  241. (org-trash (or
  242. (and (fboundp 'org-release) (fboundp 'org-git-version))
  243. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  244. (load-suffixes save-load-suffixes)
  245. (org-version (org-release))
  246. (git-version (org-git-version))
  247. (version (format "Org-mode version %s (%s @ %s)"
  248. org-version
  249. git-version
  250. (if org-install-dir
  251. (if (string= org-dir org-install-dir)
  252. org-install-dir
  253. (concat "mixed installation! " org-install-dir " and " org-dir))
  254. "org-loaddefs.el can not be found!")))
  255. (_version (if full version org-version)))
  256. (if (org-called-interactively-p 'interactive)
  257. (if here
  258. (insert version)
  259. (message version))
  260. (if message (message _version))
  261. _version)))
  262. (defconst org-version (org-version))
  263. ;;; Compatibility constants
  264. ;;; The custom variables
  265. (defgroup org nil
  266. "Outline-based notes management and organizer."
  267. :tag "Org"
  268. :group 'outlines
  269. :group 'calendar)
  270. (defcustom org-mode-hook nil
  271. "Mode hook for Org-mode, run after the mode was turned on."
  272. :group 'org
  273. :type 'hook)
  274. (defcustom org-load-hook nil
  275. "Hook that is run after org.el has been loaded."
  276. :group 'org
  277. :type 'hook)
  278. (defcustom org-log-buffer-setup-hook nil
  279. "Hook that is run after an Org log buffer is created."
  280. :group 'org
  281. :version "24.1"
  282. :type 'hook)
  283. (defvar org-modules) ; defined below
  284. (defvar org-modules-loaded nil
  285. "Have the modules been loaded already?")
  286. (defun org-load-modules-maybe (&optional force)
  287. "Load all extensions listed in `org-modules'."
  288. (when (or force (not org-modules-loaded))
  289. (mapc (lambda (ext)
  290. (condition-case nil (require ext)
  291. (error (message "Problems while trying to load feature `%s'" ext))))
  292. org-modules)
  293. (setq org-modules-loaded t)))
  294. (defun org-set-modules (var value)
  295. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  296. (set var value)
  297. (when (featurep 'org)
  298. (org-load-modules-maybe 'force)))
  299. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  300. "Modules that should always be loaded together with org.el.
  301. If a description starts with <C>, the file is not part of Emacs
  302. and loading it will require that you have downloaded and properly
  303. installed the Org mode distribution.
  304. You can also use this system to load external packages (i.e. neither Org
  305. core modules, nor modules from the CONTRIB directory). Just add symbols
  306. to the end of the list. If the package is called org-xyz.el, then you need
  307. to add the symbol `xyz', and the package must have a call to:
  308. \(provide 'org-xyz)
  309. For export specific modules, see also `org-export-backends'."
  310. :group 'org
  311. :set 'org-set-modules
  312. :version "24.4"
  313. :package-version '(Org . "8.0")
  314. :type
  315. '(set :greedy t
  316. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  317. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  318. (const :tag " crypt: Encryption of subtrees" org-crypt)
  319. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  320. (const :tag " docview: Links to doc-view buffers" org-docview)
  321. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  322. (const :tag " id: Global IDs for identifying entries" org-id)
  323. (const :tag " info: Links to Info nodes" org-info)
  324. (const :tag " habit: Track your consistency with habits" org-habit)
  325. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  326. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  327. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  328. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  329. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  330. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  331. (const :tag " mouse: Additional mouse support" org-mouse)
  332. (const :tag "C vm: Links to VM folders/messages" org-vm)
  333. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  334. (const :tag "C w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  335. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  336. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  337. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  338. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  339. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  340. (const :tag "C collector: Collect properties into tables" org-collector)
  341. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  342. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  343. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  344. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  345. (const :tag "C eval: Include command output as text" org-eval)
  346. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  347. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  348. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  349. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  350. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  351. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  352. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  353. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  354. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  355. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  356. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  357. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  358. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  359. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  360. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  361. (const :tag "C registry: A registry for Org-mode links" org-registry)
  362. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  363. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  364. (const :tag "C secretary: Team management with org-mode" org-secretary)
  365. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  366. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  367. (const :tag "C track: Keep up with Org-mode development" org-track)
  368. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  369. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  370. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  371. (defvar org-export-registered-backends) ; From ox.el
  372. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  373. (defcustom org-export-backends '(ascii html icalendar latex)
  374. "List of export back-ends that should be always available.
  375. If a description starts with <C>, the file is not part of Emacs
  376. and loading it will require that you have downloaded and properly
  377. installed the Org mode distribution.
  378. Unlike to `org-modules', libraries in this list will not be
  379. loaded along with Org, but only once the export framework is
  380. needed.
  381. This variable needs to be set before org.el is loaded. If you
  382. need to make a change while Emacs is running, use the customize
  383. interface or run the following code, where VALUE stands for the
  384. new value of the variable, after updating it:
  385. \(progn
  386. \(setq org-export-registered-backends
  387. \(org-remove-if-not
  388. \(lambda (backend)
  389. \(or (memq backend val)
  390. \(catch 'parentp
  391. \(mapc
  392. \(lambda (b)
  393. \(and (org-export-derived-backend-p b (car backend))
  394. \(throw 'parentp t)))
  395. val)
  396. nil)))
  397. org-export-registered-backends))
  398. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  399. \(dolist (backend val)
  400. \(cond
  401. \((not (load (format \"ox-%s\" backend) t t))
  402. \(message \"Problems while trying to load export back-end `%s'\"
  403. backend))
  404. \((not (memq backend new-list)) (push backend new-list))))
  405. \(set-default var new-list)))
  406. Adding a back-end to this list will also pull the back-end it
  407. depends on, if any."
  408. :group 'org
  409. :group 'org-export
  410. :version "24.4"
  411. :package-version '(Org . "8.0")
  412. :initialize 'custom-initialize-set
  413. :set (lambda (var val)
  414. (if (not (featurep 'ox)) (set-default var val)
  415. ;; Any back-end not required anymore (not present in VAL and not
  416. ;; a parent of any back-end in the new value) is removed from the
  417. ;; list of registered back-ends.
  418. (setq org-export-registered-backends
  419. (org-remove-if-not
  420. (lambda (backend)
  421. (or (memq backend val)
  422. (catch 'parentp
  423. (mapc
  424. (lambda (b)
  425. (and (org-export-derived-backend-p b (car backend))
  426. (throw 'parentp t)))
  427. val)
  428. nil)))
  429. org-export-registered-backends))
  430. ;; Now build NEW-LIST of both new back-ends and required
  431. ;; parents.
  432. (let ((new-list (mapcar 'car org-export-registered-backends)))
  433. (dolist (backend val)
  434. (cond
  435. ((not (load (format "ox-%s" backend) t t))
  436. (message "Problems while trying to load export back-end `%s'"
  437. backend))
  438. ((not (memq backend new-list)) (push backend new-list))))
  439. ;; Set VAR to that list with fixed dependencies.
  440. (set-default var new-list))))
  441. :type '(set :greedy t
  442. (const :tag " ascii Export buffer to ASCII format" ascii)
  443. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  444. (const :tag " html Export buffer to HTML format" html)
  445. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  446. (const :tag " latex Export buffer to LaTeX format" latex)
  447. (const :tag " man Export buffer to MAN format" man)
  448. (const :tag " md Export buffer to Markdown format" md)
  449. (const :tag " odt Export buffer to ODT format" odt)
  450. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  451. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  452. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  453. (const :tag "C groff Export buffer to Groff format" groff)
  454. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  455. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  456. (eval-after-load 'ox
  457. '(mapc
  458. (lambda (backend)
  459. (condition-case nil (require (intern (format "ox-%s" backend)))
  460. (error (message "Problems while trying to load export back-end `%s'"
  461. backend))))
  462. org-export-backends))
  463. (defcustom org-support-shift-select nil
  464. "Non-nil means make shift-cursor commands select text when possible.
  465. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  466. start selecting a region, or enlarge regions started in this way.
  467. In Org-mode, in special contexts, these same keys are used for
  468. other purposes, important enough to compete with shift selection.
  469. Org tries to balance these needs by supporting `shift-select-mode'
  470. outside these special contexts, under control of this variable.
  471. The default of this variable is nil, to avoid confusing behavior. Shifted
  472. cursor keys will then execute Org commands in the following contexts:
  473. - on a headline, changing TODO state (left/right) and priority (up/down)
  474. - on a time stamp, changing the time
  475. - in a plain list item, changing the bullet type
  476. - in a property definition line, switching between allowed values
  477. - in the BEGIN line of a clock table (changing the time block).
  478. Outside these contexts, the commands will throw an error.
  479. When this variable is t and the cursor is not in a special
  480. context, Org-mode will support shift-selection for making and
  481. enlarging regions. To make this more effective, the bullet
  482. cycling will no longer happen anywhere in an item line, but only
  483. if the cursor is exactly on the bullet.
  484. If you set this variable to the symbol `always', then the keys
  485. will not be special in headlines, property lines, and item lines,
  486. to make shift selection work there as well. If this is what you
  487. want, you can use the following alternative commands: `C-c C-t'
  488. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  489. can be used to switch TODO sets, `C-c -' to cycle item bullet
  490. types, and properties can be edited by hand or in column view.
  491. However, when the cursor is on a timestamp, shift-cursor commands
  492. will still edit the time stamp - this is just too good to give up.
  493. XEmacs user should have this variable set to nil, because
  494. `shift-select-mode' is in Emacs 23 or later only."
  495. :group 'org
  496. :type '(choice
  497. (const :tag "Never" nil)
  498. (const :tag "When outside special context" t)
  499. (const :tag "Everywhere except timestamps" always)))
  500. (defcustom org-loop-over-headlines-in-active-region nil
  501. "Shall some commands act upon headlines in the active region?
  502. When set to `t', some commands will be performed in all headlines
  503. within the active region.
  504. When set to `start-level', some commands will be performed in all
  505. headlines within the active region, provided that these headlines
  506. are of the same level than the first one.
  507. When set to a string, those commands will be performed on the
  508. matching headlines within the active region. Such string must be
  509. a tags/property/todo match as it is used in the agenda tags view.
  510. The list of commands is: `org-schedule', `org-deadline',
  511. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  512. `org-archive-to-archive-sibling'. The archiving commands skip
  513. already archived entries."
  514. :type '(choice (const :tag "Don't loop" nil)
  515. (const :tag "All headlines in active region" t)
  516. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  517. (string :tag "Tags/Property/Todo matcher"))
  518. :version "24.1"
  519. :group 'org-todo
  520. :group 'org-archive)
  521. (defgroup org-startup nil
  522. "Options concerning startup of Org-mode."
  523. :tag "Org Startup"
  524. :group 'org)
  525. (defcustom org-startup-folded t
  526. "Non-nil means entering Org-mode will switch to OVERVIEW.
  527. This can also be configured on a per-file basis by adding one of
  528. the following lines anywhere in the buffer:
  529. #+STARTUP: fold (or `overview', this is equivalent)
  530. #+STARTUP: nofold (or `showall', this is equivalent)
  531. #+STARTUP: content
  532. #+STARTUP: showeverything
  533. By default, this option is ignored when Org opens agenda files
  534. for the first time. If you want the agenda to honor the startup
  535. option, set `org-agenda-inhibit-startup' to nil."
  536. :group 'org-startup
  537. :type '(choice
  538. (const :tag "nofold: show all" nil)
  539. (const :tag "fold: overview" t)
  540. (const :tag "content: all headlines" content)
  541. (const :tag "show everything, even drawers" showeverything)))
  542. (defcustom org-startup-truncated t
  543. "Non-nil means entering Org-mode will set `truncate-lines'.
  544. This is useful since some lines containing links can be very long and
  545. uninteresting. Also tables look terrible when wrapped."
  546. :group 'org-startup
  547. :type 'boolean)
  548. (defcustom org-startup-indented nil
  549. "Non-nil means turn on `org-indent-mode' on startup.
  550. This can also be configured on a per-file basis by adding one of
  551. the following lines anywhere in the buffer:
  552. #+STARTUP: indent
  553. #+STARTUP: noindent"
  554. :group 'org-structure
  555. :type '(choice
  556. (const :tag "Not" nil)
  557. (const :tag "Globally (slow on startup in large files)" t)))
  558. (defcustom org-use-sub-superscripts t
  559. "Non-nil means interpret \"_\" and \"^\" for display.
  560. When this option is turned on, you can use TeX-like syntax for sub- and
  561. superscripts. Several characters after \"_\" or \"^\" will be
  562. considered as a single item - so grouping with {} is normally not
  563. needed. For example, the following things will be parsed as single
  564. sub- or superscripts.
  565. 10^24 or 10^tau several digits will be considered 1 item.
  566. 10^-12 or 10^-tau a leading sign with digits or a word
  567. x^2-y^3 will be read as x^2 - y^3, because items are
  568. terminated by almost any nonword/nondigit char.
  569. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  570. Still, ambiguity is possible - so when in doubt use {} to enclose
  571. the sub/superscript. If you set this variable to the symbol
  572. `{}', the braces are *required* in order to trigger
  573. interpretations as sub/superscript. This can be helpful in
  574. documents that need \"_\" frequently in plain text."
  575. :group 'org-startup
  576. :version "24.1"
  577. :type '(choice
  578. (const :tag "Always interpret" t)
  579. (const :tag "Only with braces" {})
  580. (const :tag "Never interpret" nil)))
  581. (defcustom org-startup-with-beamer-mode nil
  582. "Non-nil means turn on `org-beamer-mode' on startup.
  583. This can also be configured on a per-file basis by adding one of
  584. the following lines anywhere in the buffer:
  585. #+STARTUP: beamer"
  586. :group 'org-startup
  587. :version "24.1"
  588. :type 'boolean)
  589. (defcustom org-startup-align-all-tables nil
  590. "Non-nil means align all tables when visiting a file.
  591. This is useful when the column width in tables is forced with <N> cookies
  592. in table fields. Such tables will look correct only after the first re-align.
  593. This can also be configured on a per-file basis by adding one of
  594. the following lines anywhere in the buffer:
  595. #+STARTUP: align
  596. #+STARTUP: noalign"
  597. :group 'org-startup
  598. :type 'boolean)
  599. (defcustom org-startup-with-inline-images nil
  600. "Non-nil means show inline images when loading a new Org file.
  601. This can also be configured on a per-file basis by adding one of
  602. the following lines anywhere in the buffer:
  603. #+STARTUP: inlineimages
  604. #+STARTUP: noinlineimages"
  605. :group 'org-startup
  606. :version "24.1"
  607. :type 'boolean)
  608. (defcustom org-startup-with-latex-preview nil
  609. "Non-nil means preview LaTeX fragments when loading a new Org file.
  610. This can also be configured on a per-file basis by adding one of
  611. the followinglines anywhere in the buffer:
  612. #+STARTUP: latexpreview
  613. #+STARTUP: nolatexpreview"
  614. :group 'org-startup
  615. :version "24.4"
  616. :package-version '(Org . "8.0")
  617. :type 'boolean)
  618. (defcustom org-insert-mode-line-in-empty-file nil
  619. "Non-nil means insert the first line setting Org-mode in empty files.
  620. When the function `org-mode' is called interactively in an empty file, this
  621. normally means that the file name does not automatically trigger Org-mode.
  622. To ensure that the file will always be in Org-mode in the future, a
  623. line enforcing Org-mode will be inserted into the buffer, if this option
  624. has been set."
  625. :group 'org-startup
  626. :type 'boolean)
  627. (defcustom org-replace-disputed-keys nil
  628. "Non-nil means use alternative key bindings for some keys.
  629. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  630. These keys are also used by other packages like shift-selection-mode'
  631. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  632. If you want to use Org-mode together with one of these other modes,
  633. or more generally if you would like to move some Org-mode commands to
  634. other keys, set this variable and configure the keys with the variable
  635. `org-disputed-keys'.
  636. This option is only relevant at load-time of Org-mode, and must be set
  637. *before* org.el is loaded. Changing it requires a restart of Emacs to
  638. become effective."
  639. :group 'org-startup
  640. :type 'boolean)
  641. (defcustom org-use-extra-keys nil
  642. "Non-nil means use extra key sequence definitions for certain commands.
  643. This happens automatically if you run XEmacs or if `window-system'
  644. is nil. This variable lets you do the same manually. You must
  645. set it before loading org.
  646. Example: on Carbon Emacs 22 running graphically, with an external
  647. keyboard on a Powerbook, the default way of setting M-left might
  648. not work for either Alt or ESC. Setting this variable will make
  649. it work for ESC."
  650. :group 'org-startup
  651. :type 'boolean)
  652. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  653. (defcustom org-disputed-keys
  654. '(([(shift up)] . [(meta p)])
  655. ([(shift down)] . [(meta n)])
  656. ([(shift left)] . [(meta -)])
  657. ([(shift right)] . [(meta +)])
  658. ([(control shift right)] . [(meta shift +)])
  659. ([(control shift left)] . [(meta shift -)]))
  660. "Keys for which Org-mode and other modes compete.
  661. This is an alist, cars are the default keys, second element specifies
  662. the alternative to use when `org-replace-disputed-keys' is t.
  663. Keys can be specified in any syntax supported by `define-key'.
  664. The value of this option takes effect only at Org-mode's startup,
  665. therefore you'll have to restart Emacs to apply it after changing."
  666. :group 'org-startup
  667. :type 'alist)
  668. (defun org-key (key)
  669. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  670. Or return the original if not disputed.
  671. Also apply the translations defined in `org-xemacs-key-equivalents'."
  672. (when org-replace-disputed-keys
  673. (let* ((nkey (key-description key))
  674. (x (org-find-if (lambda (x)
  675. (equal (key-description (car x)) nkey))
  676. org-disputed-keys)))
  677. (setq key (if x (cdr x) key))))
  678. (when (featurep 'xemacs)
  679. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  680. key)
  681. (defun org-find-if (predicate seq)
  682. (catch 'exit
  683. (while seq
  684. (if (funcall predicate (car seq))
  685. (throw 'exit (car seq))
  686. (pop seq)))))
  687. (defun org-defkey (keymap key def)
  688. "Define a key, possibly translated, as returned by `org-key'."
  689. (define-key keymap (org-key key) def))
  690. (defcustom org-ellipsis nil
  691. "The ellipsis to use in the Org-mode outline.
  692. When nil, just use the standard three dots. When a string, use that instead,
  693. When a face, use the standard 3 dots, but with the specified face.
  694. The change affects only Org-mode (which will then use its own display table).
  695. Changing this requires executing `M-x org-mode' in a buffer to become
  696. effective."
  697. :group 'org-startup
  698. :type '(choice (const :tag "Default" nil)
  699. (face :tag "Face" :value org-warning)
  700. (string :tag "String" :value "...#")))
  701. (defvar org-display-table nil
  702. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  703. (defgroup org-keywords nil
  704. "Keywords in Org-mode."
  705. :tag "Org Keywords"
  706. :group 'org)
  707. (defcustom org-deadline-string "DEADLINE:"
  708. "String to mark deadline entries.
  709. A deadline is this string, followed by a time stamp. Should be a word,
  710. terminated by a colon. You can insert a schedule keyword and
  711. a timestamp with \\[org-deadline].
  712. Changes become only effective after restarting Emacs."
  713. :group 'org-keywords
  714. :type 'string)
  715. (defcustom org-scheduled-string "SCHEDULED:"
  716. "String to mark scheduled TODO entries.
  717. A schedule is this string, followed by a time stamp. Should be a word,
  718. terminated by a colon. You can insert a schedule keyword and
  719. a timestamp with \\[org-schedule].
  720. Changes become only effective after restarting Emacs."
  721. :group 'org-keywords
  722. :type 'string)
  723. (defcustom org-closed-string "CLOSED:"
  724. "String used as the prefix for timestamps logging closing a TODO entry."
  725. :group 'org-keywords
  726. :type 'string)
  727. (defcustom org-clock-string "CLOCK:"
  728. "String used as prefix for timestamps clocking work hours on an item."
  729. :group 'org-keywords
  730. :type 'string)
  731. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  732. org-scheduled-string "\\|"
  733. org-deadline-string "\\|"
  734. org-closed-string "\\|"
  735. org-clock-string "\\)")
  736. "Matches a line with planning or clock info.")
  737. (defcustom org-comment-string "COMMENT"
  738. "Entries starting with this keyword will never be exported.
  739. An entry can be toggled between COMMENT and normal with
  740. \\[org-toggle-comment].
  741. Changes become only effective after restarting Emacs."
  742. :group 'org-keywords
  743. :type 'string)
  744. (defcustom org-quote-string "QUOTE"
  745. "Entries starting with this keyword will be exported in fixed-width font.
  746. Quoting applies only to the text in the entry following the headline, and does
  747. not extend beyond the next headline, even if that is lower level.
  748. An entry can be toggled between QUOTE and normal with
  749. \\[org-toggle-fixed-width-section]."
  750. :group 'org-keywords
  751. :type 'string)
  752. (defconst org-repeat-re
  753. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  754. "Regular expression for specifying repeated events.
  755. After a match, group 1 contains the repeat expression.")
  756. (defgroup org-structure nil
  757. "Options concerning the general structure of Org-mode files."
  758. :tag "Org Structure"
  759. :group 'org)
  760. (defgroup org-reveal-location nil
  761. "Options about how to make context of a location visible."
  762. :tag "Org Reveal Location"
  763. :group 'org-structure)
  764. (defconst org-context-choice
  765. '(choice
  766. (const :tag "Always" t)
  767. (const :tag "Never" nil)
  768. (repeat :greedy t :tag "Individual contexts"
  769. (cons
  770. (choice :tag "Context"
  771. (const agenda)
  772. (const org-goto)
  773. (const occur-tree)
  774. (const tags-tree)
  775. (const link-search)
  776. (const mark-goto)
  777. (const bookmark-jump)
  778. (const isearch)
  779. (const default))
  780. (boolean))))
  781. "Contexts for the reveal options.")
  782. (defcustom org-show-hierarchy-above '((default . t))
  783. "Non-nil means show full hierarchy when revealing a location.
  784. Org-mode often shows locations in an org-mode file which might have
  785. been invisible before. When this is set, the hierarchy of headings
  786. above the exposed location is shown.
  787. Turning this off for example for sparse trees makes them very compact.
  788. Instead of t, this can also be an alist specifying this option for different
  789. contexts. Valid contexts are
  790. agenda when exposing an entry from the agenda
  791. org-goto when using the command `org-goto' on key C-c C-j
  792. occur-tree when using the command `org-occur' on key C-c /
  793. tags-tree when constructing a sparse tree based on tags matches
  794. link-search when exposing search matches associated with a link
  795. mark-goto when exposing the jump goal of a mark
  796. bookmark-jump when exposing a bookmark location
  797. isearch when exiting from an incremental search
  798. default default for all contexts not set explicitly"
  799. :group 'org-reveal-location
  800. :type org-context-choice)
  801. (defcustom org-show-following-heading '((default . nil))
  802. "Non-nil means show following heading when revealing a location.
  803. Org-mode often shows locations in an org-mode file which might have
  804. been invisible before. When this is set, the heading following the
  805. match is shown.
  806. Turning this off for example for sparse trees makes them very compact,
  807. but makes it harder to edit the location of the match. In such a case,
  808. use the command \\[org-reveal] to show more context.
  809. Instead of t, this can also be an alist specifying this option for different
  810. contexts. See `org-show-hierarchy-above' for valid contexts."
  811. :group 'org-reveal-location
  812. :type org-context-choice)
  813. (defcustom org-show-siblings '((default . nil) (isearch t))
  814. "Non-nil means show all sibling heading when revealing a location.
  815. Org-mode often shows locations in an org-mode file which might have
  816. been invisible before. When this is set, the sibling of the current entry
  817. heading are all made visible. If `org-show-hierarchy-above' is t,
  818. the same happens on each level of the hierarchy above the current entry.
  819. By default this is on for the isearch context, off for all other contexts.
  820. Turning this off for example for sparse trees makes them very compact,
  821. but makes it harder to edit the location of the match. In such a case,
  822. use the command \\[org-reveal] to show more context.
  823. Instead of t, this can also be an alist specifying this option for different
  824. contexts. See `org-show-hierarchy-above' for valid contexts."
  825. :group 'org-reveal-location
  826. :type org-context-choice)
  827. (defcustom org-show-entry-below '((default . nil))
  828. "Non-nil means show the entry below a headline when revealing a location.
  829. Org-mode often shows locations in an org-mode file which might have
  830. been invisible before. When this is set, the text below the headline that is
  831. exposed is also shown.
  832. By default this is off for all contexts.
  833. Instead of t, this can also be an alist specifying this option for different
  834. contexts. See `org-show-hierarchy-above' for valid contexts."
  835. :group 'org-reveal-location
  836. :type org-context-choice)
  837. (defcustom org-indirect-buffer-display 'other-window
  838. "How should indirect tree buffers be displayed?
  839. This applies to indirect buffers created with the commands
  840. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  841. Valid values are:
  842. current-window Display in the current window
  843. other-window Just display in another window.
  844. dedicated-frame Create one new frame, and re-use it each time.
  845. new-frame Make a new frame each time. Note that in this case
  846. previously-made indirect buffers are kept, and you need to
  847. kill these buffers yourself."
  848. :group 'org-structure
  849. :group 'org-agenda-windows
  850. :type '(choice
  851. (const :tag "In current window" current-window)
  852. (const :tag "In current frame, other window" other-window)
  853. (const :tag "Each time a new frame" new-frame)
  854. (const :tag "One dedicated frame" dedicated-frame)))
  855. (defcustom org-use-speed-commands nil
  856. "Non-nil means activate single letter commands at beginning of a headline.
  857. This may also be a function to test for appropriate locations where speed
  858. commands should be active."
  859. :group 'org-structure
  860. :type '(choice
  861. (const :tag "Never" nil)
  862. (const :tag "At beginning of headline stars" t)
  863. (function)))
  864. (defcustom org-speed-commands-user nil
  865. "Alist of additional speed commands.
  866. This list will be checked before `org-speed-commands-default'
  867. when the variable `org-use-speed-commands' is non-nil
  868. and when the cursor is at the beginning of a headline.
  869. The car if each entry is a string with a single letter, which must
  870. be assigned to `self-insert-command' in the global map.
  871. The cdr is either a command to be called interactively, a function
  872. to be called, or a form to be evaluated.
  873. An entry that is just a list with a single string will be interpreted
  874. as a descriptive headline that will be added when listing the speed
  875. commands in the Help buffer using the `?' speed command."
  876. :group 'org-structure
  877. :type '(repeat :value ("k" . ignore)
  878. (choice :value ("k" . ignore)
  879. (list :tag "Descriptive Headline" (string :tag "Headline"))
  880. (cons :tag "Letter and Command"
  881. (string :tag "Command letter")
  882. (choice
  883. (function)
  884. (sexp))))))
  885. (defgroup org-cycle nil
  886. "Options concerning visibility cycling in Org-mode."
  887. :tag "Org Cycle"
  888. :group 'org-structure)
  889. (defcustom org-cycle-skip-children-state-if-no-children t
  890. "Non-nil means skip CHILDREN state in entries that don't have any."
  891. :group 'org-cycle
  892. :type 'boolean)
  893. (defcustom org-cycle-max-level nil
  894. "Maximum level which should still be subject to visibility cycling.
  895. Levels higher than this will, for cycling, be treated as text, not a headline.
  896. When `org-odd-levels-only' is set, a value of N in this variable actually
  897. means 2N-1 stars as the limiting headline.
  898. When nil, cycle all levels.
  899. Note that the limiting level of cycling is also influenced by
  900. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  901. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  902. than its value."
  903. :group 'org-cycle
  904. :type '(choice
  905. (const :tag "No limit" nil)
  906. (integer :tag "Maximum level")))
  907. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  908. "Names of drawers. Drawers are not opened by cycling on the headline above.
  909. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  910. this:
  911. :DRAWERNAME:
  912. .....
  913. :END:
  914. The drawer \"PROPERTIES\" is special for capturing properties through
  915. the property API.
  916. Drawers can be defined on the per-file basis with a line like:
  917. #+DRAWERS: HIDDEN STATE PROPERTIES"
  918. :group 'org-structure
  919. :group 'org-cycle
  920. :type '(repeat (string :tag "Drawer Name")))
  921. (defcustom org-hide-block-startup nil
  922. "Non-nil means entering Org-mode will fold all blocks.
  923. This can also be set in on a per-file basis with
  924. #+STARTUP: hideblocks
  925. #+STARTUP: showblocks"
  926. :group 'org-startup
  927. :group 'org-cycle
  928. :type 'boolean)
  929. (defcustom org-cycle-global-at-bob nil
  930. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  931. This makes it possible to do global cycling without having to use S-TAB or
  932. \\[universal-argument] TAB. For this special case to work, the first line
  933. of the buffer must not be a headline -- it may be empty or some other text.
  934. When used in this way, `org-cycle-hook' is disabled temporarily to make
  935. sure the cursor stays at the beginning of the buffer. When this option is
  936. nil, don't do anything special at the beginning of the buffer."
  937. :group 'org-cycle
  938. :type 'boolean)
  939. (defcustom org-cycle-level-after-item/entry-creation t
  940. "Non-nil means cycle entry level or item indentation in new empty entries.
  941. When the cursor is at the end of an empty headline, i.e., with only stars
  942. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  943. and then all possible ancestor states, before returning to the original state.
  944. This makes data entry extremely fast: M-RET to create a new headline,
  945. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  946. When the cursor is at the end of an empty plain list item, one TAB will
  947. make it a subitem, two or more tabs will back up to make this an item
  948. higher up in the item hierarchy."
  949. :group 'org-cycle
  950. :type 'boolean)
  951. (defcustom org-cycle-emulate-tab t
  952. "Where should `org-cycle' emulate TAB.
  953. nil Never
  954. white Only in completely white lines
  955. whitestart Only at the beginning of lines, before the first non-white char
  956. t Everywhere except in headlines
  957. exc-hl-bol Everywhere except at the start of a headline
  958. If TAB is used in a place where it does not emulate TAB, the current subtree
  959. visibility is cycled."
  960. :group 'org-cycle
  961. :type '(choice (const :tag "Never" nil)
  962. (const :tag "Only in completely white lines" white)
  963. (const :tag "Before first char in a line" whitestart)
  964. (const :tag "Everywhere except in headlines" t)
  965. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  966. ))
  967. (defcustom org-cycle-separator-lines 2
  968. "Number of empty lines needed to keep an empty line between collapsed trees.
  969. If you leave an empty line between the end of a subtree and the following
  970. headline, this empty line is hidden when the subtree is folded.
  971. Org-mode will leave (exactly) one empty line visible if the number of
  972. empty lines is equal or larger to the number given in this variable.
  973. So the default 2 means at least 2 empty lines after the end of a subtree
  974. are needed to produce free space between a collapsed subtree and the
  975. following headline.
  976. If the number is negative, and the number of empty lines is at least -N,
  977. all empty lines are shown.
  978. Special case: when 0, never leave empty lines in collapsed view."
  979. :group 'org-cycle
  980. :type 'integer)
  981. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  982. (defcustom org-pre-cycle-hook nil
  983. "Hook that is run before visibility cycling is happening.
  984. The function(s) in this hook must accept a single argument which indicates
  985. the new state that will be set right after running this hook. The
  986. argument is a symbol. Before a global state change, it can have the values
  987. `overview', `content', or `all'. Before a local state change, it can have
  988. the values `folded', `children', or `subtree'."
  989. :group 'org-cycle
  990. :type 'hook)
  991. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  992. org-cycle-hide-drawers
  993. org-cycle-hide-inline-tasks
  994. org-cycle-show-empty-lines
  995. org-optimize-window-after-visibility-change)
  996. "Hook that is run after `org-cycle' has changed the buffer visibility.
  997. The function(s) in this hook must accept a single argument which indicates
  998. the new state that was set by the most recent `org-cycle' command. The
  999. argument is a symbol. After a global state change, it can have the values
  1000. `overview', `contents', or `all'. After a local state change, it can have
  1001. the values `folded', `children', or `subtree'."
  1002. :group 'org-cycle
  1003. :type 'hook)
  1004. (defgroup org-edit-structure nil
  1005. "Options concerning structure editing in Org-mode."
  1006. :tag "Org Edit Structure"
  1007. :group 'org-structure)
  1008. (defcustom org-odd-levels-only nil
  1009. "Non-nil means skip even levels and only use odd levels for the outline.
  1010. This has the effect that two stars are being added/taken away in
  1011. promotion/demotion commands. It also influences how levels are
  1012. handled by the exporters.
  1013. Changing it requires restart of `font-lock-mode' to become effective
  1014. for fontification also in regions already fontified.
  1015. You may also set this on a per-file basis by adding one of the following
  1016. lines to the buffer:
  1017. #+STARTUP: odd
  1018. #+STARTUP: oddeven"
  1019. :group 'org-edit-structure
  1020. :group 'org-appearance
  1021. :type 'boolean)
  1022. (defcustom org-adapt-indentation t
  1023. "Non-nil means adapt indentation to outline node level.
  1024. When this variable is set, Org assumes that you write outlines by
  1025. indenting text in each node to align with the headline (after the stars).
  1026. The following issues are influenced by this variable:
  1027. - When this is set and the *entire* text in an entry is indented, the
  1028. indentation is increased by one space in a demotion command, and
  1029. decreased by one in a promotion command. If any line in the entry
  1030. body starts with text at column 0, indentation is not changed at all.
  1031. - Property drawers and planning information is inserted indented when
  1032. this variable s set. When nil, they will not be indented.
  1033. - TAB indents a line relative to context. The lines below a headline
  1034. will be indented when this variable is set.
  1035. Note that this is all about true indentation, by adding and removing
  1036. space characters. See also `org-indent.el' which does level-dependent
  1037. indentation in a virtual way, i.e. at display time in Emacs."
  1038. :group 'org-edit-structure
  1039. :type 'boolean)
  1040. (defcustom org-special-ctrl-a/e nil
  1041. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1042. When t, `C-a' will bring back the cursor to the beginning of the
  1043. headline text, i.e. after the stars and after a possible TODO
  1044. keyword. In an item, this will be the position after bullet and
  1045. check-box, if any. When the cursor is already at that position,
  1046. another `C-a' will bring it to the beginning of the line.
  1047. `C-e' will jump to the end of the headline, ignoring the presence
  1048. of tags in the headline. A second `C-e' will then jump to the
  1049. true end of the line, after any tags. This also means that, when
  1050. this variable is non-nil, `C-e' also will never jump beyond the
  1051. end of the heading of a folded section, i.e. not after the
  1052. ellipses.
  1053. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1054. normally, going to the true line boundary first. Only a directly
  1055. following, identical keypress will bring the cursor to the
  1056. special positions.
  1057. This may also be a cons cell where the behavior for `C-a' and
  1058. `C-e' is set separately."
  1059. :group 'org-edit-structure
  1060. :type '(choice
  1061. (const :tag "off" nil)
  1062. (const :tag "on: after stars/bullet and before tags first" t)
  1063. (const :tag "reversed: true line boundary first" reversed)
  1064. (cons :tag "Set C-a and C-e separately"
  1065. (choice :tag "Special C-a"
  1066. (const :tag "off" nil)
  1067. (const :tag "on: after stars/bullet first" t)
  1068. (const :tag "reversed: before stars/bullet first" reversed))
  1069. (choice :tag "Special C-e"
  1070. (const :tag "off" nil)
  1071. (const :tag "on: before tags first" t)
  1072. (const :tag "reversed: after tags first" reversed)))))
  1073. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1074. (defcustom org-special-ctrl-k nil
  1075. "Non-nil means `C-k' will behave specially in headlines.
  1076. When nil, `C-k' will call the default `kill-line' command.
  1077. When t, the following will happen while the cursor is in the headline:
  1078. - When the cursor is at the beginning of a headline, kill the entire
  1079. line and possible the folded subtree below the line.
  1080. - When in the middle of the headline text, kill the headline up to the tags.
  1081. - When after the headline text, kill the tags."
  1082. :group 'org-edit-structure
  1083. :type 'boolean)
  1084. (defcustom org-ctrl-k-protect-subtree nil
  1085. "Non-nil means, do not delete a hidden subtree with C-k.
  1086. When set to the symbol `error', simply throw an error when C-k is
  1087. used to kill (part-of) a headline that has hidden text behind it.
  1088. Any other non-nil value will result in a query to the user, if it is
  1089. OK to kill that hidden subtree. When nil, kill without remorse."
  1090. :group 'org-edit-structure
  1091. :version "24.1"
  1092. :type '(choice
  1093. (const :tag "Do not protect hidden subtrees" nil)
  1094. (const :tag "Protect hidden subtrees with a security query" t)
  1095. (const :tag "Never kill a hidden subtree with C-k" error)))
  1096. (defcustom org-catch-invisible-edits nil
  1097. "Check if in invisible region before inserting or deleting a character.
  1098. Valid values are:
  1099. nil Do not check, so just do invisible edits.
  1100. error Throw an error and do nothing.
  1101. show Make point visible, and do the requested edit.
  1102. show-and-error Make point visible, then throw an error and abort the edit.
  1103. smart Make point visible, and do insertion/deletion if it is
  1104. adjacent to visible text and the change feels predictable.
  1105. Never delete a previously invisible character or add in the
  1106. middle or right after an invisible region. Basically, this
  1107. allows insertion and backward-delete right before ellipses.
  1108. FIXME: maybe in this case we should not even show?"
  1109. :group 'org-edit-structure
  1110. :version "24.1"
  1111. :type '(choice
  1112. (const :tag "Do not check" nil)
  1113. (const :tag "Throw error when trying to edit" error)
  1114. (const :tag "Unhide, but do not do the edit" show-and-error)
  1115. (const :tag "Show invisible part and do the edit" show)
  1116. (const :tag "Be smart and do the right thing" smart)))
  1117. (defcustom org-yank-folded-subtrees t
  1118. "Non-nil means when yanking subtrees, fold them.
  1119. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1120. it starts with a heading and all other headings in it are either children
  1121. or siblings, then fold all the subtrees. However, do this only if no
  1122. text after the yank would be swallowed into a folded tree by this action."
  1123. :group 'org-edit-structure
  1124. :type 'boolean)
  1125. (defcustom org-yank-adjusted-subtrees nil
  1126. "Non-nil means when yanking subtrees, adjust the level.
  1127. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1128. this function for details."
  1129. :group 'org-edit-structure
  1130. :type 'boolean)
  1131. (defcustom org-M-RET-may-split-line '((default . t))
  1132. "Non-nil means M-RET will split the line at the cursor position.
  1133. When nil, it will go to the end of the line before making a
  1134. new line.
  1135. You may also set this option in a different way for different
  1136. contexts. Valid contexts are:
  1137. headline when creating a new headline
  1138. item when creating a new item
  1139. table in a table field
  1140. default the value to be used for all contexts not explicitly
  1141. customized"
  1142. :group 'org-structure
  1143. :group 'org-table
  1144. :type '(choice
  1145. (const :tag "Always" t)
  1146. (const :tag "Never" nil)
  1147. (repeat :greedy t :tag "Individual contexts"
  1148. (cons
  1149. (choice :tag "Context"
  1150. (const headline)
  1151. (const item)
  1152. (const table)
  1153. (const default))
  1154. (boolean)))))
  1155. (defcustom org-insert-heading-respect-content nil
  1156. "Non-nil means insert new headings after the current subtree.
  1157. When nil, the new heading is created directly after the current line.
  1158. The commands \\[org-insert-heading-respect-content] and
  1159. \\[org-insert-todo-heading-respect-content] turn this variable on
  1160. for the duration of the command."
  1161. :group 'org-structure
  1162. :type 'boolean)
  1163. (defcustom org-blank-before-new-entry '((heading . auto)
  1164. (plain-list-item . auto))
  1165. "Should `org-insert-heading' leave a blank line before new heading/item?
  1166. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1167. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1168. which case Org will look at the surrounding headings/items and try to
  1169. make an intelligent decision whether to insert a blank line or not.
  1170. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1171. set, the setting here is ignored and no empty line is inserted, to avoid
  1172. breaking the list structure."
  1173. :group 'org-edit-structure
  1174. :type '(list
  1175. (cons (const heading)
  1176. (choice (const :tag "Never" nil)
  1177. (const :tag "Always" t)
  1178. (const :tag "Auto" auto)))
  1179. (cons (const plain-list-item)
  1180. (choice (const :tag "Never" nil)
  1181. (const :tag "Always" t)
  1182. (const :tag "Auto" auto)))))
  1183. (defcustom org-insert-heading-hook nil
  1184. "Hook being run after inserting a new heading."
  1185. :group 'org-edit-structure
  1186. :type 'hook)
  1187. (defcustom org-enable-fixed-width-editor t
  1188. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1189. This currently only means they are never auto-wrapped.
  1190. When nil, such lines will be treated like ordinary lines.
  1191. See also the QUOTE keyword."
  1192. :group 'org-edit-structure
  1193. :type 'boolean)
  1194. (defcustom org-goto-auto-isearch t
  1195. "Non-nil means typing characters in `org-goto' starts incremental search.
  1196. When nil, you can use these keybindings to navigate the buffer:
  1197. q Quit the org-goto interface
  1198. n Go to the next visible heading
  1199. p Go to the previous visible heading
  1200. f Go one heading forward on same level
  1201. b Go one heading backward on same level
  1202. u Go one heading up"
  1203. :group 'org-edit-structure
  1204. :type 'boolean)
  1205. (defgroup org-sparse-trees nil
  1206. "Options concerning sparse trees in Org-mode."
  1207. :tag "Org Sparse Trees"
  1208. :group 'org-structure)
  1209. (defcustom org-highlight-sparse-tree-matches t
  1210. "Non-nil means highlight all matches that define a sparse tree.
  1211. The highlights will automatically disappear the next time the buffer is
  1212. changed by an edit command."
  1213. :group 'org-sparse-trees
  1214. :type 'boolean)
  1215. (defcustom org-remove-highlights-with-change t
  1216. "Non-nil means any change to the buffer will remove temporary highlights.
  1217. Such highlights are created by `org-occur' and `org-clock-display'.
  1218. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1219. The highlights created by `org-preview-latex-fragment' always need
  1220. `C-c C-c' to be removed."
  1221. :group 'org-sparse-trees
  1222. :group 'org-time
  1223. :type 'boolean)
  1224. (defcustom org-occur-hook '(org-first-headline-recenter)
  1225. "Hook that is run after `org-occur' has constructed a sparse tree.
  1226. This can be used to recenter the window to show as much of the structure
  1227. as possible."
  1228. :group 'org-sparse-trees
  1229. :type 'hook)
  1230. (defgroup org-imenu-and-speedbar nil
  1231. "Options concerning imenu and speedbar in Org-mode."
  1232. :tag "Org Imenu and Speedbar"
  1233. :group 'org-structure)
  1234. (defcustom org-imenu-depth 2
  1235. "The maximum level for Imenu access to Org-mode headlines.
  1236. This also applied for speedbar access."
  1237. :group 'org-imenu-and-speedbar
  1238. :type 'integer)
  1239. (defgroup org-table nil
  1240. "Options concerning tables in Org-mode."
  1241. :tag "Org Table"
  1242. :group 'org)
  1243. (defcustom org-enable-table-editor 'optimized
  1244. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1245. When nil, such lines will be treated like ordinary lines.
  1246. When equal to the symbol `optimized', the table editor will be optimized to
  1247. do the following:
  1248. - Automatic overwrite mode in front of whitespace in table fields.
  1249. This makes the structure of the table stay in tact as long as the edited
  1250. field does not exceed the column width.
  1251. - Minimize the number of realigns. Normally, the table is aligned each time
  1252. TAB or RET are pressed to move to another field. With optimization this
  1253. happens only if changes to a field might have changed the column width.
  1254. Optimization requires replacing the functions `self-insert-command',
  1255. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1256. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1257. very good at guessing when a re-align will be necessary, but you can always
  1258. force one with \\[org-ctrl-c-ctrl-c].
  1259. If you would like to use the optimized version in Org-mode, but the
  1260. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1261. This variable can be used to turn on and off the table editor during a session,
  1262. but in order to toggle optimization, a restart is required.
  1263. See also the variable `org-table-auto-blank-field'."
  1264. :group 'org-table
  1265. :type '(choice
  1266. (const :tag "off" nil)
  1267. (const :tag "on" t)
  1268. (const :tag "on, optimized" optimized)))
  1269. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1270. (version<= emacs-version "24.1"))
  1271. "Non-nil means cluster self-insert commands for undo when possible.
  1272. If this is set, then, like in the Emacs command loop, 20 consecutive
  1273. characters will be undone together.
  1274. This is configurable, because there is some impact on typing performance."
  1275. :group 'org-table
  1276. :type 'boolean)
  1277. (defcustom org-table-tab-recognizes-table.el t
  1278. "Non-nil means TAB will automatically notice a table.el table.
  1279. When it sees such a table, it moves point into it and - if necessary -
  1280. calls `table-recognize-table'."
  1281. :group 'org-table-editing
  1282. :type 'boolean)
  1283. (defgroup org-link nil
  1284. "Options concerning links in Org-mode."
  1285. :tag "Org Link"
  1286. :group 'org)
  1287. (defvar org-link-abbrev-alist-local nil
  1288. "Buffer-local version of `org-link-abbrev-alist', which see.
  1289. The value of this is taken from the #+LINK lines.")
  1290. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1291. (defcustom org-link-abbrev-alist nil
  1292. "Alist of link abbreviations.
  1293. The car of each element is a string, to be replaced at the start of a link.
  1294. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1295. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1296. [[linkkey:tag][description]]
  1297. The 'linkkey' must be a word word, starting with a letter, followed
  1298. by letters, numbers, '-' or '_'.
  1299. If REPLACE is a string, the tag will simply be appended to create the link.
  1300. If the string contains \"%s\", the tag will be inserted there. If the string
  1301. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1302. at that point (see the function `url-hexify-string'). If the string contains
  1303. the specifier \"%(my-function)\", then the custom function `my-function' will
  1304. be invoked: this function takes the tag as its only argument and must return
  1305. a string.
  1306. REPLACE may also be a function that will be called with the tag as the
  1307. only argument to create the link, which should be returned as a string.
  1308. See the manual for examples."
  1309. :group 'org-link
  1310. :type '(repeat
  1311. (cons
  1312. (string :tag "Protocol")
  1313. (choice
  1314. (string :tag "Format")
  1315. (function)))))
  1316. (defcustom org-descriptive-links t
  1317. "Non-nil means Org will display descriptive links.
  1318. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1319. \"Org Website\", hiding the link itself and just displaying its
  1320. description. When set to `nil', Org will display the full links
  1321. literally.
  1322. You can interactively set the value of this variable by calling
  1323. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1324. :group 'org-link
  1325. :type 'boolean)
  1326. (defcustom org-link-file-path-type 'adaptive
  1327. "How the path name in file links should be stored.
  1328. Valid values are:
  1329. relative Relative to the current directory, i.e. the directory of the file
  1330. into which the link is being inserted.
  1331. absolute Absolute path, if possible with ~ for home directory.
  1332. noabbrev Absolute path, no abbreviation of home directory.
  1333. adaptive Use relative path for files in the current directory and sub-
  1334. directories of it. For other files, use an absolute path."
  1335. :group 'org-link
  1336. :type '(choice
  1337. (const relative)
  1338. (const absolute)
  1339. (const noabbrev)
  1340. (const adaptive)))
  1341. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1342. "Types of links that should be activated in Org-mode files.
  1343. This is a list of symbols, each leading to the activation of a certain link
  1344. type. In principle, it does not hurt to turn on most link types - there may
  1345. be a small gain when turning off unused link types. The types are:
  1346. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1347. angle Links in angular brackets that may contain whitespace like
  1348. <bbdb:Carsten Dominik>.
  1349. plain Plain links in normal text, no whitespace, like http://google.com.
  1350. radio Text that is matched by a radio target, see manual for details.
  1351. tag Tag settings in a headline (link to tag search).
  1352. date Time stamps (link to calendar).
  1353. footnote Footnote labels.
  1354. Changing this variable requires a restart of Emacs to become effective."
  1355. :group 'org-link
  1356. :type '(set :greedy t
  1357. (const :tag "Double bracket links" bracket)
  1358. (const :tag "Angular bracket links" angle)
  1359. (const :tag "Plain text links" plain)
  1360. (const :tag "Radio target matches" radio)
  1361. (const :tag "Tags" tag)
  1362. (const :tag "Timestamps" date)
  1363. (const :tag "Footnotes" footnote)))
  1364. (defcustom org-make-link-description-function nil
  1365. "Function to use for generating link descriptions from links.
  1366. When nil, the link location will be used. This function must take
  1367. two parameters: the first one is the link, the second one is the
  1368. description generated by `org-insert-link'. The function should
  1369. return the description to use."
  1370. :group 'org-link
  1371. :type 'function)
  1372. (defgroup org-link-store nil
  1373. "Options concerning storing links in Org-mode."
  1374. :tag "Org Store Link"
  1375. :group 'org-link)
  1376. (defcustom org-url-hexify-p t
  1377. "When non-nil, hexify URL when creating a link."
  1378. :type 'boolean
  1379. :version "24.3"
  1380. :group 'org-link-store)
  1381. (defcustom org-email-link-description-format "Email %c: %.30s"
  1382. "Format of the description part of a link to an email or usenet message.
  1383. The following %-escapes will be replaced by corresponding information:
  1384. %F full \"From\" field
  1385. %f name, taken from \"From\" field, address if no name
  1386. %T full \"To\" field
  1387. %t first name in \"To\" field, address if no name
  1388. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1389. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1390. %s subject
  1391. %d date
  1392. %m message-id.
  1393. You may use normal field width specification between the % and the letter.
  1394. This is for example useful to limit the length of the subject.
  1395. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1396. :group 'org-link-store
  1397. :type 'string)
  1398. (defcustom org-from-is-user-regexp
  1399. (let (r1 r2)
  1400. (when (and user-mail-address (not (string= user-mail-address "")))
  1401. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1402. (when (and user-full-name (not (string= user-full-name "")))
  1403. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1404. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1405. "Regexp matched against the \"From:\" header of an email or usenet message.
  1406. It should match if the message is from the user him/herself."
  1407. :group 'org-link-store
  1408. :type 'regexp)
  1409. (defcustom org-context-in-file-links t
  1410. "Non-nil means file links from `org-store-link' contain context.
  1411. A search string will be added to the file name with :: as separator and
  1412. used to find the context when the link is activated by the command
  1413. `org-open-at-point'. When this option is t, the entire active region
  1414. will be placed in the search string of the file link. If set to a
  1415. positive integer, only the first n lines of context will be stored.
  1416. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1417. negates this setting for the duration of the command."
  1418. :group 'org-link-store
  1419. :type '(choice boolean integer))
  1420. (defcustom org-keep-stored-link-after-insertion nil
  1421. "Non-nil means keep link in list for entire session.
  1422. The command `org-store-link' adds a link pointing to the current
  1423. location to an internal list. These links accumulate during a session.
  1424. The command `org-insert-link' can be used to insert links into any
  1425. Org-mode file (offering completion for all stored links). When this
  1426. option is nil, every link which has been inserted once using \\[org-insert-link]
  1427. will be removed from the list, to make completing the unused links
  1428. more efficient."
  1429. :group 'org-link-store
  1430. :type 'boolean)
  1431. (defgroup org-link-follow nil
  1432. "Options concerning following links in Org-mode."
  1433. :tag "Org Follow Link"
  1434. :group 'org-link)
  1435. (defcustom org-link-translation-function nil
  1436. "Function to translate links with different syntax to Org syntax.
  1437. This can be used to translate links created for example by the Planner
  1438. or emacs-wiki packages to Org syntax.
  1439. The function must accept two parameters, a TYPE containing the link
  1440. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1441. which is everything after the link protocol. It should return a cons
  1442. with possibly modified values of type and path.
  1443. Org contains a function for this, so if you set this variable to
  1444. `org-translate-link-from-planner', you should be able follow many
  1445. links created by planner."
  1446. :group 'org-link-follow
  1447. :type 'function)
  1448. (defcustom org-follow-link-hook nil
  1449. "Hook that is run after a link has been followed."
  1450. :group 'org-link-follow
  1451. :type 'hook)
  1452. (defcustom org-tab-follows-link nil
  1453. "Non-nil means on links TAB will follow the link.
  1454. Needs to be set before org.el is loaded.
  1455. This really should not be used, it does not make sense, and the
  1456. implementation is bad."
  1457. :group 'org-link-follow
  1458. :type 'boolean)
  1459. (defcustom org-return-follows-link nil
  1460. "Non-nil means on links RET will follow the link.
  1461. In tables, the special behavior of RET has precedence."
  1462. :group 'org-link-follow
  1463. :type 'boolean)
  1464. (defcustom org-mouse-1-follows-link
  1465. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1466. "Non-nil means mouse-1 on a link will follow the link.
  1467. A longer mouse click will still set point. Does not work on XEmacs.
  1468. Needs to be set before org.el is loaded."
  1469. :group 'org-link-follow
  1470. :type 'boolean)
  1471. (defcustom org-mark-ring-length 4
  1472. "Number of different positions to be recorded in the ring.
  1473. Changing this requires a restart of Emacs to work correctly."
  1474. :group 'org-link-follow
  1475. :type 'integer)
  1476. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1477. "Non-nil means internal links in Org files must exactly match a headline.
  1478. When nil, the link search tries to match a phrase with all words
  1479. in the search text."
  1480. :group 'org-link-follow
  1481. :version "24.1"
  1482. :type '(choice
  1483. (const :tag "Use fuzzy text search" nil)
  1484. (const :tag "Match only exact headline" t)
  1485. (const :tag "Match exact headline or query to create it"
  1486. query-to-create)))
  1487. (defcustom org-link-frame-setup
  1488. '((vm . vm-visit-folder-other-frame)
  1489. (vm-imap . vm-visit-imap-folder-other-frame)
  1490. (gnus . org-gnus-no-new-news)
  1491. (file . find-file-other-window)
  1492. (wl . wl-other-frame))
  1493. "Setup the frame configuration for following links.
  1494. When following a link with Emacs, it may often be useful to display
  1495. this link in another window or frame. This variable can be used to
  1496. set this up for the different types of links.
  1497. For VM, use any of
  1498. `vm-visit-folder'
  1499. `vm-visit-folder-other-window'
  1500. `vm-visit-folder-other-frame'
  1501. For Gnus, use any of
  1502. `gnus'
  1503. `gnus-other-frame'
  1504. `org-gnus-no-new-news'
  1505. For FILE, use any of
  1506. `find-file'
  1507. `find-file-other-window'
  1508. `find-file-other-frame'
  1509. For Wanderlust use any of
  1510. `wl'
  1511. `wl-other-frame'
  1512. For the calendar, use the variable `calendar-setup'.
  1513. For BBDB, it is currently only possible to display the matches in
  1514. another window."
  1515. :group 'org-link-follow
  1516. :type '(list
  1517. (cons (const vm)
  1518. (choice
  1519. (const vm-visit-folder)
  1520. (const vm-visit-folder-other-window)
  1521. (const vm-visit-folder-other-frame)))
  1522. (cons (const gnus)
  1523. (choice
  1524. (const gnus)
  1525. (const gnus-other-frame)
  1526. (const org-gnus-no-new-news)))
  1527. (cons (const file)
  1528. (choice
  1529. (const find-file)
  1530. (const find-file-other-window)
  1531. (const find-file-other-frame)))
  1532. (cons (const wl)
  1533. (choice
  1534. (const wl)
  1535. (const wl-other-frame)))))
  1536. (defcustom org-display-internal-link-with-indirect-buffer nil
  1537. "Non-nil means use indirect buffer to display infile links.
  1538. Activating internal links (from one location in a file to another location
  1539. in the same file) normally just jumps to the location. When the link is
  1540. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1541. is displayed in
  1542. another window. When this option is set, the other window actually displays
  1543. an indirect buffer clone of the current buffer, to avoid any visibility
  1544. changes to the current buffer."
  1545. :group 'org-link-follow
  1546. :type 'boolean)
  1547. (defcustom org-open-non-existing-files nil
  1548. "Non-nil means `org-open-file' will open non-existing files.
  1549. When nil, an error will be generated.
  1550. This variable applies only to external applications because they
  1551. might choke on non-existing files. If the link is to a file that
  1552. will be opened in Emacs, the variable is ignored."
  1553. :group 'org-link-follow
  1554. :type 'boolean)
  1555. (defcustom org-open-directory-means-index-dot-org nil
  1556. "Non-nil means a link to a directory really means to index.org.
  1557. When nil, following a directory link will run dired or open a finder/explorer
  1558. window on that directory."
  1559. :group 'org-link-follow
  1560. :type 'boolean)
  1561. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1562. "Function and arguments to call for following mailto links.
  1563. This is a list with the first element being a Lisp function, and the
  1564. remaining elements being arguments to the function. In string arguments,
  1565. %a will be replaced by the address, and %s will be replaced by the subject
  1566. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1567. :group 'org-link-follow
  1568. :type '(choice
  1569. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1570. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1571. (const :tag "message-mail" (message-mail "%a" "%s"))
  1572. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1573. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1574. "Non-nil means ask for confirmation before executing shell links.
  1575. Shell links can be dangerous: just think about a link
  1576. [[shell:rm -rf ~/*][Google Search]]
  1577. This link would show up in your Org-mode document as \"Google Search\",
  1578. but really it would remove your entire home directory.
  1579. Therefore we advise against setting this variable to nil.
  1580. Just change it to `y-or-n-p' if you want to confirm with a
  1581. single keystroke rather than having to type \"yes\"."
  1582. :group 'org-link-follow
  1583. :type '(choice
  1584. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1585. (const :tag "with y-or-n (faster)" y-or-n-p)
  1586. (const :tag "no confirmation (dangerous)" nil)))
  1587. (put 'org-confirm-shell-link-function
  1588. 'safe-local-variable
  1589. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1590. (defcustom org-confirm-shell-link-not-regexp ""
  1591. "A regexp to skip confirmation for shell links."
  1592. :group 'org-link-follow
  1593. :version "24.1"
  1594. :type 'regexp)
  1595. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1596. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1597. Elisp links can be dangerous: just think about a link
  1598. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1599. This link would show up in your Org-mode document as \"Google Search\",
  1600. but really it would remove your entire home directory.
  1601. Therefore we advise against setting this variable to nil.
  1602. Just change it to `y-or-n-p' if you want to confirm with a
  1603. single keystroke rather than having to type \"yes\"."
  1604. :group 'org-link-follow
  1605. :type '(choice
  1606. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1607. (const :tag "with y-or-n (faster)" y-or-n-p)
  1608. (const :tag "no confirmation (dangerous)" nil)))
  1609. (put 'org-confirm-shell-link-function
  1610. 'safe-local-variable
  1611. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1612. (defcustom org-confirm-elisp-link-not-regexp ""
  1613. "A regexp to skip confirmation for Elisp links."
  1614. :group 'org-link-follow
  1615. :version "24.1"
  1616. :type 'regexp)
  1617. (defconst org-file-apps-defaults-gnu
  1618. '((remote . emacs)
  1619. (system . mailcap)
  1620. (t . mailcap))
  1621. "Default file applications on a UNIX or GNU/Linux system.
  1622. See `org-file-apps'.")
  1623. (defconst org-file-apps-defaults-macosx
  1624. '((remote . emacs)
  1625. (t . "open %s")
  1626. (system . "open %s")
  1627. ("ps.gz" . "gv %s")
  1628. ("eps.gz" . "gv %s")
  1629. ("dvi" . "xdvi %s")
  1630. ("fig" . "xfig %s"))
  1631. "Default file applications on a MacOS X system.
  1632. The system \"open\" is known as a default, but we use X11 applications
  1633. for some files for which the OS does not have a good default.
  1634. See `org-file-apps'.")
  1635. (defconst org-file-apps-defaults-windowsnt
  1636. (list
  1637. '(remote . emacs)
  1638. (cons t
  1639. (list (if (featurep 'xemacs)
  1640. 'mswindows-shell-execute
  1641. 'w32-shell-execute)
  1642. "open" 'file))
  1643. (cons 'system
  1644. (list (if (featurep 'xemacs)
  1645. 'mswindows-shell-execute
  1646. 'w32-shell-execute)
  1647. "open" 'file)))
  1648. "Default file applications on a Windows NT system.
  1649. The system \"open\" is used for most files.
  1650. See `org-file-apps'.")
  1651. (defcustom org-file-apps
  1652. '(
  1653. (auto-mode . emacs)
  1654. ("\\.mm\\'" . default)
  1655. ("\\.x?html?\\'" . default)
  1656. ("\\.pdf\\'" . default)
  1657. )
  1658. "External applications for opening `file:path' items in a document.
  1659. Org-mode uses system defaults for different file types, but
  1660. you can use this variable to set the application for a given file
  1661. extension. The entries in this list are cons cells where the car identifies
  1662. files and the cdr the corresponding command. Possible values for the
  1663. file identifier are
  1664. \"string\" A string as a file identifier can be interpreted in different
  1665. ways, depending on its contents:
  1666. - Alphanumeric characters only:
  1667. Match links with this file extension.
  1668. Example: (\"pdf\" . \"evince %s\")
  1669. to open PDFs with evince.
  1670. - Regular expression: Match links where the
  1671. filename matches the regexp. If you want to
  1672. use groups here, use shy groups.
  1673. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1674. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1675. to open *.html and *.xhtml with firefox.
  1676. - Regular expression which contains (non-shy) groups:
  1677. Match links where the whole link, including \"::\", and
  1678. anything after that, matches the regexp.
  1679. In a custom command string, %1, %2, etc. are replaced with
  1680. the parts of the link that were matched by the groups.
  1681. For backwards compatibility, if a command string is given
  1682. that does not use any of the group matches, this case is
  1683. handled identically to the second one (i.e. match against
  1684. file name only).
  1685. In a custom lisp form, you can access the group matches with
  1686. (match-string n link).
  1687. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1688. to open [[file:document.pdf::5]] with evince at page 5.
  1689. `directory' Matches a directory
  1690. `remote' Matches a remote file, accessible through tramp or efs.
  1691. Remote files most likely should be visited through Emacs
  1692. because external applications cannot handle such paths.
  1693. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1694. so all files Emacs knows how to handle. Using this with
  1695. command `emacs' will open most files in Emacs. Beware that this
  1696. will also open html files inside Emacs, unless you add
  1697. (\"html\" . default) to the list as well.
  1698. t Default for files not matched by any of the other options.
  1699. `system' The system command to open files, like `open' on Windows
  1700. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1701. that will be selected if you call `C-c C-o' with a double
  1702. \\[universal-argument] \\[universal-argument] prefix.
  1703. Possible values for the command are:
  1704. `emacs' The file will be visited by the current Emacs process.
  1705. `default' Use the default application for this file type, which is the
  1706. association for t in the list, most likely in the system-specific
  1707. part.
  1708. This can be used to overrule an unwanted setting in the
  1709. system-specific variable.
  1710. `system' Use the system command for opening files, like \"open\".
  1711. This command is specified by the entry whose car is `system'.
  1712. Most likely, the system-specific version of this variable
  1713. does define this command, but you can overrule/replace it
  1714. here.
  1715. string A command to be executed by a shell; %s will be replaced
  1716. by the path to the file.
  1717. sexp A Lisp form which will be evaluated. The file path will
  1718. be available in the Lisp variable `file'.
  1719. For more examples, see the system specific constants
  1720. `org-file-apps-defaults-macosx'
  1721. `org-file-apps-defaults-windowsnt'
  1722. `org-file-apps-defaults-gnu'."
  1723. :group 'org-link-follow
  1724. :type '(repeat
  1725. (cons (choice :value ""
  1726. (string :tag "Extension")
  1727. (const :tag "System command to open files" system)
  1728. (const :tag "Default for unrecognized files" t)
  1729. (const :tag "Remote file" remote)
  1730. (const :tag "Links to a directory" directory)
  1731. (const :tag "Any files that have Emacs modes"
  1732. auto-mode))
  1733. (choice :value ""
  1734. (const :tag "Visit with Emacs" emacs)
  1735. (const :tag "Use default" default)
  1736. (const :tag "Use the system command" system)
  1737. (string :tag "Command")
  1738. (sexp :tag "Lisp form")))))
  1739. (defcustom org-doi-server-url "http://dx.doi.org/"
  1740. "The URL of the DOI server."
  1741. :type 'string
  1742. :version "24.3"
  1743. :group 'org-link-follow)
  1744. (defgroup org-refile nil
  1745. "Options concerning refiling entries in Org-mode."
  1746. :tag "Org Refile"
  1747. :group 'org)
  1748. (defcustom org-directory "~/org"
  1749. "Directory with org files.
  1750. This is just a default location to look for Org files. There is no need
  1751. at all to put your files into this directory. It is only used in the
  1752. following situations:
  1753. 1. When a capture template specifies a target file that is not an
  1754. absolute path. The path will then be interpreted relative to
  1755. `org-directory'
  1756. 2. When a capture note is filed away in an interactive way (when exiting the
  1757. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1758. with `org-directory' as the default path."
  1759. :group 'org-refile
  1760. :group 'org-remember
  1761. :group 'org-capture
  1762. :type 'directory)
  1763. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1764. "Default target for storing notes.
  1765. Used as a fall back file for org-remember.el and org-capture.el, for
  1766. templates that do not specify a target file."
  1767. :group 'org-refile
  1768. :group 'org-remember
  1769. :group 'org-capture
  1770. :type '(choice
  1771. (const :tag "Default from remember-data-file" nil)
  1772. file))
  1773. (defcustom org-goto-interface 'outline
  1774. "The default interface to be used for `org-goto'.
  1775. Allowed values are:
  1776. outline The interface shows an outline of the relevant file
  1777. and the correct heading is found by moving through
  1778. the outline or by searching with incremental search.
  1779. outline-path-completion Headlines in the current buffer are offered via
  1780. completion. This is the interface also used by
  1781. the refile command."
  1782. :group 'org-refile
  1783. :type '(choice
  1784. (const :tag "Outline" outline)
  1785. (const :tag "Outline-path-completion" outline-path-completion)))
  1786. (defcustom org-goto-max-level 5
  1787. "Maximum target level when running `org-goto' with refile interface."
  1788. :group 'org-refile
  1789. :type 'integer)
  1790. (defcustom org-reverse-note-order nil
  1791. "Non-nil means store new notes at the beginning of a file or entry.
  1792. When nil, new notes will be filed to the end of a file or entry.
  1793. This can also be a list with cons cells of regular expressions that
  1794. are matched against file names, and values."
  1795. :group 'org-remember
  1796. :group 'org-capture
  1797. :group 'org-refile
  1798. :type '(choice
  1799. (const :tag "Reverse always" t)
  1800. (const :tag "Reverse never" nil)
  1801. (repeat :tag "By file name regexp"
  1802. (cons regexp boolean))))
  1803. (defcustom org-log-refile nil
  1804. "Information to record when a task is refiled.
  1805. Possible values are:
  1806. nil Don't add anything
  1807. time Add a time stamp to the task
  1808. note Prompt for a note and add it with template `org-log-note-headings'
  1809. This option can also be set with on a per-file-basis with
  1810. #+STARTUP: nologrefile
  1811. #+STARTUP: logrefile
  1812. #+STARTUP: lognoterefile
  1813. You can have local logging settings for a subtree by setting the LOGGING
  1814. property to one or more of these keywords.
  1815. When bulk-refiling from the agenda, the value `note' is forbidden and
  1816. will temporarily be changed to `time'."
  1817. :group 'org-refile
  1818. :group 'org-progress
  1819. :version "24.1"
  1820. :type '(choice
  1821. (const :tag "No logging" nil)
  1822. (const :tag "Record timestamp" time)
  1823. (const :tag "Record timestamp with note." note)))
  1824. (defcustom org-refile-targets nil
  1825. "Targets for refiling entries with \\[org-refile].
  1826. This is a list of cons cells. Each cell contains:
  1827. - a specification of the files to be considered, either a list of files,
  1828. or a symbol whose function or variable value will be used to retrieve
  1829. a file name or a list of file names. If you use `org-agenda-files' for
  1830. that, all agenda files will be scanned for targets. Nil means consider
  1831. headings in the current buffer.
  1832. - A specification of how to find candidate refile targets. This may be
  1833. any of:
  1834. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1835. This tag has to be present in all target headlines, inheritance will
  1836. not be considered.
  1837. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1838. todo keyword.
  1839. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1840. headlines that are refiling targets.
  1841. - a cons cell (:level . N). Any headline of level N is considered a target.
  1842. Note that, when `org-odd-levels-only' is set, level corresponds to
  1843. order in hierarchy, not to the number of stars.
  1844. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1845. Note that, when `org-odd-levels-only' is set, level corresponds to
  1846. order in hierarchy, not to the number of stars.
  1847. Each element of this list generates a set of possible targets.
  1848. The union of these sets is presented (with completion) to
  1849. the user by `org-refile'.
  1850. You can set the variable `org-refile-target-verify-function' to a function
  1851. to verify each headline found by the simple criteria above.
  1852. When this variable is nil, all top-level headlines in the current buffer
  1853. are used, equivalent to the value `((nil . (:level . 1))'."
  1854. :group 'org-refile
  1855. :type '(repeat
  1856. (cons
  1857. (choice :value org-agenda-files
  1858. (const :tag "All agenda files" org-agenda-files)
  1859. (const :tag "Current buffer" nil)
  1860. (function) (variable) (file))
  1861. (choice :tag "Identify target headline by"
  1862. (cons :tag "Specific tag" (const :value :tag) (string))
  1863. (cons :tag "TODO keyword" (const :value :todo) (string))
  1864. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1865. (cons :tag "Level number" (const :value :level) (integer))
  1866. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1867. (defcustom org-refile-target-verify-function nil
  1868. "Function to verify if the headline at point should be a refile target.
  1869. The function will be called without arguments, with point at the
  1870. beginning of the headline. It should return t and leave point
  1871. where it is if the headline is a valid target for refiling.
  1872. If the target should not be selected, the function must return nil.
  1873. In addition to this, it may move point to a place from where the search
  1874. should be continued. For example, the function may decide that the entire
  1875. subtree of the current entry should be excluded and move point to the end
  1876. of the subtree."
  1877. :group 'org-refile
  1878. :type 'function)
  1879. (defcustom org-refile-use-cache nil
  1880. "Non-nil means cache refile targets to speed up the process.
  1881. The cache for a particular file will be updated automatically when
  1882. the buffer has been killed, or when any of the marker used for flagging
  1883. refile targets no longer points at a live buffer.
  1884. If you have added new entries to a buffer that might themselves be targets,
  1885. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1886. find that easier, `C-u C-u C-u C-c C-w'."
  1887. :group 'org-refile
  1888. :version "24.1"
  1889. :type 'boolean)
  1890. (defcustom org-refile-use-outline-path nil
  1891. "Non-nil means provide refile targets as paths.
  1892. So a level 3 headline will be available as level1/level2/level3.
  1893. When the value is `file', also include the file name (without directory)
  1894. into the path. In this case, you can also stop the completion after
  1895. the file name, to get entries inserted as top level in the file.
  1896. When `full-file-path', include the full file path."
  1897. :group 'org-refile
  1898. :type '(choice
  1899. (const :tag "Not" nil)
  1900. (const :tag "Yes" t)
  1901. (const :tag "Start with file name" file)
  1902. (const :tag "Start with full file path" full-file-path)))
  1903. (defcustom org-outline-path-complete-in-steps t
  1904. "Non-nil means complete the outline path in hierarchical steps.
  1905. When Org-mode uses the refile interface to select an outline path
  1906. \(see variable `org-refile-use-outline-path'), the completion of
  1907. the path can be done is a single go, or if can be done in steps down
  1908. the headline hierarchy. Going in steps is probably the best if you
  1909. do not use a special completion package like `ido' or `icicles'.
  1910. However, when using these packages, going in one step can be very
  1911. fast, while still showing the whole path to the entry."
  1912. :group 'org-refile
  1913. :type 'boolean)
  1914. (defcustom org-refile-allow-creating-parent-nodes nil
  1915. "Non-nil means allow to create new nodes as refile targets.
  1916. New nodes are then created by adding \"/new node name\" to the completion
  1917. of an existing node. When the value of this variable is `confirm',
  1918. new node creation must be confirmed by the user (recommended)
  1919. When nil, the completion must match an existing entry.
  1920. Note that, if the new heading is not seen by the criteria
  1921. listed in `org-refile-targets', multiple instances of the same
  1922. heading would be created by trying again to file under the new
  1923. heading."
  1924. :group 'org-refile
  1925. :type '(choice
  1926. (const :tag "Never" nil)
  1927. (const :tag "Always" t)
  1928. (const :tag "Prompt for confirmation" confirm)))
  1929. (defcustom org-refile-active-region-within-subtree nil
  1930. "Non-nil means also refile active region within a subtree.
  1931. By default `org-refile' doesn't allow refiling regions if they
  1932. don't contain a set of subtrees, but it might be convenient to
  1933. do so sometimes: in that case, the first line of the region is
  1934. converted to a headline before refiling."
  1935. :group 'org-refile
  1936. :version "24.1"
  1937. :type 'boolean)
  1938. (defgroup org-todo nil
  1939. "Options concerning TODO items in Org-mode."
  1940. :tag "Org TODO"
  1941. :group 'org)
  1942. (defgroup org-progress nil
  1943. "Options concerning Progress logging in Org-mode."
  1944. :tag "Org Progress"
  1945. :group 'org-time)
  1946. (defvar org-todo-interpretation-widgets
  1947. '((:tag "Sequence (cycling hits every state)" sequence)
  1948. (:tag "Type (cycling directly to DONE)" type))
  1949. "The available interpretation symbols for customizing `org-todo-keywords'.
  1950. Interested libraries should add to this list.")
  1951. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1952. "List of TODO entry keyword sequences and their interpretation.
  1953. \\<org-mode-map>This is a list of sequences.
  1954. Each sequence starts with a symbol, either `sequence' or `type',
  1955. indicating if the keywords should be interpreted as a sequence of
  1956. action steps, or as different types of TODO items. The first
  1957. keywords are states requiring action - these states will select a headline
  1958. for inclusion into the global TODO list Org-mode produces. If one of
  1959. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1960. signify that no further action is necessary. If \"|\" is not found,
  1961. the last keyword is treated as the only DONE state of the sequence.
  1962. The command \\[org-todo] cycles an entry through these states, and one
  1963. additional state where no keyword is present. For details about this
  1964. cycling, see the manual.
  1965. TODO keywords and interpretation can also be set on a per-file basis with
  1966. the special #+SEQ_TODO and #+TYP_TODO lines.
  1967. Each keyword can optionally specify a character for fast state selection
  1968. \(in combination with the variable `org-use-fast-todo-selection')
  1969. and specifiers for state change logging, using the same syntax that
  1970. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1971. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1972. indicates to record a time stamp each time this state is selected.
  1973. Each keyword may also specify if a timestamp or a note should be
  1974. recorded when entering or leaving the state, by adding additional
  1975. characters in the parenthesis after the keyword. This looks like this:
  1976. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1977. record only the time of the state change. With X and Y being either
  1978. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1979. Y when leaving the state if and only if the *target* state does not
  1980. define X. You may omit any of the fast-selection key or X or /Y,
  1981. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1982. For backward compatibility, this variable may also be just a list
  1983. of keywords. In this case the interpretation (sequence or type) will be
  1984. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1985. :group 'org-todo
  1986. :group 'org-keywords
  1987. :type '(choice
  1988. (repeat :tag "Old syntax, just keywords"
  1989. (string :tag "Keyword"))
  1990. (repeat :tag "New syntax"
  1991. (cons
  1992. (choice
  1993. :tag "Interpretation"
  1994. ;;Quick and dirty way to see
  1995. ;;`org-todo-interpretations'. This takes the
  1996. ;;place of item arguments
  1997. :convert-widget
  1998. (lambda (widget)
  1999. (widget-put widget
  2000. :args (mapcar
  2001. #'(lambda (x)
  2002. (widget-convert
  2003. (cons 'const x)))
  2004. org-todo-interpretation-widgets))
  2005. widget))
  2006. (repeat
  2007. (string :tag "Keyword"))))))
  2008. (defvar org-todo-keywords-1 nil
  2009. "All TODO and DONE keywords active in a buffer.")
  2010. (make-variable-buffer-local 'org-todo-keywords-1)
  2011. (defvar org-todo-keywords-for-agenda nil)
  2012. (defvar org-done-keywords-for-agenda nil)
  2013. (defvar org-drawers-for-agenda nil)
  2014. (defvar org-todo-keyword-alist-for-agenda nil)
  2015. (defvar org-tag-alist-for-agenda nil)
  2016. (defvar org-agenda-contributing-files nil)
  2017. (defvar org-not-done-keywords nil)
  2018. (make-variable-buffer-local 'org-not-done-keywords)
  2019. (defvar org-done-keywords nil)
  2020. (make-variable-buffer-local 'org-done-keywords)
  2021. (defvar org-todo-heads nil)
  2022. (make-variable-buffer-local 'org-todo-heads)
  2023. (defvar org-todo-sets nil)
  2024. (make-variable-buffer-local 'org-todo-sets)
  2025. (defvar org-todo-log-states nil)
  2026. (make-variable-buffer-local 'org-todo-log-states)
  2027. (defvar org-todo-kwd-alist nil)
  2028. (make-variable-buffer-local 'org-todo-kwd-alist)
  2029. (defvar org-todo-key-alist nil)
  2030. (make-variable-buffer-local 'org-todo-key-alist)
  2031. (defvar org-todo-key-trigger nil)
  2032. (make-variable-buffer-local 'org-todo-key-trigger)
  2033. (defcustom org-todo-interpretation 'sequence
  2034. "Controls how TODO keywords are interpreted.
  2035. This variable is in principle obsolete and is only used for
  2036. backward compatibility, if the interpretation of todo keywords is
  2037. not given already in `org-todo-keywords'. See that variable for
  2038. more information."
  2039. :group 'org-todo
  2040. :group 'org-keywords
  2041. :type '(choice (const sequence)
  2042. (const type)))
  2043. (defcustom org-use-fast-todo-selection t
  2044. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2045. This variable describes if and under what circumstances the cycling
  2046. mechanism for TODO keywords will be replaced by a single-key, direct
  2047. selection scheme.
  2048. When nil, fast selection is never used.
  2049. When the symbol `prefix', it will be used when `org-todo' is called
  2050. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2051. `C-u t' in an agenda buffer.
  2052. When t, fast selection is used by default. In this case, the prefix
  2053. argument forces cycling instead.
  2054. In all cases, the special interface is only used if access keys have
  2055. actually been assigned by the user, i.e. if keywords in the configuration
  2056. are followed by a letter in parenthesis, like TODO(t)."
  2057. :group 'org-todo
  2058. :type '(choice
  2059. (const :tag "Never" nil)
  2060. (const :tag "By default" t)
  2061. (const :tag "Only with C-u C-c C-t" prefix)))
  2062. (defcustom org-provide-todo-statistics t
  2063. "Non-nil means update todo statistics after insert and toggle.
  2064. ALL-HEADLINES means update todo statistics by including headlines
  2065. with no TODO keyword as well, counting them as not done.
  2066. A list of TODO keywords means the same, but skip keywords that are
  2067. not in this list.
  2068. When this is set, todo statistics is updated in the parent of the
  2069. current entry each time a todo state is changed."
  2070. :group 'org-todo
  2071. :type '(choice
  2072. (const :tag "Yes, only for TODO entries" t)
  2073. (const :tag "Yes, including all entries" 'all-headlines)
  2074. (repeat :tag "Yes, for TODOs in this list"
  2075. (string :tag "TODO keyword"))
  2076. (other :tag "No TODO statistics" nil)))
  2077. (defcustom org-hierarchical-todo-statistics t
  2078. "Non-nil means TODO statistics covers just direct children.
  2079. When nil, all entries in the subtree are considered.
  2080. This has only an effect if `org-provide-todo-statistics' is set.
  2081. To set this to nil for only a single subtree, use a COOKIE_DATA
  2082. property and include the word \"recursive\" into the value."
  2083. :group 'org-todo
  2084. :type 'boolean)
  2085. (defcustom org-after-todo-state-change-hook nil
  2086. "Hook which is run after the state of a TODO item was changed.
  2087. The new state (a string with a TODO keyword, or nil) is available in the
  2088. Lisp variable `org-state'."
  2089. :group 'org-todo
  2090. :type 'hook)
  2091. (defvar org-blocker-hook nil
  2092. "Hook for functions that are allowed to block a state change.
  2093. Functions in this hook should not modify the buffer.
  2094. Each function gets as its single argument a property list,
  2095. see `org-trigger-hook' for more information about this list.
  2096. If any of the functions in this hook returns nil, the state change
  2097. is blocked.")
  2098. (defvar org-trigger-hook nil
  2099. "Hook for functions that are triggered by a state change.
  2100. Each function gets as its single argument a property list with at
  2101. least the following elements:
  2102. (:type type-of-change :position pos-at-entry-start
  2103. :from old-state :to new-state)
  2104. Depending on the type, more properties may be present.
  2105. This mechanism is currently implemented for:
  2106. TODO state changes
  2107. ------------------
  2108. :type todo-state-change
  2109. :from previous state (keyword as a string), or nil, or a symbol
  2110. 'todo' or 'done', to indicate the general type of state.
  2111. :to new state, like in :from")
  2112. (defcustom org-enforce-todo-dependencies nil
  2113. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2114. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2115. be blocked if any prior sibling is not yet done.
  2116. Finally, if the parent is blocked because of ordered siblings of its own,
  2117. the child will also be blocked."
  2118. :set (lambda (var val)
  2119. (set var val)
  2120. (if val
  2121. (add-hook 'org-blocker-hook
  2122. 'org-block-todo-from-children-or-siblings-or-parent)
  2123. (remove-hook 'org-blocker-hook
  2124. 'org-block-todo-from-children-or-siblings-or-parent)))
  2125. :group 'org-todo
  2126. :type 'boolean)
  2127. (defcustom org-enforce-todo-checkbox-dependencies nil
  2128. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2129. When this is nil, checkboxes have no influence on switching TODO states.
  2130. When non-nil, you first need to check off all check boxes before the TODO
  2131. entry can be switched to DONE.
  2132. This variable needs to be set before org.el is loaded, and you need to
  2133. restart Emacs after a change to make the change effective. The only way
  2134. to change is while Emacs is running is through the customize interface."
  2135. :set (lambda (var val)
  2136. (set var val)
  2137. (if val
  2138. (add-hook 'org-blocker-hook
  2139. 'org-block-todo-from-checkboxes)
  2140. (remove-hook 'org-blocker-hook
  2141. 'org-block-todo-from-checkboxes)))
  2142. :group 'org-todo
  2143. :type 'boolean)
  2144. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2145. "Non-nil means inserting a TODO heading is treated as state change.
  2146. So when the command \\[org-insert-todo-heading] is used, state change
  2147. logging will apply if appropriate. When nil, the new TODO item will
  2148. be inserted directly, and no logging will take place."
  2149. :group 'org-todo
  2150. :type 'boolean)
  2151. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2152. "Non-nil means switching TODO states with S-cursor counts as state change.
  2153. This is the default behavior. However, setting this to nil allows a
  2154. convenient way to select a TODO state and bypass any logging associated
  2155. with that."
  2156. :group 'org-todo
  2157. :type 'boolean)
  2158. (defcustom org-todo-state-tags-triggers nil
  2159. "Tag changes that should be triggered by TODO state changes.
  2160. This is a list. Each entry is
  2161. (state-change (tag . flag) .......)
  2162. State-change can be a string with a state, and empty string to indicate the
  2163. state that has no TODO keyword, or it can be one of the symbols `todo'
  2164. or `done', meaning any not-done or done state, respectively."
  2165. :group 'org-todo
  2166. :group 'org-tags
  2167. :type '(repeat
  2168. (cons (choice :tag "When changing to"
  2169. (const :tag "Not-done state" todo)
  2170. (const :tag "Done state" done)
  2171. (string :tag "State"))
  2172. (repeat
  2173. (cons :tag "Tag action"
  2174. (string :tag "Tag")
  2175. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2176. (defcustom org-log-done nil
  2177. "Information to record when a task moves to the DONE state.
  2178. Possible values are:
  2179. nil Don't add anything, just change the keyword
  2180. time Add a time stamp to the task
  2181. note Prompt for a note and add it with template `org-log-note-headings'
  2182. This option can also be set with on a per-file-basis with
  2183. #+STARTUP: nologdone
  2184. #+STARTUP: logdone
  2185. #+STARTUP: lognotedone
  2186. You can have local logging settings for a subtree by setting the LOGGING
  2187. property to one or more of these keywords."
  2188. :group 'org-todo
  2189. :group 'org-progress
  2190. :type '(choice
  2191. (const :tag "No logging" nil)
  2192. (const :tag "Record CLOSED timestamp" time)
  2193. (const :tag "Record CLOSED timestamp with note." note)))
  2194. ;; Normalize old uses of org-log-done.
  2195. (cond
  2196. ((eq org-log-done t) (setq org-log-done 'time))
  2197. ((and (listp org-log-done) (memq 'done org-log-done))
  2198. (setq org-log-done 'note)))
  2199. (defcustom org-log-reschedule nil
  2200. "Information to record when the scheduling date of a tasks is modified.
  2201. Possible values are:
  2202. nil Don't add anything, just change the date
  2203. time Add a time stamp to the task
  2204. note Prompt for a note and add it with template `org-log-note-headings'
  2205. This option can also be set with on a per-file-basis with
  2206. #+STARTUP: nologreschedule
  2207. #+STARTUP: logreschedule
  2208. #+STARTUP: lognotereschedule"
  2209. :group 'org-todo
  2210. :group 'org-progress
  2211. :type '(choice
  2212. (const :tag "No logging" nil)
  2213. (const :tag "Record timestamp" time)
  2214. (const :tag "Record timestamp with note." note)))
  2215. (defcustom org-log-redeadline nil
  2216. "Information to record when the deadline date of a tasks is modified.
  2217. Possible values are:
  2218. nil Don't add anything, just change the date
  2219. time Add a time stamp to the task
  2220. note Prompt for a note and add it with template `org-log-note-headings'
  2221. This option can also be set with on a per-file-basis with
  2222. #+STARTUP: nologredeadline
  2223. #+STARTUP: logredeadline
  2224. #+STARTUP: lognoteredeadline
  2225. You can have local logging settings for a subtree by setting the LOGGING
  2226. property to one or more of these keywords."
  2227. :group 'org-todo
  2228. :group 'org-progress
  2229. :type '(choice
  2230. (const :tag "No logging" nil)
  2231. (const :tag "Record timestamp" time)
  2232. (const :tag "Record timestamp with note." note)))
  2233. (defcustom org-log-note-clock-out nil
  2234. "Non-nil means record a note when clocking out of an item.
  2235. This can also be configured on a per-file basis by adding one of
  2236. the following lines anywhere in the buffer:
  2237. #+STARTUP: lognoteclock-out
  2238. #+STARTUP: nolognoteclock-out"
  2239. :group 'org-todo
  2240. :group 'org-progress
  2241. :type 'boolean)
  2242. (defcustom org-log-done-with-time t
  2243. "Non-nil means the CLOSED time stamp will contain date and time.
  2244. When nil, only the date will be recorded."
  2245. :group 'org-progress
  2246. :type 'boolean)
  2247. (defcustom org-log-note-headings
  2248. '((done . "CLOSING NOTE %t")
  2249. (state . "State %-12s from %-12S %t")
  2250. (note . "Note taken on %t")
  2251. (reschedule . "Rescheduled from %S on %t")
  2252. (delschedule . "Not scheduled, was %S on %t")
  2253. (redeadline . "New deadline from %S on %t")
  2254. (deldeadline . "Removed deadline, was %S on %t")
  2255. (refile . "Refiled on %t")
  2256. (clock-out . ""))
  2257. "Headings for notes added to entries.
  2258. The value is an alist, with the car being a symbol indicating the note
  2259. context, and the cdr is the heading to be used. The heading may also be the
  2260. empty string.
  2261. %t in the heading will be replaced by a time stamp.
  2262. %T will be an active time stamp instead the default inactive one
  2263. %d will be replaced by a short-format time stamp.
  2264. %D will be replaced by an active short-format time stamp.
  2265. %s will be replaced by the new TODO state, in double quotes.
  2266. %S will be replaced by the old TODO state, in double quotes.
  2267. %u will be replaced by the user name.
  2268. %U will be replaced by the full user name.
  2269. In fact, it is not a good idea to change the `state' entry, because
  2270. agenda log mode depends on the format of these entries."
  2271. :group 'org-todo
  2272. :group 'org-progress
  2273. :type '(list :greedy t
  2274. (cons (const :tag "Heading when closing an item" done) string)
  2275. (cons (const :tag
  2276. "Heading when changing todo state (todo sequence only)"
  2277. state) string)
  2278. (cons (const :tag "Heading when just taking a note" note) string)
  2279. (cons (const :tag "Heading when clocking out" clock-out) string)
  2280. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2281. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2282. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2283. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2284. (cons (const :tag "Heading when refiling" refile) string)))
  2285. (unless (assq 'note org-log-note-headings)
  2286. (push '(note . "%t") org-log-note-headings))
  2287. (defcustom org-log-into-drawer nil
  2288. "Non-nil means insert state change notes and time stamps into a drawer.
  2289. When nil, state changes notes will be inserted after the headline and
  2290. any scheduling and clock lines, but not inside a drawer.
  2291. The value of this variable should be the name of the drawer to use.
  2292. LOGBOOK is proposed as the default drawer for this purpose, you can
  2293. also set this to a string to define the drawer of your choice.
  2294. A value of t is also allowed, representing \"LOGBOOK\".
  2295. A value of t or nil can also be set with on a per-file-basis with
  2296. #+STARTUP: logdrawer
  2297. #+STARTUP: nologdrawer
  2298. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2299. will be ignored.
  2300. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2301. a subtree."
  2302. :group 'org-todo
  2303. :group 'org-progress
  2304. :type '(choice
  2305. (const :tag "Not into a drawer" nil)
  2306. (const :tag "LOGBOOK" t)
  2307. (string :tag "Other")))
  2308. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2309. (defun org-log-into-drawer ()
  2310. "Return the value of `org-log-into-drawer', but let properties overrule.
  2311. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2312. used instead of the default value."
  2313. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2314. (cond
  2315. ((not p) org-log-into-drawer)
  2316. ((equal p "nil") nil)
  2317. ((equal p "t") "LOGBOOK")
  2318. (t p))))
  2319. (defcustom org-log-state-notes-insert-after-drawers nil
  2320. "Non-nil means insert state change notes after any drawers in entry.
  2321. Only the drawers that *immediately* follow the headline and the
  2322. deadline/scheduled line are skipped.
  2323. When nil, insert notes right after the heading and perhaps the line
  2324. with deadline/scheduling if present.
  2325. This variable will have no effect if `org-log-into-drawer' is
  2326. set."
  2327. :group 'org-todo
  2328. :group 'org-progress
  2329. :type 'boolean)
  2330. (defcustom org-log-states-order-reversed t
  2331. "Non-nil means the latest state note will be directly after heading.
  2332. When nil, the state change notes will be ordered according to time.
  2333. This option can also be set with on a per-file-basis with
  2334. #+STARTUP: logstatesreversed
  2335. #+STARTUP: nologstatesreversed"
  2336. :group 'org-todo
  2337. :group 'org-progress
  2338. :type 'boolean)
  2339. (defcustom org-todo-repeat-to-state nil
  2340. "The TODO state to which a repeater should return the repeating task.
  2341. By default this is the first task in a TODO sequence, or the previous state
  2342. in a TODO_TYP set. But you can specify another task here.
  2343. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2344. :group 'org-todo
  2345. :version "24.1"
  2346. :type '(choice (const :tag "Head of sequence" nil)
  2347. (string :tag "Specific state")))
  2348. (defcustom org-log-repeat 'time
  2349. "Non-nil means record moving through the DONE state when triggering repeat.
  2350. An auto-repeating task is immediately switched back to TODO when
  2351. marked DONE. If you are not logging state changes (by adding \"@\"
  2352. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2353. record a closing note, there will be no record of the task moving
  2354. through DONE. This variable forces taking a note anyway.
  2355. nil Don't force a record
  2356. time Record a time stamp
  2357. note Prompt for a note and add it with template `org-log-note-headings'
  2358. This option can also be set with on a per-file-basis with
  2359. #+STARTUP: nologrepeat
  2360. #+STARTUP: logrepeat
  2361. #+STARTUP: lognoterepeat
  2362. You can have local logging settings for a subtree by setting the LOGGING
  2363. property to one or more of these keywords."
  2364. :group 'org-todo
  2365. :group 'org-progress
  2366. :type '(choice
  2367. (const :tag "Don't force a record" nil)
  2368. (const :tag "Force recording the DONE state" time)
  2369. (const :tag "Force recording a note with the DONE state" note)))
  2370. (defgroup org-priorities nil
  2371. "Priorities in Org-mode."
  2372. :tag "Org Priorities"
  2373. :group 'org-todo)
  2374. (defcustom org-enable-priority-commands t
  2375. "Non-nil means priority commands are active.
  2376. When nil, these commands will be disabled, so that you never accidentally
  2377. set a priority."
  2378. :group 'org-priorities
  2379. :type 'boolean)
  2380. (defcustom org-highest-priority ?A
  2381. "The highest priority of TODO items. A character like ?A, ?B etc.
  2382. Must have a smaller ASCII number than `org-lowest-priority'."
  2383. :group 'org-priorities
  2384. :type 'character)
  2385. (defcustom org-lowest-priority ?C
  2386. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2387. Must have a larger ASCII number than `org-highest-priority'."
  2388. :group 'org-priorities
  2389. :type 'character)
  2390. (defcustom org-default-priority ?B
  2391. "The default priority of TODO items.
  2392. This is the priority an item gets if no explicit priority is given.
  2393. When starting to cycle on an empty priority the first step in the cycle
  2394. depends on `org-priority-start-cycle-with-default'. The resulting first
  2395. step priority must not exceed the range from `org-highest-priority' to
  2396. `org-lowest-priority' which means that `org-default-priority' has to be
  2397. in this range exclusive or inclusive the range boundaries. Else the
  2398. first step refuses to set the default and the second will fall back
  2399. to (depending on the command used) the highest or lowest priority."
  2400. :group 'org-priorities
  2401. :type 'character)
  2402. (defcustom org-priority-start-cycle-with-default t
  2403. "Non-nil means start with default priority when starting to cycle.
  2404. When this is nil, the first step in the cycle will be (depending on the
  2405. command used) one higher or lower than the default priority.
  2406. See also `org-default-priority'."
  2407. :group 'org-priorities
  2408. :type 'boolean)
  2409. (defcustom org-get-priority-function nil
  2410. "Function to extract the priority from a string.
  2411. The string is normally the headline. If this is nil Org computes the
  2412. priority from the priority cookie like [#A] in the headline. It returns
  2413. an integer, increasing by 1000 for each priority level.
  2414. The user can set a different function here, which should take a string
  2415. as an argument and return the numeric priority."
  2416. :group 'org-priorities
  2417. :version "24.1"
  2418. :type 'function)
  2419. (defgroup org-time nil
  2420. "Options concerning time stamps and deadlines in Org-mode."
  2421. :tag "Org Time"
  2422. :group 'org)
  2423. (defcustom org-insert-labeled-timestamps-at-point nil
  2424. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2425. When nil, these labeled time stamps are forces into the second line of an
  2426. entry, just after the headline. When scheduling from the global TODO list,
  2427. the time stamp will always be forced into the second line."
  2428. :group 'org-time
  2429. :type 'boolean)
  2430. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2431. "Formats for `format-time-string' which are used for time stamps.
  2432. It is not recommended to change this constant.")
  2433. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2434. "Number of minutes to round time stamps to.
  2435. These are two values, the first applies when first creating a time stamp.
  2436. The second applies when changing it with the commands `S-up' and `S-down'.
  2437. When changing the time stamp, this means that it will change in steps
  2438. of N minutes, as given by the second value.
  2439. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2440. numbers should be factors of 60, so for example 5, 10, 15.
  2441. When this is larger than 1, you can still force an exact time stamp by using
  2442. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2443. and by using a prefix arg to `S-up/down' to specify the exact number
  2444. of minutes to shift."
  2445. :group 'org-time
  2446. :get #'(lambda (var) ; Make sure both elements are there
  2447. (if (integerp (default-value var))
  2448. (list (default-value var) 5)
  2449. (default-value var)))
  2450. :type '(list
  2451. (integer :tag "when inserting times")
  2452. (integer :tag "when modifying times")))
  2453. ;; Normalize old customizations of this variable.
  2454. (when (integerp org-time-stamp-rounding-minutes)
  2455. (setq org-time-stamp-rounding-minutes
  2456. (list org-time-stamp-rounding-minutes
  2457. org-time-stamp-rounding-minutes)))
  2458. (defcustom org-display-custom-times nil
  2459. "Non-nil means overlay custom formats over all time stamps.
  2460. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2461. To turn this on on a per-file basis, insert anywhere in the file:
  2462. #+STARTUP: customtime"
  2463. :group 'org-time
  2464. :set 'set-default
  2465. :type 'sexp)
  2466. (make-variable-buffer-local 'org-display-custom-times)
  2467. (defcustom org-time-stamp-custom-formats
  2468. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2469. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2470. These are overlaid over the default ISO format if the variable
  2471. `org-display-custom-times' is set. Time like %H:%M should be at the
  2472. end of the second format. The custom formats are also honored by export
  2473. commands, if custom time display is turned on at the time of export."
  2474. :group 'org-time
  2475. :type 'sexp)
  2476. (defun org-time-stamp-format (&optional long inactive)
  2477. "Get the right format for a time string."
  2478. (let ((f (if long (cdr org-time-stamp-formats)
  2479. (car org-time-stamp-formats))))
  2480. (if inactive
  2481. (concat "[" (substring f 1 -1) "]")
  2482. f)))
  2483. (defcustom org-time-clocksum-format
  2484. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2485. "The format string used when creating CLOCKSUM lines.
  2486. This is also used when Org mode generates a time duration.
  2487. The value can be a single format string containing two
  2488. %-sequences, which will be filled with the number of hours and
  2489. minutes in that order.
  2490. Alternatively, the value can be a plist associating any of the
  2491. keys :years, :months, :weeks, :days, :hours or :minutes with
  2492. format strings. The time duration is formatted using only the
  2493. time components that are needed and concatenating the results.
  2494. If a time unit in absent, it falls back to the next smallest
  2495. unit.
  2496. The keys :require-years, :require-months, :require-days,
  2497. :require-weeks, :require-hours, :require-minutes are also
  2498. meaningful. A non-nil value for these keys indicates that the
  2499. corresponding time component should always be included, even if
  2500. its value is 0.
  2501. For example,
  2502. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2503. :require-minutes t)
  2504. means durations longer than a day will be expressed in days,
  2505. hours and minutes, and durations less than a day will always be
  2506. expressed in hours and minutes (even for durations less than an
  2507. hour).
  2508. The value
  2509. \(:days \"%dd\" :minutes \"%dm\")
  2510. means durations longer than a day will be expressed in days and
  2511. minutes, and durations less than a day will be expressed entirely
  2512. in minutes (even for durations longer than an hour)."
  2513. :group 'org-time
  2514. :group 'org-clock
  2515. :version "24.4"
  2516. :package-version '(Org . "8.0")
  2517. :type '(choice (string :tag "Format string")
  2518. (set :tag "Plist"
  2519. (group :inline t (const :tag "Years" :years)
  2520. (string :tag "Format string"))
  2521. (group :inline t
  2522. (const :tag "Always show years" :require-years)
  2523. (const t))
  2524. (group :inline t (const :tag "Months" :months)
  2525. (string :tag "Format string"))
  2526. (group :inline t
  2527. (const :tag "Always show months" :require-months)
  2528. (const t))
  2529. (group :inline t (const :tag "Weeks" :weeks)
  2530. (string :tag "Format string"))
  2531. (group :inline t
  2532. (const :tag "Always show weeks" :require-weeks)
  2533. (const t))
  2534. (group :inline t (const :tag "Days" :days)
  2535. (string :tag "Format string"))
  2536. (group :inline t
  2537. (const :tag "Always show days" :require-days)
  2538. (const t))
  2539. (group :inline t (const :tag "Hours" :hours)
  2540. (string :tag "Format string"))
  2541. (group :inline t
  2542. (const :tag "Always show hours" :require-hours)
  2543. (const t))
  2544. (group :inline t (const :tag "Minutes" :minutes)
  2545. (string :tag "Format string"))
  2546. (group :inline t
  2547. (const :tag "Always show minutes" :require-minutes)
  2548. (const t)))))
  2549. (defcustom org-time-clocksum-use-fractional nil
  2550. "When non-nil, \\[org-clock-display] uses fractional times.
  2551. See `org-time-clocksum-format' for more on time clock formats."
  2552. :group 'org-time
  2553. :group 'org-clock
  2554. :version "24.3"
  2555. :type 'boolean)
  2556. (defcustom org-time-clocksum-use-effort-durations nil
  2557. "When non-nil, \\[org-clock-display] uses effort durations.
  2558. E.g. by default, one day is considered to be a 8 hours effort,
  2559. so a task that has been clocked for 16 hours will be displayed
  2560. as during 2 days in the clock display or in the clocktable.
  2561. See `org-effort-durations' on how to set effort durations
  2562. and `org-time-clocksum-format' for more on time clock formats."
  2563. :group 'org-time
  2564. :group 'org-clock
  2565. :version "24.4"
  2566. :package-version '(Org . "8.0")
  2567. :type 'boolean)
  2568. (defcustom org-time-clocksum-fractional-format "%.2f"
  2569. "The format string used when creating CLOCKSUM lines,
  2570. or when Org mode generates a time duration, if
  2571. `org-time-clocksum-use-fractional' is enabled.
  2572. The value can be a single format string containing one
  2573. %-sequence, which will be filled with the number of hours as
  2574. a float.
  2575. Alternatively, the value can be a plist associating any of the
  2576. keys :years, :months, :weeks, :days, :hours or :minutes with
  2577. a format string. The time duration is formatted using the
  2578. largest time unit which gives a non-zero integer part. If all
  2579. specified formats have zero integer part, the smallest time unit
  2580. is used."
  2581. :group 'org-time
  2582. :type '(choice (string :tag "Format string")
  2583. (set (group :inline t (const :tag "Years" :years)
  2584. (string :tag "Format string"))
  2585. (group :inline t (const :tag "Months" :months)
  2586. (string :tag "Format string"))
  2587. (group :inline t (const :tag "Weeks" :weeks)
  2588. (string :tag "Format string"))
  2589. (group :inline t (const :tag "Days" :days)
  2590. (string :tag "Format string"))
  2591. (group :inline t (const :tag "Hours" :hours)
  2592. (string :tag "Format string"))
  2593. (group :inline t (const :tag "Minutes" :minutes)
  2594. (string :tag "Format string")))))
  2595. (defcustom org-deadline-warning-days 14
  2596. "Number of days before expiration during which a deadline becomes active.
  2597. This variable governs the display in sparse trees and in the agenda.
  2598. When 0 or negative, it means use this number (the absolute value of it)
  2599. even if a deadline has a different individual lead time specified.
  2600. Custom commands can set this variable in the options section."
  2601. :group 'org-time
  2602. :group 'org-agenda-daily/weekly
  2603. :type 'integer)
  2604. (defcustom org-scheduled-delay-days 0
  2605. "Number of days before a scheduled item becomes active.
  2606. This variable governs the display in sparse trees and in the agenda.
  2607. The default value (i.e. 0) means: don't delay scheduled item.
  2608. When negative, it means use this number (the absolute value of it)
  2609. even if a scheduled item has a different individual delay time
  2610. specified.
  2611. Custom commands can set this variable in the options section."
  2612. :group 'org-time
  2613. :group 'org-agenda-daily/weekly
  2614. :version "24.4"
  2615. :package-version '(Org . "8.0")
  2616. :type 'integer)
  2617. (defcustom org-read-date-prefer-future t
  2618. "Non-nil means assume future for incomplete date input from user.
  2619. This affects the following situations:
  2620. 1. The user gives a month but not a year.
  2621. For example, if it is April and you enter \"feb 2\", this will be read
  2622. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2623. 2. The user gives a day, but no month.
  2624. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2625. read this as the third of *next* month. However, if you enter \"17\",
  2626. it will be considered as *this* month.
  2627. If you set this variable to the symbol `time', then also the following
  2628. will work:
  2629. 3. If the user gives a time.
  2630. If the time is before now, it will be interpreted as tomorrow.
  2631. Currently none of this works for ISO week specifications.
  2632. When this option is nil, the current day, month and year will always be
  2633. used as defaults.
  2634. See also `org-agenda-jump-prefer-future'."
  2635. :group 'org-time
  2636. :type '(choice
  2637. (const :tag "Never" nil)
  2638. (const :tag "Check month and day" t)
  2639. (const :tag "Check month, day, and time" time)))
  2640. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2641. "Should the agenda jump command prefer the future for incomplete dates?
  2642. The default is to do the same as configured in `org-read-date-prefer-future'.
  2643. But you can also set a deviating value here.
  2644. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2645. :group 'org-agenda
  2646. :group 'org-time
  2647. :version "24.1"
  2648. :type '(choice
  2649. (const :tag "Use org-read-date-prefer-future"
  2650. org-read-date-prefer-future)
  2651. (const :tag "Never" nil)
  2652. (const :tag "Always" t)))
  2653. (defcustom org-read-date-force-compatible-dates t
  2654. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2655. Depending on the system Emacs is running on, certain dates cannot
  2656. be represented with the type used internally to represent time.
  2657. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2658. correctly. Some systems allow for earlier dates, some for later,
  2659. some for both. One way to find out it to insert any date into an
  2660. Org buffer, putting the cursor on the year and hitting S-up and
  2661. S-down to test the range.
  2662. When this variable is set to t, the date/time prompt will not let
  2663. you specify dates outside the 1970-2037 range, so it is certain that
  2664. these dates will work in whatever version of Emacs you are
  2665. running, and also that you can move a file from one Emacs implementation
  2666. to another. WHenever Org is forcing the year for you, it will display
  2667. a message and beep.
  2668. When this variable is nil, Org will check if the date is
  2669. representable in the specific Emacs implementation you are using.
  2670. If not, it will force a year, usually the current year, and beep
  2671. to remind you. Currently this setting is not recommended because
  2672. the likelihood that you will open your Org files in an Emacs that
  2673. has limited date range is not negligible.
  2674. A workaround for this problem is to use diary sexp dates for time
  2675. stamps outside of this range."
  2676. :group 'org-time
  2677. :version "24.1"
  2678. :type 'boolean)
  2679. (defcustom org-read-date-display-live t
  2680. "Non-nil means display current interpretation of date prompt live.
  2681. This display will be in an overlay, in the minibuffer."
  2682. :group 'org-time
  2683. :type 'boolean)
  2684. (defcustom org-read-date-popup-calendar t
  2685. "Non-nil means pop up a calendar when prompting for a date.
  2686. In the calendar, the date can be selected with mouse-1. However, the
  2687. minibuffer will also be active, and you can simply enter the date as well.
  2688. When nil, only the minibuffer will be available."
  2689. :group 'org-time
  2690. :type 'boolean)
  2691. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2692. 'org-read-date-popup-calendar)
  2693. (make-obsolete-variable
  2694. 'org-read-date-minibuffer-setup-hook
  2695. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2696. (defcustom org-read-date-minibuffer-setup-hook nil
  2697. "Hook to be used to set up keys for the date/time interface.
  2698. Add key definitions to `minibuffer-local-map', which will be a
  2699. temporary copy.
  2700. WARNING: This option is obsolete, you should use
  2701. `org-read-date-minibuffer-local-map' to set up keys."
  2702. :group 'org-time
  2703. :type 'hook)
  2704. (defcustom org-extend-today-until 0
  2705. "The hour when your day really ends. Must be an integer.
  2706. This has influence for the following applications:
  2707. - When switching the agenda to \"today\". It it is still earlier than
  2708. the time given here, the day recognized as TODAY is actually yesterday.
  2709. - When a date is read from the user and it is still before the time given
  2710. here, the current date and time will be assumed to be yesterday, 23:59.
  2711. Also, timestamps inserted in capture templates follow this rule.
  2712. IMPORTANT: This is a feature whose implementation is and likely will
  2713. remain incomplete. Really, it is only here because past midnight seems to
  2714. be the favorite working time of John Wiegley :-)"
  2715. :group 'org-time
  2716. :type 'integer)
  2717. (defcustom org-use-effective-time nil
  2718. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2719. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2720. \"effective time\" of any timestamps between midnight and 8am will be
  2721. 23:59 of the previous day."
  2722. :group 'org-time
  2723. :version "24.1"
  2724. :type 'boolean)
  2725. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2726. "When non-nil, use the last clock out time for `org-todo'.
  2727. Note that this option has precedence over the combined use of
  2728. `org-use-effective-time' and `org-extend-today-until'."
  2729. :group 'org-time
  2730. :version "24.4"
  2731. :package-version '(Org . "8.0")
  2732. :type 'boolean)
  2733. (defcustom org-edit-timestamp-down-means-later nil
  2734. "Non-nil means S-down will increase the time in a time stamp.
  2735. When nil, S-up will increase."
  2736. :group 'org-time
  2737. :type 'boolean)
  2738. (defcustom org-calendar-follow-timestamp-change t
  2739. "Non-nil means make the calendar window follow timestamp changes.
  2740. When a timestamp is modified and the calendar window is visible, it will be
  2741. moved to the new date."
  2742. :group 'org-time
  2743. :type 'boolean)
  2744. (defgroup org-tags nil
  2745. "Options concerning tags in Org-mode."
  2746. :tag "Org Tags"
  2747. :group 'org)
  2748. (defcustom org-tag-alist nil
  2749. "List of tags allowed in Org-mode files.
  2750. When this list is nil, Org-mode will base TAG input on what is already in the
  2751. buffer.
  2752. The value of this variable is an alist, the car of each entry must be a
  2753. keyword as a string, the cdr may be a character that is used to select
  2754. that tag through the fast-tag-selection interface.
  2755. See the manual for details."
  2756. :group 'org-tags
  2757. :type '(repeat
  2758. (choice
  2759. (cons (string :tag "Tag name")
  2760. (character :tag "Access char"))
  2761. (list :tag "Start radio group"
  2762. (const :startgroup)
  2763. (option (string :tag "Group description")))
  2764. (list :tag "End radio group"
  2765. (const :endgroup)
  2766. (option (string :tag "Group description")))
  2767. (const :tag "New line" (:newline)))))
  2768. (defcustom org-tag-persistent-alist nil
  2769. "List of tags that will always appear in all Org-mode files.
  2770. This is in addition to any in buffer settings or customizations
  2771. of `org-tag-alist'.
  2772. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2773. The value of this variable is an alist, the car of each entry must be a
  2774. keyword as a string, the cdr may be a character that is used to select
  2775. that tag through the fast-tag-selection interface.
  2776. See the manual for details.
  2777. To disable these tags on a per-file basis, insert anywhere in the file:
  2778. #+STARTUP: noptag"
  2779. :group 'org-tags
  2780. :type '(repeat
  2781. (choice
  2782. (cons (string :tag "Tag name")
  2783. (character :tag "Access char"))
  2784. (const :tag "Start radio group" (:startgroup))
  2785. (const :tag "End radio group" (:endgroup))
  2786. (const :tag "New line" (:newline)))))
  2787. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2788. "If non-nil, always offer completion for all tags of all agenda files.
  2789. Instead of customizing this variable directly, you might want to
  2790. set it locally for capture buffers, because there no list of
  2791. tags in that file can be created dynamically (there are none).
  2792. (add-hook 'org-capture-mode-hook
  2793. (lambda ()
  2794. (set (make-local-variable
  2795. 'org-complete-tags-always-offer-all-agenda-tags)
  2796. t)))"
  2797. :group 'org-tags
  2798. :version "24.1"
  2799. :type 'boolean)
  2800. (defvar org-file-tags nil
  2801. "List of tags that can be inherited by all entries in the file.
  2802. The tags will be inherited if the variable `org-use-tag-inheritance'
  2803. says they should be.
  2804. This variable is populated from #+FILETAGS lines.")
  2805. (defcustom org-use-fast-tag-selection 'auto
  2806. "Non-nil means use fast tag selection scheme.
  2807. This is a special interface to select and deselect tags with single keys.
  2808. When nil, fast selection is never used.
  2809. When the symbol `auto', fast selection is used if and only if selection
  2810. characters for tags have been configured, either through the variable
  2811. `org-tag-alist' or through a #+TAGS line in the buffer.
  2812. When t, fast selection is always used and selection keys are assigned
  2813. automatically if necessary."
  2814. :group 'org-tags
  2815. :type '(choice
  2816. (const :tag "Always" t)
  2817. (const :tag "Never" nil)
  2818. (const :tag "When selection characters are configured" 'auto)))
  2819. (defcustom org-fast-tag-selection-single-key nil
  2820. "Non-nil means fast tag selection exits after first change.
  2821. When nil, you have to press RET to exit it.
  2822. During fast tag selection, you can toggle this flag with `C-c'.
  2823. This variable can also have the value `expert'. In this case, the window
  2824. displaying the tags menu is not even shown, until you press C-c again."
  2825. :group 'org-tags
  2826. :type '(choice
  2827. (const :tag "No" nil)
  2828. (const :tag "Yes" t)
  2829. (const :tag "Expert" expert)))
  2830. (defvar org-fast-tag-selection-include-todo nil
  2831. "Non-nil means fast tags selection interface will also offer TODO states.
  2832. This is an undocumented feature, you should not rely on it.")
  2833. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2834. "The column to which tags should be indented in a headline.
  2835. If this number is positive, it specifies the column. If it is negative,
  2836. it means that the tags should be flushright to that column. For example,
  2837. -80 works well for a normal 80 character screen.
  2838. When 0, place tags directly after headline text, with only one space in
  2839. between."
  2840. :group 'org-tags
  2841. :type 'integer)
  2842. (defcustom org-auto-align-tags t
  2843. "Non-nil keeps tags aligned when modifying headlines.
  2844. Some operations (i.e. demoting) change the length of a headline and
  2845. therefore shift the tags around. With this option turned on, after
  2846. each such operation the tags are again aligned to `org-tags-column'."
  2847. :group 'org-tags
  2848. :type 'boolean)
  2849. (defcustom org-use-tag-inheritance t
  2850. "Non-nil means tags in levels apply also for sublevels.
  2851. When nil, only the tags directly given in a specific line apply there.
  2852. This may also be a list of tags that should be inherited, or a regexp that
  2853. matches tags that should be inherited. Additional control is possible
  2854. with the variable `org-tags-exclude-from-inheritance' which gives an
  2855. explicit list of tags to be excluded from inheritance, even if the value of
  2856. `org-use-tag-inheritance' would select it for inheritance.
  2857. If this option is t, a match early-on in a tree can lead to a large
  2858. number of matches in the subtree when constructing the agenda or creating
  2859. a sparse tree. If you only want to see the first match in a tree during
  2860. a search, check out the variable `org-tags-match-list-sublevels'."
  2861. :group 'org-tags
  2862. :type '(choice
  2863. (const :tag "Not" nil)
  2864. (const :tag "Always" t)
  2865. (repeat :tag "Specific tags" (string :tag "Tag"))
  2866. (regexp :tag "Tags matched by regexp")))
  2867. (defcustom org-tags-exclude-from-inheritance nil
  2868. "List of tags that should never be inherited.
  2869. This is a way to exclude a few tags from inheritance. For way to do
  2870. the opposite, to actively allow inheritance for selected tags,
  2871. see the variable `org-use-tag-inheritance'."
  2872. :group 'org-tags
  2873. :type '(repeat (string :tag "Tag")))
  2874. (defun org-tag-inherit-p (tag)
  2875. "Check if TAG is one that should be inherited."
  2876. (cond
  2877. ((member tag org-tags-exclude-from-inheritance) nil)
  2878. ((eq org-use-tag-inheritance t) t)
  2879. ((not org-use-tag-inheritance) nil)
  2880. ((stringp org-use-tag-inheritance)
  2881. (string-match org-use-tag-inheritance tag))
  2882. ((listp org-use-tag-inheritance)
  2883. (member tag org-use-tag-inheritance))
  2884. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2885. (defcustom org-tags-match-list-sublevels t
  2886. "Non-nil means list also sublevels of headlines matching a search.
  2887. This variable applies to tags/property searches, and also to stuck
  2888. projects because this search is based on a tags match as well.
  2889. When set to the symbol `indented', sublevels are indented with
  2890. leading dots.
  2891. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2892. the sublevels of a headline matching a tag search often also match
  2893. the same search. Listing all of them can create very long lists.
  2894. Setting this variable to nil causes subtrees of a match to be skipped.
  2895. This variable is semi-obsolete and probably should always be true. It
  2896. is better to limit inheritance to certain tags using the variables
  2897. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2898. :group 'org-tags
  2899. :type '(choice
  2900. (const :tag "No, don't list them" nil)
  2901. (const :tag "Yes, do list them" t)
  2902. (const :tag "List them, indented with leading dots" indented)))
  2903. (defcustom org-tags-sort-function nil
  2904. "When set, tags are sorted using this function as a comparator."
  2905. :group 'org-tags
  2906. :type '(choice
  2907. (const :tag "No sorting" nil)
  2908. (const :tag "Alphabetical" string<)
  2909. (const :tag "Reverse alphabetical" string>)
  2910. (function :tag "Custom function" nil)))
  2911. (defvar org-tags-history nil
  2912. "History of minibuffer reads for tags.")
  2913. (defvar org-last-tags-completion-table nil
  2914. "The last used completion table for tags.")
  2915. (defvar org-after-tags-change-hook nil
  2916. "Hook that is run after the tags in a line have changed.")
  2917. (defgroup org-properties nil
  2918. "Options concerning properties in Org-mode."
  2919. :tag "Org Properties"
  2920. :group 'org)
  2921. (defcustom org-property-format "%-10s %s"
  2922. "How property key/value pairs should be formatted by `indent-line'.
  2923. When `indent-line' hits a property definition, it will format the line
  2924. according to this format, mainly to make sure that the values are
  2925. lined-up with respect to each other."
  2926. :group 'org-properties
  2927. :type 'string)
  2928. (defcustom org-properties-postprocess-alist nil
  2929. "Alist of properties and functions to adjust inserted values.
  2930. Elements of this alist must be of the form
  2931. ([string] [function])
  2932. where [string] must be a property name and [function] must be a
  2933. lambda expression: this lambda expression must take one argument,
  2934. the value to adjust, and return the new value as a string.
  2935. For example, this element will allow the property \"Remaining\"
  2936. to be updated wrt the relation between the \"Effort\" property
  2937. and the clock summary:
  2938. ((\"Remaining\" (lambda(value)
  2939. (let ((clocksum (org-clock-sum-current-item))
  2940. (effort (org-duration-string-to-minutes
  2941. (org-entry-get (point) \"Effort\"))))
  2942. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2943. :group 'org-properties
  2944. :version "24.1"
  2945. :type '(alist :key-type (string :tag "Property")
  2946. :value-type (function :tag "Function")))
  2947. (defcustom org-use-property-inheritance nil
  2948. "Non-nil means properties apply also for sublevels.
  2949. This setting is chiefly used during property searches. Turning it on can
  2950. cause significant overhead when doing a search, which is why it is not
  2951. on by default.
  2952. When nil, only the properties directly given in the current entry count.
  2953. When t, every property is inherited. The value may also be a list of
  2954. properties that should have inheritance, or a regular expression matching
  2955. properties that should be inherited.
  2956. However, note that some special properties use inheritance under special
  2957. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2958. and the properties ending in \"_ALL\" when they are used as descriptor
  2959. for valid values of a property.
  2960. Note for programmers:
  2961. When querying an entry with `org-entry-get', you can control if inheritance
  2962. should be used. By default, `org-entry-get' looks only at the local
  2963. properties. You can request inheritance by setting the inherit argument
  2964. to t (to force inheritance) or to `selective' (to respect the setting
  2965. in this variable)."
  2966. :group 'org-properties
  2967. :type '(choice
  2968. (const :tag "Not" nil)
  2969. (const :tag "Always" t)
  2970. (repeat :tag "Specific properties" (string :tag "Property"))
  2971. (regexp :tag "Properties matched by regexp")))
  2972. (defun org-property-inherit-p (property)
  2973. "Check if PROPERTY is one that should be inherited."
  2974. (cond
  2975. ((eq org-use-property-inheritance t) t)
  2976. ((not org-use-property-inheritance) nil)
  2977. ((stringp org-use-property-inheritance)
  2978. (string-match org-use-property-inheritance property))
  2979. ((listp org-use-property-inheritance)
  2980. (member property org-use-property-inheritance))
  2981. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2982. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2983. "The default column format, if no other format has been defined.
  2984. This variable can be set on the per-file basis by inserting a line
  2985. #+COLUMNS: %25ITEM ....."
  2986. :group 'org-properties
  2987. :type 'string)
  2988. (defcustom org-columns-ellipses ".."
  2989. "The ellipses to be used when a field in column view is truncated.
  2990. When this is the empty string, as many characters as possible are shown,
  2991. but then there will be no visual indication that the field has been truncated.
  2992. When this is a string of length N, the last N characters of a truncated
  2993. field are replaced by this string. If the column is narrower than the
  2994. ellipses string, only part of the ellipses string will be shown."
  2995. :group 'org-properties
  2996. :type 'string)
  2997. (defcustom org-columns-modify-value-for-display-function nil
  2998. "Function that modifies values for display in column view.
  2999. For example, it can be used to cut out a certain part from a time stamp.
  3000. The function must take 2 arguments:
  3001. column-title The title of the column (*not* the property name)
  3002. value The value that should be modified.
  3003. The function should return the value that should be displayed,
  3004. or nil if the normal value should be used."
  3005. :group 'org-properties
  3006. :type 'function)
  3007. (defcustom org-effort-property "Effort"
  3008. "The property that is being used to keep track of effort estimates.
  3009. Effort estimates given in this property need to have the format H:MM."
  3010. :group 'org-properties
  3011. :group 'org-progress
  3012. :type '(string :tag "Property"))
  3013. (defconst org-global-properties-fixed
  3014. '(("VISIBILITY_ALL" . "folded children content all")
  3015. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3016. "List of property/value pairs that can be inherited by any entry.
  3017. These are fixed values, for the preset properties. The user variable
  3018. that can be used to add to this list is `org-global-properties'.
  3019. The entries in this list are cons cells where the car is a property
  3020. name and cdr is a string with the value. If the value represents
  3021. multiple items like an \"_ALL\" property, separate the items by
  3022. spaces.")
  3023. (defcustom org-global-properties nil
  3024. "List of property/value pairs that can be inherited by any entry.
  3025. This list will be combined with the constant `org-global-properties-fixed'.
  3026. The entries in this list are cons cells where the car is a property
  3027. name and cdr is a string with the value.
  3028. You can set buffer-local values for the same purpose in the variable
  3029. `org-file-properties' this by adding lines like
  3030. #+PROPERTY: NAME VALUE"
  3031. :group 'org-properties
  3032. :type '(repeat
  3033. (cons (string :tag "Property")
  3034. (string :tag "Value"))))
  3035. (defvar org-file-properties nil
  3036. "List of property/value pairs that can be inherited by any entry.
  3037. Valid for the current buffer.
  3038. This variable is populated from #+PROPERTY lines.")
  3039. (make-variable-buffer-local 'org-file-properties)
  3040. (defgroup org-agenda nil
  3041. "Options concerning agenda views in Org-mode."
  3042. :tag "Org Agenda"
  3043. :group 'org)
  3044. (defvar org-category nil
  3045. "Variable used by org files to set a category for agenda display.
  3046. Such files should use a file variable to set it, for example
  3047. # -*- mode: org; org-category: \"ELisp\"
  3048. or contain a special line
  3049. #+CATEGORY: ELisp
  3050. If the file does not specify a category, then file's base name
  3051. is used instead.")
  3052. (make-variable-buffer-local 'org-category)
  3053. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3054. (defcustom org-agenda-files nil
  3055. "The files to be used for agenda display.
  3056. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3057. \\[org-remove-file]. You can also use customize to edit the list.
  3058. If an entry is a directory, all files in that directory that are matched by
  3059. `org-agenda-file-regexp' will be part of the file list.
  3060. If the value of the variable is not a list but a single file name, then
  3061. the list of agenda files is actually stored and maintained in that file, one
  3062. agenda file per line. In this file paths can be given relative to
  3063. `org-directory'. Tilde expansion and environment variable substitution
  3064. are also made."
  3065. :group 'org-agenda
  3066. :type '(choice
  3067. (repeat :tag "List of files and directories" file)
  3068. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3069. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3070. "Regular expression to match files for `org-agenda-files'.
  3071. If any element in the list in that variable contains a directory instead
  3072. of a normal file, all files in that directory that are matched by this
  3073. regular expression will be included."
  3074. :group 'org-agenda
  3075. :type 'regexp)
  3076. (defcustom org-agenda-text-search-extra-files nil
  3077. "List of extra files to be searched by text search commands.
  3078. These files will be search in addition to the agenda files by the
  3079. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3080. Note that these files will only be searched for text search commands,
  3081. not for the other agenda views like todo lists, tag searches or the weekly
  3082. agenda. This variable is intended to list notes and possibly archive files
  3083. that should also be searched by these two commands.
  3084. In fact, if the first element in the list is the symbol `agenda-archives',
  3085. than all archive files of all agenda files will be added to the search
  3086. scope."
  3087. :group 'org-agenda
  3088. :type '(set :greedy t
  3089. (const :tag "Agenda Archives" agenda-archives)
  3090. (repeat :inline t (file))))
  3091. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3092. 'org-agenda-text-search-extra-files)
  3093. (defcustom org-agenda-skip-unavailable-files nil
  3094. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3095. A nil value means to remove them, after a query, from the list."
  3096. :group 'org-agenda
  3097. :type 'boolean)
  3098. (defcustom org-calendar-to-agenda-key [?c]
  3099. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3100. The command `org-calendar-goto-agenda' will be bound to this key. The
  3101. default is the character `c' because then `c' can be used to switch back and
  3102. forth between agenda and calendar."
  3103. :group 'org-agenda
  3104. :type 'sexp)
  3105. (defcustom org-calendar-insert-diary-entry-key [?i]
  3106. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3107. This option is irrelevant until `org-agenda-diary-file' has been configured
  3108. to point to an Org-mode file. When that is the case, the command
  3109. `org-agenda-diary-entry' will be bound to the key given here, by default
  3110. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3111. if you want to continue doing this, you need to change this to a different
  3112. key."
  3113. :group 'org-agenda
  3114. :type 'sexp)
  3115. (defcustom org-agenda-diary-file 'diary-file
  3116. "File to which to add new entries with the `i' key in agenda and calendar.
  3117. When this is the symbol `diary-file', the functionality in the Emacs
  3118. calendar will be used to add entries to the `diary-file'. But when this
  3119. points to a file, `org-agenda-diary-entry' will be used instead."
  3120. :group 'org-agenda
  3121. :type '(choice
  3122. (const :tag "The standard Emacs diary file" diary-file)
  3123. (file :tag "Special Org file diary entries")))
  3124. (eval-after-load "calendar"
  3125. '(progn
  3126. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3127. 'org-calendar-goto-agenda)
  3128. (add-hook 'calendar-mode-hook
  3129. (lambda ()
  3130. (unless (eq org-agenda-diary-file 'diary-file)
  3131. (define-key calendar-mode-map
  3132. org-calendar-insert-diary-entry-key
  3133. 'org-agenda-diary-entry))))))
  3134. (defgroup org-latex nil
  3135. "Options for embedding LaTeX code into Org-mode."
  3136. :tag "Org LaTeX"
  3137. :group 'org)
  3138. (defcustom org-format-latex-options
  3139. '(:foreground default :background default :scale 1.0
  3140. :html-foreground "Black" :html-background "Transparent"
  3141. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3142. "Options for creating images from LaTeX fragments.
  3143. This is a property list with the following properties:
  3144. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3145. `default' means use the foreground of the default face.
  3146. `auto' means use the foreground from the text face.
  3147. :background the background color, or \"Transparent\".
  3148. `default' means use the background of the default face.
  3149. `auto' means use the background from the text face.
  3150. :scale a scaling factor for the size of the images, to get more pixels
  3151. :html-foreground, :html-background, :html-scale
  3152. the same numbers for HTML export.
  3153. :matchers a list indicating which matchers should be used to
  3154. find LaTeX fragments. Valid members of this list are:
  3155. \"begin\" find environments
  3156. \"$1\" find single characters surrounded by $.$
  3157. \"$\" find math expressions surrounded by $...$
  3158. \"$$\" find math expressions surrounded by $$....$$
  3159. \"\\(\" find math expressions surrounded by \\(...\\)
  3160. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3161. :group 'org-latex
  3162. :type 'plist)
  3163. (defcustom org-format-latex-signal-error t
  3164. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3165. When nil, just push out a message."
  3166. :group 'org-latex
  3167. :version "24.1"
  3168. :type 'boolean)
  3169. (defcustom org-latex-to-mathml-jar-file nil
  3170. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3171. Use this to specify additional executable file say a jar file.
  3172. When using MathToWeb as the converter, specify the full-path to
  3173. your mathtoweb.jar file."
  3174. :group 'org-latex
  3175. :version "24.1"
  3176. :type '(choice
  3177. (const :tag "None" nil)
  3178. (file :tag "JAR file" :must-match t)))
  3179. (defcustom org-latex-to-mathml-convert-command nil
  3180. "Command to convert LaTeX fragments to MathML.
  3181. Replace format-specifiers in the command as noted below and use
  3182. `shell-command' to convert LaTeX to MathML.
  3183. %j: Executable file in fully expanded form as specified by
  3184. `org-latex-to-mathml-jar-file'.
  3185. %I: Input LaTeX file in fully expanded form
  3186. %o: Output MathML file
  3187. This command is used by `org-create-math-formula'.
  3188. When using MathToWeb as the converter, set this to
  3189. \"java -jar %j -unicode -force -df %o %I\"."
  3190. :group 'org-latex
  3191. :version "24.1"
  3192. :type '(choice
  3193. (const :tag "None" nil)
  3194. (string :tag "\nShell command")))
  3195. (defcustom org-latex-create-formula-image-program 'dvipng
  3196. "Program to convert LaTeX fragments with.
  3197. dvipng Process the LaTeX fragments to dvi file, then convert
  3198. dvi files to png files using dvipng.
  3199. This will also include processing of non-math environments.
  3200. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3201. to convert pdf files to png files"
  3202. :group 'org-latex
  3203. :version "24.1"
  3204. :type '(choice
  3205. (const :tag "dvipng" dvipng)
  3206. (const :tag "imagemagick" imagemagick)))
  3207. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3208. "Path to store latex preview images.
  3209. A relative path here creates many directories relative to the
  3210. processed org files paths. An absolute path puts all preview
  3211. images at the same place."
  3212. :group 'org-latex
  3213. :version "24.3"
  3214. :type 'string)
  3215. (defun org-format-latex-mathml-available-p ()
  3216. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3217. (save-match-data
  3218. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3219. org-latex-to-mathml-convert-command)
  3220. (let ((executable (car (split-string
  3221. org-latex-to-mathml-convert-command))))
  3222. (when (executable-find executable)
  3223. (if (string-match
  3224. "%j" org-latex-to-mathml-convert-command)
  3225. (file-readable-p org-latex-to-mathml-jar-file)
  3226. t))))))
  3227. (defcustom org-format-latex-header "\\documentclass{article}
  3228. \\usepackage[usenames]{color}
  3229. \\usepackage{amsmath}
  3230. \\usepackage[mathscr]{eucal}
  3231. \\pagestyle{empty} % do not remove
  3232. \[PACKAGES]
  3233. \[DEFAULT-PACKAGES]
  3234. % The settings below are copied from fullpage.sty
  3235. \\setlength{\\textwidth}{\\paperwidth}
  3236. \\addtolength{\\textwidth}{-3cm}
  3237. \\setlength{\\oddsidemargin}{1.5cm}
  3238. \\addtolength{\\oddsidemargin}{-2.54cm}
  3239. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3240. \\setlength{\\textheight}{\\paperheight}
  3241. \\addtolength{\\textheight}{-\\headheight}
  3242. \\addtolength{\\textheight}{-\\headsep}
  3243. \\addtolength{\\textheight}{-\\footskip}
  3244. \\addtolength{\\textheight}{-3cm}
  3245. \\setlength{\\topmargin}{1.5cm}
  3246. \\addtolength{\\topmargin}{-2.54cm}"
  3247. "The document header used for processing LaTeX fragments.
  3248. It is imperative that this header make sure that no page number
  3249. appears on the page. The package defined in the variables
  3250. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3251. will either replace the placeholder \"[PACKAGES]\" in this
  3252. header, or they will be appended."
  3253. :group 'org-latex
  3254. :type 'string)
  3255. (defun org-set-packages-alist (var val)
  3256. "Set the packages alist and make sure it has 3 elements per entry."
  3257. (set var (mapcar (lambda (x)
  3258. (if (and (consp x) (= (length x) 2))
  3259. (list (car x) (nth 1 x) t)
  3260. x))
  3261. val)))
  3262. (defun org-get-packages-alist (var)
  3263. "Get the packages alist and make sure it has 3 elements per entry."
  3264. (mapcar (lambda (x)
  3265. (if (and (consp x) (= (length x) 2))
  3266. (list (car x) (nth 1 x) t)
  3267. x))
  3268. (default-value var)))
  3269. (defcustom org-latex-default-packages-alist
  3270. '(("AUTO" "inputenc" t)
  3271. ("T1" "fontenc" t)
  3272. ("" "fixltx2e" nil)
  3273. ("" "graphicx" t)
  3274. ("" "longtable" nil)
  3275. ("" "float" nil)
  3276. ("" "wrapfig" nil)
  3277. ("" "soul" t)
  3278. ("" "textcomp" t)
  3279. ("" "marvosym" t)
  3280. ("" "wasysym" t)
  3281. ("" "latexsym" t)
  3282. ("" "amssymb" t)
  3283. ("" "hyperref" nil)
  3284. "\\tolerance=1000")
  3285. "Alist of default packages to be inserted in the header.
  3286. Change this only if one of the packages here causes an
  3287. incompatibility with another package you are using.
  3288. The packages in this list are needed by one part or another of
  3289. Org mode to function properly:
  3290. - inputenc, fontenc: for basic font and character selection
  3291. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3292. symbols used for interpreting the entities in `org-entities'.
  3293. You can skip some of these packages if you don't use any of the
  3294. symbols in it.
  3295. - graphicx: for including images
  3296. - float, wrapfig: for figure placement
  3297. - longtable: for long tables
  3298. - hyperref: for cross references
  3299. Therefore you should not modify this variable unless you know
  3300. what you are doing. The one reason to change it anyway is that
  3301. you might be loading some other package that conflicts with one
  3302. of the default packages. Each cell is of the format
  3303. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3304. the package also needs to be included when compiling LaTeX
  3305. snippets into images for inclusion into non-LaTeX output."
  3306. :group 'org-latex
  3307. :group 'org-export-latex
  3308. :set 'org-set-packages-alist
  3309. :get 'org-get-packages-alist
  3310. :version "24.1"
  3311. :type '(repeat
  3312. (choice
  3313. (list :tag "options/package pair"
  3314. (string :tag "options")
  3315. (string :tag "package")
  3316. (boolean :tag "Snippet"))
  3317. (string :tag "A line of LaTeX"))))
  3318. (defcustom org-latex-packages-alist nil
  3319. "Alist of packages to be inserted in every LaTeX header.
  3320. These will be inserted after `org-latex-default-packages-alist'.
  3321. Each cell is of the format:
  3322. \(\"options\" \"package\" snippet-flag)
  3323. SNIPPET-FLAG, when t, indicates that this package is also needed
  3324. when turning LaTeX snippets into images for inclusion into
  3325. non-LaTeX output.
  3326. Make sure that you only list packages here which:
  3327. - you want in every file
  3328. - do not conflict with the setup in `org-format-latex-header'.
  3329. - do not conflict with the default packages in
  3330. `org-latex-default-packages-alist'."
  3331. :group 'org-latex
  3332. :group 'org-export-latex
  3333. :set 'org-set-packages-alist
  3334. :get 'org-get-packages-alist
  3335. :type '(repeat
  3336. (choice
  3337. (list :tag "options/package pair"
  3338. (string :tag "options")
  3339. (string :tag "package")
  3340. (boolean :tag "Snippet"))
  3341. (string :tag "A line of LaTeX"))))
  3342. (defgroup org-appearance nil
  3343. "Settings for Org-mode appearance."
  3344. :tag "Org Appearance"
  3345. :group 'org)
  3346. (defcustom org-level-color-stars-only nil
  3347. "Non-nil means fontify only the stars in each headline.
  3348. When nil, the entire headline is fontified.
  3349. Changing it requires restart of `font-lock-mode' to become effective
  3350. also in regions already fontified."
  3351. :group 'org-appearance
  3352. :type 'boolean)
  3353. (defcustom org-hide-leading-stars nil
  3354. "Non-nil means hide the first N-1 stars in a headline.
  3355. This works by using the face `org-hide' for these stars. This
  3356. face is white for a light background, and black for a dark
  3357. background. You may have to customize the face `org-hide' to
  3358. make this work.
  3359. Changing it requires restart of `font-lock-mode' to become effective
  3360. also in regions already fontified.
  3361. You may also set this on a per-file basis by adding one of the following
  3362. lines to the buffer:
  3363. #+STARTUP: hidestars
  3364. #+STARTUP: showstars"
  3365. :group 'org-appearance
  3366. :type 'boolean)
  3367. (defcustom org-hidden-keywords nil
  3368. "List of symbols corresponding to keywords to be hidden the org buffer.
  3369. For example, a value '(title) for this list will make the document's title
  3370. appear in the buffer without the initial #+TITLE: keyword."
  3371. :group 'org-appearance
  3372. :version "24.1"
  3373. :type '(set (const :tag "#+AUTHOR" author)
  3374. (const :tag "#+DATE" date)
  3375. (const :tag "#+EMAIL" email)
  3376. (const :tag "#+TITLE" title)))
  3377. (defcustom org-custom-properties nil
  3378. "List of properties (as strings) with a special meaning.
  3379. The default use of these custom properties is to let the user
  3380. hide them with `org-toggle-custom-properties-visibility'."
  3381. :group 'org-properties
  3382. :group 'org-appearance
  3383. :version "24.3"
  3384. :type '(repeat (string :tag "Property Name")))
  3385. (defcustom org-fontify-done-headline nil
  3386. "Non-nil means change the face of a headline if it is marked DONE.
  3387. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3388. When this is non-nil, the headline after the keyword is set to the
  3389. `org-headline-done' as an additional indication."
  3390. :group 'org-appearance
  3391. :type 'boolean)
  3392. (defcustom org-fontify-emphasized-text t
  3393. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3394. Changing this variable requires a restart of Emacs to take effect."
  3395. :group 'org-appearance
  3396. :type 'boolean)
  3397. (defcustom org-fontify-whole-heading-line nil
  3398. "Non-nil means fontify the whole line for headings.
  3399. This is useful when setting a background color for the
  3400. org-level-* faces."
  3401. :group 'org-appearance
  3402. :type 'boolean)
  3403. (defcustom org-highlight-latex-and-related nil
  3404. "Non-nil means highlight LaTeX related syntax in the buffer.
  3405. When non nil, the value should be a list containing any of the
  3406. following symbols:
  3407. `latex' Highlight LaTeX snippets and environments.
  3408. `script' Highlight subscript and superscript.
  3409. `entities' Highlight entities."
  3410. :group 'org-appearance
  3411. :version "24.4"
  3412. :package-version '(Org . "8.0")
  3413. :type '(choice
  3414. (const :tag "No highlighting" nil)
  3415. (set :greedy t :tag "Highlight"
  3416. (const :tag "LaTeX snippets and environments" latex)
  3417. (const :tag "Subscript and superscript" script)
  3418. (const :tag "Entities" entities))))
  3419. (defcustom org-hide-emphasis-markers nil
  3420. "Non-nil mean font-lock should hide the emphasis marker characters."
  3421. :group 'org-appearance
  3422. :type 'boolean)
  3423. (defcustom org-pretty-entities nil
  3424. "Non-nil means show entities as UTF8 characters.
  3425. When nil, the \\name form remains in the buffer."
  3426. :group 'org-appearance
  3427. :version "24.1"
  3428. :type 'boolean)
  3429. (defcustom org-pretty-entities-include-sub-superscripts t
  3430. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3431. :group 'org-appearance
  3432. :version "24.1"
  3433. :type 'boolean)
  3434. (defvar org-emph-re nil
  3435. "Regular expression for matching emphasis.
  3436. After a match, the match groups contain these elements:
  3437. 0 The match of the full regular expression, including the characters
  3438. before and after the proper match
  3439. 1 The character before the proper match, or empty at beginning of line
  3440. 2 The proper match, including the leading and trailing markers
  3441. 3 The leading marker like * or /, indicating the type of highlighting
  3442. 4 The text between the emphasis markers, not including the markers
  3443. 5 The character after the match, empty at the end of a line")
  3444. (defvar org-verbatim-re nil
  3445. "Regular expression for matching verbatim text.")
  3446. (defvar org-emphasis-regexp-components) ; defined just below
  3447. (defvar org-emphasis-alist) ; defined just below
  3448. (defun org-set-emph-re (var val)
  3449. "Set variable and compute the emphasis regular expression."
  3450. (set var val)
  3451. (when (and (boundp 'org-emphasis-alist)
  3452. (boundp 'org-emphasis-regexp-components)
  3453. org-emphasis-alist org-emphasis-regexp-components)
  3454. (let* ((e org-emphasis-regexp-components)
  3455. (pre (car e))
  3456. (post (nth 1 e))
  3457. (border (nth 2 e))
  3458. (body (nth 3 e))
  3459. (nl (nth 4 e))
  3460. (body1 (concat body "*?"))
  3461. (markers (mapconcat 'car org-emphasis-alist ""))
  3462. (vmarkers (mapconcat
  3463. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3464. org-emphasis-alist "")))
  3465. ;; make sure special characters appear at the right position in the class
  3466. (if (string-match "\\^" markers)
  3467. (setq markers (concat (replace-match "" t t markers) "^")))
  3468. (if (string-match "-" markers)
  3469. (setq markers (concat (replace-match "" t t markers) "-")))
  3470. (if (string-match "\\^" vmarkers)
  3471. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3472. (if (string-match "-" vmarkers)
  3473. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3474. (if (> nl 0)
  3475. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3476. (int-to-string nl) "\\}")))
  3477. ;; Make the regexp
  3478. (setq org-emph-re
  3479. (concat "\\([" pre "]\\|^\\)"
  3480. "\\("
  3481. "\\([" markers "]\\)"
  3482. "\\("
  3483. "[^" border "]\\|"
  3484. "[^" border "]"
  3485. body1
  3486. "[^" border "]"
  3487. "\\)"
  3488. "\\3\\)"
  3489. "\\([" post "]\\|$\\)"))
  3490. (setq org-verbatim-re
  3491. (concat "\\([" pre "]\\|^\\)"
  3492. "\\("
  3493. "\\([" vmarkers "]\\)"
  3494. "\\("
  3495. "[^" border "]\\|"
  3496. "[^" border "]"
  3497. body1
  3498. "[^" border "]"
  3499. "\\)"
  3500. "\\3\\)"
  3501. "\\([" post "]\\|$\\)")))))
  3502. (defcustom org-emphasis-regexp-components
  3503. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3504. "Components used to build the regular expression for emphasis.
  3505. This is a list with five entries. Terminology: In an emphasis string
  3506. like \" *strong word* \", we call the initial space PREMATCH, the final
  3507. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3508. and \"trong wor\" is the body. The different components in this variable
  3509. specify what is allowed/forbidden in each part:
  3510. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3511. post Chars allowed as postmatch. End of line will be allowed too.
  3512. border The chars *forbidden* as border characters.
  3513. body-regexp A regexp like \".\" to match a body character. Don't use
  3514. non-shy groups here, and don't allow newline here.
  3515. newline The maximum number of newlines allowed in an emphasis exp.
  3516. Use customize to modify this, or restart Emacs after changing it."
  3517. :group 'org-appearance
  3518. :set 'org-set-emph-re
  3519. :type '(list
  3520. (sexp :tag "Allowed chars in pre ")
  3521. (sexp :tag "Allowed chars in post ")
  3522. (sexp :tag "Forbidden chars in border ")
  3523. (sexp :tag "Regexp for body ")
  3524. (integer :tag "number of newlines allowed")
  3525. (option (boolean :tag "Please ignore this button"))))
  3526. (defcustom org-emphasis-alist
  3527. `(("*" bold "<b>" "</b>")
  3528. ("/" italic "<i>" "</i>")
  3529. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3530. ("=" org-code "<code>" "</code>" verbatim)
  3531. ("~" org-verbatim "<code>" "</code>" verbatim)
  3532. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3533. "<del>" "</del>")
  3534. )
  3535. "Special syntax for emphasized text.
  3536. Text starting and ending with a special character will be emphasized, for
  3537. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3538. characters, the face to be used by font-lock for highlighting in Org-mode
  3539. Emacs buffers, and the HTML tags to be used for this.
  3540. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3541. Use customize to modify this, or restart Emacs after changing it."
  3542. :group 'org-appearance
  3543. :set 'org-set-emph-re
  3544. :type '(repeat
  3545. (list
  3546. (string :tag "Marker character")
  3547. (choice
  3548. (face :tag "Font-lock-face")
  3549. (plist :tag "Face property list"))
  3550. (string :tag "HTML start tag")
  3551. (string :tag "HTML end tag")
  3552. (option (const verbatim)))))
  3553. (defvar org-protecting-blocks
  3554. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3555. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3556. This is needed for font-lock setup.")
  3557. ;;; Miscellaneous options
  3558. (defgroup org-completion nil
  3559. "Completion in Org-mode."
  3560. :tag "Org Completion"
  3561. :group 'org)
  3562. (defcustom org-completion-use-ido nil
  3563. "Non-nil means use ido completion wherever possible.
  3564. Note that `ido-mode' must be active for this variable to be relevant.
  3565. If you decide to turn this variable on, you might well want to turn off
  3566. `org-outline-path-complete-in-steps'.
  3567. See also `org-completion-use-iswitchb'."
  3568. :group 'org-completion
  3569. :type 'boolean)
  3570. (defcustom org-completion-use-iswitchb nil
  3571. "Non-nil means use iswitchb completion wherever possible.
  3572. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3573. If you decide to turn this variable on, you might well want to turn off
  3574. `org-outline-path-complete-in-steps'.
  3575. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3576. :group 'org-completion
  3577. :type 'boolean)
  3578. (defcustom org-completion-fallback-command 'hippie-expand
  3579. "The expansion command called by \\[pcomplete] in normal context.
  3580. Normal means, no org-mode-specific context."
  3581. :group 'org-completion
  3582. :type 'function)
  3583. ;;; Functions and variables from their packages
  3584. ;; Declared here to avoid compiler warnings
  3585. ;; XEmacs only
  3586. (defvar outline-mode-menu-heading)
  3587. (defvar outline-mode-menu-show)
  3588. (defvar outline-mode-menu-hide)
  3589. (defvar zmacs-regions) ; XEmacs regions
  3590. ;; Emacs only
  3591. (defvar mark-active)
  3592. ;; Various packages
  3593. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3594. (declare-function calendar-forward-day "cal-move" (arg))
  3595. (declare-function calendar-goto-date "cal-move" (date))
  3596. (declare-function calendar-goto-today "cal-move" ())
  3597. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3598. (defvar calc-embedded-close-formula)
  3599. (defvar calc-embedded-open-formula)
  3600. (declare-function cdlatex-tab "ext:cdlatex" ())
  3601. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3602. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3603. (defvar font-lock-unfontify-region-function)
  3604. (declare-function iswitchb-read-buffer "iswitchb"
  3605. (prompt &optional default require-match start matches-set))
  3606. (defvar iswitchb-temp-buflist)
  3607. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3608. (defvar org-agenda-tags-todo-honor-ignore-options)
  3609. (declare-function org-agenda-skip "org-agenda" ())
  3610. (declare-function
  3611. org-agenda-format-item "org-agenda"
  3612. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3613. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3614. (declare-function org-agenda-change-all-lines "org-agenda"
  3615. (newhead hdmarker &optional fixface just-this))
  3616. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3617. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3618. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3619. (beg end))
  3620. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3621. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3622. "org-agenda" (&optional end))
  3623. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3624. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3625. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3626. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3627. (declare-function org-indent-mode "org-indent" (&optional arg))
  3628. (declare-function parse-time-string "parse-time" (string))
  3629. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3630. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3631. (defvar remember-data-file)
  3632. (defvar texmathp-why)
  3633. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3634. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3635. (defvar org-latex-regexps)
  3636. ;;; Autoload and prepare some org modules
  3637. ;; Some table stuff that needs to be defined here, because it is used
  3638. ;; by the functions setting up org-mode or checking for table context.
  3639. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3640. "Detect an org-type or table-type table.")
  3641. (defconst org-table-line-regexp "^[ \t]*|"
  3642. "Detect an org-type table line.")
  3643. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3644. "Detect an org-type table line.")
  3645. (defconst org-table-hline-regexp "^[ \t]*|-"
  3646. "Detect an org-type table hline.")
  3647. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3648. "Detect a table-type table hline.")
  3649. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3650. "Detect the first line outside a table when searching from within it.
  3651. This works for both table types.")
  3652. ;; Autoload the functions in org-table.el that are needed by functions here.
  3653. (eval-and-compile
  3654. (org-autoload "org-table"
  3655. '(org-table-begin org-table-blank-field org-table-end)))
  3656. ;;;###autoload
  3657. (defun turn-on-orgtbl ()
  3658. "Unconditionally turn on `orgtbl-mode'."
  3659. (require 'org-table)
  3660. (orgtbl-mode 1))
  3661. (defun org-at-table-p (&optional table-type)
  3662. "Return t if the cursor is inside an org-type table.
  3663. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3664. (if org-enable-table-editor
  3665. (save-excursion
  3666. (beginning-of-line 1)
  3667. (looking-at (if table-type org-table-any-line-regexp
  3668. org-table-line-regexp)))
  3669. nil))
  3670. (defsubst org-table-p () (org-at-table-p))
  3671. (defun org-at-table.el-p ()
  3672. "Return t if and only if we are at a table.el table."
  3673. (and (org-at-table-p 'any)
  3674. (save-excursion
  3675. (goto-char (org-table-begin 'any))
  3676. (looking-at org-table1-hline-regexp))))
  3677. (defun org-table-recognize-table.el ()
  3678. "If there is a table.el table nearby, recognize it and move into it."
  3679. (if org-table-tab-recognizes-table.el
  3680. (if (org-at-table.el-p)
  3681. (progn
  3682. (beginning-of-line 1)
  3683. (if (looking-at org-table-dataline-regexp)
  3684. nil
  3685. (if (looking-at org-table1-hline-regexp)
  3686. (progn
  3687. (beginning-of-line 2)
  3688. (if (looking-at org-table-any-border-regexp)
  3689. (beginning-of-line -1)))))
  3690. (if (re-search-forward "|" (org-table-end t) t)
  3691. (progn
  3692. (require 'table)
  3693. (if (table--at-cell-p (point))
  3694. t
  3695. (message "recognizing table.el table...")
  3696. (table-recognize-table)
  3697. (message "recognizing table.el table...done")))
  3698. (error "This should not happen"))
  3699. t)
  3700. nil)
  3701. nil))
  3702. (defun org-at-table-hline-p ()
  3703. "Return t if the cursor is inside a hline in a table."
  3704. (if org-enable-table-editor
  3705. (save-excursion
  3706. (beginning-of-line 1)
  3707. (looking-at org-table-hline-regexp))
  3708. nil))
  3709. (defvar org-table-clean-did-remove-column nil)
  3710. (defun org-table-map-tables (function &optional quietly)
  3711. "Apply FUNCTION to the start of all tables in the buffer."
  3712. (save-excursion
  3713. (save-restriction
  3714. (widen)
  3715. (goto-char (point-min))
  3716. (while (re-search-forward org-table-any-line-regexp nil t)
  3717. (unless quietly
  3718. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3719. (beginning-of-line 1)
  3720. (when (and (looking-at org-table-line-regexp)
  3721. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3722. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3723. (save-excursion (funcall function))
  3724. (or (looking-at org-table-line-regexp)
  3725. (forward-char 1)))
  3726. (re-search-forward org-table-any-border-regexp nil 1))))
  3727. (unless quietly (message "Mapping tables: done")))
  3728. ;; Declare and autoload functions from ox.el and al.
  3729. (declare-function org-export-get-environment "ox"
  3730. (&optional backend subtreep ext-plist))
  3731. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3732. ;; Declare and autoload functions from org-agenda.el
  3733. (eval-and-compile
  3734. (org-autoload "org-agenda"
  3735. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3736. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3737. (declare-function org-clock-update-mode-line "org-clock" ())
  3738. (declare-function org-resolve-clocks "org-clock"
  3739. (&optional also-non-dangling-p prompt last-valid))
  3740. (defvar org-clock-start-time)
  3741. (defvar org-clock-marker (make-marker)
  3742. "Marker recording the last clock-in.")
  3743. (defvar org-clock-hd-marker (make-marker)
  3744. "Marker recording the last clock-in, but the headline position.")
  3745. (defvar org-clock-heading ""
  3746. "The heading of the current clock entry.")
  3747. (defun org-clock-is-active ()
  3748. "Return non-nil if clock is currently running.
  3749. The return value is actually the clock marker."
  3750. (marker-buffer org-clock-marker))
  3751. (eval-and-compile
  3752. (org-autoload "org-clock" '(org-clock-remove-overlays
  3753. org-clock-update-time-maybe
  3754. org-clocktable-shift)))
  3755. (defun org-check-running-clock ()
  3756. "Check if the current buffer contains the running clock.
  3757. If yes, offer to stop it and to save the buffer with the changes."
  3758. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3759. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3760. (buffer-name))))
  3761. (org-clock-out)
  3762. (when (y-or-n-p "Save changed buffer?")
  3763. (save-buffer))))
  3764. (defun org-clocktable-try-shift (dir n)
  3765. "Check if this line starts a clock table, if yes, shift the time block."
  3766. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3767. (org-clocktable-shift dir n)))
  3768. ;;;###autoload
  3769. (defun org-clock-persistence-insinuate ()
  3770. "Set up hooks for clock persistence."
  3771. (require 'org-clock)
  3772. (add-hook 'org-mode-hook 'org-clock-load)
  3773. (add-hook 'kill-emacs-hook 'org-clock-save))
  3774. ;; Define the variable already here, to make sure we have it.
  3775. (defvar org-indent-mode nil
  3776. "Non-nil if Org-Indent mode is enabled.
  3777. Use the command `org-indent-mode' to change this variable.")
  3778. ;; Autoload archiving code
  3779. ;; The stuff that is needed for cycling and tags has to be defined here.
  3780. (defgroup org-archive nil
  3781. "Options concerning archiving in Org-mode."
  3782. :tag "Org Archive"
  3783. :group 'org-structure)
  3784. (defcustom org-archive-location "%s_archive::"
  3785. "The location where subtrees should be archived.
  3786. The value of this variable is a string, consisting of two parts,
  3787. separated by a double-colon. The first part is a filename and
  3788. the second part is a headline.
  3789. When the filename is omitted, archiving happens in the same file.
  3790. %s in the filename will be replaced by the current file
  3791. name (without the directory part). Archiving to a different file
  3792. is useful to keep archived entries from contributing to the
  3793. Org-mode Agenda.
  3794. The archived entries will be filed as subtrees of the specified
  3795. headline. When the headline is omitted, the subtrees are simply
  3796. filed away at the end of the file, as top-level entries. Also in
  3797. the heading you can use %s to represent the file name, this can be
  3798. useful when using the same archive for a number of different files.
  3799. Here are a few examples:
  3800. \"%s_archive::\"
  3801. If the current file is Projects.org, archive in file
  3802. Projects.org_archive, as top-level trees. This is the default.
  3803. \"::* Archived Tasks\"
  3804. Archive in the current file, under the top-level headline
  3805. \"* Archived Tasks\".
  3806. \"~/org/archive.org::\"
  3807. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3808. \"~/org/archive.org::* From %s\"
  3809. Archive in file ~/org/archive.org (absolute path), under headlines
  3810. \"From FILENAME\" where file name is the current file name.
  3811. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3812. The \"datetree/\" string is special, signifying to archive
  3813. items to the datetree. Items are placed in either the CLOSED
  3814. date of the item, or the current date if there is no CLOSED date.
  3815. The heading will be a subentry to the current date. There doesn't
  3816. need to be a heading, but there always needs to be a slash after
  3817. datetree. For example, to store archived items directly in the
  3818. datetree, use \"~/org/datetree.org::datetree/\".
  3819. \"basement::** Finished Tasks\"
  3820. Archive in file ./basement (relative path), as level 3 trees
  3821. below the level 2 heading \"** Finished Tasks\".
  3822. You may set this option on a per-file basis by adding to the buffer a
  3823. line like
  3824. #+ARCHIVE: basement::** Finished Tasks
  3825. You may also define it locally for a subtree by setting an ARCHIVE property
  3826. in the entry. If such a property is found in an entry, or anywhere up
  3827. the hierarchy, it will be used."
  3828. :group 'org-archive
  3829. :type 'string)
  3830. (defcustom org-archive-tag "ARCHIVE"
  3831. "The tag that marks a subtree as archived.
  3832. An archived subtree does not open during visibility cycling, and does
  3833. not contribute to the agenda listings.
  3834. After changing this, font-lock must be restarted in the relevant buffers to
  3835. get the proper fontification."
  3836. :group 'org-archive
  3837. :group 'org-keywords
  3838. :type 'string)
  3839. (defcustom org-agenda-skip-archived-trees t
  3840. "Non-nil means the agenda will skip any items located in archived trees.
  3841. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3842. variable is no longer recommended, you should leave it at the value t.
  3843. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3844. :group 'org-archive
  3845. :group 'org-agenda-skip
  3846. :type 'boolean)
  3847. (defcustom org-columns-skip-archived-trees t
  3848. "Non-nil means ignore archived trees when creating column view."
  3849. :group 'org-archive
  3850. :group 'org-properties
  3851. :type 'boolean)
  3852. (defcustom org-cycle-open-archived-trees nil
  3853. "Non-nil means `org-cycle' will open archived trees.
  3854. An archived tree is a tree marked with the tag ARCHIVE.
  3855. When nil, archived trees will stay folded. You can still open them with
  3856. normal outline commands like `show-all', but not with the cycling commands."
  3857. :group 'org-archive
  3858. :group 'org-cycle
  3859. :type 'boolean)
  3860. (defcustom org-sparse-tree-open-archived-trees nil
  3861. "Non-nil means sparse tree construction shows matches in archived trees.
  3862. When nil, matches in these trees are highlighted, but the trees are kept in
  3863. collapsed state."
  3864. :group 'org-archive
  3865. :group 'org-sparse-trees
  3866. :type 'boolean)
  3867. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3868. "The default date type when building a sparse tree.
  3869. When this is nil, a date is a scheduled or a deadline timestamp.
  3870. Otherwise, these types are allowed:
  3871. all: all timestamps
  3872. active: only active timestamps (<...>)
  3873. inactive: only inactive timestamps (<...)
  3874. scheduled: only scheduled timestamps
  3875. deadline: only deadline timestamps"
  3876. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3877. (const :tag "All timestamps" all)
  3878. (const :tag "Only active timestamps" active)
  3879. (const :tag "Only inactive timestamps" inactive)
  3880. (const :tag "Only scheduled timestamps" scheduled)
  3881. (const :tag "Only deadline timestamps" deadline))
  3882. :version "24.3"
  3883. :group 'org-sparse-trees)
  3884. (defun org-cycle-hide-archived-subtrees (state)
  3885. "Re-hide all archived subtrees after a visibility state change."
  3886. (when (and (not org-cycle-open-archived-trees)
  3887. (not (memq state '(overview folded))))
  3888. (save-excursion
  3889. (let* ((globalp (memq state '(contents all)))
  3890. (beg (if globalp (point-min) (point)))
  3891. (end (if globalp (point-max) (org-end-of-subtree t))))
  3892. (org-hide-archived-subtrees beg end)
  3893. (goto-char beg)
  3894. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3895. (message "%s" (substitute-command-keys
  3896. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3897. (defun org-force-cycle-archived ()
  3898. "Cycle subtree even if it is archived."
  3899. (interactive)
  3900. (setq this-command 'org-cycle)
  3901. (let ((org-cycle-open-archived-trees t))
  3902. (call-interactively 'org-cycle)))
  3903. (defun org-hide-archived-subtrees (beg end)
  3904. "Re-hide all archived subtrees after a visibility state change."
  3905. (save-excursion
  3906. (let* ((re (concat ":" org-archive-tag ":")))
  3907. (goto-char beg)
  3908. (while (re-search-forward re end t)
  3909. (when (org-at-heading-p)
  3910. (org-flag-subtree t)
  3911. (org-end-of-subtree t))))))
  3912. (declare-function outline-end-of-heading "outline" ())
  3913. (declare-function outline-flag-region "outline" (from to flag))
  3914. (defun org-flag-subtree (flag)
  3915. (save-excursion
  3916. (org-back-to-heading t)
  3917. (outline-end-of-heading)
  3918. (outline-flag-region (point)
  3919. (progn (org-end-of-subtree t) (point))
  3920. flag)))
  3921. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3922. (eval-and-compile
  3923. (org-autoload "org-archive"
  3924. '(org-add-archive-files)))
  3925. ;; Autoload Column View Code
  3926. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3927. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3928. (declare-function org-columns-compute "org-colview" (property))
  3929. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3930. '(org-columns-number-to-string
  3931. org-columns-get-format-and-top-level
  3932. org-columns-compute
  3933. org-columns-remove-overlays))
  3934. ;; Autoload ID code
  3935. (declare-function org-id-store-link "org-id")
  3936. (declare-function org-id-locations-load "org-id")
  3937. (declare-function org-id-locations-save "org-id")
  3938. (defvar org-id-track-globally)
  3939. (org-autoload "org-id"
  3940. '(org-id-new
  3941. org-id-copy
  3942. org-id-get-with-outline-path-completion
  3943. org-id-get-with-outline-drilling))
  3944. ;;; Variables for pre-computed regular expressions, all buffer local
  3945. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3946. "Matches first line of a hidden block.")
  3947. (make-variable-buffer-local 'org-drawer-regexp)
  3948. (defvar org-todo-regexp nil
  3949. "Matches any of the TODO state keywords.")
  3950. (make-variable-buffer-local 'org-todo-regexp)
  3951. (defvar org-not-done-regexp nil
  3952. "Matches any of the TODO state keywords except the last one.")
  3953. (make-variable-buffer-local 'org-not-done-regexp)
  3954. (defvar org-not-done-heading-regexp nil
  3955. "Matches a TODO headline that is not done.")
  3956. (make-variable-buffer-local 'org-not-done-regexp)
  3957. (defvar org-todo-line-regexp nil
  3958. "Matches a headline and puts TODO state into group 2 if present.")
  3959. (make-variable-buffer-local 'org-todo-line-regexp)
  3960. (defvar org-complex-heading-regexp nil
  3961. "Matches a headline and puts everything into groups:
  3962. group 1: the stars
  3963. group 2: The todo keyword, maybe
  3964. group 3: Priority cookie
  3965. group 4: True headline
  3966. group 5: Tags")
  3967. (make-variable-buffer-local 'org-complex-heading-regexp)
  3968. (defvar org-complex-heading-regexp-format nil
  3969. "Printf format to make regexp to match an exact headline.
  3970. This regexp will match the headline of any node which has the
  3971. exact headline text that is put into the format, but may have any
  3972. TODO state, priority and tags.")
  3973. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3974. (defvar org-todo-line-tags-regexp nil
  3975. "Matches a headline and puts TODO state into group 2 if present.
  3976. Also put tags into group 4 if tags are present.")
  3977. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3978. (defvar org-ds-keyword-length 12
  3979. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3980. (make-variable-buffer-local 'org-ds-keyword-length)
  3981. (defvar org-deadline-regexp nil
  3982. "Matches the DEADLINE keyword.")
  3983. (make-variable-buffer-local 'org-deadline-regexp)
  3984. (defvar org-deadline-time-regexp nil
  3985. "Matches the DEADLINE keyword together with a time stamp.")
  3986. (make-variable-buffer-local 'org-deadline-time-regexp)
  3987. (defvar org-deadline-time-hour-regexp nil
  3988. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  3989. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  3990. (defvar org-deadline-line-regexp nil
  3991. "Matches the DEADLINE keyword and the rest of the line.")
  3992. (make-variable-buffer-local 'org-deadline-line-regexp)
  3993. (defvar org-scheduled-regexp nil
  3994. "Matches the SCHEDULED keyword.")
  3995. (make-variable-buffer-local 'org-scheduled-regexp)
  3996. (defvar org-scheduled-time-regexp nil
  3997. "Matches the SCHEDULED keyword together with a time stamp.")
  3998. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3999. (defvar org-scheduled-time-hour-regexp nil
  4000. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4001. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4002. (defvar org-closed-time-regexp nil
  4003. "Matches the CLOSED keyword together with a time stamp.")
  4004. (make-variable-buffer-local 'org-closed-time-regexp)
  4005. (defvar org-keyword-time-regexp nil
  4006. "Matches any of the 4 keywords, together with the time stamp.")
  4007. (make-variable-buffer-local 'org-keyword-time-regexp)
  4008. (defvar org-keyword-time-not-clock-regexp nil
  4009. "Matches any of the 3 keywords, together with the time stamp.")
  4010. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4011. (defvar org-maybe-keyword-time-regexp nil
  4012. "Matches a timestamp, possibly preceded by a keyword.")
  4013. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4014. (defvar org-all-time-keywords nil
  4015. "List of time keywords.")
  4016. (make-variable-buffer-local 'org-all-time-keywords)
  4017. (defconst org-plain-time-of-day-regexp
  4018. (concat
  4019. "\\(\\<[012]?[0-9]"
  4020. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4021. "\\(--?"
  4022. "\\(\\<[012]?[0-9]"
  4023. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4024. "\\)?")
  4025. "Regular expression to match a plain time or time range.
  4026. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4027. groups carry important information:
  4028. 0 the full match
  4029. 1 the first time, range or not
  4030. 8 the second time, if it is a range.")
  4031. (defconst org-plain-time-extension-regexp
  4032. (concat
  4033. "\\(\\<[012]?[0-9]"
  4034. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4035. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4036. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4037. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4038. groups carry important information:
  4039. 0 the full match
  4040. 7 hours of duration
  4041. 9 minutes of duration")
  4042. (defconst org-stamp-time-of-day-regexp
  4043. (concat
  4044. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4045. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4046. "\\(--?"
  4047. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4048. "Regular expression to match a timestamp time or time range.
  4049. After a match, the following groups carry important information:
  4050. 0 the full match
  4051. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4052. 2 the first time, range or not
  4053. 4 the second time, if it is a range.")
  4054. (defconst org-startup-options
  4055. '(("fold" org-startup-folded t)
  4056. ("overview" org-startup-folded t)
  4057. ("nofold" org-startup-folded nil)
  4058. ("showall" org-startup-folded nil)
  4059. ("showeverything" org-startup-folded showeverything)
  4060. ("content" org-startup-folded content)
  4061. ("indent" org-startup-indented t)
  4062. ("noindent" org-startup-indented nil)
  4063. ("hidestars" org-hide-leading-stars t)
  4064. ("showstars" org-hide-leading-stars nil)
  4065. ("odd" org-odd-levels-only t)
  4066. ("oddeven" org-odd-levels-only nil)
  4067. ("align" org-startup-align-all-tables t)
  4068. ("noalign" org-startup-align-all-tables nil)
  4069. ("inlineimages" org-startup-with-inline-images t)
  4070. ("noinlineimages" org-startup-with-inline-images nil)
  4071. ("latexpreview" org-startup-with-latex-preview t)
  4072. ("nolatexpreview" org-startup-with-latex-preview nil)
  4073. ("customtime" org-display-custom-times t)
  4074. ("logdone" org-log-done time)
  4075. ("lognotedone" org-log-done note)
  4076. ("nologdone" org-log-done nil)
  4077. ("lognoteclock-out" org-log-note-clock-out t)
  4078. ("nolognoteclock-out" org-log-note-clock-out nil)
  4079. ("logrepeat" org-log-repeat state)
  4080. ("lognoterepeat" org-log-repeat note)
  4081. ("logdrawer" org-log-into-drawer t)
  4082. ("nologdrawer" org-log-into-drawer nil)
  4083. ("logstatesreversed" org-log-states-order-reversed t)
  4084. ("nologstatesreversed" org-log-states-order-reversed nil)
  4085. ("nologrepeat" org-log-repeat nil)
  4086. ("logreschedule" org-log-reschedule time)
  4087. ("lognotereschedule" org-log-reschedule note)
  4088. ("nologreschedule" org-log-reschedule nil)
  4089. ("logredeadline" org-log-redeadline time)
  4090. ("lognoteredeadline" org-log-redeadline note)
  4091. ("nologredeadline" org-log-redeadline nil)
  4092. ("logrefile" org-log-refile time)
  4093. ("lognoterefile" org-log-refile note)
  4094. ("nologrefile" org-log-refile nil)
  4095. ("fninline" org-footnote-define-inline t)
  4096. ("nofninline" org-footnote-define-inline nil)
  4097. ("fnlocal" org-footnote-section nil)
  4098. ("fnauto" org-footnote-auto-label t)
  4099. ("fnprompt" org-footnote-auto-label nil)
  4100. ("fnconfirm" org-footnote-auto-label confirm)
  4101. ("fnplain" org-footnote-auto-label plain)
  4102. ("fnadjust" org-footnote-auto-adjust t)
  4103. ("nofnadjust" org-footnote-auto-adjust nil)
  4104. ("constcgs" constants-unit-system cgs)
  4105. ("constSI" constants-unit-system SI)
  4106. ("noptag" org-tag-persistent-alist nil)
  4107. ("hideblocks" org-hide-block-startup t)
  4108. ("nohideblocks" org-hide-block-startup nil)
  4109. ("beamer" org-startup-with-beamer-mode t)
  4110. ("entitiespretty" org-pretty-entities t)
  4111. ("entitiesplain" org-pretty-entities nil))
  4112. "Variable associated with STARTUP options for org-mode.
  4113. Each element is a list of three items: the startup options (as written
  4114. in the #+STARTUP line), the corresponding variable, and the value to set
  4115. this variable to if the option is found. An optional forth element PUSH
  4116. means to push this value onto the list in the variable.")
  4117. (defun org-update-property-plist (key val props)
  4118. "Update PROPS with KEY and VAL."
  4119. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4120. (key (if appending (substring key 0 (- (length key) 1)) key))
  4121. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4122. (previous (cdr (assoc key props))))
  4123. (if appending
  4124. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4125. (cons (cons key val) remainder))))
  4126. (defconst org-block-regexp
  4127. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4128. "Regular expression for hiding blocks.")
  4129. (defconst org-heading-keyword-regexp-format
  4130. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4131. "Printf format for a regexp matching a headline with some keyword.
  4132. This regexp will match the headline of any node which has the
  4133. exact keyword that is put into the format. The keyword isn't in
  4134. any group by default, but the stars and the body are.")
  4135. (defconst org-heading-keyword-maybe-regexp-format
  4136. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4137. "Printf format for a regexp matching a headline, possibly with some keyword.
  4138. This regexp can match any headline with the specified keyword, or
  4139. without a keyword. The keyword isn't in any group by default,
  4140. but the stars and the body are.")
  4141. (defun org-set-regexps-and-options ()
  4142. "Precompute regular expressions for current buffer."
  4143. (when (derived-mode-p 'org-mode)
  4144. (org-set-local 'org-todo-kwd-alist nil)
  4145. (org-set-local 'org-todo-key-alist nil)
  4146. (org-set-local 'org-todo-key-trigger nil)
  4147. (org-set-local 'org-todo-keywords-1 nil)
  4148. (org-set-local 'org-done-keywords nil)
  4149. (org-set-local 'org-todo-heads nil)
  4150. (org-set-local 'org-todo-sets nil)
  4151. (org-set-local 'org-todo-log-states nil)
  4152. (org-set-local 'org-file-properties nil)
  4153. (org-set-local 'org-file-tags nil)
  4154. (let ((re (org-make-options-regexp
  4155. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
  4156. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4157. "SETUPFILE" "OPTIONS")
  4158. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4159. (splitre "[ \t]+")
  4160. (scripts org-use-sub-superscripts)
  4161. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4162. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4163. (start 0))
  4164. (save-excursion
  4165. (save-restriction
  4166. (widen)
  4167. (goto-char (point-min))
  4168. (while (or (and ext-setup-or-nil
  4169. (string-match re ext-setup-or-nil start)
  4170. (setq start (match-end 0)))
  4171. (and (setq ext-setup-or-nil nil start 0)
  4172. (re-search-forward re nil t)))
  4173. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4174. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4175. (if (stringp value) (setq value (org-trim value)))
  4176. (cond
  4177. ((equal key "CATEGORY")
  4178. (setq cat value))
  4179. ((member key '("SEQ_TODO" "TODO"))
  4180. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4181. ((equal key "TYP_TODO")
  4182. (push (cons 'type (org-split-string value splitre)) kwds))
  4183. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4184. ;; general TODO-like setup
  4185. (push (cons (intern (downcase (match-string 1 key)))
  4186. (org-split-string value splitre)) kwds))
  4187. ((equal key "TAGS")
  4188. (setq tags (append tags (if tags '("\\n") nil)
  4189. (org-split-string value splitre))))
  4190. ((equal key "COLUMNS")
  4191. (org-set-local 'org-columns-default-format value))
  4192. ((equal key "LINK")
  4193. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4194. (push (cons (match-string 1 value)
  4195. (org-trim (match-string 2 value)))
  4196. links)))
  4197. ((equal key "PRIORITIES")
  4198. (setq prio (org-split-string value " +")))
  4199. ((equal key "PROPERTY")
  4200. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4201. (setq props (org-update-property-plist (match-string 1 value)
  4202. (match-string 2 value)
  4203. props))))
  4204. ((equal key "FILETAGS")
  4205. (when (string-match "\\S-" value)
  4206. (setq ftags
  4207. (append
  4208. ftags
  4209. (apply 'append
  4210. (mapcar (lambda (x) (org-split-string x ":"))
  4211. (org-split-string value)))))))
  4212. ((equal key "DRAWERS")
  4213. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4214. ((equal key "CONSTANTS")
  4215. (setq const (append const (org-split-string value splitre))))
  4216. ((equal key "STARTUP")
  4217. (let ((opts (org-split-string value splitre))
  4218. l var val)
  4219. (while (setq l (pop opts))
  4220. (when (setq l (assoc l org-startup-options))
  4221. (setq var (nth 1 l) val (nth 2 l))
  4222. (if (not (nth 3 l))
  4223. (set (make-local-variable var) val)
  4224. (if (not (listp (symbol-value var)))
  4225. (set (make-local-variable var) nil))
  4226. (set (make-local-variable var) (symbol-value var))
  4227. (add-to-list var val))))))
  4228. ((equal key "ARCHIVE")
  4229. (setq arch value)
  4230. (remove-text-properties 0 (length arch)
  4231. '(face t fontified t) arch))
  4232. ((equal key "OPTIONS")
  4233. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4234. (setq scripts (read (match-string 2 value)))))
  4235. ((equal key "SETUPFILE")
  4236. (setq setup-contents (org-file-contents
  4237. (expand-file-name
  4238. (org-remove-double-quotes value))
  4239. 'noerror))
  4240. (if (not ext-setup-or-nil)
  4241. (setq ext-setup-or-nil setup-contents start 0)
  4242. (setq ext-setup-or-nil
  4243. (concat (substring ext-setup-or-nil 0 start)
  4244. "\n" setup-contents "\n"
  4245. (substring ext-setup-or-nil start)))))))
  4246. ;; search for property blocks
  4247. (goto-char (point-min))
  4248. (while (re-search-forward org-block-regexp nil t)
  4249. (when (equal "PROPERTY" (upcase (match-string 1)))
  4250. (setq value (replace-regexp-in-string
  4251. "[\n\r]" " " (match-string 4)))
  4252. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4253. (setq props (org-update-property-plist (match-string 1 value)
  4254. (match-string 2 value)
  4255. props)))))))
  4256. (org-set-local 'org-use-sub-superscripts scripts)
  4257. (when cat
  4258. (org-set-local 'org-category (intern cat))
  4259. (push (cons "CATEGORY" cat) props))
  4260. (when prio
  4261. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4262. (setq prio (mapcar 'string-to-char prio))
  4263. (org-set-local 'org-highest-priority (nth 0 prio))
  4264. (org-set-local 'org-lowest-priority (nth 1 prio))
  4265. (org-set-local 'org-default-priority (nth 2 prio)))
  4266. (and props (org-set-local 'org-file-properties (nreverse props)))
  4267. (and ftags (org-set-local 'org-file-tags
  4268. (mapcar 'org-add-prop-inherited ftags)))
  4269. (and drawers (org-set-local 'org-drawers drawers))
  4270. (and arch (org-set-local 'org-archive-location arch))
  4271. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4272. ;; Process the TODO keywords
  4273. (unless kwds
  4274. ;; Use the global values as if they had been given locally.
  4275. (setq kwds (default-value 'org-todo-keywords))
  4276. (if (stringp (car kwds))
  4277. (setq kwds (list (cons org-todo-interpretation
  4278. (default-value 'org-todo-keywords)))))
  4279. (setq kwds (reverse kwds)))
  4280. (setq kwds (nreverse kwds))
  4281. (let (inter kws kw)
  4282. (while (setq kws (pop kwds))
  4283. (let ((kws (or
  4284. (run-hook-with-args-until-success
  4285. 'org-todo-setup-filter-hook kws)
  4286. kws)))
  4287. (setq inter (pop kws) sep (member "|" kws)
  4288. kws0 (delete "|" (copy-sequence kws))
  4289. kwsa nil
  4290. kws1 (mapcar
  4291. (lambda (x)
  4292. ;; 1 2
  4293. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4294. (progn
  4295. (setq kw (match-string 1 x)
  4296. key (and (match-end 2) (match-string 2 x))
  4297. log (org-extract-log-state-settings x))
  4298. (push (cons kw (and key (string-to-char key))) kwsa)
  4299. (and log (push log org-todo-log-states))
  4300. kw)
  4301. (error "Invalid TODO keyword %s" x)))
  4302. kws0)
  4303. kwsa (if kwsa (append '((:startgroup))
  4304. (nreverse kwsa)
  4305. '((:endgroup))))
  4306. hw (car kws1)
  4307. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4308. tail (list inter hw (car dws) (org-last dws))))
  4309. (add-to-list 'org-todo-heads hw 'append)
  4310. (push kws1 org-todo-sets)
  4311. (setq org-done-keywords (append org-done-keywords dws nil))
  4312. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4313. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4314. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4315. (setq org-todo-sets (nreverse org-todo-sets)
  4316. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4317. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4318. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4319. ;; Process the constants
  4320. (when const
  4321. (let (e cst)
  4322. (while (setq e (pop const))
  4323. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4324. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4325. (setq org-table-formula-constants-local cst)))
  4326. ;; Process the tags.
  4327. (when tags
  4328. (let (e tgs)
  4329. (while (setq e (pop tags))
  4330. (cond
  4331. ((equal e "{") (push '(:startgroup) tgs))
  4332. ((equal e "}") (push '(:endgroup) tgs))
  4333. ((equal e "\\n") (push '(:newline) tgs))
  4334. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4335. (push (cons (match-string 1 e)
  4336. (string-to-char (match-string 2 e)))
  4337. tgs))
  4338. (t (push (list e) tgs))))
  4339. (org-set-local 'org-tag-alist nil)
  4340. (while (setq e (pop tgs))
  4341. (or (and (stringp (car e))
  4342. (assoc (car e) org-tag-alist))
  4343. (push e org-tag-alist)))))
  4344. ;; Compute the regular expressions and other local variables.
  4345. ;; Using `org-outline-regexp-bol' would complicate them much,
  4346. ;; because of the fixed white space at the end of that string.
  4347. (if (not org-done-keywords)
  4348. (setq org-done-keywords (and org-todo-keywords-1
  4349. (list (org-last org-todo-keywords-1)))))
  4350. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4351. (length org-scheduled-string)
  4352. (length org-clock-string)
  4353. (length org-closed-string)))
  4354. org-drawer-regexp
  4355. (concat "^[ \t]*:\\("
  4356. (mapconcat 'regexp-quote org-drawers "\\|")
  4357. "\\):[ \t]*$")
  4358. org-not-done-keywords
  4359. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4360. org-todo-regexp
  4361. (concat "\\("
  4362. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4363. "\\)")
  4364. org-not-done-regexp
  4365. (concat "\\("
  4366. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4367. "\\)")
  4368. org-not-done-heading-regexp
  4369. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4370. org-todo-line-regexp
  4371. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4372. org-complex-heading-regexp
  4373. (concat "^\\(\\*+\\)"
  4374. "\\(?: +" org-todo-regexp "\\)?"
  4375. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4376. "\\(?: +\\(.*?\\)\\)??"
  4377. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4378. "[ \t]*$")
  4379. org-complex-heading-regexp-format
  4380. (concat "^\\(\\*+\\)"
  4381. "\\(?: +" org-todo-regexp "\\)?"
  4382. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4383. "\\(?: +"
  4384. ;; Stats cookies can be stuck to body.
  4385. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4386. "\\(%s\\)"
  4387. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4388. "\\)"
  4389. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4390. "[ \t]*$")
  4391. org-todo-line-tags-regexp
  4392. (concat "^\\(\\*+\\)"
  4393. "\\(?: +" org-todo-regexp "\\)?"
  4394. "\\(?: +\\(.*?\\)\\)??"
  4395. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4396. "[ \t]*$")
  4397. org-deadline-regexp (concat "\\<" org-deadline-string)
  4398. org-deadline-time-regexp
  4399. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4400. org-deadline-time-hour-regexp
  4401. (concat "\\<" org-deadline-string
  4402. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4403. org-deadline-line-regexp
  4404. (concat "\\<\\(" org-deadline-string "\\).*")
  4405. org-scheduled-regexp
  4406. (concat "\\<" org-scheduled-string)
  4407. org-scheduled-time-regexp
  4408. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4409. org-scheduled-time-hour-regexp
  4410. (concat "\\<" org-scheduled-string
  4411. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4412. org-closed-time-regexp
  4413. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4414. org-keyword-time-regexp
  4415. (concat "\\<\\(" org-scheduled-string
  4416. "\\|" org-deadline-string
  4417. "\\|" org-closed-string
  4418. "\\|" org-clock-string "\\)"
  4419. " *[[<]\\([^]>]+\\)[]>]")
  4420. org-keyword-time-not-clock-regexp
  4421. (concat "\\<\\(" org-scheduled-string
  4422. "\\|" org-deadline-string
  4423. "\\|" org-closed-string
  4424. "\\)"
  4425. " *[[<]\\([^]>]+\\)[]>]")
  4426. org-maybe-keyword-time-regexp
  4427. (concat "\\(\\<\\(" org-scheduled-string
  4428. "\\|" org-deadline-string
  4429. "\\|" org-closed-string
  4430. "\\|" org-clock-string "\\)\\)?"
  4431. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4432. org-all-time-keywords
  4433. (mapcar (lambda (w) (substring w 0 -1))
  4434. (list org-scheduled-string org-deadline-string
  4435. org-clock-string org-closed-string)))
  4436. (org-compute-latex-and-related-regexp)
  4437. (org-set-font-lock-defaults))))
  4438. (defun org-file-contents (file &optional noerror)
  4439. "Return the contents of FILE, as a string."
  4440. (if (or (not file)
  4441. (not (file-readable-p file)))
  4442. (if noerror
  4443. (progn
  4444. (message "Cannot read file \"%s\"" file)
  4445. (ding) (sit-for 2)
  4446. "")
  4447. (error "Cannot read file \"%s\"" file))
  4448. (with-temp-buffer
  4449. (insert-file-contents file)
  4450. (buffer-string))))
  4451. (defun org-extract-log-state-settings (x)
  4452. "Extract the log state setting from a TODO keyword string.
  4453. This will extract info from a string like \"WAIT(w@/!)\"."
  4454. (let (kw key log1 log2)
  4455. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4456. (setq kw (match-string 1 x)
  4457. key (and (match-end 2) (match-string 2 x))
  4458. log1 (and (match-end 3) (match-string 3 x))
  4459. log2 (and (match-end 4) (match-string 4 x)))
  4460. (and (or log1 log2)
  4461. (list kw
  4462. (and log1 (if (equal log1 "!") 'time 'note))
  4463. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4464. (defun org-remove-keyword-keys (list)
  4465. "Remove a pair of parenthesis at the end of each string in LIST."
  4466. (mapcar (lambda (x)
  4467. (if (string-match "(.*)$" x)
  4468. (substring x 0 (match-beginning 0))
  4469. x))
  4470. list))
  4471. (defun org-assign-fast-keys (alist)
  4472. "Assign fast keys to a keyword-key alist.
  4473. Respect keys that are already there."
  4474. (let (new e (alt ?0))
  4475. (while (setq e (pop alist))
  4476. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4477. (cdr e)) ;; Key already assigned.
  4478. (push e new)
  4479. (let ((clist (string-to-list (downcase (car e))))
  4480. (used (append new alist)))
  4481. (when (= (car clist) ?@)
  4482. (pop clist))
  4483. (while (and clist (rassoc (car clist) used))
  4484. (pop clist))
  4485. (unless clist
  4486. (while (rassoc alt used)
  4487. (incf alt)))
  4488. (push (cons (car e) (or (car clist) alt)) new))))
  4489. (nreverse new)))
  4490. ;;; Some variables used in various places
  4491. (defvar org-window-configuration nil
  4492. "Used in various places to store a window configuration.")
  4493. (defvar org-selected-window nil
  4494. "Used in various places to store a window configuration.")
  4495. (defvar org-finish-function nil
  4496. "Function to be called when `C-c C-c' is used.
  4497. This is for getting out of special buffers like capture.")
  4498. ;; FIXME: Occasionally check by commenting these, to make sure
  4499. ;; no other functions uses these, forgetting to let-bind them.
  4500. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4501. (defvar org-last-state)
  4502. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4503. ;; Defined somewhere in this file, but used before definition.
  4504. (defvar org-entities) ;; defined in org-entities.el
  4505. (defvar org-struct-menu)
  4506. (defvar org-org-menu)
  4507. (defvar org-tbl-menu)
  4508. ;;;; Define the Org-mode
  4509. ;; We use a before-change function to check if a table might need
  4510. ;; an update.
  4511. (defvar org-table-may-need-update t
  4512. "Indicates that a table might need an update.
  4513. This variable is set by `org-before-change-function'.
  4514. `org-table-align' sets it back to nil.")
  4515. (defun org-before-change-function (beg end)
  4516. "Every change indicates that a table might need an update."
  4517. (setq org-table-may-need-update t))
  4518. (defvar org-mode-map)
  4519. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4520. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4521. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4522. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4523. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4524. (defvar org-table-buffer-is-an nil)
  4525. (defvar bidi-paragraph-direction)
  4526. (defvar buffer-face-mode-face)
  4527. (require 'outline)
  4528. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4529. (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"))
  4530. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4531. ;; Other stuff we need.
  4532. (require 'time-date)
  4533. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4534. (require 'easymenu)
  4535. (require 'overlay)
  4536. ;; (require 'org-macs) moved higher up in the file before it is first used
  4537. (require 'org-entities)
  4538. ;; (require 'org-compat) moved higher up in the file before it is first used
  4539. (require 'org-faces)
  4540. (require 'org-list)
  4541. (require 'org-pcomplete)
  4542. (require 'org-src)
  4543. (require 'org-footnote)
  4544. (require 'org-macro)
  4545. ;; babel
  4546. (require 'ob)
  4547. ;;;###autoload
  4548. (define-derived-mode org-mode outline-mode "Org"
  4549. "Outline-based notes management and organizer, alias
  4550. \"Carsten's outline-mode for keeping track of everything.\"
  4551. Org-mode develops organizational tasks around a NOTES file which
  4552. contains information about projects as plain text. Org-mode is
  4553. implemented on top of outline-mode, which is ideal to keep the content
  4554. of large files well structured. It supports ToDo items, deadlines and
  4555. time stamps, which magically appear in the diary listing of the Emacs
  4556. calendar. Tables are easily created with a built-in table editor.
  4557. Plain text URL-like links connect to websites, emails (VM), Usenet
  4558. messages (Gnus), BBDB entries, and any files related to the project.
  4559. For printing and sharing of notes, an Org-mode file (or a part of it)
  4560. can be exported as a structured ASCII or HTML file.
  4561. The following commands are available:
  4562. \\{org-mode-map}"
  4563. ;; Get rid of Outline menus, they are not needed
  4564. ;; Need to do this here because define-derived-mode sets up
  4565. ;; the keymap so late. Still, it is a waste to call this each time
  4566. ;; we switch another buffer into org-mode.
  4567. (if (featurep 'xemacs)
  4568. (when (boundp 'outline-mode-menu-heading)
  4569. ;; Assume this is Greg's port, it uses easymenu
  4570. (easy-menu-remove outline-mode-menu-heading)
  4571. (easy-menu-remove outline-mode-menu-show)
  4572. (easy-menu-remove outline-mode-menu-hide))
  4573. (define-key org-mode-map [menu-bar headings] 'undefined)
  4574. (define-key org-mode-map [menu-bar hide] 'undefined)
  4575. (define-key org-mode-map [menu-bar show] 'undefined))
  4576. (org-load-modules-maybe)
  4577. (easy-menu-add org-org-menu)
  4578. (easy-menu-add org-tbl-menu)
  4579. (org-install-agenda-files-menu)
  4580. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4581. (add-to-invisibility-spec '(org-cwidth))
  4582. (add-to-invisibility-spec '(org-hide-block . t))
  4583. (when (featurep 'xemacs)
  4584. (org-set-local 'line-move-ignore-invisible t))
  4585. (org-set-local 'outline-regexp org-outline-regexp)
  4586. (org-set-local 'outline-level 'org-outline-level)
  4587. (setq bidi-paragraph-direction 'left-to-right)
  4588. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4589. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4590. (when (and org-ellipsis
  4591. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4592. (fboundp 'make-glyph-code))
  4593. (unless org-display-table
  4594. (setq org-display-table (make-display-table)))
  4595. (set-display-table-slot
  4596. org-display-table 4
  4597. (vconcat (mapcar
  4598. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4599. org-ellipsis)))
  4600. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4601. (setq buffer-display-table org-display-table))
  4602. (org-set-regexps-and-options)
  4603. (when (and org-tag-faces (not org-tags-special-faces-re))
  4604. ;; tag faces set outside customize.... force initialization.
  4605. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4606. ;; Calc embedded
  4607. (org-set-local 'calc-embedded-open-mode "# ")
  4608. ;; Modify a few syntax entries
  4609. (modify-syntax-entry ?@ "w")
  4610. (modify-syntax-entry ?\" "\"")
  4611. (if org-startup-truncated (setq truncate-lines t))
  4612. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4613. (org-set-local 'font-lock-unfontify-region-function
  4614. 'org-unfontify-region)
  4615. ;; Activate before-change-function
  4616. (org-set-local 'org-table-may-need-update t)
  4617. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4618. 'local)
  4619. ;; Check for running clock before killing a buffer
  4620. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4621. ;; Initialize macros templates.
  4622. (org-macro-initialize-templates)
  4623. ;; Initialize radio targets.
  4624. (org-update-radio-target-regexp)
  4625. ;; Indentation.
  4626. (org-set-local 'indent-line-function 'org-indent-line)
  4627. (org-set-local 'indent-region-function 'org-indent-region)
  4628. ;; Filling and auto-filling.
  4629. (org-setup-filling)
  4630. ;; Comments.
  4631. (org-setup-comments-handling)
  4632. ;; Beginning/end of defun
  4633. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4634. (org-set-local 'end-of-defun-function 'org-forward-element)
  4635. ;; Next error for sparse trees
  4636. (org-set-local 'next-error-function 'org-occur-next-match)
  4637. ;; Make sure dependence stuff works reliably, even for users who set it
  4638. ;; too late :-(
  4639. (if org-enforce-todo-dependencies
  4640. (add-hook 'org-blocker-hook
  4641. 'org-block-todo-from-children-or-siblings-or-parent)
  4642. (remove-hook 'org-blocker-hook
  4643. 'org-block-todo-from-children-or-siblings-or-parent))
  4644. (if org-enforce-todo-checkbox-dependencies
  4645. (add-hook 'org-blocker-hook
  4646. 'org-block-todo-from-checkboxes)
  4647. (remove-hook 'org-blocker-hook
  4648. 'org-block-todo-from-checkboxes))
  4649. ;; Align options lines
  4650. (org-set-local
  4651. 'align-mode-rules-list
  4652. '((org-in-buffer-settings
  4653. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4654. (modes . '(org-mode)))))
  4655. ;; Imenu
  4656. (org-set-local 'imenu-create-index-function
  4657. 'org-imenu-get-tree)
  4658. ;; Make isearch reveal context
  4659. (if (or (featurep 'xemacs)
  4660. (not (boundp 'outline-isearch-open-invisible-function)))
  4661. ;; Emacs 21 and XEmacs make use of the hook
  4662. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4663. ;; Emacs 22 deals with this through a special variable
  4664. (org-set-local 'outline-isearch-open-invisible-function
  4665. (lambda (&rest ignore) (org-show-context 'isearch)))
  4666. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4667. ;; Setup the pcomplete hooks
  4668. (set (make-local-variable 'pcomplete-command-completion-function)
  4669. 'org-pcomplete-initial)
  4670. (set (make-local-variable 'pcomplete-command-name-function)
  4671. 'org-command-at-point)
  4672. (set (make-local-variable 'pcomplete-default-completion-function)
  4673. 'ignore)
  4674. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4675. 'org-parse-arguments)
  4676. (set (make-local-variable 'pcomplete-termination-string) "")
  4677. (when (>= emacs-major-version 23)
  4678. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4679. ;; If empty file that did not turn on org-mode automatically, make it to.
  4680. (if (and org-insert-mode-line-in-empty-file
  4681. (org-called-interactively-p 'any)
  4682. (= (point-min) (point-max)))
  4683. (insert "# -*- mode: org -*-\n\n"))
  4684. (unless org-inhibit-startup
  4685. (org-unmodified
  4686. (and org-startup-with-beamer-mode (org-beamer-mode))
  4687. (when org-startup-align-all-tables
  4688. (org-table-map-tables 'org-table-align 'quietly))
  4689. (when org-startup-with-inline-images
  4690. (org-display-inline-images))
  4691. (when org-startup-with-latex-preview
  4692. (org-preview-latex-fragment))
  4693. (unless org-inhibit-startup-visibility-stuff
  4694. (org-set-startup-visibility))))
  4695. ;; Try to set org-hide correctly
  4696. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4697. ;; Update `customize-package-emacs-version-alist'
  4698. (add-to-list 'customize-package-emacs-version-alist
  4699. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4700. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4701. ("8.0" . "24.4")))
  4702. (defvar org-mode-transpose-word-syntax-table
  4703. (let ((st (make-syntax-table)))
  4704. (mapc (lambda(c) (modify-syntax-entry
  4705. (string-to-char (car c)) "w p" st))
  4706. org-emphasis-alist)
  4707. st))
  4708. (when (fboundp 'abbrev-table-put)
  4709. (abbrev-table-put org-mode-abbrev-table
  4710. :parents (list text-mode-abbrev-table)))
  4711. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4712. (defsubst org-fix-ellipsis-at-bol ()
  4713. (save-excursion (goto-char (window-start)) (recenter 0)))
  4714. (defun org-find-invisible-foreground ()
  4715. (let ((candidates (remove
  4716. "unspecified-bg"
  4717. (nconc
  4718. (list (face-background 'default)
  4719. (face-background 'org-default))
  4720. (mapcar
  4721. (lambda (alist)
  4722. (when (boundp alist)
  4723. (cdr (assoc 'background-color (symbol-value alist)))))
  4724. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4725. (list (face-foreground 'org-hide))))))
  4726. (car (remove nil candidates))))
  4727. (defun org-current-time (&optional rounding-minutes past)
  4728. "Current time, possibly rounded to ROUNDING-MINUTES.
  4729. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4730. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4731. the rounding returns a past time."
  4732. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4733. (car org-time-stamp-rounding-minutes)))
  4734. (time (decode-time)) res)
  4735. (if (< r 1)
  4736. (current-time)
  4737. (setq res
  4738. (apply 'encode-time
  4739. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4740. (nthcdr 2 time))))
  4741. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4742. (seconds-to-time (- (org-float-time res) (* r 60)))
  4743. res))))
  4744. (defun org-today ()
  4745. "Return today date, considering `org-extend-today-until'."
  4746. (time-to-days
  4747. (time-subtract (current-time)
  4748. (list 0 (* 3600 org-extend-today-until) 0))))
  4749. ;;;; Font-Lock stuff, including the activators
  4750. (defvar org-mouse-map (make-sparse-keymap))
  4751. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4752. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4753. (when org-mouse-1-follows-link
  4754. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4755. (when org-tab-follows-link
  4756. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4757. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4758. (require 'font-lock)
  4759. (defconst org-non-link-chars "]\t\n\r<>")
  4760. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4761. "shell" "elisp" "doi" "message"))
  4762. (defvar org-link-types-re nil
  4763. "Matches a link that has a url-like prefix like \"http:\"")
  4764. (defvar org-link-re-with-space nil
  4765. "Matches a link with spaces, optional angular brackets around it.")
  4766. (defvar org-link-re-with-space2 nil
  4767. "Matches a link with spaces, optional angular brackets around it.")
  4768. (defvar org-link-re-with-space3 nil
  4769. "Matches a link with spaces, only for internal part in bracket links.")
  4770. (defvar org-angle-link-re nil
  4771. "Matches link with angular brackets, spaces are allowed.")
  4772. (defvar org-plain-link-re nil
  4773. "Matches plain link, without spaces.")
  4774. (defvar org-bracket-link-regexp nil
  4775. "Matches a link in double brackets.")
  4776. (defvar org-bracket-link-analytic-regexp nil
  4777. "Regular expression used to analyze links.
  4778. Here is what the match groups contain after a match:
  4779. 1: http:
  4780. 2: http
  4781. 3: path
  4782. 4: [desc]
  4783. 5: desc")
  4784. (defvar org-bracket-link-analytic-regexp++ nil
  4785. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4786. (defvar org-any-link-re nil
  4787. "Regular expression matching any link.")
  4788. (defconst org-match-sexp-depth 3
  4789. "Number of stacked braces for sub/superscript matching.")
  4790. (defun org-create-multibrace-regexp (left right n)
  4791. "Create a regular expression which will match a balanced sexp.
  4792. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4793. as single character strings.
  4794. The regexp returned will match the entire expression including the
  4795. delimiters. It will also define a single group which contains the
  4796. match except for the outermost delimiters. The maximum depth of
  4797. stacked delimiters is N. Escaping delimiters is not possible."
  4798. (let* ((nothing (concat "[^" left right "]*?"))
  4799. (or "\\|")
  4800. (re nothing)
  4801. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4802. (while (> n 1)
  4803. (setq n (1- n)
  4804. re (concat re or next)
  4805. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4806. (concat left "\\(" re "\\)" right)))
  4807. (defvar org-match-substring-regexp
  4808. (concat
  4809. "\\(\\S-\\)\\([_^]\\)\\("
  4810. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4811. "\\|"
  4812. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4813. "\\|"
  4814. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4815. "The regular expression matching a sub- or superscript.")
  4816. (defvar org-match-substring-with-braces-regexp
  4817. (concat
  4818. "\\(\\S-\\)\\([_^]\\)\\("
  4819. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4820. "\\)")
  4821. "The regular expression matching a sub- or superscript, forcing braces.")
  4822. (defun org-make-link-regexps ()
  4823. "Update the link regular expressions.
  4824. This should be called after the variable `org-link-types' has changed."
  4825. (setq org-link-types-re
  4826. (concat
  4827. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4828. org-link-re-with-space
  4829. (concat
  4830. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4831. "\\([^" org-non-link-chars " ]"
  4832. "[^" org-non-link-chars "]*"
  4833. "[^" org-non-link-chars " ]\\)>?")
  4834. org-link-re-with-space2
  4835. (concat
  4836. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4837. "\\([^" org-non-link-chars " ]"
  4838. "[^\t\n\r]*"
  4839. "[^" org-non-link-chars " ]\\)>?")
  4840. org-link-re-with-space3
  4841. (concat
  4842. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4843. "\\([^" org-non-link-chars " ]"
  4844. "[^\t\n\r]*\\)")
  4845. org-angle-link-re
  4846. (concat
  4847. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4848. "\\([^" org-non-link-chars " ]"
  4849. "[^" org-non-link-chars "]*"
  4850. "\\)>")
  4851. org-plain-link-re
  4852. (concat
  4853. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4854. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4855. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4856. org-bracket-link-regexp
  4857. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4858. org-bracket-link-analytic-regexp
  4859. (concat
  4860. "\\[\\["
  4861. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4862. "\\([^]]+\\)"
  4863. "\\]"
  4864. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4865. "\\]")
  4866. org-bracket-link-analytic-regexp++
  4867. (concat
  4868. "\\[\\["
  4869. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4870. "\\([^]]+\\)"
  4871. "\\]"
  4872. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4873. "\\]")
  4874. org-any-link-re
  4875. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4876. org-angle-link-re "\\)\\|\\("
  4877. org-plain-link-re "\\)")))
  4878. (org-make-link-regexps)
  4879. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4880. "Regular expression for fast time stamp matching.")
  4881. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4882. "Regular expression for fast time stamp matching.")
  4883. (defconst org-ts-regexp0
  4884. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4885. "Regular expression matching time strings for analysis.
  4886. This one does not require the space after the date, so it can be used
  4887. on a string that terminates immediately after the date.")
  4888. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4889. "Regular expression matching time strings for analysis.")
  4890. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4891. "Regular expression matching time stamps, with groups.")
  4892. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4893. "Regular expression matching time stamps (also [..]), with groups.")
  4894. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4895. "Regular expression matching a time stamp range.")
  4896. (defconst org-tr-regexp-both
  4897. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4898. "Regular expression matching a time stamp range.")
  4899. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4900. org-ts-regexp "\\)?")
  4901. "Regular expression matching a time stamp or time stamp range.")
  4902. (defconst org-tsr-regexp-both
  4903. (concat org-ts-regexp-both "\\(--?-?"
  4904. org-ts-regexp-both "\\)?")
  4905. "Regular expression matching a time stamp or time stamp range.
  4906. The time stamps may be either active or inactive.")
  4907. (defvar org-emph-face nil)
  4908. (defun org-do-emphasis-faces (limit)
  4909. "Run through the buffer and add overlays to emphasized strings."
  4910. (let (rtn a)
  4911. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4912. (if (not (= (char-after (match-beginning 3))
  4913. (char-after (match-beginning 4))))
  4914. (progn
  4915. (setq rtn t)
  4916. (setq a (assoc (match-string 3) org-emphasis-alist))
  4917. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4918. 'face
  4919. (nth 1 a))
  4920. (and (nth 4 a)
  4921. (org-remove-flyspell-overlays-in
  4922. (match-beginning 0) (match-end 0)))
  4923. (add-text-properties (match-beginning 2) (match-end 2)
  4924. '(font-lock-multiline t org-emphasis t))
  4925. (when org-hide-emphasis-markers
  4926. (add-text-properties (match-end 4) (match-beginning 5)
  4927. '(invisible org-link))
  4928. (add-text-properties (match-beginning 3) (match-end 3)
  4929. '(invisible org-link)))))
  4930. (backward-char 1))
  4931. rtn))
  4932. (defun org-emphasize (&optional char)
  4933. "Insert or change an emphasis, i.e. a font like bold or italic.
  4934. If there is an active region, change that region to a new emphasis.
  4935. If there is no region, just insert the marker characters and position
  4936. the cursor between them.
  4937. CHAR should be either the marker character, or the first character of the
  4938. HTML tag associated with that emphasis. If CHAR is a space, the means
  4939. to remove the emphasis of the selected region.
  4940. If char is not given (for example in an interactive call) it
  4941. will be prompted for."
  4942. (interactive)
  4943. (let ((eal org-emphasis-alist) e det
  4944. (erc org-emphasis-regexp-components)
  4945. (prompt "")
  4946. (string "") beg end move tag c s)
  4947. (if (org-region-active-p)
  4948. (setq beg (region-beginning) end (region-end)
  4949. string (buffer-substring beg end))
  4950. (setq move t))
  4951. (while (setq e (pop eal))
  4952. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4953. c (aref tag 0))
  4954. (push (cons c (string-to-char (car e))) det)
  4955. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4956. (substring tag 1)))))
  4957. (setq det (nreverse det))
  4958. (unless char
  4959. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4960. (setq char (read-char-exclusive)))
  4961. (setq char (or (cdr (assoc char det)) char))
  4962. (if (equal char ?\ )
  4963. (setq s "" move nil)
  4964. (unless (assoc (char-to-string char) org-emphasis-alist)
  4965. (error "No such emphasis marker: \"%c\"" char))
  4966. (setq s (char-to-string char)))
  4967. (while (and (> (length string) 1)
  4968. (equal (substring string 0 1) (substring string -1))
  4969. (assoc (substring string 0 1) org-emphasis-alist))
  4970. (setq string (substring string 1 -1)))
  4971. (setq string (concat s string s))
  4972. (if beg (delete-region beg end))
  4973. (unless (or (bolp)
  4974. (string-match (concat "[" (nth 0 erc) "\n]")
  4975. (char-to-string (char-before (point)))))
  4976. (insert " "))
  4977. (unless (or (eobp)
  4978. (string-match (concat "[" (nth 1 erc) "\n]")
  4979. (char-to-string (char-after (point)))))
  4980. (insert " ") (backward-char 1))
  4981. (insert string)
  4982. (and move (backward-char 1))))
  4983. (defconst org-nonsticky-props
  4984. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4985. (defsubst org-rear-nonsticky-at (pos)
  4986. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4987. (defun org-activate-plain-links (limit)
  4988. "Run through the buffer and add overlays to links."
  4989. (let (f hl)
  4990. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4991. (not (org-in-src-block-p)))
  4992. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4993. (setq f (get-text-property (match-beginning 0) 'face))
  4994. (setq hl (org-match-string-no-properties 0))
  4995. (if (or (eq f 'org-tag)
  4996. (and (listp f) (memq 'org-tag f)))
  4997. nil
  4998. (add-text-properties (match-beginning 0) (match-end 0)
  4999. (list 'mouse-face 'highlight
  5000. 'face 'org-link
  5001. 'htmlize-link `(:uri ,hl)
  5002. 'keymap org-mouse-map))
  5003. (org-rear-nonsticky-at (match-end 0)))
  5004. t)))
  5005. (defun org-activate-code (limit)
  5006. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5007. (progn
  5008. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5009. (remove-text-properties (match-beginning 0) (match-end 0)
  5010. '(display t invisible t intangible t))
  5011. t)))
  5012. (defcustom org-src-fontify-natively nil
  5013. "When non-nil, fontify code in code blocks."
  5014. :type 'boolean
  5015. :version "24.1"
  5016. :group 'org-appearance
  5017. :group 'org-babel)
  5018. (defcustom org-allow-promoting-top-level-subtree nil
  5019. "When non-nil, allow promoting a top level subtree.
  5020. The leading star of the top level headline will be replaced
  5021. by a #."
  5022. :type 'boolean
  5023. :version "24.1"
  5024. :group 'org-appearance)
  5025. (defun org-fontify-meta-lines-and-blocks (limit)
  5026. (condition-case nil
  5027. (org-fontify-meta-lines-and-blocks-1 limit)
  5028. (error (message "org-mode fontification error"))))
  5029. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5030. "Fontify #+ lines and blocks, in the correct ways."
  5031. (let ((case-fold-search t))
  5032. (if (re-search-forward
  5033. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5034. limit t)
  5035. (let ((beg (match-beginning 0))
  5036. (block-start (match-end 0))
  5037. (block-end nil)
  5038. (lang (match-string 7))
  5039. (beg1 (line-beginning-position 2))
  5040. (dc1 (downcase (match-string 2)))
  5041. (dc3 (downcase (match-string 3)))
  5042. end end1 quoting block-type ovl)
  5043. (cond
  5044. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5045. ;; a single line of backend-specific content
  5046. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5047. (remove-text-properties (match-beginning 0) (match-end 0)
  5048. '(display t invisible t intangible t))
  5049. (add-text-properties (match-beginning 1) (match-end 3)
  5050. '(font-lock-fontified t face org-meta-line))
  5051. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5052. '(font-lock-fontified t face org-block))
  5053. ; for backend-specific code
  5054. t)
  5055. ((and (match-end 4) (equal dc3 "+begin"))
  5056. ;; Truly a block
  5057. (setq block-type (downcase (match-string 5))
  5058. quoting (member block-type org-protecting-blocks))
  5059. (when (re-search-forward
  5060. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5061. nil t) ;; on purpose, we look further than LIMIT
  5062. (setq end (min (point-max) (match-end 0))
  5063. end1 (min (point-max) (1- (match-beginning 0))))
  5064. (setq block-end (match-beginning 0))
  5065. (when quoting
  5066. (remove-text-properties beg end
  5067. '(display t invisible t intangible t)))
  5068. (add-text-properties
  5069. beg end
  5070. '(font-lock-fontified t font-lock-multiline t))
  5071. (add-text-properties beg beg1 '(face org-meta-line))
  5072. (add-text-properties end1 (min (point-max) (1+ end))
  5073. '(face org-meta-line)) ; for end_src
  5074. (cond
  5075. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5076. (org-src-font-lock-fontify-block lang block-start block-end)
  5077. ;; remove old background overlays
  5078. (mapc (lambda (ov)
  5079. (if (eq (overlay-get ov 'face) 'org-block-background)
  5080. (delete-overlay ov)))
  5081. (overlays-at (/ (+ beg1 block-end) 2)))
  5082. ;; add a background overlay
  5083. (setq ovl (make-overlay beg1 block-end))
  5084. (overlay-put ovl 'face 'org-block-background)
  5085. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5086. (quoting
  5087. (add-text-properties beg1 (min (point-max) (1+ end1))
  5088. '(face org-block))) ; end of source block
  5089. ((not org-fontify-quote-and-verse-blocks))
  5090. ((string= block-type "quote")
  5091. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5092. ((string= block-type "verse")
  5093. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5094. (add-text-properties beg beg1 '(face org-block-begin-line))
  5095. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5096. '(face org-block-end-line))
  5097. t))
  5098. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5099. (add-text-properties
  5100. beg (match-end 3)
  5101. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5102. '(font-lock-fontified t invisible t)
  5103. '(font-lock-fontified t face org-document-info-keyword)))
  5104. (add-text-properties
  5105. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5106. (if (string-equal dc1 "+title:")
  5107. '(font-lock-fontified t face org-document-title)
  5108. '(font-lock-fontified t face org-document-info))))
  5109. ((or (equal dc1 "+results")
  5110. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5111. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5112. "+call:" "+header:" "+headers:" "+name:"))
  5113. (and (match-end 4) (equal dc3 "+attr")))
  5114. (add-text-properties
  5115. beg (match-end 0)
  5116. '(font-lock-fontified t face org-meta-line))
  5117. t)
  5118. ((member dc3 '(" " ""))
  5119. (add-text-properties
  5120. beg (match-end 0)
  5121. '(font-lock-fontified t face font-lock-comment-face)))
  5122. ((not (member (char-after beg) '(?\ ?\t)))
  5123. ;; just any other in-buffer setting, but not indented
  5124. (add-text-properties
  5125. beg (match-end 0)
  5126. '(font-lock-fontified t face org-meta-line))
  5127. t)
  5128. (t nil))))))
  5129. (defun org-activate-angle-links (limit)
  5130. "Run through the buffer and add overlays to links."
  5131. (if (and (re-search-forward org-angle-link-re limit t)
  5132. (not (org-in-src-block-p)))
  5133. (progn
  5134. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5135. (add-text-properties (match-beginning 0) (match-end 0)
  5136. (list 'mouse-face 'highlight
  5137. 'keymap org-mouse-map))
  5138. (org-rear-nonsticky-at (match-end 0))
  5139. t)))
  5140. (defun org-activate-footnote-links (limit)
  5141. "Run through the buffer and add overlays to footnotes."
  5142. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5143. (when fn
  5144. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5145. (org-remove-flyspell-overlays-in beg end)
  5146. (add-text-properties beg end
  5147. (list 'mouse-face 'highlight
  5148. 'keymap org-mouse-map
  5149. 'help-echo
  5150. (if (= (point-at-bol) beg)
  5151. "Footnote definition"
  5152. "Footnote reference")
  5153. 'font-lock-fontified t
  5154. 'font-lock-multiline t
  5155. 'face 'org-footnote))))))
  5156. (defun org-activate-bracket-links (limit)
  5157. "Run through the buffer and add overlays to bracketed links."
  5158. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5159. (not (org-in-src-block-p)))
  5160. (let* ((hl (org-match-string-no-properties 1))
  5161. (help (concat "LINK: " hl))
  5162. ;; FIXME: Above we should remove the escapes. But that
  5163. ;; requires another match, protecting match data, a lot
  5164. ;; of overhead for font-lock.
  5165. (ip (org-maybe-intangible
  5166. (list 'invisible 'org-link
  5167. 'keymap org-mouse-map 'mouse-face 'highlight
  5168. 'font-lock-multiline t 'help-echo help
  5169. 'htmlize-link `(:uri ,hl))))
  5170. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5171. 'font-lock-multiline t 'help-echo help
  5172. 'htmlize-link `(:uri ,hl))))
  5173. ;; We need to remove the invisible property here. Table narrowing
  5174. ;; may have made some of this invisible.
  5175. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5176. (remove-text-properties (match-beginning 0) (match-end 0)
  5177. '(invisible nil))
  5178. (if (match-end 3)
  5179. (progn
  5180. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5181. (org-rear-nonsticky-at (match-beginning 3))
  5182. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5183. (org-rear-nonsticky-at (match-end 3))
  5184. (add-text-properties (match-end 3) (match-end 0) ip)
  5185. (org-rear-nonsticky-at (match-end 0)))
  5186. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5187. (org-rear-nonsticky-at (match-beginning 1))
  5188. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5189. (org-rear-nonsticky-at (match-end 1))
  5190. (add-text-properties (match-end 1) (match-end 0) ip)
  5191. (org-rear-nonsticky-at (match-end 0)))
  5192. t)))
  5193. (defun org-activate-dates (limit)
  5194. "Run through the buffer and add overlays to dates."
  5195. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5196. (not (equal (char-before (match-beginning 0)) 91)))
  5197. (progn
  5198. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5199. (add-text-properties (match-beginning 0) (match-end 0)
  5200. (list 'mouse-face 'highlight
  5201. 'keymap org-mouse-map))
  5202. (org-rear-nonsticky-at (match-end 0))
  5203. (when org-display-custom-times
  5204. (if (match-end 3)
  5205. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5206. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5207. t)))
  5208. (defvar org-target-link-regexp nil
  5209. "Regular expression matching radio targets in plain text.")
  5210. (make-variable-buffer-local 'org-target-link-regexp)
  5211. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5212. "Regular expression matching a link target.")
  5213. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5214. "Regular expression matching a radio target.")
  5215. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5216. "Regular expression matching any target.")
  5217. (defun org-activate-target-links (limit)
  5218. "Run through the buffer and add overlays to target matches."
  5219. (when org-target-link-regexp
  5220. (let ((case-fold-search t))
  5221. (if (re-search-forward org-target-link-regexp limit t)
  5222. (progn
  5223. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5224. (add-text-properties (match-beginning 0) (match-end 0)
  5225. (list 'mouse-face 'highlight
  5226. 'keymap org-mouse-map
  5227. 'help-echo "Radio target link"
  5228. 'org-linked-text t))
  5229. (org-rear-nonsticky-at (match-end 0))
  5230. t)))))
  5231. (defun org-update-radio-target-regexp ()
  5232. "Find all radio targets in this file and update the regular expression."
  5233. (interactive)
  5234. (when (memq 'radio org-activate-links)
  5235. (setq org-target-link-regexp
  5236. (org-make-target-link-regexp (org-all-targets 'radio)))
  5237. (org-restart-font-lock)))
  5238. (defun org-hide-wide-columns (limit)
  5239. (let (s e)
  5240. (setq s (text-property-any (point) (or limit (point-max))
  5241. 'org-cwidth t))
  5242. (when s
  5243. (setq e (next-single-property-change s 'org-cwidth))
  5244. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5245. (goto-char e)
  5246. t)))
  5247. (defvar org-latex-and-related-regexp nil
  5248. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5249. (defvar org-match-substring-regexp)
  5250. (defvar org-match-substring-with-braces-regexp)
  5251. (defun org-compute-latex-and-related-regexp ()
  5252. "Compute regular expression for LaTeX, entities and sub/superscript.
  5253. Result depends on variable `org-highlight-latex-and-related'."
  5254. (org-set-local
  5255. 'org-latex-and-related-regexp
  5256. (let* ((re-sub
  5257. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5258. ((eq org-use-sub-superscripts '{})
  5259. (list org-match-substring-with-braces-regexp))
  5260. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5261. (re-latex
  5262. (when (memq 'latex org-highlight-latex-and-related)
  5263. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5264. (delq nil
  5265. (mapcar (lambda (x)
  5266. (and (member (car x) matchers) (nth 1 x)))
  5267. org-latex-regexps)))))
  5268. (re-entities
  5269. (when (memq 'entities org-highlight-latex-and-related)
  5270. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5271. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5272. (defun org-do-latex-and-related (limit)
  5273. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5274. LIMIT bounds the search for syntax to highlight. Stop at first
  5275. highlighted object, if any. Return t if some highlighting was
  5276. done, nil otherwise."
  5277. (when (org-string-nw-p org-latex-and-related-regexp)
  5278. (catch 'found
  5279. (while (re-search-forward org-latex-and-related-regexp limit t)
  5280. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5281. 'face))
  5282. '(org-code org-verbatim underline))
  5283. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5284. '(?_ ?^))
  5285. 1
  5286. 0)))
  5287. (font-lock-prepend-text-property
  5288. (+ offset (match-beginning 0)) (match-end 0)
  5289. 'face 'org-latex-and-related)
  5290. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5291. '(font-lock-multiline t)))
  5292. (throw 'found t)))
  5293. nil)))
  5294. (defun org-restart-font-lock ()
  5295. "Restart `font-lock-mode', to force refontification."
  5296. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5297. (font-lock-mode -1)
  5298. (font-lock-mode 1)))
  5299. (defun org-all-targets (&optional radio)
  5300. "Return a list of all targets in this file.
  5301. When optional argument RADIO is non-nil, only find radio
  5302. targets."
  5303. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5304. (save-excursion
  5305. (goto-char (point-min))
  5306. (while (re-search-forward re nil t)
  5307. ;; Make sure point is really within the object.
  5308. (backward-char)
  5309. (let ((obj (org-element-context)))
  5310. (when (memq (org-element-type obj) '(radio-target target))
  5311. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5312. rtn)))
  5313. (defun org-make-target-link-regexp (targets)
  5314. "Make regular expression matching all strings in TARGETS.
  5315. The regular expression finds the targets also if there is a line break
  5316. between words."
  5317. (and targets
  5318. (concat
  5319. "\\<\\("
  5320. (mapconcat
  5321. (lambda (x)
  5322. (setq x (regexp-quote x))
  5323. (while (string-match " +" x)
  5324. (setq x (replace-match "\\s-+" t t x)))
  5325. x)
  5326. targets
  5327. "\\|")
  5328. "\\)\\>")))
  5329. (defun org-activate-tags (limit)
  5330. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5331. (progn
  5332. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5333. (add-text-properties (match-beginning 1) (match-end 1)
  5334. (list 'mouse-face 'highlight
  5335. 'keymap org-mouse-map))
  5336. (org-rear-nonsticky-at (match-end 1))
  5337. t)))
  5338. (defun org-outline-level ()
  5339. "Compute the outline level of the heading at point.
  5340. If this is called at a normal headline, the level is the number of stars.
  5341. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5342. (save-excursion
  5343. (if (not (condition-case nil
  5344. (org-back-to-heading t)
  5345. (error nil)))
  5346. 0
  5347. (looking-at org-outline-regexp)
  5348. (1- (- (match-end 0) (match-beginning 0))))))
  5349. (defvar org-font-lock-keywords nil)
  5350. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5351. "Regular expression matching a property line.")
  5352. (defvar org-font-lock-hook nil
  5353. "Functions to be called for special font lock stuff.")
  5354. (defvar org-font-lock-set-keywords-hook nil
  5355. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5356. This is called after `org-font-lock-extra-keywords' is defined, but before
  5357. it is installed to be used by font lock. This can be useful if something
  5358. needs to be inserted at a specific position in the font-lock sequence.")
  5359. (defun org-font-lock-hook (limit)
  5360. "Run `org-font-lock-hook' within LIMIT."
  5361. (run-hook-with-args 'org-font-lock-hook limit))
  5362. (defun org-set-font-lock-defaults ()
  5363. "Set font lock defaults for the current buffer."
  5364. (let* ((em org-fontify-emphasized-text)
  5365. (lk org-activate-links)
  5366. (org-font-lock-extra-keywords
  5367. (list
  5368. ;; Call the hook
  5369. '(org-font-lock-hook)
  5370. ;; Headlines
  5371. `(,(if org-fontify-whole-heading-line
  5372. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5373. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5374. (1 (org-get-level-face 1))
  5375. (2 (org-get-level-face 2))
  5376. (3 (org-get-level-face 3)))
  5377. ;; Table lines
  5378. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5379. (1 'org-table t))
  5380. ;; Table internals
  5381. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5382. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5383. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5384. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5385. ;; Drawers
  5386. (list org-drawer-regexp '(0 'org-special-keyword t))
  5387. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5388. ;; Properties
  5389. (list org-property-re
  5390. '(1 'org-special-keyword t)
  5391. '(3 'org-property-value t))
  5392. ;; Links
  5393. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5394. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5395. (if (memq 'plain lk) '(org-activate-plain-links))
  5396. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5397. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5398. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5399. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5400. ;; Targets.
  5401. (list org-any-target-regexp '(0 'org-target t))
  5402. ;; Diary sexps.
  5403. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5404. ;; Macro
  5405. '("{{{.+}}}" (0 'org-macro t))
  5406. '(org-hide-wide-columns (0 nil append))
  5407. ;; TODO keyword
  5408. (list (format org-heading-keyword-regexp-format
  5409. org-todo-regexp)
  5410. '(2 (org-get-todo-face 2) t))
  5411. ;; DONE
  5412. (if org-fontify-done-headline
  5413. (list (format org-heading-keyword-regexp-format
  5414. (concat
  5415. "\\(?:"
  5416. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5417. "\\)"))
  5418. '(2 'org-headline-done t))
  5419. nil)
  5420. ;; Priorities
  5421. '(org-font-lock-add-priority-faces)
  5422. ;; Tags
  5423. '(org-font-lock-add-tag-faces)
  5424. ;; Special keywords
  5425. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5426. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5427. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5428. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5429. ;; Emphasis
  5430. (if em
  5431. (if (featurep 'xemacs)
  5432. '(org-do-emphasis-faces (0 nil append))
  5433. '(org-do-emphasis-faces)))
  5434. ;; Checkboxes
  5435. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5436. 1 'org-checkbox prepend)
  5437. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5438. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5439. (0 (org-get-checkbox-statistics-face) t)))
  5440. ;; Description list items
  5441. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5442. 1 'org-list-dt prepend)
  5443. ;; ARCHIVEd headings
  5444. (list (concat
  5445. org-outline-regexp-bol
  5446. "\\(.*:" org-archive-tag ":.*\\)")
  5447. '(1 'org-archived prepend))
  5448. ;; Specials
  5449. '(org-do-latex-and-related)
  5450. '(org-fontify-entities)
  5451. '(org-raise-scripts)
  5452. ;; Code
  5453. '(org-activate-code (1 'org-code t))
  5454. ;; COMMENT
  5455. (list (format org-heading-keyword-regexp-format
  5456. (concat "\\("
  5457. org-comment-string "\\|" org-quote-string
  5458. "\\)"))
  5459. '(2 'org-special-keyword t))
  5460. ;; Blocks and meta lines
  5461. '(org-fontify-meta-lines-and-blocks)
  5462. )))
  5463. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5464. (run-hooks 'org-font-lock-set-keywords-hook)
  5465. ;; Now set the full font-lock-keywords
  5466. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5467. (org-set-local 'font-lock-defaults
  5468. '(org-font-lock-keywords t nil nil backward-paragraph))
  5469. (kill-local-variable 'font-lock-keywords) nil))
  5470. (defun org-toggle-pretty-entities ()
  5471. "Toggle the composition display of entities as UTF8 characters."
  5472. (interactive)
  5473. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5474. (org-restart-font-lock)
  5475. (if org-pretty-entities
  5476. (message "Entities are displayed as UTF8 characters")
  5477. (save-restriction
  5478. (widen)
  5479. (org-decompose-region (point-min) (point-max))
  5480. (message "Entities are displayed plain"))))
  5481. (defvar org-custom-properties-overlays nil
  5482. "List of overlays used for custom properties.")
  5483. (make-variable-buffer-local 'org-custom-properties-overlays)
  5484. (defun org-toggle-custom-properties-visibility ()
  5485. "Display or hide properties in `org-custom-properties'."
  5486. (interactive)
  5487. (if org-custom-properties-overlays
  5488. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5489. (setq org-custom-properties-overlays nil))
  5490. (unless (not org-custom-properties)
  5491. (save-excursion
  5492. (save-restriction
  5493. (widen)
  5494. (goto-char (point-min))
  5495. (while (re-search-forward org-property-re nil t)
  5496. (mapc (lambda(p)
  5497. (when (equal p (substring (match-string 1) 1 -1))
  5498. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5499. (overlay-put o 'invisible t)
  5500. (overlay-put o 'org-custom-property t)
  5501. (push o org-custom-properties-overlays))))
  5502. org-custom-properties)))))))
  5503. (defun org-fontify-entities (limit)
  5504. "Find an entity to fontify."
  5505. (let (ee)
  5506. (when org-pretty-entities
  5507. (catch 'match
  5508. (while (re-search-forward
  5509. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5510. limit t)
  5511. (if (and (not (org-in-indented-comment-line))
  5512. (setq ee (org-entity-get (match-string 1)))
  5513. (= (length (nth 6 ee)) 1))
  5514. (let*
  5515. ((end (if (equal (match-string 2) "{}")
  5516. (match-end 2)
  5517. (match-end 1))))
  5518. (add-text-properties
  5519. (match-beginning 0) end
  5520. (list 'font-lock-fontified t))
  5521. (compose-region (match-beginning 0) end
  5522. (nth 6 ee) nil)
  5523. (backward-char 1)
  5524. (throw 'match t))))
  5525. nil))))
  5526. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5527. "Fontify string S like in Org-mode."
  5528. (with-temp-buffer
  5529. (insert s)
  5530. (let ((org-odd-levels-only odd-levels))
  5531. (org-mode)
  5532. (font-lock-fontify-buffer)
  5533. (buffer-string))))
  5534. (defvar org-m nil)
  5535. (defvar org-l nil)
  5536. (defvar org-f nil)
  5537. (defun org-get-level-face (n)
  5538. "Get the right face for match N in font-lock matching of headlines."
  5539. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5540. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5541. (if org-cycle-level-faces
  5542. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5543. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5544. (cond
  5545. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5546. ((eq n 2) org-f)
  5547. (t (if org-level-color-stars-only nil org-f))))
  5548. (defun org-get-todo-face (kwd)
  5549. "Get the right face for a TODO keyword KWD.
  5550. If KWD is a number, get the corresponding match group."
  5551. (if (numberp kwd) (setq kwd (match-string kwd)))
  5552. (or (org-face-from-face-or-color
  5553. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5554. (and (member kwd org-done-keywords) 'org-done)
  5555. 'org-todo))
  5556. (defun org-face-from-face-or-color (context inherit face-or-color)
  5557. "Create a face list that inherits INHERIT, but sets the foreground color.
  5558. When FACE-OR-COLOR is not a string, just return it."
  5559. (if (stringp face-or-color)
  5560. (list :inherit inherit
  5561. (cdr (assoc context org-faces-easy-properties))
  5562. face-or-color)
  5563. face-or-color))
  5564. (defun org-font-lock-add-tag-faces (limit)
  5565. "Add the special tag faces."
  5566. (when (and org-tag-faces org-tags-special-faces-re)
  5567. (while (re-search-forward org-tags-special-faces-re limit t)
  5568. (add-text-properties (match-beginning 1) (match-end 1)
  5569. (list 'face (org-get-tag-face 1)
  5570. 'font-lock-fontified t))
  5571. (backward-char 1))))
  5572. (defun org-font-lock-add-priority-faces (limit)
  5573. "Add the special priority faces."
  5574. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5575. (when (save-match-data (org-at-heading-p))
  5576. (add-text-properties
  5577. (match-beginning 0) (match-end 0)
  5578. (list 'face (or (org-face-from-face-or-color
  5579. 'priority 'org-priority
  5580. (cdr (assoc (char-after (match-beginning 1))
  5581. org-priority-faces)))
  5582. 'org-priority)
  5583. 'font-lock-fontified t)))))
  5584. (defun org-get-tag-face (kwd)
  5585. "Get the right face for a TODO keyword KWD.
  5586. If KWD is a number, get the corresponding match group."
  5587. (if (numberp kwd) (setq kwd (match-string kwd)))
  5588. (or (org-face-from-face-or-color
  5589. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5590. 'org-tag))
  5591. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5592. "Remove fontification and activation overlays from links."
  5593. (font-lock-default-unfontify-region beg end)
  5594. (let* ((buffer-undo-list t)
  5595. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5596. (inhibit-modification-hooks t)
  5597. deactivate-mark buffer-file-name buffer-file-truename)
  5598. (org-decompose-region beg end)
  5599. (remove-text-properties beg end
  5600. '(mouse-face t keymap t org-linked-text t
  5601. invisible t intangible t
  5602. org-no-flyspell t org-emphasis t))
  5603. (org-remove-font-lock-display-properties beg end)))
  5604. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5605. ((raise 0.3) (height 0.7))
  5606. ((raise -0.5))
  5607. ((raise 0.5)))
  5608. "Display properties for showing superscripts and subscripts.")
  5609. (defun org-remove-font-lock-display-properties (beg end)
  5610. "Remove specific display properties that have been added by font lock.
  5611. The will remove the raise properties that are used to show superscripts
  5612. and subscripts."
  5613. (let (next prop)
  5614. (while (< beg end)
  5615. (setq next (next-single-property-change beg 'display nil end)
  5616. prop (get-text-property beg 'display))
  5617. (if (member prop org-script-display)
  5618. (put-text-property beg next 'display nil))
  5619. (setq beg next))))
  5620. (defun org-raise-scripts (limit)
  5621. "Add raise properties to sub/superscripts."
  5622. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5623. (if (re-search-forward
  5624. (if (eq org-use-sub-superscripts t)
  5625. org-match-substring-regexp
  5626. org-match-substring-with-braces-regexp)
  5627. limit t)
  5628. (let* ((pos (point)) table-p comment-p
  5629. (mpos (match-beginning 3))
  5630. (emph-p (get-text-property mpos 'org-emphasis))
  5631. (link-p (get-text-property mpos 'mouse-face))
  5632. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5633. (goto-char (point-at-bol))
  5634. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5635. comment-p (org-looking-at-p "[ \t]*#"))
  5636. (goto-char pos)
  5637. ;; FIXME: Should we go back one character here, for a_b^c
  5638. ;; (goto-char (1- pos)) ;????????????????????
  5639. (if (or comment-p emph-p link-p keyw-p)
  5640. t
  5641. (put-text-property (match-beginning 3) (match-end 0)
  5642. 'display
  5643. (if (equal (char-after (match-beginning 2)) ?^)
  5644. (nth (if table-p 3 1) org-script-display)
  5645. (nth (if table-p 2 0) org-script-display)))
  5646. (add-text-properties (match-beginning 2) (match-end 2)
  5647. (list 'invisible t
  5648. 'org-dwidth t 'org-dwidth-n 1))
  5649. (if (and (eq (char-after (match-beginning 3)) ?{)
  5650. (eq (char-before (match-end 3)) ?}))
  5651. (progn
  5652. (add-text-properties
  5653. (match-beginning 3) (1+ (match-beginning 3))
  5654. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5655. (add-text-properties
  5656. (1- (match-end 3)) (match-end 3)
  5657. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5658. t)))))
  5659. ;;;; Visibility cycling, including org-goto and indirect buffer
  5660. ;;; Cycling
  5661. (defvar org-cycle-global-status nil)
  5662. (make-variable-buffer-local 'org-cycle-global-status)
  5663. (put 'org-cycle-global-status 'org-state t)
  5664. (defvar org-cycle-subtree-status nil)
  5665. (make-variable-buffer-local 'org-cycle-subtree-status)
  5666. (put 'org-cycle-subtree-status 'org-state t)
  5667. (defvar org-inlinetask-min-level)
  5668. ;;;###autoload
  5669. (defun org-cycle (&optional arg)
  5670. "TAB-action and visibility cycling for Org-mode.
  5671. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5672. is outline visibility cycling, but it also invokes other actions
  5673. in special contexts.
  5674. - When this function is called with a prefix argument, rotate the entire
  5675. buffer through 3 states (global cycling)
  5676. 1. OVERVIEW: Show only top-level headlines.
  5677. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5678. 3. SHOW ALL: Show everything.
  5679. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5680. determined by the variable `org-startup-folded', and by any VISIBILITY
  5681. properties in the buffer.
  5682. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5683. including any drawers.
  5684. - When inside a table, re-align the table and move to the next field.
  5685. - When point is at the beginning of a headline, rotate the subtree started
  5686. by this line through 3 different states (local cycling)
  5687. 1. FOLDED: Only the main headline is shown.
  5688. 2. CHILDREN: The main headline and the direct children are shown.
  5689. From this state, you can move to one of the children
  5690. and zoom in further.
  5691. 3. SUBTREE: Show the entire subtree, including body text.
  5692. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5693. - When point is at the beginning of an empty headline and the variable
  5694. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5695. of the headline by demoting and promoting it to likely levels. This
  5696. speeds up creation document structure by pressing TAB once or several
  5697. times right after creating a new headline.
  5698. - When there is a numeric prefix, go up to a heading with level ARG, do
  5699. a `show-subtree' and return to the previous cursor position. If ARG
  5700. is negative, go up that many levels.
  5701. - When point is not at the beginning of a headline, execute the global
  5702. binding for TAB, which is re-indenting the line. See the option
  5703. `org-cycle-emulate-tab' for details.
  5704. - Special case: if point is at the beginning of the buffer and there is
  5705. no headline in line 1, this function will act as if called with prefix arg
  5706. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5707. But only if also the variable `org-cycle-global-at-bob' is t."
  5708. (interactive "P")
  5709. (org-load-modules-maybe)
  5710. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5711. (and org-cycle-level-after-item/entry-creation
  5712. (or (org-cycle-level)
  5713. (org-cycle-item-indentation))))
  5714. (let* (message-log-max ; Don't populate the *Messages* buffer
  5715. (limit-level
  5716. (or org-cycle-max-level
  5717. (and (boundp 'org-inlinetask-min-level)
  5718. org-inlinetask-min-level
  5719. (1- org-inlinetask-min-level))))
  5720. (nstars (and limit-level
  5721. (if org-odd-levels-only
  5722. (and limit-level (1- (* limit-level 2)))
  5723. limit-level)))
  5724. (org-outline-regexp
  5725. (if (not (derived-mode-p 'org-mode))
  5726. outline-regexp
  5727. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5728. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5729. (not (looking-at org-outline-regexp))))
  5730. (org-cycle-hook
  5731. (if bob-special
  5732. (delq 'org-optimize-window-after-visibility-change
  5733. (copy-sequence org-cycle-hook))
  5734. org-cycle-hook))
  5735. (pos (point)))
  5736. (if (or bob-special (equal arg '(4)))
  5737. ;; special case: use global cycling
  5738. (setq arg t))
  5739. (cond
  5740. ((equal arg '(16))
  5741. (setq last-command 'dummy)
  5742. (org-set-startup-visibility)
  5743. (message "Startup visibility, plus VISIBILITY properties"))
  5744. ((equal arg '(64))
  5745. (show-all)
  5746. (message "Entire buffer visible, including drawers"))
  5747. ;; Table: enter it or move to the next field.
  5748. ((org-at-table-p 'any)
  5749. (if (org-at-table.el-p)
  5750. (message "Use C-c ' to edit table.el tables")
  5751. (if arg (org-table-edit-field t)
  5752. (org-table-justify-field-maybe)
  5753. (call-interactively 'org-table-next-field))))
  5754. ((run-hook-with-args-until-success
  5755. 'org-tab-after-check-for-table-hook))
  5756. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5757. ((eq arg t) (org-cycle-internal-global))
  5758. ;; Drawers: delegate to `org-flag-drawer'.
  5759. ((and org-drawers org-drawer-regexp
  5760. (save-excursion
  5761. (beginning-of-line 1)
  5762. (looking-at org-drawer-regexp)))
  5763. (org-flag-drawer ; toggle block visibility
  5764. (not (get-char-property (match-end 0) 'invisible))))
  5765. ;; Show-subtree, ARG levels up from here.
  5766. ((integerp arg)
  5767. (save-excursion
  5768. (org-back-to-heading)
  5769. (outline-up-heading (if (< arg 0) (- arg)
  5770. (- (funcall outline-level) arg)))
  5771. (org-show-subtree)))
  5772. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5773. ((and (featurep 'org-inlinetask)
  5774. (org-inlinetask-at-task-p)
  5775. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5776. (org-inlinetask-toggle-visibility))
  5777. ((org-try-cdlatex-tab))
  5778. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5779. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5780. (save-excursion (beginning-of-line 1)
  5781. (looking-at org-outline-regexp)))
  5782. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5783. (org-cycle-internal-local))
  5784. ;; From there: TAB emulation and template completion.
  5785. (buffer-read-only (org-back-to-heading))
  5786. ((run-hook-with-args-until-success
  5787. 'org-tab-after-check-for-cycling-hook))
  5788. ((org-try-structure-completion))
  5789. ((run-hook-with-args-until-success
  5790. 'org-tab-before-tab-emulation-hook))
  5791. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5792. (or (not (bolp))
  5793. (not (looking-at org-outline-regexp))))
  5794. (call-interactively (global-key-binding "\t")))
  5795. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5796. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5797. (or (and (eq org-cycle-emulate-tab 'white)
  5798. (= (match-end 0) (point-at-eol)))
  5799. (and (eq org-cycle-emulate-tab 'whitestart)
  5800. (>= (match-end 0) pos))))
  5801. t
  5802. (eq org-cycle-emulate-tab t))
  5803. (call-interactively (global-key-binding "\t")))
  5804. (t (save-excursion
  5805. (org-back-to-heading)
  5806. (org-cycle)))))))
  5807. (defun org-cycle-internal-global ()
  5808. "Do the global cycling action."
  5809. ;; Hack to avoid display of messages for .org attachments in Gnus
  5810. (let (message-log-max ; Don't populate the *Messages* buffer
  5811. (ga (string-match "\\*fontification" (buffer-name))))
  5812. (cond
  5813. ((and (eq last-command this-command)
  5814. (eq org-cycle-global-status 'overview))
  5815. ;; We just created the overview - now do table of contents
  5816. ;; This can be slow in very large buffers, so indicate action
  5817. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5818. (unless ga (message "CONTENTS..."))
  5819. (org-content)
  5820. (unless ga (message "CONTENTS...done"))
  5821. (setq org-cycle-global-status 'contents)
  5822. (run-hook-with-args 'org-cycle-hook 'contents))
  5823. ((and (eq last-command this-command)
  5824. (eq org-cycle-global-status 'contents))
  5825. ;; We just showed the table of contents - now show everything
  5826. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5827. (show-all)
  5828. (unless ga (message "SHOW ALL"))
  5829. (setq org-cycle-global-status 'all)
  5830. (run-hook-with-args 'org-cycle-hook 'all))
  5831. (t
  5832. ;; Default action: go to overview
  5833. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5834. (org-overview)
  5835. (unless ga (message "OVERVIEW"))
  5836. (setq org-cycle-global-status 'overview)
  5837. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5838. (defun org-cycle-internal-local ()
  5839. "Do the local cycling action."
  5840. (let (message-log-max ; Don't populate the *Messages* buffer
  5841. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5842. ;; First, determine end of headline (EOH), end of subtree or item
  5843. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5844. (save-excursion
  5845. (if (org-at-item-p)
  5846. (progn
  5847. (beginning-of-line)
  5848. (setq struct (org-list-struct))
  5849. (setq eoh (point-at-eol))
  5850. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5851. (setq has-children (org-list-has-child-p (point) struct)))
  5852. (org-back-to-heading)
  5853. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5854. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5855. (setq has-children
  5856. (or (save-excursion
  5857. (let ((level (funcall outline-level)))
  5858. (outline-next-heading)
  5859. (and (org-at-heading-p t)
  5860. (> (funcall outline-level) level))))
  5861. (save-excursion
  5862. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5863. ;; Determine end invisible part of buffer (EOL)
  5864. (beginning-of-line 2)
  5865. ;; XEmacs doesn't have `next-single-char-property-change'
  5866. (if (featurep 'xemacs)
  5867. (while (and (not (eobp)) ;; this is like `next-line'
  5868. (get-char-property (1- (point)) 'invisible))
  5869. (beginning-of-line 2))
  5870. (while (and (not (eobp)) ;; this is like `next-line'
  5871. (get-char-property (1- (point)) 'invisible))
  5872. (goto-char (next-single-char-property-change (point) 'invisible))
  5873. (and (eolp) (beginning-of-line 2))))
  5874. (setq eol (point)))
  5875. ;; Find out what to do next and set `this-command'
  5876. (cond
  5877. ((= eos eoh)
  5878. ;; Nothing is hidden behind this heading
  5879. (unless (org-before-first-heading-p)
  5880. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5881. (message "EMPTY ENTRY")
  5882. (setq org-cycle-subtree-status nil)
  5883. (save-excursion
  5884. (goto-char eos)
  5885. (outline-next-heading)
  5886. (if (outline-invisible-p) (org-flag-heading nil))))
  5887. ((and (or (>= eol eos)
  5888. (not (string-match "\\S-" (buffer-substring eol eos))))
  5889. (or has-children
  5890. (not (setq children-skipped
  5891. org-cycle-skip-children-state-if-no-children))))
  5892. ;; Entire subtree is hidden in one line: children view
  5893. (unless (org-before-first-heading-p)
  5894. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5895. (if (org-at-item-p)
  5896. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5897. (org-show-entry)
  5898. (org-with-limited-levels (show-children))
  5899. ;; FIXME: This slows down the func way too much.
  5900. ;; How keep drawers hidden in subtree anyway?
  5901. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5902. ;; (org-cycle-hide-drawers 'subtree))
  5903. ;; Fold every list in subtree to top-level items.
  5904. (when (eq org-cycle-include-plain-lists 'integrate)
  5905. (save-excursion
  5906. (org-back-to-heading)
  5907. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5908. (beginning-of-line 1)
  5909. (let* ((struct (org-list-struct))
  5910. (prevs (org-list-prevs-alist struct))
  5911. (end (org-list-get-bottom-point struct)))
  5912. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5913. (org-list-get-all-items (point) struct prevs))
  5914. (goto-char end))))))
  5915. (message "CHILDREN")
  5916. (save-excursion
  5917. (goto-char eos)
  5918. (outline-next-heading)
  5919. (if (outline-invisible-p) (org-flag-heading nil)))
  5920. (setq org-cycle-subtree-status 'children)
  5921. (unless (org-before-first-heading-p)
  5922. (run-hook-with-args 'org-cycle-hook 'children)))
  5923. ((or children-skipped
  5924. (and (eq last-command this-command)
  5925. (eq org-cycle-subtree-status 'children)))
  5926. ;; We just showed the children, or no children are there,
  5927. ;; now show everything.
  5928. (unless (org-before-first-heading-p)
  5929. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5930. (outline-flag-region eoh eos nil)
  5931. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5932. (setq org-cycle-subtree-status 'subtree)
  5933. (unless (org-before-first-heading-p)
  5934. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5935. (t
  5936. ;; Default action: hide the subtree.
  5937. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5938. (outline-flag-region eoh eos t)
  5939. (message "FOLDED")
  5940. (setq org-cycle-subtree-status 'folded)
  5941. (unless (org-before-first-heading-p)
  5942. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5943. ;;;###autoload
  5944. (defun org-global-cycle (&optional arg)
  5945. "Cycle the global visibility. For details see `org-cycle'.
  5946. With \\[universal-argument] prefix arg, switch to startup visibility.
  5947. With a numeric prefix, show all headlines up to that level."
  5948. (interactive "P")
  5949. (let ((org-cycle-include-plain-lists
  5950. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5951. (cond
  5952. ((integerp arg)
  5953. (show-all)
  5954. (hide-sublevels arg)
  5955. (setq org-cycle-global-status 'contents))
  5956. ((equal arg '(4))
  5957. (org-set-startup-visibility)
  5958. (message "Startup visibility, plus VISIBILITY properties."))
  5959. (t
  5960. (org-cycle '(4))))))
  5961. (defun org-set-startup-visibility ()
  5962. "Set the visibility required by startup options and properties."
  5963. (cond
  5964. ((eq org-startup-folded t)
  5965. (org-cycle '(4)))
  5966. ((eq org-startup-folded 'content)
  5967. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5968. (org-cycle '(4)) (org-cycle '(4)))))
  5969. (unless (eq org-startup-folded 'showeverything)
  5970. (if org-hide-block-startup (org-hide-block-all))
  5971. (org-set-visibility-according-to-property 'no-cleanup)
  5972. (org-cycle-hide-archived-subtrees 'all)
  5973. (org-cycle-hide-drawers 'all)
  5974. (org-cycle-show-empty-lines t)))
  5975. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5976. "Switch subtree visibilities according to :VISIBILITY: property."
  5977. (interactive)
  5978. (let (org-show-entry-below state)
  5979. (save-excursion
  5980. (goto-char (point-min))
  5981. (while (re-search-forward
  5982. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5983. nil t)
  5984. (setq state (match-string 1))
  5985. (save-excursion
  5986. (org-back-to-heading t)
  5987. (hide-subtree)
  5988. (org-reveal)
  5989. (cond
  5990. ((equal state '("fold" "folded"))
  5991. (hide-subtree))
  5992. ((equal state "children")
  5993. (org-show-hidden-entry)
  5994. (show-children))
  5995. ((equal state "content")
  5996. (save-excursion
  5997. (save-restriction
  5998. (org-narrow-to-subtree)
  5999. (org-content))))
  6000. ((member state '("all" "showall"))
  6001. (show-subtree)))))
  6002. (unless no-cleanup
  6003. (org-cycle-hide-archived-subtrees 'all)
  6004. (org-cycle-hide-drawers 'all)
  6005. (org-cycle-show-empty-lines 'all)))))
  6006. ;; This function uses outline-regexp instead of the more fundamental
  6007. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6008. ;; buffers, where outline-regexp is needed.
  6009. (defun org-overview ()
  6010. "Switch to overview mode, showing only top-level headlines.
  6011. Really, this shows all headlines with level equal or greater than the level
  6012. of the first headline in the buffer. This is important, because if the
  6013. first headline is not level one, then (hide-sublevels 1) gives confusing
  6014. results."
  6015. (interactive)
  6016. (let ((l (org-current-line))
  6017. (level (save-excursion
  6018. (goto-char (point-min))
  6019. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6020. (progn
  6021. (goto-char (match-beginning 0))
  6022. (funcall outline-level))))))
  6023. (and level (hide-sublevels level))
  6024. (recenter '(4))
  6025. (org-goto-line l)))
  6026. (defun org-content (&optional arg)
  6027. "Show all headlines in the buffer, like a table of contents.
  6028. With numerical argument N, show content up to level N."
  6029. (interactive "P")
  6030. (save-excursion
  6031. ;; Visit all headings and show their offspring
  6032. (and (integerp arg) (org-overview))
  6033. (goto-char (point-max))
  6034. (catch 'exit
  6035. (while (and (progn (condition-case nil
  6036. (outline-previous-visible-heading 1)
  6037. (error (goto-char (point-min))))
  6038. t)
  6039. (looking-at org-outline-regexp))
  6040. (if (integerp arg)
  6041. (show-children (1- arg))
  6042. (show-branches))
  6043. (if (bobp) (throw 'exit nil))))))
  6044. (defun org-optimize-window-after-visibility-change (state)
  6045. "Adjust the window after a change in outline visibility.
  6046. This function is the default value of the hook `org-cycle-hook'."
  6047. (when (get-buffer-window (current-buffer))
  6048. (cond
  6049. ((eq state 'content) nil)
  6050. ((eq state 'all) nil)
  6051. ((eq state 'folded) nil)
  6052. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6053. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6054. (defun org-remove-empty-overlays-at (pos)
  6055. "Remove outline overlays that do not contain non-white stuff."
  6056. (mapc
  6057. (lambda (o)
  6058. (and (eq 'outline (overlay-get o 'invisible))
  6059. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6060. (overlay-end o))))
  6061. (delete-overlay o)))
  6062. (overlays-at pos)))
  6063. (defun org-clean-visibility-after-subtree-move ()
  6064. "Fix visibility issues after moving a subtree."
  6065. ;; First, find a reasonable region to look at:
  6066. ;; Start two siblings above, end three below
  6067. (let* ((beg (save-excursion
  6068. (and (org-get-last-sibling)
  6069. (org-get-last-sibling))
  6070. (point)))
  6071. (end (save-excursion
  6072. (and (org-get-next-sibling)
  6073. (org-get-next-sibling)
  6074. (org-get-next-sibling))
  6075. (if (org-at-heading-p)
  6076. (point-at-eol)
  6077. (point))))
  6078. (level (looking-at "\\*+"))
  6079. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6080. (save-excursion
  6081. (save-restriction
  6082. (narrow-to-region beg end)
  6083. (when re
  6084. ;; Properly fold already folded siblings
  6085. (goto-char (point-min))
  6086. (while (re-search-forward re nil t)
  6087. (if (and (not (outline-invisible-p))
  6088. (save-excursion
  6089. (goto-char (point-at-eol)) (outline-invisible-p)))
  6090. (hide-entry))))
  6091. (org-cycle-show-empty-lines 'overview)
  6092. (org-cycle-hide-drawers 'overview)))))
  6093. (defun org-cycle-show-empty-lines (state)
  6094. "Show empty lines above all visible headlines.
  6095. The region to be covered depends on STATE when called through
  6096. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6097. entire buffer covered. Note that an empty line is only shown if there
  6098. are at least `org-cycle-separator-lines' empty lines before the headline."
  6099. (when (not (= org-cycle-separator-lines 0))
  6100. (save-excursion
  6101. (let* ((n (abs org-cycle-separator-lines))
  6102. (re (cond
  6103. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6104. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6105. (t (let ((ns (number-to-string (- n 2))))
  6106. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6107. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6108. beg end b e)
  6109. (cond
  6110. ((memq state '(overview contents t))
  6111. (setq beg (point-min) end (point-max)))
  6112. ((memq state '(children folded))
  6113. (setq beg (point) end (progn (org-end-of-subtree t t)
  6114. (beginning-of-line 2)
  6115. (point)))))
  6116. (when beg
  6117. (goto-char beg)
  6118. (while (re-search-forward re end t)
  6119. (unless (get-char-property (match-end 1) 'invisible)
  6120. (setq e (match-end 1))
  6121. (if (< org-cycle-separator-lines 0)
  6122. (setq b (save-excursion
  6123. (goto-char (match-beginning 0))
  6124. (org-back-over-empty-lines)
  6125. (if (save-excursion
  6126. (goto-char (max (point-min) (1- (point))))
  6127. (org-at-heading-p))
  6128. (1- (point))
  6129. (point))))
  6130. (setq b (match-beginning 1)))
  6131. (outline-flag-region b e nil)))))))
  6132. ;; Never hide empty lines at the end of the file.
  6133. (save-excursion
  6134. (goto-char (point-max))
  6135. (outline-previous-heading)
  6136. (outline-end-of-heading)
  6137. (if (and (looking-at "[ \t\n]+")
  6138. (= (match-end 0) (point-max)))
  6139. (outline-flag-region (point) (match-end 0) nil))))
  6140. (defun org-show-empty-lines-in-parent ()
  6141. "Move to the parent and re-show empty lines before visible headlines."
  6142. (save-excursion
  6143. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6144. (org-cycle-show-empty-lines context))))
  6145. (defun org-files-list ()
  6146. "Return `org-agenda-files' list, plus all open org-mode files.
  6147. This is useful for operations that need to scan all of a user's
  6148. open and agenda-wise Org files."
  6149. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6150. (dolist (buf (buffer-list))
  6151. (with-current-buffer buf
  6152. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6153. (let ((file (expand-file-name (buffer-file-name))))
  6154. (unless (member file files)
  6155. (push file files))))))
  6156. files))
  6157. (defsubst org-entry-beginning-position ()
  6158. "Return the beginning position of the current entry."
  6159. (save-excursion (outline-back-to-heading t) (point)))
  6160. (defsubst org-entry-end-position ()
  6161. "Return the end position of the current entry."
  6162. (save-excursion (outline-next-heading) (point)))
  6163. (defun org-cycle-hide-drawers (state)
  6164. "Re-hide all drawers after a visibility state change."
  6165. (when (and (derived-mode-p 'org-mode)
  6166. (not (memq state '(overview folded contents))))
  6167. (save-excursion
  6168. (let* ((globalp (memq state '(contents all)))
  6169. (beg (if globalp (point-min) (point)))
  6170. (end (if globalp (point-max)
  6171. (if (eq state 'children)
  6172. (save-excursion (outline-next-heading) (point))
  6173. (org-end-of-subtree t)))))
  6174. (goto-char beg)
  6175. (while (re-search-forward org-drawer-regexp end t)
  6176. (org-flag-drawer t))))))
  6177. (defun org-cycle-hide-inline-tasks (state)
  6178. "Re-hide inline task when switching to 'contents visibility state."
  6179. (when (and (eq state 'contents)
  6180. (boundp 'org-inlinetask-min-level)
  6181. org-inlinetask-min-level)
  6182. (hide-sublevels (1- org-inlinetask-min-level))))
  6183. (defun org-flag-drawer (flag)
  6184. "When FLAG is non-nil, hide the drawer we are within.
  6185. Otherwise make it visible."
  6186. (save-excursion
  6187. (beginning-of-line 1)
  6188. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6189. (let ((b (match-end 0)))
  6190. (if (re-search-forward
  6191. "^[ \t]*:END:"
  6192. (save-excursion (outline-next-heading) (point)) t)
  6193. (outline-flag-region b (point-at-eol) flag)
  6194. (error ":END: line missing at position %s" b))))))
  6195. (defun org-subtree-end-visible-p ()
  6196. "Is the end of the current subtree visible?"
  6197. (pos-visible-in-window-p
  6198. (save-excursion (org-end-of-subtree t) (point))))
  6199. (defun org-first-headline-recenter (&optional N)
  6200. "Move cursor to the first headline and recenter the headline.
  6201. Optional argument N means put the headline into the Nth line of the window."
  6202. (goto-char (point-min))
  6203. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6204. (beginning-of-line)
  6205. (recenter (prefix-numeric-value N))))
  6206. ;;; Saving and restoring visibility
  6207. (defun org-outline-overlay-data (&optional use-markers)
  6208. "Return a list of the locations of all outline overlays.
  6209. These are overlays with the `invisible' property value `outline'.
  6210. The return value is a list of cons cells, with start and stop
  6211. positions for each overlay.
  6212. If USE-MARKERS is set, return the positions as markers."
  6213. (let (beg end)
  6214. (save-excursion
  6215. (save-restriction
  6216. (widen)
  6217. (delq nil
  6218. (mapcar (lambda (o)
  6219. (when (eq (overlay-get o 'invisible) 'outline)
  6220. (setq beg (overlay-start o)
  6221. end (overlay-end o))
  6222. (and beg end (> end beg)
  6223. (if use-markers
  6224. (cons (move-marker (make-marker) beg)
  6225. (move-marker (make-marker) end))
  6226. (cons beg end)))))
  6227. (overlays-in (point-min) (point-max))))))))
  6228. (defun org-set-outline-overlay-data (data)
  6229. "Create visibility overlays for all positions in DATA.
  6230. DATA should have been made by `org-outline-overlay-data'."
  6231. (let (o)
  6232. (save-excursion
  6233. (save-restriction
  6234. (widen)
  6235. (show-all)
  6236. (mapc (lambda (c)
  6237. (outline-flag-region (car c) (cdr c) t))
  6238. data)))))
  6239. ;;; Folding of blocks
  6240. (defvar org-hide-block-overlays nil
  6241. "Overlays hiding blocks.")
  6242. (make-variable-buffer-local 'org-hide-block-overlays)
  6243. (defun org-block-map (function &optional start end)
  6244. "Call FUNCTION at the head of all source blocks in the current buffer.
  6245. Optional arguments START and END can be used to limit the range."
  6246. (let ((start (or start (point-min)))
  6247. (end (or end (point-max))))
  6248. (save-excursion
  6249. (goto-char start)
  6250. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6251. (save-excursion
  6252. (save-match-data
  6253. (goto-char (match-beginning 0))
  6254. (funcall function)))))))
  6255. (defun org-hide-block-toggle-all ()
  6256. "Toggle the visibility of all blocks in the current buffer."
  6257. (org-block-map #'org-hide-block-toggle))
  6258. (defun org-hide-block-all ()
  6259. "Fold all blocks in the current buffer."
  6260. (interactive)
  6261. (org-show-block-all)
  6262. (org-block-map #'org-hide-block-toggle-maybe))
  6263. (defun org-show-block-all ()
  6264. "Unfold all blocks in the current buffer."
  6265. (interactive)
  6266. (mapc 'delete-overlay org-hide-block-overlays)
  6267. (setq org-hide-block-overlays nil))
  6268. (defun org-hide-block-toggle-maybe ()
  6269. "Toggle visibility of block at point."
  6270. (interactive)
  6271. (let ((case-fold-search t))
  6272. (if (save-excursion
  6273. (beginning-of-line 1)
  6274. (looking-at org-block-regexp))
  6275. (progn (org-hide-block-toggle)
  6276. t) ;; to signal that we took action
  6277. nil))) ;; to signal that we did not
  6278. (defun org-hide-block-toggle (&optional force)
  6279. "Toggle the visibility of the current block."
  6280. (interactive)
  6281. (save-excursion
  6282. (beginning-of-line)
  6283. (if (re-search-forward org-block-regexp nil t)
  6284. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6285. (end (match-end 0)) ;; end of entire body
  6286. ov)
  6287. (if (memq t (mapcar (lambda (overlay)
  6288. (eq (overlay-get overlay 'invisible)
  6289. 'org-hide-block))
  6290. (overlays-at start)))
  6291. (if (or (not force) (eq force 'off))
  6292. (mapc (lambda (ov)
  6293. (when (member ov org-hide-block-overlays)
  6294. (setq org-hide-block-overlays
  6295. (delq ov org-hide-block-overlays)))
  6296. (when (eq (overlay-get ov 'invisible)
  6297. 'org-hide-block)
  6298. (delete-overlay ov)))
  6299. (overlays-at start)))
  6300. (setq ov (make-overlay start end))
  6301. (overlay-put ov 'invisible 'org-hide-block)
  6302. ;; make the block accessible to isearch
  6303. (overlay-put
  6304. ov 'isearch-open-invisible
  6305. (lambda (ov)
  6306. (when (member ov org-hide-block-overlays)
  6307. (setq org-hide-block-overlays
  6308. (delq ov org-hide-block-overlays)))
  6309. (when (eq (overlay-get ov 'invisible)
  6310. 'org-hide-block)
  6311. (delete-overlay ov))))
  6312. (push ov org-hide-block-overlays)))
  6313. (error "Not looking at a source block"))))
  6314. ;; org-tab-after-check-for-cycling-hook
  6315. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6316. ;; Remove overlays when changing major mode
  6317. (add-hook 'org-mode-hook
  6318. (lambda () (org-add-hook 'change-major-mode-hook
  6319. 'org-show-block-all 'append 'local)))
  6320. ;;; Org-goto
  6321. (defvar org-goto-window-configuration nil)
  6322. (defvar org-goto-marker nil)
  6323. (defvar org-goto-map)
  6324. (defun org-goto-map ()
  6325. "Set the keymap `org-goto'."
  6326. (setq org-goto-map
  6327. (let ((map (make-sparse-keymap)))
  6328. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6329. mouse-drag-region universal-argument org-occur))
  6330. cmd)
  6331. (while (setq cmd (pop cmds))
  6332. (substitute-key-definition cmd cmd map global-map)))
  6333. (suppress-keymap map)
  6334. (org-defkey map "\C-m" 'org-goto-ret)
  6335. (org-defkey map [(return)] 'org-goto-ret)
  6336. (org-defkey map [(left)] 'org-goto-left)
  6337. (org-defkey map [(right)] 'org-goto-right)
  6338. (org-defkey map [(control ?g)] 'org-goto-quit)
  6339. (org-defkey map "\C-i" 'org-cycle)
  6340. (org-defkey map [(tab)] 'org-cycle)
  6341. (org-defkey map [(down)] 'outline-next-visible-heading)
  6342. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6343. (if org-goto-auto-isearch
  6344. (if (fboundp 'define-key-after)
  6345. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6346. nil)
  6347. (org-defkey map "q" 'org-goto-quit)
  6348. (org-defkey map "n" 'outline-next-visible-heading)
  6349. (org-defkey map "p" 'outline-previous-visible-heading)
  6350. (org-defkey map "f" 'outline-forward-same-level)
  6351. (org-defkey map "b" 'outline-backward-same-level)
  6352. (org-defkey map "u" 'outline-up-heading))
  6353. (org-defkey map "/" 'org-occur)
  6354. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6355. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6356. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6357. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6358. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6359. map)))
  6360. (defconst org-goto-help
  6361. "Browse buffer copy, to find location or copy text.%s
  6362. RET=jump to location C-g=quit and return to previous location
  6363. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6364. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6365. ;; FIXME: Docstring does not mention both interfaces
  6366. (defun org-goto (&optional alternative-interface)
  6367. "Look up a different location in the current file, keeping current visibility.
  6368. When you want look-up or go to a different location in a
  6369. document, the fastest way is often to fold the entire buffer and
  6370. then dive into the tree. This method has the disadvantage, that
  6371. the previous location will be folded, which may not be what you
  6372. want.
  6373. This command works around this by showing a copy of the current
  6374. buffer in an indirect buffer, in overview mode. You can dive
  6375. into the tree in that copy, use org-occur and incremental search
  6376. to find a location. When pressing RET or `Q', the command
  6377. returns to the original buffer in which the visibility is still
  6378. unchanged. After RET it will also jump to the location selected
  6379. in the indirect buffer and expose the headline hierarchy above.
  6380. With a prefix argument, use the alternative interface: e.g. if
  6381. `org-goto-interface' is 'outline use 'outline-path-completion."
  6382. (interactive "P")
  6383. (org-goto-map)
  6384. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6385. (org-refile-use-outline-path t)
  6386. (org-refile-target-verify-function nil)
  6387. (interface
  6388. (if (not alternative-interface)
  6389. org-goto-interface
  6390. (if (eq org-goto-interface 'outline)
  6391. 'outline-path-completion
  6392. 'outline)))
  6393. (org-goto-start-pos (point))
  6394. (selected-point
  6395. (if (eq interface 'outline)
  6396. (car (org-get-location (current-buffer) org-goto-help))
  6397. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6398. (org-refile-check-position pa)
  6399. (nth 3 pa)))))
  6400. (if selected-point
  6401. (progn
  6402. (org-mark-ring-push org-goto-start-pos)
  6403. (goto-char selected-point)
  6404. (if (or (outline-invisible-p) (org-invisible-p2))
  6405. (org-show-context 'org-goto)))
  6406. (message "Quit"))))
  6407. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6408. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6409. (defvar org-goto-local-auto-isearch-map) ; defined below
  6410. (defun org-get-location (buf help)
  6411. "Let the user select a location in the Org-mode buffer BUF.
  6412. This function uses a recursive edit. It returns the selected position
  6413. or nil."
  6414. (org-no-popups
  6415. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6416. (isearch-hide-immediately nil)
  6417. (isearch-search-fun-function
  6418. (lambda () 'org-goto-local-search-headings))
  6419. (org-goto-selected-point org-goto-exit-command))
  6420. (save-excursion
  6421. (save-window-excursion
  6422. (delete-other-windows)
  6423. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6424. (org-pop-to-buffer-same-window
  6425. (condition-case nil
  6426. (make-indirect-buffer (current-buffer) "*org-goto*")
  6427. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6428. (with-output-to-temp-buffer "*Org Help*"
  6429. (princ (format help (if org-goto-auto-isearch
  6430. " Just type for auto-isearch."
  6431. " n/p/f/b/u to navigate, q to quit."))))
  6432. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6433. (setq buffer-read-only nil)
  6434. (let ((org-startup-truncated t)
  6435. (org-startup-folded nil)
  6436. (org-startup-align-all-tables nil))
  6437. (org-mode)
  6438. (org-overview))
  6439. (setq buffer-read-only t)
  6440. (if (and (boundp 'org-goto-start-pos)
  6441. (integer-or-marker-p org-goto-start-pos))
  6442. (let ((org-show-hierarchy-above t)
  6443. (org-show-siblings t)
  6444. (org-show-following-heading t))
  6445. (goto-char org-goto-start-pos)
  6446. (and (outline-invisible-p) (org-show-context)))
  6447. (goto-char (point-min)))
  6448. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6449. (message "Select location and press RET")
  6450. (use-local-map org-goto-map)
  6451. (recursive-edit)))
  6452. (kill-buffer "*org-goto*")
  6453. (cons org-goto-selected-point org-goto-exit-command))))
  6454. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6455. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6456. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6457. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6458. (defun org-goto-local-search-headings (string bound noerror)
  6459. "Search and make sure that any matches are in headlines."
  6460. (catch 'return
  6461. (while (if isearch-forward
  6462. (search-forward string bound noerror)
  6463. (search-backward string bound noerror))
  6464. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6465. (and (member :headline context)
  6466. (not (member :tags context))))
  6467. (throw 'return (point))))))
  6468. (defun org-goto-local-auto-isearch ()
  6469. "Start isearch."
  6470. (interactive)
  6471. (goto-char (point-min))
  6472. (let ((keys (this-command-keys)))
  6473. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6474. (isearch-mode t)
  6475. (isearch-process-search-char (string-to-char keys)))))
  6476. (defun org-goto-ret (&optional arg)
  6477. "Finish `org-goto' by going to the new location."
  6478. (interactive "P")
  6479. (setq org-goto-selected-point (point)
  6480. org-goto-exit-command 'return)
  6481. (throw 'exit nil))
  6482. (defun org-goto-left ()
  6483. "Finish `org-goto' by going to the new location."
  6484. (interactive)
  6485. (if (org-at-heading-p)
  6486. (progn
  6487. (beginning-of-line 1)
  6488. (setq org-goto-selected-point (point)
  6489. org-goto-exit-command 'left)
  6490. (throw 'exit nil))
  6491. (error "Not on a heading")))
  6492. (defun org-goto-right ()
  6493. "Finish `org-goto' by going to the new location."
  6494. (interactive)
  6495. (if (org-at-heading-p)
  6496. (progn
  6497. (setq org-goto-selected-point (point)
  6498. org-goto-exit-command 'right)
  6499. (throw 'exit nil))
  6500. (error "Not on a heading")))
  6501. (defun org-goto-quit ()
  6502. "Finish `org-goto' without cursor motion."
  6503. (interactive)
  6504. (setq org-goto-selected-point nil)
  6505. (setq org-goto-exit-command 'quit)
  6506. (throw 'exit nil))
  6507. ;;; Indirect buffer display of subtrees
  6508. (defvar org-indirect-dedicated-frame nil
  6509. "This is the frame being used for indirect tree display.")
  6510. (defvar org-last-indirect-buffer nil)
  6511. (defun org-tree-to-indirect-buffer (&optional arg)
  6512. "Create indirect buffer and narrow it to current subtree.
  6513. With a numerical prefix ARG, go up to this level and then take that tree.
  6514. If ARG is negative, go up that many levels.
  6515. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6516. indirect buffer previously made with this command, to avoid proliferation of
  6517. indirect buffers. However, when you call the command with a \
  6518. \\[universal-argument] prefix, or
  6519. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6520. is kept so that you can work with several indirect buffers at the same time.
  6521. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6522. \\[universal-argument] prefix also
  6523. requests that a new frame be made for the new buffer, so that the dedicated
  6524. frame is not changed."
  6525. (interactive "P")
  6526. (let ((cbuf (current-buffer))
  6527. (cwin (selected-window))
  6528. (pos (point))
  6529. beg end level heading ibuf)
  6530. (save-excursion
  6531. (org-back-to-heading t)
  6532. (when (numberp arg)
  6533. (setq level (org-outline-level))
  6534. (if (< arg 0) (setq arg (+ level arg)))
  6535. (while (> (setq level (org-outline-level)) arg)
  6536. (org-up-heading-safe)))
  6537. (setq beg (point)
  6538. heading (org-get-heading))
  6539. (org-end-of-subtree t t)
  6540. (if (org-at-heading-p) (backward-char 1))
  6541. (setq end (point)))
  6542. (if (and (buffer-live-p org-last-indirect-buffer)
  6543. (not (eq org-indirect-buffer-display 'new-frame))
  6544. (not arg))
  6545. (kill-buffer org-last-indirect-buffer))
  6546. (setq ibuf (org-get-indirect-buffer cbuf)
  6547. org-last-indirect-buffer ibuf)
  6548. (cond
  6549. ((or (eq org-indirect-buffer-display 'new-frame)
  6550. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6551. (select-frame (make-frame))
  6552. (delete-other-windows)
  6553. (org-pop-to-buffer-same-window ibuf)
  6554. (org-set-frame-title heading))
  6555. ((eq org-indirect-buffer-display 'dedicated-frame)
  6556. (raise-frame
  6557. (select-frame (or (and org-indirect-dedicated-frame
  6558. (frame-live-p org-indirect-dedicated-frame)
  6559. org-indirect-dedicated-frame)
  6560. (setq org-indirect-dedicated-frame (make-frame)))))
  6561. (delete-other-windows)
  6562. (org-pop-to-buffer-same-window ibuf)
  6563. (org-set-frame-title (concat "Indirect: " heading)))
  6564. ((eq org-indirect-buffer-display 'current-window)
  6565. (org-pop-to-buffer-same-window ibuf))
  6566. ((eq org-indirect-buffer-display 'other-window)
  6567. (pop-to-buffer ibuf))
  6568. (t (error "Invalid value")))
  6569. (if (featurep 'xemacs)
  6570. (save-excursion (org-mode) (turn-on-font-lock)))
  6571. (narrow-to-region beg end)
  6572. (show-all)
  6573. (goto-char pos)
  6574. (run-hook-with-args 'org-cycle-hook 'all)
  6575. (and (window-live-p cwin) (select-window cwin))))
  6576. (defun org-get-indirect-buffer (&optional buffer)
  6577. (setq buffer (or buffer (current-buffer)))
  6578. (let ((n 1) (base (buffer-name buffer)) bname)
  6579. (while (buffer-live-p
  6580. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6581. (setq n (1+ n)))
  6582. (condition-case nil
  6583. (make-indirect-buffer buffer bname 'clone)
  6584. (error (make-indirect-buffer buffer bname)))))
  6585. (defun org-set-frame-title (title)
  6586. "Set the title of the current frame to the string TITLE."
  6587. ;; FIXME: how to name a single frame in XEmacs???
  6588. (unless (featurep 'xemacs)
  6589. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6590. ;;;; Structure editing
  6591. ;;; Inserting headlines
  6592. (defun org-previous-line-empty-p (&optional next)
  6593. "Is the previous line a blank line?
  6594. When NEXT is non-nil, check the next line instead."
  6595. (save-excursion
  6596. (and (not (bobp))
  6597. (or (beginning-of-line (if next 2 0)) t)
  6598. (save-match-data
  6599. (looking-at "[ \t]*$")))))
  6600. (defun org-insert-heading (&optional arg invisible-ok)
  6601. "Insert a new heading or item with same depth at point.
  6602. If point is in a plain list and ARG is nil, create a new list item.
  6603. With one universal prefix argument, insert a heading even in lists.
  6604. With two universal prefix arguments, insert the heading at the end
  6605. of the parent subtree.
  6606. If point is at the beginning of a headline, insert a sibling before
  6607. the current headline. If point is not at the beginning, split the line
  6608. and create a new headline with the text in the current line after point
  6609. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6610. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6611. This is important for non-interactive uses of the command."
  6612. (interactive "P")
  6613. (cond
  6614. ((or (= (buffer-size) 0)
  6615. (and (not (save-excursion
  6616. (and (ignore-errors (org-back-to-heading invisible-ok))
  6617. (org-at-heading-p))))
  6618. (or arg (not (org-in-item-p)))))
  6619. (insert
  6620. (if (org-previous-line-empty-p) "" "\n")
  6621. (if (org-in-src-block-p) ",* " "* "))
  6622. (run-hooks 'org-insert-heading-hook))
  6623. ((or arg (not (org-insert-item
  6624. (save-excursion
  6625. (beginning-of-line)
  6626. (looking-at org-list-full-item-re)
  6627. (match-string 3)))))
  6628. (let (begn endn)
  6629. (when (org-buffer-narrowed-p)
  6630. (setq begn (point-min) endn (point-max))
  6631. (widen))
  6632. (let* ((empty-line-p nil)
  6633. (eops (equal arg '(16))) ; insert at end of parent subtree
  6634. (org-insert-heading-respect-content
  6635. (or (not (null arg)) org-insert-heading-respect-content))
  6636. (level nil)
  6637. (on-heading (org-at-heading-p))
  6638. (head (save-excursion
  6639. (condition-case nil
  6640. (progn
  6641. (org-back-to-heading invisible-ok)
  6642. (when (and (not on-heading)
  6643. (featurep 'org-inlinetask)
  6644. (integerp org-inlinetask-min-level)
  6645. (>= (length (match-string 0))
  6646. org-inlinetask-min-level))
  6647. ;; Find a heading level before the inline task
  6648. (while (and (setq level (org-up-heading-safe))
  6649. (>= level org-inlinetask-min-level)))
  6650. (if (org-at-heading-p)
  6651. (org-back-to-heading invisible-ok)
  6652. (error "This should not happen")))
  6653. (unless (and (save-excursion
  6654. (save-match-data
  6655. (org-backward-heading-same-level 1 invisible-ok))
  6656. (= (point) (match-beginning 0)))
  6657. (not (org-previous-line-empty-p t)))
  6658. (setq empty-line-p (org-previous-line-empty-p)))
  6659. (match-string 0))
  6660. (error "* "))))
  6661. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6662. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6663. pos hide-previous previous-pos)
  6664. (if ;; At the beginning of a heading, open a new line for insertiong
  6665. (and (bolp) (org-at-heading-p)
  6666. (not eops)
  6667. (or (bobp)
  6668. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6669. (open-line (if blank 2 1))
  6670. (save-excursion
  6671. (setq previous-pos (point-at-bol))
  6672. (end-of-line)
  6673. (setq hide-previous (outline-invisible-p)))
  6674. (and org-insert-heading-respect-content
  6675. (save-excursion
  6676. (while (outline-invisible-p)
  6677. (org-show-subtree)
  6678. (org-up-heading-safe))))
  6679. (let ((split
  6680. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6681. (save-excursion
  6682. (let ((p (point)))
  6683. (goto-char (point-at-bol))
  6684. (and (looking-at org-complex-heading-regexp)
  6685. (match-beginning 4)
  6686. (> p (match-beginning 4)))))))
  6687. tags pos)
  6688. (cond
  6689. ;; Insert a new line, possibly at end of parent subtree
  6690. (org-insert-heading-respect-content
  6691. (if (not eops)
  6692. (progn
  6693. (org-end-of-subtree nil t)
  6694. (and (looking-at "^\\*") (backward-char 1))
  6695. (while (and (not (bobp))
  6696. ;; Don't delete spaces in empty headlines
  6697. (not (looking-back org-outline-regexp))
  6698. (member (char-before) '(?\ ?\t ?\n)))
  6699. (backward-delete-char 1)))
  6700. (let ((p (point)))
  6701. (org-up-heading-safe)
  6702. (if (= p (point))
  6703. (goto-char (point-max))
  6704. (org-end-of-subtree nil t))))
  6705. (when (featurep 'org-inlinetask)
  6706. (while (and (not (eobp))
  6707. (looking-at "\\(\\*+\\)[ \t]+")
  6708. (>= (length (match-string 1))
  6709. org-inlinetask-min-level))
  6710. (org-end-of-subtree nil t)))
  6711. (or (bolp) (newline))
  6712. (or (org-previous-line-empty-p)
  6713. (and blank (newline)))
  6714. (if (or empty-line-p eops) (open-line 1)))
  6715. ;; Insert a headling containing text after point
  6716. ((org-at-heading-p)
  6717. (when hide-previous
  6718. (show-children)
  6719. (org-show-entry))
  6720. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6721. (setq tags (and (match-end 2) (match-string 2)))
  6722. (and (match-end 1)
  6723. (delete-region (match-beginning 1) (match-end 1)))
  6724. (setq pos (point-at-bol))
  6725. (or split (end-of-line 1))
  6726. (delete-horizontal-space)
  6727. (if (string-match "\\`\\*+\\'"
  6728. (buffer-substring (point-at-bol) (point)))
  6729. (insert " "))
  6730. (newline (if blank 2 1))
  6731. (when tags
  6732. (save-excursion
  6733. (goto-char pos)
  6734. (end-of-line 1)
  6735. (insert " " tags)
  6736. (org-set-tags nil 'align))))
  6737. (t
  6738. (or split (end-of-line 1))
  6739. (newline (if blank 2 1))))))
  6740. (insert head) (just-one-space)
  6741. (setq pos (point))
  6742. (end-of-line 1)
  6743. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6744. (when (and org-insert-heading-respect-content hide-previous)
  6745. (save-excursion
  6746. (goto-char previous-pos)
  6747. (hide-subtree)))
  6748. (when (and begn endn)
  6749. (narrow-to-region (min (point) begn) (max (point) endn)))
  6750. (run-hooks 'org-insert-heading-hook))))))
  6751. (defun org-get-heading (&optional no-tags no-todo)
  6752. "Return the heading of the current entry, without the stars.
  6753. When NO-TAGS is non-nil, don't include tags.
  6754. When NO-TODO is non-nil, don't include TODO keywords."
  6755. (save-excursion
  6756. (org-back-to-heading t)
  6757. (cond
  6758. ((and no-tags no-todo)
  6759. (looking-at org-complex-heading-regexp)
  6760. (match-string 4))
  6761. (no-tags
  6762. (looking-at (concat org-outline-regexp
  6763. "\\(.*?\\)"
  6764. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6765. (match-string 1))
  6766. (no-todo
  6767. (looking-at org-todo-line-regexp)
  6768. (match-string 3))
  6769. (t (looking-at org-heading-regexp)
  6770. (match-string 2)))))
  6771. (defvar orgstruct-mode) ; defined below
  6772. (defun org-heading-components ()
  6773. "Return the components of the current heading.
  6774. This is a list with the following elements:
  6775. - the level as an integer
  6776. - the reduced level, different if `org-odd-levels-only' is set.
  6777. - the TODO keyword, or nil
  6778. - the priority character, like ?A, or nil if no priority is given
  6779. - the headline text itself, or the tags string if no headline text
  6780. - the tags string, or nil."
  6781. (save-excursion
  6782. (org-back-to-heading t)
  6783. (if (let (case-fold-search)
  6784. (looking-at
  6785. (if orgstruct-mode
  6786. org-heading-regexp
  6787. org-complex-heading-regexp)))
  6788. (if orgstruct-mode
  6789. (list (length (match-string 1))
  6790. (org-reduced-level (length (match-string 1)))
  6791. nil
  6792. nil
  6793. (match-string 2)
  6794. nil)
  6795. (list (length (match-string 1))
  6796. (org-reduced-level (length (match-string 1)))
  6797. (org-match-string-no-properties 2)
  6798. (and (match-end 3) (aref (match-string 3) 2))
  6799. (org-match-string-no-properties 4)
  6800. (org-match-string-no-properties 5))))))
  6801. (defun org-get-entry ()
  6802. "Get the entry text, after heading, entire subtree."
  6803. (save-excursion
  6804. (org-back-to-heading t)
  6805. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6806. (defun org-insert-heading-after-current ()
  6807. "Insert a new heading with same level as current, after current subtree."
  6808. (interactive)
  6809. (org-back-to-heading)
  6810. (org-insert-heading)
  6811. (org-move-subtree-down)
  6812. (end-of-line 1))
  6813. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6814. "Insert heading with `org-insert-heading-respect-content' set to t."
  6815. (interactive "P")
  6816. (let ((org-insert-heading-respect-content t))
  6817. (org-insert-heading arg invisible-ok)))
  6818. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6819. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6820. (interactive "P")
  6821. (let ((org-insert-heading-respect-content t))
  6822. (org-insert-todo-heading force-state t)))
  6823. (defun org-insert-todo-heading (arg &optional force-heading)
  6824. "Insert a new heading with the same level and TODO state as current heading.
  6825. If the heading has no TODO state, or if the state is DONE, use the first
  6826. state (TODO by default). Also one prefix arg, force first state. With two
  6827. prefix args, force inserting at the end of the parent subtree."
  6828. (interactive "P")
  6829. (when (or force-heading (not (org-insert-item 'checkbox)))
  6830. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6831. force-heading))
  6832. (save-excursion
  6833. (org-back-to-heading)
  6834. (outline-previous-heading)
  6835. (looking-at org-todo-line-regexp))
  6836. (let*
  6837. ((new-mark-x
  6838. (if (or arg
  6839. (not (match-beginning 2))
  6840. (member (match-string 2) org-done-keywords))
  6841. (car org-todo-keywords-1)
  6842. (match-string 2)))
  6843. (new-mark
  6844. (or
  6845. (run-hook-with-args-until-success
  6846. 'org-todo-get-default-hook new-mark-x nil)
  6847. new-mark-x)))
  6848. (beginning-of-line 1)
  6849. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6850. (if org-treat-insert-todo-heading-as-state-change
  6851. (org-todo new-mark)
  6852. (insert new-mark " "))))
  6853. (when org-provide-todo-statistics
  6854. (org-update-parent-todo-statistics))))
  6855. (defun org-insert-subheading (arg)
  6856. "Insert a new subheading and demote it.
  6857. Works for outline headings and for plain lists alike."
  6858. (interactive "P")
  6859. (org-insert-heading arg)
  6860. (cond
  6861. ((org-at-heading-p) (org-do-demote))
  6862. ((org-at-item-p) (org-indent-item))))
  6863. (defun org-insert-todo-subheading (arg)
  6864. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6865. Works for outline headings and for plain lists alike."
  6866. (interactive "P")
  6867. (org-insert-todo-heading arg)
  6868. (cond
  6869. ((org-at-heading-p) (org-do-demote))
  6870. ((org-at-item-p) (org-indent-item))))
  6871. ;;; Promotion and Demotion
  6872. (defvar org-after-demote-entry-hook nil
  6873. "Hook run after an entry has been demoted.
  6874. The cursor will be at the beginning of the entry.
  6875. When a subtree is being demoted, the hook will be called for each node.")
  6876. (defvar org-after-promote-entry-hook nil
  6877. "Hook run after an entry has been promoted.
  6878. The cursor will be at the beginning of the entry.
  6879. When a subtree is being promoted, the hook will be called for each node.")
  6880. (defun org-promote-subtree ()
  6881. "Promote the entire subtree.
  6882. See also `org-promote'."
  6883. (interactive)
  6884. (save-excursion
  6885. (org-with-limited-levels (org-map-tree 'org-promote)))
  6886. (org-fix-position-after-promote))
  6887. (defun org-demote-subtree ()
  6888. "Demote the entire subtree. See `org-demote'.
  6889. See also `org-promote'."
  6890. (interactive)
  6891. (save-excursion
  6892. (org-with-limited-levels (org-map-tree 'org-demote)))
  6893. (org-fix-position-after-promote))
  6894. (defun org-do-promote ()
  6895. "Promote the current heading higher up the tree.
  6896. If the region is active in `transient-mark-mode', promote all headings
  6897. in the region."
  6898. (interactive)
  6899. (save-excursion
  6900. (if (org-region-active-p)
  6901. (org-map-region 'org-promote (region-beginning) (region-end))
  6902. (org-promote)))
  6903. (org-fix-position-after-promote))
  6904. (defun org-do-demote ()
  6905. "Demote the current heading lower down the tree.
  6906. If the region is active in `transient-mark-mode', demote all headings
  6907. in the region."
  6908. (interactive)
  6909. (save-excursion
  6910. (if (org-region-active-p)
  6911. (org-map-region 'org-demote (region-beginning) (region-end))
  6912. (org-demote)))
  6913. (org-fix-position-after-promote))
  6914. (defun org-fix-position-after-promote ()
  6915. "Make sure that after pro/demotion cursor position is right."
  6916. (let ((pos (point)))
  6917. (when (save-excursion
  6918. (beginning-of-line 1)
  6919. (looking-at org-todo-line-regexp)
  6920. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6921. (cond ((eobp) (insert " "))
  6922. ((eolp) (insert " "))
  6923. ((equal (char-after) ?\ ) (forward-char 1))))))
  6924. (defun org-current-level ()
  6925. "Return the level of the current entry, or nil if before the first headline.
  6926. The level is the number of stars at the beginning of the headline."
  6927. (save-excursion
  6928. (org-with-limited-levels
  6929. (if (ignore-errors (org-back-to-heading t))
  6930. (funcall outline-level)))))
  6931. (defun org-get-previous-line-level ()
  6932. "Return the outline depth of the last headline before the current line.
  6933. Returns 0 for the first headline in the buffer, and nil if before the
  6934. first headline."
  6935. (let ((current-level (org-current-level))
  6936. (prev-level (when (> (line-number-at-pos) 1)
  6937. (save-excursion
  6938. (beginning-of-line 0)
  6939. (org-current-level)))))
  6940. (cond ((null current-level) nil) ; Before first headline
  6941. ((null prev-level) 0) ; At first headline
  6942. (prev-level))))
  6943. (defun org-reduced-level (l)
  6944. "Compute the effective level of a heading.
  6945. This takes into account the setting of `org-odd-levels-only'."
  6946. (cond
  6947. ((zerop l) 0)
  6948. (org-odd-levels-only (1+ (floor (/ l 2))))
  6949. (t l)))
  6950. (defun org-level-increment ()
  6951. "Return the number of stars that will be added or removed at a
  6952. time to headlines when structure editing, based on the value of
  6953. `org-odd-levels-only'."
  6954. (if org-odd-levels-only 2 1))
  6955. (defun org-get-valid-level (level &optional change)
  6956. "Rectify a level change under the influence of `org-odd-levels-only'
  6957. LEVEL is a current level, CHANGE is by how much the level should be
  6958. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6959. even level numbers will become the next higher odd number."
  6960. (if org-odd-levels-only
  6961. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6962. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6963. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6964. (max 1 (+ level (or change 0)))))
  6965. (if (boundp 'define-obsolete-function-alias)
  6966. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6967. (define-obsolete-function-alias 'org-get-legal-level
  6968. 'org-get-valid-level)
  6969. (define-obsolete-function-alias 'org-get-legal-level
  6970. 'org-get-valid-level "23.1")))
  6971. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6972. ;; ̀org-with-limited-levels'
  6973. (defun org-promote ()
  6974. "Promote the current heading higher up the tree.
  6975. If the region is active in `transient-mark-mode', promote all headings
  6976. in the region."
  6977. (org-back-to-heading t)
  6978. (let* ((level (save-match-data (funcall outline-level)))
  6979. (after-change-functions (remove 'flyspell-after-change-function
  6980. after-change-functions))
  6981. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6982. (diff (abs (- level (length up-head) -1))))
  6983. (cond ((and (= level 1) org-called-with-limited-levels
  6984. org-allow-promoting-top-level-subtree)
  6985. (replace-match "# " nil t))
  6986. ((= level 1)
  6987. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6988. (t (replace-match up-head nil t)))
  6989. ;; Fixup tag positioning
  6990. (unless (= level 1)
  6991. (and org-auto-align-tags (org-set-tags nil t))
  6992. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6993. (run-hooks 'org-after-promote-entry-hook)))
  6994. (defun org-demote ()
  6995. "Demote the current heading lower down the tree.
  6996. If the region is active in `transient-mark-mode', demote all headings
  6997. in the region."
  6998. (org-back-to-heading t)
  6999. (let* ((level (save-match-data (funcall outline-level)))
  7000. (after-change-functions (remove 'flyspell-after-change-function
  7001. after-change-functions))
  7002. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7003. (diff (abs (- level (length down-head) -1))))
  7004. (replace-match down-head nil t)
  7005. ;; Fixup tag positioning
  7006. (and org-auto-align-tags (org-set-tags nil t))
  7007. (if org-adapt-indentation (org-fixup-indentation diff))
  7008. (run-hooks 'org-after-demote-entry-hook)))
  7009. (defun org-cycle-level ()
  7010. "Cycle the level of an empty headline through possible states.
  7011. This goes first to child, then to parent, level, then up the hierarchy.
  7012. After top level, it switches back to sibling level."
  7013. (interactive)
  7014. (let ((org-adapt-indentation nil))
  7015. (when (org-point-at-end-of-empty-headline)
  7016. (setq this-command 'org-cycle-level) ; Only needed for caching
  7017. (let ((cur-level (org-current-level))
  7018. (prev-level (org-get-previous-line-level)))
  7019. (cond
  7020. ;; If first headline in file, promote to top-level.
  7021. ((= prev-level 0)
  7022. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7023. do (org-do-promote)))
  7024. ;; If same level as prev, demote one.
  7025. ((= prev-level cur-level)
  7026. (org-do-demote))
  7027. ;; If parent is top-level, promote to top level if not already.
  7028. ((= prev-level 1)
  7029. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7030. do (org-do-promote)))
  7031. ;; If top-level, return to prev-level.
  7032. ((= cur-level 1)
  7033. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7034. do (org-do-demote)))
  7035. ;; If less than prev-level, promote one.
  7036. ((< cur-level prev-level)
  7037. (org-do-promote))
  7038. ;; If deeper than prev-level, promote until higher than
  7039. ;; prev-level.
  7040. ((> cur-level prev-level)
  7041. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7042. do (org-do-promote))))
  7043. t))))
  7044. (defun org-map-tree (fun)
  7045. "Call FUN for every heading underneath the current one."
  7046. (org-back-to-heading)
  7047. (let ((level (funcall outline-level)))
  7048. (save-excursion
  7049. (funcall fun)
  7050. (while (and (progn
  7051. (outline-next-heading)
  7052. (> (funcall outline-level) level))
  7053. (not (eobp)))
  7054. (funcall fun)))))
  7055. (defun org-map-region (fun beg end)
  7056. "Call FUN for every heading between BEG and END."
  7057. (let ((org-ignore-region t))
  7058. (save-excursion
  7059. (setq end (copy-marker end))
  7060. (goto-char beg)
  7061. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7062. (< (point) end))
  7063. (funcall fun))
  7064. (while (and (progn
  7065. (outline-next-heading)
  7066. (< (point) end))
  7067. (not (eobp)))
  7068. (funcall fun)))))
  7069. (defvar org-property-end-re) ; silence byte-compiler
  7070. (defun org-fixup-indentation (diff)
  7071. "Change the indentation in the current entry by DIFF.
  7072. However, if any line in the current entry has no indentation, or if it
  7073. would end up with no indentation after the change, nothing at all is done."
  7074. (save-excursion
  7075. (let ((end (save-excursion (outline-next-heading)
  7076. (point-marker)))
  7077. (prohibit (if (> diff 0)
  7078. "^\\S-"
  7079. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7080. col)
  7081. (unless (save-excursion (end-of-line 1)
  7082. (re-search-forward prohibit end t))
  7083. (while (and (< (point) end)
  7084. (re-search-forward "^[ \t]+" end t))
  7085. (goto-char (match-end 0))
  7086. (setq col (current-column))
  7087. (if (< diff 0) (replace-match ""))
  7088. (org-indent-to-column (+ diff col))))
  7089. (move-marker end nil))))
  7090. (defun org-convert-to-odd-levels ()
  7091. "Convert an org-mode file with all levels allowed to one with odd levels.
  7092. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7093. level 5 etc."
  7094. (interactive)
  7095. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7096. (let ((outline-level 'org-outline-level)
  7097. (org-odd-levels-only nil) n)
  7098. (save-excursion
  7099. (goto-char (point-min))
  7100. (while (re-search-forward "^\\*\\*+ " nil t)
  7101. (setq n (- (length (match-string 0)) 2))
  7102. (while (>= (setq n (1- n)) 0)
  7103. (org-demote))
  7104. (end-of-line 1))))))
  7105. (defun org-convert-to-oddeven-levels ()
  7106. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7107. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7108. file contains a section with an even level, conversion would
  7109. destroy the structure of the file. An error is signaled in this
  7110. case."
  7111. (interactive)
  7112. (goto-char (point-min))
  7113. ;; First check if there are no even levels
  7114. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7115. (org-show-context t)
  7116. (error "Not all levels are odd in this file. Conversion not possible"))
  7117. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7118. (let ((outline-regexp org-outline-regexp)
  7119. (outline-level 'org-outline-level)
  7120. (org-odd-levels-only nil) n)
  7121. (save-excursion
  7122. (goto-char (point-min))
  7123. (while (re-search-forward "^\\*\\*+ " nil t)
  7124. (setq n (/ (1- (length (match-string 0))) 2))
  7125. (while (>= (setq n (1- n)) 0)
  7126. (org-promote))
  7127. (end-of-line 1))))))
  7128. (defun org-tr-level (n)
  7129. "Make N odd if required."
  7130. (if org-odd-levels-only (1+ (/ n 2)) n))
  7131. ;;; Vertical tree motion, cutting and pasting of subtrees
  7132. (defun org-move-subtree-up (&optional arg)
  7133. "Move the current subtree up past ARG headlines of the same level."
  7134. (interactive "p")
  7135. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7136. (defun org-move-subtree-down (&optional arg)
  7137. "Move the current subtree down past ARG headlines of the same level."
  7138. (interactive "p")
  7139. (setq arg (prefix-numeric-value arg))
  7140. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7141. 'org-get-last-sibling))
  7142. (ins-point (make-marker))
  7143. (cnt (abs arg))
  7144. (col (current-column))
  7145. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7146. ;; Select the tree
  7147. (org-back-to-heading)
  7148. (setq beg0 (point))
  7149. (save-excursion
  7150. (setq ne-beg (org-back-over-empty-lines))
  7151. (setq beg (point)))
  7152. (save-match-data
  7153. (save-excursion (outline-end-of-heading)
  7154. (setq folded (outline-invisible-p)))
  7155. (outline-end-of-subtree))
  7156. (outline-next-heading)
  7157. (setq ne-end (org-back-over-empty-lines))
  7158. (setq end (point))
  7159. (goto-char beg0)
  7160. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7161. ;; include less whitespace
  7162. (save-excursion
  7163. (goto-char beg)
  7164. (forward-line (- ne-beg ne-end))
  7165. (setq beg (point))))
  7166. ;; Find insertion point, with error handling
  7167. (while (> cnt 0)
  7168. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7169. (progn (goto-char beg0)
  7170. (user-error "Cannot move past superior level or buffer limit")))
  7171. (setq cnt (1- cnt)))
  7172. (if (> arg 0)
  7173. ;; Moving forward - still need to move over subtree
  7174. (progn (org-end-of-subtree t t)
  7175. (save-excursion
  7176. (org-back-over-empty-lines)
  7177. (or (bolp) (newline)))))
  7178. (setq ne-ins (org-back-over-empty-lines))
  7179. (move-marker ins-point (point))
  7180. (setq txt (buffer-substring beg end))
  7181. (org-save-markers-in-region beg end)
  7182. (delete-region beg end)
  7183. (org-remove-empty-overlays-at beg)
  7184. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7185. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7186. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7187. (let ((bbb (point)))
  7188. (insert-before-markers txt)
  7189. (org-reinstall-markers-in-region bbb)
  7190. (move-marker ins-point bbb))
  7191. (or (bolp) (insert "\n"))
  7192. (setq ins-end (point))
  7193. (goto-char ins-point)
  7194. (org-skip-whitespace)
  7195. (when (and (< arg 0)
  7196. (org-first-sibling-p)
  7197. (> ne-ins ne-beg))
  7198. ;; Move whitespace back to beginning
  7199. (save-excursion
  7200. (goto-char ins-end)
  7201. (let ((kill-whole-line t))
  7202. (kill-line (- ne-ins ne-beg)) (point)))
  7203. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7204. (move-marker ins-point nil)
  7205. (if folded
  7206. (hide-subtree)
  7207. (org-show-entry)
  7208. (show-children)
  7209. (org-cycle-hide-drawers 'children))
  7210. (org-clean-visibility-after-subtree-move)
  7211. ;; move back to the initial column we were at
  7212. (move-to-column col)))
  7213. (defvar org-subtree-clip ""
  7214. "Clipboard for cut and paste of subtrees.
  7215. This is actually only a copy of the kill, because we use the normal kill
  7216. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7217. (defvar org-subtree-clip-folded nil
  7218. "Was the last copied subtree folded?
  7219. This is used to fold the tree back after pasting.")
  7220. (defun org-cut-subtree (&optional n)
  7221. "Cut the current subtree into the clipboard.
  7222. With prefix arg N, cut this many sequential subtrees.
  7223. This is a short-hand for marking the subtree and then cutting it."
  7224. (interactive "p")
  7225. (org-copy-subtree n 'cut))
  7226. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7227. "Cut the current subtree into the clipboard.
  7228. With prefix arg N, cut this many sequential subtrees.
  7229. This is a short-hand for marking the subtree and then copying it.
  7230. If CUT is non-nil, actually cut the subtree.
  7231. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7232. of some markers in the region, even if CUT is non-nil. This is
  7233. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7234. (interactive "p")
  7235. (let (beg end folded (beg0 (point)))
  7236. (if (org-called-interactively-p 'any)
  7237. (org-back-to-heading nil) ; take what looks like a subtree
  7238. (org-back-to-heading t)) ; take what is really there
  7239. (setq beg (point))
  7240. (skip-chars-forward " \t\r\n")
  7241. (save-match-data
  7242. (if nosubtrees
  7243. (outline-next-heading)
  7244. (save-excursion (outline-end-of-heading)
  7245. (setq folded (outline-invisible-p)))
  7246. (condition-case nil
  7247. (org-forward-heading-same-level (1- n) t)
  7248. (error nil))
  7249. (org-end-of-subtree t t)))
  7250. (setq end (point))
  7251. (goto-char beg0)
  7252. (when (> end beg)
  7253. (setq org-subtree-clip-folded folded)
  7254. (when (or cut force-store-markers)
  7255. (org-save-markers-in-region beg end))
  7256. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7257. (setq org-subtree-clip (current-kill 0))
  7258. (message "%s: Subtree(s) with %d characters"
  7259. (if cut "Cut" "Copied")
  7260. (length org-subtree-clip)))))
  7261. (defun org-paste-subtree (&optional level tree for-yank)
  7262. "Paste the clipboard as a subtree, with modification of headline level.
  7263. The entire subtree is promoted or demoted in order to match a new headline
  7264. level.
  7265. If the cursor is at the beginning of a headline, the same level as
  7266. that headline is used to paste the tree.
  7267. If not, the new level is derived from the *visible* headings
  7268. before and after the insertion point, and taken to be the inferior headline
  7269. level of the two. So if the previous visible heading is level 3 and the
  7270. next is level 4 (or vice versa), level 4 will be used for insertion.
  7271. This makes sure that the subtree remains an independent subtree and does
  7272. not swallow low level entries.
  7273. You can also force a different level, either by using a numeric prefix
  7274. argument, or by inserting the heading marker by hand. For example, if the
  7275. cursor is after \"*****\", then the tree will be shifted to level 5.
  7276. If optional TREE is given, use this text instead of the kill ring.
  7277. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7278. move back over whitespace before inserting, and move point to the end of
  7279. the inserted text when done."
  7280. (interactive "P")
  7281. (setq tree (or tree (and kill-ring (current-kill 0))))
  7282. (unless (org-kill-is-subtree-p tree)
  7283. (error "%s"
  7284. (substitute-command-keys
  7285. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7286. (org-with-limited-levels
  7287. (let* ((visp (not (outline-invisible-p)))
  7288. (txt tree)
  7289. (^re_ "\\(\\*+\\)[ \t]*")
  7290. (old-level (if (string-match org-outline-regexp-bol txt)
  7291. (- (match-end 0) (match-beginning 0) 1)
  7292. -1))
  7293. (force-level (cond (level (prefix-numeric-value level))
  7294. ((and (looking-at "[ \t]*$")
  7295. (string-match
  7296. "^\\*+$" (buffer-substring
  7297. (point-at-bol) (point))))
  7298. (- (match-end 1) (match-beginning 1)))
  7299. ((and (bolp)
  7300. (looking-at org-outline-regexp))
  7301. (- (match-end 0) (point) 1))))
  7302. (previous-level (save-excursion
  7303. (condition-case nil
  7304. (progn
  7305. (outline-previous-visible-heading 1)
  7306. (if (looking-at ^re_)
  7307. (- (match-end 0) (match-beginning 0) 1)
  7308. 1))
  7309. (error 1))))
  7310. (next-level (save-excursion
  7311. (condition-case nil
  7312. (progn
  7313. (or (looking-at org-outline-regexp)
  7314. (outline-next-visible-heading 1))
  7315. (if (looking-at ^re_)
  7316. (- (match-end 0) (match-beginning 0) 1)
  7317. 1))
  7318. (error 1))))
  7319. (new-level (or force-level (max previous-level next-level)))
  7320. (shift (if (or (= old-level -1)
  7321. (= new-level -1)
  7322. (= old-level new-level))
  7323. 0
  7324. (- new-level old-level)))
  7325. (delta (if (> shift 0) -1 1))
  7326. (func (if (> shift 0) 'org-demote 'org-promote))
  7327. (org-odd-levels-only nil)
  7328. beg end newend)
  7329. ;; Remove the forced level indicator
  7330. (if force-level
  7331. (delete-region (point-at-bol) (point)))
  7332. ;; Paste
  7333. (beginning-of-line (if (bolp) 1 2))
  7334. (setq beg (point))
  7335. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7336. (insert-before-markers txt)
  7337. (unless (string-match "\n\\'" txt) (insert "\n"))
  7338. (setq newend (point))
  7339. (org-reinstall-markers-in-region beg)
  7340. (setq end (point))
  7341. (goto-char beg)
  7342. (skip-chars-forward " \t\n\r")
  7343. (setq beg (point))
  7344. (if (and (outline-invisible-p) visp)
  7345. (save-excursion (outline-show-heading)))
  7346. ;; Shift if necessary
  7347. (unless (= shift 0)
  7348. (save-restriction
  7349. (narrow-to-region beg end)
  7350. (while (not (= shift 0))
  7351. (org-map-region func (point-min) (point-max))
  7352. (setq shift (+ delta shift)))
  7353. (goto-char (point-min))
  7354. (setq newend (point-max))))
  7355. (when (or (org-called-interactively-p 'interactive) for-yank)
  7356. (message "Clipboard pasted as level %d subtree" new-level))
  7357. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7358. kill-ring
  7359. (eq org-subtree-clip (current-kill 0))
  7360. org-subtree-clip-folded)
  7361. ;; The tree was folded before it was killed/copied
  7362. (hide-subtree))
  7363. (and for-yank (goto-char newend)))))
  7364. (defun org-kill-is-subtree-p (&optional txt)
  7365. "Check if the current kill is an outline subtree, or a set of trees.
  7366. Returns nil if kill does not start with a headline, or if the first
  7367. headline level is not the largest headline level in the tree.
  7368. So this will actually accept several entries of equal levels as well,
  7369. which is OK for `org-paste-subtree'.
  7370. If optional TXT is given, check this string instead of the current kill."
  7371. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7372. (re (org-get-limited-outline-regexp))
  7373. (^re (concat "^" re))
  7374. (start-level (and kill
  7375. (string-match
  7376. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7377. kill)
  7378. (- (match-end 2) (match-beginning 2) 1)))
  7379. (start (1+ (or (match-beginning 2) -1))))
  7380. (if (not start-level)
  7381. (progn
  7382. nil) ;; does not even start with a heading
  7383. (catch 'exit
  7384. (while (setq start (string-match ^re kill (1+ start)))
  7385. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7386. (throw 'exit nil)))
  7387. t))))
  7388. (defvar org-markers-to-move nil
  7389. "Markers that should be moved with a cut-and-paste operation.
  7390. Those markers are stored together with their positions relative to
  7391. the start of the region.")
  7392. (defun org-save-markers-in-region (beg end)
  7393. "Check markers in region.
  7394. If these markers are between BEG and END, record their position relative
  7395. to BEG, so that after moving the block of text, we can put the markers back
  7396. into place.
  7397. This function gets called just before an entry or tree gets cut from the
  7398. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7399. called immediately, to move the markers with the entries."
  7400. (setq org-markers-to-move nil)
  7401. (when (featurep 'org-clock)
  7402. (org-clock-save-markers-for-cut-and-paste beg end))
  7403. (when (featurep 'org-agenda)
  7404. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7405. (defun org-check-and-save-marker (marker beg end)
  7406. "Check if MARKER is between BEG and END.
  7407. If yes, remember the marker and the distance to BEG."
  7408. (when (and (marker-buffer marker)
  7409. (equal (marker-buffer marker) (current-buffer)))
  7410. (if (and (>= marker beg) (< marker end))
  7411. (push (cons marker (- marker beg)) org-markers-to-move))))
  7412. (defun org-reinstall-markers-in-region (beg)
  7413. "Move all remembered markers to their position relative to BEG."
  7414. (mapc (lambda (x)
  7415. (move-marker (car x) (+ beg (cdr x))))
  7416. org-markers-to-move)
  7417. (setq org-markers-to-move nil))
  7418. (defun org-narrow-to-subtree ()
  7419. "Narrow buffer to the current subtree."
  7420. (interactive)
  7421. (save-excursion
  7422. (save-match-data
  7423. (org-with-limited-levels
  7424. (narrow-to-region
  7425. (progn (org-back-to-heading t) (point))
  7426. (progn (org-end-of-subtree t t)
  7427. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7428. (point)))))))
  7429. (defun org-narrow-to-block ()
  7430. "Narrow buffer to the current block."
  7431. (interactive)
  7432. (let* ((case-fold-search t)
  7433. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7434. "^[ \t]*#\\+end_.*")))
  7435. (if blockp
  7436. (narrow-to-region (car blockp) (cdr blockp))
  7437. (error "Not in a block"))))
  7438. (eval-when-compile
  7439. (defvar org-property-drawer-re))
  7440. (defvar org-property-start-re) ;; defined below
  7441. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7442. "Clone the task (subtree) at point N times.
  7443. The clones will be inserted as siblings.
  7444. In interactive use, the user will be prompted for the number of
  7445. clones to be produced. When called with a universal prefix argument
  7446. and if the entry has a timestamp, the user will also be prompted for
  7447. a time shift, which may be a repeater as used in time stamps, for
  7448. example `+3d'.
  7449. When a valid repeater is given and the entry contains any time
  7450. stamps, the clones will become a sequence in time, with time
  7451. stamps in the subtree shifted for each clone produced. If SHIFT
  7452. is nil or the empty string, time stamps will be left alone. The
  7453. ID property of the original subtree is removed.
  7454. If the original subtree did contain time stamps with a repeater,
  7455. the following will happen:
  7456. - the repeater will be removed in each clone
  7457. - an additional clone will be produced, with the current, unshifted
  7458. date(s) in the entry.
  7459. - the original entry will be placed *after* all the clones, with
  7460. repeater intact.
  7461. - the start days in the repeater in the original entry will be shifted
  7462. to past the last clone.
  7463. In this way you can spell out a number of instances of a repeating task,
  7464. and still retain the repeater to cover future instances of the task."
  7465. (interactive "nNumber of clones to produce: ")
  7466. (let ((shift
  7467. (or shift
  7468. (if (and (equal current-prefix-arg '(4))
  7469. (save-excursion
  7470. (re-search-forward org-ts-regexp-both
  7471. (save-excursion
  7472. (org-end-of-subtree t)
  7473. (point)) t)))
  7474. (read-from-minibuffer
  7475. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7476. ""))) ;; No time shift
  7477. (n-no-remove -1)
  7478. (drawer-re org-drawer-regexp)
  7479. beg end template task idprop
  7480. shift-n shift-what doshift nmin nmax)
  7481. (if (not (and (integerp n) (> n 0)))
  7482. (error "Invalid number of replications %s" n))
  7483. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7484. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7485. shift)))
  7486. (error "Invalid shift specification %s" shift))
  7487. (when doshift
  7488. (setq shift-n (string-to-number (match-string 1 shift))
  7489. shift-what (cdr (assoc (match-string 2 shift)
  7490. '(("d" . day) ("w" . week)
  7491. ("m" . month) ("y" . year))))))
  7492. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7493. (setq nmin 1 nmax n)
  7494. (org-back-to-heading t)
  7495. (setq beg (point))
  7496. (setq idprop (org-entry-get nil "ID"))
  7497. (org-end-of-subtree t t)
  7498. (or (bolp) (insert "\n"))
  7499. (setq end (point))
  7500. (setq template (buffer-substring beg end))
  7501. (when (and doshift
  7502. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7503. (delete-region beg end)
  7504. (setq end beg)
  7505. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7506. (goto-char end)
  7507. (loop for n from nmin to nmax do
  7508. ;; prepare clone
  7509. (with-temp-buffer
  7510. (insert template)
  7511. (org-mode)
  7512. (goto-char (point-min))
  7513. (org-show-subtree)
  7514. (and idprop (if org-clone-delete-id
  7515. (org-entry-delete nil "ID")
  7516. (org-id-get-create t)))
  7517. (unless (= n 0)
  7518. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7519. (kill-whole-line))
  7520. (goto-char (point-min))
  7521. (while (re-search-forward drawer-re nil t)
  7522. (mapc (lambda (d)
  7523. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7524. (goto-char (point-min))
  7525. (when doshift
  7526. (while (re-search-forward org-ts-regexp-both nil t)
  7527. (org-timestamp-change (* n shift-n) shift-what))
  7528. (unless (= n n-no-remove)
  7529. (goto-char (point-min))
  7530. (while (re-search-forward org-ts-regexp nil t)
  7531. (save-excursion
  7532. (goto-char (match-beginning 0))
  7533. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7534. (delete-region (match-beginning 1) (match-end 1)))))))
  7535. (setq task (buffer-string)))
  7536. (insert task))
  7537. (goto-char beg)))
  7538. ;;; Outline Sorting
  7539. (defun org-sort (with-case)
  7540. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7541. Optional argument WITH-CASE means sort case-sensitively."
  7542. (interactive "P")
  7543. (cond
  7544. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7545. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7546. (t
  7547. (org-call-with-arg 'org-sort-entries with-case))))
  7548. (defun org-sort-remove-invisible (s)
  7549. "Remove invisible links from string S."
  7550. (remove-text-properties 0 (length s) org-rm-props s)
  7551. (while (string-match org-bracket-link-regexp s)
  7552. (setq s (replace-match (if (match-end 2)
  7553. (match-string 3 s)
  7554. (match-string 1 s)) t t s)))
  7555. (let ((st (format " %s " s)))
  7556. (while (string-match org-emph-re st)
  7557. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7558. (setq s (substring st 1 -1)))
  7559. s)
  7560. (defvar org-priority-regexp) ; defined later in the file
  7561. (defvar org-after-sorting-entries-or-items-hook nil
  7562. "Hook that is run after a bunch of entries or items have been sorted.
  7563. When children are sorted, the cursor is in the parent line when this
  7564. hook gets called. When a region or a plain list is sorted, the cursor
  7565. will be in the first entry of the sorted region/list.")
  7566. (defun org-sort-entries
  7567. (&optional with-case sorting-type getkey-func compare-func property)
  7568. "Sort entries on a certain level of an outline tree.
  7569. If there is an active region, the entries in the region are sorted.
  7570. Else, if the cursor is before the first entry, sort the top-level items.
  7571. Else, the children of the entry at point are sorted.
  7572. Sorting can be alphabetically, numerically, by date/time as given by
  7573. a time stamp, by a property, by priority order, or by a custom function.
  7574. The command prompts for the sorting type unless it has been given to the
  7575. function through the SORTING-TYPE argument, which needs to be a character,
  7576. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7577. precise meaning of each character:
  7578. n Numerically, by converting the beginning of the entry/item to a number.
  7579. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7580. o By order of TODO keywords.
  7581. t By date/time, either the first active time stamp in the entry, or, if
  7582. none exist, by the first inactive one.
  7583. s By the scheduled date/time.
  7584. d By deadline date/time.
  7585. c By creation time, which is assumed to be the first inactive time stamp
  7586. at the beginning of a line.
  7587. p By priority according to the cookie.
  7588. r By the value of a property.
  7589. Capital letters will reverse the sort order.
  7590. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7591. called with point at the beginning of the record. It must return either
  7592. a string or a number that should serve as the sorting key for that record.
  7593. Comparing entries ignores case by default. However, with an optional argument
  7594. WITH-CASE, the sorting considers case as well.
  7595. Sorting is done against the visible part of the headlines, it ignores hidden
  7596. links."
  7597. (interactive "P")
  7598. (let ((case-func (if with-case 'identity 'downcase))
  7599. (cmstr
  7600. ;; The clock marker is lost when using `sort-subr', let's
  7601. ;; store the clocking string.
  7602. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7603. (save-excursion
  7604. (goto-char org-clock-marker)
  7605. (looking-back "^.*") (match-string-no-properties 0))))
  7606. start beg end stars re re2
  7607. txt what tmp)
  7608. ;; Find beginning and end of region to sort
  7609. (cond
  7610. ((org-region-active-p)
  7611. ;; we will sort the region
  7612. (setq end (region-end)
  7613. what "region")
  7614. (goto-char (region-beginning))
  7615. (if (not (org-at-heading-p)) (outline-next-heading))
  7616. (setq start (point)))
  7617. ((or (org-at-heading-p)
  7618. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7619. ;; we will sort the children of the current headline
  7620. (org-back-to-heading)
  7621. (setq start (point)
  7622. end (progn (org-end-of-subtree t t)
  7623. (or (bolp) (insert "\n"))
  7624. (org-back-over-empty-lines)
  7625. (point))
  7626. what "children")
  7627. (goto-char start)
  7628. (show-subtree)
  7629. (outline-next-heading))
  7630. (t
  7631. ;; we will sort the top-level entries in this file
  7632. (goto-char (point-min))
  7633. (or (org-at-heading-p) (outline-next-heading))
  7634. (setq start (point))
  7635. (goto-char (point-max))
  7636. (beginning-of-line 1)
  7637. (when (looking-at ".*?\\S-")
  7638. ;; File ends in a non-white line
  7639. (end-of-line 1)
  7640. (insert "\n"))
  7641. (setq end (point-max))
  7642. (setq what "top-level")
  7643. (goto-char start)
  7644. (show-all)))
  7645. (setq beg (point))
  7646. (if (>= beg end) (error "Nothing to sort"))
  7647. (looking-at "\\(\\*+\\)")
  7648. (setq stars (match-string 1)
  7649. re (concat "^" (regexp-quote stars) " +")
  7650. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7651. txt (buffer-substring beg end))
  7652. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7653. (if (and (not (equal stars "*")) (string-match re2 txt))
  7654. (error "Region to sort contains a level above the first entry"))
  7655. (unless sorting-type
  7656. (message
  7657. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7658. [t]ime [s]cheduled [d]eadline [c]reated
  7659. A/N/P/R/O/F/T/S/D/C means reversed:"
  7660. what)
  7661. (setq sorting-type (read-char-exclusive))
  7662. (unless getkey-func
  7663. (and (= (downcase sorting-type) ?f)
  7664. (setq getkey-func
  7665. (org-icompleting-read "Sort using function: "
  7666. obarray 'fboundp t nil nil))
  7667. (setq getkey-func (intern getkey-func))))
  7668. (and (= (downcase sorting-type) ?r)
  7669. (not property)
  7670. (setq property
  7671. (org-icompleting-read "Property: "
  7672. (mapcar 'list (org-buffer-property-keys t))
  7673. nil t))))
  7674. (message "Sorting entries...")
  7675. (save-restriction
  7676. (narrow-to-region start end)
  7677. (let ((dcst (downcase sorting-type))
  7678. (case-fold-search nil)
  7679. (now (current-time)))
  7680. (sort-subr
  7681. (/= dcst sorting-type)
  7682. ;; This function moves to the beginning character of the "record" to
  7683. ;; be sorted.
  7684. (lambda nil
  7685. (if (re-search-forward re nil t)
  7686. (goto-char (match-beginning 0))
  7687. (goto-char (point-max))))
  7688. ;; This function moves to the last character of the "record" being
  7689. ;; sorted.
  7690. (lambda nil
  7691. (save-match-data
  7692. (condition-case nil
  7693. (outline-forward-same-level 1)
  7694. (error
  7695. (goto-char (point-max))))))
  7696. ;; This function returns the value that gets sorted against.
  7697. (lambda nil
  7698. (cond
  7699. ((= dcst ?n)
  7700. (if (looking-at org-complex-heading-regexp)
  7701. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7702. nil))
  7703. ((= dcst ?a)
  7704. (if (looking-at org-complex-heading-regexp)
  7705. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7706. nil))
  7707. ((= dcst ?t)
  7708. (let ((end (save-excursion (outline-next-heading) (point))))
  7709. (if (or (re-search-forward org-ts-regexp end t)
  7710. (re-search-forward org-ts-regexp-both end t))
  7711. (org-time-string-to-seconds (match-string 0))
  7712. (org-float-time now))))
  7713. ((= dcst ?c)
  7714. (let ((end (save-excursion (outline-next-heading) (point))))
  7715. (if (re-search-forward
  7716. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7717. end t)
  7718. (org-time-string-to-seconds (match-string 0))
  7719. (org-float-time now))))
  7720. ((= dcst ?s)
  7721. (let ((end (save-excursion (outline-next-heading) (point))))
  7722. (if (re-search-forward org-scheduled-time-regexp end t)
  7723. (org-time-string-to-seconds (match-string 1))
  7724. (org-float-time now))))
  7725. ((= dcst ?d)
  7726. (let ((end (save-excursion (outline-next-heading) (point))))
  7727. (if (re-search-forward org-deadline-time-regexp end t)
  7728. (org-time-string-to-seconds (match-string 1))
  7729. (org-float-time now))))
  7730. ((= dcst ?p)
  7731. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7732. (string-to-char (match-string 2))
  7733. org-default-priority))
  7734. ((= dcst ?r)
  7735. (or (org-entry-get nil property) ""))
  7736. ((= dcst ?o)
  7737. (if (looking-at org-complex-heading-regexp)
  7738. (- 9999 (length (member (match-string 2)
  7739. org-todo-keywords-1)))))
  7740. ((= dcst ?f)
  7741. (if getkey-func
  7742. (progn
  7743. (setq tmp (funcall getkey-func))
  7744. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7745. tmp)
  7746. (error "Invalid key function `%s'" getkey-func)))
  7747. (t (error "Invalid sorting type `%c'" sorting-type))))
  7748. nil
  7749. (cond
  7750. ((= dcst ?a) 'string<)
  7751. ((= dcst ?f) compare-func)
  7752. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7753. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7754. ;; Reset the clock marker if needed
  7755. (when cmstr
  7756. (save-excursion
  7757. (goto-char start)
  7758. (search-forward cmstr nil t)
  7759. (move-marker org-clock-marker (point))))
  7760. (message "Sorting entries...done")))
  7761. (defun org-do-sort (table what &optional with-case sorting-type)
  7762. "Sort TABLE of WHAT according to SORTING-TYPE.
  7763. The user will be prompted for the SORTING-TYPE if the call to this
  7764. function does not specify it. WHAT is only for the prompt, to indicate
  7765. what is being sorted. The sorting key will be extracted from
  7766. the car of the elements of the table.
  7767. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7768. (unless sorting-type
  7769. (message
  7770. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7771. what)
  7772. (setq sorting-type (read-char-exclusive)))
  7773. (let ((dcst (downcase sorting-type))
  7774. extractfun comparefun)
  7775. ;; Define the appropriate functions
  7776. (cond
  7777. ((= dcst ?n)
  7778. (setq extractfun 'string-to-number
  7779. comparefun (if (= dcst sorting-type) '< '>)))
  7780. ((= dcst ?a)
  7781. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7782. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7783. comparefun (if (= dcst sorting-type)
  7784. 'string<
  7785. (lambda (a b) (and (not (string< a b))
  7786. (not (string= a b)))))))
  7787. ((= dcst ?t)
  7788. (setq extractfun
  7789. (lambda (x)
  7790. (if (or (string-match org-ts-regexp x)
  7791. (string-match org-ts-regexp-both x))
  7792. (org-float-time
  7793. (org-time-string-to-time (match-string 0 x)))
  7794. 0))
  7795. comparefun (if (= dcst sorting-type) '< '>)))
  7796. (t (error "Invalid sorting type `%c'" sorting-type)))
  7797. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7798. table)
  7799. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7800. ;;; The orgstruct minor mode
  7801. ;; Define a minor mode which can be used in other modes in order to
  7802. ;; integrate the org-mode structure editing commands.
  7803. ;; This is really a hack, because the org-mode structure commands use
  7804. ;; keys which normally belong to the major mode. Here is how it
  7805. ;; works: The minor mode defines all the keys necessary to operate the
  7806. ;; structure commands, but wraps the commands into a function which
  7807. ;; tests if the cursor is currently at a headline or a plain list
  7808. ;; item. If that is the case, the structure command is used,
  7809. ;; temporarily setting many Org-mode variables like regular
  7810. ;; expressions for filling etc. However, when any of those keys is
  7811. ;; used at a different location, function uses `key-binding' to look
  7812. ;; up if the key has an associated command in another currently active
  7813. ;; keymap (minor modes, major mode, global), and executes that
  7814. ;; command. There might be problems if any of the keys is otherwise
  7815. ;; used as a prefix key.
  7816. (defcustom orgstruct-heading-prefix-regexp nil
  7817. "Regexp that matches the custom prefix of Org headlines in
  7818. orgstruct(++)-mode."
  7819. :group 'org
  7820. :version "24.4"
  7821. :package-version '(Org . "8.0")
  7822. :type 'string)
  7823. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7824. (defcustom orgstruct-setup-hook nil
  7825. "Hook run after orgstruct-mode-map is filled."
  7826. :group 'org
  7827. :version "24.4"
  7828. :package-version '(Org . "8.0")
  7829. :type 'hook)
  7830. (defvar orgstruct-initialized nil)
  7831. (defvar org-local-vars nil
  7832. "List of local variables, for use by `orgstruct-mode'.")
  7833. ;;;###autoload
  7834. (define-minor-mode orgstruct-mode
  7835. "Toggle the minor mode `orgstruct-mode'.
  7836. This mode is for using Org-mode structure commands in other
  7837. modes. The following keys behave as if Org-mode were active, if
  7838. the cursor is on a headline, or on a plain list item (both as
  7839. defined by Org-mode)."
  7840. nil " OrgStruct" (make-sparse-keymap)
  7841. (when orgstruct-mode
  7842. (org-load-modules-maybe)
  7843. (unless orgstruct-initialized
  7844. (orgstruct-setup)
  7845. (setq orgstruct-initialized t))))
  7846. ;;;###autoload
  7847. (defun turn-on-orgstruct ()
  7848. "Unconditionally turn on `orgstruct-mode'."
  7849. (orgstruct-mode 1))
  7850. (defvar org-fb-vars nil)
  7851. (make-variable-buffer-local 'org-fb-vars)
  7852. (defun orgstruct++-mode (&optional arg)
  7853. "Toggle `orgstruct-mode', the enhanced version of it.
  7854. In addition to setting orgstruct-mode, this also exports all
  7855. indentation and autofilling variables from org-mode into the
  7856. buffer. It will also recognize item context in multiline items."
  7857. (interactive "P")
  7858. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7859. (if (< arg 1)
  7860. (progn (orgstruct-mode -1)
  7861. (mapc (lambda(v)
  7862. (org-set-local (car v)
  7863. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7864. org-fb-vars))
  7865. (orgstruct-mode 1)
  7866. (setq org-fb-vars nil)
  7867. (let (var val)
  7868. (mapc
  7869. (lambda (x)
  7870. (when (string-match
  7871. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7872. (symbol-name (car x)))
  7873. (setq var (car x) val (nth 1 x))
  7874. (push (list var `(quote ,(eval var))) org-fb-vars)
  7875. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7876. org-local-vars)
  7877. (org-set-local 'orgstruct-is-++ t))))
  7878. (defvar orgstruct-is-++ nil
  7879. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7880. (make-variable-buffer-local 'orgstruct-is-++)
  7881. ;;;###autoload
  7882. (defun turn-on-orgstruct++ ()
  7883. "Unconditionally turn on `orgstruct++-mode'."
  7884. (orgstruct++-mode 1))
  7885. (defun orgstruct-error ()
  7886. "Error when there is no default binding for a structure key."
  7887. (interactive)
  7888. (funcall (if (fboundp 'user-error)
  7889. 'user-error
  7890. 'error)
  7891. "This key has no function outside structure elements"))
  7892. (defun orgstruct-setup ()
  7893. "Setup orgstruct keymap."
  7894. (dolist (cell '((org-demote . t)
  7895. (org-metaleft . t)
  7896. (org-metaright . t)
  7897. (org-promote . t)
  7898. (org-shiftmetaleft . t)
  7899. (org-shiftmetaright . t)
  7900. org-backward-element
  7901. org-backward-heading-same-level
  7902. org-ctrl-c-ret
  7903. org-ctrl-c-minus
  7904. org-ctrl-c-star
  7905. org-cycle
  7906. org-forward-heading-same-level
  7907. org-insert-heading
  7908. org-insert-heading-respect-content
  7909. org-kill-note-or-show-branches
  7910. org-mark-subtree
  7911. org-meta-return
  7912. org-metadown
  7913. org-metaup
  7914. org-narrow-to-subtree
  7915. org-promote-subtree
  7916. org-reveal
  7917. org-shiftdown
  7918. org-shiftleft
  7919. org-shiftmetadown
  7920. org-shiftmetaup
  7921. org-shiftright
  7922. org-shifttab
  7923. org-shifttab
  7924. org-shiftup
  7925. org-show-subtree
  7926. org-sort
  7927. org-up-element
  7928. outline-demote
  7929. outline-next-visible-heading
  7930. outline-previous-visible-heading
  7931. outline-promote
  7932. outline-up-heading
  7933. show-children))
  7934. (let ((f (or (car-safe cell) cell))
  7935. (disable-when-heading-prefix (cdr-safe cell)))
  7936. (when (fboundp f)
  7937. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7938. (where-is-internal f outline-mode-map)))
  7939. ;; TODO use local-function-key-map
  7940. (dolist (rep '(("<tab>" . "TAB")
  7941. ("<return>" . "RET")
  7942. ("<escape>" . "ESC")
  7943. ("<delete>" . "DEL")))
  7944. (setq binding (read-kbd-macro (replace-regexp-in-string
  7945. (regexp-quote (car rep))
  7946. (cdr rep)
  7947. (key-description binding)))))
  7948. (let ((key (lookup-key orgstruct-mode-map binding)))
  7949. (when (or (not key) (numberp key))
  7950. (condition-case nil
  7951. (org-defkey orgstruct-mode-map
  7952. binding
  7953. (orgstruct-make-binding f binding disable-when-heading-prefix))
  7954. (error nil))))))))
  7955. (run-hooks 'orgstruct-setup-hook))
  7956. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  7957. "Create a function for binding in the structure minor mode.
  7958. FUN is the command to call inside a table. KEY is the key that
  7959. should be checked in for a command to execute outside of tables.
  7960. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  7961. if `orgstruct-heading-prefix-regexp' is non-nil."
  7962. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7963. (let ((nname name)
  7964. (i 0))
  7965. (while (fboundp (intern nname))
  7966. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7967. (setq name (intern nname)))
  7968. (eval
  7969. (let ((bindings '((org-heading-regexp
  7970. (concat "^"
  7971. orgstruct-heading-prefix-regexp
  7972. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7973. (org-outline-regexp
  7974. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7975. (org-outline-regexp-bol
  7976. (concat "^" org-outline-regexp))
  7977. (outline-regexp org-outline-regexp)
  7978. (outline-heading-end-regexp "\n")
  7979. (outline-level 'org-outline-level)
  7980. (outline-heading-alist))))
  7981. `(defun ,name (arg)
  7982. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7983. "Outside of structure, run the binding of `"
  7984. (key-description key) "'."
  7985. (when disable-when-heading-prefix
  7986. (concat
  7987. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  7988. "back to the default binding due to limitations of Org's implementation of\n"
  7989. "`" (symbol-name fun) "'.")))
  7990. (interactive "p")
  7991. (let* ((disable
  7992. ,(when disable-when-heading-prefix
  7993. '(and orgstruct-heading-prefix-regexp
  7994. (not (string= orgstruct-heading-prefix-regexp "")))))
  7995. (fallback
  7996. (or disable
  7997. (not
  7998. (let* ,bindings
  7999. (org-context-p 'headline 'item
  8000. ,(when (memq fun '(org-insert-heading))
  8001. '(when orgstruct-is-++
  8002. 'item-body))))))))
  8003. (if fallback
  8004. (let* ((orgstruct-mode)
  8005. (binding (key-binding ,key)))
  8006. (if (keymapp binding)
  8007. (set-temporary-overlay-map binding)
  8008. (let ((func (or binding
  8009. (unless disable
  8010. 'orgstruct-error))))
  8011. (when func
  8012. (call-interactively func)))))
  8013. (org-run-like-in-org-mode
  8014. (lambda ()
  8015. (interactive)
  8016. (let* ,bindings
  8017. (call-interactively ',fun)))))))))
  8018. name))
  8019. (defun org-contextualize-keys (alist contexts)
  8020. "Return valid elements in ALIST depending on CONTEXTS.
  8021. `org-agenda-custom-commands' or `org-capture-templates' are the
  8022. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8023. or `org-capture-templates-contexts' are the associated contexts
  8024. definitions."
  8025. (let ((contexts
  8026. ;; normalize contexts
  8027. (mapcar
  8028. (lambda(c) (cond ((listp (cadr c))
  8029. (list (car c) (car c) (cadr c)))
  8030. ((string= "" (cadr c))
  8031. (list (car c) (car c) (caddr c)))
  8032. (t c))) contexts))
  8033. (a alist) c r s)
  8034. ;; loop over all commands or templates
  8035. (while (setq c (pop a))
  8036. (let (vrules repl)
  8037. (cond
  8038. ((not (assoc (car c) contexts))
  8039. (push c r))
  8040. ((and (assoc (car c) contexts)
  8041. (setq vrules (org-contextualize-validate-key
  8042. (car c) contexts)))
  8043. (mapc (lambda (vr)
  8044. (when (not (equal (car vr) (cadr vr)))
  8045. (setq repl vr))) vrules)
  8046. (if (not repl) (push c r)
  8047. (push (cadr repl) s)
  8048. (push
  8049. (cons (car c)
  8050. (cdr (or (assoc (cadr repl) alist)
  8051. (error "Undefined key `%s' as contextual replacement for `%s'"
  8052. (cadr repl) (car c)))))
  8053. r))))))
  8054. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8055. (delq
  8056. nil
  8057. (delete-dups
  8058. (mapcar (lambda (x)
  8059. (let ((tpl (car x)))
  8060. (when (not (delq
  8061. nil
  8062. (mapcar (lambda(y)
  8063. (equal y tpl)) s))) x)))
  8064. (reverse r))))))
  8065. (defun org-contextualize-validate-key (key contexts)
  8066. "Check CONTEXTS for agenda or capture KEY."
  8067. (let (r rr res)
  8068. (while (setq r (pop contexts))
  8069. (mapc
  8070. (lambda (rr)
  8071. (when
  8072. (and (equal key (car r))
  8073. (if (functionp rr) (funcall rr)
  8074. (or (and (eq (car rr) 'in-file)
  8075. (buffer-file-name)
  8076. (string-match (cdr rr) (buffer-file-name)))
  8077. (and (eq (car rr) 'in-mode)
  8078. (string-match (cdr rr) (symbol-name major-mode)))
  8079. (and (eq (car rr) 'in-buffer)
  8080. (string-match (cdr rr) (buffer-name)))
  8081. (when (and (eq (car rr) 'not-in-file)
  8082. (buffer-file-name))
  8083. (not (string-match (cdr rr) (buffer-file-name))))
  8084. (when (eq (car rr) 'not-in-mode)
  8085. (not (string-match (cdr rr) (symbol-name major-mode))))
  8086. (when (eq (car rr) 'not-in-buffer)
  8087. (not (string-match (cdr rr) (buffer-name)))))))
  8088. (push r res)))
  8089. (car (last r))))
  8090. (delete-dups (delq nil res))))
  8091. (defun org-context-p (&rest contexts)
  8092. "Check if local context is any of CONTEXTS.
  8093. Possible values in the list of contexts are `table', `headline', and `item'."
  8094. (let ((pos (point)))
  8095. (goto-char (point-at-bol))
  8096. (prog1 (or (and (memq 'table contexts)
  8097. (looking-at "[ \t]*|"))
  8098. (and (memq 'headline contexts)
  8099. (looking-at org-outline-regexp))
  8100. (and (memq 'item contexts)
  8101. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8102. (and (memq 'item-body contexts)
  8103. (org-in-item-p)))
  8104. (goto-char pos))))
  8105. (defun org-get-local-variables ()
  8106. "Return a list of all local variables in an Org mode buffer."
  8107. (let (varlist)
  8108. (with-current-buffer (get-buffer-create "*Org tmp*")
  8109. (erase-buffer)
  8110. (org-mode)
  8111. (setq varlist (buffer-local-variables)))
  8112. (kill-buffer "*Org tmp*")
  8113. (delq nil
  8114. (mapcar
  8115. (lambda (x)
  8116. (setq x
  8117. (if (symbolp x)
  8118. (list x)
  8119. (list (car x) (cdr x))))
  8120. (if (and (not (get (car x) 'org-state))
  8121. (string-match
  8122. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8123. (symbol-name (car x))))
  8124. x nil))
  8125. varlist))))
  8126. (defun org-clone-local-variables (from-buffer &optional regexp)
  8127. "Clone local variables from FROM-BUFFER.
  8128. Optional argument REGEXP selects variables to clone."
  8129. (mapc
  8130. (lambda (pair)
  8131. (and (symbolp (car pair))
  8132. (or (null regexp)
  8133. (string-match regexp (symbol-name (car pair))))
  8134. (set (make-local-variable (car pair))
  8135. (cdr pair))))
  8136. (buffer-local-variables from-buffer)))
  8137. ;;;###autoload
  8138. (defun org-run-like-in-org-mode (cmd)
  8139. "Run a command, pretending that the current buffer is in Org-mode.
  8140. This will temporarily bind local variables that are typically bound in
  8141. Org-mode to the values they have in Org-mode, and then interactively
  8142. call CMD."
  8143. (org-load-modules-maybe)
  8144. (unless org-local-vars
  8145. (setq org-local-vars (org-get-local-variables)))
  8146. (let (binds)
  8147. (dolist (var org-local-vars)
  8148. (when (or (not (boundp (car var)))
  8149. (eq (symbol-value (car var))
  8150. (default-value (car var))))
  8151. (push (list (car var) `(quote ,(cadr var))) binds)))
  8152. (eval `(let ,binds
  8153. (call-interactively (quote ,cmd))))))
  8154. ;;;; Archiving
  8155. (defun org-get-category (&optional pos force-refresh)
  8156. "Get the category applying to position POS."
  8157. (save-match-data
  8158. (if force-refresh (org-refresh-category-properties))
  8159. (let ((pos (or pos (point))))
  8160. (or (get-text-property pos 'org-category)
  8161. (progn (org-refresh-category-properties)
  8162. (get-text-property pos 'org-category))))))
  8163. (defun org-refresh-category-properties ()
  8164. "Refresh category text properties in the buffer."
  8165. (let ((case-fold-search t)
  8166. (inhibit-read-only t)
  8167. (def-cat (cond
  8168. ((null org-category)
  8169. (if buffer-file-name
  8170. (file-name-sans-extension
  8171. (file-name-nondirectory buffer-file-name))
  8172. "???"))
  8173. ((symbolp org-category) (symbol-name org-category))
  8174. (t org-category)))
  8175. beg end cat pos optionp)
  8176. (org-with-silent-modifications
  8177. (save-excursion
  8178. (save-restriction
  8179. (widen)
  8180. (goto-char (point-min))
  8181. (put-text-property (point) (point-max) 'org-category def-cat)
  8182. (while (re-search-forward
  8183. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8184. (setq pos (match-end 0)
  8185. optionp (equal (char-after (match-beginning 0)) ?#)
  8186. cat (org-trim (match-string 2)))
  8187. (if optionp
  8188. (setq beg (point-at-bol) end (point-max))
  8189. (org-back-to-heading t)
  8190. (setq beg (point) end (org-end-of-subtree t t)))
  8191. (put-text-property beg end 'org-category cat)
  8192. (put-text-property beg end 'org-category-position beg)
  8193. (goto-char pos)))))))
  8194. (defun org-refresh-properties (dprop tprop)
  8195. "Refresh buffer text properties.
  8196. DPROP is the drawer property and TPROP is the corresponding text
  8197. property to set."
  8198. (let ((case-fold-search t)
  8199. (inhibit-read-only t) p)
  8200. (org-with-silent-modifications
  8201. (save-excursion
  8202. (save-restriction
  8203. (widen)
  8204. (goto-char (point-min))
  8205. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8206. (setq p (org-match-string-no-properties 1))
  8207. (save-excursion
  8208. (org-back-to-heading t)
  8209. (put-text-property
  8210. (point-at-bol) (point-at-eol) tprop p))))))))
  8211. ;;;; Link Stuff
  8212. ;;; Link abbreviations
  8213. (defun org-link-expand-abbrev (link)
  8214. "Apply replacements as defined in `org-link-abbrev-alist'."
  8215. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8216. (let* ((key (match-string 1 link))
  8217. (as (or (assoc key org-link-abbrev-alist-local)
  8218. (assoc key org-link-abbrev-alist)))
  8219. (tag (and (match-end 2) (match-string 3 link)))
  8220. rpl)
  8221. (if (not as)
  8222. link
  8223. (setq rpl (cdr as))
  8224. (cond
  8225. ((symbolp rpl) (funcall rpl tag))
  8226. ((string-match "%(\\([^)]+\\))" rpl)
  8227. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8228. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8229. ((string-match "%h" rpl)
  8230. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8231. (t (concat rpl tag)))))
  8232. link))
  8233. ;;; Storing and inserting links
  8234. (defvar org-insert-link-history nil
  8235. "Minibuffer history for links inserted with `org-insert-link'.")
  8236. (defvar org-stored-links nil
  8237. "Contains the links stored with `org-store-link'.")
  8238. (defvar org-store-link-plist nil
  8239. "Plist with info about the most recently link created with `org-store-link'.")
  8240. (defvar org-link-protocols nil
  8241. "Link protocols added to Org-mode using `org-add-link-type'.")
  8242. (defvar org-store-link-functions nil
  8243. "List of functions that are called to create and store a link.
  8244. Each function will be called in turn until one returns a non-nil
  8245. value. Each function should check if it is responsible for creating
  8246. this link (for example by looking at the major mode).
  8247. If not, it must exit and return nil.
  8248. If yes, it should return a non-nil value after a calling
  8249. `org-store-link-props' with a list of properties and values.
  8250. Special properties are:
  8251. :type The link prefix, like \"http\". This must be given.
  8252. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8253. This is obligatory as well.
  8254. :description Optional default description for the second pair
  8255. of brackets in an Org-mode link. The user can still change
  8256. this when inserting this link into an Org-mode buffer.
  8257. In addition to these, any additional properties can be specified
  8258. and then used in capture templates.")
  8259. (defun org-add-link-type (type &optional follow export)
  8260. "Add TYPE to the list of `org-link-types'.
  8261. Re-compute all regular expressions depending on `org-link-types'
  8262. FOLLOW and EXPORT are two functions.
  8263. FOLLOW should take the link path as the single argument and do whatever
  8264. is necessary to follow the link, for example find a file or display
  8265. a mail message.
  8266. EXPORT should format the link path for export to one of the export formats.
  8267. It should be a function accepting three arguments:
  8268. path the path of the link, the text after the prefix (like \"http:\")
  8269. desc the description of the link, if any, or a description added by
  8270. org-export-normalize-links if there is none
  8271. format the export format, a symbol like `html' or `latex' or `ascii'..
  8272. The function may use the FORMAT information to return different values
  8273. depending on the format. The return value will be put literally into
  8274. the exported file. If the return value is nil, this means Org should
  8275. do what it normally does with links which do not have EXPORT defined.
  8276. Org-mode has a built-in default for exporting links. If you are happy with
  8277. this default, there is no need to define an export function for the link
  8278. type. For a simple example of an export function, see `org-bbdb.el'."
  8279. (add-to-list 'org-link-types type t)
  8280. (org-make-link-regexps)
  8281. (if (assoc type org-link-protocols)
  8282. (setcdr (assoc type org-link-protocols) (list follow export))
  8283. (push (list type follow export) org-link-protocols)))
  8284. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8285. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8286. ;;;###autoload
  8287. (defun org-store-link (arg)
  8288. "\\<org-mode-map>Store an org-link to the current location.
  8289. This link is added to `org-stored-links' and can later be inserted
  8290. into an org-buffer with \\[org-insert-link].
  8291. For some link types, a prefix arg is interpreted.
  8292. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8293. For file links, arg negates `org-context-in-file-links'.
  8294. A double prefix arg force skipping storing functions that are not
  8295. part of Org's core."
  8296. (interactive "P")
  8297. (org-load-modules-maybe)
  8298. (setq org-store-link-plist nil) ; reset
  8299. (org-with-limited-levels
  8300. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8301. (cond
  8302. ((and (not (equal arg '(16)))
  8303. (setq sfuns
  8304. (delq
  8305. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8306. org-store-link-functions))
  8307. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8308. (or (and (cdr sfuns)
  8309. (funcall (intern
  8310. (completing-read "Which function for creating the link? "
  8311. sfunsn t (car sfunsn)))))
  8312. (funcall (caar sfuns)))
  8313. (setq link (plist-get org-store-link-plist :link)
  8314. desc (or (plist-get org-store-link-plist :description) link))))
  8315. ((org-src-edit-buffer-p)
  8316. (let (label gc)
  8317. (while (or (not label)
  8318. (save-excursion
  8319. (save-restriction
  8320. (widen)
  8321. (goto-char (point-min))
  8322. (re-search-forward
  8323. (regexp-quote (format org-coderef-label-format label))
  8324. nil t))))
  8325. (when label (message "Label exists already") (sit-for 2))
  8326. (setq label (read-string "Code line label: " label)))
  8327. (end-of-line 1)
  8328. (setq link (format org-coderef-label-format label))
  8329. (setq gc (- 79 (length link)))
  8330. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8331. (insert link)
  8332. (setq link (concat "(" label ")") desc nil)))
  8333. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8334. ;; We are in the agenda, link to referenced location
  8335. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8336. (get-text-property (point) 'org-marker))))
  8337. (when m
  8338. (org-with-point-at m
  8339. (setq agenda-link
  8340. (if (org-called-interactively-p 'any)
  8341. (call-interactively 'org-store-link)
  8342. (org-store-link nil)))))))
  8343. ((eq major-mode 'calendar-mode)
  8344. (let ((cd (calendar-cursor-to-date)))
  8345. (setq link
  8346. (format-time-string
  8347. (car org-time-stamp-formats)
  8348. (apply 'encode-time
  8349. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8350. nil nil nil))))
  8351. (org-store-link-props :type "calendar" :date cd)))
  8352. ((eq major-mode 'help-mode)
  8353. (setq link (concat "help:" (save-excursion
  8354. (goto-char (point-min))
  8355. (looking-at "^[^ ]+")
  8356. (match-string 0))))
  8357. (org-store-link-props :type "help"))
  8358. ((eq major-mode 'w3-mode)
  8359. (setq cpltxt (if (and (buffer-name)
  8360. (not (string-match "Untitled" (buffer-name))))
  8361. (buffer-name)
  8362. (url-view-url t))
  8363. link (url-view-url t))
  8364. (org-store-link-props :type "w3" :url (url-view-url t)))
  8365. ((setq search (run-hook-with-args-until-success
  8366. 'org-create-file-search-functions))
  8367. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8368. "::" search))
  8369. (setq cpltxt (or description link)))
  8370. ((eq major-mode 'image-mode)
  8371. (setq cpltxt (concat "file:"
  8372. (abbreviate-file-name buffer-file-name))
  8373. link cpltxt)
  8374. (org-store-link-props :type "image" :file buffer-file-name))
  8375. ((eq major-mode 'dired-mode)
  8376. ;; link to the file in the current line
  8377. (let ((file (dired-get-filename nil t)))
  8378. (setq file (if file
  8379. (abbreviate-file-name
  8380. (expand-file-name (dired-get-filename nil t)))
  8381. ;; otherwise, no file so use current directory.
  8382. default-directory))
  8383. (setq cpltxt (concat "file:" file)
  8384. link cpltxt)))
  8385. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8386. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8387. (cond
  8388. ((org-in-regexp "<<\\(.*?\\)>>")
  8389. (setq cpltxt
  8390. (concat "file:"
  8391. (abbreviate-file-name
  8392. (buffer-file-name (buffer-base-buffer)))
  8393. "::" (match-string 1))
  8394. link cpltxt))
  8395. ((and (featurep 'org-id)
  8396. (or (eq org-id-link-to-org-use-id t)
  8397. (and (org-called-interactively-p 'any)
  8398. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8399. (and (eq org-id-link-to-org-use-id
  8400. 'create-if-interactive-and-no-custom-id)
  8401. (not custom-id))))
  8402. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8403. ;; We can make a link using the ID.
  8404. (setq link (condition-case nil
  8405. (prog1 (org-id-store-link)
  8406. (setq desc (plist-get org-store-link-plist :description)))
  8407. (error
  8408. ;; probably before first headline, link to file only
  8409. (concat "file:"
  8410. (abbreviate-file-name
  8411. (buffer-file-name (buffer-base-buffer))))))))
  8412. (t
  8413. ;; Just link to current headline
  8414. (setq cpltxt (concat "file:"
  8415. (abbreviate-file-name
  8416. (buffer-file-name (buffer-base-buffer)))))
  8417. ;; Add a context search string
  8418. (when (org-xor org-context-in-file-links arg)
  8419. (let* ((ee (org-element-at-point))
  8420. (et (org-element-type ee))
  8421. (ev (plist-get (cadr ee) :value)))
  8422. (setq txt (cond
  8423. ((org-at-heading-p) nil)
  8424. ((eq et 'keyword) ev)
  8425. ((org-region-active-p)
  8426. (buffer-substring (region-beginning) (region-end)))))
  8427. (when (or (null txt) (string-match "\\S-" txt))
  8428. (setq cpltxt
  8429. (concat cpltxt "::"
  8430. (condition-case nil
  8431. (org-make-org-heading-search-string txt)
  8432. (error "")))
  8433. desc (or (and (eq et 'keyword) ev)
  8434. (nth 4 (ignore-errors (org-heading-components)))
  8435. "NONE")))))
  8436. (if (string-match "::\\'" cpltxt)
  8437. (setq cpltxt (substring cpltxt 0 -2)))
  8438. (setq link cpltxt))))
  8439. ((buffer-file-name (buffer-base-buffer))
  8440. ;; Just link to this file here.
  8441. (setq cpltxt (concat "file:"
  8442. (abbreviate-file-name
  8443. (buffer-file-name (buffer-base-buffer)))))
  8444. ;; Add a context string
  8445. (when (org-xor org-context-in-file-links arg)
  8446. (setq txt (if (org-region-active-p)
  8447. (buffer-substring (region-beginning) (region-end))
  8448. (buffer-substring (point-at-bol) (point-at-eol))))
  8449. ;; Only use search option if there is some text.
  8450. (when (string-match "\\S-" txt)
  8451. (setq cpltxt
  8452. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8453. desc "NONE")))
  8454. (setq link cpltxt))
  8455. ((org-called-interactively-p 'interactive)
  8456. (user-error "No method for storing a link from this buffer"))
  8457. (t (setq link nil)))
  8458. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8459. (setq link (or link cpltxt)
  8460. desc (or desc cpltxt))
  8461. (cond ((equal desc "NONE") (setq desc nil))
  8462. ((string-match org-bracket-link-regexp desc)
  8463. (setq desc (replace-regexp-in-string
  8464. org-bracket-link-regexp
  8465. (concat "\\3" (if (equal (length (match-string 0 desc))
  8466. (length desc)) "*" "")) desc))))
  8467. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8468. (progn
  8469. (setq org-stored-links
  8470. (cons (list link desc) org-stored-links))
  8471. (message "Stored: %s" (or desc link))
  8472. (when custom-id
  8473. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8474. "::#" custom-id))
  8475. (setq org-stored-links
  8476. (cons (list link desc) org-stored-links))))
  8477. (or agenda-link (and link (org-make-link-string link desc)))))))
  8478. (defun org-store-link-props (&rest plist)
  8479. "Store link properties, extract names and addresses."
  8480. (let (x adr)
  8481. (when (setq x (plist-get plist :from))
  8482. (setq adr (mail-extract-address-components x))
  8483. (setq plist (plist-put plist :fromname (car adr)))
  8484. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8485. (when (setq x (plist-get plist :to))
  8486. (setq adr (mail-extract-address-components x))
  8487. (setq plist (plist-put plist :toname (car adr)))
  8488. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8489. (let ((from (plist-get plist :from))
  8490. (to (plist-get plist :to)))
  8491. (when (and from to org-from-is-user-regexp)
  8492. (setq plist
  8493. (plist-put plist :fromto
  8494. (if (string-match org-from-is-user-regexp from)
  8495. (concat "to %t")
  8496. (concat "from %f"))))))
  8497. (setq org-store-link-plist plist))
  8498. (defun org-add-link-props (&rest plist)
  8499. "Add these properties to the link property list."
  8500. (let (key value)
  8501. (while plist
  8502. (setq key (pop plist) value (pop plist))
  8503. (setq org-store-link-plist
  8504. (plist-put org-store-link-plist key value)))))
  8505. (defun org-email-link-description (&optional fmt)
  8506. "Return the description part of an email link.
  8507. This takes information from `org-store-link-plist' and formats it
  8508. according to FMT (default from `org-email-link-description-format')."
  8509. (setq fmt (or fmt org-email-link-description-format))
  8510. (let* ((p org-store-link-plist)
  8511. (to (plist-get p :toaddress))
  8512. (from (plist-get p :fromaddress))
  8513. (table
  8514. (list
  8515. (cons "%c" (plist-get p :fromto))
  8516. (cons "%F" (plist-get p :from))
  8517. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8518. (cons "%T" (plist-get p :to))
  8519. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8520. (cons "%s" (plist-get p :subject))
  8521. (cons "%d" (plist-get p :date))
  8522. (cons "%m" (plist-get p :message-id)))))
  8523. (when (string-match "%c" fmt)
  8524. ;; Check if the user wrote this message
  8525. (if (and org-from-is-user-regexp from to
  8526. (save-match-data (string-match org-from-is-user-regexp from)))
  8527. (setq fmt (replace-match "to %t" t t fmt))
  8528. (setq fmt (replace-match "from %f" t t fmt))))
  8529. (org-replace-escapes fmt table)))
  8530. (defun org-make-org-heading-search-string (&optional string)
  8531. "Make search string for the current headline or STRING."
  8532. (let ((s (or string
  8533. (and (derived-mode-p 'org-mode)
  8534. (save-excursion
  8535. (org-back-to-heading t)
  8536. (org-element-property :raw-value (org-element-at-point))))))
  8537. (lines org-context-in-file-links))
  8538. (or string (setq s (concat "*" s))) ; Add * for headlines
  8539. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8540. (when (and string (integerp lines) (> lines 0))
  8541. (let ((slines (org-split-string s "\n")))
  8542. (when (< lines (length slines))
  8543. (setq s (mapconcat
  8544. 'identity
  8545. (reverse (nthcdr (- (length slines) lines)
  8546. (reverse slines))) "\n")))))
  8547. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8548. (defun org-make-link-string (link &optional description)
  8549. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8550. (unless (string-match "\\S-" link)
  8551. (error "Empty link"))
  8552. (when (and description
  8553. (stringp description)
  8554. (not (string-match "\\S-" description)))
  8555. (setq description nil))
  8556. (when (stringp description)
  8557. ;; Remove brackets from the description, they are fatal.
  8558. (while (string-match "\\[" description)
  8559. (setq description (replace-match "{" t t description)))
  8560. (while (string-match "\\]" description)
  8561. (setq description (replace-match "}" t t description))))
  8562. (when (equal link description)
  8563. ;; No description needed, it is identical
  8564. (setq description nil))
  8565. (when (and (not description)
  8566. (not (string-match (org-image-file-name-regexp) link))
  8567. (not (equal link (org-link-escape link))))
  8568. (setq description (org-extract-attributes link)))
  8569. (setq link
  8570. (cond ((string-match (org-image-file-name-regexp) link) link)
  8571. ((string-match org-link-types-re link)
  8572. (concat (match-string 1 link)
  8573. (org-link-escape (substring link (match-end 1)))))
  8574. (t (org-link-escape link))))
  8575. (concat "[[" link "]"
  8576. (if description (concat "[" description "]") "")
  8577. "]"))
  8578. (defconst org-link-escape-chars
  8579. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8580. "List of characters that should be escaped in link.
  8581. This is the list that is used for internal purposes.")
  8582. (defconst org-link-escape-chars-browser
  8583. '(?\ )
  8584. "List of escapes for characters that are problematic in links.
  8585. This is the list that is used before handing over to the browser.")
  8586. (defun org-link-escape (text &optional table merge)
  8587. "Return percent escaped representation of TEXT.
  8588. TEXT is a string with the text to escape.
  8589. Optional argument TABLE is a list with characters that should be
  8590. escaped. When nil, `org-link-escape-chars' is used.
  8591. If optional argument MERGE is set, merge TABLE into
  8592. `org-link-escape-chars'."
  8593. (cond
  8594. ((and table merge)
  8595. (mapc (lambda (defchr)
  8596. (unless (member defchr table)
  8597. (setq table (cons defchr table)))) org-link-escape-chars))
  8598. ((null table)
  8599. (setq table org-link-escape-chars)))
  8600. (mapconcat
  8601. (lambda (char)
  8602. (if (or (member char table)
  8603. (and (or (< char 32) (= char 37) (> char 126))
  8604. org-url-hexify-p))
  8605. (mapconcat (lambda (sequence-element)
  8606. (format "%%%.2X" sequence-element))
  8607. (or (encode-coding-char char 'utf-8)
  8608. (error "Unable to percent escape character: %s"
  8609. (char-to-string char))) "")
  8610. (char-to-string char))) text ""))
  8611. (defun org-link-unescape (str)
  8612. "Unhex hexified Unicode strings as returned from the JavaScript function
  8613. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8614. (unless (and (null str) (string= "" str))
  8615. (let ((pos 0) (case-fold-search t) unhexed)
  8616. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8617. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8618. (setq str (replace-match unhexed t t str))
  8619. (setq pos (+ pos (length unhexed))))))
  8620. str)
  8621. (defun org-link-unescape-compound (hex)
  8622. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8623. Note: this function also decodes single byte encodings like
  8624. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8625. (save-match-data
  8626. (let* ((bytes (cdr (split-string hex "%")))
  8627. (ret "")
  8628. (eat 0)
  8629. (sum 0))
  8630. (while bytes
  8631. (let* ((val (string-to-number (pop bytes) 16))
  8632. (shift-xor
  8633. (if (= 0 eat)
  8634. (cond
  8635. ((>= val 252) (cons 6 252))
  8636. ((>= val 248) (cons 5 248))
  8637. ((>= val 240) (cons 4 240))
  8638. ((>= val 224) (cons 3 224))
  8639. ((>= val 192) (cons 2 192))
  8640. (t (cons 0 0)))
  8641. (cons 6 128))))
  8642. (if (>= val 192) (setq eat (car shift-xor)))
  8643. (setq val (logxor val (cdr shift-xor)))
  8644. (setq sum (+ (lsh sum (car shift-xor)) val))
  8645. (if (> eat 0) (setq eat (- eat 1)))
  8646. (cond
  8647. ((= 0 eat) ;multi byte
  8648. (setq ret (concat ret (org-char-to-string sum)))
  8649. (setq sum 0))
  8650. ((not bytes) ; single byte(s)
  8651. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8652. )) ;; end (while bytes
  8653. ret )))
  8654. (defun org-link-unescape-single-byte-sequence (hex)
  8655. "Unhexify hex-encoded single byte character sequences."
  8656. (mapconcat (lambda (byte)
  8657. (char-to-string (string-to-number byte 16)))
  8658. (cdr (split-string hex "%")) ""))
  8659. (defun org-xor (a b)
  8660. "Exclusive or."
  8661. (if a (not b) b))
  8662. (defun org-fixup-message-id-for-http (s)
  8663. "Replace special characters in a message id, so it can be used in an http query."
  8664. (when (string-match "%" s)
  8665. (setq s (mapconcat (lambda (c)
  8666. (if (eq c ?%)
  8667. "%25"
  8668. (char-to-string c)))
  8669. s "")))
  8670. (while (string-match "<" s)
  8671. (setq s (replace-match "%3C" t t s)))
  8672. (while (string-match ">" s)
  8673. (setq s (replace-match "%3E" t t s)))
  8674. (while (string-match "@" s)
  8675. (setq s (replace-match "%40" t t s)))
  8676. s)
  8677. (defun org-link-prettify (link)
  8678. "Return a human-readable representation of LINK.
  8679. The car of LINK must be a raw link the cdr of LINK must be either
  8680. a link description or nil."
  8681. (let ((desc (or (cadr link) "<no description>")))
  8682. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8683. "<" (car link) ">")))
  8684. ;;;###autoload
  8685. (defun org-insert-link-global ()
  8686. "Insert a link like Org-mode does.
  8687. This command can be called in any mode to insert a link in Org-mode syntax."
  8688. (interactive)
  8689. (org-load-modules-maybe)
  8690. (org-run-like-in-org-mode 'org-insert-link))
  8691. (defun org-insert-all-links (&optional keep)
  8692. "Insert all links in `org-stored-links'."
  8693. (interactive "P")
  8694. (let ((links (copy-sequence org-stored-links)) l)
  8695. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8696. (insert "- ")
  8697. (org-insert-link nil (car l) (cadr l))
  8698. (insert "\n"))))
  8699. (defun org-link-fontify-links-to-this-file ()
  8700. "Fontify links to the current file in `org-stored-links'."
  8701. (let ((f (buffer-file-name)) a b)
  8702. (setq a (mapcar (lambda(l)
  8703. (let ((ll (car l)))
  8704. (when (and (string-match "^file:\\(.+\\)::" ll)
  8705. (equal f (expand-file-name (match-string 1 ll))))
  8706. ll)))
  8707. org-stored-links))
  8708. (when (featurep 'org-id)
  8709. (setq b (mapcar (lambda(l)
  8710. (let ((ll (car l)))
  8711. (when (and (string-match "^id:\\(.+\\)$" ll)
  8712. (equal f (expand-file-name
  8713. (or (org-id-find-id-file
  8714. (match-string 1 ll)) ""))))
  8715. ll)))
  8716. org-stored-links)))
  8717. (mapcar (lambda(l)
  8718. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8719. (delq nil (append a b)))))
  8720. (defvar org-link-links-in-this-file nil)
  8721. (defun org-insert-link (&optional complete-file link-location default-description)
  8722. "Insert a link. At the prompt, enter the link.
  8723. Completion can be used to insert any of the link protocol prefixes like
  8724. http or ftp in use.
  8725. The history can be used to select a link previously stored with
  8726. `org-store-link'. When the empty string is entered (i.e. if you just
  8727. press RET at the prompt), the link defaults to the most recently
  8728. stored link. As SPC triggers completion in the minibuffer, you need to
  8729. use M-SPC or C-q SPC to force the insertion of a space character.
  8730. You will also be prompted for a description, and if one is given, it will
  8731. be displayed in the buffer instead of the link.
  8732. If there is already a link at point, this command will allow you to edit link
  8733. and description parts.
  8734. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8735. be selected using completion. The path to the file will be relative to the
  8736. current directory if the file is in the current directory or a subdirectory.
  8737. Otherwise, the link will be the absolute path as completed in the minibuffer
  8738. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8739. option `org-link-file-path-type'.
  8740. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8741. the current directory or below.
  8742. With three \\[universal-argument] prefixes, negate the meaning of
  8743. `org-keep-stored-link-after-insertion'.
  8744. If `org-make-link-description-function' is non-nil, this function will be
  8745. called with the link target, and the result will be the default
  8746. link description.
  8747. If the LINK-LOCATION parameter is non-nil, this value will be
  8748. used as the link location instead of reading one interactively.
  8749. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8750. be used as the default description."
  8751. (interactive "P")
  8752. (let* ((wcf (current-window-configuration))
  8753. (origbuf (current-buffer))
  8754. (region (if (org-region-active-p)
  8755. (buffer-substring (region-beginning) (region-end))))
  8756. (remove (and region (list (region-beginning) (region-end))))
  8757. (desc region)
  8758. tmphist ; byte-compile incorrectly complains about this
  8759. (link link-location)
  8760. (abbrevs org-link-abbrev-alist-local)
  8761. entry file all-prefixes auto-desc)
  8762. (cond
  8763. (link-location) ; specified by arg, just use it.
  8764. ((org-in-regexp org-bracket-link-regexp 1)
  8765. ;; We do have a link at point, and we are going to edit it.
  8766. (setq remove (list (match-beginning 0) (match-end 0)))
  8767. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8768. (setq link (read-string "Link: "
  8769. (org-link-unescape
  8770. (org-match-string-no-properties 1)))))
  8771. ((or (org-in-regexp org-angle-link-re)
  8772. (org-in-regexp org-plain-link-re))
  8773. ;; Convert to bracket link
  8774. (setq remove (list (match-beginning 0) (match-end 0))
  8775. link (read-string "Link: "
  8776. (org-remove-angle-brackets (match-string 0)))))
  8777. ((member complete-file '((4) (16)))
  8778. ;; Completing read for file names.
  8779. (setq link (org-file-complete-link complete-file)))
  8780. (t
  8781. ;; Read link, with completion for stored links.
  8782. (org-link-fontify-links-to-this-file)
  8783. (org-switch-to-buffer-other-window "*Org Links*")
  8784. (with-current-buffer "*Org Links*"
  8785. (erase-buffer)
  8786. (insert "Insert a link.
  8787. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8788. (when org-stored-links
  8789. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8790. (insert (mapconcat 'org-link-prettify
  8791. (reverse org-stored-links) "\n")))
  8792. (goto-char (point-min)))
  8793. (let ((cw (selected-window)))
  8794. (select-window (get-buffer-window "*Org Links*" 'visible))
  8795. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8796. (unless (pos-visible-in-window-p (point-max))
  8797. (org-fit-window-to-buffer))
  8798. (and (window-live-p cw) (select-window cw)))
  8799. ;; Fake a link history, containing the stored links.
  8800. (setq tmphist (append (mapcar 'car org-stored-links)
  8801. org-insert-link-history))
  8802. (setq all-prefixes (append (mapcar 'car abbrevs)
  8803. (mapcar 'car org-link-abbrev-alist)
  8804. org-link-types))
  8805. (unwind-protect
  8806. (progn
  8807. (setq link
  8808. (org-completing-read
  8809. "Link: "
  8810. (append
  8811. (mapcar (lambda (x) (concat x ":"))
  8812. all-prefixes)
  8813. (mapcar 'car org-stored-links))
  8814. nil nil nil
  8815. 'tmphist
  8816. (caar org-stored-links)))
  8817. (if (not (string-match "\\S-" link))
  8818. (error "No link selected"))
  8819. (mapc (lambda(l)
  8820. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8821. org-stored-links)
  8822. (if (or (member link all-prefixes)
  8823. (and (equal ":" (substring link -1))
  8824. (member (substring link 0 -1) all-prefixes)
  8825. (setq link (substring link 0 -1))))
  8826. (setq link (with-current-buffer origbuf
  8827. (org-link-try-special-completion link)))))
  8828. (set-window-configuration wcf)
  8829. (kill-buffer "*Org Links*"))
  8830. (setq entry (assoc link org-stored-links))
  8831. (or entry (push link org-insert-link-history))
  8832. (setq desc (or desc (nth 1 entry)))))
  8833. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8834. (not org-keep-stored-link-after-insertion))
  8835. (setq org-stored-links (delq (assoc link org-stored-links)
  8836. org-stored-links)))
  8837. (if (string-match org-plain-link-re link)
  8838. ;; URL-like link, normalize the use of angular brackets.
  8839. (setq link (org-remove-angle-brackets link)))
  8840. ;; Check if we are linking to the current file with a search
  8841. ;; option If yes, simplify the link by using only the search
  8842. ;; option.
  8843. (when (and buffer-file-name
  8844. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8845. (let* ((path (match-string 1 link))
  8846. (case-fold-search nil)
  8847. (search (match-string 2 link)))
  8848. (save-match-data
  8849. (if (equal (file-truename buffer-file-name) (file-truename path))
  8850. ;; We are linking to this same file, with a search option
  8851. (setq link search)))))
  8852. ;; Check if we can/should use a relative path. If yes, simplify the link
  8853. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8854. (let* ((type (match-string 1 link))
  8855. (path (match-string 2 link))
  8856. (origpath path)
  8857. (case-fold-search nil))
  8858. (cond
  8859. ((or (eq org-link-file-path-type 'absolute)
  8860. (equal complete-file '(16)))
  8861. (setq path (abbreviate-file-name (expand-file-name path))))
  8862. ((eq org-link-file-path-type 'noabbrev)
  8863. (setq path (expand-file-name path)))
  8864. ((eq org-link-file-path-type 'relative)
  8865. (setq path (file-relative-name path)))
  8866. (t
  8867. (save-match-data
  8868. (if (string-match (concat "^" (regexp-quote
  8869. (expand-file-name
  8870. (file-name-as-directory
  8871. default-directory))))
  8872. (expand-file-name path))
  8873. ;; We are linking a file with relative path name.
  8874. (setq path (substring (expand-file-name path)
  8875. (match-end 0)))
  8876. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8877. (setq link (concat type path))
  8878. (if (equal desc origpath)
  8879. (setq desc path))))
  8880. (if org-make-link-description-function
  8881. (setq desc
  8882. (or (condition-case nil
  8883. (funcall org-make-link-description-function link desc)
  8884. (error (progn (message "Can't get link description from `%s'"
  8885. (symbol-name org-make-link-description-function))
  8886. (sit-for 2) nil)))
  8887. (read-string "Description: " default-description)))
  8888. (if default-description (setq desc default-description)
  8889. (setq desc (or (and auto-desc desc)
  8890. (read-string "Description: " desc)))))
  8891. (unless (string-match "\\S-" desc) (setq desc nil))
  8892. (if remove (apply 'delete-region remove))
  8893. (insert (org-make-link-string link desc))))
  8894. (defun org-link-try-special-completion (type)
  8895. "If there is completion support for link type TYPE, offer it."
  8896. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8897. (if (functionp fun)
  8898. (funcall fun)
  8899. (read-string "Link (no completion support): " (concat type ":")))))
  8900. (defun org-file-complete-link (&optional arg)
  8901. "Create a file link using completion."
  8902. (let (file link)
  8903. (setq file (org-iread-file-name "File: "))
  8904. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8905. (pwd1 (file-name-as-directory (abbreviate-file-name
  8906. (expand-file-name ".")))))
  8907. (cond
  8908. ((equal arg '(16))
  8909. (setq link (concat
  8910. "file:"
  8911. (abbreviate-file-name (expand-file-name file)))))
  8912. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8913. (setq link (concat "file:" (match-string 1 file))))
  8914. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8915. (expand-file-name file))
  8916. (setq link (concat
  8917. "file:" (match-string 1 (expand-file-name file)))))
  8918. (t (setq link (concat "file:" file)))))
  8919. link))
  8920. (defun org-iread-file-name (&rest args)
  8921. "Read-file-name using `ido-mode' speedup if available.
  8922. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8923. See `read-file-name' for a description of parameters."
  8924. (org-without-partial-completion
  8925. (if (and org-completion-use-ido
  8926. (fboundp 'ido-read-file-name)
  8927. (boundp 'ido-mode) ido-mode
  8928. (listp (second args)))
  8929. (let ((ido-enter-matching-directory nil))
  8930. (apply 'ido-read-file-name args))
  8931. (apply 'read-file-name args))))
  8932. (defun org-completing-read (&rest args)
  8933. "Completing-read with SPACE being a normal character."
  8934. (let ((enable-recursive-minibuffers t)
  8935. (minibuffer-local-completion-map
  8936. (copy-keymap minibuffer-local-completion-map)))
  8937. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8938. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8939. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8940. (apply 'org-icompleting-read args)))
  8941. (defun org-completing-read-no-i (&rest args)
  8942. (let (org-completion-use-ido org-completion-use-iswitchb)
  8943. (apply 'org-completing-read args)))
  8944. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8945. "Use iswitch as a completing-read replacement to choose from choices.
  8946. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8947. from."
  8948. (let* ((iswitchb-use-virtual-buffers nil)
  8949. (iswitchb-make-buflist-hook
  8950. (lambda ()
  8951. (setq iswitchb-temp-buflist choices))))
  8952. (iswitchb-read-buffer prompt)))
  8953. (defun org-icompleting-read (&rest args)
  8954. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8955. (org-without-partial-completion
  8956. (if (and org-completion-use-ido
  8957. (fboundp 'ido-completing-read)
  8958. (boundp 'ido-mode) ido-mode
  8959. (listp (second args)))
  8960. (let ((ido-enter-matching-directory nil))
  8961. (apply 'ido-completing-read (concat (car args))
  8962. (if (consp (car (nth 1 args)))
  8963. (mapcar 'car (nth 1 args))
  8964. (nth 1 args))
  8965. (cddr args)))
  8966. (if (and org-completion-use-iswitchb
  8967. (boundp 'iswitchb-mode) iswitchb-mode
  8968. (listp (second args)))
  8969. (apply 'org-iswitchb-completing-read (concat (car args))
  8970. (if (consp (car (nth 1 args)))
  8971. (mapcar 'car (nth 1 args))
  8972. (nth 1 args))
  8973. (cddr args))
  8974. (apply 'completing-read args)))))
  8975. (defun org-extract-attributes (s)
  8976. "Extract the attributes cookie from a string and set as text property."
  8977. (let (a attr (start 0) key value)
  8978. (save-match-data
  8979. (when (string-match "{{\\([^}]+\\)}}$" s)
  8980. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8981. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8982. (setq key (match-string 1 a) value (match-string 2 a)
  8983. start (match-end 0)
  8984. attr (plist-put attr (intern key) value))))
  8985. (org-add-props s nil 'org-attr attr))
  8986. s))
  8987. (defun org-extract-attributes-from-string (tag)
  8988. (let (key value attr)
  8989. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8990. (setq key (match-string 1 tag) value (match-string 2 tag)
  8991. tag (replace-match "" t t tag)
  8992. attr (plist-put attr (intern key) value)))
  8993. (cons tag attr)))
  8994. (defun org-attributes-to-string (plist)
  8995. "Format a property list into an HTML attribute list."
  8996. (let ((s "") key value)
  8997. (while plist
  8998. (setq key (pop plist) value (pop plist))
  8999. (and value
  9000. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  9001. s))
  9002. ;;; Opening/following a link
  9003. (defvar org-link-search-failed nil)
  9004. (defvar org-open-link-functions nil
  9005. "Hook for functions finding a plain text link.
  9006. These functions must take a single argument, the link content.
  9007. They will be called for links that look like [[link text][description]]
  9008. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9009. like a filename (e.g. \"./blue.png\").
  9010. These functions will be called *before* Org attempts to resolve the
  9011. link by doing text searches in the current buffer - so if you want a
  9012. link \"[[target]]\" to still find \"<<target>>\", your function should
  9013. handle this as a special case.
  9014. When the function does handle the link, it must return a non-nil value.
  9015. If it decides that it is not responsible for this link, it must return
  9016. nil to indicate that that Org-mode can continue with other options
  9017. like exact and fuzzy text search.")
  9018. (defun org-next-link (&optional search-backward)
  9019. "Move forward to the next link.
  9020. If the link is in hidden text, expose it."
  9021. (interactive "P")
  9022. (when (and org-link-search-failed (eq this-command last-command))
  9023. (goto-char (point-min))
  9024. (message "Link search wrapped back to beginning of buffer"))
  9025. (setq org-link-search-failed nil)
  9026. (let* ((pos (point))
  9027. (ct (org-context))
  9028. (a (assoc :link ct))
  9029. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9030. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9031. ((looking-at org-any-link-re)
  9032. ;; Don't stay stuck at link without an org-link face
  9033. (forward-char (if search-backward -1 1))))
  9034. (if (funcall srch-fun org-any-link-re nil t)
  9035. (progn
  9036. (goto-char (match-beginning 0))
  9037. (if (outline-invisible-p) (org-show-context)))
  9038. (goto-char pos)
  9039. (setq org-link-search-failed t)
  9040. (message "No further link found"))))
  9041. (defun org-previous-link ()
  9042. "Move backward to the previous link.
  9043. If the link is in hidden text, expose it."
  9044. (interactive)
  9045. (funcall 'org-next-link t))
  9046. (defun org-translate-link (s)
  9047. "Translate a link string if a translation function has been defined."
  9048. (if (and org-link-translation-function
  9049. (fboundp org-link-translation-function)
  9050. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9051. (progn
  9052. (setq s (funcall org-link-translation-function
  9053. (match-string 1 s) (match-string 2 s)))
  9054. (concat (car s) ":" (cdr s)))
  9055. s))
  9056. (defun org-translate-link-from-planner (type path)
  9057. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9058. This is still an experimental function, your mileage may vary."
  9059. (cond
  9060. ((member type '("http" "https" "news" "ftp"))
  9061. ;; standard Internet links are the same.
  9062. nil)
  9063. ((and (equal type "irc") (string-match "^//" path))
  9064. ;; Planner has two / at the beginning of an irc link, we have 1.
  9065. ;; We should have zero, actually....
  9066. (setq path (substring path 1)))
  9067. ((and (equal type "lisp") (string-match "^/" path))
  9068. ;; Planner has a slash, we do not.
  9069. (setq type "elisp" path (substring path 1)))
  9070. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9071. ;; A typical message link. Planner has the id after the final slash,
  9072. ;; we separate it with a hash mark
  9073. (setq path (concat (match-string 1 path) "#"
  9074. (org-remove-angle-brackets (match-string 2 path)))))
  9075. )
  9076. (cons type path))
  9077. (defun org-find-file-at-mouse (ev)
  9078. "Open file link or URL at mouse."
  9079. (interactive "e")
  9080. (mouse-set-point ev)
  9081. (org-open-at-point 'in-emacs))
  9082. (defun org-open-at-mouse (ev)
  9083. "Open file link or URL at mouse.
  9084. See the docstring of `org-open-file' for details."
  9085. (interactive "e")
  9086. (mouse-set-point ev)
  9087. (if (eq major-mode 'org-agenda-mode)
  9088. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9089. (org-open-at-point))
  9090. (defvar org-window-config-before-follow-link nil
  9091. "The window configuration before following a link.
  9092. This is saved in case the need arises to restore it.")
  9093. (defvar org-open-link-marker (make-marker)
  9094. "Marker pointing to the location where `org-open-at-point; was called.")
  9095. ;;;###autoload
  9096. (defun org-open-at-point-global ()
  9097. "Follow a link like Org-mode does.
  9098. This command can be called in any mode to follow a link that has
  9099. Org-mode syntax."
  9100. (interactive)
  9101. (org-run-like-in-org-mode 'org-open-at-point))
  9102. ;;;###autoload
  9103. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9104. "Open a link in the string S, as if it was in Org-mode."
  9105. (interactive "sLink: \nP")
  9106. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9107. (with-temp-buffer
  9108. (let ((org-inhibit-startup (not reference-buffer)))
  9109. (org-mode)
  9110. (insert s)
  9111. (goto-char (point-min))
  9112. (when reference-buffer
  9113. (setq org-link-abbrev-alist-local
  9114. (with-current-buffer reference-buffer
  9115. org-link-abbrev-alist-local)))
  9116. (org-open-at-point arg reference-buffer)))))
  9117. (defvar org-open-at-point-functions nil
  9118. "Hook that is run when following a link at point.
  9119. Functions in this hook must return t if they identify and follow
  9120. a link at point. If they don't find anything interesting at point,
  9121. they must return nil.")
  9122. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9123. (defun org-open-at-point (&optional arg reference-buffer)
  9124. "Open link at or after point.
  9125. If there is no link at point, this function will search forward up to
  9126. the end of the current line.
  9127. Normally, files will be opened by an appropriate application. If the
  9128. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9129. With a double prefix argument, try to open outside of Emacs, in the
  9130. application the system uses for this file type."
  9131. (interactive "P")
  9132. ;; if in a code block, then open the block's results
  9133. (unless (call-interactively #'org-babel-open-src-block-result)
  9134. (org-load-modules-maybe)
  9135. (move-marker org-open-link-marker (point))
  9136. (setq org-window-config-before-follow-link (current-window-configuration))
  9137. (org-remove-occur-highlights nil nil t)
  9138. (cond
  9139. ((and (org-at-heading-p)
  9140. (not (org-at-timestamp-p t))
  9141. (not (org-in-regexp
  9142. (concat org-plain-link-re "\\|"
  9143. org-bracket-link-regexp "\\|"
  9144. org-angle-link-re "\\|"
  9145. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9146. (not (get-text-property (point) 'org-linked-text)))
  9147. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9148. (lk0 (car lkall))
  9149. (lk (if (stringp lk0) (list lk0) lk0))
  9150. (lkend (cdr lkall)))
  9151. (mapcar (lambda(l)
  9152. (search-forward l nil lkend)
  9153. (goto-char (match-beginning 0))
  9154. (org-open-at-point))
  9155. lk))
  9156. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9157. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9158. ((and (org-at-timestamp-p t)
  9159. (not (org-in-regexp org-bracket-link-regexp)))
  9160. (org-follow-timestamp-link))
  9161. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9162. (not (org-in-regexp org-any-link-re)))
  9163. (org-footnote-action))
  9164. (t
  9165. (let (type path link line search (pos (point)))
  9166. (catch 'match
  9167. (save-excursion
  9168. (skip-chars-forward "^]\n\r")
  9169. (when (org-in-regexp org-bracket-link-regexp 1)
  9170. (setq link (org-extract-attributes
  9171. (org-link-unescape (org-match-string-no-properties 1))))
  9172. (while (string-match " *\n *" link)
  9173. (setq link (replace-match " " t t link)))
  9174. (setq link (org-link-expand-abbrev link))
  9175. (cond
  9176. ((or (file-name-absolute-p link)
  9177. (string-match "^\\.\\.?/" link))
  9178. (setq type "file" path link))
  9179. ((string-match org-link-re-with-space3 link)
  9180. (setq type (match-string 1 link) path (match-string 2 link)))
  9181. ((string-match "^help:+\\(.+\\)" link)
  9182. (setq type "help" path (match-string 1 link)))
  9183. (t (setq type "thisfile" path link)))
  9184. (throw 'match t)))
  9185. (when (get-text-property (point) 'org-linked-text)
  9186. (setq type "thisfile"
  9187. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9188. (1+ (point)) (point))
  9189. path (buffer-substring
  9190. (or (previous-single-property-change pos 'org-linked-text)
  9191. (point-min))
  9192. (or (next-single-property-change pos 'org-linked-text)
  9193. (point-max))))
  9194. (throw 'match t))
  9195. (save-excursion
  9196. (when (or (org-in-regexp org-angle-link-re)
  9197. (let ((match (org-in-regexp org-plain-link-re)))
  9198. ;; Check a plain link is not within a bracket link
  9199. (and match
  9200. (save-excursion
  9201. (progn
  9202. (goto-char (car match))
  9203. (not (org-in-regexp org-bracket-link-regexp))))))
  9204. (let ((line_ending (save-excursion (end-of-line) (point))))
  9205. ;; We are in a line before a plain or bracket link
  9206. (or (re-search-forward org-plain-link-re line_ending t)
  9207. (re-search-forward org-bracket-link-regexp line_ending t))))
  9208. (setq type (match-string 1)
  9209. path (org-link-unescape (match-string 2)))
  9210. (throw 'match t)))
  9211. (save-excursion
  9212. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9213. (setq type "tags"
  9214. path (match-string 1))
  9215. (while (string-match ":" path)
  9216. (setq path (replace-match "+" t t path)))
  9217. (throw 'match t)))
  9218. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9219. (setq type "tree-match"
  9220. path (match-string 1))
  9221. (throw 'match t)))
  9222. (unless path
  9223. (user-error "No link found"))
  9224. ;; switch back to reference buffer
  9225. ;; needed when if called in a temporary buffer through
  9226. ;; org-open-link-from-string
  9227. (with-current-buffer (or reference-buffer (current-buffer))
  9228. ;; Remove any trailing spaces in path
  9229. (if (string-match " +\\'" path)
  9230. (setq path (replace-match "" t t path)))
  9231. (if (and org-link-translation-function
  9232. (fboundp org-link-translation-function))
  9233. ;; Check if we need to translate the link
  9234. (let ((tmp (funcall org-link-translation-function type path)))
  9235. (setq type (car tmp) path (cdr tmp))))
  9236. (cond
  9237. ((assoc type org-link-protocols)
  9238. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9239. ((equal type "help")
  9240. (let ((f-or-v (intern path)))
  9241. (cond ((fboundp f-or-v)
  9242. (describe-function f-or-v))
  9243. ((boundp f-or-v)
  9244. (describe-variable f-or-v))
  9245. (t (error "Not a known function or variable")))))
  9246. ((equal type "mailto")
  9247. (let ((cmd (car org-link-mailto-program))
  9248. (args (cdr org-link-mailto-program)) args1
  9249. (address path) (subject "") a)
  9250. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9251. (setq address (match-string 1 path)
  9252. subject (org-link-escape (match-string 2 path))))
  9253. (while args
  9254. (cond
  9255. ((not (stringp (car args))) (push (pop args) args1))
  9256. (t (setq a (pop args))
  9257. (if (string-match "%a" a)
  9258. (setq a (replace-match address t t a)))
  9259. (if (string-match "%s" a)
  9260. (setq a (replace-match subject t t a)))
  9261. (push a args1))))
  9262. (apply cmd (nreverse args1))))
  9263. ((member type '("http" "https" "ftp" "news"))
  9264. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9265. (org-link-escape
  9266. path org-link-escape-chars-browser)
  9267. path))))
  9268. ((string= type "doi")
  9269. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9270. (org-link-escape
  9271. path org-link-escape-chars-browser)
  9272. path))))
  9273. ((member type '("message"))
  9274. (browse-url (concat type ":" path)))
  9275. ((string= type "tags")
  9276. (org-tags-view arg path))
  9277. ((string= type "tree-match")
  9278. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9279. ((string= type "file")
  9280. (if (string-match "::\\([0-9]+\\)\\'" path)
  9281. (setq line (string-to-number (match-string 1 path))
  9282. path (substring path 0 (match-beginning 0)))
  9283. (if (string-match "::\\(.+\\)\\'" path)
  9284. (setq search (match-string 1 path)
  9285. path (substring path 0 (match-beginning 0)))))
  9286. (if (string-match "[*?{]" (file-name-nondirectory path))
  9287. (dired path)
  9288. (org-open-file path arg line search)))
  9289. ((string= type "shell")
  9290. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9291. (cmd path))
  9292. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9293. (string-match org-confirm-shell-link-not-regexp cmd))
  9294. (not org-confirm-shell-link-function)
  9295. (funcall org-confirm-shell-link-function
  9296. (format "Execute \"%s\" in shell? "
  9297. (org-add-props cmd nil
  9298. 'face 'org-warning))))
  9299. (progn
  9300. (message "Executing %s" cmd)
  9301. (shell-command cmd buf)
  9302. (if (featurep 'midnight)
  9303. (setq clean-buffer-list-kill-buffer-names
  9304. (cons buf clean-buffer-list-kill-buffer-names))))
  9305. (error "Abort"))))
  9306. ((string= type "elisp")
  9307. (let ((cmd path))
  9308. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9309. (string-match org-confirm-elisp-link-not-regexp cmd))
  9310. (not org-confirm-elisp-link-function)
  9311. (funcall org-confirm-elisp-link-function
  9312. (format "Execute \"%s\" as elisp? "
  9313. (org-add-props cmd nil
  9314. 'face 'org-warning))))
  9315. (message "%s => %s" cmd
  9316. (if (equal (string-to-char cmd) ?\()
  9317. (eval (read cmd))
  9318. (call-interactively (read cmd))))
  9319. (error "Abort"))))
  9320. ((and (string= type "thisfile")
  9321. (run-hook-with-args-until-success
  9322. 'org-open-link-functions path)))
  9323. ((string= type "thisfile")
  9324. (if arg
  9325. (switch-to-buffer-other-window
  9326. (org-get-buffer-for-internal-link (current-buffer)))
  9327. (org-mark-ring-push))
  9328. (let ((cmd `(org-link-search
  9329. ,path
  9330. ,(cond ((equal arg '(4)) ''occur)
  9331. ((equal arg '(16)) ''org-occur))
  9332. ,pos)))
  9333. (condition-case nil (let ((org-link-search-inhibit-query t))
  9334. (eval cmd))
  9335. (error (progn (widen) (eval cmd))))))
  9336. (t (browse-url-at-point)))))))
  9337. (move-marker org-open-link-marker nil)
  9338. (run-hook-with-args 'org-follow-link-hook)))
  9339. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9340. "Offer links in the current entry and return the selected link.
  9341. If there is only one link, return it.
  9342. If NTH is an integer, return the NTH link found.
  9343. If ZERO is a string, check also this string for a link, and if
  9344. there is one, return it."
  9345. (with-current-buffer buffer
  9346. (save-excursion
  9347. (save-restriction
  9348. (widen)
  9349. (goto-char marker)
  9350. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9351. "\\(" org-angle-link-re "\\)\\|"
  9352. "\\(" org-plain-link-re "\\)"))
  9353. (cnt ?0)
  9354. (in-emacs (if (integerp nth) nil nth))
  9355. have-zero end links link c)
  9356. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9357. (push (match-string 0 zero) links)
  9358. (setq cnt (1- cnt) have-zero t))
  9359. (save-excursion
  9360. (org-back-to-heading t)
  9361. (setq end (save-excursion (outline-next-heading) (point)))
  9362. (while (re-search-forward re end t)
  9363. (push (match-string 0) links))
  9364. (setq links (org-uniquify (reverse links))))
  9365. (cond
  9366. ((null links)
  9367. (message "No links"))
  9368. ((equal (length links) 1)
  9369. (setq link (car links)))
  9370. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9371. (setq link (nth (if have-zero nth (1- nth)) links)))
  9372. (t ; we have to select a link
  9373. (save-excursion
  9374. (save-window-excursion
  9375. (delete-other-windows)
  9376. (with-output-to-temp-buffer "*Select Link*"
  9377. (mapc (lambda (l)
  9378. (if (not (string-match org-bracket-link-regexp l))
  9379. (princ (format "[%c] %s\n" (incf cnt)
  9380. (org-remove-angle-brackets l)))
  9381. (if (match-end 3)
  9382. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9383. (match-string 3 l) (match-string 1 l)))
  9384. (princ (format "[%c] %s\n" (incf cnt)
  9385. (match-string 1 l))))))
  9386. links))
  9387. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9388. (message "Select link to open, RET to open all:")
  9389. (setq c (read-char-exclusive))
  9390. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9391. (when (equal c ?q) (error "Abort"))
  9392. (if (equal c ?\C-m)
  9393. (setq link links)
  9394. (setq nth (- c ?0))
  9395. (if have-zero (setq nth (1+ nth)))
  9396. (unless (and (integerp nth) (>= (length links) nth))
  9397. (error "Invalid link selection"))
  9398. (setq link (nth (1- nth) links)))))
  9399. (cons link end))))))
  9400. ;; Add special file links that specify the way of opening
  9401. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9402. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9403. (defun org-open-file-with-system (path)
  9404. "Open file at PATH using the system way of opening it."
  9405. (org-open-file path 'system))
  9406. (defun org-open-file-with-emacs (path)
  9407. "Open file at PATH in Emacs."
  9408. (org-open-file path 'emacs))
  9409. ;;; File search
  9410. (defvar org-create-file-search-functions nil
  9411. "List of functions to construct the right search string for a file link.
  9412. These functions are called in turn with point at the location to
  9413. which the link should point.
  9414. A function in the hook should first test if it would like to
  9415. handle this file type, for example by checking the `major-mode'
  9416. or the file extension. If it decides not to handle this file, it
  9417. should just return nil to give other functions a chance. If it
  9418. does handle the file, it must return the search string to be used
  9419. when following the link. The search string will be part of the
  9420. file link, given after a double colon, and `org-open-at-point'
  9421. will automatically search for it. If special measures must be
  9422. taken to make the search successful, another function should be
  9423. added to the companion hook `org-execute-file-search-functions',
  9424. which see.
  9425. A function in this hook may also use `setq' to set the variable
  9426. `description' to provide a suggestion for the descriptive text to
  9427. be used for this link when it gets inserted into an Org-mode
  9428. buffer with \\[org-insert-link].")
  9429. (defvar org-execute-file-search-functions nil
  9430. "List of functions to execute a file search triggered by a link.
  9431. Functions added to this hook must accept a single argument, the
  9432. search string that was part of the file link, the part after the
  9433. double colon. The function must first check if it would like to
  9434. handle this search, for example by checking the `major-mode' or
  9435. the file extension. If it decides not to handle this search, it
  9436. should just return nil to give other functions a chance. If it
  9437. does handle the search, it must return a non-nil value to keep
  9438. other functions from trying.
  9439. Each function can access the current prefix argument through the
  9440. variable `current-prefix-arg'. Note that a single prefix is used
  9441. to force opening a link in Emacs, so it may be good to only use a
  9442. numeric or double prefix to guide the search function.
  9443. In case this is needed, a function in this hook can also restore
  9444. the window configuration before `org-open-at-point' was called using:
  9445. (set-window-configuration org-window-config-before-follow-link)")
  9446. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9447. (defun org-link-search (s &optional type avoid-pos stealth)
  9448. "Search for a link search option.
  9449. If S is surrounded by forward slashes, it is interpreted as a
  9450. regular expression. In org-mode files, this will create an `org-occur'
  9451. sparse tree. In ordinary files, `occur' will be used to list matches.
  9452. If the current buffer is in `dired-mode', grep will be used to search
  9453. in all files. If AVOID-POS is given, ignore matches near that position.
  9454. When optional argument STEALTH is non-nil, do not modify
  9455. visibility around point, thus ignoring
  9456. `org-show-hierarchy-above', `org-show-following-heading' and
  9457. `org-show-siblings' variables."
  9458. (let ((case-fold-search t)
  9459. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9460. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9461. (append '(("") (" ") ("\t") ("\n"))
  9462. org-emphasis-alist)
  9463. "\\|") "\\)"))
  9464. (pos (point))
  9465. (pre nil) (post nil)
  9466. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9467. (cond
  9468. ;; First check if there are any special search functions
  9469. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9470. ;; Now try the builtin stuff
  9471. ((and (equal (string-to-char s0) ?#)
  9472. (> (length s0) 1)
  9473. (save-excursion
  9474. (goto-char (point-min))
  9475. (and
  9476. (re-search-forward
  9477. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9478. (setq type 'dedicated
  9479. pos (match-beginning 0))))
  9480. ;; There is an exact target for this
  9481. (goto-char pos)
  9482. (org-back-to-heading t)))
  9483. ((save-excursion
  9484. (goto-char (point-min))
  9485. (and
  9486. (re-search-forward
  9487. (concat "<<" (regexp-quote s0) ">>") nil t)
  9488. (setq type 'dedicated
  9489. pos (match-beginning 0))))
  9490. ;; There is an exact target for this
  9491. (goto-char pos))
  9492. ((save-excursion
  9493. (goto-char (point-min))
  9494. (and
  9495. (re-search-forward
  9496. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9497. (setq type 'dedicated pos (match-beginning 0))))
  9498. ;; Found an invisible target.
  9499. (goto-char pos))
  9500. ((save-excursion
  9501. (goto-char (point-min))
  9502. (and
  9503. (re-search-forward
  9504. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9505. (setq type 'dedicated pos (match-beginning 0))))
  9506. ;; Found an element with a matching #+name affiliated keyword.
  9507. (goto-char pos))
  9508. ((and (string-match "^(\\(.*\\))$" s0)
  9509. (save-excursion
  9510. (goto-char (point-min))
  9511. (and
  9512. (re-search-forward
  9513. (concat "[^[]" (regexp-quote
  9514. (format org-coderef-label-format
  9515. (match-string 1 s0))))
  9516. nil t)
  9517. (setq type 'dedicated
  9518. pos (1+ (match-beginning 0))))))
  9519. ;; There is a coderef target for this
  9520. (goto-char pos))
  9521. ((string-match "^/\\(.*\\)/$" s)
  9522. ;; A regular expression
  9523. (cond
  9524. ((derived-mode-p 'org-mode)
  9525. (org-occur (match-string 1 s)))
  9526. (t (org-do-occur (match-string 1 s)))))
  9527. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9528. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9529. (goto-char (point-min))
  9530. (cond
  9531. ((let (case-fold-search)
  9532. (re-search-forward (format org-complex-heading-regexp-format
  9533. (regexp-quote s))
  9534. nil t))
  9535. ;; OK, found a match
  9536. (setq type 'dedicated)
  9537. (goto-char (match-beginning 0)))
  9538. ((and (not org-link-search-inhibit-query)
  9539. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9540. (y-or-n-p "No match - create this as a new heading? "))
  9541. (goto-char (point-max))
  9542. (or (bolp) (newline))
  9543. (insert "* " s "\n")
  9544. (beginning-of-line 0))
  9545. (t
  9546. (goto-char pos)
  9547. (error "No match"))))
  9548. (t
  9549. ;; A normal search string
  9550. (when (equal (string-to-char s) ?*)
  9551. ;; Anchor on headlines, post may include tags.
  9552. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9553. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9554. s (substring s 1)))
  9555. (remove-text-properties
  9556. 0 (length s)
  9557. '(face nil mouse-face nil keymap nil fontified nil) s)
  9558. ;; Make a series of regular expressions to find a match
  9559. (setq words (org-split-string s "[ \n\r\t]+")
  9560. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9561. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9562. "\\)" markers)
  9563. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9564. re2a (concat "[ \t\r\n]" re2a_)
  9565. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9566. re4 (concat "[^a-zA-Z_]" re4_)
  9567. re1 (concat pre re2 post)
  9568. re3 (concat pre (if pre re4_ re4) post)
  9569. re5 (concat pre ".*" re4)
  9570. re2 (concat pre re2)
  9571. re2a (concat pre (if pre re2a_ re2a))
  9572. re4 (concat pre (if pre re4_ re4))
  9573. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9574. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9575. re5 "\\)"
  9576. ))
  9577. (cond
  9578. ((eq type 'org-occur) (org-occur reall))
  9579. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9580. (t (goto-char (point-min))
  9581. (setq type 'fuzzy)
  9582. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9583. (org-search-not-self 1 re1 nil t)
  9584. (org-search-not-self 1 re2 nil t)
  9585. (org-search-not-self 1 re2a nil t)
  9586. (org-search-not-self 1 re3 nil t)
  9587. (org-search-not-self 1 re4 nil t)
  9588. (org-search-not-self 1 re5 nil t)
  9589. )
  9590. (goto-char (match-beginning 1))
  9591. (goto-char pos)
  9592. (error "No match"))))))
  9593. (and (derived-mode-p 'org-mode)
  9594. (not stealth)
  9595. (org-show-context 'link-search))
  9596. type))
  9597. (defun org-search-not-self (group &rest args)
  9598. "Execute `re-search-forward', but only accept matches that do not
  9599. enclose the position of `org-open-link-marker'."
  9600. (let ((m org-open-link-marker))
  9601. (catch 'exit
  9602. (while (apply 're-search-forward args)
  9603. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9604. (goto-char (match-end group))
  9605. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9606. (> (match-beginning 0) (marker-position m))
  9607. (< (match-end 0) (marker-position m)))
  9608. (save-match-data
  9609. (or (not (org-in-regexp
  9610. org-bracket-link-analytic-regexp 1))
  9611. (not (match-end 4)) ; no description
  9612. (and (<= (match-beginning 4) (point))
  9613. (>= (match-end 4) (point))))))
  9614. (throw 'exit (point))))))))
  9615. (defun org-get-buffer-for-internal-link (buffer)
  9616. "Return a buffer to be used for displaying the link target of internal links."
  9617. (cond
  9618. ((not org-display-internal-link-with-indirect-buffer)
  9619. buffer)
  9620. ((string-match "(Clone)$" (buffer-name buffer))
  9621. (message "Buffer is already a clone, not making another one")
  9622. ;; we also do not modify visibility in this case
  9623. buffer)
  9624. (t ; make a new indirect buffer for displaying the link
  9625. (let* ((bn (buffer-name buffer))
  9626. (ibn (concat bn "(Clone)"))
  9627. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9628. (with-current-buffer ib (org-overview))
  9629. ib))))
  9630. (defun org-do-occur (regexp &optional cleanup)
  9631. "Call the Emacs command `occur'.
  9632. If CLEANUP is non-nil, remove the printout of the regular expression
  9633. in the *Occur* buffer. This is useful if the regex is long and not useful
  9634. to read."
  9635. (occur regexp)
  9636. (when cleanup
  9637. (let ((cwin (selected-window)) win beg end)
  9638. (when (setq win (get-buffer-window "*Occur*"))
  9639. (select-window win))
  9640. (goto-char (point-min))
  9641. (when (re-search-forward "match[a-z]+" nil t)
  9642. (setq beg (match-end 0))
  9643. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9644. (setq end (1- (match-beginning 0)))))
  9645. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9646. (goto-char (point-min))
  9647. (select-window cwin))))
  9648. ;;; The mark ring for links jumps
  9649. (defvar org-mark-ring nil
  9650. "Mark ring for positions before jumps in Org-mode.")
  9651. (defvar org-mark-ring-last-goto nil
  9652. "Last position in the mark ring used to go back.")
  9653. ;; Fill and close the ring
  9654. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9655. (loop for i from 1 to org-mark-ring-length do
  9656. (push (make-marker) org-mark-ring))
  9657. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9658. org-mark-ring)
  9659. (defun org-mark-ring-push (&optional pos buffer)
  9660. "Put the current position or POS into the mark ring and rotate it."
  9661. (interactive)
  9662. (setq pos (or pos (point)))
  9663. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9664. (move-marker (car org-mark-ring)
  9665. (or pos (point))
  9666. (or buffer (current-buffer)))
  9667. (message "%s"
  9668. (substitute-command-keys
  9669. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9670. (defun org-mark-ring-goto (&optional n)
  9671. "Jump to the previous position in the mark ring.
  9672. With prefix arg N, jump back that many stored positions. When
  9673. called several times in succession, walk through the entire ring.
  9674. Org-mode commands jumping to a different position in the current file,
  9675. or to another Org-mode file, automatically push the old position
  9676. onto the ring."
  9677. (interactive "p")
  9678. (let (p m)
  9679. (if (eq last-command this-command)
  9680. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9681. (setq p org-mark-ring))
  9682. (setq org-mark-ring-last-goto p)
  9683. (setq m (car p))
  9684. (org-pop-to-buffer-same-window (marker-buffer m))
  9685. (goto-char m)
  9686. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9687. (defun org-remove-angle-brackets (s)
  9688. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9689. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9690. s)
  9691. (defun org-add-angle-brackets (s)
  9692. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9693. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9694. s)
  9695. (defun org-remove-double-quotes (s)
  9696. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9697. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9698. s)
  9699. ;;; Following specific links
  9700. (defun org-follow-timestamp-link ()
  9701. "Open an agenda view for the time-stamp date/range at point."
  9702. (cond
  9703. ((org-at-date-range-p t)
  9704. (let ((org-agenda-start-on-weekday)
  9705. (t1 (match-string 1))
  9706. (t2 (match-string 2)) tt1 tt2)
  9707. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9708. tt2 (time-to-days (org-time-string-to-time t2)))
  9709. (let ((org-agenda-buffer-tmp-name
  9710. (format "*Org Agenda(a:%s)"
  9711. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9712. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9713. ((org-at-timestamp-p t)
  9714. (let ((org-agenda-buffer-tmp-name
  9715. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9716. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9717. (substring (match-string 1) 0 10)))
  9718. 1)))
  9719. (t (error "This should not happen"))))
  9720. ;;; Following file links
  9721. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9722. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9723. (declare-function mailcap-mime-info
  9724. "mailcap" (string &optional request no-decode))
  9725. (defvar org-wait nil)
  9726. (defun org-open-file (path &optional in-emacs line search)
  9727. "Open the file at PATH.
  9728. First, this expands any special file name abbreviations. Then the
  9729. configuration variable `org-file-apps' is checked if it contains an
  9730. entry for this file type, and if yes, the corresponding command is launched.
  9731. If no application is found, Emacs simply visits the file.
  9732. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9733. With a double \\[universal-argument] \\[universal-argument] \
  9734. prefix arg, Org tries to avoid opening in Emacs
  9735. and to use an external application to visit the file.
  9736. Optional LINE specifies a line to go to, optional SEARCH a string
  9737. to search for. If LINE or SEARCH is given, the file will be
  9738. opened in Emacs, unless an entry from org-file-apps that makes
  9739. use of groups in a regexp matches.
  9740. If you want to change the way frames are used when following a
  9741. link, please customize `org-link-frame-setup'.
  9742. If the file does not exist, an error is thrown."
  9743. (let* ((file (if (equal path "")
  9744. buffer-file-name
  9745. (substitute-in-file-name (expand-file-name path))))
  9746. (file-apps (append org-file-apps (org-default-apps)))
  9747. (apps (org-remove-if
  9748. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9749. (apps-dlink (org-remove-if-not
  9750. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9751. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9752. (dirp (if remp nil (file-directory-p file)))
  9753. (file (if (and dirp org-open-directory-means-index-dot-org)
  9754. (concat (file-name-as-directory file) "index.org")
  9755. file))
  9756. (a-m-a-p (assq 'auto-mode apps))
  9757. (dfile (downcase file))
  9758. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9759. (link (cond ((and (eq line nil)
  9760. (eq search nil))
  9761. file)
  9762. (line
  9763. (concat file "::" (number-to-string line)))
  9764. (search
  9765. (concat file "::" search))))
  9766. (dlink (downcase link))
  9767. (old-buffer (current-buffer))
  9768. (old-pos (point))
  9769. (old-mode major-mode)
  9770. ext cmd link-match-data)
  9771. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9772. (setq ext (match-string 1 dfile))
  9773. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9774. (setq ext (match-string 1 dfile))))
  9775. (cond
  9776. ((member in-emacs '((16) system))
  9777. (setq cmd (cdr (assoc 'system apps))))
  9778. (in-emacs (setq cmd 'emacs))
  9779. (t
  9780. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9781. (and dirp (cdr (assoc 'directory apps)))
  9782. ; first, try matching against apps-dlink
  9783. ; if we get a match here, store the match data for later
  9784. (let ((match (assoc-default dlink apps-dlink
  9785. 'string-match)))
  9786. (if match
  9787. (progn (setq link-match-data (match-data))
  9788. match)
  9789. (progn (setq in-emacs (or in-emacs line search))
  9790. nil))) ; if we have no match in apps-dlink,
  9791. ; always open the file in emacs if line or search
  9792. ; is given (for backwards compatibility)
  9793. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9794. 'string-match)
  9795. (cdr (assoc ext apps))
  9796. (cdr (assoc t apps))))))
  9797. (when (eq cmd 'system)
  9798. (setq cmd (cdr (assoc 'system apps))))
  9799. (when (eq cmd 'default)
  9800. (setq cmd (cdr (assoc t apps))))
  9801. (when (eq cmd 'mailcap)
  9802. (require 'mailcap)
  9803. (mailcap-parse-mailcaps)
  9804. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9805. (command (mailcap-mime-info mime-type)))
  9806. (if (stringp command)
  9807. (setq cmd command)
  9808. (setq cmd 'emacs))))
  9809. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9810. (not (file-exists-p file))
  9811. (not org-open-non-existing-files))
  9812. (error "No such file: %s" file))
  9813. (cond
  9814. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9815. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9816. (while (string-match "['\"]%s['\"]" cmd)
  9817. (setq cmd (replace-match "%s" t t cmd)))
  9818. (while (string-match "%s" cmd)
  9819. (setq cmd (replace-match
  9820. (save-match-data
  9821. (shell-quote-argument
  9822. (convert-standard-filename file)))
  9823. t t cmd)))
  9824. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9825. (save-match-data
  9826. (let ((match-index 1)
  9827. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9828. (set-match-data link-match-data)
  9829. (while (<= match-index number-of-groups)
  9830. (let ((regex (concat "%" (number-to-string match-index)))
  9831. (replace-with (match-string match-index dlink)))
  9832. (while (string-match regex cmd)
  9833. (setq cmd (replace-match replace-with t t cmd))))
  9834. (setq match-index (+ match-index 1)))))
  9835. (save-window-excursion
  9836. (message "Running %s...done" cmd)
  9837. (start-process-shell-command cmd nil cmd)
  9838. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9839. ((or (stringp cmd)
  9840. (eq cmd 'emacs))
  9841. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9842. (widen)
  9843. (if line (org-goto-line line)
  9844. (if search (org-link-search search))))
  9845. ((consp cmd)
  9846. (let ((file (convert-standard-filename file)))
  9847. (save-match-data
  9848. (set-match-data link-match-data)
  9849. (eval cmd))))
  9850. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9851. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9852. (or (not (equal old-buffer (current-buffer)))
  9853. (not (equal old-pos (point))))
  9854. (org-mark-ring-push old-pos old-buffer))))
  9855. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9856. "This function returns non-nil if `entry' uses a regular
  9857. expression which should be matched against the whole link by
  9858. org-open-file.
  9859. It assumes that is the case when the entry uses a regular
  9860. expression which has at least one grouping construct and the
  9861. action is either a lisp form or a command string containing
  9862. '%1', i.e. using at least one subexpression match as a
  9863. parameter."
  9864. (let ((selector (car entry))
  9865. (action (cdr entry)))
  9866. (if (stringp selector)
  9867. (and (> (regexp-opt-depth selector) 0)
  9868. (or (and (stringp action)
  9869. (string-match "%[0-9]" action))
  9870. (consp action)))
  9871. nil)))
  9872. (defun org-default-apps ()
  9873. "Return the default applications for this operating system."
  9874. (cond
  9875. ((eq system-type 'darwin)
  9876. org-file-apps-defaults-macosx)
  9877. ((eq system-type 'windows-nt)
  9878. org-file-apps-defaults-windowsnt)
  9879. (t org-file-apps-defaults-gnu)))
  9880. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9881. "Convert extensions to regular expressions in the cars of LIST.
  9882. Also, weed out any non-string entries, because the return value is used
  9883. only for regexp matching.
  9884. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9885. point to the symbol `emacs', indicating that the file should
  9886. be opened in Emacs."
  9887. (append
  9888. (delq nil
  9889. (mapcar (lambda (x)
  9890. (if (not (stringp (car x)))
  9891. nil
  9892. (if (string-match "\\W" (car x))
  9893. x
  9894. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9895. list))
  9896. (if add-auto-mode
  9897. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9898. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9899. (defun org-file-remote-p (file)
  9900. "Test whether FILE specifies a location on a remote system.
  9901. Return non-nil if the location is indeed remote.
  9902. For example, the filename \"/user@host:/foo\" specifies a location
  9903. on the system \"/user@host:\"."
  9904. (cond ((fboundp 'file-remote-p)
  9905. (file-remote-p file))
  9906. ((fboundp 'tramp-handle-file-remote-p)
  9907. (tramp-handle-file-remote-p file))
  9908. ((and (boundp 'ange-ftp-name-format)
  9909. (string-match (car ange-ftp-name-format) file))
  9910. t)))
  9911. ;;;; Refiling
  9912. (defun org-get-org-file ()
  9913. "Read a filename, with default directory `org-directory'."
  9914. (let ((default (or org-default-notes-file remember-data-file)))
  9915. (read-file-name (format "File name [%s]: " default)
  9916. (file-name-as-directory org-directory)
  9917. default)))
  9918. (defun org-notes-order-reversed-p ()
  9919. "Check if the current file should receive notes in reversed order."
  9920. (cond
  9921. ((not org-reverse-note-order) nil)
  9922. ((eq t org-reverse-note-order) t)
  9923. ((not (listp org-reverse-note-order)) nil)
  9924. (t (catch 'exit
  9925. (let ((all org-reverse-note-order)
  9926. entry)
  9927. (while (setq entry (pop all))
  9928. (if (string-match (car entry) buffer-file-name)
  9929. (throw 'exit (cdr entry))))
  9930. nil)))))
  9931. (defvar org-refile-target-table nil
  9932. "The list of refile targets, created by `org-refile'.")
  9933. (defvar org-agenda-new-buffers nil
  9934. "Buffers created to visit agenda files.")
  9935. (defvar org-refile-cache nil
  9936. "Cache for refile targets.")
  9937. (defvar org-refile-markers nil
  9938. "All the markers used for caching refile locations.")
  9939. (defun org-refile-marker (pos)
  9940. "Get a new refile marker, but only if caching is in use."
  9941. (if (not org-refile-use-cache)
  9942. pos
  9943. (let ((m (make-marker)))
  9944. (move-marker m pos)
  9945. (push m org-refile-markers)
  9946. m)))
  9947. (defun org-refile-cache-clear ()
  9948. "Clear the refile cache and disable all the markers."
  9949. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9950. (setq org-refile-markers nil)
  9951. (setq org-refile-cache nil)
  9952. (message "Refile cache has been cleared"))
  9953. (defun org-refile-cache-check-set (set)
  9954. "Check if all the markers in the cache still have live buffers."
  9955. (let (marker)
  9956. (catch 'exit
  9957. (while (and set (setq marker (nth 3 (pop set))))
  9958. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9959. (when (and marker (null (marker-buffer marker)))
  9960. (message "not found") (sit-for 3)
  9961. (throw 'exit nil)))
  9962. t)))
  9963. (defun org-refile-cache-put (set &rest identifiers)
  9964. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9965. (let* ((key (sha1 (prin1-to-string identifiers)))
  9966. (entry (assoc key org-refile-cache)))
  9967. (if entry
  9968. (setcdr entry set)
  9969. (push (cons key set) org-refile-cache))))
  9970. (defun org-refile-cache-get (&rest identifiers)
  9971. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9972. (cond
  9973. ((not org-refile-cache) nil)
  9974. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9975. (t
  9976. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9977. org-refile-cache))))
  9978. (and set (org-refile-cache-check-set set) set)))))
  9979. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9980. "Produce a table with refile targets."
  9981. (let ((case-fold-search nil)
  9982. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9983. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9984. targets tgs txt re files f desc descre fast-path-p level pos0)
  9985. (message "Getting targets...")
  9986. (with-current-buffer (or default-buffer (current-buffer))
  9987. (while (setq entry (pop entries))
  9988. (setq files (car entry) desc (cdr entry))
  9989. (setq fast-path-p nil)
  9990. (cond
  9991. ((null files) (setq files (list (current-buffer))))
  9992. ((eq files 'org-agenda-files)
  9993. (setq files (org-agenda-files 'unrestricted)))
  9994. ((and (symbolp files) (fboundp files))
  9995. (setq files (funcall files)))
  9996. ((and (symbolp files) (boundp files))
  9997. (setq files (symbol-value files))))
  9998. (if (stringp files) (setq files (list files)))
  9999. (cond
  10000. ((eq (car desc) :tag)
  10001. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10002. ((eq (car desc) :todo)
  10003. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10004. ((eq (car desc) :regexp)
  10005. (setq descre (cdr desc)))
  10006. ((eq (car desc) :level)
  10007. (setq descre (concat "^\\*\\{" (number-to-string
  10008. (if org-odd-levels-only
  10009. (1- (* 2 (cdr desc)))
  10010. (cdr desc)))
  10011. "\\}[ \t]")))
  10012. ((eq (car desc) :maxlevel)
  10013. (setq fast-path-p t)
  10014. (setq descre (concat "^\\*\\{1," (number-to-string
  10015. (if org-odd-levels-only
  10016. (1- (* 2 (cdr desc)))
  10017. (cdr desc)))
  10018. "\\}[ \t]")))
  10019. (t (error "Bad refiling target description %s" desc)))
  10020. (while (setq f (pop files))
  10021. (with-current-buffer
  10022. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10023. (or
  10024. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10025. (progn
  10026. (if (bufferp f) (setq f (buffer-file-name
  10027. (buffer-base-buffer f))))
  10028. (setq f (and f (expand-file-name f)))
  10029. (if (eq org-refile-use-outline-path 'file)
  10030. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10031. (save-excursion
  10032. (save-restriction
  10033. (widen)
  10034. (goto-char (point-min))
  10035. (while (re-search-forward descre nil t)
  10036. (goto-char (setq pos0 (point-at-bol)))
  10037. (catch 'next
  10038. (when org-refile-target-verify-function
  10039. (save-match-data
  10040. (or (funcall org-refile-target-verify-function)
  10041. (throw 'next t))))
  10042. (when (and (looking-at org-complex-heading-regexp)
  10043. (not (member (match-string 4) excluded-entries))
  10044. (match-string 4))
  10045. (setq level (org-reduced-level
  10046. (- (match-end 1) (match-beginning 1)))
  10047. txt (org-link-display-format (match-string 4))
  10048. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10049. re (format org-complex-heading-regexp-format
  10050. (regexp-quote (match-string 4))))
  10051. (when org-refile-use-outline-path
  10052. (setq txt (mapconcat
  10053. 'org-protect-slash
  10054. (append
  10055. (if (eq org-refile-use-outline-path
  10056. 'file)
  10057. (list (file-name-nondirectory
  10058. (buffer-file-name
  10059. (buffer-base-buffer))))
  10060. (if (eq org-refile-use-outline-path
  10061. 'full-file-path)
  10062. (list (buffer-file-name
  10063. (buffer-base-buffer)))))
  10064. (org-get-outline-path fast-path-p
  10065. level txt)
  10066. (list txt))
  10067. "/")))
  10068. (push (list txt f re (org-refile-marker (point)))
  10069. tgs)))
  10070. (when (= (point) pos0)
  10071. ;; verification function has not moved point
  10072. (goto-char (point-at-eol))))))))
  10073. (when org-refile-use-cache
  10074. (org-refile-cache-put tgs (buffer-file-name) descre))
  10075. (setq targets (append tgs targets))
  10076. ))))
  10077. (message "Getting targets...done")
  10078. (nreverse targets)))
  10079. (defun org-protect-slash (s)
  10080. (while (string-match "/" s)
  10081. (setq s (replace-match "\\" t t s)))
  10082. s)
  10083. (defvar org-olpa (make-vector 20 nil))
  10084. (defun org-get-outline-path (&optional fastp level heading)
  10085. "Return the outline path to the current entry, as a list.
  10086. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10087. routine which makes outline path derivations for an entire file,
  10088. avoiding backtracing. Refile target collection makes use of that."
  10089. (if fastp
  10090. (progn
  10091. (if (> level 19)
  10092. (error "Outline path failure, more than 19 levels"))
  10093. (loop for i from level upto 19 do
  10094. (aset org-olpa i nil))
  10095. (prog1
  10096. (delq nil (append org-olpa nil))
  10097. (aset org-olpa level heading)))
  10098. (let (rtn case-fold-search)
  10099. (save-excursion
  10100. (save-restriction
  10101. (widen)
  10102. (while (org-up-heading-safe)
  10103. (when (looking-at org-complex-heading-regexp)
  10104. (push (org-match-string-no-properties 4) rtn)))
  10105. rtn)))))
  10106. (defun org-format-outline-path (path &optional width prefix separator)
  10107. "Format the outline path PATH for display.
  10108. WIDTH is the maximum number of characters that is available.
  10109. PREFIX is a prefix to be included in the returned string,
  10110. such as the file name.
  10111. SEPARATOR is inserted between the different parts of the path,
  10112. the default is \"/\"."
  10113. (setq width (or width 79))
  10114. (if prefix (setq width (- width (length prefix))))
  10115. (if (not path)
  10116. (or prefix "")
  10117. (let* ((nsteps (length path))
  10118. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10119. (maxwidth (if (<= total-width width)
  10120. 10000 ;; everything fits
  10121. ;; we need to shorten the level headings
  10122. (/ (- width nsteps) nsteps)))
  10123. (org-odd-levels-only nil)
  10124. (n 0)
  10125. (total (1+ (length prefix))))
  10126. (setq maxwidth (max maxwidth 10))
  10127. (concat prefix
  10128. (if prefix (or separator "/"))
  10129. (mapconcat
  10130. (lambda (h)
  10131. (setq n (1+ n))
  10132. (if (and (= n nsteps) (< maxwidth 10000))
  10133. (setq maxwidth (- total-width total)))
  10134. (if (< (length h) maxwidth)
  10135. (progn (setq total (+ total (length h) 1)) h)
  10136. (setq h (substring h 0 (- maxwidth 2))
  10137. total (+ total maxwidth 1))
  10138. (if (string-match "[ \t]+\\'" h)
  10139. (setq h (substring h 0 (match-beginning 0))))
  10140. (setq h (concat h "..")))
  10141. (org-add-props h nil 'face
  10142. (nth (% (1- n) org-n-level-faces)
  10143. org-level-faces))
  10144. h)
  10145. path (or separator "/"))))))
  10146. (defun org-display-outline-path (&optional file current separator just-return-string)
  10147. "Display the current outline path in the echo area.
  10148. If FILE is non-nil, prepend the output with the file name.
  10149. If CURRENT is non-nil, append the current heading to the output.
  10150. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10151. the different parts of the path and defaults to \"/\".
  10152. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10153. (interactive "P")
  10154. (let* (case-fold-search
  10155. message-log-max ; Don't populate the *Messages* buffer
  10156. (bfn (buffer-file-name (buffer-base-buffer)))
  10157. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10158. res)
  10159. (if current (setq path (append path
  10160. (save-excursion
  10161. (org-back-to-heading t)
  10162. (if (looking-at org-complex-heading-regexp)
  10163. (list (match-string 4)))))))
  10164. (setq res
  10165. (org-format-outline-path
  10166. path
  10167. (1- (frame-width))
  10168. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10169. separator))
  10170. (if just-return-string
  10171. (org-no-properties res)
  10172. (message "%s" res))))
  10173. (defvar org-refile-history nil
  10174. "History for refiling operations.")
  10175. (defvar org-after-refile-insert-hook nil
  10176. "Hook run after `org-refile' has inserted its stuff at the new location.
  10177. Note that this is still *before* the stuff will be removed from
  10178. the *old* location.")
  10179. (defvar org-capture-last-stored-marker)
  10180. (defvar org-refile-keep nil
  10181. "Non-nil means `org-refile' will copy instead of refile.")
  10182. (defun org-copy ()
  10183. "Like `org-refile', but copy."
  10184. (interactive)
  10185. (let ((org-refile-keep t))
  10186. (funcall 'org-refile nil nil nil "Copy")))
  10187. (defun org-refile (&optional goto default-buffer rfloc msg)
  10188. "Move the entry or entries at point to another heading.
  10189. The list of target headings is compiled using the information in
  10190. `org-refile-targets', which see.
  10191. At the target location, the entry is filed as a subitem of the target
  10192. heading. Depending on `org-reverse-note-order', the new subitem will
  10193. either be the first or the last subitem.
  10194. If there is an active region, all entries in that region will be moved.
  10195. However, the region must fulfill the requirement that the first heading
  10196. is the first one sets the top-level of the moved text - at most siblings
  10197. below it are allowed.
  10198. With prefix arg GOTO, the command will only visit the target location
  10199. and not actually move anything.
  10200. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10201. go to the location where the last refiling operation has put the subtree.
  10202. With a prefix argument of `2', refile to the running clock.
  10203. RFLOC can be a refile location obtained in a different way.
  10204. MSG is a string to replace \"Refile\" in the default prompt with
  10205. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10206. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10207. If you are using target caching (see `org-refile-use-cache'),
  10208. you have to clear the target cache in order to find new targets.
  10209. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10210. prefix argument (`C-u C-u C-u C-c C-w')."
  10211. (interactive "P")
  10212. (if (member goto '(0 (64)))
  10213. (org-refile-cache-clear)
  10214. (let* ((actionmsg (or msg "Refile"))
  10215. (cbuf (current-buffer))
  10216. (regionp (org-region-active-p))
  10217. (region-start (and regionp (region-beginning)))
  10218. (region-end (and regionp (region-end)))
  10219. (region-length (and regionp (- region-end region-start)))
  10220. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10221. pos it nbuf file re level reversed)
  10222. (setq last-command nil)
  10223. (when regionp
  10224. (goto-char region-start)
  10225. (or (bolp) (goto-char (point-at-bol)))
  10226. (setq region-start (point))
  10227. (unless (or (org-kill-is-subtree-p
  10228. (buffer-substring region-start region-end))
  10229. (prog1 org-refile-active-region-within-subtree
  10230. (org-toggle-heading)))
  10231. (error "The region is not a (sequence of) subtree(s)")))
  10232. (if (equal goto '(16))
  10233. (org-refile-goto-last-stored)
  10234. (when (or
  10235. (and (equal goto 2)
  10236. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10237. (prog1
  10238. (setq it (list (or org-clock-heading "running clock")
  10239. (buffer-file-name
  10240. (marker-buffer org-clock-hd-marker))
  10241. ""
  10242. (marker-position org-clock-hd-marker)))
  10243. (setq goto nil)))
  10244. (setq it (or rfloc
  10245. (let (heading-text)
  10246. (save-excursion
  10247. (unless goto
  10248. (org-back-to-heading t)
  10249. (setq heading-text
  10250. (nth 4 (org-heading-components))))
  10251. (org-refile-get-location
  10252. (cond (goto "Goto")
  10253. (regionp (concat actionmsg " region to"))
  10254. (t (concat actionmsg " subtree \""
  10255. heading-text "\" to")))
  10256. default-buffer
  10257. (and (not (equal '(4) goto))
  10258. org-refile-allow-creating-parent-nodes)
  10259. goto))))))
  10260. (setq file (nth 1 it)
  10261. re (nth 2 it)
  10262. pos (nth 3 it))
  10263. (if (and (not goto)
  10264. pos
  10265. (equal (buffer-file-name) file)
  10266. (if regionp
  10267. (and (>= pos region-start)
  10268. (<= pos region-end))
  10269. (and (>= pos (point))
  10270. (< pos (save-excursion
  10271. (org-end-of-subtree t t))))))
  10272. (error "Cannot refile to position inside the tree or region"))
  10273. (setq nbuf (or (find-buffer-visiting file)
  10274. (find-file-noselect file)))
  10275. (if goto
  10276. (progn
  10277. (org-pop-to-buffer-same-window nbuf)
  10278. (goto-char pos)
  10279. (org-show-context 'org-goto))
  10280. (if regionp
  10281. (progn
  10282. (org-kill-new (buffer-substring region-start region-end))
  10283. (org-save-markers-in-region region-start region-end))
  10284. (org-copy-subtree 1 nil t))
  10285. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10286. (find-file-noselect file)))
  10287. (setq reversed (org-notes-order-reversed-p))
  10288. (save-excursion
  10289. (save-restriction
  10290. (widen)
  10291. (if pos
  10292. (progn
  10293. (goto-char pos)
  10294. (looking-at org-outline-regexp)
  10295. (setq level (org-get-valid-level (funcall outline-level) 1))
  10296. (goto-char
  10297. (if reversed
  10298. (or (outline-next-heading) (point-max))
  10299. (or (save-excursion (org-get-next-sibling))
  10300. (org-end-of-subtree t t)
  10301. (point-max)))))
  10302. (setq level 1)
  10303. (if (not reversed)
  10304. (goto-char (point-max))
  10305. (goto-char (point-min))
  10306. (or (outline-next-heading) (goto-char (point-max)))))
  10307. (if (not (bolp)) (newline))
  10308. (org-paste-subtree level)
  10309. (when org-log-refile
  10310. (org-add-log-setup 'refile nil nil 'findpos
  10311. org-log-refile)
  10312. (unless (eq org-log-refile 'note)
  10313. (save-excursion (org-add-log-note))))
  10314. (and org-auto-align-tags
  10315. (let ((org-loop-over-headlines-in-active-region nil))
  10316. (org-set-tags nil t)))
  10317. (with-demoted-errors
  10318. (bookmark-set "org-refile-last-stored"))
  10319. ;; If we are refiling for capture, make sure that the
  10320. ;; last-capture pointers point here
  10321. (when (org-bound-and-true-p org-refile-for-capture)
  10322. (with-demoted-errors
  10323. (bookmark-set "org-capture-last-stored-marker"))
  10324. (move-marker org-capture-last-stored-marker (point)))
  10325. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10326. (run-hooks 'org-after-refile-insert-hook))))
  10327. (unless org-refile-keep
  10328. (if regionp
  10329. (delete-region (point) (+ (point) region-length))
  10330. (org-cut-subtree)))
  10331. (when (featurep 'org-inlinetask)
  10332. (org-inlinetask-remove-END-maybe))
  10333. (setq org-markers-to-move nil)
  10334. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10335. (defun org-refile-goto-last-stored ()
  10336. "Go to the location where the last refile was stored."
  10337. (interactive)
  10338. (bookmark-jump "org-refile-last-stored")
  10339. (message "This is the location of the last refile"))
  10340. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10341. no-exclude)
  10342. "Prompt the user for a refile location, using PROMPT.
  10343. PROMPT should not be suffixed with a colon and a space, because
  10344. this function appends the default value from
  10345. `org-refile-history' automatically, if that is not empty.
  10346. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10347. this is used for the GOTO interface."
  10348. (let ((org-refile-targets org-refile-targets)
  10349. (org-refile-use-outline-path org-refile-use-outline-path)
  10350. excluded-entries)
  10351. (when (and (derived-mode-p 'org-mode)
  10352. (not org-refile-use-cache)
  10353. (not no-exclude))
  10354. (org-map-tree
  10355. (lambda()
  10356. (setq excluded-entries
  10357. (append excluded-entries (list (org-get-heading t t)))))))
  10358. (setq org-refile-target-table
  10359. (org-refile-get-targets default-buffer excluded-entries)))
  10360. (unless org-refile-target-table
  10361. (error "No refile targets"))
  10362. (let* ((cbuf (current-buffer))
  10363. (partial-completion-mode nil)
  10364. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10365. (cfunc (if (and org-refile-use-outline-path
  10366. org-outline-path-complete-in-steps)
  10367. 'org-olpath-completing-read
  10368. 'org-icompleting-read))
  10369. (extra (if org-refile-use-outline-path "/" ""))
  10370. (cbnex (concat (buffer-name) extra))
  10371. (filename (and cfn (expand-file-name cfn)))
  10372. (tbl (mapcar
  10373. (lambda (x)
  10374. (if (and (not (member org-refile-use-outline-path
  10375. '(file full-file-path)))
  10376. (not (equal filename (nth 1 x))))
  10377. (cons (concat (car x) extra " ("
  10378. (file-name-nondirectory (nth 1 x)) ")")
  10379. (cdr x))
  10380. (cons (concat (car x) extra) (cdr x))))
  10381. org-refile-target-table))
  10382. (completion-ignore-case t)
  10383. cdef
  10384. (prompt (concat prompt
  10385. (or (and (car org-refile-history)
  10386. (concat " (default " (car org-refile-history) ")"))
  10387. (and (assoc cbnex tbl) (setq cdef cbnex)
  10388. (concat " (default " cbnex ")"))) ": "))
  10389. pa answ parent-target child parent old-hist)
  10390. (setq old-hist org-refile-history)
  10391. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10392. nil 'org-refile-history (or cdef (car org-refile-history))))
  10393. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10394. (org-refile-check-position pa)
  10395. (if pa
  10396. (progn
  10397. (when (or (not org-refile-history)
  10398. (not (eq old-hist org-refile-history))
  10399. (not (equal (car pa) (car org-refile-history))))
  10400. (setq org-refile-history
  10401. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10402. org-refile-history
  10403. (cdr org-refile-history))))
  10404. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10405. (pop org-refile-history)))
  10406. pa)
  10407. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10408. (progn
  10409. (setq parent (match-string 1 answ)
  10410. child (match-string 2 answ))
  10411. (setq parent-target (or (assoc parent tbl)
  10412. (assoc (concat parent "/") tbl)))
  10413. (when (and parent-target
  10414. (or (eq new-nodes t)
  10415. (and (eq new-nodes 'confirm)
  10416. (y-or-n-p (format "Create new node \"%s\"? "
  10417. child)))))
  10418. (org-refile-new-child parent-target child)))
  10419. (error "Invalid target location")))))
  10420. (declare-function org-string-nw-p "org-macs" (s))
  10421. (defun org-refile-check-position (refile-pointer)
  10422. "Check if the refile pointer matches the headline to which it points."
  10423. (let* ((file (nth 1 refile-pointer))
  10424. (re (nth 2 refile-pointer))
  10425. (pos (nth 3 refile-pointer))
  10426. buffer)
  10427. (if (and (not (markerp pos)) (not file))
  10428. (error "Please save the buffer to a file before refiling")
  10429. (when (org-string-nw-p re)
  10430. (setq buffer (if (markerp pos)
  10431. (marker-buffer pos)
  10432. (or (find-buffer-visiting file)
  10433. (find-file-noselect file))))
  10434. (with-current-buffer buffer
  10435. (save-excursion
  10436. (save-restriction
  10437. (widen)
  10438. (goto-char pos)
  10439. (beginning-of-line 1)
  10440. (unless (org-looking-at-p re)
  10441. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10442. (defun org-refile-new-child (parent-target child)
  10443. "Use refile target PARENT-TARGET to add new CHILD below it."
  10444. (unless parent-target
  10445. (error "Cannot find parent for new node"))
  10446. (let ((file (nth 1 parent-target))
  10447. (pos (nth 3 parent-target))
  10448. level)
  10449. (with-current-buffer (or (find-buffer-visiting file)
  10450. (find-file-noselect file))
  10451. (save-excursion
  10452. (save-restriction
  10453. (widen)
  10454. (if pos
  10455. (goto-char pos)
  10456. (goto-char (point-max))
  10457. (if (not (bolp)) (newline)))
  10458. (when (looking-at org-outline-regexp)
  10459. (setq level (funcall outline-level))
  10460. (org-end-of-subtree t t))
  10461. (org-back-over-empty-lines)
  10462. (insert "\n" (make-string
  10463. (if pos (org-get-valid-level level 1) 1) ?*)
  10464. " " child "\n")
  10465. (beginning-of-line 0)
  10466. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10467. (defun org-olpath-completing-read (prompt collection &rest args)
  10468. "Read an outline path like a file name."
  10469. (let ((thetable collection)
  10470. (org-completion-use-ido nil) ; does not work with ido.
  10471. (org-completion-use-iswitchb nil)) ; or iswitchb
  10472. (apply
  10473. 'org-icompleting-read prompt
  10474. (lambda (string predicate &optional flag)
  10475. (let (rtn r f (l (length string)))
  10476. (cond
  10477. ((eq flag nil)
  10478. ;; try completion
  10479. (try-completion string thetable))
  10480. ((eq flag t)
  10481. ;; all-completions
  10482. (setq rtn (all-completions string thetable predicate))
  10483. (mapcar
  10484. (lambda (x)
  10485. (setq r (substring x l))
  10486. (if (string-match " ([^)]*)$" x)
  10487. (setq f (match-string 0 x))
  10488. (setq f ""))
  10489. (if (string-match "/" r)
  10490. (concat string (substring r 0 (match-end 0)) f)
  10491. x))
  10492. rtn))
  10493. ((eq flag 'lambda)
  10494. ;; exact match?
  10495. (assoc string thetable)))))
  10496. args)))
  10497. ;;;; Dynamic blocks
  10498. (defun org-find-dblock (name)
  10499. "Find the first dynamic block with name NAME in the buffer.
  10500. If not found, stay at current position and return nil."
  10501. (let ((case-fold-search t) pos)
  10502. (save-excursion
  10503. (goto-char (point-min))
  10504. (setq pos (and (re-search-forward
  10505. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10506. (match-beginning 0))))
  10507. (if pos (goto-char pos))
  10508. pos))
  10509. (defconst org-dblock-start-re
  10510. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10511. "Matches the start line of a dynamic block, with parameters.")
  10512. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10513. "Matches the end of a dynamic block.")
  10514. (defun org-create-dblock (plist)
  10515. "Create a dynamic block section, with parameters taken from PLIST.
  10516. PLIST must contain a :name entry which is used as name of the block."
  10517. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10518. (end-of-line 1)
  10519. (newline))
  10520. (let ((col (current-column))
  10521. (name (plist-get plist :name)))
  10522. (insert "#+BEGIN: " name)
  10523. (while plist
  10524. (if (eq (car plist) :name)
  10525. (setq plist (cddr plist))
  10526. (insert " " (prin1-to-string (pop plist)))))
  10527. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10528. (beginning-of-line -2)))
  10529. (defun org-prepare-dblock ()
  10530. "Prepare dynamic block for refresh.
  10531. This empties the block, puts the cursor at the insert position and returns
  10532. the property list including an extra property :name with the block name."
  10533. (unless (looking-at org-dblock-start-re)
  10534. (error "Not at a dynamic block"))
  10535. (let* ((begdel (1+ (match-end 0)))
  10536. (name (org-no-properties (match-string 1)))
  10537. (params (append (list :name name)
  10538. (read (concat "(" (match-string 3) ")")))))
  10539. (save-excursion
  10540. (beginning-of-line 1)
  10541. (skip-chars-forward " \t")
  10542. (setq params (plist-put params :indentation-column (current-column))))
  10543. (unless (re-search-forward org-dblock-end-re nil t)
  10544. (error "Dynamic block not terminated"))
  10545. (setq params
  10546. (append params
  10547. (list :content (buffer-substring
  10548. begdel (match-beginning 0)))))
  10549. (delete-region begdel (match-beginning 0))
  10550. (goto-char begdel)
  10551. (open-line 1)
  10552. params))
  10553. (defun org-map-dblocks (&optional command)
  10554. "Apply COMMAND to all dynamic blocks in the current buffer.
  10555. If COMMAND is not given, use `org-update-dblock'."
  10556. (let ((cmd (or command 'org-update-dblock)))
  10557. (save-excursion
  10558. (goto-char (point-min))
  10559. (while (re-search-forward org-dblock-start-re nil t)
  10560. (goto-char (match-beginning 0))
  10561. (save-excursion
  10562. (condition-case nil
  10563. (funcall cmd)
  10564. (error (message "Error during update of dynamic block"))))
  10565. (unless (re-search-forward org-dblock-end-re nil t)
  10566. (error "Dynamic block not terminated"))))))
  10567. (defun org-dblock-update (&optional arg)
  10568. "User command for updating dynamic blocks.
  10569. Update the dynamic block at point. With prefix ARG, update all dynamic
  10570. blocks in the buffer."
  10571. (interactive "P")
  10572. (if arg
  10573. (org-update-all-dblocks)
  10574. (or (looking-at org-dblock-start-re)
  10575. (org-beginning-of-dblock))
  10576. (org-update-dblock)))
  10577. (defun org-update-dblock ()
  10578. "Update the dynamic block at point.
  10579. This means to empty the block, parse for parameters and then call
  10580. the correct writing function."
  10581. (interactive)
  10582. (save-window-excursion
  10583. (let* ((pos (point))
  10584. (line (org-current-line))
  10585. (params (org-prepare-dblock))
  10586. (name (plist-get params :name))
  10587. (indent (plist-get params :indentation-column))
  10588. (cmd (intern (concat "org-dblock-write:" name))))
  10589. (message "Updating dynamic block `%s' at line %d..." name line)
  10590. (funcall cmd params)
  10591. (message "Updating dynamic block `%s' at line %d...done" name line)
  10592. (goto-char pos)
  10593. (when (and indent (> indent 0))
  10594. (setq indent (make-string indent ?\ ))
  10595. (save-excursion
  10596. (org-beginning-of-dblock)
  10597. (forward-line 1)
  10598. (while (not (looking-at org-dblock-end-re))
  10599. (insert indent)
  10600. (beginning-of-line 2))
  10601. (when (looking-at org-dblock-end-re)
  10602. (and (looking-at "[ \t]+")
  10603. (replace-match ""))
  10604. (insert indent)))))))
  10605. (defun org-beginning-of-dblock ()
  10606. "Find the beginning of the dynamic block at point.
  10607. Error if there is no such block at point."
  10608. (let ((pos (point))
  10609. beg)
  10610. (end-of-line 1)
  10611. (if (and (re-search-backward org-dblock-start-re nil t)
  10612. (setq beg (match-beginning 0))
  10613. (re-search-forward org-dblock-end-re nil t)
  10614. (> (match-end 0) pos))
  10615. (goto-char beg)
  10616. (goto-char pos)
  10617. (error "Not in a dynamic block"))))
  10618. (defun org-update-all-dblocks ()
  10619. "Update all dynamic blocks in the buffer.
  10620. This function can be used in a hook."
  10621. (interactive)
  10622. (when (derived-mode-p 'org-mode)
  10623. (org-map-dblocks 'org-update-dblock)))
  10624. ;;;; Completion
  10625. (defun org-get-export-keywords ()
  10626. "Return a list of all currently understood export keywords.
  10627. Export keywords include options, block names, attributes and
  10628. keywords relative to each registered export back-end."
  10629. (delq nil
  10630. (let (keywords)
  10631. (mapc
  10632. (lambda (back-end)
  10633. (let ((props (cdr back-end)))
  10634. ;; Back-end name (for keywords, like #+LATEX:)
  10635. (push (upcase (symbol-name (car back-end))) keywords)
  10636. ;; Back-end options.
  10637. (mapc (lambda (option) (push (cadr option) keywords))
  10638. (plist-get (cdr back-end) :options-alist))))
  10639. (org-bound-and-true-p org-export-registered-backends))
  10640. keywords)))
  10641. (defconst org-options-keywords
  10642. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10643. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10644. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10645. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10646. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
  10647. (defcustom org-structure-template-alist
  10648. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10649. "<src lang=\"?\">\n\n</src>")
  10650. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10651. "<example>\n?\n</example>")
  10652. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10653. "<quote>\n?\n</quote>")
  10654. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10655. "<verse>\n?\n</verse>")
  10656. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10657. "<verbatim>\n?\n</verbatim>")
  10658. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10659. "<center>\n?\n</center>")
  10660. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10661. "<literal style=\"latex\">\n?\n</literal>")
  10662. ("L" "#+LaTeX: "
  10663. "<literal style=\"latex\">?</literal>")
  10664. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10665. "<literal style=\"html\">\n?\n</literal>")
  10666. ("H" "#+HTML: "
  10667. "<literal style=\"html\">?</literal>")
  10668. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10669. ("A" "#+ASCII: ")
  10670. ("i" "#+INDEX: ?"
  10671. "#+INDEX: ?")
  10672. ("I" "#+INCLUDE: %file ?"
  10673. "<include file=%file markup=\"?\">"))
  10674. "Structure completion elements.
  10675. This is a list of abbreviation keys and values. The value gets inserted
  10676. if you type `<' followed by the key and then press the completion key,
  10677. usually `M-TAB'. %file will be replaced by a file name after prompting
  10678. for the file using completion. The cursor will be placed at the position
  10679. of the `?` in the template.
  10680. There are two templates for each key, the first uses the original Org syntax,
  10681. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10682. the default when the /org-mtags.el/ module has been loaded. See also the
  10683. variable `org-mtags-prefer-muse-templates'."
  10684. :group 'org-completion
  10685. :type '(repeat
  10686. (string :tag "Key")
  10687. (string :tag "Template")
  10688. (string :tag "Muse Template")))
  10689. (defun org-try-structure-completion ()
  10690. "Try to complete a structure template before point.
  10691. This looks for strings like \"<e\" on an otherwise empty line and
  10692. expands them."
  10693. (let ((l (buffer-substring (point-at-bol) (point)))
  10694. a)
  10695. (when (and (looking-at "[ \t]*$")
  10696. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10697. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10698. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10699. (match-beginning 1)) a)
  10700. t)))
  10701. (defun org-complete-expand-structure-template (start cell)
  10702. "Expand a structure template."
  10703. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10704. (rpl (nth (if musep 2 1) cell))
  10705. (ind ""))
  10706. (delete-region start (point))
  10707. (when (string-match "\\`#\\+" rpl)
  10708. (cond
  10709. ((bolp))
  10710. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10711. (setq ind (buffer-substring (point-at-bol) (point))))
  10712. (t (newline))))
  10713. (setq start (point))
  10714. (if (string-match "%file" rpl)
  10715. (setq rpl (replace-match
  10716. (concat
  10717. "\""
  10718. (save-match-data
  10719. (abbreviate-file-name (read-file-name "Include file: ")))
  10720. "\"")
  10721. t t rpl)))
  10722. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10723. (concat "\n" ind)))
  10724. (insert rpl)
  10725. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10726. ;;;; TODO, DEADLINE, Comments
  10727. (defun org-toggle-comment ()
  10728. "Change the COMMENT state of an entry."
  10729. (interactive)
  10730. (save-excursion
  10731. (org-back-to-heading)
  10732. (let (case-fold-search)
  10733. (cond
  10734. ((looking-at (format org-heading-keyword-regexp-format
  10735. org-comment-string))
  10736. (goto-char (match-end 1))
  10737. (looking-at (concat " +" org-comment-string))
  10738. (replace-match "" t t)
  10739. (when (eolp) (insert " ")))
  10740. ((looking-at org-outline-regexp)
  10741. (goto-char (match-end 0))
  10742. (insert org-comment-string " "))))))
  10743. (defvar org-last-todo-state-is-todo nil
  10744. "This is non-nil when the last TODO state change led to a TODO state.
  10745. If the last change removed the TODO tag or switched to DONE, then
  10746. this is nil.")
  10747. (defvar org-setting-tags nil) ; dynamically skipped
  10748. (defvar org-todo-setup-filter-hook nil
  10749. "Hook for functions that pre-filter todo specs.
  10750. Each function takes a todo spec and returns either nil or the spec
  10751. transformed into canonical form." )
  10752. (defvar org-todo-get-default-hook nil
  10753. "Hook for functions that get a default item for todo.
  10754. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10755. nil or a string to be used for the todo mark." )
  10756. (defvar org-agenda-headline-snapshot-before-repeat)
  10757. (defun org-current-effective-time ()
  10758. "Return current time adjusted for `org-extend-today-until' variable."
  10759. (let* ((ct (org-current-time))
  10760. (dct (decode-time ct))
  10761. (ct1
  10762. (cond
  10763. (org-use-last-clock-out-time-as-effective-time
  10764. (or (org-clock-get-last-clock-out-time) ct))
  10765. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10766. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10767. (t ct))))
  10768. ct1))
  10769. (defun org-todo-yesterday (&optional arg)
  10770. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10771. (interactive "P")
  10772. (if (eq major-mode 'org-agenda-mode)
  10773. (apply 'org-agenda-todo-yesterday arg)
  10774. (let* ((hour (third (decode-time
  10775. (org-current-time))))
  10776. (org-extend-today-until (1+ hour)))
  10777. (org-todo arg))))
  10778. (defvar org-block-entry-blocking ""
  10779. "First entry preventing the TODO state change.")
  10780. (defun org-todo (&optional arg)
  10781. "Change the TODO state of an item.
  10782. The state of an item is given by a keyword at the start of the heading,
  10783. like
  10784. *** TODO Write paper
  10785. *** DONE Call mom
  10786. The different keywords are specified in the variable `org-todo-keywords'.
  10787. By default the available states are \"TODO\" and \"DONE\".
  10788. So for this example: when the item starts with TODO, it is changed to DONE.
  10789. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10790. DONE are present, add TODO at the beginning of the heading.
  10791. With \\[universal-argument] prefix arg, use completion to determine the new \
  10792. state.
  10793. With numeric prefix arg, switch to that state.
  10794. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10795. keywords (nextset).
  10796. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10797. With a numeric prefix arg of 0, inhibit note taking for the change.
  10798. For calling through lisp, arg is also interpreted in the following way:
  10799. 'none -> empty state
  10800. \"\"(empty string) -> switch to empty state
  10801. 'done -> switch to DONE
  10802. 'nextset -> switch to the next set of keywords
  10803. 'previousset -> switch to the previous set of keywords
  10804. \"WAITING\" -> switch to the specified keyword, but only if it
  10805. really is a member of `org-todo-keywords'."
  10806. (interactive "P")
  10807. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10808. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10809. 'region-start-level 'region))
  10810. org-loop-over-headlines-in-active-region)
  10811. (org-map-entries
  10812. `(org-todo ,arg)
  10813. org-loop-over-headlines-in-active-region
  10814. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10815. (if (equal arg '(16)) (setq arg 'nextset))
  10816. (let ((org-blocker-hook org-blocker-hook)
  10817. commentp
  10818. case-fold-search)
  10819. (when (equal arg '(64))
  10820. (setq arg nil org-blocker-hook nil))
  10821. (when (and org-blocker-hook
  10822. (or org-inhibit-blocking
  10823. (org-entry-get nil "NOBLOCKING")))
  10824. (setq org-blocker-hook nil))
  10825. (save-excursion
  10826. (catch 'exit
  10827. (org-back-to-heading t)
  10828. (when (looking-at (concat "^\\*+ " org-comment-string))
  10829. (org-toggle-comment)
  10830. (setq commentp t))
  10831. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10832. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10833. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10834. (let* ((match-data (match-data))
  10835. (startpos (point-at-bol))
  10836. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10837. (org-log-done org-log-done)
  10838. (org-log-repeat org-log-repeat)
  10839. (org-todo-log-states org-todo-log-states)
  10840. (org-inhibit-logging
  10841. (if (equal arg 0)
  10842. (progn (setq arg nil) 'note) org-inhibit-logging))
  10843. (this (match-string 1))
  10844. (hl-pos (match-beginning 0))
  10845. (head (org-get-todo-sequence-head this))
  10846. (ass (assoc head org-todo-kwd-alist))
  10847. (interpret (nth 1 ass))
  10848. (done-word (nth 3 ass))
  10849. (final-done-word (nth 4 ass))
  10850. (org-last-state (or this ""))
  10851. (completion-ignore-case t)
  10852. (member (member this org-todo-keywords-1))
  10853. (tail (cdr member))
  10854. (org-state (cond
  10855. ((and org-todo-key-trigger
  10856. (or (and (equal arg '(4))
  10857. (eq org-use-fast-todo-selection 'prefix))
  10858. (and (not arg) org-use-fast-todo-selection
  10859. (not (eq org-use-fast-todo-selection
  10860. 'prefix)))))
  10861. ;; Use fast selection
  10862. (org-fast-todo-selection))
  10863. ((and (equal arg '(4))
  10864. (or (not org-use-fast-todo-selection)
  10865. (not org-todo-key-trigger)))
  10866. ;; Read a state with completion
  10867. (org-icompleting-read
  10868. "State: " (mapcar (lambda(x) (list x))
  10869. org-todo-keywords-1)
  10870. nil t))
  10871. ((eq arg 'right)
  10872. (if this
  10873. (if tail (car tail) nil)
  10874. (car org-todo-keywords-1)))
  10875. ((eq arg 'left)
  10876. (if (equal member org-todo-keywords-1)
  10877. nil
  10878. (if this
  10879. (nth (- (length org-todo-keywords-1)
  10880. (length tail) 2)
  10881. org-todo-keywords-1)
  10882. (org-last org-todo-keywords-1))))
  10883. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10884. (setq arg nil))) ; hack to fall back to cycling
  10885. (arg
  10886. ;; user or caller requests a specific state
  10887. (cond
  10888. ((equal arg "") nil)
  10889. ((eq arg 'none) nil)
  10890. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10891. ((eq arg 'nextset)
  10892. (or (car (cdr (member head org-todo-heads)))
  10893. (car org-todo-heads)))
  10894. ((eq arg 'previousset)
  10895. (let ((org-todo-heads (reverse org-todo-heads)))
  10896. (or (car (cdr (member head org-todo-heads)))
  10897. (car org-todo-heads))))
  10898. ((car (member arg org-todo-keywords-1)))
  10899. ((stringp arg)
  10900. (error "State `%s' not valid in this file" arg))
  10901. ((nth (1- (prefix-numeric-value arg))
  10902. org-todo-keywords-1))))
  10903. ((null member) (or head (car org-todo-keywords-1)))
  10904. ((equal this final-done-word) nil) ;; -> make empty
  10905. ((null tail) nil) ;; -> first entry
  10906. ((memq interpret '(type priority))
  10907. (if (eq this-command last-command)
  10908. (car tail)
  10909. (if (> (length tail) 0)
  10910. (or done-word (car org-done-keywords))
  10911. nil)))
  10912. (t
  10913. (car tail))))
  10914. (org-state (or
  10915. (run-hook-with-args-until-success
  10916. 'org-todo-get-default-hook org-state org-last-state)
  10917. org-state))
  10918. (next (if org-state (concat " " org-state " ") " "))
  10919. (change-plist (list :type 'todo-state-change :from this :to org-state
  10920. :position startpos))
  10921. dolog now-done-p)
  10922. (when org-blocker-hook
  10923. (setq org-last-todo-state-is-todo
  10924. (not (member this org-done-keywords)))
  10925. (unless (save-excursion
  10926. (save-match-data
  10927. (org-with-wide-buffer
  10928. (run-hook-with-args-until-failure
  10929. 'org-blocker-hook change-plist))))
  10930. (if (org-called-interactively-p 'interactive)
  10931. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10932. this org-state org-block-entry-blocking)
  10933. ;; fail silently
  10934. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10935. this org-state org-block-entry-blocking)
  10936. (throw 'exit nil))))
  10937. (store-match-data match-data)
  10938. (replace-match next t t)
  10939. (unless (pos-visible-in-window-p hl-pos)
  10940. (message "TODO state changed to %s" (org-trim next)))
  10941. (unless head
  10942. (setq head (org-get-todo-sequence-head org-state)
  10943. ass (assoc head org-todo-kwd-alist)
  10944. interpret (nth 1 ass)
  10945. done-word (nth 3 ass)
  10946. final-done-word (nth 4 ass)))
  10947. (when (memq arg '(nextset previousset))
  10948. (message "Keyword-Set %d/%d: %s"
  10949. (- (length org-todo-sets) -1
  10950. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10951. (length org-todo-sets)
  10952. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10953. (setq org-last-todo-state-is-todo
  10954. (not (member org-state org-done-keywords)))
  10955. (setq now-done-p (and (member org-state org-done-keywords)
  10956. (not (member this org-done-keywords))))
  10957. (and logging (org-local-logging logging))
  10958. (when (and (or org-todo-log-states org-log-done)
  10959. (not (eq org-inhibit-logging t))
  10960. (not (memq arg '(nextset previousset))))
  10961. ;; we need to look at recording a time and note
  10962. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10963. (nth 2 (assoc this org-todo-log-states))))
  10964. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10965. (setq dolog 'time))
  10966. (when (and org-state
  10967. (member org-state org-not-done-keywords)
  10968. (not (member this org-not-done-keywords)))
  10969. ;; This is now a todo state and was not one before
  10970. ;; If there was a CLOSED time stamp, get rid of it.
  10971. (org-add-planning-info nil nil 'closed))
  10972. (when (and now-done-p org-log-done)
  10973. ;; It is now done, and it was not done before
  10974. (org-add-planning-info 'closed (org-current-effective-time))
  10975. (if (and (not dolog) (eq 'note org-log-done))
  10976. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10977. (when (and org-state dolog)
  10978. ;; This is a non-nil state, and we need to log it
  10979. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10980. ;; Fixup tag positioning
  10981. (org-todo-trigger-tag-changes org-state)
  10982. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10983. (when org-provide-todo-statistics
  10984. (org-update-parent-todo-statistics))
  10985. (run-hooks 'org-after-todo-state-change-hook)
  10986. (if (and arg (not (member org-state org-done-keywords)))
  10987. (setq head (org-get-todo-sequence-head org-state)))
  10988. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10989. ;; Do we need to trigger a repeat?
  10990. (when now-done-p
  10991. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10992. ;; This is for the agenda, take a snapshot of the headline.
  10993. (save-match-data
  10994. (setq org-agenda-headline-snapshot-before-repeat
  10995. (org-get-heading))))
  10996. (org-auto-repeat-maybe org-state))
  10997. ;; Fixup cursor location if close to the keyword
  10998. (if (and (outline-on-heading-p)
  10999. (not (bolp))
  11000. (save-excursion (beginning-of-line 1)
  11001. (looking-at org-todo-line-regexp))
  11002. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11003. (progn
  11004. (goto-char (or (match-end 2) (match-end 1)))
  11005. (and (looking-at " ") (just-one-space))))
  11006. (when org-trigger-hook
  11007. (save-excursion
  11008. (run-hook-with-args 'org-trigger-hook change-plist)))
  11009. (when commentp (org-toggle-comment))))))))
  11010. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11011. "Block turning an entry into a TODO, using the hierarchy.
  11012. This checks whether the current task should be blocked from state
  11013. changes. Such blocking occurs when:
  11014. 1. The task has children which are not all in a completed state.
  11015. 2. A task has a parent with the property :ORDERED:, and there
  11016. are siblings prior to the current task with incomplete
  11017. status.
  11018. 3. The parent of the task is blocked because it has siblings that should
  11019. be done first, or is child of a block grandparent TODO entry."
  11020. (if (not org-enforce-todo-dependencies)
  11021. t ; if locally turned off don't block
  11022. (catch 'dont-block
  11023. ;; If this is not a todo state change, or if this entry is already DONE,
  11024. ;; do not block
  11025. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11026. (member (plist-get change-plist :from)
  11027. (cons 'done org-done-keywords))
  11028. (member (plist-get change-plist :to)
  11029. (cons 'todo org-not-done-keywords))
  11030. (not (plist-get change-plist :to)))
  11031. (throw 'dont-block t))
  11032. ;; If this task has children, and any are undone, it's blocked
  11033. (save-excursion
  11034. (org-back-to-heading t)
  11035. (let ((this-level (funcall outline-level)))
  11036. (outline-next-heading)
  11037. (let ((child-level (funcall outline-level)))
  11038. (while (and (not (eobp))
  11039. (> child-level this-level))
  11040. ;; this todo has children, check whether they are all
  11041. ;; completed
  11042. (if (and (not (org-entry-is-done-p))
  11043. (org-entry-is-todo-p))
  11044. (progn (setq org-block-entry-blocking (org-get-heading))
  11045. (throw 'dont-block nil)))
  11046. (outline-next-heading)
  11047. (setq child-level (funcall outline-level))))))
  11048. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11049. ;; any previous siblings are undone, it's blocked
  11050. (save-excursion
  11051. (org-back-to-heading t)
  11052. (let* ((pos (point))
  11053. (parent-pos (and (org-up-heading-safe) (point))))
  11054. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11055. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11056. (forward-line 1)
  11057. (re-search-forward org-not-done-heading-regexp pos t))
  11058. (setq org-block-entry-blocking (match-string 0))
  11059. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11060. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11061. (while t
  11062. (goto-char parent-pos)
  11063. (if (not (looking-at org-not-done-heading-regexp))
  11064. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11065. (setq pos (point))
  11066. (setq parent-pos (and (org-up-heading-safe) (point)))
  11067. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11068. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11069. (forward-line 1)
  11070. (re-search-forward org-not-done-heading-regexp pos t)
  11071. (setq org-block-entry-blocking (org-get-heading)))
  11072. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11073. (defcustom org-track-ordered-property-with-tag nil
  11074. "Should the ORDERED property also be shown as a tag?
  11075. The ORDERED property decides if an entry should require subtasks to be
  11076. completed in sequence. Since a property is not very visible, setting
  11077. this option means that toggling the ORDERED property with the command
  11078. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11079. not relevant for the behavior, but it makes things more visible.
  11080. Note that toggling the tag with tags commands will not change the property
  11081. and therefore not influence behavior!
  11082. This can be t, meaning the tag ORDERED should be used, It can also be a
  11083. string to select a different tag for this task."
  11084. :group 'org-todo
  11085. :type '(choice
  11086. (const :tag "No tracking" nil)
  11087. (const :tag "Track with ORDERED tag" t)
  11088. (string :tag "Use other tag")))
  11089. (defun org-toggle-ordered-property ()
  11090. "Toggle the ORDERED property of the current entry.
  11091. For better visibility, you can track the value of this property with a tag.
  11092. See variable `org-track-ordered-property-with-tag'."
  11093. (interactive)
  11094. (let* ((t1 org-track-ordered-property-with-tag)
  11095. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11096. (save-excursion
  11097. (org-back-to-heading)
  11098. (if (org-entry-get nil "ORDERED")
  11099. (progn
  11100. (org-delete-property "ORDERED" "PROPERTIES")
  11101. (and tag (org-toggle-tag tag 'off))
  11102. (message "Subtasks can be completed in arbitrary order"))
  11103. (org-entry-put nil "ORDERED" "t")
  11104. (and tag (org-toggle-tag tag 'on))
  11105. (message "Subtasks must be completed in sequence")))))
  11106. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11107. (defun org-block-todo-from-checkboxes (change-plist)
  11108. "Block turning an entry into a TODO, using checkboxes.
  11109. This checks whether the current task should be blocked from state
  11110. changes because there are unchecked boxes in this entry."
  11111. (if (not org-enforce-todo-checkbox-dependencies)
  11112. t ; if locally turned off don't block
  11113. (catch 'dont-block
  11114. ;; If this is not a todo state change, or if this entry is already DONE,
  11115. ;; do not block
  11116. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11117. (member (plist-get change-plist :from)
  11118. (cons 'done org-done-keywords))
  11119. (member (plist-get change-plist :to)
  11120. (cons 'todo org-not-done-keywords))
  11121. (not (plist-get change-plist :to)))
  11122. (throw 'dont-block t))
  11123. ;; If this task has checkboxes that are not checked, it's blocked
  11124. (save-excursion
  11125. (org-back-to-heading t)
  11126. (let ((beg (point)) end)
  11127. (outline-next-heading)
  11128. (setq end (point))
  11129. (goto-char beg)
  11130. (if (org-list-search-forward
  11131. (concat (org-item-beginning-re)
  11132. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11133. "\\[[- ]\\]")
  11134. end t)
  11135. (progn
  11136. (if (boundp 'org-blocked-by-checkboxes)
  11137. (setq org-blocked-by-checkboxes t))
  11138. (throw 'dont-block nil)))))
  11139. t))) ; do not block
  11140. (defun org-entry-blocked-p ()
  11141. "Is the current entry blocked?"
  11142. (org-with-silent-modifications
  11143. (if (org-entry-get nil "NOBLOCKING")
  11144. nil ;; Never block this entry
  11145. (not (run-hook-with-args-until-failure
  11146. 'org-blocker-hook
  11147. (list :type 'todo-state-change
  11148. :position (point)
  11149. :from 'todo
  11150. :to 'done))))))
  11151. (defun org-update-statistics-cookies (all)
  11152. "Update the statistics cookie, either from TODO or from checkboxes.
  11153. This should be called with the cursor in a line with a statistics cookie."
  11154. (interactive "P")
  11155. (if all
  11156. (progn
  11157. (org-update-checkbox-count 'all)
  11158. (org-map-entries 'org-update-parent-todo-statistics))
  11159. (if (not (org-at-heading-p))
  11160. (org-update-checkbox-count)
  11161. (let ((pos (point-marker))
  11162. end l1 l2)
  11163. (ignore-errors (org-back-to-heading t))
  11164. (if (not (org-at-heading-p))
  11165. (org-update-checkbox-count)
  11166. (setq l1 (org-outline-level))
  11167. (setq end (save-excursion
  11168. (outline-next-heading)
  11169. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11170. (point)))
  11171. (if (and (save-excursion
  11172. (re-search-forward
  11173. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11174. (not (save-excursion (re-search-forward
  11175. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11176. (org-update-checkbox-count)
  11177. (if (and l2 (> l2 l1))
  11178. (progn
  11179. (goto-char end)
  11180. (org-update-parent-todo-statistics))
  11181. (goto-char pos)
  11182. (beginning-of-line 1)
  11183. (while (re-search-forward
  11184. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11185. (point-at-eol) t)
  11186. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11187. (goto-char pos)
  11188. (move-marker pos nil)))))
  11189. (defvar org-entry-property-inherited-from) ;; defined below
  11190. (defun org-update-parent-todo-statistics ()
  11191. "Update any statistics cookie in the parent of the current headline.
  11192. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11193. the entire subtree and this will travel up the hierarchy and update
  11194. statistics everywhere."
  11195. (let* ((prop (save-excursion (org-up-heading-safe)
  11196. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11197. (recursive (or (not org-hierarchical-todo-statistics)
  11198. (and prop (string-match "\\<recursive\\>" prop))))
  11199. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11200. 0))
  11201. (first t)
  11202. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11203. level ltoggle l1 new ndel
  11204. (cnt-all 0) (cnt-done 0) is-percent kwd
  11205. checkbox-beg ov ovs ove cookie-present)
  11206. (catch 'exit
  11207. (save-excursion
  11208. (beginning-of-line 1)
  11209. (setq ltoggle (funcall outline-level))
  11210. ;; Three situations are to consider:
  11211. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11212. ;; to the top-level ancestor on the headline;
  11213. ;; 2. If parent has "recursive" property, repeat up to the
  11214. ;; headline setting that property, taking inheritance into
  11215. ;; account;
  11216. ;; 3. Else, move up to direct parent and proceed only once.
  11217. (while (and (setq level (org-up-heading-safe))
  11218. (or recursive first)
  11219. (>= (point) lim))
  11220. (setq first nil cookie-present nil)
  11221. (unless (and level
  11222. (not (string-match
  11223. "\\<checkbox\\>"
  11224. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11225. "")))))
  11226. (throw 'exit nil))
  11227. (while (re-search-forward box-re (point-at-eol) t)
  11228. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11229. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11230. (save-match-data
  11231. (unless (outline-next-heading) (throw 'exit nil))
  11232. (while (and (looking-at org-complex-heading-regexp)
  11233. (> (setq l1 (length (match-string 1))) level))
  11234. (setq kwd (and (or recursive (= l1 ltoggle))
  11235. (match-string 2)))
  11236. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11237. (and (listp org-provide-todo-statistics)
  11238. (or (member kwd org-provide-todo-statistics)
  11239. (member kwd org-done-keywords))))
  11240. (setq cnt-all (1+ cnt-all))
  11241. (if (eq org-provide-todo-statistics t)
  11242. (and kwd (setq cnt-all (1+ cnt-all)))))
  11243. (and (member kwd org-done-keywords)
  11244. (setq cnt-done (1+ cnt-done)))
  11245. (outline-next-heading)))
  11246. (setq new
  11247. (if is-percent
  11248. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11249. (format "[%d/%d]" cnt-done cnt-all))
  11250. ndel (- (match-end 0) checkbox-beg))
  11251. ;; handle overlays when updating cookie from column view
  11252. (when (setq ov (car (overlays-at checkbox-beg)))
  11253. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11254. (delete-overlay ov))
  11255. (goto-char checkbox-beg)
  11256. (insert new)
  11257. (delete-region (point) (+ (point) ndel))
  11258. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11259. (when ov (move-overlay ov ovs ove)))
  11260. (when cookie-present
  11261. (run-hook-with-args 'org-after-todo-statistics-hook
  11262. cnt-done (- cnt-all cnt-done))))))
  11263. (run-hooks 'org-todo-statistics-hook)))
  11264. (defvar org-after-todo-statistics-hook nil
  11265. "Hook that is called after a TODO statistics cookie has been updated.
  11266. Each function is called with two arguments: the number of not-done entries
  11267. and the number of done entries.
  11268. For example, the following function, when added to this hook, will switch
  11269. an entry to DONE when all children are done, and back to TODO when new
  11270. entries are set to a TODO status. Note that this hook is only called
  11271. when there is a statistics cookie in the headline!
  11272. (defun org-summary-todo (n-done n-not-done)
  11273. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11274. (let (org-log-done org-log-states) ; turn off logging
  11275. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11276. ")
  11277. (defvar org-todo-statistics-hook nil
  11278. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11279. This hook runs even if there is no statistics cookie present, in which case
  11280. `org-after-todo-statistics-hook' would not run.")
  11281. (defun org-todo-trigger-tag-changes (state)
  11282. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11283. (let ((l org-todo-state-tags-triggers)
  11284. changes)
  11285. (when (or (not state) (equal state ""))
  11286. (setq changes (append changes (cdr (assoc "" l)))))
  11287. (when (and (stringp state) (> (length state) 0))
  11288. (setq changes (append changes (cdr (assoc state l)))))
  11289. (when (member state org-not-done-keywords)
  11290. (setq changes (append changes (cdr (assoc 'todo l)))))
  11291. (when (member state org-done-keywords)
  11292. (setq changes (append changes (cdr (assoc 'done l)))))
  11293. (dolist (c changes)
  11294. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11295. (defun org-local-logging (value)
  11296. "Get logging settings from a property VALUE."
  11297. (let* (words w a)
  11298. ;; directly set the variables, they are already local.
  11299. (setq org-log-done nil
  11300. org-log-repeat nil
  11301. org-todo-log-states nil)
  11302. (setq words (org-split-string value))
  11303. (while (setq w (pop words))
  11304. (cond
  11305. ((setq a (assoc w org-startup-options))
  11306. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11307. (set (nth 1 a) (nth 2 a))))
  11308. ((setq a (org-extract-log-state-settings w))
  11309. (and (member (car a) org-todo-keywords-1)
  11310. (push a org-todo-log-states)))))))
  11311. (defun org-get-todo-sequence-head (kwd)
  11312. "Return the head of the TODO sequence to which KWD belongs.
  11313. If KWD is not set, check if there is a text property remembering the
  11314. right sequence."
  11315. (let (p)
  11316. (cond
  11317. ((not kwd)
  11318. (or (get-text-property (point-at-bol) 'org-todo-head)
  11319. (progn
  11320. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11321. nil (point-at-eol)))
  11322. (get-text-property p 'org-todo-head))))
  11323. ((not (member kwd org-todo-keywords-1))
  11324. (car org-todo-keywords-1))
  11325. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11326. (defun org-fast-todo-selection ()
  11327. "Fast TODO keyword selection with single keys.
  11328. Returns the new TODO keyword, or nil if no state change should occur."
  11329. (let* ((fulltable org-todo-key-alist)
  11330. (done-keywords org-done-keywords) ;; needed for the faces.
  11331. (maxlen (apply 'max (mapcar
  11332. (lambda (x)
  11333. (if (stringp (car x)) (string-width (car x)) 0))
  11334. fulltable)))
  11335. (expert nil)
  11336. (fwidth (+ maxlen 3 1 3))
  11337. (ncol (/ (- (window-width) 4) fwidth))
  11338. tg cnt e c tbl
  11339. groups ingroup)
  11340. (save-excursion
  11341. (save-window-excursion
  11342. (if expert
  11343. (set-buffer (get-buffer-create " *Org todo*"))
  11344. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11345. (erase-buffer)
  11346. (org-set-local 'org-done-keywords done-keywords)
  11347. (setq tbl fulltable cnt 0)
  11348. (while (setq e (pop tbl))
  11349. (cond
  11350. ((equal e '(:startgroup))
  11351. (push '() groups) (setq ingroup t)
  11352. (when (not (= cnt 0))
  11353. (setq cnt 0)
  11354. (insert "\n"))
  11355. (insert "{ "))
  11356. ((equal e '(:endgroup))
  11357. (setq ingroup nil cnt 0)
  11358. (insert "}\n"))
  11359. ((equal e '(:newline))
  11360. (when (not (= cnt 0))
  11361. (setq cnt 0)
  11362. (insert "\n")
  11363. (setq e (car tbl))
  11364. (while (equal (car tbl) '(:newline))
  11365. (insert "\n")
  11366. (setq tbl (cdr tbl)))))
  11367. (t
  11368. (setq tg (car e) c (cdr e))
  11369. (if ingroup (push tg (car groups)))
  11370. (setq tg (org-add-props tg nil 'face
  11371. (org-get-todo-face tg)))
  11372. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11373. (insert "[" c "] " tg (make-string
  11374. (- fwidth 4 (length tg)) ?\ ))
  11375. (when (= (setq cnt (1+ cnt)) ncol)
  11376. (insert "\n")
  11377. (if ingroup (insert " "))
  11378. (setq cnt 0)))))
  11379. (insert "\n")
  11380. (goto-char (point-min))
  11381. (if (not expert) (org-fit-window-to-buffer))
  11382. (message "[a-z..]:Set [SPC]:clear")
  11383. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11384. (cond
  11385. ((or (= c ?\C-g)
  11386. (and (= c ?q) (not (rassoc c fulltable))))
  11387. (setq quit-flag t))
  11388. ((= c ?\ ) nil)
  11389. ((setq e (rassoc c fulltable) tg (car e))
  11390. tg)
  11391. (t (setq quit-flag t)))))))
  11392. (defun org-entry-is-todo-p ()
  11393. (member (org-get-todo-state) org-not-done-keywords))
  11394. (defun org-entry-is-done-p ()
  11395. (member (org-get-todo-state) org-done-keywords))
  11396. (defun org-get-todo-state ()
  11397. (save-excursion
  11398. (org-back-to-heading t)
  11399. (and (looking-at org-todo-line-regexp)
  11400. (match-end 2)
  11401. (match-string 2))))
  11402. (defun org-at-date-range-p (&optional inactive-ok)
  11403. "Is the cursor inside a date range?"
  11404. (interactive)
  11405. (save-excursion
  11406. (catch 'exit
  11407. (let ((pos (point)))
  11408. (skip-chars-backward "^[<\r\n")
  11409. (skip-chars-backward "<[")
  11410. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11411. (>= (match-end 0) pos)
  11412. (throw 'exit t))
  11413. (skip-chars-backward "^<[\r\n")
  11414. (skip-chars-backward "<[")
  11415. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11416. (>= (match-end 0) pos)
  11417. (throw 'exit t)))
  11418. nil)))
  11419. (defun org-get-repeat (&optional tagline)
  11420. "Check if there is a deadline/schedule with repeater in this entry."
  11421. (save-match-data
  11422. (save-excursion
  11423. (org-back-to-heading t)
  11424. (and (re-search-forward (if tagline
  11425. (concat tagline "\\s-*" org-repeat-re)
  11426. org-repeat-re)
  11427. (org-entry-end-position) t)
  11428. (match-string-no-properties 1)))))
  11429. (defvar org-last-changed-timestamp)
  11430. (defvar org-last-inserted-timestamp)
  11431. (defvar org-log-post-message)
  11432. (defvar org-log-note-purpose)
  11433. (defvar org-log-note-how)
  11434. (defvar org-log-note-extra)
  11435. (defun org-auto-repeat-maybe (done-word)
  11436. "Check if the current headline contains a repeated deadline/schedule.
  11437. If yes, set TODO state back to what it was and change the base date
  11438. of repeating deadline/scheduled time stamps to new date.
  11439. This function is run automatically after each state change to a DONE state."
  11440. ;; last-state is dynamically scoped into this function
  11441. (let* ((repeat (org-get-repeat))
  11442. (aa (assoc org-last-state org-todo-kwd-alist))
  11443. (interpret (nth 1 aa))
  11444. (head (nth 2 aa))
  11445. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11446. (msg "Entry repeats: ")
  11447. (org-log-done nil)
  11448. (org-todo-log-states nil)
  11449. re type n what ts time to-state)
  11450. (when repeat
  11451. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11452. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11453. org-todo-repeat-to-state))
  11454. (unless (and to-state (member to-state org-todo-keywords-1))
  11455. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11456. (org-todo to-state)
  11457. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11458. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11459. (org-time-stamp-format t t))))
  11460. (when org-log-repeat
  11461. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11462. (memq 'org-add-log-note post-command-hook))
  11463. ;; OK, we are already setup for some record
  11464. (if (eq org-log-repeat 'note)
  11465. ;; make sure we take a note, not only a time stamp
  11466. (setq org-log-note-how 'note))
  11467. ;; Set up for taking a record
  11468. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11469. org-last-state
  11470. 'findpos org-log-repeat)))
  11471. (org-back-to-heading t)
  11472. (org-add-planning-info nil nil 'closed)
  11473. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11474. org-deadline-time-regexp "\\)\\|\\("
  11475. org-ts-regexp "\\)"))
  11476. (while (re-search-forward
  11477. re (save-excursion (outline-next-heading) (point)) t)
  11478. (setq type (if (match-end 1) org-scheduled-string
  11479. (if (match-end 3) org-deadline-string "Plain:"))
  11480. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11481. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11482. (setq n (string-to-number (match-string 2 ts))
  11483. what (match-string 3 ts))
  11484. (if (equal what "w") (setq n (* n 7) what "d"))
  11485. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11486. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11487. ;; Preparation, see if we need to modify the start date for the change
  11488. (when (match-end 1)
  11489. (setq time (save-match-data (org-time-string-to-time ts)))
  11490. (cond
  11491. ((equal (match-string 1 ts) ".")
  11492. ;; Shift starting date to today
  11493. (org-timestamp-change
  11494. (- (org-today) (time-to-days time))
  11495. 'day))
  11496. ((equal (match-string 1 ts) "+")
  11497. (let ((nshiftmax 10) (nshift 0))
  11498. (while (or (= nshift 0)
  11499. (<= (time-to-days time)
  11500. (time-to-days (current-time))))
  11501. (when (= (incf nshift) nshiftmax)
  11502. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11503. (error "Abort")))
  11504. (org-timestamp-change n (cdr (assoc what whata)))
  11505. (org-at-timestamp-p t)
  11506. (setq ts (match-string 1))
  11507. (setq time (save-match-data (org-time-string-to-time ts)))))
  11508. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11509. ;; rematch, so that we have everything in place for the real shift
  11510. (org-at-timestamp-p t)
  11511. (setq ts (match-string 1))
  11512. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11513. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11514. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11515. (setq org-log-post-message msg)
  11516. (message "%s" msg))))
  11517. (defun org-show-todo-tree (arg)
  11518. "Make a compact tree which shows all headlines marked with TODO.
  11519. The tree will show the lines where the regexp matches, and all higher
  11520. headlines above the match.
  11521. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11522. With a numeric prefix N, construct a sparse tree for the Nth element
  11523. of `org-todo-keywords-1'."
  11524. (interactive "P")
  11525. (let ((case-fold-search nil)
  11526. (kwd-re
  11527. (cond ((null arg) org-not-done-regexp)
  11528. ((equal arg '(4))
  11529. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11530. (mapcar 'list org-todo-keywords-1))))
  11531. (concat "\\("
  11532. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11533. "\\)\\>")))
  11534. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11535. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11536. org-todo-keywords-1)))
  11537. (t (error "Invalid prefix argument: %s" arg)))))
  11538. (message "%d TODO entries found"
  11539. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11540. (defun org-deadline (arg &optional time)
  11541. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11542. With one universal prefix argument, remove any deadline from the item.
  11543. With two universal prefix arguments, prompt for a warning delay.
  11544. With argument TIME, set the deadline at the corresponding date. TIME
  11545. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11546. (interactive "P")
  11547. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11548. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11549. 'region-start-level 'region))
  11550. org-loop-over-headlines-in-active-region)
  11551. (org-map-entries
  11552. `(org-deadline ',arg ,time)
  11553. org-loop-over-headlines-in-active-region
  11554. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11555. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11556. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11557. (repeater (and old-date
  11558. (string-match
  11559. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11560. old-date)
  11561. (match-string 1 old-date))))
  11562. (cond
  11563. ((equal arg '(4))
  11564. (when (and old-date org-log-redeadline)
  11565. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11566. org-log-redeadline))
  11567. (org-remove-timestamp-with-keyword org-deadline-string)
  11568. (message "Item no longer has a deadline."))
  11569. ((equal arg '(16))
  11570. (save-excursion
  11571. (if (re-search-forward
  11572. org-deadline-time-regexp
  11573. (save-excursion (outline-next-heading) (point)) t)
  11574. (let* ((rpl0 (match-string 1))
  11575. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11576. (replace-match
  11577. (concat org-deadline-string
  11578. " <" rpl
  11579. (format " -%dd"
  11580. (abs
  11581. (- (time-to-days
  11582. (save-match-data
  11583. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11584. (time-to-days old-date-time))))
  11585. ">") t t))
  11586. (user-error "No deadline information to update"))))
  11587. (t
  11588. (org-add-planning-info 'deadline time 'closed)
  11589. (when (and old-date org-log-redeadline
  11590. (not (equal old-date
  11591. (substring org-last-inserted-timestamp 1 -1))))
  11592. (org-add-log-setup 'redeadline nil old-date 'findpos
  11593. org-log-redeadline))
  11594. (when repeater
  11595. (save-excursion
  11596. (org-back-to-heading t)
  11597. (when (re-search-forward (concat org-deadline-string " "
  11598. org-last-inserted-timestamp)
  11599. (save-excursion
  11600. (outline-next-heading) (point)) t)
  11601. (goto-char (1- (match-end 0)))
  11602. (insert " " repeater)
  11603. (setq org-last-inserted-timestamp
  11604. (concat (substring org-last-inserted-timestamp 0 -1)
  11605. " " repeater
  11606. (substring org-last-inserted-timestamp -1))))))
  11607. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11608. (defun org-schedule (arg &optional time)
  11609. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11610. With one universal prefix argument, remove any scheduling date from the item.
  11611. With two universal prefix arguments, prompt for a delay cookie.
  11612. With argument TIME, scheduled at the corresponding date. TIME can
  11613. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11614. (interactive "P")
  11615. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11616. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11617. 'region-start-level 'region))
  11618. org-loop-over-headlines-in-active-region)
  11619. (org-map-entries
  11620. `(org-schedule ',arg ,time)
  11621. org-loop-over-headlines-in-active-region
  11622. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11623. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11624. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11625. (repeater (and old-date
  11626. (string-match
  11627. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11628. old-date)
  11629. (match-string 1 old-date))))
  11630. (cond
  11631. ((equal arg '(4))
  11632. (progn
  11633. (when (and old-date org-log-reschedule)
  11634. (org-add-log-setup 'delschedule nil old-date 'findpos
  11635. org-log-reschedule))
  11636. (org-remove-timestamp-with-keyword org-scheduled-string)
  11637. (message "Item is no longer scheduled.")))
  11638. ((equal arg '(16))
  11639. (save-excursion
  11640. (if (re-search-forward
  11641. org-scheduled-time-regexp
  11642. (save-excursion (outline-next-heading) (point)) t)
  11643. (let* ((rpl0 (match-string 1))
  11644. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11645. (replace-match
  11646. (concat org-scheduled-string
  11647. " <" rpl
  11648. (format " -%dd"
  11649. (abs
  11650. (- (time-to-days
  11651. (save-match-data
  11652. (org-read-date nil t nil "Delay until" old-date-time)))
  11653. (time-to-days old-date-time))))
  11654. ">") t t))
  11655. (user-error "No scheduled information to update"))))
  11656. (t
  11657. (org-add-planning-info 'scheduled time 'closed)
  11658. (when (and old-date org-log-reschedule
  11659. (not (equal old-date
  11660. (substring org-last-inserted-timestamp 1 -1))))
  11661. (org-add-log-setup 'reschedule nil old-date 'findpos
  11662. org-log-reschedule))
  11663. (when repeater
  11664. (save-excursion
  11665. (org-back-to-heading t)
  11666. (when (re-search-forward (concat org-scheduled-string " "
  11667. org-last-inserted-timestamp)
  11668. (save-excursion
  11669. (outline-next-heading) (point)) t)
  11670. (goto-char (1- (match-end 0)))
  11671. (insert " " repeater)
  11672. (setq org-last-inserted-timestamp
  11673. (concat (substring org-last-inserted-timestamp 0 -1)
  11674. " " repeater
  11675. (substring org-last-inserted-timestamp -1))))))
  11676. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11677. (defun org-get-scheduled-time (pom &optional inherit)
  11678. "Get the scheduled time as a time tuple, of a format suitable
  11679. for calling org-schedule with, or if there is no scheduling,
  11680. returns nil."
  11681. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11682. (when time
  11683. (apply 'encode-time (org-parse-time-string time)))))
  11684. (defun org-get-deadline-time (pom &optional inherit)
  11685. "Get the deadline as a time tuple, of a format suitable for
  11686. calling org-deadline with, or if there is no scheduling, returns
  11687. nil."
  11688. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11689. (when time
  11690. (apply 'encode-time (org-parse-time-string time)))))
  11691. (defun org-remove-timestamp-with-keyword (keyword)
  11692. "Remove all time stamps with KEYWORD in the current entry."
  11693. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11694. beg)
  11695. (save-excursion
  11696. (org-back-to-heading t)
  11697. (setq beg (point))
  11698. (outline-next-heading)
  11699. (while (re-search-backward re beg t)
  11700. (replace-match "")
  11701. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11702. (equal (char-before) ?\ ))
  11703. (backward-delete-char 1)
  11704. (if (string-match "^[ \t]*$" (buffer-substring
  11705. (point-at-bol) (point-at-eol)))
  11706. (delete-region (point-at-bol)
  11707. (min (point-max) (1+ (point-at-eol))))))))))
  11708. (defun org-add-planning-info (what &optional time &rest remove)
  11709. "Insert new timestamp with keyword in the line directly after the headline.
  11710. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11711. If non is given, the user is prompted for a date.
  11712. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11713. be removed."
  11714. (interactive)
  11715. (let (org-time-was-given org-end-time-was-given ts
  11716. end default-time default-input)
  11717. (catch 'exit
  11718. (when (and (memq what '(scheduled deadline))
  11719. (or (not time)
  11720. (and (stringp time)
  11721. (string-match "^[-+]+[0-9]" time))))
  11722. ;; Try to get a default date/time from existing timestamp
  11723. (save-excursion
  11724. (org-back-to-heading t)
  11725. (setq end (save-excursion (outline-next-heading) (point)))
  11726. (when (re-search-forward (if (eq what 'scheduled)
  11727. org-scheduled-time-regexp
  11728. org-deadline-time-regexp)
  11729. end t)
  11730. (setq ts (match-string 1)
  11731. default-time
  11732. (apply 'encode-time (org-parse-time-string ts))
  11733. default-input (and ts (org-get-compact-tod ts))))))
  11734. (when what
  11735. (setq time
  11736. (if (stringp time)
  11737. ;; This is a string (relative or absolute), set proper date
  11738. (apply 'encode-time
  11739. (org-read-date-analyze
  11740. time default-time (decode-time default-time)))
  11741. ;; If necessary, get the time from the user
  11742. (or time (org-read-date nil 'to-time nil nil
  11743. default-time default-input)))))
  11744. (when (and org-insert-labeled-timestamps-at-point
  11745. (member what '(scheduled deadline)))
  11746. (insert
  11747. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11748. (org-insert-time-stamp time org-time-was-given
  11749. nil nil nil (list org-end-time-was-given))
  11750. (setq what nil))
  11751. (save-excursion
  11752. (save-restriction
  11753. (let (col list elt ts buffer-invisibility-spec)
  11754. (org-back-to-heading t)
  11755. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11756. (goto-char (match-end 1))
  11757. (setq col (current-column))
  11758. (goto-char (match-end 0))
  11759. (if (eobp) (insert "\n") (forward-char 1))
  11760. (when (and (not what)
  11761. (not (looking-at
  11762. (concat "[ \t]*"
  11763. org-keyword-time-not-clock-regexp))))
  11764. ;; Nothing to add, nothing to remove...... :-)
  11765. (throw 'exit nil))
  11766. (if (and (not (looking-at org-outline-regexp))
  11767. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11768. "[^\r\n]*"))
  11769. (not (equal (match-string 1) org-clock-string)))
  11770. (narrow-to-region (match-beginning 0) (match-end 0))
  11771. (insert-before-markers "\n")
  11772. (backward-char 1)
  11773. (narrow-to-region (point) (point))
  11774. (and org-adapt-indentation (org-indent-to-column col)))
  11775. ;; Check if we have to remove something.
  11776. (setq list (cons what remove))
  11777. (while list
  11778. (setq elt (pop list))
  11779. (when (or (and (eq elt 'scheduled)
  11780. (re-search-forward org-scheduled-time-regexp nil t))
  11781. (and (eq elt 'deadline)
  11782. (re-search-forward org-deadline-time-regexp nil t))
  11783. (and (eq elt 'closed)
  11784. (re-search-forward org-closed-time-regexp nil t)))
  11785. (replace-match "")
  11786. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11787. (and (looking-at "[ \t]+") (replace-match ""))
  11788. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11789. (when what
  11790. (insert
  11791. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11792. (cond ((eq what 'scheduled) org-scheduled-string)
  11793. ((eq what 'deadline) org-deadline-string)
  11794. ((eq what 'closed) org-closed-string))
  11795. " ")
  11796. (setq ts (org-insert-time-stamp
  11797. time
  11798. (or org-time-was-given
  11799. (and (eq what 'closed) org-log-done-with-time))
  11800. (eq what 'closed)
  11801. nil nil (list org-end-time-was-given)))
  11802. (insert
  11803. (if (not (or (bolp) (eq (char-before) ?\ )
  11804. (memq (char-after) '(32 10))
  11805. (eobp))) " " ""))
  11806. (end-of-line 1))
  11807. (goto-char (point-min))
  11808. (widen)
  11809. (if (and (looking-at "[ \t]*\n")
  11810. (equal (char-before) ?\n))
  11811. (delete-region (1- (point)) (point-at-eol)))
  11812. ts))))))
  11813. (defvar org-log-note-marker (make-marker))
  11814. (defvar org-log-note-purpose nil)
  11815. (defvar org-log-note-state nil)
  11816. (defvar org-log-note-previous-state nil)
  11817. (defvar org-log-note-how nil)
  11818. (defvar org-log-note-extra nil)
  11819. (defvar org-log-note-window-configuration nil)
  11820. (defvar org-log-note-return-to (make-marker))
  11821. (defvar org-log-note-effective-time nil
  11822. "Remembered current time so that dynamically scoped
  11823. `org-extend-today-until' affects tha timestamps in state change
  11824. log")
  11825. (defvar org-log-post-message nil
  11826. "Message to be displayed after a log note has been stored.
  11827. The auto-repeater uses this.")
  11828. (defun org-add-note ()
  11829. "Add a note to the current entry.
  11830. This is done in the same way as adding a state change note."
  11831. (interactive)
  11832. (org-add-log-setup 'note nil nil 'findpos nil))
  11833. (defvar org-property-end-re)
  11834. (defun org-add-log-setup (&optional purpose state prev-state
  11835. findpos how extra)
  11836. "Set up the post command hook to take a note.
  11837. If this is about to TODO state change, the new state is expected in STATE.
  11838. When FINDPOS is non-nil, find the correct position for the note in
  11839. the current entry. If not, assume that it can be inserted at point.
  11840. HOW is an indicator what kind of note should be created.
  11841. EXTRA is additional text that will be inserted into the notes buffer."
  11842. (let* ((org-log-into-drawer (org-log-into-drawer))
  11843. (drawer (cond ((stringp org-log-into-drawer)
  11844. org-log-into-drawer)
  11845. (org-log-into-drawer "LOGBOOK"))))
  11846. (save-restriction
  11847. (save-excursion
  11848. (when findpos
  11849. (org-back-to-heading t)
  11850. (narrow-to-region (point) (save-excursion
  11851. (outline-next-heading) (point)))
  11852. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11853. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11854. "[^\r\n]*\\)?"))
  11855. (goto-char (match-end 0))
  11856. (cond
  11857. (drawer
  11858. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11859. nil t)
  11860. (progn
  11861. (goto-char (match-end 0))
  11862. (or org-log-states-order-reversed
  11863. (and (re-search-forward org-property-end-re nil t)
  11864. (goto-char (1- (match-beginning 0))))))
  11865. (insert "\n:" drawer ":\n:END:")
  11866. (beginning-of-line 0)
  11867. (org-indent-line)
  11868. (beginning-of-line 2)
  11869. (org-indent-line)
  11870. (end-of-line 0)))
  11871. ((and org-log-state-notes-insert-after-drawers
  11872. (save-excursion
  11873. (forward-line) (looking-at org-drawer-regexp)))
  11874. (forward-line)
  11875. (while (looking-at org-drawer-regexp)
  11876. (goto-char (match-end 0))
  11877. (re-search-forward org-property-end-re (point-max) t)
  11878. (forward-line))
  11879. (forward-line -1)))
  11880. (unless org-log-states-order-reversed
  11881. (and (= (char-after) ?\n) (forward-char 1))
  11882. (org-skip-over-state-notes)
  11883. (skip-chars-backward " \t\n\r")))
  11884. (move-marker org-log-note-marker (point))
  11885. (setq org-log-note-purpose purpose
  11886. org-log-note-state state
  11887. org-log-note-previous-state prev-state
  11888. org-log-note-how how
  11889. org-log-note-extra extra
  11890. org-log-note-effective-time (org-current-effective-time))
  11891. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11892. (defun org-skip-over-state-notes ()
  11893. "Skip past the list of State notes in an entry."
  11894. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11895. (when (ignore-errors (goto-char (org-in-item-p)))
  11896. (let* ((struct (org-list-struct))
  11897. (prevs (org-list-prevs-alist struct)))
  11898. (while (looking-at "[ \t]*- State")
  11899. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11900. (org-list-get-item-end (point) struct)))))))
  11901. (defun org-add-log-note (&optional purpose)
  11902. "Pop up a window for taking a note, and add this note later at point."
  11903. (remove-hook 'post-command-hook 'org-add-log-note)
  11904. (setq org-log-note-window-configuration (current-window-configuration))
  11905. (delete-other-windows)
  11906. (move-marker org-log-note-return-to (point))
  11907. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11908. (goto-char org-log-note-marker)
  11909. (org-switch-to-buffer-other-window "*Org Note*")
  11910. (erase-buffer)
  11911. (if (memq org-log-note-how '(time state))
  11912. (let (current-prefix-arg) (org-store-log-note))
  11913. (let ((org-inhibit-startup t)) (org-mode))
  11914. (insert (format "# Insert note for %s.
  11915. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11916. (cond
  11917. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11918. ((eq org-log-note-purpose 'done) "closed todo item")
  11919. ((eq org-log-note-purpose 'state)
  11920. (format "state change from \"%s\" to \"%s\""
  11921. (or org-log-note-previous-state "")
  11922. (or org-log-note-state "")))
  11923. ((eq org-log-note-purpose 'reschedule)
  11924. "rescheduling")
  11925. ((eq org-log-note-purpose 'delschedule)
  11926. "no longer scheduled")
  11927. ((eq org-log-note-purpose 'redeadline)
  11928. "changing deadline")
  11929. ((eq org-log-note-purpose 'deldeadline)
  11930. "removing deadline")
  11931. ((eq org-log-note-purpose 'refile)
  11932. "refiling")
  11933. ((eq org-log-note-purpose 'note)
  11934. "this entry")
  11935. (t (error "This should not happen")))))
  11936. (if org-log-note-extra (insert org-log-note-extra))
  11937. (org-set-local 'org-finish-function 'org-store-log-note)
  11938. (run-hooks 'org-log-buffer-setup-hook)))
  11939. (defvar org-note-abort nil) ; dynamically scoped
  11940. (defun org-store-log-note ()
  11941. "Finish taking a log note, and insert it to where it belongs."
  11942. (let ((txt (buffer-string)))
  11943. (kill-buffer (current-buffer))
  11944. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11945. lines ind bul)
  11946. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11947. (setq txt (replace-match "" t t txt)))
  11948. (if (string-match "\\s-+\\'" txt)
  11949. (setq txt (replace-match "" t t txt)))
  11950. (setq lines (org-split-string txt "\n"))
  11951. (when (and note (string-match "\\S-" note))
  11952. (setq note
  11953. (org-replace-escapes
  11954. note
  11955. (list (cons "%u" (user-login-name))
  11956. (cons "%U" user-full-name)
  11957. (cons "%t" (format-time-string
  11958. (org-time-stamp-format 'long 'inactive)
  11959. org-log-note-effective-time))
  11960. (cons "%T" (format-time-string
  11961. (org-time-stamp-format 'long nil)
  11962. org-log-note-effective-time))
  11963. (cons "%d" (format-time-string
  11964. (org-time-stamp-format nil 'inactive)
  11965. org-log-note-effective-time))
  11966. (cons "%D" (format-time-string
  11967. (org-time-stamp-format nil nil)
  11968. org-log-note-effective-time))
  11969. (cons "%s" (if org-log-note-state
  11970. (concat "\"" org-log-note-state "\"")
  11971. ""))
  11972. (cons "%S" (if org-log-note-previous-state
  11973. (concat "\"" org-log-note-previous-state "\"")
  11974. "\"\"")))))
  11975. (if lines (setq note (concat note " \\\\")))
  11976. (push note lines))
  11977. (when (or current-prefix-arg org-note-abort)
  11978. (when org-log-into-drawer
  11979. (org-remove-empty-drawer-at
  11980. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11981. org-log-note-marker))
  11982. (setq lines nil))
  11983. (when lines
  11984. (with-current-buffer (marker-buffer org-log-note-marker)
  11985. (save-excursion
  11986. (goto-char org-log-note-marker)
  11987. (move-marker org-log-note-marker nil)
  11988. (end-of-line 1)
  11989. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11990. (setq ind (save-excursion
  11991. (if (ignore-errors (goto-char (org-in-item-p)))
  11992. (let ((struct (org-list-struct)))
  11993. (org-list-get-ind
  11994. (org-list-get-top-point struct) struct))
  11995. (skip-chars-backward " \r\t\n")
  11996. (cond
  11997. ((and (org-at-heading-p)
  11998. org-adapt-indentation)
  11999. (1+ (org-current-level)))
  12000. ((org-at-heading-p) 0)
  12001. (t (org-get-indentation))))))
  12002. (setq bul (org-list-bullet-string "-"))
  12003. (org-indent-line-to ind)
  12004. (insert bul (pop lines))
  12005. (let ((ind-body (+ (length bul) ind)))
  12006. (while lines
  12007. (insert "\n")
  12008. (org-indent-line-to ind-body)
  12009. (insert (pop lines))))
  12010. (message "Note stored")
  12011. (org-back-to-heading t)
  12012. (org-cycle-hide-drawers 'children))))))
  12013. (set-window-configuration org-log-note-window-configuration)
  12014. (with-current-buffer (marker-buffer org-log-note-return-to)
  12015. (goto-char org-log-note-return-to))
  12016. (move-marker org-log-note-return-to nil)
  12017. (and org-log-post-message (message "%s" org-log-post-message)))
  12018. (defun org-remove-empty-drawer-at (drawer pos)
  12019. "Remove an empty drawer DRAWER at position POS.
  12020. POS may also be a marker."
  12021. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12022. (save-excursion
  12023. (save-restriction
  12024. (widen)
  12025. (goto-char pos)
  12026. (if (org-in-regexp
  12027. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12028. (replace-match ""))))))
  12029. (defvar org-ts-type nil)
  12030. (defun org-sparse-tree (&optional arg type)
  12031. "Create a sparse tree, prompt for the details.
  12032. This command can create sparse trees. You first need to select the type
  12033. of match used to create the tree:
  12034. t Show all TODO entries.
  12035. T Show entries with a specific TODO keyword.
  12036. m Show entries selected by a tags/property match.
  12037. p Enter a property name and its value (both with completion on existing
  12038. names/values) and show entries with that property.
  12039. r Show entries matching a regular expression (`/' can be used as well).
  12040. b Show deadlines and scheduled items before a date.
  12041. a Show deadlines and scheduled items after a date.
  12042. d Show deadlines due within `org-deadline-warning-days'.
  12043. D Show deadlines and scheduled items between a date range."
  12044. (interactive "P")
  12045. (let (ans kwd value ts-type)
  12046. (setq type (or type org-sparse-tree-default-date-type))
  12047. (setq org-ts-type type)
  12048. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  12049. (cond ((eq type 'all) "all timestamps")
  12050. ((eq type 'scheduled) "only scheduled")
  12051. ((eq type 'deadline) "only deadline")
  12052. ((eq type 'active) "only active timestamps")
  12053. ((eq type 'inactive) "only inactive timestamps")
  12054. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12055. (t "scheduled/deadline")))
  12056. (setq ans (read-char-exclusive))
  12057. (cond
  12058. ((equal ans ?c)
  12059. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  12060. ((equal ans ?d)
  12061. (call-interactively 'org-check-deadlines))
  12062. ((equal ans ?b)
  12063. (call-interactively 'org-check-before-date))
  12064. ((equal ans ?a)
  12065. (call-interactively 'org-check-after-date))
  12066. ((equal ans ?D)
  12067. (call-interactively 'org-check-dates-range))
  12068. ((equal ans ?t)
  12069. (call-interactively 'org-show-todo-tree))
  12070. ((equal ans ?T)
  12071. (org-show-todo-tree '(4)))
  12072. ((member ans '(?T ?m))
  12073. (call-interactively 'org-match-sparse-tree))
  12074. ((member ans '(?p ?P))
  12075. (setq kwd (org-icompleting-read "Property: "
  12076. (mapcar 'list (org-buffer-property-keys))))
  12077. (setq value (org-icompleting-read "Value: "
  12078. (mapcar 'list (org-property-values kwd))))
  12079. (unless (string-match "\\`{.*}\\'" value)
  12080. (setq value (concat "\"" value "\"")))
  12081. (org-match-sparse-tree arg (concat kwd "=" value)))
  12082. ((member ans '(?r ?R ?/))
  12083. (call-interactively 'org-occur))
  12084. (t (error "No such sparse tree command \"%c\"" ans)))))
  12085. (defvar org-occur-highlights nil
  12086. "List of overlays used for occur matches.")
  12087. (make-variable-buffer-local 'org-occur-highlights)
  12088. (defvar org-occur-parameters nil
  12089. "Parameters of the active org-occur calls.
  12090. This is a list, each call to org-occur pushes as cons cell,
  12091. containing the regular expression and the callback, onto the list.
  12092. The list can contain several entries if `org-occur' has been called
  12093. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12094. will only contain one set of parameters. When the highlights are
  12095. removed (for example with `C-c C-c', or with the next edit (depending
  12096. on `org-remove-highlights-with-change'), this variable is emptied
  12097. as well.")
  12098. (make-variable-buffer-local 'org-occur-parameters)
  12099. (defun org-occur (regexp &optional keep-previous callback)
  12100. "Make a compact tree which shows all matches of REGEXP.
  12101. The tree will show the lines where the regexp matches, and all higher
  12102. headlines above the match. It will also show the heading after the match,
  12103. to make sure editing the matching entry is easy.
  12104. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12105. call to `org-occur' will be kept, to allow stacking of calls to this
  12106. command.
  12107. If CALLBACK is non-nil, it is a function which is called to confirm
  12108. that the match should indeed be shown."
  12109. (interactive "sRegexp: \nP")
  12110. (when (equal regexp "")
  12111. (error "Regexp cannot be empty"))
  12112. (unless keep-previous
  12113. (org-remove-occur-highlights nil nil t))
  12114. (push (cons regexp callback) org-occur-parameters)
  12115. (let ((cnt 0))
  12116. (save-excursion
  12117. (goto-char (point-min))
  12118. (if (or (not keep-previous) ; do not want to keep
  12119. (not org-occur-highlights)) ; no previous matches
  12120. ;; hide everything
  12121. (org-overview))
  12122. (while (re-search-forward regexp nil t)
  12123. (when (or (not callback)
  12124. (save-match-data (funcall callback)))
  12125. (setq cnt (1+ cnt))
  12126. (when org-highlight-sparse-tree-matches
  12127. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12128. (org-show-context 'occur-tree))))
  12129. (when org-remove-highlights-with-change
  12130. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12131. nil 'local))
  12132. (unless org-sparse-tree-open-archived-trees
  12133. (org-hide-archived-subtrees (point-min) (point-max)))
  12134. (run-hooks 'org-occur-hook)
  12135. (if (org-called-interactively-p 'interactive)
  12136. (message "%d match(es) for regexp %s" cnt regexp))
  12137. cnt))
  12138. (defun org-occur-next-match (&optional n reset)
  12139. "Function for `next-error-function' to find sparse tree matches.
  12140. N is the number of matches to move, when negative move backwards.
  12141. RESET is entirely ignored - this function always goes back to the
  12142. starting point when no match is found."
  12143. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12144. (search-func (if (< n 0)
  12145. 'previous-single-char-property-change
  12146. 'next-single-char-property-change))
  12147. (n (abs n))
  12148. (pos (point))
  12149. p1)
  12150. (catch 'exit
  12151. (while (setq p1 (funcall search-func (point) 'org-type))
  12152. (when (equal p1 limit)
  12153. (goto-char pos)
  12154. (error "No more matches"))
  12155. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12156. (setq n (1- n))
  12157. (when (= n 0)
  12158. (goto-char p1)
  12159. (throw 'exit (point))))
  12160. (goto-char p1))
  12161. (goto-char p1)
  12162. (error "No more matches"))))
  12163. (defun org-show-context (&optional key)
  12164. "Make sure point and context are visible.
  12165. How much context is shown depends upon the variables
  12166. `org-show-hierarchy-above', `org-show-following-heading',
  12167. `org-show-entry-below' and `org-show-siblings'."
  12168. (let ((heading-p (org-at-heading-p t))
  12169. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12170. (following-p (org-get-alist-option org-show-following-heading key))
  12171. (entry-p (org-get-alist-option org-show-entry-below key))
  12172. (siblings-p (org-get-alist-option org-show-siblings key)))
  12173. ;; Show heading or entry text
  12174. (if (and heading-p (not entry-p))
  12175. (org-flag-heading nil) ; only show the heading
  12176. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12177. (org-show-hidden-entry))) ; show entire entry
  12178. (when following-p
  12179. ;; Show next sibling, or heading below text
  12180. (save-excursion
  12181. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12182. (org-flag-heading nil))))
  12183. (when siblings-p (org-show-siblings))
  12184. (when hierarchy-p
  12185. ;; show all higher headings, possibly with siblings
  12186. (save-excursion
  12187. (while (and (condition-case nil
  12188. (progn (org-up-heading-all 1) t)
  12189. (error nil))
  12190. (not (bobp)))
  12191. (org-flag-heading nil)
  12192. (when siblings-p (org-show-siblings)))))
  12193. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12194. (defvar org-reveal-start-hook nil
  12195. "Hook run before revealing a location.")
  12196. (defun org-reveal (&optional siblings)
  12197. "Show current entry, hierarchy above it, and the following headline.
  12198. This can be used to show a consistent set of context around locations
  12199. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12200. not t for the search context.
  12201. With optional argument SIBLINGS, on each level of the hierarchy all
  12202. siblings are shown. This repairs the tree structure to what it would
  12203. look like when opened with hierarchical calls to `org-cycle'.
  12204. With double optional argument \\[universal-argument] \\[universal-argument], \
  12205. go to the parent and show the
  12206. entire tree."
  12207. (interactive "P")
  12208. (run-hooks 'org-reveal-start-hook)
  12209. (let ((org-show-hierarchy-above t)
  12210. (org-show-following-heading t)
  12211. (org-show-siblings (if siblings t org-show-siblings)))
  12212. (org-show-context nil))
  12213. (when (equal siblings '(16))
  12214. (save-excursion
  12215. (when (org-up-heading-safe)
  12216. (org-show-subtree)
  12217. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12218. (defun org-highlight-new-match (beg end)
  12219. "Highlight from BEG to END and mark the highlight is an occur headline."
  12220. (let ((ov (make-overlay beg end)))
  12221. (overlay-put ov 'face 'secondary-selection)
  12222. (overlay-put ov 'org-type 'org-occur)
  12223. (push ov org-occur-highlights)))
  12224. (defun org-remove-occur-highlights (&optional beg end noremove)
  12225. "Remove the occur highlights from the buffer.
  12226. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12227. from the `before-change-functions' in the current buffer."
  12228. (interactive)
  12229. (unless org-inhibit-highlight-removal
  12230. (mapc 'delete-overlay org-occur-highlights)
  12231. (setq org-occur-highlights nil)
  12232. (setq org-occur-parameters nil)
  12233. (unless noremove
  12234. (remove-hook 'before-change-functions
  12235. 'org-remove-occur-highlights 'local))))
  12236. ;;;; Priorities
  12237. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12238. "Regular expression matching the priority indicator.")
  12239. (defvar org-remove-priority-next-time nil)
  12240. (defun org-priority-up ()
  12241. "Increase the priority of the current item."
  12242. (interactive)
  12243. (org-priority 'up))
  12244. (defun org-priority-down ()
  12245. "Decrease the priority of the current item."
  12246. (interactive)
  12247. (org-priority 'down))
  12248. (defun org-priority (&optional action show)
  12249. "Change the priority of an item.
  12250. ACTION can be `set', `up', `down', or a character."
  12251. (interactive "P")
  12252. (if (equal action '(4))
  12253. (org-show-priority)
  12254. (unless org-enable-priority-commands
  12255. (error "Priority commands are disabled"))
  12256. (setq action (or action 'set))
  12257. (let (current new news have remove)
  12258. (save-excursion
  12259. (org-back-to-heading t)
  12260. (if (looking-at org-priority-regexp)
  12261. (setq current (string-to-char (match-string 2))
  12262. have t))
  12263. (cond
  12264. ((eq action 'remove)
  12265. (setq remove t new ?\ ))
  12266. ((or (eq action 'set)
  12267. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12268. (if (not (eq action 'set))
  12269. (setq new action)
  12270. (message "Priority %c-%c, SPC to remove: "
  12271. org-highest-priority org-lowest-priority)
  12272. (save-match-data
  12273. (setq new (read-char-exclusive))))
  12274. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12275. (= (upcase org-lowest-priority) org-lowest-priority))
  12276. (setq new (upcase new)))
  12277. (cond ((equal new ?\ ) (setq remove t))
  12278. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12279. (error "Priority must be between `%c' and `%c'"
  12280. org-highest-priority org-lowest-priority))))
  12281. ((eq action 'up)
  12282. (setq new (if have
  12283. (1- current) ; normal cycling
  12284. ;; last priority was empty
  12285. (if (eq last-command this-command)
  12286. org-lowest-priority ; wrap around empty to lowest
  12287. ;; default
  12288. (if org-priority-start-cycle-with-default
  12289. org-default-priority
  12290. (1- org-default-priority))))))
  12291. ((eq action 'down)
  12292. (setq new (if have
  12293. (1+ current) ; normal cycling
  12294. ;; last priority was empty
  12295. (if (eq last-command this-command)
  12296. org-highest-priority ; wrap around empty to highest
  12297. ;; default
  12298. (if org-priority-start-cycle-with-default
  12299. org-default-priority
  12300. (1+ org-default-priority))))))
  12301. (t (error "Invalid action")))
  12302. (if (or (< (upcase new) org-highest-priority)
  12303. (> (upcase new) org-lowest-priority))
  12304. (if (and (memq action '(up down))
  12305. (not have) (not (eq last-command this-command)))
  12306. ;; `new' is from default priority
  12307. (error
  12308. "The default can not be set, see `org-default-priority' why")
  12309. ;; normal cycling: `new' is beyond highest/lowest priority
  12310. ;; and is wrapped around to the empty priority
  12311. (setq remove t)))
  12312. (setq news (format "%c" new))
  12313. (if have
  12314. (if remove
  12315. (replace-match "" t t nil 1)
  12316. (replace-match news t t nil 2))
  12317. (if remove
  12318. (error "No priority cookie found in line")
  12319. (let ((case-fold-search nil))
  12320. (looking-at org-todo-line-regexp))
  12321. (if (match-end 2)
  12322. (progn
  12323. (goto-char (match-end 2))
  12324. (insert " [#" news "]"))
  12325. (goto-char (match-beginning 3))
  12326. (insert "[#" news "] "))))
  12327. (org-preserve-lc (org-set-tags nil 'align)))
  12328. (if remove
  12329. (message "Priority removed")
  12330. (message "Priority of current item set to %s" news)))))
  12331. (defun org-show-priority ()
  12332. "Show the priority of the current item.
  12333. This priority is composed of the main priority given with the [#A] cookies,
  12334. and by additional input from the age of a schedules or deadline entry."
  12335. (interactive)
  12336. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12337. (org-get-at-bol 'priority)
  12338. (save-excursion
  12339. (save-match-data
  12340. (beginning-of-line)
  12341. (and (looking-at org-heading-regexp)
  12342. (org-get-priority (match-string 0))))))))
  12343. (message "Priority is %d" (if pri pri -1000))))
  12344. (defun org-get-priority (s)
  12345. "Find priority cookie and return priority."
  12346. (save-match-data
  12347. (if (functionp org-get-priority-function)
  12348. (funcall org-get-priority-function)
  12349. (if (not (string-match org-priority-regexp s))
  12350. (* 1000 (- org-lowest-priority org-default-priority))
  12351. (* 1000 (- org-lowest-priority
  12352. (string-to-char (match-string 2 s))))))))
  12353. ;;;; Tags
  12354. (defvar org-agenda-archives-mode)
  12355. (defvar org-map-continue-from nil
  12356. "Position from where mapping should continue.
  12357. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12358. (defvar org-scanner-tags nil
  12359. "The current tag list while the tags scanner is running.")
  12360. (defvar org-trust-scanner-tags nil
  12361. "Should `org-get-tags-at' use the tags for the scanner.
  12362. This is for internal dynamical scoping only.
  12363. When this is non-nil, the function `org-get-tags-at' will return the value
  12364. of `org-scanner-tags' instead of building the list by itself. This
  12365. can lead to large speed-ups when the tags scanner is used in a file with
  12366. many entries, and when the list of tags is retrieved, for example to
  12367. obtain a list of properties. Building the tags list for each entry in such
  12368. a file becomes an N^2 operation - but with this variable set, it scales
  12369. as N.")
  12370. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12371. "Sca headline tags with inheritance and produce output ACTION.
  12372. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12373. or `agenda' to produce an entry list for an agenda view. It can also be
  12374. a Lisp form or a function that should be called at each matched headline, in
  12375. this case the return value is a list of all return values from these calls.
  12376. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12377. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12378. only lines with a not-done TODO keyword are included in the output.
  12379. This should be the same variable that was scoped into
  12380. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12381. START-LEVEL can be a string with asterisks, reducing the scope to
  12382. headlines matching this string."
  12383. (require 'org-agenda)
  12384. (let* ((re (concat "^"
  12385. (if start-level
  12386. ;; Get the correct level to match
  12387. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12388. org-outline-regexp)
  12389. " *\\(\\<\\("
  12390. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12391. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12392. (props (list 'face 'default
  12393. 'done-face 'org-agenda-done
  12394. 'undone-face 'default
  12395. 'mouse-face 'highlight
  12396. 'org-not-done-regexp org-not-done-regexp
  12397. 'org-todo-regexp org-todo-regexp
  12398. 'org-complex-heading-regexp org-complex-heading-regexp
  12399. 'help-echo
  12400. (format "mouse-2 or RET jump to org file %s"
  12401. (abbreviate-file-name
  12402. (or (buffer-file-name (buffer-base-buffer))
  12403. (buffer-name (buffer-base-buffer)))))))
  12404. (case-fold-search nil)
  12405. (org-map-continue-from nil)
  12406. lspos tags tags-list
  12407. (tags-alist (list (cons 0 org-file-tags)))
  12408. (llast 0) rtn rtn1 level category i txt
  12409. todo marker entry priority)
  12410. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12411. (setq action (list 'lambda nil action)))
  12412. (save-excursion
  12413. (goto-char (point-min))
  12414. (when (eq action 'sparse-tree)
  12415. (org-overview)
  12416. (org-remove-occur-highlights))
  12417. (while (re-search-forward re nil t)
  12418. (setq org-map-continue-from nil)
  12419. (catch :skip
  12420. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12421. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12422. (goto-char (setq lspos (match-beginning 0)))
  12423. (setq level (org-reduced-level (org-outline-level))
  12424. category (org-get-category))
  12425. (setq i llast llast level)
  12426. ;; remove tag lists from same and sublevels
  12427. (while (>= i level)
  12428. (when (setq entry (assoc i tags-alist))
  12429. (setq tags-alist (delete entry tags-alist)))
  12430. (setq i (1- i)))
  12431. ;; add the next tags
  12432. (when tags
  12433. (setq tags (org-split-string tags ":")
  12434. tags-alist
  12435. (cons (cons level tags) tags-alist)))
  12436. ;; compile tags for current headline
  12437. (setq tags-list
  12438. (if org-use-tag-inheritance
  12439. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12440. tags)
  12441. org-scanner-tags tags-list)
  12442. (when org-use-tag-inheritance
  12443. (setcdr (car tags-alist)
  12444. (mapcar (lambda (x)
  12445. (setq x (copy-sequence x))
  12446. (org-add-prop-inherited x))
  12447. (cdar tags-alist))))
  12448. (when (and tags org-use-tag-inheritance
  12449. (or (not (eq t org-use-tag-inheritance))
  12450. org-tags-exclude-from-inheritance))
  12451. ;; selective inheritance, remove uninherited ones
  12452. (setcdr (car tags-alist)
  12453. (org-remove-uninherited-tags (cdar tags-alist))))
  12454. (when (and
  12455. ;; eval matcher only when the todo condition is OK
  12456. (and (or (not todo-only) (member todo org-not-done-keywords))
  12457. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12458. (eval matcher)))
  12459. ;; Call the skipper, but return t if it does not skip,
  12460. ;; so that the `and' form continues evaluating
  12461. (progn
  12462. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12463. t)
  12464. ;; Check if timestamps are deselecting this entry
  12465. (or (not todo-only)
  12466. (and (member todo org-not-done-keywords)
  12467. (or (not org-agenda-tags-todo-honor-ignore-options)
  12468. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12469. ;; select this headline
  12470. (cond
  12471. ((eq action 'sparse-tree)
  12472. (and org-highlight-sparse-tree-matches
  12473. (org-get-heading) (match-end 0)
  12474. (org-highlight-new-match
  12475. (match-beginning 1) (match-end 1)))
  12476. (org-show-context 'tags-tree))
  12477. ((eq action 'agenda)
  12478. (setq txt (org-agenda-format-item
  12479. ""
  12480. (concat
  12481. (if (eq org-tags-match-list-sublevels 'indented)
  12482. (make-string (1- level) ?.) "")
  12483. (org-get-heading))
  12484. level category
  12485. tags-list)
  12486. priority (org-get-priority txt))
  12487. (goto-char lspos)
  12488. (setq marker (org-agenda-new-marker))
  12489. (org-add-props txt props
  12490. 'org-marker marker 'org-hd-marker marker 'org-category category
  12491. 'todo-state todo
  12492. 'priority priority 'type "tagsmatch")
  12493. (push txt rtn))
  12494. ((functionp action)
  12495. (setq org-map-continue-from nil)
  12496. (save-excursion
  12497. (setq rtn1 (funcall action))
  12498. (push rtn1 rtn)))
  12499. (t (error "Invalid action")))
  12500. ;; if we are to skip sublevels, jump to end of subtree
  12501. (unless org-tags-match-list-sublevels
  12502. (org-end-of-subtree t)
  12503. (backward-char 1))))
  12504. ;; Get the correct position from where to continue
  12505. (if org-map-continue-from
  12506. (goto-char org-map-continue-from)
  12507. (and (= (point) lspos) (end-of-line 1)))))
  12508. (when (and (eq action 'sparse-tree)
  12509. (not org-sparse-tree-open-archived-trees))
  12510. (org-hide-archived-subtrees (point-min) (point-max)))
  12511. (nreverse rtn)))
  12512. (defun org-remove-uninherited-tags (tags)
  12513. "Remove all tags that are not inherited from the list TAGS."
  12514. (cond
  12515. ((eq org-use-tag-inheritance t)
  12516. (if org-tags-exclude-from-inheritance
  12517. (org-delete-all org-tags-exclude-from-inheritance tags)
  12518. tags))
  12519. ((not org-use-tag-inheritance) nil)
  12520. ((stringp org-use-tag-inheritance)
  12521. (delq nil (mapcar
  12522. (lambda (x)
  12523. (if (and (string-match org-use-tag-inheritance x)
  12524. (not (member x org-tags-exclude-from-inheritance)))
  12525. x nil))
  12526. tags)))
  12527. ((listp org-use-tag-inheritance)
  12528. (delq nil (mapcar
  12529. (lambda (x)
  12530. (if (member x org-use-tag-inheritance) x nil))
  12531. tags)))))
  12532. (defun org-match-sparse-tree (&optional todo-only match)
  12533. "Create a sparse tree according to tags string MATCH.
  12534. MATCH can contain positive and negative selection of tags, like
  12535. \"+WORK+URGENT-WITHBOSS\".
  12536. If optional argument TODO-ONLY is non-nil, only select lines that are
  12537. also TODO lines."
  12538. (interactive "P")
  12539. (org-agenda-prepare-buffers (list (current-buffer)))
  12540. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12541. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12542. (defvar org-cached-props nil)
  12543. (defun org-cached-entry-get (pom property)
  12544. (if (or (eq t org-use-property-inheritance)
  12545. (and (stringp org-use-property-inheritance)
  12546. (string-match org-use-property-inheritance property))
  12547. (and (listp org-use-property-inheritance)
  12548. (member property org-use-property-inheritance)))
  12549. ;; Caching is not possible, check it directly
  12550. (org-entry-get pom property 'inherit)
  12551. ;; Get all properties, so that we can do complicated checks easily
  12552. (cdr (assoc property (or org-cached-props
  12553. (setq org-cached-props
  12554. (org-entry-properties pom)))))))
  12555. (defun org-global-tags-completion-table (&optional files)
  12556. "Return the list of all tags in all agenda buffer/files.
  12557. Optional FILES argument is a list of files which can be used
  12558. instead of the agenda files."
  12559. (save-excursion
  12560. (org-uniquify
  12561. (delq nil
  12562. (apply 'append
  12563. (mapcar
  12564. (lambda (file)
  12565. (set-buffer (find-file-noselect file))
  12566. (append (org-get-buffer-tags)
  12567. (mapcar (lambda (x) (if (stringp (car-safe x))
  12568. (list (car-safe x)) nil))
  12569. org-tag-alist)))
  12570. (if (and files (car files))
  12571. files
  12572. (org-agenda-files))))))))
  12573. (defun org-make-tags-matcher (match)
  12574. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12575. The variable `todo-only' is scoped dynamically into this function.
  12576. It will be set to t if the matcher restricts matching to TODO entries,
  12577. otherwise will not be touched.
  12578. Returns a cons of the selection string MATCH and the constructed
  12579. lisp form implementing the matcher. The matcher is to be evaluated
  12580. at an Org entry, with point on the headline, and returns t if the
  12581. entry matches the selection string MATCH. The returned lisp form
  12582. references two variables with information about the entry, which
  12583. must be bound around the form's evaluation: todo, the TODO keyword
  12584. at the entry (or nil of none); and tags-list, the list of all tags
  12585. at the entry including inherited ones. Additionally, the category
  12586. of the entry (if any) must be specified as the text property
  12587. 'org-category on the headline.
  12588. See also `org-scan-tags'.
  12589. "
  12590. (declare (special todo-only))
  12591. (unless (boundp 'todo-only)
  12592. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12593. (unless match
  12594. ;; Get a new match request, with completion
  12595. (let ((org-last-tags-completion-table
  12596. (org-global-tags-completion-table)))
  12597. (setq match (org-completing-read-no-i
  12598. "Match: " 'org-tags-completion-function nil nil nil
  12599. 'org-tags-history))))
  12600. ;; Parse the string and create a lisp form
  12601. (let ((match0 match)
  12602. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12603. minus tag mm
  12604. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12605. orterms term orlist re-p str-p level-p level-op time-p
  12606. prop-p pn pv po gv rest)
  12607. (if (string-match "/+" match)
  12608. ;; match contains also a todo-matching request
  12609. (progn
  12610. (setq tagsmatch (substring match 0 (match-beginning 0))
  12611. todomatch (substring match (match-end 0)))
  12612. (if (string-match "^!" todomatch)
  12613. (setq todo-only t todomatch (substring todomatch 1)))
  12614. (if (string-match "^\\s-*$" todomatch)
  12615. (setq todomatch nil)))
  12616. ;; only matching tags
  12617. (setq tagsmatch match todomatch nil))
  12618. ;; Make the tags matcher
  12619. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12620. (setq tagsmatcher t)
  12621. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12622. (while (setq term (pop orterms))
  12623. (while (and (equal (substring term -1) "\\") orterms)
  12624. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12625. (while (string-match re term)
  12626. (setq rest (substring term (match-end 0))
  12627. minus (and (match-end 1)
  12628. (equal (match-string 1 term) "-"))
  12629. tag (save-match-data (replace-regexp-in-string
  12630. "\\\\-" "-"
  12631. (match-string 2 term)))
  12632. re-p (equal (string-to-char tag) ?{)
  12633. level-p (match-end 4)
  12634. prop-p (match-end 5)
  12635. mm (cond
  12636. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12637. (level-p
  12638. (setq level-op (org-op-to-function (match-string 3 term)))
  12639. `(,level-op level ,(string-to-number
  12640. (match-string 4 term))))
  12641. (prop-p
  12642. (setq pn (match-string 5 term)
  12643. po (match-string 6 term)
  12644. pv (match-string 7 term)
  12645. re-p (equal (string-to-char pv) ?{)
  12646. str-p (equal (string-to-char pv) ?\")
  12647. time-p (save-match-data
  12648. (string-match "^\"[[<].*[]>]\"$" pv))
  12649. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12650. (if time-p (setq pv (org-matcher-time pv)))
  12651. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12652. (cond
  12653. ((equal pn "CATEGORY")
  12654. (setq gv '(get-text-property (point) 'org-category)))
  12655. ((equal pn "TODO")
  12656. (setq gv 'todo))
  12657. (t
  12658. (setq gv `(org-cached-entry-get nil ,pn))))
  12659. (if re-p
  12660. (if (eq po 'org<>)
  12661. `(not (string-match ,pv (or ,gv "")))
  12662. `(string-match ,pv (or ,gv "")))
  12663. (if str-p
  12664. `(,po (or ,gv "") ,pv)
  12665. `(,po (string-to-number (or ,gv ""))
  12666. ,(string-to-number pv) ))))
  12667. (t `(member ,tag tags-list)))
  12668. mm (if minus (list 'not mm) mm)
  12669. term rest)
  12670. (push mm tagsmatcher))
  12671. (push (if (> (length tagsmatcher) 1)
  12672. (cons 'and tagsmatcher)
  12673. (car tagsmatcher))
  12674. orlist)
  12675. (setq tagsmatcher nil))
  12676. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12677. (setq tagsmatcher
  12678. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12679. ;; Make the todo matcher
  12680. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12681. (setq todomatcher t)
  12682. (setq orterms (org-split-string todomatch "|") orlist nil)
  12683. (while (setq term (pop orterms))
  12684. (while (string-match re term)
  12685. (setq minus (and (match-end 1)
  12686. (equal (match-string 1 term) "-"))
  12687. kwd (match-string 2 term)
  12688. re-p (equal (string-to-char kwd) ?{)
  12689. term (substring term (match-end 0))
  12690. mm (if re-p
  12691. `(string-match ,(substring kwd 1 -1) todo)
  12692. (list 'equal 'todo kwd))
  12693. mm (if minus (list 'not mm) mm))
  12694. (push mm todomatcher))
  12695. (push (if (> (length todomatcher) 1)
  12696. (cons 'and todomatcher)
  12697. (car todomatcher))
  12698. orlist)
  12699. (setq todomatcher nil))
  12700. (setq todomatcher (if (> (length orlist) 1)
  12701. (cons 'or orlist) (car orlist))))
  12702. ;; Return the string and lisp forms of the matcher
  12703. (setq matcher (if todomatcher
  12704. (list 'and tagsmatcher todomatcher)
  12705. tagsmatcher))
  12706. (when todo-only
  12707. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12708. matcher)))
  12709. (cons match0 matcher)))
  12710. (defun org-op-to-function (op &optional stringp)
  12711. "Turn an operator into the appropriate function."
  12712. (setq op
  12713. (cond
  12714. ((equal op "<" ) '(< string< org-time<))
  12715. ((equal op ">" ) '(> org-string> org-time>))
  12716. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12717. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12718. ((member op '("=" "==")) '(= string= org-time=))
  12719. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12720. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12721. (defun org<> (a b) (not (= a b)))
  12722. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12723. (defun org-string>= (a b) (not (string< a b)))
  12724. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12725. (defun org-string<> (a b) (not (string= a b)))
  12726. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12727. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12728. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12729. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12730. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12731. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12732. (defun org-2ft (s)
  12733. "Convert S to a floating point time.
  12734. If S is already a number, just return it. If it is a string, parse
  12735. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12736. (cond
  12737. ((numberp s) s)
  12738. ((stringp s)
  12739. (condition-case nil
  12740. (float-time (apply 'encode-time (org-parse-time-string s)))
  12741. (error 0.)))
  12742. (t 0.)))
  12743. (defun org-time-today ()
  12744. "Time in seconds today at 0:00.
  12745. Returns the float number of seconds since the beginning of the
  12746. epoch to the beginning of today (00:00)."
  12747. (float-time (apply 'encode-time
  12748. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12749. (defun org-matcher-time (s)
  12750. "Interpret a time comparison value."
  12751. (save-match-data
  12752. (cond
  12753. ((string= s "<now>") (float-time))
  12754. ((string= s "<today>") (org-time-today))
  12755. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12756. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12757. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12758. (+ (org-time-today)
  12759. (* (string-to-number (match-string 1 s))
  12760. (cdr (assoc (match-string 2 s)
  12761. '(("d" . 86400.0) ("w" . 604800.0)
  12762. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12763. (t (org-2ft s)))))
  12764. (defun org-match-any-p (re list)
  12765. "Does re match any element of list?"
  12766. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12767. (delq nil list))
  12768. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12769. (defvar org-tags-overlay (make-overlay 1 1))
  12770. (org-detach-overlay org-tags-overlay)
  12771. (defun org-get-local-tags-at (&optional pos)
  12772. "Get a list of tags defined in the current headline."
  12773. (org-get-tags-at pos 'local))
  12774. (defun org-get-local-tags ()
  12775. "Get a list of tags defined in the current headline."
  12776. (org-get-tags-at nil 'local))
  12777. (defun org-get-tags-at (&optional pos local)
  12778. "Get a list of all headline tags applicable at POS.
  12779. POS defaults to point. If tags are inherited, the list contains
  12780. the targets in the same sequence as the headlines appear, i.e.
  12781. the tags of the current headline come last.
  12782. When LOCAL is non-nil, only return tags from the current headline,
  12783. ignore inherited ones."
  12784. (interactive)
  12785. (if (and org-trust-scanner-tags
  12786. (or (not pos) (equal pos (point)))
  12787. (not local))
  12788. org-scanner-tags
  12789. (let (tags ltags lastpos parent)
  12790. (save-excursion
  12791. (save-restriction
  12792. (widen)
  12793. (goto-char (or pos (point)))
  12794. (save-match-data
  12795. (catch 'done
  12796. (condition-case nil
  12797. (progn
  12798. (org-back-to-heading t)
  12799. (while (not (equal lastpos (point)))
  12800. (setq lastpos (point))
  12801. (when (looking-at
  12802. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12803. (setq ltags (org-split-string
  12804. (org-match-string-no-properties 1) ":"))
  12805. (when parent
  12806. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12807. (setq tags (append
  12808. (if parent
  12809. (org-remove-uninherited-tags ltags)
  12810. ltags)
  12811. tags)))
  12812. (or org-use-tag-inheritance (throw 'done t))
  12813. (if local (throw 'done t))
  12814. (or (org-up-heading-safe) (error nil))
  12815. (setq parent t)))
  12816. (error nil)))))
  12817. (if local
  12818. tags
  12819. (reverse (delete-dups
  12820. (reverse (append
  12821. (org-remove-uninherited-tags
  12822. org-file-tags) tags)))))))))
  12823. (defun org-add-prop-inherited (s)
  12824. (add-text-properties 0 (length s) '(inherited t) s)
  12825. s)
  12826. (defun org-toggle-tag (tag &optional onoff)
  12827. "Toggle the tag TAG for the current line.
  12828. If ONOFF is `on' or `off', don't toggle but set to this state."
  12829. (let (res current)
  12830. (save-excursion
  12831. (org-back-to-heading t)
  12832. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12833. (point-at-eol) t)
  12834. (progn
  12835. (setq current (match-string 1))
  12836. (replace-match ""))
  12837. (setq current ""))
  12838. (setq current (nreverse (org-split-string current ":")))
  12839. (cond
  12840. ((eq onoff 'on)
  12841. (setq res t)
  12842. (or (member tag current) (push tag current)))
  12843. ((eq onoff 'off)
  12844. (or (not (member tag current)) (setq current (delete tag current))))
  12845. (t (if (member tag current)
  12846. (setq current (delete tag current))
  12847. (setq res t)
  12848. (push tag current))))
  12849. (end-of-line 1)
  12850. (if current
  12851. (progn
  12852. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12853. (org-set-tags nil t))
  12854. (delete-horizontal-space))
  12855. (run-hooks 'org-after-tags-change-hook))
  12856. res))
  12857. (defun org-align-tags-here (to-col)
  12858. ;; Assumes that this is a headline
  12859. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12860. (beginning-of-line 1)
  12861. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12862. (< pos (match-beginning 2)))
  12863. (progn
  12864. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12865. (goto-char (match-beginning 1))
  12866. (insert " ")
  12867. (delete-region (point) (1+ (match-beginning 2)))
  12868. (setq ncol (max (current-column)
  12869. (1+ col)
  12870. (if (> to-col 0)
  12871. to-col
  12872. (- (abs to-col) tags-l))))
  12873. (setq p (point))
  12874. (insert (make-string (- ncol (current-column)) ?\ ))
  12875. (setq ncol (current-column))
  12876. (when indent-tabs-mode (tabify p (point-at-eol)))
  12877. (org-move-to-column (min ncol col) t))
  12878. (goto-char pos))))
  12879. (defun org-set-tags-command (&optional arg just-align)
  12880. "Call the set-tags command for the current entry."
  12881. (interactive "P")
  12882. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12883. (org-set-tags arg just-align)
  12884. (save-excursion
  12885. (unless (and (org-region-active-p)
  12886. org-loop-over-headlines-in-active-region)
  12887. (org-back-to-heading t))
  12888. (org-set-tags arg just-align))))
  12889. (defun org-set-tags-to (data)
  12890. "Set the tags of the current entry to DATA, replacing the current tags.
  12891. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12892. If DATA is nil or the empty string, any tags will be removed."
  12893. (interactive "sTags: ")
  12894. (setq data
  12895. (cond
  12896. ((eq data nil) "")
  12897. ((equal data "") "")
  12898. ((stringp data)
  12899. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12900. ":"))
  12901. ((listp data)
  12902. (concat ":" (mapconcat 'identity data ":") ":"))))
  12903. (when data
  12904. (save-excursion
  12905. (org-back-to-heading t)
  12906. (when (looking-at org-complex-heading-regexp)
  12907. (if (match-end 5)
  12908. (progn
  12909. (goto-char (match-beginning 5))
  12910. (insert data)
  12911. (delete-region (point) (point-at-eol))
  12912. (org-set-tags nil 'align))
  12913. (goto-char (point-at-eol))
  12914. (insert " " data)
  12915. (org-set-tags nil 'align)))
  12916. (beginning-of-line 1)
  12917. (if (looking-at ".*?\\([ \t]+\\)$")
  12918. (delete-region (match-beginning 1) (match-end 1))))))
  12919. (defun org-align-all-tags ()
  12920. "Align the tags i all headings."
  12921. (interactive)
  12922. (save-excursion
  12923. (or (ignore-errors (org-back-to-heading t))
  12924. (outline-next-heading))
  12925. (if (org-at-heading-p)
  12926. (org-set-tags t)
  12927. (message "No headings"))))
  12928. (defvar org-indent-indentation-per-level)
  12929. (defun org-set-tags (&optional arg just-align)
  12930. "Set the tags for the current headline.
  12931. With prefix ARG, realign all tags in headings in the current buffer."
  12932. (interactive "P")
  12933. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12934. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12935. 'region-start-level 'region))
  12936. org-loop-over-headlines-in-active-region)
  12937. (org-map-entries
  12938. ;; We don't use ARG and JUST-ALIGN here these args are not
  12939. ;; useful when looping over headlines
  12940. `(org-set-tags)
  12941. org-loop-over-headlines-in-active-region
  12942. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12943. (let* ((re org-outline-regexp-bol)
  12944. (current (unless arg (org-get-tags-string)))
  12945. (col (current-column))
  12946. (org-setting-tags t)
  12947. table current-tags inherited-tags ; computed below when needed
  12948. tags p0 c0 c1 rpl di tc level)
  12949. (if arg
  12950. (save-excursion
  12951. (goto-char (point-min))
  12952. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12953. (while (re-search-forward re nil t)
  12954. (org-set-tags nil t)
  12955. (end-of-line 1)))
  12956. (message "All tags realigned to column %d" org-tags-column))
  12957. (if just-align
  12958. (setq tags current)
  12959. ;; Get a new set of tags from the user
  12960. (save-excursion
  12961. (setq table (append org-tag-persistent-alist
  12962. (or org-tag-alist (org-get-buffer-tags))
  12963. (and
  12964. org-complete-tags-always-offer-all-agenda-tags
  12965. (org-global-tags-completion-table
  12966. (org-agenda-files))))
  12967. org-last-tags-completion-table table
  12968. current-tags (org-split-string current ":")
  12969. inherited-tags (nreverse
  12970. (nthcdr (length current-tags)
  12971. (nreverse (org-get-tags-at))))
  12972. tags
  12973. (if (or (eq t org-use-fast-tag-selection)
  12974. (and org-use-fast-tag-selection
  12975. (delq nil (mapcar 'cdr table))))
  12976. (org-fast-tag-selection
  12977. current-tags inherited-tags table
  12978. (if org-fast-tag-selection-include-todo
  12979. org-todo-key-alist))
  12980. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12981. (org-trim
  12982. (org-icompleting-read "Tags: "
  12983. 'org-tags-completion-function
  12984. nil nil current 'org-tags-history))))))
  12985. (while (string-match "[-+&]+" tags)
  12986. ;; No boolean logic, just a list
  12987. (setq tags (replace-match ":" t t tags))))
  12988. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12989. (if org-tags-sort-function
  12990. (setq tags (mapconcat 'identity
  12991. (sort (org-split-string
  12992. tags (org-re "[^[:alnum:]_@#%]+"))
  12993. org-tags-sort-function) ":")))
  12994. (if (string-match "\\`[\t ]*\\'" tags)
  12995. (setq tags "")
  12996. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12997. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12998. ;; Insert new tags at the correct column
  12999. (beginning-of-line 1)
  13000. (setq level (or (and (looking-at org-outline-regexp)
  13001. (- (match-end 0) (point) 1))
  13002. 1))
  13003. (cond
  13004. ((and (equal current "") (equal tags "")))
  13005. ((re-search-forward
  13006. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13007. (point-at-eol) t)
  13008. (if (equal tags "")
  13009. (setq rpl "")
  13010. (goto-char (match-beginning 0))
  13011. (setq c0 (current-column)
  13012. ;; compute offset for the case of org-indent-mode active
  13013. di (if org-indent-mode
  13014. (* (1- org-indent-indentation-per-level) (1- level))
  13015. 0)
  13016. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13017. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13018. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13019. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13020. (replace-match rpl t t)
  13021. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13022. tags)
  13023. (t (error "Tags alignment failed")))
  13024. (org-move-to-column col)
  13025. (unless just-align
  13026. (run-hooks 'org-after-tags-change-hook))))))
  13027. (defun org-change-tag-in-region (beg end tag off)
  13028. "Add or remove TAG for each entry in the region.
  13029. This works in the agenda, and also in an org-mode buffer."
  13030. (interactive
  13031. (list (region-beginning) (region-end)
  13032. (let ((org-last-tags-completion-table
  13033. (if (derived-mode-p 'org-mode)
  13034. (org-get-buffer-tags)
  13035. (org-global-tags-completion-table))))
  13036. (org-icompleting-read
  13037. "Tag: " 'org-tags-completion-function nil nil nil
  13038. 'org-tags-history))
  13039. (progn
  13040. (message "[s]et or [r]emove? ")
  13041. (equal (read-char-exclusive) ?r))))
  13042. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13043. (let ((agendap (equal major-mode 'org-agenda-mode))
  13044. l1 l2 m buf pos newhead (cnt 0))
  13045. (goto-char end)
  13046. (setq l2 (1- (org-current-line)))
  13047. (goto-char beg)
  13048. (setq l1 (org-current-line))
  13049. (loop for l from l1 to l2 do
  13050. (org-goto-line l)
  13051. (setq m (get-text-property (point) 'org-hd-marker))
  13052. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13053. (and agendap m))
  13054. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13055. pos (if agendap m (point)))
  13056. (with-current-buffer buf
  13057. (save-excursion
  13058. (save-restriction
  13059. (goto-char pos)
  13060. (setq cnt (1+ cnt))
  13061. (org-toggle-tag tag (if off 'off 'on))
  13062. (setq newhead (org-get-heading)))))
  13063. (and agendap (org-agenda-change-all-lines newhead m))))
  13064. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13065. (defun org-tags-completion-function (string predicate &optional flag)
  13066. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13067. (confirm (lambda (x) (stringp (car x)))))
  13068. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13069. (setq s1 (match-string 1 string)
  13070. s2 (match-string 2 string))
  13071. (setq s1 "" s2 string))
  13072. (cond
  13073. ((eq flag nil)
  13074. ;; try completion
  13075. (setq rtn (try-completion s2 ctable confirm))
  13076. (if (stringp rtn)
  13077. (setq rtn
  13078. (concat s1 s2 (substring rtn (length s2))
  13079. (if (and org-add-colon-after-tag-completion
  13080. (assoc rtn ctable))
  13081. ":" ""))))
  13082. rtn)
  13083. ((eq flag t)
  13084. ;; all-completions
  13085. (all-completions s2 ctable confirm)
  13086. )
  13087. ((eq flag 'lambda)
  13088. ;; exact match?
  13089. (assoc s2 ctable)))
  13090. ))
  13091. (defun org-fast-tag-insert (kwd tags face &optional end)
  13092. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  13093. (insert (format "%-12s" (concat kwd ":"))
  13094. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13095. (or end "")))
  13096. (defun org-fast-tag-show-exit (flag)
  13097. (save-excursion
  13098. (org-goto-line 3)
  13099. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13100. (replace-match ""))
  13101. (when flag
  13102. (end-of-line 1)
  13103. (org-move-to-column (- (window-width) 19) t)
  13104. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13105. (defun org-set-current-tags-overlay (current prefix)
  13106. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13107. (if (featurep 'xemacs)
  13108. (org-overlay-display org-tags-overlay (concat prefix s)
  13109. 'secondary-selection)
  13110. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13111. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13112. (defvar org-last-tag-selection-key nil)
  13113. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13114. "Fast tag selection with single keys.
  13115. CURRENT is the current list of tags in the headline, INHERITED is the
  13116. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13117. possibly with grouping information. TODO-TABLE is a similar table with
  13118. TODO keywords, should these have keys assigned to them.
  13119. If the keys are nil, a-z are automatically assigned.
  13120. Returns the new tags string, or nil to not change the current settings."
  13121. (let* ((fulltable (append table todo-table))
  13122. (maxlen (apply 'max (mapcar
  13123. (lambda (x)
  13124. (if (stringp (car x)) (string-width (car x)) 0))
  13125. fulltable)))
  13126. (buf (current-buffer))
  13127. (expert (eq org-fast-tag-selection-single-key 'expert))
  13128. (buffer-tags nil)
  13129. (fwidth (+ maxlen 3 1 3))
  13130. (ncol (/ (- (window-width) 4) fwidth))
  13131. (i-face 'org-done)
  13132. (c-face 'org-todo)
  13133. tg cnt e c char c1 c2 ntable tbl rtn
  13134. ov-start ov-end ov-prefix
  13135. (exit-after-next org-fast-tag-selection-single-key)
  13136. (done-keywords org-done-keywords)
  13137. groups ingroup)
  13138. (save-excursion
  13139. (beginning-of-line 1)
  13140. (if (looking-at
  13141. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13142. (setq ov-start (match-beginning 1)
  13143. ov-end (match-end 1)
  13144. ov-prefix "")
  13145. (setq ov-start (1- (point-at-eol))
  13146. ov-end (1+ ov-start))
  13147. (skip-chars-forward "^\n\r")
  13148. (setq ov-prefix
  13149. (concat
  13150. (buffer-substring (1- (point)) (point))
  13151. (if (> (current-column) org-tags-column)
  13152. " "
  13153. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13154. (move-overlay org-tags-overlay ov-start ov-end)
  13155. (save-window-excursion
  13156. (if expert
  13157. (set-buffer (get-buffer-create " *Org tags*"))
  13158. (delete-other-windows)
  13159. (split-window-vertically)
  13160. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13161. (erase-buffer)
  13162. (org-set-local 'org-done-keywords done-keywords)
  13163. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13164. (org-fast-tag-insert "Current" current c-face "\n\n")
  13165. (org-fast-tag-show-exit exit-after-next)
  13166. (org-set-current-tags-overlay current ov-prefix)
  13167. (setq tbl fulltable char ?a cnt 0)
  13168. (while (setq e (pop tbl))
  13169. (cond
  13170. ((equal (car e) :startgroup)
  13171. (push '() groups) (setq ingroup t)
  13172. (when (not (= cnt 0))
  13173. (setq cnt 0)
  13174. (insert "\n"))
  13175. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13176. ((equal (car e) :endgroup)
  13177. (setq ingroup nil cnt 0)
  13178. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13179. ((equal e '(:newline))
  13180. (when (not (= cnt 0))
  13181. (setq cnt 0)
  13182. (insert "\n")
  13183. (setq e (car tbl))
  13184. (while (equal (car tbl) '(:newline))
  13185. (insert "\n")
  13186. (setq tbl (cdr tbl)))))
  13187. (t
  13188. (setq tg (copy-sequence (car e)) c2 nil)
  13189. (if (cdr e)
  13190. (setq c (cdr e))
  13191. ;; automatically assign a character.
  13192. (setq c1 (string-to-char
  13193. (downcase (substring
  13194. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13195. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13196. (while (or (rassoc char ntable) (rassoc char table))
  13197. (setq char (1+ char)))
  13198. (setq c2 c1))
  13199. (setq c (or c2 char)))
  13200. (if ingroup (push tg (car groups)))
  13201. (setq tg (org-add-props tg nil 'face
  13202. (cond
  13203. ((not (assoc tg table))
  13204. (org-get-todo-face tg))
  13205. ((member tg current) c-face)
  13206. ((member tg inherited) i-face))))
  13207. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13208. (insert "[" c "] " tg (make-string
  13209. (- fwidth 4 (length tg)) ?\ ))
  13210. (push (cons tg c) ntable)
  13211. (when (= (setq cnt (1+ cnt)) ncol)
  13212. (insert "\n")
  13213. (if ingroup (insert " "))
  13214. (setq cnt 0)))))
  13215. (setq ntable (nreverse ntable))
  13216. (insert "\n")
  13217. (goto-char (point-min))
  13218. (if (not expert) (org-fit-window-to-buffer))
  13219. (setq rtn
  13220. (catch 'exit
  13221. (while t
  13222. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13223. (if (not groups) "no " "")
  13224. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13225. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13226. (setq org-last-tag-selection-key c)
  13227. (cond
  13228. ((= c ?\r) (throw 'exit t))
  13229. ((= c ?!)
  13230. (setq groups (not groups))
  13231. (goto-char (point-min))
  13232. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13233. ((= c ?\C-c)
  13234. (if (not expert)
  13235. (org-fast-tag-show-exit
  13236. (setq exit-after-next (not exit-after-next)))
  13237. (setq expert nil)
  13238. (delete-other-windows)
  13239. (set-window-buffer (split-window-vertically) " *Org tags*")
  13240. (org-switch-to-buffer-other-window " *Org tags*")
  13241. (org-fit-window-to-buffer)))
  13242. ((or (= c ?\C-g)
  13243. (and (= c ?q) (not (rassoc c ntable))))
  13244. (org-detach-overlay org-tags-overlay)
  13245. (setq quit-flag t))
  13246. ((= c ?\ )
  13247. (setq current nil)
  13248. (if exit-after-next (setq exit-after-next 'now)))
  13249. ((= c ?\t)
  13250. (condition-case nil
  13251. (setq tg (org-icompleting-read
  13252. "Tag: "
  13253. (or buffer-tags
  13254. (with-current-buffer buf
  13255. (org-get-buffer-tags)))))
  13256. (quit (setq tg "")))
  13257. (when (string-match "\\S-" tg)
  13258. (add-to-list 'buffer-tags (list tg))
  13259. (if (member tg current)
  13260. (setq current (delete tg current))
  13261. (push tg current)))
  13262. (if exit-after-next (setq exit-after-next 'now)))
  13263. ((setq e (rassoc c todo-table) tg (car e))
  13264. (with-current-buffer buf
  13265. (save-excursion (org-todo tg)))
  13266. (if exit-after-next (setq exit-after-next 'now)))
  13267. ((setq e (rassoc c ntable) tg (car e))
  13268. (if (member tg current)
  13269. (setq current (delete tg current))
  13270. (loop for g in groups do
  13271. (if (member tg g)
  13272. (mapc (lambda (x)
  13273. (setq current (delete x current)))
  13274. g)))
  13275. (push tg current))
  13276. (if exit-after-next (setq exit-after-next 'now))))
  13277. ;; Create a sorted list
  13278. (setq current
  13279. (sort current
  13280. (lambda (a b)
  13281. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13282. (if (eq exit-after-next 'now) (throw 'exit t))
  13283. (goto-char (point-min))
  13284. (beginning-of-line 2)
  13285. (delete-region (point) (point-at-eol))
  13286. (org-fast-tag-insert "Current" current c-face)
  13287. (org-set-current-tags-overlay current ov-prefix)
  13288. (while (re-search-forward
  13289. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13290. (setq tg (match-string 1))
  13291. (add-text-properties
  13292. (match-beginning 1) (match-end 1)
  13293. (list 'face
  13294. (cond
  13295. ((member tg current) c-face)
  13296. ((member tg inherited) i-face)
  13297. (t (get-text-property (match-beginning 1) 'face))))))
  13298. (goto-char (point-min)))))
  13299. (org-detach-overlay org-tags-overlay)
  13300. (if rtn
  13301. (mapconcat 'identity current ":")
  13302. nil))))
  13303. (defun org-get-tags-string ()
  13304. "Get the TAGS string in the current headline."
  13305. (unless (org-at-heading-p t)
  13306. (error "Not on a heading"))
  13307. (save-excursion
  13308. (beginning-of-line 1)
  13309. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13310. (org-match-string-no-properties 1)
  13311. "")))
  13312. (defun org-get-tags ()
  13313. "Get the list of tags specified in the current headline."
  13314. (org-split-string (org-get-tags-string) ":"))
  13315. (defun org-get-buffer-tags ()
  13316. "Get a table of all tags used in the buffer, for completion."
  13317. (let (tags)
  13318. (save-excursion
  13319. (goto-char (point-min))
  13320. (while (re-search-forward
  13321. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13322. (when (equal (char-after (point-at-bol 0)) ?*)
  13323. (mapc (lambda (x) (add-to-list 'tags x))
  13324. (org-split-string (org-match-string-no-properties 1) ":")))))
  13325. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13326. (mapcar 'list tags)))
  13327. ;;;; The mapping API
  13328. (defun org-map-entries (func &optional match scope &rest skip)
  13329. "Call FUNC at each headline selected by MATCH in SCOPE.
  13330. FUNC is a function or a lisp form. The function will be called without
  13331. arguments, with the cursor positioned at the beginning of the headline.
  13332. The return values of all calls to the function will be collected and
  13333. returned as a list.
  13334. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13335. does not need to preserve point. After evaluation, the cursor will be
  13336. moved to the end of the line (presumably of the headline of the
  13337. processed entry) and search continues from there. Under some
  13338. circumstances, this may not produce the wanted results. For example,
  13339. if you have removed (e.g. archived) the current (sub)tree it could
  13340. mean that the next entry will be skipped entirely. In such cases, you
  13341. can specify the position from where search should continue by making
  13342. FUNC set the variable `org-map-continue-from' to the desired buffer
  13343. position.
  13344. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13345. Only headlines that are matched by this query will be considered during
  13346. the iteration. When MATCH is nil or t, all headlines will be
  13347. visited by the iteration.
  13348. SCOPE determines the scope of this command. It can be any of:
  13349. nil The current buffer, respecting the restriction if any
  13350. tree The subtree started with the entry at point
  13351. region The entries within the active region, if any
  13352. region-start-level
  13353. The entries within the active region, but only those at
  13354. the same level than the first one.
  13355. file The current buffer, without restriction
  13356. file-with-archives
  13357. The current buffer, and any archives associated with it
  13358. agenda All agenda files
  13359. agenda-with-archives
  13360. All agenda files with any archive files associated with them
  13361. \(file1 file2 ...)
  13362. If this is a list, all files in the list will be scanned
  13363. The remaining args are treated as settings for the skipping facilities of
  13364. the scanner. The following items can be given here:
  13365. archive skip trees with the archive tag
  13366. comment skip trees with the COMMENT keyword
  13367. function or Emacs Lisp form:
  13368. will be used as value for `org-agenda-skip-function', so
  13369. whenever the function returns a position, FUNC will not be
  13370. called for that entry and search will continue from the
  13371. position returned
  13372. If your function needs to retrieve the tags including inherited tags
  13373. at the *current* entry, you can use the value of the variable
  13374. `org-scanner-tags' which will be much faster than getting the value
  13375. with `org-get-tags-at'. If your function gets properties with
  13376. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13377. to t around the call to `org-entry-properties' to get the same speedup.
  13378. Note that if your function moves around to retrieve tags and properties at
  13379. a *different* entry, you cannot use these techniques."
  13380. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13381. (not (org-region-active-p)))
  13382. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13383. (org-agenda-skip-archived-trees (memq 'archive skip))
  13384. (org-agenda-skip-comment-trees (memq 'comment skip))
  13385. (org-agenda-skip-function
  13386. (car (org-delete-all '(comment archive) skip)))
  13387. (org-tags-match-list-sublevels t)
  13388. (start-level (eq scope 'region-start-level))
  13389. matcher file res
  13390. org-todo-keywords-for-agenda
  13391. org-done-keywords-for-agenda
  13392. org-todo-keyword-alist-for-agenda
  13393. org-drawers-for-agenda
  13394. org-tag-alist-for-agenda
  13395. todo-only)
  13396. (cond
  13397. ((eq match t) (setq matcher t))
  13398. ((eq match nil) (setq matcher t))
  13399. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13400. (save-window-excursion
  13401. (save-restriction
  13402. (cond ((eq scope 'tree)
  13403. (org-back-to-heading t)
  13404. (org-narrow-to-subtree)
  13405. (setq scope nil))
  13406. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13407. (org-region-active-p))
  13408. ;; If needed, set start-level to a string like "2"
  13409. (when start-level
  13410. (save-excursion
  13411. (goto-char (region-beginning))
  13412. (unless (org-at-heading-p) (outline-next-heading))
  13413. (setq start-level (org-current-level))))
  13414. (narrow-to-region (region-beginning)
  13415. (save-excursion
  13416. (goto-char (region-end))
  13417. (unless (and (bolp) (org-at-heading-p))
  13418. (outline-next-heading))
  13419. (point)))
  13420. (setq scope nil)))
  13421. (if (not scope)
  13422. (progn
  13423. (org-agenda-prepare-buffers
  13424. (list (buffer-file-name (current-buffer))))
  13425. (setq res (org-scan-tags func matcher todo-only start-level)))
  13426. ;; Get the right scope
  13427. (cond
  13428. ((and scope (listp scope) (symbolp (car scope)))
  13429. (setq scope (eval scope)))
  13430. ((eq scope 'agenda)
  13431. (setq scope (org-agenda-files t)))
  13432. ((eq scope 'agenda-with-archives)
  13433. (setq scope (org-agenda-files t))
  13434. (setq scope (org-add-archive-files scope)))
  13435. ((eq scope 'file)
  13436. (setq scope (list (buffer-file-name))))
  13437. ((eq scope 'file-with-archives)
  13438. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13439. (org-agenda-prepare-buffers scope)
  13440. (while (setq file (pop scope))
  13441. (with-current-buffer (org-find-base-buffer-visiting file)
  13442. (save-excursion
  13443. (save-restriction
  13444. (widen)
  13445. (goto-char (point-min))
  13446. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13447. res)))
  13448. ;;;; Properties
  13449. ;;; Setting and retrieving properties
  13450. (defconst org-special-properties
  13451. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13452. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13453. "The special properties valid in Org-mode.
  13454. These are properties that are not defined in the property drawer,
  13455. but in some other way.")
  13456. (defconst org-default-properties
  13457. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13458. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13459. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13460. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13461. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13462. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13463. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13464. "Some properties that are used by Org-mode for various purposes.
  13465. Being in this list makes sure that they are offered for completion.")
  13466. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13467. "Regular expression matching the first line of a property drawer.")
  13468. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13469. "Regular expression matching the last line of a property drawer.")
  13470. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13471. "Regular expression matching the first line of a property drawer.")
  13472. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13473. "Regular expression matching the first line of a property drawer.")
  13474. (defconst org-property-drawer-re
  13475. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13476. org-property-end-re "\\)\n?")
  13477. "Matches an entire property drawer.")
  13478. (defconst org-clock-drawer-re
  13479. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13480. org-property-end-re "\\)\n?")
  13481. "Matches an entire clock drawer.")
  13482. (defsubst org-re-property (property)
  13483. "Return a regexp matching a PROPERTY line.
  13484. Match group 1 will be set to the value."
  13485. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13486. (defsubst org-re-property-keyword (property)
  13487. "Return a regexp matching a PROPERTY line, possibly with no
  13488. value for the property."
  13489. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13490. (defun org-property-action ()
  13491. "Do an action on properties."
  13492. (interactive)
  13493. (let (c)
  13494. (org-at-property-p)
  13495. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13496. (setq c (read-char-exclusive))
  13497. (cond
  13498. ((equal c ?s)
  13499. (call-interactively 'org-set-property))
  13500. ((equal c ?d)
  13501. (call-interactively 'org-delete-property))
  13502. ((equal c ?D)
  13503. (call-interactively 'org-delete-property-globally))
  13504. ((equal c ?c)
  13505. (call-interactively 'org-compute-property-at-point))
  13506. (t (error "No such property action %c" c)))))
  13507. (defun org-inc-effort ()
  13508. "Increment the value of the effort property in the current entry."
  13509. (interactive)
  13510. (org-set-effort nil t))
  13511. (defvar org-clock-effort) ;; Defined in org-clock.el
  13512. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13513. (defun org-set-effort (&optional value increment)
  13514. "Set the effort property of the current entry.
  13515. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13516. 10th allowed value.
  13517. When INCREMENT is non-nil, set the property to the next allowed value."
  13518. (interactive "P")
  13519. (if (equal value 0) (setq value 10))
  13520. (let* ((completion-ignore-case t)
  13521. (prop org-effort-property)
  13522. (cur (org-entry-get nil prop))
  13523. (allowed (org-property-get-allowed-values nil prop 'table))
  13524. (existing (mapcar 'list (org-property-values prop)))
  13525. (heading (nth 4 (org-heading-components)))
  13526. rpl
  13527. (val (cond
  13528. ((stringp value) value)
  13529. ((and allowed (integerp value))
  13530. (or (car (nth (1- value) allowed))
  13531. (car (org-last allowed))))
  13532. ((and allowed increment)
  13533. (or (caadr (member (list cur) allowed))
  13534. (error "Allowed effort values are not set")))
  13535. (allowed
  13536. (message "Select 1-9,0, [RET%s]: %s"
  13537. (if cur (concat "=" cur) "")
  13538. (mapconcat 'car allowed " "))
  13539. (setq rpl (read-char-exclusive))
  13540. (if (equal rpl ?\r)
  13541. cur
  13542. (setq rpl (- rpl ?0))
  13543. (if (equal rpl 0) (setq rpl 10))
  13544. (if (and (> rpl 0) (<= rpl (length allowed)))
  13545. (car (nth (1- rpl) allowed))
  13546. (org-completing-read "Effort: " allowed nil))))
  13547. (t
  13548. (let (org-completion-use-ido org-completion-use-iswitchb)
  13549. (org-completing-read
  13550. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13551. (concat "[" cur "]") "")
  13552. ": ")
  13553. existing nil nil "" nil cur))))))
  13554. (unless (equal (org-entry-get nil prop) val)
  13555. (org-entry-put nil prop val))
  13556. (save-excursion
  13557. (org-back-to-heading t)
  13558. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13559. (when (string= heading org-clock-current-task)
  13560. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13561. (org-clock-update-mode-line))
  13562. (message "%s is now %s" prop val)))
  13563. (defun org-at-property-p ()
  13564. "Is cursor inside a property drawer?"
  13565. (save-excursion
  13566. (beginning-of-line 1)
  13567. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13568. (save-match-data ;; Used by calling procedures
  13569. (let ((p (point))
  13570. (range (unless (org-before-first-heading-p)
  13571. (org-get-property-block))))
  13572. (and range (<= (car range) p) (< p (cdr range))))))))
  13573. (defun org-get-property-block (&optional beg end force)
  13574. "Return the (beg . end) range of the body of the property drawer.
  13575. BEG and END are the beginning and end of the current subtree, or of
  13576. the part before the first headline. If they are not given, they will
  13577. be found. If the drawer does not exist and FORCE is non-nil, create
  13578. the drawer."
  13579. (catch 'exit
  13580. (save-excursion
  13581. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13582. (progn (org-back-to-heading t) (point))))
  13583. (end (or end (and (not (outline-next-heading)) (point-max))
  13584. (point))))
  13585. (goto-char beg)
  13586. (if (re-search-forward org-property-start-re end t)
  13587. (setq beg (1+ (match-end 0)))
  13588. (if force
  13589. (save-excursion
  13590. (org-insert-property-drawer)
  13591. (setq end (progn (outline-next-heading) (point))))
  13592. (throw 'exit nil))
  13593. (goto-char beg)
  13594. (if (re-search-forward org-property-start-re end t)
  13595. (setq beg (1+ (match-end 0)))))
  13596. (if (re-search-forward org-property-end-re end t)
  13597. (setq end (match-beginning 0))
  13598. (or force (throw 'exit nil))
  13599. (goto-char beg)
  13600. (setq end beg)
  13601. (org-indent-line)
  13602. (insert ":END:\n"))
  13603. (cons beg end)))))
  13604. (defun org-entry-properties (&optional pom which specific)
  13605. "Get all properties of the entry at point-or-marker POM.
  13606. This includes the TODO keyword, the tags, time strings for deadline,
  13607. scheduled, and clocking, and any additional properties defined in the
  13608. entry. The return value is an alist, keys may occur multiple times
  13609. if the property key was used several times.
  13610. POM may also be nil, in which case the current entry is used.
  13611. If WHICH is nil or `all', get all properties. If WHICH is
  13612. `special' or `standard', only get that subclass. If WHICH
  13613. is a string only get exactly this property. SPECIFIC can be a string, the
  13614. specific property we are interested in. Specifying it can speed
  13615. things up because then unnecessary parsing is avoided."
  13616. (setq which (or which 'all))
  13617. (org-with-point-at pom
  13618. (let ((clockstr (substring org-clock-string 0 -1))
  13619. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13620. (case-fold-search nil)
  13621. beg end range props sum-props key key1 value string clocksum clocksumt)
  13622. (save-excursion
  13623. (when (condition-case nil
  13624. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13625. (error nil))
  13626. (setq beg (point))
  13627. (setq sum-props (get-text-property (point) 'org-summaries))
  13628. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13629. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13630. (outline-next-heading)
  13631. (setq end (point))
  13632. (when (memq which '(all special))
  13633. ;; Get the special properties, like TODO and tags
  13634. (goto-char beg)
  13635. (when (and (or (not specific) (string= specific "TODO"))
  13636. (looking-at org-todo-line-regexp) (match-end 2))
  13637. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13638. (when (and (or (not specific) (string= specific "PRIORITY"))
  13639. (looking-at org-priority-regexp))
  13640. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13641. (when (or (not specific) (string= specific "FILE"))
  13642. (push (cons "FILE" buffer-file-name) props))
  13643. (when (and (or (not specific) (string= specific "TAGS"))
  13644. (setq value (org-get-tags-string))
  13645. (string-match "\\S-" value))
  13646. (push (cons "TAGS" value) props))
  13647. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13648. (setq value (org-get-tags-at)))
  13649. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13650. ":"))
  13651. props))
  13652. (when (or (not specific) (string= specific "BLOCKED"))
  13653. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13654. (when (or (not specific)
  13655. (member specific
  13656. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13657. "TIMESTAMP" "TIMESTAMP_IA")))
  13658. (catch 'match
  13659. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13660. (setq key (if (match-end 1)
  13661. (substring (org-match-string-no-properties 1)
  13662. 0 -1))
  13663. string (if (equal key clockstr)
  13664. (org-trim
  13665. (buffer-substring-no-properties
  13666. (match-beginning 3) (goto-char
  13667. (point-at-eol))))
  13668. (substring (org-match-string-no-properties 3)
  13669. 1 -1)))
  13670. ;; Get the correct property name from the key. This is
  13671. ;; necessary if the user has configured time keywords.
  13672. (setq key1 (concat key ":"))
  13673. (cond
  13674. ((not key)
  13675. (setq key
  13676. (if (= (char-after (match-beginning 3)) ?\[)
  13677. "TIMESTAMP_IA" "TIMESTAMP")))
  13678. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13679. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13680. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13681. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13682. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13683. (progn
  13684. (push (cons key string) props)
  13685. ;; no need to search further if match is found
  13686. (throw 'match t))
  13687. (when (or (equal key "CLOCK") (not (assoc key props)))
  13688. (push (cons key string) props)))))))
  13689. (when (memq which '(all standard))
  13690. ;; Get the standard properties, like :PROP: ...
  13691. (setq range (org-get-property-block beg end))
  13692. (when range
  13693. (goto-char (car range))
  13694. (while (re-search-forward
  13695. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13696. (cdr range) t)
  13697. (setq key (org-match-string-no-properties 1)
  13698. value (org-trim (or (org-match-string-no-properties 2) "")))
  13699. (unless (member key excluded)
  13700. (push (cons key (or value "")) props)))))
  13701. (if clocksum
  13702. (push (cons "CLOCKSUM"
  13703. (org-columns-number-to-string (/ (float clocksum) 60.)
  13704. 'add_times))
  13705. props))
  13706. (if clocksumt
  13707. (push (cons "CLOCKSUM_T"
  13708. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13709. 'add_times))
  13710. props))
  13711. (unless (assoc "CATEGORY" props)
  13712. (push (cons "CATEGORY" (org-get-category)) props))
  13713. (append sum-props (nreverse props)))))))
  13714. (defun org-entry-get (pom property &optional inherit literal-nil)
  13715. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13716. If INHERIT is non-nil and the entry does not have the property,
  13717. then also check higher levels of the hierarchy.
  13718. If INHERIT is the symbol `selective', use inheritance only if the setting
  13719. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13720. If the property is present but empty, the return value is the empty string.
  13721. If the property is not present at all, nil is returned.
  13722. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13723. do not interpret it as the list atom nil. This is used for inheritance
  13724. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13725. (org-with-point-at pom
  13726. (if (and inherit (if (eq inherit 'selective)
  13727. (org-property-inherit-p property)
  13728. t))
  13729. (org-entry-get-with-inheritance property literal-nil)
  13730. (if (member property org-special-properties)
  13731. ;; We need a special property. Use `org-entry-properties' to
  13732. ;; retrieve it, but specify the wanted property
  13733. (cdr (assoc property (org-entry-properties nil 'special property)))
  13734. (let ((range (org-get-property-block)))
  13735. (when (and range (not (eq (car range) (cdr range))))
  13736. (let* ((props (list (or (assoc property org-file-properties)
  13737. (assoc property org-global-properties)
  13738. (assoc property org-global-properties-fixed))))
  13739. (ap (lambda (key)
  13740. (when (re-search-forward
  13741. (org-re-property key) (cdr range) t)
  13742. (setq props
  13743. (org-update-property-plist
  13744. key
  13745. (if (match-end 1)
  13746. (org-match-string-no-properties 1) "")
  13747. props)))))
  13748. val)
  13749. (goto-char (car range))
  13750. (funcall ap property)
  13751. (goto-char (car range))
  13752. (while (funcall ap (concat property "+")))
  13753. (setq val (cdr (assoc property props)))
  13754. (when val (if literal-nil val (org-not-nil val))))))))))
  13755. (defun org-property-or-variable-value (var &optional inherit)
  13756. "Check if there is a property fixing the value of VAR.
  13757. If yes, return this value. If not, return the current value of the variable."
  13758. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13759. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13760. (read prop)
  13761. (symbol-value var))))
  13762. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13763. "Delete the property PROPERTY from entry at point-or-marker POM.
  13764. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13765. an empty drawer to delete."
  13766. (org-with-point-at pom
  13767. (if (member property org-special-properties)
  13768. nil ; cannot delete these properties.
  13769. (let ((range (org-get-property-block)))
  13770. (if (and range
  13771. (goto-char (car range))
  13772. (re-search-forward
  13773. (org-re-property property)
  13774. (cdr range) t))
  13775. (progn
  13776. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13777. (and delete-empty-drawer
  13778. (org-remove-empty-drawer-at
  13779. delete-empty-drawer (car range)))
  13780. t)
  13781. nil)))))
  13782. ;; Multi-values properties are properties that contain multiple values
  13783. ;; These values are assumed to be single words, separated by whitespace.
  13784. (defun org-entry-add-to-multivalued-property (pom property value)
  13785. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13786. (let* ((old (org-entry-get pom property))
  13787. (values (and old (org-split-string old "[ \t]"))))
  13788. (setq value (org-entry-protect-space value))
  13789. (unless (member value values)
  13790. (setq values (cons value values))
  13791. (org-entry-put pom property
  13792. (mapconcat 'identity values " ")))))
  13793. (defun org-entry-remove-from-multivalued-property (pom property value)
  13794. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13795. (let* ((old (org-entry-get pom property))
  13796. (values (and old (org-split-string old "[ \t]"))))
  13797. (setq value (org-entry-protect-space value))
  13798. (when (member value values)
  13799. (setq values (delete value values))
  13800. (org-entry-put pom property
  13801. (mapconcat 'identity values " ")))))
  13802. (defun org-entry-member-in-multivalued-property (pom property value)
  13803. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13804. (let* ((old (org-entry-get pom property))
  13805. (values (and old (org-split-string old "[ \t]"))))
  13806. (setq value (org-entry-protect-space value))
  13807. (member value values)))
  13808. (defun org-entry-get-multivalued-property (pom property)
  13809. "Return a list of values in a multivalued property."
  13810. (let* ((value (org-entry-get pom property))
  13811. (values (and value (org-split-string value "[ \t]"))))
  13812. (mapcar 'org-entry-restore-space values)))
  13813. (defun org-entry-put-multivalued-property (pom property &rest values)
  13814. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13815. VALUES should be a list of strings. Spaces will be protected."
  13816. (org-entry-put pom property
  13817. (mapconcat 'org-entry-protect-space values " "))
  13818. (let* ((value (org-entry-get pom property))
  13819. (values (and value (org-split-string value "[ \t]"))))
  13820. (mapcar 'org-entry-restore-space values)))
  13821. (defun org-entry-protect-space (s)
  13822. "Protect spaces and newline in string S."
  13823. (while (string-match " " s)
  13824. (setq s (replace-match "%20" t t s)))
  13825. (while (string-match "\n" s)
  13826. (setq s (replace-match "%0A" t t s)))
  13827. s)
  13828. (defun org-entry-restore-space (s)
  13829. "Restore spaces and newline in string S."
  13830. (while (string-match "%20" s)
  13831. (setq s (replace-match " " t t s)))
  13832. (while (string-match "%0A" s)
  13833. (setq s (replace-match "\n" t t s)))
  13834. s)
  13835. (defvar org-entry-property-inherited-from (make-marker)
  13836. "Marker pointing to the entry from where a property was inherited.
  13837. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13838. location of the entry where the inheritance search matched. If there was
  13839. no match, the marker will point nowhere.
  13840. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13841. is set.")
  13842. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13843. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13844. The search will stop at the first ancestor which has the property defined.
  13845. If the value found is \"nil\", return nil to show that the property
  13846. should be considered as undefined (this is the meaning of nil here).
  13847. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13848. (move-marker org-entry-property-inherited-from nil)
  13849. (let (tmp)
  13850. (save-excursion
  13851. (save-restriction
  13852. (widen)
  13853. (catch 'ex
  13854. (while t
  13855. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13856. (or (ignore-errors (org-back-to-heading t))
  13857. (goto-char (point-min)))
  13858. (move-marker org-entry-property-inherited-from (point))
  13859. (throw 'ex tmp))
  13860. (or (ignore-errors (org-up-heading-safe))
  13861. (throw 'ex nil))))))
  13862. (setq tmp (or tmp
  13863. (cdr (assoc property org-file-properties))
  13864. (cdr (assoc property org-global-properties))
  13865. (cdr (assoc property org-global-properties-fixed))))
  13866. (if literal-nil tmp (org-not-nil tmp))))
  13867. (defvar org-property-changed-functions nil
  13868. "Hook called when the value of a property has changed.
  13869. Each hook function should accept two arguments, the name of the property
  13870. and the new value.")
  13871. (defun org-entry-put (pom property value)
  13872. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13873. (org-with-point-at pom
  13874. (org-back-to-heading t)
  13875. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13876. range)
  13877. (cond
  13878. ((equal property "TODO")
  13879. (when (and (stringp value) (string-match "\\S-" value)
  13880. (not (member value org-todo-keywords-1)))
  13881. (error "\"%s\" is not a valid TODO state" value))
  13882. (if (or (not value)
  13883. (not (string-match "\\S-" value)))
  13884. (setq value 'none))
  13885. (org-todo value)
  13886. (org-set-tags nil 'align))
  13887. ((equal property "PRIORITY")
  13888. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13889. (string-to-char value) ?\ ))
  13890. (org-set-tags nil 'align))
  13891. ((equal property "CLOCKSUM")
  13892. (if (not (re-search-forward
  13893. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13894. (error "Cannot find a clock log")
  13895. (goto-char (- (match-end 1) 2))
  13896. (cond
  13897. ((eq value 'earlier) (org-timestamp-down))
  13898. ((eq value 'later) (org-timestamp-up)))
  13899. (org-clock-sum-current-item)))
  13900. ((equal property "SCHEDULED")
  13901. (if (re-search-forward org-scheduled-time-regexp end t)
  13902. (cond
  13903. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13904. ((eq value 'later) (org-timestamp-change 1 'day))
  13905. (t (call-interactively 'org-schedule)))
  13906. (call-interactively 'org-schedule)))
  13907. ((equal property "DEADLINE")
  13908. (if (re-search-forward org-deadline-time-regexp end t)
  13909. (cond
  13910. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13911. ((eq value 'later) (org-timestamp-change 1 'day))
  13912. (t (call-interactively 'org-deadline)))
  13913. (call-interactively 'org-deadline)))
  13914. ((member property org-special-properties)
  13915. (error "The %s property can not yet be set with `org-entry-put'"
  13916. property))
  13917. (t ; a non-special property
  13918. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13919. (setq range (org-get-property-block beg end 'force))
  13920. (goto-char (car range))
  13921. (if (re-search-forward
  13922. (org-re-property-keyword property) (cdr range) t)
  13923. (progn
  13924. (delete-region (match-beginning 0) (match-end 0))
  13925. (goto-char (match-beginning 0)))
  13926. (goto-char (cdr range))
  13927. (insert "\n")
  13928. (backward-char 1)
  13929. (org-indent-line))
  13930. (insert ":" property ":")
  13931. (and value (insert " " value))
  13932. (org-indent-line)))))
  13933. (run-hook-with-args 'org-property-changed-functions property value)))
  13934. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13935. "Get all property keys in the current buffer.
  13936. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13937. like tags and TODO state.
  13938. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13939. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13940. and others.
  13941. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13942. formats in the current buffer."
  13943. (let (rtn range cfmt s p)
  13944. (save-excursion
  13945. (save-restriction
  13946. (widen)
  13947. (goto-char (point-min))
  13948. (while (re-search-forward org-property-start-re nil t)
  13949. (setq range (org-get-property-block))
  13950. (goto-char (car range))
  13951. (while (re-search-forward
  13952. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13953. (cdr range) t)
  13954. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13955. (outline-next-heading))))
  13956. (when include-specials
  13957. (setq rtn (append org-special-properties rtn)))
  13958. (when include-defaults
  13959. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13960. (add-to-list 'rtn org-effort-property))
  13961. (when include-columns
  13962. (save-excursion
  13963. (save-restriction
  13964. (widen)
  13965. (goto-char (point-min))
  13966. (while (re-search-forward
  13967. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13968. nil t)
  13969. (setq cfmt (match-string 2) s 0)
  13970. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13971. cfmt s)
  13972. (setq s (match-end 0)
  13973. p (match-string 1 cfmt))
  13974. (unless (or (equal p "ITEM")
  13975. (member p org-special-properties))
  13976. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13977. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13978. (defun org-property-values (key)
  13979. "Return a list of all values of property KEY in the current buffer."
  13980. (save-excursion
  13981. (save-restriction
  13982. (widen)
  13983. (goto-char (point-min))
  13984. (let ((re (org-re-property key))
  13985. values)
  13986. (while (re-search-forward re nil t)
  13987. (add-to-list 'values (org-trim (match-string 1))))
  13988. (delete "" values)))))
  13989. (defun org-insert-property-drawer ()
  13990. "Insert a property drawer into the current entry."
  13991. (org-back-to-heading t)
  13992. (looking-at org-outline-regexp)
  13993. (let ((indent (if org-adapt-indentation
  13994. (- (match-end 0) (match-beginning 0))
  13995. 0))
  13996. (beg (point))
  13997. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13998. end hiddenp)
  13999. (outline-next-heading)
  14000. (setq end (point))
  14001. (goto-char beg)
  14002. (while (re-search-forward re end t))
  14003. (setq hiddenp (outline-invisible-p))
  14004. (end-of-line 1)
  14005. (and (equal (char-after) ?\n) (forward-char 1))
  14006. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14007. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14008. ;; just skip this line
  14009. (beginning-of-line 2)
  14010. ;; Drawer start, find the end
  14011. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14012. (beginning-of-line 1)))
  14013. (org-skip-over-state-notes)
  14014. (skip-chars-backward " \t\n\r")
  14015. (if (eq (char-before) ?*) (forward-char 1))
  14016. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14017. (beginning-of-line 0)
  14018. (org-indent-to-column indent)
  14019. (beginning-of-line 2)
  14020. (org-indent-to-column indent)
  14021. (beginning-of-line 0)
  14022. (if hiddenp
  14023. (save-excursion
  14024. (org-back-to-heading t)
  14025. (hide-entry))
  14026. (org-flag-drawer t))))
  14027. (defun org-insert-drawer (&optional arg drawer)
  14028. "Insert a drawer at point.
  14029. Optional argument DRAWER, when non-nil, is a string representing
  14030. drawer's name. Otherwise, the user is prompted for a name.
  14031. If a region is active, insert the drawer around that region
  14032. instead.
  14033. Point is left between drawer's boundaries."
  14034. (interactive "P")
  14035. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14036. "LOGBOOK"))
  14037. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14038. ;; internally by Org. They are meant to be inserted
  14039. ;; automatically.
  14040. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14041. ;; Remove system drawers from list. Note: For some reason,
  14042. ;; `org-completing-read' ignores the predicate while
  14043. ;; `completing-read' handles it fine.
  14044. (drawer (if arg "PROPERTIES"
  14045. (or drawer
  14046. (completing-read
  14047. "Drawer: " org-drawers
  14048. (lambda (d) (not (member d system-drawers))))))))
  14049. (cond
  14050. ;; With C-u, fall back on `org-insert-property-drawer'
  14051. (arg (org-insert-property-drawer))
  14052. ;; With an active region, insert a drawer at point.
  14053. ((not (org-region-active-p))
  14054. (progn
  14055. (unless (bolp) (insert "\n"))
  14056. (insert (format ":%s:\n\n:END:\n" drawer))
  14057. (forward-line -2)))
  14058. ;; Otherwise, insert the drawer at point
  14059. (t
  14060. (let ((rbeg (region-beginning))
  14061. (rend (copy-marker (region-end))))
  14062. (unwind-protect
  14063. (progn
  14064. (goto-char rbeg)
  14065. (beginning-of-line)
  14066. (when (save-excursion
  14067. (re-search-forward org-outline-regexp-bol rend t))
  14068. (error "Drawers cannot contain headlines"))
  14069. ;; Position point at the beginning of the first
  14070. ;; non-blank line in region. Insert drawer's opening
  14071. ;; there, then indent it.
  14072. (org-skip-whitespace)
  14073. (beginning-of-line)
  14074. (insert ":" drawer ":\n")
  14075. (forward-line -1)
  14076. (indent-for-tab-command)
  14077. ;; Move point to the beginning of the first blank line
  14078. ;; after the last non-blank line in region. Insert
  14079. ;; drawer's closing, then indent it.
  14080. (goto-char rend)
  14081. (skip-chars-backward " \r\t\n")
  14082. (insert "\n:END:")
  14083. (deactivate-mark t)
  14084. (indent-for-tab-command)
  14085. (unless (eolp) (insert "\n")))
  14086. ;; Clear marker, whatever the outcome of insertion is.
  14087. (set-marker rend nil)))))))
  14088. (defvar org-property-set-functions-alist nil
  14089. "Property set function alist.
  14090. Each entry should have the following format:
  14091. (PROPERTY . READ-FUNCTION)
  14092. The read function will be called with the same argument as
  14093. `org-completing-read'.")
  14094. (defun org-set-property-function (property)
  14095. "Get the function that should be used to set PROPERTY.
  14096. This is computed according to `org-property-set-functions-alist'."
  14097. (or (cdr (assoc property org-property-set-functions-alist))
  14098. 'org-completing-read))
  14099. (defun org-read-property-value (property)
  14100. "Read PROPERTY value from user."
  14101. (let* ((completion-ignore-case t)
  14102. (allowed (org-property-get-allowed-values nil property 'table))
  14103. (cur (org-entry-get nil property))
  14104. (prompt (concat property " value"
  14105. (if (and cur (string-match "\\S-" cur))
  14106. (concat " [" cur "]") "") ": "))
  14107. (set-function (org-set-property-function property))
  14108. (val (if allowed
  14109. (funcall set-function prompt allowed nil
  14110. (not (get-text-property 0 'org-unrestricted
  14111. (caar allowed))))
  14112. (let (org-completion-use-ido org-completion-use-iswitchb)
  14113. (funcall set-function prompt
  14114. (mapcar 'list (org-property-values property))
  14115. nil nil "" nil cur)))))
  14116. (if (equal val "")
  14117. cur
  14118. val)))
  14119. (defvar org-last-set-property nil)
  14120. (defvar org-last-set-property-value nil)
  14121. (defun org-read-property-name ()
  14122. "Read a property name."
  14123. (let* ((completion-ignore-case t)
  14124. (keys (org-buffer-property-keys nil t t))
  14125. (default-prop (or (save-excursion
  14126. (save-match-data
  14127. (beginning-of-line)
  14128. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14129. (null (string= (match-string 1) "END"))
  14130. (match-string 1))))
  14131. org-last-set-property))
  14132. (property (org-icompleting-read
  14133. (concat "Property"
  14134. (if default-prop (concat " [" default-prop "]") "")
  14135. ": ")
  14136. (mapcar 'list keys)
  14137. nil nil nil nil
  14138. default-prop)))
  14139. (if (member property keys)
  14140. property
  14141. (or (cdr (assoc (downcase property)
  14142. (mapcar (lambda (x) (cons (downcase x) x))
  14143. keys)))
  14144. property))))
  14145. (defun org-set-property-and-value (use-last)
  14146. "Allow to set [PROPERTY]: [value] direction from prompt.
  14147. When use-default, don't even ask, just use the last
  14148. \"[PROPERTY]: [value]\" string from the history."
  14149. (interactive "P")
  14150. (let* ((completion-ignore-case t)
  14151. (pv (or (and use-last org-last-set-property-value)
  14152. (org-completing-read
  14153. "Enter a \"[Property]: [value]\" pair: "
  14154. nil nil nil nil nil
  14155. org-last-set-property-value)))
  14156. prop val)
  14157. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14158. (setq prop (match-string 1 pv)
  14159. val (match-string 2 pv))
  14160. (org-set-property prop val))))
  14161. (defun org-set-property (property value)
  14162. "In the current entry, set PROPERTY to VALUE.
  14163. When called interactively, this will prompt for a property name, offering
  14164. completion on existing and default properties. And then it will prompt
  14165. for a value, offering completion either on allowed values (via an inherited
  14166. xxx_ALL property) or on existing values in other instances of this property
  14167. in the current file."
  14168. (interactive (list nil nil))
  14169. (let* ((property (or property (org-read-property-name)))
  14170. (value (or value (org-read-property-value property)))
  14171. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14172. (setq org-last-set-property property)
  14173. (setq org-last-set-property-value (concat property ": " value))
  14174. ;; Possibly postprocess the inserted value:
  14175. (when fn (setq value (funcall fn value)))
  14176. (unless (equal (org-entry-get nil property) value)
  14177. (org-entry-put nil property value))))
  14178. (defun org-delete-property (property &optional delete-empty-drawer)
  14179. "In the current entry, delete PROPERTY.
  14180. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14181. an empty drawer to delete."
  14182. (interactive
  14183. (let* ((completion-ignore-case t)
  14184. (prop (org-icompleting-read "Property: "
  14185. (org-entry-properties nil 'standard))))
  14186. (list prop)))
  14187. (message "Property %s %s" property
  14188. (if (org-entry-delete nil property delete-empty-drawer)
  14189. "deleted"
  14190. "was not present in the entry")))
  14191. (defun org-delete-property-globally (property)
  14192. "Remove PROPERTY globally, from all entries."
  14193. (interactive
  14194. (let* ((completion-ignore-case t)
  14195. (prop (org-icompleting-read
  14196. "Globally remove property: "
  14197. (mapcar 'list (org-buffer-property-keys)))))
  14198. (list prop)))
  14199. (save-excursion
  14200. (save-restriction
  14201. (widen)
  14202. (goto-char (point-min))
  14203. (let ((cnt 0))
  14204. (while (re-search-forward
  14205. (org-re-property property)
  14206. nil t)
  14207. (setq cnt (1+ cnt))
  14208. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14209. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14210. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14211. (defun org-compute-property-at-point ()
  14212. "Compute the property at point.
  14213. This looks for an enclosing column format, extracts the operator and
  14214. then applies it to the property in the column format's scope."
  14215. (interactive)
  14216. (unless (org-at-property-p)
  14217. (error "Not at a property"))
  14218. (let ((prop (org-match-string-no-properties 2)))
  14219. (org-columns-get-format-and-top-level)
  14220. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14221. (error "No operator defined for property %s" prop))
  14222. (org-columns-compute prop)))
  14223. (defvar org-property-allowed-value-functions nil
  14224. "Hook for functions supplying allowed values for a specific property.
  14225. The functions must take a single argument, the name of the property, and
  14226. return a flat list of allowed values. If \":ETC\" is one of
  14227. the values, this means that these values are intended as defaults for
  14228. completion, but that other values should be allowed too.
  14229. The functions must return nil if they are not responsible for this
  14230. property.")
  14231. (defun org-property-get-allowed-values (pom property &optional table)
  14232. "Get allowed values for the property PROPERTY.
  14233. When TABLE is non-nil, return an alist that can directly be used for
  14234. completion."
  14235. (let (vals)
  14236. (cond
  14237. ((equal property "TODO")
  14238. (setq vals (org-with-point-at pom
  14239. (append org-todo-keywords-1 '("")))))
  14240. ((equal property "PRIORITY")
  14241. (let ((n org-lowest-priority))
  14242. (while (>= n org-highest-priority)
  14243. (push (char-to-string n) vals)
  14244. (setq n (1- n)))))
  14245. ((member property org-special-properties))
  14246. ((setq vals (run-hook-with-args-until-success
  14247. 'org-property-allowed-value-functions property)))
  14248. (t
  14249. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14250. (when (and vals (string-match "\\S-" vals))
  14251. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14252. (setq vals (mapcar (lambda (x)
  14253. (cond ((stringp x) x)
  14254. ((numberp x) (number-to-string x))
  14255. ((symbolp x) (symbol-name x))
  14256. (t "???")))
  14257. vals)))))
  14258. (when (member ":ETC" vals)
  14259. (setq vals (remove ":ETC" vals))
  14260. (org-add-props (car vals) '(org-unrestricted t)))
  14261. (if table (mapcar 'list vals) vals)))
  14262. (defun org-property-previous-allowed-value (&optional previous)
  14263. "Switch to the next allowed value for this property."
  14264. (interactive)
  14265. (org-property-next-allowed-value t))
  14266. (defun org-property-next-allowed-value (&optional previous)
  14267. "Switch to the next allowed value for this property."
  14268. (interactive)
  14269. (unless (org-at-property-p)
  14270. (error "Not at a property"))
  14271. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14272. (key (match-string 2))
  14273. (value (match-string 3))
  14274. (allowed (or (org-property-get-allowed-values (point) key)
  14275. (and (member value '("[ ]" "[-]" "[X]"))
  14276. '("[ ]" "[X]"))))
  14277. (heading (save-match-data (nth 4 (org-heading-components))))
  14278. nval)
  14279. (unless allowed
  14280. (error "Allowed values for this property have not been defined"))
  14281. (if previous (setq allowed (reverse allowed)))
  14282. (if (member value allowed)
  14283. (setq nval (car (cdr (member value allowed)))))
  14284. (setq nval (or nval (car allowed)))
  14285. (if (equal nval value)
  14286. (error "Only one allowed value for this property"))
  14287. (org-at-property-p)
  14288. (replace-match (concat " :" key ": " nval) t t)
  14289. (org-indent-line)
  14290. (beginning-of-line 1)
  14291. (skip-chars-forward " \t")
  14292. (when (equal prop org-effort-property)
  14293. (save-excursion
  14294. (org-back-to-heading t)
  14295. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14296. (when (string= org-clock-current-task heading)
  14297. (setq org-clock-effort nval)
  14298. (org-clock-update-mode-line)))
  14299. (run-hook-with-args 'org-property-changed-functions key nval)))
  14300. (defun org-find-olp (path &optional this-buffer)
  14301. "Return a marker pointing to the entry at outline path OLP.
  14302. If anything goes wrong, throw an error.
  14303. You can wrap this call to catch the error like this:
  14304. (condition-case msg
  14305. (org-mobile-locate-entry (match-string 4))
  14306. (error (nth 1 msg)))
  14307. The return value will then be either a string with the error message,
  14308. or a marker if everything is OK.
  14309. If THIS-BUFFER is set, the outline path does not contain a file,
  14310. only headings."
  14311. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14312. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14313. (level 1)
  14314. (lmin 1)
  14315. (lmax 1)
  14316. limit re end found pos heading cnt flevel)
  14317. (unless buffer (error "File not found :%s" file))
  14318. (with-current-buffer buffer
  14319. (save-excursion
  14320. (save-restriction
  14321. (widen)
  14322. (setq limit (point-max))
  14323. (goto-char (point-min))
  14324. (while (setq heading (pop path))
  14325. (setq re (format org-complex-heading-regexp-format
  14326. (regexp-quote heading)))
  14327. (setq cnt 0 pos (point))
  14328. (while (re-search-forward re end t)
  14329. (setq level (- (match-end 1) (match-beginning 1)))
  14330. (if (and (>= level lmin) (<= level lmax))
  14331. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14332. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14333. lmax heading))
  14334. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14335. lmax heading))
  14336. (goto-char found)
  14337. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14338. (setq end (save-excursion (org-end-of-subtree t t))))
  14339. (when (org-at-heading-p)
  14340. (point-marker)))))))
  14341. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14342. "Find node HEADING in BUFFER.
  14343. Return a marker to the heading if it was found, or nil if not.
  14344. If POS-ONLY is set, return just the position instead of a marker.
  14345. The heading text must match exact, but it may have a TODO keyword,
  14346. a priority cookie and tags in the standard locations."
  14347. (with-current-buffer (or buffer (current-buffer))
  14348. (save-excursion
  14349. (save-restriction
  14350. (widen)
  14351. (goto-char (point-min))
  14352. (let (case-fold-search)
  14353. (if (re-search-forward
  14354. (format org-complex-heading-regexp-format
  14355. (regexp-quote heading)) nil t)
  14356. (if pos-only
  14357. (match-beginning 0)
  14358. (move-marker (make-marker) (match-beginning 0)))))))))
  14359. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14360. "Find Org node headline HEADING in all .org files in directory DIR.
  14361. When the target headline is found, return a marker to this location."
  14362. (let ((files (directory-files (or dir default-directory)
  14363. nil "\\`[^.#].*\\.org\\'"))
  14364. file visiting m buffer)
  14365. (catch 'found
  14366. (while (setq file (pop files))
  14367. (message "trying %s" file)
  14368. (setq visiting (org-find-base-buffer-visiting file))
  14369. (setq buffer (or visiting (find-file-noselect file)))
  14370. (setq m (org-find-exact-headline-in-buffer
  14371. heading buffer))
  14372. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14373. (and m (throw 'found m))))))
  14374. (defun org-find-entry-with-id (ident)
  14375. "Locate the entry that contains the ID property with exact value IDENT.
  14376. IDENT can be a string, a symbol or a number, this function will search for
  14377. the string representation of it.
  14378. Return the position where this entry starts, or nil if there is no such entry."
  14379. (interactive "sID: ")
  14380. (let ((id (cond
  14381. ((stringp ident) ident)
  14382. ((symbol-name ident) (symbol-name ident))
  14383. ((numberp ident) (number-to-string ident))
  14384. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14385. (case-fold-search nil))
  14386. (save-excursion
  14387. (save-restriction
  14388. (widen)
  14389. (goto-char (point-min))
  14390. (when (re-search-forward
  14391. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14392. nil t)
  14393. (org-back-to-heading t)
  14394. (point))))))
  14395. ;;;; Timestamps
  14396. (defvar org-last-changed-timestamp nil)
  14397. (defvar org-last-inserted-timestamp nil
  14398. "The last time stamp inserted with `org-insert-time-stamp'.")
  14399. (defvar org-time-was-given) ; dynamically scoped parameter
  14400. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14401. (defvar org-ts-what) ; dynamically scoped parameter
  14402. (defun org-time-stamp (arg &optional inactive)
  14403. "Prompt for a date/time and insert a time stamp.
  14404. If the user specifies a time like HH:MM or if this command is
  14405. called with at least one prefix argument, the time stamp contains
  14406. the date and the time. Otherwise, only the date is be included.
  14407. All parts of a date not specified by the user is filled in from
  14408. the current date/time. So if you just press return without
  14409. typing anything, the time stamp will represent the current
  14410. date/time.
  14411. If there is already a timestamp at the cursor, it will be
  14412. modified.
  14413. With two universal prefix arguments, insert an active timestamp
  14414. with the current time without prompting the user."
  14415. (interactive "P")
  14416. (let* ((ts nil)
  14417. (default-time
  14418. ;; Default time is either today, or, when entering a range,
  14419. ;; the range start.
  14420. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14421. (save-excursion
  14422. (re-search-backward
  14423. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14424. (- (point) 20) t)))
  14425. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14426. (current-time)))
  14427. (default-input (and ts (org-get-compact-tod ts)))
  14428. (repeater (save-excursion
  14429. (save-match-data
  14430. (beginning-of-line)
  14431. (when (re-search-forward
  14432. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14433. (save-excursion (progn (end-of-line) (point))) t)
  14434. (match-string 0)))))
  14435. org-time-was-given org-end-time-was-given time)
  14436. (cond
  14437. ((and (org-at-timestamp-p t)
  14438. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14439. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14440. (insert "--")
  14441. (setq time (let ((this-command this-command))
  14442. (org-read-date arg 'totime nil nil
  14443. default-time default-input inactive)))
  14444. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14445. ((org-at-timestamp-p t)
  14446. (setq time (let ((this-command this-command))
  14447. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14448. (when (org-at-timestamp-p t) ; just to get the match data
  14449. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14450. (replace-match "")
  14451. (setq org-last-changed-timestamp
  14452. (org-insert-time-stamp
  14453. time (or org-time-was-given arg)
  14454. inactive nil nil (list org-end-time-was-given)))
  14455. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14456. (setq org-last-changed-timestamp
  14457. (concat (substring org-last-inserted-timestamp 0 -1)
  14458. " " repeater ">"))))
  14459. (message "Timestamp updated"))
  14460. ((equal arg '(16))
  14461. (org-insert-time-stamp (current-time) t))
  14462. (t
  14463. (setq time (let ((this-command this-command))
  14464. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14465. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14466. nil nil (list org-end-time-was-given))))))
  14467. ;; FIXME: can we use this for something else, like computing time differences?
  14468. (defun org-get-compact-tod (s)
  14469. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14470. (let* ((t1 (match-string 1 s))
  14471. (h1 (string-to-number (match-string 2 s)))
  14472. (m1 (string-to-number (match-string 3 s)))
  14473. (t2 (and (match-end 4) (match-string 5 s)))
  14474. (h2 (and t2 (string-to-number (match-string 6 s))))
  14475. (m2 (and t2 (string-to-number (match-string 7 s))))
  14476. dh dm)
  14477. (if (not t2)
  14478. t1
  14479. (setq dh (- h2 h1) dm (- m2 m1))
  14480. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14481. (concat t1 "+" (number-to-string dh)
  14482. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14483. (defun org-time-stamp-inactive (&optional arg)
  14484. "Insert an inactive time stamp.
  14485. An inactive time stamp is enclosed in square brackets instead of angle
  14486. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14487. does not link to the calendar and cannot be changed with the S-cursor keys.
  14488. So these are more for recording a certain time/date."
  14489. (interactive "P")
  14490. (org-time-stamp arg 'inactive))
  14491. (defvar org-date-ovl (make-overlay 1 1))
  14492. (overlay-put org-date-ovl 'face 'org-date-selected)
  14493. (org-detach-overlay org-date-ovl)
  14494. (defvar org-ans1) ; dynamically scoped parameter
  14495. (defvar org-ans2) ; dynamically scoped parameter
  14496. (defvar org-plain-time-of-day-regexp) ; defined below
  14497. (defvar org-overriding-default-time nil) ; dynamically scoped
  14498. (defvar org-read-date-overlay nil)
  14499. (defvar org-dcst nil) ; dynamically scoped
  14500. (defvar org-read-date-history nil)
  14501. (defvar org-read-date-final-answer nil)
  14502. (defvar org-read-date-analyze-futurep nil)
  14503. (defvar org-read-date-analyze-forced-year nil)
  14504. (defvar org-read-date-inactive)
  14505. (defvar org-read-date-minibuffer-local-map
  14506. (let ((map (make-sparse-keymap)))
  14507. (set-keymap-parent map minibuffer-local-map)
  14508. (org-defkey map (kbd ".")
  14509. (lambda () (interactive)
  14510. (org-eval-in-calendar '(calendar-goto-today))))
  14511. (org-defkey map [(meta shift left)]
  14512. (lambda () (interactive)
  14513. (org-eval-in-calendar '(calendar-backward-month 1))))
  14514. (org-defkey map [(meta shift right)]
  14515. (lambda () (interactive)
  14516. (org-eval-in-calendar '(calendar-forward-month 1))))
  14517. (org-defkey map [(meta shift up)]
  14518. (lambda () (interactive)
  14519. (org-eval-in-calendar '(calendar-backward-year 1))))
  14520. (org-defkey map [(meta shift down)]
  14521. (lambda () (interactive)
  14522. (org-eval-in-calendar '(calendar-forward-year 1))))
  14523. (org-defkey map [?\e (shift left)]
  14524. (lambda () (interactive)
  14525. (org-eval-in-calendar '(calendar-backward-month 1))))
  14526. (org-defkey map [?\e (shift right)]
  14527. (lambda () (interactive)
  14528. (org-eval-in-calendar '(calendar-forward-month 1))))
  14529. (org-defkey map [?\e (shift up)]
  14530. (lambda () (interactive)
  14531. (org-eval-in-calendar '(calendar-backward-year 1))))
  14532. (org-defkey map [?\e (shift down)]
  14533. (lambda () (interactive)
  14534. (org-eval-in-calendar '(calendar-forward-year 1))))
  14535. (org-defkey map [(shift up)]
  14536. (lambda () (interactive)
  14537. (org-eval-in-calendar '(calendar-backward-week 1))))
  14538. (org-defkey map [(shift down)]
  14539. (lambda () (interactive)
  14540. (org-eval-in-calendar '(calendar-forward-week 1))))
  14541. (org-defkey map [(shift left)]
  14542. (lambda () (interactive)
  14543. (org-eval-in-calendar '(calendar-backward-day 1))))
  14544. (org-defkey map [(shift right)]
  14545. (lambda () (interactive)
  14546. (org-eval-in-calendar '(calendar-forward-day 1))))
  14547. (org-defkey map "!"
  14548. (lambda () (interactive)
  14549. (org-eval-in-calendar '(diary-view-entries))
  14550. (message "")))
  14551. (org-defkey map ">"
  14552. (lambda () (interactive)
  14553. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14554. (org-defkey map "<"
  14555. (lambda () (interactive)
  14556. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14557. (org-defkey map "\C-v"
  14558. (lambda () (interactive)
  14559. (org-eval-in-calendar
  14560. '(calendar-scroll-left-three-months 1))))
  14561. (org-defkey map "\M-v"
  14562. (lambda () (interactive)
  14563. (org-eval-in-calendar
  14564. '(calendar-scroll-right-three-months 1))))
  14565. map)
  14566. "Keymap for minibuffer commands when using `org-read-date'.")
  14567. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14568. default-time default-input inactive)
  14569. "Read a date, possibly a time, and make things smooth for the user.
  14570. The prompt will suggest to enter an ISO date, but you can also enter anything
  14571. which will at least partially be understood by `parse-time-string'.
  14572. Unrecognized parts of the date will default to the current day, month, year,
  14573. hour and minute. If this command is called to replace a timestamp at point,
  14574. or to enter the second timestamp of a range, the default time is taken
  14575. from the existing stamp. Furthermore, the command prefers the future,
  14576. so if you are giving a date where the year is not given, and the day-month
  14577. combination is already past in the current year, it will assume you
  14578. mean next year. For details, see the manual. A few examples:
  14579. 3-2-5 --> 2003-02-05
  14580. feb 15 --> currentyear-02-15
  14581. 2/15 --> currentyear-02-15
  14582. sep 12 9 --> 2009-09-12
  14583. 12:45 --> today 12:45
  14584. 22 sept 0:34 --> currentyear-09-22 0:34
  14585. 12 --> currentyear-currentmonth-12
  14586. Fri --> nearest Friday (today or later)
  14587. etc.
  14588. Furthermore you can specify a relative date by giving, as the *first* thing
  14589. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14590. change in days weeks, months, years.
  14591. With a single plus or minus, the date is relative to today. With a double
  14592. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14593. +4d --> four days from today
  14594. +4 --> same as above
  14595. +2w --> two weeks from today
  14596. ++5 --> five days from default date
  14597. The function understands only English month and weekday abbreviations.
  14598. While prompting, a calendar is popped up - you can also select the
  14599. date with the mouse (button 1). The calendar shows a period of three
  14600. months. To scroll it to other months, use the keys `>' and `<'.
  14601. If you don't like the calendar, turn it off with
  14602. \(setq org-read-date-popup-calendar nil)
  14603. With optional argument TO-TIME, the date will immediately be converted
  14604. to an internal time.
  14605. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14606. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14607. still enter a time, and this function will inform the calling routine
  14608. about this change. The calling routine may then choose to change the
  14609. format used to insert the time stamp into the buffer to include the time.
  14610. With optional argument FROM-STRING, read from this string instead from
  14611. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14612. the time/date that is used for everything that is not specified by the
  14613. user."
  14614. (require 'parse-time)
  14615. (let* ((org-time-stamp-rounding-minutes
  14616. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14617. (org-dcst org-display-custom-times)
  14618. (ct (org-current-time))
  14619. (org-def (or org-overriding-default-time default-time ct))
  14620. (org-defdecode (decode-time org-def))
  14621. (dummy (progn
  14622. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14623. (setcar (nthcdr 2 org-defdecode) -1)
  14624. (setcar (nthcdr 1 org-defdecode) 59)
  14625. (setq org-def (apply 'encode-time org-defdecode)
  14626. org-defdecode (decode-time org-def)))))
  14627. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14628. (calendar-frame-setup nil)
  14629. (calendar-setup nil)
  14630. (calendar-move-hook nil)
  14631. (calendar-view-diary-initially-flag nil)
  14632. (calendar-view-holidays-initially-flag nil)
  14633. (timestr (format-time-string
  14634. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14635. (prompt (concat (if prompt (concat prompt " ") "")
  14636. (format "Date+time [%s]: " timestr)))
  14637. ans (org-ans0 "") org-ans1 org-ans2 final)
  14638. (cond
  14639. (from-string (setq ans from-string))
  14640. (org-read-date-popup-calendar
  14641. (save-excursion
  14642. (save-window-excursion
  14643. (calendar)
  14644. (org-eval-in-calendar '(setq cursor-type nil) t)
  14645. (unwind-protect
  14646. (progn
  14647. (calendar-forward-day (- (time-to-days org-def)
  14648. (calendar-absolute-from-gregorian
  14649. (calendar-current-date))))
  14650. (org-eval-in-calendar nil t)
  14651. (let* ((old-map (current-local-map))
  14652. (map (copy-keymap calendar-mode-map))
  14653. (minibuffer-local-map
  14654. (copy-keymap org-read-date-minibuffer-local-map)))
  14655. (org-defkey map (kbd "RET") 'org-calendar-select)
  14656. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14657. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14658. (unwind-protect
  14659. (progn
  14660. (use-local-map map)
  14661. (setq org-read-date-inactive inactive)
  14662. (add-hook 'post-command-hook 'org-read-date-display)
  14663. (setq org-ans0 (read-string prompt default-input
  14664. 'org-read-date-history nil))
  14665. ;; org-ans0: from prompt
  14666. ;; org-ans1: from mouse click
  14667. ;; org-ans2: from calendar motion
  14668. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14669. (remove-hook 'post-command-hook 'org-read-date-display)
  14670. (use-local-map old-map)
  14671. (when org-read-date-overlay
  14672. (delete-overlay org-read-date-overlay)
  14673. (setq org-read-date-overlay nil)))))
  14674. (bury-buffer "*Calendar*")))))
  14675. (t ; Naked prompt only
  14676. (unwind-protect
  14677. (setq ans (read-string prompt default-input
  14678. 'org-read-date-history timestr))
  14679. (when org-read-date-overlay
  14680. (delete-overlay org-read-date-overlay)
  14681. (setq org-read-date-overlay nil)))))
  14682. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14683. (when org-read-date-analyze-forced-year
  14684. (message "Year was forced into %s"
  14685. (if org-read-date-force-compatible-dates
  14686. "compatible range (1970-2037)"
  14687. "range representable on this machine"))
  14688. (ding))
  14689. ;; One round trip to get rid of 34th of August and stuff like that....
  14690. (setq final (decode-time (apply 'encode-time final)))
  14691. (setq org-read-date-final-answer ans)
  14692. (if to-time
  14693. (apply 'encode-time final)
  14694. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14695. (format "%04d-%02d-%02d %02d:%02d"
  14696. (nth 5 final) (nth 4 final) (nth 3 final)
  14697. (nth 2 final) (nth 1 final))
  14698. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14699. (defvar org-def)
  14700. (defvar org-defdecode)
  14701. (defvar org-with-time)
  14702. (defun org-read-date-display ()
  14703. "Display the current date prompt interpretation in the minibuffer."
  14704. (when org-read-date-display-live
  14705. (when org-read-date-overlay
  14706. (delete-overlay org-read-date-overlay))
  14707. (when (minibufferp (current-buffer))
  14708. (save-excursion
  14709. (end-of-line 1)
  14710. (while (not (equal (buffer-substring
  14711. (max (point-min) (- (point) 4)) (point))
  14712. " "))
  14713. (insert " ")))
  14714. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14715. " " (or org-ans1 org-ans2)))
  14716. (org-end-time-was-given nil)
  14717. (f (org-read-date-analyze ans org-def org-defdecode))
  14718. (fmts (if org-dcst
  14719. org-time-stamp-custom-formats
  14720. org-time-stamp-formats))
  14721. (fmt (if (or org-with-time
  14722. (and (boundp 'org-time-was-given) org-time-was-given))
  14723. (cdr fmts)
  14724. (car fmts)))
  14725. (txt (format-time-string fmt (apply 'encode-time f)))
  14726. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14727. (txt (concat "=> " txt)))
  14728. (when (and org-end-time-was-given
  14729. (string-match org-plain-time-of-day-regexp txt))
  14730. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14731. org-end-time-was-given
  14732. (substring txt (match-end 0)))))
  14733. (when org-read-date-analyze-futurep
  14734. (setq txt (concat txt " (=>F)")))
  14735. (setq org-read-date-overlay
  14736. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14737. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14738. (defun org-read-date-analyze (ans org-def org-defdecode)
  14739. "Analyze the combined answer of the date prompt."
  14740. ;; FIXME: cleanup and comment
  14741. (let ((nowdecode (decode-time (current-time)))
  14742. delta deltan deltaw deltadef year month day
  14743. hour minute second wday pm h2 m2 tl wday1
  14744. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14745. (setq org-read-date-analyze-futurep nil
  14746. org-read-date-analyze-forced-year nil)
  14747. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14748. (setq ans "+0"))
  14749. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14750. (setq ans (replace-match "" t t ans)
  14751. deltan (car delta)
  14752. deltaw (nth 1 delta)
  14753. deltadef (nth 2 delta)))
  14754. ;; Check if there is an iso week date in there. If yes, store the
  14755. ;; info and postpone interpreting it until the rest of the parsing
  14756. ;; is done.
  14757. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14758. (setq iso-year (if (match-end 1)
  14759. (org-small-year-to-year
  14760. (string-to-number (match-string 1 ans))))
  14761. iso-weekday (if (match-end 3)
  14762. (string-to-number (match-string 3 ans)))
  14763. iso-week (string-to-number (match-string 2 ans)))
  14764. (setq ans (replace-match "" t t ans)))
  14765. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14766. (when (string-match
  14767. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14768. (setq year (if (match-end 2)
  14769. (string-to-number (match-string 2 ans))
  14770. (progn (setq kill-year t)
  14771. (string-to-number (format-time-string "%Y"))))
  14772. month (string-to-number (match-string 3 ans))
  14773. day (string-to-number (match-string 4 ans)))
  14774. (if (< year 100) (setq year (+ 2000 year)))
  14775. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14776. t nil ans)))
  14777. ;; Help matching dotted european dates
  14778. (when (string-match
  14779. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14780. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14781. (setq kill-year t)
  14782. (string-to-number (format-time-string "%Y")))
  14783. day (string-to-number (match-string 1 ans))
  14784. month (string-to-number (match-string 2 ans))
  14785. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14786. t nil ans)))
  14787. ;; Help matching american dates, like 5/30 or 5/30/7
  14788. (when (string-match
  14789. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14790. (setq year (if (match-end 4)
  14791. (string-to-number (match-string 4 ans))
  14792. (progn (setq kill-year t)
  14793. (string-to-number (format-time-string "%Y"))))
  14794. month (string-to-number (match-string 1 ans))
  14795. day (string-to-number (match-string 2 ans)))
  14796. (if (< year 100) (setq year (+ 2000 year)))
  14797. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14798. t nil ans)))
  14799. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14800. ;; If there is a time with am/pm, and *no* time without it, we convert
  14801. ;; so that matching will be successful.
  14802. (loop for i from 1 to 2 do ; twice, for end time as well
  14803. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14804. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14805. (setq hour (string-to-number (match-string 1 ans))
  14806. minute (if (match-end 3)
  14807. (string-to-number (match-string 3 ans))
  14808. 0)
  14809. pm (equal ?p
  14810. (string-to-char (downcase (match-string 4 ans)))))
  14811. (if (and (= hour 12) (not pm))
  14812. (setq hour 0)
  14813. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14814. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14815. t t ans))))
  14816. ;; Check if a time range is given as a duration
  14817. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14818. (setq hour (string-to-number (match-string 1 ans))
  14819. h2 (+ hour (string-to-number (match-string 3 ans)))
  14820. minute (string-to-number (match-string 2 ans))
  14821. m2 (+ minute (if (match-end 5) (string-to-number
  14822. (match-string 5 ans))0)))
  14823. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14824. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14825. t t ans)))
  14826. ;; Check if there is a time range
  14827. (when (boundp 'org-end-time-was-given)
  14828. (setq org-time-was-given nil)
  14829. (when (and (string-match org-plain-time-of-day-regexp ans)
  14830. (match-end 8))
  14831. (setq org-end-time-was-given (match-string 8 ans))
  14832. (setq ans (concat (substring ans 0 (match-beginning 7))
  14833. (substring ans (match-end 7))))))
  14834. (setq tl (parse-time-string ans)
  14835. day (or (nth 3 tl) (nth 3 org-defdecode))
  14836. month (or (nth 4 tl)
  14837. (if (and org-read-date-prefer-future
  14838. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14839. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14840. (nth 4 org-defdecode)))
  14841. year (or (and (not kill-year) (nth 5 tl))
  14842. (if (and org-read-date-prefer-future
  14843. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14844. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14845. (nth 5 org-defdecode)))
  14846. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14847. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14848. second (or (nth 0 tl) 0)
  14849. wday (nth 6 tl))
  14850. (when (and (eq org-read-date-prefer-future 'time)
  14851. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14852. (equal day (nth 3 nowdecode))
  14853. (equal month (nth 4 nowdecode))
  14854. (equal year (nth 5 nowdecode))
  14855. (nth 2 tl)
  14856. (or (< (nth 2 tl) (nth 2 nowdecode))
  14857. (and (= (nth 2 tl) (nth 2 nowdecode))
  14858. (nth 1 tl)
  14859. (< (nth 1 tl) (nth 1 nowdecode)))))
  14860. (setq day (1+ day)
  14861. futurep t))
  14862. ;; Special date definitions below
  14863. (cond
  14864. (iso-week
  14865. ;; There was an iso week
  14866. (require 'cal-iso)
  14867. (setq futurep nil)
  14868. (setq year (or iso-year year)
  14869. day (or iso-weekday wday 1)
  14870. wday nil ; to make sure that the trigger below does not match
  14871. iso-date (calendar-gregorian-from-absolute
  14872. (calendar-absolute-from-iso
  14873. (list iso-week day year))))
  14874. ; FIXME: Should we also push ISO weeks into the future?
  14875. ; (when (and org-read-date-prefer-future
  14876. ; (not iso-year)
  14877. ; (< (calendar-absolute-from-gregorian iso-date)
  14878. ; (time-to-days (current-time))))
  14879. ; (setq year (1+ year)
  14880. ; iso-date (calendar-gregorian-from-absolute
  14881. ; (calendar-absolute-from-iso
  14882. ; (list iso-week day year)))))
  14883. (setq month (car iso-date)
  14884. year (nth 2 iso-date)
  14885. day (nth 1 iso-date)))
  14886. (deltan
  14887. (setq futurep nil)
  14888. (unless deltadef
  14889. (let ((now (decode-time (current-time))))
  14890. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14891. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14892. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14893. ((equal deltaw "m") (setq month (+ month deltan)))
  14894. ((equal deltaw "y") (setq year (+ year deltan)))))
  14895. ((and wday (not (nth 3 tl)))
  14896. ;; Weekday was given, but no day, so pick that day in the week
  14897. ;; on or after the derived date.
  14898. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14899. (unless (equal wday wday1)
  14900. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14901. (if (and (boundp 'org-time-was-given)
  14902. (nth 2 tl))
  14903. (setq org-time-was-given t))
  14904. (if (< year 100) (setq year (+ 2000 year)))
  14905. ;; Check of the date is representable
  14906. (if org-read-date-force-compatible-dates
  14907. (progn
  14908. (if (< year 1970)
  14909. (setq year 1970 org-read-date-analyze-forced-year t))
  14910. (if (> year 2037)
  14911. (setq year 2037 org-read-date-analyze-forced-year t)))
  14912. (condition-case nil
  14913. (ignore (encode-time second minute hour day month year))
  14914. (error
  14915. (setq year (nth 5 org-defdecode))
  14916. (setq org-read-date-analyze-forced-year t))))
  14917. (setq org-read-date-analyze-futurep futurep)
  14918. (list second minute hour day month year)))
  14919. (defvar parse-time-weekdays)
  14920. (defun org-read-date-get-relative (s today default)
  14921. "Check string S for special relative date string.
  14922. TODAY and DEFAULT are internal times, for today and for a default.
  14923. Return shift list (N what def-flag)
  14924. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14925. N is the number of WHATs to shift.
  14926. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14927. the DEFAULT date rather than TODAY."
  14928. (require 'parse-time)
  14929. (when (and
  14930. (string-match
  14931. (concat
  14932. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14933. "\\([0-9]+\\)?"
  14934. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14935. "\\([ \t]\\|$\\)") s)
  14936. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14937. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14938. (string-to-char (substring (match-string 1 s) -1))
  14939. ?+))
  14940. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14941. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14942. (what (if (match-end 3) (match-string 3 s) "d"))
  14943. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14944. (date (if rel default today))
  14945. (wday (nth 6 (decode-time date)))
  14946. delta)
  14947. (if wday1
  14948. (progn
  14949. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14950. (if (= dir ?-) (setq delta (- delta 7)))
  14951. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14952. (list delta "d" rel))
  14953. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14954. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14955. "Turn a user-specified date into the internal representation.
  14956. The internal representation needed by the calendar is (month day year).
  14957. This is a wrapper to handle the brain-dead convention in calendar that
  14958. user function argument order change dependent on argument order."
  14959. (if (boundp 'calendar-date-style)
  14960. (cond
  14961. ((eq calendar-date-style 'american)
  14962. (list arg1 arg2 arg3))
  14963. ((eq calendar-date-style 'european)
  14964. (list arg2 arg1 arg3))
  14965. ((eq calendar-date-style 'iso)
  14966. (list arg2 arg3 arg1)))
  14967. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14968. (if (org-bound-and-true-p european-calendar-style)
  14969. (list arg2 arg1 arg3)
  14970. (list arg1 arg2 arg3)))))
  14971. (defun org-eval-in-calendar (form &optional keepdate)
  14972. "Eval FORM in the calendar window and return to current window.
  14973. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14974. otherwise stick to the current value of `org-ans2'."
  14975. (let ((sf (selected-frame))
  14976. (sw (selected-window)))
  14977. (select-window (get-buffer-window "*Calendar*" t))
  14978. (eval form)
  14979. (when (and (not keepdate) (calendar-cursor-to-date))
  14980. (let* ((date (calendar-cursor-to-date))
  14981. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14982. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14983. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14984. (select-window sw)
  14985. (org-select-frame-set-input-focus sf)))
  14986. (defun org-calendar-select ()
  14987. "Return to `org-read-date' with the date currently selected.
  14988. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14989. (interactive)
  14990. (when (calendar-cursor-to-date)
  14991. (let* ((date (calendar-cursor-to-date))
  14992. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14993. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14994. (if (active-minibuffer-window) (exit-minibuffer))))
  14995. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14996. "Insert a date stamp for the date given by the internal TIME.
  14997. WITH-HM means use the stamp format that includes the time of the day.
  14998. INACTIVE means use square brackets instead of angular ones, so that the
  14999. stamp will not contribute to the agenda.
  15000. PRE and POST are optional strings to be inserted before and after the
  15001. stamp.
  15002. The command returns the inserted time stamp."
  15003. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15004. stamp)
  15005. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15006. (insert-before-markers (or pre ""))
  15007. (when (listp extra)
  15008. (setq extra (car extra))
  15009. (if (and (stringp extra)
  15010. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15011. (setq extra (format "-%02d:%02d"
  15012. (string-to-number (match-string 1 extra))
  15013. (string-to-number (match-string 2 extra))))
  15014. (setq extra nil)))
  15015. (when extra
  15016. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15017. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15018. (insert-before-markers (or post ""))
  15019. (setq org-last-inserted-timestamp stamp)))
  15020. (defun org-toggle-time-stamp-overlays ()
  15021. "Toggle the use of custom time stamp formats."
  15022. (interactive)
  15023. (setq org-display-custom-times (not org-display-custom-times))
  15024. (unless org-display-custom-times
  15025. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15026. (while (setq p (next-single-property-change p 'display))
  15027. (if (and (get-text-property p 'display)
  15028. (eq (get-text-property p 'face) 'org-date))
  15029. (remove-text-properties
  15030. p (setq p (next-single-property-change p 'display))
  15031. '(display t))))
  15032. (set-buffer-modified-p bmp)))
  15033. (if (featurep 'xemacs)
  15034. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15035. (org-restart-font-lock)
  15036. (setq org-table-may-need-update t)
  15037. (if org-display-custom-times
  15038. (message "Time stamps are overlaid with custom format")
  15039. (message "Time stamp overlays removed")))
  15040. (defun org-display-custom-time (beg end)
  15041. "Overlay modified time stamp format over timestamp between BEG and END."
  15042. (let* ((ts (buffer-substring beg end))
  15043. t1 w1 with-hm tf time str w2 (off 0))
  15044. (save-match-data
  15045. (setq t1 (org-parse-time-string ts t))
  15046. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15047. (setq off (- (match-end 0) (match-beginning 0)))))
  15048. (setq end (- end off))
  15049. (setq w1 (- end beg)
  15050. with-hm (and (nth 1 t1) (nth 2 t1))
  15051. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15052. time (org-fix-decoded-time t1)
  15053. str (org-add-props
  15054. (format-time-string
  15055. (substring tf 1 -1) (apply 'encode-time time))
  15056. nil 'mouse-face 'highlight)
  15057. w2 (length str))
  15058. (if (not (= w2 w1))
  15059. (add-text-properties (1+ beg) (+ 2 beg)
  15060. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15061. (if (featurep 'xemacs)
  15062. (progn
  15063. (put-text-property beg end 'invisible t)
  15064. (put-text-property beg end 'end-glyph (make-glyph str)))
  15065. (put-text-property beg end 'display str))))
  15066. (defun org-translate-time (string)
  15067. "Translate all timestamps in STRING to custom format.
  15068. But do this only if the variable `org-display-custom-times' is set."
  15069. (when org-display-custom-times
  15070. (save-match-data
  15071. (let* ((start 0)
  15072. (re org-ts-regexp-both)
  15073. t1 with-hm inactive tf time str beg end)
  15074. (while (setq start (string-match re string start))
  15075. (setq beg (match-beginning 0)
  15076. end (match-end 0)
  15077. t1 (save-match-data
  15078. (org-parse-time-string (substring string beg end) t))
  15079. with-hm (and (nth 1 t1) (nth 2 t1))
  15080. inactive (equal (substring string beg (1+ beg)) "[")
  15081. tf (funcall (if with-hm 'cdr 'car)
  15082. org-time-stamp-custom-formats)
  15083. time (org-fix-decoded-time t1)
  15084. str (format-time-string
  15085. (concat
  15086. (if inactive "[" "<") (substring tf 1 -1)
  15087. (if inactive "]" ">"))
  15088. (apply 'encode-time time))
  15089. string (replace-match str t t string)
  15090. start (+ start (length str)))))))
  15091. string)
  15092. (defun org-fix-decoded-time (time)
  15093. "Set 0 instead of nil for the first 6 elements of time.
  15094. Don't touch the rest."
  15095. (let ((n 0))
  15096. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15097. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15098. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15099. "Difference between TIMESTAMP-STRING and now in days.
  15100. If SECONDS is non-nil, return the difference in seconds."
  15101. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15102. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15103. (funcall fdiff (current-time)))))
  15104. (defun org-deadline-close (timestamp-string &optional ndays)
  15105. "Is the time in TIMESTAMP-STRING close to the current date?"
  15106. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15107. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15108. (not (org-entry-is-done-p))))
  15109. (defun org-get-wdays (ts &optional delay zero-delay)
  15110. "Get the deadline lead time appropriate for timestring TS.
  15111. When DELAY is non-nil, get the delay time for scheduled items
  15112. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15113. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15114. don't try to find the delay cookie in the scheduled timestamp."
  15115. (let ((tv (if delay org-scheduled-delay-days
  15116. org-deadline-warning-days)))
  15117. (cond
  15118. ((or (and delay (< tv 0))
  15119. (and delay zero-delay (<= tv 0))
  15120. (and (not delay) (<= tv 0)))
  15121. ;; Enforce this value no matter what
  15122. (- tv))
  15123. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15124. ;; lead time is specified.
  15125. (floor (* (string-to-number (match-string 1 ts))
  15126. (cdr (assoc (match-string 2 ts)
  15127. '(("d" . 1) ("w" . 7)
  15128. ("m" . 30.4) ("y" . 365.25)
  15129. ("h" . 0.041667)))))))
  15130. ;; go for the default.
  15131. (t tv))))
  15132. (defun org-calendar-select-mouse (ev)
  15133. "Return to `org-read-date' with the date currently selected.
  15134. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15135. (interactive "e")
  15136. (mouse-set-point ev)
  15137. (when (calendar-cursor-to-date)
  15138. (let* ((date (calendar-cursor-to-date))
  15139. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15140. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15141. (if (active-minibuffer-window) (exit-minibuffer))))
  15142. (defun org-check-deadlines (ndays)
  15143. "Check if there are any deadlines due or past due.
  15144. A deadline is considered due if it happens within `org-deadline-warning-days'
  15145. days from today's date. If the deadline appears in an entry marked DONE,
  15146. it is not shown. The prefix arg NDAYS can be used to test that many
  15147. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15148. (interactive "P")
  15149. (let* ((org-warn-days
  15150. (cond
  15151. ((equal ndays '(4)) 100000)
  15152. (ndays (prefix-numeric-value ndays))
  15153. (t (abs org-deadline-warning-days))))
  15154. (case-fold-search nil)
  15155. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15156. (callback
  15157. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15158. (message "%d deadlines past-due or due within %d days"
  15159. (org-occur regexp nil callback)
  15160. org-warn-days)))
  15161. (defsubst org-re-timestamp (type)
  15162. "Return a regexp for timestamp TYPE.
  15163. Allowed values for TYPE are:
  15164. all: all timestamps
  15165. active: only active timestamps (<...>)
  15166. inactive: only inactive timestamps ([...])
  15167. scheduled: only scheduled timestamps
  15168. deadline: only deadline timestamps
  15169. When TYPE is nil, fall back on returning a regexp that matches
  15170. both scheduled and deadline timestamps."
  15171. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15172. ((eq type 'active) org-ts-regexp)
  15173. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15174. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15175. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15176. ((eq type 'scheduled-or-deadline)
  15177. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15178. (defun org-check-before-date (date)
  15179. "Check if there are deadlines or scheduled entries before DATE."
  15180. (interactive (list (org-read-date)))
  15181. (let ((case-fold-search nil)
  15182. (regexp (org-re-timestamp org-ts-type))
  15183. (callback
  15184. (lambda () (time-less-p
  15185. (org-time-string-to-time (match-string 1))
  15186. (org-time-string-to-time date)))))
  15187. (message "%d entries before %s"
  15188. (org-occur regexp nil callback) date)))
  15189. (defun org-check-after-date (date)
  15190. "Check if there are deadlines or scheduled entries after DATE."
  15191. (interactive (list (org-read-date)))
  15192. (let ((case-fold-search nil)
  15193. (regexp (org-re-timestamp org-ts-type))
  15194. (callback
  15195. (lambda () (not
  15196. (time-less-p
  15197. (org-time-string-to-time (match-string 1))
  15198. (org-time-string-to-time date))))))
  15199. (message "%d entries after %s"
  15200. (org-occur regexp nil callback) date)))
  15201. (defun org-check-dates-range (start-date end-date)
  15202. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15203. (interactive (list (org-read-date nil nil nil "Range starts")
  15204. (org-read-date nil nil nil "Range end")))
  15205. (let ((case-fold-search nil)
  15206. (regexp (org-re-timestamp org-ts-type))
  15207. (callback
  15208. (lambda ()
  15209. (let ((match (match-string 1)))
  15210. (and
  15211. (not (time-less-p
  15212. (org-time-string-to-time match)
  15213. (org-time-string-to-time start-date)))
  15214. (time-less-p
  15215. (org-time-string-to-time match)
  15216. (org-time-string-to-time end-date)))))))
  15217. (message "%d entries between %s and %s"
  15218. (org-occur regexp nil callback) start-date end-date)))
  15219. (defun org-evaluate-time-range (&optional to-buffer)
  15220. "Evaluate a time range by computing the difference between start and end.
  15221. Normally the result is just printed in the echo area, but with prefix arg
  15222. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15223. If the time range is actually in a table, the result is inserted into the
  15224. next column.
  15225. For time difference computation, a year is assumed to be exactly 365
  15226. days in order to avoid rounding problems."
  15227. (interactive "P")
  15228. (or
  15229. (org-clock-update-time-maybe)
  15230. (save-excursion
  15231. (unless (org-at-date-range-p t)
  15232. (goto-char (point-at-bol))
  15233. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15234. (if (not (org-at-date-range-p t))
  15235. (error "Not at a time-stamp range, and none found in current line")))
  15236. (let* ((ts1 (match-string 1))
  15237. (ts2 (match-string 2))
  15238. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15239. (match-end (match-end 0))
  15240. (time1 (org-time-string-to-time ts1))
  15241. (time2 (org-time-string-to-time ts2))
  15242. (t1 (org-float-time time1))
  15243. (t2 (org-float-time time2))
  15244. (diff (abs (- t2 t1)))
  15245. (negative (< (- t2 t1) 0))
  15246. ;; (ys (floor (* 365 24 60 60)))
  15247. (ds (* 24 60 60))
  15248. (hs (* 60 60))
  15249. (fy "%dy %dd %02d:%02d")
  15250. (fy1 "%dy %dd")
  15251. (fd "%dd %02d:%02d")
  15252. (fd1 "%dd")
  15253. (fh "%02d:%02d")
  15254. y d h m align)
  15255. (if havetime
  15256. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15257. y 0
  15258. d (floor (/ diff ds)) diff (mod diff ds)
  15259. h (floor (/ diff hs)) diff (mod diff hs)
  15260. m (floor (/ diff 60)))
  15261. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15262. y 0
  15263. d (floor (+ (/ diff ds) 0.5))
  15264. h 0 m 0))
  15265. (if (not to-buffer)
  15266. (message "%s" (org-make-tdiff-string y d h m))
  15267. (if (org-at-table-p)
  15268. (progn
  15269. (goto-char match-end)
  15270. (setq align t)
  15271. (and (looking-at " *|") (goto-char (match-end 0))))
  15272. (goto-char match-end))
  15273. (if (looking-at
  15274. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15275. (replace-match ""))
  15276. (if negative (insert " -"))
  15277. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15278. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15279. (insert " " (format fh h m))))
  15280. (if align (org-table-align))
  15281. (message "Time difference inserted")))))
  15282. (defun org-make-tdiff-string (y d h m)
  15283. (let ((fmt "")
  15284. (l nil))
  15285. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15286. l (push y l)))
  15287. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15288. l (push d l)))
  15289. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15290. l (push h l)))
  15291. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15292. l (push m l)))
  15293. (apply 'format fmt (nreverse l))))
  15294. (defun org-time-string-to-time (s &optional buffer pos)
  15295. "Convert a timestamp string into internal time."
  15296. (condition-case errdata
  15297. (apply 'encode-time (org-parse-time-string s))
  15298. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15299. s (if (not (and buffer pos))
  15300. ""
  15301. (format " at %d in buffer `%s'" pos buffer))
  15302. (cdr errdata)))))
  15303. (defun org-time-string-to-seconds (s)
  15304. "Convert a timestamp string to a number of seconds."
  15305. (org-float-time (org-time-string-to-time s)))
  15306. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15307. "Convert a time stamp to an absolute day number.
  15308. If there is a specifier for a cyclic time stamp, get the closest
  15309. date to DAYNR.
  15310. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15311. The variable `date' is bound by the calendar when this is called."
  15312. (cond
  15313. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15314. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15315. daynr
  15316. (+ daynr 1000)))
  15317. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15318. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15319. (time-to-days (current-time))) (match-string 0 s)
  15320. prefer show-all))
  15321. (t (time-to-days
  15322. (condition-case errdata
  15323. (apply 'encode-time (org-parse-time-string s))
  15324. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15325. s (if (not (and buffer pos))
  15326. ""
  15327. (format " at %d in buffer `%s'" pos buffer))
  15328. (cdr errdata))))))))
  15329. (defun org-days-to-iso-week (days)
  15330. "Return the iso week number."
  15331. (require 'cal-iso)
  15332. (car (calendar-iso-from-absolute days)))
  15333. (defun org-small-year-to-year (year)
  15334. "Convert 2-digit years into 4-digit years.
  15335. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15336. The year 2000 cannot be abbreviated. Any year larger than 99
  15337. is returned unchanged."
  15338. (if (< year 38)
  15339. (setq year (+ 2000 year))
  15340. (if (< year 100)
  15341. (setq year (+ 1900 year))))
  15342. year)
  15343. (defun org-time-from-absolute (d)
  15344. "Return the time corresponding to date D.
  15345. D may be an absolute day number, or a calendar-type list (month day year)."
  15346. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15347. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15348. (defun org-calendar-holiday ()
  15349. "List of holidays, for Diary display in Org-mode."
  15350. (require 'holidays)
  15351. (let ((hl (funcall
  15352. (if (fboundp 'calendar-check-holidays)
  15353. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15354. (if hl (mapconcat 'identity hl "; "))))
  15355. (defun org-diary-sexp-entry (sexp entry date)
  15356. "Process a SEXP diary ENTRY for DATE."
  15357. (require 'diary-lib)
  15358. (let ((result (if calendar-debug-sexp
  15359. (let ((stack-trace-on-error t))
  15360. (eval (car (read-from-string sexp))))
  15361. (condition-case nil
  15362. (eval (car (read-from-string sexp)))
  15363. (error
  15364. (beep)
  15365. (message "Bad sexp at line %d in %s: %s"
  15366. (org-current-line)
  15367. (buffer-file-name) sexp)
  15368. (sleep-for 2))))))
  15369. (cond ((stringp result) (split-string result "; "))
  15370. ((and (consp result)
  15371. (not (consp (cdr result)))
  15372. (stringp (cdr result))) (cdr result))
  15373. ((and (consp result)
  15374. (stringp (car result))) result)
  15375. (result entry))))
  15376. (defun org-diary-to-ical-string (frombuf)
  15377. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15378. This uses the icalendar.el library."
  15379. (let* ((tmpdir (if (featurep 'xemacs)
  15380. (temp-directory)
  15381. temporary-file-directory))
  15382. (tmpfile (make-temp-name
  15383. (expand-file-name "orgics" tmpdir)))
  15384. buf rtn b e)
  15385. (with-current-buffer frombuf
  15386. (icalendar-export-region (point-min) (point-max) tmpfile)
  15387. (setq buf (find-buffer-visiting tmpfile))
  15388. (set-buffer buf)
  15389. (goto-char (point-min))
  15390. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15391. (setq b (match-beginning 0)))
  15392. (goto-char (point-max))
  15393. (if (re-search-backward "^END:VEVENT" nil t)
  15394. (setq e (match-end 0)))
  15395. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15396. (kill-buffer buf)
  15397. (delete-file tmpfile)
  15398. rtn))
  15399. (defun org-closest-date (start current change prefer show-all)
  15400. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15401. When PREFER is `past', return a date that is either CURRENT or past.
  15402. When PREFER is `future', return a date that is either CURRENT or future.
  15403. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15404. ;; Make the proper lists from the dates
  15405. (catch 'exit
  15406. (let ((a1 '(("h" . hour)
  15407. ("d" . day)
  15408. ("w" . week)
  15409. ("m" . month)
  15410. ("y" . year)))
  15411. (shour (nth 2 (org-parse-time-string start)))
  15412. dn dw sday cday n1 n2 n0
  15413. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15414. (setq start (org-date-to-gregorian start)
  15415. current (org-date-to-gregorian
  15416. (if show-all
  15417. current
  15418. (time-to-days (current-time))))
  15419. sday (calendar-absolute-from-gregorian start)
  15420. cday (calendar-absolute-from-gregorian current))
  15421. (if (<= cday sday) (throw 'exit sday))
  15422. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15423. (setq dn (string-to-number (match-string 1 change))
  15424. dw (cdr (assoc (match-string 2 change) a1)))
  15425. (error "Invalid change specifier: %s" change))
  15426. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15427. (cond
  15428. ((eq dw 'hour)
  15429. (let ((missing-hours
  15430. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15431. dn)))
  15432. (setq n1 (if (zerop missing-hours) cday
  15433. (- cday (1+ (floor (/ missing-hours 24)))))
  15434. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15435. ((eq dw 'day)
  15436. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15437. n2 (+ n1 dn)))
  15438. ((eq dw 'year)
  15439. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15440. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15441. (setq date1 (list m d y1)
  15442. n1 (calendar-absolute-from-gregorian date1)
  15443. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15444. n2 (calendar-absolute-from-gregorian date2)))
  15445. ((eq dw 'month)
  15446. ;; approx number of month between the two dates
  15447. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15448. ;; How often does dn fit in there?
  15449. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15450. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15451. m (+ m nm)
  15452. ny (floor (/ m 12))
  15453. y (+ y ny)
  15454. m (- m (* ny 12)))
  15455. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15456. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15457. (setq m2 (+ m dn) y2 y)
  15458. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15459. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15460. (while (<= n2 cday)
  15461. (setq n1 n2 m m2 y y2)
  15462. (setq m2 (+ m dn) y2 y)
  15463. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15464. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15465. ;; Make sure n1 is the earlier date
  15466. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15467. (if show-all
  15468. (cond
  15469. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15470. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15471. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15472. (cond
  15473. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15474. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15475. (t (if (= cday n1) n1 n2)))))))
  15476. (defun org-date-to-gregorian (date)
  15477. "Turn any specification of DATE into a Gregorian date for the calendar."
  15478. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15479. ((and (listp date) (= (length date) 3)) date)
  15480. ((stringp date)
  15481. (setq date (org-parse-time-string date))
  15482. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15483. ((listp date)
  15484. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15485. (defun org-parse-time-string (s &optional nodefault)
  15486. "Parse the standard Org-mode time string.
  15487. This should be a lot faster than the normal `parse-time-string'.
  15488. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15489. hour and minute fields will be nil if not given."
  15490. (cond ((string-match org-ts-regexp0 s)
  15491. (list 0
  15492. (if (or (match-beginning 8) (not nodefault))
  15493. (string-to-number (or (match-string 8 s) "0")))
  15494. (if (or (match-beginning 7) (not nodefault))
  15495. (string-to-number (or (match-string 7 s) "0")))
  15496. (string-to-number (match-string 4 s))
  15497. (string-to-number (match-string 3 s))
  15498. (string-to-number (match-string 2 s))
  15499. nil nil nil))
  15500. ((string-match "^<[^>]+>$" s)
  15501. (decode-time (seconds-to-time (org-matcher-time s))))
  15502. (t (error "Not a standard Org-mode time string: %s" s))))
  15503. (defun org-timestamp-up (&optional arg)
  15504. "Increase the date item at the cursor by one.
  15505. If the cursor is on the year, change the year. If it is on the month,
  15506. the day or the time, change that.
  15507. With prefix ARG, change by that many units."
  15508. (interactive "p")
  15509. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15510. (defun org-timestamp-down (&optional arg)
  15511. "Decrease the date item at the cursor by one.
  15512. If the cursor is on the year, change the year. If it is on the month,
  15513. the day or the time, change that.
  15514. With prefix ARG, change by that many units."
  15515. (interactive "p")
  15516. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15517. (defun org-timestamp-up-day (&optional arg)
  15518. "Increase the date in the time stamp by one day.
  15519. With prefix ARG, change that many days."
  15520. (interactive "p")
  15521. (if (and (not (org-at-timestamp-p t))
  15522. (org-at-heading-p))
  15523. (org-todo 'up)
  15524. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15525. (defun org-timestamp-down-day (&optional arg)
  15526. "Decrease the date in the time stamp by one day.
  15527. With prefix ARG, change that many days."
  15528. (interactive "p")
  15529. (if (and (not (org-at-timestamp-p t))
  15530. (org-at-heading-p))
  15531. (org-todo 'down)
  15532. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15533. (defun org-at-timestamp-p (&optional inactive-ok)
  15534. "Determine if the cursor is in or at a timestamp."
  15535. (interactive)
  15536. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15537. (pos (point))
  15538. (ans (or (looking-at tsr)
  15539. (save-excursion
  15540. (skip-chars-backward "^[<\n\r\t")
  15541. (if (> (point) (point-min)) (backward-char 1))
  15542. (and (looking-at tsr)
  15543. (> (- (match-end 0) pos) -1))))))
  15544. (and ans
  15545. (boundp 'org-ts-what)
  15546. (setq org-ts-what
  15547. (cond
  15548. ((= pos (match-beginning 0)) 'bracket)
  15549. ;; Point is considered to be "on the bracket" whether
  15550. ;; it's really on it or right after it.
  15551. ((= pos (1- (match-end 0))) 'bracket)
  15552. ((= pos (match-end 0)) 'after)
  15553. ((org-pos-in-match-range pos 2) 'year)
  15554. ((org-pos-in-match-range pos 3) 'month)
  15555. ((org-pos-in-match-range pos 7) 'hour)
  15556. ((org-pos-in-match-range pos 8) 'minute)
  15557. ((or (org-pos-in-match-range pos 4)
  15558. (org-pos-in-match-range pos 5)) 'day)
  15559. ((and (> pos (or (match-end 8) (match-end 5)))
  15560. (< pos (match-end 0)))
  15561. (- pos (or (match-end 8) (match-end 5))))
  15562. (t 'day))))
  15563. ans))
  15564. (defun org-toggle-timestamp-type ()
  15565. "Toggle the type (<active> or [inactive]) of a time stamp."
  15566. (interactive)
  15567. (when (org-at-timestamp-p t)
  15568. (let ((beg (match-beginning 0)) (end (match-end 0))
  15569. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15570. (save-excursion
  15571. (goto-char beg)
  15572. (while (re-search-forward "[][<>]" end t)
  15573. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15574. t t)))
  15575. (message "Timestamp is now %sactive"
  15576. (if (equal (char-after beg) ?<) "" "in")))))
  15577. (defun org-at-clock-log-p nil
  15578. "Is the cursor on the clock log line?"
  15579. (save-excursion
  15580. (move-beginning-of-line 1)
  15581. (looking-at "^[ \t]*CLOCK:")))
  15582. (defvar org-clock-history) ; defined in org-clock.el
  15583. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15584. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15585. "Change the date in the time stamp at point.
  15586. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15587. `year', `minute', `second'. If WHAT is not given, the cursor position
  15588. in the timestamp determines what will be changed.
  15589. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15590. (let ((origin (point)) origin-cat
  15591. with-hm inactive
  15592. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15593. org-ts-what
  15594. extra rem
  15595. ts time time0 fixnext clrgx)
  15596. (if (not (org-at-timestamp-p t))
  15597. (error "Not at a timestamp"))
  15598. (if (and (not what) (eq org-ts-what 'bracket))
  15599. (org-toggle-timestamp-type)
  15600. ;; Point isn't on brackets. Remember the part of the time-stamp
  15601. ;; the point was in. Indeed, size of time-stamps may change,
  15602. ;; but point must be kept in the same category nonetheless.
  15603. (setq origin-cat org-ts-what)
  15604. (if (and (not what) (not (eq org-ts-what 'day))
  15605. org-display-custom-times
  15606. (get-text-property (point) 'display)
  15607. (not (get-text-property (1- (point)) 'display)))
  15608. (setq org-ts-what 'day))
  15609. (setq org-ts-what (or what org-ts-what)
  15610. inactive (= (char-after (match-beginning 0)) ?\[)
  15611. ts (match-string 0))
  15612. (replace-match "")
  15613. (when (string-match
  15614. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15615. ts)
  15616. (setq extra (match-string 1 ts))
  15617. (if suppress-tmp-delay
  15618. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15619. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15620. (setq with-hm t))
  15621. (setq time0 (org-parse-time-string ts))
  15622. (when (and updown
  15623. (eq org-ts-what 'minute)
  15624. (not current-prefix-arg))
  15625. ;; This looks like s-up and s-down. Change by one rounding step.
  15626. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15627. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15628. (setcar (cdr time0) (+ (nth 1 time0)
  15629. (if (> n 0) (- rem) (- dm rem))))))
  15630. (setq time
  15631. (encode-time (or (car time0) 0)
  15632. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15633. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15634. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15635. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15636. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15637. (nthcdr 6 time0)))
  15638. (when (and (member org-ts-what '(hour minute))
  15639. extra
  15640. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15641. (setq extra (org-modify-ts-extra
  15642. extra
  15643. (if (eq org-ts-what 'hour) 2 5)
  15644. n dm)))
  15645. (when (integerp org-ts-what)
  15646. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15647. (if (eq what 'calendar)
  15648. (let ((cal-date (org-get-date-from-calendar)))
  15649. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15650. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15651. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15652. (setcar time0 (or (car time0) 0))
  15653. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15654. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15655. (setq time (apply 'encode-time time0))))
  15656. ;; Insert the new time-stamp, and ensure point stays in the same
  15657. ;; category as before (i.e. not after the last position in that
  15658. ;; category).
  15659. (let ((pos (point)))
  15660. ;; Stay before inserted string. `save-excursion' is of no use.
  15661. (setq org-last-changed-timestamp
  15662. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15663. (goto-char pos))
  15664. (save-match-data
  15665. (looking-at org-ts-regexp3)
  15666. (goto-char (cond
  15667. ;; `day' category ends before `hour' if any, or at
  15668. ;; the end of the day name.
  15669. ((eq origin-cat 'day)
  15670. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15671. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15672. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15673. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15674. ;; `year' and `month' have both fixed size: point
  15675. ;; couldn't have moved into another part.
  15676. (t origin))))
  15677. ;; Update clock if on a CLOCK line.
  15678. (org-clock-update-time-maybe)
  15679. ;; Maybe adjust the closest clock in `org-clock-history'
  15680. (when org-clock-adjust-closest
  15681. (if (not (and (org-at-clock-log-p)
  15682. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15683. org-clock-history))))))
  15684. (message "No clock to adjust")
  15685. (cond ((save-excursion ; fix previous clock?
  15686. (re-search-backward org-ts-regexp0 nil t)
  15687. (org-looking-back (concat org-clock-string " \\[")))
  15688. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15689. ((save-excursion ; fix next clock?
  15690. (re-search-backward org-ts-regexp0 nil t)
  15691. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15692. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15693. (save-window-excursion
  15694. ;; Find closest clock to point, adjust the previous/next one in history
  15695. (let* ((p (save-excursion (org-back-to-heading t)))
  15696. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15697. (clfixnth
  15698. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15699. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15700. (if (not clfixpos)
  15701. (message "No clock to adjust")
  15702. (save-excursion
  15703. (org-goto-marker-or-bmk clfixpos)
  15704. (org-show-subtree)
  15705. (when (re-search-forward clrgx nil t)
  15706. (goto-char (match-beginning 1))
  15707. (let (org-clock-adjust-closest)
  15708. (org-timestamp-change n org-ts-what updown))
  15709. (message "Clock adjusted in %s for heading: %s"
  15710. (file-name-nondirectory (buffer-file-name))
  15711. (org-get-heading t t)))))))))
  15712. ;; Try to recenter the calendar window, if any.
  15713. (if (and org-calendar-follow-timestamp-change
  15714. (get-buffer-window "*Calendar*" t)
  15715. (memq org-ts-what '(day month year)))
  15716. (org-recenter-calendar (time-to-days time))))))
  15717. (defun org-modify-ts-extra (s pos n dm)
  15718. "Change the different parts of the lead-time and repeat fields in timestamp."
  15719. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15720. ng h m new rem)
  15721. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15722. (cond
  15723. ((or (org-pos-in-match-range pos 2)
  15724. (org-pos-in-match-range pos 3))
  15725. (setq m (string-to-number (match-string 3 s))
  15726. h (string-to-number (match-string 2 s)))
  15727. (if (org-pos-in-match-range pos 2)
  15728. (setq h (+ h n))
  15729. (setq n (* dm (org-no-warnings (signum n))))
  15730. (when (not (= 0 (setq rem (% m dm))))
  15731. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15732. (setq m (+ m n)))
  15733. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15734. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15735. (setq h (min 24 (max 0 h)))
  15736. (setq ng 1 new (format "-%02d:%02d" h m)))
  15737. ((org-pos-in-match-range pos 6)
  15738. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15739. ((org-pos-in-match-range pos 5)
  15740. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15741. ((org-pos-in-match-range pos 9)
  15742. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15743. ((org-pos-in-match-range pos 8)
  15744. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15745. (when ng
  15746. (setq s (concat
  15747. (substring s 0 (match-beginning ng))
  15748. new
  15749. (substring s (match-end ng))))))
  15750. s))
  15751. (defun org-recenter-calendar (date)
  15752. "If the calendar is visible, recenter it to DATE."
  15753. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15754. (when cwin
  15755. (let ((calendar-move-hook nil))
  15756. (with-selected-window cwin
  15757. (calendar-goto-date (if (listp date) date
  15758. (calendar-gregorian-from-absolute date))))))))
  15759. (defun org-goto-calendar (&optional arg)
  15760. "Go to the Emacs calendar at the current date.
  15761. If there is a time stamp in the current line, go to that date.
  15762. A prefix ARG can be used to force the current date."
  15763. (interactive "P")
  15764. (let ((tsr org-ts-regexp) diff
  15765. (calendar-move-hook nil)
  15766. (calendar-view-holidays-initially-flag nil)
  15767. (calendar-view-diary-initially-flag nil))
  15768. (if (or (org-at-timestamp-p)
  15769. (save-excursion
  15770. (beginning-of-line 1)
  15771. (looking-at (concat ".*" tsr))))
  15772. (let ((d1 (time-to-days (current-time)))
  15773. (d2 (time-to-days
  15774. (org-time-string-to-time (match-string 1)))))
  15775. (setq diff (- d2 d1))))
  15776. (calendar)
  15777. (calendar-goto-today)
  15778. (if (and diff (not arg)) (calendar-forward-day diff))))
  15779. (defun org-get-date-from-calendar ()
  15780. "Return a list (month day year) of date at point in calendar."
  15781. (with-current-buffer "*Calendar*"
  15782. (save-match-data
  15783. (calendar-cursor-to-date))))
  15784. (defun org-date-from-calendar ()
  15785. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15786. If there is already a time stamp at the cursor position, update it."
  15787. (interactive)
  15788. (if (org-at-timestamp-p t)
  15789. (org-timestamp-change 0 'calendar)
  15790. (let ((cal-date (org-get-date-from-calendar)))
  15791. (org-insert-time-stamp
  15792. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15793. (defcustom org-effort-durations
  15794. `(("h" . 60)
  15795. ("d" . ,(* 60 8))
  15796. ("w" . ,(* 60 8 5))
  15797. ("m" . ,(* 60 8 5 4))
  15798. ("y" . ,(* 60 8 5 40)))
  15799. "Conversion factor to minutes for an effort modifier.
  15800. Each entry has the form (MODIFIER . MINUTES).
  15801. In an effort string, a number followed by MODIFIER is multiplied
  15802. by the specified number of MINUTES to obtain an effort in
  15803. minutes.
  15804. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15805. effort string \"2hours\" is equivalent to 120 minutes."
  15806. :group 'org-agenda
  15807. :version "24.1"
  15808. :type '(alist :key-type (string :tag "Modifier")
  15809. :value-type (number :tag "Minutes")))
  15810. (defun org-minutes-to-clocksum-string (m)
  15811. "Format number of minutes as a clocksum string.
  15812. The format is determined by `org-time-clocksum-format',
  15813. `org-time-clocksum-use-fractional' and
  15814. `org-time-clocksum-fractional-format' and
  15815. `org-time-clocksum-use-effort-durations'."
  15816. (let ((clocksum "") h d w mo y fmt n)
  15817. (setq h (if org-time-clocksum-use-effort-durations
  15818. (cdr (assoc "h" org-effort-durations)) 60)
  15819. d (if org-time-clocksum-use-effort-durations
  15820. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15821. w (if org-time-clocksum-use-effort-durations
  15822. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15823. mo (if org-time-clocksum-use-effort-durations
  15824. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15825. y (if org-time-clocksum-use-effort-durations
  15826. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15827. ;; fractional format
  15828. (if org-time-clocksum-use-fractional
  15829. (cond
  15830. ;; single format string
  15831. ((stringp org-time-clocksum-fractional-format)
  15832. (format org-time-clocksum-fractional-format (/ m (float h))))
  15833. ;; choice of fractional formats for different time units
  15834. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15835. (> (/ (truncate m) (* y d h)) 0))
  15836. (format fmt (/ m (* y d (float h)))))
  15837. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15838. (> (/ (truncate m) (* mo d h)) 0))
  15839. (format fmt (/ m (* mo d (float h)))))
  15840. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15841. (> (/ (truncate m) (* w d h)) 0))
  15842. (format fmt (/ m (* w d (float h)))))
  15843. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15844. (> (/ (truncate m) (* d h)) 0))
  15845. (format fmt (/ m (* d (float h)))))
  15846. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15847. (> (/ (truncate m) h) 0))
  15848. (format fmt (/ m (float h))))
  15849. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15850. (format fmt m))
  15851. ;; fall back to smallest time unit with a format
  15852. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15853. (format fmt (/ m (float h))))
  15854. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15855. (format fmt (/ m (* d (float h)))))
  15856. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15857. (format fmt (/ m (* w d (float h)))))
  15858. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15859. (format fmt (/ m (* mo d (float h)))))
  15860. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15861. (format fmt (/ m (* y d (float h))))))
  15862. ;; standard (non-fractional) format, with single format string
  15863. (if (stringp org-time-clocksum-format)
  15864. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15865. ;; separate formats components
  15866. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15867. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15868. (plist-get org-time-clocksum-format :require-years))
  15869. (setq clocksum (concat clocksum (format fmt n))
  15870. m (- m (* n y d h))))
  15871. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15872. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15873. (plist-get org-time-clocksum-format :require-months))
  15874. (setq clocksum (concat clocksum (format fmt n))
  15875. m (- m (* n mo d h))))
  15876. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15877. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15878. (plist-get org-time-clocksum-format :require-weeks))
  15879. (setq clocksum (concat clocksum (format fmt n))
  15880. m (- m (* n w d h))))
  15881. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15882. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15883. (plist-get org-time-clocksum-format :require-days))
  15884. (setq clocksum (concat clocksum (format fmt n))
  15885. m (- m (* n d h))))
  15886. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15887. (or (> (setq n (/ (truncate m) h)) 0)
  15888. (plist-get org-time-clocksum-format :require-hours))
  15889. (setq clocksum (concat clocksum (format fmt n))
  15890. m (- m (* n h))))
  15891. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15892. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15893. (setq clocksum (concat clocksum (format fmt m))))
  15894. ;; return formatted time duration
  15895. clocksum))))
  15896. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15897. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15898. "Org mode version 8.0")
  15899. (defun org-hours-to-clocksum-string (n)
  15900. (org-minutes-to-clocksum-string (* n 60)))
  15901. (defun org-hh:mm-string-to-minutes (s)
  15902. "Convert a string H:MM to a number of minutes.
  15903. If the string is just a number, interpret it as minutes.
  15904. In fact, the first hh:mm or number in the string will be taken,
  15905. there can be extra stuff in the string.
  15906. If no number is found, the return value is 0."
  15907. (cond
  15908. ((integerp s) s)
  15909. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15910. (+ (* (string-to-number (match-string 1 s)) 60)
  15911. (string-to-number (match-string 2 s))))
  15912. ((string-match "\\([0-9]+\\)" s)
  15913. (string-to-number (match-string 1 s)))
  15914. (t 0)))
  15915. (defcustom org-image-actual-width t
  15916. "Should we use the actual width of images when inlining them?
  15917. When set to `t', always use the image width.
  15918. When set to a number, use imagemagick (when available) to set
  15919. the image's width to this value.
  15920. When set to a number in a list, try to get the width from the
  15921. #+ATTR.* keyword if it matches a width specification like
  15922. width=\"[0-9]+\", and fall back on that number if none is found.
  15923. When set to nil, try to get the width from an #+ATTR.* keyword
  15924. and fall back on the original width if none is found.
  15925. This requires Emacs >= 24.1, build with imagemagick support."
  15926. :group 'org-appearance
  15927. :version "24.4"
  15928. :package-version '(Org . "8.0")
  15929. :type '(choice
  15930. (const :tag "Use the image width" t)
  15931. (integer :tag "Use a number of pixels")
  15932. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15933. (const :tag "Use #+ATTR* or don't resize" nil)))
  15934. (defcustom org-agenda-inhibit-startup nil
  15935. "Inhibit startup when preparing agenda buffers.
  15936. When this variable is `t' (the default), the initialization of
  15937. the Org agenda buffers is inhibited: e.g. the visibility state
  15938. is not set, the tables are not re-aligned, etc."
  15939. :type 'boolean
  15940. :version "24.3"
  15941. :group 'org-agenda)
  15942. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15943. "Convert a duration string S to minutes.
  15944. A bare number is interpreted as minutes, modifiers can be set by
  15945. customizing `org-effort-durations' (which see).
  15946. Entries containing a colon are interpreted as H:MM by
  15947. `org-hh:mm-string-to-minutes'."
  15948. (let ((result 0)
  15949. (re (concat "\\([0-9.]+\\) *\\("
  15950. (regexp-opt (mapcar 'car org-effort-durations))
  15951. "\\)")))
  15952. (while (string-match re s)
  15953. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15954. (string-to-number (match-string 1 s))))
  15955. (setq s (replace-match "" nil t s)))
  15956. (setq result (floor result))
  15957. (incf result (org-hh:mm-string-to-minutes s))
  15958. (if output-to-string (number-to-string result) result)))
  15959. ;;;; Files
  15960. (defun org-save-all-org-buffers ()
  15961. "Save all Org-mode buffers without user confirmation."
  15962. (interactive)
  15963. (message "Saving all Org-mode buffers...")
  15964. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15965. (when (featurep 'org-id) (org-id-locations-save))
  15966. (message "Saving all Org-mode buffers... done"))
  15967. (defun org-revert-all-org-buffers ()
  15968. "Revert all Org-mode buffers.
  15969. Prompt for confirmation when there are unsaved changes.
  15970. Be sure you know what you are doing before letting this function
  15971. overwrite your changes.
  15972. This function is useful in a setup where one tracks org files
  15973. with a version control system, to revert on one machine after pulling
  15974. changes from another. I believe the procedure must be like this:
  15975. 1. M-x org-save-all-org-buffers
  15976. 2. Pull changes from the other machine, resolve conflicts
  15977. 3. M-x org-revert-all-org-buffers"
  15978. (interactive)
  15979. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15980. (error "Abort"))
  15981. (save-excursion
  15982. (save-window-excursion
  15983. (mapc
  15984. (lambda (b)
  15985. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15986. (with-current-buffer b buffer-file-name))
  15987. (org-pop-to-buffer-same-window b)
  15988. (revert-buffer t 'no-confirm)))
  15989. (buffer-list))
  15990. (when (and (featurep 'org-id) org-id-track-globally)
  15991. (org-id-locations-load)))))
  15992. ;;;; Agenda files
  15993. ;;;###autoload
  15994. (defun org-switchb (&optional arg)
  15995. "Switch between Org buffers.
  15996. With one prefix argument, restrict available buffers to files.
  15997. With two prefix arguments, restrict available buffers to agenda files.
  15998. Defaults to `iswitchb' for buffer name completion.
  15999. Set `org-completion-use-ido' to make it use ido instead."
  16000. (interactive "P")
  16001. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16002. ((equal arg '(16)) (org-buffer-list 'agenda))
  16003. (t (org-buffer-list))))
  16004. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16005. (org-completion-use-ido org-completion-use-ido))
  16006. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16007. (setq org-completion-use-iswitchb t))
  16008. (org-pop-to-buffer-same-window
  16009. (org-icompleting-read "Org buffer: "
  16010. (mapcar 'list (mapcar 'buffer-name blist))
  16011. nil t))))
  16012. ;;; Define some older names previously used for this functionality
  16013. ;;;###autoload
  16014. (defalias 'org-ido-switchb 'org-switchb)
  16015. ;;;###autoload
  16016. (defalias 'org-iswitchb 'org-switchb)
  16017. (defun org-buffer-list (&optional predicate exclude-tmp)
  16018. "Return a list of Org buffers.
  16019. PREDICATE can be `export', `files' or `agenda'.
  16020. export restrict the list to Export buffers.
  16021. files restrict the list to buffers visiting Org files.
  16022. agenda restrict the list to buffers visiting agenda files.
  16023. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16024. (let* ((bfn nil)
  16025. (agenda-files (and (eq predicate 'agenda)
  16026. (mapcar 'file-truename (org-agenda-files t))))
  16027. (filter
  16028. (cond
  16029. ((eq predicate 'files)
  16030. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16031. ((eq predicate 'export)
  16032. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16033. ((eq predicate 'agenda)
  16034. (lambda (b)
  16035. (with-current-buffer b
  16036. (and (derived-mode-p 'org-mode)
  16037. (setq bfn (buffer-file-name b))
  16038. (member (file-truename bfn) agenda-files)))))
  16039. (t (lambda (b) (with-current-buffer b
  16040. (or (derived-mode-p 'org-mode)
  16041. (string-match "\*Org .*Export"
  16042. (buffer-name b)))))))))
  16043. (delq nil
  16044. (mapcar
  16045. (lambda(b)
  16046. (if (and (funcall filter b)
  16047. (or (not exclude-tmp)
  16048. (not (string-match "tmp" (buffer-name b)))))
  16049. b
  16050. nil))
  16051. (buffer-list)))))
  16052. (defun org-agenda-files (&optional unrestricted archives)
  16053. "Get the list of agenda files.
  16054. Optional UNRESTRICTED means return the full list even if a restriction
  16055. is currently in place.
  16056. When ARCHIVES is t, include all archive files that are really being
  16057. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16058. `org-agenda-archives-mode' is t."
  16059. (let ((files
  16060. (cond
  16061. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16062. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16063. ((listp org-agenda-files) org-agenda-files)
  16064. (t (error "Invalid value of `org-agenda-files'")))))
  16065. (setq files (apply 'append
  16066. (mapcar (lambda (f)
  16067. (if (file-directory-p f)
  16068. (directory-files
  16069. f t org-agenda-file-regexp)
  16070. (list f)))
  16071. files)))
  16072. (when org-agenda-skip-unavailable-files
  16073. (setq files (delq nil
  16074. (mapcar (function
  16075. (lambda (file)
  16076. (and (file-readable-p file) file)))
  16077. files))))
  16078. (when (or (eq archives t)
  16079. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16080. (setq files (org-add-archive-files files)))
  16081. files))
  16082. (defun org-agenda-file-p (&optional file)
  16083. "Return non-nil, if FILE is an agenda file.
  16084. If FILE is omitted, use the file associated with the current
  16085. buffer."
  16086. (member (or file (buffer-file-name))
  16087. (org-agenda-files t)))
  16088. (defun org-edit-agenda-file-list ()
  16089. "Edit the list of agenda files.
  16090. Depending on setup, this either uses customize to edit the variable
  16091. `org-agenda-files', or it visits the file that is holding the list. In the
  16092. latter case, the buffer is set up in a way that saving it automatically kills
  16093. the buffer and restores the previous window configuration."
  16094. (interactive)
  16095. (if (stringp org-agenda-files)
  16096. (let ((cw (current-window-configuration)))
  16097. (find-file org-agenda-files)
  16098. (org-set-local 'org-window-configuration cw)
  16099. (org-add-hook 'after-save-hook
  16100. (lambda ()
  16101. (set-window-configuration
  16102. (prog1 org-window-configuration
  16103. (kill-buffer (current-buffer))))
  16104. (org-install-agenda-files-menu)
  16105. (message "New agenda file list installed"))
  16106. nil 'local)
  16107. (message "%s" (substitute-command-keys
  16108. "Edit list and finish with \\[save-buffer]")))
  16109. (customize-variable 'org-agenda-files)))
  16110. (defun org-store-new-agenda-file-list (list)
  16111. "Set new value for the agenda file list and save it correctly."
  16112. (if (stringp org-agenda-files)
  16113. (let ((fe (org-read-agenda-file-list t)) b u)
  16114. (while (setq b (find-buffer-visiting org-agenda-files))
  16115. (kill-buffer b))
  16116. (with-temp-file org-agenda-files
  16117. (insert
  16118. (mapconcat
  16119. (lambda (f) ;; Keep un-expanded entries.
  16120. (if (setq u (assoc f fe))
  16121. (cdr u)
  16122. f))
  16123. list "\n")
  16124. "\n")))
  16125. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16126. (org-insert-mode-line-in-empty-file nil))
  16127. (setq org-agenda-files list)
  16128. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16129. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16130. "Read the list of agenda files from a file.
  16131. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16132. filenames, used by `org-store-new-agenda-file-list' to write back
  16133. un-expanded file names."
  16134. (when (file-directory-p org-agenda-files)
  16135. (error "`org-agenda-files' cannot be a single directory"))
  16136. (when (stringp org-agenda-files)
  16137. (with-temp-buffer
  16138. (insert-file-contents org-agenda-files)
  16139. (mapcar
  16140. (lambda (f)
  16141. (let ((e (expand-file-name (substitute-in-file-name f)
  16142. org-directory)))
  16143. (if pair-with-expansion
  16144. (cons e f)
  16145. e)))
  16146. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16147. ;;;###autoload
  16148. (defun org-cycle-agenda-files ()
  16149. "Cycle through the files in `org-agenda-files'.
  16150. If the current buffer visits an agenda file, find the next one in the list.
  16151. If the current buffer does not, find the first agenda file."
  16152. (interactive)
  16153. (let* ((fs (org-agenda-files t))
  16154. (files (append fs (list (car fs))))
  16155. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16156. file)
  16157. (unless files (error "No agenda files"))
  16158. (catch 'exit
  16159. (while (setq file (pop files))
  16160. (if (equal (file-truename file) tcf)
  16161. (when (car files)
  16162. (find-file (car files))
  16163. (throw 'exit t))))
  16164. (find-file (car fs)))
  16165. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16166. (defun org-agenda-file-to-front (&optional to-end)
  16167. "Move/add the current file to the top of the agenda file list.
  16168. If the file is not present in the list, it is added to the front. If it is
  16169. present, it is moved there. With optional argument TO-END, add/move to the
  16170. end of the list."
  16171. (interactive "P")
  16172. (let ((org-agenda-skip-unavailable-files nil)
  16173. (file-alist (mapcar (lambda (x)
  16174. (cons (file-truename x) x))
  16175. (org-agenda-files t)))
  16176. (ctf (file-truename
  16177. (or buffer-file-name
  16178. (error "Please save the current buffer to a file"))))
  16179. x had)
  16180. (setq x (assoc ctf file-alist) had x)
  16181. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16182. (if to-end
  16183. (setq file-alist (append (delq x file-alist) (list x)))
  16184. (setq file-alist (cons x (delq x file-alist))))
  16185. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16186. (org-install-agenda-files-menu)
  16187. (message "File %s to %s of agenda file list"
  16188. (if had "moved" "added") (if to-end "end" "front"))))
  16189. (defun org-remove-file (&optional file)
  16190. "Remove current file from the list of files in variable `org-agenda-files'.
  16191. These are the files which are being checked for agenda entries.
  16192. Optional argument FILE means use this file instead of the current."
  16193. (interactive)
  16194. (let* ((org-agenda-skip-unavailable-files nil)
  16195. (file (or file buffer-file-name
  16196. (error "Current buffer does not visit a file")))
  16197. (true-file (file-truename file))
  16198. (afile (abbreviate-file-name file))
  16199. (files (delq nil (mapcar
  16200. (lambda (x)
  16201. (if (equal true-file
  16202. (file-truename x))
  16203. nil x))
  16204. (org-agenda-files t)))))
  16205. (if (not (= (length files) (length (org-agenda-files t))))
  16206. (progn
  16207. (org-store-new-agenda-file-list files)
  16208. (org-install-agenda-files-menu)
  16209. (message "Removed file: %s" afile))
  16210. (message "File was not in list: %s (not removed)" afile))))
  16211. (defun org-file-menu-entry (file)
  16212. (vector file (list 'find-file file) t))
  16213. (defun org-check-agenda-file (file)
  16214. "Make sure FILE exists. If not, ask user what to do."
  16215. (when (not (file-exists-p file))
  16216. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16217. (abbreviate-file-name file))
  16218. (let ((r (downcase (read-char-exclusive))))
  16219. (cond
  16220. ((equal r ?r)
  16221. (org-remove-file file)
  16222. (throw 'nextfile t))
  16223. (t (error "Abort"))))))
  16224. (defun org-get-agenda-file-buffer (file)
  16225. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16226. it to the list of buffers which might be released later."
  16227. (let ((buf (org-find-base-buffer-visiting file)))
  16228. (if buf
  16229. buf ; just return it
  16230. ;; Make a new buffer and remember it
  16231. (setq buf (find-file-noselect file))
  16232. (if buf (push buf org-agenda-new-buffers))
  16233. buf)))
  16234. (defun org-release-buffers (blist)
  16235. "Release all buffers in list, asking the user for confirmation when needed.
  16236. When a buffer is unmodified, it is just killed. When modified, it is saved
  16237. \(if the user agrees) and then killed."
  16238. (let (buf file)
  16239. (while (setq buf (pop blist))
  16240. (setq file (buffer-file-name buf))
  16241. (when (and (buffer-modified-p buf)
  16242. file
  16243. (y-or-n-p (format "Save file %s? " file)))
  16244. (with-current-buffer buf (save-buffer)))
  16245. (kill-buffer buf))))
  16246. (defun org-agenda-prepare-buffers (files)
  16247. "Create buffers for all agenda files, protect archived trees and comments."
  16248. (interactive)
  16249. (let ((pa '(:org-archived t))
  16250. (pc '(:org-comment t))
  16251. (pall '(:org-archived t :org-comment t))
  16252. (inhibit-read-only t)
  16253. (org-inhibit-startup org-agenda-inhibit-startup)
  16254. (rea (concat ":" org-archive-tag ":"))
  16255. file re)
  16256. (save-excursion
  16257. (save-restriction
  16258. (while (setq file (pop files))
  16259. (catch 'nextfile
  16260. (if (bufferp file)
  16261. (set-buffer file)
  16262. (org-check-agenda-file file)
  16263. (set-buffer (org-get-agenda-file-buffer file)))
  16264. (widen)
  16265. (org-refresh-category-properties)
  16266. (org-refresh-properties org-effort-property 'org-effort)
  16267. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16268. (setq org-todo-keywords-for-agenda
  16269. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16270. (setq org-done-keywords-for-agenda
  16271. (append org-done-keywords-for-agenda org-done-keywords))
  16272. (setq org-todo-keyword-alist-for-agenda
  16273. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16274. (setq org-drawers-for-agenda
  16275. (append org-drawers-for-agenda org-drawers))
  16276. (setq org-tag-alist-for-agenda
  16277. (append org-tag-alist-for-agenda org-tag-alist))
  16278. (org-with-silent-modifications
  16279. (save-excursion
  16280. (remove-text-properties (point-min) (point-max) pall)
  16281. (when org-agenda-skip-archived-trees
  16282. (goto-char (point-min))
  16283. (while (re-search-forward rea nil t)
  16284. (if (org-at-heading-p t)
  16285. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16286. (goto-char (point-min))
  16287. (setq re (format org-heading-keyword-regexp-format
  16288. org-comment-string))
  16289. (while (re-search-forward re nil t)
  16290. (add-text-properties
  16291. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16292. (setq org-todo-keywords-for-agenda
  16293. (org-uniquify org-todo-keywords-for-agenda))
  16294. (setq org-todo-keyword-alist-for-agenda
  16295. (org-uniquify org-todo-keyword-alist-for-agenda)
  16296. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16297. ;;;; CDLaTeX minor mode
  16298. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16299. "Keymap for the minor `org-cdlatex-mode'.")
  16300. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16301. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16302. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16303. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16304. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16305. (defvar org-cdlatex-texmathp-advice-is-done nil
  16306. "Flag remembering if we have applied the advice to texmathp already.")
  16307. (define-minor-mode org-cdlatex-mode
  16308. "Toggle the minor `org-cdlatex-mode'.
  16309. This mode supports entering LaTeX environment and math in LaTeX fragments
  16310. in Org-mode.
  16311. \\{org-cdlatex-mode-map}"
  16312. nil " OCDL" nil
  16313. (when org-cdlatex-mode
  16314. (require 'cdlatex)
  16315. (run-hooks 'cdlatex-mode-hook)
  16316. (cdlatex-compute-tables))
  16317. (unless org-cdlatex-texmathp-advice-is-done
  16318. (setq org-cdlatex-texmathp-advice-is-done t)
  16319. (defadvice texmathp (around org-math-always-on activate)
  16320. "Always return t in org-mode buffers.
  16321. This is because we want to insert math symbols without dollars even outside
  16322. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16323. an embedded LaTeX fragment, let texmathp do its job.
  16324. \\[org-cdlatex-mode-map]"
  16325. (interactive)
  16326. (let (p)
  16327. (cond
  16328. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16329. ((eq this-command 'cdlatex-math-symbol)
  16330. (setq ad-return-value t
  16331. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16332. (t
  16333. (let ((p (org-inside-LaTeX-fragment-p)))
  16334. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16335. (setq ad-return-value t
  16336. texmathp-why '("Org-mode embedded math" . 0))
  16337. (if p ad-do-it)))))))))
  16338. (defun turn-on-org-cdlatex ()
  16339. "Unconditionally turn on `org-cdlatex-mode'."
  16340. (org-cdlatex-mode 1))
  16341. (defun org-try-cdlatex-tab ()
  16342. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16343. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16344. - inside a LaTeX fragment, or
  16345. - after the first word in a line, where an abbreviation expansion could
  16346. insert a LaTeX environment."
  16347. (when org-cdlatex-mode
  16348. (cond
  16349. ;; Before any word on the line: No expansion possible.
  16350. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16351. ;; Just after first word on the line: Expand it. Make sure it
  16352. ;; cannot happen on headlines, though.
  16353. ((save-excursion
  16354. (skip-chars-backward "a-zA-Z0-9*")
  16355. (skip-chars-backward " \t")
  16356. (and (bolp) (not (org-at-heading-p))))
  16357. (cdlatex-tab) t)
  16358. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16359. (defun org-cdlatex-underscore-caret (&optional arg)
  16360. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16361. Revert to the normal definition outside of these fragments."
  16362. (interactive "P")
  16363. (if (org-inside-LaTeX-fragment-p)
  16364. (call-interactively 'cdlatex-sub-superscript)
  16365. (let (org-cdlatex-mode)
  16366. (call-interactively (key-binding (vector last-input-event))))))
  16367. (defun org-cdlatex-math-modify (&optional arg)
  16368. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16369. Revert to the normal definition outside of these fragments."
  16370. (interactive "P")
  16371. (if (org-inside-LaTeX-fragment-p)
  16372. (call-interactively 'cdlatex-math-modify)
  16373. (let (org-cdlatex-mode)
  16374. (call-interactively (key-binding (vector last-input-event))))))
  16375. ;;;; LaTeX fragments
  16376. (defvar org-latex-regexps
  16377. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16378. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16379. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16380. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16381. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16382. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16383. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16384. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16385. "Regular expressions for matching embedded LaTeX.")
  16386. (defun org-inside-LaTeX-fragment-p ()
  16387. "Test if point is inside a LaTeX fragment.
  16388. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16389. sequence appearing also before point.
  16390. Even though the matchers for math are configurable, this function assumes
  16391. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16392. delimiters are skipped when they have been removed by customization.
  16393. The return value is nil, or a cons cell with the delimiter and the
  16394. position of this delimiter.
  16395. This function does a reasonably good job, but can locally be fooled by
  16396. for example currency specifications. For example it will assume being in
  16397. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16398. fragments that are properly closed, but during editing, we have to live
  16399. with the uncertainty caused by missing closing delimiters. This function
  16400. looks only before point, not after."
  16401. (catch 'exit
  16402. (let ((pos (point))
  16403. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16404. (lim (progn
  16405. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16406. (point)))
  16407. dd-on str (start 0) m re)
  16408. (goto-char pos)
  16409. (when dodollar
  16410. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16411. re (nth 1 (assoc "$" org-latex-regexps)))
  16412. (while (string-match re str start)
  16413. (cond
  16414. ((= (match-end 0) (length str))
  16415. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16416. ((= (match-end 0) (- (length str) 5))
  16417. (throw 'exit nil))
  16418. (t (setq start (match-end 0))))))
  16419. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16420. (goto-char pos)
  16421. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16422. (and (match-beginning 2) (throw 'exit nil))
  16423. ;; count $$
  16424. (while (re-search-backward "\\$\\$" lim t)
  16425. (setq dd-on (not dd-on)))
  16426. (goto-char pos)
  16427. (if dd-on (cons "$$" m))))))
  16428. (defun org-inside-latex-macro-p ()
  16429. "Is point inside a LaTeX macro or its arguments?"
  16430. (save-match-data
  16431. (org-in-regexp
  16432. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16433. (defvar org-latex-fragment-image-overlays nil
  16434. "List of overlays carrying the images of latex fragments.")
  16435. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16436. (defun org-remove-latex-fragment-image-overlays ()
  16437. "Remove all overlays with LaTeX fragment images in current buffer."
  16438. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16439. (setq org-latex-fragment-image-overlays nil))
  16440. (defun org-preview-latex-fragment (&optional subtree)
  16441. "Preview the LaTeX fragment at point, or all locally or globally.
  16442. If the cursor is in a LaTeX fragment, create the image and overlay
  16443. it over the source code. If there is no fragment at point, display
  16444. all fragments in the current text, from one headline to the next. With
  16445. prefix SUBTREE, display all fragments in the current subtree. With a
  16446. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16447. the cursor is before the first headline,
  16448. display all fragments in the buffer.
  16449. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16450. (interactive "P")
  16451. (unless buffer-file-name
  16452. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16453. (org-remove-latex-fragment-image-overlays)
  16454. (save-excursion
  16455. (save-restriction
  16456. (let (beg end at msg)
  16457. (cond
  16458. ((or (equal subtree '(16))
  16459. (not (save-excursion
  16460. (re-search-backward org-outline-regexp-bol nil t))))
  16461. (setq beg (point-min) end (point-max)
  16462. msg "Creating images for buffer...%s"))
  16463. ((equal subtree '(4))
  16464. (org-back-to-heading)
  16465. (setq beg (point) end (org-end-of-subtree t)
  16466. msg "Creating images for subtree...%s"))
  16467. (t
  16468. (if (setq at (org-inside-LaTeX-fragment-p))
  16469. (goto-char (max (point-min) (- (cdr at) 2)))
  16470. (org-back-to-heading))
  16471. (setq beg (point) end (progn (outline-next-heading) (point))
  16472. msg (if at "Creating image...%s"
  16473. "Creating images for entry...%s"))))
  16474. (message msg "")
  16475. (narrow-to-region beg end)
  16476. (goto-char beg)
  16477. (org-format-latex
  16478. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16479. (file-name-nondirectory
  16480. buffer-file-name)))
  16481. default-directory 'overlays msg at 'forbuffer
  16482. org-latex-create-formula-image-program)
  16483. (message msg "done. Use `C-c C-c' to remove images.")))))
  16484. (defun org-format-latex (prefix &optional dir overlays msg at
  16485. forbuffer processing-type)
  16486. "Replace LaTeX fragments with links to an image, and produce images.
  16487. Some of the options can be changed using the variable
  16488. `org-format-latex-options'."
  16489. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16490. (let* ((prefixnodir (file-name-nondirectory prefix))
  16491. (absprefix (expand-file-name prefix dir))
  16492. (todir (file-name-directory absprefix))
  16493. (opt org-format-latex-options)
  16494. (optnew org-format-latex-options)
  16495. (matchers (plist-get opt :matchers))
  16496. (re-list org-latex-regexps)
  16497. (cnt 0) txt hash link beg end re e checkdir
  16498. string
  16499. m n block-type block linkfile movefile ov)
  16500. ;; Check the different regular expressions
  16501. (while (setq e (pop re-list))
  16502. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16503. block (if block-type "\n\n" ""))
  16504. (when (member m matchers)
  16505. (goto-char (point-min))
  16506. (while (re-search-forward re nil t)
  16507. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16508. (or (not overlays)
  16509. (not (eq (get-char-property (match-beginning n)
  16510. 'org-overlay-type)
  16511. 'org-latex-overlay))))
  16512. (cond
  16513. ((eq processing-type 'verbatim))
  16514. ((eq processing-type 'mathjax)
  16515. ;; Prepare for MathJax processing.
  16516. (setq string (match-string n))
  16517. (when (member m '("$" "$1"))
  16518. (save-excursion
  16519. (delete-region (match-beginning n) (match-end n))
  16520. (goto-char (match-beginning n))
  16521. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16522. ((or (eq processing-type 'dvipng)
  16523. (eq processing-type 'imagemagick))
  16524. ;; Process to an image.
  16525. (setq txt (match-string n)
  16526. beg (match-beginning n) end (match-end n)
  16527. cnt (1+ cnt))
  16528. (let ((face (face-at-point))
  16529. (fg (plist-get opt :foreground))
  16530. (bg (plist-get opt :background))
  16531. ;; Ensure full list is printed.
  16532. print-length print-level)
  16533. (when forbuffer
  16534. ;; Get the colors from the face at point.
  16535. (goto-char beg)
  16536. (when (eq fg 'auto)
  16537. (setq fg (face-attribute face :foreground nil 'default)))
  16538. (when (eq bg 'auto)
  16539. (setq bg (face-attribute face :background nil 'default)))
  16540. (setq optnew (copy-sequence opt))
  16541. (plist-put optnew :foreground fg)
  16542. (plist-put optnew :background bg))
  16543. (setq hash (sha1 (prin1-to-string
  16544. (list org-format-latex-header
  16545. org-latex-default-packages-alist
  16546. org-latex-packages-alist
  16547. org-format-latex-options
  16548. forbuffer txt fg bg)))
  16549. linkfile (format "%s_%s.png" prefix hash)
  16550. movefile (format "%s_%s.png" absprefix hash)))
  16551. (setq link (concat block "[[file:" linkfile "]]" block))
  16552. (if msg (message msg cnt))
  16553. (goto-char beg)
  16554. (unless checkdir ; Ensure the directory exists.
  16555. (setq checkdir t)
  16556. (or (file-directory-p todir) (make-directory todir t)))
  16557. (unless (file-exists-p movefile)
  16558. (org-create-formula-image
  16559. txt movefile optnew forbuffer processing-type))
  16560. (if overlays
  16561. (progn
  16562. (mapc (lambda (o)
  16563. (if (eq (overlay-get o 'org-overlay-type)
  16564. 'org-latex-overlay)
  16565. (delete-overlay o)))
  16566. (overlays-in beg end))
  16567. (setq ov (make-overlay beg end))
  16568. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16569. (if (featurep 'xemacs)
  16570. (progn
  16571. (overlay-put ov 'invisible t)
  16572. (overlay-put
  16573. ov 'end-glyph
  16574. (make-glyph (vector 'png :file movefile))))
  16575. (overlay-put
  16576. ov 'display
  16577. (list 'image :type 'png :file movefile :ascent 'center)))
  16578. (push ov org-latex-fragment-image-overlays)
  16579. (goto-char end))
  16580. (delete-region beg end)
  16581. (insert (org-add-props link
  16582. (list 'org-latex-src
  16583. (replace-regexp-in-string
  16584. "\"" "" txt)
  16585. 'org-latex-src-embed-type
  16586. (if block-type 'paragraph 'character))))))
  16587. ((eq processing-type 'mathml)
  16588. ;; Process to MathML
  16589. (unless (save-match-data (org-format-latex-mathml-available-p))
  16590. (error "LaTeX to MathML converter not configured"))
  16591. (setq txt (match-string n)
  16592. beg (match-beginning n) end (match-end n)
  16593. cnt (1+ cnt))
  16594. (if msg (message msg cnt))
  16595. (goto-char beg)
  16596. (delete-region beg end)
  16597. (insert (org-format-latex-as-mathml
  16598. txt block-type prefix dir)))
  16599. (t
  16600. (error "Unknown conversion type %s for latex fragments"
  16601. processing-type)))))))))
  16602. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16603. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16604. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16605. sucessful, return the portion between \"<math...> </math>\"
  16606. elements otherwise return nil. When MATHML-FILE is specified,
  16607. write the results in to that file. When invoked as an
  16608. interactive command, prompt for LATEX-FRAG, with initial value
  16609. set to the current active region and echo the results for user
  16610. inspection."
  16611. (interactive (list (let ((frag (when (org-region-active-p)
  16612. (buffer-substring-no-properties
  16613. (region-beginning) (region-end)))))
  16614. (read-string "LaTeX Fragment: " frag nil frag))))
  16615. (unless latex-frag (error "Invalid latex-frag"))
  16616. (let* ((tmp-in-file (file-relative-name
  16617. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16618. (ignore (write-region latex-frag nil tmp-in-file))
  16619. (tmp-out-file (file-relative-name
  16620. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16621. (cmd (format-spec
  16622. org-latex-to-mathml-convert-command
  16623. `((?j . ,(shell-quote-argument
  16624. (expand-file-name org-latex-to-mathml-jar-file)))
  16625. (?I . ,(shell-quote-argument tmp-in-file))
  16626. (?o . ,(shell-quote-argument tmp-out-file)))))
  16627. mathml shell-command-output)
  16628. (when (org-called-interactively-p 'any)
  16629. (unless (org-format-latex-mathml-available-p)
  16630. (error "LaTeX to MathML converter not configured")))
  16631. (message "Running %s" cmd)
  16632. (setq shell-command-output (shell-command-to-string cmd))
  16633. (setq mathml
  16634. (when (file-readable-p tmp-out-file)
  16635. (with-current-buffer (find-file-noselect tmp-out-file t)
  16636. (goto-char (point-min))
  16637. (when (re-search-forward
  16638. (concat
  16639. (regexp-quote
  16640. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16641. "\\(.\\|\n\\)*"
  16642. (regexp-quote "</math>")) nil t)
  16643. (prog1 (match-string 0) (kill-buffer))))))
  16644. (cond
  16645. (mathml
  16646. (setq mathml
  16647. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16648. (when mathml-file
  16649. (write-region mathml nil mathml-file))
  16650. (when (org-called-interactively-p 'any)
  16651. (message mathml)))
  16652. ((message "LaTeX to MathML conversion failed")
  16653. (message shell-command-output)))
  16654. (delete-file tmp-in-file)
  16655. (when (file-exists-p tmp-out-file)
  16656. (delete-file tmp-out-file))
  16657. mathml))
  16658. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16659. prefix &optional dir)
  16660. "Use `org-create-math-formula' but check local cache first."
  16661. (let* ((absprefix (expand-file-name prefix dir))
  16662. (print-length nil) (print-level nil)
  16663. (formula-id (concat
  16664. "formula-"
  16665. (sha1
  16666. (prin1-to-string
  16667. (list latex-frag
  16668. org-latex-to-mathml-convert-command)))))
  16669. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16670. (formula-cache-dir (file-name-directory formula-cache)))
  16671. (unless (file-directory-p formula-cache-dir)
  16672. (make-directory formula-cache-dir t))
  16673. (unless (file-exists-p formula-cache)
  16674. (org-create-math-formula latex-frag formula-cache))
  16675. (if (file-exists-p formula-cache)
  16676. ;; Successful conversion. Return the link to MathML file.
  16677. (org-add-props
  16678. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16679. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16680. 'org-latex-src-embed-type (if latex-frag-type
  16681. 'paragraph 'character)))
  16682. ;; Failed conversion. Return the LaTeX fragment verbatim
  16683. latex-frag)))
  16684. (defun org-create-formula-image (string tofile options buffer &optional type)
  16685. "Create an image from LaTeX source using dvipng or convert.
  16686. This function calls either `org-create-formula-image-with-dvipng'
  16687. or `org-create-formula-image-with-imagemagick' depending on the
  16688. value of `org-latex-create-formula-image-program' or on the value
  16689. of the optional TYPE variable.
  16690. Note: ultimately these two function should be combined as they
  16691. share a good deal of logic."
  16692. (org-check-external-command
  16693. "latex" "needed to convert LaTeX fragments to images")
  16694. (funcall
  16695. (case (or type org-latex-create-formula-image-program)
  16696. ('dvipng
  16697. (org-check-external-command
  16698. "dvipng" "needed to convert LaTeX fragments to images")
  16699. #'org-create-formula-image-with-dvipng)
  16700. ('imagemagick
  16701. (org-check-external-command
  16702. "convert" "you need to install imagemagick")
  16703. #'org-create-formula-image-with-imagemagick)
  16704. (t (error
  16705. "invalid value of `org-latex-create-formula-image-program'")))
  16706. string tofile options buffer))
  16707. (declare-function org-export--get-global-options "ox" (&optional backend))
  16708. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16709. (defun org-create-formula--latex-header ()
  16710. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16711. (org-latex-guess-inputenc
  16712. (org-splice-latex-header
  16713. org-format-latex-header
  16714. org-latex-default-packages-alist
  16715. org-latex-packages-alist t
  16716. (plist-get
  16717. (org-combine-plists
  16718. (org-export--get-global-options 'latex)
  16719. (org-export--get-inbuffer-options 'latex))
  16720. :latex-header))))
  16721. ;; This function borrows from Ganesh Swami's latex2png.el
  16722. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16723. "This calls dvipng."
  16724. (require 'ox-latex)
  16725. (let* ((tmpdir (if (featurep 'xemacs)
  16726. (temp-directory)
  16727. temporary-file-directory))
  16728. (texfilebase (make-temp-name
  16729. (expand-file-name "orgtex" tmpdir)))
  16730. (texfile (concat texfilebase ".tex"))
  16731. (dvifile (concat texfilebase ".dvi"))
  16732. (pngfile (concat texfilebase ".png"))
  16733. (fnh (if (featurep 'xemacs)
  16734. (font-height (face-font 'default))
  16735. (face-attribute 'default :height nil)))
  16736. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16737. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16738. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16739. "Black"))
  16740. (bg (or (plist-get options (if buffer :background :html-background))
  16741. "Transparent")))
  16742. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16743. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16744. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16745. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16746. (let ((latex-header (org-create-formula--latex-header)))
  16747. (with-temp-file texfile
  16748. (insert latex-header)
  16749. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16750. (let ((dir default-directory))
  16751. (condition-case nil
  16752. (progn
  16753. (cd tmpdir)
  16754. (call-process "latex" nil nil nil texfile))
  16755. (error nil))
  16756. (cd dir))
  16757. (if (not (file-exists-p dvifile))
  16758. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16759. (condition-case nil
  16760. (if (featurep 'xemacs)
  16761. (call-process "dvipng" nil nil nil
  16762. "-fg" fg "-bg" bg
  16763. "-T" "tight"
  16764. "-o" pngfile
  16765. dvifile)
  16766. (call-process "dvipng" nil nil nil
  16767. "-fg" fg "-bg" bg
  16768. "-D" dpi
  16769. ;;"-x" scale "-y" scale
  16770. "-T" "tight"
  16771. "-o" pngfile
  16772. dvifile))
  16773. (error nil))
  16774. (if (not (file-exists-p pngfile))
  16775. (if org-format-latex-signal-error
  16776. (error "Failed to create png file from %s" texfile)
  16777. (message "Failed to create png file from %s" texfile)
  16778. nil)
  16779. ;; Use the requested file name and clean up
  16780. (copy-file pngfile tofile 'replace)
  16781. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16782. (if (file-exists-p (concat texfilebase e))
  16783. (delete-file (concat texfilebase e))))
  16784. pngfile))))
  16785. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  16786. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16787. "This calls convert, which is included into imagemagick."
  16788. (require 'ox-latex)
  16789. (let* ((tmpdir (if (featurep 'xemacs)
  16790. (temp-directory)
  16791. temporary-file-directory))
  16792. (texfilebase (make-temp-name
  16793. (expand-file-name "orgtex" tmpdir)))
  16794. (texfile (concat texfilebase ".tex"))
  16795. (pdffile (concat texfilebase ".pdf"))
  16796. (pngfile (concat texfilebase ".png"))
  16797. (fnh (if (featurep 'xemacs)
  16798. (font-height (face-font 'default))
  16799. (face-attribute 'default :height nil)))
  16800. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16801. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16802. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16803. "black"))
  16804. (bg (or (plist-get options (if buffer :background :html-background))
  16805. "white")))
  16806. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16807. (setq fg (org-latex-color-format fg)))
  16808. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16809. (setq bg (org-latex-color-format
  16810. (if (string= bg "Transparent") "white" bg))))
  16811. (let ((latex-header (org-create-formula--latex-header)))
  16812. (with-temp-file texfile
  16813. (insert latex-header)
  16814. (insert "\n\\begin{document}\n"
  16815. "\\definecolor{fg}{rgb}{" fg "}\n"
  16816. "\\definecolor{bg}{rgb}{" bg "}\n"
  16817. "\n\\pagecolor{bg}\n"
  16818. "\n{\\color{fg}\n"
  16819. string
  16820. "\n}\n"
  16821. "\n\\end{document}\n")))
  16822. (org-latex-compile texfile t)
  16823. (if (not (file-exists-p pdffile))
  16824. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16825. (condition-case nil
  16826. (if (featurep 'xemacs)
  16827. (call-process "convert" nil nil nil
  16828. "-density" "96"
  16829. "-trim"
  16830. "-antialias"
  16831. pdffile
  16832. "-quality" "100"
  16833. ;; "-sharpen" "0x1.0"
  16834. pngfile)
  16835. (call-process "convert" nil nil nil
  16836. "-density" dpi
  16837. "-trim"
  16838. "-antialias"
  16839. pdffile
  16840. "-quality" "100"
  16841. ;; "-sharpen" "0x1.0"
  16842. pngfile))
  16843. (error nil))
  16844. (if (not (file-exists-p pngfile))
  16845. (if org-format-latex-signal-error
  16846. (error "Failed to create png file from %s" texfile)
  16847. (message "Failed to create png file from %s" texfile)
  16848. nil)
  16849. ;; Use the requested file name and clean up
  16850. (copy-file pngfile tofile 'replace)
  16851. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16852. (if (file-exists-p (concat texfilebase e))
  16853. (delete-file (concat texfilebase e))))
  16854. pngfile))))
  16855. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16856. "Fill a LaTeX header template TPL.
  16857. In the template, the following place holders will be recognized:
  16858. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16859. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16860. [PACKAGES] \\usepackage statements for PKG
  16861. [NO-PACKAGES] do not include PKG
  16862. [EXTRA] the string EXTRA
  16863. [NO-EXTRA] do not include EXTRA
  16864. For backward compatibility, if both the positive and the negative place
  16865. holder is missing, the positive one (without the \"NO-\") will be
  16866. assumed to be present at the end of the template.
  16867. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16868. EXTRA is a string.
  16869. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16870. (let (rpl (end ""))
  16871. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16872. (setq rpl (if (or (match-end 1) (not def-pkg))
  16873. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16874. tpl (replace-match rpl t t tpl))
  16875. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16876. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16877. (setq rpl (if (or (match-end 1) (not pkg))
  16878. "" (org-latex-packages-to-string pkg snippets-p t))
  16879. tpl (replace-match rpl t t tpl))
  16880. (if pkg (setq end
  16881. (concat end "\n"
  16882. (org-latex-packages-to-string pkg snippets-p)))))
  16883. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16884. (setq rpl (if (or (match-end 1) (not extra))
  16885. "" (concat extra "\n"))
  16886. tpl (replace-match rpl t t tpl))
  16887. (if (and extra (string-match "\\S-" extra))
  16888. (setq end (concat end "\n" extra))))
  16889. (if (string-match "\\S-" end)
  16890. (concat tpl "\n" end)
  16891. tpl)))
  16892. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16893. "Turn an alist of packages into a string with the \\usepackage macros."
  16894. (setq pkg (mapconcat (lambda(p)
  16895. (cond
  16896. ((stringp p) p)
  16897. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16898. (format "%% Package %s omitted" (cadr p)))
  16899. ((equal "" (car p))
  16900. (format "\\usepackage{%s}" (cadr p)))
  16901. (t
  16902. (format "\\usepackage[%s]{%s}"
  16903. (car p) (cadr p)))))
  16904. pkg
  16905. "\n"))
  16906. (if newline (concat pkg "\n") pkg))
  16907. (defun org-dvipng-color (attr)
  16908. "Return a RGB color specification for dvipng."
  16909. (apply 'format "rgb %s %s %s"
  16910. (mapcar 'org-normalize-color
  16911. (if (featurep 'xemacs)
  16912. (color-rgb-components
  16913. (face-property 'default
  16914. (cond ((eq attr :foreground) 'foreground)
  16915. ((eq attr :background) 'background))))
  16916. (color-values (face-attribute 'default attr nil))))))
  16917. (defun org-dvipng-color-format (color-name)
  16918. "Convert COLOR-NAME to a RGB color value for dvipng."
  16919. (apply 'format "rgb %s %s %s"
  16920. (mapcar 'org-normalize-color
  16921. (color-values color-name))))
  16922. (defun org-latex-color (attr)
  16923. "Return a RGB color for the LaTeX color package."
  16924. (apply 'format "%s,%s,%s"
  16925. (mapcar 'org-normalize-color
  16926. (if (featurep 'xemacs)
  16927. (color-rgb-components
  16928. (face-property 'default
  16929. (cond ((eq attr :foreground) 'foreground)
  16930. ((eq attr :background) 'background))))
  16931. (color-values (face-attribute 'default attr nil))))))
  16932. (defun org-latex-color-format (color-name)
  16933. "Convert COLOR-NAME to a RGB color value."
  16934. (apply 'format "%s,%s,%s"
  16935. (mapcar 'org-normalize-color
  16936. (color-values color-name))))
  16937. (defun org-normalize-color (value)
  16938. "Return string to be used as color value for an RGB component."
  16939. (format "%g" (/ value 65535.0)))
  16940. ;; Image display
  16941. (defvar org-inline-image-overlays nil)
  16942. (make-variable-buffer-local 'org-inline-image-overlays)
  16943. (defun org-toggle-inline-images (&optional include-linked)
  16944. "Toggle the display of inline images.
  16945. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16946. (interactive "P")
  16947. (if org-inline-image-overlays
  16948. (progn
  16949. (org-remove-inline-images)
  16950. (message "Inline image display turned off"))
  16951. (org-display-inline-images include-linked)
  16952. (if (and (org-called-interactively-p)
  16953. org-inline-image-overlays)
  16954. (message "%d images displayed inline"
  16955. (length org-inline-image-overlays))
  16956. (message "No images to display inline"))))
  16957. (defun org-redisplay-inline-images ()
  16958. "Refresh the display of inline images."
  16959. (interactive)
  16960. (if (not org-inline-image-overlays)
  16961. (org-toggle-inline-images)
  16962. (org-toggle-inline-images)
  16963. (org-toggle-inline-images)))
  16964. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16965. "Display inline images.
  16966. Normally only links without a description part are inlined, because this
  16967. is how it will work for export. When INCLUDE-LINKED is set, also links
  16968. with a description part will be inlined. This can be nice for a quick
  16969. look at those images, but it does not reflect what exported files will look
  16970. like.
  16971. When REFRESH is set, refresh existing images between BEG and END.
  16972. This will create new image displays only if necessary.
  16973. BEG and END default to the buffer boundaries."
  16974. (interactive "P")
  16975. (unless refresh
  16976. (org-remove-inline-images)
  16977. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16978. (save-excursion
  16979. (save-restriction
  16980. (widen)
  16981. (setq beg (or beg (point-min)) end (or end (point-max)))
  16982. (goto-char beg)
  16983. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16984. (substring (org-image-file-name-regexp) 0 -2)
  16985. "\\)\\]" (if include-linked "" "\\]")))
  16986. old file ov img type attrwidth width)
  16987. (while (re-search-forward re end t)
  16988. (setq old (get-char-property-and-overlay (match-beginning 1)
  16989. 'org-image-overlay)
  16990. file (expand-file-name
  16991. (concat (or (match-string 3) "") (match-string 4))))
  16992. (when (image-type-available-p 'imagemagick)
  16993. (setq attrwidth (if (or (listp org-image-actual-width)
  16994. (null org-image-actual-width))
  16995. (save-excursion
  16996. (save-match-data
  16997. (when (re-search-backward
  16998. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16999. (save-excursion
  17000. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17001. (string-to-number (match-string 1))))))
  17002. width (cond ((eq org-image-actual-width t) nil)
  17003. ((null org-image-actual-width) attrwidth)
  17004. ((numberp org-image-actual-width)
  17005. org-image-actual-width)
  17006. ((listp org-image-actual-width)
  17007. (or attrwidth (car org-image-actual-width))))
  17008. type (if width 'imagemagick)))
  17009. (when (file-exists-p file)
  17010. (if (and (car-safe old) refresh)
  17011. (image-refresh (overlay-get (cdr old) 'display))
  17012. (setq img (save-match-data (create-image file type nil :width width)))
  17013. (when img
  17014. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17015. (overlay-put ov 'display img)
  17016. (overlay-put ov 'face 'default)
  17017. (overlay-put ov 'org-image-overlay t)
  17018. (overlay-put ov 'modification-hooks
  17019. (list 'org-display-inline-remove-overlay))
  17020. (push ov org-inline-image-overlays)))))))))
  17021. (define-obsolete-function-alias
  17022. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17023. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17024. "Remove inline-display overlay if a corresponding region is modified."
  17025. (let ((inhibit-modification-hooks t))
  17026. (when (and ov after)
  17027. (delete ov org-inline-image-overlays)
  17028. (delete-overlay ov))))
  17029. (defun org-remove-inline-images ()
  17030. "Remove inline display of images."
  17031. (interactive)
  17032. (mapc 'delete-overlay org-inline-image-overlays)
  17033. (setq org-inline-image-overlays nil))
  17034. ;;;; Key bindings
  17035. ;; Outline functions from `outline-mode-prefix-map'
  17036. ;; that can be remapped in Org:
  17037. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17038. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17039. (define-key org-mode-map [remap outline-forward-same-level]
  17040. 'org-forward-heading-same-level)
  17041. (define-key org-mode-map [remap outline-backward-same-level]
  17042. 'org-backward-heading-same-level)
  17043. (define-key org-mode-map [remap show-branches]
  17044. 'org-kill-note-or-show-branches)
  17045. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17046. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17047. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17048. ;; Outline functions from `outline-mode-prefix-map' that can not
  17049. ;; be remapped in Org:
  17050. ;;
  17051. ;; - the column "key binding" shows whether the Outline function is still
  17052. ;; available in Org mode on the same key that it has been bound to in
  17053. ;; Outline mode:
  17054. ;; - "overridden": key used for a different functionality in Org mode
  17055. ;; - else: key still bound to the same Outline function in Org mode
  17056. ;;
  17057. ;; | Outline function | key binding | Org replacement |
  17058. ;; |------------------------------------+-------------+-----------------------|
  17059. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17060. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17061. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17062. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17063. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17064. ;; | `show-entry' | overridden | no replacement |
  17065. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17066. ;; | `show-branches' | `C-c C-k' | still same function |
  17067. ;; | `show-subtree' | overridden | visibility cycling |
  17068. ;; | `show-all' | overridden | no replacement |
  17069. ;; | `hide-subtree' | overridden | visibility cycling |
  17070. ;; | `hide-body' | overridden | no replacement |
  17071. ;; | `hide-entry' | overridden | visibility cycling |
  17072. ;; | `hide-leaves' | overridden | no replacement |
  17073. ;; | `hide-sublevels' | overridden | no replacement |
  17074. ;; | `hide-other' | overridden | no replacement |
  17075. ;; Make `C-c C-x' a prefix key
  17076. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17077. ;; TAB key with modifiers
  17078. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17079. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17080. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17081. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17082. ;; The following line is necessary under Suse GNU/Linux
  17083. (unless (featurep 'xemacs)
  17084. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17085. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17086. (define-key org-mode-map [backtab] 'org-shifttab)
  17087. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17088. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17089. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17090. ;; Cursor keys with modifiers
  17091. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17092. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17093. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17094. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17095. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17096. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17097. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17098. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17099. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17100. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17101. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17102. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17103. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17104. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17105. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17106. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17107. ;; Babel keys
  17108. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17109. (mapc (lambda (pair)
  17110. (define-key org-babel-map (car pair) (cdr pair)))
  17111. org-babel-key-bindings)
  17112. ;;; Extra keys for tty access.
  17113. ;; We only set them when really needed because otherwise the
  17114. ;; menus don't show the simple keys
  17115. (when (or org-use-extra-keys
  17116. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17117. (not window-system))
  17118. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17119. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17120. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17121. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17122. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17123. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17124. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17125. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17126. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17127. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17128. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17129. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17130. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17131. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17132. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17133. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17134. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17135. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17136. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17137. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17138. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17139. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17140. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17141. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17142. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17143. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17144. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17145. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17146. ;; All the other keys
  17147. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17148. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17149. (if (boundp 'narrow-map)
  17150. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17151. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17152. (if (boundp 'narrow-map)
  17153. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17154. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17155. (if (boundp 'narrow-map)
  17156. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17157. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17158. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17159. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17160. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17161. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17162. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17163. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17164. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17165. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17166. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17167. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17168. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17169. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17170. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17171. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17172. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17173. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17174. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17175. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17176. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17177. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17178. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17179. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17180. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17181. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17182. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17183. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17184. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17185. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17186. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17187. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17188. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17189. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17190. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17191. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17192. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17193. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17194. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17195. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17196. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17197. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17198. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17199. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17200. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17201. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17202. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17203. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17204. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17205. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17206. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17207. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17208. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17209. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17210. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17211. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17212. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17213. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17214. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17215. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17216. (org-defkey org-mode-map "\C-m" 'org-return)
  17217. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17218. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17219. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17220. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17221. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17222. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17223. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17224. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17225. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17226. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17227. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17228. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17229. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17230. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17231. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17232. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17233. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17234. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17235. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17236. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17237. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17238. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17239. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17240. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17241. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17242. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17243. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17244. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17245. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17246. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17247. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17248. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17249. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17250. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17251. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17252. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17253. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17254. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17255. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17256. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17257. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17258. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17259. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17260. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17261. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17262. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17263. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17264. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17265. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17266. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17267. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17268. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17269. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17270. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17271. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17272. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17273. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17274. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17275. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17276. (when (featurep 'xemacs)
  17277. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17278. (defconst org-speed-commands-default
  17279. '(
  17280. ("Outline Navigation")
  17281. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17282. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17283. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17284. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17285. ("F" . org-next-block)
  17286. ("B" . org-previous-block)
  17287. ("u" . (org-speed-move-safe 'outline-up-heading))
  17288. ("j" . org-goto)
  17289. ("g" . (org-refile t))
  17290. ("Outline Visibility")
  17291. ("c" . org-cycle)
  17292. ("C" . org-shifttab)
  17293. (" " . org-display-outline-path)
  17294. ("=" . org-columns)
  17295. ("Outline Structure Editing")
  17296. ("U" . org-shiftmetaup)
  17297. ("D" . org-shiftmetadown)
  17298. ("r" . org-metaright)
  17299. ("l" . org-metaleft)
  17300. ("R" . org-shiftmetaright)
  17301. ("L" . org-shiftmetaleft)
  17302. ("i" . (progn (forward-char 1) (call-interactively
  17303. 'org-insert-heading-respect-content)))
  17304. ("^" . org-sort)
  17305. ("w" . org-refile)
  17306. ("a" . org-archive-subtree-default-with-confirmation)
  17307. ("@" . org-mark-subtree)
  17308. ("#" . org-toggle-comment)
  17309. ("Clock Commands")
  17310. ("I" . org-clock-in)
  17311. ("O" . org-clock-out)
  17312. ("Meta Data Editing")
  17313. ("t" . org-todo)
  17314. ("," . (org-priority))
  17315. ("0" . (org-priority ?\ ))
  17316. ("1" . (org-priority ?A))
  17317. ("2" . (org-priority ?B))
  17318. ("3" . (org-priority ?C))
  17319. (":" . org-set-tags-command)
  17320. ("e" . org-set-effort)
  17321. ("E" . org-inc-effort)
  17322. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17323. (org-entry-put (point) "APPT_WARNTIME" m)))
  17324. ("Agenda Views etc")
  17325. ("v" . org-agenda)
  17326. ("/" . org-sparse-tree)
  17327. ("Misc")
  17328. ("o" . org-open-at-point)
  17329. ("?" . org-speed-command-help)
  17330. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17331. (">" . (org-agenda-remove-restriction-lock))
  17332. )
  17333. "The default speed commands.")
  17334. (defun org-print-speed-command (e)
  17335. (if (> (length (car e)) 1)
  17336. (progn
  17337. (princ "\n")
  17338. (princ (car e))
  17339. (princ "\n")
  17340. (princ (make-string (length (car e)) ?-))
  17341. (princ "\n"))
  17342. (princ (car e))
  17343. (princ " ")
  17344. (if (symbolp (cdr e))
  17345. (princ (symbol-name (cdr e)))
  17346. (prin1 (cdr e)))
  17347. (princ "\n")))
  17348. (defun org-speed-command-help ()
  17349. "Show the available speed commands."
  17350. (interactive)
  17351. (if (not org-use-speed-commands)
  17352. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17353. (with-output-to-temp-buffer "*Help*"
  17354. (princ "User-defined Speed commands\n===========================\n")
  17355. (mapc 'org-print-speed-command org-speed-commands-user)
  17356. (princ "\n")
  17357. (princ "Built-in Speed commands\n=======================\n")
  17358. (mapc 'org-print-speed-command org-speed-commands-default))
  17359. (with-current-buffer "*Help*"
  17360. (setq truncate-lines t))))
  17361. (defun org-speed-move-safe (cmd)
  17362. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17363. If not, return to the original position and throw an error."
  17364. (interactive)
  17365. (let ((pos (point)))
  17366. (call-interactively cmd)
  17367. (unless (and (bolp) (org-at-heading-p))
  17368. (goto-char pos)
  17369. (error "Boundary reached while executing %s" cmd))))
  17370. (defvar org-self-insert-command-undo-counter 0)
  17371. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17372. (defvar org-speed-command nil)
  17373. (define-obsolete-function-alias
  17374. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17375. (defun org-speed-command-activate (keys)
  17376. "Hook for activating single-letter speed commands.
  17377. `org-speed-commands-default' specifies a minimal command set.
  17378. Use `org-speed-commands-user' for further customization."
  17379. (when (or (and (bolp) (looking-at org-outline-regexp))
  17380. (and (functionp org-use-speed-commands)
  17381. (funcall org-use-speed-commands)))
  17382. (cdr (assoc keys (append org-speed-commands-user
  17383. org-speed-commands-default)))))
  17384. (define-obsolete-function-alias
  17385. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17386. (defun org-babel-speed-command-activate (keys)
  17387. "Hook for activating single-letter code block commands."
  17388. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17389. (cdr (assoc keys org-babel-key-bindings))))
  17390. (defcustom org-speed-command-hook
  17391. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17392. "Hook for activating speed commands at strategic locations.
  17393. Hook functions are called in sequence until a valid handler is
  17394. found.
  17395. Each hook takes a single argument, a user-pressed command key
  17396. which is also a `self-insert-command' from the global map.
  17397. Within the hook, examine the cursor position and the command key
  17398. and return nil or a valid handler as appropriate. Handler could
  17399. be one of an interactive command, a function, or a form.
  17400. Set `org-use-speed-commands' to non-nil value to enable this
  17401. hook. The default setting is `org-speed-command-activate'."
  17402. :group 'org-structure
  17403. :version "24.1"
  17404. :type 'hook)
  17405. (defun org-self-insert-command (N)
  17406. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17407. If the cursor is in a table looking at whitespace, the whitespace is
  17408. overwritten, and the table is not marked as requiring realignment."
  17409. (interactive "p")
  17410. (org-check-before-invisible-edit 'insert)
  17411. (cond
  17412. ((and org-use-speed-commands
  17413. (setq org-speed-command
  17414. (run-hook-with-args-until-success
  17415. 'org-speed-command-hook (this-command-keys))))
  17416. (cond
  17417. ((commandp org-speed-command)
  17418. (setq this-command org-speed-command)
  17419. (call-interactively org-speed-command))
  17420. ((functionp org-speed-command)
  17421. (funcall org-speed-command))
  17422. ((and org-speed-command (listp org-speed-command))
  17423. (eval org-speed-command))
  17424. (t (let (org-use-speed-commands)
  17425. (call-interactively 'org-self-insert-command)))))
  17426. ((and
  17427. (org-table-p)
  17428. (progn
  17429. ;; check if we blank the field, and if that triggers align
  17430. (and (featurep 'org-table) org-table-auto-blank-field
  17431. (member last-command
  17432. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17433. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17434. ;; got extra space, this field does not determine column width
  17435. (let (org-table-may-need-update) (org-table-blank-field))
  17436. ;; no extra space, this field may determine column width
  17437. (org-table-blank-field)))
  17438. t)
  17439. (eq N 1)
  17440. (looking-at "[^|\n]* |"))
  17441. (let (org-table-may-need-update)
  17442. (goto-char (1- (match-end 0)))
  17443. (backward-delete-char 1)
  17444. (goto-char (match-beginning 0))
  17445. (self-insert-command N)))
  17446. (t
  17447. (setq org-table-may-need-update t)
  17448. (self-insert-command N)
  17449. (org-fix-tags-on-the-fly)
  17450. (if org-self-insert-cluster-for-undo
  17451. (if (not (eq last-command 'org-self-insert-command))
  17452. (setq org-self-insert-command-undo-counter 1)
  17453. (if (>= org-self-insert-command-undo-counter 20)
  17454. (setq org-self-insert-command-undo-counter 1)
  17455. (and (> org-self-insert-command-undo-counter 0)
  17456. buffer-undo-list (listp buffer-undo-list)
  17457. (not (cadr buffer-undo-list)) ; remove nil entry
  17458. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17459. (setq org-self-insert-command-undo-counter
  17460. (1+ org-self-insert-command-undo-counter))))))))
  17461. (defun org-check-before-invisible-edit (kind)
  17462. "Check is editing if kind KIND would be dangerous with invisible text around.
  17463. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17464. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17465. (if (and org-catch-invisible-edits
  17466. (or (not (boundp 'visible-mode)) (not visible-mode))
  17467. (or (get-char-property (point) 'invisible)
  17468. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17469. ;; OK, we need to take a closer look
  17470. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17471. (invisible-before-point (if (bobp) nil (get-char-property
  17472. (1- (point)) 'invisible)))
  17473. (border-and-ok-direction
  17474. (or
  17475. ;; Check if we are acting predictably before invisible text
  17476. (and invisible-at-point (not invisible-before-point)
  17477. (memq kind '(insert delete-backward)))
  17478. ;; Check if we are acting predictably after invisible text
  17479. ;; This works not well, and I have turned it off. It seems
  17480. ;; better to always show and stop after invisible text.
  17481. ;; (and (not invisible-at-point) invisible-before-point
  17482. ;; (memq kind '(insert delete)))
  17483. )))
  17484. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17485. (memq invisible-before-point '(outline org-hide-block t)))
  17486. (if (eq org-catch-invisible-edits 'error)
  17487. (error "Editing in invisible areas is prohibited - make visible first"))
  17488. (if (and org-custom-properties-overlays
  17489. (y-or-n-p "Display invisible properties in this buffer? "))
  17490. (org-toggle-custom-properties-visibility)
  17491. ;; Make the area visible
  17492. (save-excursion
  17493. (if invisible-before-point
  17494. (goto-char (previous-single-char-property-change
  17495. (point) 'invisible)))
  17496. (org-cycle))
  17497. (cond
  17498. ((eq org-catch-invisible-edits 'show)
  17499. ;; That's it, we do the edit after showing
  17500. (message
  17501. "Unfolding invisible region around point before editing")
  17502. (sit-for 1))
  17503. ((and (eq org-catch-invisible-edits 'smart)
  17504. border-and-ok-direction)
  17505. (message "Unfolding invisible region around point before editing"))
  17506. (t
  17507. ;; Don't do the edit, make the user repeat it in full visibility
  17508. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17509. (defun org-fix-tags-on-the-fly ()
  17510. (when (and (equal (char-after (point-at-bol)) ?*)
  17511. (org-at-heading-p))
  17512. (org-align-tags-here org-tags-column)))
  17513. (defun org-delete-backward-char (N)
  17514. "Like `delete-backward-char', insert whitespace at field end in tables.
  17515. When deleting backwards, in tables this function will insert whitespace in
  17516. front of the next \"|\" separator, to keep the table aligned. The table will
  17517. still be marked for re-alignment if the field did fill the entire column,
  17518. because, in this case the deletion might narrow the column."
  17519. (interactive "p")
  17520. (save-match-data
  17521. (org-check-before-invisible-edit 'delete-backward)
  17522. (if (and (org-table-p)
  17523. (eq N 1)
  17524. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17525. (looking-at ".*?|"))
  17526. (let ((pos (point))
  17527. (noalign (looking-at "[^|\n\r]* |"))
  17528. (c org-table-may-need-update))
  17529. (backward-delete-char N)
  17530. (if (not overwrite-mode)
  17531. (progn
  17532. (skip-chars-forward "^|")
  17533. (insert " ")
  17534. (goto-char (1- pos))))
  17535. ;; noalign: if there were two spaces at the end, this field
  17536. ;; does not determine the width of the column.
  17537. (if noalign (setq org-table-may-need-update c)))
  17538. (backward-delete-char N)
  17539. (org-fix-tags-on-the-fly))))
  17540. (defun org-delete-char (N)
  17541. "Like `delete-char', but insert whitespace at field end in tables.
  17542. When deleting characters, in tables this function will insert whitespace in
  17543. front of the next \"|\" separator, to keep the table aligned. The table will
  17544. still be marked for re-alignment if the field did fill the entire column,
  17545. because, in this case the deletion might narrow the column."
  17546. (interactive "p")
  17547. (save-match-data
  17548. (org-check-before-invisible-edit 'delete)
  17549. (if (and (org-table-p)
  17550. (not (bolp))
  17551. (not (= (char-after) ?|))
  17552. (eq N 1))
  17553. (if (looking-at ".*?|")
  17554. (let ((pos (point))
  17555. (noalign (looking-at "[^|\n\r]* |"))
  17556. (c org-table-may-need-update))
  17557. (replace-match (concat
  17558. (substring (match-string 0) 1 -1)
  17559. " |"))
  17560. (goto-char pos)
  17561. ;; noalign: if there were two spaces at the end, this field
  17562. ;; does not determine the width of the column.
  17563. (if noalign (setq org-table-may-need-update c)))
  17564. (delete-char N))
  17565. (delete-char N)
  17566. (org-fix-tags-on-the-fly))))
  17567. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17568. (put 'org-self-insert-command 'delete-selection t)
  17569. (put 'orgtbl-self-insert-command 'delete-selection t)
  17570. (put 'org-delete-char 'delete-selection 'supersede)
  17571. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17572. (put 'org-yank 'delete-selection 'yank)
  17573. ;; Make `flyspell-mode' delay after some commands
  17574. (put 'org-self-insert-command 'flyspell-delayed t)
  17575. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17576. (put 'org-delete-char 'flyspell-delayed t)
  17577. (put 'org-delete-backward-char 'flyspell-delayed t)
  17578. ;; Make pabbrev-mode expand after org-mode commands
  17579. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17580. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17581. ;; How to do this: Measure non-white length of current string
  17582. ;; If equal to column width, we should realign.
  17583. (defun org-remap (map &rest commands)
  17584. "In MAP, remap the functions given in COMMANDS.
  17585. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17586. (let (new old)
  17587. (while commands
  17588. (setq old (pop commands) new (pop commands))
  17589. (if (fboundp 'command-remapping)
  17590. (org-defkey map (vector 'remap old) new)
  17591. (substitute-key-definition old new map global-map)))))
  17592. (defun org-transpose-words ()
  17593. "Transpose words for Org.
  17594. This uses the `org-mode-transpose-word-syntax-table' syntax
  17595. table, which interprets characters in `org-emphasis-alist' as
  17596. word constituants."
  17597. (interactive)
  17598. (with-syntax-table org-mode-transpose-word-syntax-table
  17599. (call-interactively 'transpose-words)))
  17600. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17601. (when (eq org-enable-table-editor 'optimized)
  17602. ;; If the user wants maximum table support, we need to hijack
  17603. ;; some standard editing functions
  17604. (org-remap org-mode-map
  17605. 'self-insert-command 'org-self-insert-command
  17606. 'delete-char 'org-delete-char
  17607. 'delete-backward-char 'org-delete-backward-char)
  17608. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17609. (defvar org-ctrl-c-ctrl-c-hook nil
  17610. "Hook for functions attaching themselves to `C-c C-c'.
  17611. This can be used to add additional functionality to the C-c C-c
  17612. key which executes context-dependent commands. This hook is run
  17613. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17614. run after the last test.
  17615. Each function will be called with no arguments. The function
  17616. must check if the context is appropriate for it to act. If yes,
  17617. it should do its thing and then return a non-nil value. If the
  17618. context is wrong, just do nothing and return nil.")
  17619. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17620. "Hook for functions attaching themselves to `C-c C-c'.
  17621. This can be used to add additional functionality to the C-c C-c
  17622. key which executes context-dependent commands. This hook is run
  17623. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17624. before the first test.
  17625. Each function will be called with no arguments. The function
  17626. must check if the context is appropriate for it to act. If yes,
  17627. it should do its thing and then return a non-nil value. If the
  17628. context is wrong, just do nothing and return nil.")
  17629. (defvar org-tab-first-hook nil
  17630. "Hook for functions to attach themselves to TAB.
  17631. See `org-ctrl-c-ctrl-c-hook' for more information.
  17632. This hook runs as the first action when TAB is pressed, even before
  17633. `org-cycle' messes around with the `outline-regexp' to cater for
  17634. inline tasks and plain list item folding.
  17635. If any function in this hook returns t, any other actions that
  17636. would have been caused by TAB (such as table field motion or visibility
  17637. cycling) will not occur.")
  17638. (defvar org-tab-after-check-for-table-hook nil
  17639. "Hook for functions to attach themselves to TAB.
  17640. See `org-ctrl-c-ctrl-c-hook' for more information.
  17641. This hook runs after it has been established that the cursor is not in a
  17642. table, but before checking if the cursor is in a headline or if global cycling
  17643. should be done.
  17644. If any function in this hook returns t, not other actions like visibility
  17645. cycling will be done.")
  17646. (defvar org-tab-after-check-for-cycling-hook nil
  17647. "Hook for functions to attach themselves to TAB.
  17648. See `org-ctrl-c-ctrl-c-hook' for more information.
  17649. This hook runs after it has been established that not table field motion and
  17650. not visibility should be done because of current context. This is probably
  17651. the place where a package like yasnippets can hook in.")
  17652. (defvar org-tab-before-tab-emulation-hook nil
  17653. "Hook for functions to attach themselves to TAB.
  17654. See `org-ctrl-c-ctrl-c-hook' for more information.
  17655. This hook runs after every other options for TAB have been exhausted, but
  17656. before indentation and \t insertion takes place.")
  17657. (defvar org-metaleft-hook nil
  17658. "Hook for functions attaching themselves to `M-left'.
  17659. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17660. (defvar org-metaright-hook nil
  17661. "Hook for functions attaching themselves to `M-right'.
  17662. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17663. (defvar org-metaup-hook nil
  17664. "Hook for functions attaching themselves to `M-up'.
  17665. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17666. (defvar org-metadown-hook nil
  17667. "Hook for functions attaching themselves to `M-down'.
  17668. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17669. (defvar org-shiftmetaleft-hook nil
  17670. "Hook for functions attaching themselves to `M-S-left'.
  17671. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17672. (defvar org-shiftmetaright-hook nil
  17673. "Hook for functions attaching themselves to `M-S-right'.
  17674. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17675. (defvar org-shiftmetaup-hook nil
  17676. "Hook for functions attaching themselves to `M-S-up'.
  17677. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17678. (defvar org-shiftmetadown-hook nil
  17679. "Hook for functions attaching themselves to `M-S-down'.
  17680. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17681. (defvar org-metareturn-hook nil
  17682. "Hook for functions attaching themselves to `M-RET'.
  17683. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17684. (defvar org-shiftup-hook nil
  17685. "Hook for functions attaching themselves to `S-up'.
  17686. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17687. (defvar org-shiftup-final-hook nil
  17688. "Hook for functions attaching themselves to `S-up'.
  17689. This one runs after all other options except shift-select have been excluded.
  17690. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17691. (defvar org-shiftdown-hook nil
  17692. "Hook for functions attaching themselves to `S-down'.
  17693. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17694. (defvar org-shiftdown-final-hook nil
  17695. "Hook for functions attaching themselves to `S-down'.
  17696. This one runs after all other options except shift-select have been excluded.
  17697. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17698. (defvar org-shiftleft-hook nil
  17699. "Hook for functions attaching themselves to `S-left'.
  17700. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17701. (defvar org-shiftleft-final-hook nil
  17702. "Hook for functions attaching themselves to `S-left'.
  17703. This one runs after all other options except shift-select have been excluded.
  17704. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17705. (defvar org-shiftright-hook nil
  17706. "Hook for functions attaching themselves to `S-right'.
  17707. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17708. (defvar org-shiftright-final-hook nil
  17709. "Hook for functions attaching themselves to `S-right'.
  17710. This one runs after all other options except shift-select have been excluded.
  17711. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17712. (defun org-modifier-cursor-error ()
  17713. "Throw an error, a modified cursor command was applied in wrong context."
  17714. (error "This command is active in special context like tables, headlines or items"))
  17715. (defun org-shiftselect-error ()
  17716. "Throw an error because Shift-Cursor command was applied in wrong context."
  17717. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17718. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17719. (error "This command works only in special context like headlines or timestamps")))
  17720. (defun org-call-for-shift-select (cmd)
  17721. (let ((this-command-keys-shift-translated t))
  17722. (call-interactively cmd)))
  17723. (defun org-shifttab (&optional arg)
  17724. "Global visibility cycling or move to previous table field.
  17725. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17726. on context.
  17727. See the individual commands for more information."
  17728. (interactive "P")
  17729. (cond
  17730. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17731. ((integerp arg)
  17732. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17733. (message "Content view to level: %d" arg)
  17734. (org-content (prefix-numeric-value arg2))
  17735. (setq org-cycle-global-status 'overview)))
  17736. (t (call-interactively 'org-global-cycle))))
  17737. (defun org-shiftmetaleft ()
  17738. "Promote subtree or delete table column.
  17739. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17740. `org-table-delete-column', depending on context. See the
  17741. individual commands for more information."
  17742. (interactive)
  17743. (cond
  17744. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17745. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17746. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17747. ((if (not (org-region-active-p)) (org-at-item-p)
  17748. (save-excursion (goto-char (region-beginning))
  17749. (org-at-item-p)))
  17750. (call-interactively 'org-outdent-item-tree))
  17751. (t (org-modifier-cursor-error))))
  17752. (defun org-shiftmetaright ()
  17753. "Demote subtree or insert table column.
  17754. Calls `org-demote-subtree', `org-indent-item-tree', or
  17755. `org-table-insert-column', depending on context. See the
  17756. individual commands for more information."
  17757. (interactive)
  17758. (cond
  17759. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17760. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17761. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17762. ((if (not (org-region-active-p)) (org-at-item-p)
  17763. (save-excursion (goto-char (region-beginning))
  17764. (org-at-item-p)))
  17765. (call-interactively 'org-indent-item-tree))
  17766. (t (org-modifier-cursor-error))))
  17767. (defun org-shiftmetaup (&optional arg)
  17768. "Move subtree up or kill table row.
  17769. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17770. `org-move-item-up' or `org-timestamp-up', depending on context.
  17771. See the individual commands for more information."
  17772. (interactive "P")
  17773. (cond
  17774. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17775. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17776. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17777. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17778. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17779. (call-interactively 'org-timestamp-up)))
  17780. (t (call-interactively 'org-drag-line-backward))))
  17781. (defun org-shiftmetadown (&optional arg)
  17782. "Move subtree down or insert table row.
  17783. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17784. `org-move-item-down' or `org-timestamp-up', depending on context.
  17785. See the individual commands for more information."
  17786. (interactive "P")
  17787. (cond
  17788. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17789. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17790. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17791. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17792. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17793. (call-interactively 'org-timestamp-down)))
  17794. (t (call-interactively 'org-drag-line-forward))))
  17795. (defsubst org-hidden-tree-error ()
  17796. (error
  17797. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17798. (defun org-metaleft (&optional arg)
  17799. "Promote heading or move table column to left.
  17800. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17801. With no specific context, calls the Emacs default `backward-word'.
  17802. See the individual commands for more information."
  17803. (interactive "P")
  17804. (cond
  17805. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17806. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17807. ((org-with-limited-levels
  17808. (or (org-at-heading-p)
  17809. (and (org-region-active-p)
  17810. (save-excursion
  17811. (goto-char (region-beginning))
  17812. (org-at-heading-p)))))
  17813. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17814. (call-interactively 'org-do-promote))
  17815. ;; At an inline task.
  17816. ((org-at-heading-p)
  17817. (call-interactively 'org-inlinetask-promote))
  17818. ((or (org-at-item-p)
  17819. (and (org-region-active-p)
  17820. (save-excursion
  17821. (goto-char (region-beginning))
  17822. (org-at-item-p))))
  17823. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17824. (call-interactively 'org-outdent-item))
  17825. (t (call-interactively 'backward-word))))
  17826. (defun org-metaright (&optional arg)
  17827. "Demote a subtree, a list item or move table column to right.
  17828. In front of a drawer or a block keyword, indent it correctly.
  17829. With no specific context, calls the Emacs default `forward-word'.
  17830. See the individual commands for more information."
  17831. (interactive "P")
  17832. (cond
  17833. ((run-hook-with-args-until-success 'org-metaright-hook))
  17834. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17835. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17836. ((org-at-block-p) (call-interactively 'org-indent-block))
  17837. ((org-with-limited-levels
  17838. (or (org-at-heading-p)
  17839. (and (org-region-active-p)
  17840. (save-excursion
  17841. (goto-char (region-beginning))
  17842. (org-at-heading-p)))))
  17843. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17844. (call-interactively 'org-do-demote))
  17845. ;; At an inline task.
  17846. ((org-at-heading-p)
  17847. (call-interactively 'org-inlinetask-demote))
  17848. ((or (org-at-item-p)
  17849. (and (org-region-active-p)
  17850. (save-excursion
  17851. (goto-char (region-beginning))
  17852. (org-at-item-p))))
  17853. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17854. (call-interactively 'org-indent-item))
  17855. (t (call-interactively 'forward-word))))
  17856. (defun org-check-for-hidden (what)
  17857. "Check if there are hidden headlines/items in the current visual line.
  17858. WHAT can be either `headlines' or `items'. If the current line is
  17859. an outline or item heading and it has a folded subtree below it,
  17860. this function returns t, nil otherwise."
  17861. (let ((re (cond
  17862. ((eq what 'headlines) org-outline-regexp-bol)
  17863. ((eq what 'items) (org-item-beginning-re))
  17864. (t (error "This should not happen"))))
  17865. beg end)
  17866. (save-excursion
  17867. (catch 'exit
  17868. (unless (org-region-active-p)
  17869. (setq beg (point-at-bol))
  17870. (beginning-of-line 2)
  17871. (while (and (not (eobp)) ;; this is like `next-line'
  17872. (get-char-property (1- (point)) 'invisible))
  17873. (beginning-of-line 2))
  17874. (setq end (point))
  17875. (goto-char beg)
  17876. (goto-char (point-at-eol))
  17877. (setq end (max end (point)))
  17878. (while (re-search-forward re end t)
  17879. (if (get-char-property (match-beginning 0) 'invisible)
  17880. (throw 'exit t))))
  17881. nil))))
  17882. (defun org-metaup (&optional arg)
  17883. "Move subtree up or move table row up.
  17884. Calls `org-move-subtree-up' or `org-table-move-row' or
  17885. `org-move-item-up', depending on context. See the individual commands
  17886. for more information."
  17887. (interactive "P")
  17888. (cond
  17889. ((run-hook-with-args-until-success 'org-metaup-hook))
  17890. ((org-region-active-p)
  17891. (let* ((a (min (region-beginning) (region-end)))
  17892. (b (1- (max (region-beginning) (region-end))))
  17893. (c (save-excursion (goto-char a)
  17894. (move-beginning-of-line 0)))
  17895. (d (save-excursion (goto-char a)
  17896. (move-end-of-line 0) (point))))
  17897. (transpose-regions a b c d)
  17898. (goto-char c)))
  17899. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17900. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17901. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17902. (t (org-drag-element-backward))))
  17903. (defun org-metadown (&optional arg)
  17904. "Move subtree down or move table row down.
  17905. Calls `org-move-subtree-down' or `org-table-move-row' or
  17906. `org-move-item-down', depending on context. See the individual
  17907. commands for more information."
  17908. (interactive "P")
  17909. (cond
  17910. ((run-hook-with-args-until-success 'org-metadown-hook))
  17911. ((org-region-active-p)
  17912. (let* ((a (min (region-beginning) (region-end)))
  17913. (b (max (region-beginning) (region-end)))
  17914. (c (save-excursion (goto-char b)
  17915. (move-beginning-of-line 1)))
  17916. (d (save-excursion (goto-char b)
  17917. (move-end-of-line 1) (1+ (point)))))
  17918. (transpose-regions a b c d)
  17919. (goto-char d)))
  17920. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17921. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17922. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17923. (t (org-drag-element-forward))))
  17924. (defun org-shiftup (&optional arg)
  17925. "Increase item in timestamp or increase priority of current headline.
  17926. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17927. depending on context. See the individual commands for more information."
  17928. (interactive "P")
  17929. (cond
  17930. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17931. ((and org-support-shift-select (org-region-active-p))
  17932. (org-call-for-shift-select 'previous-line))
  17933. ((org-at-timestamp-p t)
  17934. (call-interactively (if org-edit-timestamp-down-means-later
  17935. 'org-timestamp-down 'org-timestamp-up)))
  17936. ((and (not (eq org-support-shift-select 'always))
  17937. org-enable-priority-commands
  17938. (org-at-heading-p))
  17939. (call-interactively 'org-priority-up))
  17940. ((and (not org-support-shift-select) (org-at-item-p))
  17941. (call-interactively 'org-previous-item))
  17942. ((org-clocktable-try-shift 'up arg))
  17943. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17944. (org-support-shift-select
  17945. (org-call-for-shift-select 'previous-line))
  17946. (t (org-shiftselect-error))))
  17947. (defun org-shiftdown (&optional arg)
  17948. "Decrease item in timestamp or decrease priority of current headline.
  17949. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17950. depending on context. See the individual commands for more information."
  17951. (interactive "P")
  17952. (cond
  17953. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17954. ((and org-support-shift-select (org-region-active-p))
  17955. (org-call-for-shift-select 'next-line))
  17956. ((org-at-timestamp-p t)
  17957. (call-interactively (if org-edit-timestamp-down-means-later
  17958. 'org-timestamp-up 'org-timestamp-down)))
  17959. ((and (not (eq org-support-shift-select 'always))
  17960. org-enable-priority-commands
  17961. (org-at-heading-p))
  17962. (call-interactively 'org-priority-down))
  17963. ((and (not org-support-shift-select) (org-at-item-p))
  17964. (call-interactively 'org-next-item))
  17965. ((org-clocktable-try-shift 'down arg))
  17966. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17967. (org-support-shift-select
  17968. (org-call-for-shift-select 'next-line))
  17969. (t (org-shiftselect-error))))
  17970. (defun org-shiftright (&optional arg)
  17971. "Cycle the thing at point or in the current line, depending on context.
  17972. Depending on context, this does one of the following:
  17973. - switch a timestamp at point one day into the future
  17974. - on a headline, switch to the next TODO keyword.
  17975. - on an item, switch entire list to the next bullet type
  17976. - on a property line, switch to the next allowed value
  17977. - on a clocktable definition line, move time block into the future"
  17978. (interactive "P")
  17979. (cond
  17980. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17981. ((and org-support-shift-select (org-region-active-p))
  17982. (org-call-for-shift-select 'forward-char))
  17983. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17984. ((and (not (eq org-support-shift-select 'always))
  17985. (org-at-heading-p))
  17986. (let ((org-inhibit-logging
  17987. (not org-treat-S-cursor-todo-selection-as-state-change))
  17988. (org-inhibit-blocking
  17989. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17990. (org-call-with-arg 'org-todo 'right)))
  17991. ((or (and org-support-shift-select
  17992. (not (eq org-support-shift-select 'always))
  17993. (org-at-item-bullet-p))
  17994. (and (not org-support-shift-select) (org-at-item-p)))
  17995. (org-call-with-arg 'org-cycle-list-bullet nil))
  17996. ((and (not (eq org-support-shift-select 'always))
  17997. (org-at-property-p))
  17998. (call-interactively 'org-property-next-allowed-value))
  17999. ((org-clocktable-try-shift 'right arg))
  18000. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18001. (org-support-shift-select
  18002. (org-call-for-shift-select 'forward-char))
  18003. (t (org-shiftselect-error))))
  18004. (defun org-shiftleft (&optional arg)
  18005. "Cycle the thing at point or in the current line, depending on context.
  18006. Depending on context, this does one of the following:
  18007. - switch a timestamp at point one day into the past
  18008. - on a headline, switch to the previous TODO keyword.
  18009. - on an item, switch entire list to the previous bullet type
  18010. - on a property line, switch to the previous allowed value
  18011. - on a clocktable definition line, move time block into the past"
  18012. (interactive "P")
  18013. (cond
  18014. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18015. ((and org-support-shift-select (org-region-active-p))
  18016. (org-call-for-shift-select 'backward-char))
  18017. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18018. ((and (not (eq org-support-shift-select 'always))
  18019. (org-at-heading-p))
  18020. (let ((org-inhibit-logging
  18021. (not org-treat-S-cursor-todo-selection-as-state-change))
  18022. (org-inhibit-blocking
  18023. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18024. (org-call-with-arg 'org-todo 'left)))
  18025. ((or (and org-support-shift-select
  18026. (not (eq org-support-shift-select 'always))
  18027. (org-at-item-bullet-p))
  18028. (and (not org-support-shift-select) (org-at-item-p)))
  18029. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18030. ((and (not (eq org-support-shift-select 'always))
  18031. (org-at-property-p))
  18032. (call-interactively 'org-property-previous-allowed-value))
  18033. ((org-clocktable-try-shift 'left arg))
  18034. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18035. (org-support-shift-select
  18036. (org-call-for-shift-select 'backward-char))
  18037. (t (org-shiftselect-error))))
  18038. (defun org-shiftcontrolright ()
  18039. "Switch to next TODO set."
  18040. (interactive)
  18041. (cond
  18042. ((and org-support-shift-select (org-region-active-p))
  18043. (org-call-for-shift-select 'forward-word))
  18044. ((and (not (eq org-support-shift-select 'always))
  18045. (org-at-heading-p))
  18046. (org-call-with-arg 'org-todo 'nextset))
  18047. (org-support-shift-select
  18048. (org-call-for-shift-select 'forward-word))
  18049. (t (org-shiftselect-error))))
  18050. (defun org-shiftcontrolleft ()
  18051. "Switch to previous TODO set."
  18052. (interactive)
  18053. (cond
  18054. ((and org-support-shift-select (org-region-active-p))
  18055. (org-call-for-shift-select 'backward-word))
  18056. ((and (not (eq org-support-shift-select 'always))
  18057. (org-at-heading-p))
  18058. (org-call-with-arg 'org-todo 'previousset))
  18059. (org-support-shift-select
  18060. (org-call-for-shift-select 'backward-word))
  18061. (t (org-shiftselect-error))))
  18062. (defun org-shiftcontrolup (&optional n)
  18063. "Change timestamps synchronously up in CLOCK log lines.
  18064. Optional argument N tells to change by that many units."
  18065. (interactive "P")
  18066. (cond ((and (not org-support-shift-select)
  18067. (org-at-clock-log-p)
  18068. (org-at-timestamp-p t))
  18069. (org-clock-timestamps-up n))
  18070. (t (org-shiftselect-error))))
  18071. (defun org-shiftcontroldown (&optional n)
  18072. "Change timestamps synchronously down in CLOCK log lines.
  18073. Optional argument N tells to change by that many units."
  18074. (interactive "P")
  18075. (cond ((and (not org-support-shift-select)
  18076. (org-at-clock-log-p)
  18077. (org-at-timestamp-p t))
  18078. (org-clock-timestamps-down n))
  18079. (t (org-shiftselect-error))))
  18080. (defun org-ctrl-c-ret ()
  18081. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18082. (interactive)
  18083. (cond
  18084. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18085. (t (call-interactively 'org-insert-heading))))
  18086. (defun org-find-visible ()
  18087. (let ((s (point)))
  18088. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18089. (get-char-property s 'invisible)))
  18090. s))
  18091. (defun org-find-invisible ()
  18092. (let ((s (point)))
  18093. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18094. (not (get-char-property s 'invisible))))
  18095. s))
  18096. (defun org-copy-visible (beg end)
  18097. "Copy the visible parts of the region."
  18098. (interactive "r")
  18099. (let (snippets s)
  18100. (save-excursion
  18101. (save-restriction
  18102. (narrow-to-region beg end)
  18103. (setq s (goto-char (point-min)))
  18104. (while (not (= (point) (point-max)))
  18105. (goto-char (org-find-invisible))
  18106. (push (buffer-substring s (point)) snippets)
  18107. (setq s (goto-char (org-find-visible))))))
  18108. (kill-new (apply 'concat (nreverse snippets)))))
  18109. (defun org-copy-special ()
  18110. "Copy region in table or copy current subtree.
  18111. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18112. See the individual commands for more information."
  18113. (interactive)
  18114. (call-interactively
  18115. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18116. (defun org-cut-special ()
  18117. "Cut region in table or cut current subtree.
  18118. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18119. See the individual commands for more information."
  18120. (interactive)
  18121. (call-interactively
  18122. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18123. (defun org-paste-special (arg)
  18124. "Paste rectangular region into table, or past subtree relative to level.
  18125. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18126. See the individual commands for more information."
  18127. (interactive "P")
  18128. (if (org-at-table-p)
  18129. (org-table-paste-rectangle)
  18130. (org-paste-subtree arg)))
  18131. (defsubst org-in-fixed-width-region-p ()
  18132. "Is point in a fixed-width region?"
  18133. (save-match-data
  18134. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18135. (defun org-edit-special (&optional arg)
  18136. "Call a special editor for the element at point.
  18137. When at a table, call the formula editor with `org-table-edit-formulas'.
  18138. When in a source code block, call `org-edit-src-code'.
  18139. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18140. When at an #+INCLUDE keyword, visit the included file.
  18141. On a link, call `ffap' to visit the link at point.
  18142. Otherwise, return a user error."
  18143. (interactive)
  18144. (let ((element (org-element-at-point)))
  18145. (assert (not buffer-read-only) nil
  18146. "Buffer is read-only: %s" (buffer-name))
  18147. (case (org-element-type element)
  18148. (src-block
  18149. (if (not arg) (org-edit-src-code)
  18150. (let* ((info (org-babel-get-src-block-info))
  18151. (lang (nth 0 info))
  18152. (params (nth 2 info))
  18153. (session (cdr (assq :session params))))
  18154. (if (not session) (org-edit-src-code)
  18155. ;; At a src-block with a session and function called with
  18156. ;; an ARG: switch to the buffer related to the inferior
  18157. ;; process.
  18158. (funcall (intern (concat "org-babel-prep-session:" lang))
  18159. session params)))))
  18160. (keyword
  18161. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18162. (find-file
  18163. (org-remove-double-quotes
  18164. (car (org-split-string (org-element-property :value element)))))
  18165. (user-error "No special environment to edit here")))
  18166. (table
  18167. (if (eq (org-element-property :type element) 'table.el)
  18168. (org-edit-src-code)
  18169. (call-interactively 'org-table-edit-formulas)))
  18170. ;; Only Org tables contain `table-row' type elements.
  18171. (table-row (call-interactively 'org-table-edit-formulas))
  18172. ((example-block export-block) (org-edit-src-code))
  18173. (fixed-width (org-edit-fixed-width-region))
  18174. (otherwise
  18175. ;; No notable element at point. Though, we may be at a link,
  18176. ;; which is an object. Thus, scan deeper.
  18177. (if (eq (org-element-type (org-element-context element)) 'link)
  18178. (call-interactively 'ffap)
  18179. (user-error "No special environment to edit here"))))))
  18180. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18181. (defun org-ctrl-c-ctrl-c (&optional arg)
  18182. "Set tags in headline, or update according to changed information at point.
  18183. This command does many different things, depending on context:
  18184. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18185. this is what we do.
  18186. - If the cursor is on a statistics cookie, update it.
  18187. - If the cursor is in a headline, prompt for tags and insert them
  18188. into the current line, aligned to `org-tags-column'. When called
  18189. with prefix arg, realign all tags in the current buffer.
  18190. - If the cursor is in one of the special #+KEYWORD lines, this
  18191. triggers scanning the buffer for these lines and updating the
  18192. information.
  18193. - If the cursor is inside a table, realign the table. This command
  18194. works even if the automatic table editor has been turned off.
  18195. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18196. the entire table.
  18197. - If the cursor is at a footnote reference or definition, jump to
  18198. the corresponding definition or references, respectively.
  18199. - If the cursor is a the beginning of a dynamic block, update it.
  18200. - If the current buffer is a capture buffer, close note and file it.
  18201. - If the cursor is on a <<<target>>>, update radio targets and
  18202. corresponding links in this buffer.
  18203. - If the cursor is on a numbered item in a plain list, renumber the
  18204. ordered list.
  18205. - If the cursor is on a checkbox, toggle it.
  18206. - If the cursor is on a code block, evaluate it. The variable
  18207. `org-confirm-babel-evaluate' can be used to control prompting
  18208. before code block evaluation, by default every code block
  18209. evaluation requires confirmation. Code block evaluation can be
  18210. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18211. (interactive "P")
  18212. (cond
  18213. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18214. org-occur-highlights
  18215. org-latex-fragment-image-overlays)
  18216. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18217. (org-remove-occur-highlights)
  18218. (org-remove-latex-fragment-image-overlays)
  18219. (message "Temporary highlights/overlays removed from current buffer"))
  18220. ((and (local-variable-p 'org-finish-function (current-buffer))
  18221. (fboundp org-finish-function))
  18222. (funcall org-finish-function))
  18223. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18224. (t
  18225. (let* ((context (org-element-context)) (type (org-element-type context)))
  18226. ;; Test if point is within a blank line.
  18227. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18228. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18229. (user-error "C-c C-c can do nothing useful at this location"))
  18230. ;; For convenience: at the first line of a paragraph on the
  18231. ;; same line as an item, apply function on that item instead.
  18232. (when (eq type 'paragraph)
  18233. (let ((parent (org-element-property :parent context)))
  18234. (when (and (eq (org-element-type parent) 'item)
  18235. (= (point-at-bol) (org-element-property :begin parent)))
  18236. (setq context parent type 'item))))
  18237. ;; Act according to type of element or object at point.
  18238. (case type
  18239. (clock (org-clock-update-time-maybe))
  18240. (dynamic-block
  18241. (save-excursion
  18242. (goto-char (org-element-property :post-affiliated context))
  18243. (org-update-dblock)))
  18244. (footnote-definition
  18245. (goto-char (org-element-property :post-affiliated context))
  18246. (call-interactively 'org-footnote-action))
  18247. (footnote-reference (call-interactively 'org-footnote-action))
  18248. ((headline inlinetask)
  18249. (save-excursion (goto-char (org-element-property :begin context))
  18250. (call-interactively 'org-set-tags)))
  18251. (item
  18252. ;; At an item: a double C-u set checkbox to "[-]"
  18253. ;; unconditionally, whereas a single one will toggle its
  18254. ;; presence. Without an universal argument, if the item
  18255. ;; has a checkbox, toggle it. Otherwise repair the list.
  18256. (let* ((box (org-element-property :checkbox context))
  18257. (struct (org-element-property :structure context))
  18258. (old-struct (copy-tree struct))
  18259. (parents (org-list-parents-alist struct))
  18260. (prevs (org-list-prevs-alist struct))
  18261. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18262. (org-list-set-checkbox
  18263. (org-element-property :begin context) struct
  18264. (cond ((equal arg '(16)) "[-]")
  18265. ((and (not box) (equal arg '(4))) "[ ]")
  18266. ((or (not box) (equal arg '(4))) nil)
  18267. ((eq box 'on) "[ ]")
  18268. (t "[X]")))
  18269. ;; Mimic `org-list-write-struct' but with grabbing
  18270. ;; a return value from `org-list-struct-fix-box'.
  18271. (org-list-struct-fix-ind struct parents 2)
  18272. (org-list-struct-fix-item-end struct)
  18273. (org-list-struct-fix-bul struct prevs)
  18274. (org-list-struct-fix-ind struct parents)
  18275. (let ((block-item
  18276. (org-list-struct-fix-box struct parents prevs orderedp)))
  18277. (if (and box (equal struct old-struct))
  18278. (if (equal arg '(16))
  18279. (message "Checkboxes already reset")
  18280. (user-error "Cannot toggle this checkbox: %s"
  18281. (if (eq box 'on)
  18282. "all subitems checked"
  18283. "unchecked subitems")))
  18284. (org-list-struct-apply-struct struct old-struct)
  18285. (org-update-checkbox-count-maybe))
  18286. (when block-item
  18287. (message "Checkboxes were removed due to empty box at line %d"
  18288. (org-current-line block-item))))))
  18289. (keyword
  18290. (let ((org-inhibit-startup-visibility-stuff t)
  18291. (org-startup-align-all-tables nil))
  18292. (when (boundp 'org-table-coordinate-overlays)
  18293. (mapc 'delete-overlay org-table-coordinate-overlays)
  18294. (setq org-table-coordinate-overlays nil))
  18295. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18296. (message "Local setup has been refreshed"))
  18297. (plain-list
  18298. ;; At a plain list, with a double C-u argument, set
  18299. ;; checkboxes of each item to "[-]", whereas a single one
  18300. ;; will toggle their presence according to the state of the
  18301. ;; first item in the list. Without an argument, repair the
  18302. ;; list.
  18303. (let* ((begin (org-element-property :contents-begin context))
  18304. (struct (org-element-property :structure context))
  18305. (old-struct (copy-tree struct))
  18306. (first-box (save-excursion
  18307. (goto-char begin)
  18308. (looking-at org-list-full-item-re)
  18309. (match-string-no-properties 3)))
  18310. (new-box (cond ((equal arg '(16)) "[-]")
  18311. ((equal arg '(4)) (unless first-box "[ ]"))
  18312. ((equal first-box "[X]") "[ ]")
  18313. (t "[X]"))))
  18314. (cond
  18315. (arg
  18316. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18317. (org-list-get-all-items
  18318. begin struct (org-list-prevs-alist struct))))
  18319. ((and first-box (eq (point) begin))
  18320. ;; For convenience, when point is at bol on the first
  18321. ;; item of the list and no argument is provided, simply
  18322. ;; toggle checkbox of that item, if any.
  18323. (org-list-set-checkbox begin struct new-box)))
  18324. (org-list-write-struct
  18325. struct (org-list-parents-alist struct) old-struct)
  18326. (org-update-checkbox-count-maybe)
  18327. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18328. ((property-drawer node-property)
  18329. (call-interactively 'org-property-action))
  18330. ((radio-target target)
  18331. (call-interactively 'org-update-radio-target-regexp))
  18332. (statistics-cookie
  18333. (call-interactively 'org-update-statistics-cookies))
  18334. ((table table-cell table-row)
  18335. ;; At a table, recalculate every field and align it. Also
  18336. ;; send the table if necessary. If the table has
  18337. ;; a `table.el' type, just give up. At a table row or
  18338. ;; cell, maybe recalculate line but always align table.
  18339. (if (eq (org-element-property :type context) 'table.el)
  18340. (message "Use C-c ' to edit table.el tables")
  18341. (let ((org-enable-table-editor t))
  18342. (if (or (eq type 'table)
  18343. ;; Check if point is at a TBLFM line.
  18344. (and (eq type 'table-row)
  18345. (= (point) (org-element-property :end context))))
  18346. (save-excursion
  18347. (goto-char (org-element-property :contents-begin context))
  18348. (org-call-with-arg 'org-table-recalculate (or arg t))
  18349. (orgtbl-send-table 'maybe))
  18350. (org-table-maybe-eval-formula)
  18351. (cond (arg (call-interactively 'org-table-recalculate))
  18352. ((org-table-maybe-recalculate-line))
  18353. (t (org-table-align)))))))
  18354. (timestamp (org-timestamp-change 0 'day))
  18355. (otherwise
  18356. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18357. (user-error
  18358. "C-c C-c can do nothing useful at this location")))))))))
  18359. (defun org-mode-restart ()
  18360. "Restart Org-mode, to scan again for special lines.
  18361. Also updates the keyword regular expressions."
  18362. (interactive)
  18363. (org-mode)
  18364. (message "Org-mode restarted"))
  18365. (defun org-kill-note-or-show-branches ()
  18366. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18367. (interactive)
  18368. (if (not org-finish-function)
  18369. (progn
  18370. (hide-subtree)
  18371. (call-interactively 'show-branches))
  18372. (let ((org-note-abort t))
  18373. (funcall org-finish-function))))
  18374. (defun org-return (&optional indent)
  18375. "Goto next table row or insert a newline.
  18376. Calls `org-table-next-row' or `newline', depending on context.
  18377. See the individual commands for more information."
  18378. (interactive)
  18379. (let (org-ts-what)
  18380. (cond
  18381. ((or (bobp) (org-in-src-block-p))
  18382. (if indent (newline-and-indent) (newline)))
  18383. ((org-at-table-p)
  18384. (org-table-justify-field-maybe)
  18385. (call-interactively 'org-table-next-row))
  18386. ;; when `newline-and-indent' is called within a list, make sure
  18387. ;; text moved stays inside the item.
  18388. ((and (org-in-item-p) indent)
  18389. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18390. (progn
  18391. (save-match-data (newline))
  18392. (org-indent-line-to (length (match-string 0))))
  18393. (let ((ind (org-get-indentation)))
  18394. (newline)
  18395. (if (org-looking-back org-list-end-re)
  18396. (org-indent-line)
  18397. (org-indent-line-to ind)))))
  18398. ((and org-return-follows-link
  18399. (org-at-timestamp-p t)
  18400. (not (eq org-ts-what 'after)))
  18401. (org-follow-timestamp-link))
  18402. ((and org-return-follows-link
  18403. (let ((tprop (get-text-property (point) 'face)))
  18404. (or (eq tprop 'org-link)
  18405. (and (listp tprop) (memq 'org-link tprop)))))
  18406. (call-interactively 'org-open-at-point))
  18407. ((and (org-at-heading-p)
  18408. (looking-at
  18409. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18410. (org-show-entry)
  18411. (end-of-line 1)
  18412. (newline))
  18413. (t (if indent (newline-and-indent) (newline))))))
  18414. (defun org-return-indent ()
  18415. "Goto next table row or insert a newline and indent.
  18416. Calls `org-table-next-row' or `newline-and-indent', depending on
  18417. context. See the individual commands for more information."
  18418. (interactive)
  18419. (org-return t))
  18420. (defun org-ctrl-c-star ()
  18421. "Compute table, or change heading status of lines.
  18422. Calls `org-table-recalculate' or `org-toggle-heading',
  18423. depending on context."
  18424. (interactive)
  18425. (cond
  18426. ((org-at-table-p)
  18427. (call-interactively 'org-table-recalculate))
  18428. (t
  18429. ;; Convert all lines in region to list items
  18430. (call-interactively 'org-toggle-heading))))
  18431. (defun org-ctrl-c-minus ()
  18432. "Insert separator line in table or modify bullet status of line.
  18433. Also turns a plain line or a region of lines into list items.
  18434. Calls `org-table-insert-hline', `org-toggle-item', or
  18435. `org-cycle-list-bullet', depending on context."
  18436. (interactive)
  18437. (cond
  18438. ((org-at-table-p)
  18439. (call-interactively 'org-table-insert-hline))
  18440. ((org-region-active-p)
  18441. (call-interactively 'org-toggle-item))
  18442. ((org-in-item-p)
  18443. (call-interactively 'org-cycle-list-bullet))
  18444. (t
  18445. (call-interactively 'org-toggle-item))))
  18446. (defun org-toggle-item (arg)
  18447. "Convert headings or normal lines to items, items to normal lines.
  18448. If there is no active region, only the current line is considered.
  18449. If the first non blank line in the region is a headline, convert
  18450. all headlines to items, shifting text accordingly.
  18451. If it is an item, convert all items to normal lines.
  18452. If it is normal text, change region into a list of items.
  18453. With a prefix argument ARG, change the region in a single item."
  18454. (interactive "P")
  18455. (let ((shift-text
  18456. (function
  18457. ;; Shift text in current section to IND, from point to END.
  18458. ;; The function leaves point to END line.
  18459. (lambda (ind end)
  18460. (let ((min-i 1000) (end (copy-marker end)))
  18461. ;; First determine the minimum indentation (MIN-I) of
  18462. ;; the text.
  18463. (save-excursion
  18464. (catch 'exit
  18465. (while (< (point) end)
  18466. (let ((i (org-get-indentation)))
  18467. (cond
  18468. ;; Skip blank lines and inline tasks.
  18469. ((looking-at "^[ \t]*$"))
  18470. ((looking-at org-outline-regexp-bol))
  18471. ;; We can't find less than 0 indentation.
  18472. ((zerop i) (throw 'exit (setq min-i 0)))
  18473. ((< i min-i) (setq min-i i))))
  18474. (forward-line))))
  18475. ;; Then indent each line so that a line indented to
  18476. ;; MIN-I becomes indented to IND. Ignore blank lines
  18477. ;; and inline tasks in the process.
  18478. (let ((delta (- ind min-i)))
  18479. (while (< (point) end)
  18480. (unless (or (looking-at "^[ \t]*$")
  18481. (looking-at org-outline-regexp-bol))
  18482. (org-indent-line-to (+ (org-get-indentation) delta)))
  18483. (forward-line)))))))
  18484. (skip-blanks
  18485. (function
  18486. ;; Return beginning of first non-blank line, starting from
  18487. ;; line at POS.
  18488. (lambda (pos)
  18489. (save-excursion
  18490. (goto-char pos)
  18491. (skip-chars-forward " \r\t\n")
  18492. (point-at-bol)))))
  18493. beg end)
  18494. ;; Determine boundaries of changes.
  18495. (if (org-region-active-p)
  18496. (setq beg (funcall skip-blanks (region-beginning))
  18497. end (copy-marker (region-end)))
  18498. (setq beg (funcall skip-blanks (point-at-bol))
  18499. end (copy-marker (point-at-eol))))
  18500. ;; Depending on the starting line, choose an action on the text
  18501. ;; between BEG and END.
  18502. (org-with-limited-levels
  18503. (save-excursion
  18504. (goto-char beg)
  18505. (cond
  18506. ;; Case 1. Start at an item: de-itemize. Note that it only
  18507. ;; happens when a region is active: `org-ctrl-c-minus'
  18508. ;; would call `org-cycle-list-bullet' otherwise.
  18509. ((org-at-item-p)
  18510. (while (< (point) end)
  18511. (when (org-at-item-p)
  18512. (skip-chars-forward " \t")
  18513. (delete-region (point) (match-end 0)))
  18514. (forward-line)))
  18515. ;; Case 2. Start at an heading: convert to items.
  18516. ((org-at-heading-p)
  18517. (let* ((bul (org-list-bullet-string "-"))
  18518. (bul-len (length bul))
  18519. ;; Indentation of the first heading. It should be
  18520. ;; relative to the indentation of its parent, if any.
  18521. (start-ind (save-excursion
  18522. (cond
  18523. ((not org-adapt-indentation) 0)
  18524. ((not (outline-previous-heading)) 0)
  18525. (t (length (match-string 0))))))
  18526. ;; Level of first heading. Further headings will be
  18527. ;; compared to it to determine hierarchy in the list.
  18528. (ref-level (org-reduced-level (org-outline-level))))
  18529. (while (< (point) end)
  18530. (let* ((level (org-reduced-level (org-outline-level)))
  18531. (delta (max 0 (- level ref-level))))
  18532. ;; If current headline is less indented than the first
  18533. ;; one, set it as reference, in order to preserve
  18534. ;; subtrees.
  18535. (when (< level ref-level) (setq ref-level level))
  18536. (replace-match bul t t)
  18537. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18538. ;; Ensure all text down to END (or SECTION-END) belongs
  18539. ;; to the newly created item.
  18540. (let ((section-end (save-excursion
  18541. (or (outline-next-heading) (point)))))
  18542. (forward-line)
  18543. (funcall shift-text
  18544. (+ start-ind (* (1+ delta) bul-len))
  18545. (min end section-end)))))))
  18546. ;; Case 3. Normal line with ARG: make the first line of region
  18547. ;; an item, and shift indentation of others lines to
  18548. ;; set them as item's body.
  18549. (arg (let* ((bul (org-list-bullet-string "-"))
  18550. (bul-len (length bul))
  18551. (ref-ind (org-get-indentation)))
  18552. (skip-chars-forward " \t")
  18553. (insert bul)
  18554. (forward-line)
  18555. (while (< (point) end)
  18556. ;; Ensure that lines less indented than first one
  18557. ;; still get included in item body.
  18558. (funcall shift-text
  18559. (+ ref-ind bul-len)
  18560. (min end (save-excursion (or (outline-next-heading)
  18561. (point)))))
  18562. (forward-line))))
  18563. ;; Case 4. Normal line without ARG: turn each non-item line
  18564. ;; into an item.
  18565. (t
  18566. (while (< (point) end)
  18567. (unless (or (org-at-heading-p) (org-at-item-p))
  18568. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18569. (replace-match
  18570. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18571. (forward-line))))))))
  18572. (defun org-toggle-heading (&optional nstars)
  18573. "Convert headings to normal text, or items or text to headings.
  18574. If there is no active region, only convert the current line.
  18575. With a \\[universal-argument] prefix, convert the whole list at
  18576. point into heading.
  18577. In a region:
  18578. - If the first non blank line is a headline, remove the stars
  18579. from all headlines in the region.
  18580. - If it is a normal line, turn each and every normal line (i.e.,
  18581. not an heading or an item) in the region into headings. If you
  18582. want to convert only the first line of this region, use one
  18583. universal prefix argument.
  18584. - If it is a plain list item, turn all plain list items into headings.
  18585. When converting a line into a heading, the number of stars is chosen
  18586. such that the lines become children of the current entry. However,
  18587. when a numeric prefix argument is given, its value determines the
  18588. number of stars to add."
  18589. (interactive "P")
  18590. (let ((skip-blanks
  18591. (function
  18592. ;; Return beginning of first non-blank line, starting from
  18593. ;; line at POS.
  18594. (lambda (pos)
  18595. (save-excursion
  18596. (goto-char pos)
  18597. (while (org-at-comment-p) (forward-line))
  18598. (skip-chars-forward " \r\t\n")
  18599. (point-at-bol)))))
  18600. beg end toggled)
  18601. ;; Determine boundaries of changes. If a universal prefix has
  18602. ;; been given, put the list in a region. If region ends at a bol,
  18603. ;; do not consider the last line to be in the region.
  18604. (when (and current-prefix-arg (org-at-item-p))
  18605. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18606. (org-mark-element))
  18607. (if (org-region-active-p)
  18608. (setq beg (funcall skip-blanks (region-beginning))
  18609. end (copy-marker (save-excursion
  18610. (goto-char (region-end))
  18611. (if (bolp) (point) (point-at-eol)))))
  18612. (setq beg (funcall skip-blanks (point-at-bol))
  18613. end (copy-marker (point-at-eol))))
  18614. ;; Ensure inline tasks don't count as headings.
  18615. (org-with-limited-levels
  18616. (save-excursion
  18617. (goto-char beg)
  18618. (cond
  18619. ;; Case 1. Started at an heading: de-star headings.
  18620. ((org-at-heading-p)
  18621. (while (< (point) end)
  18622. (when (org-at-heading-p t)
  18623. (looking-at org-outline-regexp) (replace-match "")
  18624. (setq toggled t))
  18625. (forward-line)))
  18626. ;; Case 2. Started at an item: change items into headlines.
  18627. ;; One star will be added by `org-list-to-subtree'.
  18628. ((org-at-item-p)
  18629. (let* ((stars (make-string
  18630. ;; subtract the star that will be added again by
  18631. ;; `org-list-to-subtree'
  18632. (if (numberp nstars) (1- nstars)
  18633. (or (org-current-level) 0))
  18634. ?*))
  18635. (add-stars
  18636. (cond (nstars "") ; stars from prefix only
  18637. ((equal stars "") "") ; before first heading
  18638. (org-odd-levels-only "*") ; inside heading, odd
  18639. (t "")))) ; inside heading, oddeven
  18640. (while (< (point) end)
  18641. (when (org-at-item-p)
  18642. ;; Pay attention to cases when region ends before list.
  18643. (let* ((struct (org-list-struct))
  18644. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18645. (save-restriction
  18646. (narrow-to-region (point) list-end)
  18647. (insert
  18648. (org-list-to-subtree
  18649. (org-list-parse-list t)
  18650. '(:istart (concat stars add-stars (funcall get-stars depth))
  18651. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18652. (setq toggled t))
  18653. (forward-line))))
  18654. ;; Case 3. Started at normal text: make every line an heading,
  18655. ;; skipping headlines and items.
  18656. (t (let* ((stars
  18657. (make-string
  18658. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18659. (add-stars
  18660. (cond (nstars "") ; stars from prefix only
  18661. ((equal stars "") "*") ; before first heading
  18662. (org-odd-levels-only "**") ; inside heading, odd
  18663. (t "*"))) ; inside heading, oddeven
  18664. (rpl (concat stars add-stars " "))
  18665. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18666. (while (< (point) (if (equal nstars '(4)) lend end))
  18667. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18668. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18669. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18670. (forward-line)))))))
  18671. (unless toggled (message "Cannot toggle heading from here"))))
  18672. (defun org-meta-return (&optional arg)
  18673. "Insert a new heading or wrap a region in a table.
  18674. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18675. See the individual commands for more information."
  18676. (interactive "P")
  18677. (cond
  18678. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18679. ((or (org-at-drawer-p) (org-at-property-p))
  18680. (newline-and-indent))
  18681. ((org-at-table-p)
  18682. (call-interactively 'org-table-wrap-region))
  18683. (t (call-interactively 'org-insert-heading))))
  18684. ;;; Menu entries
  18685. (defsubst org-in-subtree-not-table-p ()
  18686. "Are we in a subtree and not in a table?"
  18687. (and (not (org-before-first-heading-p))
  18688. (not (org-at-table-p))))
  18689. ;; Define the Org-mode menus
  18690. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18691. '("Tbl"
  18692. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18693. ["Next Field" org-cycle (org-at-table-p)]
  18694. ["Previous Field" org-shifttab (org-at-table-p)]
  18695. ["Next Row" org-return (org-at-table-p)]
  18696. "--"
  18697. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18698. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18699. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18700. "--"
  18701. ("Column"
  18702. ["Move Column Left" org-metaleft (org-at-table-p)]
  18703. ["Move Column Right" org-metaright (org-at-table-p)]
  18704. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18705. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18706. ("Row"
  18707. ["Move Row Up" org-metaup (org-at-table-p)]
  18708. ["Move Row Down" org-metadown (org-at-table-p)]
  18709. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18710. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18711. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18712. "--"
  18713. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18714. ("Rectangle"
  18715. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18716. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18717. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18718. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18719. "--"
  18720. ("Calculate"
  18721. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18722. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18723. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18724. "--"
  18725. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18726. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18727. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18728. "--"
  18729. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18730. "--"
  18731. ["Sum Column/Rectangle" org-table-sum
  18732. (or (org-at-table-p) (org-region-active-p))]
  18733. ["Which Column?" org-table-current-column (org-at-table-p)])
  18734. ["Debug Formulas"
  18735. org-table-toggle-formula-debugger
  18736. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18737. ["Show Col/Row Numbers"
  18738. org-table-toggle-coordinate-overlays
  18739. :style toggle
  18740. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18741. "--"
  18742. ["Create" org-table-create (and (not (org-at-table-p))
  18743. org-enable-table-editor)]
  18744. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18745. ["Import from File" org-table-import (not (org-at-table-p))]
  18746. ["Export to File" org-table-export (org-at-table-p)]
  18747. "--"
  18748. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18749. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18750. '("Org"
  18751. ("Show/Hide"
  18752. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18753. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18754. ["Sparse Tree..." org-sparse-tree t]
  18755. ["Reveal Context" org-reveal t]
  18756. ["Show All" show-all t]
  18757. "--"
  18758. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18759. "--"
  18760. ["New Heading" org-insert-heading t]
  18761. ("Navigate Headings"
  18762. ["Up" outline-up-heading t]
  18763. ["Next" outline-next-visible-heading t]
  18764. ["Previous" outline-previous-visible-heading t]
  18765. ["Next Same Level" outline-forward-same-level t]
  18766. ["Previous Same Level" outline-backward-same-level t]
  18767. "--"
  18768. ["Jump" org-goto t])
  18769. ("Edit Structure"
  18770. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18771. "--"
  18772. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18773. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18774. "--"
  18775. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18776. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18777. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18778. "--"
  18779. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18780. "--"
  18781. ["Copy visible text" org-copy-visible t]
  18782. "--"
  18783. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18784. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18785. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18786. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18787. "--"
  18788. ["Sort Region/Children" org-sort t]
  18789. "--"
  18790. ["Convert to odd levels" org-convert-to-odd-levels t]
  18791. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18792. ("Editing"
  18793. ["Emphasis..." org-emphasize t]
  18794. ["Edit Source Example" org-edit-special t]
  18795. "--"
  18796. ["Footnote new/jump" org-footnote-action t]
  18797. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18798. ("Archive"
  18799. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18800. "--"
  18801. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18802. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18803. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18804. )
  18805. "--"
  18806. ("Hyperlinks"
  18807. ["Store Link (Global)" org-store-link t]
  18808. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18809. ["Insert Link" org-insert-link t]
  18810. ["Follow Link" org-open-at-point t]
  18811. "--"
  18812. ["Next link" org-next-link t]
  18813. ["Previous link" org-previous-link t]
  18814. "--"
  18815. ["Descriptive Links"
  18816. org-toggle-link-display
  18817. :style radio
  18818. :selected org-descriptive-links
  18819. ]
  18820. ["Literal Links"
  18821. org-toggle-link-display
  18822. :style radio
  18823. :selected (not org-descriptive-links)])
  18824. "--"
  18825. ("TODO Lists"
  18826. ["TODO/DONE/-" org-todo t]
  18827. ("Select keyword"
  18828. ["Next keyword" org-shiftright (org-at-heading-p)]
  18829. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18830. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18831. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18832. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18833. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18834. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18835. "--"
  18836. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18837. :selected org-enforce-todo-dependencies :style toggle :active t]
  18838. "Settings for tree at point"
  18839. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18840. :selected (org-entry-get nil "ORDERED")
  18841. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18842. ["Do Children parallel" org-toggle-ordered-property :style radio
  18843. :selected (not (org-entry-get nil "ORDERED"))
  18844. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18845. "--"
  18846. ["Set Priority" org-priority t]
  18847. ["Priority Up" org-shiftup t]
  18848. ["Priority Down" org-shiftdown t]
  18849. "--"
  18850. ["Get news from all feeds" org-feed-update-all t]
  18851. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18852. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18853. ("TAGS and Properties"
  18854. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18855. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18856. "--"
  18857. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18858. ["Column view of properties" org-columns t]
  18859. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18860. ("Dates and Scheduling"
  18861. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18862. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18863. ("Change Date"
  18864. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18865. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18866. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18867. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18868. ["Compute Time Range" org-evaluate-time-range t]
  18869. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18870. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18871. "--"
  18872. ["Custom time format" org-toggle-time-stamp-overlays
  18873. :style radio :selected org-display-custom-times]
  18874. "--"
  18875. ["Goto Calendar" org-goto-calendar t]
  18876. ["Date from Calendar" org-date-from-calendar t]
  18877. "--"
  18878. ["Start/Restart Timer" org-timer-start t]
  18879. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18880. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18881. ["Insert Timer String" org-timer t]
  18882. ["Insert Timer Item" org-timer-item t])
  18883. ("Logging work"
  18884. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18885. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18886. ["Clock out" org-clock-out t]
  18887. ["Clock cancel" org-clock-cancel t]
  18888. "--"
  18889. ["Mark as default task" org-clock-mark-default-task t]
  18890. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18891. ["Goto running clock" org-clock-goto t]
  18892. "--"
  18893. ["Display times" org-clock-display t]
  18894. ["Create clock table" org-clock-report t]
  18895. "--"
  18896. ["Record DONE time"
  18897. (progn (setq org-log-done (not org-log-done))
  18898. (message "Switching to %s will %s record a timestamp"
  18899. (car org-done-keywords)
  18900. (if org-log-done "automatically" "not")))
  18901. :style toggle :selected org-log-done])
  18902. "--"
  18903. ["Agenda Command..." org-agenda t]
  18904. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18905. ("File List for Agenda")
  18906. ("Special views current file"
  18907. ["TODO Tree" org-show-todo-tree t]
  18908. ["Check Deadlines" org-check-deadlines t]
  18909. ["Timeline" org-timeline t]
  18910. ["Tags/Property tree" org-match-sparse-tree t])
  18911. "--"
  18912. ["Export/Publish..." org-export-dispatch t]
  18913. ("LaTeX"
  18914. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18915. :selected org-cdlatex-mode]
  18916. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18917. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18918. ["Modify math symbol" org-cdlatex-math-modify
  18919. (org-inside-LaTeX-fragment-p)]
  18920. ["Insert citation" org-reftex-citation t]
  18921. "--"
  18922. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18923. "--"
  18924. ("MobileOrg"
  18925. ["Push Files and Views" org-mobile-push t]
  18926. ["Get Captured and Flagged" org-mobile-pull t]
  18927. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18928. "--"
  18929. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18930. "--"
  18931. ("Documentation"
  18932. ["Show Version" org-version t]
  18933. ["Info Documentation" org-info t])
  18934. ("Customize"
  18935. ["Browse Org Group" org-customize t]
  18936. "--"
  18937. ["Expand This Menu" org-create-customize-menu
  18938. (fboundp 'customize-menu-create)])
  18939. ["Send bug report" org-submit-bug-report t]
  18940. "--"
  18941. ("Refresh/Reload"
  18942. ["Refresh setup current buffer" org-mode-restart t]
  18943. ["Reload Org (after update)" org-reload t]
  18944. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18945. ))
  18946. (defun org-info (&optional node)
  18947. "Read documentation for Org-mode in the info system.
  18948. With optional NODE, go directly to that node."
  18949. (interactive)
  18950. (info (format "(org)%s" (or node ""))))
  18951. ;;;###autoload
  18952. (defun org-submit-bug-report ()
  18953. "Submit a bug report on Org-mode via mail.
  18954. Don't hesitate to report any problems or inaccurate documentation.
  18955. If you don't have setup sending mail from (X)Emacs, please copy the
  18956. output buffer into your mail program, as it gives us important
  18957. information about your Org-mode version and configuration."
  18958. (interactive)
  18959. (require 'reporter)
  18960. (org-load-modules-maybe)
  18961. (org-require-autoloaded-modules)
  18962. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18963. (reporter-submit-bug-report
  18964. "emacs-orgmode@gnu.org"
  18965. (org-version nil 'full)
  18966. (let (list)
  18967. (save-window-excursion
  18968. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18969. (delete-other-windows)
  18970. (erase-buffer)
  18971. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18972. We would like to add your full Org-mode and Outline configuration to the
  18973. bug report. This greatly simplifies the work of the maintainer and
  18974. other experts on the mailing list.
  18975. HOWEVER, some variables you have customized may contain private
  18976. information. The names of customers, colleagues, or friends, might
  18977. appear in the form of file names, tags, todo states, or search strings.
  18978. If you answer yes to the prompt, you might want to check and remove
  18979. such private information before sending the email.")
  18980. (add-text-properties (point-min) (point-max) '(face org-warning))
  18981. (when (yes-or-no-p "Include your Org-mode configuration ")
  18982. (mapatoms
  18983. (lambda (v)
  18984. (and (boundp v)
  18985. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18986. (or (and (symbol-value v)
  18987. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18988. (and
  18989. (get v 'custom-type) (get v 'standard-value)
  18990. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18991. (push v list)))))
  18992. (kill-buffer (get-buffer "*Warn about privacy*"))
  18993. list))
  18994. nil nil
  18995. "Remember to cover the basics, that is, what you expected to happen and
  18996. what in fact did happen. You don't know how to make a good report? See
  18997. http://orgmode.org/manual/Feedback.html#Feedback
  18998. Your bug report will be posted to the Org-mode mailing list.
  18999. ------------------------------------------------------------------------")
  19000. (save-excursion
  19001. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19002. (replace-match "\\1Bug: \\3 [\\2]")))))
  19003. (defun org-install-agenda-files-menu ()
  19004. (let ((bl (buffer-list)))
  19005. (save-excursion
  19006. (while bl
  19007. (set-buffer (pop bl))
  19008. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19009. (when (derived-mode-p 'org-mode)
  19010. (easy-menu-change
  19011. '("Org") "File List for Agenda"
  19012. (append
  19013. (list
  19014. ["Edit File List" (org-edit-agenda-file-list) t]
  19015. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19016. ["Remove Current File from List" org-remove-file t]
  19017. ["Cycle through agenda files" org-cycle-agenda-files t]
  19018. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19019. "--")
  19020. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19021. ;;;; Documentation
  19022. (defun org-require-autoloaded-modules ()
  19023. (interactive)
  19024. (mapc 'require
  19025. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19026. org-remember org-table org-timer)))
  19027. ;;;###autoload
  19028. (defun org-reload (&optional uncompiled)
  19029. "Reload all org lisp files.
  19030. With prefix arg UNCOMPILED, load the uncompiled versions."
  19031. (interactive "P")
  19032. (require 'loadhist)
  19033. (let* ((org-dir (org-find-library-dir "org"))
  19034. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19035. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19036. (remove-re (mapconcat 'identity
  19037. (mapcar (lambda (f) (concat "^" f "$"))
  19038. (list (if (featurep 'xemacs)
  19039. "org-colview"
  19040. "org-colview-xemacs")
  19041. "org" "org-loaddefs" "org-version"))
  19042. "\\|"))
  19043. (feats (delete-dups
  19044. (mapcar 'file-name-sans-extension
  19045. (mapcar 'file-name-nondirectory
  19046. (delq nil
  19047. (mapcar 'feature-file
  19048. features))))))
  19049. (lfeat (append
  19050. (sort
  19051. (setq feats
  19052. (delq nil (mapcar
  19053. (lambda (f)
  19054. (if (and (string-match feature-re f)
  19055. (not (string-match remove-re f)))
  19056. f nil))
  19057. feats)))
  19058. 'string-lessp)
  19059. (list "org-version" "org")))
  19060. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19061. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19062. load-uncore load-misses)
  19063. (setq load-misses
  19064. (delq 't
  19065. (mapcar (lambda (f)
  19066. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19067. (and (string= org-dir contrib-dir)
  19068. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19069. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19070. (add-to-list 'load-uncore f 'append)
  19071. 't)
  19072. f))
  19073. lfeat)))
  19074. (if load-uncore
  19075. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19076. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19077. (if load-misses
  19078. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19079. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19080. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19081. ;;;###autoload
  19082. (defun org-customize ()
  19083. "Call the customize function with org as argument."
  19084. (interactive)
  19085. (org-load-modules-maybe)
  19086. (org-require-autoloaded-modules)
  19087. (customize-browse 'org))
  19088. (defun org-create-customize-menu ()
  19089. "Create a full customization menu for Org-mode, insert it into the menu."
  19090. (interactive)
  19091. (org-load-modules-maybe)
  19092. (org-require-autoloaded-modules)
  19093. (if (fboundp 'customize-menu-create)
  19094. (progn
  19095. (easy-menu-change
  19096. '("Org") "Customize"
  19097. `(["Browse Org group" org-customize t]
  19098. "--"
  19099. ,(customize-menu-create 'org)
  19100. ["Set" Custom-set t]
  19101. ["Save" Custom-save t]
  19102. ["Reset to Current" Custom-reset-current t]
  19103. ["Reset to Saved" Custom-reset-saved t]
  19104. ["Reset to Standard Settings" Custom-reset-standard t]))
  19105. (message "\"Org\"-menu now contains full customization menu"))
  19106. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19107. ;;;; Miscellaneous stuff
  19108. ;;; Generally useful functions
  19109. (defun org-get-at-bol (property)
  19110. "Get text property PROPERTY at beginning of line."
  19111. (get-text-property (point-at-bol) property))
  19112. (defun org-find-text-property-in-string (prop s)
  19113. "Return the first non-nil value of property PROP in string S."
  19114. (or (get-text-property 0 prop s)
  19115. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19116. prop s)))
  19117. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19118. "Display the given MESSAGE as a warning."
  19119. (if (fboundp 'display-warning)
  19120. (display-warning 'org message
  19121. (if (featurep 'xemacs) 'warning :warning))
  19122. (let ((buf (get-buffer-create "*Org warnings*")))
  19123. (with-current-buffer buf
  19124. (goto-char (point-max))
  19125. (insert "Warning (Org): " message)
  19126. (unless (bolp)
  19127. (newline)))
  19128. (display-buffer buf)
  19129. (sit-for 0))))
  19130. (defun org-eval (form)
  19131. "Eval FORM and return result."
  19132. (condition-case error
  19133. (eval form)
  19134. (error (format "%%![Error: %s]" error))))
  19135. (defun org-in-clocktable-p ()
  19136. "Check if the cursor is in a clocktable."
  19137. (let ((pos (point)) start)
  19138. (save-excursion
  19139. (end-of-line 1)
  19140. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19141. (setq start (match-beginning 0))
  19142. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19143. (>= (match-end 0) pos)
  19144. start))))
  19145. (defun org-in-commented-line ()
  19146. "Is point in a line starting with `#'?"
  19147. (equal (char-after (point-at-bol)) ?#))
  19148. (defun org-in-indented-comment-line ()
  19149. "Is point in a line starting with `#' after some white space?"
  19150. (save-excursion
  19151. (save-match-data
  19152. (goto-char (point-at-bol))
  19153. (looking-at "[ \t]*#"))))
  19154. (defun org-in-verbatim-emphasis ()
  19155. (save-match-data
  19156. (and (org-in-regexp org-emph-re 2)
  19157. (>= (point) (match-beginning 3))
  19158. (<= (point) (match-end 4))
  19159. (member (match-string 3) '("=" "~")))))
  19160. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19161. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19162. (if (and marker (marker-buffer marker)
  19163. (buffer-live-p (marker-buffer marker)))
  19164. (progn
  19165. (org-pop-to-buffer-same-window (marker-buffer marker))
  19166. (if (or (> marker (point-max)) (< marker (point-min)))
  19167. (widen))
  19168. (goto-char marker)
  19169. (org-show-context 'org-goto))
  19170. (if bookmark
  19171. (bookmark-jump bookmark)
  19172. (error "Cannot find location"))))
  19173. (defun org-quote-csv-field (s)
  19174. "Quote field for inclusion in CSV material."
  19175. (if (string-match "[\",]" s)
  19176. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19177. s))
  19178. (defun org-force-self-insert (N)
  19179. "Needed to enforce self-insert under remapping."
  19180. (interactive "p")
  19181. (self-insert-command N))
  19182. (defun org-string-width (s)
  19183. "Compute width of string, ignoring invisible characters.
  19184. This ignores character with invisibility property `org-link', and also
  19185. characters with property `org-cwidth', because these will become invisible
  19186. upon the next fontification round."
  19187. (let (b l)
  19188. (when (or (eq t buffer-invisibility-spec)
  19189. (assq 'org-link buffer-invisibility-spec))
  19190. (while (setq b (text-property-any 0 (length s)
  19191. 'invisible 'org-link s))
  19192. (setq s (concat (substring s 0 b)
  19193. (substring s (or (next-single-property-change
  19194. b 'invisible s) (length s)))))))
  19195. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19196. (setq s (concat (substring s 0 b)
  19197. (substring s (or (next-single-property-change
  19198. b 'org-cwidth s) (length s))))))
  19199. (setq l (string-width s) b -1)
  19200. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19201. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19202. l))
  19203. (defun org-shorten-string (s maxlength)
  19204. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19205. If the string is shorter or has length MAXLENGTH, just return the
  19206. original string. If it is longer, the functions finds a space in the
  19207. string, breaks this string off at that locations and adds three dots
  19208. as ellipsis. Including the ellipsis, the string will not be longer
  19209. than MAXLENGTH. If finding a good breaking point in the string does
  19210. not work, the string is just chopped off in the middle of a word
  19211. if necessary."
  19212. (if (<= (length s) maxlength)
  19213. s
  19214. (let* ((n (max (- maxlength 4) 1))
  19215. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19216. (if (string-match re s)
  19217. (concat (match-string 1 s) "...")
  19218. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19219. (defun org-get-indentation (&optional line)
  19220. "Get the indentation of the current line, interpreting tabs.
  19221. When LINE is given, assume it represents a line and compute its indentation."
  19222. (if line
  19223. (if (string-match "^ *" (org-remove-tabs line))
  19224. (match-end 0))
  19225. (save-excursion
  19226. (beginning-of-line 1)
  19227. (skip-chars-forward " \t")
  19228. (current-column))))
  19229. (defun org-get-string-indentation (s)
  19230. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19231. (let ((n -1) (i 0) (w tab-width) c)
  19232. (catch 'exit
  19233. (while (< (setq n (1+ n)) (length s))
  19234. (setq c (aref s n))
  19235. (cond ((= c ?\ ) (setq i (1+ i)))
  19236. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19237. (t (throw 'exit t)))))
  19238. i))
  19239. (defun org-remove-tabs (s &optional width)
  19240. "Replace tabulators in S with spaces.
  19241. Assumes that s is a single line, starting in column 0."
  19242. (setq width (or width tab-width))
  19243. (while (string-match "\t" s)
  19244. (setq s (replace-match
  19245. (make-string
  19246. (- (* width (/ (+ (match-beginning 0) width) width))
  19247. (match-beginning 0)) ?\ )
  19248. t t s)))
  19249. s)
  19250. (defun org-fix-indentation (line ind)
  19251. "Fix indentation in LINE.
  19252. IND is a cons cell with target and minimum indentation.
  19253. If the current indentation in LINE is smaller than the minimum,
  19254. leave it alone. If it is larger than ind, set it to the target."
  19255. (let* ((l (org-remove-tabs line))
  19256. (i (org-get-indentation l))
  19257. (i1 (car ind)) (i2 (cdr ind)))
  19258. (if (>= i i2) (setq l (substring line i2)))
  19259. (if (> i1 0)
  19260. (concat (make-string i1 ?\ ) l)
  19261. l)))
  19262. (defun org-remove-indentation (code &optional n)
  19263. "Remove the maximum common indentation from the lines in CODE.
  19264. N may optionally be the number of spaces to remove."
  19265. (with-temp-buffer
  19266. (insert code)
  19267. (org-do-remove-indentation n)
  19268. (buffer-string)))
  19269. (defun org-do-remove-indentation (&optional n)
  19270. "Remove the maximum common indentation from the buffer."
  19271. (untabify (point-min) (point-max))
  19272. (let ((min 10000) re)
  19273. (if n
  19274. (setq min n)
  19275. (goto-char (point-min))
  19276. (while (re-search-forward "^ *[^ \n]" nil t)
  19277. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19278. (unless (or (= min 0) (= min 10000))
  19279. (setq re (format "^ \\{%d\\}" min))
  19280. (goto-char (point-min))
  19281. (while (re-search-forward re nil t)
  19282. (replace-match "")
  19283. (end-of-line 1))
  19284. min)))
  19285. (defun org-fill-template (template alist)
  19286. "Find each %key of ALIST in TEMPLATE and replace it."
  19287. (let ((case-fold-search nil)
  19288. entry key value)
  19289. (setq alist (sort (copy-sequence alist)
  19290. (lambda (a b) (< (length (car a)) (length (car b))))))
  19291. (while (setq entry (pop alist))
  19292. (setq template
  19293. (replace-regexp-in-string
  19294. (concat "%" (regexp-quote (car entry)))
  19295. (or (cdr entry) "") template t t)))
  19296. template))
  19297. (defun org-base-buffer (buffer)
  19298. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19299. (if (not buffer)
  19300. buffer
  19301. (or (buffer-base-buffer buffer)
  19302. buffer)))
  19303. (defun org-trim (s)
  19304. "Remove whitespace at beginning and end of string."
  19305. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19306. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19307. s)
  19308. (defun org-wrap (string &optional width lines)
  19309. "Wrap string to either a number of lines, or a width in characters.
  19310. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19311. that costs. If there is a word longer than WIDTH, the text is actually
  19312. wrapped to the length of that word.
  19313. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19314. many lines, whatever width that takes.
  19315. The return value is a list of lines, without newlines at the end."
  19316. (let* ((words (org-split-string string "[ \t\n]+"))
  19317. (maxword (apply 'max (mapcar 'org-string-width words)))
  19318. w ll)
  19319. (cond (width
  19320. (org-do-wrap words (max maxword width)))
  19321. (lines
  19322. (setq w maxword)
  19323. (setq ll (org-do-wrap words maxword))
  19324. (if (<= (length ll) lines)
  19325. ll
  19326. (setq ll words)
  19327. (while (> (length ll) lines)
  19328. (setq w (1+ w))
  19329. (setq ll (org-do-wrap words w)))
  19330. ll))
  19331. (t (error "Cannot wrap this")))))
  19332. (defun org-do-wrap (words width)
  19333. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19334. (let (lines line)
  19335. (while words
  19336. (setq line (pop words))
  19337. (while (and words (< (+ (length line) (length (car words))) width))
  19338. (setq line (concat line " " (pop words))))
  19339. (setq lines (push line lines)))
  19340. (nreverse lines)))
  19341. (defun org-split-string (string &optional separators)
  19342. "Splits STRING into substrings at SEPARATORS.
  19343. No empty strings are returned if there are matches at the beginning
  19344. and end of string."
  19345. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19346. (start 0)
  19347. notfirst
  19348. (list nil))
  19349. (while (and (string-match rexp string
  19350. (if (and notfirst
  19351. (= start (match-beginning 0))
  19352. (< start (length string)))
  19353. (1+ start) start))
  19354. (< (match-beginning 0) (length string)))
  19355. (setq notfirst t)
  19356. (or (eq (match-beginning 0) 0)
  19357. (and (eq (match-beginning 0) (match-end 0))
  19358. (eq (match-beginning 0) start))
  19359. (setq list
  19360. (cons (substring string start (match-beginning 0))
  19361. list)))
  19362. (setq start (match-end 0)))
  19363. (or (eq start (length string))
  19364. (setq list
  19365. (cons (substring string start)
  19366. list)))
  19367. (nreverse list)))
  19368. (defun org-quote-vert (s)
  19369. "Replace \"|\" with \"\\vert\"."
  19370. (while (string-match "|" s)
  19371. (setq s (replace-match "\\vert" t t s)))
  19372. s)
  19373. (defun org-uuidgen-p (s)
  19374. "Is S an ID created by UUIDGEN?"
  19375. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19376. (defun org-in-src-block-p (&optional inside)
  19377. "Whether point is in a code source block.
  19378. When INSIDE is non-nil, don't consider we are within a src block
  19379. when point is at #+BEGIN_SRC or #+END_SRC."
  19380. (let ((case-fold-search t) ov)
  19381. (or (and (setq ov (overlays-at (point)))
  19382. (memq 'org-block-background
  19383. (overlay-properties (car ov))))
  19384. (and (not inside)
  19385. (save-match-data
  19386. (save-excursion
  19387. (beginning-of-line)
  19388. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19389. (defun org-context ()
  19390. "Return a list of contexts of the current cursor position.
  19391. If several contexts apply, all are returned.
  19392. Each context entry is a list with a symbol naming the context, and
  19393. two positions indicating start and end of the context. Possible
  19394. contexts are:
  19395. :headline anywhere in a headline
  19396. :headline-stars on the leading stars in a headline
  19397. :todo-keyword on a TODO keyword (including DONE) in a headline
  19398. :tags on the TAGS in a headline
  19399. :priority on the priority cookie in a headline
  19400. :item on the first line of a plain list item
  19401. :item-bullet on the bullet/number of a plain list item
  19402. :checkbox on the checkbox in a plain list item
  19403. :table in an org-mode table
  19404. :table-special on a special filed in a table
  19405. :table-table in a table.el table
  19406. :clocktable in a clocktable
  19407. :src-block in a source block
  19408. :link on a hyperlink
  19409. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19410. :target on a <<target>>
  19411. :radio-target on a <<<radio-target>>>
  19412. :latex-fragment on a LaTeX fragment
  19413. :latex-preview on a LaTeX fragment with overlaid preview image
  19414. This function expects the position to be visible because it uses font-lock
  19415. faces as a help to recognize the following contexts: :table-special, :link,
  19416. and :keyword."
  19417. (let* ((f (get-text-property (point) 'face))
  19418. (faces (if (listp f) f (list f)))
  19419. (case-fold-search t)
  19420. (p (point)) clist o)
  19421. ;; First the large context
  19422. (cond
  19423. ((org-at-heading-p t)
  19424. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19425. (when (progn
  19426. (beginning-of-line 1)
  19427. (looking-at org-todo-line-tags-regexp))
  19428. (push (org-point-in-group p 1 :headline-stars) clist)
  19429. (push (org-point-in-group p 2 :todo-keyword) clist)
  19430. (push (org-point-in-group p 4 :tags) clist))
  19431. (goto-char p)
  19432. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19433. (if (looking-at "\\[#[A-Z0-9]\\]")
  19434. (push (org-point-in-group p 0 :priority) clist)))
  19435. ((org-at-item-p)
  19436. (push (org-point-in-group p 2 :item-bullet) clist)
  19437. (push (list :item (point-at-bol)
  19438. (save-excursion (org-end-of-item) (point)))
  19439. clist)
  19440. (and (org-at-item-checkbox-p)
  19441. (push (org-point-in-group p 0 :checkbox) clist)))
  19442. ((org-at-table-p)
  19443. (push (list :table (org-table-begin) (org-table-end)) clist)
  19444. (if (memq 'org-formula faces)
  19445. (push (list :table-special
  19446. (previous-single-property-change p 'face)
  19447. (next-single-property-change p 'face)) clist)))
  19448. ((org-at-table-p 'any)
  19449. (push (list :table-table) clist)))
  19450. (goto-char p)
  19451. (let ((case-fold-search t))
  19452. ;; New the "medium" contexts: clocktables, source blocks
  19453. (cond ((org-in-clocktable-p)
  19454. (push (list :clocktable
  19455. (and (or (looking-at "#\\+BEGIN: clocktable")
  19456. (search-backward "#+BEGIN: clocktable" nil t))
  19457. (match-beginning 0))
  19458. (and (re-search-forward "#\\+END:?" nil t)
  19459. (match-end 0))) clist))
  19460. ((org-in-src-block-p)
  19461. (push (list :src-block
  19462. (and (or (looking-at "#\\+BEGIN_SRC")
  19463. (search-backward "#+BEGIN_SRC" nil t))
  19464. (match-beginning 0))
  19465. (and (search-forward "#+END_SRC" nil t)
  19466. (match-beginning 0))) clist))))
  19467. (goto-char p)
  19468. ;; Now the small context
  19469. (cond
  19470. ((org-at-timestamp-p)
  19471. (push (org-point-in-group p 0 :timestamp) clist))
  19472. ((memq 'org-link faces)
  19473. (push (list :link
  19474. (previous-single-property-change p 'face)
  19475. (next-single-property-change p 'face)) clist))
  19476. ((memq 'org-special-keyword faces)
  19477. (push (list :keyword
  19478. (previous-single-property-change p 'face)
  19479. (next-single-property-change p 'face)) clist))
  19480. ((org-at-target-p)
  19481. (push (org-point-in-group p 0 :target) clist)
  19482. (goto-char (1- (match-beginning 0)))
  19483. (if (looking-at org-radio-target-regexp)
  19484. (push (org-point-in-group p 0 :radio-target) clist))
  19485. (goto-char p))
  19486. ((setq o (car (delq nil
  19487. (mapcar
  19488. (lambda (x)
  19489. (if (memq x org-latex-fragment-image-overlays) x))
  19490. (overlays-at (point))))))
  19491. (push (list :latex-fragment
  19492. (overlay-start o) (overlay-end o)) clist)
  19493. (push (list :latex-preview
  19494. (overlay-start o) (overlay-end o)) clist))
  19495. ((org-inside-LaTeX-fragment-p)
  19496. ;; FIXME: positions wrong.
  19497. (push (list :latex-fragment (point) (point)) clist)))
  19498. (setq clist (nreverse (delq nil clist)))
  19499. clist))
  19500. ;; FIXME: Compare with at-regexp-p Do we need both?
  19501. (defun org-in-regexp (re &optional nlines visually)
  19502. "Check if point is inside a match of regexp.
  19503. Normally only the current line is checked, but you can include NLINES extra
  19504. lines both before and after point into the search.
  19505. If VISUALLY is set, require that the cursor is not after the match but
  19506. really on, so that the block visually is on the match."
  19507. (catch 'exit
  19508. (let ((pos (point))
  19509. (eol (point-at-eol (+ 1 (or nlines 0))))
  19510. (inc (if visually 1 0)))
  19511. (save-excursion
  19512. (beginning-of-line (- 1 (or nlines 0)))
  19513. (while (re-search-forward re eol t)
  19514. (if (and (<= (match-beginning 0) pos)
  19515. (>= (+ inc (match-end 0)) pos))
  19516. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19517. (defun org-at-regexp-p (regexp)
  19518. "Is point inside a match of REGEXP in the current line?"
  19519. (catch 'exit
  19520. (save-excursion
  19521. (let ((pos (point)) (end (point-at-eol)))
  19522. (beginning-of-line 1)
  19523. (while (re-search-forward regexp end t)
  19524. (if (and (<= (match-beginning 0) pos)
  19525. (>= (match-end 0) pos))
  19526. (throw 'exit t)))
  19527. nil))))
  19528. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19529. "Non-nil when point is between matches of START-RE and END-RE.
  19530. Also return a non-nil value when point is on one of the matches.
  19531. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19532. buffer positions. Default values are the positions of headlines
  19533. surrounding the point.
  19534. The functions returns a cons cell whose car (resp. cdr) is the
  19535. position before START-RE (resp. after END-RE)."
  19536. (save-match-data
  19537. (let ((pos (point))
  19538. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19539. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19540. beg end)
  19541. (save-excursion
  19542. ;; Point is on a block when on START-RE or if START-RE can be
  19543. ;; found before it...
  19544. (and (or (org-at-regexp-p start-re)
  19545. (re-search-backward start-re limit-up t))
  19546. (setq beg (match-beginning 0))
  19547. ;; ... and END-RE after it...
  19548. (goto-char (match-end 0))
  19549. (re-search-forward end-re limit-down t)
  19550. (> (setq end (match-end 0)) pos)
  19551. ;; ... without another START-RE in-between.
  19552. (goto-char (match-beginning 0))
  19553. (not (re-search-backward start-re (1+ beg) t))
  19554. ;; Return value.
  19555. (cons beg end))))))
  19556. (defun org-in-block-p (names)
  19557. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19558. NAMES is a list of strings containing names of blocks.
  19559. Return first block name matched, or nil. Beware that in case of
  19560. nested blocks, the returned name may not belong to the closest
  19561. block from point."
  19562. (save-match-data
  19563. (catch 'exit
  19564. (let ((case-fold-search t)
  19565. (lim-up (save-excursion (outline-previous-heading)))
  19566. (lim-down (save-excursion (outline-next-heading))))
  19567. (mapc (lambda (name)
  19568. (let ((n (regexp-quote name)))
  19569. (when (org-between-regexps-p
  19570. (concat "^[ \t]*#\\+begin_" n)
  19571. (concat "^[ \t]*#\\+end_" n)
  19572. lim-up lim-down)
  19573. (throw 'exit n))))
  19574. names))
  19575. nil)))
  19576. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19577. "Call `multi-occur' with buffers for all agenda files."
  19578. (interactive "sOrg-files matching: \np")
  19579. (let* ((files (org-agenda-files))
  19580. (tnames (mapcar 'file-truename files))
  19581. (extra org-agenda-text-search-extra-files)
  19582. f)
  19583. (when (eq (car extra) 'agenda-archives)
  19584. (setq extra (cdr extra))
  19585. (setq files (org-add-archive-files files)))
  19586. (while (setq f (pop extra))
  19587. (unless (member (file-truename f) tnames)
  19588. (add-to-list 'files f 'append)
  19589. (add-to-list 'tnames (file-truename f) 'append)))
  19590. (multi-occur
  19591. (mapcar (lambda (x)
  19592. (with-current-buffer
  19593. (or (get-file-buffer x) (find-file-noselect x))
  19594. (widen)
  19595. (current-buffer)))
  19596. files)
  19597. regexp)))
  19598. (if (boundp 'occur-mode-find-occurrence-hook)
  19599. ;; Emacs 23
  19600. (add-hook 'occur-mode-find-occurrence-hook
  19601. (lambda ()
  19602. (when (derived-mode-p 'org-mode)
  19603. (org-reveal))))
  19604. ;; Emacs 22
  19605. (defadvice occur-mode-goto-occurrence
  19606. (after org-occur-reveal activate)
  19607. (and (derived-mode-p 'org-mode) (org-reveal)))
  19608. (defadvice occur-mode-goto-occurrence-other-window
  19609. (after org-occur-reveal activate)
  19610. (and (derived-mode-p 'org-mode) (org-reveal)))
  19611. (defadvice occur-mode-display-occurrence
  19612. (after org-occur-reveal activate)
  19613. (when (derived-mode-p 'org-mode)
  19614. (let ((pos (occur-mode-find-occurrence)))
  19615. (with-current-buffer (marker-buffer pos)
  19616. (save-excursion
  19617. (goto-char pos)
  19618. (org-reveal)))))))
  19619. (defun org-occur-link-in-agenda-files ()
  19620. "Create a link and search for it in the agendas.
  19621. The link is not stored in `org-stored-links', it is just created
  19622. for the search purpose."
  19623. (interactive)
  19624. (let ((link (condition-case nil
  19625. (org-store-link nil)
  19626. (error "Unable to create a link to here"))))
  19627. (org-occur-in-agenda-files (regexp-quote link))))
  19628. (defun org-reverse-string (string)
  19629. "Return the reverse of STRING."
  19630. (apply 'string (reverse (string-to-list string))))
  19631. (defun org-uniquify (list)
  19632. "Remove duplicate elements from LIST."
  19633. (let (res)
  19634. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19635. res))
  19636. (defun org-delete-all (elts list)
  19637. "Remove all elements in ELTS from LIST."
  19638. (while elts
  19639. (setq list (delete (pop elts) list)))
  19640. list)
  19641. (defun org-count (cl-item cl-seq)
  19642. "Count the number of occurrences of ITEM in SEQ.
  19643. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19644. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19645. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19646. (while (< cl-start cl-end)
  19647. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19648. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19649. (setq cl-start (1+ cl-start)))
  19650. cl-count))
  19651. (defun org-remove-if (predicate seq)
  19652. "Remove everything from SEQ that fulfills PREDICATE."
  19653. (let (res e)
  19654. (while seq
  19655. (setq e (pop seq))
  19656. (if (not (funcall predicate e)) (push e res)))
  19657. (nreverse res)))
  19658. (defun org-remove-if-not (predicate seq)
  19659. "Remove everything from SEQ that does not fulfill PREDICATE."
  19660. (let (res e)
  19661. (while seq
  19662. (setq e (pop seq))
  19663. (if (funcall predicate e) (push e res)))
  19664. (nreverse res)))
  19665. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19666. "Reduce two-argument FUNCTION across SEQ.
  19667. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19668. \":initial-value\" removed."
  19669. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19670. (cadr (memq :initial-value cl-keys)))
  19671. (cl-seq (pop cl-seq))
  19672. (t (funcall cl-func)))))
  19673. (while cl-seq
  19674. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19675. cl-accum))
  19676. (defun org-back-over-empty-lines ()
  19677. "Move backwards over whitespace, to the beginning of the first empty line.
  19678. Returns the number of empty lines passed."
  19679. (let ((pos (point)))
  19680. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19681. (skip-chars-backward " \t\n\r")
  19682. (unless (eobp)
  19683. (forward-line -1)))
  19684. (beginning-of-line 2)
  19685. (goto-char (min (point) pos))
  19686. (count-lines (point) pos)))
  19687. (defun org-skip-whitespace ()
  19688. (skip-chars-forward " \t\n\r"))
  19689. (defun org-point-in-group (point group &optional context)
  19690. "Check if POINT is in match-group GROUP.
  19691. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19692. match. If the match group does not exist or point is not inside it,
  19693. return nil."
  19694. (and (match-beginning group)
  19695. (>= point (match-beginning group))
  19696. (<= point (match-end group))
  19697. (if context
  19698. (list context (match-beginning group) (match-end group))
  19699. t)))
  19700. (defun org-switch-to-buffer-other-window (&rest args)
  19701. "Switch to buffer in a second window on the current frame.
  19702. In particular, do not allow pop-up frames.
  19703. Returns the newly created buffer."
  19704. (org-no-popups
  19705. (apply 'switch-to-buffer-other-window args)))
  19706. (defun org-combine-plists (&rest plists)
  19707. "Create a single property list from all plists in PLISTS.
  19708. The process starts by copying the first list, and then setting properties
  19709. from the other lists. Settings in the last list are the most significant
  19710. ones and overrule settings in the other lists."
  19711. (let ((rtn (copy-sequence (pop plists)))
  19712. p v ls)
  19713. (while plists
  19714. (setq ls (pop plists))
  19715. (while ls
  19716. (setq p (pop ls) v (pop ls))
  19717. (setq rtn (plist-put rtn p v))))
  19718. rtn))
  19719. (defun org-replace-escapes (string table)
  19720. "Replace %-escapes in STRING with values in TABLE.
  19721. TABLE is an association list with keys like \"%a\" and string values.
  19722. The sequences in STRING may contain normal field width and padding information,
  19723. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19724. so values can contain further %-escapes if they are define later in TABLE."
  19725. (let ((tbl (copy-alist table))
  19726. (case-fold-search nil)
  19727. (pchg 0)
  19728. e re rpl)
  19729. (while (setq e (pop tbl))
  19730. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19731. (when (and (cdr e) (string-match re (cdr e)))
  19732. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19733. (safe "SREF"))
  19734. (add-text-properties 0 3 (list 'sref sref) safe)
  19735. (setcdr e (replace-match safe t t (cdr e)))))
  19736. (while (string-match re string)
  19737. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19738. (cdr e)))
  19739. (setq string (replace-match rpl t t string))))
  19740. (while (setq pchg (next-property-change pchg string))
  19741. (let ((sref (get-text-property pchg 'sref string)))
  19742. (when (and sref (string-match "SREF" string pchg))
  19743. (setq string (replace-match sref t t string)))))
  19744. string))
  19745. (defun org-sublist (list start end)
  19746. "Return a section of LIST, from START to END.
  19747. Counting starts at 1."
  19748. (let (rtn (c start))
  19749. (setq list (nthcdr (1- start) list))
  19750. (while (and list (<= c end))
  19751. (push (pop list) rtn)
  19752. (setq c (1+ c)))
  19753. (nreverse rtn)))
  19754. (defun org-find-base-buffer-visiting (file)
  19755. "Like `find-buffer-visiting' but always return the base buffer and
  19756. not an indirect buffer."
  19757. (let ((buf (or (get-file-buffer file)
  19758. (find-buffer-visiting file))))
  19759. (if buf
  19760. (or (buffer-base-buffer buf) buf)
  19761. nil)))
  19762. (defun org-image-file-name-regexp (&optional extensions)
  19763. "Return regexp matching the file names of images.
  19764. If EXTENSIONS is given, only match these."
  19765. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19766. (image-file-name-regexp)
  19767. (let ((image-file-name-extensions
  19768. (or extensions
  19769. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19770. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19771. (concat "\\."
  19772. (regexp-opt (nconc (mapcar 'upcase
  19773. image-file-name-extensions)
  19774. image-file-name-extensions)
  19775. t)
  19776. "\\'"))))
  19777. (defun org-file-image-p (file &optional extensions)
  19778. "Return non-nil if FILE is an image."
  19779. (save-match-data
  19780. (string-match (org-image-file-name-regexp extensions) file)))
  19781. (defun org-get-cursor-date (&optional with-time)
  19782. "Return the date at cursor in as a time.
  19783. This works in the calendar and in the agenda, anywhere else it just
  19784. returns the current time.
  19785. If WITH-TIME is non-nil, returns the time of the event at point (in
  19786. the agenda) or the current time of the day."
  19787. (let (date day defd tp tm hod mod)
  19788. (when with-time
  19789. (setq tp (get-text-property (point) 'time))
  19790. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19791. (setq hod (string-to-number (match-string 1 tp))
  19792. mod (string-to-number (match-string 2 tp))))
  19793. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19794. mod (nth 1 (decode-time (current-time))))))
  19795. (cond
  19796. ((eq major-mode 'calendar-mode)
  19797. (setq date (calendar-cursor-to-date)
  19798. defd (encode-time 0 (or mod 0) (or hod 0)
  19799. (nth 1 date) (nth 0 date) (nth 2 date))))
  19800. ((eq major-mode 'org-agenda-mode)
  19801. (setq day (get-text-property (point) 'day))
  19802. (if day
  19803. (setq date (calendar-gregorian-from-absolute day)
  19804. defd (encode-time 0 (or mod 0) (or hod 0)
  19805. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19806. (or defd (current-time))))
  19807. (defun org-mark-subtree (&optional up)
  19808. "Mark the current subtree.
  19809. This puts point at the start of the current subtree, and mark at
  19810. the end. If a numeric prefix UP is given, move up into the
  19811. hierarchy of headlines by UP levels before marking the subtree."
  19812. (interactive "P")
  19813. (org-with-limited-levels
  19814. (cond ((org-at-heading-p) (beginning-of-line))
  19815. ((org-before-first-heading-p) (error "Not in a subtree"))
  19816. (t (outline-previous-visible-heading 1))))
  19817. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19818. (if (org-called-interactively-p 'any)
  19819. (call-interactively 'org-mark-element)
  19820. (org-mark-element)))
  19821. ;;; Indentation
  19822. (defun org-indent-line ()
  19823. "Indent line depending on context."
  19824. (interactive)
  19825. (let* ((pos (point))
  19826. (itemp (org-at-item-p))
  19827. (case-fold-search t)
  19828. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19829. (inline-task-p (and (featurep 'org-inlinetask)
  19830. (org-inlinetask-in-task-p)))
  19831. (inline-re (and inline-task-p
  19832. (org-inlinetask-outline-regexp)))
  19833. column)
  19834. (if (and orgstruct-is-++ (eq pos (point)))
  19835. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19836. (indent-according-to-mode))
  19837. (beginning-of-line 1)
  19838. (cond
  19839. ;; Headings
  19840. ((looking-at org-outline-regexp) (setq column 0))
  19841. ;; Footnote definition
  19842. ((looking-at org-footnote-definition-re) (setq column 0))
  19843. ;; Literal examples
  19844. ((looking-at "[ \t]*:\\( \\|$\\)")
  19845. (setq column (org-get-indentation))) ; do nothing
  19846. ;; Lists
  19847. ((ignore-errors (goto-char (org-in-item-p)))
  19848. (setq column (if itemp
  19849. (org-get-indentation)
  19850. (org-list-item-body-column (point))))
  19851. (goto-char pos))
  19852. ;; Drawers
  19853. ((and (looking-at "[ \t]*:END:")
  19854. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19855. (save-excursion
  19856. (goto-char (1- (match-beginning 1)))
  19857. (setq column (current-column))))
  19858. ;; Special blocks
  19859. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19860. (save-excursion
  19861. (re-search-backward
  19862. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19863. (setq column (org-get-indentation (match-string 0))))
  19864. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19865. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19866. (save-excursion
  19867. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19868. (setq column
  19869. (cond ((equal (downcase (match-string 1)) "src")
  19870. ;; src blocks: let `org-edit-src-exit' handle them
  19871. (org-get-indentation))
  19872. ((equal (downcase (match-string 1)) "example")
  19873. (max (org-get-indentation)
  19874. (org-get-indentation (match-string 0))))
  19875. (t
  19876. (org-get-indentation (match-string 0))))))
  19877. ;; This line has nothing special, look at the previous relevant
  19878. ;; line to compute indentation
  19879. (t
  19880. (beginning-of-line 0)
  19881. (while (and (not (bobp))
  19882. (not (looking-at org-table-line-regexp))
  19883. (not (looking-at org-drawer-regexp))
  19884. ;; When point started in an inline task, do not move
  19885. ;; above task starting line.
  19886. (not (and inline-task-p (looking-at inline-re)))
  19887. ;; Skip drawers, blocks, empty lines, verbatim,
  19888. ;; comments, tables, footnotes definitions, lists,
  19889. ;; inline tasks.
  19890. (or (and (looking-at "[ \t]*:END:")
  19891. (re-search-backward org-drawer-regexp nil t))
  19892. (and (looking-at "[ \t]*#\\+end_")
  19893. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19894. (looking-at "[ \t]*[\n:#|]")
  19895. (looking-at org-footnote-definition-re)
  19896. (and (ignore-errors (goto-char (org-in-item-p)))
  19897. (goto-char
  19898. (org-list-get-top-point (org-list-struct))))
  19899. (and (not inline-task-p)
  19900. (featurep 'org-inlinetask)
  19901. (org-inlinetask-in-task-p)
  19902. (or (org-inlinetask-goto-beginning) t))))
  19903. (beginning-of-line 0))
  19904. (cond
  19905. ;; There was an heading above.
  19906. ((looking-at "\\*+[ \t]+")
  19907. (if (not org-adapt-indentation)
  19908. (setq column 0)
  19909. (goto-char (match-end 0))
  19910. (setq column (current-column))))
  19911. ;; A drawer had started and is unfinished
  19912. ((looking-at org-drawer-regexp)
  19913. (goto-char (1- (match-beginning 1)))
  19914. (setq column (current-column)))
  19915. ;; Else, nothing noticeable found: get indentation and go on.
  19916. (t (setq column (org-get-indentation))))))
  19917. ;; Now apply indentation and move cursor accordingly
  19918. (goto-char pos)
  19919. (if (<= (current-column) (current-indentation))
  19920. (org-indent-line-to column)
  19921. (save-excursion (org-indent-line-to column)))
  19922. ;; Special polishing for properties, see `org-property-format'
  19923. (setq column (current-column))
  19924. (beginning-of-line 1)
  19925. (if (looking-at
  19926. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19927. (replace-match (concat (match-string 1)
  19928. (format org-property-format
  19929. (match-string 2) (match-string 3)))
  19930. t t))
  19931. (org-move-to-column column))))
  19932. (defun org-indent-drawer ()
  19933. "Indent the drawer at point."
  19934. (interactive)
  19935. (let ((p (point))
  19936. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19937. (match-end 0)))
  19938. (folded
  19939. (save-excursion
  19940. (end-of-line)
  19941. (when (overlays-at (point))
  19942. (member 'invisible (overlay-properties
  19943. (car (overlays-at (point)))))))))
  19944. (when folded (org-cycle))
  19945. (indent-for-tab-command)
  19946. (while (and (move-beginning-of-line 2) (< (point) e))
  19947. (indent-for-tab-command))
  19948. (goto-char p)
  19949. (when folded (org-cycle)))
  19950. (message "Drawer at point indented"))
  19951. (defun org-indent-block ()
  19952. "Indent the block at point."
  19953. (interactive)
  19954. (let ((p (point))
  19955. (case-fold-search t)
  19956. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19957. (match-end 0)))
  19958. (folded
  19959. (save-excursion
  19960. (end-of-line)
  19961. (when (overlays-at (point))
  19962. (member 'invisible (overlay-properties
  19963. (car (overlays-at (point)))))))))
  19964. (when folded (org-cycle))
  19965. (indent-for-tab-command)
  19966. (while (and (move-beginning-of-line 2) (< (point) e))
  19967. (indent-for-tab-command))
  19968. (goto-char p)
  19969. (when folded (org-cycle)))
  19970. (message "Block at point indented"))
  19971. (defun org-indent-region (start end)
  19972. "Indent region."
  19973. (interactive "r")
  19974. (save-excursion
  19975. (let ((line-end (org-current-line end)))
  19976. (goto-char start)
  19977. (while (< (org-current-line) line-end)
  19978. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19979. (t (call-interactively 'org-indent-line)))
  19980. (move-beginning-of-line 2)))))
  19981. ;;; Filling
  19982. ;; We use our own fill-paragraph and auto-fill functions.
  19983. ;; `org-fill-paragraph' relies on adaptive filling and context
  19984. ;; checking. Appropriate `fill-prefix' is computed with
  19985. ;; `org-adaptive-fill-function'.
  19986. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19987. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19988. ;; `org-adaptive-fill-function' value. Internally,
  19989. ;; `org-comment-line-break-function' breaks the line.
  19990. ;; `org-setup-filling' installs filling and auto-filling related
  19991. ;; variables during `org-mode' initialization.
  19992. (defun org-setup-filling ()
  19993. (interactive)
  19994. ;; Prevent auto-fill from inserting unwanted new items.
  19995. (when (boundp 'fill-nobreak-predicate)
  19996. (org-set-local
  19997. 'fill-nobreak-predicate
  19998. (org-uniquify
  19999. (append fill-nobreak-predicate
  20000. '(org-fill-paragraph-separate-nobreak-p
  20001. org-fill-line-break-nobreak-p
  20002. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20003. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20004. (org-set-local 'auto-fill-inhibit-regexp nil)
  20005. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20006. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20007. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20008. (defvar org-element-paragraph-separate) ; org-element.el
  20009. (defun org-fill-paragraph-separate-nobreak-p ()
  20010. "Non-nil when a line break at point would insert a new item."
  20011. (looking-at (substring org-element-paragraph-separate 1)))
  20012. (defun org-fill-line-break-nobreak-p ()
  20013. "Non-nil when a line break at point would create an Org line break."
  20014. (save-excursion
  20015. (skip-chars-backward "[ \t]")
  20016. (skip-chars-backward "\\\\")
  20017. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20018. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20019. "Non-nil when a line break at point would insert a new item."
  20020. (and (org-at-timestamp-p t)
  20021. (not (looking-at org-ts-regexp-both))))
  20022. (declare-function message-in-body-p "message" ())
  20023. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20024. (defun org-adaptive-fill-function ()
  20025. "Compute a fill prefix for the current line.
  20026. Return fill prefix, as a string, or nil if current line isn't
  20027. meant to be filled."
  20028. (let (prefix)
  20029. (catch 'exit
  20030. (when (derived-mode-p 'message-mode)
  20031. (save-excursion
  20032. (beginning-of-line)
  20033. (cond ((or (not (message-in-body-p))
  20034. (looking-at orgtbl-line-start-regexp))
  20035. (throw 'exit nil))
  20036. ((looking-at message-cite-prefix-regexp)
  20037. (throw 'exit (match-string-no-properties 0)))
  20038. ((looking-at org-outline-regexp)
  20039. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20040. (org-with-wide-buffer
  20041. (let* ((p (line-beginning-position))
  20042. (element (save-excursion (beginning-of-line)
  20043. (org-element-at-point)))
  20044. (type (org-element-type element))
  20045. (post-affiliated (org-element-property :post-affiliated element)))
  20046. (unless (and post-affiliated (< p post-affiliated))
  20047. (case type
  20048. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20049. (footnote-definition "")
  20050. ((item plain-list)
  20051. (make-string (org-list-item-body-column
  20052. (or post-affiliated
  20053. (org-element-property :begin element)))
  20054. ? ))
  20055. (paragraph
  20056. ;; Fill prefix is usually the same as the current line,
  20057. ;; except if the paragraph is at the beginning of an item.
  20058. (let ((parent (org-element-property :parent element)))
  20059. (cond ((eq (org-element-type parent) 'item)
  20060. (make-string (org-list-item-body-column
  20061. (org-element-property :begin parent))
  20062. ? ))
  20063. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20064. (match-string 0))
  20065. (t ""))))
  20066. (comment-block
  20067. ;; Only fill contents if P is within block boundaries.
  20068. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20069. (forward-line)
  20070. (point)))
  20071. (cend (save-excursion
  20072. (goto-char (org-element-property :end element))
  20073. (skip-chars-backward " \r\t\n")
  20074. (line-beginning-position))))
  20075. (when (and (>= p cbeg) (< p cend))
  20076. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20077. (match-string 0)
  20078. "")))))))))))
  20079. (declare-function message-goto-body "message" ())
  20080. (defvar message-cite-prefix-regexp) ; From message.el
  20081. (defun org-fill-paragraph (&optional justify)
  20082. "Fill element at point, when applicable.
  20083. This function only applies to comment blocks, comments, example
  20084. blocks and paragraphs. Also, as a special case, re-align table
  20085. when point is at one.
  20086. If JUSTIFY is non-nil (interactively, with prefix argument),
  20087. justify as well. If `sentence-end-double-space' is non-nil, then
  20088. period followed by one space does not end a sentence, so don't
  20089. break a line there. The variable `fill-column' controls the
  20090. width for filling.
  20091. For convenience, when point is at a plain list, an item or
  20092. a footnote definition, try to fill the first paragraph within."
  20093. (interactive)
  20094. (if (and (derived-mode-p 'message-mode)
  20095. (or (not (message-in-body-p))
  20096. (save-excursion (move-beginning-of-line 1)
  20097. (looking-at message-cite-prefix-regexp))))
  20098. ;; First ensure filling is correct in message-mode.
  20099. (let ((fill-paragraph-function
  20100. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20101. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20102. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20103. (paragraph-separate
  20104. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20105. (fill-paragraph nil))
  20106. (with-syntax-table org-mode-transpose-word-syntax-table
  20107. ;; Move to end of line in order to get the first paragraph
  20108. ;; within a plain list or a footnote definition.
  20109. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20110. ;; First check if point is in a blank line at the beginning of
  20111. ;; the buffer. In that case, ignore filling.
  20112. (case (org-element-type element)
  20113. ;; Use major mode filling function is src blocks.
  20114. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20115. ;; Align Org tables, leave table.el tables as-is.
  20116. (table-row (org-table-align) t)
  20117. (table
  20118. (when (eq (org-element-property :type element) 'org)
  20119. (org-table-align))
  20120. t)
  20121. (paragraph
  20122. ;; Paragraphs may contain `line-break' type objects.
  20123. (let ((beg (max (point-min)
  20124. (org-element-property :contents-begin element)))
  20125. (end (min (point-max)
  20126. (org-element-property :contents-end element))))
  20127. ;; Do nothing if point is at an affiliated keyword.
  20128. (if (< (line-end-position) beg) t
  20129. (when (derived-mode-p 'message-mode)
  20130. ;; In `message-mode', do not fill following citation
  20131. ;; in current paragraph nor text before message body.
  20132. (let ((body-start (save-excursion (message-goto-body))))
  20133. (when body-start (setq beg (max body-start beg))))
  20134. (when (save-excursion
  20135. (re-search-forward
  20136. (concat "^" message-cite-prefix-regexp) end t))
  20137. (setq end (match-beginning 0))))
  20138. ;; Fill paragraph, taking line breaks into account.
  20139. ;; For that, slice the paragraph using line breaks as
  20140. ;; separators, and fill the parts in reverse order to
  20141. ;; avoid messing with markers.
  20142. (save-excursion
  20143. (goto-char end)
  20144. (mapc
  20145. (lambda (pos)
  20146. (fill-region-as-paragraph pos (point) justify)
  20147. (goto-char pos))
  20148. ;; Find the list of ending positions for line breaks
  20149. ;; in the current paragraph. Add paragraph
  20150. ;; beginning to include first slice.
  20151. (nreverse
  20152. (cons beg
  20153. (org-element-map
  20154. (org-element--parse-objects
  20155. beg end nil (org-element-restriction 'paragraph))
  20156. 'line-break
  20157. (lambda (lb) (org-element-property :end lb)))))))
  20158. t)))
  20159. ;; Contents of `comment-block' type elements should be
  20160. ;; filled as plain text, but only if point is within block
  20161. ;; markers.
  20162. (comment-block
  20163. (let* ((case-fold-search t)
  20164. (beg (save-excursion
  20165. (goto-char (org-element-property :begin element))
  20166. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20167. (forward-line)
  20168. (point)))
  20169. (end (save-excursion
  20170. (goto-char (org-element-property :end element))
  20171. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20172. (line-beginning-position))))
  20173. (when (and (>= (point) beg) (< (point) end))
  20174. (fill-region-as-paragraph
  20175. (save-excursion
  20176. (end-of-line)
  20177. (re-search-backward "^[ \t]*$" beg 'move)
  20178. (line-beginning-position))
  20179. (save-excursion
  20180. (beginning-of-line)
  20181. (re-search-forward "^[ \t]*$" end 'move)
  20182. (line-beginning-position))
  20183. justify)))
  20184. t)
  20185. ;; Fill comments.
  20186. (comment (fill-comment-paragraph justify))
  20187. ;; Ignore every other element.
  20188. (otherwise t))))))
  20189. (defun org-auto-fill-function ()
  20190. "Auto-fill function."
  20191. ;; Check if auto-filling is meaningful.
  20192. (let ((fc (current-fill-column)))
  20193. (when (and fc (> (current-column) fc))
  20194. (let* ((fill-prefix (org-adaptive-fill-function))
  20195. ;; Enforce empty fill prefix, if required. Otherwise, it
  20196. ;; will be computed again.
  20197. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20198. (when fill-prefix (do-auto-fill))))))
  20199. (defun org-comment-line-break-function (&optional soft)
  20200. "Break line at point and indent, continuing comment if within one.
  20201. The inserted newline is marked hard if variable
  20202. `use-hard-newlines' is true, unless optional argument SOFT is
  20203. non-nil."
  20204. (if soft (insert-and-inherit ?\n) (newline 1))
  20205. (save-excursion (forward-char -1) (delete-horizontal-space))
  20206. (delete-horizontal-space)
  20207. (indent-to-left-margin)
  20208. (insert-before-markers-and-inherit fill-prefix))
  20209. ;;; Comments
  20210. ;; Org comments syntax is quite complex. It requires the entire line
  20211. ;; to be just a comment. Also, even with the right syntax at the
  20212. ;; beginning of line, some some elements (i.e. verse-block or
  20213. ;; example-block) don't accept comments. Usual Emacs comment commands
  20214. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20215. ;; Org still relies on `comment-dwim', but cannot trust
  20216. ;; `comment-only-p'. So, `comment-region-function' and
  20217. ;; `uncomment-region-function' both point
  20218. ;; to`org-comment-or-uncomment-region'. Eventually,
  20219. ;; `org-insert-comment' takes care of insertion of comments at the
  20220. ;; beginning of line.
  20221. ;; `org-setup-comments-handling' install comments related variables
  20222. ;; during `org-mode' initialization.
  20223. (defun org-setup-comments-handling ()
  20224. (interactive)
  20225. (org-set-local 'comment-use-syntax nil)
  20226. (org-set-local 'comment-start "# ")
  20227. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20228. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20229. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20230. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20231. (defun org-insert-comment ()
  20232. "Insert an empty comment above current line.
  20233. If the line is empty, insert comment at its beginning."
  20234. (beginning-of-line)
  20235. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20236. (org-indent-line)
  20237. (insert "# "))
  20238. (defvar comment-empty-lines) ; From newcomment.el.
  20239. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20240. "Comment or uncomment each non-blank line in the region.
  20241. Uncomment each non-blank line between BEG and END if it only
  20242. contains commented lines. Otherwise, comment them."
  20243. (save-restriction
  20244. ;; Restrict region
  20245. (narrow-to-region (save-excursion (goto-char beg)
  20246. (skip-chars-forward " \r\t\n" end)
  20247. (line-beginning-position))
  20248. (save-excursion (goto-char end)
  20249. (skip-chars-backward " \r\t\n" beg)
  20250. (line-end-position)))
  20251. (let ((uncommentp
  20252. ;; UNCOMMENTP is non-nil when every non blank line between
  20253. ;; BEG and END is a comment.
  20254. (save-excursion
  20255. (goto-char (point-min))
  20256. (while (and (not (eobp))
  20257. (let ((element (org-element-at-point)))
  20258. (and (eq (org-element-type element) 'comment)
  20259. (goto-char (min (point-max)
  20260. (org-element-property
  20261. :end element)))))))
  20262. (eobp))))
  20263. (if uncommentp
  20264. ;; Only blank lines and comments in region: uncomment it.
  20265. (save-excursion
  20266. (goto-char (point-min))
  20267. (while (not (eobp))
  20268. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20269. (replace-match "" nil nil nil 1))
  20270. (forward-line)))
  20271. ;; Comment each line in region.
  20272. (let ((min-indent (point-max)))
  20273. ;; First find the minimum indentation across all lines.
  20274. (save-excursion
  20275. (goto-char (point-min))
  20276. (while (and (not (eobp)) (not (zerop min-indent)))
  20277. (unless (looking-at "[ \t]*$")
  20278. (setq min-indent (min min-indent (current-indentation))))
  20279. (forward-line)))
  20280. ;; Then loop over all lines.
  20281. (save-excursion
  20282. (goto-char (point-min))
  20283. (while (not (eobp))
  20284. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20285. (org-move-to-column min-indent t)
  20286. (insert comment-start))
  20287. (forward-line))))))))
  20288. ;;; Planning
  20289. ;; This section contains tools to operate on timestamp objects, as
  20290. ;; returned by, e.g. `org-element-context'.
  20291. (defun org-timestamp-has-time-p (timestamp)
  20292. "Non-nil when TIMESTAMP has a time specified."
  20293. (org-element-property :hour-start timestamp))
  20294. (defun org-timestamp-format (timestamp format &optional end utc)
  20295. "Format a TIMESTAMP element into a string.
  20296. FORMAT is a format specifier to be passed to
  20297. `format-time-string'.
  20298. When optional argument END is non-nil, use end of date-range or
  20299. time-range, if possible.
  20300. When optional argument UTC is non-nil, time will be expressed as
  20301. Universal Time."
  20302. (format-time-string
  20303. format
  20304. (apply 'encode-time
  20305. (cons 0
  20306. (mapcar
  20307. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20308. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20309. '(:minute-start :hour-start :day-start :month-start
  20310. :year-start)))))
  20311. utc))
  20312. (defun org-timestamp-split-range (timestamp &optional end)
  20313. "Extract a timestamp object from a date or time range.
  20314. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20315. the end of the range. Otherwise, extract its start.
  20316. Return a new timestamp object sharing the same parent as
  20317. TIMESTAMP."
  20318. (let ((type (org-element-property :type timestamp)))
  20319. (if (memq type '(active inactive diary)) timestamp
  20320. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20321. ;; Set new type.
  20322. (org-element-put-property
  20323. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20324. ;; Copy start properties over end properties if END is
  20325. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20326. (let ((p-alist '((:minute-start . :minute-end)
  20327. (:hour-start . :hour-end)
  20328. (:day-start . :day-end)
  20329. (:month-start . :month-end)
  20330. (:year-start . :year-end))))
  20331. (dolist (p-cell p-alist)
  20332. (org-element-put-property
  20333. split-ts
  20334. (funcall (if end 'car 'cdr) p-cell)
  20335. (org-element-property
  20336. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20337. ;; Eventually refresh `:raw-value'.
  20338. (org-element-put-property split-ts :raw-value nil)
  20339. (org-element-put-property
  20340. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20341. (defun org-timestamp-translate (timestamp &optional boundary)
  20342. "Apply `org-translate-time' on a TIMESTAMP object.
  20343. When optional argument BOUNDARY is non-nil, it is either the
  20344. symbol `start' or `end'. In this case, only translate the
  20345. starting or ending part of TIMESTAMP if it is a date or time
  20346. range. Otherwise, translate both parts."
  20347. (if (and (not boundary)
  20348. (memq (org-element-property :type timestamp)
  20349. '(active-range inactive-range)))
  20350. (concat
  20351. (org-translate-time
  20352. (org-element-property :raw-value
  20353. (org-timestamp-split-range timestamp)))
  20354. "--"
  20355. (org-translate-time
  20356. (org-element-property :raw-value
  20357. (org-timestamp-split-range timestamp t))))
  20358. (org-translate-time
  20359. (org-element-property
  20360. :raw-value
  20361. (if (not boundary) timestamp
  20362. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20363. ;;; Other stuff.
  20364. (defun org-toggle-fixed-width-section (arg)
  20365. "Toggle the fixed-width export.
  20366. If there is no active region, the QUOTE keyword at the current headline is
  20367. inserted or removed. When present, it causes the text between this headline
  20368. and the next to be exported as fixed-width text, and unmodified.
  20369. If there is an active region, this command adds or removes a colon as the
  20370. first character of this line. If the first character of a line is a colon,
  20371. this line is also exported in fixed-width font."
  20372. (interactive "P")
  20373. (let* ((cc 0)
  20374. (regionp (org-region-active-p))
  20375. (beg (if regionp (region-beginning) (point)))
  20376. (end (if regionp (region-end)))
  20377. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20378. (case-fold-search nil)
  20379. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20380. off)
  20381. (if regionp
  20382. (save-excursion
  20383. (goto-char beg)
  20384. (setq cc (current-column))
  20385. (beginning-of-line 1)
  20386. (setq off (looking-at re))
  20387. (while (> nlines 0)
  20388. (setq nlines (1- nlines))
  20389. (beginning-of-line 1)
  20390. (cond
  20391. (arg
  20392. (org-move-to-column cc t)
  20393. (insert ": \n")
  20394. (forward-line -1))
  20395. ((and off (looking-at re))
  20396. (replace-match "" t t nil 1))
  20397. ((not off) (org-move-to-column cc t) (insert ": ")))
  20398. (forward-line 1)))
  20399. (save-excursion
  20400. (org-back-to-heading)
  20401. (cond
  20402. ((looking-at (format org-heading-keyword-regexp-format
  20403. org-quote-string))
  20404. (goto-char (match-end 1))
  20405. (looking-at (concat " +" org-quote-string))
  20406. (replace-match "" t t)
  20407. (when (eolp) (insert " ")))
  20408. ((looking-at org-outline-regexp)
  20409. (goto-char (match-end 0))
  20410. (insert org-quote-string " ")))))))
  20411. (defun org-reftex-citation ()
  20412. "Use reftex-citation to insert a citation into the buffer.
  20413. This looks for a line like
  20414. #+BIBLIOGRAPHY: foo plain option:-d
  20415. and derives from it that foo.bib is the bibliography file relevant
  20416. for this document. It then installs the necessary environment for RefTeX
  20417. to work in this buffer and calls `reftex-citation' to insert a citation
  20418. into the buffer.
  20419. Export of such citations to both LaTeX and HTML is handled by the contributed
  20420. package org-exp-bibtex by Taru Karttunen."
  20421. (interactive)
  20422. (let ((reftex-docstruct-symbol 'rds)
  20423. (reftex-cite-format "\\cite{%l}")
  20424. rds bib)
  20425. (save-excursion
  20426. (save-restriction
  20427. (widen)
  20428. (let ((case-fold-search t)
  20429. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20430. (if (not (save-excursion
  20431. (or (re-search-forward re nil t)
  20432. (re-search-backward re nil t))))
  20433. (error "No bibliography defined in file")
  20434. (setq bib (concat (match-string 1) ".bib")
  20435. rds (list (list 'bib bib)))))))
  20436. (call-interactively 'reftex-citation)))
  20437. ;;;; Functions extending outline functionality
  20438. (defun org-beginning-of-line (&optional arg)
  20439. "Go to the beginning of the current line. If that is invisible, continue
  20440. to a visible line beginning. This makes the function of C-a more intuitive.
  20441. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20442. first attempt, and only move to after the tags when the cursor is already
  20443. beyond the end of the headline."
  20444. (interactive "P")
  20445. (let ((pos (point))
  20446. (special (if (consp org-special-ctrl-a/e)
  20447. (car org-special-ctrl-a/e)
  20448. org-special-ctrl-a/e))
  20449. refpos)
  20450. (if (org-bound-and-true-p visual-line-mode)
  20451. (beginning-of-visual-line 1)
  20452. (beginning-of-line 1))
  20453. (if (and arg (fboundp 'move-beginning-of-line))
  20454. (call-interactively 'move-beginning-of-line)
  20455. (if (bobp)
  20456. nil
  20457. (backward-char 1)
  20458. (if (org-truely-invisible-p)
  20459. (while (and (not (bobp)) (org-truely-invisible-p))
  20460. (backward-char 1)
  20461. (beginning-of-line 1))
  20462. (forward-char 1))))
  20463. (when special
  20464. (cond
  20465. ((and (looking-at org-complex-heading-regexp)
  20466. (= (char-after (match-end 1)) ?\ ))
  20467. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20468. (point-at-eol)))
  20469. (goto-char
  20470. (if (eq special t)
  20471. (cond ((> pos refpos) refpos)
  20472. ((= pos (point)) refpos)
  20473. (t (point)))
  20474. (cond ((> pos (point)) (point))
  20475. ((not (eq last-command this-command)) (point))
  20476. (t refpos)))))
  20477. ((org-at-item-p)
  20478. ;; Being at an item and not looking at an the item means point
  20479. ;; was previously moved to beginning of a visual line, which
  20480. ;; doesn't contain the item. Therefore, do nothing special,
  20481. ;; just stay here.
  20482. (when (looking-at org-list-full-item-re)
  20483. ;; Set special position at first white space character after
  20484. ;; bullet, and check-box, if any.
  20485. (let ((after-bullet
  20486. (let ((box (match-end 3)))
  20487. (if (not box) (match-end 1)
  20488. (let ((after (char-after box)))
  20489. (if (and after (= after ? )) (1+ box) box))))))
  20490. ;; Special case: Move point to special position when
  20491. ;; currently after it or at beginning of line.
  20492. (if (eq special t)
  20493. (when (or (> pos after-bullet) (= (point) pos))
  20494. (goto-char after-bullet))
  20495. ;; Reversed case: Move point to special position when
  20496. ;; point was already at beginning of line and command is
  20497. ;; repeated.
  20498. (when (and (= (point) pos) (eq last-command this-command))
  20499. (goto-char after-bullet))))))))
  20500. (org-no-warnings
  20501. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20502. (defun org-end-of-line (&optional arg)
  20503. "Go to the end of the line.
  20504. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20505. tags on the first attempt, and only move to after the tags when
  20506. the cursor is already beyond the end of the headline."
  20507. (interactive "P")
  20508. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20509. org-special-ctrl-a/e))
  20510. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20511. 'end-of-visual-line)
  20512. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20513. (t 'end-of-line))))
  20514. (if (or (not special) arg) (call-interactively move-fun)
  20515. (let* ((element (save-excursion (beginning-of-line)
  20516. (org-element-at-point)))
  20517. (type (org-element-type element)))
  20518. (cond
  20519. ((memq type '(headline inlinetask))
  20520. (let ((pos (point)))
  20521. (beginning-of-line 1)
  20522. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20523. (if (eq special t)
  20524. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20525. (goto-char (match-beginning 1))
  20526. (goto-char (match-end 0)))
  20527. (if (or (< pos (match-end 0))
  20528. (not (eq this-command last-command)))
  20529. (goto-char (match-end 0))
  20530. (goto-char (match-beginning 1))))
  20531. (call-interactively move-fun))))
  20532. ((org-element-property :hiddenp element)
  20533. ;; If element is hidden, `move-end-of-line' would put point
  20534. ;; after it. Use `end-of-line' to stay on current line.
  20535. (call-interactively 'end-of-line))
  20536. (t (call-interactively move-fun)))))
  20537. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20538. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20539. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20540. (defun org-backward-sentence (&optional arg)
  20541. "Go to beginning of sentence, or beginning of table field.
  20542. This will call `backward-sentence' or `org-table-beginning-of-field',
  20543. depending on context."
  20544. (interactive "P")
  20545. (cond
  20546. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20547. (t (call-interactively 'backward-sentence))))
  20548. (defun org-forward-sentence (&optional arg)
  20549. "Go to end of sentence, or end of table field.
  20550. This will call `forward-sentence' or `org-table-end-of-field',
  20551. depending on context."
  20552. (interactive "P")
  20553. (cond
  20554. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20555. (t (call-interactively 'forward-sentence))))
  20556. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20557. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20558. (defun org-kill-line (&optional arg)
  20559. "Kill line, to tags or end of line."
  20560. (interactive "P")
  20561. (cond
  20562. ((or (not org-special-ctrl-k)
  20563. (bolp)
  20564. (not (org-at-heading-p)))
  20565. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20566. org-ctrl-k-protect-subtree)
  20567. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20568. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20569. (error "C-k aborted - would kill hidden subtree")))
  20570. (call-interactively
  20571. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20572. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20573. (kill-region (point) (match-beginning 1))
  20574. (org-set-tags nil t))
  20575. (t (kill-region (point) (point-at-eol)))))
  20576. (define-key org-mode-map "\C-k" 'org-kill-line)
  20577. (defun org-yank (&optional arg)
  20578. "Yank. If the kill is a subtree, treat it specially.
  20579. This command will look at the current kill and check if is a single
  20580. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20581. cursor is at the beginning of a line or after the stars of a currently
  20582. empty headline, then the yank is handled specially. How exactly depends
  20583. on the value of the following variables, both set by default.
  20584. org-yank-folded-subtrees
  20585. When set, the subtree(s) will be folded after insertion, but only
  20586. if doing so would now swallow text after the yanked text.
  20587. org-yank-adjusted-subtrees
  20588. When set, the subtree will be promoted or demoted in order to
  20589. fit into the local outline tree structure, which means that the level
  20590. will be adjusted so that it becomes the smaller one of the two
  20591. *visible* surrounding headings.
  20592. Any prefix to this command will cause `yank' to be called directly with
  20593. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20594. will just
  20595. plainly yank the text as it is.
  20596. \[1] The test checks if the first non-white line is a heading
  20597. and if there are no other headings with fewer stars."
  20598. (interactive "P")
  20599. (org-yank-generic 'yank arg))
  20600. (defun org-yank-generic (command arg)
  20601. "Perform some yank-like command.
  20602. This function implements the behavior described in the `org-yank'
  20603. documentation. However, it has been generalized to work for any
  20604. interactive command with similar behavior."
  20605. ;; pretend to be command COMMAND
  20606. (setq this-command command)
  20607. (if arg
  20608. (call-interactively command)
  20609. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20610. (and (org-kill-is-subtree-p)
  20611. (or (bolp)
  20612. (and (looking-at "[ \t]*$")
  20613. (string-match
  20614. "\\`\\*+\\'"
  20615. (buffer-substring (point-at-bol) (point)))))))
  20616. swallowp)
  20617. (cond
  20618. ((and subtreep org-yank-folded-subtrees)
  20619. (let ((beg (point))
  20620. end)
  20621. (if (and subtreep org-yank-adjusted-subtrees)
  20622. (org-paste-subtree nil nil 'for-yank)
  20623. (call-interactively command))
  20624. (setq end (point))
  20625. (goto-char beg)
  20626. (when (and (bolp) subtreep
  20627. (not (setq swallowp
  20628. (org-yank-folding-would-swallow-text beg end))))
  20629. (org-with-limited-levels
  20630. (or (looking-at org-outline-regexp)
  20631. (re-search-forward org-outline-regexp-bol end t))
  20632. (while (and (< (point) end) (looking-at org-outline-regexp))
  20633. (hide-subtree)
  20634. (org-cycle-show-empty-lines 'folded)
  20635. (condition-case nil
  20636. (outline-forward-same-level 1)
  20637. (error (goto-char end))))))
  20638. (when swallowp
  20639. (message
  20640. "Inserted text not folded because that would swallow text"))
  20641. (goto-char end)
  20642. (skip-chars-forward " \t\n\r")
  20643. (beginning-of-line 1)
  20644. (push-mark beg 'nomsg)))
  20645. ((and subtreep org-yank-adjusted-subtrees)
  20646. (let ((beg (point-at-bol)))
  20647. (org-paste-subtree nil nil 'for-yank)
  20648. (push-mark beg 'nomsg)))
  20649. (t
  20650. (call-interactively command))))))
  20651. (defun org-yank-folding-would-swallow-text (beg end)
  20652. "Would hide-subtree at BEG swallow any text after END?"
  20653. (let (level)
  20654. (org-with-limited-levels
  20655. (save-excursion
  20656. (goto-char beg)
  20657. (when (or (looking-at org-outline-regexp)
  20658. (re-search-forward org-outline-regexp-bol end t))
  20659. (setq level (org-outline-level)))
  20660. (goto-char end)
  20661. (skip-chars-forward " \t\r\n\v\f")
  20662. (if (or (eobp)
  20663. (and (bolp) (looking-at org-outline-regexp)
  20664. (<= (org-outline-level) level)))
  20665. nil ; Nothing would be swallowed
  20666. t))))) ; something would swallow
  20667. (define-key org-mode-map "\C-y" 'org-yank)
  20668. (defun org-truely-invisible-p ()
  20669. "Check if point is at a character currently not visible.
  20670. This version does not only check the character property, but also
  20671. `visible-mode'."
  20672. ;; Early versions of noutline don't have `outline-invisible-p'.
  20673. (if (org-bound-and-true-p visible-mode)
  20674. nil
  20675. (outline-invisible-p)))
  20676. (defun org-invisible-p2 ()
  20677. "Check if point is at a character currently not visible."
  20678. (save-excursion
  20679. (if (and (eolp) (not (bobp))) (backward-char 1))
  20680. ;; Early versions of noutline don't have `outline-invisible-p'.
  20681. (outline-invisible-p)))
  20682. (defun org-back-to-heading (&optional invisible-ok)
  20683. "Call `outline-back-to-heading', but provide a better error message."
  20684. (condition-case nil
  20685. (outline-back-to-heading invisible-ok)
  20686. (error (error "Before first headline at position %d in buffer %s"
  20687. (point) (current-buffer)))))
  20688. (defun org-before-first-heading-p ()
  20689. "Before first heading?"
  20690. (save-excursion
  20691. (end-of-line)
  20692. (null (re-search-backward org-outline-regexp-bol nil t))))
  20693. (defun org-at-heading-p (&optional ignored)
  20694. (outline-on-heading-p t))
  20695. ;; Compatibility alias with Org versions < 7.8.03
  20696. (defalias 'org-on-heading-p 'org-at-heading-p)
  20697. (defun org-at-comment-p nil
  20698. "Is cursor in a line starting with a # character?"
  20699. (save-excursion
  20700. (beginning-of-line)
  20701. (looking-at "^#")))
  20702. (defun org-at-drawer-p nil
  20703. "Is cursor at a drawer keyword?"
  20704. (save-excursion
  20705. (move-beginning-of-line 1)
  20706. (looking-at org-drawer-regexp)))
  20707. (defun org-at-block-p nil
  20708. "Is cursor at a block keyword?"
  20709. (save-excursion
  20710. (move-beginning-of-line 1)
  20711. (looking-at org-block-regexp)))
  20712. (defun org-point-at-end-of-empty-headline ()
  20713. "If point is at the end of an empty headline, return t, else nil.
  20714. If the heading only contains a TODO keyword, it is still still considered
  20715. empty."
  20716. (and (looking-at "[ \t]*$")
  20717. (when org-todo-line-regexp
  20718. (save-excursion
  20719. (beginning-of-line 1)
  20720. (let ((case-fold-search nil))
  20721. (looking-at org-todo-line-regexp)
  20722. (string= (match-string 3) ""))))))
  20723. (defun org-at-heading-or-item-p ()
  20724. (or (org-at-heading-p) (org-at-item-p)))
  20725. (defun org-at-target-p ()
  20726. (or (org-in-regexp org-radio-target-regexp)
  20727. (org-in-regexp org-target-regexp)))
  20728. ;; Compatibility alias with Org versions < 7.8.03
  20729. (defalias 'org-on-target-p 'org-at-target-p)
  20730. (defun org-up-heading-all (arg)
  20731. "Move to the heading line of which the present line is a subheading.
  20732. This function considers both visible and invisible heading lines.
  20733. With argument, move up ARG levels."
  20734. (if (fboundp 'outline-up-heading-all)
  20735. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20736. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20737. (defun org-up-heading-safe ()
  20738. "Move to the heading line of which the present line is a subheading.
  20739. This version will not throw an error. It will return the level of the
  20740. headline found, or nil if no higher level is found.
  20741. Also, this function will be a lot faster than `outline-up-heading',
  20742. because it relies on stars being the outline starters. This can really
  20743. make a significant difference in outlines with very many siblings."
  20744. (let (start-level re)
  20745. (org-back-to-heading t)
  20746. (setq start-level (funcall outline-level))
  20747. (if (equal start-level 1)
  20748. nil
  20749. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20750. (if (re-search-backward re nil t)
  20751. (funcall outline-level)))))
  20752. (defun org-first-sibling-p ()
  20753. "Is this heading the first child of its parents?"
  20754. (interactive)
  20755. (let ((re org-outline-regexp-bol)
  20756. level l)
  20757. (unless (org-at-heading-p t)
  20758. (error "Not at a heading"))
  20759. (setq level (funcall outline-level))
  20760. (save-excursion
  20761. (if (not (re-search-backward re nil t))
  20762. t
  20763. (setq l (funcall outline-level))
  20764. (< l level)))))
  20765. (defun org-goto-sibling (&optional previous)
  20766. "Goto the next sibling, even if it is invisible.
  20767. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20768. when a sibling was found. When none is found, return nil and don't
  20769. move point."
  20770. (let ((fun (if previous 're-search-backward 're-search-forward))
  20771. (pos (point))
  20772. (re org-outline-regexp-bol)
  20773. level l)
  20774. (when (condition-case nil (org-back-to-heading t) (error nil))
  20775. (setq level (funcall outline-level))
  20776. (catch 'exit
  20777. (or previous (forward-char 1))
  20778. (while (funcall fun re nil t)
  20779. (setq l (funcall outline-level))
  20780. (when (< l level) (goto-char pos) (throw 'exit nil))
  20781. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20782. (goto-char pos)
  20783. nil))))
  20784. (defun org-show-siblings ()
  20785. "Show all siblings of the current headline."
  20786. (save-excursion
  20787. (while (org-goto-sibling) (org-flag-heading nil)))
  20788. (save-excursion
  20789. (while (org-goto-sibling 'previous)
  20790. (org-flag-heading nil))))
  20791. (defun org-goto-first-child ()
  20792. "Goto the first child, even if it is invisible.
  20793. Return t when a child was found. Otherwise don't move point and
  20794. return nil."
  20795. (let (level (pos (point)) (re org-outline-regexp-bol))
  20796. (when (condition-case nil (org-back-to-heading t) (error nil))
  20797. (setq level (outline-level))
  20798. (forward-char 1)
  20799. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20800. (progn (goto-char (match-beginning 0)) t)
  20801. (goto-char pos) nil))))
  20802. (defun org-show-hidden-entry ()
  20803. "Show an entry where even the heading is hidden."
  20804. (save-excursion
  20805. (org-show-entry)))
  20806. (defun org-flag-heading (flag &optional entry)
  20807. "Flag the current heading. FLAG non-nil means make invisible.
  20808. When ENTRY is non-nil, show the entire entry."
  20809. (save-excursion
  20810. (org-back-to-heading t)
  20811. ;; Check if we should show the entire entry
  20812. (if entry
  20813. (progn
  20814. (org-show-entry)
  20815. (save-excursion
  20816. (and (outline-next-heading)
  20817. (org-flag-heading nil))))
  20818. (outline-flag-region (max (point-min) (1- (point)))
  20819. (save-excursion (outline-end-of-heading) (point))
  20820. flag))))
  20821. (defun org-get-next-sibling ()
  20822. "Move to next heading of the same level, and return point.
  20823. If there is no such heading, return nil.
  20824. This is like outline-next-sibling, but invisible headings are ok."
  20825. (let ((level (funcall outline-level)))
  20826. (outline-next-heading)
  20827. (while (and (not (eobp)) (> (funcall outline-level) level))
  20828. (outline-next-heading))
  20829. (if (or (eobp) (< (funcall outline-level) level))
  20830. nil
  20831. (point))))
  20832. (defun org-get-last-sibling ()
  20833. "Move to previous heading of the same level, and return point.
  20834. If there is no such heading, return nil."
  20835. (let ((opoint (point))
  20836. (level (funcall outline-level)))
  20837. (outline-previous-heading)
  20838. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20839. (while (and (> (funcall outline-level) level)
  20840. (not (bobp)))
  20841. (outline-previous-heading))
  20842. (if (< (funcall outline-level) level)
  20843. nil
  20844. (point)))))
  20845. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20846. "Goto to the end of a subtree."
  20847. ;; This contains an exact copy of the original function, but it uses
  20848. ;; `org-back-to-heading', to make it work also in invisible
  20849. ;; trees. And is uses an invisible-ok argument.
  20850. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20851. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20852. ;; with many children and grandchildren etc, this can be much faster
  20853. ;; than the outline version.
  20854. (org-back-to-heading invisible-ok)
  20855. (let ((first t)
  20856. (level (funcall outline-level)))
  20857. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20858. ;; A true heading (not a plain list item), in Org-mode
  20859. ;; This means we can easily find the end by looking
  20860. ;; only for the right number of stars. Using a regexp to do
  20861. ;; this is so much faster than using a Lisp loop.
  20862. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20863. (forward-char 1)
  20864. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20865. ;; something else, do it the slow way
  20866. (while (and (not (eobp))
  20867. (or first (> (funcall outline-level) level)))
  20868. (setq first nil)
  20869. (outline-next-heading)))
  20870. (unless to-heading
  20871. (if (memq (preceding-char) '(?\n ?\^M))
  20872. (progn
  20873. ;; Go to end of line before heading
  20874. (forward-char -1)
  20875. (if (memq (preceding-char) '(?\n ?\^M))
  20876. ;; leave blank line before heading
  20877. (forward-char -1))))))
  20878. (point))
  20879. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20880. "Use Org version in org-mode, for dramatic speed-up."
  20881. (if (derived-mode-p 'org-mode)
  20882. (progn
  20883. (org-end-of-subtree nil t)
  20884. (unless (eobp) (backward-char 1)))
  20885. ad-do-it))
  20886. (defun org-end-of-meta-data-and-drawers ()
  20887. "Jump to the first text after meta data and drawers in the current entry.
  20888. This will move over empty lines, lines with planning time stamps,
  20889. clocking lines, and drawers."
  20890. (org-back-to-heading t)
  20891. (let ((end (save-excursion (outline-next-heading) (point)))
  20892. (re (concat "\\(" org-drawer-regexp "\\)"
  20893. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20894. (forward-line 1)
  20895. (while (re-search-forward re end t)
  20896. (if (not (match-end 1))
  20897. ;; empty or planning line
  20898. (forward-line 1)
  20899. ;; a drawer, find the end
  20900. (re-search-forward "^[ \t]*:END:" end 'move)
  20901. (forward-line 1)))
  20902. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20903. (point)))
  20904. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20905. "Move forward to the ARG'th subheading at same level as this one.
  20906. Stop at the first and last subheadings of a superior heading.
  20907. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20908. non-nil it will also look at invisible ones."
  20909. (interactive "p")
  20910. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20911. (if (and arg (< arg 0))
  20912. (goto-char (point-min))
  20913. (outline-next-heading))
  20914. (org-at-heading-p)
  20915. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20916. (f (if (and arg (< arg 0))
  20917. 're-search-backward
  20918. 're-search-forward))
  20919. (count (if arg (abs arg) 1))
  20920. (result (point)))
  20921. (forward-char (if (and arg (< arg 0)) -1 1))
  20922. (while (and (> count 0)
  20923. (funcall f org-outline-regexp-bol nil 'move))
  20924. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20925. (cond ((< l level) (setq count 0))
  20926. ((and (= l level)
  20927. (or invisible-ok
  20928. (progn
  20929. (goto-char (line-beginning-position))
  20930. (not (outline-invisible-p)))))
  20931. (setq count (1- count))
  20932. (when (eq l level)
  20933. (setq result (point)))))))
  20934. (goto-char result))
  20935. (beginning-of-line 1)))
  20936. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20937. "Move backward to the ARG'th subheading at same level as this one.
  20938. Stop at the first and last subheadings of a superior heading."
  20939. (interactive "p")
  20940. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20941. (defun org-next-block (arg &optional backward block-regexp)
  20942. "Jump to the next block.
  20943. With a prefix argument ARG, jump forward ARG many source blocks.
  20944. When BACKWARD is non-nil, jump to the previous block.
  20945. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20946. (interactive "p")
  20947. (let ((re (or block-regexp org-block-regexp))
  20948. (re-search-fn (or (and backward 're-search-backward)
  20949. 're-search-forward)))
  20950. (if (looking-at re) (forward-char 1))
  20951. (condition-case nil
  20952. (funcall re-search-fn re nil nil arg)
  20953. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20954. (goto-char (match-beginning 0)) (org-show-context)))
  20955. (defun org-previous-block (arg &optional block-regexp)
  20956. "Jump to the previous block.
  20957. With a prefix argument ARG, jump backward ARG many source blocks.
  20958. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20959. (interactive "p")
  20960. (org-next-block arg t block-regexp))
  20961. (defun org-forward-element ()
  20962. "Move forward by one element.
  20963. Move to the next element at the same level, when possible."
  20964. (interactive)
  20965. (cond ((eobp) (user-error "Cannot move further down"))
  20966. ((org-with-limited-levels (org-at-heading-p))
  20967. (let ((origin (point)))
  20968. (goto-char (org-end-of-subtree nil t))
  20969. (unless (org-with-limited-levels (org-at-heading-p))
  20970. (goto-char origin)
  20971. (user-error "Cannot move further down"))))
  20972. (t
  20973. (let* ((elem (org-element-at-point))
  20974. (end (org-element-property :end elem))
  20975. (parent (org-element-property :parent elem)))
  20976. (if (and parent (= (org-element-property :contents-end parent) end))
  20977. (goto-char (org-element-property :end parent))
  20978. (goto-char end))))))
  20979. (defun org-backward-element ()
  20980. "Move backward by one element.
  20981. Move to the previous element at the same level, when possible."
  20982. (interactive)
  20983. (cond ((bobp) (user-error "Cannot move further up"))
  20984. ((org-with-limited-levels (org-at-heading-p))
  20985. ;; At a headline, move to the previous one, if any, or stay
  20986. ;; here.
  20987. (let ((origin (point)))
  20988. (org-with-limited-levels (org-backward-heading-same-level 1))
  20989. ;; When current headline has no sibling above, move to its
  20990. ;; parent.
  20991. (when (= (point) origin)
  20992. (or (org-with-limited-levels (org-up-heading-safe))
  20993. (progn (goto-char origin)
  20994. (user-error "Cannot move further up"))))))
  20995. (t
  20996. (let* ((trail (org-element-at-point 'keep-trail))
  20997. (elem (car trail))
  20998. (prev-elem (nth 1 trail))
  20999. (beg (org-element-property :begin elem)))
  21000. (cond
  21001. ;; Move to beginning of current element if point isn't
  21002. ;; there already.
  21003. ((/= (point) beg) (goto-char beg))
  21004. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21005. ((org-before-first-heading-p) (goto-char (point-min)))
  21006. (t (org-back-to-heading)))))))
  21007. (defun org-up-element ()
  21008. "Move to upper element."
  21009. (interactive)
  21010. (if (org-with-limited-levels (org-at-heading-p))
  21011. (unless (org-up-heading-safe) (error "No surrounding element"))
  21012. (let* ((elem (org-element-at-point))
  21013. (parent (org-element-property :parent elem)))
  21014. (if parent (goto-char (org-element-property :begin parent))
  21015. (if (org-with-limited-levels (org-before-first-heading-p))
  21016. (error "No surrounding element")
  21017. (org-with-limited-levels (org-back-to-heading)))))))
  21018. (defvar org-element-greater-elements)
  21019. (defun org-down-element ()
  21020. "Move to inner element."
  21021. (interactive)
  21022. (let ((element (org-element-at-point)))
  21023. (cond
  21024. ((memq (org-element-type element) '(plain-list table))
  21025. (goto-char (org-element-property :contents-begin element))
  21026. (forward-char))
  21027. ((memq (org-element-type element) org-element-greater-elements)
  21028. ;; If contents are hidden, first disclose them.
  21029. (when (org-element-property :hiddenp element) (org-cycle))
  21030. (goto-char (or (org-element-property :contents-begin element)
  21031. (error "No content for this element"))))
  21032. (t (error "No inner element")))))
  21033. (defun org-drag-element-backward ()
  21034. "Move backward element at point."
  21035. (interactive)
  21036. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21037. (let* ((trail (org-element-at-point 'keep-trail))
  21038. (elem (car trail))
  21039. (prev-elem (nth 1 trail)))
  21040. ;; Error out if no previous element or previous element is
  21041. ;; a parent of the current one.
  21042. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21043. (error "Cannot drag element backward")
  21044. (let ((pos (point)))
  21045. (org-element-swap-A-B prev-elem elem)
  21046. (goto-char (+ (org-element-property :begin prev-elem)
  21047. (- pos (org-element-property :begin elem)))))))))
  21048. (defun org-drag-element-forward ()
  21049. "Move forward element at point."
  21050. (interactive)
  21051. (let* ((pos (point))
  21052. (elem (org-element-at-point)))
  21053. (when (= (point-max) (org-element-property :end elem))
  21054. (error "Cannot drag element forward"))
  21055. (goto-char (org-element-property :end elem))
  21056. (let ((next-elem (org-element-at-point)))
  21057. (when (or (org-element-nested-p elem next-elem)
  21058. (and (eq (org-element-type next-elem) 'headline)
  21059. (not (eq (org-element-type elem) 'headline))))
  21060. (goto-char pos)
  21061. (error "Cannot drag element forward"))
  21062. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21063. ;; body's length (without final blanks) and by the length of
  21064. ;; blanks between ELEM and NEXT-ELEM.
  21065. (let ((size-next (- (save-excursion
  21066. (goto-char (org-element-property :end next-elem))
  21067. (skip-chars-backward " \r\t\n")
  21068. (forward-line)
  21069. ;; Small correction if buffer doesn't end
  21070. ;; with a newline character.
  21071. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21072. (org-element-property :begin next-elem)))
  21073. (size-blank (- (org-element-property :end elem)
  21074. (save-excursion
  21075. (goto-char (org-element-property :end elem))
  21076. (skip-chars-backward " \r\t\n")
  21077. (forward-line)
  21078. (point)))))
  21079. (org-element-swap-A-B elem next-elem)
  21080. (goto-char (+ pos size-next size-blank))))))
  21081. (defun org-drag-line-forward (arg)
  21082. "Drag the line at point ARG lines forward."
  21083. (interactive "p")
  21084. (dotimes (n (abs arg))
  21085. (let ((c (current-column)))
  21086. (if (< 0 arg)
  21087. (progn
  21088. (beginning-of-line 2)
  21089. (transpose-lines 1)
  21090. (beginning-of-line 0))
  21091. (transpose-lines 1)
  21092. (beginning-of-line -1))
  21093. (org-move-to-column c))))
  21094. (defun org-drag-line-backward (arg)
  21095. "Drag the line at point ARG lines backward."
  21096. (interactive "p")
  21097. (org-drag-line-forward (- arg)))
  21098. (defun org-mark-element ()
  21099. "Put point at beginning of this element, mark at end.
  21100. Interactively, if this command is repeated or (in Transient Mark
  21101. mode) if the mark is active, it marks the next element after the
  21102. ones already marked."
  21103. (interactive)
  21104. (let (deactivate-mark)
  21105. (if (and (org-called-interactively-p 'any)
  21106. (or (and (eq last-command this-command) (mark t))
  21107. (and transient-mark-mode mark-active)))
  21108. (set-mark
  21109. (save-excursion
  21110. (goto-char (mark))
  21111. (goto-char (org-element-property :end (org-element-at-point)))))
  21112. (let ((element (org-element-at-point)))
  21113. (end-of-line)
  21114. (push-mark (org-element-property :end element) t t)
  21115. (goto-char (org-element-property :begin element))))))
  21116. (defun org-narrow-to-element ()
  21117. "Narrow buffer to current element."
  21118. (interactive)
  21119. (let ((elem (org-element-at-point)))
  21120. (cond
  21121. ((eq (car elem) 'headline)
  21122. (narrow-to-region
  21123. (org-element-property :begin elem)
  21124. (org-element-property :end elem)))
  21125. ((memq (car elem) org-element-greater-elements)
  21126. (narrow-to-region
  21127. (org-element-property :contents-begin elem)
  21128. (org-element-property :contents-end elem)))
  21129. (t
  21130. (narrow-to-region
  21131. (org-element-property :begin elem)
  21132. (org-element-property :end elem))))))
  21133. (defun org-transpose-element ()
  21134. "Transpose current and previous elements, keeping blank lines between.
  21135. Point is moved after both elements."
  21136. (interactive)
  21137. (org-skip-whitespace)
  21138. (let ((end (org-element-property :end (org-element-at-point))))
  21139. (org-drag-element-backward)
  21140. (goto-char end)))
  21141. (defun org-unindent-buffer ()
  21142. "Un-indent the visible part of the buffer.
  21143. Relative indentation (between items, inside blocks, etc.) isn't
  21144. modified."
  21145. (interactive)
  21146. (unless (eq major-mode 'org-mode)
  21147. (error "Cannot un-indent a buffer not in Org mode"))
  21148. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21149. unindent-tree ; For byte-compiler.
  21150. (unindent-tree
  21151. (function
  21152. (lambda (contents)
  21153. (mapc
  21154. (lambda (element)
  21155. (if (memq (org-element-type element) '(headline section))
  21156. (funcall unindent-tree (org-element-contents element))
  21157. (save-excursion
  21158. (save-restriction
  21159. (narrow-to-region
  21160. (org-element-property :begin element)
  21161. (org-element-property :end element))
  21162. (org-do-remove-indentation)))))
  21163. (reverse contents))))))
  21164. (funcall unindent-tree (org-element-contents parse-tree))))
  21165. (defun org-show-subtree ()
  21166. "Show everything after this heading at deeper levels."
  21167. (interactive)
  21168. (outline-flag-region
  21169. (point)
  21170. (save-excursion
  21171. (org-end-of-subtree t t))
  21172. nil))
  21173. (defun org-show-entry ()
  21174. "Show the body directly following this heading.
  21175. Show the heading too, if it is currently invisible."
  21176. (interactive)
  21177. (save-excursion
  21178. (condition-case nil
  21179. (progn
  21180. (org-back-to-heading t)
  21181. (outline-flag-region
  21182. (max (point-min) (1- (point)))
  21183. (save-excursion
  21184. (if (re-search-forward
  21185. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21186. (match-beginning 1)
  21187. (point-max)))
  21188. nil)
  21189. (org-cycle-hide-drawers 'children))
  21190. (error nil))))
  21191. (defun org-make-options-regexp (kwds &optional extra)
  21192. "Make a regular expression for keyword lines."
  21193. (concat
  21194. "^#\\+\\("
  21195. (mapconcat 'regexp-quote kwds "\\|")
  21196. (if extra (concat "\\|" extra))
  21197. "\\):[ \t]*\\(.*\\)"))
  21198. ;; Make isearch reveal the necessary context
  21199. (defun org-isearch-end ()
  21200. "Reveal context after isearch exits."
  21201. (when isearch-success ; only if search was successful
  21202. (if (featurep 'xemacs)
  21203. ;; Under XEmacs, the hook is run in the correct place,
  21204. ;; we directly show the context.
  21205. (org-show-context 'isearch)
  21206. ;; In Emacs the hook runs *before* restoring the overlays.
  21207. ;; So we have to use a one-time post-command-hook to do this.
  21208. ;; (Emacs 22 has a special variable, see function `org-mode')
  21209. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21210. isearch-mode-end-hook-quit)
  21211. ;; Only when the isearch was not quitted.
  21212. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21213. 'append 'local)))
  21214. (org-fix-ellipsis-at-bol)))
  21215. (defun org-isearch-post-command ()
  21216. "Remove self from hook, and show context."
  21217. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21218. (org-show-context 'isearch))
  21219. ;;;; Integration with and fixes for other packages
  21220. ;;; Imenu support
  21221. (defvar org-imenu-markers nil
  21222. "All markers currently used by Imenu.")
  21223. (make-variable-buffer-local 'org-imenu-markers)
  21224. (defun org-imenu-new-marker (&optional pos)
  21225. "Return a new marker for use by Imenu, and remember the marker."
  21226. (let ((m (make-marker)))
  21227. (move-marker m (or pos (point)))
  21228. (push m org-imenu-markers)
  21229. m))
  21230. (defun org-imenu-get-tree ()
  21231. "Produce the index for Imenu."
  21232. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21233. (setq org-imenu-markers nil)
  21234. (let* ((n org-imenu-depth)
  21235. (re (concat "^" (org-get-limited-outline-regexp)))
  21236. (subs (make-vector (1+ n) nil))
  21237. (last-level 0)
  21238. m level head0 head)
  21239. (save-excursion
  21240. (save-restriction
  21241. (widen)
  21242. (goto-char (point-max))
  21243. (while (re-search-backward re nil t)
  21244. (setq level (org-reduced-level (funcall outline-level)))
  21245. (when (and (<= level n)
  21246. (looking-at org-complex-heading-regexp)
  21247. (setq head0 (org-match-string-no-properties 4)))
  21248. (setq head (org-link-display-format head0)
  21249. m (org-imenu-new-marker))
  21250. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21251. (if (>= level last-level)
  21252. (push (cons head m) (aref subs level))
  21253. (push (cons head (aref subs (1+ level))) (aref subs level))
  21254. (loop for i from (1+ level) to n do (aset subs i nil)))
  21255. (setq last-level level)))))
  21256. (aref subs 1)))
  21257. (eval-after-load "imenu"
  21258. '(progn
  21259. (add-hook 'imenu-after-jump-hook
  21260. (lambda ()
  21261. (if (derived-mode-p 'org-mode)
  21262. (org-show-context 'org-goto))))))
  21263. (defun org-link-display-format (link)
  21264. "Replace a link with either the description, or the link target
  21265. if no description is present"
  21266. (save-match-data
  21267. (if (string-match org-bracket-link-analytic-regexp link)
  21268. (replace-match (if (match-end 5)
  21269. (match-string 5 link)
  21270. (concat (match-string 1 link)
  21271. (match-string 3 link)))
  21272. nil t link)
  21273. link)))
  21274. (defun org-toggle-link-display ()
  21275. "Toggle the literal or descriptive display of links."
  21276. (interactive)
  21277. (if org-descriptive-links
  21278. (progn (org-remove-from-invisibility-spec '(org-link))
  21279. (org-restart-font-lock)
  21280. (setq org-descriptive-links nil))
  21281. (progn (add-to-invisibility-spec '(org-link))
  21282. (org-restart-font-lock)
  21283. (setq org-descriptive-links t))))
  21284. ;; Speedbar support
  21285. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21286. "Overlay marking the agenda restriction line in speedbar.")
  21287. (overlay-put org-speedbar-restriction-lock-overlay
  21288. 'face 'org-agenda-restriction-lock)
  21289. (overlay-put org-speedbar-restriction-lock-overlay
  21290. 'help-echo "Agendas are currently limited to this item.")
  21291. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21292. (defun org-speedbar-set-agenda-restriction ()
  21293. "Restrict future agenda commands to the location at point in speedbar.
  21294. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21295. (interactive)
  21296. (require 'org-agenda)
  21297. (let (p m tp np dir txt)
  21298. (cond
  21299. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21300. 'org-imenu t))
  21301. (setq m (get-text-property p 'org-imenu-marker))
  21302. (with-current-buffer (marker-buffer m)
  21303. (goto-char m)
  21304. (org-agenda-set-restriction-lock 'subtree)))
  21305. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21306. 'speedbar-function 'speedbar-find-file))
  21307. (setq tp (previous-single-property-change
  21308. (1+ p) 'speedbar-function)
  21309. np (next-single-property-change
  21310. tp 'speedbar-function)
  21311. dir (speedbar-line-directory)
  21312. txt (buffer-substring-no-properties (or tp (point-min))
  21313. (or np (point-max))))
  21314. (with-current-buffer (find-file-noselect
  21315. (let ((default-directory dir))
  21316. (expand-file-name txt)))
  21317. (unless (derived-mode-p 'org-mode)
  21318. (error "Cannot restrict to non-Org-mode file"))
  21319. (org-agenda-set-restriction-lock 'file)))
  21320. (t (error "Don't know how to restrict Org-mode's agenda")))
  21321. (move-overlay org-speedbar-restriction-lock-overlay
  21322. (point-at-bol) (point-at-eol))
  21323. (setq current-prefix-arg nil)
  21324. (org-agenda-maybe-redo)))
  21325. (eval-after-load "speedbar"
  21326. '(progn
  21327. (speedbar-add-supported-extension ".org")
  21328. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21329. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21330. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21331. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21332. (add-hook 'speedbar-visiting-tag-hook
  21333. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21334. ;;; Fixes and Hacks for problems with other packages
  21335. ;; Make flyspell not check words in links, to not mess up our keymap
  21336. (defvar org-element-affiliated-keywords) ; From org-element.el
  21337. (defvar org-element-block-name-alist) ; From org-element.el
  21338. (defun org-mode-flyspell-verify ()
  21339. "Don't let flyspell put overlays at active buttons, or on
  21340. {todo,all-time,additional-option-like}-keywords."
  21341. (let ((pos (max (1- (point)) (point-min)))
  21342. (word (thing-at-point 'word)))
  21343. (and (not (get-text-property pos 'keymap))
  21344. (not (get-text-property pos 'org-no-flyspell))
  21345. (not (member word org-todo-keywords-1))
  21346. (not (member word org-all-time-keywords))
  21347. (not (member word org-options-keywords))
  21348. (not (member word (mapcar 'car org-startup-options)))
  21349. (not (member-ignore-case word org-element-affiliated-keywords))
  21350. (not (member-ignore-case word (org-get-export-keywords)))
  21351. (not (member-ignore-case
  21352. word (mapcar 'car org-element-block-name-alist)))
  21353. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21354. (defun org-remove-flyspell-overlays-in (beg end)
  21355. "Remove flyspell overlays in region."
  21356. (and (org-bound-and-true-p flyspell-mode)
  21357. (fboundp 'flyspell-delete-region-overlays)
  21358. (flyspell-delete-region-overlays beg end))
  21359. (add-text-properties beg end '(org-no-flyspell t)))
  21360. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21361. (eval-after-load "bookmark"
  21362. '(if (boundp 'bookmark-after-jump-hook)
  21363. ;; We can use the hook
  21364. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21365. ;; Hook not available, use advice
  21366. (defadvice bookmark-jump (after org-make-visible activate)
  21367. "Make the position visible."
  21368. (org-bookmark-jump-unhide))))
  21369. ;; Make sure saveplace shows the location if it was hidden
  21370. (eval-after-load "saveplace"
  21371. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21372. "Make the position visible."
  21373. (org-bookmark-jump-unhide)))
  21374. ;; Make sure ecb shows the location if it was hidden
  21375. (eval-after-load "ecb"
  21376. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21377. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21378. (if (derived-mode-p 'org-mode)
  21379. (org-show-context))))
  21380. (defun org-bookmark-jump-unhide ()
  21381. "Unhide the current position, to show the bookmark location."
  21382. (and (derived-mode-p 'org-mode)
  21383. (or (outline-invisible-p)
  21384. (save-excursion (goto-char (max (point-min) (1- (point))))
  21385. (outline-invisible-p)))
  21386. (org-show-context 'bookmark-jump)))
  21387. ;; Make session.el ignore our circular variable
  21388. (eval-after-load "session"
  21389. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21390. ;;;; Experimental code
  21391. (defun org-closed-in-range ()
  21392. "Sparse tree of items closed in a certain time range.
  21393. Still experimental, may disappear in the future."
  21394. (interactive)
  21395. ;; Get the time interval from the user.
  21396. (let* ((time1 (org-float-time
  21397. (org-read-date nil 'to-time nil "Starting date: ")))
  21398. (time2 (org-float-time
  21399. (org-read-date nil 'to-time nil "End date:")))
  21400. ;; callback function
  21401. (callback (lambda ()
  21402. (let ((time
  21403. (org-float-time
  21404. (apply 'encode-time
  21405. (org-parse-time-string
  21406. (match-string 1))))))
  21407. ;; check if time in interval
  21408. (and (>= time time1) (<= time time2))))))
  21409. ;; make tree, check each match with the callback
  21410. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21411. ;;;; Finish up
  21412. (provide 'org)
  21413. (run-hooks 'org-load-hook)
  21414. ;;; org.el ends here