org.el 882 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321
  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 deck Export buffer to deck.js presentations" deck)
  453. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  454. (const :tag "C groff Export buffer to Groff format" groff)
  455. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  456. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  457. (const :tag "C s5 Export buffer to s5 presentations" s5)
  458. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  459. (eval-after-load 'ox
  460. '(mapc
  461. (lambda (backend)
  462. (condition-case nil (require (intern (format "ox-%s" backend)))
  463. (error (message "Problems while trying to load export back-end `%s'"
  464. backend))))
  465. org-export-backends))
  466. (defcustom org-support-shift-select nil
  467. "Non-nil means make shift-cursor commands select text when possible.
  468. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  469. start selecting a region, or enlarge regions started in this way.
  470. In Org-mode, in special contexts, these same keys are used for
  471. other purposes, important enough to compete with shift selection.
  472. Org tries to balance these needs by supporting `shift-select-mode'
  473. outside these special contexts, under control of this variable.
  474. The default of this variable is nil, to avoid confusing behavior. Shifted
  475. cursor keys will then execute Org commands in the following contexts:
  476. - on a headline, changing TODO state (left/right) and priority (up/down)
  477. - on a time stamp, changing the time
  478. - in a plain list item, changing the bullet type
  479. - in a property definition line, switching between allowed values
  480. - in the BEGIN line of a clock table (changing the time block).
  481. Outside these contexts, the commands will throw an error.
  482. When this variable is t and the cursor is not in a special
  483. context, Org-mode will support shift-selection for making and
  484. enlarging regions. To make this more effective, the bullet
  485. cycling will no longer happen anywhere in an item line, but only
  486. if the cursor is exactly on the bullet.
  487. If you set this variable to the symbol `always', then the keys
  488. will not be special in headlines, property lines, and item lines,
  489. to make shift selection work there as well. If this is what you
  490. want, you can use the following alternative commands: `C-c C-t'
  491. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  492. can be used to switch TODO sets, `C-c -' to cycle item bullet
  493. types, and properties can be edited by hand or in column view.
  494. However, when the cursor is on a timestamp, shift-cursor commands
  495. will still edit the time stamp - this is just too good to give up.
  496. XEmacs user should have this variable set to nil, because
  497. `shift-select-mode' is in Emacs 23 or later only."
  498. :group 'org
  499. :type '(choice
  500. (const :tag "Never" nil)
  501. (const :tag "When outside special context" t)
  502. (const :tag "Everywhere except timestamps" always)))
  503. (defcustom org-loop-over-headlines-in-active-region nil
  504. "Shall some commands act upon headlines in the active region?
  505. When set to `t', some commands will be performed in all headlines
  506. within the active region.
  507. When set to `start-level', some commands will be performed in all
  508. headlines within the active region, provided that these headlines
  509. are of the same level than the first one.
  510. When set to a string, those commands will be performed on the
  511. matching headlines within the active region. Such string must be
  512. a tags/property/todo match as it is used in the agenda tags view.
  513. The list of commands is: `org-schedule', `org-deadline',
  514. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  515. `org-archive-to-archive-sibling'. The archiving commands skip
  516. already archived entries."
  517. :type '(choice (const :tag "Don't loop" nil)
  518. (const :tag "All headlines in active region" t)
  519. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  520. (string :tag "Tags/Property/Todo matcher"))
  521. :version "24.1"
  522. :group 'org-todo
  523. :group 'org-archive)
  524. (defgroup org-startup nil
  525. "Options concerning startup of Org-mode."
  526. :tag "Org Startup"
  527. :group 'org)
  528. (defcustom org-startup-folded t
  529. "Non-nil means entering Org-mode will switch to OVERVIEW.
  530. This can also be configured on a per-file basis by adding one of
  531. the following lines anywhere in the buffer:
  532. #+STARTUP: fold (or `overview', this is equivalent)
  533. #+STARTUP: nofold (or `showall', this is equivalent)
  534. #+STARTUP: content
  535. #+STARTUP: showeverything
  536. By default, this option is ignored when Org opens agenda files
  537. for the first time. If you want the agenda to honor the startup
  538. option, set `org-agenda-inhibit-startup' to nil."
  539. :group 'org-startup
  540. :type '(choice
  541. (const :tag "nofold: show all" nil)
  542. (const :tag "fold: overview" t)
  543. (const :tag "content: all headlines" content)
  544. (const :tag "show everything, even drawers" showeverything)))
  545. (defcustom org-startup-truncated t
  546. "Non-nil means entering Org-mode will set `truncate-lines'.
  547. This is useful since some lines containing links can be very long and
  548. uninteresting. Also tables look terrible when wrapped."
  549. :group 'org-startup
  550. :type 'boolean)
  551. (defcustom org-startup-indented nil
  552. "Non-nil means turn on `org-indent-mode' on startup.
  553. This can also be configured on a per-file basis by adding one of
  554. the following lines anywhere in the buffer:
  555. #+STARTUP: indent
  556. #+STARTUP: noindent"
  557. :group 'org-structure
  558. :type '(choice
  559. (const :tag "Not" nil)
  560. (const :tag "Globally (slow on startup in large files)" t)))
  561. (defcustom org-use-sub-superscripts t
  562. "Non-nil means interpret \"_\" and \"^\" for display.
  563. When this option is turned on, you can use TeX-like syntax for sub- and
  564. superscripts. Several characters after \"_\" or \"^\" will be
  565. considered as a single item - so grouping with {} is normally not
  566. needed. For example, the following things will be parsed as single
  567. sub- or superscripts.
  568. 10^24 or 10^tau several digits will be considered 1 item.
  569. 10^-12 or 10^-tau a leading sign with digits or a word
  570. x^2-y^3 will be read as x^2 - y^3, because items are
  571. terminated by almost any nonword/nondigit char.
  572. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  573. Still, ambiguity is possible - so when in doubt use {} to enclose
  574. the sub/superscript. If you set this variable to the symbol
  575. `{}', the braces are *required* in order to trigger
  576. interpretations as sub/superscript. This can be helpful in
  577. documents that need \"_\" frequently in plain text."
  578. :group 'org-startup
  579. :version "24.1"
  580. :type '(choice
  581. (const :tag "Always interpret" t)
  582. (const :tag "Only with braces" {})
  583. (const :tag "Never interpret" nil)))
  584. (defcustom org-startup-with-beamer-mode nil
  585. "Non-nil means turn on `org-beamer-mode' on startup.
  586. This can also be configured on a per-file basis by adding one of
  587. the following lines anywhere in the buffer:
  588. #+STARTUP: beamer"
  589. :group 'org-startup
  590. :version "24.1"
  591. :type 'boolean)
  592. (defcustom org-startup-align-all-tables nil
  593. "Non-nil means align all tables when visiting a file.
  594. This is useful when the column width in tables is forced with <N> cookies
  595. in table fields. Such tables will look correct only after the first re-align.
  596. This can also be configured on a per-file basis by adding one of
  597. the following lines anywhere in the buffer:
  598. #+STARTUP: align
  599. #+STARTUP: noalign"
  600. :group 'org-startup
  601. :type 'boolean)
  602. (defcustom org-startup-with-inline-images nil
  603. "Non-nil means show inline images when loading a new Org file.
  604. This can also be configured on a per-file basis by adding one of
  605. the following lines anywhere in the buffer:
  606. #+STARTUP: inlineimages
  607. #+STARTUP: noinlineimages"
  608. :group 'org-startup
  609. :version "24.1"
  610. :type 'boolean)
  611. (defcustom org-startup-with-latex-preview nil
  612. "Non-nil means preview LaTeX fragments when loading a new Org file.
  613. This can also be configured on a per-file basis by adding one of
  614. the followinglines anywhere in the buffer:
  615. #+STARTUP: latexpreview
  616. #+STARTUP: nolatexpreview"
  617. :group 'org-startup
  618. :version "24.4"
  619. :package-version '(Org . "8.0")
  620. :type 'boolean)
  621. (defcustom org-insert-mode-line-in-empty-file nil
  622. "Non-nil means insert the first line setting Org-mode in empty files.
  623. When the function `org-mode' is called interactively in an empty file, this
  624. normally means that the file name does not automatically trigger Org-mode.
  625. To ensure that the file will always be in Org-mode in the future, a
  626. line enforcing Org-mode will be inserted into the buffer, if this option
  627. has been set."
  628. :group 'org-startup
  629. :type 'boolean)
  630. (defcustom org-replace-disputed-keys nil
  631. "Non-nil means use alternative key bindings for some keys.
  632. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  633. These keys are also used by other packages like shift-selection-mode'
  634. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  635. If you want to use Org-mode together with one of these other modes,
  636. or more generally if you would like to move some Org-mode commands to
  637. other keys, set this variable and configure the keys with the variable
  638. `org-disputed-keys'.
  639. This option is only relevant at load-time of Org-mode, and must be set
  640. *before* org.el is loaded. Changing it requires a restart of Emacs to
  641. become effective."
  642. :group 'org-startup
  643. :type 'boolean)
  644. (defcustom org-use-extra-keys nil
  645. "Non-nil means use extra key sequence definitions for certain commands.
  646. This happens automatically if you run XEmacs or if `window-system'
  647. is nil. This variable lets you do the same manually. You must
  648. set it before loading org.
  649. Example: on Carbon Emacs 22 running graphically, with an external
  650. keyboard on a Powerbook, the default way of setting M-left might
  651. not work for either Alt or ESC. Setting this variable will make
  652. it work for ESC."
  653. :group 'org-startup
  654. :type 'boolean)
  655. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  656. (defcustom org-disputed-keys
  657. '(([(shift up)] . [(meta p)])
  658. ([(shift down)] . [(meta n)])
  659. ([(shift left)] . [(meta -)])
  660. ([(shift right)] . [(meta +)])
  661. ([(control shift right)] . [(meta shift +)])
  662. ([(control shift left)] . [(meta shift -)]))
  663. "Keys for which Org-mode and other modes compete.
  664. This is an alist, cars are the default keys, second element specifies
  665. the alternative to use when `org-replace-disputed-keys' is t.
  666. Keys can be specified in any syntax supported by `define-key'.
  667. The value of this option takes effect only at Org-mode's startup,
  668. therefore you'll have to restart Emacs to apply it after changing."
  669. :group 'org-startup
  670. :type 'alist)
  671. (defun org-key (key)
  672. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  673. Or return the original if not disputed.
  674. Also apply the translations defined in `org-xemacs-key-equivalents'."
  675. (when org-replace-disputed-keys
  676. (let* ((nkey (key-description key))
  677. (x (org-find-if (lambda (x)
  678. (equal (key-description (car x)) nkey))
  679. org-disputed-keys)))
  680. (setq key (if x (cdr x) key))))
  681. (when (featurep 'xemacs)
  682. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  683. key)
  684. (defun org-find-if (predicate seq)
  685. (catch 'exit
  686. (while seq
  687. (if (funcall predicate (car seq))
  688. (throw 'exit (car seq))
  689. (pop seq)))))
  690. (defun org-defkey (keymap key def)
  691. "Define a key, possibly translated, as returned by `org-key'."
  692. (define-key keymap (org-key key) def))
  693. (defcustom org-ellipsis nil
  694. "The ellipsis to use in the Org-mode outline.
  695. When nil, just use the standard three dots. When a string, use that instead,
  696. When a face, use the standard 3 dots, but with the specified face.
  697. The change affects only Org-mode (which will then use its own display table).
  698. Changing this requires executing `M-x org-mode' in a buffer to become
  699. effective."
  700. :group 'org-startup
  701. :type '(choice (const :tag "Default" nil)
  702. (face :tag "Face" :value org-warning)
  703. (string :tag "String" :value "...#")))
  704. (defvar org-display-table nil
  705. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  706. (defgroup org-keywords nil
  707. "Keywords in Org-mode."
  708. :tag "Org Keywords"
  709. :group 'org)
  710. (defcustom org-deadline-string "DEADLINE:"
  711. "String to mark deadline entries.
  712. A deadline is this string, followed by a time stamp. Should be a word,
  713. terminated by a colon. You can insert a schedule keyword and
  714. a timestamp with \\[org-deadline].
  715. Changes become only effective after restarting Emacs."
  716. :group 'org-keywords
  717. :type 'string)
  718. (defcustom org-scheduled-string "SCHEDULED:"
  719. "String to mark scheduled TODO entries.
  720. A schedule is this string, followed by a time stamp. Should be a word,
  721. terminated by a colon. You can insert a schedule keyword and
  722. a timestamp with \\[org-schedule].
  723. Changes become only effective after restarting Emacs."
  724. :group 'org-keywords
  725. :type 'string)
  726. (defcustom org-closed-string "CLOSED:"
  727. "String used as the prefix for timestamps logging closing a TODO entry."
  728. :group 'org-keywords
  729. :type 'string)
  730. (defcustom org-clock-string "CLOCK:"
  731. "String used as prefix for timestamps clocking work hours on an item."
  732. :group 'org-keywords
  733. :type 'string)
  734. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  735. org-scheduled-string "\\|"
  736. org-deadline-string "\\|"
  737. org-closed-string "\\|"
  738. org-clock-string "\\)")
  739. "Matches a line with planning or clock info.")
  740. (defcustom org-comment-string "COMMENT"
  741. "Entries starting with this keyword will never be exported.
  742. An entry can be toggled between COMMENT and normal with
  743. \\[org-toggle-comment].
  744. Changes become only effective after restarting Emacs."
  745. :group 'org-keywords
  746. :type 'string)
  747. (defcustom org-quote-string "QUOTE"
  748. "Entries starting with this keyword will be exported in fixed-width font.
  749. Quoting applies only to the text in the entry following the headline, and does
  750. not extend beyond the next headline, even if that is lower level.
  751. An entry can be toggled between QUOTE and normal with
  752. \\[org-toggle-fixed-width-section]."
  753. :group 'org-keywords
  754. :type 'string)
  755. (defconst org-repeat-re
  756. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  757. "Regular expression for specifying repeated events.
  758. After a match, group 1 contains the repeat expression.")
  759. (defgroup org-structure nil
  760. "Options concerning the general structure of Org-mode files."
  761. :tag "Org Structure"
  762. :group 'org)
  763. (defgroup org-reveal-location nil
  764. "Options about how to make context of a location visible."
  765. :tag "Org Reveal Location"
  766. :group 'org-structure)
  767. (defconst org-context-choice
  768. '(choice
  769. (const :tag "Always" t)
  770. (const :tag "Never" nil)
  771. (repeat :greedy t :tag "Individual contexts"
  772. (cons
  773. (choice :tag "Context"
  774. (const agenda)
  775. (const org-goto)
  776. (const occur-tree)
  777. (const tags-tree)
  778. (const link-search)
  779. (const mark-goto)
  780. (const bookmark-jump)
  781. (const isearch)
  782. (const default))
  783. (boolean))))
  784. "Contexts for the reveal options.")
  785. (defcustom org-show-hierarchy-above '((default . t))
  786. "Non-nil means show full hierarchy when revealing a location.
  787. Org-mode often shows locations in an org-mode file which might have
  788. been invisible before. When this is set, the hierarchy of headings
  789. above the exposed location is shown.
  790. Turning this off for example for sparse trees makes them very compact.
  791. Instead of t, this can also be an alist specifying this option for different
  792. contexts. Valid contexts are
  793. agenda when exposing an entry from the agenda
  794. org-goto when using the command `org-goto' on key C-c C-j
  795. occur-tree when using the command `org-occur' on key C-c /
  796. tags-tree when constructing a sparse tree based on tags matches
  797. link-search when exposing search matches associated with a link
  798. mark-goto when exposing the jump goal of a mark
  799. bookmark-jump when exposing a bookmark location
  800. isearch when exiting from an incremental search
  801. default default for all contexts not set explicitly"
  802. :group 'org-reveal-location
  803. :type org-context-choice)
  804. (defcustom org-show-following-heading '((default . nil))
  805. "Non-nil means show following heading when revealing a location.
  806. Org-mode often shows locations in an org-mode file which might have
  807. been invisible before. When this is set, the heading following the
  808. match is shown.
  809. Turning this off for example for sparse trees makes them very compact,
  810. but makes it harder to edit the location of the match. In such a case,
  811. use the command \\[org-reveal] to show more context.
  812. Instead of t, this can also be an alist specifying this option for different
  813. contexts. See `org-show-hierarchy-above' for valid contexts."
  814. :group 'org-reveal-location
  815. :type org-context-choice)
  816. (defcustom org-show-siblings '((default . nil) (isearch t))
  817. "Non-nil means show all sibling heading when revealing a location.
  818. Org-mode often shows locations in an org-mode file which might have
  819. been invisible before. When this is set, the sibling of the current entry
  820. heading are all made visible. If `org-show-hierarchy-above' is t,
  821. the same happens on each level of the hierarchy above the current entry.
  822. By default this is on for the isearch context, off for all other contexts.
  823. Turning this off for example for sparse trees makes them very compact,
  824. but makes it harder to edit the location of the match. In such a case,
  825. use the command \\[org-reveal] to show more context.
  826. Instead of t, this can also be an alist specifying this option for different
  827. contexts. See `org-show-hierarchy-above' for valid contexts."
  828. :group 'org-reveal-location
  829. :type org-context-choice)
  830. (defcustom org-show-entry-below '((default . nil))
  831. "Non-nil means show the entry below a headline when revealing a location.
  832. Org-mode often shows locations in an org-mode file which might have
  833. been invisible before. When this is set, the text below the headline that is
  834. exposed is also shown.
  835. By default this is off for all contexts.
  836. Instead of t, this can also be an alist specifying this option for different
  837. contexts. See `org-show-hierarchy-above' for valid contexts."
  838. :group 'org-reveal-location
  839. :type org-context-choice)
  840. (defcustom org-indirect-buffer-display 'other-window
  841. "How should indirect tree buffers be displayed?
  842. This applies to indirect buffers created with the commands
  843. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  844. Valid values are:
  845. current-window Display in the current window
  846. other-window Just display in another window.
  847. dedicated-frame Create one new frame, and re-use it each time.
  848. new-frame Make a new frame each time. Note that in this case
  849. previously-made indirect buffers are kept, and you need to
  850. kill these buffers yourself."
  851. :group 'org-structure
  852. :group 'org-agenda-windows
  853. :type '(choice
  854. (const :tag "In current window" current-window)
  855. (const :tag "In current frame, other window" other-window)
  856. (const :tag "Each time a new frame" new-frame)
  857. (const :tag "One dedicated frame" dedicated-frame)))
  858. (defcustom org-use-speed-commands nil
  859. "Non-nil means activate single letter commands at beginning of a headline.
  860. This may also be a function to test for appropriate locations where speed
  861. commands should be active."
  862. :group 'org-structure
  863. :type '(choice
  864. (const :tag "Never" nil)
  865. (const :tag "At beginning of headline stars" t)
  866. (function)))
  867. (defcustom org-speed-commands-user nil
  868. "Alist of additional speed commands.
  869. This list will be checked before `org-speed-commands-default'
  870. when the variable `org-use-speed-commands' is non-nil
  871. and when the cursor is at the beginning of a headline.
  872. The car if each entry is a string with a single letter, which must
  873. be assigned to `self-insert-command' in the global map.
  874. The cdr is either a command to be called interactively, a function
  875. to be called, or a form to be evaluated.
  876. An entry that is just a list with a single string will be interpreted
  877. as a descriptive headline that will be added when listing the speed
  878. commands in the Help buffer using the `?' speed command."
  879. :group 'org-structure
  880. :type '(repeat :value ("k" . ignore)
  881. (choice :value ("k" . ignore)
  882. (list :tag "Descriptive Headline" (string :tag "Headline"))
  883. (cons :tag "Letter and Command"
  884. (string :tag "Command letter")
  885. (choice
  886. (function)
  887. (sexp))))))
  888. (defgroup org-cycle nil
  889. "Options concerning visibility cycling in Org-mode."
  890. :tag "Org Cycle"
  891. :group 'org-structure)
  892. (defcustom org-cycle-skip-children-state-if-no-children t
  893. "Non-nil means skip CHILDREN state in entries that don't have any."
  894. :group 'org-cycle
  895. :type 'boolean)
  896. (defcustom org-cycle-max-level nil
  897. "Maximum level which should still be subject to visibility cycling.
  898. Levels higher than this will, for cycling, be treated as text, not a headline.
  899. When `org-odd-levels-only' is set, a value of N in this variable actually
  900. means 2N-1 stars as the limiting headline.
  901. When nil, cycle all levels.
  902. Note that the limiting level of cycling is also influenced by
  903. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  904. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  905. than its value."
  906. :group 'org-cycle
  907. :type '(choice
  908. (const :tag "No limit" nil)
  909. (integer :tag "Maximum level")))
  910. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  911. "Names of drawers. Drawers are not opened by cycling on the headline above.
  912. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  913. this:
  914. :DRAWERNAME:
  915. .....
  916. :END:
  917. The drawer \"PROPERTIES\" is special for capturing properties through
  918. the property API.
  919. Drawers can be defined on the per-file basis with a line like:
  920. #+DRAWERS: HIDDEN STATE PROPERTIES"
  921. :group 'org-structure
  922. :group 'org-cycle
  923. :type '(repeat (string :tag "Drawer Name")))
  924. (defcustom org-hide-block-startup nil
  925. "Non-nil means entering Org-mode will fold all blocks.
  926. This can also be set in on a per-file basis with
  927. #+STARTUP: hideblocks
  928. #+STARTUP: showblocks"
  929. :group 'org-startup
  930. :group 'org-cycle
  931. :type 'boolean)
  932. (defcustom org-cycle-global-at-bob nil
  933. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  934. This makes it possible to do global cycling without having to use S-TAB or
  935. \\[universal-argument] TAB. For this special case to work, the first line
  936. of the buffer must not be a headline -- it may be empty or some other text.
  937. When used in this way, `org-cycle-hook' is disabled temporarily to make
  938. sure the cursor stays at the beginning of the buffer. When this option is
  939. nil, don't do anything special at the beginning of the buffer."
  940. :group 'org-cycle
  941. :type 'boolean)
  942. (defcustom org-cycle-level-after-item/entry-creation t
  943. "Non-nil means cycle entry level or item indentation in new empty entries.
  944. When the cursor is at the end of an empty headline, i.e., with only stars
  945. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  946. and then all possible ancestor states, before returning to the original state.
  947. This makes data entry extremely fast: M-RET to create a new headline,
  948. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  949. When the cursor is at the end of an empty plain list item, one TAB will
  950. make it a subitem, two or more tabs will back up to make this an item
  951. higher up in the item hierarchy."
  952. :group 'org-cycle
  953. :type 'boolean)
  954. (defcustom org-cycle-emulate-tab t
  955. "Where should `org-cycle' emulate TAB.
  956. nil Never
  957. white Only in completely white lines
  958. whitestart Only at the beginning of lines, before the first non-white char
  959. t Everywhere except in headlines
  960. exc-hl-bol Everywhere except at the start of a headline
  961. If TAB is used in a place where it does not emulate TAB, the current subtree
  962. visibility is cycled."
  963. :group 'org-cycle
  964. :type '(choice (const :tag "Never" nil)
  965. (const :tag "Only in completely white lines" white)
  966. (const :tag "Before first char in a line" whitestart)
  967. (const :tag "Everywhere except in headlines" t)
  968. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  969. ))
  970. (defcustom org-cycle-separator-lines 2
  971. "Number of empty lines needed to keep an empty line between collapsed trees.
  972. If you leave an empty line between the end of a subtree and the following
  973. headline, this empty line is hidden when the subtree is folded.
  974. Org-mode will leave (exactly) one empty line visible if the number of
  975. empty lines is equal or larger to the number given in this variable.
  976. So the default 2 means at least 2 empty lines after the end of a subtree
  977. are needed to produce free space between a collapsed subtree and the
  978. following headline.
  979. If the number is negative, and the number of empty lines is at least -N,
  980. all empty lines are shown.
  981. Special case: when 0, never leave empty lines in collapsed view."
  982. :group 'org-cycle
  983. :type 'integer)
  984. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  985. (defcustom org-pre-cycle-hook nil
  986. "Hook that is run before visibility cycling is happening.
  987. The function(s) in this hook must accept a single argument which indicates
  988. the new state that will be set right after running this hook. The
  989. argument is a symbol. Before a global state change, it can have the values
  990. `overview', `content', or `all'. Before a local state change, it can have
  991. the values `folded', `children', or `subtree'."
  992. :group 'org-cycle
  993. :type 'hook)
  994. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  995. org-cycle-hide-drawers
  996. org-cycle-hide-inline-tasks
  997. org-cycle-show-empty-lines
  998. org-optimize-window-after-visibility-change)
  999. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1000. The function(s) in this hook must accept a single argument which indicates
  1001. the new state that was set by the most recent `org-cycle' command. The
  1002. argument is a symbol. After a global state change, it can have the values
  1003. `overview', `contents', or `all'. After a local state change, it can have
  1004. the values `folded', `children', or `subtree'."
  1005. :group 'org-cycle
  1006. :type 'hook)
  1007. (defgroup org-edit-structure nil
  1008. "Options concerning structure editing in Org-mode."
  1009. :tag "Org Edit Structure"
  1010. :group 'org-structure)
  1011. (defcustom org-odd-levels-only nil
  1012. "Non-nil means skip even levels and only use odd levels for the outline.
  1013. This has the effect that two stars are being added/taken away in
  1014. promotion/demotion commands. It also influences how levels are
  1015. handled by the exporters.
  1016. Changing it requires restart of `font-lock-mode' to become effective
  1017. for fontification also in regions already fontified.
  1018. You may also set this on a per-file basis by adding one of the following
  1019. lines to the buffer:
  1020. #+STARTUP: odd
  1021. #+STARTUP: oddeven"
  1022. :group 'org-edit-structure
  1023. :group 'org-appearance
  1024. :type 'boolean)
  1025. (defcustom org-adapt-indentation t
  1026. "Non-nil means adapt indentation to outline node level.
  1027. When this variable is set, Org assumes that you write outlines by
  1028. indenting text in each node to align with the headline (after the stars).
  1029. The following issues are influenced by this variable:
  1030. - When this is set and the *entire* text in an entry is indented, the
  1031. indentation is increased by one space in a demotion command, and
  1032. decreased by one in a promotion command. If any line in the entry
  1033. body starts with text at column 0, indentation is not changed at all.
  1034. - Property drawers and planning information is inserted indented when
  1035. this variable s set. When nil, they will not be indented.
  1036. - TAB indents a line relative to context. The lines below a headline
  1037. will be indented when this variable is set.
  1038. Note that this is all about true indentation, by adding and removing
  1039. space characters. See also `org-indent.el' which does level-dependent
  1040. indentation in a virtual way, i.e. at display time in Emacs."
  1041. :group 'org-edit-structure
  1042. :type 'boolean)
  1043. (defcustom org-special-ctrl-a/e nil
  1044. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1045. When t, `C-a' will bring back the cursor to the beginning of the
  1046. headline text, i.e. after the stars and after a possible TODO
  1047. keyword. In an item, this will be the position after bullet and
  1048. check-box, if any. When the cursor is already at that position,
  1049. another `C-a' will bring it to the beginning of the line.
  1050. `C-e' will jump to the end of the headline, ignoring the presence
  1051. of tags in the headline. A second `C-e' will then jump to the
  1052. true end of the line, after any tags. This also means that, when
  1053. this variable is non-nil, `C-e' also will never jump beyond the
  1054. end of the heading of a folded section, i.e. not after the
  1055. ellipses.
  1056. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1057. normally, going to the true line boundary first. Only a directly
  1058. following, identical keypress will bring the cursor to the
  1059. special positions.
  1060. This may also be a cons cell where the behavior for `C-a' and
  1061. `C-e' is set separately."
  1062. :group 'org-edit-structure
  1063. :type '(choice
  1064. (const :tag "off" nil)
  1065. (const :tag "on: after stars/bullet and before tags first" t)
  1066. (const :tag "reversed: true line boundary first" reversed)
  1067. (cons :tag "Set C-a and C-e separately"
  1068. (choice :tag "Special C-a"
  1069. (const :tag "off" nil)
  1070. (const :tag "on: after stars/bullet first" t)
  1071. (const :tag "reversed: before stars/bullet first" reversed))
  1072. (choice :tag "Special C-e"
  1073. (const :tag "off" nil)
  1074. (const :tag "on: before tags first" t)
  1075. (const :tag "reversed: after tags first" reversed)))))
  1076. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1077. (defcustom org-special-ctrl-k nil
  1078. "Non-nil means `C-k' will behave specially in headlines.
  1079. When nil, `C-k' will call the default `kill-line' command.
  1080. When t, the following will happen while the cursor is in the headline:
  1081. - When the cursor is at the beginning of a headline, kill the entire
  1082. line and possible the folded subtree below the line.
  1083. - When in the middle of the headline text, kill the headline up to the tags.
  1084. - When after the headline text, kill the tags."
  1085. :group 'org-edit-structure
  1086. :type 'boolean)
  1087. (defcustom org-ctrl-k-protect-subtree nil
  1088. "Non-nil means, do not delete a hidden subtree with C-k.
  1089. When set to the symbol `error', simply throw an error when C-k is
  1090. used to kill (part-of) a headline that has hidden text behind it.
  1091. Any other non-nil value will result in a query to the user, if it is
  1092. OK to kill that hidden subtree. When nil, kill without remorse."
  1093. :group 'org-edit-structure
  1094. :version "24.1"
  1095. :type '(choice
  1096. (const :tag "Do not protect hidden subtrees" nil)
  1097. (const :tag "Protect hidden subtrees with a security query" t)
  1098. (const :tag "Never kill a hidden subtree with C-k" error)))
  1099. (defcustom org-catch-invisible-edits nil
  1100. "Check if in invisible region before inserting or deleting a character.
  1101. Valid values are:
  1102. nil Do not check, so just do invisible edits.
  1103. error Throw an error and do nothing.
  1104. show Make point visible, and do the requested edit.
  1105. show-and-error Make point visible, then throw an error and abort the edit.
  1106. smart Make point visible, and do insertion/deletion if it is
  1107. adjacent to visible text and the change feels predictable.
  1108. Never delete a previously invisible character or add in the
  1109. middle or right after an invisible region. Basically, this
  1110. allows insertion and backward-delete right before ellipses.
  1111. FIXME: maybe in this case we should not even show?"
  1112. :group 'org-edit-structure
  1113. :version "24.1"
  1114. :type '(choice
  1115. (const :tag "Do not check" nil)
  1116. (const :tag "Throw error when trying to edit" error)
  1117. (const :tag "Unhide, but do not do the edit" show-and-error)
  1118. (const :tag "Show invisible part and do the edit" show)
  1119. (const :tag "Be smart and do the right thing" smart)))
  1120. (defcustom org-yank-folded-subtrees t
  1121. "Non-nil means when yanking subtrees, fold them.
  1122. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1123. it starts with a heading and all other headings in it are either children
  1124. or siblings, then fold all the subtrees. However, do this only if no
  1125. text after the yank would be swallowed into a folded tree by this action."
  1126. :group 'org-edit-structure
  1127. :type 'boolean)
  1128. (defcustom org-yank-adjusted-subtrees nil
  1129. "Non-nil means when yanking subtrees, adjust the level.
  1130. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1131. this function for details."
  1132. :group 'org-edit-structure
  1133. :type 'boolean)
  1134. (defcustom org-M-RET-may-split-line '((default . t))
  1135. "Non-nil means M-RET will split the line at the cursor position.
  1136. When nil, it will go to the end of the line before making a
  1137. new line.
  1138. You may also set this option in a different way for different
  1139. contexts. Valid contexts are:
  1140. headline when creating a new headline
  1141. item when creating a new item
  1142. table in a table field
  1143. default the value to be used for all contexts not explicitly
  1144. customized"
  1145. :group 'org-structure
  1146. :group 'org-table
  1147. :type '(choice
  1148. (const :tag "Always" t)
  1149. (const :tag "Never" nil)
  1150. (repeat :greedy t :tag "Individual contexts"
  1151. (cons
  1152. (choice :tag "Context"
  1153. (const headline)
  1154. (const item)
  1155. (const table)
  1156. (const default))
  1157. (boolean)))))
  1158. (defcustom org-insert-heading-respect-content nil
  1159. "Non-nil means insert new headings after the current subtree.
  1160. When nil, the new heading is created directly after the current line.
  1161. The commands \\[org-insert-heading-respect-content] and
  1162. \\[org-insert-todo-heading-respect-content] turn this variable on
  1163. for the duration of the command."
  1164. :group 'org-structure
  1165. :type 'boolean)
  1166. (defcustom org-blank-before-new-entry '((heading . auto)
  1167. (plain-list-item . auto))
  1168. "Should `org-insert-heading' leave a blank line before new heading/item?
  1169. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1170. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1171. which case Org will look at the surrounding headings/items and try to
  1172. make an intelligent decision whether to insert a blank line or not.
  1173. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1174. set, the setting here is ignored and no empty line is inserted, to avoid
  1175. breaking the list structure."
  1176. :group 'org-edit-structure
  1177. :type '(list
  1178. (cons (const heading)
  1179. (choice (const :tag "Never" nil)
  1180. (const :tag "Always" t)
  1181. (const :tag "Auto" auto)))
  1182. (cons (const plain-list-item)
  1183. (choice (const :tag "Never" nil)
  1184. (const :tag "Always" t)
  1185. (const :tag "Auto" auto)))))
  1186. (defcustom org-insert-heading-hook nil
  1187. "Hook being run after inserting a new heading."
  1188. :group 'org-edit-structure
  1189. :type 'hook)
  1190. (defcustom org-enable-fixed-width-editor t
  1191. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1192. This currently only means they are never auto-wrapped.
  1193. When nil, such lines will be treated like ordinary lines.
  1194. See also the QUOTE keyword."
  1195. :group 'org-edit-structure
  1196. :type 'boolean)
  1197. (defcustom org-goto-auto-isearch t
  1198. "Non-nil means typing characters in `org-goto' starts incremental search.
  1199. When nil, you can use these keybindings to navigate the buffer:
  1200. q Quit the org-goto interface
  1201. n Go to the next visible heading
  1202. p Go to the previous visible heading
  1203. f Go one heading forward on same level
  1204. b Go one heading backward on same level
  1205. u Go one heading up"
  1206. :group 'org-edit-structure
  1207. :type 'boolean)
  1208. (defgroup org-sparse-trees nil
  1209. "Options concerning sparse trees in Org-mode."
  1210. :tag "Org Sparse Trees"
  1211. :group 'org-structure)
  1212. (defcustom org-highlight-sparse-tree-matches t
  1213. "Non-nil means highlight all matches that define a sparse tree.
  1214. The highlights will automatically disappear the next time the buffer is
  1215. changed by an edit command."
  1216. :group 'org-sparse-trees
  1217. :type 'boolean)
  1218. (defcustom org-remove-highlights-with-change t
  1219. "Non-nil means any change to the buffer will remove temporary highlights.
  1220. Such highlights are created by `org-occur' and `org-clock-display'.
  1221. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1222. The highlights created by `org-preview-latex-fragment' always need
  1223. `C-c C-c' to be removed."
  1224. :group 'org-sparse-trees
  1225. :group 'org-time
  1226. :type 'boolean)
  1227. (defcustom org-occur-hook '(org-first-headline-recenter)
  1228. "Hook that is run after `org-occur' has constructed a sparse tree.
  1229. This can be used to recenter the window to show as much of the structure
  1230. as possible."
  1231. :group 'org-sparse-trees
  1232. :type 'hook)
  1233. (defgroup org-imenu-and-speedbar nil
  1234. "Options concerning imenu and speedbar in Org-mode."
  1235. :tag "Org Imenu and Speedbar"
  1236. :group 'org-structure)
  1237. (defcustom org-imenu-depth 2
  1238. "The maximum level for Imenu access to Org-mode headlines.
  1239. This also applied for speedbar access."
  1240. :group 'org-imenu-and-speedbar
  1241. :type 'integer)
  1242. (defgroup org-table nil
  1243. "Options concerning tables in Org-mode."
  1244. :tag "Org Table"
  1245. :group 'org)
  1246. (defcustom org-enable-table-editor 'optimized
  1247. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1248. When nil, such lines will be treated like ordinary lines.
  1249. When equal to the symbol `optimized', the table editor will be optimized to
  1250. do the following:
  1251. - Automatic overwrite mode in front of whitespace in table fields.
  1252. This makes the structure of the table stay in tact as long as the edited
  1253. field does not exceed the column width.
  1254. - Minimize the number of realigns. Normally, the table is aligned each time
  1255. TAB or RET are pressed to move to another field. With optimization this
  1256. happens only if changes to a field might have changed the column width.
  1257. Optimization requires replacing the functions `self-insert-command',
  1258. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1259. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1260. very good at guessing when a re-align will be necessary, but you can always
  1261. force one with \\[org-ctrl-c-ctrl-c].
  1262. If you would like to use the optimized version in Org-mode, but the
  1263. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1264. This variable can be used to turn on and off the table editor during a session,
  1265. but in order to toggle optimization, a restart is required.
  1266. See also the variable `org-table-auto-blank-field'."
  1267. :group 'org-table
  1268. :type '(choice
  1269. (const :tag "off" nil)
  1270. (const :tag "on" t)
  1271. (const :tag "on, optimized" optimized)))
  1272. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1273. (version<= emacs-version "24.1"))
  1274. "Non-nil means cluster self-insert commands for undo when possible.
  1275. If this is set, then, like in the Emacs command loop, 20 consecutive
  1276. characters will be undone together.
  1277. This is configurable, because there is some impact on typing performance."
  1278. :group 'org-table
  1279. :type 'boolean)
  1280. (defcustom org-table-tab-recognizes-table.el t
  1281. "Non-nil means TAB will automatically notice a table.el table.
  1282. When it sees such a table, it moves point into it and - if necessary -
  1283. calls `table-recognize-table'."
  1284. :group 'org-table-editing
  1285. :type 'boolean)
  1286. (defgroup org-link nil
  1287. "Options concerning links in Org-mode."
  1288. :tag "Org Link"
  1289. :group 'org)
  1290. (defvar org-link-abbrev-alist-local nil
  1291. "Buffer-local version of `org-link-abbrev-alist', which see.
  1292. The value of this is taken from the #+LINK lines.")
  1293. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1294. (defcustom org-link-abbrev-alist nil
  1295. "Alist of link abbreviations.
  1296. The car of each element is a string, to be replaced at the start of a link.
  1297. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1298. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1299. [[linkkey:tag][description]]
  1300. The 'linkkey' must be a word word, starting with a letter, followed
  1301. by letters, numbers, '-' or '_'.
  1302. If REPLACE is a string, the tag will simply be appended to create the link.
  1303. If the string contains \"%s\", the tag will be inserted there. If the string
  1304. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1305. at that point (see the function `url-hexify-string'). If the string contains
  1306. the specifier \"%(my-function)\", then the custom function `my-function' will
  1307. be invoked: this function takes the tag as its only argument and must return
  1308. a string.
  1309. REPLACE may also be a function that will be called with the tag as the
  1310. only argument to create the link, which should be returned as a string.
  1311. See the manual for examples."
  1312. :group 'org-link
  1313. :type '(repeat
  1314. (cons
  1315. (string :tag "Protocol")
  1316. (choice
  1317. (string :tag "Format")
  1318. (function)))))
  1319. (defcustom org-descriptive-links t
  1320. "Non-nil means Org will display descriptive links.
  1321. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1322. \"Org Website\", hiding the link itself and just displaying its
  1323. description. When set to `nil', Org will display the full links
  1324. literally.
  1325. You can interactively set the value of this variable by calling
  1326. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1327. :group 'org-link
  1328. :type 'boolean)
  1329. (defcustom org-link-file-path-type 'adaptive
  1330. "How the path name in file links should be stored.
  1331. Valid values are:
  1332. relative Relative to the current directory, i.e. the directory of the file
  1333. into which the link is being inserted.
  1334. absolute Absolute path, if possible with ~ for home directory.
  1335. noabbrev Absolute path, no abbreviation of home directory.
  1336. adaptive Use relative path for files in the current directory and sub-
  1337. directories of it. For other files, use an absolute path."
  1338. :group 'org-link
  1339. :type '(choice
  1340. (const relative)
  1341. (const absolute)
  1342. (const noabbrev)
  1343. (const adaptive)))
  1344. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1345. "Types of links that should be activated in Org-mode files.
  1346. This is a list of symbols, each leading to the activation of a certain link
  1347. type. In principle, it does not hurt to turn on most link types - there may
  1348. be a small gain when turning off unused link types. The types are:
  1349. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1350. angle Links in angular brackets that may contain whitespace like
  1351. <bbdb:Carsten Dominik>.
  1352. plain Plain links in normal text, no whitespace, like http://google.com.
  1353. radio Text that is matched by a radio target, see manual for details.
  1354. tag Tag settings in a headline (link to tag search).
  1355. date Time stamps (link to calendar).
  1356. footnote Footnote labels.
  1357. Changing this variable requires a restart of Emacs to become effective."
  1358. :group 'org-link
  1359. :type '(set :greedy t
  1360. (const :tag "Double bracket links" bracket)
  1361. (const :tag "Angular bracket links" angle)
  1362. (const :tag "Plain text links" plain)
  1363. (const :tag "Radio target matches" radio)
  1364. (const :tag "Tags" tag)
  1365. (const :tag "Timestamps" date)
  1366. (const :tag "Footnotes" footnote)))
  1367. (defcustom org-make-link-description-function nil
  1368. "Function to use for generating link descriptions from links.
  1369. When nil, the link location will be used. This function must take
  1370. two parameters: the first one is the link, the second one is the
  1371. description generated by `org-insert-link'. The function should
  1372. return the description to use."
  1373. :group 'org-link
  1374. :type 'function)
  1375. (defgroup org-link-store nil
  1376. "Options concerning storing links in Org-mode."
  1377. :tag "Org Store Link"
  1378. :group 'org-link)
  1379. (defcustom org-url-hexify-p t
  1380. "When non-nil, hexify URL when creating a link."
  1381. :type 'boolean
  1382. :version "24.3"
  1383. :group 'org-link-store)
  1384. (defcustom org-email-link-description-format "Email %c: %.30s"
  1385. "Format of the description part of a link to an email or usenet message.
  1386. The following %-escapes will be replaced by corresponding information:
  1387. %F full \"From\" field
  1388. %f name, taken from \"From\" field, address if no name
  1389. %T full \"To\" field
  1390. %t first name in \"To\" field, address if no name
  1391. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1392. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1393. %s subject
  1394. %d date
  1395. %m message-id.
  1396. You may use normal field width specification between the % and the letter.
  1397. This is for example useful to limit the length of the subject.
  1398. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1399. :group 'org-link-store
  1400. :type 'string)
  1401. (defcustom org-from-is-user-regexp
  1402. (let (r1 r2)
  1403. (when (and user-mail-address (not (string= user-mail-address "")))
  1404. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1405. (when (and user-full-name (not (string= user-full-name "")))
  1406. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1407. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1408. "Regexp matched against the \"From:\" header of an email or usenet message.
  1409. It should match if the message is from the user him/herself."
  1410. :group 'org-link-store
  1411. :type 'regexp)
  1412. (defcustom org-context-in-file-links t
  1413. "Non-nil means file links from `org-store-link' contain context.
  1414. A search string will be added to the file name with :: as separator and
  1415. used to find the context when the link is activated by the command
  1416. `org-open-at-point'. When this option is t, the entire active region
  1417. will be placed in the search string of the file link. If set to a
  1418. positive integer, only the first n lines of context will be stored.
  1419. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1420. negates this setting for the duration of the command."
  1421. :group 'org-link-store
  1422. :type '(choice boolean integer))
  1423. (defcustom org-keep-stored-link-after-insertion nil
  1424. "Non-nil means keep link in list for entire session.
  1425. The command `org-store-link' adds a link pointing to the current
  1426. location to an internal list. These links accumulate during a session.
  1427. The command `org-insert-link' can be used to insert links into any
  1428. Org-mode file (offering completion for all stored links). When this
  1429. option is nil, every link which has been inserted once using \\[org-insert-link]
  1430. will be removed from the list, to make completing the unused links
  1431. more efficient."
  1432. :group 'org-link-store
  1433. :type 'boolean)
  1434. (defgroup org-link-follow nil
  1435. "Options concerning following links in Org-mode."
  1436. :tag "Org Follow Link"
  1437. :group 'org-link)
  1438. (defcustom org-link-translation-function nil
  1439. "Function to translate links with different syntax to Org syntax.
  1440. This can be used to translate links created for example by the Planner
  1441. or emacs-wiki packages to Org syntax.
  1442. The function must accept two parameters, a TYPE containing the link
  1443. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1444. which is everything after the link protocol. It should return a cons
  1445. with possibly modified values of type and path.
  1446. Org contains a function for this, so if you set this variable to
  1447. `org-translate-link-from-planner', you should be able follow many
  1448. links created by planner."
  1449. :group 'org-link-follow
  1450. :type 'function)
  1451. (defcustom org-follow-link-hook nil
  1452. "Hook that is run after a link has been followed."
  1453. :group 'org-link-follow
  1454. :type 'hook)
  1455. (defcustom org-tab-follows-link nil
  1456. "Non-nil means on links TAB will follow the link.
  1457. Needs to be set before org.el is loaded.
  1458. This really should not be used, it does not make sense, and the
  1459. implementation is bad."
  1460. :group 'org-link-follow
  1461. :type 'boolean)
  1462. (defcustom org-return-follows-link nil
  1463. "Non-nil means on links RET will follow the link.
  1464. In tables, the special behavior of RET has precedence."
  1465. :group 'org-link-follow
  1466. :type 'boolean)
  1467. (defcustom org-mouse-1-follows-link
  1468. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1469. "Non-nil means mouse-1 on a link will follow the link.
  1470. A longer mouse click will still set point. Does not work on XEmacs.
  1471. Needs to be set before org.el is loaded."
  1472. :group 'org-link-follow
  1473. :type 'boolean)
  1474. (defcustom org-mark-ring-length 4
  1475. "Number of different positions to be recorded in the ring.
  1476. Changing this requires a restart of Emacs to work correctly."
  1477. :group 'org-link-follow
  1478. :type 'integer)
  1479. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1480. "Non-nil means internal links in Org files must exactly match a headline.
  1481. When nil, the link search tries to match a phrase with all words
  1482. in the search text."
  1483. :group 'org-link-follow
  1484. :version "24.1"
  1485. :type '(choice
  1486. (const :tag "Use fuzzy text search" nil)
  1487. (const :tag "Match only exact headline" t)
  1488. (const :tag "Match exact headline or query to create it"
  1489. query-to-create)))
  1490. (defcustom org-link-frame-setup
  1491. '((vm . vm-visit-folder-other-frame)
  1492. (vm-imap . vm-visit-imap-folder-other-frame)
  1493. (gnus . org-gnus-no-new-news)
  1494. (file . find-file-other-window)
  1495. (wl . wl-other-frame))
  1496. "Setup the frame configuration for following links.
  1497. When following a link with Emacs, it may often be useful to display
  1498. this link in another window or frame. This variable can be used to
  1499. set this up for the different types of links.
  1500. For VM, use any of
  1501. `vm-visit-folder'
  1502. `vm-visit-folder-other-window'
  1503. `vm-visit-folder-other-frame'
  1504. For Gnus, use any of
  1505. `gnus'
  1506. `gnus-other-frame'
  1507. `org-gnus-no-new-news'
  1508. For FILE, use any of
  1509. `find-file'
  1510. `find-file-other-window'
  1511. `find-file-other-frame'
  1512. For Wanderlust use any of
  1513. `wl'
  1514. `wl-other-frame'
  1515. For the calendar, use the variable `calendar-setup'.
  1516. For BBDB, it is currently only possible to display the matches in
  1517. another window."
  1518. :group 'org-link-follow
  1519. :type '(list
  1520. (cons (const vm)
  1521. (choice
  1522. (const vm-visit-folder)
  1523. (const vm-visit-folder-other-window)
  1524. (const vm-visit-folder-other-frame)))
  1525. (cons (const gnus)
  1526. (choice
  1527. (const gnus)
  1528. (const gnus-other-frame)
  1529. (const org-gnus-no-new-news)))
  1530. (cons (const file)
  1531. (choice
  1532. (const find-file)
  1533. (const find-file-other-window)
  1534. (const find-file-other-frame)))
  1535. (cons (const wl)
  1536. (choice
  1537. (const wl)
  1538. (const wl-other-frame)))))
  1539. (defcustom org-display-internal-link-with-indirect-buffer nil
  1540. "Non-nil means use indirect buffer to display infile links.
  1541. Activating internal links (from one location in a file to another location
  1542. in the same file) normally just jumps to the location. When the link is
  1543. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1544. is displayed in
  1545. another window. When this option is set, the other window actually displays
  1546. an indirect buffer clone of the current buffer, to avoid any visibility
  1547. changes to the current buffer."
  1548. :group 'org-link-follow
  1549. :type 'boolean)
  1550. (defcustom org-open-non-existing-files nil
  1551. "Non-nil means `org-open-file' will open non-existing files.
  1552. When nil, an error will be generated.
  1553. This variable applies only to external applications because they
  1554. might choke on non-existing files. If the link is to a file that
  1555. will be opened in Emacs, the variable is ignored."
  1556. :group 'org-link-follow
  1557. :type 'boolean)
  1558. (defcustom org-open-directory-means-index-dot-org nil
  1559. "Non-nil means a link to a directory really means to index.org.
  1560. When nil, following a directory link will run dired or open a finder/explorer
  1561. window on that directory."
  1562. :group 'org-link-follow
  1563. :type 'boolean)
  1564. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1565. "Function and arguments to call for following mailto links.
  1566. This is a list with the first element being a Lisp function, and the
  1567. remaining elements being arguments to the function. In string arguments,
  1568. %a will be replaced by the address, and %s will be replaced by the subject
  1569. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1570. :group 'org-link-follow
  1571. :type '(choice
  1572. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1573. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1574. (const :tag "message-mail" (message-mail "%a" "%s"))
  1575. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1576. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1577. "Non-nil means ask for confirmation before executing shell links.
  1578. Shell links can be dangerous: just think about a link
  1579. [[shell:rm -rf ~/*][Google Search]]
  1580. This link would show up in your Org-mode document as \"Google Search\",
  1581. but really it would remove your entire home directory.
  1582. Therefore we advise against setting this variable to nil.
  1583. Just change it to `y-or-n-p' if you want to confirm with a
  1584. single keystroke rather than having to type \"yes\"."
  1585. :group 'org-link-follow
  1586. :type '(choice
  1587. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1588. (const :tag "with y-or-n (faster)" y-or-n-p)
  1589. (const :tag "no confirmation (dangerous)" nil)))
  1590. (put 'org-confirm-shell-link-function
  1591. 'safe-local-variable
  1592. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1593. (defcustom org-confirm-shell-link-not-regexp ""
  1594. "A regexp to skip confirmation for shell links."
  1595. :group 'org-link-follow
  1596. :version "24.1"
  1597. :type 'regexp)
  1598. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1599. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1600. Elisp links can be dangerous: just think about a link
  1601. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1602. This link would show up in your Org-mode document as \"Google Search\",
  1603. but really it would remove your entire home directory.
  1604. Therefore we advise against setting this variable to nil.
  1605. Just change it to `y-or-n-p' if you want to confirm with a
  1606. single keystroke rather than having to type \"yes\"."
  1607. :group 'org-link-follow
  1608. :type '(choice
  1609. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1610. (const :tag "with y-or-n (faster)" y-or-n-p)
  1611. (const :tag "no confirmation (dangerous)" nil)))
  1612. (put 'org-confirm-shell-link-function
  1613. 'safe-local-variable
  1614. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1615. (defcustom org-confirm-elisp-link-not-regexp ""
  1616. "A regexp to skip confirmation for Elisp links."
  1617. :group 'org-link-follow
  1618. :version "24.1"
  1619. :type 'regexp)
  1620. (defconst org-file-apps-defaults-gnu
  1621. '((remote . emacs)
  1622. (system . mailcap)
  1623. (t . mailcap))
  1624. "Default file applications on a UNIX or GNU/Linux system.
  1625. See `org-file-apps'.")
  1626. (defconst org-file-apps-defaults-macosx
  1627. '((remote . emacs)
  1628. (t . "open %s")
  1629. (system . "open %s")
  1630. ("ps.gz" . "gv %s")
  1631. ("eps.gz" . "gv %s")
  1632. ("dvi" . "xdvi %s")
  1633. ("fig" . "xfig %s"))
  1634. "Default file applications on a MacOS X system.
  1635. The system \"open\" is known as a default, but we use X11 applications
  1636. for some files for which the OS does not have a good default.
  1637. See `org-file-apps'.")
  1638. (defconst org-file-apps-defaults-windowsnt
  1639. (list
  1640. '(remote . emacs)
  1641. (cons t
  1642. (list (if (featurep 'xemacs)
  1643. 'mswindows-shell-execute
  1644. 'w32-shell-execute)
  1645. "open" 'file))
  1646. (cons 'system
  1647. (list (if (featurep 'xemacs)
  1648. 'mswindows-shell-execute
  1649. 'w32-shell-execute)
  1650. "open" 'file)))
  1651. "Default file applications on a Windows NT system.
  1652. The system \"open\" is used for most files.
  1653. See `org-file-apps'.")
  1654. (defcustom org-file-apps
  1655. '(
  1656. (auto-mode . emacs)
  1657. ("\\.mm\\'" . default)
  1658. ("\\.x?html?\\'" . default)
  1659. ("\\.pdf\\'" . default)
  1660. )
  1661. "External applications for opening `file:path' items in a document.
  1662. Org-mode uses system defaults for different file types, but
  1663. you can use this variable to set the application for a given file
  1664. extension. The entries in this list are cons cells where the car identifies
  1665. files and the cdr the corresponding command. Possible values for the
  1666. file identifier are
  1667. \"string\" A string as a file identifier can be interpreted in different
  1668. ways, depending on its contents:
  1669. - Alphanumeric characters only:
  1670. Match links with this file extension.
  1671. Example: (\"pdf\" . \"evince %s\")
  1672. to open PDFs with evince.
  1673. - Regular expression: Match links where the
  1674. filename matches the regexp. If you want to
  1675. use groups here, use shy groups.
  1676. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1677. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1678. to open *.html and *.xhtml with firefox.
  1679. - Regular expression which contains (non-shy) groups:
  1680. Match links where the whole link, including \"::\", and
  1681. anything after that, matches the regexp.
  1682. In a custom command string, %1, %2, etc. are replaced with
  1683. the parts of the link that were matched by the groups.
  1684. For backwards compatibility, if a command string is given
  1685. that does not use any of the group matches, this case is
  1686. handled identically to the second one (i.e. match against
  1687. file name only).
  1688. In a custom lisp form, you can access the group matches with
  1689. (match-string n link).
  1690. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1691. to open [[file:document.pdf::5]] with evince at page 5.
  1692. `directory' Matches a directory
  1693. `remote' Matches a remote file, accessible through tramp or efs.
  1694. Remote files most likely should be visited through Emacs
  1695. because external applications cannot handle such paths.
  1696. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1697. so all files Emacs knows how to handle. Using this with
  1698. command `emacs' will open most files in Emacs. Beware that this
  1699. will also open html files inside Emacs, unless you add
  1700. (\"html\" . default) to the list as well.
  1701. t Default for files not matched by any of the other options.
  1702. `system' The system command to open files, like `open' on Windows
  1703. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1704. that will be selected if you call `C-c C-o' with a double
  1705. \\[universal-argument] \\[universal-argument] prefix.
  1706. Possible values for the command are:
  1707. `emacs' The file will be visited by the current Emacs process.
  1708. `default' Use the default application for this file type, which is the
  1709. association for t in the list, most likely in the system-specific
  1710. part.
  1711. This can be used to overrule an unwanted setting in the
  1712. system-specific variable.
  1713. `system' Use the system command for opening files, like \"open\".
  1714. This command is specified by the entry whose car is `system'.
  1715. Most likely, the system-specific version of this variable
  1716. does define this command, but you can overrule/replace it
  1717. here.
  1718. string A command to be executed by a shell; %s will be replaced
  1719. by the path to the file.
  1720. sexp A Lisp form which will be evaluated. The file path will
  1721. be available in the Lisp variable `file'.
  1722. For more examples, see the system specific constants
  1723. `org-file-apps-defaults-macosx'
  1724. `org-file-apps-defaults-windowsnt'
  1725. `org-file-apps-defaults-gnu'."
  1726. :group 'org-link-follow
  1727. :type '(repeat
  1728. (cons (choice :value ""
  1729. (string :tag "Extension")
  1730. (const :tag "System command to open files" system)
  1731. (const :tag "Default for unrecognized files" t)
  1732. (const :tag "Remote file" remote)
  1733. (const :tag "Links to a directory" directory)
  1734. (const :tag "Any files that have Emacs modes"
  1735. auto-mode))
  1736. (choice :value ""
  1737. (const :tag "Visit with Emacs" emacs)
  1738. (const :tag "Use default" default)
  1739. (const :tag "Use the system command" system)
  1740. (string :tag "Command")
  1741. (sexp :tag "Lisp form")))))
  1742. (defcustom org-doi-server-url "http://dx.doi.org/"
  1743. "The URL of the DOI server."
  1744. :type 'string
  1745. :version "24.3"
  1746. :group 'org-link-follow)
  1747. (defgroup org-refile nil
  1748. "Options concerning refiling entries in Org-mode."
  1749. :tag "Org Refile"
  1750. :group 'org)
  1751. (defcustom org-directory "~/org"
  1752. "Directory with org files.
  1753. This is just a default location to look for Org files. There is no need
  1754. at all to put your files into this directory. It is only used in the
  1755. following situations:
  1756. 1. When a capture template specifies a target file that is not an
  1757. absolute path. The path will then be interpreted relative to
  1758. `org-directory'
  1759. 2. When a capture note is filed away in an interactive way (when exiting the
  1760. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1761. with `org-directory' as the default path."
  1762. :group 'org-refile
  1763. :group 'org-remember
  1764. :group 'org-capture
  1765. :type 'directory)
  1766. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1767. "Default target for storing notes.
  1768. Used as a fall back file for org-remember.el and org-capture.el, for
  1769. templates that do not specify a target file."
  1770. :group 'org-refile
  1771. :group 'org-remember
  1772. :group 'org-capture
  1773. :type '(choice
  1774. (const :tag "Default from remember-data-file" nil)
  1775. file))
  1776. (defcustom org-goto-interface 'outline
  1777. "The default interface to be used for `org-goto'.
  1778. Allowed values are:
  1779. outline The interface shows an outline of the relevant file
  1780. and the correct heading is found by moving through
  1781. the outline or by searching with incremental search.
  1782. outline-path-completion Headlines in the current buffer are offered via
  1783. completion. This is the interface also used by
  1784. the refile command."
  1785. :group 'org-refile
  1786. :type '(choice
  1787. (const :tag "Outline" outline)
  1788. (const :tag "Outline-path-completion" outline-path-completion)))
  1789. (defcustom org-goto-max-level 5
  1790. "Maximum target level when running `org-goto' with refile interface."
  1791. :group 'org-refile
  1792. :type 'integer)
  1793. (defcustom org-reverse-note-order nil
  1794. "Non-nil means store new notes at the beginning of a file or entry.
  1795. When nil, new notes will be filed to the end of a file or entry.
  1796. This can also be a list with cons cells of regular expressions that
  1797. are matched against file names, and values."
  1798. :group 'org-remember
  1799. :group 'org-capture
  1800. :group 'org-refile
  1801. :type '(choice
  1802. (const :tag "Reverse always" t)
  1803. (const :tag "Reverse never" nil)
  1804. (repeat :tag "By file name regexp"
  1805. (cons regexp boolean))))
  1806. (defcustom org-log-refile nil
  1807. "Information to record when a task is refiled.
  1808. Possible values are:
  1809. nil Don't add anything
  1810. time Add a time stamp to the task
  1811. note Prompt for a note and add it with template `org-log-note-headings'
  1812. This option can also be set with on a per-file-basis with
  1813. #+STARTUP: nologrefile
  1814. #+STARTUP: logrefile
  1815. #+STARTUP: lognoterefile
  1816. You can have local logging settings for a subtree by setting the LOGGING
  1817. property to one or more of these keywords.
  1818. When bulk-refiling from the agenda, the value `note' is forbidden and
  1819. will temporarily be changed to `time'."
  1820. :group 'org-refile
  1821. :group 'org-progress
  1822. :version "24.1"
  1823. :type '(choice
  1824. (const :tag "No logging" nil)
  1825. (const :tag "Record timestamp" time)
  1826. (const :tag "Record timestamp with note." note)))
  1827. (defcustom org-refile-targets nil
  1828. "Targets for refiling entries with \\[org-refile].
  1829. This is a list of cons cells. Each cell contains:
  1830. - a specification of the files to be considered, either a list of files,
  1831. or a symbol whose function or variable value will be used to retrieve
  1832. a file name or a list of file names. If you use `org-agenda-files' for
  1833. that, all agenda files will be scanned for targets. Nil means consider
  1834. headings in the current buffer.
  1835. - A specification of how to find candidate refile targets. This may be
  1836. any of:
  1837. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1838. This tag has to be present in all target headlines, inheritance will
  1839. not be considered.
  1840. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1841. todo keyword.
  1842. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1843. headlines that are refiling targets.
  1844. - a cons cell (:level . N). Any headline of level N is considered 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. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1848. Note that, when `org-odd-levels-only' is set, level corresponds to
  1849. order in hierarchy, not to the number of stars.
  1850. Each element of this list generates a set of possible targets.
  1851. The union of these sets is presented (with completion) to
  1852. the user by `org-refile'.
  1853. You can set the variable `org-refile-target-verify-function' to a function
  1854. to verify each headline found by the simple criteria above.
  1855. When this variable is nil, all top-level headlines in the current buffer
  1856. are used, equivalent to the value `((nil . (:level . 1))'."
  1857. :group 'org-refile
  1858. :type '(repeat
  1859. (cons
  1860. (choice :value org-agenda-files
  1861. (const :tag "All agenda files" org-agenda-files)
  1862. (const :tag "Current buffer" nil)
  1863. (function) (variable) (file))
  1864. (choice :tag "Identify target headline by"
  1865. (cons :tag "Specific tag" (const :value :tag) (string))
  1866. (cons :tag "TODO keyword" (const :value :todo) (string))
  1867. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1868. (cons :tag "Level number" (const :value :level) (integer))
  1869. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1870. (defcustom org-refile-target-verify-function nil
  1871. "Function to verify if the headline at point should be a refile target.
  1872. The function will be called without arguments, with point at the
  1873. beginning of the headline. It should return t and leave point
  1874. where it is if the headline is a valid target for refiling.
  1875. If the target should not be selected, the function must return nil.
  1876. In addition to this, it may move point to a place from where the search
  1877. should be continued. For example, the function may decide that the entire
  1878. subtree of the current entry should be excluded and move point to the end
  1879. of the subtree."
  1880. :group 'org-refile
  1881. :type 'function)
  1882. (defcustom org-refile-use-cache nil
  1883. "Non-nil means cache refile targets to speed up the process.
  1884. The cache for a particular file will be updated automatically when
  1885. the buffer has been killed, or when any of the marker used for flagging
  1886. refile targets no longer points at a live buffer.
  1887. If you have added new entries to a buffer that might themselves be targets,
  1888. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1889. find that easier, `C-u C-u C-u C-c C-w'."
  1890. :group 'org-refile
  1891. :version "24.1"
  1892. :type 'boolean)
  1893. (defcustom org-refile-use-outline-path nil
  1894. "Non-nil means provide refile targets as paths.
  1895. So a level 3 headline will be available as level1/level2/level3.
  1896. When the value is `file', also include the file name (without directory)
  1897. into the path. In this case, you can also stop the completion after
  1898. the file name, to get entries inserted as top level in the file.
  1899. When `full-file-path', include the full file path."
  1900. :group 'org-refile
  1901. :type '(choice
  1902. (const :tag "Not" nil)
  1903. (const :tag "Yes" t)
  1904. (const :tag "Start with file name" file)
  1905. (const :tag "Start with full file path" full-file-path)))
  1906. (defcustom org-outline-path-complete-in-steps t
  1907. "Non-nil means complete the outline path in hierarchical steps.
  1908. When Org-mode uses the refile interface to select an outline path
  1909. \(see variable `org-refile-use-outline-path'), the completion of
  1910. the path can be done is a single go, or if can be done in steps down
  1911. the headline hierarchy. Going in steps is probably the best if you
  1912. do not use a special completion package like `ido' or `icicles'.
  1913. However, when using these packages, going in one step can be very
  1914. fast, while still showing the whole path to the entry."
  1915. :group 'org-refile
  1916. :type 'boolean)
  1917. (defcustom org-refile-allow-creating-parent-nodes nil
  1918. "Non-nil means allow to create new nodes as refile targets.
  1919. New nodes are then created by adding \"/new node name\" to the completion
  1920. of an existing node. When the value of this variable is `confirm',
  1921. new node creation must be confirmed by the user (recommended)
  1922. When nil, the completion must match an existing entry.
  1923. Note that, if the new heading is not seen by the criteria
  1924. listed in `org-refile-targets', multiple instances of the same
  1925. heading would be created by trying again to file under the new
  1926. heading."
  1927. :group 'org-refile
  1928. :type '(choice
  1929. (const :tag "Never" nil)
  1930. (const :tag "Always" t)
  1931. (const :tag "Prompt for confirmation" confirm)))
  1932. (defcustom org-refile-active-region-within-subtree nil
  1933. "Non-nil means also refile active region within a subtree.
  1934. By default `org-refile' doesn't allow refiling regions if they
  1935. don't contain a set of subtrees, but it might be convenient to
  1936. do so sometimes: in that case, the first line of the region is
  1937. converted to a headline before refiling."
  1938. :group 'org-refile
  1939. :version "24.1"
  1940. :type 'boolean)
  1941. (defgroup org-todo nil
  1942. "Options concerning TODO items in Org-mode."
  1943. :tag "Org TODO"
  1944. :group 'org)
  1945. (defgroup org-progress nil
  1946. "Options concerning Progress logging in Org-mode."
  1947. :tag "Org Progress"
  1948. :group 'org-time)
  1949. (defvar org-todo-interpretation-widgets
  1950. '((:tag "Sequence (cycling hits every state)" sequence)
  1951. (:tag "Type (cycling directly to DONE)" type))
  1952. "The available interpretation symbols for customizing `org-todo-keywords'.
  1953. Interested libraries should add to this list.")
  1954. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1955. "List of TODO entry keyword sequences and their interpretation.
  1956. \\<org-mode-map>This is a list of sequences.
  1957. Each sequence starts with a symbol, either `sequence' or `type',
  1958. indicating if the keywords should be interpreted as a sequence of
  1959. action steps, or as different types of TODO items. The first
  1960. keywords are states requiring action - these states will select a headline
  1961. for inclusion into the global TODO list Org-mode produces. If one of
  1962. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1963. signify that no further action is necessary. If \"|\" is not found,
  1964. the last keyword is treated as the only DONE state of the sequence.
  1965. The command \\[org-todo] cycles an entry through these states, and one
  1966. additional state where no keyword is present. For details about this
  1967. cycling, see the manual.
  1968. TODO keywords and interpretation can also be set on a per-file basis with
  1969. the special #+SEQ_TODO and #+TYP_TODO lines.
  1970. Each keyword can optionally specify a character for fast state selection
  1971. \(in combination with the variable `org-use-fast-todo-selection')
  1972. and specifiers for state change logging, using the same syntax that
  1973. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1974. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1975. indicates to record a time stamp each time this state is selected.
  1976. Each keyword may also specify if a timestamp or a note should be
  1977. recorded when entering or leaving the state, by adding additional
  1978. characters in the parenthesis after the keyword. This looks like this:
  1979. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1980. record only the time of the state change. With X and Y being either
  1981. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1982. Y when leaving the state if and only if the *target* state does not
  1983. define X. You may omit any of the fast-selection key or X or /Y,
  1984. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1985. For backward compatibility, this variable may also be just a list
  1986. of keywords. In this case the interpretation (sequence or type) will be
  1987. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1988. :group 'org-todo
  1989. :group 'org-keywords
  1990. :type '(choice
  1991. (repeat :tag "Old syntax, just keywords"
  1992. (string :tag "Keyword"))
  1993. (repeat :tag "New syntax"
  1994. (cons
  1995. (choice
  1996. :tag "Interpretation"
  1997. ;;Quick and dirty way to see
  1998. ;;`org-todo-interpretations'. This takes the
  1999. ;;place of item arguments
  2000. :convert-widget
  2001. (lambda (widget)
  2002. (widget-put widget
  2003. :args (mapcar
  2004. #'(lambda (x)
  2005. (widget-convert
  2006. (cons 'const x)))
  2007. org-todo-interpretation-widgets))
  2008. widget))
  2009. (repeat
  2010. (string :tag "Keyword"))))))
  2011. (defvar org-todo-keywords-1 nil
  2012. "All TODO and DONE keywords active in a buffer.")
  2013. (make-variable-buffer-local 'org-todo-keywords-1)
  2014. (defvar org-todo-keywords-for-agenda nil)
  2015. (defvar org-done-keywords-for-agenda nil)
  2016. (defvar org-drawers-for-agenda nil)
  2017. (defvar org-todo-keyword-alist-for-agenda nil)
  2018. (defvar org-tag-alist-for-agenda nil)
  2019. (defvar org-agenda-contributing-files nil)
  2020. (defvar org-not-done-keywords nil)
  2021. (make-variable-buffer-local 'org-not-done-keywords)
  2022. (defvar org-done-keywords nil)
  2023. (make-variable-buffer-local 'org-done-keywords)
  2024. (defvar org-todo-heads nil)
  2025. (make-variable-buffer-local 'org-todo-heads)
  2026. (defvar org-todo-sets nil)
  2027. (make-variable-buffer-local 'org-todo-sets)
  2028. (defvar org-todo-log-states nil)
  2029. (make-variable-buffer-local 'org-todo-log-states)
  2030. (defvar org-todo-kwd-alist nil)
  2031. (make-variable-buffer-local 'org-todo-kwd-alist)
  2032. (defvar org-todo-key-alist nil)
  2033. (make-variable-buffer-local 'org-todo-key-alist)
  2034. (defvar org-todo-key-trigger nil)
  2035. (make-variable-buffer-local 'org-todo-key-trigger)
  2036. (defcustom org-todo-interpretation 'sequence
  2037. "Controls how TODO keywords are interpreted.
  2038. This variable is in principle obsolete and is only used for
  2039. backward compatibility, if the interpretation of todo keywords is
  2040. not given already in `org-todo-keywords'. See that variable for
  2041. more information."
  2042. :group 'org-todo
  2043. :group 'org-keywords
  2044. :type '(choice (const sequence)
  2045. (const type)))
  2046. (defcustom org-use-fast-todo-selection t
  2047. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2048. This variable describes if and under what circumstances the cycling
  2049. mechanism for TODO keywords will be replaced by a single-key, direct
  2050. selection scheme.
  2051. When nil, fast selection is never used.
  2052. When the symbol `prefix', it will be used when `org-todo' is called
  2053. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2054. `C-u t' in an agenda buffer.
  2055. When t, fast selection is used by default. In this case, the prefix
  2056. argument forces cycling instead.
  2057. In all cases, the special interface is only used if access keys have
  2058. actually been assigned by the user, i.e. if keywords in the configuration
  2059. are followed by a letter in parenthesis, like TODO(t)."
  2060. :group 'org-todo
  2061. :type '(choice
  2062. (const :tag "Never" nil)
  2063. (const :tag "By default" t)
  2064. (const :tag "Only with C-u C-c C-t" prefix)))
  2065. (defcustom org-provide-todo-statistics t
  2066. "Non-nil means update todo statistics after insert and toggle.
  2067. ALL-HEADLINES means update todo statistics by including headlines
  2068. with no TODO keyword as well, counting them as not done.
  2069. A list of TODO keywords means the same, but skip keywords that are
  2070. not in this list.
  2071. When this is set, todo statistics is updated in the parent of the
  2072. current entry each time a todo state is changed."
  2073. :group 'org-todo
  2074. :type '(choice
  2075. (const :tag "Yes, only for TODO entries" t)
  2076. (const :tag "Yes, including all entries" 'all-headlines)
  2077. (repeat :tag "Yes, for TODOs in this list"
  2078. (string :tag "TODO keyword"))
  2079. (other :tag "No TODO statistics" nil)))
  2080. (defcustom org-hierarchical-todo-statistics t
  2081. "Non-nil means TODO statistics covers just direct children.
  2082. When nil, all entries in the subtree are considered.
  2083. This has only an effect if `org-provide-todo-statistics' is set.
  2084. To set this to nil for only a single subtree, use a COOKIE_DATA
  2085. property and include the word \"recursive\" into the value."
  2086. :group 'org-todo
  2087. :type 'boolean)
  2088. (defcustom org-after-todo-state-change-hook nil
  2089. "Hook which is run after the state of a TODO item was changed.
  2090. The new state (a string with a TODO keyword, or nil) is available in the
  2091. Lisp variable `org-state'."
  2092. :group 'org-todo
  2093. :type 'hook)
  2094. (defvar org-blocker-hook nil
  2095. "Hook for functions that are allowed to block a state change.
  2096. Functions in this hook should not modify the buffer.
  2097. Each function gets as its single argument a property list,
  2098. see `org-trigger-hook' for more information about this list.
  2099. If any of the functions in this hook returns nil, the state change
  2100. is blocked.")
  2101. (defvar org-trigger-hook nil
  2102. "Hook for functions that are triggered by a state change.
  2103. Each function gets as its single argument a property list with at
  2104. least the following elements:
  2105. (:type type-of-change :position pos-at-entry-start
  2106. :from old-state :to new-state)
  2107. Depending on the type, more properties may be present.
  2108. This mechanism is currently implemented for:
  2109. TODO state changes
  2110. ------------------
  2111. :type todo-state-change
  2112. :from previous state (keyword as a string), or nil, or a symbol
  2113. 'todo' or 'done', to indicate the general type of state.
  2114. :to new state, like in :from")
  2115. (defcustom org-enforce-todo-dependencies nil
  2116. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2117. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2118. be blocked if any prior sibling is not yet done.
  2119. Finally, if the parent is blocked because of ordered siblings of its own,
  2120. the child will also be blocked."
  2121. :set (lambda (var val)
  2122. (set var val)
  2123. (if val
  2124. (add-hook 'org-blocker-hook
  2125. 'org-block-todo-from-children-or-siblings-or-parent)
  2126. (remove-hook 'org-blocker-hook
  2127. 'org-block-todo-from-children-or-siblings-or-parent)))
  2128. :group 'org-todo
  2129. :type 'boolean)
  2130. (defcustom org-enforce-todo-checkbox-dependencies nil
  2131. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2132. When this is nil, checkboxes have no influence on switching TODO states.
  2133. When non-nil, you first need to check off all check boxes before the TODO
  2134. entry can be switched to DONE.
  2135. This variable needs to be set before org.el is loaded, and you need to
  2136. restart Emacs after a change to make the change effective. The only way
  2137. to change is while Emacs is running is through the customize interface."
  2138. :set (lambda (var val)
  2139. (set var val)
  2140. (if val
  2141. (add-hook 'org-blocker-hook
  2142. 'org-block-todo-from-checkboxes)
  2143. (remove-hook 'org-blocker-hook
  2144. 'org-block-todo-from-checkboxes)))
  2145. :group 'org-todo
  2146. :type 'boolean)
  2147. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2148. "Non-nil means inserting a TODO heading is treated as state change.
  2149. So when the command \\[org-insert-todo-heading] is used, state change
  2150. logging will apply if appropriate. When nil, the new TODO item will
  2151. be inserted directly, and no logging will take place."
  2152. :group 'org-todo
  2153. :type 'boolean)
  2154. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2155. "Non-nil means switching TODO states with S-cursor counts as state change.
  2156. This is the default behavior. However, setting this to nil allows a
  2157. convenient way to select a TODO state and bypass any logging associated
  2158. with that."
  2159. :group 'org-todo
  2160. :type 'boolean)
  2161. (defcustom org-todo-state-tags-triggers nil
  2162. "Tag changes that should be triggered by TODO state changes.
  2163. This is a list. Each entry is
  2164. (state-change (tag . flag) .......)
  2165. State-change can be a string with a state, and empty string to indicate the
  2166. state that has no TODO keyword, or it can be one of the symbols `todo'
  2167. or `done', meaning any not-done or done state, respectively."
  2168. :group 'org-todo
  2169. :group 'org-tags
  2170. :type '(repeat
  2171. (cons (choice :tag "When changing to"
  2172. (const :tag "Not-done state" todo)
  2173. (const :tag "Done state" done)
  2174. (string :tag "State"))
  2175. (repeat
  2176. (cons :tag "Tag action"
  2177. (string :tag "Tag")
  2178. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2179. (defcustom org-log-done nil
  2180. "Information to record when a task moves to the DONE state.
  2181. Possible values are:
  2182. nil Don't add anything, just change the keyword
  2183. time Add a time stamp to the task
  2184. note Prompt for a note and add it with template `org-log-note-headings'
  2185. This option can also be set with on a per-file-basis with
  2186. #+STARTUP: nologdone
  2187. #+STARTUP: logdone
  2188. #+STARTUP: lognotedone
  2189. You can have local logging settings for a subtree by setting the LOGGING
  2190. property to one or more of these keywords."
  2191. :group 'org-todo
  2192. :group 'org-progress
  2193. :type '(choice
  2194. (const :tag "No logging" nil)
  2195. (const :tag "Record CLOSED timestamp" time)
  2196. (const :tag "Record CLOSED timestamp with note." note)))
  2197. ;; Normalize old uses of org-log-done.
  2198. (cond
  2199. ((eq org-log-done t) (setq org-log-done 'time))
  2200. ((and (listp org-log-done) (memq 'done org-log-done))
  2201. (setq org-log-done 'note)))
  2202. (defcustom org-log-reschedule nil
  2203. "Information to record when the scheduling date of a tasks is modified.
  2204. Possible values are:
  2205. nil Don't add anything, just change the date
  2206. time Add a time stamp to the task
  2207. note Prompt for a note and add it with template `org-log-note-headings'
  2208. This option can also be set with on a per-file-basis with
  2209. #+STARTUP: nologreschedule
  2210. #+STARTUP: logreschedule
  2211. #+STARTUP: lognotereschedule"
  2212. :group 'org-todo
  2213. :group 'org-progress
  2214. :type '(choice
  2215. (const :tag "No logging" nil)
  2216. (const :tag "Record timestamp" time)
  2217. (const :tag "Record timestamp with note." note)))
  2218. (defcustom org-log-redeadline nil
  2219. "Information to record when the deadline date of a tasks is modified.
  2220. Possible values are:
  2221. nil Don't add anything, just change the date
  2222. time Add a time stamp to the task
  2223. note Prompt for a note and add it with template `org-log-note-headings'
  2224. This option can also be set with on a per-file-basis with
  2225. #+STARTUP: nologredeadline
  2226. #+STARTUP: logredeadline
  2227. #+STARTUP: lognoteredeadline
  2228. You can have local logging settings for a subtree by setting the LOGGING
  2229. property to one or more of these keywords."
  2230. :group 'org-todo
  2231. :group 'org-progress
  2232. :type '(choice
  2233. (const :tag "No logging" nil)
  2234. (const :tag "Record timestamp" time)
  2235. (const :tag "Record timestamp with note." note)))
  2236. (defcustom org-log-note-clock-out nil
  2237. "Non-nil means record a note when clocking out of an item.
  2238. This can also be configured on a per-file basis by adding one of
  2239. the following lines anywhere in the buffer:
  2240. #+STARTUP: lognoteclock-out
  2241. #+STARTUP: nolognoteclock-out"
  2242. :group 'org-todo
  2243. :group 'org-progress
  2244. :type 'boolean)
  2245. (defcustom org-log-done-with-time t
  2246. "Non-nil means the CLOSED time stamp will contain date and time.
  2247. When nil, only the date will be recorded."
  2248. :group 'org-progress
  2249. :type 'boolean)
  2250. (defcustom org-log-note-headings
  2251. '((done . "CLOSING NOTE %t")
  2252. (state . "State %-12s from %-12S %t")
  2253. (note . "Note taken on %t")
  2254. (reschedule . "Rescheduled from %S on %t")
  2255. (delschedule . "Not scheduled, was %S on %t")
  2256. (redeadline . "New deadline from %S on %t")
  2257. (deldeadline . "Removed deadline, was %S on %t")
  2258. (refile . "Refiled on %t")
  2259. (clock-out . ""))
  2260. "Headings for notes added to entries.
  2261. The value is an alist, with the car being a symbol indicating the note
  2262. context, and the cdr is the heading to be used. The heading may also be the
  2263. empty string.
  2264. %t in the heading will be replaced by a time stamp.
  2265. %T will be an active time stamp instead the default inactive one
  2266. %d will be replaced by a short-format time stamp.
  2267. %D will be replaced by an active short-format time stamp.
  2268. %s will be replaced by the new TODO state, in double quotes.
  2269. %S will be replaced by the old TODO state, in double quotes.
  2270. %u will be replaced by the user name.
  2271. %U will be replaced by the full user name.
  2272. In fact, it is not a good idea to change the `state' entry, because
  2273. agenda log mode depends on the format of these entries."
  2274. :group 'org-todo
  2275. :group 'org-progress
  2276. :type '(list :greedy t
  2277. (cons (const :tag "Heading when closing an item" done) string)
  2278. (cons (const :tag
  2279. "Heading when changing todo state (todo sequence only)"
  2280. state) string)
  2281. (cons (const :tag "Heading when just taking a note" note) string)
  2282. (cons (const :tag "Heading when clocking out" clock-out) string)
  2283. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2284. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2285. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2286. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2287. (cons (const :tag "Heading when refiling" refile) string)))
  2288. (unless (assq 'note org-log-note-headings)
  2289. (push '(note . "%t") org-log-note-headings))
  2290. (defcustom org-log-into-drawer nil
  2291. "Non-nil means insert state change notes and time stamps into a drawer.
  2292. When nil, state changes notes will be inserted after the headline and
  2293. any scheduling and clock lines, but not inside a drawer.
  2294. The value of this variable should be the name of the drawer to use.
  2295. LOGBOOK is proposed as the default drawer for this purpose, you can
  2296. also set this to a string to define the drawer of your choice.
  2297. A value of t is also allowed, representing \"LOGBOOK\".
  2298. A value of t or nil can also be set with on a per-file-basis with
  2299. #+STARTUP: logdrawer
  2300. #+STARTUP: nologdrawer
  2301. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2302. will be ignored.
  2303. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2304. a subtree."
  2305. :group 'org-todo
  2306. :group 'org-progress
  2307. :type '(choice
  2308. (const :tag "Not into a drawer" nil)
  2309. (const :tag "LOGBOOK" t)
  2310. (string :tag "Other")))
  2311. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2312. (defun org-log-into-drawer ()
  2313. "Return the value of `org-log-into-drawer', but let properties overrule.
  2314. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2315. used instead of the default value."
  2316. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2317. (cond
  2318. ((not p) org-log-into-drawer)
  2319. ((equal p "nil") nil)
  2320. ((equal p "t") "LOGBOOK")
  2321. (t p))))
  2322. (defcustom org-log-state-notes-insert-after-drawers nil
  2323. "Non-nil means insert state change notes after any drawers in entry.
  2324. Only the drawers that *immediately* follow the headline and the
  2325. deadline/scheduled line are skipped.
  2326. When nil, insert notes right after the heading and perhaps the line
  2327. with deadline/scheduling if present.
  2328. This variable will have no effect if `org-log-into-drawer' is
  2329. set."
  2330. :group 'org-todo
  2331. :group 'org-progress
  2332. :type 'boolean)
  2333. (defcustom org-log-states-order-reversed t
  2334. "Non-nil means the latest state note will be directly after heading.
  2335. When nil, the state change notes will be ordered according to time.
  2336. This option can also be set with on a per-file-basis with
  2337. #+STARTUP: logstatesreversed
  2338. #+STARTUP: nologstatesreversed"
  2339. :group 'org-todo
  2340. :group 'org-progress
  2341. :type 'boolean)
  2342. (defcustom org-todo-repeat-to-state nil
  2343. "The TODO state to which a repeater should return the repeating task.
  2344. By default this is the first task in a TODO sequence, or the previous state
  2345. in a TODO_TYP set. But you can specify another task here.
  2346. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2347. :group 'org-todo
  2348. :version "24.1"
  2349. :type '(choice (const :tag "Head of sequence" nil)
  2350. (string :tag "Specific state")))
  2351. (defcustom org-log-repeat 'time
  2352. "Non-nil means record moving through the DONE state when triggering repeat.
  2353. An auto-repeating task is immediately switched back to TODO when
  2354. marked DONE. If you are not logging state changes (by adding \"@\"
  2355. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2356. record a closing note, there will be no record of the task moving
  2357. through DONE. This variable forces taking a note anyway.
  2358. nil Don't force a record
  2359. time Record a time stamp
  2360. note Prompt for a note and add it with template `org-log-note-headings'
  2361. This option can also be set with on a per-file-basis with
  2362. #+STARTUP: nologrepeat
  2363. #+STARTUP: logrepeat
  2364. #+STARTUP: lognoterepeat
  2365. You can have local logging settings for a subtree by setting the LOGGING
  2366. property to one or more of these keywords."
  2367. :group 'org-todo
  2368. :group 'org-progress
  2369. :type '(choice
  2370. (const :tag "Don't force a record" nil)
  2371. (const :tag "Force recording the DONE state" time)
  2372. (const :tag "Force recording a note with the DONE state" note)))
  2373. (defgroup org-priorities nil
  2374. "Priorities in Org-mode."
  2375. :tag "Org Priorities"
  2376. :group 'org-todo)
  2377. (defcustom org-enable-priority-commands t
  2378. "Non-nil means priority commands are active.
  2379. When nil, these commands will be disabled, so that you never accidentally
  2380. set a priority."
  2381. :group 'org-priorities
  2382. :type 'boolean)
  2383. (defcustom org-highest-priority ?A
  2384. "The highest priority of TODO items. A character like ?A, ?B etc.
  2385. Must have a smaller ASCII number than `org-lowest-priority'."
  2386. :group 'org-priorities
  2387. :type 'character)
  2388. (defcustom org-lowest-priority ?C
  2389. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2390. Must have a larger ASCII number than `org-highest-priority'."
  2391. :group 'org-priorities
  2392. :type 'character)
  2393. (defcustom org-default-priority ?B
  2394. "The default priority of TODO items.
  2395. This is the priority an item gets if no explicit priority is given.
  2396. When starting to cycle on an empty priority the first step in the cycle
  2397. depends on `org-priority-start-cycle-with-default'. The resulting first
  2398. step priority must not exceed the range from `org-highest-priority' to
  2399. `org-lowest-priority' which means that `org-default-priority' has to be
  2400. in this range exclusive or inclusive the range boundaries. Else the
  2401. first step refuses to set the default and the second will fall back
  2402. to (depending on the command used) the highest or lowest priority."
  2403. :group 'org-priorities
  2404. :type 'character)
  2405. (defcustom org-priority-start-cycle-with-default t
  2406. "Non-nil means start with default priority when starting to cycle.
  2407. When this is nil, the first step in the cycle will be (depending on the
  2408. command used) one higher or lower than the default priority.
  2409. See also `org-default-priority'."
  2410. :group 'org-priorities
  2411. :type 'boolean)
  2412. (defcustom org-get-priority-function nil
  2413. "Function to extract the priority from a string.
  2414. The string is normally the headline. If this is nil Org computes the
  2415. priority from the priority cookie like [#A] in the headline. It returns
  2416. an integer, increasing by 1000 for each priority level.
  2417. The user can set a different function here, which should take a string
  2418. as an argument and return the numeric priority."
  2419. :group 'org-priorities
  2420. :version "24.1"
  2421. :type 'function)
  2422. (defgroup org-time nil
  2423. "Options concerning time stamps and deadlines in Org-mode."
  2424. :tag "Org Time"
  2425. :group 'org)
  2426. (defcustom org-insert-labeled-timestamps-at-point nil
  2427. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2428. When nil, these labeled time stamps are forces into the second line of an
  2429. entry, just after the headline. When scheduling from the global TODO list,
  2430. the time stamp will always be forced into the second line."
  2431. :group 'org-time
  2432. :type 'boolean)
  2433. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2434. "Formats for `format-time-string' which are used for time stamps.
  2435. It is not recommended to change this constant.")
  2436. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2437. "Number of minutes to round time stamps to.
  2438. These are two values, the first applies when first creating a time stamp.
  2439. The second applies when changing it with the commands `S-up' and `S-down'.
  2440. When changing the time stamp, this means that it will change in steps
  2441. of N minutes, as given by the second value.
  2442. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2443. numbers should be factors of 60, so for example 5, 10, 15.
  2444. When this is larger than 1, you can still force an exact time stamp by using
  2445. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2446. and by using a prefix arg to `S-up/down' to specify the exact number
  2447. of minutes to shift."
  2448. :group 'org-time
  2449. :get #'(lambda (var) ; Make sure both elements are there
  2450. (if (integerp (default-value var))
  2451. (list (default-value var) 5)
  2452. (default-value var)))
  2453. :type '(list
  2454. (integer :tag "when inserting times")
  2455. (integer :tag "when modifying times")))
  2456. ;; Normalize old customizations of this variable.
  2457. (when (integerp org-time-stamp-rounding-minutes)
  2458. (setq org-time-stamp-rounding-minutes
  2459. (list org-time-stamp-rounding-minutes
  2460. org-time-stamp-rounding-minutes)))
  2461. (defcustom org-display-custom-times nil
  2462. "Non-nil means overlay custom formats over all time stamps.
  2463. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2464. To turn this on on a per-file basis, insert anywhere in the file:
  2465. #+STARTUP: customtime"
  2466. :group 'org-time
  2467. :set 'set-default
  2468. :type 'sexp)
  2469. (make-variable-buffer-local 'org-display-custom-times)
  2470. (defcustom org-time-stamp-custom-formats
  2471. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2472. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2473. These are overlaid over the default ISO format if the variable
  2474. `org-display-custom-times' is set. Time like %H:%M should be at the
  2475. end of the second format. The custom formats are also honored by export
  2476. commands, if custom time display is turned on at the time of export."
  2477. :group 'org-time
  2478. :type 'sexp)
  2479. (defun org-time-stamp-format (&optional long inactive)
  2480. "Get the right format for a time string."
  2481. (let ((f (if long (cdr org-time-stamp-formats)
  2482. (car org-time-stamp-formats))))
  2483. (if inactive
  2484. (concat "[" (substring f 1 -1) "]")
  2485. f)))
  2486. (defcustom org-time-clocksum-format
  2487. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2488. "The format string used when creating CLOCKSUM lines.
  2489. This is also used when Org mode generates a time duration.
  2490. The value can be a single format string containing two
  2491. %-sequences, which will be filled with the number of hours and
  2492. minutes in that order.
  2493. Alternatively, the value can be a plist associating any of the
  2494. keys :years, :months, :weeks, :days, :hours or :minutes with
  2495. format strings. The time duration is formatted using only the
  2496. time components that are needed and concatenating the results.
  2497. If a time unit in absent, it falls back to the next smallest
  2498. unit.
  2499. The keys :require-years, :require-months, :require-days,
  2500. :require-weeks, :require-hours, :require-minutes are also
  2501. meaningful. A non-nil value for these keys indicates that the
  2502. corresponding time component should always be included, even if
  2503. its value is 0.
  2504. For example,
  2505. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2506. :require-minutes t)
  2507. means durations longer than a day will be expressed in days,
  2508. hours and minutes, and durations less than a day will always be
  2509. expressed in hours and minutes (even for durations less than an
  2510. hour).
  2511. The value
  2512. \(:days \"%dd\" :minutes \"%dm\")
  2513. means durations longer than a day will be expressed in days and
  2514. minutes, and durations less than a day will be expressed entirely
  2515. in minutes (even for durations longer than an hour)."
  2516. :group 'org-time
  2517. :group 'org-clock
  2518. :version "24.4"
  2519. :package-version '(Org . "8.0")
  2520. :type '(choice (string :tag "Format string")
  2521. (set :tag "Plist"
  2522. (group :inline t (const :tag "Years" :years)
  2523. (string :tag "Format string"))
  2524. (group :inline t
  2525. (const :tag "Always show years" :require-years)
  2526. (const t))
  2527. (group :inline t (const :tag "Months" :months)
  2528. (string :tag "Format string"))
  2529. (group :inline t
  2530. (const :tag "Always show months" :require-months)
  2531. (const t))
  2532. (group :inline t (const :tag "Weeks" :weeks)
  2533. (string :tag "Format string"))
  2534. (group :inline t
  2535. (const :tag "Always show weeks" :require-weeks)
  2536. (const t))
  2537. (group :inline t (const :tag "Days" :days)
  2538. (string :tag "Format string"))
  2539. (group :inline t
  2540. (const :tag "Always show days" :require-days)
  2541. (const t))
  2542. (group :inline t (const :tag "Hours" :hours)
  2543. (string :tag "Format string"))
  2544. (group :inline t
  2545. (const :tag "Always show hours" :require-hours)
  2546. (const t))
  2547. (group :inline t (const :tag "Minutes" :minutes)
  2548. (string :tag "Format string"))
  2549. (group :inline t
  2550. (const :tag "Always show minutes" :require-minutes)
  2551. (const t)))))
  2552. (defcustom org-time-clocksum-use-fractional nil
  2553. "When non-nil, \\[org-clock-display] uses fractional times.
  2554. See `org-time-clocksum-format' for more on time clock formats."
  2555. :group 'org-time
  2556. :group 'org-clock
  2557. :version "24.3"
  2558. :type 'boolean)
  2559. (defcustom org-time-clocksum-use-effort-durations nil
  2560. "When non-nil, \\[org-clock-display] uses effort durations.
  2561. E.g. by default, one day is considered to be a 8 hours effort,
  2562. so a task that has been clocked for 16 hours will be displayed
  2563. as during 2 days in the clock display or in the clocktable.
  2564. See `org-effort-durations' on how to set effort durations
  2565. and `org-time-clocksum-format' for more on time clock formats."
  2566. :group 'org-time
  2567. :group 'org-clock
  2568. :version "24.4"
  2569. :package-version '(Org . "8.0")
  2570. :type 'boolean)
  2571. (defcustom org-time-clocksum-fractional-format "%.2f"
  2572. "The format string used when creating CLOCKSUM lines,
  2573. or when Org mode generates a time duration, if
  2574. `org-time-clocksum-use-fractional' is enabled.
  2575. The value can be a single format string containing one
  2576. %-sequence, which will be filled with the number of hours as
  2577. a float.
  2578. Alternatively, the value can be a plist associating any of the
  2579. keys :years, :months, :weeks, :days, :hours or :minutes with
  2580. a format string. The time duration is formatted using the
  2581. largest time unit which gives a non-zero integer part. If all
  2582. specified formats have zero integer part, the smallest time unit
  2583. is used."
  2584. :group 'org-time
  2585. :type '(choice (string :tag "Format string")
  2586. (set (group :inline t (const :tag "Years" :years)
  2587. (string :tag "Format string"))
  2588. (group :inline t (const :tag "Months" :months)
  2589. (string :tag "Format string"))
  2590. (group :inline t (const :tag "Weeks" :weeks)
  2591. (string :tag "Format string"))
  2592. (group :inline t (const :tag "Days" :days)
  2593. (string :tag "Format string"))
  2594. (group :inline t (const :tag "Hours" :hours)
  2595. (string :tag "Format string"))
  2596. (group :inline t (const :tag "Minutes" :minutes)
  2597. (string :tag "Format string")))))
  2598. (defcustom org-deadline-warning-days 14
  2599. "Number of days before expiration during which a deadline becomes active.
  2600. This variable governs the display in sparse trees and in the agenda.
  2601. When 0 or negative, it means use this number (the absolute value of it)
  2602. even if a deadline has a different individual lead time specified.
  2603. Custom commands can set this variable in the options section."
  2604. :group 'org-time
  2605. :group 'org-agenda-daily/weekly
  2606. :type 'integer)
  2607. (defcustom org-scheduled-delay-days 0
  2608. "Number of days before a scheduled item becomes active.
  2609. This variable governs the display in sparse trees and in the agenda.
  2610. The default value (i.e. 0) means: don't delay scheduled item.
  2611. When negative, it means use this number (the absolute value of it)
  2612. even if a scheduled item has a different individual delay time
  2613. specified.
  2614. Custom commands can set this variable in the options section."
  2615. :group 'org-time
  2616. :group 'org-agenda-daily/weekly
  2617. :version "24.4"
  2618. :package-version '(Org . "8.0")
  2619. :type 'integer)
  2620. (defcustom org-read-date-prefer-future t
  2621. "Non-nil means assume future for incomplete date input from user.
  2622. This affects the following situations:
  2623. 1. The user gives a month but not a year.
  2624. For example, if it is April and you enter \"feb 2\", this will be read
  2625. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2626. 2. The user gives a day, but no month.
  2627. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2628. read this as the third of *next* month. However, if you enter \"17\",
  2629. it will be considered as *this* month.
  2630. If you set this variable to the symbol `time', then also the following
  2631. will work:
  2632. 3. If the user gives a time.
  2633. If the time is before now, it will be interpreted as tomorrow.
  2634. Currently none of this works for ISO week specifications.
  2635. When this option is nil, the current day, month and year will always be
  2636. used as defaults.
  2637. See also `org-agenda-jump-prefer-future'."
  2638. :group 'org-time
  2639. :type '(choice
  2640. (const :tag "Never" nil)
  2641. (const :tag "Check month and day" t)
  2642. (const :tag "Check month, day, and time" time)))
  2643. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2644. "Should the agenda jump command prefer the future for incomplete dates?
  2645. The default is to do the same as configured in `org-read-date-prefer-future'.
  2646. But you can also set a deviating value here.
  2647. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2648. :group 'org-agenda
  2649. :group 'org-time
  2650. :version "24.1"
  2651. :type '(choice
  2652. (const :tag "Use org-read-date-prefer-future"
  2653. org-read-date-prefer-future)
  2654. (const :tag "Never" nil)
  2655. (const :tag "Always" t)))
  2656. (defcustom org-read-date-force-compatible-dates t
  2657. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2658. Depending on the system Emacs is running on, certain dates cannot
  2659. be represented with the type used internally to represent time.
  2660. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2661. correctly. Some systems allow for earlier dates, some for later,
  2662. some for both. One way to find out it to insert any date into an
  2663. Org buffer, putting the cursor on the year and hitting S-up and
  2664. S-down to test the range.
  2665. When this variable is set to t, the date/time prompt will not let
  2666. you specify dates outside the 1970-2037 range, so it is certain that
  2667. these dates will work in whatever version of Emacs you are
  2668. running, and also that you can move a file from one Emacs implementation
  2669. to another. WHenever Org is forcing the year for you, it will display
  2670. a message and beep.
  2671. When this variable is nil, Org will check if the date is
  2672. representable in the specific Emacs implementation you are using.
  2673. If not, it will force a year, usually the current year, and beep
  2674. to remind you. Currently this setting is not recommended because
  2675. the likelihood that you will open your Org files in an Emacs that
  2676. has limited date range is not negligible.
  2677. A workaround for this problem is to use diary sexp dates for time
  2678. stamps outside of this range."
  2679. :group 'org-time
  2680. :version "24.1"
  2681. :type 'boolean)
  2682. (defcustom org-read-date-display-live t
  2683. "Non-nil means display current interpretation of date prompt live.
  2684. This display will be in an overlay, in the minibuffer."
  2685. :group 'org-time
  2686. :type 'boolean)
  2687. (defcustom org-read-date-popup-calendar t
  2688. "Non-nil means pop up a calendar when prompting for a date.
  2689. In the calendar, the date can be selected with mouse-1. However, the
  2690. minibuffer will also be active, and you can simply enter the date as well.
  2691. When nil, only the minibuffer will be available."
  2692. :group 'org-time
  2693. :type 'boolean)
  2694. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2695. 'org-read-date-popup-calendar)
  2696. (make-obsolete-variable
  2697. 'org-read-date-minibuffer-setup-hook
  2698. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2699. (defcustom org-read-date-minibuffer-setup-hook nil
  2700. "Hook to be used to set up keys for the date/time interface.
  2701. Add key definitions to `minibuffer-local-map', which will be a
  2702. temporary copy.
  2703. WARNING: This option is obsolete, you should use
  2704. `org-read-date-minibuffer-local-map' to set up keys."
  2705. :group 'org-time
  2706. :type 'hook)
  2707. (defcustom org-extend-today-until 0
  2708. "The hour when your day really ends. Must be an integer.
  2709. This has influence for the following applications:
  2710. - When switching the agenda to \"today\". It it is still earlier than
  2711. the time given here, the day recognized as TODAY is actually yesterday.
  2712. - When a date is read from the user and it is still before the time given
  2713. here, the current date and time will be assumed to be yesterday, 23:59.
  2714. Also, timestamps inserted in capture templates follow this rule.
  2715. IMPORTANT: This is a feature whose implementation is and likely will
  2716. remain incomplete. Really, it is only here because past midnight seems to
  2717. be the favorite working time of John Wiegley :-)"
  2718. :group 'org-time
  2719. :type 'integer)
  2720. (defcustom org-use-effective-time nil
  2721. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2722. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2723. \"effective time\" of any timestamps between midnight and 8am will be
  2724. 23:59 of the previous day."
  2725. :group 'org-time
  2726. :version "24.1"
  2727. :type 'boolean)
  2728. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2729. "When non-nil, use the last clock out time for `org-todo'.
  2730. Note that this option has precedence over the combined use of
  2731. `org-use-effective-time' and `org-extend-today-until'."
  2732. :group 'org-time
  2733. :version "24.4"
  2734. :package-version '(Org . "8.0")
  2735. :type 'boolean)
  2736. (defcustom org-edit-timestamp-down-means-later nil
  2737. "Non-nil means S-down will increase the time in a time stamp.
  2738. When nil, S-up will increase."
  2739. :group 'org-time
  2740. :type 'boolean)
  2741. (defcustom org-calendar-follow-timestamp-change t
  2742. "Non-nil means make the calendar window follow timestamp changes.
  2743. When a timestamp is modified and the calendar window is visible, it will be
  2744. moved to the new date."
  2745. :group 'org-time
  2746. :type 'boolean)
  2747. (defgroup org-tags nil
  2748. "Options concerning tags in Org-mode."
  2749. :tag "Org Tags"
  2750. :group 'org)
  2751. (defcustom org-tag-alist nil
  2752. "List of tags allowed in Org-mode files.
  2753. When this list is nil, Org-mode will base TAG input on what is already in the
  2754. buffer.
  2755. The value of this variable is an alist, the car of each entry must be a
  2756. keyword as a string, the cdr may be a character that is used to select
  2757. that tag through the fast-tag-selection interface.
  2758. See the manual for details."
  2759. :group 'org-tags
  2760. :type '(repeat
  2761. (choice
  2762. (cons (string :tag "Tag name")
  2763. (character :tag "Access char"))
  2764. (list :tag "Start radio group"
  2765. (const :startgroup)
  2766. (option (string :tag "Group description")))
  2767. (list :tag "End radio group"
  2768. (const :endgroup)
  2769. (option (string :tag "Group description")))
  2770. (const :tag "New line" (:newline)))))
  2771. (defcustom org-tag-persistent-alist nil
  2772. "List of tags that will always appear in all Org-mode files.
  2773. This is in addition to any in buffer settings or customizations
  2774. of `org-tag-alist'.
  2775. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2776. The value of this variable is an alist, the car of each entry must be a
  2777. keyword as a string, the cdr may be a character that is used to select
  2778. that tag through the fast-tag-selection interface.
  2779. See the manual for details.
  2780. To disable these tags on a per-file basis, insert anywhere in the file:
  2781. #+STARTUP: noptag"
  2782. :group 'org-tags
  2783. :type '(repeat
  2784. (choice
  2785. (cons (string :tag "Tag name")
  2786. (character :tag "Access char"))
  2787. (const :tag "Start radio group" (:startgroup))
  2788. (const :tag "End radio group" (:endgroup))
  2789. (const :tag "New line" (:newline)))))
  2790. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2791. "If non-nil, always offer completion for all tags of all agenda files.
  2792. Instead of customizing this variable directly, you might want to
  2793. set it locally for capture buffers, because there no list of
  2794. tags in that file can be created dynamically (there are none).
  2795. (add-hook 'org-capture-mode-hook
  2796. (lambda ()
  2797. (set (make-local-variable
  2798. 'org-complete-tags-always-offer-all-agenda-tags)
  2799. t)))"
  2800. :group 'org-tags
  2801. :version "24.1"
  2802. :type 'boolean)
  2803. (defvar org-file-tags nil
  2804. "List of tags that can be inherited by all entries in the file.
  2805. The tags will be inherited if the variable `org-use-tag-inheritance'
  2806. says they should be.
  2807. This variable is populated from #+FILETAGS lines.")
  2808. (defcustom org-use-fast-tag-selection 'auto
  2809. "Non-nil means use fast tag selection scheme.
  2810. This is a special interface to select and deselect tags with single keys.
  2811. When nil, fast selection is never used.
  2812. When the symbol `auto', fast selection is used if and only if selection
  2813. characters for tags have been configured, either through the variable
  2814. `org-tag-alist' or through a #+TAGS line in the buffer.
  2815. When t, fast selection is always used and selection keys are assigned
  2816. automatically if necessary."
  2817. :group 'org-tags
  2818. :type '(choice
  2819. (const :tag "Always" t)
  2820. (const :tag "Never" nil)
  2821. (const :tag "When selection characters are configured" 'auto)))
  2822. (defcustom org-fast-tag-selection-single-key nil
  2823. "Non-nil means fast tag selection exits after first change.
  2824. When nil, you have to press RET to exit it.
  2825. During fast tag selection, you can toggle this flag with `C-c'.
  2826. This variable can also have the value `expert'. In this case, the window
  2827. displaying the tags menu is not even shown, until you press C-c again."
  2828. :group 'org-tags
  2829. :type '(choice
  2830. (const :tag "No" nil)
  2831. (const :tag "Yes" t)
  2832. (const :tag "Expert" expert)))
  2833. (defvar org-fast-tag-selection-include-todo nil
  2834. "Non-nil means fast tags selection interface will also offer TODO states.
  2835. This is an undocumented feature, you should not rely on it.")
  2836. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2837. "The column to which tags should be indented in a headline.
  2838. If this number is positive, it specifies the column. If it is negative,
  2839. it means that the tags should be flushright to that column. For example,
  2840. -80 works well for a normal 80 character screen.
  2841. When 0, place tags directly after headline text, with only one space in
  2842. between."
  2843. :group 'org-tags
  2844. :type 'integer)
  2845. (defcustom org-auto-align-tags t
  2846. "Non-nil keeps tags aligned when modifying headlines.
  2847. Some operations (i.e. demoting) change the length of a headline and
  2848. therefore shift the tags around. With this option turned on, after
  2849. each such operation the tags are again aligned to `org-tags-column'."
  2850. :group 'org-tags
  2851. :type 'boolean)
  2852. (defcustom org-use-tag-inheritance t
  2853. "Non-nil means tags in levels apply also for sublevels.
  2854. When nil, only the tags directly given in a specific line apply there.
  2855. This may also be a list of tags that should be inherited, or a regexp that
  2856. matches tags that should be inherited. Additional control is possible
  2857. with the variable `org-tags-exclude-from-inheritance' which gives an
  2858. explicit list of tags to be excluded from inheritance, even if the value of
  2859. `org-use-tag-inheritance' would select it for inheritance.
  2860. If this option is t, a match early-on in a tree can lead to a large
  2861. number of matches in the subtree when constructing the agenda or creating
  2862. a sparse tree. If you only want to see the first match in a tree during
  2863. a search, check out the variable `org-tags-match-list-sublevels'."
  2864. :group 'org-tags
  2865. :type '(choice
  2866. (const :tag "Not" nil)
  2867. (const :tag "Always" t)
  2868. (repeat :tag "Specific tags" (string :tag "Tag"))
  2869. (regexp :tag "Tags matched by regexp")))
  2870. (defcustom org-tags-exclude-from-inheritance nil
  2871. "List of tags that should never be inherited.
  2872. This is a way to exclude a few tags from inheritance. For way to do
  2873. the opposite, to actively allow inheritance for selected tags,
  2874. see the variable `org-use-tag-inheritance'."
  2875. :group 'org-tags
  2876. :type '(repeat (string :tag "Tag")))
  2877. (defun org-tag-inherit-p (tag)
  2878. "Check if TAG is one that should be inherited."
  2879. (cond
  2880. ((member tag org-tags-exclude-from-inheritance) nil)
  2881. ((eq org-use-tag-inheritance t) t)
  2882. ((not org-use-tag-inheritance) nil)
  2883. ((stringp org-use-tag-inheritance)
  2884. (string-match org-use-tag-inheritance tag))
  2885. ((listp org-use-tag-inheritance)
  2886. (member tag org-use-tag-inheritance))
  2887. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2888. (defcustom org-tags-match-list-sublevels t
  2889. "Non-nil means list also sublevels of headlines matching a search.
  2890. This variable applies to tags/property searches, and also to stuck
  2891. projects because this search is based on a tags match as well.
  2892. When set to the symbol `indented', sublevels are indented with
  2893. leading dots.
  2894. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2895. the sublevels of a headline matching a tag search often also match
  2896. the same search. Listing all of them can create very long lists.
  2897. Setting this variable to nil causes subtrees of a match to be skipped.
  2898. This variable is semi-obsolete and probably should always be true. It
  2899. is better to limit inheritance to certain tags using the variables
  2900. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2901. :group 'org-tags
  2902. :type '(choice
  2903. (const :tag "No, don't list them" nil)
  2904. (const :tag "Yes, do list them" t)
  2905. (const :tag "List them, indented with leading dots" indented)))
  2906. (defcustom org-tags-sort-function nil
  2907. "When set, tags are sorted using this function as a comparator."
  2908. :group 'org-tags
  2909. :type '(choice
  2910. (const :tag "No sorting" nil)
  2911. (const :tag "Alphabetical" string<)
  2912. (const :tag "Reverse alphabetical" string>)
  2913. (function :tag "Custom function" nil)))
  2914. (defvar org-tags-history nil
  2915. "History of minibuffer reads for tags.")
  2916. (defvar org-last-tags-completion-table nil
  2917. "The last used completion table for tags.")
  2918. (defvar org-after-tags-change-hook nil
  2919. "Hook that is run after the tags in a line have changed.")
  2920. (defgroup org-properties nil
  2921. "Options concerning properties in Org-mode."
  2922. :tag "Org Properties"
  2923. :group 'org)
  2924. (defcustom org-property-format "%-10s %s"
  2925. "How property key/value pairs should be formatted by `indent-line'.
  2926. When `indent-line' hits a property definition, it will format the line
  2927. according to this format, mainly to make sure that the values are
  2928. lined-up with respect to each other."
  2929. :group 'org-properties
  2930. :type 'string)
  2931. (defcustom org-properties-postprocess-alist nil
  2932. "Alist of properties and functions to adjust inserted values.
  2933. Elements of this alist must be of the form
  2934. ([string] [function])
  2935. where [string] must be a property name and [function] must be a
  2936. lambda expression: this lambda expression must take one argument,
  2937. the value to adjust, and return the new value as a string.
  2938. For example, this element will allow the property \"Remaining\"
  2939. to be updated wrt the relation between the \"Effort\" property
  2940. and the clock summary:
  2941. ((\"Remaining\" (lambda(value)
  2942. (let ((clocksum (org-clock-sum-current-item))
  2943. (effort (org-duration-string-to-minutes
  2944. (org-entry-get (point) \"Effort\"))))
  2945. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2946. :group 'org-properties
  2947. :version "24.1"
  2948. :type '(alist :key-type (string :tag "Property")
  2949. :value-type (function :tag "Function")))
  2950. (defcustom org-use-property-inheritance nil
  2951. "Non-nil means properties apply also for sublevels.
  2952. This setting is chiefly used during property searches. Turning it on can
  2953. cause significant overhead when doing a search, which is why it is not
  2954. on by default.
  2955. When nil, only the properties directly given in the current entry count.
  2956. When t, every property is inherited. The value may also be a list of
  2957. properties that should have inheritance, or a regular expression matching
  2958. properties that should be inherited.
  2959. However, note that some special properties use inheritance under special
  2960. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2961. and the properties ending in \"_ALL\" when they are used as descriptor
  2962. for valid values of a property.
  2963. Note for programmers:
  2964. When querying an entry with `org-entry-get', you can control if inheritance
  2965. should be used. By default, `org-entry-get' looks only at the local
  2966. properties. You can request inheritance by setting the inherit argument
  2967. to t (to force inheritance) or to `selective' (to respect the setting
  2968. in this variable)."
  2969. :group 'org-properties
  2970. :type '(choice
  2971. (const :tag "Not" nil)
  2972. (const :tag "Always" t)
  2973. (repeat :tag "Specific properties" (string :tag "Property"))
  2974. (regexp :tag "Properties matched by regexp")))
  2975. (defun org-property-inherit-p (property)
  2976. "Check if PROPERTY is one that should be inherited."
  2977. (cond
  2978. ((eq org-use-property-inheritance t) t)
  2979. ((not org-use-property-inheritance) nil)
  2980. ((stringp org-use-property-inheritance)
  2981. (string-match org-use-property-inheritance property))
  2982. ((listp org-use-property-inheritance)
  2983. (member property org-use-property-inheritance))
  2984. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2985. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2986. "The default column format, if no other format has been defined.
  2987. This variable can be set on the per-file basis by inserting a line
  2988. #+COLUMNS: %25ITEM ....."
  2989. :group 'org-properties
  2990. :type 'string)
  2991. (defcustom org-columns-ellipses ".."
  2992. "The ellipses to be used when a field in column view is truncated.
  2993. When this is the empty string, as many characters as possible are shown,
  2994. but then there will be no visual indication that the field has been truncated.
  2995. When this is a string of length N, the last N characters of a truncated
  2996. field are replaced by this string. If the column is narrower than the
  2997. ellipses string, only part of the ellipses string will be shown."
  2998. :group 'org-properties
  2999. :type 'string)
  3000. (defcustom org-columns-modify-value-for-display-function nil
  3001. "Function that modifies values for display in column view.
  3002. For example, it can be used to cut out a certain part from a time stamp.
  3003. The function must take 2 arguments:
  3004. column-title The title of the column (*not* the property name)
  3005. value The value that should be modified.
  3006. The function should return the value that should be displayed,
  3007. or nil if the normal value should be used."
  3008. :group 'org-properties
  3009. :type 'function)
  3010. (defcustom org-effort-property "Effort"
  3011. "The property that is being used to keep track of effort estimates.
  3012. Effort estimates given in this property need to have the format H:MM."
  3013. :group 'org-properties
  3014. :group 'org-progress
  3015. :type '(string :tag "Property"))
  3016. (defconst org-global-properties-fixed
  3017. '(("VISIBILITY_ALL" . "folded children content all")
  3018. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3019. "List of property/value pairs that can be inherited by any entry.
  3020. These are fixed values, for the preset properties. The user variable
  3021. that can be used to add to this list is `org-global-properties'.
  3022. The entries in this list are cons cells where the car is a property
  3023. name and cdr is a string with the value. If the value represents
  3024. multiple items like an \"_ALL\" property, separate the items by
  3025. spaces.")
  3026. (defcustom org-global-properties nil
  3027. "List of property/value pairs that can be inherited by any entry.
  3028. This list will be combined with the constant `org-global-properties-fixed'.
  3029. The entries in this list are cons cells where the car is a property
  3030. name and cdr is a string with the value.
  3031. You can set buffer-local values for the same purpose in the variable
  3032. `org-file-properties' this by adding lines like
  3033. #+PROPERTY: NAME VALUE"
  3034. :group 'org-properties
  3035. :type '(repeat
  3036. (cons (string :tag "Property")
  3037. (string :tag "Value"))))
  3038. (defvar org-file-properties nil
  3039. "List of property/value pairs that can be inherited by any entry.
  3040. Valid for the current buffer.
  3041. This variable is populated from #+PROPERTY lines.")
  3042. (make-variable-buffer-local 'org-file-properties)
  3043. (defgroup org-agenda nil
  3044. "Options concerning agenda views in Org-mode."
  3045. :tag "Org Agenda"
  3046. :group 'org)
  3047. (defvar org-category nil
  3048. "Variable used by org files to set a category for agenda display.
  3049. Such files should use a file variable to set it, for example
  3050. # -*- mode: org; org-category: \"ELisp\"
  3051. or contain a special line
  3052. #+CATEGORY: ELisp
  3053. If the file does not specify a category, then file's base name
  3054. is used instead.")
  3055. (make-variable-buffer-local 'org-category)
  3056. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3057. (defcustom org-agenda-files nil
  3058. "The files to be used for agenda display.
  3059. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3060. \\[org-remove-file]. You can also use customize to edit the list.
  3061. If an entry is a directory, all files in that directory that are matched by
  3062. `org-agenda-file-regexp' will be part of the file list.
  3063. If the value of the variable is not a list but a single file name, then
  3064. the list of agenda files is actually stored and maintained in that file, one
  3065. agenda file per line. In this file paths can be given relative to
  3066. `org-directory'. Tilde expansion and environment variable substitution
  3067. are also made."
  3068. :group 'org-agenda
  3069. :type '(choice
  3070. (repeat :tag "List of files and directories" file)
  3071. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3072. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3073. "Regular expression to match files for `org-agenda-files'.
  3074. If any element in the list in that variable contains a directory instead
  3075. of a normal file, all files in that directory that are matched by this
  3076. regular expression will be included."
  3077. :group 'org-agenda
  3078. :type 'regexp)
  3079. (defcustom org-agenda-text-search-extra-files nil
  3080. "List of extra files to be searched by text search commands.
  3081. These files will be search in addition to the agenda files by the
  3082. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3083. Note that these files will only be searched for text search commands,
  3084. not for the other agenda views like todo lists, tag searches or the weekly
  3085. agenda. This variable is intended to list notes and possibly archive files
  3086. that should also be searched by these two commands.
  3087. In fact, if the first element in the list is the symbol `agenda-archives',
  3088. than all archive files of all agenda files will be added to the search
  3089. scope."
  3090. :group 'org-agenda
  3091. :type '(set :greedy t
  3092. (const :tag "Agenda Archives" agenda-archives)
  3093. (repeat :inline t (file))))
  3094. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3095. 'org-agenda-text-search-extra-files)
  3096. (defcustom org-agenda-skip-unavailable-files nil
  3097. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3098. A nil value means to remove them, after a query, from the list."
  3099. :group 'org-agenda
  3100. :type 'boolean)
  3101. (defcustom org-calendar-to-agenda-key [?c]
  3102. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3103. The command `org-calendar-goto-agenda' will be bound to this key. The
  3104. default is the character `c' because then `c' can be used to switch back and
  3105. forth between agenda and calendar."
  3106. :group 'org-agenda
  3107. :type 'sexp)
  3108. (defcustom org-calendar-insert-diary-entry-key [?i]
  3109. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3110. This option is irrelevant until `org-agenda-diary-file' has been configured
  3111. to point to an Org-mode file. When that is the case, the command
  3112. `org-agenda-diary-entry' will be bound to the key given here, by default
  3113. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3114. if you want to continue doing this, you need to change this to a different
  3115. key."
  3116. :group 'org-agenda
  3117. :type 'sexp)
  3118. (defcustom org-agenda-diary-file 'diary-file
  3119. "File to which to add new entries with the `i' key in agenda and calendar.
  3120. When this is the symbol `diary-file', the functionality in the Emacs
  3121. calendar will be used to add entries to the `diary-file'. But when this
  3122. points to a file, `org-agenda-diary-entry' will be used instead."
  3123. :group 'org-agenda
  3124. :type '(choice
  3125. (const :tag "The standard Emacs diary file" diary-file)
  3126. (file :tag "Special Org file diary entries")))
  3127. (eval-after-load "calendar"
  3128. '(progn
  3129. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3130. 'org-calendar-goto-agenda)
  3131. (add-hook 'calendar-mode-hook
  3132. (lambda ()
  3133. (unless (eq org-agenda-diary-file 'diary-file)
  3134. (define-key calendar-mode-map
  3135. org-calendar-insert-diary-entry-key
  3136. 'org-agenda-diary-entry))))))
  3137. (defgroup org-latex nil
  3138. "Options for embedding LaTeX code into Org-mode."
  3139. :tag "Org LaTeX"
  3140. :group 'org)
  3141. (defcustom org-format-latex-options
  3142. '(:foreground default :background default :scale 1.0
  3143. :html-foreground "Black" :html-background "Transparent"
  3144. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3145. "Options for creating images from LaTeX fragments.
  3146. This is a property list with the following properties:
  3147. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3148. `default' means use the foreground of the default face.
  3149. `auto' means use the foreground from the text face.
  3150. :background the background color, or \"Transparent\".
  3151. `default' means use the background of the default face.
  3152. `auto' means use the background from the text face.
  3153. :scale a scaling factor for the size of the images, to get more pixels
  3154. :html-foreground, :html-background, :html-scale
  3155. the same numbers for HTML export.
  3156. :matchers a list indicating which matchers should be used to
  3157. find LaTeX fragments. Valid members of this list are:
  3158. \"begin\" find environments
  3159. \"$1\" find single characters surrounded by $.$
  3160. \"$\" find math expressions surrounded by $...$
  3161. \"$$\" find math expressions surrounded by $$....$$
  3162. \"\\(\" find math expressions surrounded by \\(...\\)
  3163. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3164. :group 'org-latex
  3165. :type 'plist)
  3166. (defcustom org-format-latex-signal-error t
  3167. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3168. When nil, just push out a message."
  3169. :group 'org-latex
  3170. :version "24.1"
  3171. :type 'boolean)
  3172. (defcustom org-latex-to-mathml-jar-file nil
  3173. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3174. Use this to specify additional executable file say a jar file.
  3175. When using MathToWeb as the converter, specify the full-path to
  3176. your mathtoweb.jar file."
  3177. :group 'org-latex
  3178. :version "24.1"
  3179. :type '(choice
  3180. (const :tag "None" nil)
  3181. (file :tag "JAR file" :must-match t)))
  3182. (defcustom org-latex-to-mathml-convert-command nil
  3183. "Command to convert LaTeX fragments to MathML.
  3184. Replace format-specifiers in the command as noted below and use
  3185. `shell-command' to convert LaTeX to MathML.
  3186. %j: Executable file in fully expanded form as specified by
  3187. `org-latex-to-mathml-jar-file'.
  3188. %I: Input LaTeX file in fully expanded form
  3189. %o: Output MathML file
  3190. This command is used by `org-create-math-formula'.
  3191. When using MathToWeb as the converter, set this to
  3192. \"java -jar %j -unicode -force -df %o %I\"."
  3193. :group 'org-latex
  3194. :version "24.1"
  3195. :type '(choice
  3196. (const :tag "None" nil)
  3197. (string :tag "\nShell command")))
  3198. (defcustom org-latex-create-formula-image-program 'dvipng
  3199. "Program to convert LaTeX fragments with.
  3200. dvipng Process the LaTeX fragments to dvi file, then convert
  3201. dvi files to png files using dvipng.
  3202. This will also include processing of non-math environments.
  3203. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3204. to convert pdf files to png files"
  3205. :group 'org-latex
  3206. :version "24.1"
  3207. :type '(choice
  3208. (const :tag "dvipng" dvipng)
  3209. (const :tag "imagemagick" imagemagick)))
  3210. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3211. "Path to store latex preview images.
  3212. A relative path here creates many directories relative to the
  3213. processed org files paths. An absolute path puts all preview
  3214. images at the same place."
  3215. :group 'org-latex
  3216. :version "24.3"
  3217. :type 'string)
  3218. (defun org-format-latex-mathml-available-p ()
  3219. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3220. (save-match-data
  3221. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3222. org-latex-to-mathml-convert-command)
  3223. (let ((executable (car (split-string
  3224. org-latex-to-mathml-convert-command))))
  3225. (when (executable-find executable)
  3226. (if (string-match
  3227. "%j" org-latex-to-mathml-convert-command)
  3228. (file-readable-p org-latex-to-mathml-jar-file)
  3229. t))))))
  3230. (defcustom org-format-latex-header "\\documentclass{article}
  3231. \\usepackage[usenames]{color}
  3232. \\usepackage{amsmath}
  3233. \\usepackage[mathscr]{eucal}
  3234. \\pagestyle{empty} % do not remove
  3235. \[PACKAGES]
  3236. \[DEFAULT-PACKAGES]
  3237. % The settings below are copied from fullpage.sty
  3238. \\setlength{\\textwidth}{\\paperwidth}
  3239. \\addtolength{\\textwidth}{-3cm}
  3240. \\setlength{\\oddsidemargin}{1.5cm}
  3241. \\addtolength{\\oddsidemargin}{-2.54cm}
  3242. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3243. \\setlength{\\textheight}{\\paperheight}
  3244. \\addtolength{\\textheight}{-\\headheight}
  3245. \\addtolength{\\textheight}{-\\headsep}
  3246. \\addtolength{\\textheight}{-\\footskip}
  3247. \\addtolength{\\textheight}{-3cm}
  3248. \\setlength{\\topmargin}{1.5cm}
  3249. \\addtolength{\\topmargin}{-2.54cm}"
  3250. "The document header used for processing LaTeX fragments.
  3251. It is imperative that this header make sure that no page number
  3252. appears on the page. The package defined in the variables
  3253. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3254. will either replace the placeholder \"[PACKAGES]\" in this
  3255. header, or they will be appended."
  3256. :group 'org-latex
  3257. :type 'string)
  3258. (defun org-set-packages-alist (var val)
  3259. "Set the packages alist and make sure it has 3 elements per entry."
  3260. (set var (mapcar (lambda (x)
  3261. (if (and (consp x) (= (length x) 2))
  3262. (list (car x) (nth 1 x) t)
  3263. x))
  3264. val)))
  3265. (defun org-get-packages-alist (var)
  3266. "Get the packages alist and make sure it has 3 elements per entry."
  3267. (mapcar (lambda (x)
  3268. (if (and (consp x) (= (length x) 2))
  3269. (list (car x) (nth 1 x) t)
  3270. x))
  3271. (default-value var)))
  3272. (defcustom org-latex-default-packages-alist
  3273. '(("AUTO" "inputenc" t)
  3274. ("T1" "fontenc" t)
  3275. ("" "fixltx2e" nil)
  3276. ("" "graphicx" t)
  3277. ("" "longtable" nil)
  3278. ("" "float" nil)
  3279. ("" "wrapfig" nil)
  3280. ("" "soul" t)
  3281. ("" "textcomp" t)
  3282. ("" "marvosym" t)
  3283. ("" "wasysym" t)
  3284. ("" "latexsym" t)
  3285. ("" "amssymb" t)
  3286. ("" "hyperref" nil)
  3287. "\\tolerance=1000")
  3288. "Alist of default packages to be inserted in the header.
  3289. Change this only if one of the packages here causes an
  3290. incompatibility with another package you are using.
  3291. The packages in this list are needed by one part or another of
  3292. Org mode to function properly:
  3293. - inputenc, fontenc: for basic font and character selection
  3294. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3295. symbols used for interpreting the entities in `org-entities'.
  3296. You can skip some of these packages if you don't use any of the
  3297. symbols in it.
  3298. - graphicx: for including images
  3299. - float, wrapfig: for figure placement
  3300. - longtable: for long tables
  3301. - hyperref: for cross references
  3302. Therefore you should not modify this variable unless you know
  3303. what you are doing. The one reason to change it anyway is that
  3304. you might be loading some other package that conflicts with one
  3305. of the default packages. Each cell is of the format
  3306. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3307. the package also needs to be included when compiling LaTeX
  3308. snippets into images for inclusion into non-LaTeX output."
  3309. :group 'org-latex
  3310. :group 'org-export-latex
  3311. :set 'org-set-packages-alist
  3312. :get 'org-get-packages-alist
  3313. :version "24.1"
  3314. :type '(repeat
  3315. (choice
  3316. (list :tag "options/package pair"
  3317. (string :tag "options")
  3318. (string :tag "package")
  3319. (boolean :tag "Snippet"))
  3320. (string :tag "A line of LaTeX"))))
  3321. (defcustom org-latex-packages-alist nil
  3322. "Alist of packages to be inserted in every LaTeX header.
  3323. These will be inserted after `org-latex-default-packages-alist'.
  3324. Each cell is of the format:
  3325. \(\"options\" \"package\" snippet-flag)
  3326. SNIPPET-FLAG, when t, indicates that this package is also needed
  3327. when turning LaTeX snippets into images for inclusion into
  3328. non-LaTeX output.
  3329. Make sure that you only list packages here which:
  3330. - you want in every file
  3331. - do not conflict with the setup in `org-format-latex-header'.
  3332. - do not conflict with the default packages in
  3333. `org-latex-default-packages-alist'."
  3334. :group 'org-latex
  3335. :group 'org-export-latex
  3336. :set 'org-set-packages-alist
  3337. :get 'org-get-packages-alist
  3338. :type '(repeat
  3339. (choice
  3340. (list :tag "options/package pair"
  3341. (string :tag "options")
  3342. (string :tag "package")
  3343. (boolean :tag "Snippet"))
  3344. (string :tag "A line of LaTeX"))))
  3345. (defgroup org-appearance nil
  3346. "Settings for Org-mode appearance."
  3347. :tag "Org Appearance"
  3348. :group 'org)
  3349. (defcustom org-level-color-stars-only nil
  3350. "Non-nil means fontify only the stars in each headline.
  3351. When nil, the entire headline is fontified.
  3352. Changing it requires restart of `font-lock-mode' to become effective
  3353. also in regions already fontified."
  3354. :group 'org-appearance
  3355. :type 'boolean)
  3356. (defcustom org-hide-leading-stars nil
  3357. "Non-nil means hide the first N-1 stars in a headline.
  3358. This works by using the face `org-hide' for these stars. This
  3359. face is white for a light background, and black for a dark
  3360. background. You may have to customize the face `org-hide' to
  3361. make this work.
  3362. Changing it requires restart of `font-lock-mode' to become effective
  3363. also in regions already fontified.
  3364. You may also set this on a per-file basis by adding one of the following
  3365. lines to the buffer:
  3366. #+STARTUP: hidestars
  3367. #+STARTUP: showstars"
  3368. :group 'org-appearance
  3369. :type 'boolean)
  3370. (defcustom org-hidden-keywords nil
  3371. "List of symbols corresponding to keywords to be hidden the org buffer.
  3372. For example, a value '(title) for this list will make the document's title
  3373. appear in the buffer without the initial #+TITLE: keyword."
  3374. :group 'org-appearance
  3375. :version "24.1"
  3376. :type '(set (const :tag "#+AUTHOR" author)
  3377. (const :tag "#+DATE" date)
  3378. (const :tag "#+EMAIL" email)
  3379. (const :tag "#+TITLE" title)))
  3380. (defcustom org-custom-properties nil
  3381. "List of properties (as strings) with a special meaning.
  3382. The default use of these custom properties is to let the user
  3383. hide them with `org-toggle-custom-properties-visibility'."
  3384. :group 'org-properties
  3385. :group 'org-appearance
  3386. :version "24.3"
  3387. :type '(repeat (string :tag "Property Name")))
  3388. (defcustom org-fontify-done-headline nil
  3389. "Non-nil means change the face of a headline if it is marked DONE.
  3390. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3391. When this is non-nil, the headline after the keyword is set to the
  3392. `org-headline-done' as an additional indication."
  3393. :group 'org-appearance
  3394. :type 'boolean)
  3395. (defcustom org-fontify-emphasized-text t
  3396. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3397. Changing this variable requires a restart of Emacs to take effect."
  3398. :group 'org-appearance
  3399. :type 'boolean)
  3400. (defcustom org-fontify-whole-heading-line nil
  3401. "Non-nil means fontify the whole line for headings.
  3402. This is useful when setting a background color for the
  3403. org-level-* faces."
  3404. :group 'org-appearance
  3405. :type 'boolean)
  3406. (defcustom org-highlight-latex-and-related nil
  3407. "Non-nil means highlight LaTeX related syntax in the buffer.
  3408. When non nil, the value should be a list containing any of the
  3409. following symbols:
  3410. `latex' Highlight LaTeX snippets and environments.
  3411. `script' Highlight subscript and superscript.
  3412. `entities' Highlight entities."
  3413. :group 'org-appearance
  3414. :version "24.4"
  3415. :package-version '(Org . "8.0")
  3416. :type '(choice
  3417. (const :tag "No highlighting" nil)
  3418. (set :greedy t :tag "Highlight"
  3419. (const :tag "LaTeX snippets and environments" latex)
  3420. (const :tag "Subscript and superscript" script)
  3421. (const :tag "Entities" entities))))
  3422. (defcustom org-hide-emphasis-markers nil
  3423. "Non-nil mean font-lock should hide the emphasis marker characters."
  3424. :group 'org-appearance
  3425. :type 'boolean)
  3426. (defcustom org-pretty-entities nil
  3427. "Non-nil means show entities as UTF8 characters.
  3428. When nil, the \\name form remains in the buffer."
  3429. :group 'org-appearance
  3430. :version "24.1"
  3431. :type 'boolean)
  3432. (defcustom org-pretty-entities-include-sub-superscripts t
  3433. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3434. :group 'org-appearance
  3435. :version "24.1"
  3436. :type 'boolean)
  3437. (defvar org-emph-re nil
  3438. "Regular expression for matching emphasis.
  3439. After a match, the match groups contain these elements:
  3440. 0 The match of the full regular expression, including the characters
  3441. before and after the proper match
  3442. 1 The character before the proper match, or empty at beginning of line
  3443. 2 The proper match, including the leading and trailing markers
  3444. 3 The leading marker like * or /, indicating the type of highlighting
  3445. 4 The text between the emphasis markers, not including the markers
  3446. 5 The character after the match, empty at the end of a line")
  3447. (defvar org-verbatim-re nil
  3448. "Regular expression for matching verbatim text.")
  3449. (defvar org-emphasis-regexp-components) ; defined just below
  3450. (defvar org-emphasis-alist) ; defined just below
  3451. (defun org-set-emph-re (var val)
  3452. "Set variable and compute the emphasis regular expression."
  3453. (set var val)
  3454. (when (and (boundp 'org-emphasis-alist)
  3455. (boundp 'org-emphasis-regexp-components)
  3456. org-emphasis-alist org-emphasis-regexp-components)
  3457. (let* ((e org-emphasis-regexp-components)
  3458. (pre (car e))
  3459. (post (nth 1 e))
  3460. (border (nth 2 e))
  3461. (body (nth 3 e))
  3462. (nl (nth 4 e))
  3463. (body1 (concat body "*?"))
  3464. (markers (mapconcat 'car org-emphasis-alist ""))
  3465. (vmarkers (mapconcat
  3466. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3467. org-emphasis-alist "")))
  3468. ;; make sure special characters appear at the right position in the class
  3469. (if (string-match "\\^" markers)
  3470. (setq markers (concat (replace-match "" t t markers) "^")))
  3471. (if (string-match "-" markers)
  3472. (setq markers (concat (replace-match "" t t markers) "-")))
  3473. (if (string-match "\\^" vmarkers)
  3474. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3475. (if (string-match "-" vmarkers)
  3476. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3477. (if (> nl 0)
  3478. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3479. (int-to-string nl) "\\}")))
  3480. ;; Make the regexp
  3481. (setq org-emph-re
  3482. (concat "\\([" pre "]\\|^\\)"
  3483. "\\("
  3484. "\\([" markers "]\\)"
  3485. "\\("
  3486. "[^" border "]\\|"
  3487. "[^" border "]"
  3488. body1
  3489. "[^" border "]"
  3490. "\\)"
  3491. "\\3\\)"
  3492. "\\([" post "]\\|$\\)"))
  3493. (setq org-verbatim-re
  3494. (concat "\\([" pre "]\\|^\\)"
  3495. "\\("
  3496. "\\([" vmarkers "]\\)"
  3497. "\\("
  3498. "[^" border "]\\|"
  3499. "[^" border "]"
  3500. body1
  3501. "[^" border "]"
  3502. "\\)"
  3503. "\\3\\)"
  3504. "\\([" post "]\\|$\\)")))))
  3505. (defcustom org-emphasis-regexp-components
  3506. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3507. "Components used to build the regular expression for emphasis.
  3508. This is a list with five entries. Terminology: In an emphasis string
  3509. like \" *strong word* \", we call the initial space PREMATCH, the final
  3510. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3511. and \"trong wor\" is the body. The different components in this variable
  3512. specify what is allowed/forbidden in each part:
  3513. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3514. post Chars allowed as postmatch. End of line will be allowed too.
  3515. border The chars *forbidden* as border characters.
  3516. body-regexp A regexp like \".\" to match a body character. Don't use
  3517. non-shy groups here, and don't allow newline here.
  3518. newline The maximum number of newlines allowed in an emphasis exp.
  3519. Use customize to modify this, or restart Emacs after changing it."
  3520. :group 'org-appearance
  3521. :set 'org-set-emph-re
  3522. :type '(list
  3523. (sexp :tag "Allowed chars in pre ")
  3524. (sexp :tag "Allowed chars in post ")
  3525. (sexp :tag "Forbidden chars in border ")
  3526. (sexp :tag "Regexp for body ")
  3527. (integer :tag "number of newlines allowed")
  3528. (option (boolean :tag "Please ignore this button"))))
  3529. (defcustom org-emphasis-alist
  3530. `(("*" bold "<b>" "</b>")
  3531. ("/" italic "<i>" "</i>")
  3532. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3533. ("=" org-code "<code>" "</code>" verbatim)
  3534. ("~" org-verbatim "<code>" "</code>" verbatim)
  3535. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3536. "<del>" "</del>")
  3537. )
  3538. "Special syntax for emphasized text.
  3539. Text starting and ending with a special character will be emphasized, for
  3540. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3541. characters, the face to be used by font-lock for highlighting in Org-mode
  3542. Emacs buffers, and the HTML tags to be used for this.
  3543. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3544. Use customize to modify this, or restart Emacs after changing it."
  3545. :group 'org-appearance
  3546. :set 'org-set-emph-re
  3547. :type '(repeat
  3548. (list
  3549. (string :tag "Marker character")
  3550. (choice
  3551. (face :tag "Font-lock-face")
  3552. (plist :tag "Face property list"))
  3553. (string :tag "HTML start tag")
  3554. (string :tag "HTML end tag")
  3555. (option (const verbatim)))))
  3556. (defvar org-protecting-blocks
  3557. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3558. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3559. This is needed for font-lock setup.")
  3560. ;;; Miscellaneous options
  3561. (defgroup org-completion nil
  3562. "Completion in Org-mode."
  3563. :tag "Org Completion"
  3564. :group 'org)
  3565. (defcustom org-completion-use-ido nil
  3566. "Non-nil means use ido completion wherever possible.
  3567. Note that `ido-mode' must be active for this variable to be relevant.
  3568. If you decide to turn this variable on, you might well want to turn off
  3569. `org-outline-path-complete-in-steps'.
  3570. See also `org-completion-use-iswitchb'."
  3571. :group 'org-completion
  3572. :type 'boolean)
  3573. (defcustom org-completion-use-iswitchb nil
  3574. "Non-nil means use iswitchb completion wherever possible.
  3575. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3576. If you decide to turn this variable on, you might well want to turn off
  3577. `org-outline-path-complete-in-steps'.
  3578. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3579. :group 'org-completion
  3580. :type 'boolean)
  3581. (defcustom org-completion-fallback-command 'hippie-expand
  3582. "The expansion command called by \\[pcomplete] in normal context.
  3583. Normal means, no org-mode-specific context."
  3584. :group 'org-completion
  3585. :type 'function)
  3586. ;;; Functions and variables from their packages
  3587. ;; Declared here to avoid compiler warnings
  3588. ;; XEmacs only
  3589. (defvar outline-mode-menu-heading)
  3590. (defvar outline-mode-menu-show)
  3591. (defvar outline-mode-menu-hide)
  3592. (defvar zmacs-regions) ; XEmacs regions
  3593. ;; Emacs only
  3594. (defvar mark-active)
  3595. ;; Various packages
  3596. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3597. (declare-function calendar-forward-day "cal-move" (arg))
  3598. (declare-function calendar-goto-date "cal-move" (date))
  3599. (declare-function calendar-goto-today "cal-move" ())
  3600. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3601. (defvar calc-embedded-close-formula)
  3602. (defvar calc-embedded-open-formula)
  3603. (declare-function cdlatex-tab "ext:cdlatex" ())
  3604. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3605. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3606. (defvar font-lock-unfontify-region-function)
  3607. (declare-function iswitchb-read-buffer "iswitchb"
  3608. (prompt &optional default require-match start matches-set))
  3609. (defvar iswitchb-temp-buflist)
  3610. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3611. (defvar org-agenda-tags-todo-honor-ignore-options)
  3612. (declare-function org-agenda-skip "org-agenda" ())
  3613. (declare-function
  3614. org-agenda-format-item "org-agenda"
  3615. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3616. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3617. (declare-function org-agenda-change-all-lines "org-agenda"
  3618. (newhead hdmarker &optional fixface just-this))
  3619. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3620. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3621. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3622. (beg end))
  3623. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3624. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3625. "org-agenda" (&optional end))
  3626. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3627. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3628. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3629. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3630. (declare-function org-indent-mode "org-indent" (&optional arg))
  3631. (declare-function parse-time-string "parse-time" (string))
  3632. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3633. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3634. (defvar remember-data-file)
  3635. (defvar texmathp-why)
  3636. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3637. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3638. (defvar org-latex-regexps)
  3639. ;;; Autoload and prepare some org modules
  3640. ;; Some table stuff that needs to be defined here, because it is used
  3641. ;; by the functions setting up org-mode or checking for table context.
  3642. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3643. "Detect an org-type or table-type table.")
  3644. (defconst org-table-line-regexp "^[ \t]*|"
  3645. "Detect an org-type table line.")
  3646. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3647. "Detect an org-type table line.")
  3648. (defconst org-table-hline-regexp "^[ \t]*|-"
  3649. "Detect an org-type table hline.")
  3650. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3651. "Detect a table-type table hline.")
  3652. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3653. "Detect the first line outside a table when searching from within it.
  3654. This works for both table types.")
  3655. ;; Autoload the functions in org-table.el that are needed by functions here.
  3656. (eval-and-compile
  3657. (org-autoload "org-table"
  3658. '(org-table-begin org-table-blank-field org-table-end)))
  3659. ;;;###autoload
  3660. (defun turn-on-orgtbl ()
  3661. "Unconditionally turn on `orgtbl-mode'."
  3662. (require 'org-table)
  3663. (orgtbl-mode 1))
  3664. (defun org-at-table-p (&optional table-type)
  3665. "Return t if the cursor is inside an org-type table.
  3666. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3667. (if org-enable-table-editor
  3668. (save-excursion
  3669. (beginning-of-line 1)
  3670. (looking-at (if table-type org-table-any-line-regexp
  3671. org-table-line-regexp)))
  3672. nil))
  3673. (defsubst org-table-p () (org-at-table-p))
  3674. (defun org-at-table.el-p ()
  3675. "Return t if and only if we are at a table.el table."
  3676. (and (org-at-table-p 'any)
  3677. (save-excursion
  3678. (goto-char (org-table-begin 'any))
  3679. (looking-at org-table1-hline-regexp))))
  3680. (defun org-table-recognize-table.el ()
  3681. "If there is a table.el table nearby, recognize it and move into it."
  3682. (if org-table-tab-recognizes-table.el
  3683. (if (org-at-table.el-p)
  3684. (progn
  3685. (beginning-of-line 1)
  3686. (if (looking-at org-table-dataline-regexp)
  3687. nil
  3688. (if (looking-at org-table1-hline-regexp)
  3689. (progn
  3690. (beginning-of-line 2)
  3691. (if (looking-at org-table-any-border-regexp)
  3692. (beginning-of-line -1)))))
  3693. (if (re-search-forward "|" (org-table-end t) t)
  3694. (progn
  3695. (require 'table)
  3696. (if (table--at-cell-p (point))
  3697. t
  3698. (message "recognizing table.el table...")
  3699. (table-recognize-table)
  3700. (message "recognizing table.el table...done")))
  3701. (error "This should not happen"))
  3702. t)
  3703. nil)
  3704. nil))
  3705. (defun org-at-table-hline-p ()
  3706. "Return t if the cursor is inside a hline in a table."
  3707. (if org-enable-table-editor
  3708. (save-excursion
  3709. (beginning-of-line 1)
  3710. (looking-at org-table-hline-regexp))
  3711. nil))
  3712. (defvar org-table-clean-did-remove-column nil)
  3713. (defun org-table-map-tables (function &optional quietly)
  3714. "Apply FUNCTION to the start of all tables in the buffer."
  3715. (save-excursion
  3716. (save-restriction
  3717. (widen)
  3718. (goto-char (point-min))
  3719. (while (re-search-forward org-table-any-line-regexp nil t)
  3720. (unless quietly
  3721. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3722. (beginning-of-line 1)
  3723. (when (and (looking-at org-table-line-regexp)
  3724. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3725. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3726. (save-excursion (funcall function))
  3727. (or (looking-at org-table-line-regexp)
  3728. (forward-char 1)))
  3729. (re-search-forward org-table-any-border-regexp nil 1))))
  3730. (unless quietly (message "Mapping tables: done")))
  3731. ;; Declare and autoload functions from ox.el and al.
  3732. (declare-function org-export-get-environment "ox"
  3733. (&optional backend subtreep ext-plist))
  3734. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3735. ;; Declare and autoload functions from org-agenda.el
  3736. (eval-and-compile
  3737. (org-autoload "org-agenda"
  3738. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3739. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3740. (declare-function org-clock-update-mode-line "org-clock" ())
  3741. (declare-function org-resolve-clocks "org-clock"
  3742. (&optional also-non-dangling-p prompt last-valid))
  3743. (defvar org-clock-start-time)
  3744. (defvar org-clock-marker (make-marker)
  3745. "Marker recording the last clock-in.")
  3746. (defvar org-clock-hd-marker (make-marker)
  3747. "Marker recording the last clock-in, but the headline position.")
  3748. (defvar org-clock-heading ""
  3749. "The heading of the current clock entry.")
  3750. (defun org-clock-is-active ()
  3751. "Return non-nil if clock is currently running.
  3752. The return value is actually the clock marker."
  3753. (marker-buffer org-clock-marker))
  3754. (eval-and-compile
  3755. (org-autoload "org-clock" '(org-clock-remove-overlays
  3756. org-clock-update-time-maybe
  3757. org-clocktable-shift)))
  3758. (defun org-check-running-clock ()
  3759. "Check if the current buffer contains the running clock.
  3760. If yes, offer to stop it and to save the buffer with the changes."
  3761. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3762. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3763. (buffer-name))))
  3764. (org-clock-out)
  3765. (when (y-or-n-p "Save changed buffer?")
  3766. (save-buffer))))
  3767. (defun org-clocktable-try-shift (dir n)
  3768. "Check if this line starts a clock table, if yes, shift the time block."
  3769. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3770. (org-clocktable-shift dir n)))
  3771. ;;;###autoload
  3772. (defun org-clock-persistence-insinuate ()
  3773. "Set up hooks for clock persistence."
  3774. (require 'org-clock)
  3775. (add-hook 'org-mode-hook 'org-clock-load)
  3776. (add-hook 'kill-emacs-hook 'org-clock-save))
  3777. ;; Define the variable already here, to make sure we have it.
  3778. (defvar org-indent-mode nil
  3779. "Non-nil if Org-Indent mode is enabled.
  3780. Use the command `org-indent-mode' to change this variable.")
  3781. ;; Autoload archiving code
  3782. ;; The stuff that is needed for cycling and tags has to be defined here.
  3783. (defgroup org-archive nil
  3784. "Options concerning archiving in Org-mode."
  3785. :tag "Org Archive"
  3786. :group 'org-structure)
  3787. (defcustom org-archive-location "%s_archive::"
  3788. "The location where subtrees should be archived.
  3789. The value of this variable is a string, consisting of two parts,
  3790. separated by a double-colon. The first part is a filename and
  3791. the second part is a headline.
  3792. When the filename is omitted, archiving happens in the same file.
  3793. %s in the filename will be replaced by the current file
  3794. name (without the directory part). Archiving to a different file
  3795. is useful to keep archived entries from contributing to the
  3796. Org-mode Agenda.
  3797. The archived entries will be filed as subtrees of the specified
  3798. headline. When the headline is omitted, the subtrees are simply
  3799. filed away at the end of the file, as top-level entries. Also in
  3800. the heading you can use %s to represent the file name, this can be
  3801. useful when using the same archive for a number of different files.
  3802. Here are a few examples:
  3803. \"%s_archive::\"
  3804. If the current file is Projects.org, archive in file
  3805. Projects.org_archive, as top-level trees. This is the default.
  3806. \"::* Archived Tasks\"
  3807. Archive in the current file, under the top-level headline
  3808. \"* Archived Tasks\".
  3809. \"~/org/archive.org::\"
  3810. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3811. \"~/org/archive.org::* From %s\"
  3812. Archive in file ~/org/archive.org (absolute path), under headlines
  3813. \"From FILENAME\" where file name is the current file name.
  3814. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3815. The \"datetree/\" string is special, signifying to archive
  3816. items to the datetree. Items are placed in either the CLOSED
  3817. date of the item, or the current date if there is no CLOSED date.
  3818. The heading will be a subentry to the current date. There doesn't
  3819. need to be a heading, but there always needs to be a slash after
  3820. datetree. For example, to store archived items directly in the
  3821. datetree, use \"~/org/datetree.org::datetree/\".
  3822. \"basement::** Finished Tasks\"
  3823. Archive in file ./basement (relative path), as level 3 trees
  3824. below the level 2 heading \"** Finished Tasks\".
  3825. You may set this option on a per-file basis by adding to the buffer a
  3826. line like
  3827. #+ARCHIVE: basement::** Finished Tasks
  3828. You may also define it locally for a subtree by setting an ARCHIVE property
  3829. in the entry. If such a property is found in an entry, or anywhere up
  3830. the hierarchy, it will be used."
  3831. :group 'org-archive
  3832. :type 'string)
  3833. (defcustom org-archive-tag "ARCHIVE"
  3834. "The tag that marks a subtree as archived.
  3835. An archived subtree does not open during visibility cycling, and does
  3836. not contribute to the agenda listings.
  3837. After changing this, font-lock must be restarted in the relevant buffers to
  3838. get the proper fontification."
  3839. :group 'org-archive
  3840. :group 'org-keywords
  3841. :type 'string)
  3842. (defcustom org-agenda-skip-archived-trees t
  3843. "Non-nil means the agenda will skip any items located in archived trees.
  3844. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3845. variable is no longer recommended, you should leave it at the value t.
  3846. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3847. :group 'org-archive
  3848. :group 'org-agenda-skip
  3849. :type 'boolean)
  3850. (defcustom org-columns-skip-archived-trees t
  3851. "Non-nil means ignore archived trees when creating column view."
  3852. :group 'org-archive
  3853. :group 'org-properties
  3854. :type 'boolean)
  3855. (defcustom org-cycle-open-archived-trees nil
  3856. "Non-nil means `org-cycle' will open archived trees.
  3857. An archived tree is a tree marked with the tag ARCHIVE.
  3858. When nil, archived trees will stay folded. You can still open them with
  3859. normal outline commands like `show-all', but not with the cycling commands."
  3860. :group 'org-archive
  3861. :group 'org-cycle
  3862. :type 'boolean)
  3863. (defcustom org-sparse-tree-open-archived-trees nil
  3864. "Non-nil means sparse tree construction shows matches in archived trees.
  3865. When nil, matches in these trees are highlighted, but the trees are kept in
  3866. collapsed state."
  3867. :group 'org-archive
  3868. :group 'org-sparse-trees
  3869. :type 'boolean)
  3870. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3871. "The default date type when building a sparse tree.
  3872. When this is nil, a date is a scheduled or a deadline timestamp.
  3873. Otherwise, these types are allowed:
  3874. all: all timestamps
  3875. active: only active timestamps (<...>)
  3876. inactive: only inactive timestamps (<...)
  3877. scheduled: only scheduled timestamps
  3878. deadline: only deadline timestamps"
  3879. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3880. (const :tag "All timestamps" all)
  3881. (const :tag "Only active timestamps" active)
  3882. (const :tag "Only inactive timestamps" inactive)
  3883. (const :tag "Only scheduled timestamps" scheduled)
  3884. (const :tag "Only deadline timestamps" deadline))
  3885. :version "24.3"
  3886. :group 'org-sparse-trees)
  3887. (defun org-cycle-hide-archived-subtrees (state)
  3888. "Re-hide all archived subtrees after a visibility state change."
  3889. (when (and (not org-cycle-open-archived-trees)
  3890. (not (memq state '(overview folded))))
  3891. (save-excursion
  3892. (let* ((globalp (memq state '(contents all)))
  3893. (beg (if globalp (point-min) (point)))
  3894. (end (if globalp (point-max) (org-end-of-subtree t))))
  3895. (org-hide-archived-subtrees beg end)
  3896. (goto-char beg)
  3897. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3898. (message "%s" (substitute-command-keys
  3899. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3900. (defun org-force-cycle-archived ()
  3901. "Cycle subtree even if it is archived."
  3902. (interactive)
  3903. (setq this-command 'org-cycle)
  3904. (let ((org-cycle-open-archived-trees t))
  3905. (call-interactively 'org-cycle)))
  3906. (defun org-hide-archived-subtrees (beg end)
  3907. "Re-hide all archived subtrees after a visibility state change."
  3908. (save-excursion
  3909. (let* ((re (concat ":" org-archive-tag ":")))
  3910. (goto-char beg)
  3911. (while (re-search-forward re end t)
  3912. (when (org-at-heading-p)
  3913. (org-flag-subtree t)
  3914. (org-end-of-subtree t))))))
  3915. (declare-function outline-end-of-heading "outline" ())
  3916. (declare-function outline-flag-region "outline" (from to flag))
  3917. (defun org-flag-subtree (flag)
  3918. (save-excursion
  3919. (org-back-to-heading t)
  3920. (outline-end-of-heading)
  3921. (outline-flag-region (point)
  3922. (progn (org-end-of-subtree t) (point))
  3923. flag)))
  3924. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3925. (eval-and-compile
  3926. (org-autoload "org-archive"
  3927. '(org-add-archive-files)))
  3928. ;; Autoload Column View Code
  3929. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3930. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3931. (declare-function org-columns-compute "org-colview" (property))
  3932. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3933. '(org-columns-number-to-string
  3934. org-columns-get-format-and-top-level
  3935. org-columns-compute
  3936. org-columns-remove-overlays))
  3937. ;; Autoload ID code
  3938. (declare-function org-id-store-link "org-id")
  3939. (declare-function org-id-locations-load "org-id")
  3940. (declare-function org-id-locations-save "org-id")
  3941. (defvar org-id-track-globally)
  3942. (org-autoload "org-id"
  3943. '(org-id-new
  3944. org-id-copy
  3945. org-id-get-with-outline-path-completion
  3946. org-id-get-with-outline-drilling))
  3947. ;;; Variables for pre-computed regular expressions, all buffer local
  3948. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3949. "Matches first line of a hidden block.")
  3950. (make-variable-buffer-local 'org-drawer-regexp)
  3951. (defvar org-todo-regexp nil
  3952. "Matches any of the TODO state keywords.")
  3953. (make-variable-buffer-local 'org-todo-regexp)
  3954. (defvar org-not-done-regexp nil
  3955. "Matches any of the TODO state keywords except the last one.")
  3956. (make-variable-buffer-local 'org-not-done-regexp)
  3957. (defvar org-not-done-heading-regexp nil
  3958. "Matches a TODO headline that is not done.")
  3959. (make-variable-buffer-local 'org-not-done-regexp)
  3960. (defvar org-todo-line-regexp nil
  3961. "Matches a headline and puts TODO state into group 2 if present.")
  3962. (make-variable-buffer-local 'org-todo-line-regexp)
  3963. (defvar org-complex-heading-regexp nil
  3964. "Matches a headline and puts everything into groups:
  3965. group 1: the stars
  3966. group 2: The todo keyword, maybe
  3967. group 3: Priority cookie
  3968. group 4: True headline
  3969. group 5: Tags")
  3970. (make-variable-buffer-local 'org-complex-heading-regexp)
  3971. (defvar org-complex-heading-regexp-format nil
  3972. "Printf format to make regexp to match an exact headline.
  3973. This regexp will match the headline of any node which has the
  3974. exact headline text that is put into the format, but may have any
  3975. TODO state, priority and tags.")
  3976. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3977. (defvar org-todo-line-tags-regexp nil
  3978. "Matches a headline and puts TODO state into group 2 if present.
  3979. Also put tags into group 4 if tags are present.")
  3980. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3981. (defvar org-ds-keyword-length 12
  3982. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3983. (make-variable-buffer-local 'org-ds-keyword-length)
  3984. (defvar org-deadline-regexp nil
  3985. "Matches the DEADLINE keyword.")
  3986. (make-variable-buffer-local 'org-deadline-regexp)
  3987. (defvar org-deadline-time-regexp nil
  3988. "Matches the DEADLINE keyword together with a time stamp.")
  3989. (make-variable-buffer-local 'org-deadline-time-regexp)
  3990. (defvar org-deadline-time-hour-regexp nil
  3991. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  3992. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  3993. (defvar org-deadline-line-regexp nil
  3994. "Matches the DEADLINE keyword and the rest of the line.")
  3995. (make-variable-buffer-local 'org-deadline-line-regexp)
  3996. (defvar org-scheduled-regexp nil
  3997. "Matches the SCHEDULED keyword.")
  3998. (make-variable-buffer-local 'org-scheduled-regexp)
  3999. (defvar org-scheduled-time-regexp nil
  4000. "Matches the SCHEDULED keyword together with a time stamp.")
  4001. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4002. (defvar org-scheduled-time-hour-regexp nil
  4003. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4004. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4005. (defvar org-closed-time-regexp nil
  4006. "Matches the CLOSED keyword together with a time stamp.")
  4007. (make-variable-buffer-local 'org-closed-time-regexp)
  4008. (defvar org-keyword-time-regexp nil
  4009. "Matches any of the 4 keywords, together with the time stamp.")
  4010. (make-variable-buffer-local 'org-keyword-time-regexp)
  4011. (defvar org-keyword-time-not-clock-regexp nil
  4012. "Matches any of the 3 keywords, together with the time stamp.")
  4013. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4014. (defvar org-maybe-keyword-time-regexp nil
  4015. "Matches a timestamp, possibly preceded by a keyword.")
  4016. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4017. (defvar org-all-time-keywords nil
  4018. "List of time keywords.")
  4019. (make-variable-buffer-local 'org-all-time-keywords)
  4020. (defconst org-plain-time-of-day-regexp
  4021. (concat
  4022. "\\(\\<[012]?[0-9]"
  4023. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4024. "\\(--?"
  4025. "\\(\\<[012]?[0-9]"
  4026. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4027. "\\)?")
  4028. "Regular expression to match a plain time or time range.
  4029. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4030. groups carry important information:
  4031. 0 the full match
  4032. 1 the first time, range or not
  4033. 8 the second time, if it is a range.")
  4034. (defconst org-plain-time-extension-regexp
  4035. (concat
  4036. "\\(\\<[012]?[0-9]"
  4037. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4038. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4039. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4040. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4041. groups carry important information:
  4042. 0 the full match
  4043. 7 hours of duration
  4044. 9 minutes of duration")
  4045. (defconst org-stamp-time-of-day-regexp
  4046. (concat
  4047. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4048. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4049. "\\(--?"
  4050. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4051. "Regular expression to match a timestamp time or time range.
  4052. After a match, the following groups carry important information:
  4053. 0 the full match
  4054. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4055. 2 the first time, range or not
  4056. 4 the second time, if it is a range.")
  4057. (defconst org-startup-options
  4058. '(("fold" org-startup-folded t)
  4059. ("overview" org-startup-folded t)
  4060. ("nofold" org-startup-folded nil)
  4061. ("showall" org-startup-folded nil)
  4062. ("showeverything" org-startup-folded showeverything)
  4063. ("content" org-startup-folded content)
  4064. ("indent" org-startup-indented t)
  4065. ("noindent" org-startup-indented nil)
  4066. ("hidestars" org-hide-leading-stars t)
  4067. ("showstars" org-hide-leading-stars nil)
  4068. ("odd" org-odd-levels-only t)
  4069. ("oddeven" org-odd-levels-only nil)
  4070. ("align" org-startup-align-all-tables t)
  4071. ("noalign" org-startup-align-all-tables nil)
  4072. ("inlineimages" org-startup-with-inline-images t)
  4073. ("noinlineimages" org-startup-with-inline-images nil)
  4074. ("latexpreview" org-startup-with-latex-preview t)
  4075. ("nolatexpreview" org-startup-with-latex-preview nil)
  4076. ("customtime" org-display-custom-times t)
  4077. ("logdone" org-log-done time)
  4078. ("lognotedone" org-log-done note)
  4079. ("nologdone" org-log-done nil)
  4080. ("lognoteclock-out" org-log-note-clock-out t)
  4081. ("nolognoteclock-out" org-log-note-clock-out nil)
  4082. ("logrepeat" org-log-repeat state)
  4083. ("lognoterepeat" org-log-repeat note)
  4084. ("logdrawer" org-log-into-drawer t)
  4085. ("nologdrawer" org-log-into-drawer nil)
  4086. ("logstatesreversed" org-log-states-order-reversed t)
  4087. ("nologstatesreversed" org-log-states-order-reversed nil)
  4088. ("nologrepeat" org-log-repeat nil)
  4089. ("logreschedule" org-log-reschedule time)
  4090. ("lognotereschedule" org-log-reschedule note)
  4091. ("nologreschedule" org-log-reschedule nil)
  4092. ("logredeadline" org-log-redeadline time)
  4093. ("lognoteredeadline" org-log-redeadline note)
  4094. ("nologredeadline" org-log-redeadline nil)
  4095. ("logrefile" org-log-refile time)
  4096. ("lognoterefile" org-log-refile note)
  4097. ("nologrefile" org-log-refile nil)
  4098. ("fninline" org-footnote-define-inline t)
  4099. ("nofninline" org-footnote-define-inline nil)
  4100. ("fnlocal" org-footnote-section nil)
  4101. ("fnauto" org-footnote-auto-label t)
  4102. ("fnprompt" org-footnote-auto-label nil)
  4103. ("fnconfirm" org-footnote-auto-label confirm)
  4104. ("fnplain" org-footnote-auto-label plain)
  4105. ("fnadjust" org-footnote-auto-adjust t)
  4106. ("nofnadjust" org-footnote-auto-adjust nil)
  4107. ("constcgs" constants-unit-system cgs)
  4108. ("constSI" constants-unit-system SI)
  4109. ("noptag" org-tag-persistent-alist nil)
  4110. ("hideblocks" org-hide-block-startup t)
  4111. ("nohideblocks" org-hide-block-startup nil)
  4112. ("beamer" org-startup-with-beamer-mode t)
  4113. ("entitiespretty" org-pretty-entities t)
  4114. ("entitiesplain" org-pretty-entities nil))
  4115. "Variable associated with STARTUP options for org-mode.
  4116. Each element is a list of three items: the startup options (as written
  4117. in the #+STARTUP line), the corresponding variable, and the value to set
  4118. this variable to if the option is found. An optional forth element PUSH
  4119. means to push this value onto the list in the variable.")
  4120. (defun org-update-property-plist (key val props)
  4121. "Update PROPS with KEY and VAL."
  4122. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4123. (key (if appending (substring key 0 (- (length key) 1)) key))
  4124. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4125. (previous (cdr (assoc key props))))
  4126. (if appending
  4127. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4128. (cons (cons key val) remainder))))
  4129. (defconst org-block-regexp
  4130. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4131. "Regular expression for hiding blocks.")
  4132. (defconst org-heading-keyword-regexp-format
  4133. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4134. "Printf format for a regexp matching a headline with some keyword.
  4135. This regexp will match the headline of any node which has the
  4136. exact keyword that is put into the format. The keyword isn't in
  4137. any group by default, but the stars and the body are.")
  4138. (defconst org-heading-keyword-maybe-regexp-format
  4139. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4140. "Printf format for a regexp matching a headline, possibly with some keyword.
  4141. This regexp can match any headline with the specified keyword, or
  4142. without a keyword. The keyword isn't in any group by default,
  4143. but the stars and the body are.")
  4144. (defun org-set-regexps-and-options ()
  4145. "Precompute regular expressions for current buffer."
  4146. (when (derived-mode-p 'org-mode)
  4147. (org-set-local 'org-todo-kwd-alist nil)
  4148. (org-set-local 'org-todo-key-alist nil)
  4149. (org-set-local 'org-todo-key-trigger nil)
  4150. (org-set-local 'org-todo-keywords-1 nil)
  4151. (org-set-local 'org-done-keywords nil)
  4152. (org-set-local 'org-todo-heads nil)
  4153. (org-set-local 'org-todo-sets nil)
  4154. (org-set-local 'org-todo-log-states nil)
  4155. (org-set-local 'org-file-properties nil)
  4156. (org-set-local 'org-file-tags nil)
  4157. (let ((re (org-make-options-regexp
  4158. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
  4159. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4160. "SETUPFILE" "OPTIONS")
  4161. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4162. (splitre "[ \t]+")
  4163. (scripts org-use-sub-superscripts)
  4164. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4165. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4166. (start 0))
  4167. (save-excursion
  4168. (save-restriction
  4169. (widen)
  4170. (goto-char (point-min))
  4171. (while (or (and ext-setup-or-nil
  4172. (string-match re ext-setup-or-nil start)
  4173. (setq start (match-end 0)))
  4174. (and (setq ext-setup-or-nil nil start 0)
  4175. (re-search-forward re nil t)))
  4176. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4177. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4178. (if (stringp value) (setq value (org-trim value)))
  4179. (cond
  4180. ((equal key "CATEGORY")
  4181. (setq cat value))
  4182. ((member key '("SEQ_TODO" "TODO"))
  4183. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4184. ((equal key "TYP_TODO")
  4185. (push (cons 'type (org-split-string value splitre)) kwds))
  4186. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4187. ;; general TODO-like setup
  4188. (push (cons (intern (downcase (match-string 1 key)))
  4189. (org-split-string value splitre)) kwds))
  4190. ((equal key "TAGS")
  4191. (setq tags (append tags (if tags '("\\n") nil)
  4192. (org-split-string value splitre))))
  4193. ((equal key "COLUMNS")
  4194. (org-set-local 'org-columns-default-format value))
  4195. ((equal key "LINK")
  4196. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4197. (push (cons (match-string 1 value)
  4198. (org-trim (match-string 2 value)))
  4199. links)))
  4200. ((equal key "PRIORITIES")
  4201. (setq prio (org-split-string value " +")))
  4202. ((equal key "PROPERTY")
  4203. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4204. (setq props (org-update-property-plist (match-string 1 value)
  4205. (match-string 2 value)
  4206. props))))
  4207. ((equal key "FILETAGS")
  4208. (when (string-match "\\S-" value)
  4209. (setq ftags
  4210. (append
  4211. ftags
  4212. (apply 'append
  4213. (mapcar (lambda (x) (org-split-string x ":"))
  4214. (org-split-string value)))))))
  4215. ((equal key "DRAWERS")
  4216. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4217. ((equal key "CONSTANTS")
  4218. (setq const (append const (org-split-string value splitre))))
  4219. ((equal key "STARTUP")
  4220. (let ((opts (org-split-string value splitre))
  4221. l var val)
  4222. (while (setq l (pop opts))
  4223. (when (setq l (assoc l org-startup-options))
  4224. (setq var (nth 1 l) val (nth 2 l))
  4225. (if (not (nth 3 l))
  4226. (set (make-local-variable var) val)
  4227. (if (not (listp (symbol-value var)))
  4228. (set (make-local-variable var) nil))
  4229. (set (make-local-variable var) (symbol-value var))
  4230. (add-to-list var val))))))
  4231. ((equal key "ARCHIVE")
  4232. (setq arch value)
  4233. (remove-text-properties 0 (length arch)
  4234. '(face t fontified t) arch))
  4235. ((equal key "OPTIONS")
  4236. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4237. (setq scripts (read (match-string 2 value)))))
  4238. ((equal key "SETUPFILE")
  4239. (setq setup-contents (org-file-contents
  4240. (expand-file-name
  4241. (org-remove-double-quotes value))
  4242. 'noerror))
  4243. (if (not ext-setup-or-nil)
  4244. (setq ext-setup-or-nil setup-contents start 0)
  4245. (setq ext-setup-or-nil
  4246. (concat (substring ext-setup-or-nil 0 start)
  4247. "\n" setup-contents "\n"
  4248. (substring ext-setup-or-nil start)))))))
  4249. ;; search for property blocks
  4250. (goto-char (point-min))
  4251. (while (re-search-forward org-block-regexp nil t)
  4252. (when (equal "PROPERTY" (upcase (match-string 1)))
  4253. (setq value (replace-regexp-in-string
  4254. "[\n\r]" " " (match-string 4)))
  4255. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4256. (setq props (org-update-property-plist (match-string 1 value)
  4257. (match-string 2 value)
  4258. props)))))))
  4259. (org-set-local 'org-use-sub-superscripts scripts)
  4260. (when cat
  4261. (org-set-local 'org-category (intern cat))
  4262. (push (cons "CATEGORY" cat) props))
  4263. (when prio
  4264. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4265. (setq prio (mapcar 'string-to-char prio))
  4266. (org-set-local 'org-highest-priority (nth 0 prio))
  4267. (org-set-local 'org-lowest-priority (nth 1 prio))
  4268. (org-set-local 'org-default-priority (nth 2 prio)))
  4269. (and props (org-set-local 'org-file-properties (nreverse props)))
  4270. (and ftags (org-set-local 'org-file-tags
  4271. (mapcar 'org-add-prop-inherited ftags)))
  4272. (and drawers (org-set-local 'org-drawers drawers))
  4273. (and arch (org-set-local 'org-archive-location arch))
  4274. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4275. ;; Process the TODO keywords
  4276. (unless kwds
  4277. ;; Use the global values as if they had been given locally.
  4278. (setq kwds (default-value 'org-todo-keywords))
  4279. (if (stringp (car kwds))
  4280. (setq kwds (list (cons org-todo-interpretation
  4281. (default-value 'org-todo-keywords)))))
  4282. (setq kwds (reverse kwds)))
  4283. (setq kwds (nreverse kwds))
  4284. (let (inter kws kw)
  4285. (while (setq kws (pop kwds))
  4286. (let ((kws (or
  4287. (run-hook-with-args-until-success
  4288. 'org-todo-setup-filter-hook kws)
  4289. kws)))
  4290. (setq inter (pop kws) sep (member "|" kws)
  4291. kws0 (delete "|" (copy-sequence kws))
  4292. kwsa nil
  4293. kws1 (mapcar
  4294. (lambda (x)
  4295. ;; 1 2
  4296. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4297. (progn
  4298. (setq kw (match-string 1 x)
  4299. key (and (match-end 2) (match-string 2 x))
  4300. log (org-extract-log-state-settings x))
  4301. (push (cons kw (and key (string-to-char key))) kwsa)
  4302. (and log (push log org-todo-log-states))
  4303. kw)
  4304. (error "Invalid TODO keyword %s" x)))
  4305. kws0)
  4306. kwsa (if kwsa (append '((:startgroup))
  4307. (nreverse kwsa)
  4308. '((:endgroup))))
  4309. hw (car kws1)
  4310. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4311. tail (list inter hw (car dws) (org-last dws))))
  4312. (add-to-list 'org-todo-heads hw 'append)
  4313. (push kws1 org-todo-sets)
  4314. (setq org-done-keywords (append org-done-keywords dws nil))
  4315. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4316. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4317. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4318. (setq org-todo-sets (nreverse org-todo-sets)
  4319. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4320. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4321. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4322. ;; Process the constants
  4323. (when const
  4324. (let (e cst)
  4325. (while (setq e (pop const))
  4326. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4327. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4328. (setq org-table-formula-constants-local cst)))
  4329. ;; Process the tags.
  4330. (when tags
  4331. (let (e tgs)
  4332. (while (setq e (pop tags))
  4333. (cond
  4334. ((equal e "{") (push '(:startgroup) tgs))
  4335. ((equal e "}") (push '(:endgroup) tgs))
  4336. ((equal e "\\n") (push '(:newline) tgs))
  4337. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4338. (push (cons (match-string 1 e)
  4339. (string-to-char (match-string 2 e)))
  4340. tgs))
  4341. (t (push (list e) tgs))))
  4342. (org-set-local 'org-tag-alist nil)
  4343. (while (setq e (pop tgs))
  4344. (or (and (stringp (car e))
  4345. (assoc (car e) org-tag-alist))
  4346. (push e org-tag-alist)))))
  4347. ;; Compute the regular expressions and other local variables.
  4348. ;; Using `org-outline-regexp-bol' would complicate them much,
  4349. ;; because of the fixed white space at the end of that string.
  4350. (if (not org-done-keywords)
  4351. (setq org-done-keywords (and org-todo-keywords-1
  4352. (list (org-last org-todo-keywords-1)))))
  4353. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4354. (length org-scheduled-string)
  4355. (length org-clock-string)
  4356. (length org-closed-string)))
  4357. org-drawer-regexp
  4358. (concat "^[ \t]*:\\("
  4359. (mapconcat 'regexp-quote org-drawers "\\|")
  4360. "\\):[ \t]*$")
  4361. org-not-done-keywords
  4362. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4363. org-todo-regexp
  4364. (concat "\\("
  4365. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4366. "\\)")
  4367. org-not-done-regexp
  4368. (concat "\\("
  4369. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4370. "\\)")
  4371. org-not-done-heading-regexp
  4372. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4373. org-todo-line-regexp
  4374. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4375. org-complex-heading-regexp
  4376. (concat "^\\(\\*+\\)"
  4377. "\\(?: +" org-todo-regexp "\\)?"
  4378. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4379. "\\(?: +\\(.*?\\)\\)??"
  4380. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4381. "[ \t]*$")
  4382. org-complex-heading-regexp-format
  4383. (concat "^\\(\\*+\\)"
  4384. "\\(?: +" org-todo-regexp "\\)?"
  4385. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4386. "\\(?: +"
  4387. ;; Stats cookies can be stuck to body.
  4388. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4389. "\\(%s\\)"
  4390. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4391. "\\)"
  4392. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4393. "[ \t]*$")
  4394. org-todo-line-tags-regexp
  4395. (concat "^\\(\\*+\\)"
  4396. "\\(?: +" org-todo-regexp "\\)?"
  4397. "\\(?: +\\(.*?\\)\\)??"
  4398. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4399. "[ \t]*$")
  4400. org-deadline-regexp (concat "\\<" org-deadline-string)
  4401. org-deadline-time-regexp
  4402. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4403. org-deadline-time-hour-regexp
  4404. (concat "\\<" org-deadline-string
  4405. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4406. org-deadline-line-regexp
  4407. (concat "\\<\\(" org-deadline-string "\\).*")
  4408. org-scheduled-regexp
  4409. (concat "\\<" org-scheduled-string)
  4410. org-scheduled-time-regexp
  4411. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4412. org-scheduled-time-hour-regexp
  4413. (concat "\\<" org-scheduled-string
  4414. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4415. org-closed-time-regexp
  4416. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4417. org-keyword-time-regexp
  4418. (concat "\\<\\(" org-scheduled-string
  4419. "\\|" org-deadline-string
  4420. "\\|" org-closed-string
  4421. "\\|" org-clock-string "\\)"
  4422. " *[[<]\\([^]>]+\\)[]>]")
  4423. org-keyword-time-not-clock-regexp
  4424. (concat "\\<\\(" org-scheduled-string
  4425. "\\|" org-deadline-string
  4426. "\\|" org-closed-string
  4427. "\\)"
  4428. " *[[<]\\([^]>]+\\)[]>]")
  4429. org-maybe-keyword-time-regexp
  4430. (concat "\\(\\<\\(" org-scheduled-string
  4431. "\\|" org-deadline-string
  4432. "\\|" org-closed-string
  4433. "\\|" org-clock-string "\\)\\)?"
  4434. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4435. org-all-time-keywords
  4436. (mapcar (lambda (w) (substring w 0 -1))
  4437. (list org-scheduled-string org-deadline-string
  4438. org-clock-string org-closed-string)))
  4439. (org-compute-latex-and-related-regexp)
  4440. (org-set-font-lock-defaults))))
  4441. (defun org-file-contents (file &optional noerror)
  4442. "Return the contents of FILE, as a string."
  4443. (if (or (not file)
  4444. (not (file-readable-p file)))
  4445. (if noerror
  4446. (progn
  4447. (message "Cannot read file \"%s\"" file)
  4448. (ding) (sit-for 2)
  4449. "")
  4450. (error "Cannot read file \"%s\"" file))
  4451. (with-temp-buffer
  4452. (insert-file-contents file)
  4453. (buffer-string))))
  4454. (defun org-extract-log-state-settings (x)
  4455. "Extract the log state setting from a TODO keyword string.
  4456. This will extract info from a string like \"WAIT(w@/!)\"."
  4457. (let (kw key log1 log2)
  4458. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4459. (setq kw (match-string 1 x)
  4460. key (and (match-end 2) (match-string 2 x))
  4461. log1 (and (match-end 3) (match-string 3 x))
  4462. log2 (and (match-end 4) (match-string 4 x)))
  4463. (and (or log1 log2)
  4464. (list kw
  4465. (and log1 (if (equal log1 "!") 'time 'note))
  4466. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4467. (defun org-remove-keyword-keys (list)
  4468. "Remove a pair of parenthesis at the end of each string in LIST."
  4469. (mapcar (lambda (x)
  4470. (if (string-match "(.*)$" x)
  4471. (substring x 0 (match-beginning 0))
  4472. x))
  4473. list))
  4474. (defun org-assign-fast-keys (alist)
  4475. "Assign fast keys to a keyword-key alist.
  4476. Respect keys that are already there."
  4477. (let (new e (alt ?0))
  4478. (while (setq e (pop alist))
  4479. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4480. (cdr e)) ;; Key already assigned.
  4481. (push e new)
  4482. (let ((clist (string-to-list (downcase (car e))))
  4483. (used (append new alist)))
  4484. (when (= (car clist) ?@)
  4485. (pop clist))
  4486. (while (and clist (rassoc (car clist) used))
  4487. (pop clist))
  4488. (unless clist
  4489. (while (rassoc alt used)
  4490. (incf alt)))
  4491. (push (cons (car e) (or (car clist) alt)) new))))
  4492. (nreverse new)))
  4493. ;;; Some variables used in various places
  4494. (defvar org-window-configuration nil
  4495. "Used in various places to store a window configuration.")
  4496. (defvar org-selected-window nil
  4497. "Used in various places to store a window configuration.")
  4498. (defvar org-finish-function nil
  4499. "Function to be called when `C-c C-c' is used.
  4500. This is for getting out of special buffers like capture.")
  4501. ;; FIXME: Occasionally check by commenting these, to make sure
  4502. ;; no other functions uses these, forgetting to let-bind them.
  4503. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4504. (defvar org-last-state)
  4505. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4506. ;; Defined somewhere in this file, but used before definition.
  4507. (defvar org-entities) ;; defined in org-entities.el
  4508. (defvar org-struct-menu)
  4509. (defvar org-org-menu)
  4510. (defvar org-tbl-menu)
  4511. ;;;; Define the Org-mode
  4512. ;; We use a before-change function to check if a table might need
  4513. ;; an update.
  4514. (defvar org-table-may-need-update t
  4515. "Indicates that a table might need an update.
  4516. This variable is set by `org-before-change-function'.
  4517. `org-table-align' sets it back to nil.")
  4518. (defun org-before-change-function (beg end)
  4519. "Every change indicates that a table might need an update."
  4520. (setq org-table-may-need-update t))
  4521. (defvar org-mode-map)
  4522. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4523. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4524. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4525. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4526. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4527. (defvar org-table-buffer-is-an nil)
  4528. (defvar bidi-paragraph-direction)
  4529. (defvar buffer-face-mode-face)
  4530. (require 'outline)
  4531. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4532. (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"))
  4533. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4534. ;; Other stuff we need.
  4535. (require 'time-date)
  4536. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4537. (require 'easymenu)
  4538. (require 'overlay)
  4539. ;; (require 'org-macs) moved higher up in the file before it is first used
  4540. (require 'org-entities)
  4541. ;; (require 'org-compat) moved higher up in the file before it is first used
  4542. (require 'org-faces)
  4543. (require 'org-list)
  4544. (require 'org-pcomplete)
  4545. (require 'org-src)
  4546. (require 'org-footnote)
  4547. (require 'org-macro)
  4548. ;; babel
  4549. (require 'ob)
  4550. ;;;###autoload
  4551. (define-derived-mode org-mode outline-mode "Org"
  4552. "Outline-based notes management and organizer, alias
  4553. \"Carsten's outline-mode for keeping track of everything.\"
  4554. Org-mode develops organizational tasks around a NOTES file which
  4555. contains information about projects as plain text. Org-mode is
  4556. implemented on top of outline-mode, which is ideal to keep the content
  4557. of large files well structured. It supports ToDo items, deadlines and
  4558. time stamps, which magically appear in the diary listing of the Emacs
  4559. calendar. Tables are easily created with a built-in table editor.
  4560. Plain text URL-like links connect to websites, emails (VM), Usenet
  4561. messages (Gnus), BBDB entries, and any files related to the project.
  4562. For printing and sharing of notes, an Org-mode file (or a part of it)
  4563. can be exported as a structured ASCII or HTML file.
  4564. The following commands are available:
  4565. \\{org-mode-map}"
  4566. ;; Get rid of Outline menus, they are not needed
  4567. ;; Need to do this here because define-derived-mode sets up
  4568. ;; the keymap so late. Still, it is a waste to call this each time
  4569. ;; we switch another buffer into org-mode.
  4570. (if (featurep 'xemacs)
  4571. (when (boundp 'outline-mode-menu-heading)
  4572. ;; Assume this is Greg's port, it uses easymenu
  4573. (easy-menu-remove outline-mode-menu-heading)
  4574. (easy-menu-remove outline-mode-menu-show)
  4575. (easy-menu-remove outline-mode-menu-hide))
  4576. (define-key org-mode-map [menu-bar headings] 'undefined)
  4577. (define-key org-mode-map [menu-bar hide] 'undefined)
  4578. (define-key org-mode-map [menu-bar show] 'undefined))
  4579. (org-load-modules-maybe)
  4580. (easy-menu-add org-org-menu)
  4581. (easy-menu-add org-tbl-menu)
  4582. (org-install-agenda-files-menu)
  4583. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4584. (add-to-invisibility-spec '(org-cwidth))
  4585. (add-to-invisibility-spec '(org-hide-block . t))
  4586. (when (featurep 'xemacs)
  4587. (org-set-local 'line-move-ignore-invisible t))
  4588. (org-set-local 'outline-regexp org-outline-regexp)
  4589. (org-set-local 'outline-level 'org-outline-level)
  4590. (setq bidi-paragraph-direction 'left-to-right)
  4591. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4592. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4593. (when (and org-ellipsis
  4594. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4595. (fboundp 'make-glyph-code))
  4596. (unless org-display-table
  4597. (setq org-display-table (make-display-table)))
  4598. (set-display-table-slot
  4599. org-display-table 4
  4600. (vconcat (mapcar
  4601. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4602. org-ellipsis)))
  4603. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4604. (setq buffer-display-table org-display-table))
  4605. (org-set-regexps-and-options)
  4606. (when (and org-tag-faces (not org-tags-special-faces-re))
  4607. ;; tag faces set outside customize.... force initialization.
  4608. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4609. ;; Calc embedded
  4610. (org-set-local 'calc-embedded-open-mode "# ")
  4611. ;; Modify a few syntax entries
  4612. (modify-syntax-entry ?@ "w")
  4613. (modify-syntax-entry ?\" "\"")
  4614. (if org-startup-truncated (setq truncate-lines t))
  4615. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4616. (org-set-local 'font-lock-unfontify-region-function
  4617. 'org-unfontify-region)
  4618. ;; Activate before-change-function
  4619. (org-set-local 'org-table-may-need-update t)
  4620. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4621. 'local)
  4622. ;; Check for running clock before killing a buffer
  4623. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4624. ;; Initialize macros templates.
  4625. (org-macro-initialize-templates)
  4626. ;; Initialize radio targets.
  4627. (org-update-radio-target-regexp)
  4628. ;; Indentation.
  4629. (org-set-local 'indent-line-function 'org-indent-line)
  4630. (org-set-local 'indent-region-function 'org-indent-region)
  4631. ;; Filling and auto-filling.
  4632. (org-setup-filling)
  4633. ;; Comments.
  4634. (org-setup-comments-handling)
  4635. ;; Beginning/end of defun
  4636. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4637. (org-set-local 'end-of-defun-function 'org-forward-element)
  4638. ;; Next error for sparse trees
  4639. (org-set-local 'next-error-function 'org-occur-next-match)
  4640. ;; Make sure dependence stuff works reliably, even for users who set it
  4641. ;; too late :-(
  4642. (if org-enforce-todo-dependencies
  4643. (add-hook 'org-blocker-hook
  4644. 'org-block-todo-from-children-or-siblings-or-parent)
  4645. (remove-hook 'org-blocker-hook
  4646. 'org-block-todo-from-children-or-siblings-or-parent))
  4647. (if org-enforce-todo-checkbox-dependencies
  4648. (add-hook 'org-blocker-hook
  4649. 'org-block-todo-from-checkboxes)
  4650. (remove-hook 'org-blocker-hook
  4651. 'org-block-todo-from-checkboxes))
  4652. ;; Align options lines
  4653. (org-set-local
  4654. 'align-mode-rules-list
  4655. '((org-in-buffer-settings
  4656. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4657. (modes . '(org-mode)))))
  4658. ;; Imenu
  4659. (org-set-local 'imenu-create-index-function
  4660. 'org-imenu-get-tree)
  4661. ;; Make isearch reveal context
  4662. (if (or (featurep 'xemacs)
  4663. (not (boundp 'outline-isearch-open-invisible-function)))
  4664. ;; Emacs 21 and XEmacs make use of the hook
  4665. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4666. ;; Emacs 22 deals with this through a special variable
  4667. (org-set-local 'outline-isearch-open-invisible-function
  4668. (lambda (&rest ignore) (org-show-context 'isearch)))
  4669. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4670. ;; Setup the pcomplete hooks
  4671. (set (make-local-variable 'pcomplete-command-completion-function)
  4672. 'org-pcomplete-initial)
  4673. (set (make-local-variable 'pcomplete-command-name-function)
  4674. 'org-command-at-point)
  4675. (set (make-local-variable 'pcomplete-default-completion-function)
  4676. 'ignore)
  4677. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4678. 'org-parse-arguments)
  4679. (set (make-local-variable 'pcomplete-termination-string) "")
  4680. (when (>= emacs-major-version 23)
  4681. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4682. ;; If empty file that did not turn on org-mode automatically, make it to.
  4683. (if (and org-insert-mode-line-in-empty-file
  4684. (org-called-interactively-p 'any)
  4685. (= (point-min) (point-max)))
  4686. (insert "# -*- mode: org -*-\n\n"))
  4687. (unless org-inhibit-startup
  4688. (org-unmodified
  4689. (and org-startup-with-beamer-mode (org-beamer-mode))
  4690. (when org-startup-align-all-tables
  4691. (org-table-map-tables 'org-table-align 'quietly))
  4692. (when org-startup-with-inline-images
  4693. (org-display-inline-images))
  4694. (when org-startup-with-latex-preview
  4695. (org-preview-latex-fragment))
  4696. (unless org-inhibit-startup-visibility-stuff
  4697. (org-set-startup-visibility))))
  4698. ;; Try to set org-hide correctly
  4699. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4700. ;; Update `customize-package-emacs-version-alist'
  4701. (add-to-list 'customize-package-emacs-version-alist
  4702. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4703. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4704. ("8.0" . "24.4")))
  4705. (defvar org-mode-transpose-word-syntax-table
  4706. (let ((st (make-syntax-table)))
  4707. (mapc (lambda(c) (modify-syntax-entry
  4708. (string-to-char (car c)) "w p" st))
  4709. org-emphasis-alist)
  4710. st))
  4711. (when (fboundp 'abbrev-table-put)
  4712. (abbrev-table-put org-mode-abbrev-table
  4713. :parents (list text-mode-abbrev-table)))
  4714. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4715. (defsubst org-fix-ellipsis-at-bol ()
  4716. (save-excursion (goto-char (window-start)) (recenter 0)))
  4717. (defun org-find-invisible-foreground ()
  4718. (let ((candidates (remove
  4719. "unspecified-bg"
  4720. (nconc
  4721. (list (face-background 'default)
  4722. (face-background 'org-default))
  4723. (mapcar
  4724. (lambda (alist)
  4725. (when (boundp alist)
  4726. (cdr (assoc 'background-color (symbol-value alist)))))
  4727. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4728. (list (face-foreground 'org-hide))))))
  4729. (car (remove nil candidates))))
  4730. (defun org-current-time (&optional rounding-minutes past)
  4731. "Current time, possibly rounded to ROUNDING-MINUTES.
  4732. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4733. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4734. the rounding returns a past time."
  4735. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4736. (car org-time-stamp-rounding-minutes)))
  4737. (time (decode-time)) res)
  4738. (if (< r 1)
  4739. (current-time)
  4740. (setq res
  4741. (apply 'encode-time
  4742. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4743. (nthcdr 2 time))))
  4744. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4745. (seconds-to-time (- (org-float-time res) (* r 60)))
  4746. res))))
  4747. (defun org-today ()
  4748. "Return today date, considering `org-extend-today-until'."
  4749. (time-to-days
  4750. (time-subtract (current-time)
  4751. (list 0 (* 3600 org-extend-today-until) 0))))
  4752. ;;;; Font-Lock stuff, including the activators
  4753. (defvar org-mouse-map (make-sparse-keymap))
  4754. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4755. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4756. (when org-mouse-1-follows-link
  4757. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4758. (when org-tab-follows-link
  4759. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4760. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4761. (require 'font-lock)
  4762. (defconst org-non-link-chars "]\t\n\r<>")
  4763. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4764. "shell" "elisp" "doi" "message"))
  4765. (defvar org-link-types-re nil
  4766. "Matches a link that has a url-like prefix like \"http:\"")
  4767. (defvar org-link-re-with-space nil
  4768. "Matches a link with spaces, optional angular brackets around it.")
  4769. (defvar org-link-re-with-space2 nil
  4770. "Matches a link with spaces, optional angular brackets around it.")
  4771. (defvar org-link-re-with-space3 nil
  4772. "Matches a link with spaces, only for internal part in bracket links.")
  4773. (defvar org-angle-link-re nil
  4774. "Matches link with angular brackets, spaces are allowed.")
  4775. (defvar org-plain-link-re nil
  4776. "Matches plain link, without spaces.")
  4777. (defvar org-bracket-link-regexp nil
  4778. "Matches a link in double brackets.")
  4779. (defvar org-bracket-link-analytic-regexp nil
  4780. "Regular expression used to analyze links.
  4781. Here is what the match groups contain after a match:
  4782. 1: http:
  4783. 2: http
  4784. 3: path
  4785. 4: [desc]
  4786. 5: desc")
  4787. (defvar org-bracket-link-analytic-regexp++ nil
  4788. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4789. (defvar org-any-link-re nil
  4790. "Regular expression matching any link.")
  4791. (defconst org-match-sexp-depth 3
  4792. "Number of stacked braces for sub/superscript matching.")
  4793. (defun org-create-multibrace-regexp (left right n)
  4794. "Create a regular expression which will match a balanced sexp.
  4795. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4796. as single character strings.
  4797. The regexp returned will match the entire expression including the
  4798. delimiters. It will also define a single group which contains the
  4799. match except for the outermost delimiters. The maximum depth of
  4800. stacked delimiters is N. Escaping delimiters is not possible."
  4801. (let* ((nothing (concat "[^" left right "]*?"))
  4802. (or "\\|")
  4803. (re nothing)
  4804. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4805. (while (> n 1)
  4806. (setq n (1- n)
  4807. re (concat re or next)
  4808. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4809. (concat left "\\(" re "\\)" right)))
  4810. (defvar org-match-substring-regexp
  4811. (concat
  4812. "\\(\\S-\\)\\([_^]\\)\\("
  4813. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4814. "\\|"
  4815. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4816. "\\|"
  4817. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4818. "The regular expression matching a sub- or superscript.")
  4819. (defvar org-match-substring-with-braces-regexp
  4820. (concat
  4821. "\\(\\S-\\)\\([_^]\\)\\("
  4822. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4823. "\\)")
  4824. "The regular expression matching a sub- or superscript, forcing braces.")
  4825. (defun org-make-link-regexps ()
  4826. "Update the link regular expressions.
  4827. This should be called after the variable `org-link-types' has changed."
  4828. (setq org-link-types-re
  4829. (concat
  4830. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4831. org-link-re-with-space
  4832. (concat
  4833. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4834. "\\([^" org-non-link-chars " ]"
  4835. "[^" org-non-link-chars "]*"
  4836. "[^" org-non-link-chars " ]\\)>?")
  4837. org-link-re-with-space2
  4838. (concat
  4839. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4840. "\\([^" org-non-link-chars " ]"
  4841. "[^\t\n\r]*"
  4842. "[^" org-non-link-chars " ]\\)>?")
  4843. org-link-re-with-space3
  4844. (concat
  4845. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4846. "\\([^" org-non-link-chars " ]"
  4847. "[^\t\n\r]*\\)")
  4848. org-angle-link-re
  4849. (concat
  4850. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4851. "\\([^" org-non-link-chars " ]"
  4852. "[^" org-non-link-chars "]*"
  4853. "\\)>")
  4854. org-plain-link-re
  4855. (concat
  4856. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4857. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4858. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4859. org-bracket-link-regexp
  4860. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4861. org-bracket-link-analytic-regexp
  4862. (concat
  4863. "\\[\\["
  4864. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4865. "\\([^]]+\\)"
  4866. "\\]"
  4867. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4868. "\\]")
  4869. org-bracket-link-analytic-regexp++
  4870. (concat
  4871. "\\[\\["
  4872. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4873. "\\([^]]+\\)"
  4874. "\\]"
  4875. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4876. "\\]")
  4877. org-any-link-re
  4878. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4879. org-angle-link-re "\\)\\|\\("
  4880. org-plain-link-re "\\)")))
  4881. (org-make-link-regexps)
  4882. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4883. "Regular expression for fast time stamp matching.")
  4884. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4885. "Regular expression for fast time stamp matching.")
  4886. (defconst org-ts-regexp0
  4887. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4888. "Regular expression matching time strings for analysis.
  4889. This one does not require the space after the date, so it can be used
  4890. on a string that terminates immediately after the date.")
  4891. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4892. "Regular expression matching time strings for analysis.")
  4893. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4894. "Regular expression matching time stamps, with groups.")
  4895. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4896. "Regular expression matching time stamps (also [..]), with groups.")
  4897. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4898. "Regular expression matching a time stamp range.")
  4899. (defconst org-tr-regexp-both
  4900. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4901. "Regular expression matching a time stamp range.")
  4902. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4903. org-ts-regexp "\\)?")
  4904. "Regular expression matching a time stamp or time stamp range.")
  4905. (defconst org-tsr-regexp-both
  4906. (concat org-ts-regexp-both "\\(--?-?"
  4907. org-ts-regexp-both "\\)?")
  4908. "Regular expression matching a time stamp or time stamp range.
  4909. The time stamps may be either active or inactive.")
  4910. (defvar org-emph-face nil)
  4911. (defun org-do-emphasis-faces (limit)
  4912. "Run through the buffer and add overlays to emphasized strings."
  4913. (let (rtn a)
  4914. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4915. (if (not (= (char-after (match-beginning 3))
  4916. (char-after (match-beginning 4))))
  4917. (progn
  4918. (setq rtn t)
  4919. (setq a (assoc (match-string 3) org-emphasis-alist))
  4920. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4921. 'face
  4922. (nth 1 a))
  4923. (and (nth 4 a)
  4924. (org-remove-flyspell-overlays-in
  4925. (match-beginning 0) (match-end 0)))
  4926. (add-text-properties (match-beginning 2) (match-end 2)
  4927. '(font-lock-multiline t org-emphasis t))
  4928. (when org-hide-emphasis-markers
  4929. (add-text-properties (match-end 4) (match-beginning 5)
  4930. '(invisible org-link))
  4931. (add-text-properties (match-beginning 3) (match-end 3)
  4932. '(invisible org-link)))))
  4933. (backward-char 1))
  4934. rtn))
  4935. (defun org-emphasize (&optional char)
  4936. "Insert or change an emphasis, i.e. a font like bold or italic.
  4937. If there is an active region, change that region to a new emphasis.
  4938. If there is no region, just insert the marker characters and position
  4939. the cursor between them.
  4940. CHAR should be either the marker character, or the first character of the
  4941. HTML tag associated with that emphasis. If CHAR is a space, the means
  4942. to remove the emphasis of the selected region.
  4943. If char is not given (for example in an interactive call) it
  4944. will be prompted for."
  4945. (interactive)
  4946. (let ((eal org-emphasis-alist) e det
  4947. (erc org-emphasis-regexp-components)
  4948. (prompt "")
  4949. (string "") beg end move tag c s)
  4950. (if (org-region-active-p)
  4951. (setq beg (region-beginning) end (region-end)
  4952. string (buffer-substring beg end))
  4953. (setq move t))
  4954. (while (setq e (pop eal))
  4955. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4956. c (aref tag 0))
  4957. (push (cons c (string-to-char (car e))) det)
  4958. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4959. (substring tag 1)))))
  4960. (setq det (nreverse det))
  4961. (unless char
  4962. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4963. (setq char (read-char-exclusive)))
  4964. (setq char (or (cdr (assoc char det)) char))
  4965. (if (equal char ?\ )
  4966. (setq s "" move nil)
  4967. (unless (assoc (char-to-string char) org-emphasis-alist)
  4968. (error "No such emphasis marker: \"%c\"" char))
  4969. (setq s (char-to-string char)))
  4970. (while (and (> (length string) 1)
  4971. (equal (substring string 0 1) (substring string -1))
  4972. (assoc (substring string 0 1) org-emphasis-alist))
  4973. (setq string (substring string 1 -1)))
  4974. (setq string (concat s string s))
  4975. (if beg (delete-region beg end))
  4976. (unless (or (bolp)
  4977. (string-match (concat "[" (nth 0 erc) "\n]")
  4978. (char-to-string (char-before (point)))))
  4979. (insert " "))
  4980. (unless (or (eobp)
  4981. (string-match (concat "[" (nth 1 erc) "\n]")
  4982. (char-to-string (char-after (point)))))
  4983. (insert " ") (backward-char 1))
  4984. (insert string)
  4985. (and move (backward-char 1))))
  4986. (defconst org-nonsticky-props
  4987. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4988. (defsubst org-rear-nonsticky-at (pos)
  4989. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4990. (defun org-activate-plain-links (limit)
  4991. "Run through the buffer and add overlays to links."
  4992. (let (f hl)
  4993. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4994. (not (org-in-src-block-p)))
  4995. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4996. (setq f (get-text-property (match-beginning 0) 'face))
  4997. (setq hl (org-match-string-no-properties 0))
  4998. (if (or (eq f 'org-tag)
  4999. (and (listp f) (memq 'org-tag f)))
  5000. nil
  5001. (add-text-properties (match-beginning 0) (match-end 0)
  5002. (list 'mouse-face 'highlight
  5003. 'face 'org-link
  5004. 'htmlize-link `(:uri ,hl)
  5005. 'keymap org-mouse-map))
  5006. (org-rear-nonsticky-at (match-end 0)))
  5007. t)))
  5008. (defun org-activate-code (limit)
  5009. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5010. (progn
  5011. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5012. (remove-text-properties (match-beginning 0) (match-end 0)
  5013. '(display t invisible t intangible t))
  5014. t)))
  5015. (defcustom org-src-fontify-natively nil
  5016. "When non-nil, fontify code in code blocks."
  5017. :type 'boolean
  5018. :version "24.1"
  5019. :group 'org-appearance
  5020. :group 'org-babel)
  5021. (defcustom org-allow-promoting-top-level-subtree nil
  5022. "When non-nil, allow promoting a top level subtree.
  5023. The leading star of the top level headline will be replaced
  5024. by a #."
  5025. :type 'boolean
  5026. :version "24.1"
  5027. :group 'org-appearance)
  5028. (defun org-fontify-meta-lines-and-blocks (limit)
  5029. (condition-case nil
  5030. (org-fontify-meta-lines-and-blocks-1 limit)
  5031. (error (message "org-mode fontification error"))))
  5032. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5033. "Fontify #+ lines and blocks, in the correct ways."
  5034. (let ((case-fold-search t))
  5035. (if (re-search-forward
  5036. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5037. limit t)
  5038. (let ((beg (match-beginning 0))
  5039. (block-start (match-end 0))
  5040. (block-end nil)
  5041. (lang (match-string 7))
  5042. (beg1 (line-beginning-position 2))
  5043. (dc1 (downcase (match-string 2)))
  5044. (dc3 (downcase (match-string 3)))
  5045. end end1 quoting block-type ovl)
  5046. (cond
  5047. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5048. ;; a single line of backend-specific content
  5049. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5050. (remove-text-properties (match-beginning 0) (match-end 0)
  5051. '(display t invisible t intangible t))
  5052. (add-text-properties (match-beginning 1) (match-end 3)
  5053. '(font-lock-fontified t face org-meta-line))
  5054. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5055. '(font-lock-fontified t face org-block))
  5056. ; for backend-specific code
  5057. t)
  5058. ((and (match-end 4) (equal dc3 "+begin"))
  5059. ;; Truly a block
  5060. (setq block-type (downcase (match-string 5))
  5061. quoting (member block-type org-protecting-blocks))
  5062. (when (re-search-forward
  5063. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5064. nil t) ;; on purpose, we look further than LIMIT
  5065. (setq end (min (point-max) (match-end 0))
  5066. end1 (min (point-max) (1- (match-beginning 0))))
  5067. (setq block-end (match-beginning 0))
  5068. (when quoting
  5069. (remove-text-properties beg end
  5070. '(display t invisible t intangible t)))
  5071. (add-text-properties
  5072. beg end
  5073. '(font-lock-fontified t font-lock-multiline t))
  5074. (add-text-properties beg beg1 '(face org-meta-line))
  5075. (add-text-properties end1 (min (point-max) (1+ end))
  5076. '(face org-meta-line)) ; for end_src
  5077. (cond
  5078. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5079. (org-src-font-lock-fontify-block lang block-start block-end)
  5080. ;; remove old background overlays
  5081. (mapc (lambda (ov)
  5082. (if (eq (overlay-get ov 'face) 'org-block-background)
  5083. (delete-overlay ov)))
  5084. (overlays-at (/ (+ beg1 block-end) 2)))
  5085. ;; add a background overlay
  5086. (setq ovl (make-overlay beg1 block-end))
  5087. (overlay-put ovl 'face 'org-block-background)
  5088. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5089. (quoting
  5090. (add-text-properties beg1 (min (point-max) (1+ end1))
  5091. '(face org-block))) ; end of source block
  5092. ((not org-fontify-quote-and-verse-blocks))
  5093. ((string= block-type "quote")
  5094. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5095. ((string= block-type "verse")
  5096. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5097. (add-text-properties beg beg1 '(face org-block-begin-line))
  5098. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5099. '(face org-block-end-line))
  5100. t))
  5101. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5102. (add-text-properties
  5103. beg (match-end 3)
  5104. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5105. '(font-lock-fontified t invisible t)
  5106. '(font-lock-fontified t face org-document-info-keyword)))
  5107. (add-text-properties
  5108. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5109. (if (string-equal dc1 "+title:")
  5110. '(font-lock-fontified t face org-document-title)
  5111. '(font-lock-fontified t face org-document-info))))
  5112. ((or (equal dc1 "+results")
  5113. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5114. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5115. "+call:" "+header:" "+headers:" "+name:"))
  5116. (and (match-end 4) (equal dc3 "+attr")))
  5117. (add-text-properties
  5118. beg (match-end 0)
  5119. '(font-lock-fontified t face org-meta-line))
  5120. t)
  5121. ((member dc3 '(" " ""))
  5122. (add-text-properties
  5123. beg (match-end 0)
  5124. '(font-lock-fontified t face font-lock-comment-face)))
  5125. ((not (member (char-after beg) '(?\ ?\t)))
  5126. ;; just any other in-buffer setting, but not indented
  5127. (add-text-properties
  5128. beg (match-end 0)
  5129. '(font-lock-fontified t face org-meta-line))
  5130. t)
  5131. (t nil))))))
  5132. (defun org-activate-angle-links (limit)
  5133. "Run through the buffer and add overlays to links."
  5134. (if (and (re-search-forward org-angle-link-re limit t)
  5135. (not (org-in-src-block-p)))
  5136. (progn
  5137. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5138. (add-text-properties (match-beginning 0) (match-end 0)
  5139. (list 'mouse-face 'highlight
  5140. 'keymap org-mouse-map))
  5141. (org-rear-nonsticky-at (match-end 0))
  5142. t)))
  5143. (defun org-activate-footnote-links (limit)
  5144. "Run through the buffer and add overlays to footnotes."
  5145. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5146. (when fn
  5147. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5148. (org-remove-flyspell-overlays-in beg end)
  5149. (add-text-properties beg end
  5150. (list 'mouse-face 'highlight
  5151. 'keymap org-mouse-map
  5152. 'help-echo
  5153. (if (= (point-at-bol) beg)
  5154. "Footnote definition"
  5155. "Footnote reference")
  5156. 'font-lock-fontified t
  5157. 'font-lock-multiline t
  5158. 'face 'org-footnote))))))
  5159. (defun org-activate-bracket-links (limit)
  5160. "Run through the buffer and add overlays to bracketed links."
  5161. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5162. (not (org-in-src-block-p)))
  5163. (let* ((hl (org-match-string-no-properties 1))
  5164. (help (concat "LINK: " hl))
  5165. ;; FIXME: Above we should remove the escapes. But that
  5166. ;; requires another match, protecting match data, a lot
  5167. ;; of overhead for font-lock.
  5168. (ip (org-maybe-intangible
  5169. (list 'invisible 'org-link
  5170. 'keymap org-mouse-map 'mouse-face 'highlight
  5171. 'font-lock-multiline t 'help-echo help
  5172. 'htmlize-link `(:uri ,hl))))
  5173. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5174. 'font-lock-multiline t 'help-echo help
  5175. 'htmlize-link `(:uri ,hl))))
  5176. ;; We need to remove the invisible property here. Table narrowing
  5177. ;; may have made some of this invisible.
  5178. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5179. (remove-text-properties (match-beginning 0) (match-end 0)
  5180. '(invisible nil))
  5181. (if (match-end 3)
  5182. (progn
  5183. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5184. (org-rear-nonsticky-at (match-beginning 3))
  5185. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5186. (org-rear-nonsticky-at (match-end 3))
  5187. (add-text-properties (match-end 3) (match-end 0) ip)
  5188. (org-rear-nonsticky-at (match-end 0)))
  5189. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5190. (org-rear-nonsticky-at (match-beginning 1))
  5191. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5192. (org-rear-nonsticky-at (match-end 1))
  5193. (add-text-properties (match-end 1) (match-end 0) ip)
  5194. (org-rear-nonsticky-at (match-end 0)))
  5195. t)))
  5196. (defun org-activate-dates (limit)
  5197. "Run through the buffer and add overlays to dates."
  5198. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5199. (not (equal (char-before (match-beginning 0)) 91)))
  5200. (progn
  5201. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5202. (add-text-properties (match-beginning 0) (match-end 0)
  5203. (list 'mouse-face 'highlight
  5204. 'keymap org-mouse-map))
  5205. (org-rear-nonsticky-at (match-end 0))
  5206. (when org-display-custom-times
  5207. (if (match-end 3)
  5208. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5209. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5210. t)))
  5211. (defvar org-target-link-regexp nil
  5212. "Regular expression matching radio targets in plain text.")
  5213. (make-variable-buffer-local 'org-target-link-regexp)
  5214. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5215. "Regular expression matching a link target.")
  5216. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5217. "Regular expression matching a radio target.")
  5218. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5219. "Regular expression matching any target.")
  5220. (defun org-activate-target-links (limit)
  5221. "Run through the buffer and add overlays to target matches."
  5222. (when org-target-link-regexp
  5223. (let ((case-fold-search t))
  5224. (if (re-search-forward org-target-link-regexp limit t)
  5225. (progn
  5226. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5227. (add-text-properties (match-beginning 0) (match-end 0)
  5228. (list 'mouse-face 'highlight
  5229. 'keymap org-mouse-map
  5230. 'help-echo "Radio target link"
  5231. 'org-linked-text t))
  5232. (org-rear-nonsticky-at (match-end 0))
  5233. t)))))
  5234. (defun org-update-radio-target-regexp ()
  5235. "Find all radio targets in this file and update the regular expression."
  5236. (interactive)
  5237. (when (memq 'radio org-activate-links)
  5238. (setq org-target-link-regexp
  5239. (org-make-target-link-regexp (org-all-targets 'radio)))
  5240. (org-restart-font-lock)))
  5241. (defun org-hide-wide-columns (limit)
  5242. (let (s e)
  5243. (setq s (text-property-any (point) (or limit (point-max))
  5244. 'org-cwidth t))
  5245. (when s
  5246. (setq e (next-single-property-change s 'org-cwidth))
  5247. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5248. (goto-char e)
  5249. t)))
  5250. (defvar org-latex-and-related-regexp nil
  5251. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5252. (defvar org-match-substring-regexp)
  5253. (defvar org-match-substring-with-braces-regexp)
  5254. (defun org-compute-latex-and-related-regexp ()
  5255. "Compute regular expression for LaTeX, entities and sub/superscript.
  5256. Result depends on variable `org-highlight-latex-and-related'."
  5257. (org-set-local
  5258. 'org-latex-and-related-regexp
  5259. (let* ((re-sub
  5260. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5261. ((eq org-use-sub-superscripts '{})
  5262. (list org-match-substring-with-braces-regexp))
  5263. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5264. (re-latex
  5265. (when (memq 'latex org-highlight-latex-and-related)
  5266. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5267. (delq nil
  5268. (mapcar (lambda (x)
  5269. (and (member (car x) matchers) (nth 1 x)))
  5270. org-latex-regexps)))))
  5271. (re-entities
  5272. (when (memq 'entities org-highlight-latex-and-related)
  5273. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5274. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5275. (defun org-do-latex-and-related (limit)
  5276. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5277. LIMIT bounds the search for syntax to highlight. Stop at first
  5278. highlighted object, if any. Return t if some highlighting was
  5279. done, nil otherwise."
  5280. (when (org-string-nw-p org-latex-and-related-regexp)
  5281. (catch 'found
  5282. (while (re-search-forward org-latex-and-related-regexp limit t)
  5283. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5284. 'face))
  5285. '(org-code org-verbatim underline))
  5286. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5287. '(?_ ?^))
  5288. 1
  5289. 0)))
  5290. (font-lock-prepend-text-property
  5291. (+ offset (match-beginning 0)) (match-end 0)
  5292. 'face 'org-latex-and-related)
  5293. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5294. '(font-lock-multiline t)))
  5295. (throw 'found t)))
  5296. nil)))
  5297. (defun org-restart-font-lock ()
  5298. "Restart `font-lock-mode', to force refontification."
  5299. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5300. (font-lock-mode -1)
  5301. (font-lock-mode 1)))
  5302. (defun org-all-targets (&optional radio)
  5303. "Return a list of all targets in this file.
  5304. When optional argument RADIO is non-nil, only find radio
  5305. targets."
  5306. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5307. (save-excursion
  5308. (goto-char (point-min))
  5309. (while (re-search-forward re nil t)
  5310. ;; Make sure point is really within the object.
  5311. (backward-char)
  5312. (let ((obj (org-element-context)))
  5313. (when (memq (org-element-type obj) '(radio-target target))
  5314. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5315. rtn)))
  5316. (defun org-make-target-link-regexp (targets)
  5317. "Make regular expression matching all strings in TARGETS.
  5318. The regular expression finds the targets also if there is a line break
  5319. between words."
  5320. (and targets
  5321. (concat
  5322. "\\<\\("
  5323. (mapconcat
  5324. (lambda (x)
  5325. (setq x (regexp-quote x))
  5326. (while (string-match " +" x)
  5327. (setq x (replace-match "\\s-+" t t x)))
  5328. x)
  5329. targets
  5330. "\\|")
  5331. "\\)\\>")))
  5332. (defun org-activate-tags (limit)
  5333. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5334. (progn
  5335. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5336. (add-text-properties (match-beginning 1) (match-end 1)
  5337. (list 'mouse-face 'highlight
  5338. 'keymap org-mouse-map))
  5339. (org-rear-nonsticky-at (match-end 1))
  5340. t)))
  5341. (defun org-outline-level ()
  5342. "Compute the outline level of the heading at point.
  5343. If this is called at a normal headline, the level is the number of stars.
  5344. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5345. (save-excursion
  5346. (if (not (condition-case nil
  5347. (org-back-to-heading t)
  5348. (error nil)))
  5349. 0
  5350. (looking-at org-outline-regexp)
  5351. (1- (- (match-end 0) (match-beginning 0))))))
  5352. (defvar org-font-lock-keywords nil)
  5353. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5354. "Regular expression matching a property line.")
  5355. (defvar org-font-lock-hook nil
  5356. "Functions to be called for special font lock stuff.")
  5357. (defvar org-font-lock-set-keywords-hook nil
  5358. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5359. This is called after `org-font-lock-extra-keywords' is defined, but before
  5360. it is installed to be used by font lock. This can be useful if something
  5361. needs to be inserted at a specific position in the font-lock sequence.")
  5362. (defun org-font-lock-hook (limit)
  5363. "Run `org-font-lock-hook' within LIMIT."
  5364. (run-hook-with-args 'org-font-lock-hook limit))
  5365. (defun org-set-font-lock-defaults ()
  5366. "Set font lock defaults for the current buffer."
  5367. (let* ((em org-fontify-emphasized-text)
  5368. (lk org-activate-links)
  5369. (org-font-lock-extra-keywords
  5370. (list
  5371. ;; Call the hook
  5372. '(org-font-lock-hook)
  5373. ;; Headlines
  5374. `(,(if org-fontify-whole-heading-line
  5375. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5376. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5377. (1 (org-get-level-face 1))
  5378. (2 (org-get-level-face 2))
  5379. (3 (org-get-level-face 3)))
  5380. ;; Table lines
  5381. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5382. (1 'org-table t))
  5383. ;; Table internals
  5384. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5385. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5386. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5387. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5388. ;; Drawers
  5389. (list org-drawer-regexp '(0 'org-special-keyword t))
  5390. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5391. ;; Properties
  5392. (list org-property-re
  5393. '(1 'org-special-keyword t)
  5394. '(3 'org-property-value t))
  5395. ;; Links
  5396. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5397. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5398. (if (memq 'plain lk) '(org-activate-plain-links))
  5399. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5400. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5401. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5402. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5403. ;; Targets.
  5404. (list org-any-target-regexp '(0 'org-target t))
  5405. ;; Diary sexps.
  5406. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5407. ;; Macro
  5408. '("{{{.+}}}" (0 'org-macro t))
  5409. '(org-hide-wide-columns (0 nil append))
  5410. ;; TODO keyword
  5411. (list (format org-heading-keyword-regexp-format
  5412. org-todo-regexp)
  5413. '(2 (org-get-todo-face 2) t))
  5414. ;; DONE
  5415. (if org-fontify-done-headline
  5416. (list (format org-heading-keyword-regexp-format
  5417. (concat
  5418. "\\(?:"
  5419. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5420. "\\)"))
  5421. '(2 'org-headline-done t))
  5422. nil)
  5423. ;; Priorities
  5424. '(org-font-lock-add-priority-faces)
  5425. ;; Tags
  5426. '(org-font-lock-add-tag-faces)
  5427. ;; Special keywords
  5428. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5429. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5430. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5431. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5432. ;; Emphasis
  5433. (if em
  5434. (if (featurep 'xemacs)
  5435. '(org-do-emphasis-faces (0 nil append))
  5436. '(org-do-emphasis-faces)))
  5437. ;; Checkboxes
  5438. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5439. 1 'org-checkbox prepend)
  5440. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5441. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5442. (0 (org-get-checkbox-statistics-face) t)))
  5443. ;; Description list items
  5444. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5445. 1 'org-list-dt prepend)
  5446. ;; ARCHIVEd headings
  5447. (list (concat
  5448. org-outline-regexp-bol
  5449. "\\(.*:" org-archive-tag ":.*\\)")
  5450. '(1 'org-archived prepend))
  5451. ;; Specials
  5452. '(org-do-latex-and-related)
  5453. '(org-fontify-entities)
  5454. '(org-raise-scripts)
  5455. ;; Code
  5456. '(org-activate-code (1 'org-code t))
  5457. ;; COMMENT
  5458. (list (format org-heading-keyword-regexp-format
  5459. (concat "\\("
  5460. org-comment-string "\\|" org-quote-string
  5461. "\\)"))
  5462. '(2 'org-special-keyword t))
  5463. ;; Blocks and meta lines
  5464. '(org-fontify-meta-lines-and-blocks)
  5465. )))
  5466. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5467. (run-hooks 'org-font-lock-set-keywords-hook)
  5468. ;; Now set the full font-lock-keywords
  5469. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5470. (org-set-local 'font-lock-defaults
  5471. '(org-font-lock-keywords t nil nil backward-paragraph))
  5472. (kill-local-variable 'font-lock-keywords) nil))
  5473. (defun org-toggle-pretty-entities ()
  5474. "Toggle the composition display of entities as UTF8 characters."
  5475. (interactive)
  5476. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5477. (org-restart-font-lock)
  5478. (if org-pretty-entities
  5479. (message "Entities are displayed as UTF8 characters")
  5480. (save-restriction
  5481. (widen)
  5482. (org-decompose-region (point-min) (point-max))
  5483. (message "Entities are displayed plain"))))
  5484. (defvar org-custom-properties-overlays nil
  5485. "List of overlays used for custom properties.")
  5486. (make-variable-buffer-local 'org-custom-properties-overlays)
  5487. (defun org-toggle-custom-properties-visibility ()
  5488. "Display or hide properties in `org-custom-properties'."
  5489. (interactive)
  5490. (if org-custom-properties-overlays
  5491. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5492. (setq org-custom-properties-overlays nil))
  5493. (unless (not org-custom-properties)
  5494. (save-excursion
  5495. (save-restriction
  5496. (widen)
  5497. (goto-char (point-min))
  5498. (while (re-search-forward org-property-re nil t)
  5499. (mapc (lambda(p)
  5500. (when (equal p (substring (match-string 1) 1 -1))
  5501. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5502. (overlay-put o 'invisible t)
  5503. (overlay-put o 'org-custom-property t)
  5504. (push o org-custom-properties-overlays))))
  5505. org-custom-properties)))))))
  5506. (defun org-fontify-entities (limit)
  5507. "Find an entity to fontify."
  5508. (let (ee)
  5509. (when org-pretty-entities
  5510. (catch 'match
  5511. (while (re-search-forward
  5512. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5513. limit t)
  5514. (if (and (not (org-in-indented-comment-line))
  5515. (setq ee (org-entity-get (match-string 1)))
  5516. (= (length (nth 6 ee)) 1))
  5517. (let*
  5518. ((end (if (equal (match-string 2) "{}")
  5519. (match-end 2)
  5520. (match-end 1))))
  5521. (add-text-properties
  5522. (match-beginning 0) end
  5523. (list 'font-lock-fontified t))
  5524. (compose-region (match-beginning 0) end
  5525. (nth 6 ee) nil)
  5526. (backward-char 1)
  5527. (throw 'match t))))
  5528. nil))))
  5529. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5530. "Fontify string S like in Org-mode."
  5531. (with-temp-buffer
  5532. (insert s)
  5533. (let ((org-odd-levels-only odd-levels))
  5534. (org-mode)
  5535. (font-lock-fontify-buffer)
  5536. (buffer-string))))
  5537. (defvar org-m nil)
  5538. (defvar org-l nil)
  5539. (defvar org-f nil)
  5540. (defun org-get-level-face (n)
  5541. "Get the right face for match N in font-lock matching of headlines."
  5542. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5543. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5544. (if org-cycle-level-faces
  5545. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5546. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5547. (cond
  5548. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5549. ((eq n 2) org-f)
  5550. (t (if org-level-color-stars-only nil org-f))))
  5551. (defun org-get-todo-face (kwd)
  5552. "Get the right face for a TODO keyword KWD.
  5553. If KWD is a number, get the corresponding match group."
  5554. (if (numberp kwd) (setq kwd (match-string kwd)))
  5555. (or (org-face-from-face-or-color
  5556. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5557. (and (member kwd org-done-keywords) 'org-done)
  5558. 'org-todo))
  5559. (defun org-face-from-face-or-color (context inherit face-or-color)
  5560. "Create a face list that inherits INHERIT, but sets the foreground color.
  5561. When FACE-OR-COLOR is not a string, just return it."
  5562. (if (stringp face-or-color)
  5563. (list :inherit inherit
  5564. (cdr (assoc context org-faces-easy-properties))
  5565. face-or-color)
  5566. face-or-color))
  5567. (defun org-font-lock-add-tag-faces (limit)
  5568. "Add the special tag faces."
  5569. (when (and org-tag-faces org-tags-special-faces-re)
  5570. (while (re-search-forward org-tags-special-faces-re limit t)
  5571. (add-text-properties (match-beginning 1) (match-end 1)
  5572. (list 'face (org-get-tag-face 1)
  5573. 'font-lock-fontified t))
  5574. (backward-char 1))))
  5575. (defun org-font-lock-add-priority-faces (limit)
  5576. "Add the special priority faces."
  5577. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5578. (when (save-match-data (org-at-heading-p))
  5579. (add-text-properties
  5580. (match-beginning 0) (match-end 0)
  5581. (list 'face (or (org-face-from-face-or-color
  5582. 'priority 'org-priority
  5583. (cdr (assoc (char-after (match-beginning 1))
  5584. org-priority-faces)))
  5585. 'org-priority)
  5586. 'font-lock-fontified t)))))
  5587. (defun org-get-tag-face (kwd)
  5588. "Get the right face for a TODO keyword KWD.
  5589. If KWD is a number, get the corresponding match group."
  5590. (if (numberp kwd) (setq kwd (match-string kwd)))
  5591. (or (org-face-from-face-or-color
  5592. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5593. 'org-tag))
  5594. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5595. "Remove fontification and activation overlays from links."
  5596. (font-lock-default-unfontify-region beg end)
  5597. (let* ((buffer-undo-list t)
  5598. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5599. (inhibit-modification-hooks t)
  5600. deactivate-mark buffer-file-name buffer-file-truename)
  5601. (org-decompose-region beg end)
  5602. (remove-text-properties beg end
  5603. '(mouse-face t keymap t org-linked-text t
  5604. invisible t intangible t
  5605. org-no-flyspell t org-emphasis t))
  5606. (org-remove-font-lock-display-properties beg end)))
  5607. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5608. ((raise 0.3) (height 0.7))
  5609. ((raise -0.5))
  5610. ((raise 0.5)))
  5611. "Display properties for showing superscripts and subscripts.")
  5612. (defun org-remove-font-lock-display-properties (beg end)
  5613. "Remove specific display properties that have been added by font lock.
  5614. The will remove the raise properties that are used to show superscripts
  5615. and subscripts."
  5616. (let (next prop)
  5617. (while (< beg end)
  5618. (setq next (next-single-property-change beg 'display nil end)
  5619. prop (get-text-property beg 'display))
  5620. (if (member prop org-script-display)
  5621. (put-text-property beg next 'display nil))
  5622. (setq beg next))))
  5623. (defun org-raise-scripts (limit)
  5624. "Add raise properties to sub/superscripts."
  5625. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5626. (if (re-search-forward
  5627. (if (eq org-use-sub-superscripts t)
  5628. org-match-substring-regexp
  5629. org-match-substring-with-braces-regexp)
  5630. limit t)
  5631. (let* ((pos (point)) table-p comment-p
  5632. (mpos (match-beginning 3))
  5633. (emph-p (get-text-property mpos 'org-emphasis))
  5634. (link-p (get-text-property mpos 'mouse-face))
  5635. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5636. (goto-char (point-at-bol))
  5637. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5638. comment-p (org-looking-at-p "[ \t]*#"))
  5639. (goto-char pos)
  5640. ;; FIXME: Should we go back one character here, for a_b^c
  5641. ;; (goto-char (1- pos)) ;????????????????????
  5642. (if (or comment-p emph-p link-p keyw-p)
  5643. t
  5644. (put-text-property (match-beginning 3) (match-end 0)
  5645. 'display
  5646. (if (equal (char-after (match-beginning 2)) ?^)
  5647. (nth (if table-p 3 1) org-script-display)
  5648. (nth (if table-p 2 0) org-script-display)))
  5649. (add-text-properties (match-beginning 2) (match-end 2)
  5650. (list 'invisible t
  5651. 'org-dwidth t 'org-dwidth-n 1))
  5652. (if (and (eq (char-after (match-beginning 3)) ?{)
  5653. (eq (char-before (match-end 3)) ?}))
  5654. (progn
  5655. (add-text-properties
  5656. (match-beginning 3) (1+ (match-beginning 3))
  5657. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5658. (add-text-properties
  5659. (1- (match-end 3)) (match-end 3)
  5660. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5661. t)))))
  5662. ;;;; Visibility cycling, including org-goto and indirect buffer
  5663. ;;; Cycling
  5664. (defvar org-cycle-global-status nil)
  5665. (make-variable-buffer-local 'org-cycle-global-status)
  5666. (put 'org-cycle-global-status 'org-state t)
  5667. (defvar org-cycle-subtree-status nil)
  5668. (make-variable-buffer-local 'org-cycle-subtree-status)
  5669. (put 'org-cycle-subtree-status 'org-state t)
  5670. (defvar org-inlinetask-min-level)
  5671. ;;;###autoload
  5672. (defun org-cycle (&optional arg)
  5673. "TAB-action and visibility cycling for Org-mode.
  5674. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5675. is outline visibility cycling, but it also invokes other actions
  5676. in special contexts.
  5677. - When this function is called with a prefix argument, rotate the entire
  5678. buffer through 3 states (global cycling)
  5679. 1. OVERVIEW: Show only top-level headlines.
  5680. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5681. 3. SHOW ALL: Show everything.
  5682. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5683. determined by the variable `org-startup-folded', and by any VISIBILITY
  5684. properties in the buffer.
  5685. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5686. including any drawers.
  5687. - When inside a table, re-align the table and move to the next field.
  5688. - When point is at the beginning of a headline, rotate the subtree started
  5689. by this line through 3 different states (local cycling)
  5690. 1. FOLDED: Only the main headline is shown.
  5691. 2. CHILDREN: The main headline and the direct children are shown.
  5692. From this state, you can move to one of the children
  5693. and zoom in further.
  5694. 3. SUBTREE: Show the entire subtree, including body text.
  5695. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5696. - When point is at the beginning of an empty headline and the variable
  5697. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5698. of the headline by demoting and promoting it to likely levels. This
  5699. speeds up creation document structure by pressing TAB once or several
  5700. times right after creating a new headline.
  5701. - When there is a numeric prefix, go up to a heading with level ARG, do
  5702. a `show-subtree' and return to the previous cursor position. If ARG
  5703. is negative, go up that many levels.
  5704. - When point is not at the beginning of a headline, execute the global
  5705. binding for TAB, which is re-indenting the line. See the option
  5706. `org-cycle-emulate-tab' for details.
  5707. - Special case: if point is at the beginning of the buffer and there is
  5708. no headline in line 1, this function will act as if called with prefix arg
  5709. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5710. But only if also the variable `org-cycle-global-at-bob' is t."
  5711. (interactive "P")
  5712. (org-load-modules-maybe)
  5713. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5714. (and org-cycle-level-after-item/entry-creation
  5715. (or (org-cycle-level)
  5716. (org-cycle-item-indentation))))
  5717. (let* (message-log-max ; Don't populate the *Messages* buffer
  5718. (limit-level
  5719. (or org-cycle-max-level
  5720. (and (boundp 'org-inlinetask-min-level)
  5721. org-inlinetask-min-level
  5722. (1- org-inlinetask-min-level))))
  5723. (nstars (and limit-level
  5724. (if org-odd-levels-only
  5725. (and limit-level (1- (* limit-level 2)))
  5726. limit-level)))
  5727. (org-outline-regexp
  5728. (if (not (derived-mode-p 'org-mode))
  5729. outline-regexp
  5730. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5731. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5732. (not (looking-at org-outline-regexp))))
  5733. (org-cycle-hook
  5734. (if bob-special
  5735. (delq 'org-optimize-window-after-visibility-change
  5736. (copy-sequence org-cycle-hook))
  5737. org-cycle-hook))
  5738. (pos (point)))
  5739. (if (or bob-special (equal arg '(4)))
  5740. ;; special case: use global cycling
  5741. (setq arg t))
  5742. (cond
  5743. ((equal arg '(16))
  5744. (setq last-command 'dummy)
  5745. (org-set-startup-visibility)
  5746. (message "Startup visibility, plus VISIBILITY properties"))
  5747. ((equal arg '(64))
  5748. (show-all)
  5749. (message "Entire buffer visible, including drawers"))
  5750. ;; Table: enter it or move to the next field.
  5751. ((org-at-table-p 'any)
  5752. (if (org-at-table.el-p)
  5753. (message "Use C-c ' to edit table.el tables")
  5754. (if arg (org-table-edit-field t)
  5755. (org-table-justify-field-maybe)
  5756. (call-interactively 'org-table-next-field))))
  5757. ((run-hook-with-args-until-success
  5758. 'org-tab-after-check-for-table-hook))
  5759. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5760. ((eq arg t) (org-cycle-internal-global))
  5761. ;; Drawers: delegate to `org-flag-drawer'.
  5762. ((and org-drawers org-drawer-regexp
  5763. (save-excursion
  5764. (beginning-of-line 1)
  5765. (looking-at org-drawer-regexp)))
  5766. (org-flag-drawer ; toggle block visibility
  5767. (not (get-char-property (match-end 0) 'invisible))))
  5768. ;; Show-subtree, ARG levels up from here.
  5769. ((integerp arg)
  5770. (save-excursion
  5771. (org-back-to-heading)
  5772. (outline-up-heading (if (< arg 0) (- arg)
  5773. (- (funcall outline-level) arg)))
  5774. (org-show-subtree)))
  5775. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5776. ((and (featurep 'org-inlinetask)
  5777. (org-inlinetask-at-task-p)
  5778. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5779. (org-inlinetask-toggle-visibility))
  5780. ((org-try-cdlatex-tab))
  5781. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5782. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5783. (save-excursion (beginning-of-line 1)
  5784. (looking-at org-outline-regexp)))
  5785. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5786. (org-cycle-internal-local))
  5787. ;; From there: TAB emulation and template completion.
  5788. (buffer-read-only (org-back-to-heading))
  5789. ((run-hook-with-args-until-success
  5790. 'org-tab-after-check-for-cycling-hook))
  5791. ((org-try-structure-completion))
  5792. ((run-hook-with-args-until-success
  5793. 'org-tab-before-tab-emulation-hook))
  5794. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5795. (or (not (bolp))
  5796. (not (looking-at org-outline-regexp))))
  5797. (call-interactively (global-key-binding "\t")))
  5798. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5799. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5800. (or (and (eq org-cycle-emulate-tab 'white)
  5801. (= (match-end 0) (point-at-eol)))
  5802. (and (eq org-cycle-emulate-tab 'whitestart)
  5803. (>= (match-end 0) pos))))
  5804. t
  5805. (eq org-cycle-emulate-tab t))
  5806. (call-interactively (global-key-binding "\t")))
  5807. (t (save-excursion
  5808. (org-back-to-heading)
  5809. (org-cycle)))))))
  5810. (defun org-cycle-internal-global ()
  5811. "Do the global cycling action."
  5812. ;; Hack to avoid display of messages for .org attachments in Gnus
  5813. (let (message-log-max ; Don't populate the *Messages* buffer
  5814. (ga (string-match "\\*fontification" (buffer-name))))
  5815. (cond
  5816. ((and (eq last-command this-command)
  5817. (eq org-cycle-global-status 'overview))
  5818. ;; We just created the overview - now do table of contents
  5819. ;; This can be slow in very large buffers, so indicate action
  5820. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5821. (unless ga (message "CONTENTS..."))
  5822. (org-content)
  5823. (unless ga (message "CONTENTS...done"))
  5824. (setq org-cycle-global-status 'contents)
  5825. (run-hook-with-args 'org-cycle-hook 'contents))
  5826. ((and (eq last-command this-command)
  5827. (eq org-cycle-global-status 'contents))
  5828. ;; We just showed the table of contents - now show everything
  5829. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5830. (show-all)
  5831. (unless ga (message "SHOW ALL"))
  5832. (setq org-cycle-global-status 'all)
  5833. (run-hook-with-args 'org-cycle-hook 'all))
  5834. (t
  5835. ;; Default action: go to overview
  5836. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5837. (org-overview)
  5838. (unless ga (message "OVERVIEW"))
  5839. (setq org-cycle-global-status 'overview)
  5840. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5841. (defun org-cycle-internal-local ()
  5842. "Do the local cycling action."
  5843. (let (message-log-max ; Don't populate the *Messages* buffer
  5844. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5845. ;; First, determine end of headline (EOH), end of subtree or item
  5846. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5847. (save-excursion
  5848. (if (org-at-item-p)
  5849. (progn
  5850. (beginning-of-line)
  5851. (setq struct (org-list-struct))
  5852. (setq eoh (point-at-eol))
  5853. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5854. (setq has-children (org-list-has-child-p (point) struct)))
  5855. (org-back-to-heading)
  5856. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5857. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5858. (setq has-children
  5859. (or (save-excursion
  5860. (let ((level (funcall outline-level)))
  5861. (outline-next-heading)
  5862. (and (org-at-heading-p t)
  5863. (> (funcall outline-level) level))))
  5864. (save-excursion
  5865. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5866. ;; Determine end invisible part of buffer (EOL)
  5867. (beginning-of-line 2)
  5868. ;; XEmacs doesn't have `next-single-char-property-change'
  5869. (if (featurep 'xemacs)
  5870. (while (and (not (eobp)) ;; this is like `next-line'
  5871. (get-char-property (1- (point)) 'invisible))
  5872. (beginning-of-line 2))
  5873. (while (and (not (eobp)) ;; this is like `next-line'
  5874. (get-char-property (1- (point)) 'invisible))
  5875. (goto-char (next-single-char-property-change (point) 'invisible))
  5876. (and (eolp) (beginning-of-line 2))))
  5877. (setq eol (point)))
  5878. ;; Find out what to do next and set `this-command'
  5879. (cond
  5880. ((= eos eoh)
  5881. ;; Nothing is hidden behind this heading
  5882. (unless (org-before-first-heading-p)
  5883. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5884. (message "EMPTY ENTRY")
  5885. (setq org-cycle-subtree-status nil)
  5886. (save-excursion
  5887. (goto-char eos)
  5888. (outline-next-heading)
  5889. (if (outline-invisible-p) (org-flag-heading nil))))
  5890. ((and (or (>= eol eos)
  5891. (not (string-match "\\S-" (buffer-substring eol eos))))
  5892. (or has-children
  5893. (not (setq children-skipped
  5894. org-cycle-skip-children-state-if-no-children))))
  5895. ;; Entire subtree is hidden in one line: children view
  5896. (unless (org-before-first-heading-p)
  5897. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5898. (if (org-at-item-p)
  5899. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5900. (org-show-entry)
  5901. (org-with-limited-levels (show-children))
  5902. ;; FIXME: This slows down the func way too much.
  5903. ;; How keep drawers hidden in subtree anyway?
  5904. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5905. ;; (org-cycle-hide-drawers 'subtree))
  5906. ;; Fold every list in subtree to top-level items.
  5907. (when (eq org-cycle-include-plain-lists 'integrate)
  5908. (save-excursion
  5909. (org-back-to-heading)
  5910. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5911. (beginning-of-line 1)
  5912. (let* ((struct (org-list-struct))
  5913. (prevs (org-list-prevs-alist struct))
  5914. (end (org-list-get-bottom-point struct)))
  5915. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5916. (org-list-get-all-items (point) struct prevs))
  5917. (goto-char end))))))
  5918. (message "CHILDREN")
  5919. (save-excursion
  5920. (goto-char eos)
  5921. (outline-next-heading)
  5922. (if (outline-invisible-p) (org-flag-heading nil)))
  5923. (setq org-cycle-subtree-status 'children)
  5924. (unless (org-before-first-heading-p)
  5925. (run-hook-with-args 'org-cycle-hook 'children)))
  5926. ((or children-skipped
  5927. (and (eq last-command this-command)
  5928. (eq org-cycle-subtree-status 'children)))
  5929. ;; We just showed the children, or no children are there,
  5930. ;; now show everything.
  5931. (unless (org-before-first-heading-p)
  5932. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5933. (outline-flag-region eoh eos nil)
  5934. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5935. (setq org-cycle-subtree-status 'subtree)
  5936. (unless (org-before-first-heading-p)
  5937. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5938. (t
  5939. ;; Default action: hide the subtree.
  5940. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5941. (outline-flag-region eoh eos t)
  5942. (message "FOLDED")
  5943. (setq org-cycle-subtree-status 'folded)
  5944. (unless (org-before-first-heading-p)
  5945. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5946. ;;;###autoload
  5947. (defun org-global-cycle (&optional arg)
  5948. "Cycle the global visibility. For details see `org-cycle'.
  5949. With \\[universal-argument] prefix arg, switch to startup visibility.
  5950. With a numeric prefix, show all headlines up to that level."
  5951. (interactive "P")
  5952. (let ((org-cycle-include-plain-lists
  5953. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5954. (cond
  5955. ((integerp arg)
  5956. (show-all)
  5957. (hide-sublevels arg)
  5958. (setq org-cycle-global-status 'contents))
  5959. ((equal arg '(4))
  5960. (org-set-startup-visibility)
  5961. (message "Startup visibility, plus VISIBILITY properties."))
  5962. (t
  5963. (org-cycle '(4))))))
  5964. (defun org-set-startup-visibility ()
  5965. "Set the visibility required by startup options and properties."
  5966. (cond
  5967. ((eq org-startup-folded t)
  5968. (org-cycle '(4)))
  5969. ((eq org-startup-folded 'content)
  5970. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5971. (org-cycle '(4)) (org-cycle '(4)))))
  5972. (unless (eq org-startup-folded 'showeverything)
  5973. (if org-hide-block-startup (org-hide-block-all))
  5974. (org-set-visibility-according-to-property 'no-cleanup)
  5975. (org-cycle-hide-archived-subtrees 'all)
  5976. (org-cycle-hide-drawers 'all)
  5977. (org-cycle-show-empty-lines t)))
  5978. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5979. "Switch subtree visibilities according to :VISIBILITY: property."
  5980. (interactive)
  5981. (let (org-show-entry-below state)
  5982. (save-excursion
  5983. (goto-char (point-min))
  5984. (while (re-search-forward
  5985. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5986. nil t)
  5987. (setq state (match-string 1))
  5988. (save-excursion
  5989. (org-back-to-heading t)
  5990. (hide-subtree)
  5991. (org-reveal)
  5992. (cond
  5993. ((equal state '("fold" "folded"))
  5994. (hide-subtree))
  5995. ((equal state "children")
  5996. (org-show-hidden-entry)
  5997. (show-children))
  5998. ((equal state "content")
  5999. (save-excursion
  6000. (save-restriction
  6001. (org-narrow-to-subtree)
  6002. (org-content))))
  6003. ((member state '("all" "showall"))
  6004. (show-subtree)))))
  6005. (unless no-cleanup
  6006. (org-cycle-hide-archived-subtrees 'all)
  6007. (org-cycle-hide-drawers 'all)
  6008. (org-cycle-show-empty-lines 'all)))))
  6009. ;; This function uses outline-regexp instead of the more fundamental
  6010. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6011. ;; buffers, where outline-regexp is needed.
  6012. (defun org-overview ()
  6013. "Switch to overview mode, showing only top-level headlines.
  6014. Really, this shows all headlines with level equal or greater than the level
  6015. of the first headline in the buffer. This is important, because if the
  6016. first headline is not level one, then (hide-sublevels 1) gives confusing
  6017. results."
  6018. (interactive)
  6019. (let ((l (org-current-line))
  6020. (level (save-excursion
  6021. (goto-char (point-min))
  6022. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6023. (progn
  6024. (goto-char (match-beginning 0))
  6025. (funcall outline-level))))))
  6026. (and level (hide-sublevels level))
  6027. (recenter '(4))
  6028. (org-goto-line l)))
  6029. (defun org-content (&optional arg)
  6030. "Show all headlines in the buffer, like a table of contents.
  6031. With numerical argument N, show content up to level N."
  6032. (interactive "P")
  6033. (save-excursion
  6034. ;; Visit all headings and show their offspring
  6035. (and (integerp arg) (org-overview))
  6036. (goto-char (point-max))
  6037. (catch 'exit
  6038. (while (and (progn (condition-case nil
  6039. (outline-previous-visible-heading 1)
  6040. (error (goto-char (point-min))))
  6041. t)
  6042. (looking-at org-outline-regexp))
  6043. (if (integerp arg)
  6044. (show-children (1- arg))
  6045. (show-branches))
  6046. (if (bobp) (throw 'exit nil))))))
  6047. (defun org-optimize-window-after-visibility-change (state)
  6048. "Adjust the window after a change in outline visibility.
  6049. This function is the default value of the hook `org-cycle-hook'."
  6050. (when (get-buffer-window (current-buffer))
  6051. (cond
  6052. ((eq state 'content) nil)
  6053. ((eq state 'all) nil)
  6054. ((eq state 'folded) nil)
  6055. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6056. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6057. (defun org-remove-empty-overlays-at (pos)
  6058. "Remove outline overlays that do not contain non-white stuff."
  6059. (mapc
  6060. (lambda (o)
  6061. (and (eq 'outline (overlay-get o 'invisible))
  6062. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6063. (overlay-end o))))
  6064. (delete-overlay o)))
  6065. (overlays-at pos)))
  6066. (defun org-clean-visibility-after-subtree-move ()
  6067. "Fix visibility issues after moving a subtree."
  6068. ;; First, find a reasonable region to look at:
  6069. ;; Start two siblings above, end three below
  6070. (let* ((beg (save-excursion
  6071. (and (org-get-last-sibling)
  6072. (org-get-last-sibling))
  6073. (point)))
  6074. (end (save-excursion
  6075. (and (org-get-next-sibling)
  6076. (org-get-next-sibling)
  6077. (org-get-next-sibling))
  6078. (if (org-at-heading-p)
  6079. (point-at-eol)
  6080. (point))))
  6081. (level (looking-at "\\*+"))
  6082. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6083. (save-excursion
  6084. (save-restriction
  6085. (narrow-to-region beg end)
  6086. (when re
  6087. ;; Properly fold already folded siblings
  6088. (goto-char (point-min))
  6089. (while (re-search-forward re nil t)
  6090. (if (and (not (outline-invisible-p))
  6091. (save-excursion
  6092. (goto-char (point-at-eol)) (outline-invisible-p)))
  6093. (hide-entry))))
  6094. (org-cycle-show-empty-lines 'overview)
  6095. (org-cycle-hide-drawers 'overview)))))
  6096. (defun org-cycle-show-empty-lines (state)
  6097. "Show empty lines above all visible headlines.
  6098. The region to be covered depends on STATE when called through
  6099. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6100. entire buffer covered. Note that an empty line is only shown if there
  6101. are at least `org-cycle-separator-lines' empty lines before the headline."
  6102. (when (not (= org-cycle-separator-lines 0))
  6103. (save-excursion
  6104. (let* ((n (abs org-cycle-separator-lines))
  6105. (re (cond
  6106. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6107. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6108. (t (let ((ns (number-to-string (- n 2))))
  6109. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6110. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6111. beg end b e)
  6112. (cond
  6113. ((memq state '(overview contents t))
  6114. (setq beg (point-min) end (point-max)))
  6115. ((memq state '(children folded))
  6116. (setq beg (point) end (progn (org-end-of-subtree t t)
  6117. (beginning-of-line 2)
  6118. (point)))))
  6119. (when beg
  6120. (goto-char beg)
  6121. (while (re-search-forward re end t)
  6122. (unless (get-char-property (match-end 1) 'invisible)
  6123. (setq e (match-end 1))
  6124. (if (< org-cycle-separator-lines 0)
  6125. (setq b (save-excursion
  6126. (goto-char (match-beginning 0))
  6127. (org-back-over-empty-lines)
  6128. (if (save-excursion
  6129. (goto-char (max (point-min) (1- (point))))
  6130. (org-at-heading-p))
  6131. (1- (point))
  6132. (point))))
  6133. (setq b (match-beginning 1)))
  6134. (outline-flag-region b e nil)))))))
  6135. ;; Never hide empty lines at the end of the file.
  6136. (save-excursion
  6137. (goto-char (point-max))
  6138. (outline-previous-heading)
  6139. (outline-end-of-heading)
  6140. (if (and (looking-at "[ \t\n]+")
  6141. (= (match-end 0) (point-max)))
  6142. (outline-flag-region (point) (match-end 0) nil))))
  6143. (defun org-show-empty-lines-in-parent ()
  6144. "Move to the parent and re-show empty lines before visible headlines."
  6145. (save-excursion
  6146. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6147. (org-cycle-show-empty-lines context))))
  6148. (defun org-files-list ()
  6149. "Return `org-agenda-files' list, plus all open org-mode files.
  6150. This is useful for operations that need to scan all of a user's
  6151. open and agenda-wise Org files."
  6152. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6153. (dolist (buf (buffer-list))
  6154. (with-current-buffer buf
  6155. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6156. (let ((file (expand-file-name (buffer-file-name))))
  6157. (unless (member file files)
  6158. (push file files))))))
  6159. files))
  6160. (defsubst org-entry-beginning-position ()
  6161. "Return the beginning position of the current entry."
  6162. (save-excursion (outline-back-to-heading t) (point)))
  6163. (defsubst org-entry-end-position ()
  6164. "Return the end position of the current entry."
  6165. (save-excursion (outline-next-heading) (point)))
  6166. (defun org-cycle-hide-drawers (state)
  6167. "Re-hide all drawers after a visibility state change."
  6168. (when (and (derived-mode-p 'org-mode)
  6169. (not (memq state '(overview folded contents))))
  6170. (save-excursion
  6171. (let* ((globalp (memq state '(contents all)))
  6172. (beg (if globalp (point-min) (point)))
  6173. (end (if globalp (point-max)
  6174. (if (eq state 'children)
  6175. (save-excursion (outline-next-heading) (point))
  6176. (org-end-of-subtree t)))))
  6177. (goto-char beg)
  6178. (while (re-search-forward org-drawer-regexp end t)
  6179. (org-flag-drawer t))))))
  6180. (defun org-cycle-hide-inline-tasks (state)
  6181. "Re-hide inline task when switching to 'contents visibility state."
  6182. (when (and (eq state 'contents)
  6183. (boundp 'org-inlinetask-min-level)
  6184. org-inlinetask-min-level)
  6185. (hide-sublevels (1- org-inlinetask-min-level))))
  6186. (defun org-flag-drawer (flag)
  6187. "When FLAG is non-nil, hide the drawer we are within.
  6188. Otherwise make it visible."
  6189. (save-excursion
  6190. (beginning-of-line 1)
  6191. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6192. (let ((b (match-end 0)))
  6193. (if (re-search-forward
  6194. "^[ \t]*:END:"
  6195. (save-excursion (outline-next-heading) (point)) t)
  6196. (outline-flag-region b (point-at-eol) flag)
  6197. (error ":END: line missing at position %s" b))))))
  6198. (defun org-subtree-end-visible-p ()
  6199. "Is the end of the current subtree visible?"
  6200. (pos-visible-in-window-p
  6201. (save-excursion (org-end-of-subtree t) (point))))
  6202. (defun org-first-headline-recenter (&optional N)
  6203. "Move cursor to the first headline and recenter the headline.
  6204. Optional argument N means put the headline into the Nth line of the window."
  6205. (goto-char (point-min))
  6206. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6207. (beginning-of-line)
  6208. (recenter (prefix-numeric-value N))))
  6209. ;;; Saving and restoring visibility
  6210. (defun org-outline-overlay-data (&optional use-markers)
  6211. "Return a list of the locations of all outline overlays.
  6212. These are overlays with the `invisible' property value `outline'.
  6213. The return value is a list of cons cells, with start and stop
  6214. positions for each overlay.
  6215. If USE-MARKERS is set, return the positions as markers."
  6216. (let (beg end)
  6217. (save-excursion
  6218. (save-restriction
  6219. (widen)
  6220. (delq nil
  6221. (mapcar (lambda (o)
  6222. (when (eq (overlay-get o 'invisible) 'outline)
  6223. (setq beg (overlay-start o)
  6224. end (overlay-end o))
  6225. (and beg end (> end beg)
  6226. (if use-markers
  6227. (cons (move-marker (make-marker) beg)
  6228. (move-marker (make-marker) end))
  6229. (cons beg end)))))
  6230. (overlays-in (point-min) (point-max))))))))
  6231. (defun org-set-outline-overlay-data (data)
  6232. "Create visibility overlays for all positions in DATA.
  6233. DATA should have been made by `org-outline-overlay-data'."
  6234. (let (o)
  6235. (save-excursion
  6236. (save-restriction
  6237. (widen)
  6238. (show-all)
  6239. (mapc (lambda (c)
  6240. (outline-flag-region (car c) (cdr c) t))
  6241. data)))))
  6242. ;;; Folding of blocks
  6243. (defvar org-hide-block-overlays nil
  6244. "Overlays hiding blocks.")
  6245. (make-variable-buffer-local 'org-hide-block-overlays)
  6246. (defun org-block-map (function &optional start end)
  6247. "Call FUNCTION at the head of all source blocks in the current buffer.
  6248. Optional arguments START and END can be used to limit the range."
  6249. (let ((start (or start (point-min)))
  6250. (end (or end (point-max))))
  6251. (save-excursion
  6252. (goto-char start)
  6253. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6254. (save-excursion
  6255. (save-match-data
  6256. (goto-char (match-beginning 0))
  6257. (funcall function)))))))
  6258. (defun org-hide-block-toggle-all ()
  6259. "Toggle the visibility of all blocks in the current buffer."
  6260. (org-block-map #'org-hide-block-toggle))
  6261. (defun org-hide-block-all ()
  6262. "Fold all blocks in the current buffer."
  6263. (interactive)
  6264. (org-show-block-all)
  6265. (org-block-map #'org-hide-block-toggle-maybe))
  6266. (defun org-show-block-all ()
  6267. "Unfold all blocks in the current buffer."
  6268. (interactive)
  6269. (mapc 'delete-overlay org-hide-block-overlays)
  6270. (setq org-hide-block-overlays nil))
  6271. (defun org-hide-block-toggle-maybe ()
  6272. "Toggle visibility of block at point."
  6273. (interactive)
  6274. (let ((case-fold-search t))
  6275. (if (save-excursion
  6276. (beginning-of-line 1)
  6277. (looking-at org-block-regexp))
  6278. (progn (org-hide-block-toggle)
  6279. t) ;; to signal that we took action
  6280. nil))) ;; to signal that we did not
  6281. (defun org-hide-block-toggle (&optional force)
  6282. "Toggle the visibility of the current block."
  6283. (interactive)
  6284. (save-excursion
  6285. (beginning-of-line)
  6286. (if (re-search-forward org-block-regexp nil t)
  6287. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6288. (end (match-end 0)) ;; end of entire body
  6289. ov)
  6290. (if (memq t (mapcar (lambda (overlay)
  6291. (eq (overlay-get overlay 'invisible)
  6292. 'org-hide-block))
  6293. (overlays-at start)))
  6294. (if (or (not force) (eq force 'off))
  6295. (mapc (lambda (ov)
  6296. (when (member ov org-hide-block-overlays)
  6297. (setq org-hide-block-overlays
  6298. (delq ov org-hide-block-overlays)))
  6299. (when (eq (overlay-get ov 'invisible)
  6300. 'org-hide-block)
  6301. (delete-overlay ov)))
  6302. (overlays-at start)))
  6303. (setq ov (make-overlay start end))
  6304. (overlay-put ov 'invisible 'org-hide-block)
  6305. ;; make the block accessible to isearch
  6306. (overlay-put
  6307. ov 'isearch-open-invisible
  6308. (lambda (ov)
  6309. (when (member ov org-hide-block-overlays)
  6310. (setq org-hide-block-overlays
  6311. (delq ov org-hide-block-overlays)))
  6312. (when (eq (overlay-get ov 'invisible)
  6313. 'org-hide-block)
  6314. (delete-overlay ov))))
  6315. (push ov org-hide-block-overlays)))
  6316. (error "Not looking at a source block"))))
  6317. ;; org-tab-after-check-for-cycling-hook
  6318. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6319. ;; Remove overlays when changing major mode
  6320. (add-hook 'org-mode-hook
  6321. (lambda () (org-add-hook 'change-major-mode-hook
  6322. 'org-show-block-all 'append 'local)))
  6323. ;;; Org-goto
  6324. (defvar org-goto-window-configuration nil)
  6325. (defvar org-goto-marker nil)
  6326. (defvar org-goto-map)
  6327. (defun org-goto-map ()
  6328. "Set the keymap `org-goto'."
  6329. (setq org-goto-map
  6330. (let ((map (make-sparse-keymap)))
  6331. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6332. mouse-drag-region universal-argument org-occur))
  6333. cmd)
  6334. (while (setq cmd (pop cmds))
  6335. (substitute-key-definition cmd cmd map global-map)))
  6336. (suppress-keymap map)
  6337. (org-defkey map "\C-m" 'org-goto-ret)
  6338. (org-defkey map [(return)] 'org-goto-ret)
  6339. (org-defkey map [(left)] 'org-goto-left)
  6340. (org-defkey map [(right)] 'org-goto-right)
  6341. (org-defkey map [(control ?g)] 'org-goto-quit)
  6342. (org-defkey map "\C-i" 'org-cycle)
  6343. (org-defkey map [(tab)] 'org-cycle)
  6344. (org-defkey map [(down)] 'outline-next-visible-heading)
  6345. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6346. (if org-goto-auto-isearch
  6347. (if (fboundp 'define-key-after)
  6348. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6349. nil)
  6350. (org-defkey map "q" 'org-goto-quit)
  6351. (org-defkey map "n" 'outline-next-visible-heading)
  6352. (org-defkey map "p" 'outline-previous-visible-heading)
  6353. (org-defkey map "f" 'outline-forward-same-level)
  6354. (org-defkey map "b" 'outline-backward-same-level)
  6355. (org-defkey map "u" 'outline-up-heading))
  6356. (org-defkey map "/" 'org-occur)
  6357. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6358. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6359. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6360. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6361. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6362. map)))
  6363. (defconst org-goto-help
  6364. "Browse buffer copy, to find location or copy text.%s
  6365. RET=jump to location C-g=quit and return to previous location
  6366. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6367. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6368. ;; FIXME: Docstring does not mention both interfaces
  6369. (defun org-goto (&optional alternative-interface)
  6370. "Look up a different location in the current file, keeping current visibility.
  6371. When you want look-up or go to a different location in a
  6372. document, the fastest way is often to fold the entire buffer and
  6373. then dive into the tree. This method has the disadvantage, that
  6374. the previous location will be folded, which may not be what you
  6375. want.
  6376. This command works around this by showing a copy of the current
  6377. buffer in an indirect buffer, in overview mode. You can dive
  6378. into the tree in that copy, use org-occur and incremental search
  6379. to find a location. When pressing RET or `Q', the command
  6380. returns to the original buffer in which the visibility is still
  6381. unchanged. After RET it will also jump to the location selected
  6382. in the indirect buffer and expose the headline hierarchy above.
  6383. With a prefix argument, use the alternative interface: e.g. if
  6384. `org-goto-interface' is 'outline use 'outline-path-completion."
  6385. (interactive "P")
  6386. (org-goto-map)
  6387. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6388. (org-refile-use-outline-path t)
  6389. (org-refile-target-verify-function nil)
  6390. (interface
  6391. (if (not alternative-interface)
  6392. org-goto-interface
  6393. (if (eq org-goto-interface 'outline)
  6394. 'outline-path-completion
  6395. 'outline)))
  6396. (org-goto-start-pos (point))
  6397. (selected-point
  6398. (if (eq interface 'outline)
  6399. (car (org-get-location (current-buffer) org-goto-help))
  6400. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6401. (org-refile-check-position pa)
  6402. (nth 3 pa)))))
  6403. (if selected-point
  6404. (progn
  6405. (org-mark-ring-push org-goto-start-pos)
  6406. (goto-char selected-point)
  6407. (if (or (outline-invisible-p) (org-invisible-p2))
  6408. (org-show-context 'org-goto)))
  6409. (message "Quit"))))
  6410. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6411. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6412. (defvar org-goto-local-auto-isearch-map) ; defined below
  6413. (defun org-get-location (buf help)
  6414. "Let the user select a location in the Org-mode buffer BUF.
  6415. This function uses a recursive edit. It returns the selected position
  6416. or nil."
  6417. (org-no-popups
  6418. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6419. (isearch-hide-immediately nil)
  6420. (isearch-search-fun-function
  6421. (lambda () 'org-goto-local-search-headings))
  6422. (org-goto-selected-point org-goto-exit-command))
  6423. (save-excursion
  6424. (save-window-excursion
  6425. (delete-other-windows)
  6426. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6427. (org-pop-to-buffer-same-window
  6428. (condition-case nil
  6429. (make-indirect-buffer (current-buffer) "*org-goto*")
  6430. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6431. (with-output-to-temp-buffer "*Org Help*"
  6432. (princ (format help (if org-goto-auto-isearch
  6433. " Just type for auto-isearch."
  6434. " n/p/f/b/u to navigate, q to quit."))))
  6435. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6436. (setq buffer-read-only nil)
  6437. (let ((org-startup-truncated t)
  6438. (org-startup-folded nil)
  6439. (org-startup-align-all-tables nil))
  6440. (org-mode)
  6441. (org-overview))
  6442. (setq buffer-read-only t)
  6443. (if (and (boundp 'org-goto-start-pos)
  6444. (integer-or-marker-p org-goto-start-pos))
  6445. (let ((org-show-hierarchy-above t)
  6446. (org-show-siblings t)
  6447. (org-show-following-heading t))
  6448. (goto-char org-goto-start-pos)
  6449. (and (outline-invisible-p) (org-show-context)))
  6450. (goto-char (point-min)))
  6451. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6452. (message "Select location and press RET")
  6453. (use-local-map org-goto-map)
  6454. (recursive-edit)))
  6455. (kill-buffer "*org-goto*")
  6456. (cons org-goto-selected-point org-goto-exit-command))))
  6457. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6458. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6459. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6460. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6461. (defun org-goto-local-search-headings (string bound noerror)
  6462. "Search and make sure that any matches are in headlines."
  6463. (catch 'return
  6464. (while (if isearch-forward
  6465. (search-forward string bound noerror)
  6466. (search-backward string bound noerror))
  6467. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6468. (and (member :headline context)
  6469. (not (member :tags context))))
  6470. (throw 'return (point))))))
  6471. (defun org-goto-local-auto-isearch ()
  6472. "Start isearch."
  6473. (interactive)
  6474. (goto-char (point-min))
  6475. (let ((keys (this-command-keys)))
  6476. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6477. (isearch-mode t)
  6478. (isearch-process-search-char (string-to-char keys)))))
  6479. (defun org-goto-ret (&optional arg)
  6480. "Finish `org-goto' by going to the new location."
  6481. (interactive "P")
  6482. (setq org-goto-selected-point (point)
  6483. org-goto-exit-command 'return)
  6484. (throw 'exit nil))
  6485. (defun org-goto-left ()
  6486. "Finish `org-goto' by going to the new location."
  6487. (interactive)
  6488. (if (org-at-heading-p)
  6489. (progn
  6490. (beginning-of-line 1)
  6491. (setq org-goto-selected-point (point)
  6492. org-goto-exit-command 'left)
  6493. (throw 'exit nil))
  6494. (error "Not on a heading")))
  6495. (defun org-goto-right ()
  6496. "Finish `org-goto' by going to the new location."
  6497. (interactive)
  6498. (if (org-at-heading-p)
  6499. (progn
  6500. (setq org-goto-selected-point (point)
  6501. org-goto-exit-command 'right)
  6502. (throw 'exit nil))
  6503. (error "Not on a heading")))
  6504. (defun org-goto-quit ()
  6505. "Finish `org-goto' without cursor motion."
  6506. (interactive)
  6507. (setq org-goto-selected-point nil)
  6508. (setq org-goto-exit-command 'quit)
  6509. (throw 'exit nil))
  6510. ;;; Indirect buffer display of subtrees
  6511. (defvar org-indirect-dedicated-frame nil
  6512. "This is the frame being used for indirect tree display.")
  6513. (defvar org-last-indirect-buffer nil)
  6514. (defun org-tree-to-indirect-buffer (&optional arg)
  6515. "Create indirect buffer and narrow it to current subtree.
  6516. With a numerical prefix ARG, go up to this level and then take that tree.
  6517. If ARG is negative, go up that many levels.
  6518. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6519. indirect buffer previously made with this command, to avoid proliferation of
  6520. indirect buffers. However, when you call the command with a \
  6521. \\[universal-argument] prefix, or
  6522. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6523. is kept so that you can work with several indirect buffers at the same time.
  6524. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6525. \\[universal-argument] prefix also
  6526. requests that a new frame be made for the new buffer, so that the dedicated
  6527. frame is not changed."
  6528. (interactive "P")
  6529. (let ((cbuf (current-buffer))
  6530. (cwin (selected-window))
  6531. (pos (point))
  6532. beg end level heading ibuf)
  6533. (save-excursion
  6534. (org-back-to-heading t)
  6535. (when (numberp arg)
  6536. (setq level (org-outline-level))
  6537. (if (< arg 0) (setq arg (+ level arg)))
  6538. (while (> (setq level (org-outline-level)) arg)
  6539. (org-up-heading-safe)))
  6540. (setq beg (point)
  6541. heading (org-get-heading))
  6542. (org-end-of-subtree t t)
  6543. (if (org-at-heading-p) (backward-char 1))
  6544. (setq end (point)))
  6545. (if (and (buffer-live-p org-last-indirect-buffer)
  6546. (not (eq org-indirect-buffer-display 'new-frame))
  6547. (not arg))
  6548. (kill-buffer org-last-indirect-buffer))
  6549. (setq ibuf (org-get-indirect-buffer cbuf)
  6550. org-last-indirect-buffer ibuf)
  6551. (cond
  6552. ((or (eq org-indirect-buffer-display 'new-frame)
  6553. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6554. (select-frame (make-frame))
  6555. (delete-other-windows)
  6556. (org-pop-to-buffer-same-window ibuf)
  6557. (org-set-frame-title heading))
  6558. ((eq org-indirect-buffer-display 'dedicated-frame)
  6559. (raise-frame
  6560. (select-frame (or (and org-indirect-dedicated-frame
  6561. (frame-live-p org-indirect-dedicated-frame)
  6562. org-indirect-dedicated-frame)
  6563. (setq org-indirect-dedicated-frame (make-frame)))))
  6564. (delete-other-windows)
  6565. (org-pop-to-buffer-same-window ibuf)
  6566. (org-set-frame-title (concat "Indirect: " heading)))
  6567. ((eq org-indirect-buffer-display 'current-window)
  6568. (org-pop-to-buffer-same-window ibuf))
  6569. ((eq org-indirect-buffer-display 'other-window)
  6570. (pop-to-buffer ibuf))
  6571. (t (error "Invalid value")))
  6572. (if (featurep 'xemacs)
  6573. (save-excursion (org-mode) (turn-on-font-lock)))
  6574. (narrow-to-region beg end)
  6575. (show-all)
  6576. (goto-char pos)
  6577. (run-hook-with-args 'org-cycle-hook 'all)
  6578. (and (window-live-p cwin) (select-window cwin))))
  6579. (defun org-get-indirect-buffer (&optional buffer)
  6580. (setq buffer (or buffer (current-buffer)))
  6581. (let ((n 1) (base (buffer-name buffer)) bname)
  6582. (while (buffer-live-p
  6583. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6584. (setq n (1+ n)))
  6585. (condition-case nil
  6586. (make-indirect-buffer buffer bname 'clone)
  6587. (error (make-indirect-buffer buffer bname)))))
  6588. (defun org-set-frame-title (title)
  6589. "Set the title of the current frame to the string TITLE."
  6590. ;; FIXME: how to name a single frame in XEmacs???
  6591. (unless (featurep 'xemacs)
  6592. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6593. ;;;; Structure editing
  6594. ;;; Inserting headlines
  6595. (defun org-previous-line-empty-p (&optional next)
  6596. "Is the previous line a blank line?
  6597. When NEXT is non-nil, check the next line instead."
  6598. (save-excursion
  6599. (and (not (bobp))
  6600. (or (beginning-of-line (if next 2 0)) t)
  6601. (save-match-data
  6602. (looking-at "[ \t]*$")))))
  6603. (defun org-insert-heading (&optional arg invisible-ok)
  6604. "Insert a new heading or item with same depth at point.
  6605. If point is in a plain list and ARG is nil, create a new list item.
  6606. With one universal prefix argument, insert a heading even in lists.
  6607. With two universal prefix arguments, insert the heading at the end
  6608. of the parent subtree.
  6609. If point is at the beginning of a headline, insert a sibling before
  6610. the current headline. If point is not at the beginning, split the line
  6611. and create a new headline with the text in the current line after point
  6612. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6613. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6614. This is important for non-interactive uses of the command."
  6615. (interactive "P")
  6616. (cond
  6617. ((or (= (buffer-size) 0)
  6618. (and (not (save-excursion
  6619. (and (ignore-errors (org-back-to-heading invisible-ok))
  6620. (org-at-heading-p))))
  6621. (or arg (not (org-in-item-p)))))
  6622. (insert
  6623. (if (org-previous-line-empty-p) "" "\n")
  6624. (if (org-in-src-block-p) ",* " "* "))
  6625. (run-hooks 'org-insert-heading-hook))
  6626. ((or arg (not (org-insert-item
  6627. (save-excursion
  6628. (beginning-of-line)
  6629. (looking-at org-list-full-item-re)
  6630. (match-string 3)))))
  6631. (let (begn endn)
  6632. (when (org-buffer-narrowed-p)
  6633. (setq begn (point-min) endn (point-max))
  6634. (widen))
  6635. (let* ((empty-line-p nil)
  6636. (eops (equal arg '(16))) ; insert at end of parent subtree
  6637. (org-insert-heading-respect-content
  6638. (or (not (null arg)) org-insert-heading-respect-content))
  6639. (level nil)
  6640. (on-heading (org-at-heading-p))
  6641. (head (save-excursion
  6642. (condition-case nil
  6643. (progn
  6644. (org-back-to-heading invisible-ok)
  6645. (when (and (not on-heading)
  6646. (featurep 'org-inlinetask)
  6647. (integerp org-inlinetask-min-level)
  6648. (>= (length (match-string 0))
  6649. org-inlinetask-min-level))
  6650. ;; Find a heading level before the inline task
  6651. (while (and (setq level (org-up-heading-safe))
  6652. (>= level org-inlinetask-min-level)))
  6653. (if (org-at-heading-p)
  6654. (org-back-to-heading invisible-ok)
  6655. (error "This should not happen")))
  6656. (unless (and (save-excursion
  6657. (save-match-data
  6658. (org-backward-heading-same-level 1 invisible-ok))
  6659. (= (point) (match-beginning 0)))
  6660. (not (org-previous-line-empty-p t)))
  6661. (setq empty-line-p (org-previous-line-empty-p)))
  6662. (match-string 0))
  6663. (error "* "))))
  6664. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6665. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6666. pos hide-previous previous-pos)
  6667. (if ;; At the beginning of a heading, open a new line for insertiong
  6668. (and (bolp) (org-at-heading-p)
  6669. (not eops)
  6670. (or (bobp)
  6671. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6672. (open-line (if blank 2 1))
  6673. (save-excursion
  6674. (setq previous-pos (point-at-bol))
  6675. (end-of-line)
  6676. (setq hide-previous (outline-invisible-p)))
  6677. (and org-insert-heading-respect-content
  6678. (save-excursion
  6679. (while (outline-invisible-p)
  6680. (org-show-subtree)
  6681. (org-up-heading-safe))))
  6682. (let ((split
  6683. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6684. (save-excursion
  6685. (let ((p (point)))
  6686. (goto-char (point-at-bol))
  6687. (and (looking-at org-complex-heading-regexp)
  6688. (match-beginning 4)
  6689. (> p (match-beginning 4)))))))
  6690. tags pos)
  6691. (cond
  6692. ;; Insert a new line, possibly at end of parent subtree
  6693. (org-insert-heading-respect-content
  6694. (if (not eops)
  6695. (progn
  6696. (org-end-of-subtree nil t)
  6697. (and (looking-at "^\\*") (backward-char 1))
  6698. (while (and (not (bobp))
  6699. ;; Don't delete spaces in empty headlines
  6700. (not (looking-back org-outline-regexp))
  6701. (member (char-before) '(?\ ?\t ?\n)))
  6702. (backward-delete-char 1)))
  6703. (let ((p (point)))
  6704. (org-up-heading-safe)
  6705. (if (= p (point))
  6706. (goto-char (point-max))
  6707. (org-end-of-subtree nil t))))
  6708. (when (featurep 'org-inlinetask)
  6709. (while (and (not (eobp))
  6710. (looking-at "\\(\\*+\\)[ \t]+")
  6711. (>= (length (match-string 1))
  6712. org-inlinetask-min-level))
  6713. (org-end-of-subtree nil t)))
  6714. (or (bolp) (newline))
  6715. (or (org-previous-line-empty-p)
  6716. (and blank (newline)))
  6717. (if (or empty-line-p eops) (open-line 1)))
  6718. ;; Insert a headling containing text after point
  6719. ((org-at-heading-p)
  6720. (when hide-previous
  6721. (show-children)
  6722. (org-show-entry))
  6723. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6724. (setq tags (and (match-end 2) (match-string 2)))
  6725. (and (match-end 1)
  6726. (delete-region (match-beginning 1) (match-end 1)))
  6727. (setq pos (point-at-bol))
  6728. (or split (end-of-line 1))
  6729. (delete-horizontal-space)
  6730. (if (string-match "\\`\\*+\\'"
  6731. (buffer-substring (point-at-bol) (point)))
  6732. (insert " "))
  6733. (newline (if blank 2 1))
  6734. (when tags
  6735. (save-excursion
  6736. (goto-char pos)
  6737. (end-of-line 1)
  6738. (insert " " tags)
  6739. (org-set-tags nil 'align))))
  6740. (t
  6741. (or split (end-of-line 1))
  6742. (newline (if blank 2 1))))))
  6743. (insert head) (just-one-space)
  6744. (setq pos (point))
  6745. (end-of-line 1)
  6746. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6747. (when (and org-insert-heading-respect-content hide-previous)
  6748. (save-excursion
  6749. (goto-char previous-pos)
  6750. (hide-subtree)))
  6751. (when (and begn endn)
  6752. (narrow-to-region (min (point) begn) (max (point) endn)))
  6753. (run-hooks 'org-insert-heading-hook))))))
  6754. (defun org-get-heading (&optional no-tags no-todo)
  6755. "Return the heading of the current entry, without the stars.
  6756. When NO-TAGS is non-nil, don't include tags.
  6757. When NO-TODO is non-nil, don't include TODO keywords."
  6758. (save-excursion
  6759. (org-back-to-heading t)
  6760. (cond
  6761. ((and no-tags no-todo)
  6762. (looking-at org-complex-heading-regexp)
  6763. (match-string 4))
  6764. (no-tags
  6765. (looking-at (concat org-outline-regexp
  6766. "\\(.*?\\)"
  6767. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6768. (match-string 1))
  6769. (no-todo
  6770. (looking-at org-todo-line-regexp)
  6771. (match-string 3))
  6772. (t (looking-at org-heading-regexp)
  6773. (match-string 2)))))
  6774. (defvar orgstruct-mode) ; defined below
  6775. (defun org-heading-components ()
  6776. "Return the components of the current heading.
  6777. This is a list with the following elements:
  6778. - the level as an integer
  6779. - the reduced level, different if `org-odd-levels-only' is set.
  6780. - the TODO keyword, or nil
  6781. - the priority character, like ?A, or nil if no priority is given
  6782. - the headline text itself, or the tags string if no headline text
  6783. - the tags string, or nil."
  6784. (save-excursion
  6785. (org-back-to-heading t)
  6786. (if (let (case-fold-search)
  6787. (looking-at
  6788. (if orgstruct-mode
  6789. org-heading-regexp
  6790. org-complex-heading-regexp)))
  6791. (if orgstruct-mode
  6792. (list (length (match-string 1))
  6793. (org-reduced-level (length (match-string 1)))
  6794. nil
  6795. nil
  6796. (match-string 2)
  6797. nil)
  6798. (list (length (match-string 1))
  6799. (org-reduced-level (length (match-string 1)))
  6800. (org-match-string-no-properties 2)
  6801. (and (match-end 3) (aref (match-string 3) 2))
  6802. (org-match-string-no-properties 4)
  6803. (org-match-string-no-properties 5))))))
  6804. (defun org-get-entry ()
  6805. "Get the entry text, after heading, entire subtree."
  6806. (save-excursion
  6807. (org-back-to-heading t)
  6808. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6809. (defun org-insert-heading-after-current ()
  6810. "Insert a new heading with same level as current, after current subtree."
  6811. (interactive)
  6812. (org-back-to-heading)
  6813. (org-insert-heading)
  6814. (org-move-subtree-down)
  6815. (end-of-line 1))
  6816. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6817. "Insert heading with `org-insert-heading-respect-content' set to t."
  6818. (interactive "P")
  6819. (let ((org-insert-heading-respect-content t))
  6820. (org-insert-heading arg invisible-ok)))
  6821. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6822. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6823. (interactive "P")
  6824. (let ((org-insert-heading-respect-content t))
  6825. (org-insert-todo-heading force-state t)))
  6826. (defun org-insert-todo-heading (arg &optional force-heading)
  6827. "Insert a new heading with the same level and TODO state as current heading.
  6828. If the heading has no TODO state, or if the state is DONE, use the first
  6829. state (TODO by default). Also one prefix arg, force first state. With two
  6830. prefix args, force inserting at the end of the parent subtree."
  6831. (interactive "P")
  6832. (when (or force-heading (not (org-insert-item 'checkbox)))
  6833. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6834. force-heading))
  6835. (save-excursion
  6836. (org-back-to-heading)
  6837. (outline-previous-heading)
  6838. (looking-at org-todo-line-regexp))
  6839. (let*
  6840. ((new-mark-x
  6841. (if (or arg
  6842. (not (match-beginning 2))
  6843. (member (match-string 2) org-done-keywords))
  6844. (car org-todo-keywords-1)
  6845. (match-string 2)))
  6846. (new-mark
  6847. (or
  6848. (run-hook-with-args-until-success
  6849. 'org-todo-get-default-hook new-mark-x nil)
  6850. new-mark-x)))
  6851. (beginning-of-line 1)
  6852. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6853. (if org-treat-insert-todo-heading-as-state-change
  6854. (org-todo new-mark)
  6855. (insert new-mark " "))))
  6856. (when org-provide-todo-statistics
  6857. (org-update-parent-todo-statistics))))
  6858. (defun org-insert-subheading (arg)
  6859. "Insert a new subheading and demote it.
  6860. Works for outline headings and for plain lists alike."
  6861. (interactive "P")
  6862. (org-insert-heading arg)
  6863. (cond
  6864. ((org-at-heading-p) (org-do-demote))
  6865. ((org-at-item-p) (org-indent-item))))
  6866. (defun org-insert-todo-subheading (arg)
  6867. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6868. Works for outline headings and for plain lists alike."
  6869. (interactive "P")
  6870. (org-insert-todo-heading arg)
  6871. (cond
  6872. ((org-at-heading-p) (org-do-demote))
  6873. ((org-at-item-p) (org-indent-item))))
  6874. ;;; Promotion and Demotion
  6875. (defvar org-after-demote-entry-hook nil
  6876. "Hook run after an entry has been demoted.
  6877. The cursor will be at the beginning of the entry.
  6878. When a subtree is being demoted, the hook will be called for each node.")
  6879. (defvar org-after-promote-entry-hook nil
  6880. "Hook run after an entry has been promoted.
  6881. The cursor will be at the beginning of the entry.
  6882. When a subtree is being promoted, the hook will be called for each node.")
  6883. (defun org-promote-subtree ()
  6884. "Promote the entire subtree.
  6885. See also `org-promote'."
  6886. (interactive)
  6887. (save-excursion
  6888. (org-with-limited-levels (org-map-tree 'org-promote)))
  6889. (org-fix-position-after-promote))
  6890. (defun org-demote-subtree ()
  6891. "Demote the entire subtree. See `org-demote'.
  6892. See also `org-promote'."
  6893. (interactive)
  6894. (save-excursion
  6895. (org-with-limited-levels (org-map-tree 'org-demote)))
  6896. (org-fix-position-after-promote))
  6897. (defun org-do-promote ()
  6898. "Promote the current heading higher up the tree.
  6899. If the region is active in `transient-mark-mode', promote all headings
  6900. in the region."
  6901. (interactive)
  6902. (save-excursion
  6903. (if (org-region-active-p)
  6904. (org-map-region 'org-promote (region-beginning) (region-end))
  6905. (org-promote)))
  6906. (org-fix-position-after-promote))
  6907. (defun org-do-demote ()
  6908. "Demote the current heading lower down the tree.
  6909. If the region is active in `transient-mark-mode', demote all headings
  6910. in the region."
  6911. (interactive)
  6912. (save-excursion
  6913. (if (org-region-active-p)
  6914. (org-map-region 'org-demote (region-beginning) (region-end))
  6915. (org-demote)))
  6916. (org-fix-position-after-promote))
  6917. (defun org-fix-position-after-promote ()
  6918. "Make sure that after pro/demotion cursor position is right."
  6919. (let ((pos (point)))
  6920. (when (save-excursion
  6921. (beginning-of-line 1)
  6922. (looking-at org-todo-line-regexp)
  6923. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6924. (cond ((eobp) (insert " "))
  6925. ((eolp) (insert " "))
  6926. ((equal (char-after) ?\ ) (forward-char 1))))))
  6927. (defun org-current-level ()
  6928. "Return the level of the current entry, or nil if before the first headline.
  6929. The level is the number of stars at the beginning of the headline."
  6930. (save-excursion
  6931. (org-with-limited-levels
  6932. (if (ignore-errors (org-back-to-heading t))
  6933. (funcall outline-level)))))
  6934. (defun org-get-previous-line-level ()
  6935. "Return the outline depth of the last headline before the current line.
  6936. Returns 0 for the first headline in the buffer, and nil if before the
  6937. first headline."
  6938. (let ((current-level (org-current-level))
  6939. (prev-level (when (> (line-number-at-pos) 1)
  6940. (save-excursion
  6941. (beginning-of-line 0)
  6942. (org-current-level)))))
  6943. (cond ((null current-level) nil) ; Before first headline
  6944. ((null prev-level) 0) ; At first headline
  6945. (prev-level))))
  6946. (defun org-reduced-level (l)
  6947. "Compute the effective level of a heading.
  6948. This takes into account the setting of `org-odd-levels-only'."
  6949. (cond
  6950. ((zerop l) 0)
  6951. (org-odd-levels-only (1+ (floor (/ l 2))))
  6952. (t l)))
  6953. (defun org-level-increment ()
  6954. "Return the number of stars that will be added or removed at a
  6955. time to headlines when structure editing, based on the value of
  6956. `org-odd-levels-only'."
  6957. (if org-odd-levels-only 2 1))
  6958. (defun org-get-valid-level (level &optional change)
  6959. "Rectify a level change under the influence of `org-odd-levels-only'
  6960. LEVEL is a current level, CHANGE is by how much the level should be
  6961. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6962. even level numbers will become the next higher odd number."
  6963. (if org-odd-levels-only
  6964. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6965. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6966. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6967. (max 1 (+ level (or change 0)))))
  6968. (if (boundp 'define-obsolete-function-alias)
  6969. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6970. (define-obsolete-function-alias 'org-get-legal-level
  6971. 'org-get-valid-level)
  6972. (define-obsolete-function-alias 'org-get-legal-level
  6973. 'org-get-valid-level "23.1")))
  6974. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6975. ;; ̀org-with-limited-levels'
  6976. (defun org-promote ()
  6977. "Promote the current heading higher up the tree.
  6978. If the region is active in `transient-mark-mode', promote all headings
  6979. in the region."
  6980. (org-back-to-heading t)
  6981. (let* ((level (save-match-data (funcall outline-level)))
  6982. (after-change-functions (remove 'flyspell-after-change-function
  6983. after-change-functions))
  6984. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6985. (diff (abs (- level (length up-head) -1))))
  6986. (cond ((and (= level 1) org-called-with-limited-levels
  6987. org-allow-promoting-top-level-subtree)
  6988. (replace-match "# " nil t))
  6989. ((= level 1)
  6990. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6991. (t (replace-match up-head nil t)))
  6992. ;; Fixup tag positioning
  6993. (unless (= level 1)
  6994. (and org-auto-align-tags (org-set-tags nil t))
  6995. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6996. (run-hooks 'org-after-promote-entry-hook)))
  6997. (defun org-demote ()
  6998. "Demote the current heading lower down the tree.
  6999. If the region is active in `transient-mark-mode', demote all headings
  7000. in the region."
  7001. (org-back-to-heading t)
  7002. (let* ((level (save-match-data (funcall outline-level)))
  7003. (after-change-functions (remove 'flyspell-after-change-function
  7004. after-change-functions))
  7005. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7006. (diff (abs (- level (length down-head) -1))))
  7007. (replace-match down-head nil t)
  7008. ;; Fixup tag positioning
  7009. (and org-auto-align-tags (org-set-tags nil t))
  7010. (if org-adapt-indentation (org-fixup-indentation diff))
  7011. (run-hooks 'org-after-demote-entry-hook)))
  7012. (defun org-cycle-level ()
  7013. "Cycle the level of an empty headline through possible states.
  7014. This goes first to child, then to parent, level, then up the hierarchy.
  7015. After top level, it switches back to sibling level."
  7016. (interactive)
  7017. (let ((org-adapt-indentation nil))
  7018. (when (org-point-at-end-of-empty-headline)
  7019. (setq this-command 'org-cycle-level) ; Only needed for caching
  7020. (let ((cur-level (org-current-level))
  7021. (prev-level (org-get-previous-line-level)))
  7022. (cond
  7023. ;; If first headline in file, promote to top-level.
  7024. ((= prev-level 0)
  7025. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7026. do (org-do-promote)))
  7027. ;; If same level as prev, demote one.
  7028. ((= prev-level cur-level)
  7029. (org-do-demote))
  7030. ;; If parent is top-level, promote to top level if not already.
  7031. ((= prev-level 1)
  7032. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7033. do (org-do-promote)))
  7034. ;; If top-level, return to prev-level.
  7035. ((= cur-level 1)
  7036. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7037. do (org-do-demote)))
  7038. ;; If less than prev-level, promote one.
  7039. ((< cur-level prev-level)
  7040. (org-do-promote))
  7041. ;; If deeper than prev-level, promote until higher than
  7042. ;; prev-level.
  7043. ((> cur-level prev-level)
  7044. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7045. do (org-do-promote))))
  7046. t))))
  7047. (defun org-map-tree (fun)
  7048. "Call FUN for every heading underneath the current one."
  7049. (org-back-to-heading)
  7050. (let ((level (funcall outline-level)))
  7051. (save-excursion
  7052. (funcall fun)
  7053. (while (and (progn
  7054. (outline-next-heading)
  7055. (> (funcall outline-level) level))
  7056. (not (eobp)))
  7057. (funcall fun)))))
  7058. (defun org-map-region (fun beg end)
  7059. "Call FUN for every heading between BEG and END."
  7060. (let ((org-ignore-region t))
  7061. (save-excursion
  7062. (setq end (copy-marker end))
  7063. (goto-char beg)
  7064. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7065. (< (point) end))
  7066. (funcall fun))
  7067. (while (and (progn
  7068. (outline-next-heading)
  7069. (< (point) end))
  7070. (not (eobp)))
  7071. (funcall fun)))))
  7072. (defvar org-property-end-re) ; silence byte-compiler
  7073. (defun org-fixup-indentation (diff)
  7074. "Change the indentation in the current entry by DIFF.
  7075. However, if any line in the current entry has no indentation, or if it
  7076. would end up with no indentation after the change, nothing at all is done."
  7077. (save-excursion
  7078. (let ((end (save-excursion (outline-next-heading)
  7079. (point-marker)))
  7080. (prohibit (if (> diff 0)
  7081. "^\\S-"
  7082. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7083. col)
  7084. (unless (save-excursion (end-of-line 1)
  7085. (re-search-forward prohibit end t))
  7086. (while (and (< (point) end)
  7087. (re-search-forward "^[ \t]+" end t))
  7088. (goto-char (match-end 0))
  7089. (setq col (current-column))
  7090. (if (< diff 0) (replace-match ""))
  7091. (org-indent-to-column (+ diff col))))
  7092. (move-marker end nil))))
  7093. (defun org-convert-to-odd-levels ()
  7094. "Convert an org-mode file with all levels allowed to one with odd levels.
  7095. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7096. level 5 etc."
  7097. (interactive)
  7098. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7099. (let ((outline-level 'org-outline-level)
  7100. (org-odd-levels-only nil) n)
  7101. (save-excursion
  7102. (goto-char (point-min))
  7103. (while (re-search-forward "^\\*\\*+ " nil t)
  7104. (setq n (- (length (match-string 0)) 2))
  7105. (while (>= (setq n (1- n)) 0)
  7106. (org-demote))
  7107. (end-of-line 1))))))
  7108. (defun org-convert-to-oddeven-levels ()
  7109. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7110. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7111. file contains a section with an even level, conversion would
  7112. destroy the structure of the file. An error is signaled in this
  7113. case."
  7114. (interactive)
  7115. (goto-char (point-min))
  7116. ;; First check if there are no even levels
  7117. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7118. (org-show-context t)
  7119. (error "Not all levels are odd in this file. Conversion not possible"))
  7120. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7121. (let ((outline-regexp org-outline-regexp)
  7122. (outline-level 'org-outline-level)
  7123. (org-odd-levels-only nil) n)
  7124. (save-excursion
  7125. (goto-char (point-min))
  7126. (while (re-search-forward "^\\*\\*+ " nil t)
  7127. (setq n (/ (1- (length (match-string 0))) 2))
  7128. (while (>= (setq n (1- n)) 0)
  7129. (org-promote))
  7130. (end-of-line 1))))))
  7131. (defun org-tr-level (n)
  7132. "Make N odd if required."
  7133. (if org-odd-levels-only (1+ (/ n 2)) n))
  7134. ;;; Vertical tree motion, cutting and pasting of subtrees
  7135. (defun org-move-subtree-up (&optional arg)
  7136. "Move the current subtree up past ARG headlines of the same level."
  7137. (interactive "p")
  7138. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7139. (defun org-move-subtree-down (&optional arg)
  7140. "Move the current subtree down past ARG headlines of the same level."
  7141. (interactive "p")
  7142. (setq arg (prefix-numeric-value arg))
  7143. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7144. 'org-get-last-sibling))
  7145. (ins-point (make-marker))
  7146. (cnt (abs arg))
  7147. (col (current-column))
  7148. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7149. ;; Select the tree
  7150. (org-back-to-heading)
  7151. (setq beg0 (point))
  7152. (save-excursion
  7153. (setq ne-beg (org-back-over-empty-lines))
  7154. (setq beg (point)))
  7155. (save-match-data
  7156. (save-excursion (outline-end-of-heading)
  7157. (setq folded (outline-invisible-p)))
  7158. (outline-end-of-subtree))
  7159. (outline-next-heading)
  7160. (setq ne-end (org-back-over-empty-lines))
  7161. (setq end (point))
  7162. (goto-char beg0)
  7163. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7164. ;; include less whitespace
  7165. (save-excursion
  7166. (goto-char beg)
  7167. (forward-line (- ne-beg ne-end))
  7168. (setq beg (point))))
  7169. ;; Find insertion point, with error handling
  7170. (while (> cnt 0)
  7171. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7172. (progn (goto-char beg0)
  7173. (user-error "Cannot move past superior level or buffer limit")))
  7174. (setq cnt (1- cnt)))
  7175. (if (> arg 0)
  7176. ;; Moving forward - still need to move over subtree
  7177. (progn (org-end-of-subtree t t)
  7178. (save-excursion
  7179. (org-back-over-empty-lines)
  7180. (or (bolp) (newline)))))
  7181. (setq ne-ins (org-back-over-empty-lines))
  7182. (move-marker ins-point (point))
  7183. (setq txt (buffer-substring beg end))
  7184. (org-save-markers-in-region beg end)
  7185. (delete-region beg end)
  7186. (org-remove-empty-overlays-at beg)
  7187. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7188. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7189. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7190. (let ((bbb (point)))
  7191. (insert-before-markers txt)
  7192. (org-reinstall-markers-in-region bbb)
  7193. (move-marker ins-point bbb))
  7194. (or (bolp) (insert "\n"))
  7195. (setq ins-end (point))
  7196. (goto-char ins-point)
  7197. (org-skip-whitespace)
  7198. (when (and (< arg 0)
  7199. (org-first-sibling-p)
  7200. (> ne-ins ne-beg))
  7201. ;; Move whitespace back to beginning
  7202. (save-excursion
  7203. (goto-char ins-end)
  7204. (let ((kill-whole-line t))
  7205. (kill-line (- ne-ins ne-beg)) (point)))
  7206. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7207. (move-marker ins-point nil)
  7208. (if folded
  7209. (hide-subtree)
  7210. (org-show-entry)
  7211. (show-children)
  7212. (org-cycle-hide-drawers 'children))
  7213. (org-clean-visibility-after-subtree-move)
  7214. ;; move back to the initial column we were at
  7215. (move-to-column col)))
  7216. (defvar org-subtree-clip ""
  7217. "Clipboard for cut and paste of subtrees.
  7218. This is actually only a copy of the kill, because we use the normal kill
  7219. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7220. (defvar org-subtree-clip-folded nil
  7221. "Was the last copied subtree folded?
  7222. This is used to fold the tree back after pasting.")
  7223. (defun org-cut-subtree (&optional n)
  7224. "Cut the current subtree into the clipboard.
  7225. With prefix arg N, cut this many sequential subtrees.
  7226. This is a short-hand for marking the subtree and then cutting it."
  7227. (interactive "p")
  7228. (org-copy-subtree n 'cut))
  7229. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7230. "Cut the current subtree into the clipboard.
  7231. With prefix arg N, cut this many sequential subtrees.
  7232. This is a short-hand for marking the subtree and then copying it.
  7233. If CUT is non-nil, actually cut the subtree.
  7234. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7235. of some markers in the region, even if CUT is non-nil. This is
  7236. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7237. (interactive "p")
  7238. (let (beg end folded (beg0 (point)))
  7239. (if (org-called-interactively-p 'any)
  7240. (org-back-to-heading nil) ; take what looks like a subtree
  7241. (org-back-to-heading t)) ; take what is really there
  7242. (setq beg (point))
  7243. (skip-chars-forward " \t\r\n")
  7244. (save-match-data
  7245. (if nosubtrees
  7246. (outline-next-heading)
  7247. (save-excursion (outline-end-of-heading)
  7248. (setq folded (outline-invisible-p)))
  7249. (condition-case nil
  7250. (org-forward-heading-same-level (1- n) t)
  7251. (error nil))
  7252. (org-end-of-subtree t t)))
  7253. (setq end (point))
  7254. (goto-char beg0)
  7255. (when (> end beg)
  7256. (setq org-subtree-clip-folded folded)
  7257. (when (or cut force-store-markers)
  7258. (org-save-markers-in-region beg end))
  7259. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7260. (setq org-subtree-clip (current-kill 0))
  7261. (message "%s: Subtree(s) with %d characters"
  7262. (if cut "Cut" "Copied")
  7263. (length org-subtree-clip)))))
  7264. (defun org-paste-subtree (&optional level tree for-yank)
  7265. "Paste the clipboard as a subtree, with modification of headline level.
  7266. The entire subtree is promoted or demoted in order to match a new headline
  7267. level.
  7268. If the cursor is at the beginning of a headline, the same level as
  7269. that headline is used to paste the tree.
  7270. If not, the new level is derived from the *visible* headings
  7271. before and after the insertion point, and taken to be the inferior headline
  7272. level of the two. So if the previous visible heading is level 3 and the
  7273. next is level 4 (or vice versa), level 4 will be used for insertion.
  7274. This makes sure that the subtree remains an independent subtree and does
  7275. not swallow low level entries.
  7276. You can also force a different level, either by using a numeric prefix
  7277. argument, or by inserting the heading marker by hand. For example, if the
  7278. cursor is after \"*****\", then the tree will be shifted to level 5.
  7279. If optional TREE is given, use this text instead of the kill ring.
  7280. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7281. move back over whitespace before inserting, and move point to the end of
  7282. the inserted text when done."
  7283. (interactive "P")
  7284. (setq tree (or tree (and kill-ring (current-kill 0))))
  7285. (unless (org-kill-is-subtree-p tree)
  7286. (error "%s"
  7287. (substitute-command-keys
  7288. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7289. (org-with-limited-levels
  7290. (let* ((visp (not (outline-invisible-p)))
  7291. (txt tree)
  7292. (^re_ "\\(\\*+\\)[ \t]*")
  7293. (old-level (if (string-match org-outline-regexp-bol txt)
  7294. (- (match-end 0) (match-beginning 0) 1)
  7295. -1))
  7296. (force-level (cond (level (prefix-numeric-value level))
  7297. ((and (looking-at "[ \t]*$")
  7298. (string-match
  7299. "^\\*+$" (buffer-substring
  7300. (point-at-bol) (point))))
  7301. (- (match-end 1) (match-beginning 1)))
  7302. ((and (bolp)
  7303. (looking-at org-outline-regexp))
  7304. (- (match-end 0) (point) 1))))
  7305. (previous-level (save-excursion
  7306. (condition-case nil
  7307. (progn
  7308. (outline-previous-visible-heading 1)
  7309. (if (looking-at ^re_)
  7310. (- (match-end 0) (match-beginning 0) 1)
  7311. 1))
  7312. (error 1))))
  7313. (next-level (save-excursion
  7314. (condition-case nil
  7315. (progn
  7316. (or (looking-at org-outline-regexp)
  7317. (outline-next-visible-heading 1))
  7318. (if (looking-at ^re_)
  7319. (- (match-end 0) (match-beginning 0) 1)
  7320. 1))
  7321. (error 1))))
  7322. (new-level (or force-level (max previous-level next-level)))
  7323. (shift (if (or (= old-level -1)
  7324. (= new-level -1)
  7325. (= old-level new-level))
  7326. 0
  7327. (- new-level old-level)))
  7328. (delta (if (> shift 0) -1 1))
  7329. (func (if (> shift 0) 'org-demote 'org-promote))
  7330. (org-odd-levels-only nil)
  7331. beg end newend)
  7332. ;; Remove the forced level indicator
  7333. (if force-level
  7334. (delete-region (point-at-bol) (point)))
  7335. ;; Paste
  7336. (beginning-of-line (if (bolp) 1 2))
  7337. (setq beg (point))
  7338. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7339. (insert-before-markers txt)
  7340. (unless (string-match "\n\\'" txt) (insert "\n"))
  7341. (setq newend (point))
  7342. (org-reinstall-markers-in-region beg)
  7343. (setq end (point))
  7344. (goto-char beg)
  7345. (skip-chars-forward " \t\n\r")
  7346. (setq beg (point))
  7347. (if (and (outline-invisible-p) visp)
  7348. (save-excursion (outline-show-heading)))
  7349. ;; Shift if necessary
  7350. (unless (= shift 0)
  7351. (save-restriction
  7352. (narrow-to-region beg end)
  7353. (while (not (= shift 0))
  7354. (org-map-region func (point-min) (point-max))
  7355. (setq shift (+ delta shift)))
  7356. (goto-char (point-min))
  7357. (setq newend (point-max))))
  7358. (when (or (org-called-interactively-p 'interactive) for-yank)
  7359. (message "Clipboard pasted as level %d subtree" new-level))
  7360. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7361. kill-ring
  7362. (eq org-subtree-clip (current-kill 0))
  7363. org-subtree-clip-folded)
  7364. ;; The tree was folded before it was killed/copied
  7365. (hide-subtree))
  7366. (and for-yank (goto-char newend)))))
  7367. (defun org-kill-is-subtree-p (&optional txt)
  7368. "Check if the current kill is an outline subtree, or a set of trees.
  7369. Returns nil if kill does not start with a headline, or if the first
  7370. headline level is not the largest headline level in the tree.
  7371. So this will actually accept several entries of equal levels as well,
  7372. which is OK for `org-paste-subtree'.
  7373. If optional TXT is given, check this string instead of the current kill."
  7374. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7375. (re (org-get-limited-outline-regexp))
  7376. (^re (concat "^" re))
  7377. (start-level (and kill
  7378. (string-match
  7379. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7380. kill)
  7381. (- (match-end 2) (match-beginning 2) 1)))
  7382. (start (1+ (or (match-beginning 2) -1))))
  7383. (if (not start-level)
  7384. (progn
  7385. nil) ;; does not even start with a heading
  7386. (catch 'exit
  7387. (while (setq start (string-match ^re kill (1+ start)))
  7388. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7389. (throw 'exit nil)))
  7390. t))))
  7391. (defvar org-markers-to-move nil
  7392. "Markers that should be moved with a cut-and-paste operation.
  7393. Those markers are stored together with their positions relative to
  7394. the start of the region.")
  7395. (defun org-save-markers-in-region (beg end)
  7396. "Check markers in region.
  7397. If these markers are between BEG and END, record their position relative
  7398. to BEG, so that after moving the block of text, we can put the markers back
  7399. into place.
  7400. This function gets called just before an entry or tree gets cut from the
  7401. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7402. called immediately, to move the markers with the entries."
  7403. (setq org-markers-to-move nil)
  7404. (when (featurep 'org-clock)
  7405. (org-clock-save-markers-for-cut-and-paste beg end))
  7406. (when (featurep 'org-agenda)
  7407. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7408. (defun org-check-and-save-marker (marker beg end)
  7409. "Check if MARKER is between BEG and END.
  7410. If yes, remember the marker and the distance to BEG."
  7411. (when (and (marker-buffer marker)
  7412. (equal (marker-buffer marker) (current-buffer)))
  7413. (if (and (>= marker beg) (< marker end))
  7414. (push (cons marker (- marker beg)) org-markers-to-move))))
  7415. (defun org-reinstall-markers-in-region (beg)
  7416. "Move all remembered markers to their position relative to BEG."
  7417. (mapc (lambda (x)
  7418. (move-marker (car x) (+ beg (cdr x))))
  7419. org-markers-to-move)
  7420. (setq org-markers-to-move nil))
  7421. (defun org-narrow-to-subtree ()
  7422. "Narrow buffer to the current subtree."
  7423. (interactive)
  7424. (save-excursion
  7425. (save-match-data
  7426. (org-with-limited-levels
  7427. (narrow-to-region
  7428. (progn (org-back-to-heading t) (point))
  7429. (progn (org-end-of-subtree t t)
  7430. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7431. (point)))))))
  7432. (defun org-narrow-to-block ()
  7433. "Narrow buffer to the current block."
  7434. (interactive)
  7435. (let* ((case-fold-search t)
  7436. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7437. "^[ \t]*#\\+end_.*")))
  7438. (if blockp
  7439. (narrow-to-region (car blockp) (cdr blockp))
  7440. (error "Not in a block"))))
  7441. (eval-when-compile
  7442. (defvar org-property-drawer-re))
  7443. (defvar org-property-start-re) ;; defined below
  7444. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7445. "Clone the task (subtree) at point N times.
  7446. The clones will be inserted as siblings.
  7447. In interactive use, the user will be prompted for the number of
  7448. clones to be produced. When called with a universal prefix argument
  7449. and if the entry has a timestamp, the user will also be prompted for
  7450. a time shift, which may be a repeater as used in time stamps, for
  7451. example `+3d'.
  7452. When a valid repeater is given and the entry contains any time
  7453. stamps, the clones will become a sequence in time, with time
  7454. stamps in the subtree shifted for each clone produced. If SHIFT
  7455. is nil or the empty string, time stamps will be left alone. The
  7456. ID property of the original subtree is removed.
  7457. If the original subtree did contain time stamps with a repeater,
  7458. the following will happen:
  7459. - the repeater will be removed in each clone
  7460. - an additional clone will be produced, with the current, unshifted
  7461. date(s) in the entry.
  7462. - the original entry will be placed *after* all the clones, with
  7463. repeater intact.
  7464. - the start days in the repeater in the original entry will be shifted
  7465. to past the last clone.
  7466. In this way you can spell out a number of instances of a repeating task,
  7467. and still retain the repeater to cover future instances of the task."
  7468. (interactive "nNumber of clones to produce: ")
  7469. (let ((shift
  7470. (or shift
  7471. (if (and (equal current-prefix-arg '(4))
  7472. (save-excursion
  7473. (re-search-forward org-ts-regexp-both
  7474. (save-excursion
  7475. (org-end-of-subtree t)
  7476. (point)) t)))
  7477. (read-from-minibuffer
  7478. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7479. ""))) ;; No time shift
  7480. (n-no-remove -1)
  7481. (drawer-re org-drawer-regexp)
  7482. beg end template task idprop
  7483. shift-n shift-what doshift nmin nmax)
  7484. (if (not (and (integerp n) (> n 0)))
  7485. (error "Invalid number of replications %s" n))
  7486. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7487. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7488. shift)))
  7489. (error "Invalid shift specification %s" shift))
  7490. (when doshift
  7491. (setq shift-n (string-to-number (match-string 1 shift))
  7492. shift-what (cdr (assoc (match-string 2 shift)
  7493. '(("d" . day) ("w" . week)
  7494. ("m" . month) ("y" . year))))))
  7495. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7496. (setq nmin 1 nmax n)
  7497. (org-back-to-heading t)
  7498. (setq beg (point))
  7499. (setq idprop (org-entry-get nil "ID"))
  7500. (org-end-of-subtree t t)
  7501. (or (bolp) (insert "\n"))
  7502. (setq end (point))
  7503. (setq template (buffer-substring beg end))
  7504. (when (and doshift
  7505. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7506. (delete-region beg end)
  7507. (setq end beg)
  7508. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7509. (goto-char end)
  7510. (loop for n from nmin to nmax do
  7511. ;; prepare clone
  7512. (with-temp-buffer
  7513. (insert template)
  7514. (org-mode)
  7515. (goto-char (point-min))
  7516. (org-show-subtree)
  7517. (and idprop (if org-clone-delete-id
  7518. (org-entry-delete nil "ID")
  7519. (org-id-get-create t)))
  7520. (unless (= n 0)
  7521. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7522. (kill-whole-line))
  7523. (goto-char (point-min))
  7524. (while (re-search-forward drawer-re nil t)
  7525. (mapc (lambda (d)
  7526. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7527. (goto-char (point-min))
  7528. (when doshift
  7529. (while (re-search-forward org-ts-regexp-both nil t)
  7530. (org-timestamp-change (* n shift-n) shift-what))
  7531. (unless (= n n-no-remove)
  7532. (goto-char (point-min))
  7533. (while (re-search-forward org-ts-regexp nil t)
  7534. (save-excursion
  7535. (goto-char (match-beginning 0))
  7536. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7537. (delete-region (match-beginning 1) (match-end 1)))))))
  7538. (setq task (buffer-string)))
  7539. (insert task))
  7540. (goto-char beg)))
  7541. ;;; Outline Sorting
  7542. (defun org-sort (with-case)
  7543. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7544. Optional argument WITH-CASE means sort case-sensitively."
  7545. (interactive "P")
  7546. (cond
  7547. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7548. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7549. (t
  7550. (org-call-with-arg 'org-sort-entries with-case))))
  7551. (defun org-sort-remove-invisible (s)
  7552. "Remove invisible links from string S."
  7553. (remove-text-properties 0 (length s) org-rm-props s)
  7554. (while (string-match org-bracket-link-regexp s)
  7555. (setq s (replace-match (if (match-end 2)
  7556. (match-string 3 s)
  7557. (match-string 1 s)) t t s)))
  7558. (let ((st (format " %s " s)))
  7559. (while (string-match org-emph-re st)
  7560. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7561. (setq s (substring st 1 -1)))
  7562. s)
  7563. (defvar org-priority-regexp) ; defined later in the file
  7564. (defvar org-after-sorting-entries-or-items-hook nil
  7565. "Hook that is run after a bunch of entries or items have been sorted.
  7566. When children are sorted, the cursor is in the parent line when this
  7567. hook gets called. When a region or a plain list is sorted, the cursor
  7568. will be in the first entry of the sorted region/list.")
  7569. (defun org-sort-entries
  7570. (&optional with-case sorting-type getkey-func compare-func property)
  7571. "Sort entries on a certain level of an outline tree.
  7572. If there is an active region, the entries in the region are sorted.
  7573. Else, if the cursor is before the first entry, sort the top-level items.
  7574. Else, the children of the entry at point are sorted.
  7575. Sorting can be alphabetically, numerically, by date/time as given by
  7576. a time stamp, by a property, by priority order, or by a custom function.
  7577. The command prompts for the sorting type unless it has been given to the
  7578. function through the SORTING-TYPE argument, which needs to be a character,
  7579. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7580. precise meaning of each character:
  7581. n Numerically, by converting the beginning of the entry/item to a number.
  7582. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7583. o By order of TODO keywords.
  7584. t By date/time, either the first active time stamp in the entry, or, if
  7585. none exist, by the first inactive one.
  7586. s By the scheduled date/time.
  7587. d By deadline date/time.
  7588. c By creation time, which is assumed to be the first inactive time stamp
  7589. at the beginning of a line.
  7590. p By priority according to the cookie.
  7591. r By the value of a property.
  7592. Capital letters will reverse the sort order.
  7593. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7594. called with point at the beginning of the record. It must return either
  7595. a string or a number that should serve as the sorting key for that record.
  7596. Comparing entries ignores case by default. However, with an optional argument
  7597. WITH-CASE, the sorting considers case as well.
  7598. Sorting is done against the visible part of the headlines, it ignores hidden
  7599. links."
  7600. (interactive "P")
  7601. (let ((case-func (if with-case 'identity 'downcase))
  7602. (cmstr
  7603. ;; The clock marker is lost when using `sort-subr', let's
  7604. ;; store the clocking string.
  7605. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7606. (save-excursion
  7607. (goto-char org-clock-marker)
  7608. (looking-back "^.*") (match-string-no-properties 0))))
  7609. start beg end stars re re2
  7610. txt what tmp)
  7611. ;; Find beginning and end of region to sort
  7612. (cond
  7613. ((org-region-active-p)
  7614. ;; we will sort the region
  7615. (setq end (region-end)
  7616. what "region")
  7617. (goto-char (region-beginning))
  7618. (if (not (org-at-heading-p)) (outline-next-heading))
  7619. (setq start (point)))
  7620. ((or (org-at-heading-p)
  7621. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7622. ;; we will sort the children of the current headline
  7623. (org-back-to-heading)
  7624. (setq start (point)
  7625. end (progn (org-end-of-subtree t t)
  7626. (or (bolp) (insert "\n"))
  7627. (org-back-over-empty-lines)
  7628. (point))
  7629. what "children")
  7630. (goto-char start)
  7631. (show-subtree)
  7632. (outline-next-heading))
  7633. (t
  7634. ;; we will sort the top-level entries in this file
  7635. (goto-char (point-min))
  7636. (or (org-at-heading-p) (outline-next-heading))
  7637. (setq start (point))
  7638. (goto-char (point-max))
  7639. (beginning-of-line 1)
  7640. (when (looking-at ".*?\\S-")
  7641. ;; File ends in a non-white line
  7642. (end-of-line 1)
  7643. (insert "\n"))
  7644. (setq end (point-max))
  7645. (setq what "top-level")
  7646. (goto-char start)
  7647. (show-all)))
  7648. (setq beg (point))
  7649. (if (>= beg end) (error "Nothing to sort"))
  7650. (looking-at "\\(\\*+\\)")
  7651. (setq stars (match-string 1)
  7652. re (concat "^" (regexp-quote stars) " +")
  7653. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7654. txt (buffer-substring beg end))
  7655. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7656. (if (and (not (equal stars "*")) (string-match re2 txt))
  7657. (error "Region to sort contains a level above the first entry"))
  7658. (unless sorting-type
  7659. (message
  7660. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7661. [t]ime [s]cheduled [d]eadline [c]reated
  7662. A/N/P/R/O/F/T/S/D/C means reversed:"
  7663. what)
  7664. (setq sorting-type (read-char-exclusive))
  7665. (unless getkey-func
  7666. (and (= (downcase sorting-type) ?f)
  7667. (setq getkey-func
  7668. (org-icompleting-read "Sort using function: "
  7669. obarray 'fboundp t nil nil))
  7670. (setq getkey-func (intern getkey-func))))
  7671. (and (= (downcase sorting-type) ?r)
  7672. (not property)
  7673. (setq property
  7674. (org-icompleting-read "Property: "
  7675. (mapcar 'list (org-buffer-property-keys t))
  7676. nil t))))
  7677. (message "Sorting entries...")
  7678. (save-restriction
  7679. (narrow-to-region start end)
  7680. (let ((dcst (downcase sorting-type))
  7681. (case-fold-search nil)
  7682. (now (current-time)))
  7683. (sort-subr
  7684. (/= dcst sorting-type)
  7685. ;; This function moves to the beginning character of the "record" to
  7686. ;; be sorted.
  7687. (lambda nil
  7688. (if (re-search-forward re nil t)
  7689. (goto-char (match-beginning 0))
  7690. (goto-char (point-max))))
  7691. ;; This function moves to the last character of the "record" being
  7692. ;; sorted.
  7693. (lambda nil
  7694. (save-match-data
  7695. (condition-case nil
  7696. (outline-forward-same-level 1)
  7697. (error
  7698. (goto-char (point-max))))))
  7699. ;; This function returns the value that gets sorted against.
  7700. (lambda nil
  7701. (cond
  7702. ((= dcst ?n)
  7703. (if (looking-at org-complex-heading-regexp)
  7704. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7705. nil))
  7706. ((= dcst ?a)
  7707. (if (looking-at org-complex-heading-regexp)
  7708. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7709. nil))
  7710. ((= dcst ?t)
  7711. (let ((end (save-excursion (outline-next-heading) (point))))
  7712. (if (or (re-search-forward org-ts-regexp end t)
  7713. (re-search-forward org-ts-regexp-both end t))
  7714. (org-time-string-to-seconds (match-string 0))
  7715. (org-float-time now))))
  7716. ((= dcst ?c)
  7717. (let ((end (save-excursion (outline-next-heading) (point))))
  7718. (if (re-search-forward
  7719. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7720. end t)
  7721. (org-time-string-to-seconds (match-string 0))
  7722. (org-float-time now))))
  7723. ((= dcst ?s)
  7724. (let ((end (save-excursion (outline-next-heading) (point))))
  7725. (if (re-search-forward org-scheduled-time-regexp end t)
  7726. (org-time-string-to-seconds (match-string 1))
  7727. (org-float-time now))))
  7728. ((= dcst ?d)
  7729. (let ((end (save-excursion (outline-next-heading) (point))))
  7730. (if (re-search-forward org-deadline-time-regexp end t)
  7731. (org-time-string-to-seconds (match-string 1))
  7732. (org-float-time now))))
  7733. ((= dcst ?p)
  7734. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7735. (string-to-char (match-string 2))
  7736. org-default-priority))
  7737. ((= dcst ?r)
  7738. (or (org-entry-get nil property) ""))
  7739. ((= dcst ?o)
  7740. (if (looking-at org-complex-heading-regexp)
  7741. (- 9999 (length (member (match-string 2)
  7742. org-todo-keywords-1)))))
  7743. ((= dcst ?f)
  7744. (if getkey-func
  7745. (progn
  7746. (setq tmp (funcall getkey-func))
  7747. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7748. tmp)
  7749. (error "Invalid key function `%s'" getkey-func)))
  7750. (t (error "Invalid sorting type `%c'" sorting-type))))
  7751. nil
  7752. (cond
  7753. ((= dcst ?a) 'string<)
  7754. ((= dcst ?f) compare-func)
  7755. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7756. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7757. ;; Reset the clock marker if needed
  7758. (when cmstr
  7759. (save-excursion
  7760. (goto-char start)
  7761. (search-forward cmstr nil t)
  7762. (move-marker org-clock-marker (point))))
  7763. (message "Sorting entries...done")))
  7764. (defun org-do-sort (table what &optional with-case sorting-type)
  7765. "Sort TABLE of WHAT according to SORTING-TYPE.
  7766. The user will be prompted for the SORTING-TYPE if the call to this
  7767. function does not specify it. WHAT is only for the prompt, to indicate
  7768. what is being sorted. The sorting key will be extracted from
  7769. the car of the elements of the table.
  7770. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7771. (unless sorting-type
  7772. (message
  7773. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7774. what)
  7775. (setq sorting-type (read-char-exclusive)))
  7776. (let ((dcst (downcase sorting-type))
  7777. extractfun comparefun)
  7778. ;; Define the appropriate functions
  7779. (cond
  7780. ((= dcst ?n)
  7781. (setq extractfun 'string-to-number
  7782. comparefun (if (= dcst sorting-type) '< '>)))
  7783. ((= dcst ?a)
  7784. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7785. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7786. comparefun (if (= dcst sorting-type)
  7787. 'string<
  7788. (lambda (a b) (and (not (string< a b))
  7789. (not (string= a b)))))))
  7790. ((= dcst ?t)
  7791. (setq extractfun
  7792. (lambda (x)
  7793. (if (or (string-match org-ts-regexp x)
  7794. (string-match org-ts-regexp-both x))
  7795. (org-float-time
  7796. (org-time-string-to-time (match-string 0 x)))
  7797. 0))
  7798. comparefun (if (= dcst sorting-type) '< '>)))
  7799. (t (error "Invalid sorting type `%c'" sorting-type)))
  7800. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7801. table)
  7802. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7803. ;;; The orgstruct minor mode
  7804. ;; Define a minor mode which can be used in other modes in order to
  7805. ;; integrate the org-mode structure editing commands.
  7806. ;; This is really a hack, because the org-mode structure commands use
  7807. ;; keys which normally belong to the major mode. Here is how it
  7808. ;; works: The minor mode defines all the keys necessary to operate the
  7809. ;; structure commands, but wraps the commands into a function which
  7810. ;; tests if the cursor is currently at a headline or a plain list
  7811. ;; item. If that is the case, the structure command is used,
  7812. ;; temporarily setting many Org-mode variables like regular
  7813. ;; expressions for filling etc. However, when any of those keys is
  7814. ;; used at a different location, function uses `key-binding' to look
  7815. ;; up if the key has an associated command in another currently active
  7816. ;; keymap (minor modes, major mode, global), and executes that
  7817. ;; command. There might be problems if any of the keys is otherwise
  7818. ;; used as a prefix key.
  7819. (defcustom orgstruct-heading-prefix-regexp nil
  7820. "Regexp that matches the custom prefix of Org headlines in
  7821. orgstruct(++)-mode."
  7822. :group 'org
  7823. :version "24.4"
  7824. :package-version '(Org . "8.0")
  7825. :type 'string)
  7826. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7827. (defcustom orgstruct-setup-hook nil
  7828. "Hook run after orgstruct-mode-map is filled."
  7829. :group 'org
  7830. :version "24.4"
  7831. :package-version '(Org . "8.0")
  7832. :type 'hook)
  7833. (defvar orgstruct-initialized nil)
  7834. (defvar org-local-vars nil
  7835. "List of local variables, for use by `orgstruct-mode'.")
  7836. ;;;###autoload
  7837. (define-minor-mode orgstruct-mode
  7838. "Toggle the minor mode `orgstruct-mode'.
  7839. This mode is for using Org-mode structure commands in other
  7840. modes. The following keys behave as if Org-mode were active, if
  7841. the cursor is on a headline, or on a plain list item (both as
  7842. defined by Org-mode)."
  7843. nil " OrgStruct" (make-sparse-keymap)
  7844. (funcall (if orgstruct-mode
  7845. 'add-to-invisibility-spec
  7846. 'remove-from-invisibility-spec)
  7847. '(outline . t))
  7848. (when orgstruct-mode
  7849. (org-load-modules-maybe)
  7850. (unless orgstruct-initialized
  7851. (orgstruct-setup)
  7852. (setq orgstruct-initialized t))))
  7853. ;;;###autoload
  7854. (defun turn-on-orgstruct ()
  7855. "Unconditionally turn on `orgstruct-mode'."
  7856. (orgstruct-mode 1))
  7857. (defvar org-fb-vars nil)
  7858. (make-variable-buffer-local 'org-fb-vars)
  7859. (defun orgstruct++-mode (&optional arg)
  7860. "Toggle `orgstruct-mode', the enhanced version of it.
  7861. In addition to setting orgstruct-mode, this also exports all
  7862. indentation and autofilling variables from org-mode into the
  7863. buffer. It will also recognize item context in multiline items."
  7864. (interactive "P")
  7865. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7866. (if (< arg 1)
  7867. (progn (orgstruct-mode -1)
  7868. (mapc (lambda(v)
  7869. (org-set-local (car v)
  7870. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7871. org-fb-vars))
  7872. (orgstruct-mode 1)
  7873. (setq org-fb-vars nil)
  7874. (let (var val)
  7875. (mapc
  7876. (lambda (x)
  7877. (when (string-match
  7878. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7879. (symbol-name (car x)))
  7880. (setq var (car x) val (nth 1 x))
  7881. (push (list var `(quote ,(eval var))) org-fb-vars)
  7882. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7883. org-local-vars)
  7884. (org-set-local 'orgstruct-is-++ t))))
  7885. (defvar orgstruct-is-++ nil
  7886. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7887. (make-variable-buffer-local 'orgstruct-is-++)
  7888. ;;;###autoload
  7889. (defun turn-on-orgstruct++ ()
  7890. "Unconditionally turn on `orgstruct++-mode'."
  7891. (orgstruct++-mode 1))
  7892. (defun orgstruct-error ()
  7893. "Error when there is no default binding for a structure key."
  7894. (interactive)
  7895. (funcall (if (fboundp 'user-error)
  7896. 'user-error
  7897. 'error)
  7898. "This key has no function outside structure elements"))
  7899. (defun orgstruct-setup ()
  7900. "Setup orgstruct keymap."
  7901. (dolist (cell '((org-demote . t)
  7902. (org-metaleft . t)
  7903. (org-metaright . t)
  7904. (org-promote . t)
  7905. (org-shiftmetaleft . t)
  7906. (org-shiftmetaright . t)
  7907. org-backward-element
  7908. org-backward-heading-same-level
  7909. org-ctrl-c-ret
  7910. org-ctrl-c-minus
  7911. org-ctrl-c-star
  7912. org-cycle
  7913. org-forward-heading-same-level
  7914. org-insert-heading
  7915. org-insert-heading-respect-content
  7916. org-kill-note-or-show-branches
  7917. org-mark-subtree
  7918. org-meta-return
  7919. org-metadown
  7920. org-metaup
  7921. org-narrow-to-subtree
  7922. org-promote-subtree
  7923. org-reveal
  7924. org-shiftdown
  7925. org-shiftleft
  7926. org-shiftmetadown
  7927. org-shiftmetaup
  7928. org-shiftright
  7929. org-shifttab
  7930. org-shifttab
  7931. org-shiftup
  7932. org-show-subtree
  7933. org-sort
  7934. org-up-element
  7935. outline-demote
  7936. outline-next-visible-heading
  7937. outline-previous-visible-heading
  7938. outline-promote
  7939. outline-up-heading
  7940. show-children))
  7941. (let ((f (or (car-safe cell) cell))
  7942. (disable-when-heading-prefix (cdr-safe cell)))
  7943. (when (fboundp f)
  7944. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7945. (where-is-internal f outline-mode-map)))
  7946. ;; TODO use local-function-key-map
  7947. (dolist (rep '(("<tab>" . "TAB")
  7948. ("<return>" . "RET")
  7949. ("<escape>" . "ESC")
  7950. ("<delete>" . "DEL")))
  7951. (setq binding (read-kbd-macro
  7952. (let ((case-fold-search))
  7953. (replace-regexp-in-string
  7954. (regexp-quote (cdr rep))
  7955. (car rep)
  7956. (key-description binding))))))
  7957. (let ((key (lookup-key orgstruct-mode-map binding)))
  7958. (when (or (not key) (numberp key))
  7959. (condition-case nil
  7960. (org-defkey orgstruct-mode-map
  7961. binding
  7962. (orgstruct-make-binding f binding disable-when-heading-prefix))
  7963. (error nil))))))))
  7964. (run-hooks 'orgstruct-setup-hook))
  7965. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  7966. "Create a function for binding in the structure minor mode.
  7967. FUN is the command to call inside a table. KEY is the key that
  7968. should be checked in for a command to execute outside of tables.
  7969. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  7970. if `orgstruct-heading-prefix-regexp' is non-nil."
  7971. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7972. (let ((nname name)
  7973. (i 0))
  7974. (while (fboundp (intern nname))
  7975. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7976. (setq name (intern nname)))
  7977. (eval
  7978. (let ((bindings '((org-heading-regexp
  7979. (concat "^"
  7980. orgstruct-heading-prefix-regexp
  7981. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7982. (org-outline-regexp
  7983. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7984. (org-outline-regexp-bol
  7985. (concat "^" org-outline-regexp))
  7986. (outline-regexp org-outline-regexp)
  7987. (outline-heading-end-regexp "\n")
  7988. (outline-level 'org-outline-level)
  7989. (outline-heading-alist))))
  7990. `(defun ,name (arg)
  7991. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7992. "Outside of structure, run the binding of `"
  7993. (key-description key) "'."
  7994. (when disable-when-heading-prefix
  7995. (concat
  7996. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  7997. "back to the default binding due to limitations of Org's implementation of\n"
  7998. "`" (symbol-name fun) "'.")))
  7999. (interactive "p")
  8000. (let* ((disable
  8001. ,(when disable-when-heading-prefix
  8002. '(and orgstruct-heading-prefix-regexp
  8003. (not (string= orgstruct-heading-prefix-regexp "")))))
  8004. (fallback
  8005. (or disable
  8006. (not
  8007. (let* ,bindings
  8008. (org-context-p 'headline 'item
  8009. ,(when (memq fun '(org-insert-heading))
  8010. '(when orgstruct-is-++
  8011. 'item-body))))))))
  8012. (if fallback
  8013. (let* ((orgstruct-mode)
  8014. (binding
  8015. (loop with key = ,key
  8016. for rep in '(nil
  8017. ("<tab>" . "TAB")
  8018. ("<return>" . "RET")
  8019. ("<escape>" . "ESC")
  8020. ("<delete>" . "DEL"))
  8021. do
  8022. (when rep
  8023. (setq key (read-kbd-macro
  8024. (let ((case-fold-search))
  8025. (replace-regexp-in-string
  8026. (regexp-quote (car rep))
  8027. (cdr rep)
  8028. (key-description key))))))
  8029. thereis (key-binding key))))
  8030. (if (keymapp binding)
  8031. (set-temporary-overlay-map binding)
  8032. (let ((func (or binding
  8033. (unless disable
  8034. 'orgstruct-error))))
  8035. (when func
  8036. (call-interactively func)))))
  8037. (org-run-like-in-org-mode
  8038. (lambda ()
  8039. (interactive)
  8040. (let* ,bindings
  8041. (call-interactively ',fun)))))))))
  8042. name))
  8043. (defun org-contextualize-keys (alist contexts)
  8044. "Return valid elements in ALIST depending on CONTEXTS.
  8045. `org-agenda-custom-commands' or `org-capture-templates' are the
  8046. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8047. or `org-capture-templates-contexts' are the associated contexts
  8048. definitions."
  8049. (let ((contexts
  8050. ;; normalize contexts
  8051. (mapcar
  8052. (lambda(c) (cond ((listp (cadr c))
  8053. (list (car c) (car c) (cadr c)))
  8054. ((string= "" (cadr c))
  8055. (list (car c) (car c) (caddr c)))
  8056. (t c))) contexts))
  8057. (a alist) c r s)
  8058. ;; loop over all commands or templates
  8059. (while (setq c (pop a))
  8060. (let (vrules repl)
  8061. (cond
  8062. ((not (assoc (car c) contexts))
  8063. (push c r))
  8064. ((and (assoc (car c) contexts)
  8065. (setq vrules (org-contextualize-validate-key
  8066. (car c) contexts)))
  8067. (mapc (lambda (vr)
  8068. (when (not (equal (car vr) (cadr vr)))
  8069. (setq repl vr))) vrules)
  8070. (if (not repl) (push c r)
  8071. (push (cadr repl) s)
  8072. (push
  8073. (cons (car c)
  8074. (cdr (or (assoc (cadr repl) alist)
  8075. (error "Undefined key `%s' as contextual replacement for `%s'"
  8076. (cadr repl) (car c)))))
  8077. r))))))
  8078. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8079. (delq
  8080. nil
  8081. (delete-dups
  8082. (mapcar (lambda (x)
  8083. (let ((tpl (car x)))
  8084. (when (not (delq
  8085. nil
  8086. (mapcar (lambda(y)
  8087. (equal y tpl)) s))) x)))
  8088. (reverse r))))))
  8089. (defun org-contextualize-validate-key (key contexts)
  8090. "Check CONTEXTS for agenda or capture KEY."
  8091. (let (r rr res)
  8092. (while (setq r (pop contexts))
  8093. (mapc
  8094. (lambda (rr)
  8095. (when
  8096. (and (equal key (car r))
  8097. (if (functionp rr) (funcall rr)
  8098. (or (and (eq (car rr) 'in-file)
  8099. (buffer-file-name)
  8100. (string-match (cdr rr) (buffer-file-name)))
  8101. (and (eq (car rr) 'in-mode)
  8102. (string-match (cdr rr) (symbol-name major-mode)))
  8103. (and (eq (car rr) 'in-buffer)
  8104. (string-match (cdr rr) (buffer-name)))
  8105. (when (and (eq (car rr) 'not-in-file)
  8106. (buffer-file-name))
  8107. (not (string-match (cdr rr) (buffer-file-name))))
  8108. (when (eq (car rr) 'not-in-mode)
  8109. (not (string-match (cdr rr) (symbol-name major-mode))))
  8110. (when (eq (car rr) 'not-in-buffer)
  8111. (not (string-match (cdr rr) (buffer-name)))))))
  8112. (push r res)))
  8113. (car (last r))))
  8114. (delete-dups (delq nil res))))
  8115. (defun org-context-p (&rest contexts)
  8116. "Check if local context is any of CONTEXTS.
  8117. Possible values in the list of contexts are `table', `headline', and `item'."
  8118. (let ((pos (point)))
  8119. (goto-char (point-at-bol))
  8120. (prog1 (or (and (memq 'table contexts)
  8121. (looking-at "[ \t]*|"))
  8122. (and (memq 'headline contexts)
  8123. (looking-at org-outline-regexp))
  8124. (and (memq 'item contexts)
  8125. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8126. (and (memq 'item-body contexts)
  8127. (org-in-item-p)))
  8128. (goto-char pos))))
  8129. (defun org-get-local-variables ()
  8130. "Return a list of all local variables in an Org mode buffer."
  8131. (let (varlist)
  8132. (with-current-buffer (get-buffer-create "*Org tmp*")
  8133. (erase-buffer)
  8134. (org-mode)
  8135. (setq varlist (buffer-local-variables)))
  8136. (kill-buffer "*Org tmp*")
  8137. (delq nil
  8138. (mapcar
  8139. (lambda (x)
  8140. (setq x
  8141. (if (symbolp x)
  8142. (list x)
  8143. (list (car x) (cdr x))))
  8144. (if (and (not (get (car x) 'org-state))
  8145. (string-match
  8146. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8147. (symbol-name (car x))))
  8148. x nil))
  8149. varlist))))
  8150. (defun org-clone-local-variables (from-buffer &optional regexp)
  8151. "Clone local variables from FROM-BUFFER.
  8152. Optional argument REGEXP selects variables to clone."
  8153. (mapc
  8154. (lambda (pair)
  8155. (and (symbolp (car pair))
  8156. (or (null regexp)
  8157. (string-match regexp (symbol-name (car pair))))
  8158. (set (make-local-variable (car pair))
  8159. (cdr pair))))
  8160. (buffer-local-variables from-buffer)))
  8161. ;;;###autoload
  8162. (defun org-run-like-in-org-mode (cmd)
  8163. "Run a command, pretending that the current buffer is in Org-mode.
  8164. This will temporarily bind local variables that are typically bound in
  8165. Org-mode to the values they have in Org-mode, and then interactively
  8166. call CMD."
  8167. (org-load-modules-maybe)
  8168. (unless org-local-vars
  8169. (setq org-local-vars (org-get-local-variables)))
  8170. (let (binds)
  8171. (dolist (var org-local-vars)
  8172. (when (or (not (boundp (car var)))
  8173. (eq (symbol-value (car var))
  8174. (default-value (car var))))
  8175. (push (list (car var) `(quote ,(cadr var))) binds)))
  8176. (eval `(let ,binds
  8177. (call-interactively (quote ,cmd))))))
  8178. ;;;; Archiving
  8179. (defun org-get-category (&optional pos force-refresh)
  8180. "Get the category applying to position POS."
  8181. (save-match-data
  8182. (if force-refresh (org-refresh-category-properties))
  8183. (let ((pos (or pos (point))))
  8184. (or (get-text-property pos 'org-category)
  8185. (progn (org-refresh-category-properties)
  8186. (get-text-property pos 'org-category))))))
  8187. (defun org-refresh-category-properties ()
  8188. "Refresh category text properties in the buffer."
  8189. (let ((case-fold-search t)
  8190. (inhibit-read-only t)
  8191. (def-cat (cond
  8192. ((null org-category)
  8193. (if buffer-file-name
  8194. (file-name-sans-extension
  8195. (file-name-nondirectory buffer-file-name))
  8196. "???"))
  8197. ((symbolp org-category) (symbol-name org-category))
  8198. (t org-category)))
  8199. beg end cat pos optionp)
  8200. (org-with-silent-modifications
  8201. (save-excursion
  8202. (save-restriction
  8203. (widen)
  8204. (goto-char (point-min))
  8205. (put-text-property (point) (point-max) 'org-category def-cat)
  8206. (while (re-search-forward
  8207. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8208. (setq pos (match-end 0)
  8209. optionp (equal (char-after (match-beginning 0)) ?#)
  8210. cat (org-trim (match-string 2)))
  8211. (if optionp
  8212. (setq beg (point-at-bol) end (point-max))
  8213. (org-back-to-heading t)
  8214. (setq beg (point) end (org-end-of-subtree t t)))
  8215. (put-text-property beg end 'org-category cat)
  8216. (put-text-property beg end 'org-category-position beg)
  8217. (goto-char pos)))))))
  8218. (defun org-refresh-properties (dprop tprop)
  8219. "Refresh buffer text properties.
  8220. DPROP is the drawer property and TPROP is the corresponding text
  8221. property to set."
  8222. (let ((case-fold-search t)
  8223. (inhibit-read-only t) p)
  8224. (org-with-silent-modifications
  8225. (save-excursion
  8226. (save-restriction
  8227. (widen)
  8228. (goto-char (point-min))
  8229. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8230. (setq p (org-match-string-no-properties 1))
  8231. (save-excursion
  8232. (org-back-to-heading t)
  8233. (put-text-property
  8234. (point-at-bol) (point-at-eol) tprop p))))))))
  8235. ;;;; Link Stuff
  8236. ;;; Link abbreviations
  8237. (defun org-link-expand-abbrev (link)
  8238. "Apply replacements as defined in `org-link-abbrev-alist'."
  8239. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8240. (let* ((key (match-string 1 link))
  8241. (as (or (assoc key org-link-abbrev-alist-local)
  8242. (assoc key org-link-abbrev-alist)))
  8243. (tag (and (match-end 2) (match-string 3 link)))
  8244. rpl)
  8245. (if (not as)
  8246. link
  8247. (setq rpl (cdr as))
  8248. (cond
  8249. ((symbolp rpl) (funcall rpl tag))
  8250. ((string-match "%(\\([^)]+\\))" rpl)
  8251. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8252. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8253. ((string-match "%h" rpl)
  8254. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8255. (t (concat rpl tag)))))
  8256. link))
  8257. ;;; Storing and inserting links
  8258. (defvar org-insert-link-history nil
  8259. "Minibuffer history for links inserted with `org-insert-link'.")
  8260. (defvar org-stored-links nil
  8261. "Contains the links stored with `org-store-link'.")
  8262. (defvar org-store-link-plist nil
  8263. "Plist with info about the most recently link created with `org-store-link'.")
  8264. (defvar org-link-protocols nil
  8265. "Link protocols added to Org-mode using `org-add-link-type'.")
  8266. (defvar org-store-link-functions nil
  8267. "List of functions that are called to create and store a link.
  8268. Each function will be called in turn until one returns a non-nil
  8269. value. Each function should check if it is responsible for creating
  8270. this link (for example by looking at the major mode).
  8271. If not, it must exit and return nil.
  8272. If yes, it should return a non-nil value after a calling
  8273. `org-store-link-props' with a list of properties and values.
  8274. Special properties are:
  8275. :type The link prefix, like \"http\". This must be given.
  8276. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8277. This is obligatory as well.
  8278. :description Optional default description for the second pair
  8279. of brackets in an Org-mode link. The user can still change
  8280. this when inserting this link into an Org-mode buffer.
  8281. In addition to these, any additional properties can be specified
  8282. and then used in capture templates.")
  8283. (defun org-add-link-type (type &optional follow export)
  8284. "Add TYPE to the list of `org-link-types'.
  8285. Re-compute all regular expressions depending on `org-link-types'
  8286. FOLLOW and EXPORT are two functions.
  8287. FOLLOW should take the link path as the single argument and do whatever
  8288. is necessary to follow the link, for example find a file or display
  8289. a mail message.
  8290. EXPORT should format the link path for export to one of the export formats.
  8291. It should be a function accepting three arguments:
  8292. path the path of the link, the text after the prefix (like \"http:\")
  8293. desc the description of the link, if any, or a description added by
  8294. org-export-normalize-links if there is none
  8295. format the export format, a symbol like `html' or `latex' or `ascii'..
  8296. The function may use the FORMAT information to return different values
  8297. depending on the format. The return value will be put literally into
  8298. the exported file. If the return value is nil, this means Org should
  8299. do what it normally does with links which do not have EXPORT defined.
  8300. Org-mode has a built-in default for exporting links. If you are happy with
  8301. this default, there is no need to define an export function for the link
  8302. type. For a simple example of an export function, see `org-bbdb.el'."
  8303. (add-to-list 'org-link-types type t)
  8304. (org-make-link-regexps)
  8305. (if (assoc type org-link-protocols)
  8306. (setcdr (assoc type org-link-protocols) (list follow export))
  8307. (push (list type follow export) org-link-protocols)))
  8308. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8309. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8310. ;;;###autoload
  8311. (defun org-store-link (arg)
  8312. "\\<org-mode-map>Store an org-link to the current location.
  8313. This link is added to `org-stored-links' and can later be inserted
  8314. into an org-buffer with \\[org-insert-link].
  8315. For some link types, a prefix arg is interpreted.
  8316. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8317. For file links, arg negates `org-context-in-file-links'.
  8318. A double prefix arg force skipping storing functions that are not
  8319. part of Org's core."
  8320. (interactive "P")
  8321. (org-load-modules-maybe)
  8322. (setq org-store-link-plist nil) ; reset
  8323. (org-with-limited-levels
  8324. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8325. (cond
  8326. ((and (not (equal arg '(16)))
  8327. (setq sfuns
  8328. (delq
  8329. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8330. org-store-link-functions))
  8331. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8332. (or (and (cdr sfuns)
  8333. (funcall (intern
  8334. (completing-read "Which function for creating the link? "
  8335. sfunsn t (car sfunsn)))))
  8336. (funcall (caar sfuns)))
  8337. (setq link (plist-get org-store-link-plist :link)
  8338. desc (or (plist-get org-store-link-plist :description) link))))
  8339. ((org-src-edit-buffer-p)
  8340. (let (label gc)
  8341. (while (or (not label)
  8342. (save-excursion
  8343. (save-restriction
  8344. (widen)
  8345. (goto-char (point-min))
  8346. (re-search-forward
  8347. (regexp-quote (format org-coderef-label-format label))
  8348. nil t))))
  8349. (when label (message "Label exists already") (sit-for 2))
  8350. (setq label (read-string "Code line label: " label)))
  8351. (end-of-line 1)
  8352. (setq link (format org-coderef-label-format label))
  8353. (setq gc (- 79 (length link)))
  8354. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8355. (insert link)
  8356. (setq link (concat "(" label ")") desc nil)))
  8357. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8358. ;; We are in the agenda, link to referenced location
  8359. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8360. (get-text-property (point) 'org-marker))))
  8361. (when m
  8362. (org-with-point-at m
  8363. (setq agenda-link
  8364. (if (org-called-interactively-p 'any)
  8365. (call-interactively 'org-store-link)
  8366. (org-store-link nil)))))))
  8367. ((eq major-mode 'calendar-mode)
  8368. (let ((cd (calendar-cursor-to-date)))
  8369. (setq link
  8370. (format-time-string
  8371. (car org-time-stamp-formats)
  8372. (apply 'encode-time
  8373. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8374. nil nil nil))))
  8375. (org-store-link-props :type "calendar" :date cd)))
  8376. ((eq major-mode 'help-mode)
  8377. (setq link (concat "help:" (save-excursion
  8378. (goto-char (point-min))
  8379. (looking-at "^[^ ]+")
  8380. (match-string 0))))
  8381. (org-store-link-props :type "help"))
  8382. ((eq major-mode 'w3-mode)
  8383. (setq cpltxt (if (and (buffer-name)
  8384. (not (string-match "Untitled" (buffer-name))))
  8385. (buffer-name)
  8386. (url-view-url t))
  8387. link (url-view-url t))
  8388. (org-store-link-props :type "w3" :url (url-view-url t)))
  8389. ((setq search (run-hook-with-args-until-success
  8390. 'org-create-file-search-functions))
  8391. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8392. "::" search))
  8393. (setq cpltxt (or description link)))
  8394. ((eq major-mode 'image-mode)
  8395. (setq cpltxt (concat "file:"
  8396. (abbreviate-file-name buffer-file-name))
  8397. link cpltxt)
  8398. (org-store-link-props :type "image" :file buffer-file-name))
  8399. ((eq major-mode 'dired-mode)
  8400. ;; link to the file in the current line
  8401. (let ((file (dired-get-filename nil t)))
  8402. (setq file (if file
  8403. (abbreviate-file-name
  8404. (expand-file-name (dired-get-filename nil t)))
  8405. ;; otherwise, no file so use current directory.
  8406. default-directory))
  8407. (setq cpltxt (concat "file:" file)
  8408. link cpltxt)))
  8409. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8410. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8411. (cond
  8412. ((org-in-regexp "<<\\(.*?\\)>>")
  8413. (setq cpltxt
  8414. (concat "file:"
  8415. (abbreviate-file-name
  8416. (buffer-file-name (buffer-base-buffer)))
  8417. "::" (match-string 1))
  8418. link cpltxt))
  8419. ((and (featurep 'org-id)
  8420. (or (eq org-id-link-to-org-use-id t)
  8421. (and (org-called-interactively-p 'any)
  8422. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8423. (and (eq org-id-link-to-org-use-id
  8424. 'create-if-interactive-and-no-custom-id)
  8425. (not custom-id))))
  8426. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8427. ;; We can make a link using the ID.
  8428. (setq link (condition-case nil
  8429. (prog1 (org-id-store-link)
  8430. (setq desc (plist-get org-store-link-plist :description)))
  8431. (error
  8432. ;; probably before first headline, link to file only
  8433. (concat "file:"
  8434. (abbreviate-file-name
  8435. (buffer-file-name (buffer-base-buffer))))))))
  8436. (t
  8437. ;; Just link to current headline
  8438. (setq cpltxt (concat "file:"
  8439. (abbreviate-file-name
  8440. (buffer-file-name (buffer-base-buffer)))))
  8441. ;; Add a context search string
  8442. (when (org-xor org-context-in-file-links arg)
  8443. (let* ((ee (org-element-at-point))
  8444. (et (org-element-type ee))
  8445. (ev (plist-get (cadr ee) :value)))
  8446. (setq txt (cond
  8447. ((org-at-heading-p) nil)
  8448. ((eq et 'keyword) ev)
  8449. ((org-region-active-p)
  8450. (buffer-substring (region-beginning) (region-end)))))
  8451. (when (or (null txt) (string-match "\\S-" txt))
  8452. (setq cpltxt
  8453. (concat cpltxt "::"
  8454. (condition-case nil
  8455. (org-make-org-heading-search-string txt)
  8456. (error "")))
  8457. desc (or (and (eq et 'keyword) ev)
  8458. (nth 4 (ignore-errors (org-heading-components)))
  8459. "NONE")))))
  8460. (if (string-match "::\\'" cpltxt)
  8461. (setq cpltxt (substring cpltxt 0 -2)))
  8462. (setq link cpltxt))))
  8463. ((buffer-file-name (buffer-base-buffer))
  8464. ;; Just link to this file here.
  8465. (setq cpltxt (concat "file:"
  8466. (abbreviate-file-name
  8467. (buffer-file-name (buffer-base-buffer)))))
  8468. ;; Add a context string
  8469. (when (org-xor org-context-in-file-links arg)
  8470. (setq txt (if (org-region-active-p)
  8471. (buffer-substring (region-beginning) (region-end))
  8472. (buffer-substring (point-at-bol) (point-at-eol))))
  8473. ;; Only use search option if there is some text.
  8474. (when (string-match "\\S-" txt)
  8475. (setq cpltxt
  8476. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8477. desc "NONE")))
  8478. (setq link cpltxt))
  8479. ((org-called-interactively-p 'interactive)
  8480. (user-error "No method for storing a link from this buffer"))
  8481. (t (setq link nil)))
  8482. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8483. (setq link (or link cpltxt)
  8484. desc (or desc cpltxt))
  8485. (cond ((equal desc "NONE") (setq desc nil))
  8486. ((string-match org-bracket-link-regexp desc)
  8487. (setq desc (replace-regexp-in-string
  8488. org-bracket-link-regexp
  8489. (concat "\\3" (if (equal (length (match-string 0 desc))
  8490. (length desc)) "*" "")) desc))))
  8491. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8492. (progn
  8493. (setq org-stored-links
  8494. (cons (list link desc) org-stored-links))
  8495. (message "Stored: %s" (or desc link))
  8496. (when custom-id
  8497. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8498. "::#" custom-id))
  8499. (setq org-stored-links
  8500. (cons (list link desc) org-stored-links))))
  8501. (or agenda-link (and link (org-make-link-string link desc)))))))
  8502. (defun org-store-link-props (&rest plist)
  8503. "Store link properties, extract names and addresses."
  8504. (let (x adr)
  8505. (when (setq x (plist-get plist :from))
  8506. (setq adr (mail-extract-address-components x))
  8507. (setq plist (plist-put plist :fromname (car adr)))
  8508. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8509. (when (setq x (plist-get plist :to))
  8510. (setq adr (mail-extract-address-components x))
  8511. (setq plist (plist-put plist :toname (car adr)))
  8512. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8513. (let ((from (plist-get plist :from))
  8514. (to (plist-get plist :to)))
  8515. (when (and from to org-from-is-user-regexp)
  8516. (setq plist
  8517. (plist-put plist :fromto
  8518. (if (string-match org-from-is-user-regexp from)
  8519. (concat "to %t")
  8520. (concat "from %f"))))))
  8521. (setq org-store-link-plist plist))
  8522. (defun org-add-link-props (&rest plist)
  8523. "Add these properties to the link property list."
  8524. (let (key value)
  8525. (while plist
  8526. (setq key (pop plist) value (pop plist))
  8527. (setq org-store-link-plist
  8528. (plist-put org-store-link-plist key value)))))
  8529. (defun org-email-link-description (&optional fmt)
  8530. "Return the description part of an email link.
  8531. This takes information from `org-store-link-plist' and formats it
  8532. according to FMT (default from `org-email-link-description-format')."
  8533. (setq fmt (or fmt org-email-link-description-format))
  8534. (let* ((p org-store-link-plist)
  8535. (to (plist-get p :toaddress))
  8536. (from (plist-get p :fromaddress))
  8537. (table
  8538. (list
  8539. (cons "%c" (plist-get p :fromto))
  8540. (cons "%F" (plist-get p :from))
  8541. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8542. (cons "%T" (plist-get p :to))
  8543. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8544. (cons "%s" (plist-get p :subject))
  8545. (cons "%d" (plist-get p :date))
  8546. (cons "%m" (plist-get p :message-id)))))
  8547. (when (string-match "%c" fmt)
  8548. ;; Check if the user wrote this message
  8549. (if (and org-from-is-user-regexp from to
  8550. (save-match-data (string-match org-from-is-user-regexp from)))
  8551. (setq fmt (replace-match "to %t" t t fmt))
  8552. (setq fmt (replace-match "from %f" t t fmt))))
  8553. (org-replace-escapes fmt table)))
  8554. (defun org-make-org-heading-search-string (&optional string)
  8555. "Make search string for the current headline or STRING."
  8556. (let ((s (or string
  8557. (and (derived-mode-p 'org-mode)
  8558. (save-excursion
  8559. (org-back-to-heading t)
  8560. (org-element-property :raw-value (org-element-at-point))))))
  8561. (lines org-context-in-file-links))
  8562. (or string (setq s (concat "*" s))) ; Add * for headlines
  8563. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8564. (when (and string (integerp lines) (> lines 0))
  8565. (let ((slines (org-split-string s "\n")))
  8566. (when (< lines (length slines))
  8567. (setq s (mapconcat
  8568. 'identity
  8569. (reverse (nthcdr (- (length slines) lines)
  8570. (reverse slines))) "\n")))))
  8571. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8572. (defun org-make-link-string (link &optional description)
  8573. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8574. (unless (string-match "\\S-" link)
  8575. (error "Empty link"))
  8576. (when (and description
  8577. (stringp description)
  8578. (not (string-match "\\S-" description)))
  8579. (setq description nil))
  8580. (when (stringp description)
  8581. ;; Remove brackets from the description, they are fatal.
  8582. (while (string-match "\\[" description)
  8583. (setq description (replace-match "{" t t description)))
  8584. (while (string-match "\\]" description)
  8585. (setq description (replace-match "}" t t description))))
  8586. (when (equal link description)
  8587. ;; No description needed, it is identical
  8588. (setq description nil))
  8589. (when (and (not description)
  8590. (not (string-match (org-image-file-name-regexp) link))
  8591. (not (equal link (org-link-escape link))))
  8592. (setq description (org-extract-attributes link)))
  8593. (setq link
  8594. (cond ((string-match (org-image-file-name-regexp) link) link)
  8595. ((string-match org-link-types-re link)
  8596. (concat (match-string 1 link)
  8597. (org-link-escape (substring link (match-end 1)))))
  8598. (t (org-link-escape link))))
  8599. (concat "[[" link "]"
  8600. (if description (concat "[" description "]") "")
  8601. "]"))
  8602. (defconst org-link-escape-chars
  8603. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8604. "List of characters that should be escaped in link.
  8605. This is the list that is used for internal purposes.")
  8606. (defconst org-link-escape-chars-browser
  8607. '(?\ )
  8608. "List of escapes for characters that are problematic in links.
  8609. This is the list that is used before handing over to the browser.")
  8610. (defun org-link-escape (text &optional table merge)
  8611. "Return percent escaped representation of TEXT.
  8612. TEXT is a string with the text to escape.
  8613. Optional argument TABLE is a list with characters that should be
  8614. escaped. When nil, `org-link-escape-chars' is used.
  8615. If optional argument MERGE is set, merge TABLE into
  8616. `org-link-escape-chars'."
  8617. (cond
  8618. ((and table merge)
  8619. (mapc (lambda (defchr)
  8620. (unless (member defchr table)
  8621. (setq table (cons defchr table)))) org-link-escape-chars))
  8622. ((null table)
  8623. (setq table org-link-escape-chars)))
  8624. (mapconcat
  8625. (lambda (char)
  8626. (if (or (member char table)
  8627. (and (or (< char 32) (= char 37) (> char 126))
  8628. org-url-hexify-p))
  8629. (mapconcat (lambda (sequence-element)
  8630. (format "%%%.2X" sequence-element))
  8631. (or (encode-coding-char char 'utf-8)
  8632. (error "Unable to percent escape character: %s"
  8633. (char-to-string char))) "")
  8634. (char-to-string char))) text ""))
  8635. (defun org-link-unescape (str)
  8636. "Unhex hexified Unicode strings as returned from the JavaScript function
  8637. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8638. (unless (and (null str) (string= "" str))
  8639. (let ((pos 0) (case-fold-search t) unhexed)
  8640. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8641. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8642. (setq str (replace-match unhexed t t str))
  8643. (setq pos (+ pos (length unhexed))))))
  8644. str)
  8645. (defun org-link-unescape-compound (hex)
  8646. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8647. Note: this function also decodes single byte encodings like
  8648. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8649. (save-match-data
  8650. (let* ((bytes (cdr (split-string hex "%")))
  8651. (ret "")
  8652. (eat 0)
  8653. (sum 0))
  8654. (while bytes
  8655. (let* ((val (string-to-number (pop bytes) 16))
  8656. (shift-xor
  8657. (if (= 0 eat)
  8658. (cond
  8659. ((>= val 252) (cons 6 252))
  8660. ((>= val 248) (cons 5 248))
  8661. ((>= val 240) (cons 4 240))
  8662. ((>= val 224) (cons 3 224))
  8663. ((>= val 192) (cons 2 192))
  8664. (t (cons 0 0)))
  8665. (cons 6 128))))
  8666. (if (>= val 192) (setq eat (car shift-xor)))
  8667. (setq val (logxor val (cdr shift-xor)))
  8668. (setq sum (+ (lsh sum (car shift-xor)) val))
  8669. (if (> eat 0) (setq eat (- eat 1)))
  8670. (cond
  8671. ((= 0 eat) ;multi byte
  8672. (setq ret (concat ret (org-char-to-string sum)))
  8673. (setq sum 0))
  8674. ((not bytes) ; single byte(s)
  8675. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8676. )) ;; end (while bytes
  8677. ret )))
  8678. (defun org-link-unescape-single-byte-sequence (hex)
  8679. "Unhexify hex-encoded single byte character sequences."
  8680. (mapconcat (lambda (byte)
  8681. (char-to-string (string-to-number byte 16)))
  8682. (cdr (split-string hex "%")) ""))
  8683. (defun org-xor (a b)
  8684. "Exclusive or."
  8685. (if a (not b) b))
  8686. (defun org-fixup-message-id-for-http (s)
  8687. "Replace special characters in a message id, so it can be used in an http query."
  8688. (when (string-match "%" s)
  8689. (setq s (mapconcat (lambda (c)
  8690. (if (eq c ?%)
  8691. "%25"
  8692. (char-to-string c)))
  8693. s "")))
  8694. (while (string-match "<" s)
  8695. (setq s (replace-match "%3C" t t s)))
  8696. (while (string-match ">" s)
  8697. (setq s (replace-match "%3E" t t s)))
  8698. (while (string-match "@" s)
  8699. (setq s (replace-match "%40" t t s)))
  8700. s)
  8701. (defun org-link-prettify (link)
  8702. "Return a human-readable representation of LINK.
  8703. The car of LINK must be a raw link the cdr of LINK must be either
  8704. a link description or nil."
  8705. (let ((desc (or (cadr link) "<no description>")))
  8706. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8707. "<" (car link) ">")))
  8708. ;;;###autoload
  8709. (defun org-insert-link-global ()
  8710. "Insert a link like Org-mode does.
  8711. This command can be called in any mode to insert a link in Org-mode syntax."
  8712. (interactive)
  8713. (org-load-modules-maybe)
  8714. (org-run-like-in-org-mode 'org-insert-link))
  8715. (defun org-insert-all-links (&optional keep)
  8716. "Insert all links in `org-stored-links'."
  8717. (interactive "P")
  8718. (let ((links (copy-sequence org-stored-links)) l)
  8719. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8720. (insert "- ")
  8721. (org-insert-link nil (car l) (cadr l))
  8722. (insert "\n"))))
  8723. (defun org-link-fontify-links-to-this-file ()
  8724. "Fontify links to the current file in `org-stored-links'."
  8725. (let ((f (buffer-file-name)) a b)
  8726. (setq a (mapcar (lambda(l)
  8727. (let ((ll (car l)))
  8728. (when (and (string-match "^file:\\(.+\\)::" ll)
  8729. (equal f (expand-file-name (match-string 1 ll))))
  8730. ll)))
  8731. org-stored-links))
  8732. (when (featurep 'org-id)
  8733. (setq b (mapcar (lambda(l)
  8734. (let ((ll (car l)))
  8735. (when (and (string-match "^id:\\(.+\\)$" ll)
  8736. (equal f (expand-file-name
  8737. (or (org-id-find-id-file
  8738. (match-string 1 ll)) ""))))
  8739. ll)))
  8740. org-stored-links)))
  8741. (mapcar (lambda(l)
  8742. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8743. (delq nil (append a b)))))
  8744. (defvar org-link-links-in-this-file nil)
  8745. (defun org-insert-link (&optional complete-file link-location default-description)
  8746. "Insert a link. At the prompt, enter the link.
  8747. Completion can be used to insert any of the link protocol prefixes like
  8748. http or ftp in use.
  8749. The history can be used to select a link previously stored with
  8750. `org-store-link'. When the empty string is entered (i.e. if you just
  8751. press RET at the prompt), the link defaults to the most recently
  8752. stored link. As SPC triggers completion in the minibuffer, you need to
  8753. use M-SPC or C-q SPC to force the insertion of a space character.
  8754. You will also be prompted for a description, and if one is given, it will
  8755. be displayed in the buffer instead of the link.
  8756. If there is already a link at point, this command will allow you to edit link
  8757. and description parts.
  8758. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8759. be selected using completion. The path to the file will be relative to the
  8760. current directory if the file is in the current directory or a subdirectory.
  8761. Otherwise, the link will be the absolute path as completed in the minibuffer
  8762. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8763. option `org-link-file-path-type'.
  8764. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8765. the current directory or below.
  8766. With three \\[universal-argument] prefixes, negate the meaning of
  8767. `org-keep-stored-link-after-insertion'.
  8768. If `org-make-link-description-function' is non-nil, this function will be
  8769. called with the link target, and the result will be the default
  8770. link description.
  8771. If the LINK-LOCATION parameter is non-nil, this value will be
  8772. used as the link location instead of reading one interactively.
  8773. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8774. be used as the default description."
  8775. (interactive "P")
  8776. (let* ((wcf (current-window-configuration))
  8777. (origbuf (current-buffer))
  8778. (region (if (org-region-active-p)
  8779. (buffer-substring (region-beginning) (region-end))))
  8780. (remove (and region (list (region-beginning) (region-end))))
  8781. (desc region)
  8782. tmphist ; byte-compile incorrectly complains about this
  8783. (link link-location)
  8784. (abbrevs org-link-abbrev-alist-local)
  8785. entry file all-prefixes auto-desc)
  8786. (cond
  8787. (link-location) ; specified by arg, just use it.
  8788. ((org-in-regexp org-bracket-link-regexp 1)
  8789. ;; We do have a link at point, and we are going to edit it.
  8790. (setq remove (list (match-beginning 0) (match-end 0)))
  8791. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8792. (setq link (read-string "Link: "
  8793. (org-link-unescape
  8794. (org-match-string-no-properties 1)))))
  8795. ((or (org-in-regexp org-angle-link-re)
  8796. (org-in-regexp org-plain-link-re))
  8797. ;; Convert to bracket link
  8798. (setq remove (list (match-beginning 0) (match-end 0))
  8799. link (read-string "Link: "
  8800. (org-remove-angle-brackets (match-string 0)))))
  8801. ((member complete-file '((4) (16)))
  8802. ;; Completing read for file names.
  8803. (setq link (org-file-complete-link complete-file)))
  8804. (t
  8805. ;; Read link, with completion for stored links.
  8806. (org-link-fontify-links-to-this-file)
  8807. (org-switch-to-buffer-other-window "*Org Links*")
  8808. (with-current-buffer "*Org Links*"
  8809. (erase-buffer)
  8810. (insert "Insert a link.
  8811. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8812. (when org-stored-links
  8813. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8814. (insert (mapconcat 'org-link-prettify
  8815. (reverse org-stored-links) "\n")))
  8816. (goto-char (point-min)))
  8817. (let ((cw (selected-window)))
  8818. (select-window (get-buffer-window "*Org Links*" 'visible))
  8819. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8820. (unless (pos-visible-in-window-p (point-max))
  8821. (org-fit-window-to-buffer))
  8822. (and (window-live-p cw) (select-window cw)))
  8823. ;; Fake a link history, containing the stored links.
  8824. (setq tmphist (append (mapcar 'car org-stored-links)
  8825. org-insert-link-history))
  8826. (setq all-prefixes (append (mapcar 'car abbrevs)
  8827. (mapcar 'car org-link-abbrev-alist)
  8828. org-link-types))
  8829. (unwind-protect
  8830. (progn
  8831. (setq link
  8832. (org-completing-read
  8833. "Link: "
  8834. (append
  8835. (mapcar (lambda (x) (concat x ":"))
  8836. all-prefixes)
  8837. (mapcar 'car org-stored-links))
  8838. nil nil nil
  8839. 'tmphist
  8840. (caar org-stored-links)))
  8841. (if (not (string-match "\\S-" link))
  8842. (error "No link selected"))
  8843. (mapc (lambda(l)
  8844. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8845. org-stored-links)
  8846. (if (or (member link all-prefixes)
  8847. (and (equal ":" (substring link -1))
  8848. (member (substring link 0 -1) all-prefixes)
  8849. (setq link (substring link 0 -1))))
  8850. (setq link (with-current-buffer origbuf
  8851. (org-link-try-special-completion link)))))
  8852. (set-window-configuration wcf)
  8853. (kill-buffer "*Org Links*"))
  8854. (setq entry (assoc link org-stored-links))
  8855. (or entry (push link org-insert-link-history))
  8856. (setq desc (or desc (nth 1 entry)))))
  8857. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8858. (not org-keep-stored-link-after-insertion))
  8859. (setq org-stored-links (delq (assoc link org-stored-links)
  8860. org-stored-links)))
  8861. (if (string-match org-plain-link-re link)
  8862. ;; URL-like link, normalize the use of angular brackets.
  8863. (setq link (org-remove-angle-brackets link)))
  8864. ;; Check if we are linking to the current file with a search
  8865. ;; option If yes, simplify the link by using only the search
  8866. ;; option.
  8867. (when (and buffer-file-name
  8868. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8869. (let* ((path (match-string 1 link))
  8870. (case-fold-search nil)
  8871. (search (match-string 2 link)))
  8872. (save-match-data
  8873. (if (equal (file-truename buffer-file-name) (file-truename path))
  8874. ;; We are linking to this same file, with a search option
  8875. (setq link search)))))
  8876. ;; Check if we can/should use a relative path. If yes, simplify the link
  8877. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8878. (let* ((type (match-string 1 link))
  8879. (path (match-string 2 link))
  8880. (origpath path)
  8881. (case-fold-search nil))
  8882. (cond
  8883. ((or (eq org-link-file-path-type 'absolute)
  8884. (equal complete-file '(16)))
  8885. (setq path (abbreviate-file-name (expand-file-name path))))
  8886. ((eq org-link-file-path-type 'noabbrev)
  8887. (setq path (expand-file-name path)))
  8888. ((eq org-link-file-path-type 'relative)
  8889. (setq path (file-relative-name path)))
  8890. (t
  8891. (save-match-data
  8892. (if (string-match (concat "^" (regexp-quote
  8893. (expand-file-name
  8894. (file-name-as-directory
  8895. default-directory))))
  8896. (expand-file-name path))
  8897. ;; We are linking a file with relative path name.
  8898. (setq path (substring (expand-file-name path)
  8899. (match-end 0)))
  8900. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8901. (setq link (concat type path))
  8902. (if (equal desc origpath)
  8903. (setq desc path))))
  8904. (if org-make-link-description-function
  8905. (setq desc
  8906. (or (condition-case nil
  8907. (funcall org-make-link-description-function link desc)
  8908. (error (progn (message "Can't get link description from `%s'"
  8909. (symbol-name org-make-link-description-function))
  8910. (sit-for 2) nil)))
  8911. (read-string "Description: " default-description)))
  8912. (if default-description (setq desc default-description)
  8913. (setq desc (or (and auto-desc desc)
  8914. (read-string "Description: " desc)))))
  8915. (unless (string-match "\\S-" desc) (setq desc nil))
  8916. (if remove (apply 'delete-region remove))
  8917. (insert (org-make-link-string link desc))))
  8918. (defun org-link-try-special-completion (type)
  8919. "If there is completion support for link type TYPE, offer it."
  8920. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8921. (if (functionp fun)
  8922. (funcall fun)
  8923. (read-string "Link (no completion support): " (concat type ":")))))
  8924. (defun org-file-complete-link (&optional arg)
  8925. "Create a file link using completion."
  8926. (let (file link)
  8927. (setq file (org-iread-file-name "File: "))
  8928. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8929. (pwd1 (file-name-as-directory (abbreviate-file-name
  8930. (expand-file-name ".")))))
  8931. (cond
  8932. ((equal arg '(16))
  8933. (setq link (concat
  8934. "file:"
  8935. (abbreviate-file-name (expand-file-name file)))))
  8936. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8937. (setq link (concat "file:" (match-string 1 file))))
  8938. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8939. (expand-file-name file))
  8940. (setq link (concat
  8941. "file:" (match-string 1 (expand-file-name file)))))
  8942. (t (setq link (concat "file:" file)))))
  8943. link))
  8944. (defun org-iread-file-name (&rest args)
  8945. "Read-file-name using `ido-mode' speedup if available.
  8946. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8947. See `read-file-name' for a description of parameters."
  8948. (org-without-partial-completion
  8949. (if (and org-completion-use-ido
  8950. (fboundp 'ido-read-file-name)
  8951. (boundp 'ido-mode) ido-mode
  8952. (listp (second args)))
  8953. (let ((ido-enter-matching-directory nil))
  8954. (apply 'ido-read-file-name args))
  8955. (apply 'read-file-name args))))
  8956. (defun org-completing-read (&rest args)
  8957. "Completing-read with SPACE being a normal character."
  8958. (let ((enable-recursive-minibuffers t)
  8959. (minibuffer-local-completion-map
  8960. (copy-keymap minibuffer-local-completion-map)))
  8961. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8962. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8963. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8964. (apply 'org-icompleting-read args)))
  8965. (defun org-completing-read-no-i (&rest args)
  8966. (let (org-completion-use-ido org-completion-use-iswitchb)
  8967. (apply 'org-completing-read args)))
  8968. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8969. "Use iswitch as a completing-read replacement to choose from choices.
  8970. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8971. from."
  8972. (let* ((iswitchb-use-virtual-buffers nil)
  8973. (iswitchb-make-buflist-hook
  8974. (lambda ()
  8975. (setq iswitchb-temp-buflist choices))))
  8976. (iswitchb-read-buffer prompt)))
  8977. (defun org-icompleting-read (&rest args)
  8978. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8979. (org-without-partial-completion
  8980. (if (and org-completion-use-ido
  8981. (fboundp 'ido-completing-read)
  8982. (boundp 'ido-mode) ido-mode
  8983. (listp (second args)))
  8984. (let ((ido-enter-matching-directory nil))
  8985. (apply 'ido-completing-read (concat (car args))
  8986. (if (consp (car (nth 1 args)))
  8987. (mapcar 'car (nth 1 args))
  8988. (nth 1 args))
  8989. (cddr args)))
  8990. (if (and org-completion-use-iswitchb
  8991. (boundp 'iswitchb-mode) iswitchb-mode
  8992. (listp (second args)))
  8993. (apply 'org-iswitchb-completing-read (concat (car args))
  8994. (if (consp (car (nth 1 args)))
  8995. (mapcar 'car (nth 1 args))
  8996. (nth 1 args))
  8997. (cddr args))
  8998. (apply 'completing-read args)))))
  8999. (defun org-extract-attributes (s)
  9000. "Extract the attributes cookie from a string and set as text property."
  9001. (let (a attr (start 0) key value)
  9002. (save-match-data
  9003. (when (string-match "{{\\([^}]+\\)}}$" s)
  9004. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9005. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9006. (setq key (match-string 1 a) value (match-string 2 a)
  9007. start (match-end 0)
  9008. attr (plist-put attr (intern key) value))))
  9009. (org-add-props s nil 'org-attr attr))
  9010. s))
  9011. (defun org-extract-attributes-from-string (tag)
  9012. (let (key value attr)
  9013. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  9014. (setq key (match-string 1 tag) value (match-string 2 tag)
  9015. tag (replace-match "" t t tag)
  9016. attr (plist-put attr (intern key) value)))
  9017. (cons tag attr)))
  9018. (defun org-attributes-to-string (plist)
  9019. "Format a property list into an HTML attribute list."
  9020. (let ((s "") key value)
  9021. (while plist
  9022. (setq key (pop plist) value (pop plist))
  9023. (and value
  9024. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  9025. s))
  9026. ;;; Opening/following a link
  9027. (defvar org-link-search-failed nil)
  9028. (defvar org-open-link-functions nil
  9029. "Hook for functions finding a plain text link.
  9030. These functions must take a single argument, the link content.
  9031. They will be called for links that look like [[link text][description]]
  9032. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9033. like a filename (e.g. \"./blue.png\").
  9034. These functions will be called *before* Org attempts to resolve the
  9035. link by doing text searches in the current buffer - so if you want a
  9036. link \"[[target]]\" to still find \"<<target>>\", your function should
  9037. handle this as a special case.
  9038. When the function does handle the link, it must return a non-nil value.
  9039. If it decides that it is not responsible for this link, it must return
  9040. nil to indicate that that Org-mode can continue with other options
  9041. like exact and fuzzy text search.")
  9042. (defun org-next-link (&optional search-backward)
  9043. "Move forward to the next link.
  9044. If the link is in hidden text, expose it."
  9045. (interactive "P")
  9046. (when (and org-link-search-failed (eq this-command last-command))
  9047. (goto-char (point-min))
  9048. (message "Link search wrapped back to beginning of buffer"))
  9049. (setq org-link-search-failed nil)
  9050. (let* ((pos (point))
  9051. (ct (org-context))
  9052. (a (assoc :link ct))
  9053. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9054. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9055. ((looking-at org-any-link-re)
  9056. ;; Don't stay stuck at link without an org-link face
  9057. (forward-char (if search-backward -1 1))))
  9058. (if (funcall srch-fun org-any-link-re nil t)
  9059. (progn
  9060. (goto-char (match-beginning 0))
  9061. (if (outline-invisible-p) (org-show-context)))
  9062. (goto-char pos)
  9063. (setq org-link-search-failed t)
  9064. (message "No further link found"))))
  9065. (defun org-previous-link ()
  9066. "Move backward to the previous link.
  9067. If the link is in hidden text, expose it."
  9068. (interactive)
  9069. (funcall 'org-next-link t))
  9070. (defun org-translate-link (s)
  9071. "Translate a link string if a translation function has been defined."
  9072. (if (and org-link-translation-function
  9073. (fboundp org-link-translation-function)
  9074. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9075. (progn
  9076. (setq s (funcall org-link-translation-function
  9077. (match-string 1 s) (match-string 2 s)))
  9078. (concat (car s) ":" (cdr s)))
  9079. s))
  9080. (defun org-translate-link-from-planner (type path)
  9081. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9082. This is still an experimental function, your mileage may vary."
  9083. (cond
  9084. ((member type '("http" "https" "news" "ftp"))
  9085. ;; standard Internet links are the same.
  9086. nil)
  9087. ((and (equal type "irc") (string-match "^//" path))
  9088. ;; Planner has two / at the beginning of an irc link, we have 1.
  9089. ;; We should have zero, actually....
  9090. (setq path (substring path 1)))
  9091. ((and (equal type "lisp") (string-match "^/" path))
  9092. ;; Planner has a slash, we do not.
  9093. (setq type "elisp" path (substring path 1)))
  9094. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9095. ;; A typical message link. Planner has the id after the final slash,
  9096. ;; we separate it with a hash mark
  9097. (setq path (concat (match-string 1 path) "#"
  9098. (org-remove-angle-brackets (match-string 2 path)))))
  9099. )
  9100. (cons type path))
  9101. (defun org-find-file-at-mouse (ev)
  9102. "Open file link or URL at mouse."
  9103. (interactive "e")
  9104. (mouse-set-point ev)
  9105. (org-open-at-point 'in-emacs))
  9106. (defun org-open-at-mouse (ev)
  9107. "Open file link or URL at mouse.
  9108. See the docstring of `org-open-file' for details."
  9109. (interactive "e")
  9110. (mouse-set-point ev)
  9111. (if (eq major-mode 'org-agenda-mode)
  9112. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9113. (org-open-at-point))
  9114. (defvar org-window-config-before-follow-link nil
  9115. "The window configuration before following a link.
  9116. This is saved in case the need arises to restore it.")
  9117. (defvar org-open-link-marker (make-marker)
  9118. "Marker pointing to the location where `org-open-at-point; was called.")
  9119. ;;;###autoload
  9120. (defun org-open-at-point-global ()
  9121. "Follow a link like Org-mode does.
  9122. This command can be called in any mode to follow a link that has
  9123. Org-mode syntax."
  9124. (interactive)
  9125. (org-run-like-in-org-mode 'org-open-at-point))
  9126. ;;;###autoload
  9127. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9128. "Open a link in the string S, as if it was in Org-mode."
  9129. (interactive "sLink: \nP")
  9130. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9131. (with-temp-buffer
  9132. (let ((org-inhibit-startup (not reference-buffer)))
  9133. (org-mode)
  9134. (insert s)
  9135. (goto-char (point-min))
  9136. (when reference-buffer
  9137. (setq org-link-abbrev-alist-local
  9138. (with-current-buffer reference-buffer
  9139. org-link-abbrev-alist-local)))
  9140. (org-open-at-point arg reference-buffer)))))
  9141. (defvar org-open-at-point-functions nil
  9142. "Hook that is run when following a link at point.
  9143. Functions in this hook must return t if they identify and follow
  9144. a link at point. If they don't find anything interesting at point,
  9145. they must return nil.")
  9146. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9147. (defun org-open-at-point (&optional arg reference-buffer)
  9148. "Open link at or after point.
  9149. If there is no link at point, this function will search forward up to
  9150. the end of the current line.
  9151. Normally, files will be opened by an appropriate application. If the
  9152. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9153. With a double prefix argument, try to open outside of Emacs, in the
  9154. application the system uses for this file type."
  9155. (interactive "P")
  9156. ;; if in a code block, then open the block's results
  9157. (unless (call-interactively #'org-babel-open-src-block-result)
  9158. (org-load-modules-maybe)
  9159. (move-marker org-open-link-marker (point))
  9160. (setq org-window-config-before-follow-link (current-window-configuration))
  9161. (org-remove-occur-highlights nil nil t)
  9162. (cond
  9163. ((and (org-at-heading-p)
  9164. (not (org-at-timestamp-p t))
  9165. (not (org-in-regexp
  9166. (concat org-plain-link-re "\\|"
  9167. org-bracket-link-regexp "\\|"
  9168. org-angle-link-re "\\|"
  9169. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9170. (not (get-text-property (point) 'org-linked-text)))
  9171. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9172. (lk0 (car lkall))
  9173. (lk (if (stringp lk0) (list lk0) lk0))
  9174. (lkend (cdr lkall)))
  9175. (mapcar (lambda(l)
  9176. (search-forward l nil lkend)
  9177. (goto-char (match-beginning 0))
  9178. (org-open-at-point))
  9179. lk))
  9180. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9181. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9182. ((and (org-at-timestamp-p t)
  9183. (not (org-in-regexp org-bracket-link-regexp)))
  9184. (org-follow-timestamp-link))
  9185. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9186. (not (org-in-regexp org-any-link-re)))
  9187. (org-footnote-action))
  9188. (t
  9189. (let (type path link line search (pos (point)))
  9190. (catch 'match
  9191. (save-excursion
  9192. (skip-chars-forward "^]\n\r")
  9193. (when (org-in-regexp org-bracket-link-regexp 1)
  9194. (setq link (org-extract-attributes
  9195. (org-link-unescape (org-match-string-no-properties 1))))
  9196. (while (string-match " *\n *" link)
  9197. (setq link (replace-match " " t t link)))
  9198. (setq link (org-link-expand-abbrev link))
  9199. (cond
  9200. ((or (file-name-absolute-p link)
  9201. (string-match "^\\.\\.?/" link))
  9202. (setq type "file" path link))
  9203. ((string-match org-link-re-with-space3 link)
  9204. (setq type (match-string 1 link) path (match-string 2 link)))
  9205. ((string-match "^help:+\\(.+\\)" link)
  9206. (setq type "help" path (match-string 1 link)))
  9207. (t (setq type "thisfile" path link)))
  9208. (throw 'match t)))
  9209. (when (get-text-property (point) 'org-linked-text)
  9210. (setq type "thisfile"
  9211. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9212. (1+ (point)) (point))
  9213. path (buffer-substring
  9214. (or (previous-single-property-change pos 'org-linked-text)
  9215. (point-min))
  9216. (or (next-single-property-change pos 'org-linked-text)
  9217. (point-max))))
  9218. (throw 'match t))
  9219. (save-excursion
  9220. (when (or (org-in-regexp org-angle-link-re)
  9221. (let ((match (org-in-regexp org-plain-link-re)))
  9222. ;; Check a plain link is not within a bracket link
  9223. (and match
  9224. (save-excursion
  9225. (progn
  9226. (goto-char (car match))
  9227. (not (org-in-regexp org-bracket-link-regexp))))))
  9228. (let ((line_ending (save-excursion (end-of-line) (point))))
  9229. ;; We are in a line before a plain or bracket link
  9230. (or (re-search-forward org-plain-link-re line_ending t)
  9231. (re-search-forward org-bracket-link-regexp line_ending t))))
  9232. (setq type (match-string 1)
  9233. path (org-link-unescape (match-string 2)))
  9234. (throw 'match t)))
  9235. (save-excursion
  9236. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9237. (setq type "tags"
  9238. path (match-string 1))
  9239. (while (string-match ":" path)
  9240. (setq path (replace-match "+" t t path)))
  9241. (throw 'match t)))
  9242. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9243. (setq type "tree-match"
  9244. path (match-string 1))
  9245. (throw 'match t)))
  9246. (unless path
  9247. (user-error "No link found"))
  9248. ;; switch back to reference buffer
  9249. ;; needed when if called in a temporary buffer through
  9250. ;; org-open-link-from-string
  9251. (with-current-buffer (or reference-buffer (current-buffer))
  9252. ;; Remove any trailing spaces in path
  9253. (if (string-match " +\\'" path)
  9254. (setq path (replace-match "" t t path)))
  9255. (if (and org-link-translation-function
  9256. (fboundp org-link-translation-function))
  9257. ;; Check if we need to translate the link
  9258. (let ((tmp (funcall org-link-translation-function type path)))
  9259. (setq type (car tmp) path (cdr tmp))))
  9260. (cond
  9261. ((assoc type org-link-protocols)
  9262. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9263. ((equal type "help")
  9264. (let ((f-or-v (intern path)))
  9265. (cond ((fboundp f-or-v)
  9266. (describe-function f-or-v))
  9267. ((boundp f-or-v)
  9268. (describe-variable f-or-v))
  9269. (t (error "Not a known function or variable")))))
  9270. ((equal type "mailto")
  9271. (let ((cmd (car org-link-mailto-program))
  9272. (args (cdr org-link-mailto-program)) args1
  9273. (address path) (subject "") a)
  9274. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9275. (setq address (match-string 1 path)
  9276. subject (org-link-escape (match-string 2 path))))
  9277. (while args
  9278. (cond
  9279. ((not (stringp (car args))) (push (pop args) args1))
  9280. (t (setq a (pop args))
  9281. (if (string-match "%a" a)
  9282. (setq a (replace-match address t t a)))
  9283. (if (string-match "%s" a)
  9284. (setq a (replace-match subject t t a)))
  9285. (push a args1))))
  9286. (apply cmd (nreverse args1))))
  9287. ((member type '("http" "https" "ftp" "news"))
  9288. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9289. (org-link-escape
  9290. path org-link-escape-chars-browser)
  9291. path))))
  9292. ((string= type "doi")
  9293. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9294. (org-link-escape
  9295. path org-link-escape-chars-browser)
  9296. path))))
  9297. ((member type '("message"))
  9298. (browse-url (concat type ":" path)))
  9299. ((string= type "tags")
  9300. (org-tags-view arg path))
  9301. ((string= type "tree-match")
  9302. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9303. ((string= type "file")
  9304. (if (string-match "::\\([0-9]+\\)\\'" path)
  9305. (setq line (string-to-number (match-string 1 path))
  9306. path (substring path 0 (match-beginning 0)))
  9307. (if (string-match "::\\(.+\\)\\'" path)
  9308. (setq search (match-string 1 path)
  9309. path (substring path 0 (match-beginning 0)))))
  9310. (if (string-match "[*?{]" (file-name-nondirectory path))
  9311. (dired path)
  9312. (org-open-file path arg line search)))
  9313. ((string= type "shell")
  9314. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9315. (cmd path))
  9316. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9317. (string-match org-confirm-shell-link-not-regexp cmd))
  9318. (not org-confirm-shell-link-function)
  9319. (funcall org-confirm-shell-link-function
  9320. (format "Execute \"%s\" in shell? "
  9321. (org-add-props cmd nil
  9322. 'face 'org-warning))))
  9323. (progn
  9324. (message "Executing %s" cmd)
  9325. (shell-command cmd buf)
  9326. (if (featurep 'midnight)
  9327. (setq clean-buffer-list-kill-buffer-names
  9328. (cons buf clean-buffer-list-kill-buffer-names))))
  9329. (error "Abort"))))
  9330. ((string= type "elisp")
  9331. (let ((cmd path))
  9332. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9333. (string-match org-confirm-elisp-link-not-regexp cmd))
  9334. (not org-confirm-elisp-link-function)
  9335. (funcall org-confirm-elisp-link-function
  9336. (format "Execute \"%s\" as elisp? "
  9337. (org-add-props cmd nil
  9338. 'face 'org-warning))))
  9339. (message "%s => %s" cmd
  9340. (if (equal (string-to-char cmd) ?\()
  9341. (eval (read cmd))
  9342. (call-interactively (read cmd))))
  9343. (error "Abort"))))
  9344. ((and (string= type "thisfile")
  9345. (run-hook-with-args-until-success
  9346. 'org-open-link-functions path)))
  9347. ((string= type "thisfile")
  9348. (if arg
  9349. (switch-to-buffer-other-window
  9350. (org-get-buffer-for-internal-link (current-buffer)))
  9351. (org-mark-ring-push))
  9352. (let ((cmd `(org-link-search
  9353. ,path
  9354. ,(cond ((equal arg '(4)) ''occur)
  9355. ((equal arg '(16)) ''org-occur))
  9356. ,pos)))
  9357. (condition-case nil (let ((org-link-search-inhibit-query t))
  9358. (eval cmd))
  9359. (error (progn (widen) (eval cmd))))))
  9360. (t (browse-url-at-point)))))))
  9361. (move-marker org-open-link-marker nil)
  9362. (run-hook-with-args 'org-follow-link-hook)))
  9363. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9364. "Offer links in the current entry and return the selected link.
  9365. If there is only one link, return it.
  9366. If NTH is an integer, return the NTH link found.
  9367. If ZERO is a string, check also this string for a link, and if
  9368. there is one, return it."
  9369. (with-current-buffer buffer
  9370. (save-excursion
  9371. (save-restriction
  9372. (widen)
  9373. (goto-char marker)
  9374. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9375. "\\(" org-angle-link-re "\\)\\|"
  9376. "\\(" org-plain-link-re "\\)"))
  9377. (cnt ?0)
  9378. (in-emacs (if (integerp nth) nil nth))
  9379. have-zero end links link c)
  9380. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9381. (push (match-string 0 zero) links)
  9382. (setq cnt (1- cnt) have-zero t))
  9383. (save-excursion
  9384. (org-back-to-heading t)
  9385. (setq end (save-excursion (outline-next-heading) (point)))
  9386. (while (re-search-forward re end t)
  9387. (push (match-string 0) links))
  9388. (setq links (org-uniquify (reverse links))))
  9389. (cond
  9390. ((null links)
  9391. (message "No links"))
  9392. ((equal (length links) 1)
  9393. (setq link (car links)))
  9394. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9395. (setq link (nth (if have-zero nth (1- nth)) links)))
  9396. (t ; we have to select a link
  9397. (save-excursion
  9398. (save-window-excursion
  9399. (delete-other-windows)
  9400. (with-output-to-temp-buffer "*Select Link*"
  9401. (mapc (lambda (l)
  9402. (if (not (string-match org-bracket-link-regexp l))
  9403. (princ (format "[%c] %s\n" (incf cnt)
  9404. (org-remove-angle-brackets l)))
  9405. (if (match-end 3)
  9406. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9407. (match-string 3 l) (match-string 1 l)))
  9408. (princ (format "[%c] %s\n" (incf cnt)
  9409. (match-string 1 l))))))
  9410. links))
  9411. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9412. (message "Select link to open, RET to open all:")
  9413. (setq c (read-char-exclusive))
  9414. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9415. (when (equal c ?q) (error "Abort"))
  9416. (if (equal c ?\C-m)
  9417. (setq link links)
  9418. (setq nth (- c ?0))
  9419. (if have-zero (setq nth (1+ nth)))
  9420. (unless (and (integerp nth) (>= (length links) nth))
  9421. (error "Invalid link selection"))
  9422. (setq link (nth (1- nth) links)))))
  9423. (cons link end))))))
  9424. ;; Add special file links that specify the way of opening
  9425. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9426. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9427. (defun org-open-file-with-system (path)
  9428. "Open file at PATH using the system way of opening it."
  9429. (org-open-file path 'system))
  9430. (defun org-open-file-with-emacs (path)
  9431. "Open file at PATH in Emacs."
  9432. (org-open-file path 'emacs))
  9433. ;;; File search
  9434. (defvar org-create-file-search-functions nil
  9435. "List of functions to construct the right search string for a file link.
  9436. These functions are called in turn with point at the location to
  9437. which the link should point.
  9438. A function in the hook should first test if it would like to
  9439. handle this file type, for example by checking the `major-mode'
  9440. or the file extension. If it decides not to handle this file, it
  9441. should just return nil to give other functions a chance. If it
  9442. does handle the file, it must return the search string to be used
  9443. when following the link. The search string will be part of the
  9444. file link, given after a double colon, and `org-open-at-point'
  9445. will automatically search for it. If special measures must be
  9446. taken to make the search successful, another function should be
  9447. added to the companion hook `org-execute-file-search-functions',
  9448. which see.
  9449. A function in this hook may also use `setq' to set the variable
  9450. `description' to provide a suggestion for the descriptive text to
  9451. be used for this link when it gets inserted into an Org-mode
  9452. buffer with \\[org-insert-link].")
  9453. (defvar org-execute-file-search-functions nil
  9454. "List of functions to execute a file search triggered by a link.
  9455. Functions added to this hook must accept a single argument, the
  9456. search string that was part of the file link, the part after the
  9457. double colon. The function must first check if it would like to
  9458. handle this search, for example by checking the `major-mode' or
  9459. the file extension. If it decides not to handle this search, it
  9460. should just return nil to give other functions a chance. If it
  9461. does handle the search, it must return a non-nil value to keep
  9462. other functions from trying.
  9463. Each function can access the current prefix argument through the
  9464. variable `current-prefix-arg'. Note that a single prefix is used
  9465. to force opening a link in Emacs, so it may be good to only use a
  9466. numeric or double prefix to guide the search function.
  9467. In case this is needed, a function in this hook can also restore
  9468. the window configuration before `org-open-at-point' was called using:
  9469. (set-window-configuration org-window-config-before-follow-link)")
  9470. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9471. (defun org-link-search (s &optional type avoid-pos stealth)
  9472. "Search for a link search option.
  9473. If S is surrounded by forward slashes, it is interpreted as a
  9474. regular expression. In org-mode files, this will create an `org-occur'
  9475. sparse tree. In ordinary files, `occur' will be used to list matches.
  9476. If the current buffer is in `dired-mode', grep will be used to search
  9477. in all files. If AVOID-POS is given, ignore matches near that position.
  9478. When optional argument STEALTH is non-nil, do not modify
  9479. visibility around point, thus ignoring
  9480. `org-show-hierarchy-above', `org-show-following-heading' and
  9481. `org-show-siblings' variables."
  9482. (let ((case-fold-search t)
  9483. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9484. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9485. (append '(("") (" ") ("\t") ("\n"))
  9486. org-emphasis-alist)
  9487. "\\|") "\\)"))
  9488. (pos (point))
  9489. (pre nil) (post nil)
  9490. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9491. (cond
  9492. ;; First check if there are any special search functions
  9493. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9494. ;; Now try the builtin stuff
  9495. ((and (equal (string-to-char s0) ?#)
  9496. (> (length s0) 1)
  9497. (save-excursion
  9498. (goto-char (point-min))
  9499. (and
  9500. (re-search-forward
  9501. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9502. (setq type 'dedicated
  9503. pos (match-beginning 0))))
  9504. ;; There is an exact target for this
  9505. (goto-char pos)
  9506. (org-back-to-heading t)))
  9507. ((save-excursion
  9508. (goto-char (point-min))
  9509. (and
  9510. (re-search-forward
  9511. (concat "<<" (regexp-quote s0) ">>") nil t)
  9512. (setq type 'dedicated
  9513. pos (match-beginning 0))))
  9514. ;; There is an exact target for this
  9515. (goto-char pos))
  9516. ((save-excursion
  9517. (goto-char (point-min))
  9518. (and
  9519. (re-search-forward
  9520. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9521. (setq type 'dedicated pos (match-beginning 0))))
  9522. ;; Found an invisible target.
  9523. (goto-char pos))
  9524. ((save-excursion
  9525. (goto-char (point-min))
  9526. (and
  9527. (re-search-forward
  9528. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9529. (setq type 'dedicated pos (match-beginning 0))))
  9530. ;; Found an element with a matching #+name affiliated keyword.
  9531. (goto-char pos))
  9532. ((and (string-match "^(\\(.*\\))$" s0)
  9533. (save-excursion
  9534. (goto-char (point-min))
  9535. (and
  9536. (re-search-forward
  9537. (concat "[^[]" (regexp-quote
  9538. (format org-coderef-label-format
  9539. (match-string 1 s0))))
  9540. nil t)
  9541. (setq type 'dedicated
  9542. pos (1+ (match-beginning 0))))))
  9543. ;; There is a coderef target for this
  9544. (goto-char pos))
  9545. ((string-match "^/\\(.*\\)/$" s)
  9546. ;; A regular expression
  9547. (cond
  9548. ((derived-mode-p 'org-mode)
  9549. (org-occur (match-string 1 s)))
  9550. (t (org-do-occur (match-string 1 s)))))
  9551. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9552. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9553. (goto-char (point-min))
  9554. (cond
  9555. ((let (case-fold-search)
  9556. (re-search-forward (format org-complex-heading-regexp-format
  9557. (regexp-quote s))
  9558. nil t))
  9559. ;; OK, found a match
  9560. (setq type 'dedicated)
  9561. (goto-char (match-beginning 0)))
  9562. ((and (not org-link-search-inhibit-query)
  9563. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9564. (y-or-n-p "No match - create this as a new heading? "))
  9565. (goto-char (point-max))
  9566. (or (bolp) (newline))
  9567. (insert "* " s "\n")
  9568. (beginning-of-line 0))
  9569. (t
  9570. (goto-char pos)
  9571. (error "No match"))))
  9572. (t
  9573. ;; A normal search string
  9574. (when (equal (string-to-char s) ?*)
  9575. ;; Anchor on headlines, post may include tags.
  9576. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9577. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9578. s (substring s 1)))
  9579. (remove-text-properties
  9580. 0 (length s)
  9581. '(face nil mouse-face nil keymap nil fontified nil) s)
  9582. ;; Make a series of regular expressions to find a match
  9583. (setq words (org-split-string s "[ \n\r\t]+")
  9584. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9585. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9586. "\\)" markers)
  9587. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9588. re2a (concat "[ \t\r\n]" re2a_)
  9589. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9590. re4 (concat "[^a-zA-Z_]" re4_)
  9591. re1 (concat pre re2 post)
  9592. re3 (concat pre (if pre re4_ re4) post)
  9593. re5 (concat pre ".*" re4)
  9594. re2 (concat pre re2)
  9595. re2a (concat pre (if pre re2a_ re2a))
  9596. re4 (concat pre (if pre re4_ re4))
  9597. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9598. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9599. re5 "\\)"
  9600. ))
  9601. (cond
  9602. ((eq type 'org-occur) (org-occur reall))
  9603. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9604. (t (goto-char (point-min))
  9605. (setq type 'fuzzy)
  9606. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9607. (org-search-not-self 1 re1 nil t)
  9608. (org-search-not-self 1 re2 nil t)
  9609. (org-search-not-self 1 re2a nil t)
  9610. (org-search-not-self 1 re3 nil t)
  9611. (org-search-not-self 1 re4 nil t)
  9612. (org-search-not-self 1 re5 nil t)
  9613. )
  9614. (goto-char (match-beginning 1))
  9615. (goto-char pos)
  9616. (error "No match"))))))
  9617. (and (derived-mode-p 'org-mode)
  9618. (not stealth)
  9619. (org-show-context 'link-search))
  9620. type))
  9621. (defun org-search-not-self (group &rest args)
  9622. "Execute `re-search-forward', but only accept matches that do not
  9623. enclose the position of `org-open-link-marker'."
  9624. (let ((m org-open-link-marker))
  9625. (catch 'exit
  9626. (while (apply 're-search-forward args)
  9627. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9628. (goto-char (match-end group))
  9629. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9630. (> (match-beginning 0) (marker-position m))
  9631. (< (match-end 0) (marker-position m)))
  9632. (save-match-data
  9633. (or (not (org-in-regexp
  9634. org-bracket-link-analytic-regexp 1))
  9635. (not (match-end 4)) ; no description
  9636. (and (<= (match-beginning 4) (point))
  9637. (>= (match-end 4) (point))))))
  9638. (throw 'exit (point))))))))
  9639. (defun org-get-buffer-for-internal-link (buffer)
  9640. "Return a buffer to be used for displaying the link target of internal links."
  9641. (cond
  9642. ((not org-display-internal-link-with-indirect-buffer)
  9643. buffer)
  9644. ((string-match "(Clone)$" (buffer-name buffer))
  9645. (message "Buffer is already a clone, not making another one")
  9646. ;; we also do not modify visibility in this case
  9647. buffer)
  9648. (t ; make a new indirect buffer for displaying the link
  9649. (let* ((bn (buffer-name buffer))
  9650. (ibn (concat bn "(Clone)"))
  9651. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9652. (with-current-buffer ib (org-overview))
  9653. ib))))
  9654. (defun org-do-occur (regexp &optional cleanup)
  9655. "Call the Emacs command `occur'.
  9656. If CLEANUP is non-nil, remove the printout of the regular expression
  9657. in the *Occur* buffer. This is useful if the regex is long and not useful
  9658. to read."
  9659. (occur regexp)
  9660. (when cleanup
  9661. (let ((cwin (selected-window)) win beg end)
  9662. (when (setq win (get-buffer-window "*Occur*"))
  9663. (select-window win))
  9664. (goto-char (point-min))
  9665. (when (re-search-forward "match[a-z]+" nil t)
  9666. (setq beg (match-end 0))
  9667. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9668. (setq end (1- (match-beginning 0)))))
  9669. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9670. (goto-char (point-min))
  9671. (select-window cwin))))
  9672. ;;; The mark ring for links jumps
  9673. (defvar org-mark-ring nil
  9674. "Mark ring for positions before jumps in Org-mode.")
  9675. (defvar org-mark-ring-last-goto nil
  9676. "Last position in the mark ring used to go back.")
  9677. ;; Fill and close the ring
  9678. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9679. (loop for i from 1 to org-mark-ring-length do
  9680. (push (make-marker) org-mark-ring))
  9681. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9682. org-mark-ring)
  9683. (defun org-mark-ring-push (&optional pos buffer)
  9684. "Put the current position or POS into the mark ring and rotate it."
  9685. (interactive)
  9686. (setq pos (or pos (point)))
  9687. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9688. (move-marker (car org-mark-ring)
  9689. (or pos (point))
  9690. (or buffer (current-buffer)))
  9691. (message "%s"
  9692. (substitute-command-keys
  9693. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9694. (defun org-mark-ring-goto (&optional n)
  9695. "Jump to the previous position in the mark ring.
  9696. With prefix arg N, jump back that many stored positions. When
  9697. called several times in succession, walk through the entire ring.
  9698. Org-mode commands jumping to a different position in the current file,
  9699. or to another Org-mode file, automatically push the old position
  9700. onto the ring."
  9701. (interactive "p")
  9702. (let (p m)
  9703. (if (eq last-command this-command)
  9704. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9705. (setq p org-mark-ring))
  9706. (setq org-mark-ring-last-goto p)
  9707. (setq m (car p))
  9708. (org-pop-to-buffer-same-window (marker-buffer m))
  9709. (goto-char m)
  9710. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9711. (defun org-remove-angle-brackets (s)
  9712. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9713. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9714. s)
  9715. (defun org-add-angle-brackets (s)
  9716. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9717. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9718. s)
  9719. (defun org-remove-double-quotes (s)
  9720. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9721. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9722. s)
  9723. ;;; Following specific links
  9724. (defun org-follow-timestamp-link ()
  9725. "Open an agenda view for the time-stamp date/range at point."
  9726. (cond
  9727. ((org-at-date-range-p t)
  9728. (let ((org-agenda-start-on-weekday)
  9729. (t1 (match-string 1))
  9730. (t2 (match-string 2)) tt1 tt2)
  9731. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9732. tt2 (time-to-days (org-time-string-to-time t2)))
  9733. (let ((org-agenda-buffer-tmp-name
  9734. (format "*Org Agenda(a:%s)"
  9735. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9736. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9737. ((org-at-timestamp-p t)
  9738. (let ((org-agenda-buffer-tmp-name
  9739. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9740. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9741. (substring (match-string 1) 0 10)))
  9742. 1)))
  9743. (t (error "This should not happen"))))
  9744. ;;; Following file links
  9745. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9746. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9747. (declare-function mailcap-mime-info
  9748. "mailcap" (string &optional request no-decode))
  9749. (defvar org-wait nil)
  9750. (defun org-open-file (path &optional in-emacs line search)
  9751. "Open the file at PATH.
  9752. First, this expands any special file name abbreviations. Then the
  9753. configuration variable `org-file-apps' is checked if it contains an
  9754. entry for this file type, and if yes, the corresponding command is launched.
  9755. If no application is found, Emacs simply visits the file.
  9756. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9757. With a double \\[universal-argument] \\[universal-argument] \
  9758. prefix arg, Org tries to avoid opening in Emacs
  9759. and to use an external application to visit the file.
  9760. Optional LINE specifies a line to go to, optional SEARCH a string
  9761. to search for. If LINE or SEARCH is given, the file will be
  9762. opened in Emacs, unless an entry from org-file-apps that makes
  9763. use of groups in a regexp matches.
  9764. If you want to change the way frames are used when following a
  9765. link, please customize `org-link-frame-setup'.
  9766. If the file does not exist, an error is thrown."
  9767. (let* ((file (if (equal path "")
  9768. buffer-file-name
  9769. (substitute-in-file-name (expand-file-name path))))
  9770. (file-apps (append org-file-apps (org-default-apps)))
  9771. (apps (org-remove-if
  9772. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9773. (apps-dlink (org-remove-if-not
  9774. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9775. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9776. (dirp (if remp nil (file-directory-p file)))
  9777. (file (if (and dirp org-open-directory-means-index-dot-org)
  9778. (concat (file-name-as-directory file) "index.org")
  9779. file))
  9780. (a-m-a-p (assq 'auto-mode apps))
  9781. (dfile (downcase file))
  9782. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9783. (link (cond ((and (eq line nil)
  9784. (eq search nil))
  9785. file)
  9786. (line
  9787. (concat file "::" (number-to-string line)))
  9788. (search
  9789. (concat file "::" search))))
  9790. (dlink (downcase link))
  9791. (old-buffer (current-buffer))
  9792. (old-pos (point))
  9793. (old-mode major-mode)
  9794. ext cmd link-match-data)
  9795. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9796. (setq ext (match-string 1 dfile))
  9797. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9798. (setq ext (match-string 1 dfile))))
  9799. (cond
  9800. ((member in-emacs '((16) system))
  9801. (setq cmd (cdr (assoc 'system apps))))
  9802. (in-emacs (setq cmd 'emacs))
  9803. (t
  9804. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9805. (and dirp (cdr (assoc 'directory apps)))
  9806. ; first, try matching against apps-dlink
  9807. ; if we get a match here, store the match data for later
  9808. (let ((match (assoc-default dlink apps-dlink
  9809. 'string-match)))
  9810. (if match
  9811. (progn (setq link-match-data (match-data))
  9812. match)
  9813. (progn (setq in-emacs (or in-emacs line search))
  9814. nil))) ; if we have no match in apps-dlink,
  9815. ; always open the file in emacs if line or search
  9816. ; is given (for backwards compatibility)
  9817. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9818. 'string-match)
  9819. (cdr (assoc ext apps))
  9820. (cdr (assoc t apps))))))
  9821. (when (eq cmd 'system)
  9822. (setq cmd (cdr (assoc 'system apps))))
  9823. (when (eq cmd 'default)
  9824. (setq cmd (cdr (assoc t apps))))
  9825. (when (eq cmd 'mailcap)
  9826. (require 'mailcap)
  9827. (mailcap-parse-mailcaps)
  9828. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9829. (command (mailcap-mime-info mime-type)))
  9830. (if (stringp command)
  9831. (setq cmd command)
  9832. (setq cmd 'emacs))))
  9833. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9834. (not (file-exists-p file))
  9835. (not org-open-non-existing-files))
  9836. (error "No such file: %s" file))
  9837. (cond
  9838. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9839. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9840. (while (string-match "['\"]%s['\"]" cmd)
  9841. (setq cmd (replace-match "%s" t t cmd)))
  9842. (while (string-match "%s" cmd)
  9843. (setq cmd (replace-match
  9844. (save-match-data
  9845. (shell-quote-argument
  9846. (convert-standard-filename file)))
  9847. t t cmd)))
  9848. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9849. (save-match-data
  9850. (let ((match-index 1)
  9851. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9852. (set-match-data link-match-data)
  9853. (while (<= match-index number-of-groups)
  9854. (let ((regex (concat "%" (number-to-string match-index)))
  9855. (replace-with (match-string match-index dlink)))
  9856. (while (string-match regex cmd)
  9857. (setq cmd (replace-match replace-with t t cmd))))
  9858. (setq match-index (+ match-index 1)))))
  9859. (save-window-excursion
  9860. (message "Running %s...done" cmd)
  9861. (start-process-shell-command cmd nil cmd)
  9862. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9863. ((or (stringp cmd)
  9864. (eq cmd 'emacs))
  9865. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9866. (widen)
  9867. (if line (org-goto-line line)
  9868. (if search (org-link-search search))))
  9869. ((consp cmd)
  9870. (let ((file (convert-standard-filename file)))
  9871. (save-match-data
  9872. (set-match-data link-match-data)
  9873. (eval cmd))))
  9874. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9875. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9876. (or (not (equal old-buffer (current-buffer)))
  9877. (not (equal old-pos (point))))
  9878. (org-mark-ring-push old-pos old-buffer))))
  9879. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9880. "This function returns non-nil if `entry' uses a regular
  9881. expression which should be matched against the whole link by
  9882. org-open-file.
  9883. It assumes that is the case when the entry uses a regular
  9884. expression which has at least one grouping construct and the
  9885. action is either a lisp form or a command string containing
  9886. '%1', i.e. using at least one subexpression match as a
  9887. parameter."
  9888. (let ((selector (car entry))
  9889. (action (cdr entry)))
  9890. (if (stringp selector)
  9891. (and (> (regexp-opt-depth selector) 0)
  9892. (or (and (stringp action)
  9893. (string-match "%[0-9]" action))
  9894. (consp action)))
  9895. nil)))
  9896. (defun org-default-apps ()
  9897. "Return the default applications for this operating system."
  9898. (cond
  9899. ((eq system-type 'darwin)
  9900. org-file-apps-defaults-macosx)
  9901. ((eq system-type 'windows-nt)
  9902. org-file-apps-defaults-windowsnt)
  9903. (t org-file-apps-defaults-gnu)))
  9904. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9905. "Convert extensions to regular expressions in the cars of LIST.
  9906. Also, weed out any non-string entries, because the return value is used
  9907. only for regexp matching.
  9908. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9909. point to the symbol `emacs', indicating that the file should
  9910. be opened in Emacs."
  9911. (append
  9912. (delq nil
  9913. (mapcar (lambda (x)
  9914. (if (not (stringp (car x)))
  9915. nil
  9916. (if (string-match "\\W" (car x))
  9917. x
  9918. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9919. list))
  9920. (if add-auto-mode
  9921. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9922. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9923. (defun org-file-remote-p (file)
  9924. "Test whether FILE specifies a location on a remote system.
  9925. Return non-nil if the location is indeed remote.
  9926. For example, the filename \"/user@host:/foo\" specifies a location
  9927. on the system \"/user@host:\"."
  9928. (cond ((fboundp 'file-remote-p)
  9929. (file-remote-p file))
  9930. ((fboundp 'tramp-handle-file-remote-p)
  9931. (tramp-handle-file-remote-p file))
  9932. ((and (boundp 'ange-ftp-name-format)
  9933. (string-match (car ange-ftp-name-format) file))
  9934. t)))
  9935. ;;;; Refiling
  9936. (defun org-get-org-file ()
  9937. "Read a filename, with default directory `org-directory'."
  9938. (let ((default (or org-default-notes-file remember-data-file)))
  9939. (read-file-name (format "File name [%s]: " default)
  9940. (file-name-as-directory org-directory)
  9941. default)))
  9942. (defun org-notes-order-reversed-p ()
  9943. "Check if the current file should receive notes in reversed order."
  9944. (cond
  9945. ((not org-reverse-note-order) nil)
  9946. ((eq t org-reverse-note-order) t)
  9947. ((not (listp org-reverse-note-order)) nil)
  9948. (t (catch 'exit
  9949. (let ((all org-reverse-note-order)
  9950. entry)
  9951. (while (setq entry (pop all))
  9952. (if (string-match (car entry) buffer-file-name)
  9953. (throw 'exit (cdr entry))))
  9954. nil)))))
  9955. (defvar org-refile-target-table nil
  9956. "The list of refile targets, created by `org-refile'.")
  9957. (defvar org-agenda-new-buffers nil
  9958. "Buffers created to visit agenda files.")
  9959. (defvar org-refile-cache nil
  9960. "Cache for refile targets.")
  9961. (defvar org-refile-markers nil
  9962. "All the markers used for caching refile locations.")
  9963. (defun org-refile-marker (pos)
  9964. "Get a new refile marker, but only if caching is in use."
  9965. (if (not org-refile-use-cache)
  9966. pos
  9967. (let ((m (make-marker)))
  9968. (move-marker m pos)
  9969. (push m org-refile-markers)
  9970. m)))
  9971. (defun org-refile-cache-clear ()
  9972. "Clear the refile cache and disable all the markers."
  9973. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9974. (setq org-refile-markers nil)
  9975. (setq org-refile-cache nil)
  9976. (message "Refile cache has been cleared"))
  9977. (defun org-refile-cache-check-set (set)
  9978. "Check if all the markers in the cache still have live buffers."
  9979. (let (marker)
  9980. (catch 'exit
  9981. (while (and set (setq marker (nth 3 (pop set))))
  9982. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9983. (when (and marker (null (marker-buffer marker)))
  9984. (message "not found") (sit-for 3)
  9985. (throw 'exit nil)))
  9986. t)))
  9987. (defun org-refile-cache-put (set &rest identifiers)
  9988. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9989. (let* ((key (sha1 (prin1-to-string identifiers)))
  9990. (entry (assoc key org-refile-cache)))
  9991. (if entry
  9992. (setcdr entry set)
  9993. (push (cons key set) org-refile-cache))))
  9994. (defun org-refile-cache-get (&rest identifiers)
  9995. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9996. (cond
  9997. ((not org-refile-cache) nil)
  9998. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9999. (t
  10000. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10001. org-refile-cache))))
  10002. (and set (org-refile-cache-check-set set) set)))))
  10003. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10004. "Produce a table with refile targets."
  10005. (let ((case-fold-search nil)
  10006. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10007. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10008. targets tgs txt re files f desc descre fast-path-p level pos0)
  10009. (message "Getting targets...")
  10010. (with-current-buffer (or default-buffer (current-buffer))
  10011. (while (setq entry (pop entries))
  10012. (setq files (car entry) desc (cdr entry))
  10013. (setq fast-path-p nil)
  10014. (cond
  10015. ((null files) (setq files (list (current-buffer))))
  10016. ((eq files 'org-agenda-files)
  10017. (setq files (org-agenda-files 'unrestricted)))
  10018. ((and (symbolp files) (fboundp files))
  10019. (setq files (funcall files)))
  10020. ((and (symbolp files) (boundp files))
  10021. (setq files (symbol-value files))))
  10022. (if (stringp files) (setq files (list files)))
  10023. (cond
  10024. ((eq (car desc) :tag)
  10025. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10026. ((eq (car desc) :todo)
  10027. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10028. ((eq (car desc) :regexp)
  10029. (setq descre (cdr desc)))
  10030. ((eq (car desc) :level)
  10031. (setq descre (concat "^\\*\\{" (number-to-string
  10032. (if org-odd-levels-only
  10033. (1- (* 2 (cdr desc)))
  10034. (cdr desc)))
  10035. "\\}[ \t]")))
  10036. ((eq (car desc) :maxlevel)
  10037. (setq fast-path-p t)
  10038. (setq descre (concat "^\\*\\{1," (number-to-string
  10039. (if org-odd-levels-only
  10040. (1- (* 2 (cdr desc)))
  10041. (cdr desc)))
  10042. "\\}[ \t]")))
  10043. (t (error "Bad refiling target description %s" desc)))
  10044. (while (setq f (pop files))
  10045. (with-current-buffer
  10046. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10047. (or
  10048. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10049. (progn
  10050. (if (bufferp f) (setq f (buffer-file-name
  10051. (buffer-base-buffer f))))
  10052. (setq f (and f (expand-file-name f)))
  10053. (if (eq org-refile-use-outline-path 'file)
  10054. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10055. (save-excursion
  10056. (save-restriction
  10057. (widen)
  10058. (goto-char (point-min))
  10059. (while (re-search-forward descre nil t)
  10060. (goto-char (setq pos0 (point-at-bol)))
  10061. (catch 'next
  10062. (when org-refile-target-verify-function
  10063. (save-match-data
  10064. (or (funcall org-refile-target-verify-function)
  10065. (throw 'next t))))
  10066. (when (and (looking-at org-complex-heading-regexp)
  10067. (not (member (match-string 4) excluded-entries))
  10068. (match-string 4))
  10069. (setq level (org-reduced-level
  10070. (- (match-end 1) (match-beginning 1)))
  10071. txt (org-link-display-format (match-string 4))
  10072. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10073. re (format org-complex-heading-regexp-format
  10074. (regexp-quote (match-string 4))))
  10075. (when org-refile-use-outline-path
  10076. (setq txt (mapconcat
  10077. 'org-protect-slash
  10078. (append
  10079. (if (eq org-refile-use-outline-path
  10080. 'file)
  10081. (list (file-name-nondirectory
  10082. (buffer-file-name
  10083. (buffer-base-buffer))))
  10084. (if (eq org-refile-use-outline-path
  10085. 'full-file-path)
  10086. (list (buffer-file-name
  10087. (buffer-base-buffer)))))
  10088. (org-get-outline-path fast-path-p
  10089. level txt)
  10090. (list txt))
  10091. "/")))
  10092. (push (list txt f re (org-refile-marker (point)))
  10093. tgs)))
  10094. (when (= (point) pos0)
  10095. ;; verification function has not moved point
  10096. (goto-char (point-at-eol))))))))
  10097. (when org-refile-use-cache
  10098. (org-refile-cache-put tgs (buffer-file-name) descre))
  10099. (setq targets (append tgs targets))
  10100. ))))
  10101. (message "Getting targets...done")
  10102. (nreverse targets)))
  10103. (defun org-protect-slash (s)
  10104. (while (string-match "/" s)
  10105. (setq s (replace-match "\\" t t s)))
  10106. s)
  10107. (defvar org-olpa (make-vector 20 nil))
  10108. (defun org-get-outline-path (&optional fastp level heading)
  10109. "Return the outline path to the current entry, as a list.
  10110. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10111. routine which makes outline path derivations for an entire file,
  10112. avoiding backtracing. Refile target collection makes use of that."
  10113. (if fastp
  10114. (progn
  10115. (if (> level 19)
  10116. (error "Outline path failure, more than 19 levels"))
  10117. (loop for i from level upto 19 do
  10118. (aset org-olpa i nil))
  10119. (prog1
  10120. (delq nil (append org-olpa nil))
  10121. (aset org-olpa level heading)))
  10122. (let (rtn case-fold-search)
  10123. (save-excursion
  10124. (save-restriction
  10125. (widen)
  10126. (while (org-up-heading-safe)
  10127. (when (looking-at org-complex-heading-regexp)
  10128. (push (org-match-string-no-properties 4) rtn)))
  10129. rtn)))))
  10130. (defun org-format-outline-path (path &optional width prefix separator)
  10131. "Format the outline path PATH for display.
  10132. WIDTH is the maximum number of characters that is available.
  10133. PREFIX is a prefix to be included in the returned string,
  10134. such as the file name.
  10135. SEPARATOR is inserted between the different parts of the path,
  10136. the default is \"/\"."
  10137. (setq width (or width 79))
  10138. (if prefix (setq width (- width (length prefix))))
  10139. (if (not path)
  10140. (or prefix "")
  10141. (let* ((nsteps (length path))
  10142. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10143. (maxwidth (if (<= total-width width)
  10144. 10000 ;; everything fits
  10145. ;; we need to shorten the level headings
  10146. (/ (- width nsteps) nsteps)))
  10147. (org-odd-levels-only nil)
  10148. (n 0)
  10149. (total (1+ (length prefix))))
  10150. (setq maxwidth (max maxwidth 10))
  10151. (concat prefix
  10152. (if prefix (or separator "/"))
  10153. (mapconcat
  10154. (lambda (h)
  10155. (setq n (1+ n))
  10156. (if (and (= n nsteps) (< maxwidth 10000))
  10157. (setq maxwidth (- total-width total)))
  10158. (if (< (length h) maxwidth)
  10159. (progn (setq total (+ total (length h) 1)) h)
  10160. (setq h (substring h 0 (- maxwidth 2))
  10161. total (+ total maxwidth 1))
  10162. (if (string-match "[ \t]+\\'" h)
  10163. (setq h (substring h 0 (match-beginning 0))))
  10164. (setq h (concat h "..")))
  10165. (org-add-props h nil 'face
  10166. (nth (% (1- n) org-n-level-faces)
  10167. org-level-faces))
  10168. h)
  10169. path (or separator "/"))))))
  10170. (defun org-display-outline-path (&optional file current separator just-return-string)
  10171. "Display the current outline path in the echo area.
  10172. If FILE is non-nil, prepend the output with the file name.
  10173. If CURRENT is non-nil, append the current heading to the output.
  10174. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10175. the different parts of the path and defaults to \"/\".
  10176. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10177. (interactive "P")
  10178. (let* (case-fold-search
  10179. message-log-max ; Don't populate the *Messages* buffer
  10180. (bfn (buffer-file-name (buffer-base-buffer)))
  10181. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10182. res)
  10183. (if current (setq path (append path
  10184. (save-excursion
  10185. (org-back-to-heading t)
  10186. (if (looking-at org-complex-heading-regexp)
  10187. (list (match-string 4)))))))
  10188. (setq res
  10189. (org-format-outline-path
  10190. path
  10191. (1- (frame-width))
  10192. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10193. separator))
  10194. (if just-return-string
  10195. (org-no-properties res)
  10196. (message "%s" res))))
  10197. (defvar org-refile-history nil
  10198. "History for refiling operations.")
  10199. (defvar org-after-refile-insert-hook nil
  10200. "Hook run after `org-refile' has inserted its stuff at the new location.
  10201. Note that this is still *before* the stuff will be removed from
  10202. the *old* location.")
  10203. (defvar org-capture-last-stored-marker)
  10204. (defvar org-refile-keep nil
  10205. "Non-nil means `org-refile' will copy instead of refile.")
  10206. (defun org-copy ()
  10207. "Like `org-refile', but copy."
  10208. (interactive)
  10209. (let ((org-refile-keep t))
  10210. (funcall 'org-refile nil nil nil "Copy")))
  10211. (defun org-refile (&optional goto default-buffer rfloc msg)
  10212. "Move the entry or entries at point to another heading.
  10213. The list of target headings is compiled using the information in
  10214. `org-refile-targets', which see.
  10215. At the target location, the entry is filed as a subitem of the target
  10216. heading. Depending on `org-reverse-note-order', the new subitem will
  10217. either be the first or the last subitem.
  10218. If there is an active region, all entries in that region will be moved.
  10219. However, the region must fulfill the requirement that the first heading
  10220. is the first one sets the top-level of the moved text - at most siblings
  10221. below it are allowed.
  10222. With prefix arg GOTO, the command will only visit the target location
  10223. and not actually move anything.
  10224. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10225. go to the location where the last refiling operation has put the subtree.
  10226. With a prefix argument of `2', refile to the running clock.
  10227. RFLOC can be a refile location obtained in a different way.
  10228. MSG is a string to replace \"Refile\" in the default prompt with
  10229. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10230. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10231. If you are using target caching (see `org-refile-use-cache'),
  10232. you have to clear the target cache in order to find new targets.
  10233. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10234. prefix argument (`C-u C-u C-u C-c C-w')."
  10235. (interactive "P")
  10236. (if (member goto '(0 (64)))
  10237. (org-refile-cache-clear)
  10238. (let* ((actionmsg (or msg "Refile"))
  10239. (cbuf (current-buffer))
  10240. (regionp (org-region-active-p))
  10241. (region-start (and regionp (region-beginning)))
  10242. (region-end (and regionp (region-end)))
  10243. (region-length (and regionp (- region-end region-start)))
  10244. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10245. pos it nbuf file re level reversed)
  10246. (setq last-command nil)
  10247. (when regionp
  10248. (goto-char region-start)
  10249. (or (bolp) (goto-char (point-at-bol)))
  10250. (setq region-start (point))
  10251. (unless (or (org-kill-is-subtree-p
  10252. (buffer-substring region-start region-end))
  10253. (prog1 org-refile-active-region-within-subtree
  10254. (org-toggle-heading)))
  10255. (error "The region is not a (sequence of) subtree(s)")))
  10256. (if (equal goto '(16))
  10257. (org-refile-goto-last-stored)
  10258. (when (or
  10259. (and (equal goto 2)
  10260. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10261. (prog1
  10262. (setq it (list (or org-clock-heading "running clock")
  10263. (buffer-file-name
  10264. (marker-buffer org-clock-hd-marker))
  10265. ""
  10266. (marker-position org-clock-hd-marker)))
  10267. (setq goto nil)))
  10268. (setq it (or rfloc
  10269. (let (heading-text)
  10270. (save-excursion
  10271. (unless goto
  10272. (org-back-to-heading t)
  10273. (setq heading-text
  10274. (nth 4 (org-heading-components))))
  10275. (org-refile-get-location
  10276. (cond (goto "Goto")
  10277. (regionp (concat actionmsg " region to"))
  10278. (t (concat actionmsg " subtree \""
  10279. heading-text "\" to")))
  10280. default-buffer
  10281. (and (not (equal '(4) goto))
  10282. org-refile-allow-creating-parent-nodes)
  10283. goto))))))
  10284. (setq file (nth 1 it)
  10285. re (nth 2 it)
  10286. pos (nth 3 it))
  10287. (if (and (not goto)
  10288. pos
  10289. (equal (buffer-file-name) file)
  10290. (if regionp
  10291. (and (>= pos region-start)
  10292. (<= pos region-end))
  10293. (and (>= pos (point))
  10294. (< pos (save-excursion
  10295. (org-end-of-subtree t t))))))
  10296. (error "Cannot refile to position inside the tree or region"))
  10297. (setq nbuf (or (find-buffer-visiting file)
  10298. (find-file-noselect file)))
  10299. (if goto
  10300. (progn
  10301. (org-pop-to-buffer-same-window nbuf)
  10302. (goto-char pos)
  10303. (org-show-context 'org-goto))
  10304. (if regionp
  10305. (progn
  10306. (org-kill-new (buffer-substring region-start region-end))
  10307. (org-save-markers-in-region region-start region-end))
  10308. (org-copy-subtree 1 nil t))
  10309. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10310. (find-file-noselect file)))
  10311. (setq reversed (org-notes-order-reversed-p))
  10312. (save-excursion
  10313. (save-restriction
  10314. (widen)
  10315. (if pos
  10316. (progn
  10317. (goto-char pos)
  10318. (looking-at org-outline-regexp)
  10319. (setq level (org-get-valid-level (funcall outline-level) 1))
  10320. (goto-char
  10321. (if reversed
  10322. (or (outline-next-heading) (point-max))
  10323. (or (save-excursion (org-get-next-sibling))
  10324. (org-end-of-subtree t t)
  10325. (point-max)))))
  10326. (setq level 1)
  10327. (if (not reversed)
  10328. (goto-char (point-max))
  10329. (goto-char (point-min))
  10330. (or (outline-next-heading) (goto-char (point-max)))))
  10331. (if (not (bolp)) (newline))
  10332. (org-paste-subtree level)
  10333. (when org-log-refile
  10334. (org-add-log-setup 'refile nil nil 'findpos
  10335. org-log-refile)
  10336. (unless (eq org-log-refile 'note)
  10337. (save-excursion (org-add-log-note))))
  10338. (and org-auto-align-tags
  10339. (let ((org-loop-over-headlines-in-active-region nil))
  10340. (org-set-tags nil t)))
  10341. (with-demoted-errors
  10342. (bookmark-set "org-refile-last-stored"))
  10343. ;; If we are refiling for capture, make sure that the
  10344. ;; last-capture pointers point here
  10345. (when (org-bound-and-true-p org-refile-for-capture)
  10346. (with-demoted-errors
  10347. (bookmark-set "org-capture-last-stored-marker"))
  10348. (move-marker org-capture-last-stored-marker (point)))
  10349. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10350. (run-hooks 'org-after-refile-insert-hook))))
  10351. (unless org-refile-keep
  10352. (if regionp
  10353. (delete-region (point) (+ (point) region-length))
  10354. (org-cut-subtree)))
  10355. (when (featurep 'org-inlinetask)
  10356. (org-inlinetask-remove-END-maybe))
  10357. (setq org-markers-to-move nil)
  10358. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10359. (defun org-refile-goto-last-stored ()
  10360. "Go to the location where the last refile was stored."
  10361. (interactive)
  10362. (bookmark-jump "org-refile-last-stored")
  10363. (message "This is the location of the last refile"))
  10364. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10365. no-exclude)
  10366. "Prompt the user for a refile location, using PROMPT.
  10367. PROMPT should not be suffixed with a colon and a space, because
  10368. this function appends the default value from
  10369. `org-refile-history' automatically, if that is not empty.
  10370. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10371. this is used for the GOTO interface."
  10372. (let ((org-refile-targets org-refile-targets)
  10373. (org-refile-use-outline-path org-refile-use-outline-path)
  10374. excluded-entries)
  10375. (when (and (derived-mode-p 'org-mode)
  10376. (not org-refile-use-cache)
  10377. (not no-exclude))
  10378. (org-map-tree
  10379. (lambda()
  10380. (setq excluded-entries
  10381. (append excluded-entries (list (org-get-heading t t)))))))
  10382. (setq org-refile-target-table
  10383. (org-refile-get-targets default-buffer excluded-entries)))
  10384. (unless org-refile-target-table
  10385. (error "No refile targets"))
  10386. (let* ((cbuf (current-buffer))
  10387. (partial-completion-mode nil)
  10388. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10389. (cfunc (if (and org-refile-use-outline-path
  10390. org-outline-path-complete-in-steps)
  10391. 'org-olpath-completing-read
  10392. 'org-icompleting-read))
  10393. (extra (if org-refile-use-outline-path "/" ""))
  10394. (cbnex (concat (buffer-name) extra))
  10395. (filename (and cfn (expand-file-name cfn)))
  10396. (tbl (mapcar
  10397. (lambda (x)
  10398. (if (and (not (member org-refile-use-outline-path
  10399. '(file full-file-path)))
  10400. (not (equal filename (nth 1 x))))
  10401. (cons (concat (car x) extra " ("
  10402. (file-name-nondirectory (nth 1 x)) ")")
  10403. (cdr x))
  10404. (cons (concat (car x) extra) (cdr x))))
  10405. org-refile-target-table))
  10406. (completion-ignore-case t)
  10407. cdef
  10408. (prompt (concat prompt
  10409. (or (and (car org-refile-history)
  10410. (concat " (default " (car org-refile-history) ")"))
  10411. (and (assoc cbnex tbl) (setq cdef cbnex)
  10412. (concat " (default " cbnex ")"))) ": "))
  10413. pa answ parent-target child parent old-hist)
  10414. (setq old-hist org-refile-history)
  10415. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10416. nil 'org-refile-history (or cdef (car org-refile-history))))
  10417. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10418. (org-refile-check-position pa)
  10419. (if pa
  10420. (progn
  10421. (when (or (not org-refile-history)
  10422. (not (eq old-hist org-refile-history))
  10423. (not (equal (car pa) (car org-refile-history))))
  10424. (setq org-refile-history
  10425. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10426. org-refile-history
  10427. (cdr org-refile-history))))
  10428. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10429. (pop org-refile-history)))
  10430. pa)
  10431. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10432. (progn
  10433. (setq parent (match-string 1 answ)
  10434. child (match-string 2 answ))
  10435. (setq parent-target (or (assoc parent tbl)
  10436. (assoc (concat parent "/") tbl)))
  10437. (when (and parent-target
  10438. (or (eq new-nodes t)
  10439. (and (eq new-nodes 'confirm)
  10440. (y-or-n-p (format "Create new node \"%s\"? "
  10441. child)))))
  10442. (org-refile-new-child parent-target child)))
  10443. (error "Invalid target location")))))
  10444. (declare-function org-string-nw-p "org-macs" (s))
  10445. (defun org-refile-check-position (refile-pointer)
  10446. "Check if the refile pointer matches the headline to which it points."
  10447. (let* ((file (nth 1 refile-pointer))
  10448. (re (nth 2 refile-pointer))
  10449. (pos (nth 3 refile-pointer))
  10450. buffer)
  10451. (if (and (not (markerp pos)) (not file))
  10452. (error "Please save the buffer to a file before refiling")
  10453. (when (org-string-nw-p re)
  10454. (setq buffer (if (markerp pos)
  10455. (marker-buffer pos)
  10456. (or (find-buffer-visiting file)
  10457. (find-file-noselect file))))
  10458. (with-current-buffer buffer
  10459. (save-excursion
  10460. (save-restriction
  10461. (widen)
  10462. (goto-char pos)
  10463. (beginning-of-line 1)
  10464. (unless (org-looking-at-p re)
  10465. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10466. (defun org-refile-new-child (parent-target child)
  10467. "Use refile target PARENT-TARGET to add new CHILD below it."
  10468. (unless parent-target
  10469. (error "Cannot find parent for new node"))
  10470. (let ((file (nth 1 parent-target))
  10471. (pos (nth 3 parent-target))
  10472. level)
  10473. (with-current-buffer (or (find-buffer-visiting file)
  10474. (find-file-noselect file))
  10475. (save-excursion
  10476. (save-restriction
  10477. (widen)
  10478. (if pos
  10479. (goto-char pos)
  10480. (goto-char (point-max))
  10481. (if (not (bolp)) (newline)))
  10482. (when (looking-at org-outline-regexp)
  10483. (setq level (funcall outline-level))
  10484. (org-end-of-subtree t t))
  10485. (org-back-over-empty-lines)
  10486. (insert "\n" (make-string
  10487. (if pos (org-get-valid-level level 1) 1) ?*)
  10488. " " child "\n")
  10489. (beginning-of-line 0)
  10490. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10491. (defun org-olpath-completing-read (prompt collection &rest args)
  10492. "Read an outline path like a file name."
  10493. (let ((thetable collection)
  10494. (org-completion-use-ido nil) ; does not work with ido.
  10495. (org-completion-use-iswitchb nil)) ; or iswitchb
  10496. (apply
  10497. 'org-icompleting-read prompt
  10498. (lambda (string predicate &optional flag)
  10499. (let (rtn r f (l (length string)))
  10500. (cond
  10501. ((eq flag nil)
  10502. ;; try completion
  10503. (try-completion string thetable))
  10504. ((eq flag t)
  10505. ;; all-completions
  10506. (setq rtn (all-completions string thetable predicate))
  10507. (mapcar
  10508. (lambda (x)
  10509. (setq r (substring x l))
  10510. (if (string-match " ([^)]*)$" x)
  10511. (setq f (match-string 0 x))
  10512. (setq f ""))
  10513. (if (string-match "/" r)
  10514. (concat string (substring r 0 (match-end 0)) f)
  10515. x))
  10516. rtn))
  10517. ((eq flag 'lambda)
  10518. ;; exact match?
  10519. (assoc string thetable)))))
  10520. args)))
  10521. ;;;; Dynamic blocks
  10522. (defun org-find-dblock (name)
  10523. "Find the first dynamic block with name NAME in the buffer.
  10524. If not found, stay at current position and return nil."
  10525. (let ((case-fold-search t) pos)
  10526. (save-excursion
  10527. (goto-char (point-min))
  10528. (setq pos (and (re-search-forward
  10529. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10530. (match-beginning 0))))
  10531. (if pos (goto-char pos))
  10532. pos))
  10533. (defconst org-dblock-start-re
  10534. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10535. "Matches the start line of a dynamic block, with parameters.")
  10536. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10537. "Matches the end of a dynamic block.")
  10538. (defun org-create-dblock (plist)
  10539. "Create a dynamic block section, with parameters taken from PLIST.
  10540. PLIST must contain a :name entry which is used as name of the block."
  10541. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10542. (end-of-line 1)
  10543. (newline))
  10544. (let ((col (current-column))
  10545. (name (plist-get plist :name)))
  10546. (insert "#+BEGIN: " name)
  10547. (while plist
  10548. (if (eq (car plist) :name)
  10549. (setq plist (cddr plist))
  10550. (insert " " (prin1-to-string (pop plist)))))
  10551. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10552. (beginning-of-line -2)))
  10553. (defun org-prepare-dblock ()
  10554. "Prepare dynamic block for refresh.
  10555. This empties the block, puts the cursor at the insert position and returns
  10556. the property list including an extra property :name with the block name."
  10557. (unless (looking-at org-dblock-start-re)
  10558. (error "Not at a dynamic block"))
  10559. (let* ((begdel (1+ (match-end 0)))
  10560. (name (org-no-properties (match-string 1)))
  10561. (params (append (list :name name)
  10562. (read (concat "(" (match-string 3) ")")))))
  10563. (save-excursion
  10564. (beginning-of-line 1)
  10565. (skip-chars-forward " \t")
  10566. (setq params (plist-put params :indentation-column (current-column))))
  10567. (unless (re-search-forward org-dblock-end-re nil t)
  10568. (error "Dynamic block not terminated"))
  10569. (setq params
  10570. (append params
  10571. (list :content (buffer-substring
  10572. begdel (match-beginning 0)))))
  10573. (delete-region begdel (match-beginning 0))
  10574. (goto-char begdel)
  10575. (open-line 1)
  10576. params))
  10577. (defun org-map-dblocks (&optional command)
  10578. "Apply COMMAND to all dynamic blocks in the current buffer.
  10579. If COMMAND is not given, use `org-update-dblock'."
  10580. (let ((cmd (or command 'org-update-dblock)))
  10581. (save-excursion
  10582. (goto-char (point-min))
  10583. (while (re-search-forward org-dblock-start-re nil t)
  10584. (goto-char (match-beginning 0))
  10585. (save-excursion
  10586. (condition-case nil
  10587. (funcall cmd)
  10588. (error (message "Error during update of dynamic block"))))
  10589. (unless (re-search-forward org-dblock-end-re nil t)
  10590. (error "Dynamic block not terminated"))))))
  10591. (defun org-dblock-update (&optional arg)
  10592. "User command for updating dynamic blocks.
  10593. Update the dynamic block at point. With prefix ARG, update all dynamic
  10594. blocks in the buffer."
  10595. (interactive "P")
  10596. (if arg
  10597. (org-update-all-dblocks)
  10598. (or (looking-at org-dblock-start-re)
  10599. (org-beginning-of-dblock))
  10600. (org-update-dblock)))
  10601. (defun org-update-dblock ()
  10602. "Update the dynamic block at point.
  10603. This means to empty the block, parse for parameters and then call
  10604. the correct writing function."
  10605. (interactive)
  10606. (save-window-excursion
  10607. (let* ((pos (point))
  10608. (line (org-current-line))
  10609. (params (org-prepare-dblock))
  10610. (name (plist-get params :name))
  10611. (indent (plist-get params :indentation-column))
  10612. (cmd (intern (concat "org-dblock-write:" name))))
  10613. (message "Updating dynamic block `%s' at line %d..." name line)
  10614. (funcall cmd params)
  10615. (message "Updating dynamic block `%s' at line %d...done" name line)
  10616. (goto-char pos)
  10617. (when (and indent (> indent 0))
  10618. (setq indent (make-string indent ?\ ))
  10619. (save-excursion
  10620. (org-beginning-of-dblock)
  10621. (forward-line 1)
  10622. (while (not (looking-at org-dblock-end-re))
  10623. (insert indent)
  10624. (beginning-of-line 2))
  10625. (when (looking-at org-dblock-end-re)
  10626. (and (looking-at "[ \t]+")
  10627. (replace-match ""))
  10628. (insert indent)))))))
  10629. (defun org-beginning-of-dblock ()
  10630. "Find the beginning of the dynamic block at point.
  10631. Error if there is no such block at point."
  10632. (let ((pos (point))
  10633. beg)
  10634. (end-of-line 1)
  10635. (if (and (re-search-backward org-dblock-start-re nil t)
  10636. (setq beg (match-beginning 0))
  10637. (re-search-forward org-dblock-end-re nil t)
  10638. (> (match-end 0) pos))
  10639. (goto-char beg)
  10640. (goto-char pos)
  10641. (error "Not in a dynamic block"))))
  10642. (defun org-update-all-dblocks ()
  10643. "Update all dynamic blocks in the buffer.
  10644. This function can be used in a hook."
  10645. (interactive)
  10646. (when (derived-mode-p 'org-mode)
  10647. (org-map-dblocks 'org-update-dblock)))
  10648. ;;;; Completion
  10649. (defun org-get-export-keywords ()
  10650. "Return a list of all currently understood export keywords.
  10651. Export keywords include options, block names, attributes and
  10652. keywords relative to each registered export back-end."
  10653. (delq nil
  10654. (let (keywords)
  10655. (mapc
  10656. (lambda (back-end)
  10657. (let ((props (cdr back-end)))
  10658. ;; Back-end name (for keywords, like #+LATEX:)
  10659. (push (upcase (symbol-name (car back-end))) keywords)
  10660. ;; Back-end options.
  10661. (mapc (lambda (option) (push (cadr option) keywords))
  10662. (plist-get (cdr back-end) :options-alist))))
  10663. (org-bound-and-true-p org-export-registered-backends))
  10664. keywords)))
  10665. (defconst org-options-keywords
  10666. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10667. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10668. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10669. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10670. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
  10671. (defcustom org-structure-template-alist
  10672. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10673. "<src lang=\"?\">\n\n</src>")
  10674. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10675. "<example>\n?\n</example>")
  10676. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10677. "<quote>\n?\n</quote>")
  10678. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10679. "<verse>\n?\n</verse>")
  10680. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10681. "<verbatim>\n?\n</verbatim>")
  10682. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10683. "<center>\n?\n</center>")
  10684. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10685. "<literal style=\"latex\">\n?\n</literal>")
  10686. ("L" "#+LaTeX: "
  10687. "<literal style=\"latex\">?</literal>")
  10688. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10689. "<literal style=\"html\">\n?\n</literal>")
  10690. ("H" "#+HTML: "
  10691. "<literal style=\"html\">?</literal>")
  10692. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10693. ("A" "#+ASCII: ")
  10694. ("i" "#+INDEX: ?"
  10695. "#+INDEX: ?")
  10696. ("I" "#+INCLUDE: %file ?"
  10697. "<include file=%file markup=\"?\">"))
  10698. "Structure completion elements.
  10699. This is a list of abbreviation keys and values. The value gets inserted
  10700. if you type `<' followed by the key and then press the completion key,
  10701. usually `M-TAB'. %file will be replaced by a file name after prompting
  10702. for the file using completion. The cursor will be placed at the position
  10703. of the `?` in the template.
  10704. There are two templates for each key, the first uses the original Org syntax,
  10705. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10706. the default when the /org-mtags.el/ module has been loaded. See also the
  10707. variable `org-mtags-prefer-muse-templates'."
  10708. :group 'org-completion
  10709. :type '(repeat
  10710. (string :tag "Key")
  10711. (string :tag "Template")
  10712. (string :tag "Muse Template")))
  10713. (defun org-try-structure-completion ()
  10714. "Try to complete a structure template before point.
  10715. This looks for strings like \"<e\" on an otherwise empty line and
  10716. expands them."
  10717. (let ((l (buffer-substring (point-at-bol) (point)))
  10718. a)
  10719. (when (and (looking-at "[ \t]*$")
  10720. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10721. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10722. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10723. (match-beginning 1)) a)
  10724. t)))
  10725. (defun org-complete-expand-structure-template (start cell)
  10726. "Expand a structure template."
  10727. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10728. (rpl (nth (if musep 2 1) cell))
  10729. (ind ""))
  10730. (delete-region start (point))
  10731. (when (string-match "\\`#\\+" rpl)
  10732. (cond
  10733. ((bolp))
  10734. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10735. (setq ind (buffer-substring (point-at-bol) (point))))
  10736. (t (newline))))
  10737. (setq start (point))
  10738. (if (string-match "%file" rpl)
  10739. (setq rpl (replace-match
  10740. (concat
  10741. "\""
  10742. (save-match-data
  10743. (abbreviate-file-name (read-file-name "Include file: ")))
  10744. "\"")
  10745. t t rpl)))
  10746. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10747. (concat "\n" ind)))
  10748. (insert rpl)
  10749. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10750. ;;;; TODO, DEADLINE, Comments
  10751. (defun org-toggle-comment ()
  10752. "Change the COMMENT state of an entry."
  10753. (interactive)
  10754. (save-excursion
  10755. (org-back-to-heading)
  10756. (let (case-fold-search)
  10757. (cond
  10758. ((looking-at (format org-heading-keyword-regexp-format
  10759. org-comment-string))
  10760. (goto-char (match-end 1))
  10761. (looking-at (concat " +" org-comment-string))
  10762. (replace-match "" t t)
  10763. (when (eolp) (insert " ")))
  10764. ((looking-at org-outline-regexp)
  10765. (goto-char (match-end 0))
  10766. (insert org-comment-string " "))))))
  10767. (defvar org-last-todo-state-is-todo nil
  10768. "This is non-nil when the last TODO state change led to a TODO state.
  10769. If the last change removed the TODO tag or switched to DONE, then
  10770. this is nil.")
  10771. (defvar org-setting-tags nil) ; dynamically skipped
  10772. (defvar org-todo-setup-filter-hook nil
  10773. "Hook for functions that pre-filter todo specs.
  10774. Each function takes a todo spec and returns either nil or the spec
  10775. transformed into canonical form." )
  10776. (defvar org-todo-get-default-hook nil
  10777. "Hook for functions that get a default item for todo.
  10778. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10779. nil or a string to be used for the todo mark." )
  10780. (defvar org-agenda-headline-snapshot-before-repeat)
  10781. (defun org-current-effective-time ()
  10782. "Return current time adjusted for `org-extend-today-until' variable."
  10783. (let* ((ct (org-current-time))
  10784. (dct (decode-time ct))
  10785. (ct1
  10786. (cond
  10787. (org-use-last-clock-out-time-as-effective-time
  10788. (or (org-clock-get-last-clock-out-time) ct))
  10789. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10790. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10791. (t ct))))
  10792. ct1))
  10793. (defun org-todo-yesterday (&optional arg)
  10794. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10795. (interactive "P")
  10796. (if (eq major-mode 'org-agenda-mode)
  10797. (apply 'org-agenda-todo-yesterday arg)
  10798. (let* ((hour (third (decode-time
  10799. (org-current-time))))
  10800. (org-extend-today-until (1+ hour)))
  10801. (org-todo arg))))
  10802. (defvar org-block-entry-blocking ""
  10803. "First entry preventing the TODO state change.")
  10804. (defun org-todo (&optional arg)
  10805. "Change the TODO state of an item.
  10806. The state of an item is given by a keyword at the start of the heading,
  10807. like
  10808. *** TODO Write paper
  10809. *** DONE Call mom
  10810. The different keywords are specified in the variable `org-todo-keywords'.
  10811. By default the available states are \"TODO\" and \"DONE\".
  10812. So for this example: when the item starts with TODO, it is changed to DONE.
  10813. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10814. DONE are present, add TODO at the beginning of the heading.
  10815. With \\[universal-argument] prefix arg, use completion to determine the new \
  10816. state.
  10817. With numeric prefix arg, switch to that state.
  10818. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10819. keywords (nextset).
  10820. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10821. With a numeric prefix arg of 0, inhibit note taking for the change.
  10822. For calling through lisp, arg is also interpreted in the following way:
  10823. 'none -> empty state
  10824. \"\"(empty string) -> switch to empty state
  10825. 'done -> switch to DONE
  10826. 'nextset -> switch to the next set of keywords
  10827. 'previousset -> switch to the previous set of keywords
  10828. \"WAITING\" -> switch to the specified keyword, but only if it
  10829. really is a member of `org-todo-keywords'."
  10830. (interactive "P")
  10831. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10832. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10833. 'region-start-level 'region))
  10834. org-loop-over-headlines-in-active-region)
  10835. (org-map-entries
  10836. `(org-todo ,arg)
  10837. org-loop-over-headlines-in-active-region
  10838. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10839. (if (equal arg '(16)) (setq arg 'nextset))
  10840. (let ((org-blocker-hook org-blocker-hook)
  10841. commentp
  10842. case-fold-search)
  10843. (when (equal arg '(64))
  10844. (setq arg nil org-blocker-hook nil))
  10845. (when (and org-blocker-hook
  10846. (or org-inhibit-blocking
  10847. (org-entry-get nil "NOBLOCKING")))
  10848. (setq org-blocker-hook nil))
  10849. (save-excursion
  10850. (catch 'exit
  10851. (org-back-to-heading t)
  10852. (when (looking-at (concat "^\\*+ " org-comment-string))
  10853. (org-toggle-comment)
  10854. (setq commentp t))
  10855. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10856. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10857. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10858. (let* ((match-data (match-data))
  10859. (startpos (point-at-bol))
  10860. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10861. (org-log-done org-log-done)
  10862. (org-log-repeat org-log-repeat)
  10863. (org-todo-log-states org-todo-log-states)
  10864. (org-inhibit-logging
  10865. (if (equal arg 0)
  10866. (progn (setq arg nil) 'note) org-inhibit-logging))
  10867. (this (match-string 1))
  10868. (hl-pos (match-beginning 0))
  10869. (head (org-get-todo-sequence-head this))
  10870. (ass (assoc head org-todo-kwd-alist))
  10871. (interpret (nth 1 ass))
  10872. (done-word (nth 3 ass))
  10873. (final-done-word (nth 4 ass))
  10874. (org-last-state (or this ""))
  10875. (completion-ignore-case t)
  10876. (member (member this org-todo-keywords-1))
  10877. (tail (cdr member))
  10878. (org-state (cond
  10879. ((and org-todo-key-trigger
  10880. (or (and (equal arg '(4))
  10881. (eq org-use-fast-todo-selection 'prefix))
  10882. (and (not arg) org-use-fast-todo-selection
  10883. (not (eq org-use-fast-todo-selection
  10884. 'prefix)))))
  10885. ;; Use fast selection
  10886. (org-fast-todo-selection))
  10887. ((and (equal arg '(4))
  10888. (or (not org-use-fast-todo-selection)
  10889. (not org-todo-key-trigger)))
  10890. ;; Read a state with completion
  10891. (org-icompleting-read
  10892. "State: " (mapcar (lambda(x) (list x))
  10893. org-todo-keywords-1)
  10894. nil t))
  10895. ((eq arg 'right)
  10896. (if this
  10897. (if tail (car tail) nil)
  10898. (car org-todo-keywords-1)))
  10899. ((eq arg 'left)
  10900. (if (equal member org-todo-keywords-1)
  10901. nil
  10902. (if this
  10903. (nth (- (length org-todo-keywords-1)
  10904. (length tail) 2)
  10905. org-todo-keywords-1)
  10906. (org-last org-todo-keywords-1))))
  10907. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10908. (setq arg nil))) ; hack to fall back to cycling
  10909. (arg
  10910. ;; user or caller requests a specific state
  10911. (cond
  10912. ((equal arg "") nil)
  10913. ((eq arg 'none) nil)
  10914. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10915. ((eq arg 'nextset)
  10916. (or (car (cdr (member head org-todo-heads)))
  10917. (car org-todo-heads)))
  10918. ((eq arg 'previousset)
  10919. (let ((org-todo-heads (reverse org-todo-heads)))
  10920. (or (car (cdr (member head org-todo-heads)))
  10921. (car org-todo-heads))))
  10922. ((car (member arg org-todo-keywords-1)))
  10923. ((stringp arg)
  10924. (error "State `%s' not valid in this file" arg))
  10925. ((nth (1- (prefix-numeric-value arg))
  10926. org-todo-keywords-1))))
  10927. ((null member) (or head (car org-todo-keywords-1)))
  10928. ((equal this final-done-word) nil) ;; -> make empty
  10929. ((null tail) nil) ;; -> first entry
  10930. ((memq interpret '(type priority))
  10931. (if (eq this-command last-command)
  10932. (car tail)
  10933. (if (> (length tail) 0)
  10934. (or done-word (car org-done-keywords))
  10935. nil)))
  10936. (t
  10937. (car tail))))
  10938. (org-state (or
  10939. (run-hook-with-args-until-success
  10940. 'org-todo-get-default-hook org-state org-last-state)
  10941. org-state))
  10942. (next (if org-state (concat " " org-state " ") " "))
  10943. (change-plist (list :type 'todo-state-change :from this :to org-state
  10944. :position startpos))
  10945. dolog now-done-p)
  10946. (when org-blocker-hook
  10947. (setq org-last-todo-state-is-todo
  10948. (not (member this org-done-keywords)))
  10949. (unless (save-excursion
  10950. (save-match-data
  10951. (org-with-wide-buffer
  10952. (run-hook-with-args-until-failure
  10953. 'org-blocker-hook change-plist))))
  10954. (if (org-called-interactively-p 'interactive)
  10955. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10956. this org-state org-block-entry-blocking)
  10957. ;; fail silently
  10958. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10959. this org-state org-block-entry-blocking)
  10960. (throw 'exit nil))))
  10961. (store-match-data match-data)
  10962. (replace-match next t t)
  10963. (unless (pos-visible-in-window-p hl-pos)
  10964. (message "TODO state changed to %s" (org-trim next)))
  10965. (unless head
  10966. (setq head (org-get-todo-sequence-head org-state)
  10967. ass (assoc head org-todo-kwd-alist)
  10968. interpret (nth 1 ass)
  10969. done-word (nth 3 ass)
  10970. final-done-word (nth 4 ass)))
  10971. (when (memq arg '(nextset previousset))
  10972. (message "Keyword-Set %d/%d: %s"
  10973. (- (length org-todo-sets) -1
  10974. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10975. (length org-todo-sets)
  10976. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10977. (setq org-last-todo-state-is-todo
  10978. (not (member org-state org-done-keywords)))
  10979. (setq now-done-p (and (member org-state org-done-keywords)
  10980. (not (member this org-done-keywords))))
  10981. (and logging (org-local-logging logging))
  10982. (when (and (or org-todo-log-states org-log-done)
  10983. (not (eq org-inhibit-logging t))
  10984. (not (memq arg '(nextset previousset))))
  10985. ;; we need to look at recording a time and note
  10986. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10987. (nth 2 (assoc this org-todo-log-states))))
  10988. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10989. (setq dolog 'time))
  10990. (when (and org-state
  10991. (member org-state org-not-done-keywords)
  10992. (not (member this org-not-done-keywords)))
  10993. ;; This is now a todo state and was not one before
  10994. ;; If there was a CLOSED time stamp, get rid of it.
  10995. (org-add-planning-info nil nil 'closed))
  10996. (when (and now-done-p org-log-done)
  10997. ;; It is now done, and it was not done before
  10998. (org-add-planning-info 'closed (org-current-effective-time))
  10999. (if (and (not dolog) (eq 'note org-log-done))
  11000. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11001. (when (and org-state dolog)
  11002. ;; This is a non-nil state, and we need to log it
  11003. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11004. ;; Fixup tag positioning
  11005. (org-todo-trigger-tag-changes org-state)
  11006. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11007. (when org-provide-todo-statistics
  11008. (org-update-parent-todo-statistics))
  11009. (run-hooks 'org-after-todo-state-change-hook)
  11010. (if (and arg (not (member org-state org-done-keywords)))
  11011. (setq head (org-get-todo-sequence-head org-state)))
  11012. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11013. ;; Do we need to trigger a repeat?
  11014. (when now-done-p
  11015. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11016. ;; This is for the agenda, take a snapshot of the headline.
  11017. (save-match-data
  11018. (setq org-agenda-headline-snapshot-before-repeat
  11019. (org-get-heading))))
  11020. (org-auto-repeat-maybe org-state))
  11021. ;; Fixup cursor location if close to the keyword
  11022. (if (and (outline-on-heading-p)
  11023. (not (bolp))
  11024. (save-excursion (beginning-of-line 1)
  11025. (looking-at org-todo-line-regexp))
  11026. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11027. (progn
  11028. (goto-char (or (match-end 2) (match-end 1)))
  11029. (and (looking-at " ") (just-one-space))))
  11030. (when org-trigger-hook
  11031. (save-excursion
  11032. (run-hook-with-args 'org-trigger-hook change-plist)))
  11033. (when commentp (org-toggle-comment))))))))
  11034. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11035. "Block turning an entry into a TODO, using the hierarchy.
  11036. This checks whether the current task should be blocked from state
  11037. changes. Such blocking occurs when:
  11038. 1. The task has children which are not all in a completed state.
  11039. 2. A task has a parent with the property :ORDERED:, and there
  11040. are siblings prior to the current task with incomplete
  11041. status.
  11042. 3. The parent of the task is blocked because it has siblings that should
  11043. be done first, or is child of a block grandparent TODO entry."
  11044. (if (not org-enforce-todo-dependencies)
  11045. t ; if locally turned off don't block
  11046. (catch 'dont-block
  11047. ;; If this is not a todo state change, or if this entry is already DONE,
  11048. ;; do not block
  11049. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11050. (member (plist-get change-plist :from)
  11051. (cons 'done org-done-keywords))
  11052. (member (plist-get change-plist :to)
  11053. (cons 'todo org-not-done-keywords))
  11054. (not (plist-get change-plist :to)))
  11055. (throw 'dont-block t))
  11056. ;; If this task has children, and any are undone, it's blocked
  11057. (save-excursion
  11058. (org-back-to-heading t)
  11059. (let ((this-level (funcall outline-level)))
  11060. (outline-next-heading)
  11061. (let ((child-level (funcall outline-level)))
  11062. (while (and (not (eobp))
  11063. (> child-level this-level))
  11064. ;; this todo has children, check whether they are all
  11065. ;; completed
  11066. (if (and (not (org-entry-is-done-p))
  11067. (org-entry-is-todo-p))
  11068. (progn (setq org-block-entry-blocking (org-get-heading))
  11069. (throw 'dont-block nil)))
  11070. (outline-next-heading)
  11071. (setq child-level (funcall outline-level))))))
  11072. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11073. ;; any previous siblings are undone, it's blocked
  11074. (save-excursion
  11075. (org-back-to-heading t)
  11076. (let* ((pos (point))
  11077. (parent-pos (and (org-up-heading-safe) (point))))
  11078. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11079. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11080. (forward-line 1)
  11081. (re-search-forward org-not-done-heading-regexp pos t))
  11082. (setq org-block-entry-blocking (match-string 0))
  11083. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11084. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11085. (while t
  11086. (goto-char parent-pos)
  11087. (if (not (looking-at org-not-done-heading-regexp))
  11088. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11089. (setq pos (point))
  11090. (setq parent-pos (and (org-up-heading-safe) (point)))
  11091. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11092. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11093. (forward-line 1)
  11094. (re-search-forward org-not-done-heading-regexp pos t)
  11095. (setq org-block-entry-blocking (org-get-heading)))
  11096. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11097. (defcustom org-track-ordered-property-with-tag nil
  11098. "Should the ORDERED property also be shown as a tag?
  11099. The ORDERED property decides if an entry should require subtasks to be
  11100. completed in sequence. Since a property is not very visible, setting
  11101. this option means that toggling the ORDERED property with the command
  11102. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11103. not relevant for the behavior, but it makes things more visible.
  11104. Note that toggling the tag with tags commands will not change the property
  11105. and therefore not influence behavior!
  11106. This can be t, meaning the tag ORDERED should be used, It can also be a
  11107. string to select a different tag for this task."
  11108. :group 'org-todo
  11109. :type '(choice
  11110. (const :tag "No tracking" nil)
  11111. (const :tag "Track with ORDERED tag" t)
  11112. (string :tag "Use other tag")))
  11113. (defun org-toggle-ordered-property ()
  11114. "Toggle the ORDERED property of the current entry.
  11115. For better visibility, you can track the value of this property with a tag.
  11116. See variable `org-track-ordered-property-with-tag'."
  11117. (interactive)
  11118. (let* ((t1 org-track-ordered-property-with-tag)
  11119. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11120. (save-excursion
  11121. (org-back-to-heading)
  11122. (if (org-entry-get nil "ORDERED")
  11123. (progn
  11124. (org-delete-property "ORDERED" "PROPERTIES")
  11125. (and tag (org-toggle-tag tag 'off))
  11126. (message "Subtasks can be completed in arbitrary order"))
  11127. (org-entry-put nil "ORDERED" "t")
  11128. (and tag (org-toggle-tag tag 'on))
  11129. (message "Subtasks must be completed in sequence")))))
  11130. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11131. (defun org-block-todo-from-checkboxes (change-plist)
  11132. "Block turning an entry into a TODO, using checkboxes.
  11133. This checks whether the current task should be blocked from state
  11134. changes because there are unchecked boxes in this entry."
  11135. (if (not org-enforce-todo-checkbox-dependencies)
  11136. t ; if locally turned off don't block
  11137. (catch 'dont-block
  11138. ;; If this is not a todo state change, or if this entry is already DONE,
  11139. ;; do not block
  11140. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11141. (member (plist-get change-plist :from)
  11142. (cons 'done org-done-keywords))
  11143. (member (plist-get change-plist :to)
  11144. (cons 'todo org-not-done-keywords))
  11145. (not (plist-get change-plist :to)))
  11146. (throw 'dont-block t))
  11147. ;; If this task has checkboxes that are not checked, it's blocked
  11148. (save-excursion
  11149. (org-back-to-heading t)
  11150. (let ((beg (point)) end)
  11151. (outline-next-heading)
  11152. (setq end (point))
  11153. (goto-char beg)
  11154. (if (org-list-search-forward
  11155. (concat (org-item-beginning-re)
  11156. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11157. "\\[[- ]\\]")
  11158. end t)
  11159. (progn
  11160. (if (boundp 'org-blocked-by-checkboxes)
  11161. (setq org-blocked-by-checkboxes t))
  11162. (throw 'dont-block nil)))))
  11163. t))) ; do not block
  11164. (defun org-entry-blocked-p ()
  11165. "Is the current entry blocked?"
  11166. (org-with-silent-modifications
  11167. (if (org-entry-get nil "NOBLOCKING")
  11168. nil ;; Never block this entry
  11169. (not (run-hook-with-args-until-failure
  11170. 'org-blocker-hook
  11171. (list :type 'todo-state-change
  11172. :position (point)
  11173. :from 'todo
  11174. :to 'done))))))
  11175. (defun org-update-statistics-cookies (all)
  11176. "Update the statistics cookie, either from TODO or from checkboxes.
  11177. This should be called with the cursor in a line with a statistics cookie."
  11178. (interactive "P")
  11179. (if all
  11180. (progn
  11181. (org-update-checkbox-count 'all)
  11182. (org-map-entries 'org-update-parent-todo-statistics))
  11183. (if (not (org-at-heading-p))
  11184. (org-update-checkbox-count)
  11185. (let ((pos (point-marker))
  11186. end l1 l2)
  11187. (ignore-errors (org-back-to-heading t))
  11188. (if (not (org-at-heading-p))
  11189. (org-update-checkbox-count)
  11190. (setq l1 (org-outline-level))
  11191. (setq end (save-excursion
  11192. (outline-next-heading)
  11193. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11194. (point)))
  11195. (if (and (save-excursion
  11196. (re-search-forward
  11197. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11198. (not (save-excursion (re-search-forward
  11199. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11200. (org-update-checkbox-count)
  11201. (if (and l2 (> l2 l1))
  11202. (progn
  11203. (goto-char end)
  11204. (org-update-parent-todo-statistics))
  11205. (goto-char pos)
  11206. (beginning-of-line 1)
  11207. (while (re-search-forward
  11208. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11209. (point-at-eol) t)
  11210. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11211. (goto-char pos)
  11212. (move-marker pos nil)))))
  11213. (defvar org-entry-property-inherited-from) ;; defined below
  11214. (defun org-update-parent-todo-statistics ()
  11215. "Update any statistics cookie in the parent of the current headline.
  11216. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11217. the entire subtree and this will travel up the hierarchy and update
  11218. statistics everywhere."
  11219. (let* ((prop (save-excursion (org-up-heading-safe)
  11220. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11221. (recursive (or (not org-hierarchical-todo-statistics)
  11222. (and prop (string-match "\\<recursive\\>" prop))))
  11223. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11224. 0))
  11225. (first t)
  11226. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11227. level ltoggle l1 new ndel
  11228. (cnt-all 0) (cnt-done 0) is-percent kwd
  11229. checkbox-beg ov ovs ove cookie-present)
  11230. (catch 'exit
  11231. (save-excursion
  11232. (beginning-of-line 1)
  11233. (setq ltoggle (funcall outline-level))
  11234. ;; Three situations are to consider:
  11235. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11236. ;; to the top-level ancestor on the headline;
  11237. ;; 2. If parent has "recursive" property, repeat up to the
  11238. ;; headline setting that property, taking inheritance into
  11239. ;; account;
  11240. ;; 3. Else, move up to direct parent and proceed only once.
  11241. (while (and (setq level (org-up-heading-safe))
  11242. (or recursive first)
  11243. (>= (point) lim))
  11244. (setq first nil cookie-present nil)
  11245. (unless (and level
  11246. (not (string-match
  11247. "\\<checkbox\\>"
  11248. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11249. "")))))
  11250. (throw 'exit nil))
  11251. (while (re-search-forward box-re (point-at-eol) t)
  11252. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11253. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11254. (save-match-data
  11255. (unless (outline-next-heading) (throw 'exit nil))
  11256. (while (and (looking-at org-complex-heading-regexp)
  11257. (> (setq l1 (length (match-string 1))) level))
  11258. (setq kwd (and (or recursive (= l1 ltoggle))
  11259. (match-string 2)))
  11260. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11261. (and (listp org-provide-todo-statistics)
  11262. (or (member kwd org-provide-todo-statistics)
  11263. (member kwd org-done-keywords))))
  11264. (setq cnt-all (1+ cnt-all))
  11265. (if (eq org-provide-todo-statistics t)
  11266. (and kwd (setq cnt-all (1+ cnt-all)))))
  11267. (and (member kwd org-done-keywords)
  11268. (setq cnt-done (1+ cnt-done)))
  11269. (outline-next-heading)))
  11270. (setq new
  11271. (if is-percent
  11272. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11273. (format "[%d/%d]" cnt-done cnt-all))
  11274. ndel (- (match-end 0) checkbox-beg))
  11275. ;; handle overlays when updating cookie from column view
  11276. (when (setq ov (car (overlays-at checkbox-beg)))
  11277. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11278. (delete-overlay ov))
  11279. (goto-char checkbox-beg)
  11280. (insert new)
  11281. (delete-region (point) (+ (point) ndel))
  11282. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11283. (when ov (move-overlay ov ovs ove)))
  11284. (when cookie-present
  11285. (run-hook-with-args 'org-after-todo-statistics-hook
  11286. cnt-done (- cnt-all cnt-done))))))
  11287. (run-hooks 'org-todo-statistics-hook)))
  11288. (defvar org-after-todo-statistics-hook nil
  11289. "Hook that is called after a TODO statistics cookie has been updated.
  11290. Each function is called with two arguments: the number of not-done entries
  11291. and the number of done entries.
  11292. For example, the following function, when added to this hook, will switch
  11293. an entry to DONE when all children are done, and back to TODO when new
  11294. entries are set to a TODO status. Note that this hook is only called
  11295. when there is a statistics cookie in the headline!
  11296. (defun org-summary-todo (n-done n-not-done)
  11297. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11298. (let (org-log-done org-log-states) ; turn off logging
  11299. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11300. ")
  11301. (defvar org-todo-statistics-hook nil
  11302. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11303. This hook runs even if there is no statistics cookie present, in which case
  11304. `org-after-todo-statistics-hook' would not run.")
  11305. (defun org-todo-trigger-tag-changes (state)
  11306. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11307. (let ((l org-todo-state-tags-triggers)
  11308. changes)
  11309. (when (or (not state) (equal state ""))
  11310. (setq changes (append changes (cdr (assoc "" l)))))
  11311. (when (and (stringp state) (> (length state) 0))
  11312. (setq changes (append changes (cdr (assoc state l)))))
  11313. (when (member state org-not-done-keywords)
  11314. (setq changes (append changes (cdr (assoc 'todo l)))))
  11315. (when (member state org-done-keywords)
  11316. (setq changes (append changes (cdr (assoc 'done l)))))
  11317. (dolist (c changes)
  11318. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11319. (defun org-local-logging (value)
  11320. "Get logging settings from a property VALUE."
  11321. (let* (words w a)
  11322. ;; directly set the variables, they are already local.
  11323. (setq org-log-done nil
  11324. org-log-repeat nil
  11325. org-todo-log-states nil)
  11326. (setq words (org-split-string value))
  11327. (while (setq w (pop words))
  11328. (cond
  11329. ((setq a (assoc w org-startup-options))
  11330. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11331. (set (nth 1 a) (nth 2 a))))
  11332. ((setq a (org-extract-log-state-settings w))
  11333. (and (member (car a) org-todo-keywords-1)
  11334. (push a org-todo-log-states)))))))
  11335. (defun org-get-todo-sequence-head (kwd)
  11336. "Return the head of the TODO sequence to which KWD belongs.
  11337. If KWD is not set, check if there is a text property remembering the
  11338. right sequence."
  11339. (let (p)
  11340. (cond
  11341. ((not kwd)
  11342. (or (get-text-property (point-at-bol) 'org-todo-head)
  11343. (progn
  11344. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11345. nil (point-at-eol)))
  11346. (get-text-property p 'org-todo-head))))
  11347. ((not (member kwd org-todo-keywords-1))
  11348. (car org-todo-keywords-1))
  11349. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11350. (defun org-fast-todo-selection ()
  11351. "Fast TODO keyword selection with single keys.
  11352. Returns the new TODO keyword, or nil if no state change should occur."
  11353. (let* ((fulltable org-todo-key-alist)
  11354. (done-keywords org-done-keywords) ;; needed for the faces.
  11355. (maxlen (apply 'max (mapcar
  11356. (lambda (x)
  11357. (if (stringp (car x)) (string-width (car x)) 0))
  11358. fulltable)))
  11359. (expert nil)
  11360. (fwidth (+ maxlen 3 1 3))
  11361. (ncol (/ (- (window-width) 4) fwidth))
  11362. tg cnt e c tbl
  11363. groups ingroup)
  11364. (save-excursion
  11365. (save-window-excursion
  11366. (if expert
  11367. (set-buffer (get-buffer-create " *Org todo*"))
  11368. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11369. (erase-buffer)
  11370. (org-set-local 'org-done-keywords done-keywords)
  11371. (setq tbl fulltable cnt 0)
  11372. (while (setq e (pop tbl))
  11373. (cond
  11374. ((equal e '(:startgroup))
  11375. (push '() groups) (setq ingroup t)
  11376. (when (not (= cnt 0))
  11377. (setq cnt 0)
  11378. (insert "\n"))
  11379. (insert "{ "))
  11380. ((equal e '(:endgroup))
  11381. (setq ingroup nil cnt 0)
  11382. (insert "}\n"))
  11383. ((equal e '(:newline))
  11384. (when (not (= cnt 0))
  11385. (setq cnt 0)
  11386. (insert "\n")
  11387. (setq e (car tbl))
  11388. (while (equal (car tbl) '(:newline))
  11389. (insert "\n")
  11390. (setq tbl (cdr tbl)))))
  11391. (t
  11392. (setq tg (car e) c (cdr e))
  11393. (if ingroup (push tg (car groups)))
  11394. (setq tg (org-add-props tg nil 'face
  11395. (org-get-todo-face tg)))
  11396. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11397. (insert "[" c "] " tg (make-string
  11398. (- fwidth 4 (length tg)) ?\ ))
  11399. (when (= (setq cnt (1+ cnt)) ncol)
  11400. (insert "\n")
  11401. (if ingroup (insert " "))
  11402. (setq cnt 0)))))
  11403. (insert "\n")
  11404. (goto-char (point-min))
  11405. (if (not expert) (org-fit-window-to-buffer))
  11406. (message "[a-z..]:Set [SPC]:clear")
  11407. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11408. (cond
  11409. ((or (= c ?\C-g)
  11410. (and (= c ?q) (not (rassoc c fulltable))))
  11411. (setq quit-flag t))
  11412. ((= c ?\ ) nil)
  11413. ((setq e (rassoc c fulltable) tg (car e))
  11414. tg)
  11415. (t (setq quit-flag t)))))))
  11416. (defun org-entry-is-todo-p ()
  11417. (member (org-get-todo-state) org-not-done-keywords))
  11418. (defun org-entry-is-done-p ()
  11419. (member (org-get-todo-state) org-done-keywords))
  11420. (defun org-get-todo-state ()
  11421. (save-excursion
  11422. (org-back-to-heading t)
  11423. (and (looking-at org-todo-line-regexp)
  11424. (match-end 2)
  11425. (match-string 2))))
  11426. (defun org-at-date-range-p (&optional inactive-ok)
  11427. "Is the cursor inside a date range?"
  11428. (interactive)
  11429. (save-excursion
  11430. (catch 'exit
  11431. (let ((pos (point)))
  11432. (skip-chars-backward "^[<\r\n")
  11433. (skip-chars-backward "<[")
  11434. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11435. (>= (match-end 0) pos)
  11436. (throw 'exit t))
  11437. (skip-chars-backward "^<[\r\n")
  11438. (skip-chars-backward "<[")
  11439. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11440. (>= (match-end 0) pos)
  11441. (throw 'exit t)))
  11442. nil)))
  11443. (defun org-get-repeat (&optional tagline)
  11444. "Check if there is a deadline/schedule with repeater in this entry."
  11445. (save-match-data
  11446. (save-excursion
  11447. (org-back-to-heading t)
  11448. (and (re-search-forward (if tagline
  11449. (concat tagline "\\s-*" org-repeat-re)
  11450. org-repeat-re)
  11451. (org-entry-end-position) t)
  11452. (match-string-no-properties 1)))))
  11453. (defvar org-last-changed-timestamp)
  11454. (defvar org-last-inserted-timestamp)
  11455. (defvar org-log-post-message)
  11456. (defvar org-log-note-purpose)
  11457. (defvar org-log-note-how)
  11458. (defvar org-log-note-extra)
  11459. (defun org-auto-repeat-maybe (done-word)
  11460. "Check if the current headline contains a repeated deadline/schedule.
  11461. If yes, set TODO state back to what it was and change the base date
  11462. of repeating deadline/scheduled time stamps to new date.
  11463. This function is run automatically after each state change to a DONE state."
  11464. ;; last-state is dynamically scoped into this function
  11465. (let* ((repeat (org-get-repeat))
  11466. (aa (assoc org-last-state org-todo-kwd-alist))
  11467. (interpret (nth 1 aa))
  11468. (head (nth 2 aa))
  11469. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11470. (msg "Entry repeats: ")
  11471. (org-log-done nil)
  11472. (org-todo-log-states nil)
  11473. re type n what ts time to-state)
  11474. (when repeat
  11475. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11476. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11477. org-todo-repeat-to-state))
  11478. (unless (and to-state (member to-state org-todo-keywords-1))
  11479. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11480. (org-todo to-state)
  11481. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11482. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11483. (org-time-stamp-format t t))))
  11484. (when org-log-repeat
  11485. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11486. (memq 'org-add-log-note post-command-hook))
  11487. ;; OK, we are already setup for some record
  11488. (if (eq org-log-repeat 'note)
  11489. ;; make sure we take a note, not only a time stamp
  11490. (setq org-log-note-how 'note))
  11491. ;; Set up for taking a record
  11492. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11493. org-last-state
  11494. 'findpos org-log-repeat)))
  11495. (org-back-to-heading t)
  11496. (org-add-planning-info nil nil 'closed)
  11497. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11498. org-deadline-time-regexp "\\)\\|\\("
  11499. org-ts-regexp "\\)"))
  11500. (while (re-search-forward
  11501. re (save-excursion (outline-next-heading) (point)) t)
  11502. (setq type (if (match-end 1) org-scheduled-string
  11503. (if (match-end 3) org-deadline-string "Plain:"))
  11504. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11505. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11506. (setq n (string-to-number (match-string 2 ts))
  11507. what (match-string 3 ts))
  11508. (if (equal what "w") (setq n (* n 7) what "d"))
  11509. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11510. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11511. ;; Preparation, see if we need to modify the start date for the change
  11512. (when (match-end 1)
  11513. (setq time (save-match-data (org-time-string-to-time ts)))
  11514. (cond
  11515. ((equal (match-string 1 ts) ".")
  11516. ;; Shift starting date to today
  11517. (org-timestamp-change
  11518. (- (org-today) (time-to-days time))
  11519. 'day))
  11520. ((equal (match-string 1 ts) "+")
  11521. (let ((nshiftmax 10) (nshift 0))
  11522. (while (or (= nshift 0)
  11523. (<= (time-to-days time)
  11524. (time-to-days (current-time))))
  11525. (when (= (incf nshift) nshiftmax)
  11526. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11527. (error "Abort")))
  11528. (org-timestamp-change n (cdr (assoc what whata)))
  11529. (org-at-timestamp-p t)
  11530. (setq ts (match-string 1))
  11531. (setq time (save-match-data (org-time-string-to-time ts)))))
  11532. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11533. ;; rematch, so that we have everything in place for the real shift
  11534. (org-at-timestamp-p t)
  11535. (setq ts (match-string 1))
  11536. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11537. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11538. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11539. (setq org-log-post-message msg)
  11540. (message "%s" msg))))
  11541. (defun org-show-todo-tree (arg)
  11542. "Make a compact tree which shows all headlines marked with TODO.
  11543. The tree will show the lines where the regexp matches, and all higher
  11544. headlines above the match.
  11545. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11546. With a numeric prefix N, construct a sparse tree for the Nth element
  11547. of `org-todo-keywords-1'."
  11548. (interactive "P")
  11549. (let ((case-fold-search nil)
  11550. (kwd-re
  11551. (cond ((null arg) org-not-done-regexp)
  11552. ((equal arg '(4))
  11553. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11554. (mapcar 'list org-todo-keywords-1))))
  11555. (concat "\\("
  11556. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11557. "\\)\\>")))
  11558. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11559. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11560. org-todo-keywords-1)))
  11561. (t (error "Invalid prefix argument: %s" arg)))))
  11562. (message "%d TODO entries found"
  11563. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11564. (defun org-deadline (arg &optional time)
  11565. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11566. With one universal prefix argument, remove any deadline from the item.
  11567. With two universal prefix arguments, prompt for a warning delay.
  11568. With argument TIME, set the deadline at the corresponding date. TIME
  11569. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11570. (interactive "P")
  11571. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11572. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11573. 'region-start-level 'region))
  11574. org-loop-over-headlines-in-active-region)
  11575. (org-map-entries
  11576. `(org-deadline ',arg ,time)
  11577. org-loop-over-headlines-in-active-region
  11578. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11579. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11580. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11581. (repeater (and old-date
  11582. (string-match
  11583. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11584. old-date)
  11585. (match-string 1 old-date))))
  11586. (cond
  11587. ((equal arg '(4))
  11588. (when (and old-date org-log-redeadline)
  11589. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11590. org-log-redeadline))
  11591. (org-remove-timestamp-with-keyword org-deadline-string)
  11592. (message "Item no longer has a deadline."))
  11593. ((equal arg '(16))
  11594. (save-excursion
  11595. (if (re-search-forward
  11596. org-deadline-time-regexp
  11597. (save-excursion (outline-next-heading) (point)) t)
  11598. (let* ((rpl0 (match-string 1))
  11599. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11600. (replace-match
  11601. (concat org-deadline-string
  11602. " <" rpl
  11603. (format " -%dd"
  11604. (abs
  11605. (- (time-to-days
  11606. (save-match-data
  11607. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11608. (time-to-days old-date-time))))
  11609. ">") t t))
  11610. (user-error "No deadline information to update"))))
  11611. (t
  11612. (org-add-planning-info 'deadline time 'closed)
  11613. (when (and old-date org-log-redeadline
  11614. (not (equal old-date
  11615. (substring org-last-inserted-timestamp 1 -1))))
  11616. (org-add-log-setup 'redeadline nil old-date 'findpos
  11617. org-log-redeadline))
  11618. (when repeater
  11619. (save-excursion
  11620. (org-back-to-heading t)
  11621. (when (re-search-forward (concat org-deadline-string " "
  11622. org-last-inserted-timestamp)
  11623. (save-excursion
  11624. (outline-next-heading) (point)) t)
  11625. (goto-char (1- (match-end 0)))
  11626. (insert " " repeater)
  11627. (setq org-last-inserted-timestamp
  11628. (concat (substring org-last-inserted-timestamp 0 -1)
  11629. " " repeater
  11630. (substring org-last-inserted-timestamp -1))))))
  11631. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11632. (defun org-schedule (arg &optional time)
  11633. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11634. With one universal prefix argument, remove any scheduling date from the item.
  11635. With two universal prefix arguments, prompt for a delay cookie.
  11636. With argument TIME, scheduled at the corresponding date. TIME can
  11637. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11638. (interactive "P")
  11639. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11640. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11641. 'region-start-level 'region))
  11642. org-loop-over-headlines-in-active-region)
  11643. (org-map-entries
  11644. `(org-schedule ',arg ,time)
  11645. org-loop-over-headlines-in-active-region
  11646. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11647. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11648. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11649. (repeater (and old-date
  11650. (string-match
  11651. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11652. old-date)
  11653. (match-string 1 old-date))))
  11654. (cond
  11655. ((equal arg '(4))
  11656. (progn
  11657. (when (and old-date org-log-reschedule)
  11658. (org-add-log-setup 'delschedule nil old-date 'findpos
  11659. org-log-reschedule))
  11660. (org-remove-timestamp-with-keyword org-scheduled-string)
  11661. (message "Item is no longer scheduled.")))
  11662. ((equal arg '(16))
  11663. (save-excursion
  11664. (if (re-search-forward
  11665. org-scheduled-time-regexp
  11666. (save-excursion (outline-next-heading) (point)) t)
  11667. (let* ((rpl0 (match-string 1))
  11668. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11669. (replace-match
  11670. (concat org-scheduled-string
  11671. " <" rpl
  11672. (format " -%dd"
  11673. (abs
  11674. (- (time-to-days
  11675. (save-match-data
  11676. (org-read-date nil t nil "Delay until" old-date-time)))
  11677. (time-to-days old-date-time))))
  11678. ">") t t))
  11679. (user-error "No scheduled information to update"))))
  11680. (t
  11681. (org-add-planning-info 'scheduled time 'closed)
  11682. (when (and old-date org-log-reschedule
  11683. (not (equal old-date
  11684. (substring org-last-inserted-timestamp 1 -1))))
  11685. (org-add-log-setup 'reschedule nil old-date 'findpos
  11686. org-log-reschedule))
  11687. (when repeater
  11688. (save-excursion
  11689. (org-back-to-heading t)
  11690. (when (re-search-forward (concat org-scheduled-string " "
  11691. org-last-inserted-timestamp)
  11692. (save-excursion
  11693. (outline-next-heading) (point)) t)
  11694. (goto-char (1- (match-end 0)))
  11695. (insert " " repeater)
  11696. (setq org-last-inserted-timestamp
  11697. (concat (substring org-last-inserted-timestamp 0 -1)
  11698. " " repeater
  11699. (substring org-last-inserted-timestamp -1))))))
  11700. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11701. (defun org-get-scheduled-time (pom &optional inherit)
  11702. "Get the scheduled time as a time tuple, of a format suitable
  11703. for calling org-schedule with, or if there is no scheduling,
  11704. returns nil."
  11705. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11706. (when time
  11707. (apply 'encode-time (org-parse-time-string time)))))
  11708. (defun org-get-deadline-time (pom &optional inherit)
  11709. "Get the deadline as a time tuple, of a format suitable for
  11710. calling org-deadline with, or if there is no scheduling, returns
  11711. nil."
  11712. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11713. (when time
  11714. (apply 'encode-time (org-parse-time-string time)))))
  11715. (defun org-remove-timestamp-with-keyword (keyword)
  11716. "Remove all time stamps with KEYWORD in the current entry."
  11717. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11718. beg)
  11719. (save-excursion
  11720. (org-back-to-heading t)
  11721. (setq beg (point))
  11722. (outline-next-heading)
  11723. (while (re-search-backward re beg t)
  11724. (replace-match "")
  11725. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11726. (equal (char-before) ?\ ))
  11727. (backward-delete-char 1)
  11728. (if (string-match "^[ \t]*$" (buffer-substring
  11729. (point-at-bol) (point-at-eol)))
  11730. (delete-region (point-at-bol)
  11731. (min (point-max) (1+ (point-at-eol))))))))))
  11732. (defun org-add-planning-info (what &optional time &rest remove)
  11733. "Insert new timestamp with keyword in the line directly after the headline.
  11734. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11735. If non is given, the user is prompted for a date.
  11736. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11737. be removed."
  11738. (interactive)
  11739. (let (org-time-was-given org-end-time-was-given ts
  11740. end default-time default-input)
  11741. (catch 'exit
  11742. (when (and (memq what '(scheduled deadline))
  11743. (or (not time)
  11744. (and (stringp time)
  11745. (string-match "^[-+]+[0-9]" time))))
  11746. ;; Try to get a default date/time from existing timestamp
  11747. (save-excursion
  11748. (org-back-to-heading t)
  11749. (setq end (save-excursion (outline-next-heading) (point)))
  11750. (when (re-search-forward (if (eq what 'scheduled)
  11751. org-scheduled-time-regexp
  11752. org-deadline-time-regexp)
  11753. end t)
  11754. (setq ts (match-string 1)
  11755. default-time
  11756. (apply 'encode-time (org-parse-time-string ts))
  11757. default-input (and ts (org-get-compact-tod ts))))))
  11758. (when what
  11759. (setq time
  11760. (if (stringp time)
  11761. ;; This is a string (relative or absolute), set proper date
  11762. (apply 'encode-time
  11763. (org-read-date-analyze
  11764. time default-time (decode-time default-time)))
  11765. ;; If necessary, get the time from the user
  11766. (or time (org-read-date nil 'to-time nil nil
  11767. default-time default-input)))))
  11768. (when (and org-insert-labeled-timestamps-at-point
  11769. (member what '(scheduled deadline)))
  11770. (insert
  11771. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11772. (org-insert-time-stamp time org-time-was-given
  11773. nil nil nil (list org-end-time-was-given))
  11774. (setq what nil))
  11775. (save-excursion
  11776. (save-restriction
  11777. (let (col list elt ts buffer-invisibility-spec)
  11778. (org-back-to-heading t)
  11779. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11780. (goto-char (match-end 1))
  11781. (setq col (current-column))
  11782. (goto-char (match-end 0))
  11783. (if (eobp) (insert "\n") (forward-char 1))
  11784. (when (and (not what)
  11785. (not (looking-at
  11786. (concat "[ \t]*"
  11787. org-keyword-time-not-clock-regexp))))
  11788. ;; Nothing to add, nothing to remove...... :-)
  11789. (throw 'exit nil))
  11790. (if (and (not (looking-at org-outline-regexp))
  11791. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11792. "[^\r\n]*"))
  11793. (not (equal (match-string 1) org-clock-string)))
  11794. (narrow-to-region (match-beginning 0) (match-end 0))
  11795. (insert-before-markers "\n")
  11796. (backward-char 1)
  11797. (narrow-to-region (point) (point))
  11798. (and org-adapt-indentation (org-indent-to-column col)))
  11799. ;; Check if we have to remove something.
  11800. (setq list (cons what remove))
  11801. (while list
  11802. (setq elt (pop list))
  11803. (when (or (and (eq elt 'scheduled)
  11804. (re-search-forward org-scheduled-time-regexp nil t))
  11805. (and (eq elt 'deadline)
  11806. (re-search-forward org-deadline-time-regexp nil t))
  11807. (and (eq elt 'closed)
  11808. (re-search-forward org-closed-time-regexp nil t)))
  11809. (replace-match "")
  11810. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11811. (and (looking-at "[ \t]+") (replace-match ""))
  11812. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11813. (when what
  11814. (insert
  11815. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11816. (cond ((eq what 'scheduled) org-scheduled-string)
  11817. ((eq what 'deadline) org-deadline-string)
  11818. ((eq what 'closed) org-closed-string))
  11819. " ")
  11820. (setq ts (org-insert-time-stamp
  11821. time
  11822. (or org-time-was-given
  11823. (and (eq what 'closed) org-log-done-with-time))
  11824. (eq what 'closed)
  11825. nil nil (list org-end-time-was-given)))
  11826. (insert
  11827. (if (not (or (bolp) (eq (char-before) ?\ )
  11828. (memq (char-after) '(32 10))
  11829. (eobp))) " " ""))
  11830. (end-of-line 1))
  11831. (goto-char (point-min))
  11832. (widen)
  11833. (if (and (looking-at "[ \t]*\n")
  11834. (equal (char-before) ?\n))
  11835. (delete-region (1- (point)) (point-at-eol)))
  11836. ts))))))
  11837. (defvar org-log-note-marker (make-marker))
  11838. (defvar org-log-note-purpose nil)
  11839. (defvar org-log-note-state nil)
  11840. (defvar org-log-note-previous-state nil)
  11841. (defvar org-log-note-how nil)
  11842. (defvar org-log-note-extra nil)
  11843. (defvar org-log-note-window-configuration nil)
  11844. (defvar org-log-note-return-to (make-marker))
  11845. (defvar org-log-note-effective-time nil
  11846. "Remembered current time so that dynamically scoped
  11847. `org-extend-today-until' affects tha timestamps in state change
  11848. log")
  11849. (defvar org-log-post-message nil
  11850. "Message to be displayed after a log note has been stored.
  11851. The auto-repeater uses this.")
  11852. (defun org-add-note ()
  11853. "Add a note to the current entry.
  11854. This is done in the same way as adding a state change note."
  11855. (interactive)
  11856. (org-add-log-setup 'note nil nil 'findpos nil))
  11857. (defvar org-property-end-re)
  11858. (defun org-add-log-setup (&optional purpose state prev-state
  11859. findpos how extra)
  11860. "Set up the post command hook to take a note.
  11861. If this is about to TODO state change, the new state is expected in STATE.
  11862. When FINDPOS is non-nil, find the correct position for the note in
  11863. the current entry. If not, assume that it can be inserted at point.
  11864. HOW is an indicator what kind of note should be created.
  11865. EXTRA is additional text that will be inserted into the notes buffer."
  11866. (let* ((org-log-into-drawer (org-log-into-drawer))
  11867. (drawer (cond ((stringp org-log-into-drawer)
  11868. org-log-into-drawer)
  11869. (org-log-into-drawer "LOGBOOK"))))
  11870. (save-restriction
  11871. (save-excursion
  11872. (when findpos
  11873. (org-back-to-heading t)
  11874. (narrow-to-region (point) (save-excursion
  11875. (outline-next-heading) (point)))
  11876. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11877. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11878. "[^\r\n]*\\)?"))
  11879. (goto-char (match-end 0))
  11880. (cond
  11881. (drawer
  11882. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11883. nil t)
  11884. (progn
  11885. (goto-char (match-end 0))
  11886. (or org-log-states-order-reversed
  11887. (and (re-search-forward org-property-end-re nil t)
  11888. (goto-char (1- (match-beginning 0))))))
  11889. (insert "\n:" drawer ":\n:END:")
  11890. (beginning-of-line 0)
  11891. (org-indent-line)
  11892. (beginning-of-line 2)
  11893. (org-indent-line)
  11894. (end-of-line 0)))
  11895. ((and org-log-state-notes-insert-after-drawers
  11896. (save-excursion
  11897. (forward-line) (looking-at org-drawer-regexp)))
  11898. (forward-line)
  11899. (while (looking-at org-drawer-regexp)
  11900. (goto-char (match-end 0))
  11901. (re-search-forward org-property-end-re (point-max) t)
  11902. (forward-line))
  11903. (forward-line -1)))
  11904. (unless org-log-states-order-reversed
  11905. (and (= (char-after) ?\n) (forward-char 1))
  11906. (org-skip-over-state-notes)
  11907. (skip-chars-backward " \t\n\r")))
  11908. (move-marker org-log-note-marker (point))
  11909. (setq org-log-note-purpose purpose
  11910. org-log-note-state state
  11911. org-log-note-previous-state prev-state
  11912. org-log-note-how how
  11913. org-log-note-extra extra
  11914. org-log-note-effective-time (org-current-effective-time))
  11915. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11916. (defun org-skip-over-state-notes ()
  11917. "Skip past the list of State notes in an entry."
  11918. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11919. (when (ignore-errors (goto-char (org-in-item-p)))
  11920. (let* ((struct (org-list-struct))
  11921. (prevs (org-list-prevs-alist struct)))
  11922. (while (looking-at "[ \t]*- State")
  11923. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11924. (org-list-get-item-end (point) struct)))))))
  11925. (defun org-add-log-note (&optional purpose)
  11926. "Pop up a window for taking a note, and add this note later at point."
  11927. (remove-hook 'post-command-hook 'org-add-log-note)
  11928. (setq org-log-note-window-configuration (current-window-configuration))
  11929. (delete-other-windows)
  11930. (move-marker org-log-note-return-to (point))
  11931. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11932. (goto-char org-log-note-marker)
  11933. (org-switch-to-buffer-other-window "*Org Note*")
  11934. (erase-buffer)
  11935. (if (memq org-log-note-how '(time state))
  11936. (let (current-prefix-arg) (org-store-log-note))
  11937. (let ((org-inhibit-startup t)) (org-mode))
  11938. (insert (format "# Insert note for %s.
  11939. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11940. (cond
  11941. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11942. ((eq org-log-note-purpose 'done) "closed todo item")
  11943. ((eq org-log-note-purpose 'state)
  11944. (format "state change from \"%s\" to \"%s\""
  11945. (or org-log-note-previous-state "")
  11946. (or org-log-note-state "")))
  11947. ((eq org-log-note-purpose 'reschedule)
  11948. "rescheduling")
  11949. ((eq org-log-note-purpose 'delschedule)
  11950. "no longer scheduled")
  11951. ((eq org-log-note-purpose 'redeadline)
  11952. "changing deadline")
  11953. ((eq org-log-note-purpose 'deldeadline)
  11954. "removing deadline")
  11955. ((eq org-log-note-purpose 'refile)
  11956. "refiling")
  11957. ((eq org-log-note-purpose 'note)
  11958. "this entry")
  11959. (t (error "This should not happen")))))
  11960. (if org-log-note-extra (insert org-log-note-extra))
  11961. (org-set-local 'org-finish-function 'org-store-log-note)
  11962. (run-hooks 'org-log-buffer-setup-hook)))
  11963. (defvar org-note-abort nil) ; dynamically scoped
  11964. (defun org-store-log-note ()
  11965. "Finish taking a log note, and insert it to where it belongs."
  11966. (let ((txt (buffer-string)))
  11967. (kill-buffer (current-buffer))
  11968. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11969. lines ind bul)
  11970. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11971. (setq txt (replace-match "" t t txt)))
  11972. (if (string-match "\\s-+\\'" txt)
  11973. (setq txt (replace-match "" t t txt)))
  11974. (setq lines (org-split-string txt "\n"))
  11975. (when (and note (string-match "\\S-" note))
  11976. (setq note
  11977. (org-replace-escapes
  11978. note
  11979. (list (cons "%u" (user-login-name))
  11980. (cons "%U" user-full-name)
  11981. (cons "%t" (format-time-string
  11982. (org-time-stamp-format 'long 'inactive)
  11983. org-log-note-effective-time))
  11984. (cons "%T" (format-time-string
  11985. (org-time-stamp-format 'long nil)
  11986. org-log-note-effective-time))
  11987. (cons "%d" (format-time-string
  11988. (org-time-stamp-format nil 'inactive)
  11989. org-log-note-effective-time))
  11990. (cons "%D" (format-time-string
  11991. (org-time-stamp-format nil nil)
  11992. org-log-note-effective-time))
  11993. (cons "%s" (if org-log-note-state
  11994. (concat "\"" org-log-note-state "\"")
  11995. ""))
  11996. (cons "%S" (if org-log-note-previous-state
  11997. (concat "\"" org-log-note-previous-state "\"")
  11998. "\"\"")))))
  11999. (if lines (setq note (concat note " \\\\")))
  12000. (push note lines))
  12001. (when (or current-prefix-arg org-note-abort)
  12002. (when org-log-into-drawer
  12003. (org-remove-empty-drawer-at
  12004. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12005. org-log-note-marker))
  12006. (setq lines nil))
  12007. (when lines
  12008. (with-current-buffer (marker-buffer org-log-note-marker)
  12009. (save-excursion
  12010. (goto-char org-log-note-marker)
  12011. (move-marker org-log-note-marker nil)
  12012. (end-of-line 1)
  12013. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12014. (setq ind (save-excursion
  12015. (if (ignore-errors (goto-char (org-in-item-p)))
  12016. (let ((struct (org-list-struct)))
  12017. (org-list-get-ind
  12018. (org-list-get-top-point struct) struct))
  12019. (skip-chars-backward " \r\t\n")
  12020. (cond
  12021. ((and (org-at-heading-p)
  12022. org-adapt-indentation)
  12023. (1+ (org-current-level)))
  12024. ((org-at-heading-p) 0)
  12025. (t (org-get-indentation))))))
  12026. (setq bul (org-list-bullet-string "-"))
  12027. (org-indent-line-to ind)
  12028. (insert bul (pop lines))
  12029. (let ((ind-body (+ (length bul) ind)))
  12030. (while lines
  12031. (insert "\n")
  12032. (org-indent-line-to ind-body)
  12033. (insert (pop lines))))
  12034. (message "Note stored")
  12035. (org-back-to-heading t)
  12036. (org-cycle-hide-drawers 'children))))))
  12037. (set-window-configuration org-log-note-window-configuration)
  12038. (with-current-buffer (marker-buffer org-log-note-return-to)
  12039. (goto-char org-log-note-return-to))
  12040. (move-marker org-log-note-return-to nil)
  12041. (and org-log-post-message (message "%s" org-log-post-message)))
  12042. (defun org-remove-empty-drawer-at (drawer pos)
  12043. "Remove an empty drawer DRAWER at position POS.
  12044. POS may also be a marker."
  12045. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12046. (save-excursion
  12047. (save-restriction
  12048. (widen)
  12049. (goto-char pos)
  12050. (if (org-in-regexp
  12051. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12052. (replace-match ""))))))
  12053. (defvar org-ts-type nil)
  12054. (defun org-sparse-tree (&optional arg type)
  12055. "Create a sparse tree, prompt for the details.
  12056. This command can create sparse trees. You first need to select the type
  12057. of match used to create the tree:
  12058. t Show all TODO entries.
  12059. T Show entries with a specific TODO keyword.
  12060. m Show entries selected by a tags/property match.
  12061. p Enter a property name and its value (both with completion on existing
  12062. names/values) and show entries with that property.
  12063. r Show entries matching a regular expression (`/' can be used as well).
  12064. b Show deadlines and scheduled items before a date.
  12065. a Show deadlines and scheduled items after a date.
  12066. d Show deadlines due within `org-deadline-warning-days'.
  12067. D Show deadlines and scheduled items between a date range."
  12068. (interactive "P")
  12069. (let (ans kwd value ts-type)
  12070. (setq type (or type org-sparse-tree-default-date-type))
  12071. (setq org-ts-type type)
  12072. (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"
  12073. (cond ((eq type 'all) "all timestamps")
  12074. ((eq type 'scheduled) "only scheduled")
  12075. ((eq type 'deadline) "only deadline")
  12076. ((eq type 'active) "only active timestamps")
  12077. ((eq type 'inactive) "only inactive timestamps")
  12078. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12079. (t "scheduled/deadline")))
  12080. (setq ans (read-char-exclusive))
  12081. (cond
  12082. ((equal ans ?c)
  12083. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  12084. ((equal ans ?d)
  12085. (call-interactively 'org-check-deadlines))
  12086. ((equal ans ?b)
  12087. (call-interactively 'org-check-before-date))
  12088. ((equal ans ?a)
  12089. (call-interactively 'org-check-after-date))
  12090. ((equal ans ?D)
  12091. (call-interactively 'org-check-dates-range))
  12092. ((equal ans ?t)
  12093. (call-interactively 'org-show-todo-tree))
  12094. ((equal ans ?T)
  12095. (org-show-todo-tree '(4)))
  12096. ((member ans '(?T ?m))
  12097. (call-interactively 'org-match-sparse-tree))
  12098. ((member ans '(?p ?P))
  12099. (setq kwd (org-icompleting-read "Property: "
  12100. (mapcar 'list (org-buffer-property-keys))))
  12101. (setq value (org-icompleting-read "Value: "
  12102. (mapcar 'list (org-property-values kwd))))
  12103. (unless (string-match "\\`{.*}\\'" value)
  12104. (setq value (concat "\"" value "\"")))
  12105. (org-match-sparse-tree arg (concat kwd "=" value)))
  12106. ((member ans '(?r ?R ?/))
  12107. (call-interactively 'org-occur))
  12108. (t (error "No such sparse tree command \"%c\"" ans)))))
  12109. (defvar org-occur-highlights nil
  12110. "List of overlays used for occur matches.")
  12111. (make-variable-buffer-local 'org-occur-highlights)
  12112. (defvar org-occur-parameters nil
  12113. "Parameters of the active org-occur calls.
  12114. This is a list, each call to org-occur pushes as cons cell,
  12115. containing the regular expression and the callback, onto the list.
  12116. The list can contain several entries if `org-occur' has been called
  12117. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12118. will only contain one set of parameters. When the highlights are
  12119. removed (for example with `C-c C-c', or with the next edit (depending
  12120. on `org-remove-highlights-with-change'), this variable is emptied
  12121. as well.")
  12122. (make-variable-buffer-local 'org-occur-parameters)
  12123. (defun org-occur (regexp &optional keep-previous callback)
  12124. "Make a compact tree which shows all matches of REGEXP.
  12125. The tree will show the lines where the regexp matches, and all higher
  12126. headlines above the match. It will also show the heading after the match,
  12127. to make sure editing the matching entry is easy.
  12128. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12129. call to `org-occur' will be kept, to allow stacking of calls to this
  12130. command.
  12131. If CALLBACK is non-nil, it is a function which is called to confirm
  12132. that the match should indeed be shown."
  12133. (interactive "sRegexp: \nP")
  12134. (when (equal regexp "")
  12135. (error "Regexp cannot be empty"))
  12136. (unless keep-previous
  12137. (org-remove-occur-highlights nil nil t))
  12138. (push (cons regexp callback) org-occur-parameters)
  12139. (let ((cnt 0))
  12140. (save-excursion
  12141. (goto-char (point-min))
  12142. (if (or (not keep-previous) ; do not want to keep
  12143. (not org-occur-highlights)) ; no previous matches
  12144. ;; hide everything
  12145. (org-overview))
  12146. (while (re-search-forward regexp nil t)
  12147. (when (or (not callback)
  12148. (save-match-data (funcall callback)))
  12149. (setq cnt (1+ cnt))
  12150. (when org-highlight-sparse-tree-matches
  12151. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12152. (org-show-context 'occur-tree))))
  12153. (when org-remove-highlights-with-change
  12154. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12155. nil 'local))
  12156. (unless org-sparse-tree-open-archived-trees
  12157. (org-hide-archived-subtrees (point-min) (point-max)))
  12158. (run-hooks 'org-occur-hook)
  12159. (if (org-called-interactively-p 'interactive)
  12160. (message "%d match(es) for regexp %s" cnt regexp))
  12161. cnt))
  12162. (defun org-occur-next-match (&optional n reset)
  12163. "Function for `next-error-function' to find sparse tree matches.
  12164. N is the number of matches to move, when negative move backwards.
  12165. RESET is entirely ignored - this function always goes back to the
  12166. starting point when no match is found."
  12167. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12168. (search-func (if (< n 0)
  12169. 'previous-single-char-property-change
  12170. 'next-single-char-property-change))
  12171. (n (abs n))
  12172. (pos (point))
  12173. p1)
  12174. (catch 'exit
  12175. (while (setq p1 (funcall search-func (point) 'org-type))
  12176. (when (equal p1 limit)
  12177. (goto-char pos)
  12178. (error "No more matches"))
  12179. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12180. (setq n (1- n))
  12181. (when (= n 0)
  12182. (goto-char p1)
  12183. (throw 'exit (point))))
  12184. (goto-char p1))
  12185. (goto-char p1)
  12186. (error "No more matches"))))
  12187. (defun org-show-context (&optional key)
  12188. "Make sure point and context are visible.
  12189. How much context is shown depends upon the variables
  12190. `org-show-hierarchy-above', `org-show-following-heading',
  12191. `org-show-entry-below' and `org-show-siblings'."
  12192. (let ((heading-p (org-at-heading-p t))
  12193. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12194. (following-p (org-get-alist-option org-show-following-heading key))
  12195. (entry-p (org-get-alist-option org-show-entry-below key))
  12196. (siblings-p (org-get-alist-option org-show-siblings key)))
  12197. ;; Show heading or entry text
  12198. (if (and heading-p (not entry-p))
  12199. (org-flag-heading nil) ; only show the heading
  12200. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12201. (org-show-hidden-entry))) ; show entire entry
  12202. (when following-p
  12203. ;; Show next sibling, or heading below text
  12204. (save-excursion
  12205. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12206. (org-flag-heading nil))))
  12207. (when siblings-p (org-show-siblings))
  12208. (when hierarchy-p
  12209. ;; show all higher headings, possibly with siblings
  12210. (save-excursion
  12211. (while (and (condition-case nil
  12212. (progn (org-up-heading-all 1) t)
  12213. (error nil))
  12214. (not (bobp)))
  12215. (org-flag-heading nil)
  12216. (when siblings-p (org-show-siblings)))))
  12217. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12218. (defvar org-reveal-start-hook nil
  12219. "Hook run before revealing a location.")
  12220. (defun org-reveal (&optional siblings)
  12221. "Show current entry, hierarchy above it, and the following headline.
  12222. This can be used to show a consistent set of context around locations
  12223. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12224. not t for the search context.
  12225. With optional argument SIBLINGS, on each level of the hierarchy all
  12226. siblings are shown. This repairs the tree structure to what it would
  12227. look like when opened with hierarchical calls to `org-cycle'.
  12228. With double optional argument \\[universal-argument] \\[universal-argument], \
  12229. go to the parent and show the
  12230. entire tree."
  12231. (interactive "P")
  12232. (run-hooks 'org-reveal-start-hook)
  12233. (let ((org-show-hierarchy-above t)
  12234. (org-show-following-heading t)
  12235. (org-show-siblings (if siblings t org-show-siblings)))
  12236. (org-show-context nil))
  12237. (when (equal siblings '(16))
  12238. (save-excursion
  12239. (when (org-up-heading-safe)
  12240. (org-show-subtree)
  12241. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12242. (defun org-highlight-new-match (beg end)
  12243. "Highlight from BEG to END and mark the highlight is an occur headline."
  12244. (let ((ov (make-overlay beg end)))
  12245. (overlay-put ov 'face 'secondary-selection)
  12246. (overlay-put ov 'org-type 'org-occur)
  12247. (push ov org-occur-highlights)))
  12248. (defun org-remove-occur-highlights (&optional beg end noremove)
  12249. "Remove the occur highlights from the buffer.
  12250. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12251. from the `before-change-functions' in the current buffer."
  12252. (interactive)
  12253. (unless org-inhibit-highlight-removal
  12254. (mapc 'delete-overlay org-occur-highlights)
  12255. (setq org-occur-highlights nil)
  12256. (setq org-occur-parameters nil)
  12257. (unless noremove
  12258. (remove-hook 'before-change-functions
  12259. 'org-remove-occur-highlights 'local))))
  12260. ;;;; Priorities
  12261. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12262. "Regular expression matching the priority indicator.")
  12263. (defvar org-remove-priority-next-time nil)
  12264. (defun org-priority-up ()
  12265. "Increase the priority of the current item."
  12266. (interactive)
  12267. (org-priority 'up))
  12268. (defun org-priority-down ()
  12269. "Decrease the priority of the current item."
  12270. (interactive)
  12271. (org-priority 'down))
  12272. (defun org-priority (&optional action show)
  12273. "Change the priority of an item.
  12274. ACTION can be `set', `up', `down', or a character."
  12275. (interactive "P")
  12276. (if (equal action '(4))
  12277. (org-show-priority)
  12278. (unless org-enable-priority-commands
  12279. (error "Priority commands are disabled"))
  12280. (setq action (or action 'set))
  12281. (let (current new news have remove)
  12282. (save-excursion
  12283. (org-back-to-heading t)
  12284. (if (looking-at org-priority-regexp)
  12285. (setq current (string-to-char (match-string 2))
  12286. have t))
  12287. (cond
  12288. ((eq action 'remove)
  12289. (setq remove t new ?\ ))
  12290. ((or (eq action 'set)
  12291. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12292. (if (not (eq action 'set))
  12293. (setq new action)
  12294. (message "Priority %c-%c, SPC to remove: "
  12295. org-highest-priority org-lowest-priority)
  12296. (save-match-data
  12297. (setq new (read-char-exclusive))))
  12298. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12299. (= (upcase org-lowest-priority) org-lowest-priority))
  12300. (setq new (upcase new)))
  12301. (cond ((equal new ?\ ) (setq remove t))
  12302. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12303. (error "Priority must be between `%c' and `%c'"
  12304. org-highest-priority org-lowest-priority))))
  12305. ((eq action 'up)
  12306. (setq new (if have
  12307. (1- current) ; normal cycling
  12308. ;; last priority was empty
  12309. (if (eq last-command this-command)
  12310. org-lowest-priority ; wrap around empty to lowest
  12311. ;; default
  12312. (if org-priority-start-cycle-with-default
  12313. org-default-priority
  12314. (1- org-default-priority))))))
  12315. ((eq action 'down)
  12316. (setq new (if have
  12317. (1+ current) ; normal cycling
  12318. ;; last priority was empty
  12319. (if (eq last-command this-command)
  12320. org-highest-priority ; wrap around empty to highest
  12321. ;; default
  12322. (if org-priority-start-cycle-with-default
  12323. org-default-priority
  12324. (1+ org-default-priority))))))
  12325. (t (error "Invalid action")))
  12326. (if (or (< (upcase new) org-highest-priority)
  12327. (> (upcase new) org-lowest-priority))
  12328. (if (and (memq action '(up down))
  12329. (not have) (not (eq last-command this-command)))
  12330. ;; `new' is from default priority
  12331. (error
  12332. "The default can not be set, see `org-default-priority' why")
  12333. ;; normal cycling: `new' is beyond highest/lowest priority
  12334. ;; and is wrapped around to the empty priority
  12335. (setq remove t)))
  12336. (setq news (format "%c" new))
  12337. (if have
  12338. (if remove
  12339. (replace-match "" t t nil 1)
  12340. (replace-match news t t nil 2))
  12341. (if remove
  12342. (error "No priority cookie found in line")
  12343. (let ((case-fold-search nil))
  12344. (looking-at org-todo-line-regexp))
  12345. (if (match-end 2)
  12346. (progn
  12347. (goto-char (match-end 2))
  12348. (insert " [#" news "]"))
  12349. (goto-char (match-beginning 3))
  12350. (insert "[#" news "] "))))
  12351. (org-preserve-lc (org-set-tags nil 'align)))
  12352. (if remove
  12353. (message "Priority removed")
  12354. (message "Priority of current item set to %s" news)))))
  12355. (defun org-show-priority ()
  12356. "Show the priority of the current item.
  12357. This priority is composed of the main priority given with the [#A] cookies,
  12358. and by additional input from the age of a schedules or deadline entry."
  12359. (interactive)
  12360. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12361. (org-get-at-bol 'priority)
  12362. (save-excursion
  12363. (save-match-data
  12364. (beginning-of-line)
  12365. (and (looking-at org-heading-regexp)
  12366. (org-get-priority (match-string 0))))))))
  12367. (message "Priority is %d" (if pri pri -1000))))
  12368. (defun org-get-priority (s)
  12369. "Find priority cookie and return priority."
  12370. (save-match-data
  12371. (if (functionp org-get-priority-function)
  12372. (funcall org-get-priority-function)
  12373. (if (not (string-match org-priority-regexp s))
  12374. (* 1000 (- org-lowest-priority org-default-priority))
  12375. (* 1000 (- org-lowest-priority
  12376. (string-to-char (match-string 2 s))))))))
  12377. ;;;; Tags
  12378. (defvar org-agenda-archives-mode)
  12379. (defvar org-map-continue-from nil
  12380. "Position from where mapping should continue.
  12381. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12382. (defvar org-scanner-tags nil
  12383. "The current tag list while the tags scanner is running.")
  12384. (defvar org-trust-scanner-tags nil
  12385. "Should `org-get-tags-at' use the tags for the scanner.
  12386. This is for internal dynamical scoping only.
  12387. When this is non-nil, the function `org-get-tags-at' will return the value
  12388. of `org-scanner-tags' instead of building the list by itself. This
  12389. can lead to large speed-ups when the tags scanner is used in a file with
  12390. many entries, and when the list of tags is retrieved, for example to
  12391. obtain a list of properties. Building the tags list for each entry in such
  12392. a file becomes an N^2 operation - but with this variable set, it scales
  12393. as N.")
  12394. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12395. "Sca headline tags with inheritance and produce output ACTION.
  12396. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12397. or `agenda' to produce an entry list for an agenda view. It can also be
  12398. a Lisp form or a function that should be called at each matched headline, in
  12399. this case the return value is a list of all return values from these calls.
  12400. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12401. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12402. only lines with a not-done TODO keyword are included in the output.
  12403. This should be the same variable that was scoped into
  12404. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12405. START-LEVEL can be a string with asterisks, reducing the scope to
  12406. headlines matching this string."
  12407. (require 'org-agenda)
  12408. (let* ((re (concat "^"
  12409. (if start-level
  12410. ;; Get the correct level to match
  12411. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12412. org-outline-regexp)
  12413. " *\\(\\<\\("
  12414. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12415. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12416. (props (list 'face 'default
  12417. 'done-face 'org-agenda-done
  12418. 'undone-face 'default
  12419. 'mouse-face 'highlight
  12420. 'org-not-done-regexp org-not-done-regexp
  12421. 'org-todo-regexp org-todo-regexp
  12422. 'org-complex-heading-regexp org-complex-heading-regexp
  12423. 'help-echo
  12424. (format "mouse-2 or RET jump to org file %s"
  12425. (abbreviate-file-name
  12426. (or (buffer-file-name (buffer-base-buffer))
  12427. (buffer-name (buffer-base-buffer)))))))
  12428. (case-fold-search nil)
  12429. (org-map-continue-from nil)
  12430. lspos tags tags-list
  12431. (tags-alist (list (cons 0 org-file-tags)))
  12432. (llast 0) rtn rtn1 level category i txt
  12433. todo marker entry priority)
  12434. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12435. (setq action (list 'lambda nil action)))
  12436. (save-excursion
  12437. (goto-char (point-min))
  12438. (when (eq action 'sparse-tree)
  12439. (org-overview)
  12440. (org-remove-occur-highlights))
  12441. (while (re-search-forward re nil t)
  12442. (setq org-map-continue-from nil)
  12443. (catch :skip
  12444. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12445. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12446. (goto-char (setq lspos (match-beginning 0)))
  12447. (setq level (org-reduced-level (org-outline-level))
  12448. category (org-get-category))
  12449. (setq i llast llast level)
  12450. ;; remove tag lists from same and sublevels
  12451. (while (>= i level)
  12452. (when (setq entry (assoc i tags-alist))
  12453. (setq tags-alist (delete entry tags-alist)))
  12454. (setq i (1- i)))
  12455. ;; add the next tags
  12456. (when tags
  12457. (setq tags (org-split-string tags ":")
  12458. tags-alist
  12459. (cons (cons level tags) tags-alist)))
  12460. ;; compile tags for current headline
  12461. (setq tags-list
  12462. (if org-use-tag-inheritance
  12463. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12464. tags)
  12465. org-scanner-tags tags-list)
  12466. (when org-use-tag-inheritance
  12467. (setcdr (car tags-alist)
  12468. (mapcar (lambda (x)
  12469. (setq x (copy-sequence x))
  12470. (org-add-prop-inherited x))
  12471. (cdar tags-alist))))
  12472. (when (and tags org-use-tag-inheritance
  12473. (or (not (eq t org-use-tag-inheritance))
  12474. org-tags-exclude-from-inheritance))
  12475. ;; selective inheritance, remove uninherited ones
  12476. (setcdr (car tags-alist)
  12477. (org-remove-uninherited-tags (cdar tags-alist))))
  12478. (when (and
  12479. ;; eval matcher only when the todo condition is OK
  12480. (and (or (not todo-only) (member todo org-not-done-keywords))
  12481. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12482. (eval matcher)))
  12483. ;; Call the skipper, but return t if it does not skip,
  12484. ;; so that the `and' form continues evaluating
  12485. (progn
  12486. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12487. t)
  12488. ;; Check if timestamps are deselecting this entry
  12489. (or (not todo-only)
  12490. (and (member todo org-not-done-keywords)
  12491. (or (not org-agenda-tags-todo-honor-ignore-options)
  12492. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12493. ;; select this headline
  12494. (cond
  12495. ((eq action 'sparse-tree)
  12496. (and org-highlight-sparse-tree-matches
  12497. (org-get-heading) (match-end 0)
  12498. (org-highlight-new-match
  12499. (match-beginning 1) (match-end 1)))
  12500. (org-show-context 'tags-tree))
  12501. ((eq action 'agenda)
  12502. (setq txt (org-agenda-format-item
  12503. ""
  12504. (concat
  12505. (if (eq org-tags-match-list-sublevels 'indented)
  12506. (make-string (1- level) ?.) "")
  12507. (org-get-heading))
  12508. level category
  12509. tags-list)
  12510. priority (org-get-priority txt))
  12511. (goto-char lspos)
  12512. (setq marker (org-agenda-new-marker))
  12513. (org-add-props txt props
  12514. 'org-marker marker 'org-hd-marker marker 'org-category category
  12515. 'todo-state todo
  12516. 'priority priority 'type "tagsmatch")
  12517. (push txt rtn))
  12518. ((functionp action)
  12519. (setq org-map-continue-from nil)
  12520. (save-excursion
  12521. (setq rtn1 (funcall action))
  12522. (push rtn1 rtn)))
  12523. (t (error "Invalid action")))
  12524. ;; if we are to skip sublevels, jump to end of subtree
  12525. (unless org-tags-match-list-sublevels
  12526. (org-end-of-subtree t)
  12527. (backward-char 1))))
  12528. ;; Get the correct position from where to continue
  12529. (if org-map-continue-from
  12530. (goto-char org-map-continue-from)
  12531. (and (= (point) lspos) (end-of-line 1)))))
  12532. (when (and (eq action 'sparse-tree)
  12533. (not org-sparse-tree-open-archived-trees))
  12534. (org-hide-archived-subtrees (point-min) (point-max)))
  12535. (nreverse rtn)))
  12536. (defun org-remove-uninherited-tags (tags)
  12537. "Remove all tags that are not inherited from the list TAGS."
  12538. (cond
  12539. ((eq org-use-tag-inheritance t)
  12540. (if org-tags-exclude-from-inheritance
  12541. (org-delete-all org-tags-exclude-from-inheritance tags)
  12542. tags))
  12543. ((not org-use-tag-inheritance) nil)
  12544. ((stringp org-use-tag-inheritance)
  12545. (delq nil (mapcar
  12546. (lambda (x)
  12547. (if (and (string-match org-use-tag-inheritance x)
  12548. (not (member x org-tags-exclude-from-inheritance)))
  12549. x nil))
  12550. tags)))
  12551. ((listp org-use-tag-inheritance)
  12552. (delq nil (mapcar
  12553. (lambda (x)
  12554. (if (member x org-use-tag-inheritance) x nil))
  12555. tags)))))
  12556. (defun org-match-sparse-tree (&optional todo-only match)
  12557. "Create a sparse tree according to tags string MATCH.
  12558. MATCH can contain positive and negative selection of tags, like
  12559. \"+WORK+URGENT-WITHBOSS\".
  12560. If optional argument TODO-ONLY is non-nil, only select lines that are
  12561. also TODO lines."
  12562. (interactive "P")
  12563. (org-agenda-prepare-buffers (list (current-buffer)))
  12564. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12565. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12566. (defvar org-cached-props nil)
  12567. (defun org-cached-entry-get (pom property)
  12568. (if (or (eq t org-use-property-inheritance)
  12569. (and (stringp org-use-property-inheritance)
  12570. (string-match org-use-property-inheritance property))
  12571. (and (listp org-use-property-inheritance)
  12572. (member property org-use-property-inheritance)))
  12573. ;; Caching is not possible, check it directly
  12574. (org-entry-get pom property 'inherit)
  12575. ;; Get all properties, so that we can do complicated checks easily
  12576. (cdr (assoc property (or org-cached-props
  12577. (setq org-cached-props
  12578. (org-entry-properties pom)))))))
  12579. (defun org-global-tags-completion-table (&optional files)
  12580. "Return the list of all tags in all agenda buffer/files.
  12581. Optional FILES argument is a list of files which can be used
  12582. instead of the agenda files."
  12583. (save-excursion
  12584. (org-uniquify
  12585. (delq nil
  12586. (apply 'append
  12587. (mapcar
  12588. (lambda (file)
  12589. (set-buffer (find-file-noselect file))
  12590. (append (org-get-buffer-tags)
  12591. (mapcar (lambda (x) (if (stringp (car-safe x))
  12592. (list (car-safe x)) nil))
  12593. org-tag-alist)))
  12594. (if (and files (car files))
  12595. files
  12596. (org-agenda-files))))))))
  12597. (defun org-make-tags-matcher (match)
  12598. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12599. The variable `todo-only' is scoped dynamically into this function.
  12600. It will be set to t if the matcher restricts matching to TODO entries,
  12601. otherwise will not be touched.
  12602. Returns a cons of the selection string MATCH and the constructed
  12603. lisp form implementing the matcher. The matcher is to be evaluated
  12604. at an Org entry, with point on the headline, and returns t if the
  12605. entry matches the selection string MATCH. The returned lisp form
  12606. references two variables with information about the entry, which
  12607. must be bound around the form's evaluation: todo, the TODO keyword
  12608. at the entry (or nil of none); and tags-list, the list of all tags
  12609. at the entry including inherited ones. Additionally, the category
  12610. of the entry (if any) must be specified as the text property
  12611. 'org-category on the headline.
  12612. See also `org-scan-tags'.
  12613. "
  12614. (declare (special todo-only))
  12615. (unless (boundp 'todo-only)
  12616. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12617. (unless match
  12618. ;; Get a new match request, with completion
  12619. (let ((org-last-tags-completion-table
  12620. (org-global-tags-completion-table)))
  12621. (setq match (org-completing-read-no-i
  12622. "Match: " 'org-tags-completion-function nil nil nil
  12623. 'org-tags-history))))
  12624. ;; Parse the string and create a lisp form
  12625. (let ((match0 match)
  12626. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12627. minus tag mm
  12628. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12629. orterms term orlist re-p str-p level-p level-op time-p
  12630. prop-p pn pv po gv rest)
  12631. (if (string-match "/+" match)
  12632. ;; match contains also a todo-matching request
  12633. (progn
  12634. (setq tagsmatch (substring match 0 (match-beginning 0))
  12635. todomatch (substring match (match-end 0)))
  12636. (if (string-match "^!" todomatch)
  12637. (setq todo-only t todomatch (substring todomatch 1)))
  12638. (if (string-match "^\\s-*$" todomatch)
  12639. (setq todomatch nil)))
  12640. ;; only matching tags
  12641. (setq tagsmatch match todomatch nil))
  12642. ;; Make the tags matcher
  12643. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12644. (setq tagsmatcher t)
  12645. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12646. (while (setq term (pop orterms))
  12647. (while (and (equal (substring term -1) "\\") orterms)
  12648. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12649. (while (string-match re term)
  12650. (setq rest (substring term (match-end 0))
  12651. minus (and (match-end 1)
  12652. (equal (match-string 1 term) "-"))
  12653. tag (save-match-data (replace-regexp-in-string
  12654. "\\\\-" "-"
  12655. (match-string 2 term)))
  12656. re-p (equal (string-to-char tag) ?{)
  12657. level-p (match-end 4)
  12658. prop-p (match-end 5)
  12659. mm (cond
  12660. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12661. (level-p
  12662. (setq level-op (org-op-to-function (match-string 3 term)))
  12663. `(,level-op level ,(string-to-number
  12664. (match-string 4 term))))
  12665. (prop-p
  12666. (setq pn (match-string 5 term)
  12667. po (match-string 6 term)
  12668. pv (match-string 7 term)
  12669. re-p (equal (string-to-char pv) ?{)
  12670. str-p (equal (string-to-char pv) ?\")
  12671. time-p (save-match-data
  12672. (string-match "^\"[[<].*[]>]\"$" pv))
  12673. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12674. (if time-p (setq pv (org-matcher-time pv)))
  12675. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12676. (cond
  12677. ((equal pn "CATEGORY")
  12678. (setq gv '(get-text-property (point) 'org-category)))
  12679. ((equal pn "TODO")
  12680. (setq gv 'todo))
  12681. (t
  12682. (setq gv `(org-cached-entry-get nil ,pn))))
  12683. (if re-p
  12684. (if (eq po 'org<>)
  12685. `(not (string-match ,pv (or ,gv "")))
  12686. `(string-match ,pv (or ,gv "")))
  12687. (if str-p
  12688. `(,po (or ,gv "") ,pv)
  12689. `(,po (string-to-number (or ,gv ""))
  12690. ,(string-to-number pv) ))))
  12691. (t `(member ,tag tags-list)))
  12692. mm (if minus (list 'not mm) mm)
  12693. term rest)
  12694. (push mm tagsmatcher))
  12695. (push (if (> (length tagsmatcher) 1)
  12696. (cons 'and tagsmatcher)
  12697. (car tagsmatcher))
  12698. orlist)
  12699. (setq tagsmatcher nil))
  12700. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12701. (setq tagsmatcher
  12702. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12703. ;; Make the todo matcher
  12704. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12705. (setq todomatcher t)
  12706. (setq orterms (org-split-string todomatch "|") orlist nil)
  12707. (while (setq term (pop orterms))
  12708. (while (string-match re term)
  12709. (setq minus (and (match-end 1)
  12710. (equal (match-string 1 term) "-"))
  12711. kwd (match-string 2 term)
  12712. re-p (equal (string-to-char kwd) ?{)
  12713. term (substring term (match-end 0))
  12714. mm (if re-p
  12715. `(string-match ,(substring kwd 1 -1) todo)
  12716. (list 'equal 'todo kwd))
  12717. mm (if minus (list 'not mm) mm))
  12718. (push mm todomatcher))
  12719. (push (if (> (length todomatcher) 1)
  12720. (cons 'and todomatcher)
  12721. (car todomatcher))
  12722. orlist)
  12723. (setq todomatcher nil))
  12724. (setq todomatcher (if (> (length orlist) 1)
  12725. (cons 'or orlist) (car orlist))))
  12726. ;; Return the string and lisp forms of the matcher
  12727. (setq matcher (if todomatcher
  12728. (list 'and tagsmatcher todomatcher)
  12729. tagsmatcher))
  12730. (when todo-only
  12731. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12732. matcher)))
  12733. (cons match0 matcher)))
  12734. (defun org-op-to-function (op &optional stringp)
  12735. "Turn an operator into the appropriate function."
  12736. (setq op
  12737. (cond
  12738. ((equal op "<" ) '(< string< org-time<))
  12739. ((equal op ">" ) '(> org-string> org-time>))
  12740. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12741. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12742. ((member op '("=" "==")) '(= string= org-time=))
  12743. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12744. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12745. (defun org<> (a b) (not (= a b)))
  12746. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12747. (defun org-string>= (a b) (not (string< a b)))
  12748. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12749. (defun org-string<> (a b) (not (string= a b)))
  12750. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12751. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12752. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12753. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12754. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12755. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12756. (defun org-2ft (s)
  12757. "Convert S to a floating point time.
  12758. If S is already a number, just return it. If it is a string, parse
  12759. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12760. (cond
  12761. ((numberp s) s)
  12762. ((stringp s)
  12763. (condition-case nil
  12764. (float-time (apply 'encode-time (org-parse-time-string s)))
  12765. (error 0.)))
  12766. (t 0.)))
  12767. (defun org-time-today ()
  12768. "Time in seconds today at 0:00.
  12769. Returns the float number of seconds since the beginning of the
  12770. epoch to the beginning of today (00:00)."
  12771. (float-time (apply 'encode-time
  12772. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12773. (defun org-matcher-time (s)
  12774. "Interpret a time comparison value."
  12775. (save-match-data
  12776. (cond
  12777. ((string= s "<now>") (float-time))
  12778. ((string= s "<today>") (org-time-today))
  12779. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12780. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12781. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12782. (+ (org-time-today)
  12783. (* (string-to-number (match-string 1 s))
  12784. (cdr (assoc (match-string 2 s)
  12785. '(("d" . 86400.0) ("w" . 604800.0)
  12786. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12787. (t (org-2ft s)))))
  12788. (defun org-match-any-p (re list)
  12789. "Does re match any element of list?"
  12790. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12791. (delq nil list))
  12792. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12793. (defvar org-tags-overlay (make-overlay 1 1))
  12794. (org-detach-overlay org-tags-overlay)
  12795. (defun org-get-local-tags-at (&optional pos)
  12796. "Get a list of tags defined in the current headline."
  12797. (org-get-tags-at pos 'local))
  12798. (defun org-get-local-tags ()
  12799. "Get a list of tags defined in the current headline."
  12800. (org-get-tags-at nil 'local))
  12801. (defun org-get-tags-at (&optional pos local)
  12802. "Get a list of all headline tags applicable at POS.
  12803. POS defaults to point. If tags are inherited, the list contains
  12804. the targets in the same sequence as the headlines appear, i.e.
  12805. the tags of the current headline come last.
  12806. When LOCAL is non-nil, only return tags from the current headline,
  12807. ignore inherited ones."
  12808. (interactive)
  12809. (if (and org-trust-scanner-tags
  12810. (or (not pos) (equal pos (point)))
  12811. (not local))
  12812. org-scanner-tags
  12813. (let (tags ltags lastpos parent)
  12814. (save-excursion
  12815. (save-restriction
  12816. (widen)
  12817. (goto-char (or pos (point)))
  12818. (save-match-data
  12819. (catch 'done
  12820. (condition-case nil
  12821. (progn
  12822. (org-back-to-heading t)
  12823. (while (not (equal lastpos (point)))
  12824. (setq lastpos (point))
  12825. (when (looking-at
  12826. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12827. (setq ltags (org-split-string
  12828. (org-match-string-no-properties 1) ":"))
  12829. (when parent
  12830. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12831. (setq tags (append
  12832. (if parent
  12833. (org-remove-uninherited-tags ltags)
  12834. ltags)
  12835. tags)))
  12836. (or org-use-tag-inheritance (throw 'done t))
  12837. (if local (throw 'done t))
  12838. (or (org-up-heading-safe) (error nil))
  12839. (setq parent t)))
  12840. (error nil)))))
  12841. (if local
  12842. tags
  12843. (reverse (delete-dups
  12844. (reverse (append
  12845. (org-remove-uninherited-tags
  12846. org-file-tags) tags)))))))))
  12847. (defun org-add-prop-inherited (s)
  12848. (add-text-properties 0 (length s) '(inherited t) s)
  12849. s)
  12850. (defun org-toggle-tag (tag &optional onoff)
  12851. "Toggle the tag TAG for the current line.
  12852. If ONOFF is `on' or `off', don't toggle but set to this state."
  12853. (let (res current)
  12854. (save-excursion
  12855. (org-back-to-heading t)
  12856. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12857. (point-at-eol) t)
  12858. (progn
  12859. (setq current (match-string 1))
  12860. (replace-match ""))
  12861. (setq current ""))
  12862. (setq current (nreverse (org-split-string current ":")))
  12863. (cond
  12864. ((eq onoff 'on)
  12865. (setq res t)
  12866. (or (member tag current) (push tag current)))
  12867. ((eq onoff 'off)
  12868. (or (not (member tag current)) (setq current (delete tag current))))
  12869. (t (if (member tag current)
  12870. (setq current (delete tag current))
  12871. (setq res t)
  12872. (push tag current))))
  12873. (end-of-line 1)
  12874. (if current
  12875. (progn
  12876. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12877. (org-set-tags nil t))
  12878. (delete-horizontal-space))
  12879. (run-hooks 'org-after-tags-change-hook))
  12880. res))
  12881. (defun org-align-tags-here (to-col)
  12882. ;; Assumes that this is a headline
  12883. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12884. (beginning-of-line 1)
  12885. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12886. (< pos (match-beginning 2)))
  12887. (progn
  12888. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12889. (goto-char (match-beginning 1))
  12890. (insert " ")
  12891. (delete-region (point) (1+ (match-beginning 2)))
  12892. (setq ncol (max (current-column)
  12893. (1+ col)
  12894. (if (> to-col 0)
  12895. to-col
  12896. (- (abs to-col) tags-l))))
  12897. (setq p (point))
  12898. (insert (make-string (- ncol (current-column)) ?\ ))
  12899. (setq ncol (current-column))
  12900. (when indent-tabs-mode (tabify p (point-at-eol)))
  12901. (org-move-to-column (min ncol col) t))
  12902. (goto-char pos))))
  12903. (defun org-set-tags-command (&optional arg just-align)
  12904. "Call the set-tags command for the current entry."
  12905. (interactive "P")
  12906. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12907. (org-set-tags arg just-align)
  12908. (save-excursion
  12909. (unless (and (org-region-active-p)
  12910. org-loop-over-headlines-in-active-region)
  12911. (org-back-to-heading t))
  12912. (org-set-tags arg just-align))))
  12913. (defun org-set-tags-to (data)
  12914. "Set the tags of the current entry to DATA, replacing the current tags.
  12915. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12916. If DATA is nil or the empty string, any tags will be removed."
  12917. (interactive "sTags: ")
  12918. (setq data
  12919. (cond
  12920. ((eq data nil) "")
  12921. ((equal data "") "")
  12922. ((stringp data)
  12923. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12924. ":"))
  12925. ((listp data)
  12926. (concat ":" (mapconcat 'identity data ":") ":"))))
  12927. (when data
  12928. (save-excursion
  12929. (org-back-to-heading t)
  12930. (when (looking-at org-complex-heading-regexp)
  12931. (if (match-end 5)
  12932. (progn
  12933. (goto-char (match-beginning 5))
  12934. (insert data)
  12935. (delete-region (point) (point-at-eol))
  12936. (org-set-tags nil 'align))
  12937. (goto-char (point-at-eol))
  12938. (insert " " data)
  12939. (org-set-tags nil 'align)))
  12940. (beginning-of-line 1)
  12941. (if (looking-at ".*?\\([ \t]+\\)$")
  12942. (delete-region (match-beginning 1) (match-end 1))))))
  12943. (defun org-align-all-tags ()
  12944. "Align the tags i all headings."
  12945. (interactive)
  12946. (save-excursion
  12947. (or (ignore-errors (org-back-to-heading t))
  12948. (outline-next-heading))
  12949. (if (org-at-heading-p)
  12950. (org-set-tags t)
  12951. (message "No headings"))))
  12952. (defvar org-indent-indentation-per-level)
  12953. (defun org-set-tags (&optional arg just-align)
  12954. "Set the tags for the current headline.
  12955. With prefix ARG, realign all tags in headings in the current buffer."
  12956. (interactive "P")
  12957. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12958. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12959. 'region-start-level 'region))
  12960. org-loop-over-headlines-in-active-region)
  12961. (org-map-entries
  12962. ;; We don't use ARG and JUST-ALIGN here these args are not
  12963. ;; useful when looping over headlines
  12964. `(org-set-tags)
  12965. org-loop-over-headlines-in-active-region
  12966. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12967. (let* ((re org-outline-regexp-bol)
  12968. (current (unless arg (org-get-tags-string)))
  12969. (col (current-column))
  12970. (org-setting-tags t)
  12971. table current-tags inherited-tags ; computed below when needed
  12972. tags p0 c0 c1 rpl di tc level)
  12973. (if arg
  12974. (save-excursion
  12975. (goto-char (point-min))
  12976. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12977. (while (re-search-forward re nil t)
  12978. (org-set-tags nil t)
  12979. (end-of-line 1)))
  12980. (message "All tags realigned to column %d" org-tags-column))
  12981. (if just-align
  12982. (setq tags current)
  12983. ;; Get a new set of tags from the user
  12984. (save-excursion
  12985. (setq table (append org-tag-persistent-alist
  12986. (or org-tag-alist (org-get-buffer-tags))
  12987. (and
  12988. org-complete-tags-always-offer-all-agenda-tags
  12989. (org-global-tags-completion-table
  12990. (org-agenda-files))))
  12991. org-last-tags-completion-table table
  12992. current-tags (org-split-string current ":")
  12993. inherited-tags (nreverse
  12994. (nthcdr (length current-tags)
  12995. (nreverse (org-get-tags-at))))
  12996. tags
  12997. (if (or (eq t org-use-fast-tag-selection)
  12998. (and org-use-fast-tag-selection
  12999. (delq nil (mapcar 'cdr table))))
  13000. (org-fast-tag-selection
  13001. current-tags inherited-tags table
  13002. (if org-fast-tag-selection-include-todo
  13003. org-todo-key-alist))
  13004. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13005. (org-trim
  13006. (org-icompleting-read "Tags: "
  13007. 'org-tags-completion-function
  13008. nil nil current 'org-tags-history))))))
  13009. (while (string-match "[-+&]+" tags)
  13010. ;; No boolean logic, just a list
  13011. (setq tags (replace-match ":" t t tags))))
  13012. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13013. (if org-tags-sort-function
  13014. (setq tags (mapconcat 'identity
  13015. (sort (org-split-string
  13016. tags (org-re "[^[:alnum:]_@#%]+"))
  13017. org-tags-sort-function) ":")))
  13018. (if (string-match "\\`[\t ]*\\'" tags)
  13019. (setq tags "")
  13020. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13021. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13022. ;; Insert new tags at the correct column
  13023. (beginning-of-line 1)
  13024. (setq level (or (and (looking-at org-outline-regexp)
  13025. (- (match-end 0) (point) 1))
  13026. 1))
  13027. (cond
  13028. ((and (equal current "") (equal tags "")))
  13029. ((re-search-forward
  13030. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13031. (point-at-eol) t)
  13032. (if (equal tags "")
  13033. (setq rpl "")
  13034. (goto-char (match-beginning 0))
  13035. (setq c0 (current-column)
  13036. ;; compute offset for the case of org-indent-mode active
  13037. di (if org-indent-mode
  13038. (* (1- org-indent-indentation-per-level) (1- level))
  13039. 0)
  13040. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13041. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13042. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13043. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13044. (replace-match rpl t t)
  13045. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13046. tags)
  13047. (t (error "Tags alignment failed")))
  13048. (org-move-to-column col)
  13049. (unless just-align
  13050. (run-hooks 'org-after-tags-change-hook))))))
  13051. (defun org-change-tag-in-region (beg end tag off)
  13052. "Add or remove TAG for each entry in the region.
  13053. This works in the agenda, and also in an org-mode buffer."
  13054. (interactive
  13055. (list (region-beginning) (region-end)
  13056. (let ((org-last-tags-completion-table
  13057. (if (derived-mode-p 'org-mode)
  13058. (org-get-buffer-tags)
  13059. (org-global-tags-completion-table))))
  13060. (org-icompleting-read
  13061. "Tag: " 'org-tags-completion-function nil nil nil
  13062. 'org-tags-history))
  13063. (progn
  13064. (message "[s]et or [r]emove? ")
  13065. (equal (read-char-exclusive) ?r))))
  13066. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13067. (let ((agendap (equal major-mode 'org-agenda-mode))
  13068. l1 l2 m buf pos newhead (cnt 0))
  13069. (goto-char end)
  13070. (setq l2 (1- (org-current-line)))
  13071. (goto-char beg)
  13072. (setq l1 (org-current-line))
  13073. (loop for l from l1 to l2 do
  13074. (org-goto-line l)
  13075. (setq m (get-text-property (point) 'org-hd-marker))
  13076. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13077. (and agendap m))
  13078. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13079. pos (if agendap m (point)))
  13080. (with-current-buffer buf
  13081. (save-excursion
  13082. (save-restriction
  13083. (goto-char pos)
  13084. (setq cnt (1+ cnt))
  13085. (org-toggle-tag tag (if off 'off 'on))
  13086. (setq newhead (org-get-heading)))))
  13087. (and agendap (org-agenda-change-all-lines newhead m))))
  13088. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13089. (defun org-tags-completion-function (string predicate &optional flag)
  13090. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13091. (confirm (lambda (x) (stringp (car x)))))
  13092. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13093. (setq s1 (match-string 1 string)
  13094. s2 (match-string 2 string))
  13095. (setq s1 "" s2 string))
  13096. (cond
  13097. ((eq flag nil)
  13098. ;; try completion
  13099. (setq rtn (try-completion s2 ctable confirm))
  13100. (if (stringp rtn)
  13101. (setq rtn
  13102. (concat s1 s2 (substring rtn (length s2))
  13103. (if (and org-add-colon-after-tag-completion
  13104. (assoc rtn ctable))
  13105. ":" ""))))
  13106. rtn)
  13107. ((eq flag t)
  13108. ;; all-completions
  13109. (all-completions s2 ctable confirm)
  13110. )
  13111. ((eq flag 'lambda)
  13112. ;; exact match?
  13113. (assoc s2 ctable)))
  13114. ))
  13115. (defun org-fast-tag-insert (kwd tags face &optional end)
  13116. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  13117. (insert (format "%-12s" (concat kwd ":"))
  13118. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13119. (or end "")))
  13120. (defun org-fast-tag-show-exit (flag)
  13121. (save-excursion
  13122. (org-goto-line 3)
  13123. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13124. (replace-match ""))
  13125. (when flag
  13126. (end-of-line 1)
  13127. (org-move-to-column (- (window-width) 19) t)
  13128. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13129. (defun org-set-current-tags-overlay (current prefix)
  13130. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13131. (if (featurep 'xemacs)
  13132. (org-overlay-display org-tags-overlay (concat prefix s)
  13133. 'secondary-selection)
  13134. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13135. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13136. (defvar org-last-tag-selection-key nil)
  13137. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13138. "Fast tag selection with single keys.
  13139. CURRENT is the current list of tags in the headline, INHERITED is the
  13140. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13141. possibly with grouping information. TODO-TABLE is a similar table with
  13142. TODO keywords, should these have keys assigned to them.
  13143. If the keys are nil, a-z are automatically assigned.
  13144. Returns the new tags string, or nil to not change the current settings."
  13145. (let* ((fulltable (append table todo-table))
  13146. (maxlen (apply 'max (mapcar
  13147. (lambda (x)
  13148. (if (stringp (car x)) (string-width (car x)) 0))
  13149. fulltable)))
  13150. (buf (current-buffer))
  13151. (expert (eq org-fast-tag-selection-single-key 'expert))
  13152. (buffer-tags nil)
  13153. (fwidth (+ maxlen 3 1 3))
  13154. (ncol (/ (- (window-width) 4) fwidth))
  13155. (i-face 'org-done)
  13156. (c-face 'org-todo)
  13157. tg cnt e c char c1 c2 ntable tbl rtn
  13158. ov-start ov-end ov-prefix
  13159. (exit-after-next org-fast-tag-selection-single-key)
  13160. (done-keywords org-done-keywords)
  13161. groups ingroup)
  13162. (save-excursion
  13163. (beginning-of-line 1)
  13164. (if (looking-at
  13165. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13166. (setq ov-start (match-beginning 1)
  13167. ov-end (match-end 1)
  13168. ov-prefix "")
  13169. (setq ov-start (1- (point-at-eol))
  13170. ov-end (1+ ov-start))
  13171. (skip-chars-forward "^\n\r")
  13172. (setq ov-prefix
  13173. (concat
  13174. (buffer-substring (1- (point)) (point))
  13175. (if (> (current-column) org-tags-column)
  13176. " "
  13177. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13178. (move-overlay org-tags-overlay ov-start ov-end)
  13179. (save-window-excursion
  13180. (if expert
  13181. (set-buffer (get-buffer-create " *Org tags*"))
  13182. (delete-other-windows)
  13183. (split-window-vertically)
  13184. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13185. (erase-buffer)
  13186. (org-set-local 'org-done-keywords done-keywords)
  13187. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13188. (org-fast-tag-insert "Current" current c-face "\n\n")
  13189. (org-fast-tag-show-exit exit-after-next)
  13190. (org-set-current-tags-overlay current ov-prefix)
  13191. (setq tbl fulltable char ?a cnt 0)
  13192. (while (setq e (pop tbl))
  13193. (cond
  13194. ((equal (car e) :startgroup)
  13195. (push '() groups) (setq ingroup t)
  13196. (when (not (= cnt 0))
  13197. (setq cnt 0)
  13198. (insert "\n"))
  13199. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13200. ((equal (car e) :endgroup)
  13201. (setq ingroup nil cnt 0)
  13202. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13203. ((equal e '(:newline))
  13204. (when (not (= cnt 0))
  13205. (setq cnt 0)
  13206. (insert "\n")
  13207. (setq e (car tbl))
  13208. (while (equal (car tbl) '(:newline))
  13209. (insert "\n")
  13210. (setq tbl (cdr tbl)))))
  13211. (t
  13212. (setq tg (copy-sequence (car e)) c2 nil)
  13213. (if (cdr e)
  13214. (setq c (cdr e))
  13215. ;; automatically assign a character.
  13216. (setq c1 (string-to-char
  13217. (downcase (substring
  13218. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13219. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13220. (while (or (rassoc char ntable) (rassoc char table))
  13221. (setq char (1+ char)))
  13222. (setq c2 c1))
  13223. (setq c (or c2 char)))
  13224. (if ingroup (push tg (car groups)))
  13225. (setq tg (org-add-props tg nil 'face
  13226. (cond
  13227. ((not (assoc tg table))
  13228. (org-get-todo-face tg))
  13229. ((member tg current) c-face)
  13230. ((member tg inherited) i-face))))
  13231. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13232. (insert "[" c "] " tg (make-string
  13233. (- fwidth 4 (length tg)) ?\ ))
  13234. (push (cons tg c) ntable)
  13235. (when (= (setq cnt (1+ cnt)) ncol)
  13236. (insert "\n")
  13237. (if ingroup (insert " "))
  13238. (setq cnt 0)))))
  13239. (setq ntable (nreverse ntable))
  13240. (insert "\n")
  13241. (goto-char (point-min))
  13242. (if (not expert) (org-fit-window-to-buffer))
  13243. (setq rtn
  13244. (catch 'exit
  13245. (while t
  13246. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13247. (if (not groups) "no " "")
  13248. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13249. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13250. (setq org-last-tag-selection-key c)
  13251. (cond
  13252. ((= c ?\r) (throw 'exit t))
  13253. ((= c ?!)
  13254. (setq groups (not groups))
  13255. (goto-char (point-min))
  13256. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13257. ((= c ?\C-c)
  13258. (if (not expert)
  13259. (org-fast-tag-show-exit
  13260. (setq exit-after-next (not exit-after-next)))
  13261. (setq expert nil)
  13262. (delete-other-windows)
  13263. (set-window-buffer (split-window-vertically) " *Org tags*")
  13264. (org-switch-to-buffer-other-window " *Org tags*")
  13265. (org-fit-window-to-buffer)))
  13266. ((or (= c ?\C-g)
  13267. (and (= c ?q) (not (rassoc c ntable))))
  13268. (org-detach-overlay org-tags-overlay)
  13269. (setq quit-flag t))
  13270. ((= c ?\ )
  13271. (setq current nil)
  13272. (if exit-after-next (setq exit-after-next 'now)))
  13273. ((= c ?\t)
  13274. (condition-case nil
  13275. (setq tg (org-icompleting-read
  13276. "Tag: "
  13277. (or buffer-tags
  13278. (with-current-buffer buf
  13279. (org-get-buffer-tags)))))
  13280. (quit (setq tg "")))
  13281. (when (string-match "\\S-" tg)
  13282. (add-to-list 'buffer-tags (list tg))
  13283. (if (member tg current)
  13284. (setq current (delete tg current))
  13285. (push tg current)))
  13286. (if exit-after-next (setq exit-after-next 'now)))
  13287. ((setq e (rassoc c todo-table) tg (car e))
  13288. (with-current-buffer buf
  13289. (save-excursion (org-todo tg)))
  13290. (if exit-after-next (setq exit-after-next 'now)))
  13291. ((setq e (rassoc c ntable) tg (car e))
  13292. (if (member tg current)
  13293. (setq current (delete tg current))
  13294. (loop for g in groups do
  13295. (if (member tg g)
  13296. (mapc (lambda (x)
  13297. (setq current (delete x current)))
  13298. g)))
  13299. (push tg current))
  13300. (if exit-after-next (setq exit-after-next 'now))))
  13301. ;; Create a sorted list
  13302. (setq current
  13303. (sort current
  13304. (lambda (a b)
  13305. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13306. (if (eq exit-after-next 'now) (throw 'exit t))
  13307. (goto-char (point-min))
  13308. (beginning-of-line 2)
  13309. (delete-region (point) (point-at-eol))
  13310. (org-fast-tag-insert "Current" current c-face)
  13311. (org-set-current-tags-overlay current ov-prefix)
  13312. (while (re-search-forward
  13313. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13314. (setq tg (match-string 1))
  13315. (add-text-properties
  13316. (match-beginning 1) (match-end 1)
  13317. (list 'face
  13318. (cond
  13319. ((member tg current) c-face)
  13320. ((member tg inherited) i-face)
  13321. (t (get-text-property (match-beginning 1) 'face))))))
  13322. (goto-char (point-min)))))
  13323. (org-detach-overlay org-tags-overlay)
  13324. (if rtn
  13325. (mapconcat 'identity current ":")
  13326. nil))))
  13327. (defun org-get-tags-string ()
  13328. "Get the TAGS string in the current headline."
  13329. (unless (org-at-heading-p t)
  13330. (error "Not on a heading"))
  13331. (save-excursion
  13332. (beginning-of-line 1)
  13333. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13334. (org-match-string-no-properties 1)
  13335. "")))
  13336. (defun org-get-tags ()
  13337. "Get the list of tags specified in the current headline."
  13338. (org-split-string (org-get-tags-string) ":"))
  13339. (defun org-get-buffer-tags ()
  13340. "Get a table of all tags used in the buffer, for completion."
  13341. (let (tags)
  13342. (save-excursion
  13343. (goto-char (point-min))
  13344. (while (re-search-forward
  13345. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13346. (when (equal (char-after (point-at-bol 0)) ?*)
  13347. (mapc (lambda (x) (add-to-list 'tags x))
  13348. (org-split-string (org-match-string-no-properties 1) ":")))))
  13349. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13350. (mapcar 'list tags)))
  13351. ;;;; The mapping API
  13352. (defun org-map-entries (func &optional match scope &rest skip)
  13353. "Call FUNC at each headline selected by MATCH in SCOPE.
  13354. FUNC is a function or a lisp form. The function will be called without
  13355. arguments, with the cursor positioned at the beginning of the headline.
  13356. The return values of all calls to the function will be collected and
  13357. returned as a list.
  13358. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13359. does not need to preserve point. After evaluation, the cursor will be
  13360. moved to the end of the line (presumably of the headline of the
  13361. processed entry) and search continues from there. Under some
  13362. circumstances, this may not produce the wanted results. For example,
  13363. if you have removed (e.g. archived) the current (sub)tree it could
  13364. mean that the next entry will be skipped entirely. In such cases, you
  13365. can specify the position from where search should continue by making
  13366. FUNC set the variable `org-map-continue-from' to the desired buffer
  13367. position.
  13368. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13369. Only headlines that are matched by this query will be considered during
  13370. the iteration. When MATCH is nil or t, all headlines will be
  13371. visited by the iteration.
  13372. SCOPE determines the scope of this command. It can be any of:
  13373. nil The current buffer, respecting the restriction if any
  13374. tree The subtree started with the entry at point
  13375. region The entries within the active region, if any
  13376. region-start-level
  13377. The entries within the active region, but only those at
  13378. the same level than the first one.
  13379. file The current buffer, without restriction
  13380. file-with-archives
  13381. The current buffer, and any archives associated with it
  13382. agenda All agenda files
  13383. agenda-with-archives
  13384. All agenda files with any archive files associated with them
  13385. \(file1 file2 ...)
  13386. If this is a list, all files in the list will be scanned
  13387. The remaining args are treated as settings for the skipping facilities of
  13388. the scanner. The following items can be given here:
  13389. archive skip trees with the archive tag
  13390. comment skip trees with the COMMENT keyword
  13391. function or Emacs Lisp form:
  13392. will be used as value for `org-agenda-skip-function', so
  13393. whenever the function returns a position, FUNC will not be
  13394. called for that entry and search will continue from the
  13395. position returned
  13396. If your function needs to retrieve the tags including inherited tags
  13397. at the *current* entry, you can use the value of the variable
  13398. `org-scanner-tags' which will be much faster than getting the value
  13399. with `org-get-tags-at'. If your function gets properties with
  13400. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13401. to t around the call to `org-entry-properties' to get the same speedup.
  13402. Note that if your function moves around to retrieve tags and properties at
  13403. a *different* entry, you cannot use these techniques."
  13404. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13405. (not (org-region-active-p)))
  13406. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13407. (org-agenda-skip-archived-trees (memq 'archive skip))
  13408. (org-agenda-skip-comment-trees (memq 'comment skip))
  13409. (org-agenda-skip-function
  13410. (car (org-delete-all '(comment archive) skip)))
  13411. (org-tags-match-list-sublevels t)
  13412. (start-level (eq scope 'region-start-level))
  13413. matcher file res
  13414. org-todo-keywords-for-agenda
  13415. org-done-keywords-for-agenda
  13416. org-todo-keyword-alist-for-agenda
  13417. org-drawers-for-agenda
  13418. org-tag-alist-for-agenda
  13419. todo-only)
  13420. (cond
  13421. ((eq match t) (setq matcher t))
  13422. ((eq match nil) (setq matcher t))
  13423. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13424. (save-window-excursion
  13425. (save-restriction
  13426. (cond ((eq scope 'tree)
  13427. (org-back-to-heading t)
  13428. (org-narrow-to-subtree)
  13429. (setq scope nil))
  13430. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13431. (org-region-active-p))
  13432. ;; If needed, set start-level to a string like "2"
  13433. (when start-level
  13434. (save-excursion
  13435. (goto-char (region-beginning))
  13436. (unless (org-at-heading-p) (outline-next-heading))
  13437. (setq start-level (org-current-level))))
  13438. (narrow-to-region (region-beginning)
  13439. (save-excursion
  13440. (goto-char (region-end))
  13441. (unless (and (bolp) (org-at-heading-p))
  13442. (outline-next-heading))
  13443. (point)))
  13444. (setq scope nil)))
  13445. (if (not scope)
  13446. (progn
  13447. (org-agenda-prepare-buffers
  13448. (list (buffer-file-name (current-buffer))))
  13449. (setq res (org-scan-tags func matcher todo-only start-level)))
  13450. ;; Get the right scope
  13451. (cond
  13452. ((and scope (listp scope) (symbolp (car scope)))
  13453. (setq scope (eval scope)))
  13454. ((eq scope 'agenda)
  13455. (setq scope (org-agenda-files t)))
  13456. ((eq scope 'agenda-with-archives)
  13457. (setq scope (org-agenda-files t))
  13458. (setq scope (org-add-archive-files scope)))
  13459. ((eq scope 'file)
  13460. (setq scope (list (buffer-file-name))))
  13461. ((eq scope 'file-with-archives)
  13462. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13463. (org-agenda-prepare-buffers scope)
  13464. (while (setq file (pop scope))
  13465. (with-current-buffer (org-find-base-buffer-visiting file)
  13466. (save-excursion
  13467. (save-restriction
  13468. (widen)
  13469. (goto-char (point-min))
  13470. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13471. res)))
  13472. ;;;; Properties
  13473. ;;; Setting and retrieving properties
  13474. (defconst org-special-properties
  13475. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13476. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13477. "The special properties valid in Org-mode.
  13478. These are properties that are not defined in the property drawer,
  13479. but in some other way.")
  13480. (defconst org-default-properties
  13481. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13482. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13483. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13484. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13485. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13486. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13487. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13488. "Some properties that are used by Org-mode for various purposes.
  13489. Being in this list makes sure that they are offered for completion.")
  13490. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13491. "Regular expression matching the first line of a property drawer.")
  13492. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13493. "Regular expression matching the last line of a property drawer.")
  13494. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13495. "Regular expression matching the first line of a property drawer.")
  13496. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13497. "Regular expression matching the first line of a property drawer.")
  13498. (defconst org-property-drawer-re
  13499. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13500. org-property-end-re "\\)\n?")
  13501. "Matches an entire property drawer.")
  13502. (defconst org-clock-drawer-re
  13503. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13504. org-property-end-re "\\)\n?")
  13505. "Matches an entire clock drawer.")
  13506. (defsubst org-re-property (property)
  13507. "Return a regexp matching a PROPERTY line.
  13508. Match group 1 will be set to the value."
  13509. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13510. (defsubst org-re-property-keyword (property)
  13511. "Return a regexp matching a PROPERTY line, possibly with no
  13512. value for the property."
  13513. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13514. (defun org-property-action ()
  13515. "Do an action on properties."
  13516. (interactive)
  13517. (let (c)
  13518. (org-at-property-p)
  13519. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13520. (setq c (read-char-exclusive))
  13521. (cond
  13522. ((equal c ?s)
  13523. (call-interactively 'org-set-property))
  13524. ((equal c ?d)
  13525. (call-interactively 'org-delete-property))
  13526. ((equal c ?D)
  13527. (call-interactively 'org-delete-property-globally))
  13528. ((equal c ?c)
  13529. (call-interactively 'org-compute-property-at-point))
  13530. (t (error "No such property action %c" c)))))
  13531. (defun org-inc-effort ()
  13532. "Increment the value of the effort property in the current entry."
  13533. (interactive)
  13534. (org-set-effort nil t))
  13535. (defvar org-clock-effort) ;; Defined in org-clock.el
  13536. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13537. (defun org-set-effort (&optional value increment)
  13538. "Set the effort property of the current entry.
  13539. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13540. 10th allowed value.
  13541. When INCREMENT is non-nil, set the property to the next allowed value."
  13542. (interactive "P")
  13543. (if (equal value 0) (setq value 10))
  13544. (let* ((completion-ignore-case t)
  13545. (prop org-effort-property)
  13546. (cur (org-entry-get nil prop))
  13547. (allowed (org-property-get-allowed-values nil prop 'table))
  13548. (existing (mapcar 'list (org-property-values prop)))
  13549. (heading (nth 4 (org-heading-components)))
  13550. rpl
  13551. (val (cond
  13552. ((stringp value) value)
  13553. ((and allowed (integerp value))
  13554. (or (car (nth (1- value) allowed))
  13555. (car (org-last allowed))))
  13556. ((and allowed increment)
  13557. (or (caadr (member (list cur) allowed))
  13558. (error "Allowed effort values are not set")))
  13559. (allowed
  13560. (message "Select 1-9,0, [RET%s]: %s"
  13561. (if cur (concat "=" cur) "")
  13562. (mapconcat 'car allowed " "))
  13563. (setq rpl (read-char-exclusive))
  13564. (if (equal rpl ?\r)
  13565. cur
  13566. (setq rpl (- rpl ?0))
  13567. (if (equal rpl 0) (setq rpl 10))
  13568. (if (and (> rpl 0) (<= rpl (length allowed)))
  13569. (car (nth (1- rpl) allowed))
  13570. (org-completing-read "Effort: " allowed nil))))
  13571. (t
  13572. (let (org-completion-use-ido org-completion-use-iswitchb)
  13573. (org-completing-read
  13574. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13575. (concat "[" cur "]") "")
  13576. ": ")
  13577. existing nil nil "" nil cur))))))
  13578. (unless (equal (org-entry-get nil prop) val)
  13579. (org-entry-put nil prop val))
  13580. (save-excursion
  13581. (org-back-to-heading t)
  13582. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13583. (when (string= heading org-clock-current-task)
  13584. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13585. (org-clock-update-mode-line))
  13586. (message "%s is now %s" prop val)))
  13587. (defun org-at-property-p ()
  13588. "Is cursor inside a property drawer?"
  13589. (save-excursion
  13590. (beginning-of-line 1)
  13591. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13592. (save-match-data ;; Used by calling procedures
  13593. (let ((p (point))
  13594. (range (unless (org-before-first-heading-p)
  13595. (org-get-property-block))))
  13596. (and range (<= (car range) p) (< p (cdr range))))))))
  13597. (defun org-get-property-block (&optional beg end force)
  13598. "Return the (beg . end) range of the body of the property drawer.
  13599. BEG and END are the beginning and end of the current subtree, or of
  13600. the part before the first headline. If they are not given, they will
  13601. be found. If the drawer does not exist and FORCE is non-nil, create
  13602. the drawer."
  13603. (catch 'exit
  13604. (save-excursion
  13605. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13606. (progn (org-back-to-heading t) (point))))
  13607. (end (or end (and (not (outline-next-heading)) (point-max))
  13608. (point))))
  13609. (goto-char beg)
  13610. (if (re-search-forward org-property-start-re end t)
  13611. (setq beg (1+ (match-end 0)))
  13612. (if force
  13613. (save-excursion
  13614. (org-insert-property-drawer)
  13615. (setq end (progn (outline-next-heading) (point))))
  13616. (throw 'exit nil))
  13617. (goto-char beg)
  13618. (if (re-search-forward org-property-start-re end t)
  13619. (setq beg (1+ (match-end 0)))))
  13620. (if (re-search-forward org-property-end-re end t)
  13621. (setq end (match-beginning 0))
  13622. (or force (throw 'exit nil))
  13623. (goto-char beg)
  13624. (setq end beg)
  13625. (org-indent-line)
  13626. (insert ":END:\n"))
  13627. (cons beg end)))))
  13628. (defun org-entry-properties (&optional pom which specific)
  13629. "Get all properties of the entry at point-or-marker POM.
  13630. This includes the TODO keyword, the tags, time strings for deadline,
  13631. scheduled, and clocking, and any additional properties defined in the
  13632. entry. The return value is an alist, keys may occur multiple times
  13633. if the property key was used several times.
  13634. POM may also be nil, in which case the current entry is used.
  13635. If WHICH is nil or `all', get all properties. If WHICH is
  13636. `special' or `standard', only get that subclass. If WHICH
  13637. is a string only get exactly this property. SPECIFIC can be a string, the
  13638. specific property we are interested in. Specifying it can speed
  13639. things up because then unnecessary parsing is avoided."
  13640. (setq which (or which 'all))
  13641. (org-with-point-at pom
  13642. (let ((clockstr (substring org-clock-string 0 -1))
  13643. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13644. (case-fold-search nil)
  13645. beg end range props sum-props key key1 value string clocksum clocksumt)
  13646. (save-excursion
  13647. (when (condition-case nil
  13648. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13649. (error nil))
  13650. (setq beg (point))
  13651. (setq sum-props (get-text-property (point) 'org-summaries))
  13652. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13653. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13654. (outline-next-heading)
  13655. (setq end (point))
  13656. (when (memq which '(all special))
  13657. ;; Get the special properties, like TODO and tags
  13658. (goto-char beg)
  13659. (when (and (or (not specific) (string= specific "TODO"))
  13660. (looking-at org-todo-line-regexp) (match-end 2))
  13661. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13662. (when (and (or (not specific) (string= specific "PRIORITY"))
  13663. (looking-at org-priority-regexp))
  13664. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13665. (when (or (not specific) (string= specific "FILE"))
  13666. (push (cons "FILE" buffer-file-name) props))
  13667. (when (and (or (not specific) (string= specific "TAGS"))
  13668. (setq value (org-get-tags-string))
  13669. (string-match "\\S-" value))
  13670. (push (cons "TAGS" value) props))
  13671. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13672. (setq value (org-get-tags-at)))
  13673. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13674. ":"))
  13675. props))
  13676. (when (or (not specific) (string= specific "BLOCKED"))
  13677. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13678. (when (or (not specific)
  13679. (member specific
  13680. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13681. "TIMESTAMP" "TIMESTAMP_IA")))
  13682. (catch 'match
  13683. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13684. (setq key (if (match-end 1)
  13685. (substring (org-match-string-no-properties 1)
  13686. 0 -1))
  13687. string (if (equal key clockstr)
  13688. (org-trim
  13689. (buffer-substring-no-properties
  13690. (match-beginning 3) (goto-char
  13691. (point-at-eol))))
  13692. (substring (org-match-string-no-properties 3)
  13693. 1 -1)))
  13694. ;; Get the correct property name from the key. This is
  13695. ;; necessary if the user has configured time keywords.
  13696. (setq key1 (concat key ":"))
  13697. (cond
  13698. ((not key)
  13699. (setq key
  13700. (if (= (char-after (match-beginning 3)) ?\[)
  13701. "TIMESTAMP_IA" "TIMESTAMP")))
  13702. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13703. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13704. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13705. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13706. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13707. (progn
  13708. (push (cons key string) props)
  13709. ;; no need to search further if match is found
  13710. (throw 'match t))
  13711. (when (or (equal key "CLOCK") (not (assoc key props)))
  13712. (push (cons key string) props)))))))
  13713. (when (memq which '(all standard))
  13714. ;; Get the standard properties, like :PROP: ...
  13715. (setq range (org-get-property-block beg end))
  13716. (when range
  13717. (goto-char (car range))
  13718. (while (re-search-forward
  13719. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13720. (cdr range) t)
  13721. (setq key (org-match-string-no-properties 1)
  13722. value (org-trim (or (org-match-string-no-properties 2) "")))
  13723. (unless (member key excluded)
  13724. (push (cons key (or value "")) props)))))
  13725. (if clocksum
  13726. (push (cons "CLOCKSUM"
  13727. (org-columns-number-to-string (/ (float clocksum) 60.)
  13728. 'add_times))
  13729. props))
  13730. (if clocksumt
  13731. (push (cons "CLOCKSUM_T"
  13732. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13733. 'add_times))
  13734. props))
  13735. (unless (assoc "CATEGORY" props)
  13736. (push (cons "CATEGORY" (org-get-category)) props))
  13737. (append sum-props (nreverse props)))))))
  13738. (defun org-entry-get (pom property &optional inherit literal-nil)
  13739. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13740. If INHERIT is non-nil and the entry does not have the property,
  13741. then also check higher levels of the hierarchy.
  13742. If INHERIT is the symbol `selective', use inheritance only if the setting
  13743. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13744. If the property is present but empty, the return value is the empty string.
  13745. If the property is not present at all, nil is returned.
  13746. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13747. do not interpret it as the list atom nil. This is used for inheritance
  13748. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13749. (org-with-point-at pom
  13750. (if (and inherit (if (eq inherit 'selective)
  13751. (org-property-inherit-p property)
  13752. t))
  13753. (org-entry-get-with-inheritance property literal-nil)
  13754. (if (member property org-special-properties)
  13755. ;; We need a special property. Use `org-entry-properties' to
  13756. ;; retrieve it, but specify the wanted property
  13757. (cdr (assoc property (org-entry-properties nil 'special property)))
  13758. (let ((range (org-get-property-block)))
  13759. (when (and range (not (eq (car range) (cdr range))))
  13760. (let* ((props (list (or (assoc property org-file-properties)
  13761. (assoc property org-global-properties)
  13762. (assoc property org-global-properties-fixed))))
  13763. (ap (lambda (key)
  13764. (when (re-search-forward
  13765. (org-re-property key) (cdr range) t)
  13766. (setq props
  13767. (org-update-property-plist
  13768. key
  13769. (if (match-end 1)
  13770. (org-match-string-no-properties 1) "")
  13771. props)))))
  13772. val)
  13773. (goto-char (car range))
  13774. (funcall ap property)
  13775. (goto-char (car range))
  13776. (while (funcall ap (concat property "+")))
  13777. (setq val (cdr (assoc property props)))
  13778. (when val (if literal-nil val (org-not-nil val))))))))))
  13779. (defun org-property-or-variable-value (var &optional inherit)
  13780. "Check if there is a property fixing the value of VAR.
  13781. If yes, return this value. If not, return the current value of the variable."
  13782. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13783. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13784. (read prop)
  13785. (symbol-value var))))
  13786. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13787. "Delete the property PROPERTY from entry at point-or-marker POM.
  13788. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13789. an empty drawer to delete."
  13790. (org-with-point-at pom
  13791. (if (member property org-special-properties)
  13792. nil ; cannot delete these properties.
  13793. (let ((range (org-get-property-block)))
  13794. (if (and range
  13795. (goto-char (car range))
  13796. (re-search-forward
  13797. (org-re-property property)
  13798. (cdr range) t))
  13799. (progn
  13800. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13801. (and delete-empty-drawer
  13802. (org-remove-empty-drawer-at
  13803. delete-empty-drawer (car range)))
  13804. t)
  13805. nil)))))
  13806. ;; Multi-values properties are properties that contain multiple values
  13807. ;; These values are assumed to be single words, separated by whitespace.
  13808. (defun org-entry-add-to-multivalued-property (pom property value)
  13809. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13810. (let* ((old (org-entry-get pom property))
  13811. (values (and old (org-split-string old "[ \t]"))))
  13812. (setq value (org-entry-protect-space value))
  13813. (unless (member value values)
  13814. (setq values (cons value values))
  13815. (org-entry-put pom property
  13816. (mapconcat 'identity values " ")))))
  13817. (defun org-entry-remove-from-multivalued-property (pom property value)
  13818. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13819. (let* ((old (org-entry-get pom property))
  13820. (values (and old (org-split-string old "[ \t]"))))
  13821. (setq value (org-entry-protect-space value))
  13822. (when (member value values)
  13823. (setq values (delete value values))
  13824. (org-entry-put pom property
  13825. (mapconcat 'identity values " ")))))
  13826. (defun org-entry-member-in-multivalued-property (pom property value)
  13827. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13828. (let* ((old (org-entry-get pom property))
  13829. (values (and old (org-split-string old "[ \t]"))))
  13830. (setq value (org-entry-protect-space value))
  13831. (member value values)))
  13832. (defun org-entry-get-multivalued-property (pom property)
  13833. "Return a list of values in a multivalued property."
  13834. (let* ((value (org-entry-get pom property))
  13835. (values (and value (org-split-string value "[ \t]"))))
  13836. (mapcar 'org-entry-restore-space values)))
  13837. (defun org-entry-put-multivalued-property (pom property &rest values)
  13838. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13839. VALUES should be a list of strings. Spaces will be protected."
  13840. (org-entry-put pom property
  13841. (mapconcat 'org-entry-protect-space values " "))
  13842. (let* ((value (org-entry-get pom property))
  13843. (values (and value (org-split-string value "[ \t]"))))
  13844. (mapcar 'org-entry-restore-space values)))
  13845. (defun org-entry-protect-space (s)
  13846. "Protect spaces and newline in string S."
  13847. (while (string-match " " s)
  13848. (setq s (replace-match "%20" t t s)))
  13849. (while (string-match "\n" s)
  13850. (setq s (replace-match "%0A" t t s)))
  13851. s)
  13852. (defun org-entry-restore-space (s)
  13853. "Restore spaces and newline in string S."
  13854. (while (string-match "%20" s)
  13855. (setq s (replace-match " " t t s)))
  13856. (while (string-match "%0A" s)
  13857. (setq s (replace-match "\n" t t s)))
  13858. s)
  13859. (defvar org-entry-property-inherited-from (make-marker)
  13860. "Marker pointing to the entry from where a property was inherited.
  13861. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13862. location of the entry where the inheritance search matched. If there was
  13863. no match, the marker will point nowhere.
  13864. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13865. is set.")
  13866. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13867. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13868. The search will stop at the first ancestor which has the property defined.
  13869. If the value found is \"nil\", return nil to show that the property
  13870. should be considered as undefined (this is the meaning of nil here).
  13871. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13872. (move-marker org-entry-property-inherited-from nil)
  13873. (let (tmp)
  13874. (save-excursion
  13875. (save-restriction
  13876. (widen)
  13877. (catch 'ex
  13878. (while t
  13879. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13880. (or (ignore-errors (org-back-to-heading t))
  13881. (goto-char (point-min)))
  13882. (move-marker org-entry-property-inherited-from (point))
  13883. (throw 'ex tmp))
  13884. (or (ignore-errors (org-up-heading-safe))
  13885. (throw 'ex nil))))))
  13886. (setq tmp (or tmp
  13887. (cdr (assoc property org-file-properties))
  13888. (cdr (assoc property org-global-properties))
  13889. (cdr (assoc property org-global-properties-fixed))))
  13890. (if literal-nil tmp (org-not-nil tmp))))
  13891. (defvar org-property-changed-functions nil
  13892. "Hook called when the value of a property has changed.
  13893. Each hook function should accept two arguments, the name of the property
  13894. and the new value.")
  13895. (defun org-entry-put (pom property value)
  13896. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13897. (org-with-point-at pom
  13898. (org-back-to-heading t)
  13899. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13900. range)
  13901. (cond
  13902. ((equal property "TODO")
  13903. (when (and (stringp value) (string-match "\\S-" value)
  13904. (not (member value org-todo-keywords-1)))
  13905. (error "\"%s\" is not a valid TODO state" value))
  13906. (if (or (not value)
  13907. (not (string-match "\\S-" value)))
  13908. (setq value 'none))
  13909. (org-todo value)
  13910. (org-set-tags nil 'align))
  13911. ((equal property "PRIORITY")
  13912. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13913. (string-to-char value) ?\ ))
  13914. (org-set-tags nil 'align))
  13915. ((equal property "CLOCKSUM")
  13916. (if (not (re-search-forward
  13917. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13918. (error "Cannot find a clock log")
  13919. (goto-char (- (match-end 1) 2))
  13920. (cond
  13921. ((eq value 'earlier) (org-timestamp-down))
  13922. ((eq value 'later) (org-timestamp-up)))
  13923. (org-clock-sum-current-item)))
  13924. ((equal property "SCHEDULED")
  13925. (if (re-search-forward org-scheduled-time-regexp end t)
  13926. (cond
  13927. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13928. ((eq value 'later) (org-timestamp-change 1 'day))
  13929. (t (call-interactively 'org-schedule)))
  13930. (call-interactively 'org-schedule)))
  13931. ((equal property "DEADLINE")
  13932. (if (re-search-forward org-deadline-time-regexp end t)
  13933. (cond
  13934. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13935. ((eq value 'later) (org-timestamp-change 1 'day))
  13936. (t (call-interactively 'org-deadline)))
  13937. (call-interactively 'org-deadline)))
  13938. ((member property org-special-properties)
  13939. (error "The %s property can not yet be set with `org-entry-put'"
  13940. property))
  13941. (t ; a non-special property
  13942. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13943. (setq range (org-get-property-block beg end 'force))
  13944. (goto-char (car range))
  13945. (if (re-search-forward
  13946. (org-re-property-keyword property) (cdr range) t)
  13947. (progn
  13948. (delete-region (match-beginning 0) (match-end 0))
  13949. (goto-char (match-beginning 0)))
  13950. (goto-char (cdr range))
  13951. (insert "\n")
  13952. (backward-char 1)
  13953. (org-indent-line))
  13954. (insert ":" property ":")
  13955. (and value (insert " " value))
  13956. (org-indent-line)))))
  13957. (run-hook-with-args 'org-property-changed-functions property value)))
  13958. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13959. "Get all property keys in the current buffer.
  13960. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13961. like tags and TODO state.
  13962. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13963. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13964. and others.
  13965. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13966. formats in the current buffer."
  13967. (let (rtn range cfmt s p)
  13968. (save-excursion
  13969. (save-restriction
  13970. (widen)
  13971. (goto-char (point-min))
  13972. (while (re-search-forward org-property-start-re nil t)
  13973. (setq range (org-get-property-block))
  13974. (goto-char (car range))
  13975. (while (re-search-forward
  13976. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13977. (cdr range) t)
  13978. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13979. (outline-next-heading))))
  13980. (when include-specials
  13981. (setq rtn (append org-special-properties rtn)))
  13982. (when include-defaults
  13983. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13984. (add-to-list 'rtn org-effort-property))
  13985. (when include-columns
  13986. (save-excursion
  13987. (save-restriction
  13988. (widen)
  13989. (goto-char (point-min))
  13990. (while (re-search-forward
  13991. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13992. nil t)
  13993. (setq cfmt (match-string 2) s 0)
  13994. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13995. cfmt s)
  13996. (setq s (match-end 0)
  13997. p (match-string 1 cfmt))
  13998. (unless (or (equal p "ITEM")
  13999. (member p org-special-properties))
  14000. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14001. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14002. (defun org-property-values (key)
  14003. "Return a list of all values of property KEY in the current buffer."
  14004. (save-excursion
  14005. (save-restriction
  14006. (widen)
  14007. (goto-char (point-min))
  14008. (let ((re (org-re-property key))
  14009. values)
  14010. (while (re-search-forward re nil t)
  14011. (add-to-list 'values (org-trim (match-string 1))))
  14012. (delete "" values)))))
  14013. (defun org-insert-property-drawer ()
  14014. "Insert a property drawer into the current entry."
  14015. (org-back-to-heading t)
  14016. (looking-at org-outline-regexp)
  14017. (let ((indent (if org-adapt-indentation
  14018. (- (match-end 0) (match-beginning 0))
  14019. 0))
  14020. (beg (point))
  14021. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14022. end hiddenp)
  14023. (outline-next-heading)
  14024. (setq end (point))
  14025. (goto-char beg)
  14026. (while (re-search-forward re end t))
  14027. (setq hiddenp (outline-invisible-p))
  14028. (end-of-line 1)
  14029. (and (equal (char-after) ?\n) (forward-char 1))
  14030. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14031. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14032. ;; just skip this line
  14033. (beginning-of-line 2)
  14034. ;; Drawer start, find the end
  14035. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14036. (beginning-of-line 1)))
  14037. (org-skip-over-state-notes)
  14038. (skip-chars-backward " \t\n\r")
  14039. (if (eq (char-before) ?*) (forward-char 1))
  14040. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14041. (beginning-of-line 0)
  14042. (org-indent-to-column indent)
  14043. (beginning-of-line 2)
  14044. (org-indent-to-column indent)
  14045. (beginning-of-line 0)
  14046. (if hiddenp
  14047. (save-excursion
  14048. (org-back-to-heading t)
  14049. (hide-entry))
  14050. (org-flag-drawer t))))
  14051. (defun org-insert-drawer (&optional arg drawer)
  14052. "Insert a drawer at point.
  14053. Optional argument DRAWER, when non-nil, is a string representing
  14054. drawer's name. Otherwise, the user is prompted for a name.
  14055. If a region is active, insert the drawer around that region
  14056. instead.
  14057. Point is left between drawer's boundaries."
  14058. (interactive "P")
  14059. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14060. "LOGBOOK"))
  14061. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14062. ;; internally by Org. They are meant to be inserted
  14063. ;; automatically.
  14064. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14065. ;; Remove system drawers from list. Note: For some reason,
  14066. ;; `org-completing-read' ignores the predicate while
  14067. ;; `completing-read' handles it fine.
  14068. (drawer (if arg "PROPERTIES"
  14069. (or drawer
  14070. (completing-read
  14071. "Drawer: " org-drawers
  14072. (lambda (d) (not (member d system-drawers))))))))
  14073. (cond
  14074. ;; With C-u, fall back on `org-insert-property-drawer'
  14075. (arg (org-insert-property-drawer))
  14076. ;; With an active region, insert a drawer at point.
  14077. ((not (org-region-active-p))
  14078. (progn
  14079. (unless (bolp) (insert "\n"))
  14080. (insert (format ":%s:\n\n:END:\n" drawer))
  14081. (forward-line -2)))
  14082. ;; Otherwise, insert the drawer at point
  14083. (t
  14084. (let ((rbeg (region-beginning))
  14085. (rend (copy-marker (region-end))))
  14086. (unwind-protect
  14087. (progn
  14088. (goto-char rbeg)
  14089. (beginning-of-line)
  14090. (when (save-excursion
  14091. (re-search-forward org-outline-regexp-bol rend t))
  14092. (error "Drawers cannot contain headlines"))
  14093. ;; Position point at the beginning of the first
  14094. ;; non-blank line in region. Insert drawer's opening
  14095. ;; there, then indent it.
  14096. (org-skip-whitespace)
  14097. (beginning-of-line)
  14098. (insert ":" drawer ":\n")
  14099. (forward-line -1)
  14100. (indent-for-tab-command)
  14101. ;; Move point to the beginning of the first blank line
  14102. ;; after the last non-blank line in region. Insert
  14103. ;; drawer's closing, then indent it.
  14104. (goto-char rend)
  14105. (skip-chars-backward " \r\t\n")
  14106. (insert "\n:END:")
  14107. (deactivate-mark t)
  14108. (indent-for-tab-command)
  14109. (unless (eolp) (insert "\n")))
  14110. ;; Clear marker, whatever the outcome of insertion is.
  14111. (set-marker rend nil)))))))
  14112. (defvar org-property-set-functions-alist nil
  14113. "Property set function alist.
  14114. Each entry should have the following format:
  14115. (PROPERTY . READ-FUNCTION)
  14116. The read function will be called with the same argument as
  14117. `org-completing-read'.")
  14118. (defun org-set-property-function (property)
  14119. "Get the function that should be used to set PROPERTY.
  14120. This is computed according to `org-property-set-functions-alist'."
  14121. (or (cdr (assoc property org-property-set-functions-alist))
  14122. 'org-completing-read))
  14123. (defun org-read-property-value (property)
  14124. "Read PROPERTY value from user."
  14125. (let* ((completion-ignore-case t)
  14126. (allowed (org-property-get-allowed-values nil property 'table))
  14127. (cur (org-entry-get nil property))
  14128. (prompt (concat property " value"
  14129. (if (and cur (string-match "\\S-" cur))
  14130. (concat " [" cur "]") "") ": "))
  14131. (set-function (org-set-property-function property))
  14132. (val (if allowed
  14133. (funcall set-function prompt allowed nil
  14134. (not (get-text-property 0 'org-unrestricted
  14135. (caar allowed))))
  14136. (let (org-completion-use-ido org-completion-use-iswitchb)
  14137. (funcall set-function prompt
  14138. (mapcar 'list (org-property-values property))
  14139. nil nil "" nil cur)))))
  14140. (if (equal val "")
  14141. cur
  14142. val)))
  14143. (defvar org-last-set-property nil)
  14144. (defvar org-last-set-property-value nil)
  14145. (defun org-read-property-name ()
  14146. "Read a property name."
  14147. (let* ((completion-ignore-case t)
  14148. (keys (org-buffer-property-keys nil t t))
  14149. (default-prop (or (save-excursion
  14150. (save-match-data
  14151. (beginning-of-line)
  14152. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14153. (null (string= (match-string 1) "END"))
  14154. (match-string 1))))
  14155. org-last-set-property))
  14156. (property (org-icompleting-read
  14157. (concat "Property"
  14158. (if default-prop (concat " [" default-prop "]") "")
  14159. ": ")
  14160. (mapcar 'list keys)
  14161. nil nil nil nil
  14162. default-prop)))
  14163. (if (member property keys)
  14164. property
  14165. (or (cdr (assoc (downcase property)
  14166. (mapcar (lambda (x) (cons (downcase x) x))
  14167. keys)))
  14168. property))))
  14169. (defun org-set-property-and-value (use-last)
  14170. "Allow to set [PROPERTY]: [value] direction from prompt.
  14171. When use-default, don't even ask, just use the last
  14172. \"[PROPERTY]: [value]\" string from the history."
  14173. (interactive "P")
  14174. (let* ((completion-ignore-case t)
  14175. (pv (or (and use-last org-last-set-property-value)
  14176. (org-completing-read
  14177. "Enter a \"[Property]: [value]\" pair: "
  14178. nil nil nil nil nil
  14179. org-last-set-property-value)))
  14180. prop val)
  14181. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14182. (setq prop (match-string 1 pv)
  14183. val (match-string 2 pv))
  14184. (org-set-property prop val))))
  14185. (defun org-set-property (property value)
  14186. "In the current entry, set PROPERTY to VALUE.
  14187. When called interactively, this will prompt for a property name, offering
  14188. completion on existing and default properties. And then it will prompt
  14189. for a value, offering completion either on allowed values (via an inherited
  14190. xxx_ALL property) or on existing values in other instances of this property
  14191. in the current file."
  14192. (interactive (list nil nil))
  14193. (let* ((property (or property (org-read-property-name)))
  14194. (value (or value (org-read-property-value property)))
  14195. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14196. (setq org-last-set-property property)
  14197. (setq org-last-set-property-value (concat property ": " value))
  14198. ;; Possibly postprocess the inserted value:
  14199. (when fn (setq value (funcall fn value)))
  14200. (unless (equal (org-entry-get nil property) value)
  14201. (org-entry-put nil property value))))
  14202. (defun org-delete-property (property &optional delete-empty-drawer)
  14203. "In the current entry, delete PROPERTY.
  14204. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14205. an empty drawer to delete."
  14206. (interactive
  14207. (let* ((completion-ignore-case t)
  14208. (prop (org-icompleting-read "Property: "
  14209. (org-entry-properties nil 'standard))))
  14210. (list prop)))
  14211. (message "Property %s %s" property
  14212. (if (org-entry-delete nil property delete-empty-drawer)
  14213. "deleted"
  14214. "was not present in the entry")))
  14215. (defun org-delete-property-globally (property)
  14216. "Remove PROPERTY globally, from all entries."
  14217. (interactive
  14218. (let* ((completion-ignore-case t)
  14219. (prop (org-icompleting-read
  14220. "Globally remove property: "
  14221. (mapcar 'list (org-buffer-property-keys)))))
  14222. (list prop)))
  14223. (save-excursion
  14224. (save-restriction
  14225. (widen)
  14226. (goto-char (point-min))
  14227. (let ((cnt 0))
  14228. (while (re-search-forward
  14229. (org-re-property property)
  14230. nil t)
  14231. (setq cnt (1+ cnt))
  14232. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14233. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14234. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14235. (defun org-compute-property-at-point ()
  14236. "Compute the property at point.
  14237. This looks for an enclosing column format, extracts the operator and
  14238. then applies it to the property in the column format's scope."
  14239. (interactive)
  14240. (unless (org-at-property-p)
  14241. (error "Not at a property"))
  14242. (let ((prop (org-match-string-no-properties 2)))
  14243. (org-columns-get-format-and-top-level)
  14244. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14245. (error "No operator defined for property %s" prop))
  14246. (org-columns-compute prop)))
  14247. (defvar org-property-allowed-value-functions nil
  14248. "Hook for functions supplying allowed values for a specific property.
  14249. The functions must take a single argument, the name of the property, and
  14250. return a flat list of allowed values. If \":ETC\" is one of
  14251. the values, this means that these values are intended as defaults for
  14252. completion, but that other values should be allowed too.
  14253. The functions must return nil if they are not responsible for this
  14254. property.")
  14255. (defun org-property-get-allowed-values (pom property &optional table)
  14256. "Get allowed values for the property PROPERTY.
  14257. When TABLE is non-nil, return an alist that can directly be used for
  14258. completion."
  14259. (let (vals)
  14260. (cond
  14261. ((equal property "TODO")
  14262. (setq vals (org-with-point-at pom
  14263. (append org-todo-keywords-1 '("")))))
  14264. ((equal property "PRIORITY")
  14265. (let ((n org-lowest-priority))
  14266. (while (>= n org-highest-priority)
  14267. (push (char-to-string n) vals)
  14268. (setq n (1- n)))))
  14269. ((member property org-special-properties))
  14270. ((setq vals (run-hook-with-args-until-success
  14271. 'org-property-allowed-value-functions property)))
  14272. (t
  14273. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14274. (when (and vals (string-match "\\S-" vals))
  14275. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14276. (setq vals (mapcar (lambda (x)
  14277. (cond ((stringp x) x)
  14278. ((numberp x) (number-to-string x))
  14279. ((symbolp x) (symbol-name x))
  14280. (t "???")))
  14281. vals)))))
  14282. (when (member ":ETC" vals)
  14283. (setq vals (remove ":ETC" vals))
  14284. (org-add-props (car vals) '(org-unrestricted t)))
  14285. (if table (mapcar 'list vals) vals)))
  14286. (defun org-property-previous-allowed-value (&optional previous)
  14287. "Switch to the next allowed value for this property."
  14288. (interactive)
  14289. (org-property-next-allowed-value t))
  14290. (defun org-property-next-allowed-value (&optional previous)
  14291. "Switch to the next allowed value for this property."
  14292. (interactive)
  14293. (unless (org-at-property-p)
  14294. (error "Not at a property"))
  14295. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14296. (key (match-string 2))
  14297. (value (match-string 3))
  14298. (allowed (or (org-property-get-allowed-values (point) key)
  14299. (and (member value '("[ ]" "[-]" "[X]"))
  14300. '("[ ]" "[X]"))))
  14301. (heading (save-match-data (nth 4 (org-heading-components))))
  14302. nval)
  14303. (unless allowed
  14304. (error "Allowed values for this property have not been defined"))
  14305. (if previous (setq allowed (reverse allowed)))
  14306. (if (member value allowed)
  14307. (setq nval (car (cdr (member value allowed)))))
  14308. (setq nval (or nval (car allowed)))
  14309. (if (equal nval value)
  14310. (error "Only one allowed value for this property"))
  14311. (org-at-property-p)
  14312. (replace-match (concat " :" key ": " nval) t t)
  14313. (org-indent-line)
  14314. (beginning-of-line 1)
  14315. (skip-chars-forward " \t")
  14316. (when (equal prop org-effort-property)
  14317. (save-excursion
  14318. (org-back-to-heading t)
  14319. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14320. (when (string= org-clock-current-task heading)
  14321. (setq org-clock-effort nval)
  14322. (org-clock-update-mode-line)))
  14323. (run-hook-with-args 'org-property-changed-functions key nval)))
  14324. (defun org-find-olp (path &optional this-buffer)
  14325. "Return a marker pointing to the entry at outline path OLP.
  14326. If anything goes wrong, throw an error.
  14327. You can wrap this call to catch the error like this:
  14328. (condition-case msg
  14329. (org-mobile-locate-entry (match-string 4))
  14330. (error (nth 1 msg)))
  14331. The return value will then be either a string with the error message,
  14332. or a marker if everything is OK.
  14333. If THIS-BUFFER is set, the outline path does not contain a file,
  14334. only headings."
  14335. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14336. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14337. (level 1)
  14338. (lmin 1)
  14339. (lmax 1)
  14340. limit re end found pos heading cnt flevel)
  14341. (unless buffer (error "File not found :%s" file))
  14342. (with-current-buffer buffer
  14343. (save-excursion
  14344. (save-restriction
  14345. (widen)
  14346. (setq limit (point-max))
  14347. (goto-char (point-min))
  14348. (while (setq heading (pop path))
  14349. (setq re (format org-complex-heading-regexp-format
  14350. (regexp-quote heading)))
  14351. (setq cnt 0 pos (point))
  14352. (while (re-search-forward re end t)
  14353. (setq level (- (match-end 1) (match-beginning 1)))
  14354. (if (and (>= level lmin) (<= level lmax))
  14355. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14356. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14357. lmax heading))
  14358. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14359. lmax heading))
  14360. (goto-char found)
  14361. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14362. (setq end (save-excursion (org-end-of-subtree t t))))
  14363. (when (org-at-heading-p)
  14364. (point-marker)))))))
  14365. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14366. "Find node HEADING in BUFFER.
  14367. Return a marker to the heading if it was found, or nil if not.
  14368. If POS-ONLY is set, return just the position instead of a marker.
  14369. The heading text must match exact, but it may have a TODO keyword,
  14370. a priority cookie and tags in the standard locations."
  14371. (with-current-buffer (or buffer (current-buffer))
  14372. (save-excursion
  14373. (save-restriction
  14374. (widen)
  14375. (goto-char (point-min))
  14376. (let (case-fold-search)
  14377. (if (re-search-forward
  14378. (format org-complex-heading-regexp-format
  14379. (regexp-quote heading)) nil t)
  14380. (if pos-only
  14381. (match-beginning 0)
  14382. (move-marker (make-marker) (match-beginning 0)))))))))
  14383. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14384. "Find Org node headline HEADING in all .org files in directory DIR.
  14385. When the target headline is found, return a marker to this location."
  14386. (let ((files (directory-files (or dir default-directory)
  14387. nil "\\`[^.#].*\\.org\\'"))
  14388. file visiting m buffer)
  14389. (catch 'found
  14390. (while (setq file (pop files))
  14391. (message "trying %s" file)
  14392. (setq visiting (org-find-base-buffer-visiting file))
  14393. (setq buffer (or visiting (find-file-noselect file)))
  14394. (setq m (org-find-exact-headline-in-buffer
  14395. heading buffer))
  14396. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14397. (and m (throw 'found m))))))
  14398. (defun org-find-entry-with-id (ident)
  14399. "Locate the entry that contains the ID property with exact value IDENT.
  14400. IDENT can be a string, a symbol or a number, this function will search for
  14401. the string representation of it.
  14402. Return the position where this entry starts, or nil if there is no such entry."
  14403. (interactive "sID: ")
  14404. (let ((id (cond
  14405. ((stringp ident) ident)
  14406. ((symbol-name ident) (symbol-name ident))
  14407. ((numberp ident) (number-to-string ident))
  14408. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14409. (case-fold-search nil))
  14410. (save-excursion
  14411. (save-restriction
  14412. (widen)
  14413. (goto-char (point-min))
  14414. (when (re-search-forward
  14415. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14416. nil t)
  14417. (org-back-to-heading t)
  14418. (point))))))
  14419. ;;;; Timestamps
  14420. (defvar org-last-changed-timestamp nil)
  14421. (defvar org-last-inserted-timestamp nil
  14422. "The last time stamp inserted with `org-insert-time-stamp'.")
  14423. (defvar org-time-was-given) ; dynamically scoped parameter
  14424. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14425. (defvar org-ts-what) ; dynamically scoped parameter
  14426. (defun org-time-stamp (arg &optional inactive)
  14427. "Prompt for a date/time and insert a time stamp.
  14428. If the user specifies a time like HH:MM or if this command is
  14429. called with at least one prefix argument, the time stamp contains
  14430. the date and the time. Otherwise, only the date is be included.
  14431. All parts of a date not specified by the user is filled in from
  14432. the current date/time. So if you just press return without
  14433. typing anything, the time stamp will represent the current
  14434. date/time.
  14435. If there is already a timestamp at the cursor, it will be
  14436. modified.
  14437. With two universal prefix arguments, insert an active timestamp
  14438. with the current time without prompting the user."
  14439. (interactive "P")
  14440. (let* ((ts nil)
  14441. (default-time
  14442. ;; Default time is either today, or, when entering a range,
  14443. ;; the range start.
  14444. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14445. (save-excursion
  14446. (re-search-backward
  14447. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14448. (- (point) 20) t)))
  14449. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14450. (current-time)))
  14451. (default-input (and ts (org-get-compact-tod ts)))
  14452. (repeater (save-excursion
  14453. (save-match-data
  14454. (beginning-of-line)
  14455. (when (re-search-forward
  14456. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14457. (save-excursion (progn (end-of-line) (point))) t)
  14458. (match-string 0)))))
  14459. org-time-was-given org-end-time-was-given time)
  14460. (cond
  14461. ((and (org-at-timestamp-p t)
  14462. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14463. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14464. (insert "--")
  14465. (setq time (let ((this-command this-command))
  14466. (org-read-date arg 'totime nil nil
  14467. default-time default-input inactive)))
  14468. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14469. ((org-at-timestamp-p t)
  14470. (setq time (let ((this-command this-command))
  14471. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14472. (when (org-at-timestamp-p t) ; just to get the match data
  14473. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14474. (replace-match "")
  14475. (setq org-last-changed-timestamp
  14476. (org-insert-time-stamp
  14477. time (or org-time-was-given arg)
  14478. inactive nil nil (list org-end-time-was-given)))
  14479. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14480. (setq org-last-changed-timestamp
  14481. (concat (substring org-last-inserted-timestamp 0 -1)
  14482. " " repeater ">"))))
  14483. (message "Timestamp updated"))
  14484. ((equal arg '(16))
  14485. (org-insert-time-stamp (current-time) t))
  14486. (t
  14487. (setq time (let ((this-command this-command))
  14488. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14489. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14490. nil nil (list org-end-time-was-given))))))
  14491. ;; FIXME: can we use this for something else, like computing time differences?
  14492. (defun org-get-compact-tod (s)
  14493. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14494. (let* ((t1 (match-string 1 s))
  14495. (h1 (string-to-number (match-string 2 s)))
  14496. (m1 (string-to-number (match-string 3 s)))
  14497. (t2 (and (match-end 4) (match-string 5 s)))
  14498. (h2 (and t2 (string-to-number (match-string 6 s))))
  14499. (m2 (and t2 (string-to-number (match-string 7 s))))
  14500. dh dm)
  14501. (if (not t2)
  14502. t1
  14503. (setq dh (- h2 h1) dm (- m2 m1))
  14504. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14505. (concat t1 "+" (number-to-string dh)
  14506. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14507. (defun org-time-stamp-inactive (&optional arg)
  14508. "Insert an inactive time stamp.
  14509. An inactive time stamp is enclosed in square brackets instead of angle
  14510. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14511. does not link to the calendar and cannot be changed with the S-cursor keys.
  14512. So these are more for recording a certain time/date."
  14513. (interactive "P")
  14514. (org-time-stamp arg 'inactive))
  14515. (defvar org-date-ovl (make-overlay 1 1))
  14516. (overlay-put org-date-ovl 'face 'org-date-selected)
  14517. (org-detach-overlay org-date-ovl)
  14518. (defvar org-ans1) ; dynamically scoped parameter
  14519. (defvar org-ans2) ; dynamically scoped parameter
  14520. (defvar org-plain-time-of-day-regexp) ; defined below
  14521. (defvar org-overriding-default-time nil) ; dynamically scoped
  14522. (defvar org-read-date-overlay nil)
  14523. (defvar org-dcst nil) ; dynamically scoped
  14524. (defvar org-read-date-history nil)
  14525. (defvar org-read-date-final-answer nil)
  14526. (defvar org-read-date-analyze-futurep nil)
  14527. (defvar org-read-date-analyze-forced-year nil)
  14528. (defvar org-read-date-inactive)
  14529. (defvar org-read-date-minibuffer-local-map
  14530. (let ((map (make-sparse-keymap)))
  14531. (set-keymap-parent map minibuffer-local-map)
  14532. (org-defkey map (kbd ".")
  14533. (lambda () (interactive)
  14534. (org-eval-in-calendar '(calendar-goto-today))))
  14535. (org-defkey map [(meta shift left)]
  14536. (lambda () (interactive)
  14537. (org-eval-in-calendar '(calendar-backward-month 1))))
  14538. (org-defkey map [(meta shift right)]
  14539. (lambda () (interactive)
  14540. (org-eval-in-calendar '(calendar-forward-month 1))))
  14541. (org-defkey map [(meta shift up)]
  14542. (lambda () (interactive)
  14543. (org-eval-in-calendar '(calendar-backward-year 1))))
  14544. (org-defkey map [(meta shift down)]
  14545. (lambda () (interactive)
  14546. (org-eval-in-calendar '(calendar-forward-year 1))))
  14547. (org-defkey map [?\e (shift left)]
  14548. (lambda () (interactive)
  14549. (org-eval-in-calendar '(calendar-backward-month 1))))
  14550. (org-defkey map [?\e (shift right)]
  14551. (lambda () (interactive)
  14552. (org-eval-in-calendar '(calendar-forward-month 1))))
  14553. (org-defkey map [?\e (shift up)]
  14554. (lambda () (interactive)
  14555. (org-eval-in-calendar '(calendar-backward-year 1))))
  14556. (org-defkey map [?\e (shift down)]
  14557. (lambda () (interactive)
  14558. (org-eval-in-calendar '(calendar-forward-year 1))))
  14559. (org-defkey map [(shift up)]
  14560. (lambda () (interactive)
  14561. (org-eval-in-calendar '(calendar-backward-week 1))))
  14562. (org-defkey map [(shift down)]
  14563. (lambda () (interactive)
  14564. (org-eval-in-calendar '(calendar-forward-week 1))))
  14565. (org-defkey map [(shift left)]
  14566. (lambda () (interactive)
  14567. (org-eval-in-calendar '(calendar-backward-day 1))))
  14568. (org-defkey map [(shift right)]
  14569. (lambda () (interactive)
  14570. (org-eval-in-calendar '(calendar-forward-day 1))))
  14571. (org-defkey map "!"
  14572. (lambda () (interactive)
  14573. (org-eval-in-calendar '(diary-view-entries))
  14574. (message "")))
  14575. (org-defkey map ">"
  14576. (lambda () (interactive)
  14577. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14578. (org-defkey map "<"
  14579. (lambda () (interactive)
  14580. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14581. (org-defkey map "\C-v"
  14582. (lambda () (interactive)
  14583. (org-eval-in-calendar
  14584. '(calendar-scroll-left-three-months 1))))
  14585. (org-defkey map "\M-v"
  14586. (lambda () (interactive)
  14587. (org-eval-in-calendar
  14588. '(calendar-scroll-right-three-months 1))))
  14589. map)
  14590. "Keymap for minibuffer commands when using `org-read-date'.")
  14591. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14592. default-time default-input inactive)
  14593. "Read a date, possibly a time, and make things smooth for the user.
  14594. The prompt will suggest to enter an ISO date, but you can also enter anything
  14595. which will at least partially be understood by `parse-time-string'.
  14596. Unrecognized parts of the date will default to the current day, month, year,
  14597. hour and minute. If this command is called to replace a timestamp at point,
  14598. or to enter the second timestamp of a range, the default time is taken
  14599. from the existing stamp. Furthermore, the command prefers the future,
  14600. so if you are giving a date where the year is not given, and the day-month
  14601. combination is already past in the current year, it will assume you
  14602. mean next year. For details, see the manual. A few examples:
  14603. 3-2-5 --> 2003-02-05
  14604. feb 15 --> currentyear-02-15
  14605. 2/15 --> currentyear-02-15
  14606. sep 12 9 --> 2009-09-12
  14607. 12:45 --> today 12:45
  14608. 22 sept 0:34 --> currentyear-09-22 0:34
  14609. 12 --> currentyear-currentmonth-12
  14610. Fri --> nearest Friday (today or later)
  14611. etc.
  14612. Furthermore you can specify a relative date by giving, as the *first* thing
  14613. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14614. change in days weeks, months, years.
  14615. With a single plus or minus, the date is relative to today. With a double
  14616. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14617. +4d --> four days from today
  14618. +4 --> same as above
  14619. +2w --> two weeks from today
  14620. ++5 --> five days from default date
  14621. The function understands only English month and weekday abbreviations.
  14622. While prompting, a calendar is popped up - you can also select the
  14623. date with the mouse (button 1). The calendar shows a period of three
  14624. months. To scroll it to other months, use the keys `>' and `<'.
  14625. If you don't like the calendar, turn it off with
  14626. \(setq org-read-date-popup-calendar nil)
  14627. With optional argument TO-TIME, the date will immediately be converted
  14628. to an internal time.
  14629. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14630. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14631. still enter a time, and this function will inform the calling routine
  14632. about this change. The calling routine may then choose to change the
  14633. format used to insert the time stamp into the buffer to include the time.
  14634. With optional argument FROM-STRING, read from this string instead from
  14635. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14636. the time/date that is used for everything that is not specified by the
  14637. user."
  14638. (require 'parse-time)
  14639. (let* ((org-time-stamp-rounding-minutes
  14640. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14641. (org-dcst org-display-custom-times)
  14642. (ct (org-current-time))
  14643. (org-def (or org-overriding-default-time default-time ct))
  14644. (org-defdecode (decode-time org-def))
  14645. (dummy (progn
  14646. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14647. (setcar (nthcdr 2 org-defdecode) -1)
  14648. (setcar (nthcdr 1 org-defdecode) 59)
  14649. (setq org-def (apply 'encode-time org-defdecode)
  14650. org-defdecode (decode-time org-def)))))
  14651. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14652. (calendar-frame-setup nil)
  14653. (calendar-setup nil)
  14654. (calendar-move-hook nil)
  14655. (calendar-view-diary-initially-flag nil)
  14656. (calendar-view-holidays-initially-flag nil)
  14657. (timestr (format-time-string
  14658. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14659. (prompt (concat (if prompt (concat prompt " ") "")
  14660. (format "Date+time [%s]: " timestr)))
  14661. ans (org-ans0 "") org-ans1 org-ans2 final)
  14662. (cond
  14663. (from-string (setq ans from-string))
  14664. (org-read-date-popup-calendar
  14665. (save-excursion
  14666. (save-window-excursion
  14667. (calendar)
  14668. (org-eval-in-calendar '(setq cursor-type nil) t)
  14669. (unwind-protect
  14670. (progn
  14671. (calendar-forward-day (- (time-to-days org-def)
  14672. (calendar-absolute-from-gregorian
  14673. (calendar-current-date))))
  14674. (org-eval-in-calendar nil t)
  14675. (let* ((old-map (current-local-map))
  14676. (map (copy-keymap calendar-mode-map))
  14677. (minibuffer-local-map
  14678. (copy-keymap org-read-date-minibuffer-local-map)))
  14679. (org-defkey map (kbd "RET") 'org-calendar-select)
  14680. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14681. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14682. (unwind-protect
  14683. (progn
  14684. (use-local-map map)
  14685. (setq org-read-date-inactive inactive)
  14686. (add-hook 'post-command-hook 'org-read-date-display)
  14687. (setq org-ans0 (read-string prompt default-input
  14688. 'org-read-date-history nil))
  14689. ;; org-ans0: from prompt
  14690. ;; org-ans1: from mouse click
  14691. ;; org-ans2: from calendar motion
  14692. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14693. (remove-hook 'post-command-hook 'org-read-date-display)
  14694. (use-local-map old-map)
  14695. (when org-read-date-overlay
  14696. (delete-overlay org-read-date-overlay)
  14697. (setq org-read-date-overlay nil)))))
  14698. (bury-buffer "*Calendar*")))))
  14699. (t ; Naked prompt only
  14700. (unwind-protect
  14701. (setq ans (read-string prompt default-input
  14702. 'org-read-date-history timestr))
  14703. (when org-read-date-overlay
  14704. (delete-overlay org-read-date-overlay)
  14705. (setq org-read-date-overlay nil)))))
  14706. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14707. (when org-read-date-analyze-forced-year
  14708. (message "Year was forced into %s"
  14709. (if org-read-date-force-compatible-dates
  14710. "compatible range (1970-2037)"
  14711. "range representable on this machine"))
  14712. (ding))
  14713. ;; One round trip to get rid of 34th of August and stuff like that....
  14714. (setq final (decode-time (apply 'encode-time final)))
  14715. (setq org-read-date-final-answer ans)
  14716. (if to-time
  14717. (apply 'encode-time final)
  14718. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14719. (format "%04d-%02d-%02d %02d:%02d"
  14720. (nth 5 final) (nth 4 final) (nth 3 final)
  14721. (nth 2 final) (nth 1 final))
  14722. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14723. (defvar org-def)
  14724. (defvar org-defdecode)
  14725. (defvar org-with-time)
  14726. (defun org-read-date-display ()
  14727. "Display the current date prompt interpretation in the minibuffer."
  14728. (when org-read-date-display-live
  14729. (when org-read-date-overlay
  14730. (delete-overlay org-read-date-overlay))
  14731. (when (minibufferp (current-buffer))
  14732. (save-excursion
  14733. (end-of-line 1)
  14734. (while (not (equal (buffer-substring
  14735. (max (point-min) (- (point) 4)) (point))
  14736. " "))
  14737. (insert " ")))
  14738. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14739. " " (or org-ans1 org-ans2)))
  14740. (org-end-time-was-given nil)
  14741. (f (org-read-date-analyze ans org-def org-defdecode))
  14742. (fmts (if org-dcst
  14743. org-time-stamp-custom-formats
  14744. org-time-stamp-formats))
  14745. (fmt (if (or org-with-time
  14746. (and (boundp 'org-time-was-given) org-time-was-given))
  14747. (cdr fmts)
  14748. (car fmts)))
  14749. (txt (format-time-string fmt (apply 'encode-time f)))
  14750. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14751. (txt (concat "=> " txt)))
  14752. (when (and org-end-time-was-given
  14753. (string-match org-plain-time-of-day-regexp txt))
  14754. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14755. org-end-time-was-given
  14756. (substring txt (match-end 0)))))
  14757. (when org-read-date-analyze-futurep
  14758. (setq txt (concat txt " (=>F)")))
  14759. (setq org-read-date-overlay
  14760. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14761. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14762. (defun org-read-date-analyze (ans org-def org-defdecode)
  14763. "Analyze the combined answer of the date prompt."
  14764. ;; FIXME: cleanup and comment
  14765. (let ((nowdecode (decode-time (current-time)))
  14766. delta deltan deltaw deltadef year month day
  14767. hour minute second wday pm h2 m2 tl wday1
  14768. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14769. (setq org-read-date-analyze-futurep nil
  14770. org-read-date-analyze-forced-year nil)
  14771. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14772. (setq ans "+0"))
  14773. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14774. (setq ans (replace-match "" t t ans)
  14775. deltan (car delta)
  14776. deltaw (nth 1 delta)
  14777. deltadef (nth 2 delta)))
  14778. ;; Check if there is an iso week date in there. If yes, store the
  14779. ;; info and postpone interpreting it until the rest of the parsing
  14780. ;; is done.
  14781. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14782. (setq iso-year (if (match-end 1)
  14783. (org-small-year-to-year
  14784. (string-to-number (match-string 1 ans))))
  14785. iso-weekday (if (match-end 3)
  14786. (string-to-number (match-string 3 ans)))
  14787. iso-week (string-to-number (match-string 2 ans)))
  14788. (setq ans (replace-match "" t t ans)))
  14789. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14790. (when (string-match
  14791. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14792. (setq year (if (match-end 2)
  14793. (string-to-number (match-string 2 ans))
  14794. (progn (setq kill-year t)
  14795. (string-to-number (format-time-string "%Y"))))
  14796. month (string-to-number (match-string 3 ans))
  14797. day (string-to-number (match-string 4 ans)))
  14798. (if (< year 100) (setq year (+ 2000 year)))
  14799. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14800. t nil ans)))
  14801. ;; Help matching dotted european dates
  14802. (when (string-match
  14803. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14804. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14805. (setq kill-year t)
  14806. (string-to-number (format-time-string "%Y")))
  14807. day (string-to-number (match-string 1 ans))
  14808. month (string-to-number (match-string 2 ans))
  14809. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14810. t nil ans)))
  14811. ;; Help matching american dates, like 5/30 or 5/30/7
  14812. (when (string-match
  14813. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14814. (setq year (if (match-end 4)
  14815. (string-to-number (match-string 4 ans))
  14816. (progn (setq kill-year t)
  14817. (string-to-number (format-time-string "%Y"))))
  14818. month (string-to-number (match-string 1 ans))
  14819. day (string-to-number (match-string 2 ans)))
  14820. (if (< year 100) (setq year (+ 2000 year)))
  14821. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14822. t nil ans)))
  14823. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14824. ;; If there is a time with am/pm, and *no* time without it, we convert
  14825. ;; so that matching will be successful.
  14826. (loop for i from 1 to 2 do ; twice, for end time as well
  14827. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14828. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14829. (setq hour (string-to-number (match-string 1 ans))
  14830. minute (if (match-end 3)
  14831. (string-to-number (match-string 3 ans))
  14832. 0)
  14833. pm (equal ?p
  14834. (string-to-char (downcase (match-string 4 ans)))))
  14835. (if (and (= hour 12) (not pm))
  14836. (setq hour 0)
  14837. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14838. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14839. t t ans))))
  14840. ;; Check if a time range is given as a duration
  14841. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14842. (setq hour (string-to-number (match-string 1 ans))
  14843. h2 (+ hour (string-to-number (match-string 3 ans)))
  14844. minute (string-to-number (match-string 2 ans))
  14845. m2 (+ minute (if (match-end 5) (string-to-number
  14846. (match-string 5 ans))0)))
  14847. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14848. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14849. t t ans)))
  14850. ;; Check if there is a time range
  14851. (when (boundp 'org-end-time-was-given)
  14852. (setq org-time-was-given nil)
  14853. (when (and (string-match org-plain-time-of-day-regexp ans)
  14854. (match-end 8))
  14855. (setq org-end-time-was-given (match-string 8 ans))
  14856. (setq ans (concat (substring ans 0 (match-beginning 7))
  14857. (substring ans (match-end 7))))))
  14858. (setq tl (parse-time-string ans)
  14859. day (or (nth 3 tl) (nth 3 org-defdecode))
  14860. month (or (nth 4 tl)
  14861. (if (and org-read-date-prefer-future
  14862. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14863. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14864. (nth 4 org-defdecode)))
  14865. year (or (and (not kill-year) (nth 5 tl))
  14866. (if (and org-read-date-prefer-future
  14867. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14868. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14869. (nth 5 org-defdecode)))
  14870. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14871. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14872. second (or (nth 0 tl) 0)
  14873. wday (nth 6 tl))
  14874. (when (and (eq org-read-date-prefer-future 'time)
  14875. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14876. (equal day (nth 3 nowdecode))
  14877. (equal month (nth 4 nowdecode))
  14878. (equal year (nth 5 nowdecode))
  14879. (nth 2 tl)
  14880. (or (< (nth 2 tl) (nth 2 nowdecode))
  14881. (and (= (nth 2 tl) (nth 2 nowdecode))
  14882. (nth 1 tl)
  14883. (< (nth 1 tl) (nth 1 nowdecode)))))
  14884. (setq day (1+ day)
  14885. futurep t))
  14886. ;; Special date definitions below
  14887. (cond
  14888. (iso-week
  14889. ;; There was an iso week
  14890. (require 'cal-iso)
  14891. (setq futurep nil)
  14892. (setq year (or iso-year year)
  14893. day (or iso-weekday wday 1)
  14894. wday nil ; to make sure that the trigger below does not match
  14895. iso-date (calendar-gregorian-from-absolute
  14896. (calendar-absolute-from-iso
  14897. (list iso-week day year))))
  14898. ; FIXME: Should we also push ISO weeks into the future?
  14899. ; (when (and org-read-date-prefer-future
  14900. ; (not iso-year)
  14901. ; (< (calendar-absolute-from-gregorian iso-date)
  14902. ; (time-to-days (current-time))))
  14903. ; (setq year (1+ year)
  14904. ; iso-date (calendar-gregorian-from-absolute
  14905. ; (calendar-absolute-from-iso
  14906. ; (list iso-week day year)))))
  14907. (setq month (car iso-date)
  14908. year (nth 2 iso-date)
  14909. day (nth 1 iso-date)))
  14910. (deltan
  14911. (setq futurep nil)
  14912. (unless deltadef
  14913. (let ((now (decode-time (current-time))))
  14914. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14915. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14916. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14917. ((equal deltaw "m") (setq month (+ month deltan)))
  14918. ((equal deltaw "y") (setq year (+ year deltan)))))
  14919. ((and wday (not (nth 3 tl)))
  14920. ;; Weekday was given, but no day, so pick that day in the week
  14921. ;; on or after the derived date.
  14922. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14923. (unless (equal wday wday1)
  14924. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14925. (if (and (boundp 'org-time-was-given)
  14926. (nth 2 tl))
  14927. (setq org-time-was-given t))
  14928. (if (< year 100) (setq year (+ 2000 year)))
  14929. ;; Check of the date is representable
  14930. (if org-read-date-force-compatible-dates
  14931. (progn
  14932. (if (< year 1970)
  14933. (setq year 1970 org-read-date-analyze-forced-year t))
  14934. (if (> year 2037)
  14935. (setq year 2037 org-read-date-analyze-forced-year t)))
  14936. (condition-case nil
  14937. (ignore (encode-time second minute hour day month year))
  14938. (error
  14939. (setq year (nth 5 org-defdecode))
  14940. (setq org-read-date-analyze-forced-year t))))
  14941. (setq org-read-date-analyze-futurep futurep)
  14942. (list second minute hour day month year)))
  14943. (defvar parse-time-weekdays)
  14944. (defun org-read-date-get-relative (s today default)
  14945. "Check string S for special relative date string.
  14946. TODAY and DEFAULT are internal times, for today and for a default.
  14947. Return shift list (N what def-flag)
  14948. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14949. N is the number of WHATs to shift.
  14950. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14951. the DEFAULT date rather than TODAY."
  14952. (require 'parse-time)
  14953. (when (and
  14954. (string-match
  14955. (concat
  14956. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14957. "\\([0-9]+\\)?"
  14958. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14959. "\\([ \t]\\|$\\)") s)
  14960. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14961. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14962. (string-to-char (substring (match-string 1 s) -1))
  14963. ?+))
  14964. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14965. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14966. (what (if (match-end 3) (match-string 3 s) "d"))
  14967. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14968. (date (if rel default today))
  14969. (wday (nth 6 (decode-time date)))
  14970. delta)
  14971. (if wday1
  14972. (progn
  14973. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14974. (if (= dir ?-) (setq delta (- delta 7)))
  14975. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14976. (list delta "d" rel))
  14977. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14978. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14979. "Turn a user-specified date into the internal representation.
  14980. The internal representation needed by the calendar is (month day year).
  14981. This is a wrapper to handle the brain-dead convention in calendar that
  14982. user function argument order change dependent on argument order."
  14983. (if (boundp 'calendar-date-style)
  14984. (cond
  14985. ((eq calendar-date-style 'american)
  14986. (list arg1 arg2 arg3))
  14987. ((eq calendar-date-style 'european)
  14988. (list arg2 arg1 arg3))
  14989. ((eq calendar-date-style 'iso)
  14990. (list arg2 arg3 arg1)))
  14991. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14992. (if (org-bound-and-true-p european-calendar-style)
  14993. (list arg2 arg1 arg3)
  14994. (list arg1 arg2 arg3)))))
  14995. (defun org-eval-in-calendar (form &optional keepdate)
  14996. "Eval FORM in the calendar window and return to current window.
  14997. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14998. otherwise stick to the current value of `org-ans2'."
  14999. (let ((sf (selected-frame))
  15000. (sw (selected-window)))
  15001. (select-window (get-buffer-window "*Calendar*" t))
  15002. (eval form)
  15003. (when (and (not keepdate) (calendar-cursor-to-date))
  15004. (let* ((date (calendar-cursor-to-date))
  15005. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15006. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15007. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15008. (select-window sw)
  15009. (org-select-frame-set-input-focus sf)))
  15010. (defun org-calendar-select ()
  15011. "Return to `org-read-date' with the date currently selected.
  15012. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15013. (interactive)
  15014. (when (calendar-cursor-to-date)
  15015. (let* ((date (calendar-cursor-to-date))
  15016. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15017. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15018. (if (active-minibuffer-window) (exit-minibuffer))))
  15019. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15020. "Insert a date stamp for the date given by the internal TIME.
  15021. WITH-HM means use the stamp format that includes the time of the day.
  15022. INACTIVE means use square brackets instead of angular ones, so that the
  15023. stamp will not contribute to the agenda.
  15024. PRE and POST are optional strings to be inserted before and after the
  15025. stamp.
  15026. The command returns the inserted time stamp."
  15027. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15028. stamp)
  15029. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15030. (insert-before-markers (or pre ""))
  15031. (when (listp extra)
  15032. (setq extra (car extra))
  15033. (if (and (stringp extra)
  15034. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15035. (setq extra (format "-%02d:%02d"
  15036. (string-to-number (match-string 1 extra))
  15037. (string-to-number (match-string 2 extra))))
  15038. (setq extra nil)))
  15039. (when extra
  15040. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15041. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15042. (insert-before-markers (or post ""))
  15043. (setq org-last-inserted-timestamp stamp)))
  15044. (defun org-toggle-time-stamp-overlays ()
  15045. "Toggle the use of custom time stamp formats."
  15046. (interactive)
  15047. (setq org-display-custom-times (not org-display-custom-times))
  15048. (unless org-display-custom-times
  15049. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15050. (while (setq p (next-single-property-change p 'display))
  15051. (if (and (get-text-property p 'display)
  15052. (eq (get-text-property p 'face) 'org-date))
  15053. (remove-text-properties
  15054. p (setq p (next-single-property-change p 'display))
  15055. '(display t))))
  15056. (set-buffer-modified-p bmp)))
  15057. (if (featurep 'xemacs)
  15058. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15059. (org-restart-font-lock)
  15060. (setq org-table-may-need-update t)
  15061. (if org-display-custom-times
  15062. (message "Time stamps are overlaid with custom format")
  15063. (message "Time stamp overlays removed")))
  15064. (defun org-display-custom-time (beg end)
  15065. "Overlay modified time stamp format over timestamp between BEG and END."
  15066. (let* ((ts (buffer-substring beg end))
  15067. t1 w1 with-hm tf time str w2 (off 0))
  15068. (save-match-data
  15069. (setq t1 (org-parse-time-string ts t))
  15070. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15071. (setq off (- (match-end 0) (match-beginning 0)))))
  15072. (setq end (- end off))
  15073. (setq w1 (- end beg)
  15074. with-hm (and (nth 1 t1) (nth 2 t1))
  15075. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15076. time (org-fix-decoded-time t1)
  15077. str (org-add-props
  15078. (format-time-string
  15079. (substring tf 1 -1) (apply 'encode-time time))
  15080. nil 'mouse-face 'highlight)
  15081. w2 (length str))
  15082. (if (not (= w2 w1))
  15083. (add-text-properties (1+ beg) (+ 2 beg)
  15084. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15085. (if (featurep 'xemacs)
  15086. (progn
  15087. (put-text-property beg end 'invisible t)
  15088. (put-text-property beg end 'end-glyph (make-glyph str)))
  15089. (put-text-property beg end 'display str))))
  15090. (defun org-translate-time (string)
  15091. "Translate all timestamps in STRING to custom format.
  15092. But do this only if the variable `org-display-custom-times' is set."
  15093. (when org-display-custom-times
  15094. (save-match-data
  15095. (let* ((start 0)
  15096. (re org-ts-regexp-both)
  15097. t1 with-hm inactive tf time str beg end)
  15098. (while (setq start (string-match re string start))
  15099. (setq beg (match-beginning 0)
  15100. end (match-end 0)
  15101. t1 (save-match-data
  15102. (org-parse-time-string (substring string beg end) t))
  15103. with-hm (and (nth 1 t1) (nth 2 t1))
  15104. inactive (equal (substring string beg (1+ beg)) "[")
  15105. tf (funcall (if with-hm 'cdr 'car)
  15106. org-time-stamp-custom-formats)
  15107. time (org-fix-decoded-time t1)
  15108. str (format-time-string
  15109. (concat
  15110. (if inactive "[" "<") (substring tf 1 -1)
  15111. (if inactive "]" ">"))
  15112. (apply 'encode-time time))
  15113. string (replace-match str t t string)
  15114. start (+ start (length str)))))))
  15115. string)
  15116. (defun org-fix-decoded-time (time)
  15117. "Set 0 instead of nil for the first 6 elements of time.
  15118. Don't touch the rest."
  15119. (let ((n 0))
  15120. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15121. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15122. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15123. "Difference between TIMESTAMP-STRING and now in days.
  15124. If SECONDS is non-nil, return the difference in seconds."
  15125. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15126. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15127. (funcall fdiff (current-time)))))
  15128. (defun org-deadline-close (timestamp-string &optional ndays)
  15129. "Is the time in TIMESTAMP-STRING close to the current date?"
  15130. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15131. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15132. (not (org-entry-is-done-p))))
  15133. (defun org-get-wdays (ts &optional delay zero-delay)
  15134. "Get the deadline lead time appropriate for timestring TS.
  15135. When DELAY is non-nil, get the delay time for scheduled items
  15136. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15137. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15138. don't try to find the delay cookie in the scheduled timestamp."
  15139. (let ((tv (if delay org-scheduled-delay-days
  15140. org-deadline-warning-days)))
  15141. (cond
  15142. ((or (and delay (< tv 0))
  15143. (and delay zero-delay (<= tv 0))
  15144. (and (not delay) (<= tv 0)))
  15145. ;; Enforce this value no matter what
  15146. (- tv))
  15147. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15148. ;; lead time is specified.
  15149. (floor (* (string-to-number (match-string 1 ts))
  15150. (cdr (assoc (match-string 2 ts)
  15151. '(("d" . 1) ("w" . 7)
  15152. ("m" . 30.4) ("y" . 365.25)
  15153. ("h" . 0.041667)))))))
  15154. ;; go for the default.
  15155. (t tv))))
  15156. (defun org-calendar-select-mouse (ev)
  15157. "Return to `org-read-date' with the date currently selected.
  15158. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15159. (interactive "e")
  15160. (mouse-set-point ev)
  15161. (when (calendar-cursor-to-date)
  15162. (let* ((date (calendar-cursor-to-date))
  15163. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15164. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15165. (if (active-minibuffer-window) (exit-minibuffer))))
  15166. (defun org-check-deadlines (ndays)
  15167. "Check if there are any deadlines due or past due.
  15168. A deadline is considered due if it happens within `org-deadline-warning-days'
  15169. days from today's date. If the deadline appears in an entry marked DONE,
  15170. it is not shown. The prefix arg NDAYS can be used to test that many
  15171. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15172. (interactive "P")
  15173. (let* ((org-warn-days
  15174. (cond
  15175. ((equal ndays '(4)) 100000)
  15176. (ndays (prefix-numeric-value ndays))
  15177. (t (abs org-deadline-warning-days))))
  15178. (case-fold-search nil)
  15179. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15180. (callback
  15181. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15182. (message "%d deadlines past-due or due within %d days"
  15183. (org-occur regexp nil callback)
  15184. org-warn-days)))
  15185. (defsubst org-re-timestamp (type)
  15186. "Return a regexp for timestamp TYPE.
  15187. Allowed values for TYPE are:
  15188. all: all timestamps
  15189. active: only active timestamps (<...>)
  15190. inactive: only inactive timestamps ([...])
  15191. scheduled: only scheduled timestamps
  15192. deadline: only deadline timestamps
  15193. When TYPE is nil, fall back on returning a regexp that matches
  15194. both scheduled and deadline timestamps."
  15195. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15196. ((eq type 'active) org-ts-regexp)
  15197. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15198. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15199. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15200. ((eq type 'scheduled-or-deadline)
  15201. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15202. (defun org-check-before-date (date)
  15203. "Check if there are deadlines or scheduled entries before DATE."
  15204. (interactive (list (org-read-date)))
  15205. (let ((case-fold-search nil)
  15206. (regexp (org-re-timestamp org-ts-type))
  15207. (callback
  15208. (lambda () (time-less-p
  15209. (org-time-string-to-time (match-string 1))
  15210. (org-time-string-to-time date)))))
  15211. (message "%d entries before %s"
  15212. (org-occur regexp nil callback) date)))
  15213. (defun org-check-after-date (date)
  15214. "Check if there are deadlines or scheduled entries after DATE."
  15215. (interactive (list (org-read-date)))
  15216. (let ((case-fold-search nil)
  15217. (regexp (org-re-timestamp org-ts-type))
  15218. (callback
  15219. (lambda () (not
  15220. (time-less-p
  15221. (org-time-string-to-time (match-string 1))
  15222. (org-time-string-to-time date))))))
  15223. (message "%d entries after %s"
  15224. (org-occur regexp nil callback) date)))
  15225. (defun org-check-dates-range (start-date end-date)
  15226. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15227. (interactive (list (org-read-date nil nil nil "Range starts")
  15228. (org-read-date nil nil nil "Range end")))
  15229. (let ((case-fold-search nil)
  15230. (regexp (org-re-timestamp org-ts-type))
  15231. (callback
  15232. (lambda ()
  15233. (let ((match (match-string 1)))
  15234. (and
  15235. (not (time-less-p
  15236. (org-time-string-to-time match)
  15237. (org-time-string-to-time start-date)))
  15238. (time-less-p
  15239. (org-time-string-to-time match)
  15240. (org-time-string-to-time end-date)))))))
  15241. (message "%d entries between %s and %s"
  15242. (org-occur regexp nil callback) start-date end-date)))
  15243. (defun org-evaluate-time-range (&optional to-buffer)
  15244. "Evaluate a time range by computing the difference between start and end.
  15245. Normally the result is just printed in the echo area, but with prefix arg
  15246. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15247. If the time range is actually in a table, the result is inserted into the
  15248. next column.
  15249. For time difference computation, a year is assumed to be exactly 365
  15250. days in order to avoid rounding problems."
  15251. (interactive "P")
  15252. (or
  15253. (org-clock-update-time-maybe)
  15254. (save-excursion
  15255. (unless (org-at-date-range-p t)
  15256. (goto-char (point-at-bol))
  15257. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15258. (if (not (org-at-date-range-p t))
  15259. (error "Not at a time-stamp range, and none found in current line")))
  15260. (let* ((ts1 (match-string 1))
  15261. (ts2 (match-string 2))
  15262. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15263. (match-end (match-end 0))
  15264. (time1 (org-time-string-to-time ts1))
  15265. (time2 (org-time-string-to-time ts2))
  15266. (t1 (org-float-time time1))
  15267. (t2 (org-float-time time2))
  15268. (diff (abs (- t2 t1)))
  15269. (negative (< (- t2 t1) 0))
  15270. ;; (ys (floor (* 365 24 60 60)))
  15271. (ds (* 24 60 60))
  15272. (hs (* 60 60))
  15273. (fy "%dy %dd %02d:%02d")
  15274. (fy1 "%dy %dd")
  15275. (fd "%dd %02d:%02d")
  15276. (fd1 "%dd")
  15277. (fh "%02d:%02d")
  15278. y d h m align)
  15279. (if havetime
  15280. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15281. y 0
  15282. d (floor (/ diff ds)) diff (mod diff ds)
  15283. h (floor (/ diff hs)) diff (mod diff hs)
  15284. m (floor (/ diff 60)))
  15285. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15286. y 0
  15287. d (floor (+ (/ diff ds) 0.5))
  15288. h 0 m 0))
  15289. (if (not to-buffer)
  15290. (message "%s" (org-make-tdiff-string y d h m))
  15291. (if (org-at-table-p)
  15292. (progn
  15293. (goto-char match-end)
  15294. (setq align t)
  15295. (and (looking-at " *|") (goto-char (match-end 0))))
  15296. (goto-char match-end))
  15297. (if (looking-at
  15298. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15299. (replace-match ""))
  15300. (if negative (insert " -"))
  15301. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15302. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15303. (insert " " (format fh h m))))
  15304. (if align (org-table-align))
  15305. (message "Time difference inserted")))))
  15306. (defun org-make-tdiff-string (y d h m)
  15307. (let ((fmt "")
  15308. (l nil))
  15309. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15310. l (push y l)))
  15311. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15312. l (push d l)))
  15313. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15314. l (push h l)))
  15315. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15316. l (push m l)))
  15317. (apply 'format fmt (nreverse l))))
  15318. (defun org-time-string-to-time (s &optional buffer pos)
  15319. "Convert a timestamp string into internal time."
  15320. (condition-case errdata
  15321. (apply 'encode-time (org-parse-time-string s))
  15322. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15323. s (if (not (and buffer pos))
  15324. ""
  15325. (format " at %d in buffer `%s'" pos buffer))
  15326. (cdr errdata)))))
  15327. (defun org-time-string-to-seconds (s)
  15328. "Convert a timestamp string to a number of seconds."
  15329. (org-float-time (org-time-string-to-time s)))
  15330. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15331. "Convert a time stamp to an absolute day number.
  15332. If there is a specifier for a cyclic time stamp, get the closest
  15333. date to DAYNR.
  15334. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15335. The variable `date' is bound by the calendar when this is called."
  15336. (cond
  15337. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15338. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15339. daynr
  15340. (+ daynr 1000)))
  15341. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15342. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15343. (time-to-days (current-time))) (match-string 0 s)
  15344. prefer show-all))
  15345. (t (time-to-days
  15346. (condition-case errdata
  15347. (apply 'encode-time (org-parse-time-string s))
  15348. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15349. s (if (not (and buffer pos))
  15350. ""
  15351. (format " at %d in buffer `%s'" pos buffer))
  15352. (cdr errdata))))))))
  15353. (defun org-days-to-iso-week (days)
  15354. "Return the iso week number."
  15355. (require 'cal-iso)
  15356. (car (calendar-iso-from-absolute days)))
  15357. (defun org-small-year-to-year (year)
  15358. "Convert 2-digit years into 4-digit years.
  15359. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15360. The year 2000 cannot be abbreviated. Any year larger than 99
  15361. is returned unchanged."
  15362. (if (< year 38)
  15363. (setq year (+ 2000 year))
  15364. (if (< year 100)
  15365. (setq year (+ 1900 year))))
  15366. year)
  15367. (defun org-time-from-absolute (d)
  15368. "Return the time corresponding to date D.
  15369. D may be an absolute day number, or a calendar-type list (month day year)."
  15370. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15371. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15372. (defun org-calendar-holiday ()
  15373. "List of holidays, for Diary display in Org-mode."
  15374. (require 'holidays)
  15375. (let ((hl (funcall
  15376. (if (fboundp 'calendar-check-holidays)
  15377. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15378. (if hl (mapconcat 'identity hl "; "))))
  15379. (defun org-diary-sexp-entry (sexp entry date)
  15380. "Process a SEXP diary ENTRY for DATE."
  15381. (require 'diary-lib)
  15382. (let ((result (if calendar-debug-sexp
  15383. (let ((stack-trace-on-error t))
  15384. (eval (car (read-from-string sexp))))
  15385. (condition-case nil
  15386. (eval (car (read-from-string sexp)))
  15387. (error
  15388. (beep)
  15389. (message "Bad sexp at line %d in %s: %s"
  15390. (org-current-line)
  15391. (buffer-file-name) sexp)
  15392. (sleep-for 2))))))
  15393. (cond ((stringp result) (split-string result "; "))
  15394. ((and (consp result)
  15395. (not (consp (cdr result)))
  15396. (stringp (cdr result))) (cdr result))
  15397. ((and (consp result)
  15398. (stringp (car result))) result)
  15399. (result entry))))
  15400. (defun org-diary-to-ical-string (frombuf)
  15401. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15402. This uses the icalendar.el library."
  15403. (let* ((tmpdir (if (featurep 'xemacs)
  15404. (temp-directory)
  15405. temporary-file-directory))
  15406. (tmpfile (make-temp-name
  15407. (expand-file-name "orgics" tmpdir)))
  15408. buf rtn b e)
  15409. (with-current-buffer frombuf
  15410. (icalendar-export-region (point-min) (point-max) tmpfile)
  15411. (setq buf (find-buffer-visiting tmpfile))
  15412. (set-buffer buf)
  15413. (goto-char (point-min))
  15414. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15415. (setq b (match-beginning 0)))
  15416. (goto-char (point-max))
  15417. (if (re-search-backward "^END:VEVENT" nil t)
  15418. (setq e (match-end 0)))
  15419. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15420. (kill-buffer buf)
  15421. (delete-file tmpfile)
  15422. rtn))
  15423. (defun org-closest-date (start current change prefer show-all)
  15424. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15425. When PREFER is `past', return a date that is either CURRENT or past.
  15426. When PREFER is `future', return a date that is either CURRENT or future.
  15427. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15428. ;; Make the proper lists from the dates
  15429. (catch 'exit
  15430. (let ((a1 '(("h" . hour)
  15431. ("d" . day)
  15432. ("w" . week)
  15433. ("m" . month)
  15434. ("y" . year)))
  15435. (shour (nth 2 (org-parse-time-string start)))
  15436. dn dw sday cday n1 n2 n0
  15437. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15438. (setq start (org-date-to-gregorian start)
  15439. current (org-date-to-gregorian
  15440. (if show-all
  15441. current
  15442. (time-to-days (current-time))))
  15443. sday (calendar-absolute-from-gregorian start)
  15444. cday (calendar-absolute-from-gregorian current))
  15445. (if (<= cday sday) (throw 'exit sday))
  15446. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15447. (setq dn (string-to-number (match-string 1 change))
  15448. dw (cdr (assoc (match-string 2 change) a1)))
  15449. (error "Invalid change specifier: %s" change))
  15450. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15451. (cond
  15452. ((eq dw 'hour)
  15453. (let ((missing-hours
  15454. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15455. dn)))
  15456. (setq n1 (if (zerop missing-hours) cday
  15457. (- cday (1+ (floor (/ missing-hours 24)))))
  15458. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15459. ((eq dw 'day)
  15460. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15461. n2 (+ n1 dn)))
  15462. ((eq dw 'year)
  15463. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15464. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15465. (setq date1 (list m d y1)
  15466. n1 (calendar-absolute-from-gregorian date1)
  15467. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15468. n2 (calendar-absolute-from-gregorian date2)))
  15469. ((eq dw 'month)
  15470. ;; approx number of month between the two dates
  15471. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15472. ;; How often does dn fit in there?
  15473. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15474. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15475. m (+ m nm)
  15476. ny (floor (/ m 12))
  15477. y (+ y ny)
  15478. m (- m (* ny 12)))
  15479. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15480. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15481. (setq m2 (+ m dn) y2 y)
  15482. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15483. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15484. (while (<= n2 cday)
  15485. (setq n1 n2 m m2 y y2)
  15486. (setq m2 (+ m dn) y2 y)
  15487. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15488. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15489. ;; Make sure n1 is the earlier date
  15490. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15491. (if show-all
  15492. (cond
  15493. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15494. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15495. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15496. (cond
  15497. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15498. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15499. (t (if (= cday n1) n1 n2)))))))
  15500. (defun org-date-to-gregorian (date)
  15501. "Turn any specification of DATE into a Gregorian date for the calendar."
  15502. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15503. ((and (listp date) (= (length date) 3)) date)
  15504. ((stringp date)
  15505. (setq date (org-parse-time-string date))
  15506. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15507. ((listp date)
  15508. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15509. (defun org-parse-time-string (s &optional nodefault)
  15510. "Parse the standard Org-mode time string.
  15511. This should be a lot faster than the normal `parse-time-string'.
  15512. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15513. hour and minute fields will be nil if not given."
  15514. (cond ((string-match org-ts-regexp0 s)
  15515. (list 0
  15516. (if (or (match-beginning 8) (not nodefault))
  15517. (string-to-number (or (match-string 8 s) "0")))
  15518. (if (or (match-beginning 7) (not nodefault))
  15519. (string-to-number (or (match-string 7 s) "0")))
  15520. (string-to-number (match-string 4 s))
  15521. (string-to-number (match-string 3 s))
  15522. (string-to-number (match-string 2 s))
  15523. nil nil nil))
  15524. ((string-match "^<[^>]+>$" s)
  15525. (decode-time (seconds-to-time (org-matcher-time s))))
  15526. (t (error "Not a standard Org-mode time string: %s" s))))
  15527. (defun org-timestamp-up (&optional arg)
  15528. "Increase the date item at the cursor by one.
  15529. If the cursor is on the year, change the year. If it is on the month,
  15530. the day or the time, change that.
  15531. With prefix ARG, change by that many units."
  15532. (interactive "p")
  15533. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15534. (defun org-timestamp-down (&optional arg)
  15535. "Decrease the date item at the cursor by one.
  15536. If the cursor is on the year, change the year. If it is on the month,
  15537. the day or the time, change that.
  15538. With prefix ARG, change by that many units."
  15539. (interactive "p")
  15540. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15541. (defun org-timestamp-up-day (&optional arg)
  15542. "Increase the date in the time stamp by one day.
  15543. With prefix ARG, change that many days."
  15544. (interactive "p")
  15545. (if (and (not (org-at-timestamp-p t))
  15546. (org-at-heading-p))
  15547. (org-todo 'up)
  15548. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15549. (defun org-timestamp-down-day (&optional arg)
  15550. "Decrease the date in the time stamp by one day.
  15551. With prefix ARG, change that many days."
  15552. (interactive "p")
  15553. (if (and (not (org-at-timestamp-p t))
  15554. (org-at-heading-p))
  15555. (org-todo 'down)
  15556. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15557. (defun org-at-timestamp-p (&optional inactive-ok)
  15558. "Determine if the cursor is in or at a timestamp."
  15559. (interactive)
  15560. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15561. (pos (point))
  15562. (ans (or (looking-at tsr)
  15563. (save-excursion
  15564. (skip-chars-backward "^[<\n\r\t")
  15565. (if (> (point) (point-min)) (backward-char 1))
  15566. (and (looking-at tsr)
  15567. (> (- (match-end 0) pos) -1))))))
  15568. (and ans
  15569. (boundp 'org-ts-what)
  15570. (setq org-ts-what
  15571. (cond
  15572. ((= pos (match-beginning 0)) 'bracket)
  15573. ;; Point is considered to be "on the bracket" whether
  15574. ;; it's really on it or right after it.
  15575. ((= pos (1- (match-end 0))) 'bracket)
  15576. ((= pos (match-end 0)) 'after)
  15577. ((org-pos-in-match-range pos 2) 'year)
  15578. ((org-pos-in-match-range pos 3) 'month)
  15579. ((org-pos-in-match-range pos 7) 'hour)
  15580. ((org-pos-in-match-range pos 8) 'minute)
  15581. ((or (org-pos-in-match-range pos 4)
  15582. (org-pos-in-match-range pos 5)) 'day)
  15583. ((and (> pos (or (match-end 8) (match-end 5)))
  15584. (< pos (match-end 0)))
  15585. (- pos (or (match-end 8) (match-end 5))))
  15586. (t 'day))))
  15587. ans))
  15588. (defun org-toggle-timestamp-type ()
  15589. "Toggle the type (<active> or [inactive]) of a time stamp."
  15590. (interactive)
  15591. (when (org-at-timestamp-p t)
  15592. (let ((beg (match-beginning 0)) (end (match-end 0))
  15593. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15594. (save-excursion
  15595. (goto-char beg)
  15596. (while (re-search-forward "[][<>]" end t)
  15597. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15598. t t)))
  15599. (message "Timestamp is now %sactive"
  15600. (if (equal (char-after beg) ?<) "" "in")))))
  15601. (defun org-at-clock-log-p nil
  15602. "Is the cursor on the clock log line?"
  15603. (save-excursion
  15604. (move-beginning-of-line 1)
  15605. (looking-at "^[ \t]*CLOCK:")))
  15606. (defvar org-clock-history) ; defined in org-clock.el
  15607. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15608. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15609. "Change the date in the time stamp at point.
  15610. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15611. `year', `minute', `second'. If WHAT is not given, the cursor position
  15612. in the timestamp determines what will be changed.
  15613. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15614. (let ((origin (point)) origin-cat
  15615. with-hm inactive
  15616. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15617. org-ts-what
  15618. extra rem
  15619. ts time time0 fixnext clrgx)
  15620. (if (not (org-at-timestamp-p t))
  15621. (error "Not at a timestamp"))
  15622. (if (and (not what) (eq org-ts-what 'bracket))
  15623. (org-toggle-timestamp-type)
  15624. ;; Point isn't on brackets. Remember the part of the time-stamp
  15625. ;; the point was in. Indeed, size of time-stamps may change,
  15626. ;; but point must be kept in the same category nonetheless.
  15627. (setq origin-cat org-ts-what)
  15628. (if (and (not what) (not (eq org-ts-what 'day))
  15629. org-display-custom-times
  15630. (get-text-property (point) 'display)
  15631. (not (get-text-property (1- (point)) 'display)))
  15632. (setq org-ts-what 'day))
  15633. (setq org-ts-what (or what org-ts-what)
  15634. inactive (= (char-after (match-beginning 0)) ?\[)
  15635. ts (match-string 0))
  15636. (replace-match "")
  15637. (when (string-match
  15638. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15639. ts)
  15640. (setq extra (match-string 1 ts))
  15641. (if suppress-tmp-delay
  15642. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15643. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15644. (setq with-hm t))
  15645. (setq time0 (org-parse-time-string ts))
  15646. (when (and updown
  15647. (eq org-ts-what 'minute)
  15648. (not current-prefix-arg))
  15649. ;; This looks like s-up and s-down. Change by one rounding step.
  15650. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15651. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15652. (setcar (cdr time0) (+ (nth 1 time0)
  15653. (if (> n 0) (- rem) (- dm rem))))))
  15654. (setq time
  15655. (encode-time (or (car time0) 0)
  15656. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15657. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15658. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15659. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15660. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15661. (nthcdr 6 time0)))
  15662. (when (and (member org-ts-what '(hour minute))
  15663. extra
  15664. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15665. (setq extra (org-modify-ts-extra
  15666. extra
  15667. (if (eq org-ts-what 'hour) 2 5)
  15668. n dm)))
  15669. (when (integerp org-ts-what)
  15670. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15671. (if (eq what 'calendar)
  15672. (let ((cal-date (org-get-date-from-calendar)))
  15673. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15674. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15675. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15676. (setcar time0 (or (car time0) 0))
  15677. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15678. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15679. (setq time (apply 'encode-time time0))))
  15680. ;; Insert the new time-stamp, and ensure point stays in the same
  15681. ;; category as before (i.e. not after the last position in that
  15682. ;; category).
  15683. (let ((pos (point)))
  15684. ;; Stay before inserted string. `save-excursion' is of no use.
  15685. (setq org-last-changed-timestamp
  15686. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15687. (goto-char pos))
  15688. (save-match-data
  15689. (looking-at org-ts-regexp3)
  15690. (goto-char (cond
  15691. ;; `day' category ends before `hour' if any, or at
  15692. ;; the end of the day name.
  15693. ((eq origin-cat 'day)
  15694. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15695. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15696. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15697. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15698. ;; `year' and `month' have both fixed size: point
  15699. ;; couldn't have moved into another part.
  15700. (t origin))))
  15701. ;; Update clock if on a CLOCK line.
  15702. (org-clock-update-time-maybe)
  15703. ;; Maybe adjust the closest clock in `org-clock-history'
  15704. (when org-clock-adjust-closest
  15705. (if (not (and (org-at-clock-log-p)
  15706. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15707. org-clock-history))))))
  15708. (message "No clock to adjust")
  15709. (cond ((save-excursion ; fix previous clock?
  15710. (re-search-backward org-ts-regexp0 nil t)
  15711. (org-looking-back (concat org-clock-string " \\[")))
  15712. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15713. ((save-excursion ; fix next clock?
  15714. (re-search-backward org-ts-regexp0 nil t)
  15715. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15716. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15717. (save-window-excursion
  15718. ;; Find closest clock to point, adjust the previous/next one in history
  15719. (let* ((p (save-excursion (org-back-to-heading t)))
  15720. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15721. (clfixnth
  15722. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15723. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15724. (if (not clfixpos)
  15725. (message "No clock to adjust")
  15726. (save-excursion
  15727. (org-goto-marker-or-bmk clfixpos)
  15728. (org-show-subtree)
  15729. (when (re-search-forward clrgx nil t)
  15730. (goto-char (match-beginning 1))
  15731. (let (org-clock-adjust-closest)
  15732. (org-timestamp-change n org-ts-what updown))
  15733. (message "Clock adjusted in %s for heading: %s"
  15734. (file-name-nondirectory (buffer-file-name))
  15735. (org-get-heading t t)))))))))
  15736. ;; Try to recenter the calendar window, if any.
  15737. (if (and org-calendar-follow-timestamp-change
  15738. (get-buffer-window "*Calendar*" t)
  15739. (memq org-ts-what '(day month year)))
  15740. (org-recenter-calendar (time-to-days time))))))
  15741. (defun org-modify-ts-extra (s pos n dm)
  15742. "Change the different parts of the lead-time and repeat fields in timestamp."
  15743. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15744. ng h m new rem)
  15745. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15746. (cond
  15747. ((or (org-pos-in-match-range pos 2)
  15748. (org-pos-in-match-range pos 3))
  15749. (setq m (string-to-number (match-string 3 s))
  15750. h (string-to-number (match-string 2 s)))
  15751. (if (org-pos-in-match-range pos 2)
  15752. (setq h (+ h n))
  15753. (setq n (* dm (org-no-warnings (signum n))))
  15754. (when (not (= 0 (setq rem (% m dm))))
  15755. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15756. (setq m (+ m n)))
  15757. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15758. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15759. (setq h (min 24 (max 0 h)))
  15760. (setq ng 1 new (format "-%02d:%02d" h m)))
  15761. ((org-pos-in-match-range pos 6)
  15762. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15763. ((org-pos-in-match-range pos 5)
  15764. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15765. ((org-pos-in-match-range pos 9)
  15766. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15767. ((org-pos-in-match-range pos 8)
  15768. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15769. (when ng
  15770. (setq s (concat
  15771. (substring s 0 (match-beginning ng))
  15772. new
  15773. (substring s (match-end ng))))))
  15774. s))
  15775. (defun org-recenter-calendar (date)
  15776. "If the calendar is visible, recenter it to DATE."
  15777. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15778. (when cwin
  15779. (let ((calendar-move-hook nil))
  15780. (with-selected-window cwin
  15781. (calendar-goto-date (if (listp date) date
  15782. (calendar-gregorian-from-absolute date))))))))
  15783. (defun org-goto-calendar (&optional arg)
  15784. "Go to the Emacs calendar at the current date.
  15785. If there is a time stamp in the current line, go to that date.
  15786. A prefix ARG can be used to force the current date."
  15787. (interactive "P")
  15788. (let ((tsr org-ts-regexp) diff
  15789. (calendar-move-hook nil)
  15790. (calendar-view-holidays-initially-flag nil)
  15791. (calendar-view-diary-initially-flag nil))
  15792. (if (or (org-at-timestamp-p)
  15793. (save-excursion
  15794. (beginning-of-line 1)
  15795. (looking-at (concat ".*" tsr))))
  15796. (let ((d1 (time-to-days (current-time)))
  15797. (d2 (time-to-days
  15798. (org-time-string-to-time (match-string 1)))))
  15799. (setq diff (- d2 d1))))
  15800. (calendar)
  15801. (calendar-goto-today)
  15802. (if (and diff (not arg)) (calendar-forward-day diff))))
  15803. (defun org-get-date-from-calendar ()
  15804. "Return a list (month day year) of date at point in calendar."
  15805. (with-current-buffer "*Calendar*"
  15806. (save-match-data
  15807. (calendar-cursor-to-date))))
  15808. (defun org-date-from-calendar ()
  15809. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15810. If there is already a time stamp at the cursor position, update it."
  15811. (interactive)
  15812. (if (org-at-timestamp-p t)
  15813. (org-timestamp-change 0 'calendar)
  15814. (let ((cal-date (org-get-date-from-calendar)))
  15815. (org-insert-time-stamp
  15816. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15817. (defcustom org-effort-durations
  15818. `(("h" . 60)
  15819. ("d" . ,(* 60 8))
  15820. ("w" . ,(* 60 8 5))
  15821. ("m" . ,(* 60 8 5 4))
  15822. ("y" . ,(* 60 8 5 40)))
  15823. "Conversion factor to minutes for an effort modifier.
  15824. Each entry has the form (MODIFIER . MINUTES).
  15825. In an effort string, a number followed by MODIFIER is multiplied
  15826. by the specified number of MINUTES to obtain an effort in
  15827. minutes.
  15828. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15829. effort string \"2hours\" is equivalent to 120 minutes."
  15830. :group 'org-agenda
  15831. :version "24.1"
  15832. :type '(alist :key-type (string :tag "Modifier")
  15833. :value-type (number :tag "Minutes")))
  15834. (defun org-minutes-to-clocksum-string (m)
  15835. "Format number of minutes as a clocksum string.
  15836. The format is determined by `org-time-clocksum-format',
  15837. `org-time-clocksum-use-fractional' and
  15838. `org-time-clocksum-fractional-format' and
  15839. `org-time-clocksum-use-effort-durations'."
  15840. (let ((clocksum "") h d w mo y fmt n)
  15841. (setq h (if org-time-clocksum-use-effort-durations
  15842. (cdr (assoc "h" org-effort-durations)) 60)
  15843. d (if org-time-clocksum-use-effort-durations
  15844. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15845. w (if org-time-clocksum-use-effort-durations
  15846. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15847. mo (if org-time-clocksum-use-effort-durations
  15848. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15849. y (if org-time-clocksum-use-effort-durations
  15850. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15851. ;; fractional format
  15852. (if org-time-clocksum-use-fractional
  15853. (cond
  15854. ;; single format string
  15855. ((stringp org-time-clocksum-fractional-format)
  15856. (format org-time-clocksum-fractional-format (/ m (float h))))
  15857. ;; choice of fractional formats for different time units
  15858. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15859. (> (/ (truncate m) (* y d h)) 0))
  15860. (format fmt (/ m (* y d (float h)))))
  15861. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15862. (> (/ (truncate m) (* mo d h)) 0))
  15863. (format fmt (/ m (* mo d (float h)))))
  15864. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15865. (> (/ (truncate m) (* w d h)) 0))
  15866. (format fmt (/ m (* w d (float h)))))
  15867. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15868. (> (/ (truncate m) (* d h)) 0))
  15869. (format fmt (/ m (* d (float h)))))
  15870. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15871. (> (/ (truncate m) h) 0))
  15872. (format fmt (/ m (float h))))
  15873. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15874. (format fmt m))
  15875. ;; fall back to smallest time unit with a format
  15876. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15877. (format fmt (/ m (float h))))
  15878. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15879. (format fmt (/ m (* d (float h)))))
  15880. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15881. (format fmt (/ m (* w d (float h)))))
  15882. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15883. (format fmt (/ m (* mo d (float h)))))
  15884. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15885. (format fmt (/ m (* y d (float h))))))
  15886. ;; standard (non-fractional) format, with single format string
  15887. (if (stringp org-time-clocksum-format)
  15888. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15889. ;; separate formats components
  15890. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15891. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15892. (plist-get org-time-clocksum-format :require-years))
  15893. (setq clocksum (concat clocksum (format fmt n))
  15894. m (- m (* n y d h))))
  15895. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15896. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15897. (plist-get org-time-clocksum-format :require-months))
  15898. (setq clocksum (concat clocksum (format fmt n))
  15899. m (- m (* n mo d h))))
  15900. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15901. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15902. (plist-get org-time-clocksum-format :require-weeks))
  15903. (setq clocksum (concat clocksum (format fmt n))
  15904. m (- m (* n w d h))))
  15905. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15906. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15907. (plist-get org-time-clocksum-format :require-days))
  15908. (setq clocksum (concat clocksum (format fmt n))
  15909. m (- m (* n d h))))
  15910. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15911. (or (> (setq n (/ (truncate m) h)) 0)
  15912. (plist-get org-time-clocksum-format :require-hours))
  15913. (setq clocksum (concat clocksum (format fmt n))
  15914. m (- m (* n h))))
  15915. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15916. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15917. (setq clocksum (concat clocksum (format fmt m))))
  15918. ;; return formatted time duration
  15919. clocksum))))
  15920. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15921. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15922. "Org mode version 8.0")
  15923. (defun org-hours-to-clocksum-string (n)
  15924. (org-minutes-to-clocksum-string (* n 60)))
  15925. (defun org-hh:mm-string-to-minutes (s)
  15926. "Convert a string H:MM to a number of minutes.
  15927. If the string is just a number, interpret it as minutes.
  15928. In fact, the first hh:mm or number in the string will be taken,
  15929. there can be extra stuff in the string.
  15930. If no number is found, the return value is 0."
  15931. (cond
  15932. ((integerp s) s)
  15933. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15934. (+ (* (string-to-number (match-string 1 s)) 60)
  15935. (string-to-number (match-string 2 s))))
  15936. ((string-match "\\([0-9]+\\)" s)
  15937. (string-to-number (match-string 1 s)))
  15938. (t 0)))
  15939. (defcustom org-image-actual-width t
  15940. "Should we use the actual width of images when inlining them?
  15941. When set to `t', always use the image width.
  15942. When set to a number, use imagemagick (when available) to set
  15943. the image's width to this value.
  15944. When set to a number in a list, try to get the width from the
  15945. #+ATTR.* keyword if it matches a width specification like
  15946. width=\"[0-9]+\", and fall back on that number if none is found.
  15947. When set to nil, try to get the width from an #+ATTR.* keyword
  15948. and fall back on the original width if none is found.
  15949. This requires Emacs >= 24.1, build with imagemagick support."
  15950. :group 'org-appearance
  15951. :version "24.4"
  15952. :package-version '(Org . "8.0")
  15953. :type '(choice
  15954. (const :tag "Use the image width" t)
  15955. (integer :tag "Use a number of pixels")
  15956. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15957. (const :tag "Use #+ATTR* or don't resize" nil)))
  15958. (defcustom org-agenda-inhibit-startup nil
  15959. "Inhibit startup when preparing agenda buffers.
  15960. When this variable is `t' (the default), the initialization of
  15961. the Org agenda buffers is inhibited: e.g. the visibility state
  15962. is not set, the tables are not re-aligned, etc."
  15963. :type 'boolean
  15964. :version "24.3"
  15965. :group 'org-agenda)
  15966. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15967. "Convert a duration string S to minutes.
  15968. A bare number is interpreted as minutes, modifiers can be set by
  15969. customizing `org-effort-durations' (which see).
  15970. Entries containing a colon are interpreted as H:MM by
  15971. `org-hh:mm-string-to-minutes'."
  15972. (let ((result 0)
  15973. (re (concat "\\([0-9.]+\\) *\\("
  15974. (regexp-opt (mapcar 'car org-effort-durations))
  15975. "\\)")))
  15976. (while (string-match re s)
  15977. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15978. (string-to-number (match-string 1 s))))
  15979. (setq s (replace-match "" nil t s)))
  15980. (setq result (floor result))
  15981. (incf result (org-hh:mm-string-to-minutes s))
  15982. (if output-to-string (number-to-string result) result)))
  15983. ;;;; Files
  15984. (defun org-save-all-org-buffers ()
  15985. "Save all Org-mode buffers without user confirmation."
  15986. (interactive)
  15987. (message "Saving all Org-mode buffers...")
  15988. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15989. (when (featurep 'org-id) (org-id-locations-save))
  15990. (message "Saving all Org-mode buffers... done"))
  15991. (defun org-revert-all-org-buffers ()
  15992. "Revert all Org-mode buffers.
  15993. Prompt for confirmation when there are unsaved changes.
  15994. Be sure you know what you are doing before letting this function
  15995. overwrite your changes.
  15996. This function is useful in a setup where one tracks org files
  15997. with a version control system, to revert on one machine after pulling
  15998. changes from another. I believe the procedure must be like this:
  15999. 1. M-x org-save-all-org-buffers
  16000. 2. Pull changes from the other machine, resolve conflicts
  16001. 3. M-x org-revert-all-org-buffers"
  16002. (interactive)
  16003. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16004. (error "Abort"))
  16005. (save-excursion
  16006. (save-window-excursion
  16007. (mapc
  16008. (lambda (b)
  16009. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16010. (with-current-buffer b buffer-file-name))
  16011. (org-pop-to-buffer-same-window b)
  16012. (revert-buffer t 'no-confirm)))
  16013. (buffer-list))
  16014. (when (and (featurep 'org-id) org-id-track-globally)
  16015. (org-id-locations-load)))))
  16016. ;;;; Agenda files
  16017. ;;;###autoload
  16018. (defun org-switchb (&optional arg)
  16019. "Switch between Org buffers.
  16020. With one prefix argument, restrict available buffers to files.
  16021. With two prefix arguments, restrict available buffers to agenda files.
  16022. Defaults to `iswitchb' for buffer name completion.
  16023. Set `org-completion-use-ido' to make it use ido instead."
  16024. (interactive "P")
  16025. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16026. ((equal arg '(16)) (org-buffer-list 'agenda))
  16027. (t (org-buffer-list))))
  16028. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16029. (org-completion-use-ido org-completion-use-ido))
  16030. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16031. (setq org-completion-use-iswitchb t))
  16032. (org-pop-to-buffer-same-window
  16033. (org-icompleting-read "Org buffer: "
  16034. (mapcar 'list (mapcar 'buffer-name blist))
  16035. nil t))))
  16036. ;;; Define some older names previously used for this functionality
  16037. ;;;###autoload
  16038. (defalias 'org-ido-switchb 'org-switchb)
  16039. ;;;###autoload
  16040. (defalias 'org-iswitchb 'org-switchb)
  16041. (defun org-buffer-list (&optional predicate exclude-tmp)
  16042. "Return a list of Org buffers.
  16043. PREDICATE can be `export', `files' or `agenda'.
  16044. export restrict the list to Export buffers.
  16045. files restrict the list to buffers visiting Org files.
  16046. agenda restrict the list to buffers visiting agenda files.
  16047. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16048. (let* ((bfn nil)
  16049. (agenda-files (and (eq predicate 'agenda)
  16050. (mapcar 'file-truename (org-agenda-files t))))
  16051. (filter
  16052. (cond
  16053. ((eq predicate 'files)
  16054. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16055. ((eq predicate 'export)
  16056. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16057. ((eq predicate 'agenda)
  16058. (lambda (b)
  16059. (with-current-buffer b
  16060. (and (derived-mode-p 'org-mode)
  16061. (setq bfn (buffer-file-name b))
  16062. (member (file-truename bfn) agenda-files)))))
  16063. (t (lambda (b) (with-current-buffer b
  16064. (or (derived-mode-p 'org-mode)
  16065. (string-match "\*Org .*Export"
  16066. (buffer-name b)))))))))
  16067. (delq nil
  16068. (mapcar
  16069. (lambda(b)
  16070. (if (and (funcall filter b)
  16071. (or (not exclude-tmp)
  16072. (not (string-match "tmp" (buffer-name b)))))
  16073. b
  16074. nil))
  16075. (buffer-list)))))
  16076. (defun org-agenda-files (&optional unrestricted archives)
  16077. "Get the list of agenda files.
  16078. Optional UNRESTRICTED means return the full list even if a restriction
  16079. is currently in place.
  16080. When ARCHIVES is t, include all archive files that are really being
  16081. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16082. `org-agenda-archives-mode' is t."
  16083. (let ((files
  16084. (cond
  16085. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16086. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16087. ((listp org-agenda-files) org-agenda-files)
  16088. (t (error "Invalid value of `org-agenda-files'")))))
  16089. (setq files (apply 'append
  16090. (mapcar (lambda (f)
  16091. (if (file-directory-p f)
  16092. (directory-files
  16093. f t org-agenda-file-regexp)
  16094. (list f)))
  16095. files)))
  16096. (when org-agenda-skip-unavailable-files
  16097. (setq files (delq nil
  16098. (mapcar (function
  16099. (lambda (file)
  16100. (and (file-readable-p file) file)))
  16101. files))))
  16102. (when (or (eq archives t)
  16103. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16104. (setq files (org-add-archive-files files)))
  16105. files))
  16106. (defun org-agenda-file-p (&optional file)
  16107. "Return non-nil, if FILE is an agenda file.
  16108. If FILE is omitted, use the file associated with the current
  16109. buffer."
  16110. (member (or file (buffer-file-name))
  16111. (org-agenda-files t)))
  16112. (defun org-edit-agenda-file-list ()
  16113. "Edit the list of agenda files.
  16114. Depending on setup, this either uses customize to edit the variable
  16115. `org-agenda-files', or it visits the file that is holding the list. In the
  16116. latter case, the buffer is set up in a way that saving it automatically kills
  16117. the buffer and restores the previous window configuration."
  16118. (interactive)
  16119. (if (stringp org-agenda-files)
  16120. (let ((cw (current-window-configuration)))
  16121. (find-file org-agenda-files)
  16122. (org-set-local 'org-window-configuration cw)
  16123. (org-add-hook 'after-save-hook
  16124. (lambda ()
  16125. (set-window-configuration
  16126. (prog1 org-window-configuration
  16127. (kill-buffer (current-buffer))))
  16128. (org-install-agenda-files-menu)
  16129. (message "New agenda file list installed"))
  16130. nil 'local)
  16131. (message "%s" (substitute-command-keys
  16132. "Edit list and finish with \\[save-buffer]")))
  16133. (customize-variable 'org-agenda-files)))
  16134. (defun org-store-new-agenda-file-list (list)
  16135. "Set new value for the agenda file list and save it correctly."
  16136. (if (stringp org-agenda-files)
  16137. (let ((fe (org-read-agenda-file-list t)) b u)
  16138. (while (setq b (find-buffer-visiting org-agenda-files))
  16139. (kill-buffer b))
  16140. (with-temp-file org-agenda-files
  16141. (insert
  16142. (mapconcat
  16143. (lambda (f) ;; Keep un-expanded entries.
  16144. (if (setq u (assoc f fe))
  16145. (cdr u)
  16146. f))
  16147. list "\n")
  16148. "\n")))
  16149. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16150. (org-insert-mode-line-in-empty-file nil))
  16151. (setq org-agenda-files list)
  16152. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16153. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16154. "Read the list of agenda files from a file.
  16155. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16156. filenames, used by `org-store-new-agenda-file-list' to write back
  16157. un-expanded file names."
  16158. (when (file-directory-p org-agenda-files)
  16159. (error "`org-agenda-files' cannot be a single directory"))
  16160. (when (stringp org-agenda-files)
  16161. (with-temp-buffer
  16162. (insert-file-contents org-agenda-files)
  16163. (mapcar
  16164. (lambda (f)
  16165. (let ((e (expand-file-name (substitute-in-file-name f)
  16166. org-directory)))
  16167. (if pair-with-expansion
  16168. (cons e f)
  16169. e)))
  16170. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16171. ;;;###autoload
  16172. (defun org-cycle-agenda-files ()
  16173. "Cycle through the files in `org-agenda-files'.
  16174. If the current buffer visits an agenda file, find the next one in the list.
  16175. If the current buffer does not, find the first agenda file."
  16176. (interactive)
  16177. (let* ((fs (org-agenda-files t))
  16178. (files (append fs (list (car fs))))
  16179. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16180. file)
  16181. (unless files (error "No agenda files"))
  16182. (catch 'exit
  16183. (while (setq file (pop files))
  16184. (if (equal (file-truename file) tcf)
  16185. (when (car files)
  16186. (find-file (car files))
  16187. (throw 'exit t))))
  16188. (find-file (car fs)))
  16189. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16190. (defun org-agenda-file-to-front (&optional to-end)
  16191. "Move/add the current file to the top of the agenda file list.
  16192. If the file is not present in the list, it is added to the front. If it is
  16193. present, it is moved there. With optional argument TO-END, add/move to the
  16194. end of the list."
  16195. (interactive "P")
  16196. (let ((org-agenda-skip-unavailable-files nil)
  16197. (file-alist (mapcar (lambda (x)
  16198. (cons (file-truename x) x))
  16199. (org-agenda-files t)))
  16200. (ctf (file-truename
  16201. (or buffer-file-name
  16202. (error "Please save the current buffer to a file"))))
  16203. x had)
  16204. (setq x (assoc ctf file-alist) had x)
  16205. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16206. (if to-end
  16207. (setq file-alist (append (delq x file-alist) (list x)))
  16208. (setq file-alist (cons x (delq x file-alist))))
  16209. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16210. (org-install-agenda-files-menu)
  16211. (message "File %s to %s of agenda file list"
  16212. (if had "moved" "added") (if to-end "end" "front"))))
  16213. (defun org-remove-file (&optional file)
  16214. "Remove current file from the list of files in variable `org-agenda-files'.
  16215. These are the files which are being checked for agenda entries.
  16216. Optional argument FILE means use this file instead of the current."
  16217. (interactive)
  16218. (let* ((org-agenda-skip-unavailable-files nil)
  16219. (file (or file buffer-file-name
  16220. (error "Current buffer does not visit a file")))
  16221. (true-file (file-truename file))
  16222. (afile (abbreviate-file-name file))
  16223. (files (delq nil (mapcar
  16224. (lambda (x)
  16225. (if (equal true-file
  16226. (file-truename x))
  16227. nil x))
  16228. (org-agenda-files t)))))
  16229. (if (not (= (length files) (length (org-agenda-files t))))
  16230. (progn
  16231. (org-store-new-agenda-file-list files)
  16232. (org-install-agenda-files-menu)
  16233. (message "Removed file: %s" afile))
  16234. (message "File was not in list: %s (not removed)" afile))))
  16235. (defun org-file-menu-entry (file)
  16236. (vector file (list 'find-file file) t))
  16237. (defun org-check-agenda-file (file)
  16238. "Make sure FILE exists. If not, ask user what to do."
  16239. (when (not (file-exists-p file))
  16240. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16241. (abbreviate-file-name file))
  16242. (let ((r (downcase (read-char-exclusive))))
  16243. (cond
  16244. ((equal r ?r)
  16245. (org-remove-file file)
  16246. (throw 'nextfile t))
  16247. (t (error "Abort"))))))
  16248. (defun org-get-agenda-file-buffer (file)
  16249. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16250. it to the list of buffers which might be released later."
  16251. (let ((buf (org-find-base-buffer-visiting file)))
  16252. (if buf
  16253. buf ; just return it
  16254. ;; Make a new buffer and remember it
  16255. (setq buf (find-file-noselect file))
  16256. (if buf (push buf org-agenda-new-buffers))
  16257. buf)))
  16258. (defun org-release-buffers (blist)
  16259. "Release all buffers in list, asking the user for confirmation when needed.
  16260. When a buffer is unmodified, it is just killed. When modified, it is saved
  16261. \(if the user agrees) and then killed."
  16262. (let (buf file)
  16263. (while (setq buf (pop blist))
  16264. (setq file (buffer-file-name buf))
  16265. (when (and (buffer-modified-p buf)
  16266. file
  16267. (y-or-n-p (format "Save file %s? " file)))
  16268. (with-current-buffer buf (save-buffer)))
  16269. (kill-buffer buf))))
  16270. (defun org-agenda-prepare-buffers (files)
  16271. "Create buffers for all agenda files, protect archived trees and comments."
  16272. (interactive)
  16273. (let ((pa '(:org-archived t))
  16274. (pc '(:org-comment t))
  16275. (pall '(:org-archived t :org-comment t))
  16276. (inhibit-read-only t)
  16277. (org-inhibit-startup org-agenda-inhibit-startup)
  16278. (rea (concat ":" org-archive-tag ":"))
  16279. file re)
  16280. (save-excursion
  16281. (save-restriction
  16282. (while (setq file (pop files))
  16283. (catch 'nextfile
  16284. (if (bufferp file)
  16285. (set-buffer file)
  16286. (org-check-agenda-file file)
  16287. (set-buffer (org-get-agenda-file-buffer file)))
  16288. (widen)
  16289. (org-refresh-category-properties)
  16290. (org-refresh-properties org-effort-property 'org-effort)
  16291. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16292. (setq org-todo-keywords-for-agenda
  16293. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16294. (setq org-done-keywords-for-agenda
  16295. (append org-done-keywords-for-agenda org-done-keywords))
  16296. (setq org-todo-keyword-alist-for-agenda
  16297. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16298. (setq org-drawers-for-agenda
  16299. (append org-drawers-for-agenda org-drawers))
  16300. (setq org-tag-alist-for-agenda
  16301. (append org-tag-alist-for-agenda org-tag-alist))
  16302. (org-with-silent-modifications
  16303. (save-excursion
  16304. (remove-text-properties (point-min) (point-max) pall)
  16305. (when org-agenda-skip-archived-trees
  16306. (goto-char (point-min))
  16307. (while (re-search-forward rea nil t)
  16308. (if (org-at-heading-p t)
  16309. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16310. (goto-char (point-min))
  16311. (setq re (format org-heading-keyword-regexp-format
  16312. org-comment-string))
  16313. (while (re-search-forward re nil t)
  16314. (add-text-properties
  16315. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16316. (setq org-todo-keywords-for-agenda
  16317. (org-uniquify org-todo-keywords-for-agenda))
  16318. (setq org-todo-keyword-alist-for-agenda
  16319. (org-uniquify org-todo-keyword-alist-for-agenda)
  16320. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16321. ;;;; CDLaTeX minor mode
  16322. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16323. "Keymap for the minor `org-cdlatex-mode'.")
  16324. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16325. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16326. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16327. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16328. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16329. (defvar org-cdlatex-texmathp-advice-is-done nil
  16330. "Flag remembering if we have applied the advice to texmathp already.")
  16331. (define-minor-mode org-cdlatex-mode
  16332. "Toggle the minor `org-cdlatex-mode'.
  16333. This mode supports entering LaTeX environment and math in LaTeX fragments
  16334. in Org-mode.
  16335. \\{org-cdlatex-mode-map}"
  16336. nil " OCDL" nil
  16337. (when org-cdlatex-mode
  16338. (require 'cdlatex)
  16339. (run-hooks 'cdlatex-mode-hook)
  16340. (cdlatex-compute-tables))
  16341. (unless org-cdlatex-texmathp-advice-is-done
  16342. (setq org-cdlatex-texmathp-advice-is-done t)
  16343. (defadvice texmathp (around org-math-always-on activate)
  16344. "Always return t in org-mode buffers.
  16345. This is because we want to insert math symbols without dollars even outside
  16346. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16347. an embedded LaTeX fragment, let texmathp do its job.
  16348. \\[org-cdlatex-mode-map]"
  16349. (interactive)
  16350. (let (p)
  16351. (cond
  16352. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16353. ((eq this-command 'cdlatex-math-symbol)
  16354. (setq ad-return-value t
  16355. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16356. (t
  16357. (let ((p (org-inside-LaTeX-fragment-p)))
  16358. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16359. (setq ad-return-value t
  16360. texmathp-why '("Org-mode embedded math" . 0))
  16361. (if p ad-do-it)))))))))
  16362. (defun turn-on-org-cdlatex ()
  16363. "Unconditionally turn on `org-cdlatex-mode'."
  16364. (org-cdlatex-mode 1))
  16365. (defun org-try-cdlatex-tab ()
  16366. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16367. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16368. - inside a LaTeX fragment, or
  16369. - after the first word in a line, where an abbreviation expansion could
  16370. insert a LaTeX environment."
  16371. (when org-cdlatex-mode
  16372. (cond
  16373. ;; Before any word on the line: No expansion possible.
  16374. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16375. ;; Just after first word on the line: Expand it. Make sure it
  16376. ;; cannot happen on headlines, though.
  16377. ((save-excursion
  16378. (skip-chars-backward "a-zA-Z0-9*")
  16379. (skip-chars-backward " \t")
  16380. (and (bolp) (not (org-at-heading-p))))
  16381. (cdlatex-tab) t)
  16382. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16383. (defun org-cdlatex-underscore-caret (&optional arg)
  16384. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16385. Revert to the normal definition outside of these fragments."
  16386. (interactive "P")
  16387. (if (org-inside-LaTeX-fragment-p)
  16388. (call-interactively 'cdlatex-sub-superscript)
  16389. (let (org-cdlatex-mode)
  16390. (call-interactively (key-binding (vector last-input-event))))))
  16391. (defun org-cdlatex-math-modify (&optional arg)
  16392. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16393. Revert to the normal definition outside of these fragments."
  16394. (interactive "P")
  16395. (if (org-inside-LaTeX-fragment-p)
  16396. (call-interactively 'cdlatex-math-modify)
  16397. (let (org-cdlatex-mode)
  16398. (call-interactively (key-binding (vector last-input-event))))))
  16399. ;;;; LaTeX fragments
  16400. (defvar org-latex-regexps
  16401. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16402. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16403. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16404. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16405. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16406. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16407. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16408. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16409. "Regular expressions for matching embedded LaTeX.")
  16410. (defun org-inside-LaTeX-fragment-p ()
  16411. "Test if point is inside a LaTeX fragment.
  16412. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16413. sequence appearing also before point.
  16414. Even though the matchers for math are configurable, this function assumes
  16415. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16416. delimiters are skipped when they have been removed by customization.
  16417. The return value is nil, or a cons cell with the delimiter and the
  16418. position of this delimiter.
  16419. This function does a reasonably good job, but can locally be fooled by
  16420. for example currency specifications. For example it will assume being in
  16421. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16422. fragments that are properly closed, but during editing, we have to live
  16423. with the uncertainty caused by missing closing delimiters. This function
  16424. looks only before point, not after."
  16425. (catch 'exit
  16426. (let ((pos (point))
  16427. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16428. (lim (progn
  16429. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16430. (point)))
  16431. dd-on str (start 0) m re)
  16432. (goto-char pos)
  16433. (when dodollar
  16434. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16435. re (nth 1 (assoc "$" org-latex-regexps)))
  16436. (while (string-match re str start)
  16437. (cond
  16438. ((= (match-end 0) (length str))
  16439. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16440. ((= (match-end 0) (- (length str) 5))
  16441. (throw 'exit nil))
  16442. (t (setq start (match-end 0))))))
  16443. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16444. (goto-char pos)
  16445. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16446. (and (match-beginning 2) (throw 'exit nil))
  16447. ;; count $$
  16448. (while (re-search-backward "\\$\\$" lim t)
  16449. (setq dd-on (not dd-on)))
  16450. (goto-char pos)
  16451. (if dd-on (cons "$$" m))))))
  16452. (defun org-inside-latex-macro-p ()
  16453. "Is point inside a LaTeX macro or its arguments?"
  16454. (save-match-data
  16455. (org-in-regexp
  16456. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16457. (defvar org-latex-fragment-image-overlays nil
  16458. "List of overlays carrying the images of latex fragments.")
  16459. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16460. (defun org-remove-latex-fragment-image-overlays ()
  16461. "Remove all overlays with LaTeX fragment images in current buffer."
  16462. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16463. (setq org-latex-fragment-image-overlays nil))
  16464. (defun org-preview-latex-fragment (&optional subtree)
  16465. "Preview the LaTeX fragment at point, or all locally or globally.
  16466. If the cursor is in a LaTeX fragment, create the image and overlay
  16467. it over the source code. If there is no fragment at point, display
  16468. all fragments in the current text, from one headline to the next. With
  16469. prefix SUBTREE, display all fragments in the current subtree. With a
  16470. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16471. the cursor is before the first headline,
  16472. display all fragments in the buffer.
  16473. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16474. (interactive "P")
  16475. (unless buffer-file-name
  16476. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16477. (org-remove-latex-fragment-image-overlays)
  16478. (save-excursion
  16479. (save-restriction
  16480. (let (beg end at msg)
  16481. (cond
  16482. ((or (equal subtree '(16))
  16483. (not (save-excursion
  16484. (re-search-backward org-outline-regexp-bol nil t))))
  16485. (setq beg (point-min) end (point-max)
  16486. msg "Creating images for buffer...%s"))
  16487. ((equal subtree '(4))
  16488. (org-back-to-heading)
  16489. (setq beg (point) end (org-end-of-subtree t)
  16490. msg "Creating images for subtree...%s"))
  16491. (t
  16492. (if (setq at (org-inside-LaTeX-fragment-p))
  16493. (goto-char (max (point-min) (- (cdr at) 2)))
  16494. (org-back-to-heading))
  16495. (setq beg (point) end (progn (outline-next-heading) (point))
  16496. msg (if at "Creating image...%s"
  16497. "Creating images for entry...%s"))))
  16498. (message msg "")
  16499. (narrow-to-region beg end)
  16500. (goto-char beg)
  16501. (org-format-latex
  16502. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16503. (file-name-nondirectory
  16504. buffer-file-name)))
  16505. default-directory 'overlays msg at 'forbuffer
  16506. org-latex-create-formula-image-program)
  16507. (message msg "done. Use `C-c C-c' to remove images.")))))
  16508. (defun org-format-latex (prefix &optional dir overlays msg at
  16509. forbuffer processing-type)
  16510. "Replace LaTeX fragments with links to an image, and produce images.
  16511. Some of the options can be changed using the variable
  16512. `org-format-latex-options'."
  16513. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16514. (let* ((prefixnodir (file-name-nondirectory prefix))
  16515. (absprefix (expand-file-name prefix dir))
  16516. (todir (file-name-directory absprefix))
  16517. (opt org-format-latex-options)
  16518. (optnew org-format-latex-options)
  16519. (matchers (plist-get opt :matchers))
  16520. (re-list org-latex-regexps)
  16521. (cnt 0) txt hash link beg end re e checkdir
  16522. string
  16523. m n block-type block linkfile movefile ov)
  16524. ;; Check the different regular expressions
  16525. (while (setq e (pop re-list))
  16526. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16527. block (if block-type "\n\n" ""))
  16528. (when (member m matchers)
  16529. (goto-char (point-min))
  16530. (while (re-search-forward re nil t)
  16531. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16532. (or (not overlays)
  16533. (not (eq (get-char-property (match-beginning n)
  16534. 'org-overlay-type)
  16535. 'org-latex-overlay))))
  16536. (cond
  16537. ((eq processing-type 'verbatim))
  16538. ((eq processing-type 'mathjax)
  16539. ;; Prepare for MathJax processing.
  16540. (setq string (match-string n))
  16541. (when (member m '("$" "$1"))
  16542. (save-excursion
  16543. (delete-region (match-beginning n) (match-end n))
  16544. (goto-char (match-beginning n))
  16545. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16546. ((or (eq processing-type 'dvipng)
  16547. (eq processing-type 'imagemagick))
  16548. ;; Process to an image.
  16549. (setq txt (match-string n)
  16550. beg (match-beginning n) end (match-end n)
  16551. cnt (1+ cnt))
  16552. (let ((face (face-at-point))
  16553. (fg (plist-get opt :foreground))
  16554. (bg (plist-get opt :background))
  16555. ;; Ensure full list is printed.
  16556. print-length print-level)
  16557. (when forbuffer
  16558. ;; Get the colors from the face at point.
  16559. (goto-char beg)
  16560. (when (eq fg 'auto)
  16561. (setq fg (face-attribute face :foreground nil 'default)))
  16562. (when (eq bg 'auto)
  16563. (setq bg (face-attribute face :background nil 'default)))
  16564. (setq optnew (copy-sequence opt))
  16565. (plist-put optnew :foreground fg)
  16566. (plist-put optnew :background bg))
  16567. (setq hash (sha1 (prin1-to-string
  16568. (list org-format-latex-header
  16569. org-latex-default-packages-alist
  16570. org-latex-packages-alist
  16571. org-format-latex-options
  16572. forbuffer txt fg bg)))
  16573. linkfile (format "%s_%s.png" prefix hash)
  16574. movefile (format "%s_%s.png" absprefix hash)))
  16575. (setq link (concat block "[[file:" linkfile "]]" block))
  16576. (if msg (message msg cnt))
  16577. (goto-char beg)
  16578. (unless checkdir ; Ensure the directory exists.
  16579. (setq checkdir t)
  16580. (or (file-directory-p todir) (make-directory todir t)))
  16581. (unless (file-exists-p movefile)
  16582. (org-create-formula-image
  16583. txt movefile optnew forbuffer processing-type))
  16584. (if overlays
  16585. (progn
  16586. (mapc (lambda (o)
  16587. (if (eq (overlay-get o 'org-overlay-type)
  16588. 'org-latex-overlay)
  16589. (delete-overlay o)))
  16590. (overlays-in beg end))
  16591. (setq ov (make-overlay beg end))
  16592. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16593. (if (featurep 'xemacs)
  16594. (progn
  16595. (overlay-put ov 'invisible t)
  16596. (overlay-put
  16597. ov 'end-glyph
  16598. (make-glyph (vector 'png :file movefile))))
  16599. (overlay-put
  16600. ov 'display
  16601. (list 'image :type 'png :file movefile :ascent 'center)))
  16602. (push ov org-latex-fragment-image-overlays)
  16603. (goto-char end))
  16604. (delete-region beg end)
  16605. (insert (org-add-props link
  16606. (list 'org-latex-src
  16607. (replace-regexp-in-string
  16608. "\"" "" txt)
  16609. 'org-latex-src-embed-type
  16610. (if block-type 'paragraph 'character))))))
  16611. ((eq processing-type 'mathml)
  16612. ;; Process to MathML
  16613. (unless (save-match-data (org-format-latex-mathml-available-p))
  16614. (error "LaTeX to MathML converter not configured"))
  16615. (setq txt (match-string n)
  16616. beg (match-beginning n) end (match-end n)
  16617. cnt (1+ cnt))
  16618. (if msg (message msg cnt))
  16619. (goto-char beg)
  16620. (delete-region beg end)
  16621. (insert (org-format-latex-as-mathml
  16622. txt block-type prefix dir)))
  16623. (t
  16624. (error "Unknown conversion type %s for latex fragments"
  16625. processing-type)))))))))
  16626. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16627. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16628. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16629. sucessful, return the portion between \"<math...> </math>\"
  16630. elements otherwise return nil. When MATHML-FILE is specified,
  16631. write the results in to that file. When invoked as an
  16632. interactive command, prompt for LATEX-FRAG, with initial value
  16633. set to the current active region and echo the results for user
  16634. inspection."
  16635. (interactive (list (let ((frag (when (org-region-active-p)
  16636. (buffer-substring-no-properties
  16637. (region-beginning) (region-end)))))
  16638. (read-string "LaTeX Fragment: " frag nil frag))))
  16639. (unless latex-frag (error "Invalid latex-frag"))
  16640. (let* ((tmp-in-file (file-relative-name
  16641. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16642. (ignore (write-region latex-frag nil tmp-in-file))
  16643. (tmp-out-file (file-relative-name
  16644. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16645. (cmd (format-spec
  16646. org-latex-to-mathml-convert-command
  16647. `((?j . ,(shell-quote-argument
  16648. (expand-file-name org-latex-to-mathml-jar-file)))
  16649. (?I . ,(shell-quote-argument tmp-in-file))
  16650. (?o . ,(shell-quote-argument tmp-out-file)))))
  16651. mathml shell-command-output)
  16652. (when (org-called-interactively-p 'any)
  16653. (unless (org-format-latex-mathml-available-p)
  16654. (error "LaTeX to MathML converter not configured")))
  16655. (message "Running %s" cmd)
  16656. (setq shell-command-output (shell-command-to-string cmd))
  16657. (setq mathml
  16658. (when (file-readable-p tmp-out-file)
  16659. (with-current-buffer (find-file-noselect tmp-out-file t)
  16660. (goto-char (point-min))
  16661. (when (re-search-forward
  16662. (concat
  16663. (regexp-quote
  16664. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16665. "\\(.\\|\n\\)*"
  16666. (regexp-quote "</math>")) nil t)
  16667. (prog1 (match-string 0) (kill-buffer))))))
  16668. (cond
  16669. (mathml
  16670. (setq mathml
  16671. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16672. (when mathml-file
  16673. (write-region mathml nil mathml-file))
  16674. (when (org-called-interactively-p 'any)
  16675. (message mathml)))
  16676. ((message "LaTeX to MathML conversion failed")
  16677. (message shell-command-output)))
  16678. (delete-file tmp-in-file)
  16679. (when (file-exists-p tmp-out-file)
  16680. (delete-file tmp-out-file))
  16681. mathml))
  16682. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16683. prefix &optional dir)
  16684. "Use `org-create-math-formula' but check local cache first."
  16685. (let* ((absprefix (expand-file-name prefix dir))
  16686. (print-length nil) (print-level nil)
  16687. (formula-id (concat
  16688. "formula-"
  16689. (sha1
  16690. (prin1-to-string
  16691. (list latex-frag
  16692. org-latex-to-mathml-convert-command)))))
  16693. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16694. (formula-cache-dir (file-name-directory formula-cache)))
  16695. (unless (file-directory-p formula-cache-dir)
  16696. (make-directory formula-cache-dir t))
  16697. (unless (file-exists-p formula-cache)
  16698. (org-create-math-formula latex-frag formula-cache))
  16699. (if (file-exists-p formula-cache)
  16700. ;; Successful conversion. Return the link to MathML file.
  16701. (org-add-props
  16702. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16703. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16704. 'org-latex-src-embed-type (if latex-frag-type
  16705. 'paragraph 'character)))
  16706. ;; Failed conversion. Return the LaTeX fragment verbatim
  16707. latex-frag)))
  16708. (defun org-create-formula-image (string tofile options buffer &optional type)
  16709. "Create an image from LaTeX source using dvipng or convert.
  16710. This function calls either `org-create-formula-image-with-dvipng'
  16711. or `org-create-formula-image-with-imagemagick' depending on the
  16712. value of `org-latex-create-formula-image-program' or on the value
  16713. of the optional TYPE variable.
  16714. Note: ultimately these two function should be combined as they
  16715. share a good deal of logic."
  16716. (org-check-external-command
  16717. "latex" "needed to convert LaTeX fragments to images")
  16718. (funcall
  16719. (case (or type org-latex-create-formula-image-program)
  16720. ('dvipng
  16721. (org-check-external-command
  16722. "dvipng" "needed to convert LaTeX fragments to images")
  16723. #'org-create-formula-image-with-dvipng)
  16724. ('imagemagick
  16725. (org-check-external-command
  16726. "convert" "you need to install imagemagick")
  16727. #'org-create-formula-image-with-imagemagick)
  16728. (t (error
  16729. "invalid value of `org-latex-create-formula-image-program'")))
  16730. string tofile options buffer))
  16731. (declare-function org-export--get-global-options "ox" (&optional backend))
  16732. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16733. (defun org-create-formula--latex-header ()
  16734. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16735. (org-latex-guess-inputenc
  16736. (org-splice-latex-header
  16737. org-format-latex-header
  16738. org-latex-default-packages-alist
  16739. org-latex-packages-alist t
  16740. (plist-get
  16741. (org-combine-plists
  16742. (org-export--get-global-options 'latex)
  16743. (org-export--get-inbuffer-options 'latex))
  16744. :latex-header))))
  16745. ;; This function borrows from Ganesh Swami's latex2png.el
  16746. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16747. "This calls dvipng."
  16748. (require 'ox-latex)
  16749. (let* ((tmpdir (if (featurep 'xemacs)
  16750. (temp-directory)
  16751. temporary-file-directory))
  16752. (texfilebase (make-temp-name
  16753. (expand-file-name "orgtex" tmpdir)))
  16754. (texfile (concat texfilebase ".tex"))
  16755. (dvifile (concat texfilebase ".dvi"))
  16756. (pngfile (concat texfilebase ".png"))
  16757. (fnh (if (featurep 'xemacs)
  16758. (font-height (face-font 'default))
  16759. (face-attribute 'default :height nil)))
  16760. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16761. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16762. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16763. "Black"))
  16764. (bg (or (plist-get options (if buffer :background :html-background))
  16765. "Transparent")))
  16766. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16767. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16768. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16769. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16770. (let ((latex-header (org-create-formula--latex-header)))
  16771. (with-temp-file texfile
  16772. (insert latex-header)
  16773. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16774. (let ((dir default-directory))
  16775. (condition-case nil
  16776. (progn
  16777. (cd tmpdir)
  16778. (call-process "latex" nil nil nil texfile))
  16779. (error nil))
  16780. (cd dir))
  16781. (if (not (file-exists-p dvifile))
  16782. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16783. (condition-case nil
  16784. (if (featurep 'xemacs)
  16785. (call-process "dvipng" nil nil nil
  16786. "-fg" fg "-bg" bg
  16787. "-T" "tight"
  16788. "-o" pngfile
  16789. dvifile)
  16790. (call-process "dvipng" nil nil nil
  16791. "-fg" fg "-bg" bg
  16792. "-D" dpi
  16793. ;;"-x" scale "-y" scale
  16794. "-T" "tight"
  16795. "-o" pngfile
  16796. dvifile))
  16797. (error nil))
  16798. (if (not (file-exists-p pngfile))
  16799. (if org-format-latex-signal-error
  16800. (error "Failed to create png file from %s" texfile)
  16801. (message "Failed to create png file from %s" texfile)
  16802. nil)
  16803. ;; Use the requested file name and clean up
  16804. (copy-file pngfile tofile 'replace)
  16805. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16806. (if (file-exists-p (concat texfilebase e))
  16807. (delete-file (concat texfilebase e))))
  16808. pngfile))))
  16809. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  16810. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16811. "This calls convert, which is included into imagemagick."
  16812. (require 'ox-latex)
  16813. (let* ((tmpdir (if (featurep 'xemacs)
  16814. (temp-directory)
  16815. temporary-file-directory))
  16816. (texfilebase (make-temp-name
  16817. (expand-file-name "orgtex" tmpdir)))
  16818. (texfile (concat texfilebase ".tex"))
  16819. (pdffile (concat texfilebase ".pdf"))
  16820. (pngfile (concat texfilebase ".png"))
  16821. (fnh (if (featurep 'xemacs)
  16822. (font-height (face-font 'default))
  16823. (face-attribute 'default :height nil)))
  16824. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16825. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16826. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16827. "black"))
  16828. (bg (or (plist-get options (if buffer :background :html-background))
  16829. "white")))
  16830. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16831. (setq fg (org-latex-color-format fg)))
  16832. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16833. (setq bg (org-latex-color-format
  16834. (if (string= bg "Transparent") "white" bg))))
  16835. (let ((latex-header (org-create-formula--latex-header)))
  16836. (with-temp-file texfile
  16837. (insert latex-header)
  16838. (insert "\n\\begin{document}\n"
  16839. "\\definecolor{fg}{rgb}{" fg "}\n"
  16840. "\\definecolor{bg}{rgb}{" bg "}\n"
  16841. "\n\\pagecolor{bg}\n"
  16842. "\n{\\color{fg}\n"
  16843. string
  16844. "\n}\n"
  16845. "\n\\end{document}\n")))
  16846. (org-latex-compile texfile t)
  16847. (if (not (file-exists-p pdffile))
  16848. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16849. (condition-case nil
  16850. (if (featurep 'xemacs)
  16851. (call-process "convert" nil nil nil
  16852. "-density" "96"
  16853. "-trim"
  16854. "-antialias"
  16855. pdffile
  16856. "-quality" "100"
  16857. ;; "-sharpen" "0x1.0"
  16858. pngfile)
  16859. (call-process "convert" nil nil nil
  16860. "-density" dpi
  16861. "-trim"
  16862. "-antialias"
  16863. pdffile
  16864. "-quality" "100"
  16865. ;; "-sharpen" "0x1.0"
  16866. pngfile))
  16867. (error nil))
  16868. (if (not (file-exists-p pngfile))
  16869. (if org-format-latex-signal-error
  16870. (error "Failed to create png file from %s" texfile)
  16871. (message "Failed to create png file from %s" texfile)
  16872. nil)
  16873. ;; Use the requested file name and clean up
  16874. (copy-file pngfile tofile 'replace)
  16875. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16876. (if (file-exists-p (concat texfilebase e))
  16877. (delete-file (concat texfilebase e))))
  16878. pngfile))))
  16879. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16880. "Fill a LaTeX header template TPL.
  16881. In the template, the following place holders will be recognized:
  16882. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16883. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16884. [PACKAGES] \\usepackage statements for PKG
  16885. [NO-PACKAGES] do not include PKG
  16886. [EXTRA] the string EXTRA
  16887. [NO-EXTRA] do not include EXTRA
  16888. For backward compatibility, if both the positive and the negative place
  16889. holder is missing, the positive one (without the \"NO-\") will be
  16890. assumed to be present at the end of the template.
  16891. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16892. EXTRA is a string.
  16893. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16894. (let (rpl (end ""))
  16895. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16896. (setq rpl (if (or (match-end 1) (not def-pkg))
  16897. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16898. tpl (replace-match rpl t t tpl))
  16899. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16900. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16901. (setq rpl (if (or (match-end 1) (not pkg))
  16902. "" (org-latex-packages-to-string pkg snippets-p t))
  16903. tpl (replace-match rpl t t tpl))
  16904. (if pkg (setq end
  16905. (concat end "\n"
  16906. (org-latex-packages-to-string pkg snippets-p)))))
  16907. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16908. (setq rpl (if (or (match-end 1) (not extra))
  16909. "" (concat extra "\n"))
  16910. tpl (replace-match rpl t t tpl))
  16911. (if (and extra (string-match "\\S-" extra))
  16912. (setq end (concat end "\n" extra))))
  16913. (if (string-match "\\S-" end)
  16914. (concat tpl "\n" end)
  16915. tpl)))
  16916. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16917. "Turn an alist of packages into a string with the \\usepackage macros."
  16918. (setq pkg (mapconcat (lambda(p)
  16919. (cond
  16920. ((stringp p) p)
  16921. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16922. (format "%% Package %s omitted" (cadr p)))
  16923. ((equal "" (car p))
  16924. (format "\\usepackage{%s}" (cadr p)))
  16925. (t
  16926. (format "\\usepackage[%s]{%s}"
  16927. (car p) (cadr p)))))
  16928. pkg
  16929. "\n"))
  16930. (if newline (concat pkg "\n") pkg))
  16931. (defun org-dvipng-color (attr)
  16932. "Return a RGB color specification for dvipng."
  16933. (apply 'format "rgb %s %s %s"
  16934. (mapcar 'org-normalize-color
  16935. (if (featurep 'xemacs)
  16936. (color-rgb-components
  16937. (face-property 'default
  16938. (cond ((eq attr :foreground) 'foreground)
  16939. ((eq attr :background) 'background))))
  16940. (color-values (face-attribute 'default attr nil))))))
  16941. (defun org-dvipng-color-format (color-name)
  16942. "Convert COLOR-NAME to a RGB color value for dvipng."
  16943. (apply 'format "rgb %s %s %s"
  16944. (mapcar 'org-normalize-color
  16945. (color-values color-name))))
  16946. (defun org-latex-color (attr)
  16947. "Return a RGB color for the LaTeX color package."
  16948. (apply 'format "%s,%s,%s"
  16949. (mapcar 'org-normalize-color
  16950. (if (featurep 'xemacs)
  16951. (color-rgb-components
  16952. (face-property 'default
  16953. (cond ((eq attr :foreground) 'foreground)
  16954. ((eq attr :background) 'background))))
  16955. (color-values (face-attribute 'default attr nil))))))
  16956. (defun org-latex-color-format (color-name)
  16957. "Convert COLOR-NAME to a RGB color value."
  16958. (apply 'format "%s,%s,%s"
  16959. (mapcar 'org-normalize-color
  16960. (color-values color-name))))
  16961. (defun org-normalize-color (value)
  16962. "Return string to be used as color value for an RGB component."
  16963. (format "%g" (/ value 65535.0)))
  16964. ;; Image display
  16965. (defvar org-inline-image-overlays nil)
  16966. (make-variable-buffer-local 'org-inline-image-overlays)
  16967. (defun org-toggle-inline-images (&optional include-linked)
  16968. "Toggle the display of inline images.
  16969. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16970. (interactive "P")
  16971. (if org-inline-image-overlays
  16972. (progn
  16973. (org-remove-inline-images)
  16974. (message "Inline image display turned off"))
  16975. (org-display-inline-images include-linked)
  16976. (if (and (org-called-interactively-p)
  16977. org-inline-image-overlays)
  16978. (message "%d images displayed inline"
  16979. (length org-inline-image-overlays))
  16980. (message "No images to display inline"))))
  16981. (defun org-redisplay-inline-images ()
  16982. "Refresh the display of inline images."
  16983. (interactive)
  16984. (if (not org-inline-image-overlays)
  16985. (org-toggle-inline-images)
  16986. (org-toggle-inline-images)
  16987. (org-toggle-inline-images)))
  16988. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16989. "Display inline images.
  16990. Normally only links without a description part are inlined, because this
  16991. is how it will work for export. When INCLUDE-LINKED is set, also links
  16992. with a description part will be inlined. This can be nice for a quick
  16993. look at those images, but it does not reflect what exported files will look
  16994. like.
  16995. When REFRESH is set, refresh existing images between BEG and END.
  16996. This will create new image displays only if necessary.
  16997. BEG and END default to the buffer boundaries."
  16998. (interactive "P")
  16999. (unless refresh
  17000. (org-remove-inline-images)
  17001. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17002. (save-excursion
  17003. (save-restriction
  17004. (widen)
  17005. (setq beg (or beg (point-min)) end (or end (point-max)))
  17006. (goto-char beg)
  17007. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17008. (substring (org-image-file-name-regexp) 0 -2)
  17009. "\\)\\]" (if include-linked "" "\\]")))
  17010. old file ov img type attrwidth width)
  17011. (while (re-search-forward re end t)
  17012. (setq old (get-char-property-and-overlay (match-beginning 1)
  17013. 'org-image-overlay)
  17014. file (expand-file-name
  17015. (concat (or (match-string 3) "") (match-string 4))))
  17016. (when (image-type-available-p 'imagemagick)
  17017. (setq attrwidth (if (or (listp org-image-actual-width)
  17018. (null org-image-actual-width))
  17019. (save-excursion
  17020. (save-match-data
  17021. (when (re-search-backward
  17022. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  17023. (save-excursion
  17024. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17025. (string-to-number (match-string 1))))))
  17026. width (cond ((eq org-image-actual-width t) nil)
  17027. ((null org-image-actual-width) attrwidth)
  17028. ((numberp org-image-actual-width)
  17029. org-image-actual-width)
  17030. ((listp org-image-actual-width)
  17031. (or attrwidth (car org-image-actual-width))))
  17032. type (if width 'imagemagick)))
  17033. (when (file-exists-p file)
  17034. (if (and (car-safe old) refresh)
  17035. (image-refresh (overlay-get (cdr old) 'display))
  17036. (setq img (save-match-data (create-image file type nil :width width)))
  17037. (when img
  17038. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17039. (overlay-put ov 'display img)
  17040. (overlay-put ov 'face 'default)
  17041. (overlay-put ov 'org-image-overlay t)
  17042. (overlay-put ov 'modification-hooks
  17043. (list 'org-display-inline-remove-overlay))
  17044. (push ov org-inline-image-overlays)))))))))
  17045. (define-obsolete-function-alias
  17046. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17047. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17048. "Remove inline-display overlay if a corresponding region is modified."
  17049. (let ((inhibit-modification-hooks t))
  17050. (when (and ov after)
  17051. (delete ov org-inline-image-overlays)
  17052. (delete-overlay ov))))
  17053. (defun org-remove-inline-images ()
  17054. "Remove inline display of images."
  17055. (interactive)
  17056. (mapc 'delete-overlay org-inline-image-overlays)
  17057. (setq org-inline-image-overlays nil))
  17058. ;;;; Key bindings
  17059. ;; Outline functions from `outline-mode-prefix-map'
  17060. ;; that can be remapped in Org:
  17061. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17062. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17063. (define-key org-mode-map [remap outline-forward-same-level]
  17064. 'org-forward-heading-same-level)
  17065. (define-key org-mode-map [remap outline-backward-same-level]
  17066. 'org-backward-heading-same-level)
  17067. (define-key org-mode-map [remap show-branches]
  17068. 'org-kill-note-or-show-branches)
  17069. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17070. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17071. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17072. ;; Outline functions from `outline-mode-prefix-map' that can not
  17073. ;; be remapped in Org:
  17074. ;;
  17075. ;; - the column "key binding" shows whether the Outline function is still
  17076. ;; available in Org mode on the same key that it has been bound to in
  17077. ;; Outline mode:
  17078. ;; - "overridden": key used for a different functionality in Org mode
  17079. ;; - else: key still bound to the same Outline function in Org mode
  17080. ;;
  17081. ;; | Outline function | key binding | Org replacement |
  17082. ;; |------------------------------------+-------------+-----------------------|
  17083. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17084. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17085. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17086. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17087. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17088. ;; | `show-entry' | overridden | no replacement |
  17089. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17090. ;; | `show-branches' | `C-c C-k' | still same function |
  17091. ;; | `show-subtree' | overridden | visibility cycling |
  17092. ;; | `show-all' | overridden | no replacement |
  17093. ;; | `hide-subtree' | overridden | visibility cycling |
  17094. ;; | `hide-body' | overridden | no replacement |
  17095. ;; | `hide-entry' | overridden | visibility cycling |
  17096. ;; | `hide-leaves' | overridden | no replacement |
  17097. ;; | `hide-sublevels' | overridden | no replacement |
  17098. ;; | `hide-other' | overridden | no replacement |
  17099. ;; Make `C-c C-x' a prefix key
  17100. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17101. ;; TAB key with modifiers
  17102. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17103. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17104. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17105. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17106. ;; The following line is necessary under Suse GNU/Linux
  17107. (unless (featurep 'xemacs)
  17108. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17109. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17110. (define-key org-mode-map [backtab] 'org-shifttab)
  17111. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17112. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17113. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17114. ;; Cursor keys with modifiers
  17115. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17116. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17117. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17118. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17119. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17120. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17121. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17122. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17123. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17124. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17125. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17126. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17127. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17128. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17129. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17130. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17131. ;; Babel keys
  17132. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17133. (mapc (lambda (pair)
  17134. (define-key org-babel-map (car pair) (cdr pair)))
  17135. org-babel-key-bindings)
  17136. ;;; Extra keys for tty access.
  17137. ;; We only set them when really needed because otherwise the
  17138. ;; menus don't show the simple keys
  17139. (when (or org-use-extra-keys
  17140. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17141. (not window-system))
  17142. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17143. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17144. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17145. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17146. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17147. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17148. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17149. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17150. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17151. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17152. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17153. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17154. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17155. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17156. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17157. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17158. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17159. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17160. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17161. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17162. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17163. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17164. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17165. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17166. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17167. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17168. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17169. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17170. ;; All the other keys
  17171. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17172. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17173. (if (boundp 'narrow-map)
  17174. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17175. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17176. (if (boundp 'narrow-map)
  17177. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17178. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17179. (if (boundp 'narrow-map)
  17180. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17181. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17182. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17183. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17184. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17185. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17186. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17187. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17188. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17189. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17190. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17191. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17192. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17193. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17194. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17195. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17196. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17197. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17198. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17199. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17200. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17201. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17202. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17203. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17204. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17205. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17206. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17207. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17208. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17209. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17210. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17211. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17212. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17213. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17214. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17215. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17216. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17217. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17218. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17219. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17220. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17221. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17222. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17223. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17224. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17225. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17226. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17227. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17228. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17229. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17230. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17231. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17232. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17233. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17234. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17235. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17236. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17237. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17238. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17239. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17240. (org-defkey org-mode-map "\C-m" 'org-return)
  17241. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17242. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17243. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17244. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17245. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17246. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17247. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17248. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17249. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17250. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17251. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17252. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17253. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17254. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17255. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17256. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17257. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17258. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17259. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17260. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17261. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17262. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17263. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17264. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17265. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17266. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17267. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17268. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17269. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17270. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17271. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17272. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17273. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17274. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17275. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17276. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17277. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17278. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17279. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17280. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17281. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17282. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17283. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17284. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17285. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17286. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17287. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17288. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17289. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17290. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17291. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17292. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17293. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17294. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17295. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17296. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17297. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17298. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17299. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17300. (when (featurep 'xemacs)
  17301. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17302. (defconst org-speed-commands-default
  17303. '(
  17304. ("Outline Navigation")
  17305. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17306. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17307. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17308. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17309. ("F" . org-next-block)
  17310. ("B" . org-previous-block)
  17311. ("u" . (org-speed-move-safe 'outline-up-heading))
  17312. ("j" . org-goto)
  17313. ("g" . (org-refile t))
  17314. ("Outline Visibility")
  17315. ("c" . org-cycle)
  17316. ("C" . org-shifttab)
  17317. (" " . org-display-outline-path)
  17318. ("=" . org-columns)
  17319. ("Outline Structure Editing")
  17320. ("U" . org-shiftmetaup)
  17321. ("D" . org-shiftmetadown)
  17322. ("r" . org-metaright)
  17323. ("l" . org-metaleft)
  17324. ("R" . org-shiftmetaright)
  17325. ("L" . org-shiftmetaleft)
  17326. ("i" . (progn (forward-char 1) (call-interactively
  17327. 'org-insert-heading-respect-content)))
  17328. ("^" . org-sort)
  17329. ("w" . org-refile)
  17330. ("a" . org-archive-subtree-default-with-confirmation)
  17331. ("@" . org-mark-subtree)
  17332. ("#" . org-toggle-comment)
  17333. ("Clock Commands")
  17334. ("I" . org-clock-in)
  17335. ("O" . org-clock-out)
  17336. ("Meta Data Editing")
  17337. ("t" . org-todo)
  17338. ("," . (org-priority))
  17339. ("0" . (org-priority ?\ ))
  17340. ("1" . (org-priority ?A))
  17341. ("2" . (org-priority ?B))
  17342. ("3" . (org-priority ?C))
  17343. (":" . org-set-tags-command)
  17344. ("e" . org-set-effort)
  17345. ("E" . org-inc-effort)
  17346. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17347. (org-entry-put (point) "APPT_WARNTIME" m)))
  17348. ("Agenda Views etc")
  17349. ("v" . org-agenda)
  17350. ("/" . org-sparse-tree)
  17351. ("Misc")
  17352. ("o" . org-open-at-point)
  17353. ("?" . org-speed-command-help)
  17354. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17355. (">" . (org-agenda-remove-restriction-lock))
  17356. )
  17357. "The default speed commands.")
  17358. (defun org-print-speed-command (e)
  17359. (if (> (length (car e)) 1)
  17360. (progn
  17361. (princ "\n")
  17362. (princ (car e))
  17363. (princ "\n")
  17364. (princ (make-string (length (car e)) ?-))
  17365. (princ "\n"))
  17366. (princ (car e))
  17367. (princ " ")
  17368. (if (symbolp (cdr e))
  17369. (princ (symbol-name (cdr e)))
  17370. (prin1 (cdr e)))
  17371. (princ "\n")))
  17372. (defun org-speed-command-help ()
  17373. "Show the available speed commands."
  17374. (interactive)
  17375. (if (not org-use-speed-commands)
  17376. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17377. (with-output-to-temp-buffer "*Help*"
  17378. (princ "User-defined Speed commands\n===========================\n")
  17379. (mapc 'org-print-speed-command org-speed-commands-user)
  17380. (princ "\n")
  17381. (princ "Built-in Speed commands\n=======================\n")
  17382. (mapc 'org-print-speed-command org-speed-commands-default))
  17383. (with-current-buffer "*Help*"
  17384. (setq truncate-lines t))))
  17385. (defun org-speed-move-safe (cmd)
  17386. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17387. If not, return to the original position and throw an error."
  17388. (interactive)
  17389. (let ((pos (point)))
  17390. (call-interactively cmd)
  17391. (unless (and (bolp) (org-at-heading-p))
  17392. (goto-char pos)
  17393. (error "Boundary reached while executing %s" cmd))))
  17394. (defvar org-self-insert-command-undo-counter 0)
  17395. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17396. (defvar org-speed-command nil)
  17397. (define-obsolete-function-alias
  17398. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17399. (defun org-speed-command-activate (keys)
  17400. "Hook for activating single-letter speed commands.
  17401. `org-speed-commands-default' specifies a minimal command set.
  17402. Use `org-speed-commands-user' for further customization."
  17403. (when (or (and (bolp) (looking-at org-outline-regexp))
  17404. (and (functionp org-use-speed-commands)
  17405. (funcall org-use-speed-commands)))
  17406. (cdr (assoc keys (append org-speed-commands-user
  17407. org-speed-commands-default)))))
  17408. (define-obsolete-function-alias
  17409. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17410. (defun org-babel-speed-command-activate (keys)
  17411. "Hook for activating single-letter code block commands."
  17412. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17413. (cdr (assoc keys org-babel-key-bindings))))
  17414. (defcustom org-speed-command-hook
  17415. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17416. "Hook for activating speed commands at strategic locations.
  17417. Hook functions are called in sequence until a valid handler is
  17418. found.
  17419. Each hook takes a single argument, a user-pressed command key
  17420. which is also a `self-insert-command' from the global map.
  17421. Within the hook, examine the cursor position and the command key
  17422. and return nil or a valid handler as appropriate. Handler could
  17423. be one of an interactive command, a function, or a form.
  17424. Set `org-use-speed-commands' to non-nil value to enable this
  17425. hook. The default setting is `org-speed-command-activate'."
  17426. :group 'org-structure
  17427. :version "24.1"
  17428. :type 'hook)
  17429. (defun org-self-insert-command (N)
  17430. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17431. If the cursor is in a table looking at whitespace, the whitespace is
  17432. overwritten, and the table is not marked as requiring realignment."
  17433. (interactive "p")
  17434. (org-check-before-invisible-edit 'insert)
  17435. (cond
  17436. ((and org-use-speed-commands
  17437. (setq org-speed-command
  17438. (run-hook-with-args-until-success
  17439. 'org-speed-command-hook (this-command-keys))))
  17440. (cond
  17441. ((commandp org-speed-command)
  17442. (setq this-command org-speed-command)
  17443. (call-interactively org-speed-command))
  17444. ((functionp org-speed-command)
  17445. (funcall org-speed-command))
  17446. ((and org-speed-command (listp org-speed-command))
  17447. (eval org-speed-command))
  17448. (t (let (org-use-speed-commands)
  17449. (call-interactively 'org-self-insert-command)))))
  17450. ((and
  17451. (org-table-p)
  17452. (progn
  17453. ;; check if we blank the field, and if that triggers align
  17454. (and (featurep 'org-table) org-table-auto-blank-field
  17455. (member last-command
  17456. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17457. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17458. ;; got extra space, this field does not determine column width
  17459. (let (org-table-may-need-update) (org-table-blank-field))
  17460. ;; no extra space, this field may determine column width
  17461. (org-table-blank-field)))
  17462. t)
  17463. (eq N 1)
  17464. (looking-at "[^|\n]* |"))
  17465. (let (org-table-may-need-update)
  17466. (goto-char (1- (match-end 0)))
  17467. (backward-delete-char 1)
  17468. (goto-char (match-beginning 0))
  17469. (self-insert-command N)))
  17470. (t
  17471. (setq org-table-may-need-update t)
  17472. (self-insert-command N)
  17473. (org-fix-tags-on-the-fly)
  17474. (if org-self-insert-cluster-for-undo
  17475. (if (not (eq last-command 'org-self-insert-command))
  17476. (setq org-self-insert-command-undo-counter 1)
  17477. (if (>= org-self-insert-command-undo-counter 20)
  17478. (setq org-self-insert-command-undo-counter 1)
  17479. (and (> org-self-insert-command-undo-counter 0)
  17480. buffer-undo-list (listp buffer-undo-list)
  17481. (not (cadr buffer-undo-list)) ; remove nil entry
  17482. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17483. (setq org-self-insert-command-undo-counter
  17484. (1+ org-self-insert-command-undo-counter))))))))
  17485. (defun org-check-before-invisible-edit (kind)
  17486. "Check is editing if kind KIND would be dangerous with invisible text around.
  17487. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17488. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17489. (if (and org-catch-invisible-edits
  17490. (or (not (boundp 'visible-mode)) (not visible-mode))
  17491. (or (get-char-property (point) 'invisible)
  17492. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17493. ;; OK, we need to take a closer look
  17494. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17495. (invisible-before-point (if (bobp) nil (get-char-property
  17496. (1- (point)) 'invisible)))
  17497. (border-and-ok-direction
  17498. (or
  17499. ;; Check if we are acting predictably before invisible text
  17500. (and invisible-at-point (not invisible-before-point)
  17501. (memq kind '(insert delete-backward)))
  17502. ;; Check if we are acting predictably after invisible text
  17503. ;; This works not well, and I have turned it off. It seems
  17504. ;; better to always show and stop after invisible text.
  17505. ;; (and (not invisible-at-point) invisible-before-point
  17506. ;; (memq kind '(insert delete)))
  17507. )))
  17508. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17509. (memq invisible-before-point '(outline org-hide-block t)))
  17510. (if (eq org-catch-invisible-edits 'error)
  17511. (error "Editing in invisible areas is prohibited - make visible first"))
  17512. (if (and org-custom-properties-overlays
  17513. (y-or-n-p "Display invisible properties in this buffer? "))
  17514. (org-toggle-custom-properties-visibility)
  17515. ;; Make the area visible
  17516. (save-excursion
  17517. (if invisible-before-point
  17518. (goto-char (previous-single-char-property-change
  17519. (point) 'invisible)))
  17520. (org-cycle))
  17521. (cond
  17522. ((eq org-catch-invisible-edits 'show)
  17523. ;; That's it, we do the edit after showing
  17524. (message
  17525. "Unfolding invisible region around point before editing")
  17526. (sit-for 1))
  17527. ((and (eq org-catch-invisible-edits 'smart)
  17528. border-and-ok-direction)
  17529. (message "Unfolding invisible region around point before editing"))
  17530. (t
  17531. ;; Don't do the edit, make the user repeat it in full visibility
  17532. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17533. (defun org-fix-tags-on-the-fly ()
  17534. (when (and (equal (char-after (point-at-bol)) ?*)
  17535. (org-at-heading-p))
  17536. (org-align-tags-here org-tags-column)))
  17537. (defun org-delete-backward-char (N)
  17538. "Like `delete-backward-char', insert whitespace at field end in tables.
  17539. When deleting backwards, in tables this function will insert whitespace in
  17540. front of the next \"|\" separator, to keep the table aligned. The table will
  17541. still be marked for re-alignment if the field did fill the entire column,
  17542. because, in this case the deletion might narrow the column."
  17543. (interactive "p")
  17544. (save-match-data
  17545. (org-check-before-invisible-edit 'delete-backward)
  17546. (if (and (org-table-p)
  17547. (eq N 1)
  17548. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17549. (looking-at ".*?|"))
  17550. (let ((pos (point))
  17551. (noalign (looking-at "[^|\n\r]* |"))
  17552. (c org-table-may-need-update))
  17553. (backward-delete-char N)
  17554. (if (not overwrite-mode)
  17555. (progn
  17556. (skip-chars-forward "^|")
  17557. (insert " ")
  17558. (goto-char (1- pos))))
  17559. ;; noalign: if there were two spaces at the end, this field
  17560. ;; does not determine the width of the column.
  17561. (if noalign (setq org-table-may-need-update c)))
  17562. (backward-delete-char N)
  17563. (org-fix-tags-on-the-fly))))
  17564. (defun org-delete-char (N)
  17565. "Like `delete-char', but insert whitespace at field end in tables.
  17566. When deleting characters, in tables this function will insert whitespace in
  17567. front of the next \"|\" separator, to keep the table aligned. The table will
  17568. still be marked for re-alignment if the field did fill the entire column,
  17569. because, in this case the deletion might narrow the column."
  17570. (interactive "p")
  17571. (save-match-data
  17572. (org-check-before-invisible-edit 'delete)
  17573. (if (and (org-table-p)
  17574. (not (bolp))
  17575. (not (= (char-after) ?|))
  17576. (eq N 1))
  17577. (if (looking-at ".*?|")
  17578. (let ((pos (point))
  17579. (noalign (looking-at "[^|\n\r]* |"))
  17580. (c org-table-may-need-update))
  17581. (replace-match (concat
  17582. (substring (match-string 0) 1 -1)
  17583. " |"))
  17584. (goto-char pos)
  17585. ;; noalign: if there were two spaces at the end, this field
  17586. ;; does not determine the width of the column.
  17587. (if noalign (setq org-table-may-need-update c)))
  17588. (delete-char N))
  17589. (delete-char N)
  17590. (org-fix-tags-on-the-fly))))
  17591. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17592. (put 'org-self-insert-command 'delete-selection t)
  17593. (put 'orgtbl-self-insert-command 'delete-selection t)
  17594. (put 'org-delete-char 'delete-selection 'supersede)
  17595. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17596. (put 'org-yank 'delete-selection 'yank)
  17597. ;; Make `flyspell-mode' delay after some commands
  17598. (put 'org-self-insert-command 'flyspell-delayed t)
  17599. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17600. (put 'org-delete-char 'flyspell-delayed t)
  17601. (put 'org-delete-backward-char 'flyspell-delayed t)
  17602. ;; Make pabbrev-mode expand after org-mode commands
  17603. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17604. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17605. ;; How to do this: Measure non-white length of current string
  17606. ;; If equal to column width, we should realign.
  17607. (defun org-remap (map &rest commands)
  17608. "In MAP, remap the functions given in COMMANDS.
  17609. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17610. (let (new old)
  17611. (while commands
  17612. (setq old (pop commands) new (pop commands))
  17613. (if (fboundp 'command-remapping)
  17614. (org-defkey map (vector 'remap old) new)
  17615. (substitute-key-definition old new map global-map)))))
  17616. (defun org-transpose-words ()
  17617. "Transpose words for Org.
  17618. This uses the `org-mode-transpose-word-syntax-table' syntax
  17619. table, which interprets characters in `org-emphasis-alist' as
  17620. word constituants."
  17621. (interactive)
  17622. (with-syntax-table org-mode-transpose-word-syntax-table
  17623. (call-interactively 'transpose-words)))
  17624. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17625. (when (eq org-enable-table-editor 'optimized)
  17626. ;; If the user wants maximum table support, we need to hijack
  17627. ;; some standard editing functions
  17628. (org-remap org-mode-map
  17629. 'self-insert-command 'org-self-insert-command
  17630. 'delete-char 'org-delete-char
  17631. 'delete-backward-char 'org-delete-backward-char)
  17632. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17633. (defvar org-ctrl-c-ctrl-c-hook nil
  17634. "Hook for functions attaching themselves to `C-c C-c'.
  17635. This can be used to add additional functionality to the C-c C-c
  17636. key which executes context-dependent commands. This hook is run
  17637. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17638. run after the last test.
  17639. Each function will be called with no arguments. The function
  17640. must check if the context is appropriate for it to act. If yes,
  17641. it should do its thing and then return a non-nil value. If the
  17642. context is wrong, just do nothing and return nil.")
  17643. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17644. "Hook for functions attaching themselves to `C-c C-c'.
  17645. This can be used to add additional functionality to the C-c C-c
  17646. key which executes context-dependent commands. This hook is run
  17647. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17648. before the first test.
  17649. Each function will be called with no arguments. The function
  17650. must check if the context is appropriate for it to act. If yes,
  17651. it should do its thing and then return a non-nil value. If the
  17652. context is wrong, just do nothing and return nil.")
  17653. (defvar org-tab-first-hook nil
  17654. "Hook for functions to attach themselves to TAB.
  17655. See `org-ctrl-c-ctrl-c-hook' for more information.
  17656. This hook runs as the first action when TAB is pressed, even before
  17657. `org-cycle' messes around with the `outline-regexp' to cater for
  17658. inline tasks and plain list item folding.
  17659. If any function in this hook returns t, any other actions that
  17660. would have been caused by TAB (such as table field motion or visibility
  17661. cycling) will not occur.")
  17662. (defvar org-tab-after-check-for-table-hook nil
  17663. "Hook for functions to attach themselves to TAB.
  17664. See `org-ctrl-c-ctrl-c-hook' for more information.
  17665. This hook runs after it has been established that the cursor is not in a
  17666. table, but before checking if the cursor is in a headline or if global cycling
  17667. should be done.
  17668. If any function in this hook returns t, not other actions like visibility
  17669. cycling will be done.")
  17670. (defvar org-tab-after-check-for-cycling-hook nil
  17671. "Hook for functions to attach themselves to TAB.
  17672. See `org-ctrl-c-ctrl-c-hook' for more information.
  17673. This hook runs after it has been established that not table field motion and
  17674. not visibility should be done because of current context. This is probably
  17675. the place where a package like yasnippets can hook in.")
  17676. (defvar org-tab-before-tab-emulation-hook nil
  17677. "Hook for functions to attach themselves to TAB.
  17678. See `org-ctrl-c-ctrl-c-hook' for more information.
  17679. This hook runs after every other options for TAB have been exhausted, but
  17680. before indentation and \t insertion takes place.")
  17681. (defvar org-metaleft-hook nil
  17682. "Hook for functions attaching themselves to `M-left'.
  17683. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17684. (defvar org-metaright-hook nil
  17685. "Hook for functions attaching themselves to `M-right'.
  17686. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17687. (defvar org-metaup-hook nil
  17688. "Hook for functions attaching themselves to `M-up'.
  17689. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17690. (defvar org-metadown-hook nil
  17691. "Hook for functions attaching themselves to `M-down'.
  17692. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17693. (defvar org-shiftmetaleft-hook nil
  17694. "Hook for functions attaching themselves to `M-S-left'.
  17695. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17696. (defvar org-shiftmetaright-hook nil
  17697. "Hook for functions attaching themselves to `M-S-right'.
  17698. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17699. (defvar org-shiftmetaup-hook nil
  17700. "Hook for functions attaching themselves to `M-S-up'.
  17701. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17702. (defvar org-shiftmetadown-hook nil
  17703. "Hook for functions attaching themselves to `M-S-down'.
  17704. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17705. (defvar org-metareturn-hook nil
  17706. "Hook for functions attaching themselves to `M-RET'.
  17707. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17708. (defvar org-shiftup-hook nil
  17709. "Hook for functions attaching themselves to `S-up'.
  17710. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17711. (defvar org-shiftup-final-hook nil
  17712. "Hook for functions attaching themselves to `S-up'.
  17713. This one runs after all other options except shift-select have been excluded.
  17714. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17715. (defvar org-shiftdown-hook nil
  17716. "Hook for functions attaching themselves to `S-down'.
  17717. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17718. (defvar org-shiftdown-final-hook nil
  17719. "Hook for functions attaching themselves to `S-down'.
  17720. This one runs after all other options except shift-select have been excluded.
  17721. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17722. (defvar org-shiftleft-hook nil
  17723. "Hook for functions attaching themselves to `S-left'.
  17724. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17725. (defvar org-shiftleft-final-hook nil
  17726. "Hook for functions attaching themselves to `S-left'.
  17727. This one runs after all other options except shift-select have been excluded.
  17728. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17729. (defvar org-shiftright-hook nil
  17730. "Hook for functions attaching themselves to `S-right'.
  17731. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17732. (defvar org-shiftright-final-hook nil
  17733. "Hook for functions attaching themselves to `S-right'.
  17734. This one runs after all other options except shift-select have been excluded.
  17735. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17736. (defun org-modifier-cursor-error ()
  17737. "Throw an error, a modified cursor command was applied in wrong context."
  17738. (error "This command is active in special context like tables, headlines or items"))
  17739. (defun org-shiftselect-error ()
  17740. "Throw an error because Shift-Cursor command was applied in wrong context."
  17741. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17742. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17743. (error "This command works only in special context like headlines or timestamps")))
  17744. (defun org-call-for-shift-select (cmd)
  17745. (let ((this-command-keys-shift-translated t))
  17746. (call-interactively cmd)))
  17747. (defun org-shifttab (&optional arg)
  17748. "Global visibility cycling or move to previous table field.
  17749. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17750. on context.
  17751. See the individual commands for more information."
  17752. (interactive "P")
  17753. (cond
  17754. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17755. ((integerp arg)
  17756. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17757. (message "Content view to level: %d" arg)
  17758. (org-content (prefix-numeric-value arg2))
  17759. (setq org-cycle-global-status 'overview)))
  17760. (t (call-interactively 'org-global-cycle))))
  17761. (defun org-shiftmetaleft ()
  17762. "Promote subtree or delete table column.
  17763. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17764. `org-table-delete-column', depending on context. See the
  17765. individual commands for more information."
  17766. (interactive)
  17767. (cond
  17768. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17769. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17770. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17771. ((if (not (org-region-active-p)) (org-at-item-p)
  17772. (save-excursion (goto-char (region-beginning))
  17773. (org-at-item-p)))
  17774. (call-interactively 'org-outdent-item-tree))
  17775. (t (org-modifier-cursor-error))))
  17776. (defun org-shiftmetaright ()
  17777. "Demote subtree or insert table column.
  17778. Calls `org-demote-subtree', `org-indent-item-tree', or
  17779. `org-table-insert-column', depending on context. See the
  17780. individual commands for more information."
  17781. (interactive)
  17782. (cond
  17783. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17784. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17785. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17786. ((if (not (org-region-active-p)) (org-at-item-p)
  17787. (save-excursion (goto-char (region-beginning))
  17788. (org-at-item-p)))
  17789. (call-interactively 'org-indent-item-tree))
  17790. (t (org-modifier-cursor-error))))
  17791. (defun org-shiftmetaup (&optional arg)
  17792. "Move subtree up or kill table row.
  17793. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17794. `org-move-item-up' or `org-timestamp-up', depending on context.
  17795. See the individual commands for more information."
  17796. (interactive "P")
  17797. (cond
  17798. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17799. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17800. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17801. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17802. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17803. (call-interactively 'org-timestamp-up)))
  17804. (t (call-interactively 'org-drag-line-backward))))
  17805. (defun org-shiftmetadown (&optional arg)
  17806. "Move subtree down or insert table row.
  17807. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17808. `org-move-item-down' or `org-timestamp-up', depending on context.
  17809. See the individual commands for more information."
  17810. (interactive "P")
  17811. (cond
  17812. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17813. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17814. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17815. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17816. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17817. (call-interactively 'org-timestamp-down)))
  17818. (t (call-interactively 'org-drag-line-forward))))
  17819. (defsubst org-hidden-tree-error ()
  17820. (error
  17821. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17822. (defun org-metaleft (&optional arg)
  17823. "Promote heading or move table column to left.
  17824. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17825. With no specific context, calls the Emacs default `backward-word'.
  17826. See the individual commands for more information."
  17827. (interactive "P")
  17828. (cond
  17829. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17830. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17831. ((org-with-limited-levels
  17832. (or (org-at-heading-p)
  17833. (and (org-region-active-p)
  17834. (save-excursion
  17835. (goto-char (region-beginning))
  17836. (org-at-heading-p)))))
  17837. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17838. (call-interactively 'org-do-promote))
  17839. ;; At an inline task.
  17840. ((org-at-heading-p)
  17841. (call-interactively 'org-inlinetask-promote))
  17842. ((or (org-at-item-p)
  17843. (and (org-region-active-p)
  17844. (save-excursion
  17845. (goto-char (region-beginning))
  17846. (org-at-item-p))))
  17847. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17848. (call-interactively 'org-outdent-item))
  17849. (t (call-interactively 'backward-word))))
  17850. (defun org-metaright (&optional arg)
  17851. "Demote a subtree, a list item or move table column to right.
  17852. In front of a drawer or a block keyword, indent it correctly.
  17853. With no specific context, calls the Emacs default `forward-word'.
  17854. See the individual commands for more information."
  17855. (interactive "P")
  17856. (cond
  17857. ((run-hook-with-args-until-success 'org-metaright-hook))
  17858. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17859. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17860. ((org-at-block-p) (call-interactively 'org-indent-block))
  17861. ((org-with-limited-levels
  17862. (or (org-at-heading-p)
  17863. (and (org-region-active-p)
  17864. (save-excursion
  17865. (goto-char (region-beginning))
  17866. (org-at-heading-p)))))
  17867. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17868. (call-interactively 'org-do-demote))
  17869. ;; At an inline task.
  17870. ((org-at-heading-p)
  17871. (call-interactively 'org-inlinetask-demote))
  17872. ((or (org-at-item-p)
  17873. (and (org-region-active-p)
  17874. (save-excursion
  17875. (goto-char (region-beginning))
  17876. (org-at-item-p))))
  17877. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17878. (call-interactively 'org-indent-item))
  17879. (t (call-interactively 'forward-word))))
  17880. (defun org-check-for-hidden (what)
  17881. "Check if there are hidden headlines/items in the current visual line.
  17882. WHAT can be either `headlines' or `items'. If the current line is
  17883. an outline or item heading and it has a folded subtree below it,
  17884. this function returns t, nil otherwise."
  17885. (let ((re (cond
  17886. ((eq what 'headlines) org-outline-regexp-bol)
  17887. ((eq what 'items) (org-item-beginning-re))
  17888. (t (error "This should not happen"))))
  17889. beg end)
  17890. (save-excursion
  17891. (catch 'exit
  17892. (unless (org-region-active-p)
  17893. (setq beg (point-at-bol))
  17894. (beginning-of-line 2)
  17895. (while (and (not (eobp)) ;; this is like `next-line'
  17896. (get-char-property (1- (point)) 'invisible))
  17897. (beginning-of-line 2))
  17898. (setq end (point))
  17899. (goto-char beg)
  17900. (goto-char (point-at-eol))
  17901. (setq end (max end (point)))
  17902. (while (re-search-forward re end t)
  17903. (if (get-char-property (match-beginning 0) 'invisible)
  17904. (throw 'exit t))))
  17905. nil))))
  17906. (defun org-metaup (&optional arg)
  17907. "Move subtree up or move table row up.
  17908. Calls `org-move-subtree-up' or `org-table-move-row' or
  17909. `org-move-item-up', depending on context. See the individual commands
  17910. for more information."
  17911. (interactive "P")
  17912. (cond
  17913. ((run-hook-with-args-until-success 'org-metaup-hook))
  17914. ((org-region-active-p)
  17915. (let* ((a (min (region-beginning) (region-end)))
  17916. (b (1- (max (region-beginning) (region-end))))
  17917. (c (save-excursion (goto-char a)
  17918. (move-beginning-of-line 0)))
  17919. (d (save-excursion (goto-char a)
  17920. (move-end-of-line 0) (point))))
  17921. (transpose-regions a b c d)
  17922. (goto-char c)))
  17923. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17924. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17925. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17926. (t (org-drag-element-backward))))
  17927. (defun org-metadown (&optional arg)
  17928. "Move subtree down or move table row down.
  17929. Calls `org-move-subtree-down' or `org-table-move-row' or
  17930. `org-move-item-down', depending on context. See the individual
  17931. commands for more information."
  17932. (interactive "P")
  17933. (cond
  17934. ((run-hook-with-args-until-success 'org-metadown-hook))
  17935. ((org-region-active-p)
  17936. (let* ((a (min (region-beginning) (region-end)))
  17937. (b (max (region-beginning) (region-end)))
  17938. (c (save-excursion (goto-char b)
  17939. (move-beginning-of-line 1)))
  17940. (d (save-excursion (goto-char b)
  17941. (move-end-of-line 1) (1+ (point)))))
  17942. (transpose-regions a b c d)
  17943. (goto-char d)))
  17944. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17945. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17946. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17947. (t (org-drag-element-forward))))
  17948. (defun org-shiftup (&optional arg)
  17949. "Increase item in timestamp or increase priority of current headline.
  17950. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17951. depending on context. See the individual commands for more information."
  17952. (interactive "P")
  17953. (cond
  17954. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17955. ((and org-support-shift-select (org-region-active-p))
  17956. (org-call-for-shift-select 'previous-line))
  17957. ((org-at-timestamp-p t)
  17958. (call-interactively (if org-edit-timestamp-down-means-later
  17959. 'org-timestamp-down 'org-timestamp-up)))
  17960. ((and (not (eq org-support-shift-select 'always))
  17961. org-enable-priority-commands
  17962. (org-at-heading-p))
  17963. (call-interactively 'org-priority-up))
  17964. ((and (not org-support-shift-select) (org-at-item-p))
  17965. (call-interactively 'org-previous-item))
  17966. ((org-clocktable-try-shift 'up arg))
  17967. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17968. (org-support-shift-select
  17969. (org-call-for-shift-select 'previous-line))
  17970. (t (org-shiftselect-error))))
  17971. (defun org-shiftdown (&optional arg)
  17972. "Decrease item in timestamp or decrease priority of current headline.
  17973. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17974. depending on context. See the individual commands for more information."
  17975. (interactive "P")
  17976. (cond
  17977. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17978. ((and org-support-shift-select (org-region-active-p))
  17979. (org-call-for-shift-select 'next-line))
  17980. ((org-at-timestamp-p t)
  17981. (call-interactively (if org-edit-timestamp-down-means-later
  17982. 'org-timestamp-up 'org-timestamp-down)))
  17983. ((and (not (eq org-support-shift-select 'always))
  17984. org-enable-priority-commands
  17985. (org-at-heading-p))
  17986. (call-interactively 'org-priority-down))
  17987. ((and (not org-support-shift-select) (org-at-item-p))
  17988. (call-interactively 'org-next-item))
  17989. ((org-clocktable-try-shift 'down arg))
  17990. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17991. (org-support-shift-select
  17992. (org-call-for-shift-select 'next-line))
  17993. (t (org-shiftselect-error))))
  17994. (defun org-shiftright (&optional arg)
  17995. "Cycle the thing at point or in the current line, depending on context.
  17996. Depending on context, this does one of the following:
  17997. - switch a timestamp at point one day into the future
  17998. - on a headline, switch to the next TODO keyword.
  17999. - on an item, switch entire list to the next bullet type
  18000. - on a property line, switch to the next allowed value
  18001. - on a clocktable definition line, move time block into the future"
  18002. (interactive "P")
  18003. (cond
  18004. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18005. ((and org-support-shift-select (org-region-active-p))
  18006. (org-call-for-shift-select 'forward-char))
  18007. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18008. ((and (not (eq org-support-shift-select 'always))
  18009. (org-at-heading-p))
  18010. (let ((org-inhibit-logging
  18011. (not org-treat-S-cursor-todo-selection-as-state-change))
  18012. (org-inhibit-blocking
  18013. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18014. (org-call-with-arg 'org-todo 'right)))
  18015. ((or (and org-support-shift-select
  18016. (not (eq org-support-shift-select 'always))
  18017. (org-at-item-bullet-p))
  18018. (and (not org-support-shift-select) (org-at-item-p)))
  18019. (org-call-with-arg 'org-cycle-list-bullet nil))
  18020. ((and (not (eq org-support-shift-select 'always))
  18021. (org-at-property-p))
  18022. (call-interactively 'org-property-next-allowed-value))
  18023. ((org-clocktable-try-shift 'right arg))
  18024. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18025. (org-support-shift-select
  18026. (org-call-for-shift-select 'forward-char))
  18027. (t (org-shiftselect-error))))
  18028. (defun org-shiftleft (&optional arg)
  18029. "Cycle the thing at point or in the current line, depending on context.
  18030. Depending on context, this does one of the following:
  18031. - switch a timestamp at point one day into the past
  18032. - on a headline, switch to the previous TODO keyword.
  18033. - on an item, switch entire list to the previous bullet type
  18034. - on a property line, switch to the previous allowed value
  18035. - on a clocktable definition line, move time block into the past"
  18036. (interactive "P")
  18037. (cond
  18038. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18039. ((and org-support-shift-select (org-region-active-p))
  18040. (org-call-for-shift-select 'backward-char))
  18041. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18042. ((and (not (eq org-support-shift-select 'always))
  18043. (org-at-heading-p))
  18044. (let ((org-inhibit-logging
  18045. (not org-treat-S-cursor-todo-selection-as-state-change))
  18046. (org-inhibit-blocking
  18047. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18048. (org-call-with-arg 'org-todo 'left)))
  18049. ((or (and org-support-shift-select
  18050. (not (eq org-support-shift-select 'always))
  18051. (org-at-item-bullet-p))
  18052. (and (not org-support-shift-select) (org-at-item-p)))
  18053. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18054. ((and (not (eq org-support-shift-select 'always))
  18055. (org-at-property-p))
  18056. (call-interactively 'org-property-previous-allowed-value))
  18057. ((org-clocktable-try-shift 'left arg))
  18058. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18059. (org-support-shift-select
  18060. (org-call-for-shift-select 'backward-char))
  18061. (t (org-shiftselect-error))))
  18062. (defun org-shiftcontrolright ()
  18063. "Switch to next TODO set."
  18064. (interactive)
  18065. (cond
  18066. ((and org-support-shift-select (org-region-active-p))
  18067. (org-call-for-shift-select 'forward-word))
  18068. ((and (not (eq org-support-shift-select 'always))
  18069. (org-at-heading-p))
  18070. (org-call-with-arg 'org-todo 'nextset))
  18071. (org-support-shift-select
  18072. (org-call-for-shift-select 'forward-word))
  18073. (t (org-shiftselect-error))))
  18074. (defun org-shiftcontrolleft ()
  18075. "Switch to previous TODO set."
  18076. (interactive)
  18077. (cond
  18078. ((and org-support-shift-select (org-region-active-p))
  18079. (org-call-for-shift-select 'backward-word))
  18080. ((and (not (eq org-support-shift-select 'always))
  18081. (org-at-heading-p))
  18082. (org-call-with-arg 'org-todo 'previousset))
  18083. (org-support-shift-select
  18084. (org-call-for-shift-select 'backward-word))
  18085. (t (org-shiftselect-error))))
  18086. (defun org-shiftcontrolup (&optional n)
  18087. "Change timestamps synchronously up in CLOCK log lines.
  18088. Optional argument N tells to change by that many units."
  18089. (interactive "P")
  18090. (cond ((and (not org-support-shift-select)
  18091. (org-at-clock-log-p)
  18092. (org-at-timestamp-p t))
  18093. (org-clock-timestamps-up n))
  18094. (t (org-shiftselect-error))))
  18095. (defun org-shiftcontroldown (&optional n)
  18096. "Change timestamps synchronously down in CLOCK log lines.
  18097. Optional argument N tells to change by that many units."
  18098. (interactive "P")
  18099. (cond ((and (not org-support-shift-select)
  18100. (org-at-clock-log-p)
  18101. (org-at-timestamp-p t))
  18102. (org-clock-timestamps-down n))
  18103. (t (org-shiftselect-error))))
  18104. (defun org-ctrl-c-ret ()
  18105. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18106. (interactive)
  18107. (cond
  18108. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18109. (t (call-interactively 'org-insert-heading))))
  18110. (defun org-find-visible ()
  18111. (let ((s (point)))
  18112. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18113. (get-char-property s 'invisible)))
  18114. s))
  18115. (defun org-find-invisible ()
  18116. (let ((s (point)))
  18117. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18118. (not (get-char-property s 'invisible))))
  18119. s))
  18120. (defun org-copy-visible (beg end)
  18121. "Copy the visible parts of the region."
  18122. (interactive "r")
  18123. (let (snippets s)
  18124. (save-excursion
  18125. (save-restriction
  18126. (narrow-to-region beg end)
  18127. (setq s (goto-char (point-min)))
  18128. (while (not (= (point) (point-max)))
  18129. (goto-char (org-find-invisible))
  18130. (push (buffer-substring s (point)) snippets)
  18131. (setq s (goto-char (org-find-visible))))))
  18132. (kill-new (apply 'concat (nreverse snippets)))))
  18133. (defun org-copy-special ()
  18134. "Copy region in table or copy current subtree.
  18135. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18136. See the individual commands for more information."
  18137. (interactive)
  18138. (call-interactively
  18139. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18140. (defun org-cut-special ()
  18141. "Cut region in table or cut current subtree.
  18142. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18143. See the individual commands for more information."
  18144. (interactive)
  18145. (call-interactively
  18146. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18147. (defun org-paste-special (arg)
  18148. "Paste rectangular region into table, or past subtree relative to level.
  18149. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18150. See the individual commands for more information."
  18151. (interactive "P")
  18152. (if (org-at-table-p)
  18153. (org-table-paste-rectangle)
  18154. (org-paste-subtree arg)))
  18155. (defsubst org-in-fixed-width-region-p ()
  18156. "Is point in a fixed-width region?"
  18157. (save-match-data
  18158. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18159. (defun org-edit-special (&optional arg)
  18160. "Call a special editor for the element at point.
  18161. When at a table, call the formula editor with `org-table-edit-formulas'.
  18162. When in a source code block, call `org-edit-src-code'.
  18163. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18164. When at an #+INCLUDE keyword, visit the included file.
  18165. On a link, call `ffap' to visit the link at point.
  18166. Otherwise, return a user error."
  18167. (interactive)
  18168. (let ((element (org-element-at-point)))
  18169. (assert (not buffer-read-only) nil
  18170. "Buffer is read-only: %s" (buffer-name))
  18171. (case (org-element-type element)
  18172. (src-block
  18173. (if (not arg) (org-edit-src-code)
  18174. (let* ((info (org-babel-get-src-block-info))
  18175. (lang (nth 0 info))
  18176. (params (nth 2 info))
  18177. (session (cdr (assq :session params))))
  18178. (if (not session) (org-edit-src-code)
  18179. ;; At a src-block with a session and function called with
  18180. ;; an ARG: switch to the buffer related to the inferior
  18181. ;; process.
  18182. (funcall (intern (concat "org-babel-prep-session:" lang))
  18183. session params)))))
  18184. (keyword
  18185. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18186. (find-file
  18187. (org-remove-double-quotes
  18188. (car (org-split-string (org-element-property :value element)))))
  18189. (user-error "No special environment to edit here")))
  18190. (table
  18191. (if (eq (org-element-property :type element) 'table.el)
  18192. (org-edit-src-code)
  18193. (call-interactively 'org-table-edit-formulas)))
  18194. ;; Only Org tables contain `table-row' type elements.
  18195. (table-row (call-interactively 'org-table-edit-formulas))
  18196. ((example-block export-block) (org-edit-src-code))
  18197. (fixed-width (org-edit-fixed-width-region))
  18198. (otherwise
  18199. ;; No notable element at point. Though, we may be at a link,
  18200. ;; which is an object. Thus, scan deeper.
  18201. (if (eq (org-element-type (org-element-context element)) 'link)
  18202. (call-interactively 'ffap)
  18203. (user-error "No special environment to edit here"))))))
  18204. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18205. (defun org-ctrl-c-ctrl-c (&optional arg)
  18206. "Set tags in headline, or update according to changed information at point.
  18207. This command does many different things, depending on context:
  18208. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18209. this is what we do.
  18210. - If the cursor is on a statistics cookie, update it.
  18211. - If the cursor is in a headline, prompt for tags and insert them
  18212. into the current line, aligned to `org-tags-column'. When called
  18213. with prefix arg, realign all tags in the current buffer.
  18214. - If the cursor is in one of the special #+KEYWORD lines, this
  18215. triggers scanning the buffer for these lines and updating the
  18216. information.
  18217. - If the cursor is inside a table, realign the table. This command
  18218. works even if the automatic table editor has been turned off.
  18219. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18220. the entire table.
  18221. - If the cursor is at a footnote reference or definition, jump to
  18222. the corresponding definition or references, respectively.
  18223. - If the cursor is a the beginning of a dynamic block, update it.
  18224. - If the current buffer is a capture buffer, close note and file it.
  18225. - If the cursor is on a <<<target>>>, update radio targets and
  18226. corresponding links in this buffer.
  18227. - If the cursor is on a numbered item in a plain list, renumber the
  18228. ordered list.
  18229. - If the cursor is on a checkbox, toggle it.
  18230. - If the cursor is on a code block, evaluate it. The variable
  18231. `org-confirm-babel-evaluate' can be used to control prompting
  18232. before code block evaluation, by default every code block
  18233. evaluation requires confirmation. Code block evaluation can be
  18234. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18235. (interactive "P")
  18236. (cond
  18237. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18238. org-occur-highlights
  18239. org-latex-fragment-image-overlays)
  18240. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18241. (org-remove-occur-highlights)
  18242. (org-remove-latex-fragment-image-overlays)
  18243. (message "Temporary highlights/overlays removed from current buffer"))
  18244. ((and (local-variable-p 'org-finish-function (current-buffer))
  18245. (fboundp org-finish-function))
  18246. (funcall org-finish-function))
  18247. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18248. (t
  18249. (let* ((context (org-element-context)) (type (org-element-type context)))
  18250. ;; Test if point is within a blank line.
  18251. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18252. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18253. (user-error "C-c C-c can do nothing useful at this location"))
  18254. ;; For convenience: at the first line of a paragraph on the
  18255. ;; same line as an item, apply function on that item instead.
  18256. (when (eq type 'paragraph)
  18257. (let ((parent (org-element-property :parent context)))
  18258. (when (and (eq (org-element-type parent) 'item)
  18259. (= (point-at-bol) (org-element-property :begin parent)))
  18260. (setq context parent type 'item))))
  18261. ;; Act according to type of element or object at point.
  18262. (case type
  18263. (clock (org-clock-update-time-maybe))
  18264. (dynamic-block
  18265. (save-excursion
  18266. (goto-char (org-element-property :post-affiliated context))
  18267. (org-update-dblock)))
  18268. (footnote-definition
  18269. (goto-char (org-element-property :post-affiliated context))
  18270. (call-interactively 'org-footnote-action))
  18271. (footnote-reference (call-interactively 'org-footnote-action))
  18272. ((headline inlinetask)
  18273. (save-excursion (goto-char (org-element-property :begin context))
  18274. (call-interactively 'org-set-tags)))
  18275. (item
  18276. ;; At an item: a double C-u set checkbox to "[-]"
  18277. ;; unconditionally, whereas a single one will toggle its
  18278. ;; presence. Without an universal argument, if the item
  18279. ;; has a checkbox, toggle it. Otherwise repair the list.
  18280. (let* ((box (org-element-property :checkbox context))
  18281. (struct (org-element-property :structure context))
  18282. (old-struct (copy-tree struct))
  18283. (parents (org-list-parents-alist struct))
  18284. (prevs (org-list-prevs-alist struct))
  18285. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18286. (org-list-set-checkbox
  18287. (org-element-property :begin context) struct
  18288. (cond ((equal arg '(16)) "[-]")
  18289. ((and (not box) (equal arg '(4))) "[ ]")
  18290. ((or (not box) (equal arg '(4))) nil)
  18291. ((eq box 'on) "[ ]")
  18292. (t "[X]")))
  18293. ;; Mimic `org-list-write-struct' but with grabbing
  18294. ;; a return value from `org-list-struct-fix-box'.
  18295. (org-list-struct-fix-ind struct parents 2)
  18296. (org-list-struct-fix-item-end struct)
  18297. (org-list-struct-fix-bul struct prevs)
  18298. (org-list-struct-fix-ind struct parents)
  18299. (let ((block-item
  18300. (org-list-struct-fix-box struct parents prevs orderedp)))
  18301. (if (and box (equal struct old-struct))
  18302. (if (equal arg '(16))
  18303. (message "Checkboxes already reset")
  18304. (user-error "Cannot toggle this checkbox: %s"
  18305. (if (eq box 'on)
  18306. "all subitems checked"
  18307. "unchecked subitems")))
  18308. (org-list-struct-apply-struct struct old-struct)
  18309. (org-update-checkbox-count-maybe))
  18310. (when block-item
  18311. (message "Checkboxes were removed due to empty box at line %d"
  18312. (org-current-line block-item))))))
  18313. (keyword
  18314. (let ((org-inhibit-startup-visibility-stuff t)
  18315. (org-startup-align-all-tables nil))
  18316. (when (boundp 'org-table-coordinate-overlays)
  18317. (mapc 'delete-overlay org-table-coordinate-overlays)
  18318. (setq org-table-coordinate-overlays nil))
  18319. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18320. (message "Local setup has been refreshed"))
  18321. (plain-list
  18322. ;; At a plain list, with a double C-u argument, set
  18323. ;; checkboxes of each item to "[-]", whereas a single one
  18324. ;; will toggle their presence according to the state of the
  18325. ;; first item in the list. Without an argument, repair the
  18326. ;; list.
  18327. (let* ((begin (org-element-property :contents-begin context))
  18328. (struct (org-element-property :structure context))
  18329. (old-struct (copy-tree struct))
  18330. (first-box (save-excursion
  18331. (goto-char begin)
  18332. (looking-at org-list-full-item-re)
  18333. (match-string-no-properties 3)))
  18334. (new-box (cond ((equal arg '(16)) "[-]")
  18335. ((equal arg '(4)) (unless first-box "[ ]"))
  18336. ((equal first-box "[X]") "[ ]")
  18337. (t "[X]"))))
  18338. (cond
  18339. (arg
  18340. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18341. (org-list-get-all-items
  18342. begin struct (org-list-prevs-alist struct))))
  18343. ((and first-box (eq (point) begin))
  18344. ;; For convenience, when point is at bol on the first
  18345. ;; item of the list and no argument is provided, simply
  18346. ;; toggle checkbox of that item, if any.
  18347. (org-list-set-checkbox begin struct new-box)))
  18348. (org-list-write-struct
  18349. struct (org-list-parents-alist struct) old-struct)
  18350. (org-update-checkbox-count-maybe)
  18351. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18352. ((property-drawer node-property)
  18353. (call-interactively 'org-property-action))
  18354. ((radio-target target)
  18355. (call-interactively 'org-update-radio-target-regexp))
  18356. (statistics-cookie
  18357. (call-interactively 'org-update-statistics-cookies))
  18358. ((table table-cell table-row)
  18359. ;; At a table, recalculate every field and align it. Also
  18360. ;; send the table if necessary. If the table has
  18361. ;; a `table.el' type, just give up. At a table row or
  18362. ;; cell, maybe recalculate line but always align table.
  18363. (if (eq (org-element-property :type context) 'table.el)
  18364. (message "Use C-c ' to edit table.el tables")
  18365. (let ((org-enable-table-editor t))
  18366. (if (or (eq type 'table)
  18367. ;; Check if point is at a TBLFM line.
  18368. (and (eq type 'table-row)
  18369. (= (point) (org-element-property :end context))))
  18370. (save-excursion
  18371. (goto-char (org-element-property :contents-begin context))
  18372. (org-call-with-arg 'org-table-recalculate (or arg t))
  18373. (orgtbl-send-table 'maybe))
  18374. (org-table-maybe-eval-formula)
  18375. (cond (arg (call-interactively 'org-table-recalculate))
  18376. ((org-table-maybe-recalculate-line))
  18377. (t (org-table-align)))))))
  18378. (timestamp (org-timestamp-change 0 'day))
  18379. (otherwise
  18380. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18381. (user-error
  18382. "C-c C-c can do nothing useful at this location")))))))))
  18383. (defun org-mode-restart ()
  18384. "Restart Org-mode, to scan again for special lines.
  18385. Also updates the keyword regular expressions."
  18386. (interactive)
  18387. (org-mode)
  18388. (message "Org-mode restarted"))
  18389. (defun org-kill-note-or-show-branches ()
  18390. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18391. (interactive)
  18392. (if (not org-finish-function)
  18393. (progn
  18394. (hide-subtree)
  18395. (call-interactively 'show-branches))
  18396. (let ((org-note-abort t))
  18397. (funcall org-finish-function))))
  18398. (defun org-return (&optional indent)
  18399. "Goto next table row or insert a newline.
  18400. Calls `org-table-next-row' or `newline', depending on context.
  18401. See the individual commands for more information."
  18402. (interactive)
  18403. (let (org-ts-what)
  18404. (cond
  18405. ((or (bobp) (org-in-src-block-p))
  18406. (if indent (newline-and-indent) (newline)))
  18407. ((org-at-table-p)
  18408. (org-table-justify-field-maybe)
  18409. (call-interactively 'org-table-next-row))
  18410. ;; when `newline-and-indent' is called within a list, make sure
  18411. ;; text moved stays inside the item.
  18412. ((and (org-in-item-p) indent)
  18413. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18414. (progn
  18415. (save-match-data (newline))
  18416. (org-indent-line-to (length (match-string 0))))
  18417. (let ((ind (org-get-indentation)))
  18418. (newline)
  18419. (if (org-looking-back org-list-end-re)
  18420. (org-indent-line)
  18421. (org-indent-line-to ind)))))
  18422. ((and org-return-follows-link
  18423. (org-at-timestamp-p t)
  18424. (not (eq org-ts-what 'after)))
  18425. (org-follow-timestamp-link))
  18426. ((and org-return-follows-link
  18427. (let ((tprop (get-text-property (point) 'face)))
  18428. (or (eq tprop 'org-link)
  18429. (and (listp tprop) (memq 'org-link tprop)))))
  18430. (call-interactively 'org-open-at-point))
  18431. ((and (org-at-heading-p)
  18432. (looking-at
  18433. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18434. (org-show-entry)
  18435. (end-of-line 1)
  18436. (newline))
  18437. (t (if indent (newline-and-indent) (newline))))))
  18438. (defun org-return-indent ()
  18439. "Goto next table row or insert a newline and indent.
  18440. Calls `org-table-next-row' or `newline-and-indent', depending on
  18441. context. See the individual commands for more information."
  18442. (interactive)
  18443. (org-return t))
  18444. (defun org-ctrl-c-star ()
  18445. "Compute table, or change heading status of lines.
  18446. Calls `org-table-recalculate' or `org-toggle-heading',
  18447. depending on context."
  18448. (interactive)
  18449. (cond
  18450. ((org-at-table-p)
  18451. (call-interactively 'org-table-recalculate))
  18452. (t
  18453. ;; Convert all lines in region to list items
  18454. (call-interactively 'org-toggle-heading))))
  18455. (defun org-ctrl-c-minus ()
  18456. "Insert separator line in table or modify bullet status of line.
  18457. Also turns a plain line or a region of lines into list items.
  18458. Calls `org-table-insert-hline', `org-toggle-item', or
  18459. `org-cycle-list-bullet', depending on context."
  18460. (interactive)
  18461. (cond
  18462. ((org-at-table-p)
  18463. (call-interactively 'org-table-insert-hline))
  18464. ((org-region-active-p)
  18465. (call-interactively 'org-toggle-item))
  18466. ((org-in-item-p)
  18467. (call-interactively 'org-cycle-list-bullet))
  18468. (t
  18469. (call-interactively 'org-toggle-item))))
  18470. (defun org-toggle-item (arg)
  18471. "Convert headings or normal lines to items, items to normal lines.
  18472. If there is no active region, only the current line is considered.
  18473. If the first non blank line in the region is a headline, convert
  18474. all headlines to items, shifting text accordingly.
  18475. If it is an item, convert all items to normal lines.
  18476. If it is normal text, change region into a list of items.
  18477. With a prefix argument ARG, change the region in a single item."
  18478. (interactive "P")
  18479. (let ((shift-text
  18480. (function
  18481. ;; Shift text in current section to IND, from point to END.
  18482. ;; The function leaves point to END line.
  18483. (lambda (ind end)
  18484. (let ((min-i 1000) (end (copy-marker end)))
  18485. ;; First determine the minimum indentation (MIN-I) of
  18486. ;; the text.
  18487. (save-excursion
  18488. (catch 'exit
  18489. (while (< (point) end)
  18490. (let ((i (org-get-indentation)))
  18491. (cond
  18492. ;; Skip blank lines and inline tasks.
  18493. ((looking-at "^[ \t]*$"))
  18494. ((looking-at org-outline-regexp-bol))
  18495. ;; We can't find less than 0 indentation.
  18496. ((zerop i) (throw 'exit (setq min-i 0)))
  18497. ((< i min-i) (setq min-i i))))
  18498. (forward-line))))
  18499. ;; Then indent each line so that a line indented to
  18500. ;; MIN-I becomes indented to IND. Ignore blank lines
  18501. ;; and inline tasks in the process.
  18502. (let ((delta (- ind min-i)))
  18503. (while (< (point) end)
  18504. (unless (or (looking-at "^[ \t]*$")
  18505. (looking-at org-outline-regexp-bol))
  18506. (org-indent-line-to (+ (org-get-indentation) delta)))
  18507. (forward-line)))))))
  18508. (skip-blanks
  18509. (function
  18510. ;; Return beginning of first non-blank line, starting from
  18511. ;; line at POS.
  18512. (lambda (pos)
  18513. (save-excursion
  18514. (goto-char pos)
  18515. (skip-chars-forward " \r\t\n")
  18516. (point-at-bol)))))
  18517. beg end)
  18518. ;; Determine boundaries of changes.
  18519. (if (org-region-active-p)
  18520. (setq beg (funcall skip-blanks (region-beginning))
  18521. end (copy-marker (region-end)))
  18522. (setq beg (funcall skip-blanks (point-at-bol))
  18523. end (copy-marker (point-at-eol))))
  18524. ;; Depending on the starting line, choose an action on the text
  18525. ;; between BEG and END.
  18526. (org-with-limited-levels
  18527. (save-excursion
  18528. (goto-char beg)
  18529. (cond
  18530. ;; Case 1. Start at an item: de-itemize. Note that it only
  18531. ;; happens when a region is active: `org-ctrl-c-minus'
  18532. ;; would call `org-cycle-list-bullet' otherwise.
  18533. ((org-at-item-p)
  18534. (while (< (point) end)
  18535. (when (org-at-item-p)
  18536. (skip-chars-forward " \t")
  18537. (delete-region (point) (match-end 0)))
  18538. (forward-line)))
  18539. ;; Case 2. Start at an heading: convert to items.
  18540. ((org-at-heading-p)
  18541. (let* ((bul (org-list-bullet-string "-"))
  18542. (bul-len (length bul))
  18543. ;; Indentation of the first heading. It should be
  18544. ;; relative to the indentation of its parent, if any.
  18545. (start-ind (save-excursion
  18546. (cond
  18547. ((not org-adapt-indentation) 0)
  18548. ((not (outline-previous-heading)) 0)
  18549. (t (length (match-string 0))))))
  18550. ;; Level of first heading. Further headings will be
  18551. ;; compared to it to determine hierarchy in the list.
  18552. (ref-level (org-reduced-level (org-outline-level))))
  18553. (while (< (point) end)
  18554. (let* ((level (org-reduced-level (org-outline-level)))
  18555. (delta (max 0 (- level ref-level))))
  18556. ;; If current headline is less indented than the first
  18557. ;; one, set it as reference, in order to preserve
  18558. ;; subtrees.
  18559. (when (< level ref-level) (setq ref-level level))
  18560. (replace-match bul t t)
  18561. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18562. ;; Ensure all text down to END (or SECTION-END) belongs
  18563. ;; to the newly created item.
  18564. (let ((section-end (save-excursion
  18565. (or (outline-next-heading) (point)))))
  18566. (forward-line)
  18567. (funcall shift-text
  18568. (+ start-ind (* (1+ delta) bul-len))
  18569. (min end section-end)))))))
  18570. ;; Case 3. Normal line with ARG: make the first line of region
  18571. ;; an item, and shift indentation of others lines to
  18572. ;; set them as item's body.
  18573. (arg (let* ((bul (org-list-bullet-string "-"))
  18574. (bul-len (length bul))
  18575. (ref-ind (org-get-indentation)))
  18576. (skip-chars-forward " \t")
  18577. (insert bul)
  18578. (forward-line)
  18579. (while (< (point) end)
  18580. ;; Ensure that lines less indented than first one
  18581. ;; still get included in item body.
  18582. (funcall shift-text
  18583. (+ ref-ind bul-len)
  18584. (min end (save-excursion (or (outline-next-heading)
  18585. (point)))))
  18586. (forward-line))))
  18587. ;; Case 4. Normal line without ARG: turn each non-item line
  18588. ;; into an item.
  18589. (t
  18590. (while (< (point) end)
  18591. (unless (or (org-at-heading-p) (org-at-item-p))
  18592. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18593. (replace-match
  18594. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18595. (forward-line))))))))
  18596. (defun org-toggle-heading (&optional nstars)
  18597. "Convert headings to normal text, or items or text to headings.
  18598. If there is no active region, only convert the current line.
  18599. With a \\[universal-argument] prefix, convert the whole list at
  18600. point into heading.
  18601. In a region:
  18602. - If the first non blank line is a headline, remove the stars
  18603. from all headlines in the region.
  18604. - If it is a normal line, turn each and every normal line (i.e.,
  18605. not an heading or an item) in the region into headings. If you
  18606. want to convert only the first line of this region, use one
  18607. universal prefix argument.
  18608. - If it is a plain list item, turn all plain list items into headings.
  18609. When converting a line into a heading, the number of stars is chosen
  18610. such that the lines become children of the current entry. However,
  18611. when a numeric prefix argument is given, its value determines the
  18612. number of stars to add."
  18613. (interactive "P")
  18614. (let ((skip-blanks
  18615. (function
  18616. ;; Return beginning of first non-blank line, starting from
  18617. ;; line at POS.
  18618. (lambda (pos)
  18619. (save-excursion
  18620. (goto-char pos)
  18621. (while (org-at-comment-p) (forward-line))
  18622. (skip-chars-forward " \r\t\n")
  18623. (point-at-bol)))))
  18624. beg end toggled)
  18625. ;; Determine boundaries of changes. If a universal prefix has
  18626. ;; been given, put the list in a region. If region ends at a bol,
  18627. ;; do not consider the last line to be in the region.
  18628. (when (and current-prefix-arg (org-at-item-p))
  18629. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18630. (org-mark-element))
  18631. (if (org-region-active-p)
  18632. (setq beg (funcall skip-blanks (region-beginning))
  18633. end (copy-marker (save-excursion
  18634. (goto-char (region-end))
  18635. (if (bolp) (point) (point-at-eol)))))
  18636. (setq beg (funcall skip-blanks (point-at-bol))
  18637. end (copy-marker (point-at-eol))))
  18638. ;; Ensure inline tasks don't count as headings.
  18639. (org-with-limited-levels
  18640. (save-excursion
  18641. (goto-char beg)
  18642. (cond
  18643. ;; Case 1. Started at an heading: de-star headings.
  18644. ((org-at-heading-p)
  18645. (while (< (point) end)
  18646. (when (org-at-heading-p t)
  18647. (looking-at org-outline-regexp) (replace-match "")
  18648. (setq toggled t))
  18649. (forward-line)))
  18650. ;; Case 2. Started at an item: change items into headlines.
  18651. ;; One star will be added by `org-list-to-subtree'.
  18652. ((org-at-item-p)
  18653. (let* ((stars (make-string
  18654. ;; subtract the star that will be added again by
  18655. ;; `org-list-to-subtree'
  18656. (if (numberp nstars) (1- nstars)
  18657. (or (org-current-level) 0))
  18658. ?*))
  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. (while (< (point) end)
  18665. (when (org-at-item-p)
  18666. ;; Pay attention to cases when region ends before list.
  18667. (let* ((struct (org-list-struct))
  18668. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18669. (save-restriction
  18670. (narrow-to-region (point) list-end)
  18671. (insert
  18672. (org-list-to-subtree
  18673. (org-list-parse-list t)
  18674. '(:istart (concat stars add-stars (funcall get-stars depth))
  18675. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18676. (setq toggled t))
  18677. (forward-line))))
  18678. ;; Case 3. Started at normal text: make every line an heading,
  18679. ;; skipping headlines and items.
  18680. (t (let* ((stars
  18681. (make-string
  18682. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18683. (add-stars
  18684. (cond (nstars "") ; stars from prefix only
  18685. ((equal stars "") "*") ; before first heading
  18686. (org-odd-levels-only "**") ; inside heading, odd
  18687. (t "*"))) ; inside heading, oddeven
  18688. (rpl (concat stars add-stars " "))
  18689. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18690. (while (< (point) (if (equal nstars '(4)) lend end))
  18691. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18692. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18693. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18694. (forward-line)))))))
  18695. (unless toggled (message "Cannot toggle heading from here"))))
  18696. (defun org-meta-return (&optional arg)
  18697. "Insert a new heading or wrap a region in a table.
  18698. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18699. See the individual commands for more information."
  18700. (interactive "P")
  18701. (cond
  18702. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18703. ((or (org-at-drawer-p) (org-at-property-p))
  18704. (newline-and-indent))
  18705. ((org-at-table-p)
  18706. (call-interactively 'org-table-wrap-region))
  18707. (t (call-interactively 'org-insert-heading))))
  18708. ;;; Menu entries
  18709. (defsubst org-in-subtree-not-table-p ()
  18710. "Are we in a subtree and not in a table?"
  18711. (and (not (org-before-first-heading-p))
  18712. (not (org-at-table-p))))
  18713. ;; Define the Org-mode menus
  18714. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18715. '("Tbl"
  18716. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18717. ["Next Field" org-cycle (org-at-table-p)]
  18718. ["Previous Field" org-shifttab (org-at-table-p)]
  18719. ["Next Row" org-return (org-at-table-p)]
  18720. "--"
  18721. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18722. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18723. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18724. "--"
  18725. ("Column"
  18726. ["Move Column Left" org-metaleft (org-at-table-p)]
  18727. ["Move Column Right" org-metaright (org-at-table-p)]
  18728. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18729. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18730. ("Row"
  18731. ["Move Row Up" org-metaup (org-at-table-p)]
  18732. ["Move Row Down" org-metadown (org-at-table-p)]
  18733. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18734. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18735. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18736. "--"
  18737. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18738. ("Rectangle"
  18739. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18740. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18741. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18742. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18743. "--"
  18744. ("Calculate"
  18745. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18746. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18747. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18748. "--"
  18749. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18750. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18751. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18752. "--"
  18753. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18754. "--"
  18755. ["Sum Column/Rectangle" org-table-sum
  18756. (or (org-at-table-p) (org-region-active-p))]
  18757. ["Which Column?" org-table-current-column (org-at-table-p)])
  18758. ["Debug Formulas"
  18759. org-table-toggle-formula-debugger
  18760. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18761. ["Show Col/Row Numbers"
  18762. org-table-toggle-coordinate-overlays
  18763. :style toggle
  18764. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18765. "--"
  18766. ["Create" org-table-create (and (not (org-at-table-p))
  18767. org-enable-table-editor)]
  18768. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18769. ["Import from File" org-table-import (not (org-at-table-p))]
  18770. ["Export to File" org-table-export (org-at-table-p)]
  18771. "--"
  18772. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18773. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18774. '("Org"
  18775. ("Show/Hide"
  18776. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18777. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18778. ["Sparse Tree..." org-sparse-tree t]
  18779. ["Reveal Context" org-reveal t]
  18780. ["Show All" show-all t]
  18781. "--"
  18782. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18783. "--"
  18784. ["New Heading" org-insert-heading t]
  18785. ("Navigate Headings"
  18786. ["Up" outline-up-heading t]
  18787. ["Next" outline-next-visible-heading t]
  18788. ["Previous" outline-previous-visible-heading t]
  18789. ["Next Same Level" outline-forward-same-level t]
  18790. ["Previous Same Level" outline-backward-same-level t]
  18791. "--"
  18792. ["Jump" org-goto t])
  18793. ("Edit Structure"
  18794. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18795. "--"
  18796. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18797. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18798. "--"
  18799. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18800. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18801. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18802. "--"
  18803. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18804. "--"
  18805. ["Copy visible text" org-copy-visible t]
  18806. "--"
  18807. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18808. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18809. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18810. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18811. "--"
  18812. ["Sort Region/Children" org-sort t]
  18813. "--"
  18814. ["Convert to odd levels" org-convert-to-odd-levels t]
  18815. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18816. ("Editing"
  18817. ["Emphasis..." org-emphasize t]
  18818. ["Edit Source Example" org-edit-special t]
  18819. "--"
  18820. ["Footnote new/jump" org-footnote-action t]
  18821. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18822. ("Archive"
  18823. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18824. "--"
  18825. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18826. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18827. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18828. )
  18829. "--"
  18830. ("Hyperlinks"
  18831. ["Store Link (Global)" org-store-link t]
  18832. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18833. ["Insert Link" org-insert-link t]
  18834. ["Follow Link" org-open-at-point t]
  18835. "--"
  18836. ["Next link" org-next-link t]
  18837. ["Previous link" org-previous-link t]
  18838. "--"
  18839. ["Descriptive Links"
  18840. org-toggle-link-display
  18841. :style radio
  18842. :selected org-descriptive-links
  18843. ]
  18844. ["Literal Links"
  18845. org-toggle-link-display
  18846. :style radio
  18847. :selected (not org-descriptive-links)])
  18848. "--"
  18849. ("TODO Lists"
  18850. ["TODO/DONE/-" org-todo t]
  18851. ("Select keyword"
  18852. ["Next keyword" org-shiftright (org-at-heading-p)]
  18853. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18854. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18855. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18856. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18857. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18858. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18859. "--"
  18860. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18861. :selected org-enforce-todo-dependencies :style toggle :active t]
  18862. "Settings for tree at point"
  18863. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18864. :selected (org-entry-get nil "ORDERED")
  18865. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18866. ["Do Children parallel" org-toggle-ordered-property :style radio
  18867. :selected (not (org-entry-get nil "ORDERED"))
  18868. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18869. "--"
  18870. ["Set Priority" org-priority t]
  18871. ["Priority Up" org-shiftup t]
  18872. ["Priority Down" org-shiftdown t]
  18873. "--"
  18874. ["Get news from all feeds" org-feed-update-all t]
  18875. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18876. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18877. ("TAGS and Properties"
  18878. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18879. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18880. "--"
  18881. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18882. ["Column view of properties" org-columns t]
  18883. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18884. ("Dates and Scheduling"
  18885. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18886. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18887. ("Change Date"
  18888. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18889. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18890. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18891. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18892. ["Compute Time Range" org-evaluate-time-range t]
  18893. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18894. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18895. "--"
  18896. ["Custom time format" org-toggle-time-stamp-overlays
  18897. :style radio :selected org-display-custom-times]
  18898. "--"
  18899. ["Goto Calendar" org-goto-calendar t]
  18900. ["Date from Calendar" org-date-from-calendar t]
  18901. "--"
  18902. ["Start/Restart Timer" org-timer-start t]
  18903. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18904. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18905. ["Insert Timer String" org-timer t]
  18906. ["Insert Timer Item" org-timer-item t])
  18907. ("Logging work"
  18908. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18909. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18910. ["Clock out" org-clock-out t]
  18911. ["Clock cancel" org-clock-cancel t]
  18912. "--"
  18913. ["Mark as default task" org-clock-mark-default-task t]
  18914. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18915. ["Goto running clock" org-clock-goto t]
  18916. "--"
  18917. ["Display times" org-clock-display t]
  18918. ["Create clock table" org-clock-report t]
  18919. "--"
  18920. ["Record DONE time"
  18921. (progn (setq org-log-done (not org-log-done))
  18922. (message "Switching to %s will %s record a timestamp"
  18923. (car org-done-keywords)
  18924. (if org-log-done "automatically" "not")))
  18925. :style toggle :selected org-log-done])
  18926. "--"
  18927. ["Agenda Command..." org-agenda t]
  18928. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18929. ("File List for Agenda")
  18930. ("Special views current file"
  18931. ["TODO Tree" org-show-todo-tree t]
  18932. ["Check Deadlines" org-check-deadlines t]
  18933. ["Timeline" org-timeline t]
  18934. ["Tags/Property tree" org-match-sparse-tree t])
  18935. "--"
  18936. ["Export/Publish..." org-export-dispatch t]
  18937. ("LaTeX"
  18938. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18939. :selected org-cdlatex-mode]
  18940. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18941. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18942. ["Modify math symbol" org-cdlatex-math-modify
  18943. (org-inside-LaTeX-fragment-p)]
  18944. ["Insert citation" org-reftex-citation t]
  18945. "--"
  18946. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18947. "--"
  18948. ("MobileOrg"
  18949. ["Push Files and Views" org-mobile-push t]
  18950. ["Get Captured and Flagged" org-mobile-pull t]
  18951. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18952. "--"
  18953. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18954. "--"
  18955. ("Documentation"
  18956. ["Show Version" org-version t]
  18957. ["Info Documentation" org-info t])
  18958. ("Customize"
  18959. ["Browse Org Group" org-customize t]
  18960. "--"
  18961. ["Expand This Menu" org-create-customize-menu
  18962. (fboundp 'customize-menu-create)])
  18963. ["Send bug report" org-submit-bug-report t]
  18964. "--"
  18965. ("Refresh/Reload"
  18966. ["Refresh setup current buffer" org-mode-restart t]
  18967. ["Reload Org (after update)" org-reload t]
  18968. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18969. ))
  18970. (defun org-info (&optional node)
  18971. "Read documentation for Org-mode in the info system.
  18972. With optional NODE, go directly to that node."
  18973. (interactive)
  18974. (info (format "(org)%s" (or node ""))))
  18975. ;;;###autoload
  18976. (defun org-submit-bug-report ()
  18977. "Submit a bug report on Org-mode via mail.
  18978. Don't hesitate to report any problems or inaccurate documentation.
  18979. If you don't have setup sending mail from (X)Emacs, please copy the
  18980. output buffer into your mail program, as it gives us important
  18981. information about your Org-mode version and configuration."
  18982. (interactive)
  18983. (require 'reporter)
  18984. (org-load-modules-maybe)
  18985. (org-require-autoloaded-modules)
  18986. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18987. (reporter-submit-bug-report
  18988. "emacs-orgmode@gnu.org"
  18989. (org-version nil 'full)
  18990. (let (list)
  18991. (save-window-excursion
  18992. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18993. (delete-other-windows)
  18994. (erase-buffer)
  18995. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18996. We would like to add your full Org-mode and Outline configuration to the
  18997. bug report. This greatly simplifies the work of the maintainer and
  18998. other experts on the mailing list.
  18999. HOWEVER, some variables you have customized may contain private
  19000. information. The names of customers, colleagues, or friends, might
  19001. appear in the form of file names, tags, todo states, or search strings.
  19002. If you answer yes to the prompt, you might want to check and remove
  19003. such private information before sending the email.")
  19004. (add-text-properties (point-min) (point-max) '(face org-warning))
  19005. (when (yes-or-no-p "Include your Org-mode configuration ")
  19006. (mapatoms
  19007. (lambda (v)
  19008. (and (boundp v)
  19009. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19010. (or (and (symbol-value v)
  19011. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19012. (and
  19013. (get v 'custom-type) (get v 'standard-value)
  19014. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19015. (push v list)))))
  19016. (kill-buffer (get-buffer "*Warn about privacy*"))
  19017. list))
  19018. nil nil
  19019. "Remember to cover the basics, that is, what you expected to happen and
  19020. what in fact did happen. You don't know how to make a good report? See
  19021. http://orgmode.org/manual/Feedback.html#Feedback
  19022. Your bug report will be posted to the Org-mode mailing list.
  19023. ------------------------------------------------------------------------")
  19024. (save-excursion
  19025. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19026. (replace-match "\\1Bug: \\3 [\\2]")))))
  19027. (defun org-install-agenda-files-menu ()
  19028. (let ((bl (buffer-list)))
  19029. (save-excursion
  19030. (while bl
  19031. (set-buffer (pop bl))
  19032. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19033. (when (derived-mode-p 'org-mode)
  19034. (easy-menu-change
  19035. '("Org") "File List for Agenda"
  19036. (append
  19037. (list
  19038. ["Edit File List" (org-edit-agenda-file-list) t]
  19039. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19040. ["Remove Current File from List" org-remove-file t]
  19041. ["Cycle through agenda files" org-cycle-agenda-files t]
  19042. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19043. "--")
  19044. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19045. ;;;; Documentation
  19046. (defun org-require-autoloaded-modules ()
  19047. (interactive)
  19048. (mapc 'require
  19049. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19050. org-remember org-table org-timer)))
  19051. ;;;###autoload
  19052. (defun org-reload (&optional uncompiled)
  19053. "Reload all org lisp files.
  19054. With prefix arg UNCOMPILED, load the uncompiled versions."
  19055. (interactive "P")
  19056. (require 'loadhist)
  19057. (let* ((org-dir (org-find-library-dir "org"))
  19058. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19059. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19060. (remove-re (mapconcat 'identity
  19061. (mapcar (lambda (f) (concat "^" f "$"))
  19062. (list (if (featurep 'xemacs)
  19063. "org-colview"
  19064. "org-colview-xemacs")
  19065. "org" "org-loaddefs" "org-version"))
  19066. "\\|"))
  19067. (feats (delete-dups
  19068. (mapcar 'file-name-sans-extension
  19069. (mapcar 'file-name-nondirectory
  19070. (delq nil
  19071. (mapcar 'feature-file
  19072. features))))))
  19073. (lfeat (append
  19074. (sort
  19075. (setq feats
  19076. (delq nil (mapcar
  19077. (lambda (f)
  19078. (if (and (string-match feature-re f)
  19079. (not (string-match remove-re f)))
  19080. f nil))
  19081. feats)))
  19082. 'string-lessp)
  19083. (list "org-version" "org")))
  19084. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19085. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19086. load-uncore load-misses)
  19087. (setq load-misses
  19088. (delq 't
  19089. (mapcar (lambda (f)
  19090. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19091. (and (string= org-dir contrib-dir)
  19092. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19093. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19094. (add-to-list 'load-uncore f 'append)
  19095. 't)
  19096. f))
  19097. lfeat)))
  19098. (if load-uncore
  19099. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19100. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19101. (if load-misses
  19102. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19103. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19104. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19105. ;;;###autoload
  19106. (defun org-customize ()
  19107. "Call the customize function with org as argument."
  19108. (interactive)
  19109. (org-load-modules-maybe)
  19110. (org-require-autoloaded-modules)
  19111. (customize-browse 'org))
  19112. (defun org-create-customize-menu ()
  19113. "Create a full customization menu for Org-mode, insert it into the menu."
  19114. (interactive)
  19115. (org-load-modules-maybe)
  19116. (org-require-autoloaded-modules)
  19117. (if (fboundp 'customize-menu-create)
  19118. (progn
  19119. (easy-menu-change
  19120. '("Org") "Customize"
  19121. `(["Browse Org group" org-customize t]
  19122. "--"
  19123. ,(customize-menu-create 'org)
  19124. ["Set" Custom-set t]
  19125. ["Save" Custom-save t]
  19126. ["Reset to Current" Custom-reset-current t]
  19127. ["Reset to Saved" Custom-reset-saved t]
  19128. ["Reset to Standard Settings" Custom-reset-standard t]))
  19129. (message "\"Org\"-menu now contains full customization menu"))
  19130. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19131. ;;;; Miscellaneous stuff
  19132. ;;; Generally useful functions
  19133. (defun org-get-at-bol (property)
  19134. "Get text property PROPERTY at beginning of line."
  19135. (get-text-property (point-at-bol) property))
  19136. (defun org-find-text-property-in-string (prop s)
  19137. "Return the first non-nil value of property PROP in string S."
  19138. (or (get-text-property 0 prop s)
  19139. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19140. prop s)))
  19141. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19142. "Display the given MESSAGE as a warning."
  19143. (if (fboundp 'display-warning)
  19144. (display-warning 'org message
  19145. (if (featurep 'xemacs) 'warning :warning))
  19146. (let ((buf (get-buffer-create "*Org warnings*")))
  19147. (with-current-buffer buf
  19148. (goto-char (point-max))
  19149. (insert "Warning (Org): " message)
  19150. (unless (bolp)
  19151. (newline)))
  19152. (display-buffer buf)
  19153. (sit-for 0))))
  19154. (defun org-eval (form)
  19155. "Eval FORM and return result."
  19156. (condition-case error
  19157. (eval form)
  19158. (error (format "%%![Error: %s]" error))))
  19159. (defun org-in-clocktable-p ()
  19160. "Check if the cursor is in a clocktable."
  19161. (let ((pos (point)) start)
  19162. (save-excursion
  19163. (end-of-line 1)
  19164. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19165. (setq start (match-beginning 0))
  19166. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19167. (>= (match-end 0) pos)
  19168. start))))
  19169. (defun org-in-commented-line ()
  19170. "Is point in a line starting with `#'?"
  19171. (equal (char-after (point-at-bol)) ?#))
  19172. (defun org-in-indented-comment-line ()
  19173. "Is point in a line starting with `#' after some white space?"
  19174. (save-excursion
  19175. (save-match-data
  19176. (goto-char (point-at-bol))
  19177. (looking-at "[ \t]*#"))))
  19178. (defun org-in-verbatim-emphasis ()
  19179. (save-match-data
  19180. (and (org-in-regexp org-emph-re 2)
  19181. (>= (point) (match-beginning 3))
  19182. (<= (point) (match-end 4))
  19183. (member (match-string 3) '("=" "~")))))
  19184. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19185. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19186. (if (and marker (marker-buffer marker)
  19187. (buffer-live-p (marker-buffer marker)))
  19188. (progn
  19189. (org-pop-to-buffer-same-window (marker-buffer marker))
  19190. (if (or (> marker (point-max)) (< marker (point-min)))
  19191. (widen))
  19192. (goto-char marker)
  19193. (org-show-context 'org-goto))
  19194. (if bookmark
  19195. (bookmark-jump bookmark)
  19196. (error "Cannot find location"))))
  19197. (defun org-quote-csv-field (s)
  19198. "Quote field for inclusion in CSV material."
  19199. (if (string-match "[\",]" s)
  19200. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19201. s))
  19202. (defun org-force-self-insert (N)
  19203. "Needed to enforce self-insert under remapping."
  19204. (interactive "p")
  19205. (self-insert-command N))
  19206. (defun org-string-width (s)
  19207. "Compute width of string, ignoring invisible characters.
  19208. This ignores character with invisibility property `org-link', and also
  19209. characters with property `org-cwidth', because these will become invisible
  19210. upon the next fontification round."
  19211. (let (b l)
  19212. (when (or (eq t buffer-invisibility-spec)
  19213. (assq 'org-link buffer-invisibility-spec))
  19214. (while (setq b (text-property-any 0 (length s)
  19215. 'invisible 'org-link s))
  19216. (setq s (concat (substring s 0 b)
  19217. (substring s (or (next-single-property-change
  19218. b 'invisible s) (length s)))))))
  19219. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19220. (setq s (concat (substring s 0 b)
  19221. (substring s (or (next-single-property-change
  19222. b 'org-cwidth s) (length s))))))
  19223. (setq l (string-width s) b -1)
  19224. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19225. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19226. l))
  19227. (defun org-shorten-string (s maxlength)
  19228. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19229. If the string is shorter or has length MAXLENGTH, just return the
  19230. original string. If it is longer, the functions finds a space in the
  19231. string, breaks this string off at that locations and adds three dots
  19232. as ellipsis. Including the ellipsis, the string will not be longer
  19233. than MAXLENGTH. If finding a good breaking point in the string does
  19234. not work, the string is just chopped off in the middle of a word
  19235. if necessary."
  19236. (if (<= (length s) maxlength)
  19237. s
  19238. (let* ((n (max (- maxlength 4) 1))
  19239. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19240. (if (string-match re s)
  19241. (concat (match-string 1 s) "...")
  19242. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19243. (defun org-get-indentation (&optional line)
  19244. "Get the indentation of the current line, interpreting tabs.
  19245. When LINE is given, assume it represents a line and compute its indentation."
  19246. (if line
  19247. (if (string-match "^ *" (org-remove-tabs line))
  19248. (match-end 0))
  19249. (save-excursion
  19250. (beginning-of-line 1)
  19251. (skip-chars-forward " \t")
  19252. (current-column))))
  19253. (defun org-get-string-indentation (s)
  19254. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19255. (let ((n -1) (i 0) (w tab-width) c)
  19256. (catch 'exit
  19257. (while (< (setq n (1+ n)) (length s))
  19258. (setq c (aref s n))
  19259. (cond ((= c ?\ ) (setq i (1+ i)))
  19260. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19261. (t (throw 'exit t)))))
  19262. i))
  19263. (defun org-remove-tabs (s &optional width)
  19264. "Replace tabulators in S with spaces.
  19265. Assumes that s is a single line, starting in column 0."
  19266. (setq width (or width tab-width))
  19267. (while (string-match "\t" s)
  19268. (setq s (replace-match
  19269. (make-string
  19270. (- (* width (/ (+ (match-beginning 0) width) width))
  19271. (match-beginning 0)) ?\ )
  19272. t t s)))
  19273. s)
  19274. (defun org-fix-indentation (line ind)
  19275. "Fix indentation in LINE.
  19276. IND is a cons cell with target and minimum indentation.
  19277. If the current indentation in LINE is smaller than the minimum,
  19278. leave it alone. If it is larger than ind, set it to the target."
  19279. (let* ((l (org-remove-tabs line))
  19280. (i (org-get-indentation l))
  19281. (i1 (car ind)) (i2 (cdr ind)))
  19282. (if (>= i i2) (setq l (substring line i2)))
  19283. (if (> i1 0)
  19284. (concat (make-string i1 ?\ ) l)
  19285. l)))
  19286. (defun org-remove-indentation (code &optional n)
  19287. "Remove the maximum common indentation from the lines in CODE.
  19288. N may optionally be the number of spaces to remove."
  19289. (with-temp-buffer
  19290. (insert code)
  19291. (org-do-remove-indentation n)
  19292. (buffer-string)))
  19293. (defun org-do-remove-indentation (&optional n)
  19294. "Remove the maximum common indentation from the buffer."
  19295. (untabify (point-min) (point-max))
  19296. (let ((min 10000) re)
  19297. (if n
  19298. (setq min n)
  19299. (goto-char (point-min))
  19300. (while (re-search-forward "^ *[^ \n]" nil t)
  19301. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19302. (unless (or (= min 0) (= min 10000))
  19303. (setq re (format "^ \\{%d\\}" min))
  19304. (goto-char (point-min))
  19305. (while (re-search-forward re nil t)
  19306. (replace-match "")
  19307. (end-of-line 1))
  19308. min)))
  19309. (defun org-fill-template (template alist)
  19310. "Find each %key of ALIST in TEMPLATE and replace it."
  19311. (let ((case-fold-search nil)
  19312. entry key value)
  19313. (setq alist (sort (copy-sequence alist)
  19314. (lambda (a b) (< (length (car a)) (length (car b))))))
  19315. (while (setq entry (pop alist))
  19316. (setq template
  19317. (replace-regexp-in-string
  19318. (concat "%" (regexp-quote (car entry)))
  19319. (or (cdr entry) "") template t t)))
  19320. template))
  19321. (defun org-base-buffer (buffer)
  19322. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19323. (if (not buffer)
  19324. buffer
  19325. (or (buffer-base-buffer buffer)
  19326. buffer)))
  19327. (defun org-trim (s)
  19328. "Remove whitespace at beginning and end of string."
  19329. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19330. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19331. s)
  19332. (defun org-wrap (string &optional width lines)
  19333. "Wrap string to either a number of lines, or a width in characters.
  19334. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19335. that costs. If there is a word longer than WIDTH, the text is actually
  19336. wrapped to the length of that word.
  19337. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19338. many lines, whatever width that takes.
  19339. The return value is a list of lines, without newlines at the end."
  19340. (let* ((words (org-split-string string "[ \t\n]+"))
  19341. (maxword (apply 'max (mapcar 'org-string-width words)))
  19342. w ll)
  19343. (cond (width
  19344. (org-do-wrap words (max maxword width)))
  19345. (lines
  19346. (setq w maxword)
  19347. (setq ll (org-do-wrap words maxword))
  19348. (if (<= (length ll) lines)
  19349. ll
  19350. (setq ll words)
  19351. (while (> (length ll) lines)
  19352. (setq w (1+ w))
  19353. (setq ll (org-do-wrap words w)))
  19354. ll))
  19355. (t (error "Cannot wrap this")))))
  19356. (defun org-do-wrap (words width)
  19357. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19358. (let (lines line)
  19359. (while words
  19360. (setq line (pop words))
  19361. (while (and words (< (+ (length line) (length (car words))) width))
  19362. (setq line (concat line " " (pop words))))
  19363. (setq lines (push line lines)))
  19364. (nreverse lines)))
  19365. (defun org-split-string (string &optional separators)
  19366. "Splits STRING into substrings at SEPARATORS.
  19367. No empty strings are returned if there are matches at the beginning
  19368. and end of string."
  19369. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19370. (start 0)
  19371. notfirst
  19372. (list nil))
  19373. (while (and (string-match rexp string
  19374. (if (and notfirst
  19375. (= start (match-beginning 0))
  19376. (< start (length string)))
  19377. (1+ start) start))
  19378. (< (match-beginning 0) (length string)))
  19379. (setq notfirst t)
  19380. (or (eq (match-beginning 0) 0)
  19381. (and (eq (match-beginning 0) (match-end 0))
  19382. (eq (match-beginning 0) start))
  19383. (setq list
  19384. (cons (substring string start (match-beginning 0))
  19385. list)))
  19386. (setq start (match-end 0)))
  19387. (or (eq start (length string))
  19388. (setq list
  19389. (cons (substring string start)
  19390. list)))
  19391. (nreverse list)))
  19392. (defun org-quote-vert (s)
  19393. "Replace \"|\" with \"\\vert\"."
  19394. (while (string-match "|" s)
  19395. (setq s (replace-match "\\vert" t t s)))
  19396. s)
  19397. (defun org-uuidgen-p (s)
  19398. "Is S an ID created by UUIDGEN?"
  19399. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19400. (defun org-in-src-block-p (&optional inside)
  19401. "Whether point is in a code source block.
  19402. When INSIDE is non-nil, don't consider we are within a src block
  19403. when point is at #+BEGIN_SRC or #+END_SRC."
  19404. (let ((case-fold-search t) ov)
  19405. (or (and (setq ov (overlays-at (point)))
  19406. (memq 'org-block-background
  19407. (overlay-properties (car ov))))
  19408. (and (not inside)
  19409. (save-match-data
  19410. (save-excursion
  19411. (beginning-of-line)
  19412. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19413. (defun org-context ()
  19414. "Return a list of contexts of the current cursor position.
  19415. If several contexts apply, all are returned.
  19416. Each context entry is a list with a symbol naming the context, and
  19417. two positions indicating start and end of the context. Possible
  19418. contexts are:
  19419. :headline anywhere in a headline
  19420. :headline-stars on the leading stars in a headline
  19421. :todo-keyword on a TODO keyword (including DONE) in a headline
  19422. :tags on the TAGS in a headline
  19423. :priority on the priority cookie in a headline
  19424. :item on the first line of a plain list item
  19425. :item-bullet on the bullet/number of a plain list item
  19426. :checkbox on the checkbox in a plain list item
  19427. :table in an org-mode table
  19428. :table-special on a special filed in a table
  19429. :table-table in a table.el table
  19430. :clocktable in a clocktable
  19431. :src-block in a source block
  19432. :link on a hyperlink
  19433. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19434. :target on a <<target>>
  19435. :radio-target on a <<<radio-target>>>
  19436. :latex-fragment on a LaTeX fragment
  19437. :latex-preview on a LaTeX fragment with overlaid preview image
  19438. This function expects the position to be visible because it uses font-lock
  19439. faces as a help to recognize the following contexts: :table-special, :link,
  19440. and :keyword."
  19441. (let* ((f (get-text-property (point) 'face))
  19442. (faces (if (listp f) f (list f)))
  19443. (case-fold-search t)
  19444. (p (point)) clist o)
  19445. ;; First the large context
  19446. (cond
  19447. ((org-at-heading-p t)
  19448. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19449. (when (progn
  19450. (beginning-of-line 1)
  19451. (looking-at org-todo-line-tags-regexp))
  19452. (push (org-point-in-group p 1 :headline-stars) clist)
  19453. (push (org-point-in-group p 2 :todo-keyword) clist)
  19454. (push (org-point-in-group p 4 :tags) clist))
  19455. (goto-char p)
  19456. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19457. (if (looking-at "\\[#[A-Z0-9]\\]")
  19458. (push (org-point-in-group p 0 :priority) clist)))
  19459. ((org-at-item-p)
  19460. (push (org-point-in-group p 2 :item-bullet) clist)
  19461. (push (list :item (point-at-bol)
  19462. (save-excursion (org-end-of-item) (point)))
  19463. clist)
  19464. (and (org-at-item-checkbox-p)
  19465. (push (org-point-in-group p 0 :checkbox) clist)))
  19466. ((org-at-table-p)
  19467. (push (list :table (org-table-begin) (org-table-end)) clist)
  19468. (if (memq 'org-formula faces)
  19469. (push (list :table-special
  19470. (previous-single-property-change p 'face)
  19471. (next-single-property-change p 'face)) clist)))
  19472. ((org-at-table-p 'any)
  19473. (push (list :table-table) clist)))
  19474. (goto-char p)
  19475. (let ((case-fold-search t))
  19476. ;; New the "medium" contexts: clocktables, source blocks
  19477. (cond ((org-in-clocktable-p)
  19478. (push (list :clocktable
  19479. (and (or (looking-at "#\\+BEGIN: clocktable")
  19480. (search-backward "#+BEGIN: clocktable" nil t))
  19481. (match-beginning 0))
  19482. (and (re-search-forward "#\\+END:?" nil t)
  19483. (match-end 0))) clist))
  19484. ((org-in-src-block-p)
  19485. (push (list :src-block
  19486. (and (or (looking-at "#\\+BEGIN_SRC")
  19487. (search-backward "#+BEGIN_SRC" nil t))
  19488. (match-beginning 0))
  19489. (and (search-forward "#+END_SRC" nil t)
  19490. (match-beginning 0))) clist))))
  19491. (goto-char p)
  19492. ;; Now the small context
  19493. (cond
  19494. ((org-at-timestamp-p)
  19495. (push (org-point-in-group p 0 :timestamp) clist))
  19496. ((memq 'org-link faces)
  19497. (push (list :link
  19498. (previous-single-property-change p 'face)
  19499. (next-single-property-change p 'face)) clist))
  19500. ((memq 'org-special-keyword faces)
  19501. (push (list :keyword
  19502. (previous-single-property-change p 'face)
  19503. (next-single-property-change p 'face)) clist))
  19504. ((org-at-target-p)
  19505. (push (org-point-in-group p 0 :target) clist)
  19506. (goto-char (1- (match-beginning 0)))
  19507. (if (looking-at org-radio-target-regexp)
  19508. (push (org-point-in-group p 0 :radio-target) clist))
  19509. (goto-char p))
  19510. ((setq o (car (delq nil
  19511. (mapcar
  19512. (lambda (x)
  19513. (if (memq x org-latex-fragment-image-overlays) x))
  19514. (overlays-at (point))))))
  19515. (push (list :latex-fragment
  19516. (overlay-start o) (overlay-end o)) clist)
  19517. (push (list :latex-preview
  19518. (overlay-start o) (overlay-end o)) clist))
  19519. ((org-inside-LaTeX-fragment-p)
  19520. ;; FIXME: positions wrong.
  19521. (push (list :latex-fragment (point) (point)) clist)))
  19522. (setq clist (nreverse (delq nil clist)))
  19523. clist))
  19524. ;; FIXME: Compare with at-regexp-p Do we need both?
  19525. (defun org-in-regexp (re &optional nlines visually)
  19526. "Check if point is inside a match of regexp.
  19527. Normally only the current line is checked, but you can include NLINES extra
  19528. lines both before and after point into the search.
  19529. If VISUALLY is set, require that the cursor is not after the match but
  19530. really on, so that the block visually is on the match."
  19531. (catch 'exit
  19532. (let ((pos (point))
  19533. (eol (point-at-eol (+ 1 (or nlines 0))))
  19534. (inc (if visually 1 0)))
  19535. (save-excursion
  19536. (beginning-of-line (- 1 (or nlines 0)))
  19537. (while (re-search-forward re eol t)
  19538. (if (and (<= (match-beginning 0) pos)
  19539. (>= (+ inc (match-end 0)) pos))
  19540. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19541. (defun org-at-regexp-p (regexp)
  19542. "Is point inside a match of REGEXP in the current line?"
  19543. (catch 'exit
  19544. (save-excursion
  19545. (let ((pos (point)) (end (point-at-eol)))
  19546. (beginning-of-line 1)
  19547. (while (re-search-forward regexp end t)
  19548. (if (and (<= (match-beginning 0) pos)
  19549. (>= (match-end 0) pos))
  19550. (throw 'exit t)))
  19551. nil))))
  19552. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19553. "Non-nil when point is between matches of START-RE and END-RE.
  19554. Also return a non-nil value when point is on one of the matches.
  19555. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19556. buffer positions. Default values are the positions of headlines
  19557. surrounding the point.
  19558. The functions returns a cons cell whose car (resp. cdr) is the
  19559. position before START-RE (resp. after END-RE)."
  19560. (save-match-data
  19561. (let ((pos (point))
  19562. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19563. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19564. beg end)
  19565. (save-excursion
  19566. ;; Point is on a block when on START-RE or if START-RE can be
  19567. ;; found before it...
  19568. (and (or (org-at-regexp-p start-re)
  19569. (re-search-backward start-re limit-up t))
  19570. (setq beg (match-beginning 0))
  19571. ;; ... and END-RE after it...
  19572. (goto-char (match-end 0))
  19573. (re-search-forward end-re limit-down t)
  19574. (> (setq end (match-end 0)) pos)
  19575. ;; ... without another START-RE in-between.
  19576. (goto-char (match-beginning 0))
  19577. (not (re-search-backward start-re (1+ beg) t))
  19578. ;; Return value.
  19579. (cons beg end))))))
  19580. (defun org-in-block-p (names)
  19581. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19582. NAMES is a list of strings containing names of blocks.
  19583. Return first block name matched, or nil. Beware that in case of
  19584. nested blocks, the returned name may not belong to the closest
  19585. block from point."
  19586. (save-match-data
  19587. (catch 'exit
  19588. (let ((case-fold-search t)
  19589. (lim-up (save-excursion (outline-previous-heading)))
  19590. (lim-down (save-excursion (outline-next-heading))))
  19591. (mapc (lambda (name)
  19592. (let ((n (regexp-quote name)))
  19593. (when (org-between-regexps-p
  19594. (concat "^[ \t]*#\\+begin_" n)
  19595. (concat "^[ \t]*#\\+end_" n)
  19596. lim-up lim-down)
  19597. (throw 'exit n))))
  19598. names))
  19599. nil)))
  19600. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19601. "Call `multi-occur' with buffers for all agenda files."
  19602. (interactive "sOrg-files matching: \np")
  19603. (let* ((files (org-agenda-files))
  19604. (tnames (mapcar 'file-truename files))
  19605. (extra org-agenda-text-search-extra-files)
  19606. f)
  19607. (when (eq (car extra) 'agenda-archives)
  19608. (setq extra (cdr extra))
  19609. (setq files (org-add-archive-files files)))
  19610. (while (setq f (pop extra))
  19611. (unless (member (file-truename f) tnames)
  19612. (add-to-list 'files f 'append)
  19613. (add-to-list 'tnames (file-truename f) 'append)))
  19614. (multi-occur
  19615. (mapcar (lambda (x)
  19616. (with-current-buffer
  19617. (or (get-file-buffer x) (find-file-noselect x))
  19618. (widen)
  19619. (current-buffer)))
  19620. files)
  19621. regexp)))
  19622. (if (boundp 'occur-mode-find-occurrence-hook)
  19623. ;; Emacs 23
  19624. (add-hook 'occur-mode-find-occurrence-hook
  19625. (lambda ()
  19626. (when (derived-mode-p 'org-mode)
  19627. (org-reveal))))
  19628. ;; Emacs 22
  19629. (defadvice occur-mode-goto-occurrence
  19630. (after org-occur-reveal activate)
  19631. (and (derived-mode-p 'org-mode) (org-reveal)))
  19632. (defadvice occur-mode-goto-occurrence-other-window
  19633. (after org-occur-reveal activate)
  19634. (and (derived-mode-p 'org-mode) (org-reveal)))
  19635. (defadvice occur-mode-display-occurrence
  19636. (after org-occur-reveal activate)
  19637. (when (derived-mode-p 'org-mode)
  19638. (let ((pos (occur-mode-find-occurrence)))
  19639. (with-current-buffer (marker-buffer pos)
  19640. (save-excursion
  19641. (goto-char pos)
  19642. (org-reveal)))))))
  19643. (defun org-occur-link-in-agenda-files ()
  19644. "Create a link and search for it in the agendas.
  19645. The link is not stored in `org-stored-links', it is just created
  19646. for the search purpose."
  19647. (interactive)
  19648. (let ((link (condition-case nil
  19649. (org-store-link nil)
  19650. (error "Unable to create a link to here"))))
  19651. (org-occur-in-agenda-files (regexp-quote link))))
  19652. (defun org-reverse-string (string)
  19653. "Return the reverse of STRING."
  19654. (apply 'string (reverse (string-to-list string))))
  19655. (defun org-uniquify (list)
  19656. "Remove duplicate elements from LIST."
  19657. (let (res)
  19658. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19659. res))
  19660. (defun org-delete-all (elts list)
  19661. "Remove all elements in ELTS from LIST."
  19662. (while elts
  19663. (setq list (delete (pop elts) list)))
  19664. list)
  19665. (defun org-count (cl-item cl-seq)
  19666. "Count the number of occurrences of ITEM in SEQ.
  19667. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19668. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19669. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19670. (while (< cl-start cl-end)
  19671. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19672. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19673. (setq cl-start (1+ cl-start)))
  19674. cl-count))
  19675. (defun org-remove-if (predicate seq)
  19676. "Remove everything from SEQ that fulfills PREDICATE."
  19677. (let (res e)
  19678. (while seq
  19679. (setq e (pop seq))
  19680. (if (not (funcall predicate e)) (push e res)))
  19681. (nreverse res)))
  19682. (defun org-remove-if-not (predicate seq)
  19683. "Remove everything from SEQ that does not fulfill PREDICATE."
  19684. (let (res e)
  19685. (while seq
  19686. (setq e (pop seq))
  19687. (if (funcall predicate e) (push e res)))
  19688. (nreverse res)))
  19689. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19690. "Reduce two-argument FUNCTION across SEQ.
  19691. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19692. \":initial-value\" removed."
  19693. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19694. (cadr (memq :initial-value cl-keys)))
  19695. (cl-seq (pop cl-seq))
  19696. (t (funcall cl-func)))))
  19697. (while cl-seq
  19698. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19699. cl-accum))
  19700. (defun org-back-over-empty-lines ()
  19701. "Move backwards over whitespace, to the beginning of the first empty line.
  19702. Returns the number of empty lines passed."
  19703. (let ((pos (point)))
  19704. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19705. (skip-chars-backward " \t\n\r")
  19706. (unless (eobp)
  19707. (forward-line -1)))
  19708. (beginning-of-line 2)
  19709. (goto-char (min (point) pos))
  19710. (count-lines (point) pos)))
  19711. (defun org-skip-whitespace ()
  19712. (skip-chars-forward " \t\n\r"))
  19713. (defun org-point-in-group (point group &optional context)
  19714. "Check if POINT is in match-group GROUP.
  19715. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19716. match. If the match group does not exist or point is not inside it,
  19717. return nil."
  19718. (and (match-beginning group)
  19719. (>= point (match-beginning group))
  19720. (<= point (match-end group))
  19721. (if context
  19722. (list context (match-beginning group) (match-end group))
  19723. t)))
  19724. (defun org-switch-to-buffer-other-window (&rest args)
  19725. "Switch to buffer in a second window on the current frame.
  19726. In particular, do not allow pop-up frames.
  19727. Returns the newly created buffer."
  19728. (org-no-popups
  19729. (apply 'switch-to-buffer-other-window args)))
  19730. (defun org-combine-plists (&rest plists)
  19731. "Create a single property list from all plists in PLISTS.
  19732. The process starts by copying the first list, and then setting properties
  19733. from the other lists. Settings in the last list are the most significant
  19734. ones and overrule settings in the other lists."
  19735. (let ((rtn (copy-sequence (pop plists)))
  19736. p v ls)
  19737. (while plists
  19738. (setq ls (pop plists))
  19739. (while ls
  19740. (setq p (pop ls) v (pop ls))
  19741. (setq rtn (plist-put rtn p v))))
  19742. rtn))
  19743. (defun org-replace-escapes (string table)
  19744. "Replace %-escapes in STRING with values in TABLE.
  19745. TABLE is an association list with keys like \"%a\" and string values.
  19746. The sequences in STRING may contain normal field width and padding information,
  19747. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19748. so values can contain further %-escapes if they are define later in TABLE."
  19749. (let ((tbl (copy-alist table))
  19750. (case-fold-search nil)
  19751. (pchg 0)
  19752. e re rpl)
  19753. (while (setq e (pop tbl))
  19754. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19755. (when (and (cdr e) (string-match re (cdr e)))
  19756. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19757. (safe "SREF"))
  19758. (add-text-properties 0 3 (list 'sref sref) safe)
  19759. (setcdr e (replace-match safe t t (cdr e)))))
  19760. (while (string-match re string)
  19761. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19762. (cdr e)))
  19763. (setq string (replace-match rpl t t string))))
  19764. (while (setq pchg (next-property-change pchg string))
  19765. (let ((sref (get-text-property pchg 'sref string)))
  19766. (when (and sref (string-match "SREF" string pchg))
  19767. (setq string (replace-match sref t t string)))))
  19768. string))
  19769. (defun org-sublist (list start end)
  19770. "Return a section of LIST, from START to END.
  19771. Counting starts at 1."
  19772. (let (rtn (c start))
  19773. (setq list (nthcdr (1- start) list))
  19774. (while (and list (<= c end))
  19775. (push (pop list) rtn)
  19776. (setq c (1+ c)))
  19777. (nreverse rtn)))
  19778. (defun org-find-base-buffer-visiting (file)
  19779. "Like `find-buffer-visiting' but always return the base buffer and
  19780. not an indirect buffer."
  19781. (let ((buf (or (get-file-buffer file)
  19782. (find-buffer-visiting file))))
  19783. (if buf
  19784. (or (buffer-base-buffer buf) buf)
  19785. nil)))
  19786. (defun org-image-file-name-regexp (&optional extensions)
  19787. "Return regexp matching the file names of images.
  19788. If EXTENSIONS is given, only match these."
  19789. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19790. (image-file-name-regexp)
  19791. (let ((image-file-name-extensions
  19792. (or extensions
  19793. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19794. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19795. (concat "\\."
  19796. (regexp-opt (nconc (mapcar 'upcase
  19797. image-file-name-extensions)
  19798. image-file-name-extensions)
  19799. t)
  19800. "\\'"))))
  19801. (defun org-file-image-p (file &optional extensions)
  19802. "Return non-nil if FILE is an image."
  19803. (save-match-data
  19804. (string-match (org-image-file-name-regexp extensions) file)))
  19805. (defun org-get-cursor-date (&optional with-time)
  19806. "Return the date at cursor in as a time.
  19807. This works in the calendar and in the agenda, anywhere else it just
  19808. returns the current time.
  19809. If WITH-TIME is non-nil, returns the time of the event at point (in
  19810. the agenda) or the current time of the day."
  19811. (let (date day defd tp tm hod mod)
  19812. (when with-time
  19813. (setq tp (get-text-property (point) 'time))
  19814. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19815. (setq hod (string-to-number (match-string 1 tp))
  19816. mod (string-to-number (match-string 2 tp))))
  19817. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19818. mod (nth 1 (decode-time (current-time))))))
  19819. (cond
  19820. ((eq major-mode 'calendar-mode)
  19821. (setq date (calendar-cursor-to-date)
  19822. defd (encode-time 0 (or mod 0) (or hod 0)
  19823. (nth 1 date) (nth 0 date) (nth 2 date))))
  19824. ((eq major-mode 'org-agenda-mode)
  19825. (setq day (get-text-property (point) 'day))
  19826. (if day
  19827. (setq date (calendar-gregorian-from-absolute day)
  19828. defd (encode-time 0 (or mod 0) (or hod 0)
  19829. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19830. (or defd (current-time))))
  19831. (defun org-mark-subtree (&optional up)
  19832. "Mark the current subtree.
  19833. This puts point at the start of the current subtree, and mark at
  19834. the end. If a numeric prefix UP is given, move up into the
  19835. hierarchy of headlines by UP levels before marking the subtree."
  19836. (interactive "P")
  19837. (org-with-limited-levels
  19838. (cond ((org-at-heading-p) (beginning-of-line))
  19839. ((org-before-first-heading-p) (error "Not in a subtree"))
  19840. (t (outline-previous-visible-heading 1))))
  19841. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19842. (if (org-called-interactively-p 'any)
  19843. (call-interactively 'org-mark-element)
  19844. (org-mark-element)))
  19845. ;;; Indentation
  19846. (defun org-indent-line ()
  19847. "Indent line depending on context."
  19848. (interactive)
  19849. (let* ((pos (point))
  19850. (itemp (org-at-item-p))
  19851. (case-fold-search t)
  19852. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19853. (inline-task-p (and (featurep 'org-inlinetask)
  19854. (org-inlinetask-in-task-p)))
  19855. (inline-re (and inline-task-p
  19856. (org-inlinetask-outline-regexp)))
  19857. column)
  19858. (if (and orgstruct-is-++ (eq pos (point)))
  19859. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19860. (indent-according-to-mode))
  19861. (beginning-of-line 1)
  19862. (cond
  19863. ;; Headings
  19864. ((looking-at org-outline-regexp) (setq column 0))
  19865. ;; Footnote definition
  19866. ((looking-at org-footnote-definition-re) (setq column 0))
  19867. ;; Literal examples
  19868. ((looking-at "[ \t]*:\\( \\|$\\)")
  19869. (setq column (org-get-indentation))) ; do nothing
  19870. ;; Lists
  19871. ((ignore-errors (goto-char (org-in-item-p)))
  19872. (setq column (if itemp
  19873. (org-get-indentation)
  19874. (org-list-item-body-column (point))))
  19875. (goto-char pos))
  19876. ;; Drawers
  19877. ((and (looking-at "[ \t]*:END:")
  19878. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19879. (save-excursion
  19880. (goto-char (1- (match-beginning 1)))
  19881. (setq column (current-column))))
  19882. ;; Special blocks
  19883. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19884. (save-excursion
  19885. (re-search-backward
  19886. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19887. (setq column (org-get-indentation (match-string 0))))
  19888. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19889. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19890. (save-excursion
  19891. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19892. (setq column
  19893. (cond ((equal (downcase (match-string 1)) "src")
  19894. ;; src blocks: let `org-edit-src-exit' handle them
  19895. (org-get-indentation))
  19896. ((equal (downcase (match-string 1)) "example")
  19897. (max (org-get-indentation)
  19898. (org-get-indentation (match-string 0))))
  19899. (t
  19900. (org-get-indentation (match-string 0))))))
  19901. ;; This line has nothing special, look at the previous relevant
  19902. ;; line to compute indentation
  19903. (t
  19904. (beginning-of-line 0)
  19905. (while (and (not (bobp))
  19906. (not (looking-at org-table-line-regexp))
  19907. (not (looking-at org-drawer-regexp))
  19908. ;; When point started in an inline task, do not move
  19909. ;; above task starting line.
  19910. (not (and inline-task-p (looking-at inline-re)))
  19911. ;; Skip drawers, blocks, empty lines, verbatim,
  19912. ;; comments, tables, footnotes definitions, lists,
  19913. ;; inline tasks.
  19914. (or (and (looking-at "[ \t]*:END:")
  19915. (re-search-backward org-drawer-regexp nil t))
  19916. (and (looking-at "[ \t]*#\\+end_")
  19917. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19918. (looking-at "[ \t]*[\n:#|]")
  19919. (looking-at org-footnote-definition-re)
  19920. (and (ignore-errors (goto-char (org-in-item-p)))
  19921. (goto-char
  19922. (org-list-get-top-point (org-list-struct))))
  19923. (and (not inline-task-p)
  19924. (featurep 'org-inlinetask)
  19925. (org-inlinetask-in-task-p)
  19926. (or (org-inlinetask-goto-beginning) t))))
  19927. (beginning-of-line 0))
  19928. (cond
  19929. ;; There was an heading above.
  19930. ((looking-at "\\*+[ \t]+")
  19931. (if (not org-adapt-indentation)
  19932. (setq column 0)
  19933. (goto-char (match-end 0))
  19934. (setq column (current-column))))
  19935. ;; A drawer had started and is unfinished
  19936. ((looking-at org-drawer-regexp)
  19937. (goto-char (1- (match-beginning 1)))
  19938. (setq column (current-column)))
  19939. ;; Else, nothing noticeable found: get indentation and go on.
  19940. (t (setq column (org-get-indentation))))))
  19941. ;; Now apply indentation and move cursor accordingly
  19942. (goto-char pos)
  19943. (if (<= (current-column) (current-indentation))
  19944. (org-indent-line-to column)
  19945. (save-excursion (org-indent-line-to column)))
  19946. ;; Special polishing for properties, see `org-property-format'
  19947. (setq column (current-column))
  19948. (beginning-of-line 1)
  19949. (if (looking-at
  19950. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19951. (replace-match (concat (match-string 1)
  19952. (format org-property-format
  19953. (match-string 2) (match-string 3)))
  19954. t t))
  19955. (org-move-to-column column))))
  19956. (defun org-indent-drawer ()
  19957. "Indent the drawer at point."
  19958. (interactive)
  19959. (let ((p (point))
  19960. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19961. (match-end 0)))
  19962. (folded
  19963. (save-excursion
  19964. (end-of-line)
  19965. (when (overlays-at (point))
  19966. (member 'invisible (overlay-properties
  19967. (car (overlays-at (point)))))))))
  19968. (when folded (org-cycle))
  19969. (indent-for-tab-command)
  19970. (while (and (move-beginning-of-line 2) (< (point) e))
  19971. (indent-for-tab-command))
  19972. (goto-char p)
  19973. (when folded (org-cycle)))
  19974. (message "Drawer at point indented"))
  19975. (defun org-indent-block ()
  19976. "Indent the block at point."
  19977. (interactive)
  19978. (let ((p (point))
  19979. (case-fold-search t)
  19980. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19981. (match-end 0)))
  19982. (folded
  19983. (save-excursion
  19984. (end-of-line)
  19985. (when (overlays-at (point))
  19986. (member 'invisible (overlay-properties
  19987. (car (overlays-at (point)))))))))
  19988. (when folded (org-cycle))
  19989. (indent-for-tab-command)
  19990. (while (and (move-beginning-of-line 2) (< (point) e))
  19991. (indent-for-tab-command))
  19992. (goto-char p)
  19993. (when folded (org-cycle)))
  19994. (message "Block at point indented"))
  19995. (defun org-indent-region (start end)
  19996. "Indent region."
  19997. (interactive "r")
  19998. (save-excursion
  19999. (let ((line-end (org-current-line end)))
  20000. (goto-char start)
  20001. (while (< (org-current-line) line-end)
  20002. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20003. (t (call-interactively 'org-indent-line)))
  20004. (move-beginning-of-line 2)))))
  20005. ;;; Filling
  20006. ;; We use our own fill-paragraph and auto-fill functions.
  20007. ;; `org-fill-paragraph' relies on adaptive filling and context
  20008. ;; checking. Appropriate `fill-prefix' is computed with
  20009. ;; `org-adaptive-fill-function'.
  20010. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20011. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20012. ;; `org-adaptive-fill-function' value. Internally,
  20013. ;; `org-comment-line-break-function' breaks the line.
  20014. ;; `org-setup-filling' installs filling and auto-filling related
  20015. ;; variables during `org-mode' initialization.
  20016. (defun org-setup-filling ()
  20017. (interactive)
  20018. ;; Prevent auto-fill from inserting unwanted new items.
  20019. (when (boundp 'fill-nobreak-predicate)
  20020. (org-set-local
  20021. 'fill-nobreak-predicate
  20022. (org-uniquify
  20023. (append fill-nobreak-predicate
  20024. '(org-fill-paragraph-separate-nobreak-p
  20025. org-fill-line-break-nobreak-p
  20026. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20027. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20028. (org-set-local 'auto-fill-inhibit-regexp nil)
  20029. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20030. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20031. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20032. (defvar org-element-paragraph-separate) ; org-element.el
  20033. (defun org-fill-paragraph-separate-nobreak-p ()
  20034. "Non-nil when a line break at point would insert a new item."
  20035. (looking-at (substring org-element-paragraph-separate 1)))
  20036. (defun org-fill-line-break-nobreak-p ()
  20037. "Non-nil when a line break at point would create an Org line break."
  20038. (save-excursion
  20039. (skip-chars-backward "[ \t]")
  20040. (skip-chars-backward "\\\\")
  20041. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20042. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20043. "Non-nil when a line break at point would insert a new item."
  20044. (and (org-at-timestamp-p t)
  20045. (not (looking-at org-ts-regexp-both))))
  20046. (declare-function message-in-body-p "message" ())
  20047. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20048. (defun org-adaptive-fill-function ()
  20049. "Compute a fill prefix for the current line.
  20050. Return fill prefix, as a string, or nil if current line isn't
  20051. meant to be filled."
  20052. (let (prefix)
  20053. (catch 'exit
  20054. (when (derived-mode-p 'message-mode)
  20055. (save-excursion
  20056. (beginning-of-line)
  20057. (cond ((or (not (message-in-body-p))
  20058. (looking-at orgtbl-line-start-regexp))
  20059. (throw 'exit nil))
  20060. ((looking-at message-cite-prefix-regexp)
  20061. (throw 'exit (match-string-no-properties 0)))
  20062. ((looking-at org-outline-regexp)
  20063. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20064. (org-with-wide-buffer
  20065. (let* ((p (line-beginning-position))
  20066. (element (save-excursion (beginning-of-line)
  20067. (org-element-at-point)))
  20068. (type (org-element-type element))
  20069. (post-affiliated (org-element-property :post-affiliated element)))
  20070. (unless (and post-affiliated (< p post-affiliated))
  20071. (case type
  20072. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20073. (footnote-definition "")
  20074. ((item plain-list)
  20075. (make-string (org-list-item-body-column
  20076. (or post-affiliated
  20077. (org-element-property :begin element)))
  20078. ? ))
  20079. (paragraph
  20080. ;; Fill prefix is usually the same as the current line,
  20081. ;; except if the paragraph is at the beginning of an item.
  20082. (let ((parent (org-element-property :parent element)))
  20083. (cond ((eq (org-element-type parent) 'item)
  20084. (make-string (org-list-item-body-column
  20085. (org-element-property :begin parent))
  20086. ? ))
  20087. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20088. (match-string 0))
  20089. (t ""))))
  20090. (comment-block
  20091. ;; Only fill contents if P is within block boundaries.
  20092. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20093. (forward-line)
  20094. (point)))
  20095. (cend (save-excursion
  20096. (goto-char (org-element-property :end element))
  20097. (skip-chars-backward " \r\t\n")
  20098. (line-beginning-position))))
  20099. (when (and (>= p cbeg) (< p cend))
  20100. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20101. (match-string 0)
  20102. "")))))))))))
  20103. (declare-function message-goto-body "message" ())
  20104. (defvar message-cite-prefix-regexp) ; From message.el
  20105. (defun org-fill-paragraph (&optional justify)
  20106. "Fill element at point, when applicable.
  20107. This function only applies to comment blocks, comments, example
  20108. blocks and paragraphs. Also, as a special case, re-align table
  20109. when point is at one.
  20110. If JUSTIFY is non-nil (interactively, with prefix argument),
  20111. justify as well. If `sentence-end-double-space' is non-nil, then
  20112. period followed by one space does not end a sentence, so don't
  20113. break a line there. The variable `fill-column' controls the
  20114. width for filling.
  20115. For convenience, when point is at a plain list, an item or
  20116. a footnote definition, try to fill the first paragraph within."
  20117. (interactive)
  20118. (if (and (derived-mode-p 'message-mode)
  20119. (or (not (message-in-body-p))
  20120. (save-excursion (move-beginning-of-line 1)
  20121. (looking-at message-cite-prefix-regexp))))
  20122. ;; First ensure filling is correct in message-mode.
  20123. (let ((fill-paragraph-function
  20124. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20125. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20126. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20127. (paragraph-separate
  20128. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20129. (fill-paragraph nil))
  20130. (with-syntax-table org-mode-transpose-word-syntax-table
  20131. ;; Move to end of line in order to get the first paragraph
  20132. ;; within a plain list or a footnote definition.
  20133. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20134. ;; First check if point is in a blank line at the beginning of
  20135. ;; the buffer. In that case, ignore filling.
  20136. (case (org-element-type element)
  20137. ;; Use major mode filling function is src blocks.
  20138. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20139. ;; Align Org tables, leave table.el tables as-is.
  20140. (table-row (org-table-align) t)
  20141. (table
  20142. (when (eq (org-element-property :type element) 'org)
  20143. (org-table-align))
  20144. t)
  20145. (paragraph
  20146. ;; Paragraphs may contain `line-break' type objects.
  20147. (let ((beg (max (point-min)
  20148. (org-element-property :contents-begin element)))
  20149. (end (min (point-max)
  20150. (org-element-property :contents-end element))))
  20151. ;; Do nothing if point is at an affiliated keyword.
  20152. (if (< (line-end-position) beg) t
  20153. (when (derived-mode-p 'message-mode)
  20154. ;; In `message-mode', do not fill following citation
  20155. ;; in current paragraph nor text before message body.
  20156. (let ((body-start (save-excursion (message-goto-body))))
  20157. (when body-start (setq beg (max body-start beg))))
  20158. (when (save-excursion
  20159. (re-search-forward
  20160. (concat "^" message-cite-prefix-regexp) end t))
  20161. (setq end (match-beginning 0))))
  20162. ;; Fill paragraph, taking line breaks into account.
  20163. ;; For that, slice the paragraph using line breaks as
  20164. ;; separators, and fill the parts in reverse order to
  20165. ;; avoid messing with markers.
  20166. (save-excursion
  20167. (goto-char end)
  20168. (mapc
  20169. (lambda (pos)
  20170. (fill-region-as-paragraph pos (point) justify)
  20171. (goto-char pos))
  20172. ;; Find the list of ending positions for line breaks
  20173. ;; in the current paragraph. Add paragraph
  20174. ;; beginning to include first slice.
  20175. (nreverse
  20176. (cons beg
  20177. (org-element-map
  20178. (org-element--parse-objects
  20179. beg end nil (org-element-restriction 'paragraph))
  20180. 'line-break
  20181. (lambda (lb) (org-element-property :end lb)))))))
  20182. t)))
  20183. ;; Contents of `comment-block' type elements should be
  20184. ;; filled as plain text, but only if point is within block
  20185. ;; markers.
  20186. (comment-block
  20187. (let* ((case-fold-search t)
  20188. (beg (save-excursion
  20189. (goto-char (org-element-property :begin element))
  20190. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20191. (forward-line)
  20192. (point)))
  20193. (end (save-excursion
  20194. (goto-char (org-element-property :end element))
  20195. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20196. (line-beginning-position))))
  20197. (when (and (>= (point) beg) (< (point) end))
  20198. (fill-region-as-paragraph
  20199. (save-excursion
  20200. (end-of-line)
  20201. (re-search-backward "^[ \t]*$" beg 'move)
  20202. (line-beginning-position))
  20203. (save-excursion
  20204. (beginning-of-line)
  20205. (re-search-forward "^[ \t]*$" end 'move)
  20206. (line-beginning-position))
  20207. justify)))
  20208. t)
  20209. ;; Fill comments.
  20210. (comment (fill-comment-paragraph justify))
  20211. ;; Ignore every other element.
  20212. (otherwise t))))))
  20213. (defun org-auto-fill-function ()
  20214. "Auto-fill function."
  20215. ;; Check if auto-filling is meaningful.
  20216. (let ((fc (current-fill-column)))
  20217. (when (and fc (> (current-column) fc))
  20218. (let* ((fill-prefix (org-adaptive-fill-function))
  20219. ;; Enforce empty fill prefix, if required. Otherwise, it
  20220. ;; will be computed again.
  20221. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20222. (when fill-prefix (do-auto-fill))))))
  20223. (defun org-comment-line-break-function (&optional soft)
  20224. "Break line at point and indent, continuing comment if within one.
  20225. The inserted newline is marked hard if variable
  20226. `use-hard-newlines' is true, unless optional argument SOFT is
  20227. non-nil."
  20228. (if soft (insert-and-inherit ?\n) (newline 1))
  20229. (save-excursion (forward-char -1) (delete-horizontal-space))
  20230. (delete-horizontal-space)
  20231. (indent-to-left-margin)
  20232. (insert-before-markers-and-inherit fill-prefix))
  20233. ;;; Comments
  20234. ;; Org comments syntax is quite complex. It requires the entire line
  20235. ;; to be just a comment. Also, even with the right syntax at the
  20236. ;; beginning of line, some some elements (i.e. verse-block or
  20237. ;; example-block) don't accept comments. Usual Emacs comment commands
  20238. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20239. ;; Org still relies on `comment-dwim', but cannot trust
  20240. ;; `comment-only-p'. So, `comment-region-function' and
  20241. ;; `uncomment-region-function' both point
  20242. ;; to`org-comment-or-uncomment-region'. Eventually,
  20243. ;; `org-insert-comment' takes care of insertion of comments at the
  20244. ;; beginning of line.
  20245. ;; `org-setup-comments-handling' install comments related variables
  20246. ;; during `org-mode' initialization.
  20247. (defun org-setup-comments-handling ()
  20248. (interactive)
  20249. (org-set-local 'comment-use-syntax nil)
  20250. (org-set-local 'comment-start "# ")
  20251. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20252. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20253. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20254. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20255. (defun org-insert-comment ()
  20256. "Insert an empty comment above current line.
  20257. If the line is empty, insert comment at its beginning."
  20258. (beginning-of-line)
  20259. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20260. (org-indent-line)
  20261. (insert "# "))
  20262. (defvar comment-empty-lines) ; From newcomment.el.
  20263. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20264. "Comment or uncomment each non-blank line in the region.
  20265. Uncomment each non-blank line between BEG and END if it only
  20266. contains commented lines. Otherwise, comment them."
  20267. (save-restriction
  20268. ;; Restrict region
  20269. (narrow-to-region (save-excursion (goto-char beg)
  20270. (skip-chars-forward " \r\t\n" end)
  20271. (line-beginning-position))
  20272. (save-excursion (goto-char end)
  20273. (skip-chars-backward " \r\t\n" beg)
  20274. (line-end-position)))
  20275. (let ((uncommentp
  20276. ;; UNCOMMENTP is non-nil when every non blank line between
  20277. ;; BEG and END is a comment.
  20278. (save-excursion
  20279. (goto-char (point-min))
  20280. (while (and (not (eobp))
  20281. (let ((element (org-element-at-point)))
  20282. (and (eq (org-element-type element) 'comment)
  20283. (goto-char (min (point-max)
  20284. (org-element-property
  20285. :end element)))))))
  20286. (eobp))))
  20287. (if uncommentp
  20288. ;; Only blank lines and comments in region: uncomment it.
  20289. (save-excursion
  20290. (goto-char (point-min))
  20291. (while (not (eobp))
  20292. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20293. (replace-match "" nil nil nil 1))
  20294. (forward-line)))
  20295. ;; Comment each line in region.
  20296. (let ((min-indent (point-max)))
  20297. ;; First find the minimum indentation across all lines.
  20298. (save-excursion
  20299. (goto-char (point-min))
  20300. (while (and (not (eobp)) (not (zerop min-indent)))
  20301. (unless (looking-at "[ \t]*$")
  20302. (setq min-indent (min min-indent (current-indentation))))
  20303. (forward-line)))
  20304. ;; Then loop over all lines.
  20305. (save-excursion
  20306. (goto-char (point-min))
  20307. (while (not (eobp))
  20308. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20309. (org-move-to-column min-indent t)
  20310. (insert comment-start))
  20311. (forward-line))))))))
  20312. ;;; Planning
  20313. ;; This section contains tools to operate on timestamp objects, as
  20314. ;; returned by, e.g. `org-element-context'.
  20315. (defun org-timestamp-has-time-p (timestamp)
  20316. "Non-nil when TIMESTAMP has a time specified."
  20317. (org-element-property :hour-start timestamp))
  20318. (defun org-timestamp-format (timestamp format &optional end utc)
  20319. "Format a TIMESTAMP element into a string.
  20320. FORMAT is a format specifier to be passed to
  20321. `format-time-string'.
  20322. When optional argument END is non-nil, use end of date-range or
  20323. time-range, if possible.
  20324. When optional argument UTC is non-nil, time will be expressed as
  20325. Universal Time."
  20326. (format-time-string
  20327. format
  20328. (apply 'encode-time
  20329. (cons 0
  20330. (mapcar
  20331. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20332. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20333. '(:minute-start :hour-start :day-start :month-start
  20334. :year-start)))))
  20335. utc))
  20336. (defun org-timestamp-split-range (timestamp &optional end)
  20337. "Extract a timestamp object from a date or time range.
  20338. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20339. the end of the range. Otherwise, extract its start.
  20340. Return a new timestamp object sharing the same parent as
  20341. TIMESTAMP."
  20342. (let ((type (org-element-property :type timestamp)))
  20343. (if (memq type '(active inactive diary)) timestamp
  20344. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20345. ;; Set new type.
  20346. (org-element-put-property
  20347. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20348. ;; Copy start properties over end properties if END is
  20349. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20350. (let ((p-alist '((:minute-start . :minute-end)
  20351. (:hour-start . :hour-end)
  20352. (:day-start . :day-end)
  20353. (:month-start . :month-end)
  20354. (:year-start . :year-end))))
  20355. (dolist (p-cell p-alist)
  20356. (org-element-put-property
  20357. split-ts
  20358. (funcall (if end 'car 'cdr) p-cell)
  20359. (org-element-property
  20360. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20361. ;; Eventually refresh `:raw-value'.
  20362. (org-element-put-property split-ts :raw-value nil)
  20363. (org-element-put-property
  20364. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20365. (defun org-timestamp-translate (timestamp &optional boundary)
  20366. "Apply `org-translate-time' on a TIMESTAMP object.
  20367. When optional argument BOUNDARY is non-nil, it is either the
  20368. symbol `start' or `end'. In this case, only translate the
  20369. starting or ending part of TIMESTAMP if it is a date or time
  20370. range. Otherwise, translate both parts."
  20371. (if (and (not boundary)
  20372. (memq (org-element-property :type timestamp)
  20373. '(active-range inactive-range)))
  20374. (concat
  20375. (org-translate-time
  20376. (org-element-property :raw-value
  20377. (org-timestamp-split-range timestamp)))
  20378. "--"
  20379. (org-translate-time
  20380. (org-element-property :raw-value
  20381. (org-timestamp-split-range timestamp t))))
  20382. (org-translate-time
  20383. (org-element-property
  20384. :raw-value
  20385. (if (not boundary) timestamp
  20386. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20387. ;;; Other stuff.
  20388. (defun org-toggle-fixed-width-section (arg)
  20389. "Toggle the fixed-width export.
  20390. If there is no active region, the QUOTE keyword at the current headline is
  20391. inserted or removed. When present, it causes the text between this headline
  20392. and the next to be exported as fixed-width text, and unmodified.
  20393. If there is an active region, this command adds or removes a colon as the
  20394. first character of this line. If the first character of a line is a colon,
  20395. this line is also exported in fixed-width font."
  20396. (interactive "P")
  20397. (let* ((cc 0)
  20398. (regionp (org-region-active-p))
  20399. (beg (if regionp (region-beginning) (point)))
  20400. (end (if regionp (region-end)))
  20401. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20402. (case-fold-search nil)
  20403. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20404. off)
  20405. (if regionp
  20406. (save-excursion
  20407. (goto-char beg)
  20408. (setq cc (current-column))
  20409. (beginning-of-line 1)
  20410. (setq off (looking-at re))
  20411. (while (> nlines 0)
  20412. (setq nlines (1- nlines))
  20413. (beginning-of-line 1)
  20414. (cond
  20415. (arg
  20416. (org-move-to-column cc t)
  20417. (insert ": \n")
  20418. (forward-line -1))
  20419. ((and off (looking-at re))
  20420. (replace-match "" t t nil 1))
  20421. ((not off) (org-move-to-column cc t) (insert ": ")))
  20422. (forward-line 1)))
  20423. (save-excursion
  20424. (org-back-to-heading)
  20425. (cond
  20426. ((looking-at (format org-heading-keyword-regexp-format
  20427. org-quote-string))
  20428. (goto-char (match-end 1))
  20429. (looking-at (concat " +" org-quote-string))
  20430. (replace-match "" t t)
  20431. (when (eolp) (insert " ")))
  20432. ((looking-at org-outline-regexp)
  20433. (goto-char (match-end 0))
  20434. (insert org-quote-string " ")))))))
  20435. (defun org-reftex-citation ()
  20436. "Use reftex-citation to insert a citation into the buffer.
  20437. This looks for a line like
  20438. #+BIBLIOGRAPHY: foo plain option:-d
  20439. and derives from it that foo.bib is the bibliography file relevant
  20440. for this document. It then installs the necessary environment for RefTeX
  20441. to work in this buffer and calls `reftex-citation' to insert a citation
  20442. into the buffer.
  20443. Export of such citations to both LaTeX and HTML is handled by the contributed
  20444. package org-exp-bibtex by Taru Karttunen."
  20445. (interactive)
  20446. (let ((reftex-docstruct-symbol 'rds)
  20447. (reftex-cite-format "\\cite{%l}")
  20448. rds bib)
  20449. (save-excursion
  20450. (save-restriction
  20451. (widen)
  20452. (let ((case-fold-search t)
  20453. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20454. (if (not (save-excursion
  20455. (or (re-search-forward re nil t)
  20456. (re-search-backward re nil t))))
  20457. (error "No bibliography defined in file")
  20458. (setq bib (concat (match-string 1) ".bib")
  20459. rds (list (list 'bib bib)))))))
  20460. (call-interactively 'reftex-citation)))
  20461. ;;;; Functions extending outline functionality
  20462. (defun org-beginning-of-line (&optional arg)
  20463. "Go to the beginning of the current line. If that is invisible, continue
  20464. to a visible line beginning. This makes the function of C-a more intuitive.
  20465. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20466. first attempt, and only move to after the tags when the cursor is already
  20467. beyond the end of the headline."
  20468. (interactive "P")
  20469. (let ((pos (point))
  20470. (special (if (consp org-special-ctrl-a/e)
  20471. (car org-special-ctrl-a/e)
  20472. org-special-ctrl-a/e))
  20473. refpos)
  20474. (if (org-bound-and-true-p visual-line-mode)
  20475. (beginning-of-visual-line 1)
  20476. (beginning-of-line 1))
  20477. (if (and arg (fboundp 'move-beginning-of-line))
  20478. (call-interactively 'move-beginning-of-line)
  20479. (if (bobp)
  20480. nil
  20481. (backward-char 1)
  20482. (if (org-truely-invisible-p)
  20483. (while (and (not (bobp)) (org-truely-invisible-p))
  20484. (backward-char 1)
  20485. (beginning-of-line 1))
  20486. (forward-char 1))))
  20487. (when special
  20488. (cond
  20489. ((and (looking-at org-complex-heading-regexp)
  20490. (= (char-after (match-end 1)) ?\ ))
  20491. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20492. (point-at-eol)))
  20493. (goto-char
  20494. (if (eq special t)
  20495. (cond ((> pos refpos) refpos)
  20496. ((= pos (point)) refpos)
  20497. (t (point)))
  20498. (cond ((> pos (point)) (point))
  20499. ((not (eq last-command this-command)) (point))
  20500. (t refpos)))))
  20501. ((org-at-item-p)
  20502. ;; Being at an item and not looking at an the item means point
  20503. ;; was previously moved to beginning of a visual line, which
  20504. ;; doesn't contain the item. Therefore, do nothing special,
  20505. ;; just stay here.
  20506. (when (looking-at org-list-full-item-re)
  20507. ;; Set special position at first white space character after
  20508. ;; bullet, and check-box, if any.
  20509. (let ((after-bullet
  20510. (let ((box (match-end 3)))
  20511. (if (not box) (match-end 1)
  20512. (let ((after (char-after box)))
  20513. (if (and after (= after ? )) (1+ box) box))))))
  20514. ;; Special case: Move point to special position when
  20515. ;; currently after it or at beginning of line.
  20516. (if (eq special t)
  20517. (when (or (> pos after-bullet) (= (point) pos))
  20518. (goto-char after-bullet))
  20519. ;; Reversed case: Move point to special position when
  20520. ;; point was already at beginning of line and command is
  20521. ;; repeated.
  20522. (when (and (= (point) pos) (eq last-command this-command))
  20523. (goto-char after-bullet))))))))
  20524. (org-no-warnings
  20525. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20526. (defun org-end-of-line (&optional arg)
  20527. "Go to the end of the line.
  20528. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20529. tags on the first attempt, and only move to after the tags when
  20530. the cursor is already beyond the end of the headline."
  20531. (interactive "P")
  20532. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20533. org-special-ctrl-a/e))
  20534. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20535. 'end-of-visual-line)
  20536. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20537. (t 'end-of-line))))
  20538. (if (or (not special) arg) (call-interactively move-fun)
  20539. (let* ((element (save-excursion (beginning-of-line)
  20540. (org-element-at-point)))
  20541. (type (org-element-type element)))
  20542. (cond
  20543. ((memq type '(headline inlinetask))
  20544. (let ((pos (point)))
  20545. (beginning-of-line 1)
  20546. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20547. (if (eq special t)
  20548. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20549. (goto-char (match-beginning 1))
  20550. (goto-char (match-end 0)))
  20551. (if (or (< pos (match-end 0))
  20552. (not (eq this-command last-command)))
  20553. (goto-char (match-end 0))
  20554. (goto-char (match-beginning 1))))
  20555. (call-interactively move-fun))))
  20556. ((org-element-property :hiddenp element)
  20557. ;; If element is hidden, `move-end-of-line' would put point
  20558. ;; after it. Use `end-of-line' to stay on current line.
  20559. (call-interactively 'end-of-line))
  20560. (t (call-interactively move-fun)))))
  20561. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20562. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20563. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20564. (defun org-backward-sentence (&optional arg)
  20565. "Go to beginning of sentence, or beginning of table field.
  20566. This will call `backward-sentence' or `org-table-beginning-of-field',
  20567. depending on context."
  20568. (interactive "P")
  20569. (cond
  20570. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20571. (t (call-interactively 'backward-sentence))))
  20572. (defun org-forward-sentence (&optional arg)
  20573. "Go to end of sentence, or end of table field.
  20574. This will call `forward-sentence' or `org-table-end-of-field',
  20575. depending on context."
  20576. (interactive "P")
  20577. (cond
  20578. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20579. (t (call-interactively 'forward-sentence))))
  20580. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20581. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20582. (defun org-kill-line (&optional arg)
  20583. "Kill line, to tags or end of line."
  20584. (interactive "P")
  20585. (cond
  20586. ((or (not org-special-ctrl-k)
  20587. (bolp)
  20588. (not (org-at-heading-p)))
  20589. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20590. org-ctrl-k-protect-subtree)
  20591. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20592. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20593. (error "C-k aborted - would kill hidden subtree")))
  20594. (call-interactively
  20595. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20596. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20597. (kill-region (point) (match-beginning 1))
  20598. (org-set-tags nil t))
  20599. (t (kill-region (point) (point-at-eol)))))
  20600. (define-key org-mode-map "\C-k" 'org-kill-line)
  20601. (defun org-yank (&optional arg)
  20602. "Yank. If the kill is a subtree, treat it specially.
  20603. This command will look at the current kill and check if is a single
  20604. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20605. cursor is at the beginning of a line or after the stars of a currently
  20606. empty headline, then the yank is handled specially. How exactly depends
  20607. on the value of the following variables, both set by default.
  20608. org-yank-folded-subtrees
  20609. When set, the subtree(s) will be folded after insertion, but only
  20610. if doing so would now swallow text after the yanked text.
  20611. org-yank-adjusted-subtrees
  20612. When set, the subtree will be promoted or demoted in order to
  20613. fit into the local outline tree structure, which means that the level
  20614. will be adjusted so that it becomes the smaller one of the two
  20615. *visible* surrounding headings.
  20616. Any prefix to this command will cause `yank' to be called directly with
  20617. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20618. will just
  20619. plainly yank the text as it is.
  20620. \[1] The test checks if the first non-white line is a heading
  20621. and if there are no other headings with fewer stars."
  20622. (interactive "P")
  20623. (org-yank-generic 'yank arg))
  20624. (defun org-yank-generic (command arg)
  20625. "Perform some yank-like command.
  20626. This function implements the behavior described in the `org-yank'
  20627. documentation. However, it has been generalized to work for any
  20628. interactive command with similar behavior."
  20629. ;; pretend to be command COMMAND
  20630. (setq this-command command)
  20631. (if arg
  20632. (call-interactively command)
  20633. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20634. (and (org-kill-is-subtree-p)
  20635. (or (bolp)
  20636. (and (looking-at "[ \t]*$")
  20637. (string-match
  20638. "\\`\\*+\\'"
  20639. (buffer-substring (point-at-bol) (point)))))))
  20640. swallowp)
  20641. (cond
  20642. ((and subtreep org-yank-folded-subtrees)
  20643. (let ((beg (point))
  20644. end)
  20645. (if (and subtreep org-yank-adjusted-subtrees)
  20646. (org-paste-subtree nil nil 'for-yank)
  20647. (call-interactively command))
  20648. (setq end (point))
  20649. (goto-char beg)
  20650. (when (and (bolp) subtreep
  20651. (not (setq swallowp
  20652. (org-yank-folding-would-swallow-text beg end))))
  20653. (org-with-limited-levels
  20654. (or (looking-at org-outline-regexp)
  20655. (re-search-forward org-outline-regexp-bol end t))
  20656. (while (and (< (point) end) (looking-at org-outline-regexp))
  20657. (hide-subtree)
  20658. (org-cycle-show-empty-lines 'folded)
  20659. (condition-case nil
  20660. (outline-forward-same-level 1)
  20661. (error (goto-char end))))))
  20662. (when swallowp
  20663. (message
  20664. "Inserted text not folded because that would swallow text"))
  20665. (goto-char end)
  20666. (skip-chars-forward " \t\n\r")
  20667. (beginning-of-line 1)
  20668. (push-mark beg 'nomsg)))
  20669. ((and subtreep org-yank-adjusted-subtrees)
  20670. (let ((beg (point-at-bol)))
  20671. (org-paste-subtree nil nil 'for-yank)
  20672. (push-mark beg 'nomsg)))
  20673. (t
  20674. (call-interactively command))))))
  20675. (defun org-yank-folding-would-swallow-text (beg end)
  20676. "Would hide-subtree at BEG swallow any text after END?"
  20677. (let (level)
  20678. (org-with-limited-levels
  20679. (save-excursion
  20680. (goto-char beg)
  20681. (when (or (looking-at org-outline-regexp)
  20682. (re-search-forward org-outline-regexp-bol end t))
  20683. (setq level (org-outline-level)))
  20684. (goto-char end)
  20685. (skip-chars-forward " \t\r\n\v\f")
  20686. (if (or (eobp)
  20687. (and (bolp) (looking-at org-outline-regexp)
  20688. (<= (org-outline-level) level)))
  20689. nil ; Nothing would be swallowed
  20690. t))))) ; something would swallow
  20691. (define-key org-mode-map "\C-y" 'org-yank)
  20692. (defun org-truely-invisible-p ()
  20693. "Check if point is at a character currently not visible.
  20694. This version does not only check the character property, but also
  20695. `visible-mode'."
  20696. ;; Early versions of noutline don't have `outline-invisible-p'.
  20697. (if (org-bound-and-true-p visible-mode)
  20698. nil
  20699. (outline-invisible-p)))
  20700. (defun org-invisible-p2 ()
  20701. "Check if point is at a character currently not visible."
  20702. (save-excursion
  20703. (if (and (eolp) (not (bobp))) (backward-char 1))
  20704. ;; Early versions of noutline don't have `outline-invisible-p'.
  20705. (outline-invisible-p)))
  20706. (defun org-back-to-heading (&optional invisible-ok)
  20707. "Call `outline-back-to-heading', but provide a better error message."
  20708. (condition-case nil
  20709. (outline-back-to-heading invisible-ok)
  20710. (error (error "Before first headline at position %d in buffer %s"
  20711. (point) (current-buffer)))))
  20712. (defun org-before-first-heading-p ()
  20713. "Before first heading?"
  20714. (save-excursion
  20715. (end-of-line)
  20716. (null (re-search-backward org-outline-regexp-bol nil t))))
  20717. (defun org-at-heading-p (&optional ignored)
  20718. (outline-on-heading-p t))
  20719. ;; Compatibility alias with Org versions < 7.8.03
  20720. (defalias 'org-on-heading-p 'org-at-heading-p)
  20721. (defun org-at-comment-p nil
  20722. "Is cursor in a line starting with a # character?"
  20723. (save-excursion
  20724. (beginning-of-line)
  20725. (looking-at "^#")))
  20726. (defun org-at-drawer-p nil
  20727. "Is cursor at a drawer keyword?"
  20728. (save-excursion
  20729. (move-beginning-of-line 1)
  20730. (looking-at org-drawer-regexp)))
  20731. (defun org-at-block-p nil
  20732. "Is cursor at a block keyword?"
  20733. (save-excursion
  20734. (move-beginning-of-line 1)
  20735. (looking-at org-block-regexp)))
  20736. (defun org-point-at-end-of-empty-headline ()
  20737. "If point is at the end of an empty headline, return t, else nil.
  20738. If the heading only contains a TODO keyword, it is still still considered
  20739. empty."
  20740. (and (looking-at "[ \t]*$")
  20741. (when org-todo-line-regexp
  20742. (save-excursion
  20743. (beginning-of-line 1)
  20744. (let ((case-fold-search nil))
  20745. (looking-at org-todo-line-regexp)
  20746. (string= (match-string 3) ""))))))
  20747. (defun org-at-heading-or-item-p ()
  20748. (or (org-at-heading-p) (org-at-item-p)))
  20749. (defun org-at-target-p ()
  20750. (or (org-in-regexp org-radio-target-regexp)
  20751. (org-in-regexp org-target-regexp)))
  20752. ;; Compatibility alias with Org versions < 7.8.03
  20753. (defalias 'org-on-target-p 'org-at-target-p)
  20754. (defun org-up-heading-all (arg)
  20755. "Move to the heading line of which the present line is a subheading.
  20756. This function considers both visible and invisible heading lines.
  20757. With argument, move up ARG levels."
  20758. (if (fboundp 'outline-up-heading-all)
  20759. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20760. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20761. (defun org-up-heading-safe ()
  20762. "Move to the heading line of which the present line is a subheading.
  20763. This version will not throw an error. It will return the level of the
  20764. headline found, or nil if no higher level is found.
  20765. Also, this function will be a lot faster than `outline-up-heading',
  20766. because it relies on stars being the outline starters. This can really
  20767. make a significant difference in outlines with very many siblings."
  20768. (let (start-level re)
  20769. (org-back-to-heading t)
  20770. (setq start-level (funcall outline-level))
  20771. (if (equal start-level 1)
  20772. nil
  20773. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20774. (if (re-search-backward re nil t)
  20775. (funcall outline-level)))))
  20776. (defun org-first-sibling-p ()
  20777. "Is this heading the first child of its parents?"
  20778. (interactive)
  20779. (let ((re org-outline-regexp-bol)
  20780. level l)
  20781. (unless (org-at-heading-p t)
  20782. (error "Not at a heading"))
  20783. (setq level (funcall outline-level))
  20784. (save-excursion
  20785. (if (not (re-search-backward re nil t))
  20786. t
  20787. (setq l (funcall outline-level))
  20788. (< l level)))))
  20789. (defun org-goto-sibling (&optional previous)
  20790. "Goto the next sibling, even if it is invisible.
  20791. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20792. when a sibling was found. When none is found, return nil and don't
  20793. move point."
  20794. (let ((fun (if previous 're-search-backward 're-search-forward))
  20795. (pos (point))
  20796. (re org-outline-regexp-bol)
  20797. level l)
  20798. (when (condition-case nil (org-back-to-heading t) (error nil))
  20799. (setq level (funcall outline-level))
  20800. (catch 'exit
  20801. (or previous (forward-char 1))
  20802. (while (funcall fun re nil t)
  20803. (setq l (funcall outline-level))
  20804. (when (< l level) (goto-char pos) (throw 'exit nil))
  20805. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20806. (goto-char pos)
  20807. nil))))
  20808. (defun org-show-siblings ()
  20809. "Show all siblings of the current headline."
  20810. (save-excursion
  20811. (while (org-goto-sibling) (org-flag-heading nil)))
  20812. (save-excursion
  20813. (while (org-goto-sibling 'previous)
  20814. (org-flag-heading nil))))
  20815. (defun org-goto-first-child ()
  20816. "Goto the first child, even if it is invisible.
  20817. Return t when a child was found. Otherwise don't move point and
  20818. return nil."
  20819. (let (level (pos (point)) (re org-outline-regexp-bol))
  20820. (when (condition-case nil (org-back-to-heading t) (error nil))
  20821. (setq level (outline-level))
  20822. (forward-char 1)
  20823. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20824. (progn (goto-char (match-beginning 0)) t)
  20825. (goto-char pos) nil))))
  20826. (defun org-show-hidden-entry ()
  20827. "Show an entry where even the heading is hidden."
  20828. (save-excursion
  20829. (org-show-entry)))
  20830. (defun org-flag-heading (flag &optional entry)
  20831. "Flag the current heading. FLAG non-nil means make invisible.
  20832. When ENTRY is non-nil, show the entire entry."
  20833. (save-excursion
  20834. (org-back-to-heading t)
  20835. ;; Check if we should show the entire entry
  20836. (if entry
  20837. (progn
  20838. (org-show-entry)
  20839. (save-excursion
  20840. (and (outline-next-heading)
  20841. (org-flag-heading nil))))
  20842. (outline-flag-region (max (point-min) (1- (point)))
  20843. (save-excursion (outline-end-of-heading) (point))
  20844. flag))))
  20845. (defun org-get-next-sibling ()
  20846. "Move to next heading of the same level, and return point.
  20847. If there is no such heading, return nil.
  20848. This is like outline-next-sibling, but invisible headings are ok."
  20849. (let ((level (funcall outline-level)))
  20850. (outline-next-heading)
  20851. (while (and (not (eobp)) (> (funcall outline-level) level))
  20852. (outline-next-heading))
  20853. (if (or (eobp) (< (funcall outline-level) level))
  20854. nil
  20855. (point))))
  20856. (defun org-get-last-sibling ()
  20857. "Move to previous heading of the same level, and return point.
  20858. If there is no such heading, return nil."
  20859. (let ((opoint (point))
  20860. (level (funcall outline-level)))
  20861. (outline-previous-heading)
  20862. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20863. (while (and (> (funcall outline-level) level)
  20864. (not (bobp)))
  20865. (outline-previous-heading))
  20866. (if (< (funcall outline-level) level)
  20867. nil
  20868. (point)))))
  20869. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20870. "Goto to the end of a subtree."
  20871. ;; This contains an exact copy of the original function, but it uses
  20872. ;; `org-back-to-heading', to make it work also in invisible
  20873. ;; trees. And is uses an invisible-ok argument.
  20874. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20875. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20876. ;; with many children and grandchildren etc, this can be much faster
  20877. ;; than the outline version.
  20878. (org-back-to-heading invisible-ok)
  20879. (let ((first t)
  20880. (level (funcall outline-level)))
  20881. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20882. ;; A true heading (not a plain list item), in Org-mode
  20883. ;; This means we can easily find the end by looking
  20884. ;; only for the right number of stars. Using a regexp to do
  20885. ;; this is so much faster than using a Lisp loop.
  20886. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20887. (forward-char 1)
  20888. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20889. ;; something else, do it the slow way
  20890. (while (and (not (eobp))
  20891. (or first (> (funcall outline-level) level)))
  20892. (setq first nil)
  20893. (outline-next-heading)))
  20894. (unless to-heading
  20895. (if (memq (preceding-char) '(?\n ?\^M))
  20896. (progn
  20897. ;; Go to end of line before heading
  20898. (forward-char -1)
  20899. (if (memq (preceding-char) '(?\n ?\^M))
  20900. ;; leave blank line before heading
  20901. (forward-char -1))))))
  20902. (point))
  20903. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20904. "Use Org version in org-mode, for dramatic speed-up."
  20905. (if (derived-mode-p 'org-mode)
  20906. (progn
  20907. (org-end-of-subtree nil t)
  20908. (unless (eobp) (backward-char 1)))
  20909. ad-do-it))
  20910. (defun org-end-of-meta-data-and-drawers ()
  20911. "Jump to the first text after meta data and drawers in the current entry.
  20912. This will move over empty lines, lines with planning time stamps,
  20913. clocking lines, and drawers."
  20914. (org-back-to-heading t)
  20915. (let ((end (save-excursion (outline-next-heading) (point)))
  20916. (re (concat "\\(" org-drawer-regexp "\\)"
  20917. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20918. (forward-line 1)
  20919. (while (re-search-forward re end t)
  20920. (if (not (match-end 1))
  20921. ;; empty or planning line
  20922. (forward-line 1)
  20923. ;; a drawer, find the end
  20924. (re-search-forward "^[ \t]*:END:" end 'move)
  20925. (forward-line 1)))
  20926. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20927. (point)))
  20928. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20929. "Move forward to the ARG'th subheading at same level as this one.
  20930. Stop at the first and last subheadings of a superior heading.
  20931. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20932. non-nil it will also look at invisible ones."
  20933. (interactive "p")
  20934. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20935. (if (and arg (< arg 0))
  20936. (goto-char (point-min))
  20937. (outline-next-heading))
  20938. (org-at-heading-p)
  20939. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20940. (f (if (and arg (< arg 0))
  20941. 're-search-backward
  20942. 're-search-forward))
  20943. (count (if arg (abs arg) 1))
  20944. (result (point)))
  20945. (forward-char (if (and arg (< arg 0)) -1 1))
  20946. (while (and (> count 0)
  20947. (funcall f org-outline-regexp-bol nil 'move))
  20948. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20949. (cond ((< l level) (setq count 0))
  20950. ((and (= l level)
  20951. (or invisible-ok
  20952. (progn
  20953. (goto-char (line-beginning-position))
  20954. (not (outline-invisible-p)))))
  20955. (setq count (1- count))
  20956. (when (eq l level)
  20957. (setq result (point)))))))
  20958. (goto-char result))
  20959. (beginning-of-line 1)))
  20960. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20961. "Move backward to the ARG'th subheading at same level as this one.
  20962. Stop at the first and last subheadings of a superior heading."
  20963. (interactive "p")
  20964. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20965. (defun org-next-block (arg &optional backward block-regexp)
  20966. "Jump to the next block.
  20967. With a prefix argument ARG, jump forward ARG many source blocks.
  20968. When BACKWARD is non-nil, jump to the previous block.
  20969. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20970. (interactive "p")
  20971. (let ((re (or block-regexp org-block-regexp))
  20972. (re-search-fn (or (and backward 're-search-backward)
  20973. 're-search-forward)))
  20974. (if (looking-at re) (forward-char 1))
  20975. (condition-case nil
  20976. (funcall re-search-fn re nil nil arg)
  20977. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20978. (goto-char (match-beginning 0)) (org-show-context)))
  20979. (defun org-previous-block (arg &optional block-regexp)
  20980. "Jump to the previous block.
  20981. With a prefix argument ARG, jump backward ARG many source blocks.
  20982. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20983. (interactive "p")
  20984. (org-next-block arg t block-regexp))
  20985. (defun org-forward-element ()
  20986. "Move forward by one element.
  20987. Move to the next element at the same level, when possible."
  20988. (interactive)
  20989. (cond ((eobp) (user-error "Cannot move further down"))
  20990. ((org-with-limited-levels (org-at-heading-p))
  20991. (let ((origin (point)))
  20992. (goto-char (org-end-of-subtree nil t))
  20993. (unless (org-with-limited-levels (org-at-heading-p))
  20994. (goto-char origin)
  20995. (user-error "Cannot move further down"))))
  20996. (t
  20997. (let* ((elem (org-element-at-point))
  20998. (end (org-element-property :end elem))
  20999. (parent (org-element-property :parent elem)))
  21000. (if (and parent (= (org-element-property :contents-end parent) end))
  21001. (goto-char (org-element-property :end parent))
  21002. (goto-char end))))))
  21003. (defun org-backward-element ()
  21004. "Move backward by one element.
  21005. Move to the previous element at the same level, when possible."
  21006. (interactive)
  21007. (cond ((bobp) (user-error "Cannot move further up"))
  21008. ((org-with-limited-levels (org-at-heading-p))
  21009. ;; At a headline, move to the previous one, if any, or stay
  21010. ;; here.
  21011. (let ((origin (point)))
  21012. (org-with-limited-levels (org-backward-heading-same-level 1))
  21013. ;; When current headline has no sibling above, move to its
  21014. ;; parent.
  21015. (when (= (point) origin)
  21016. (or (org-with-limited-levels (org-up-heading-safe))
  21017. (progn (goto-char origin)
  21018. (user-error "Cannot move further up"))))))
  21019. (t
  21020. (let* ((trail (org-element-at-point 'keep-trail))
  21021. (elem (car trail))
  21022. (prev-elem (nth 1 trail))
  21023. (beg (org-element-property :begin elem)))
  21024. (cond
  21025. ;; Move to beginning of current element if point isn't
  21026. ;; there already.
  21027. ((/= (point) beg) (goto-char beg))
  21028. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21029. ((org-before-first-heading-p) (goto-char (point-min)))
  21030. (t (org-back-to-heading)))))))
  21031. (defun org-up-element ()
  21032. "Move to upper element."
  21033. (interactive)
  21034. (if (org-with-limited-levels (org-at-heading-p))
  21035. (unless (org-up-heading-safe) (error "No surrounding element"))
  21036. (let* ((elem (org-element-at-point))
  21037. (parent (org-element-property :parent elem)))
  21038. (if parent (goto-char (org-element-property :begin parent))
  21039. (if (org-with-limited-levels (org-before-first-heading-p))
  21040. (error "No surrounding element")
  21041. (org-with-limited-levels (org-back-to-heading)))))))
  21042. (defvar org-element-greater-elements)
  21043. (defun org-down-element ()
  21044. "Move to inner element."
  21045. (interactive)
  21046. (let ((element (org-element-at-point)))
  21047. (cond
  21048. ((memq (org-element-type element) '(plain-list table))
  21049. (goto-char (org-element-property :contents-begin element))
  21050. (forward-char))
  21051. ((memq (org-element-type element) org-element-greater-elements)
  21052. ;; If contents are hidden, first disclose them.
  21053. (when (org-element-property :hiddenp element) (org-cycle))
  21054. (goto-char (or (org-element-property :contents-begin element)
  21055. (error "No content for this element"))))
  21056. (t (error "No inner element")))))
  21057. (defun org-drag-element-backward ()
  21058. "Move backward element at point."
  21059. (interactive)
  21060. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21061. (let* ((trail (org-element-at-point 'keep-trail))
  21062. (elem (car trail))
  21063. (prev-elem (nth 1 trail)))
  21064. ;; Error out if no previous element or previous element is
  21065. ;; a parent of the current one.
  21066. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21067. (error "Cannot drag element backward")
  21068. (let ((pos (point)))
  21069. (org-element-swap-A-B prev-elem elem)
  21070. (goto-char (+ (org-element-property :begin prev-elem)
  21071. (- pos (org-element-property :begin elem)))))))))
  21072. (defun org-drag-element-forward ()
  21073. "Move forward element at point."
  21074. (interactive)
  21075. (let* ((pos (point))
  21076. (elem (org-element-at-point)))
  21077. (when (= (point-max) (org-element-property :end elem))
  21078. (error "Cannot drag element forward"))
  21079. (goto-char (org-element-property :end elem))
  21080. (let ((next-elem (org-element-at-point)))
  21081. (when (or (org-element-nested-p elem next-elem)
  21082. (and (eq (org-element-type next-elem) 'headline)
  21083. (not (eq (org-element-type elem) 'headline))))
  21084. (goto-char pos)
  21085. (error "Cannot drag element forward"))
  21086. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21087. ;; body's length (without final blanks) and by the length of
  21088. ;; blanks between ELEM and NEXT-ELEM.
  21089. (let ((size-next (- (save-excursion
  21090. (goto-char (org-element-property :end next-elem))
  21091. (skip-chars-backward " \r\t\n")
  21092. (forward-line)
  21093. ;; Small correction if buffer doesn't end
  21094. ;; with a newline character.
  21095. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21096. (org-element-property :begin next-elem)))
  21097. (size-blank (- (org-element-property :end elem)
  21098. (save-excursion
  21099. (goto-char (org-element-property :end elem))
  21100. (skip-chars-backward " \r\t\n")
  21101. (forward-line)
  21102. (point)))))
  21103. (org-element-swap-A-B elem next-elem)
  21104. (goto-char (+ pos size-next size-blank))))))
  21105. (defun org-drag-line-forward (arg)
  21106. "Drag the line at point ARG lines forward."
  21107. (interactive "p")
  21108. (dotimes (n (abs arg))
  21109. (let ((c (current-column)))
  21110. (if (< 0 arg)
  21111. (progn
  21112. (beginning-of-line 2)
  21113. (transpose-lines 1)
  21114. (beginning-of-line 0))
  21115. (transpose-lines 1)
  21116. (beginning-of-line -1))
  21117. (org-move-to-column c))))
  21118. (defun org-drag-line-backward (arg)
  21119. "Drag the line at point ARG lines backward."
  21120. (interactive "p")
  21121. (org-drag-line-forward (- arg)))
  21122. (defun org-mark-element ()
  21123. "Put point at beginning of this element, mark at end.
  21124. Interactively, if this command is repeated or (in Transient Mark
  21125. mode) if the mark is active, it marks the next element after the
  21126. ones already marked."
  21127. (interactive)
  21128. (let (deactivate-mark)
  21129. (if (and (org-called-interactively-p 'any)
  21130. (or (and (eq last-command this-command) (mark t))
  21131. (and transient-mark-mode mark-active)))
  21132. (set-mark
  21133. (save-excursion
  21134. (goto-char (mark))
  21135. (goto-char (org-element-property :end (org-element-at-point)))))
  21136. (let ((element (org-element-at-point)))
  21137. (end-of-line)
  21138. (push-mark (org-element-property :end element) t t)
  21139. (goto-char (org-element-property :begin element))))))
  21140. (defun org-narrow-to-element ()
  21141. "Narrow buffer to current element."
  21142. (interactive)
  21143. (let ((elem (org-element-at-point)))
  21144. (cond
  21145. ((eq (car elem) 'headline)
  21146. (narrow-to-region
  21147. (org-element-property :begin elem)
  21148. (org-element-property :end elem)))
  21149. ((memq (car elem) org-element-greater-elements)
  21150. (narrow-to-region
  21151. (org-element-property :contents-begin elem)
  21152. (org-element-property :contents-end elem)))
  21153. (t
  21154. (narrow-to-region
  21155. (org-element-property :begin elem)
  21156. (org-element-property :end elem))))))
  21157. (defun org-transpose-element ()
  21158. "Transpose current and previous elements, keeping blank lines between.
  21159. Point is moved after both elements."
  21160. (interactive)
  21161. (org-skip-whitespace)
  21162. (let ((end (org-element-property :end (org-element-at-point))))
  21163. (org-drag-element-backward)
  21164. (goto-char end)))
  21165. (defun org-unindent-buffer ()
  21166. "Un-indent the visible part of the buffer.
  21167. Relative indentation (between items, inside blocks, etc.) isn't
  21168. modified."
  21169. (interactive)
  21170. (unless (eq major-mode 'org-mode)
  21171. (error "Cannot un-indent a buffer not in Org mode"))
  21172. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21173. unindent-tree ; For byte-compiler.
  21174. (unindent-tree
  21175. (function
  21176. (lambda (contents)
  21177. (mapc
  21178. (lambda (element)
  21179. (if (memq (org-element-type element) '(headline section))
  21180. (funcall unindent-tree (org-element-contents element))
  21181. (save-excursion
  21182. (save-restriction
  21183. (narrow-to-region
  21184. (org-element-property :begin element)
  21185. (org-element-property :end element))
  21186. (org-do-remove-indentation)))))
  21187. (reverse contents))))))
  21188. (funcall unindent-tree (org-element-contents parse-tree))))
  21189. (defun org-show-subtree ()
  21190. "Show everything after this heading at deeper levels."
  21191. (interactive)
  21192. (outline-flag-region
  21193. (point)
  21194. (save-excursion
  21195. (org-end-of-subtree t t))
  21196. nil))
  21197. (defun org-show-entry ()
  21198. "Show the body directly following this heading.
  21199. Show the heading too, if it is currently invisible."
  21200. (interactive)
  21201. (save-excursion
  21202. (condition-case nil
  21203. (progn
  21204. (org-back-to-heading t)
  21205. (outline-flag-region
  21206. (max (point-min) (1- (point)))
  21207. (save-excursion
  21208. (if (re-search-forward
  21209. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21210. (match-beginning 1)
  21211. (point-max)))
  21212. nil)
  21213. (org-cycle-hide-drawers 'children))
  21214. (error nil))))
  21215. (defun org-make-options-regexp (kwds &optional extra)
  21216. "Make a regular expression for keyword lines."
  21217. (concat
  21218. "^#\\+\\("
  21219. (mapconcat 'regexp-quote kwds "\\|")
  21220. (if extra (concat "\\|" extra))
  21221. "\\):[ \t]*\\(.*\\)"))
  21222. ;; Make isearch reveal the necessary context
  21223. (defun org-isearch-end ()
  21224. "Reveal context after isearch exits."
  21225. (when isearch-success ; only if search was successful
  21226. (if (featurep 'xemacs)
  21227. ;; Under XEmacs, the hook is run in the correct place,
  21228. ;; we directly show the context.
  21229. (org-show-context 'isearch)
  21230. ;; In Emacs the hook runs *before* restoring the overlays.
  21231. ;; So we have to use a one-time post-command-hook to do this.
  21232. ;; (Emacs 22 has a special variable, see function `org-mode')
  21233. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21234. isearch-mode-end-hook-quit)
  21235. ;; Only when the isearch was not quitted.
  21236. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21237. 'append 'local)))
  21238. (org-fix-ellipsis-at-bol)))
  21239. (defun org-isearch-post-command ()
  21240. "Remove self from hook, and show context."
  21241. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21242. (org-show-context 'isearch))
  21243. ;;;; Integration with and fixes for other packages
  21244. ;;; Imenu support
  21245. (defvar org-imenu-markers nil
  21246. "All markers currently used by Imenu.")
  21247. (make-variable-buffer-local 'org-imenu-markers)
  21248. (defun org-imenu-new-marker (&optional pos)
  21249. "Return a new marker for use by Imenu, and remember the marker."
  21250. (let ((m (make-marker)))
  21251. (move-marker m (or pos (point)))
  21252. (push m org-imenu-markers)
  21253. m))
  21254. (defun org-imenu-get-tree ()
  21255. "Produce the index for Imenu."
  21256. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21257. (setq org-imenu-markers nil)
  21258. (let* ((n org-imenu-depth)
  21259. (re (concat "^" (org-get-limited-outline-regexp)))
  21260. (subs (make-vector (1+ n) nil))
  21261. (last-level 0)
  21262. m level head0 head)
  21263. (save-excursion
  21264. (save-restriction
  21265. (widen)
  21266. (goto-char (point-max))
  21267. (while (re-search-backward re nil t)
  21268. (setq level (org-reduced-level (funcall outline-level)))
  21269. (when (and (<= level n)
  21270. (looking-at org-complex-heading-regexp)
  21271. (setq head0 (org-match-string-no-properties 4)))
  21272. (setq head (org-link-display-format head0)
  21273. m (org-imenu-new-marker))
  21274. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21275. (if (>= level last-level)
  21276. (push (cons head m) (aref subs level))
  21277. (push (cons head (aref subs (1+ level))) (aref subs level))
  21278. (loop for i from (1+ level) to n do (aset subs i nil)))
  21279. (setq last-level level)))))
  21280. (aref subs 1)))
  21281. (eval-after-load "imenu"
  21282. '(progn
  21283. (add-hook 'imenu-after-jump-hook
  21284. (lambda ()
  21285. (if (derived-mode-p 'org-mode)
  21286. (org-show-context 'org-goto))))))
  21287. (defun org-link-display-format (link)
  21288. "Replace a link with either the description, or the link target
  21289. if no description is present"
  21290. (save-match-data
  21291. (if (string-match org-bracket-link-analytic-regexp link)
  21292. (replace-match (if (match-end 5)
  21293. (match-string 5 link)
  21294. (concat (match-string 1 link)
  21295. (match-string 3 link)))
  21296. nil t link)
  21297. link)))
  21298. (defun org-toggle-link-display ()
  21299. "Toggle the literal or descriptive display of links."
  21300. (interactive)
  21301. (if org-descriptive-links
  21302. (progn (org-remove-from-invisibility-spec '(org-link))
  21303. (org-restart-font-lock)
  21304. (setq org-descriptive-links nil))
  21305. (progn (add-to-invisibility-spec '(org-link))
  21306. (org-restart-font-lock)
  21307. (setq org-descriptive-links t))))
  21308. ;; Speedbar support
  21309. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21310. "Overlay marking the agenda restriction line in speedbar.")
  21311. (overlay-put org-speedbar-restriction-lock-overlay
  21312. 'face 'org-agenda-restriction-lock)
  21313. (overlay-put org-speedbar-restriction-lock-overlay
  21314. 'help-echo "Agendas are currently limited to this item.")
  21315. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21316. (defun org-speedbar-set-agenda-restriction ()
  21317. "Restrict future agenda commands to the location at point in speedbar.
  21318. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21319. (interactive)
  21320. (require 'org-agenda)
  21321. (let (p m tp np dir txt)
  21322. (cond
  21323. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21324. 'org-imenu t))
  21325. (setq m (get-text-property p 'org-imenu-marker))
  21326. (with-current-buffer (marker-buffer m)
  21327. (goto-char m)
  21328. (org-agenda-set-restriction-lock 'subtree)))
  21329. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21330. 'speedbar-function 'speedbar-find-file))
  21331. (setq tp (previous-single-property-change
  21332. (1+ p) 'speedbar-function)
  21333. np (next-single-property-change
  21334. tp 'speedbar-function)
  21335. dir (speedbar-line-directory)
  21336. txt (buffer-substring-no-properties (or tp (point-min))
  21337. (or np (point-max))))
  21338. (with-current-buffer (find-file-noselect
  21339. (let ((default-directory dir))
  21340. (expand-file-name txt)))
  21341. (unless (derived-mode-p 'org-mode)
  21342. (error "Cannot restrict to non-Org-mode file"))
  21343. (org-agenda-set-restriction-lock 'file)))
  21344. (t (error "Don't know how to restrict Org-mode's agenda")))
  21345. (move-overlay org-speedbar-restriction-lock-overlay
  21346. (point-at-bol) (point-at-eol))
  21347. (setq current-prefix-arg nil)
  21348. (org-agenda-maybe-redo)))
  21349. (eval-after-load "speedbar"
  21350. '(progn
  21351. (speedbar-add-supported-extension ".org")
  21352. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21353. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21354. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21355. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21356. (add-hook 'speedbar-visiting-tag-hook
  21357. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21358. ;;; Fixes and Hacks for problems with other packages
  21359. ;; Make flyspell not check words in links, to not mess up our keymap
  21360. (defvar org-element-affiliated-keywords) ; From org-element.el
  21361. (defvar org-element-block-name-alist) ; From org-element.el
  21362. (defun org-mode-flyspell-verify ()
  21363. "Don't let flyspell put overlays at active buttons, or on
  21364. {todo,all-time,additional-option-like}-keywords."
  21365. (let ((pos (max (1- (point)) (point-min)))
  21366. (word (thing-at-point 'word)))
  21367. (and (not (get-text-property pos 'keymap))
  21368. (not (get-text-property pos 'org-no-flyspell))
  21369. (not (member word org-todo-keywords-1))
  21370. (not (member word org-all-time-keywords))
  21371. (not (member word org-options-keywords))
  21372. (not (member word (mapcar 'car org-startup-options)))
  21373. (not (member-ignore-case word org-element-affiliated-keywords))
  21374. (not (member-ignore-case word (org-get-export-keywords)))
  21375. (not (member-ignore-case
  21376. word (mapcar 'car org-element-block-name-alist)))
  21377. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21378. (defun org-remove-flyspell-overlays-in (beg end)
  21379. "Remove flyspell overlays in region."
  21380. (and (org-bound-and-true-p flyspell-mode)
  21381. (fboundp 'flyspell-delete-region-overlays)
  21382. (flyspell-delete-region-overlays beg end))
  21383. (add-text-properties beg end '(org-no-flyspell t)))
  21384. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21385. (eval-after-load "bookmark"
  21386. '(if (boundp 'bookmark-after-jump-hook)
  21387. ;; We can use the hook
  21388. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21389. ;; Hook not available, use advice
  21390. (defadvice bookmark-jump (after org-make-visible activate)
  21391. "Make the position visible."
  21392. (org-bookmark-jump-unhide))))
  21393. ;; Make sure saveplace shows the location if it was hidden
  21394. (eval-after-load "saveplace"
  21395. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21396. "Make the position visible."
  21397. (org-bookmark-jump-unhide)))
  21398. ;; Make sure ecb shows the location if it was hidden
  21399. (eval-after-load "ecb"
  21400. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21401. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21402. (if (derived-mode-p 'org-mode)
  21403. (org-show-context))))
  21404. (defun org-bookmark-jump-unhide ()
  21405. "Unhide the current position, to show the bookmark location."
  21406. (and (derived-mode-p 'org-mode)
  21407. (or (outline-invisible-p)
  21408. (save-excursion (goto-char (max (point-min) (1- (point))))
  21409. (outline-invisible-p)))
  21410. (org-show-context 'bookmark-jump)))
  21411. ;; Make session.el ignore our circular variable
  21412. (eval-after-load "session"
  21413. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21414. ;;;; Experimental code
  21415. (defun org-closed-in-range ()
  21416. "Sparse tree of items closed in a certain time range.
  21417. Still experimental, may disappear in the future."
  21418. (interactive)
  21419. ;; Get the time interval from the user.
  21420. (let* ((time1 (org-float-time
  21421. (org-read-date nil 'to-time nil "Starting date: ")))
  21422. (time2 (org-float-time
  21423. (org-read-date nil 'to-time nil "End date:")))
  21424. ;; callback function
  21425. (callback (lambda ()
  21426. (let ((time
  21427. (org-float-time
  21428. (apply 'encode-time
  21429. (org-parse-time-string
  21430. (match-string 1))))))
  21431. ;; check if time in interval
  21432. (and (>= time time1) (<= time time2))))))
  21433. ;; make tree, check each match with the callback
  21434. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21435. ;;;; Finish up
  21436. (provide 'org)
  21437. (run-hooks 'org-load-hook)
  21438. ;;; org.el ends here